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