xref: /aosp_15_r20/external/intel-media-driver/media_driver/linux/common/codec/ddi/media_ddi_encode_avc.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2017-2020, 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_avc.h
24 //! \brief    Defines class for DDI media avc encode
25 //!
26 
27 #ifndef __MEDIA_DDI_ENCODE_AVC_H__
28 #define __MEDIA_DDI_ENCODE_AVC_H__
29 
30 #include "media_ddi_encode_base.h"
31 
32 class AvcInBits
33 {
34 public:
35     AvcInBits(uint8_t *pInBits, uint32_t BitSize);
36 
~AvcInBits()37     virtual ~AvcInBits() {};
38 
39     void SkipBits(uint32_t n);
40 
41     uint32_t GetBit();
42 
43     uint32_t GetBits(uint32_t n);
44 
45     uint32_t GetUE();
46 
47     uint32_t GetBitOffset();
48 
49     void ResetBitOffset();
50 
51 protected:
52     uint8_t const *m_pInBits = nullptr;
53     uint32_t m_BitSize = 0;
54     uint32_t m_BitOffset = 0;
55 
56 private:
AvcInBits()57     AvcInBits() {};
58 };
59 
60 class AvcOutBits
61 {
62 public:
63     AvcOutBits(uint8_t *pOutBits, uint32_t BitSize);
64 
~AvcOutBits()65     virtual ~AvcOutBits() {};
66 
67     void PutBit(uint32_t v);
68 
69     void PutBits(uint32_t v, uint32_t n);
70 
71     uint32_t GetBitOffset();
72 
73 protected:
74     uint8_t *m_pOutBits = nullptr;
75     uint8_t *m_pOutBitsEnd = nullptr;
76     uint32_t m_BitSize = 0;
77     uint32_t m_ByteSize = 0;
78     uint32_t m_BitOffset = 0;
79 
80 private:
AvcOutBits()81     AvcOutBits() {};
82 };
83 
84 //!
85 //! \class  DdiEncodeAvc
86 //! \brief  Ddi encode AVC
87 //!
88 class DdiEncodeAvc : public DdiEncodeBase
89 {
90 public:
91     //!
92     //! \brief    Constructor
93     //!
DdiEncodeAvc()94     DdiEncodeAvc(){};
95 
96     //!
97     //! \brief    Destructor
98     //!
99     virtual ~DdiEncodeAvc();
100 
101     virtual VAStatus ContextInitialize(CodechalSetting * codecHalSettings) override;
102 
103     virtual VAStatus RenderPicture(
104         VADriverContextP ctx,
105         VAContextID      context,
106         VABufferID       *buffers,
107         int32_t          numBuffers) override;
108 
109     //!
110     //! \brief    Parse Qp matrix
111     //! \details  Parse Qp matrix which called by RenderPicture
112     //!
113     //! \param    [in] ptr
114     //!           Pointer to buffer VAIQMatrixBufferH264
115     //!
116     //! \return   VAStatus
117     //!           VA_STATUS_SUCCESS if successful, else fail reason
118     //!
119     VAStatus Qmatrix(void *ptr);
120 
121     //!
122     //! \brief    Parse Sequence parameters
123     //! \details  Parse Sequence parameters which called by RenderPicture
124     //!
125     //! \param    [in] ptr
126     //!           Pointer to buffer VAEncSequenceParameterBufferH264
127     //!
128     //! \return   VAStatus
129     //!           VA_STATUS_SUCCESS if successful, else fail reason
130     //!
131     virtual VAStatus ParseSeqParams(void *ptr);
132 
133     //!
134     //! \brief    Parse slice parameters
135     //! \details  Parse slice parameters which called by RenderPicture
136     //!
137     //! \param    [in] mediaCtx
138     //!           Ddi media context
139     //!
140     //! \param    [in] ptr
141     //!           Pointer to buffer VAEncSliceParameterBufferH264
142     //!
143     //! \param    [in] numSlices
144     //!           Number of slices
145     //!
146     //! \return   VAStatus
147     //!           VA_STATUS_SUCCESS if successful, else fail reason
148     //!
149     VAStatus ParseSlcParams(
150         DDI_MEDIA_CONTEXT *mediaCtx,
151         void              *ptr,
152         uint32_t          numSlices);
153 
154     //!
155     //! \brief    Find the NAL Unit Start Codes
156     //! \details  Find the NAL unit start codes offset and NAL Unit start codes
157     //!           length in packed header NAL unit data buffer
158     //!
159     //! \param    [in] buf
160     //!           Pointer to packed header NAL unit data
161     //! \param    [in] size
162     //!           byte size of packed header NAL unit data
163     //! \param    [in] startCodesOffset
164     //!           Pointer to NAL unit start codes offset from the packed header
165     //!           NAL unit data buf
166     //! \param    [in] startCodesLength
167     //!           Pointer to NAL unit start codes length
168     //!
169     //! \return   VAStatus
170     //!           VA_STATUS_SUCCESS if success,
171     //!           else VA_STATUS_ERROR_INVALID_BUFFER if start codes doesn't exit
172     //!
173     VAStatus FindNalUnitStartCodes(
174         uint8_t * buf,
175         uint32_t size,
176         uint32_t * startCodesOffset,
177         uint32_t * startCodesLength);
178 
179     //!
180     //! \brief    Parse packedHeader paramters
181     //! \details  Parse packedHeader parameters which called by RenderPicture
182     //!
183     //! \param    [in] ptr
184     //!           Pointer to buffer VAEncPackedHeaderParameterBuffer
185     //!
186     //! \return   MOS_STATUS
187     //!           VA_STATUS_SUCCESS if successful, else fail reason
188     //!
189     VAStatus ParsePackedHeaderParams(void *ptr);
190 
191     //!
192     //! \brief    Parse packedHeader data
193     //! \details  Parse packedHeader data which called by RenderPicture
194     //!
195     //! \param    [in] ptr
196     //!           Pointer to packedHeader data
197     //!
198     //! \return   MOS_STATUS
199     //!           VA_STATUS_SUCCESS if successful, else fail reason
200     //!
201     VAStatus ParsePackedHeaderData(void *ptr);
202 
203     //!
204     //! \brief    Parse misc parameters
205     //! \details  Parse misc parameters data which called by RenderPicture
206     //!           including hrd, framerate, ratecontrol, private, quantization,
207     //!           RIR, SkipFrame, FrameSize, QualityLevel, SliceSize, ROI,
208     //!           CustomRoundingControl, SubMbPartPelIntel, ROIPrivate
209     //!
210     //! \param    [in] ptr
211     //!           Pointer to misc paramters buffer
212     //!
213     //! \return   MOS_STATUS
214     //!           VA_STATUS_SUCCESS if successful, else fail reason
215     //!
216     virtual VAStatus ParseMiscParams(void *ptr);
217 
218 protected:
219     uint32_t getSliceParameterBufferSize() override;
220 
221     uint32_t getSequenceParameterBufferSize() override;
222 
223     uint32_t getPictureParameterBufferSize() override;
224 
225     uint32_t getQMatrixBufferSize() override;
226 
227     virtual VAStatus EncodeInCodecHal(uint32_t numSlices) override;
228 
229     virtual VAStatus ResetAtFrameLevel() override;
230 
231     virtual VAStatus ParsePicParams(
232         DDI_MEDIA_CONTEXT *mediaCtx,
233         void              *ptr) override;
234 
235     virtual void ClearPicParams() override;
236 
237     virtual MOS_STATUS CheckPackedSlcHeaderData(
238         void *pInSlcHdr,
239         uint32_t InBitSize,
240         void **ppOutSlcHdr,
241         uint32_t &OutBitSize);
242 
243     //!
244     //! \brief    Convert slice struct from VA to codechal
245     //! \details  Convert slice struct from VA to codechal
246     //!
247     //! \param    [in] vaSliceStruct
248     //!           VA Slice Struct
249     //!
250     //! \return   uint8_t
251     //!           CODECHAL_SLICE_STRUCT_ARBITRARYROWSLICE,
252     //!           CODECHAL_SLICE_STRUCT_POW2ROWS,
253     //!           CODECHAL_SLICE_STRUCT_ARBITRARYMBSLICE,
254     //!           CODECHAL_SLICE_STRUCT_ROWSLICE,
255     //!           default is CODECHAL_SLICE_STRUCT_ONESLICE
256     //!
257     uint8_t ConvertSliceStruct(uint32_t vaSliceStruct);
258 
259     //!
260     //! \brief    Convert va slice type to codechal picture type
261     //! \details  Convert va slice type to codechal picture type
262     //!
263     //! \param    [in] vaSliceType
264     //!           VA Slice Type
265     //!
266     //! \return   uin8_t
267     //!           I_TYPE, P_TYPE, B_TYPE
268     //!
269     uint8_t CodechalPicTypeFromVaSlcType(uint8_t vaSliceType);
270 
271     //!
272     //! \brief    Get profile from va profile
273     //! \details  Get profile from va profile
274     //!
275     //! \return   CODEC_AVC_PROFILE_IDC
276     //!           CODEC_AVC_BASE_PROFILE, CODEC_AVC_MAIN_PROFILE
277     //!           or CODEC_AVC_HIGH_PROFILE, default is
278     //!           CODEC_AVC_MAIN_PROFILE
279     //!
280     CODEC_AVC_PROFILE_IDC GetAVCProfileFromVAProfile();
281 
282     //!
283     //! \brief    Parse DirtyROI params
284     //! \details  Parse DirtyROI params
285     //!
286     //! \param    [in] data
287     //!           Pointer to buffer VAEncMiscParameterBufferDirtyROI
288     //!
289     //! \return   VAStatus
290     //!           VA_STATUS_SUCCESS if successful, else fail reason
291     //!
292     VAStatus ParseMiscParamDirtyROI(void *data);
293 
294     //!
295     //! \brief    Parse frame rate settings
296     //! \details  Parse VAEncMiscParameterFrameRate to
297     //!           PCODEC_AVC_ENCODE_SEQUENCE_PARAMS
298     //!
299     //! \param    [in] data
300     //!           Pointer to buffer VAEncMiscParameterFrameRate
301     //!
302     //! \return   VAStatus
303     //!           VA_STATUS_SUCCESS if successful, else fail reason
304     //!
305     VAStatus ParseMiscParamFR(void *data);
306 
307     //!
308     //! \brief    Parse HRD settings
309     //! \details  Parse VAEncMiscParameterHRD to
310     //!           PCODEC_AVC_ENCODE_SEQUENCE_PARAMS
311     //!
312     //! \param    [in] data
313     //!           Pointer to buffer VAEncMiscParameterHRD
314     //!
315     //! \return   VAStatus
316     //!           VA_STATUS_SUCCESS if successful, else fail reason
317     //!
318     VAStatus ParseMiscParamHRD(void *data);
319 
320     //!
321     //! \brief    Parse max frame size setting
322     //! \details  Parse VAEncMiscParameterMaxFrameSize to
323     //!           PCODECHAL_AVC_ENCODE_SEQ_PARAMS
324     //!
325     //! \param    [in] data
326     //!           Pointer to buffer VAEncMiscParameterBufferMaxFrameSize
327     //!
328     //! \return   VAStatus
329     //!           VA_STATUS_SUCCESS if successful, else fail reason
330     //!
331     VAStatus ParseMiscParamMaxFrameSize(void *data);
332 
333     //!
334     //! \brief    Parse max frame size setting for multiple pass
335     //! \details  Parse VAEncMiscParameterBufferMultiPassFrameSize
336     //!           to PCODEC_AVC_ENCODE_PIC_PARAMS
337     //!
338     //! \param    [in] data
339     //!           Pointer to buffer VAEncMiscParameterBufferMultiPassFrameSize
340     //!
341     //! \return   VAStatus
342     //!           VA_STATUS_SUCCESS if successful, else fail reason
343     //!
344     VAStatus ParseMiscParamMultiPassFrameSize(void *data);
345 
346     //!
347     //! \brief    Parse sliceSizeInBytes for enabling VDENC dynamic slice
348     //! \details  Parse sliceSizeInBytes for enabling VDENC dynamic slice
349     //!
350     //! \param    [in] data
351     //!           Pointer to buffer VAEncMiscParameterMaxSliceSize
352     //!
353     //! \return   VAStatus
354     //!           VA_STATUS_SUCCESS if successful, else fail reason
355     //!
356     VAStatus ParseMiscParamMaxSliceSize(void *data);
357 
358     //!
359     //! \brief    Parse enc quality parameters settings
360     //! \details  Parse enc quality parameters settings
361     //!
362     //! \param    [in] data
363     //!           Pointer to buffer VAEncMiscParameterPrivate
364     //!
365     //! \return   VAStatus
366     //!           VA_STATUS_SUCCESS if successful, else fail reason
367     //!
368 
369     VAStatus ParseMiscParamEncQuality(void *data);
370 
371     //!
372     //! \brief    Parse qualtiy level settings
373     //! \details  Parse quality level(Target Usage) settings passed by app
374     //!
375     //! \param    [in] data
376     //!           Pointer to buffer VAEncMiscParameterBufferQualityLevel
377     //!
378     //! \return   VAStatus
379     //!           VA_STATUS_SUCCESS if successful, else fail reason
380     //!
381     VAStatus ParseMiscParamQualityLevel(void *data);
382 
383     //!
384     //! \brief    Parse trellis related settings
385     //! \details  Parse trellis related settings
386     //!
387     //! \param    [in] data
388     //!           Pointer to buffer VAEncMiscParameterQuantization
389     //!
390     //! \return   VAStatus
391     //!           VA_STATUS_SUCCESS if successful, else fail reason
392     //!
393     VAStatus ParseMiscParamQuantization(void *data);
394 
395     //!
396     //! \brief    Parse rate control related settings
397     //! \details  Parse rate control related settings
398     //!
399     //! \param    [in] data
400     //!           Pointer to buffer VAEncMiscParameterRateControl
401     //!
402     //! \return   VAStatus
403     //!           VA_STATUS_SUCCESS if successful, else fail reason
404     //!
405     VAStatus ParseMiscParamRC(void *data);
406 
407     //!
408     //! \brief    Parse region of interest settings
409     //! \details  Parse region of interest settings
410     //!
411     //! \param    [in] data
412     //!           Pointer to buffer VAEncMiscParameterBufferROI
413     //!
414     //! \return   VAStatus
415     //!           VA_STATUS_SUCCESS if successful, else fail reason
416     //!
417     VAStatus ParseMiscParamROI(void *data);
418 
419     //!
420     //! \brief    Parse rounding parameters for slices
421     //! \details  Parse rounding parameters for slices
422     //!
423     //! \param    [in] data
424     //!           Pointer to buffer VAEncMiscParameterCustomRoundingControl
425     //!
426     //! \return   VAStatus
427     //!           VA_STATUS_SUCCESS if successful, else fail reason
428     //!
429     VAStatus ParseMiscParamRounding(void *data);
430 
431     //!
432     //! \brief    Parse rolling intra refresh setting
433     //! \details  Parse rolling intra refresh setting, Row/Colum
434     //!           rolling supported on Gen8
435     //!
436     //! \param    [in] data
437     //!           Pointer to buffer VAEncMiscParameterSkipFrame
438     //!
439     //! \return   VAStatus
440     //!           VA_STATUS_SUCCESS if successful, else fail reason
441     //!
442     VAStatus ParseMiscParamSkipFrame(void *data);
443 
444     //!
445     //! \brief    Parse sub macroblock partition setting
446     //! \details  Parse sub marcoblock partition setting
447     //!
448     //! \param    [in] data
449     //!           Pointer to buffer VAEncMiscParameterSubMbPartPelH264
450     //!
451     //! \return   VAStatus
452     //!           VA_STATUS_SUCCESS if successful, else fail reason
453     //!
454     VAStatus ParseMiscParamSubMbPartPel(void *data);
455 
456     //!
457     //! \brief    Parse rolling intra refresh setting
458     //! \details  Parse rolling intra refresh setting, Row/Colum
459     //!           rolling supported on Gen8
460     //!
461     //! \param    [in] data
462     //!           Pointer to buffer VAEncMiscParameterRIR
463     //!
464     //! \return   VAStatus
465     //!           VA_STATUS_SUCCESS if successful, else fail reason
466     //!
467     VAStatus ParseMiscParameterRIR(void *data);
468 
469     //!
470     //! \brief    Get Slice Reference Index
471     //! \details  To change the value of  sliceParams->RefPicList[][].FrameIdx
472     //!           from the frame itself  to the index in picParams->RefFrameList
473     //!
474     //! \param    [in] picReference
475     //!           Pointer to CODEC_PICTURE
476     //! \param    [in] slcReference
477     //!           Pointer to CODEC_PICTURE
478     //!
479     //! \return   void
480     //!
481     void GetSlcRefIdx(CODEC_PICTURE *picReference, CODEC_PICTURE *slcReference);
482 
483     //!
484     //! \brief    Setup Codec Picture for AVC
485     //!
486     //! \param    [in] mediaCtx
487     //!           Pointer to DDI_MEDIA_CONTEXT
488     //! \param    [in] rtTbl
489     //!           Pointer to DDI_CODEC_RENDER_TARGET_TABLE
490     //! \param    [in] vaPic
491     //!           H264 VAPicture structure
492     //! \param    [in] fieldPicFlag
493     //!           Field picture flag
494     //! \param    [in] picReference
495     //!           Reference picture flag
496     //! \param    [in] sliceReference
497     //!           Reference slice flag
498     //! \param    [out] codecHalPic
499     //!           Pointer to CODEC_PICTURE
500     //!
501     //! \return   void
502     //!
503     void SetupCodecPicture(
504         DDI_MEDIA_CONTEXT                   *mediaCtx,
505         DDI_CODEC_RENDER_TARGET_TABLE       *rtTbl,
506         CODEC_PICTURE                       *codecHalPic,
507         VAPictureH264                       vaPic,
508         bool                                fieldPicFlag,
509         bool                                picReference,
510         bool                                sliceReference);
511 
512     CODECHAL_ENCODE_AVC_QUALITY_CTRL_PARAMS *m_qcParams = nullptr;       //!< Quality control parameters.
513     CODECHAL_ENCODE_AVC_ROUNDING_PARAMS     *m_roundingParams = nullptr; //!< Rounding parameters.
514     uint8_t                                 m_weightScale4x4[6][16];     //!< Inverse quantization weight scale 4x4.
515     uint8_t                                 m_weightScale8x8[2][64];     //!< Inverse quantization weight scale 8x8.
516     uint16_t                                m_previousFRper100sec = 0;   //!< For saving FR value to be used in case of dynamic BRC reset.
517 
518 private:
519     //!
520     //! \brief    Check whether swizzle needed
521     //!
522     //! \param    [in] rawSurface
523     //!           Pointer of Raw Surface
524     //!
525     //! \return   bool, true if need, otherwise false
526     //!
NeedDisplayFormatSwizzle(DDI_MEDIA_SURFACE * rawSurface)527     inline bool NeedDisplayFormatSwizzle(DDI_MEDIA_SURFACE *rawSurface)
528     {
529         if (Media_Format_A8R8G8B8 == rawSurface->format ||
530             Media_Format_X8R8G8B8 == rawSurface->format ||
531             Media_Format_B10G10R10A2 == rawSurface->format)
532         {
533             return true;
534         }
535 
536         return false;
537     }
538 
539     //! \brief H.264 current picture parameter set id
540     uint8_t current_pic_parameter_set_id = 0;
541 
542     //! \brief H.264 current sequence parameter set id
543     uint8_t current_seq_parameter_set_id = 0;
544 
545     //! \brief H.264 Inverse Quantization Matrix Buffer.
546     PCODEC_AVC_IQ_MATRIX_PARAMS iqMatrixParams = nullptr;
547 
548     //! \brief H.264 Inverse Quantization Weight Scale.
549     PCODEC_AVC_ENCODE_IQ_WEIGTHSCALE_LISTS iqWeightScaleLists = nullptr;
550 };
551 #endif /* __MEDIA_DDI_ENCODE_AVC_H__ */
552