xref: /aosp_15_r20/external/intel-media-driver/media_driver/linux/common/codec/ddi/media_ddi_encode_jpeg.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 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     media_ddi_encode_jpeg.h
24 //! \brief    Defines class for DDI media jpeg encode
25 //!
26 
27 #ifndef __MEDIA_LIBVA_ENCODER_JPEG_H__
28 #define __MEDIA_LIBVA_ENCODER_JPEG_H__
29 
30 #include "media_ddi_encode_base.h"
31 
32 static const int32_t maxNumQuantTableIndex = 3;
33 static const int32_t quantMatrixSize       = 64;
34 static const int32_t maxNumHuffTables      = 2;
35 
36 static const uint32_t defaultLumaQuant[64] =  //!< Default Quantization Matrix for luma component
37 {                                             //!< of JPEG Encode in zig zag scan order (from JPEG Spec, Table K.1)
38     16, 11, 12, 14, 12, 10, 16, 14,
39     13, 14, 18, 17, 16, 19, 24, 40,
40     26, 24, 22, 22, 24, 49, 35, 37,
41     29, 40, 58, 51, 61, 60, 57, 51,
42     56, 55, 64, 72, 92, 78, 64, 68,
43     87, 69, 55, 56, 80, 109, 81, 87,
44     95, 98, 103, 104, 103, 62, 77, 113,
45     121, 112, 100, 120, 92, 101, 103, 99
46 };
47 
48 static const uint32_t defaultChromaQuant[64] =  //!< Default Quantization Matrix for chroma component
49 {                                               //!< of JPEG Encode in zig zag scan order (from JPEG Spec, Table K.2)
50     17, 18, 18, 24, 21, 24, 47, 26,
51     26, 47, 99, 66, 56, 66, 99, 99,
52     99, 99, 99, 99, 99, 99, 99, 99,
53     99, 99, 99, 99, 99, 99, 99, 99,
54     99, 99, 99, 99, 99, 99, 99, 99,
55     99, 99, 99, 99, 99, 99, 99, 99,
56     99, 99, 99, 99, 99, 99, 99, 99,
57     99, 99, 99, 99, 99, 99, 99, 99
58 };
59 
60 //!
61 //! \enum   DDI_ENCODE_JPEG_INPUTSURFACEFORMATS
62 //! \brief  Ddi encode JPEG input surface formats
63 //!
64 enum DDI_ENCODE_JPEG_INPUTSURFACEFORMATS  //!< Jpeg input surface formats.
65 {
66     DDI_ENCODE_JPEG_INPUTFORMAT_RESERVED = 0,
67     DDI_ENCODE_JPEG_INPUTFORMAT_NV12     = 1,
68     DDI_ENCODE_JPEG_INPUTFORMAT_UYVY     = 2,
69     DDI_ENCODE_JPEG_INPUTFORMAT_YUY2     = 3,
70     DDI_ENCODE_JPEG_INPUTFORMAT_Y8       = 4,
71     DDI_ENCODE_JPEG_INPUTFORMAT_RGB      = 5
72 };
73 
74 //!
75 //! \class  DdiEncodeJpeg
76 //! \brief  Ddi encode JPEG
77 //!
78 class DdiEncodeJpeg : public DdiEncodeBase
79 {
80 public:
81     //!
82     //! \brief    Constructor
83     //!
DdiEncodeJpeg()84     DdiEncodeJpeg(){};
85 
86     //!
87     //! \brief    Destructor
88     //!
89     ~DdiEncodeJpeg();
90 
91     //!
92     //! \brief    Initialize Encode Context and CodecHal Setting for Jpeg
93     //!
94     //! \param    [out] codecHalSettings
95     //!           Pointer to CodechalSetting *
96     //!
97     //! \return   VAStatus
98     //!           VA_STATUS_SUCCESS if success, else fail reason
99     //!
100     VAStatus ContextInitialize(
101         CodechalSetting *codecHalSettings) override;
102 
103     //!
104     //! \brief    Parse buffer to the server.
105     //!
106     //! \param    [in] ctx
107     //!           Pointer to VADriverContextP
108     //! \param    [in] context
109     //!           VA context ID
110     //! \param    [in] buffers
111     //!           Pointer to VABufferID
112     //! \param    [in] numBuffers
113     //!           Number of buffers
114     //!
115     //! \return   VAStatus
116     //!           VA_STATUS_SUCCESS if success, else fail reason
117     //!
118     VAStatus RenderPicture(
119         VADriverContextP ctx,
120         VAContextID      context,
121         VABufferID       *buffers,
122         int32_t          numBuffers) override;
123 
124 protected:
125     //!
126     //! \brief    Reset Encode Context At Frame Level
127     //!
128     //! \return   VAStatus
129     //!           VA_STATUS_SUCCESS if success, else fail reason
130     //!
131     VAStatus ResetAtFrameLevel() override;
132 
133     //!
134     //! \brief    Encode in CodecHal for Jpeg
135     //!
136     //! \param    [in] numSlices
137     //!           Number of slice data structures
138     //!
139     //! \return   VAStatus
140     //!           VA_STATUS_SUCCESS if success, else fail reason
141     //!
142     VAStatus EncodeInCodecHal(
143         uint32_t numSlices) override;
144 
145     //!
146     //! \brief    Parse Picture Parameter buffer to Encode Context
147     //!
148     //! \param    [in] mediaCtx
149     //!           Pointer to DDI_MEDIA_CONTEXT
150     //! \param    [in] ptr
151     //!           Pointer to Picture Parameter buffer
152     //!
153     //! \return   VAStatus
154     //!           VA_STATUS_SUCCESS if success, else fail reason
155     //!
156     VAStatus ParsePicParams(
157         DDI_MEDIA_CONTEXT *mediaCtx,
158         void              *ptr) override;
159 
160     uint32_t getSliceParameterBufferSize() override;
161 
162     uint32_t getPictureParameterBufferSize() override;
163 
164     uint32_t getQMatrixBufferSize() override;
165 
166     //!
167     //! \brief    Parse QMatrix buffer to Encode Context
168     //!
169     //! \param    [in] ptr
170     //!           Pointer to QMatrix buffer
171     //!
172     //! \return   VAStatus
173     //!           VA_STATUS_SUCCESS if success, else fail reason
174     //!
175     VAStatus Qmatrix(
176         void *ptr);
177 
178     //!
179     //! \brief    Parse Slice Parameter buffer to Encode Context
180     //!
181     //! \param    [in] mediaCtx
182     //!           Pointer to DDI_MEDIA_CONTEXT
183     //! \param    [in] ptr
184     //!           Pointer to Slice Parameter buffer
185     //! \param    [in] numSlices
186     //!           Number of slice
187     //!
188     //! \return   VAStatus
189     //!           VA_STATUS_SUCCESS if success, else fail reason
190     //!
191     VAStatus ParseSlcParams(
192         DDI_MEDIA_CONTEXT *mediaCtx,
193         void              *ptr,
194         uint32_t          numSlices);
195 
196     //!
197     //! \brief    Parse Huffman Parameter buffer to Encode Context
198     //!
199     //! \param    [in] ptr
200     //!           Pointer to Huffman Parameter buffer
201     //!
202     //! \return   VAStatus
203     //!           VA_STATUS_SUCCESS if success, else fail reason
204     //!
205     VAStatus ParseHuffmanParams(void *ptr);
206 
207     //!
208     //! \brief    Parse Application Data buffer to Encode Context
209     //!
210     //! \param    [in] ptr
211     //!           Pointer to Application Data buffer
212     //! \param    [in] size
213     //!           Size of Application Data buffer
214     //!
215     //! \return   VAStatus
216     //!           VA_STATUS_SUCCESS if success, else fail reason
217     //!
218     VAStatus ParseAppData(
219         void    *ptr,
220         int32_t size);
221 
222 private:
223     //!
224     //! \brief    Parse QMatrix buffer to Encode Context,
225     //!           if quant table is not supplied by application
226     //!
227     //! \return   VAStatus
228     //!           VA_STATUS_SUCCESS if success, else fail reason
229     //!
230     VAStatus DefaultQmatrix();
231     //!
232     //! \brief    Application send whole header and qmatrix
233     //!           must be extracted from it
234     //!
235     //! \return   VAStatus
236     //!           VA_STATUS_SUCCESS if success, else fail reason
237     //!
238     VAStatus QmatrixFromHeader();
239     //!
240     //! \brief    Convert Media Format To Input Surface Format
241     //!
242     //! \param    [in] format
243     //!           Media format
244     //!
245     //! \return   uint32_t
246     //!           Input surface format
247     //!
248     uint32_t ConvertMediaFormatToInputSurfaceFormat(DDI_MEDIA_FORMAT format);
249 
250     CodecEncodeJpegHuffmanDataArray    *m_huffmanTable = nullptr;    //!< Huffman table.
251     void                               *m_appData      = nullptr;    //!< Application data.
252     bool                               m_quantSupplied = false;      //!< whether Quant table is supplied by the app for JPEG encoder.
253     uint32_t                           m_appDataTotalSize   = 0;          //!< Total size of application data.
254     uint32_t                           m_appDataSize   = 0;          //!< Size of application data.
255     uint32_t                           m_appDataWholeHeader = false; //!< whether the app data include whole headers , such as SOI, DQT ...
256 };
257 #endif /* __MEDIA_LIBVA_ENCODER_JPEG_H__ */
258