1 /*
2 * Copyright (c) 2021, 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     encode_jpeg_packer_feature.h
24 //! \brief    Defines packing logic for jpeg encode
25 //!
26 #ifndef __ENCODE_JPEG_PACKER_FEATURE_H__
27 #define __ENCODE_JPEG_PACKER_FEATURE_H__
28 
29 #include "media_feature.h"
30 #include "encode_jpeg_basic_feature.h"
31 
32 namespace encode
33 {
34 
35 //!
36 //! \struct EncodeJpegQuantHeader
37 //! \brief Define JPEG Quant Header structure
38 //!
39 struct EncodeJpegQuantHeader
40 {
41     uint16_t m_dqt;                           //!< Define Quantization Marker
42     uint16_t m_lq;                            //!< Quantization table definition length
43     uint8_t  m_tablePrecisionAndDestination;  //!< 4 bits of element precision and 4 bits of destination selector
44     uint8_t  m_qk[JPEG_NUM_QUANTMATRIX];      //!< Quantization table elements
45 };
46 
47 #pragma pack(push, 1)
48 //!
49 //! \struct CodechalEncodeJpegFrameHeader
50 //! \brief Define JPEG Frame Header structure
51 //!
52 struct EncodeJpegFrameHeader
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 EncodeJpegHuffmanHeader
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 CodechalEncodeJpegRestartHeader
84 //! \brief Define JPEG Restart Header structure
85 //!
86 struct EncodeJpegRestartHeader
87 {
88     uint16_t m_dri;  //!< Define restart interval marker
89     uint16_t m_lr;   //!< Restart interval segment length
90     uint16_t m_ri;   //!< Restart interval
91 };
92 
93 //!
94 //! \struct CodechalEncodeJpegScanComponent
95 //! \brief JPEG Scan Component structure
96 //!
97 struct EncodeJpegScanComponent
98 {
99     uint8_t m_csj;   //!< Scan component selector
100     uint8_t m_tdaj;  //!< 4 bits of DC huffman table destination selector and 4 bits of AC huffman table selector
101 };
102 
103 //!
104 //! \struct CodechalEncodeJpegScanHeader
105 //! \brief JPEG Scan Header structure
106 //!
107 struct EncodeJpegScanHeader
108 {
109     uint16_t                m_sos;  //!< Start of scan marker
110     uint16_t                m_ls;   //!< Scan header length
111     uint8_t                 m_ns;   //!< Number of image components in scan
112     EncodeJpegScanComponent m_scanComponent[jpegNumComponent];
113     uint8_t                 m_ss;   //!< start of spectral selection
114     uint8_t                 m_se;   //!< end of spectral selection
115     uint8_t                 m_ahl;  //!< successive approximation bit position high and low
116 };
117 #pragma pack(pop)
118 
119 
120 class JpegPackerFeature : public MediaFeature
121 {
122 public:
123 
124     //!
125     //! \brief  AvcEncodeHeaderPacker constructor
126     //!
127     JpegPackerFeature(MediaFeatureManager *featureManager,
128         EncodeAllocator *                  allocator,
129         CodechalHwInterfaceNext *              hwInterface,
130         void *                             constSettings);
131 
132     //!
133     //! \brief  AvcEncodeHeaderPacker deconstructor
134     //!
~JpegPackerFeature()135     virtual ~JpegPackerFeature() {}
136 
137     //!
138     //! \brief    Pack SOI
139     //!
140     //! \param    [out] buffer
141     //!           Bitstream buffer
142     //!
143     //! \return   MOS_STATUS
144     //!           MOS_STATUS_SUCCESS if success, else fail reason
145     //!
146     MOS_STATUS PackSOI(BSBuffer *buffer);
147 
148     //!
149     //! \brief    Pack Application Data
150     //!
151     //! \param    [out] buffer
152     //!           Bitstream buffer
153     //! \param    [in] appDataChunk
154     //!           Application Data Chunk
155     //! \param    [in] size
156     //!           Application Data Size
157     //!
158     //! \return   MOS_STATUS
159     //!           MOS_STATUS_SUCCESS if success, else fail reason
160     //!
161     MOS_STATUS PackApplicationData(
162         BSBuffer *buffer,
163         uint8_t * appDataChunk,
164         uint32_t  size);
165 
166     //!
167     //! \brief    Pack Quant Table
168     //!
169     //! \param    [out] buffer
170     //!           Bitstream buffer
171     //! \param    [in] componentType
172     //!           The Component Type
173     //!
174     //! \return   MOS_STATUS
175     //!           MOS_STATUS_SUCCESS if success, else fail reason
176     //!
177     MOS_STATUS PackQuantTable(
178         BSBuffer *          buffer,
179         CodecJpegComponents componentType);
180 
181     //!
182     //! \brief    Pack Frame Header
183     //!
184     //! \param    [out] buffer
185     //!           Bitstream buffer
186     //! \param    [in] useSingleDefaultQuantTable
187     //!           The flag of using single default Quant Table
188     //!
189     //! \return   MOS_STATUS
190     //!           MOS_STATUS_SUCCESS if success, else fail reason
191     //!
192     MOS_STATUS PackFrameHeader(
193         BSBuffer *buffer,
194         bool      useSingleDefaultQuantTable);
195 
196     //!
197     //! \brief    Pack Huffman Table
198     //!
199     //! \param    [out] buffer
200     //!           Bitstream buffer
201     //! \param    [in] tableIndex
202     //!           The Huffman Table Index
203     //!
204     //! \return   MOS_STATUS
205     //!           MOS_STATUS_SUCCESS if success, else fail reason
206     //!
207     MOS_STATUS PackHuffmanTable(
208         BSBuffer *buffer,
209         uint32_t  tableIndex);
210 
211     //!
212     //! \brief    Pack Restart Interval
213     //!
214     //! \param    [out] buffer
215     //!           Bitstream buffer
216     //!
217     //! \return   MOS_STATUS
218     //!           MOS_STATUS_SUCCESS if success, else fail reason
219     //!
220     MOS_STATUS PackRestartInterval(
221         BSBuffer *buffer);
222 
223     //!
224     //! \brief    Pack Scan Header
225     //!
226     //! \param    [out] buffer
227     //!           Bitstream buffer
228     //!
229     //! \return   MOS_STATUS
230     //!           MOS_STATUS_SUCCESS if success, else fail reason
231     //!
232     MOS_STATUS PackScanHeader(
233         BSBuffer *buffer);
234 
235 protected:
236 
237     static const uint32_t m_jpegEncodeSoi = 0xFFD8;  //!< JPEG Encode Header Markers SOI
238     static const uint32_t m_jpegEncodeSos = 0xFFDA;  //!< JPEG Encode Header Markers SOS
239 
240 MEDIA_CLASS_DEFINE_END(encode__JpegPackerFeature)
241 };
242 
243 }  // namespace encode
244 
245 #endif  // !__ENCODE_JPEG_PACKER_FEATURE_H__
246