1 /* 2 * Copyright (c) 2012-2017, Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included 12 * in all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 //! 23 //! \file codechal_encode_jpeg.h 24 //! \brief Defines the encode interface extension for JPEG. 25 //! \details Defines all types, macros, and functions required by CodecHal for JPEG encoding. Definitions are not externally facing. 26 //! 27 28 #ifndef __CODECHAL_ENCODER_JPEG_H__ 29 #define __CODECHAL_ENCODER_JPEG_H__ 30 31 #include "codechal_encoder_base.h" 32 33 //! 34 //! \struct CodechalEncodeJpegHuffTable 35 //! \brief Define the Huffman Table structure used by JPEG Encode 36 //! 37 struct CodechalEncodeJpegHuffTable 38 { 39 uint32_t m_tableClass; //!< table class 40 uint32_t m_tableID; //!< table ID 41 //This is the max size possible for these arrays, for DC table the actual occupied bits will be lesser 42 // For AC table we need one extra byte to store 00, denoting end of huffman values 43 uint8_t m_huffSize[JPEG_NUM_HUFF_TABLE_AC_HUFFVAL + 1]; //!< Huffman size, occupies 1 byte in HW command 44 uint16_t m_huffCode[JPEG_NUM_HUFF_TABLE_AC_HUFFVAL + 1]; //!< Huffman code, occupies 2 bytes in HW command 45 }; 46 47 #pragma pack(push,1) 48 //! 49 //! \struct CodechalEncodeJpegFrameHeader 50 //! \brief Define JPEG Frame Header structure 51 //! 52 struct CodechalEncodeJpegFrameHeader 53 { 54 uint16_t m_sof; //!< Start of frame marker 55 uint16_t m_lf; //!< Frame header length 56 uint8_t m_p; //!< Precision 57 uint16_t m_y; //!< max number of lines in image 58 uint16_t m_x; //!< max number of samples per line in image 59 uint8_t m_nf; //!< Number of image components in the frame 60 61 struct 62 { 63 uint8_t m_ci; //!< Component identifier 64 uint8_t m_samplingFactori; //!< 4 MSBs are the horizontal and 4 LSBs are vertical sampling factors 65 uint8_t m_tqi; //!< Quantization table selector (0-3) 66 } m_codechalJpegFrameComponent[256]; //!< JPEG frame component array 67 }; 68 69 //! 70 //! \struct CodechalJpegHuffmanHeader 71 //! \brief Define JPEG Huffman Header structure 72 //! 73 struct CodechalJpegHuffmanHeader 74 { 75 uint16_t m_dht; //!< Define Huffman Table Marker 76 uint16_t m_lh; //!< Huffman table definition length 77 uint8_t m_tableClassAndDestn; //!< 4 bits of Huffman table class (0 = DC, 1 = AC) and 4 bits of destination identifier 78 uint8_t m_li[JPEG_NUM_HUFF_TABLE_AC_BITS]; //!< List BITS of Huffman table 79 uint8_t m_vij[JPEG_NUM_HUFF_TABLE_AC_HUFFVAL]; //!< Value associated with each huffman code 80 }; 81 82 //! 83 //! \struct CodechalEncodeJpegQuantHeader 84 //! \brief Define JPEG Quant Header structure 85 //! 86 struct CodechalEncodeJpegQuantHeader 87 { 88 uint16_t m_dqt; //!< Define Quantization Marker 89 uint16_t m_lq; //!< Quantization table definition length 90 uint8_t m_tablePrecisionAndDestination; //!< 4 bits of element precision and 4 bits of destination selector 91 uint8_t m_qk[JPEG_NUM_QUANTMATRIX]; //!< Quantization table elements 92 }; 93 94 //! 95 //! \struct CodechalEncodeJpegRestartHeader 96 //! \brief Define JPEG Restart Header structure 97 //! 98 struct CodechalEncodeJpegRestartHeader 99 { 100 uint16_t m_dri; //!< Define restart interval marker 101 uint16_t m_lr; //!< Restart interval segment length 102 uint16_t m_ri; //!< Restart interval 103 }; 104 105 //! 106 //! \struct CodechalEncodeJpegScanComponent 107 //! \brief JPEG Scan Component structure 108 //! 109 struct CodechalEncodeJpegScanComponent 110 { 111 uint8_t m_csj; //!< Scan component selector 112 uint8_t m_tdaj; //!< 4 bits of DC huffman table destination selector and 4 bits of AC huffman table selector 113 }; 114 115 //! 116 //! \struct CodechalEncodeJpegScanHeader 117 //! \brief JPEG Scan Header structure 118 //! 119 struct CodechalEncodeJpegScanHeader 120 { 121 uint16_t m_sos; //!< Start of scan marker 122 uint16_t m_ls; //!< Scan header length 123 uint8_t m_ns; //!< Number of image components in scan 124 CodechalEncodeJpegScanComponent m_scanComponent[jpegNumComponent]; 125 uint8_t m_ss; //!< start of spectral selection 126 uint8_t m_se; //!< end of spectral selection 127 uint8_t m_ahl; //!< successive approximation bit position high and low 128 }; 129 #pragma pack(pop) 130 131 //! JPEG Encoder State class 132 //! 133 //!This class defines the JPEG encoder state, it includes 134 //!common member fields, functions, interfaces etc for all Gens. 135 //! 136 //!To create a JPEG encoder instance, client needs to call new(std::nothrow) CodechalEncodeJpegState(pEncoder) 137 //! 138 class CodechalEncodeJpegState : public CodechalEncoderState 139 { 140 public: 141 //! 142 //! \brief Constructor 143 //! 144 CodechalEncodeJpegState( 145 CodechalHwInterface* hwInterface, 146 CodechalDebugInterface* debugInterface, 147 PCODECHAL_STANDARD_INFO standardInfo); 148 149 //! 150 //! \brief Destructor 151 //! ~CodechalEncodeJpegState()152 virtual ~CodechalEncodeJpegState() {}; 153 154 //derived from base class 155 MOS_STATUS Initialize(CodechalSetting *settings) override; 156 157 MOS_STATUS AllocateResources() override; 158 159 void FreeResources() override; 160 161 MOS_STATUS InitializePicture(const EncoderParams& params) override; 162 163 virtual MOS_STATUS CheckResChangeAndCsc() override; 164 165 MOS_STATUS ExecutePictureLevel() override; 166 167 MOS_STATUS ExecuteSliceLevel() override; 168 169 uint32_t CalculateCommandBufferSize() override; 170 171 virtual MOS_STATUS GetStatusReport( 172 EncodeStatus* encodeStatus, 173 EncodeStatusReport* encodeStatusReport) override; 174 ExecuteKernelFunctions()175 MOS_STATUS ExecuteKernelFunctions() override { return MOS_STATUS_SUCCESS;}; 176 177 #if USE_CODECHAL_DEBUG_TOOL 178 MOS_STATUS DumpQuantTables( 179 CodecEncodeJpegQuantTable *quantTable); 180 181 MOS_STATUS DumpPicParams( 182 CodecEncodeJpegPictureParams *picParams); 183 184 MOS_STATUS DumpScanParams( 185 CodecEncodeJpegScanHeader *scanParams); 186 187 MOS_STATUS DumpHuffmanTable( 188 CodecEncodeJpegHuffmanDataArray *huffmanTable); 189 #endif 190 191 // Variables 192 static const uint32_t m_jpegEncodeSoi = 0xFFD8; //!< JPEG Encode Header Markers SOI 193 static const uint32_t m_jpegEncodeSos = 0xFFDA; //!< JPEG Encode Header Markers SOS 194 195 // Parameters passed by application 196 CodecEncodeJpegPictureParams *m_jpegPicParams = nullptr; //!< Pointer to picture parameter 197 CodecEncodeJpegScanHeader *m_jpegScanParams = nullptr; //!< Pointer to scan parameter 198 CodecEncodeJpegQuantTable *m_jpegQuantTables = nullptr; //!< Pointer to quant tables 199 CodecEncodeJpegHuffmanDataArray *m_jpegHuffmanTable = nullptr; //!< Pointer to Huffman table 200 void *m_applicationData = nullptr; //!< Pointer to Application data 201 CODEC_REF_LIST *m_refList[CODECHAL_NUM_UNCOMPRESSED_SURFACE_JPEG]; //!< Pointer to reference pictures, added for eStatus reporting 202 203 // Other 204 uint32_t m_appDataSize = 0; //!< Pointer to Application data size 205 bool m_jpegQuantMatrixSent = false; //!< JPEG: bool to tell if quant matrix was sent by the app or not 206 bool m_fullHeaderInAppData = false; 207 208 protected: 209 //! 210 //! \brief Map Huffman value index, implemented based on table K.5 in JPEG spec. 211 //! 212 //! \param [in] huffValIndex 213 //! Huffman Value Index 214 //! 215 //! \return The mapped index 216 //! 217 uint8_t MapHuffValIndex(uint8_t huffValIndex); 218 219 //! 220 //! \brief Generate table of Huffman code sizes, implemented based on Flowchart in figure C.1 in JPEG spec 221 //! 222 //! \param [in] bits 223 //! Contains the number of codes of each size 224 //! \param [out] huffSize 225 //! Huffman Size table 226 //! \param [out] lastK 227 //! Index of the last entry in the table 228 //! 229 //! \return MOS_STATUS 230 //! MOS_STATUS_SUCCESS if success, else fail reason 231 //! 232 MOS_STATUS GenerateSizeTable( 233 uint8_t bits[], 234 uint8_t huffSize[], 235 uint8_t& lastK); 236 237 //! 238 //! \brief Generate table of Huffman codes, implemented based on Flowchart in figure C.2 in JPEG spec 239 //! 240 //! \param [in] huffSize 241 //! Huffman Size table 242 //! \param [out] huffCode 243 //! Huffman Code table 244 //! 245 //! \return MOS_STATUS 246 //! MOS_STATUS_SUCCESS if success, else fail reason 247 //! 248 MOS_STATUS GenerateCodeTable( 249 uint8_t huffSize[], 250 uint16_t huffCode[]); 251 252 //! 253 //! \brief Generate Huffman codes in symbol value order, implemented based on Flowchart in figure C.3 in JPEG spec 254 //! 255 //! \param [in] huffVal 256 //! Huffman Value table 257 //! \param [in, out] huffSize 258 //! Huffman Size table 259 //! \param [in, out] huffCode 260 //! Huffman Code table 261 //! \param [in] lastK 262 //! Index of the last entry in the table 263 //! 264 //! \return MOS_STATUS 265 //! MOS_STATUS_SUCCESS if success, else fail reason 266 //! 267 MOS_STATUS OrderCodes( 268 uint8_t huffVal[], 269 uint8_t huffSize[], 270 uint16_t huffCode[], 271 uint8_t lastK); 272 273 //! 274 //! \brief Convert Huffman data to table, including 3 steps: Step 1 - Generate size table, Step2 - Generate code table, Step 3 - Order codes. 275 //! 276 //! \param [in] huffmanData 277 //! Huffman Data 278 //! \param [out] huffmanTable 279 //! Huffman table 280 //! 281 //! \return MOS_STATUS 282 //! MOS_STATUS_SUCCESS if success, else fail reason 283 //! 284 MOS_STATUS ConvertHuffDataToTable( 285 CodecEncodeJpegHuffData huffmanData, 286 CodechalEncodeJpegHuffTable *huffmanTable); 287 288 //! 289 //! \brief Pack SOI 290 //! 291 //! \param [out] buffer 292 //! Bitstream buffer 293 //! 294 //! \return MOS_STATUS 295 //! MOS_STATUS_SUCCESS if success, else fail reason 296 //! 297 MOS_STATUS PackSOI(BSBuffer *buffer); 298 299 //! 300 //! \brief Pack Application Data 301 //! 302 //! \param [out] buffer 303 //! Bitstream buffer 304 //! \param [in] appDataChunk 305 //! Application Data Chunk 306 //! \param [in] size 307 //! Application Data Size 308 //! 309 //! \return MOS_STATUS 310 //! MOS_STATUS_SUCCESS if success, else fail reason 311 //! 312 MOS_STATUS PackApplicationData( 313 BSBuffer *buffer, 314 uint8_t *appDataChunk, 315 uint32_t size); 316 317 //! 318 //! \brief Pack Frame Header 319 //! 320 //! \param [out] buffer 321 //! Bitstream buffer 322 //! \param [in] useSingleDefaultQuantTable 323 //! The flag of using single default Quant Table 324 //! 325 //! \return MOS_STATUS 326 //! MOS_STATUS_SUCCESS if success, else fail reason 327 //! 328 MOS_STATUS PackFrameHeader( 329 BSBuffer *buffer, 330 bool useSingleDefaultQuantTable); 331 332 //! 333 //! \brief Pack Huffman Table 334 //! 335 //! \param [out] buffer 336 //! Bitstream buffer 337 //! \param [in] tableIndex 338 //! The Huffman Table Index 339 //! 340 //! \return MOS_STATUS 341 //! MOS_STATUS_SUCCESS if success, else fail reason 342 //! 343 MOS_STATUS PackHuffmanTable( 344 BSBuffer *buffer, 345 uint32_t tableIndex); 346 347 //! 348 //! \brief Pack Quant Table 349 //! 350 //! \param [out] buffer 351 //! Bitstream buffer 352 //! \param [in] componentType 353 //! The Component Type 354 //! 355 //! \return MOS_STATUS 356 //! MOS_STATUS_SUCCESS if success, else fail reason 357 //! 358 MOS_STATUS PackQuantTable( 359 BSBuffer *buffer, 360 CodecJpegComponents componentType); 361 362 //! 363 //! \brief Pack Restart Interval 364 //! 365 //! \param [out] buffer 366 //! Bitstream buffer 367 //! 368 //! \return MOS_STATUS 369 //! MOS_STATUS_SUCCESS if success, else fail reason 370 //! 371 MOS_STATUS PackRestartInterval( 372 BSBuffer *buffer); 373 374 //! 375 //! \brief Pack Scan Header 376 //! 377 //! \param [out] buffer 378 //! Bitstream buffer 379 //! 380 //! \return MOS_STATUS 381 //! MOS_STATUS_SUCCESS if success, else fail reason 382 //! 383 MOS_STATUS PackScanHeader( 384 BSBuffer *buffer); 385 }; 386 387 #endif //__CODECHAL_ENCODER_JPEG_H__ 388