Debian Codegraph @

media_softlet/linux/common/codec/ddi/enc/ddi_encode_avc_specific.h

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