Debian Codegraph @

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

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.cpp
24 //! \brief Implements class for DDI media avc encode
25 //!
26
27 #include "media_libva_encoder.h"
28 #include "media_libva_util_next.h"
29 #include "hwinfo_linux.h"
30 #include "ddi_encode_base_specific.h"
31 #include "ddi_encode_avc_specific.h"
32 #include "media_ddi_encode_const.h"
33 #include "media_ddi_factory.h"
34 #include "media_libva_caps_next.h"
35 #include "media_libva_interface_next.h"
36
37 namespace encode
38 {
39
40 // refer to spec section E.2.2, bit rate and cbp buffer size are shifted left with several bits, k is 1024
41 static constexpr uint16_t VuiKbps = 1024;
42
43 static constexpr uint8_t SliceTypeP = 0;
44 static constexpr uint8_t SliceTypeB = 1;
45 static constexpr uint8_t SliceTypeI = 2;
46 static constexpr uint8_t MaxPassesNum = 4;
47
48 //Inter MB partition 16x16 can't be disabled.
49 static constexpr uint8_t DisMbPartMask = 0x7E;
50 static constexpr uint8_t SubpelModeMask = 0x3;
51 static constexpr uint8_t SubpelModeQuant = 0x3;
52 static constexpr uint8_t SubpelModeReserved = 0x2;
53 static constexpr uint8_t SubpelModeHalf = 0x1;
54
55 DdiEncodeAvc::~DdiEncodeAvc()
56 {
57 if (nullptr == m_encodeCtx)
58 {
59 return;
60 }
61 MOS_FreeMemory(m_encodeCtx->pSeqParams);
62 m_encodeCtx->pSeqParams = nullptr;
63
64 MOS_FreeMemory(m_encodeCtx->pPicParams);
65 m_encodeCtx->pPicParams = nullptr;
66
67 if (nullptr != m_encodeCtx->ppNALUnitParams)
68 {
69 // Allocate one contiguous memory for the NALUnitParams buffers
70 // only need to free one time
71 MOS_FreeMemory(m_encodeCtx->ppNALUnitParams[0]);
72 m_encodeCtx->ppNALUnitParams[0] = nullptr;
73
74 MOS_FreeMemory(m_encodeCtx->ppNALUnitParams);
75 m_encodeCtx->ppNALUnitParams = nullptr;
76 }
77
78 MOS_FreeMemory(m_encodeCtx->pVuiParams);
79 m_encodeCtx->pVuiParams = nullptr;
80
81 MOS_FreeMemory(m_encodeCtx->pSliceParams);
82 m_encodeCtx->pSliceParams = nullptr;
83
84 MOS_FreeMemory(m_encodeCtx->pEncodeStatusReport);
85 m_encodeCtx->pEncodeStatusReport = nullptr;
86
87 if (m_encodeCtx->pSEIFromApp)
88 {
89 MOS_FreeMemory(m_encodeCtx->pSEIFromApp->pSEIBuffer);
90 m_encodeCtx->pSEIFromApp->pSEIBuffer = nullptr;
91
92 MOS_FreeMemory(m_encodeCtx->pSEIFromApp);
93 m_encodeCtx->pSEIFromApp = nullptr;
94 }
95
96 MOS_FreeMemory(m_encodeCtx->pSliceHeaderData);
97 m_encodeCtx->pSliceHeaderData = nullptr;
98
99 if (m_encodeCtx->pbsBuffer)
100 {
101 MOS_FreeMemory(m_encodeCtx->pbsBuffer->pBase);
102 m_encodeCtx->pbsBuffer->pBase = nullptr;
103
104 MOS_FreeMemory(m_encodeCtx->pbsBuffer);
105 m_encodeCtx->pbsBuffer = nullptr;
106 }
107
108 MOS_FreeMemory(m_qcParams);
109 m_qcParams = nullptr;
110
111 MOS_FreeMemory(m_roundingParams);
112 m_roundingParams = nullptr;
113
114 MOS_FreeMemory(iqMatrixParams);
115 iqMatrixParams = nullptr;
116
117 MOS_FreeMemory(iqWeightScaleLists);
118 iqWeightScaleLists = nullptr;
119 }
120
121 uint8_t DdiEncodeAvc::ConvertSliceStruct(uint32_t vaSliceStruct)
122 {
123 switch (vaSliceStruct)
124 {
125 case VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS:
126 return CODECHAL_SLICE_STRUCT_ARBITRARYROWSLICE;
127 case VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS:
128 return CODECHAL_SLICE_STRUCT_POW2ROWS;
129 case VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS:
130 return CODECHAL_SLICE_STRUCT_ARBITRARYMBSLICE;
131 case VA_ENC_SLICE_STRUCTURE_EQUAL_MULTI_ROWS:
132 case VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS:
133 return CODECHAL_SLICE_STRUCT_ROWSLICE;
134 default:
135 return CODECHAL_SLICE_STRUCT_ONESLICE;
136 }
137 }
138
139 CODEC_AVC_PROFILE_IDC DdiEncodeAvc::GetAVCProfileFromVAProfile()
140 {
141 switch (m_encodeCtx->vaProfile)
142 {
143 case VAProfileH264ConstrainedBaseline:
144 return CODEC_AVC_BASE_PROFILE;
145 case VAProfileH264Main:
146 return CODEC_AVC_MAIN_PROFILE;
147 case VAProfileH264High:
148 return CODEC_AVC_HIGH_PROFILE;
149 default:
150 return CODEC_AVC_MAIN_PROFILE;
151 }
152 }
153
154 uint8_t DdiEncodeAvc::CodechalPicTypeFromVaSlcType(uint8_t vaSliceType)
155 {
156 switch (vaSliceType)
157 {
158 case SliceTypeI:
159 case (SliceTypeI + 5):
160 return I_TYPE;
161 case SliceTypeP:
162 case (SliceTypeP + 5):
163 return P_TYPE;
164 case SliceTypeB:
165 case (SliceTypeB + 5):
166 return B_TYPE;
167 default:
168 return 0;
169 }
170 }
171
172 VAStatus DdiEncodeAvc::ParseMiscParamHRD(void *data)
173 {
174 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
175
176 VAEncMiscParameterHRD *vaEncMiscParamHRD = (VAEncMiscParameterHRD *)data;
177 CODECHAL_ENCODE_AVC_VUI_PARAMS *vuiParam = (CODECHAL_ENCODE_AVC_VUI_PARAMS *)m_encodeCtx->pVuiParams;
178 // Assume only one SPS here, modify when enable multiple SPS support
179 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams);
180 DDI_CODEC_CHK_NULL(vuiParam, "nullptr vuiParam", VA_STATUS_ERROR_INVALID_PARAMETER);
181 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
182
183 vuiParam->cbr_flag = 0x1;
184 seqParams->VBVBufferSizeInBit = vaEncMiscParamHRD->buffer_size;
185 seqParams->InitVBVBufferFullnessInBit = vaEncMiscParamHRD->initial_buffer_fullness;
186 vuiParam->cpb_size_value_minus1[0] = MOS_ROUNDUP_DIVIDE(seqParams->VBVBufferSizeInBit, VuiKbps) - 1;
187
188 return VA_STATUS_SUCCESS;
189 }
190
191 VAStatus DdiEncodeAvc::ParseMiscParamFR(void *data)
192 {
193 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
194
195 // Assume only one SPS here, modify when enable multiple SPS support
196 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams);
197 VAEncMiscParameterFrameRate *encMiscParamFR = (VAEncMiscParameterFrameRate *)data;
198 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
199
200 uint32_t numerator = (encMiscParamFR->framerate & 0xffff) * 100; /** 100*/;
201 auto denominator = (encMiscParamFR->framerate >> 16)&0xfff;
202 if(denominator == 0)
203 {
204 denominator = 1;
205 }
206
207 // Pass frame rate value to seqParams and set BRC reset flag and bNewSeq to true while dynamic BRC occures
208 seqParams->FramesPer100Sec = (uint16_t)(numerator / denominator);
209 if(m_previousFRper100sec != 0)
210 {
211 if(m_previousFRper100sec != seqParams->FramesPer100Sec)
212 {
213 seqParams->bResetBRC = 0x1;
214 m_encodeCtx->bNewSeq = true;
215 }
216 }
217 m_previousFRper100sec = seqParams->FramesPer100Sec;
218
219 return VA_STATUS_SUCCESS;
220 }
221
222 VAStatus DdiEncodeAvc::ParseMiscParamRC(void *data)
223 {
224 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
225
226 CODECHAL_ENCODE_AVC_VUI_PARAMS *vuiParam = (CODECHAL_ENCODE_AVC_VUI_PARAMS *)m_encodeCtx->pVuiParams;
227
228 // Assume only one SPS here, modify when enable multiple SPS support
229 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams) + current_seq_parameter_set_id;
230 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)(m_encodeCtx->pPicParams) + current_pic_parameter_set_id;
231 VAEncMiscParameterRateControl *encMiscParamRC = (VAEncMiscParameterRateControl *)data;
232 DDI_CODEC_CHK_NULL(vuiParam, "nullptr vuiParam", VA_STATUS_ERROR_INVALID_PARAMETER);
233 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
234 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
235
236 seqParams->TargetBitRate = encMiscParamRC->bits_per_second;
237 vuiParam->bit_rate_value_minus1[0] = MOS_ROUNDUP_SHIFT(encMiscParamRC->bits_per_second, 6 + vuiParam->bit_rate_scale) - 1;
238
239 // Assuming picParams are sent before MiscParams
240 picParams->ucMinimumQP = encMiscParamRC->min_qp;
241 picParams->ucMaximumQP = encMiscParamRC->max_qp;
242 if (picParams->ucMaximumQP == 0 && picParams->ucMinimumQP)
243 picParams->ucMaximumQP = 51;
244
245 if ((VA_RC_CBR == m_encodeCtx->uiRCMethod) || ((VA_RC_CBR | VA_RC_MB) == m_encodeCtx->uiRCMethod))
246 {
247 seqParams->MaxBitRate = seqParams->TargetBitRate;
248 seqParams->MinBitRate = seqParams->TargetBitRate;
249 vuiParam->cbr_flag = 0x1;
250 if (m_encodeCtx->uiTargetBitRate != seqParams->TargetBitRate)
251 {
252 if (m_encodeCtx->uiTargetBitRate)
253 {
254 seqParams->bResetBRC = 0x1;
255 m_encodeCtx->bNewSeq = true;
256 }
257 m_encodeCtx->uiTargetBitRate = seqParams->TargetBitRate;
258 m_encodeCtx->uiMaxBitRate = seqParams->TargetBitRate;
259 }
260 }
261 else if (VA_RC_ICQ == m_encodeCtx->uiRCMethod)
262 {
263 seqParams->ICQQualityFactor = encMiscParamRC->ICQ_quality_factor;
264 }
265 else if (VA_RC_AVBR == m_encodeCtx->uiRCMethod)
266 {
267 seqParams->AVBRAccuracy = encMiscParamRC->target_percentage;
268 seqParams->AVBRConvergence = encMiscParamRC->window_size;
269 }
270 else
271 {
272 seqParams->MaxBitRate = seqParams->TargetBitRate;
273 seqParams->TargetBitRate = (uint32_t)((uint64_t)seqParams->TargetBitRate * encMiscParamRC->target_percentage / 100);
274 seqParams->MinBitRate = seqParams->TargetBitRate;
275 vuiParam->cbr_flag = 0x0;
276
277 if (VA_RC_QVBR == m_encodeCtx->uiRCMethod)
278 {
279 seqParams->ICQQualityFactor = encMiscParamRC->quality_factor;
280 }
281
282 if ((m_encodeCtx->uiTargetBitRate != seqParams->TargetBitRate) ||
283 (m_encodeCtx->uiMaxBitRate != seqParams->MaxBitRate))
284 {
285 if ((m_encodeCtx->uiTargetBitRate != 0) && (m_encodeCtx->uiMaxBitRate != 0))
286 {
287 seqParams->bResetBRC = 0x1;
288 m_encodeCtx->bNewSeq = true;
289 }
290 m_encodeCtx->uiTargetBitRate = seqParams->TargetBitRate;
291 m_encodeCtx->uiMaxBitRate = seqParams->MaxBitRate;
292 }
293 }
294 //if RateControl method is VBR/CBR and VC_RC_MB bit is set, we can set MBBRC to enable or disable
295 if (VA_RC_CQP != m_encodeCtx->uiRCMethod && (VA_RC_MB & m_encodeCtx->uiRCMethod) && encMiscParamRC->rc_flags.bits.mb_rate_control <= mbBrcDisabled)
296 {
297 seqParams->MBBRC = encMiscParamRC->rc_flags.bits.mb_rate_control;
298 }
299 else
300 {
301 seqParams->MBBRC = mbBrcDisabled;
302 }
303
304 #ifndef ANDROID
305 seqParams->FrameSizeTolerance = static_cast<ENCODE_FRAMESIZE_TOLERANCE>(encMiscParamRC->rc_flags.bits.frame_tolerance_mode);
306 #endif
307
308 #if VA_CHECK_VERSION(1, 10, 0)
309 if (m_encodeCtx->bVdencActive &&
310 (m_encodeCtx->uiRCMethod & (VA_RC_VBR | VA_RC_QVBR | VA_RC_VCM)))
311 {
312 picParams->TargetFrameSize = encMiscParamRC->target_frame_size;
313 // to force VC scenario for TCBRC
314 seqParams->bAutoMaxPBFrameSizeForSceneChange = encMiscParamRC->target_frame_size != 0;
315 }
316 #endif
317
318 return VA_STATUS_SUCCESS;
319 }
320
321 VAStatus DdiEncodeAvc::ParseMiscParamSkipFrame(void *data)
322 {
323 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
324
325 // Assume only one PPS here, modify when enable multiple PPS support
326 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)(m_encodeCtx->pPicParams) + current_pic_parameter_set_id;
327 VAEncMiscParameterSkipFrame *vaEncMiscParamSkipFrame = (VAEncMiscParameterSkipFrame *)data;
328 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
329
330 // populate skipped frame params from DDI
331 picParams->SkipFrameFlag = vaEncMiscParamSkipFrame->skip_frame_flag;
332 picParams->NumSkipFrames = vaEncMiscParamSkipFrame->num_skip_frames;
333 picParams->SizeSkipFrames = vaEncMiscParamSkipFrame->size_skip_frames;
334
335 if (FRAME_SKIP_NORMAL < picParams->SkipFrameFlag)
336 {
337 return VA_STATUS_ERROR_INVALID_PARAMETER;
338 }
339
340 return VA_STATUS_SUCCESS;
341 }
342
343 VAStatus DdiEncodeAvc::ParseMiscParamMaxFrameSize(void *data)
344 {
345 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
346
347 // Assume only one SPS here, modify when enable multiple SPS support
348 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams);
349 VAEncMiscParameterBufferMaxFrameSize *vaEncMiscParamMaxFrameSize = (VAEncMiscParameterBufferMaxFrameSize *)data;
350 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
351
352 if (seqParams->UserMaxFrameSize != vaEncMiscParamMaxFrameSize->max_frame_size >> 3)
353 {
354 seqParams->bResetBRC = 0x1;
355 }
356
357 // populate MaxFrameSize from DDI
358 seqParams->UserMaxFrameSize = vaEncMiscParamMaxFrameSize->max_frame_size >> 3; // convert to byte
359
360 return VA_STATUS_SUCCESS;
361 }
362
363 VAStatus DdiEncodeAvc::ParseMiscParamMultiPassFrameSize(void *data)
364 {
365 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
366
367 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)(m_encodeCtx->pPicParams) + current_pic_parameter_set_id;
368 VAEncMiscParameterBufferMultiPassFrameSize *vaEncMiscParamMultiPassFrameSize = (VAEncMiscParameterBufferMultiPassFrameSize *)data;
369 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
370
371 //add for multiple pass pak
372 picParams->dwMaxFrameSize = vaEncMiscParamMultiPassFrameSize->max_frame_size;
373 if (picParams->dwMaxFrameSize)
374 {
375 picParams->dwNumPasses = vaEncMiscParamMultiPassFrameSize->num_passes;
376 if ((picParams->dwNumPasses == 0) || (picParams->dwNumPasses > MaxPassesNum))
377 {
378 return VA_STATUS_ERROR_INVALID_PARAMETER;
379 }
380 if (picParams->pDeltaQp != nullptr)
381 {
382 MOS_FreeMemory(picParams->pDeltaQp);
383 }
384 picParams->pDeltaQp = (uint8_t *)MOS_AllocAndZeroMemory(sizeof(uint8_t) * picParams->dwNumPasses);
385 if (!picParams->pDeltaQp)
386 {
387 return VA_STATUS_ERROR_INVALID_PARAMETER;
388 }
389
390 if (MOS_STATUS_SUCCESS != MOS_SecureMemcpy(picParams->pDeltaQp, picParams->dwNumPasses, vaEncMiscParamMultiPassFrameSize->delta_qp, picParams->dwNumPasses))
391 {
392 return VA_STATUS_ERROR_INVALID_PARAMETER;
393 }
394 }
395
396 return VA_STATUS_SUCCESS;
397 }
398
399 VAStatus DdiEncodeAvc::ParseMiscParamEncQuality(void *data)
400 {
401 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
402
403 // Assume only one SPS here, modify when enable multiple SPS support
404 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams) + current_seq_parameter_set_id;
405
406 VAEncMiscParameterEncQuality *vaEncMiscParamEncQuality = (VAEncMiscParameterEncQuality *)data;
407 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)(m_encodeCtx->pPicParams) + current_pic_parameter_set_id;
408
409 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
410
411 seqParams->bForcePanicModeControl = 0;
412 seqParams->bPanicModeDisable = (uint8_t)vaEncMiscParamEncQuality->PanicModeDisable;
413 picParams->UserFlags.bUseRawPicForRef = vaEncMiscParamEncQuality->useRawPicForRef;
414 m_qcParams->skipCheckDisable = vaEncMiscParamEncQuality->skipCheckDisable;
415 m_qcParams->FTQOverride = vaEncMiscParamEncQuality->FTQOverride;
416 if (m_qcParams->FTQOverride)
417 {
418 m_qcParams->FTQEnable = vaEncMiscParamEncQuality->FTQEnable;
419 }
420 m_qcParams->FTQSkipThresholdLUTInput = vaEncMiscParamEncQuality->FTQSkipThresholdLUTInput;
421 if (m_qcParams->FTQSkipThresholdLUTInput)
422 {
423 MOS_SecureMemcpy(m_qcParams->FTQSkipThresholdLUT, 52, vaEncMiscParamEncQuality->FTQSkipThresholdLUT, 52);
424 }
425 m_qcParams->NonFTQSkipThresholdLUTInput = vaEncMiscParamEncQuality->NonFTQSkipThresholdLUTInput;
426 if (m_qcParams->NonFTQSkipThresholdLUTInput)
427 {
428 MOS_SecureMemcpy(m_qcParams->NonFTQSkipThresholdLUT, 52, vaEncMiscParamEncQuality->NonFTQSkipThresholdLUT, 52);
429 }
430
431 m_qcParams->directBiasAdjustmentEnable = vaEncMiscParamEncQuality->directBiasAdjustmentEnable;
432 m_qcParams->globalMotionBiasAdjustmentEnable = vaEncMiscParamEncQuality->globalMotionBiasAdjustmentEnable;
433 m_qcParams->HMEMVCostScalingFactor = vaEncMiscParamEncQuality->HMEMVCostScalingFactor;
434 //disable HME
435 m_qcParams->HMEDisable = vaEncMiscParamEncQuality->HMEDisable;
436 //disable Super HME
437 m_qcParams->SuperHMEDisable = vaEncMiscParamEncQuality->SuperHMEDisable;
438 //disable Ultra HME
439 m_qcParams->UltraHMEDisable = vaEncMiscParamEncQuality->UltraHMEDisable;
440
441 // Force RepartitionCheck
442 m_qcParams->ForceRepartitionCheck = vaEncMiscParamEncQuality->ForceRepartitionCheck;
443
444 return VA_STATUS_SUCCESS;
445 }
446
447 VAStatus DdiEncodeAvc::ParseMiscParamQuantization(void *data)
448 {
449 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
450
451 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams);
452 VAEncMiscParameterQuantization *vaEncMiscParamQuantization = (VAEncMiscParameterQuantization *)data;
453 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
454
455 seqParams->Trellis = trellisInternal;
456
457 if (vaEncMiscParamQuantization->quantization_flags.bits.disable_trellis)
458 {
459 seqParams->Trellis = trellisDisabled;
460 }
461 else
462 {
463 if (vaEncMiscParamQuantization->quantization_flags.bits.enable_trellis_I)
464 {
465 seqParams->Trellis |= trellisEnabledI;
466 }
467 if (vaEncMiscParamQuantization->quantization_flags.bits.enable_trellis_P)
468 {
469 seqParams->Trellis |= trellisEnabledP;
470 }
471 if (vaEncMiscParamQuantization->quantization_flags.bits.enable_trellis_B)
472 {
473 seqParams->Trellis |= trellisEnabledB;
474 }
475 if (!seqParams->Trellis)
476 {
477 DDI_CODEC_ASSERTMESSAGE("trellis enabled, but the input parameters is invalided");
478 }
479 }
480
481 return VA_STATUS_SUCCESS;
482 }
483
484 VAStatus DdiEncodeAvc::ParseMiscParameterRIR(void *data)
485 {
486 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
487
488 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)(m_encodeCtx->pPicParams) + current_pic_parameter_set_id;
489 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams) + current_seq_parameter_set_id;
490 VAEncMiscParameterRIR *vaEncMiscParamRIR = (VAEncMiscParameterRIR *)data;
491 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
492 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
493
494 picParams->EnableRollingIntraRefresh = vaEncMiscParamRIR->rir_flags.value & 0x3; //only lower two bits are valid
495
496 #ifdef Android
497 //Convert row based rolling I to suqare region based rolling I before MSDK is ready to support square region based rolling I
498 if (ROLLING_I_ROW == picParams->EnableRollingIntraRefresh)
499 {
500 if (GFX_IS_PRODUCT(m_encodeCtx->pMediaCtx->platform, IGFX_BROADWELL))
501 {
502 picParams->EnableRollingIntraRefresh = ROLLING_I_SQUARE;
503 }
504 vaEncMiscParamRIR->intra_insert_size *= (seqParams->FrameWidth / CODECHAL_ENCODE_AVC_ROI_WIDTH_SCALE_FACTOR);
505 // From the experiment result, -6 could be the proper setting
506 vaEncMiscParamRIR->qp_delta_for_inserted_intra = -6;
507 }
508 #endif
509 // Set for all frames since pic type is not known yet. Disable in slice params if its I or B type.
510 switch (picParams->EnableRollingIntraRefresh)
511 {
512 case ROLLING_I_COLUMN:
513 picParams->IntraRefreshMBx = (uint8_t)vaEncMiscParamRIR->intra_insertion_location;
514 picParams->IntraRefreshMBNum = (uint8_t)vaEncMiscParamRIR->intra_insertion_location;
515 picParams->IntraRefreshUnitinMB = (uint8_t)vaEncMiscParamRIR->intra_insert_size;
516 break;
517 case ROLLING_I_ROW:
518 picParams->IntraRefreshMBy = (uint8_t)vaEncMiscParamRIR->intra_insertion_location;
519 picParams->IntraRefreshMBNum = (uint8_t)vaEncMiscParamRIR->intra_insertion_location;
520 picParams->IntraRefreshUnitinMB = (uint8_t)vaEncMiscParamRIR->intra_insert_size;
521 break;
522 case ROLLING_I_SQUARE:
523 picParams->IntraRefreshUnitinMB = (uint8_t)GFX_UF_ROUND(sqrt((double)vaEncMiscParamRIR->intra_insert_size));
524 break;
525 case ROLLING_I_DISABLED:
526 break;
527 default:
528 return MOS_STATUS_INVALID_PARAMETER;
529 }
530 picParams->IntraRefreshQPDelta = vaEncMiscParamRIR->qp_delta_for_inserted_intra;
531
532 // UMD tracks the MBx/MBy for the intra square
533 uint32_t rightBorder = ((seqParams->FrameWidth + CODECHAL_ENCODE_AVC_ROI_WIDTH_SCALE_FACTOR - 1) / CODECHAL_ENCODE_AVC_ROI_WIDTH_SCALE_FACTOR) - 1;
534 uint32_t bottomBorder = ((seqParams->FrameHeight + CODECHAL_ENCODE_AVC_ROI_FRAME_HEIGHT_SCALE_FACTOR - 1) / CODECHAL_ENCODE_AVC_ROI_FRAME_HEIGHT_SCALE_FACTOR) - 1;
535 if ((ROLLING_I_SQUARE == picParams->EnableRollingIntraRefresh))
536 {
537 if (m_encodeCtx->uiIntraRefreshFrameCnt == 0)
538 {
539 m_encodeCtx->uiIntraRefreshFrameCnt = 1;
540 m_encodeCtx->uiIntraRefreshMBx = 0;
541 m_encodeCtx->uiIntraRefreshMBy = 0;
542 }
543 else
544 {
545 m_encodeCtx->uiIntraRefreshMBx += picParams->IntraRefreshUnitinMB;
546 if (m_encodeCtx->uiIntraRefreshMBx >= rightBorder)
547 {
548 m_encodeCtx->uiIntraRefreshMBx = 0;
549 m_encodeCtx->uiIntraRefreshMBy += picParams->IntraRefreshUnitinMB;
550 if (m_encodeCtx->uiIntraRefreshMBy >= bottomBorder)
551 {
552 m_encodeCtx->uiIntraRefreshMBx = 0;
553 m_encodeCtx->uiIntraRefreshMBy = 0;
554 }
555 }
556 }
557 // set MBx/MBy to CODECHAL
558 picParams->IntraRefreshMBx = m_encodeCtx->uiIntraRefreshMBx;
559 picParams->IntraRefreshMBy = m_encodeCtx->uiIntraRefreshMBy;
560 }
561
562 return VA_STATUS_SUCCESS;
563 }
564
565 VAStatus DdiEncodeAvc::ParseMiscParamQualityLevel(void *data)
566 {
567 // Assume only one SPS here, modify when enable multiple SPS support
568 VAEncMiscParameterBufferQualityLevel *vaEncMiscParamQualityLevel = (VAEncMiscParameterBufferQualityLevel *)data;
569
570 m_encodeCtx->targetUsage = (uint8_t)vaEncMiscParamQualityLevel->quality_level;
571
572 // check if TU setting is valid, otherwise change to default
573 if ((m_encodeCtx->targetUsage > TARGETUSAGE_BEST_SPEED) || (0 == m_encodeCtx->targetUsage))
574 {
575 m_encodeCtx->targetUsage = TARGETUSAGE_RT_SPEED;
576 DDI_CODEC_ASSERTMESSAGE("Quality Level setting from application is not correct, should be in (0,%d].", TARGETUSAGE_BEST_SPEED);
577 }
578
579 return VA_STATUS_SUCCESS;
580 }
581
582 VAStatus DdiEncodeAvc::ParseMiscParamDirtyROI(void *data)
583 {
584 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)(m_encodeCtx->pPicParams) + current_pic_parameter_set_id;
585 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams) + current_seq_parameter_set_id;
586 VAEncMiscParameterBufferDirtyRect *dirtyRect = (VAEncMiscParameterBufferDirtyRect *)data;
587 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
588 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
589 DDI_CODEC_CHK_NULL(dirtyRect, "nullptr dirtyRect", VA_STATUS_ERROR_INVALID_PARAMETER);
590 DDI_CODEC_CHK_NULL(dirtyRect->roi_rectangle, "nullptr dirtyRect->roi_rectangle", VA_STATUS_ERROR_INVALID_PARAMETER);
591
592 if (dirtyRect->num_roi_rectangle > 0)
593 {
594 uint16_t mbHeightScaleFactor = picParams->FieldCodingFlag ? CODECHAL_ENCODE_AVC_ROI_FIELD_HEIGHT_SCALE_FACTOR : CODECHAL_ENCODE_AVC_ROI_FRAME_HEIGHT_SCALE_FACTOR;
595
596 // Support upto 4 Dirty rectangles
597 int32_t maxDirtyRects = (dirtyRect->num_roi_rectangle > CODEC_AVC_NUM_MAX_DIRTY_RECT) ? CODEC_AVC_NUM_MAX_DIRTY_RECT : dirtyRect->num_roi_rectangle;
598
599 picParams->NumDirtyROI = 0;
600
601 MOS_ZeroMemory(picParams->DirtyROI, CODEC_AVC_NUM_MAX_DIRTY_RECT * sizeof(CODEC_ROI));
602
603 PCODEC_ROI roi;
604 for (int32_t i = 0; i < maxDirtyRects; i++)
605 {
606 if (nullptr != dirtyRect->roi_rectangle)
607 {
608 roi = &picParams->DirtyROI[picParams->NumDirtyROI];
609 roi->Left = MOS_MIN(MOS_MAX(dirtyRect->roi_rectangle->x, 0), seqParams->FrameWidth - 1);
610 roi->Top = MOS_MIN(MOS_MAX(dirtyRect->roi_rectangle->y, 0), seqParams->FrameHeight - 1);
611 roi->Right = MOS_MIN(dirtyRect->roi_rectangle->x + dirtyRect->roi_rectangle->width, seqParams->FrameWidth - 1);
612 roi->Bottom = MOS_MIN(dirtyRect->roi_rectangle->y + dirtyRect->roi_rectangle->height, seqParams->FrameHeight - 1);
613
614 // Check and adjust if ROI not within frame boundaries
615 roi->Left = MOS_MIN(roi->Left, seqParams->FrameWidth - 1);
616 roi->Top = MOS_MIN(roi->Top, seqParams->FrameHeight - 1);
617 roi->Right = MOS_MIN(roi->Right, seqParams->FrameWidth - 1);
618 roi->Bottom = MOS_MIN(roi->Bottom, seqParams->FrameHeight - 1);
619
620 roi->Right = MOS_ALIGN_CEIL(roi->Right, CODECHAL_ENCODE_AVC_ROI_WIDTH_SCALE_FACTOR);
621 roi->Bottom = MOS_ALIGN_CEIL(roi->Bottom, mbHeightScaleFactor);
622
623 // Convert from pixel units to MB units
624 roi->Left /= CODECHAL_ENCODE_AVC_ROI_WIDTH_SCALE_FACTOR;
625 roi->Right /= CODECHAL_ENCODE_AVC_ROI_WIDTH_SCALE_FACTOR;
626 roi->Top /= mbHeightScaleFactor;
627 roi->Bottom /= mbHeightScaleFactor;
628
629 dirtyRect->roi_rectangle++;
630 picParams->NumDirtyROI++;
631 }
632 else
633 {
634 continue;
635 }
636 }
637 }
638
639 return VA_STATUS_SUCCESS;
640 }
641
642 VAStatus DdiEncodeAvc::ParseMiscParamROI(void *data)
643 {
644 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
645 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
646
647 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)(m_encodeCtx->pPicParams) + current_pic_parameter_set_id;
648 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams) + current_seq_parameter_set_id;
649 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
650 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
651
652 VAEncMiscParameterBufferROI *vaEncMiscParamROI = (VAEncMiscParameterBufferROI *)data;
653 PCODEC_ROI roi = &picParams->ROI[0];
654
655 DDI_CODEC_CHK_NULL(m_encodeCtx->pMediaCtx, "nullptr pMediaCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
656
657 bool isROIValueInDeltaQP = true;
658
659 seqParams->ROIValueInDeltaQP = true;
660
661 picParams->NumROI = MOS_MIN(vaEncMiscParamROI->num_roi, ENCODE_VDENC_AVC_MAX_ROI_NUMBER_ADV);
662 picParams->MaxDeltaQp = vaEncMiscParamROI->max_delta_qp;
663 picParams->MinDeltaQp = vaEncMiscParamROI->min_delta_qp;
664
665 uint8_t mbHeightScaleFactor = picParams->FieldCodingFlag ? CODECHAL_ENCODE_AVC_ROI_FIELD_HEIGHT_SCALE_FACTOR : CODECHAL_ENCODE_AVC_ROI_FRAME_HEIGHT_SCALE_FACTOR;
666
667 // Process information for all ROIs and pass to codecHAL
668 for (uint8_t i = 0; i < picParams->NumROI; i++)
669 {
670 DDI_CODEC_CHK_NULL(vaEncMiscParamROI->roi, "nullptr vaEncMiscParamROI->roi", VA_STATUS_ERROR_INVALID_PARAMETER);
671 DDI_CODEC_CHK_NULL(roi, "nullptr roi", VA_STATUS_ERROR_INVALID_PARAMETER);
672
673 // Check and adjust if ROI not within frame boundaries
674 roi->Left = MOS_MIN(MOS_MAX(vaEncMiscParamROI->roi->roi_rectangle.x, 0), seqParams->FrameWidth - 1);
675 roi->Top = MOS_MIN(MOS_MAX(vaEncMiscParamROI->roi->roi_rectangle.y, 0), seqParams->FrameHeight - 1);
676 roi->Right = MOS_MIN(roi->Left + vaEncMiscParamROI->roi->roi_rectangle.width, seqParams->FrameWidth - 1);
677 roi->Bottom = MOS_MIN(roi->Top + vaEncMiscParamROI->roi->roi_rectangle.height, seqParams->FrameHeight - 1);
678
679 // Convert from pixel units to MB units
680 roi->Left /= CODECHAL_ENCODE_AVC_ROI_WIDTH_SCALE_FACTOR;
681 roi->Top /= mbHeightScaleFactor;
682 roi->Right = (roi->Right + CODECHAL_ENCODE_AVC_ROI_WIDTH_SCALE_FACTOR - 1) / CODECHAL_ENCODE_AVC_ROI_WIDTH_SCALE_FACTOR;
683 roi->Bottom = (roi->Bottom + mbHeightScaleFactor - 1) / mbHeightScaleFactor;
684
685 roi->PriorityLevelOrDQp = vaEncMiscParamROI->roi->roi_value;
686
687 // Move to next ROI
688 vaEncMiscParamROI->roi++;
689 roi++;
690 }
691 #ifndef ANDROID
692 seqParams->ROIValueInDeltaQP = vaEncMiscParamROI->roi_flags.bits.roi_value_is_qp_delta;
693 if(picParams->NumROI != 0 && seqParams->ROIValueInDeltaQP == 0)
694 {
695 DDI_CODEC_ASSERTMESSAGE("ROI does not support priority level now.");
696 return VA_STATUS_ERROR_INVALID_PARAMETER;
697 }
698 #endif
699 return VA_STATUS_SUCCESS;
700 }
701
702 VAStatus DdiEncodeAvc::ParseMiscParamMaxSliceSize(void *data)
703 {
704 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
705 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
706
707 m_encodeCtx->EnableSliceLevelRateCtrl = true;
708
709 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)(m_encodeCtx->pPicParams) + current_pic_parameter_set_id;
710 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams) + current_seq_parameter_set_id;
711 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
712 DDI_CODEC_CHK_NULL(seqParams, "nullptr seqParams", VA_STATUS_ERROR_INVALID_PARAMETER);
713
714 seqParams->EnableSliceLevelRateCtrl = m_encodeCtx->EnableSliceLevelRateCtrl;
715 VAEncMiscParameterMaxSliceSize *vaEncMiscParamMaxSliceSize = (VAEncMiscParameterMaxSliceSize *)data;
716
717 picParams->SliceSizeInBytes = vaEncMiscParamMaxSliceSize->max_slice_size;
718
719 return VA_STATUS_SUCCESS;
720 }
721
722 VAStatus DdiEncodeAvc::ParseMiscParamRounding(void *data)
723 {
724 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
725 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
726
727 VAEncMiscParameterCustomRoundingControl *vaEncMiscParamRounding = (VAEncMiscParameterCustomRoundingControl *)data;
728
729 if (vaEncMiscParamRounding->rounding_offset_setting.bits.enable_custom_rouding_intra)
730 {
731 m_roundingParams->bEnableCustomRoudingIntra = vaEncMiscParamRounding->rounding_offset_setting.bits.enable_custom_rouding_intra;
732 m_roundingParams->dwRoundingIntra = vaEncMiscParamRounding->rounding_offset_setting.bits.rounding_offset_intra;
733 }
734 if (vaEncMiscParamRounding->rounding_offset_setting.bits.enable_custom_rounding_inter)
735 {
736 m_roundingParams->bEnableCustomRoudingInter = vaEncMiscParamRounding->rounding_offset_setting.bits.enable_custom_rounding_inter;
737 m_roundingParams->dwRoundingInter = vaEncMiscParamRounding->rounding_offset_setting.bits.rounding_offset_inter;
738 }
739
740 return VA_STATUS_SUCCESS;
741 }
742
743 VAStatus DdiEncodeAvc::ParseMiscParamSubMbPartPel(void *data)
744 {
745 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
746 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_PARAMETER);
747
748 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)(m_encodeCtx->pPicParams) + current_pic_parameter_set_id;
749 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
750
751 VAEncMiscParameterSubMbPartPelH264 *vaEncMiscParamSubMbPartPel = (VAEncMiscParameterSubMbPartPelH264*)data;
752 if (vaEncMiscParamSubMbPartPel->disable_inter_sub_mb_partition)
753 {
754 picParams->bEnableSubMbPartMask = true;
755 //Inter 16x16 can't be disabled
756 picParams->SubMbPartMask = vaEncMiscParamSubMbPartPel->inter_sub_mb_partition_mask.value & DisMbPartMask;
757 }
758
759 if (vaEncMiscParamSubMbPartPel->enable_sub_pel_mode)
760 {
761 picParams->bEnableSubPelMode = true;
762 picParams->SubPelMode = vaEncMiscParamSubMbPartPel->sub_pel_mode & SubpelModeMask;
763 if (picParams->SubPelMode == SubpelModeReserved)
764 {
765 //When Quarter-pel mode is enabled, Half-pel must be enabled as well.
766 picParams->SubPelMode = SubpelModeQuant;
767 }
768 }
769
770 return VA_STATUS_SUCCESS;
771 }
772
773 VAStatus DdiEncodeAvc::ContextInitialize(CodechalSetting * codecHalSettings)
774 {
775 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
776 DDI_CODEC_CHK_NULL(m_encodeCtx->pCpDdiInterfaceNext, "nullptr m_encodeCtx->pCpDdiInterfaceNext.", VA_STATUS_ERROR_INVALID_CONTEXT);
777 DDI_CODEC_CHK_NULL(codecHalSettings, "nullptr codecHalSettings.", VA_STATUS_ERROR_INVALID_CONTEXT);
778
779 codecHalSettings->codecFunction = CODECHAL_FUNCTION_ENC_VDENC_PAK;
780 codecHalSettings->width = m_encodeCtx->dworiFrameWidth;
781 codecHalSettings->height = m_encodeCtx->dworiFrameHeight;
782 codecHalSettings->mode = m_encodeCtx->wModeType;
783 codecHalSettings->standard = CODECHAL_AVC;
784
785 m_encodeCtx->pSeqParams = (void *)MOS_AllocAndZeroMemory(CODEC_AVC_MAX_SPS_NUM * sizeof(CODEC_AVC_ENCODE_SEQUENCE_PARAMS));
786 DDI_CODEC_CHK_NULL(m_encodeCtx->pSeqParams, "nullptr m_encodeCtx->pSeqParams", VA_STATUS_ERROR_ALLOCATION_FAILED);
787
788 m_encodeCtx->pPicParams = (void *)MOS_AllocAndZeroMemory(CODEC_AVC_MAX_PPS_NUM * sizeof(CODEC_AVC_ENCODE_PIC_PARAMS));
789 DDI_CODEC_CHK_NULL(m_encodeCtx->pPicParams, "nullptr m_encodeCtx->pPicParams", VA_STATUS_ERROR_ALLOCATION_FAILED);
790
791 // Allocate NAL unit params
792 m_encodeCtx->ppNALUnitParams = (PCODECHAL_NAL_UNIT_PARAMS *)MOS_AllocAndZeroMemory(sizeof(PCODECHAL_NAL_UNIT_PARAMS) * CODECHAL_ENCODE_AVC_MAX_NAL_TYPE);
793 DDI_CODEC_CHK_NULL(m_encodeCtx->ppNALUnitParams, "nullptr m_encodeCtx->ppNALUnitParams", VA_STATUS_ERROR_ALLOCATION_FAILED);
794
795 PCODECHAL_NAL_UNIT_PARAMS nalUnitParams = (PCODECHAL_NAL_UNIT_PARAMS)MOS_AllocAndZeroMemory(sizeof(CODECHAL_NAL_UNIT_PARAMS) * CODECHAL_ENCODE_AVC_MAX_NAL_TYPE);
796 DDI_CODEC_CHK_NULL(nalUnitParams, "nullptr nalUnitParams", VA_STATUS_ERROR_ALLOCATION_FAILED);
797
798 for (int32_t i = 0; i < CODECHAL_ENCODE_AVC_MAX_NAL_TYPE; i++)
799 {
800 m_encodeCtx->ppNALUnitParams[i] = &(nalUnitParams[i]);
801 }
802
803 VAStatus status = m_encodeCtx->pCpDdiInterfaceNext->ParseCpParamsForEncode();
804 if (VA_STATUS_SUCCESS != status)
805 {
806 return status;
807 }
808
809 m_encodeCtx->pVuiParams = (void *)MOS_AllocAndZeroMemory(sizeof(CODECHAL_ENCODE_AVC_VUI_PARAMS));
810 DDI_CODEC_CHK_NULL(m_encodeCtx->pVuiParams, "nullptr m_encodeCtx->pVuiParams", VA_STATUS_ERROR_ALLOCATION_FAILED);
811
812 // Allocate SliceParams
813 // Supports equal row slices.
814 m_encodeCtx->pSliceParams = (void *)MOS_AllocAndZeroMemory(ENCODE_AVC_MAX_SLICES_SUPPORTED * sizeof(CODEC_AVC_ENCODE_SLICE_PARAMS));
815 DDI_CODEC_CHK_NULL(m_encodeCtx->pSliceParams, "nullptr m_encodeCtx->pSliceParams", VA_STATUS_ERROR_ALLOCATION_FAILED);
816
817 // Allocate Encode Status Report
818 m_encodeCtx->pEncodeStatusReport = (void *)MOS_AllocAndZeroMemory(CODECHAL_ENCODE_STATUS_NUM * sizeof(EncodeStatusReportData));
819 DDI_CODEC_CHK_NULL(m_encodeCtx->pEncodeStatusReport, "nullptr m_encodeCtx->pEncodeStatusReport", VA_STATUS_ERROR_ALLOCATION_FAILED);
820
821 // Allocate SEI structure
822 m_encodeCtx->pSEIFromApp = (CodechalEncodeSeiData*)MOS_AllocAndZeroMemory(sizeof(CodechalEncodeSeiData));
823 DDI_CODEC_CHK_NULL(m_encodeCtx->pSEIFromApp, "nullptr m_encodeCtx->pSEIFromApp", VA_STATUS_ERROR_ALLOCATION_FAILED);
824
825 // for slice header from application
826 m_encodeCtx->pSliceHeaderData = (PCODEC_ENCODER_SLCDATA)MOS_AllocAndZeroMemory(ENCODE_AVC_MAX_SLICES_SUPPORTED *
827 sizeof(CODEC_ENCODER_SLCDATA));
828 DDI_CODEC_CHK_NULL(m_encodeCtx->pSliceHeaderData, "nullptr m_encodeCtx->pSliceHeaderData", VA_STATUS_ERROR_ALLOCATION_FAILED);
829
830 // Create the bit stream buffer to hold the packed headers from application
831 m_encodeCtx->pbsBuffer = (PBSBuffer)MOS_AllocAndZeroMemory(sizeof(BSBuffer));
832 DDI_CODEC_CHK_NULL(m_encodeCtx->pbsBuffer, "nullptr m_encodeCtx->pbsBuffer", VA_STATUS_ERROR_ALLOCATION_FAILED);
833
834 m_encodeCtx->pbsBuffer->pBase = (uint8_t *)MOS_AllocAndZeroMemory(ENCODE_AVC_MAX_SLICES_SUPPORTED * PACKED_HEADER_SIZE_PER_ROW);
835 DDI_CODEC_CHK_NULL(m_encodeCtx->pbsBuffer->pBase, "nullptr m_encodeCtx->pbsBuffer->pBase", VA_STATUS_ERROR_ALLOCATION_FAILED);
836
837 m_encodeCtx->pbsBuffer->BufferSize = ENCODE_AVC_MAX_SLICES_SUPPORTED * PACKED_HEADER_SIZE_PER_ROW;
838
839 m_qcParams = (CODECHAL_ENCODE_AVC_QUALITY_CTRL_PARAMS *)MOS_AllocAndZeroMemory(sizeof(CODECHAL_ENCODE_AVC_QUALITY_CTRL_PARAMS));
840 DDI_CODEC_CHK_NULL(m_qcParams, "nullptr m_qcParams", VA_STATUS_ERROR_ALLOCATION_FAILED);
841
842 m_roundingParams = (CODECHAL_ENCODE_AVC_ROUNDING_PARAMS *)MOS_AllocAndZeroMemory(sizeof(CODECHAL_ENCODE_AVC_ROUNDING_PARAMS));
843 DDI_CODEC_CHK_NULL(m_roundingParams, "nullptr m_roundingParams", VA_STATUS_ERROR_ALLOCATION_FAILED);
844
845 iqMatrixParams = (PCODEC_AVC_IQ_MATRIX_PARAMS)MOS_AllocAndZeroMemory(sizeof(CODEC_AVC_IQ_MATRIX_PARAMS));
846 DDI_CODEC_CHK_NULL(iqMatrixParams, "nullptr iqMatrixParams", VA_STATUS_ERROR_ALLOCATION_FAILED);
847
848 iqWeightScaleLists = (PCODEC_AVC_ENCODE_IQ_WEIGTHSCALE_LISTS)MOS_AllocAndZeroMemory(sizeof(CODEC_AVC_ENCODE_IQ_WEIGTHSCALE_LISTS));
849 DDI_CODEC_CHK_NULL(iqWeightScaleLists, "nullptr iqWeightScaleLists", VA_STATUS_ERROR_ALLOCATION_FAILED);
850
851 return VA_STATUS_SUCCESS;
852 }
853
854 VAStatus DdiEncodeAvc::RenderPicture(
855 VADriverContextP ctx,
856 VAContextID context,
857 VABufferID *buffers,
858 int32_t numBuffers)
859 {
860 DDI_CODEC_FUNC_ENTER;
861
862 DDI_CODEC_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
863 PDDI_MEDIA_CONTEXT mediaCtx = GetMediaContext(ctx);
864 DDI_CODEC_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
865
866 // assume the VAContextID is encoder ID
867 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
868
869 uint32_t numSlices = 0;
870 VAStatus vaStatus = VA_STATUS_SUCCESS;
871
872 DDI_MEDIA_BUFFER *buf;
873 void *data;
874 uint32_t dataSize;
875 for (int32_t i = 0; i < numBuffers; i++)
876 {
877 buf = MediaLibvaCommonNext::GetBufferFromVABufferID(mediaCtx, buffers[i]);
878 DDI_CODEC_CHK_NULL(buf, "Invalid buffer.", VA_STATUS_ERROR_INVALID_BUFFER);
879 if (buf->uiType == VAEncMacroblockDisableSkipMapBufferType)
880 {
881 MediaLibvaCommonNext::MediaBufferToMosResource(buf, &(m_encodeCtx->resPerMBSkipMapBuffer));
882 m_encodeCtx->bMbDisableSkipMapEnabled = true;
883 continue;
884 }
885 dataSize = buf->iSize;
886 // can use internal function instead of MediaLibvaInterfaceNext::MapBuffer here?
887 MediaLibvaInterfaceNext::MapBuffer(ctx, buffers[i], &data);
888
889 DDI_CODEC_CHK_NULL(data, "nullptr data", VA_STATUS_ERROR_INVALID_BUFFER);
890
891 switch (buf->uiType)
892 {
893 case VAIQMatrixBufferType:
894 case VAQMatrixBufferType:
895 DDI_CHK_STATUS(Qmatrix(data), VA_STATUS_ERROR_INVALID_BUFFER);
896 break;
897
898 case VAEncSequenceParameterBufferType:
899 DDI_CHK_STATUS(ParseSeqParams(data), VA_STATUS_ERROR_INVALID_BUFFER);
900 m_encodeCtx->bNewSeq = true;
901 break;
902
903 case VAEncPictureParameterBufferType:
904 DDI_CHK_STATUS(ParsePicParams(mediaCtx, data), VA_STATUS_ERROR_INVALID_BUFFER);
905
906 DDI_CHK_STATUS(
907 AddToStatusReportQueue((void *)m_encodeCtx->resBitstreamBuffer.bo),
908 VA_STATUS_ERROR_INVALID_BUFFER);
909 break;
910
911 case VAEncSliceParameterBufferType:
912 numSlices = buf->uiNumElements;
913 DDI_CHK_STATUS(ParseSlcParams(mediaCtx, data, numSlices), VA_STATUS_ERROR_INVALID_BUFFER);
914 break;
915
916 case VAEncPackedHeaderParameterBufferType:
917 DDI_CHK_STATUS(ParsePackedHeaderParams(data), VA_STATUS_ERROR_INVALID_BUFFER);
918 break;
919
920 case VAEncPackedHeaderDataBufferType:
921 DDI_CHK_STATUS(ParsePackedHeaderData(data), VA_STATUS_ERROR_INVALID_BUFFER);
922 break;
923
924 case VAEncMiscParameterBufferType:
925 DDI_CHK_STATUS(ParseMiscParams(data), VA_STATUS_ERROR_INVALID_BUFFER);
926 break;
927
928 case VAEncQPBufferType:
929 MediaLibvaCommonNext::MediaBufferToMosResource(buf, &m_encodeCtx->resMBQpBuffer);
930 m_encodeCtx->bMBQpEnable = true;
931 break;
932
933 default:
934 if(m_encodeCtx->pCpDdiInterfaceNext)
935 {
936 vaStatus = m_encodeCtx->pCpDdiInterfaceNext->RenderCencPicture(ctx, context, buf, data);
937 }
938 else
939 {
940 vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
941 }
942 break;
943 }
944 MediaLibvaInterfaceNext::UnmapBuffer(ctx, buffers[i]);
945 }
946
947 return vaStatus;
948 }
949
950 VAStatus DdiEncodeAvc::EncodeInCodecHal(uint32_t numSlices)
951 {
952 uint8_t ppsIdx, spsIdx;
953 PCODEC_AVC_ENCODE_PIC_PARAMS picParams;
954
955 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
956 DDI_CODEC_CHK_NULL(m_encodeCtx->pMediaCtx, "nullptr m_encodeCtx->pMediaCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
957 DDI_CODEC_CHK_NULL(m_encodeCtx->pCpDdiInterfaceNext, "nullptr m_encodeCtx->pCpDdiInterfaceNext.", VA_STATUS_ERROR_INVALID_PARAMETER);
958
959 DDI_CODEC_RENDER_TARGET_TABLE *rtTbl = &(m_encodeCtx->RTtbl);
960
961 EncoderParams *encodeParams = &m_encodeCtx->EncodeParams;
962 MOS_ZeroMemory(encodeParams, sizeof(EncoderParams));
963
964 encodeParams->newSeqHeader = m_newSeqHeader;
965 encodeParams->newPpsHeader = m_newPpsHeader;
966 encodeParams->arbitraryNumMbsInSlice = m_arbitraryNumMbsInSlice;
967
968 encodeParams->ExecCodecFunction = CODECHAL_FUNCTION_ENC_VDENC_PAK;
969
970 // Raw Surface
971 PMOS_SURFACE rawSurface = &encodeParams->rawSurface;
972 rawSurface->Format = Format_NV12;
973 rawSurface->dwOffset = 0;
974
975 MediaLibvaCommonNext::MediaSurfaceToMosResource(rtTbl->pCurrentRT, &(rawSurface->OsResource));
976
977 PMOS_INTERFACE osInterface = m_encodeCtx->pCodecHal->GetOsInterface();
978 m_encodeCtx->pCpDdiInterfaceNext->SetInputResourceEncryption(osInterface, &(rawSurface->OsResource));
979
980 // Recon Surface
981 PMOS_SURFACE reconSurface = &encodeParams->reconSurface;
982 reconSurface->Format = Format_NV12;
983 reconSurface->dwOffset = 0;
984
985 MediaLibvaCommonNext::MediaSurfaceToMosResource(rtTbl->pCurrentReconTarget, &(reconSurface->OsResource));
986
987 //clear registered recon/ref surface flags
988 DDI_CODEC_CHK_RET(ClearRefList(&m_encodeCtx->RTtbl, false), "ClearRefList failed!");
989
990 // Bitstream surface
991 PMOS_RESOURCE bitstreamSurface = &encodeParams->resBitstreamBuffer;
992 *bitstreamSurface = m_encodeCtx->resBitstreamBuffer; // in render picture
993 bitstreamSurface->Format = Format_Buffer;
994
995 encodeParams->psRawSurface = &encodeParams->rawSurface;
996 encodeParams->psReconSurface = &encodeParams->reconSurface;
997 encodeParams->presBitstreamBuffer = &encodeParams->resBitstreamBuffer;
998
999 PMOS_SURFACE mbQpSurface = &encodeParams->mbQpSurface;
1000 if (m_encodeCtx->bMBQpEnable)
1001 {
1002 mbQpSurface->Format = Format_Buffer_2D;
1003 mbQpSurface->dwOffset = 0;
1004 mbQpSurface->OsResource = m_encodeCtx->resMBQpBuffer;
1005 encodeParams->psMbQpDataSurface = &encodeParams->mbQpSurface;
1006 encodeParams->bMbQpDataEnabled = true;
1007 }
1008
1009 PMOS_SURFACE disableSkipMapSurface = &encodeParams->disableSkipMapSurface;
1010 encodeParams->bMbDisableSkipMapEnabled = m_encodeCtx->bMbDisableSkipMapEnabled;
1011 if (encodeParams->bMbDisableSkipMapEnabled)
1012 {
1013 disableSkipMapSurface->Format = Format_Buffer;
1014 disableSkipMapSurface->dwOffset = 0;
1015 disableSkipMapSurface->OsResource = m_encodeCtx->resPerMBSkipMapBuffer;
1016 encodeParams->psMbDisableSkipMapSurface = &encodeParams->disableSkipMapSurface;
1017 }
1018
1019 // correct some params
1020 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams;
1021 CODECHAL_ENCODE_AVC_VUI_PARAMS *vuiParam;
1022
1023 vuiParam = (CODECHAL_ENCODE_AVC_VUI_PARAMS *)m_encodeCtx->pVuiParams;
1024 seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams);
1025 seqParams->TargetUsage = m_encodeCtx->targetUsage;
1026 if (VA_RC_CQP == m_encodeCtx->uiRCMethod)
1027 {
1028 vuiParam->bit_rate_value_minus1[0] = 0;
1029 vuiParam->cpb_size_value_minus1[0] = 0;
1030 seqParams->TargetBitRate = 0;
1031 seqParams->MaxBitRate = 0;
1032 seqParams->MinBitRate = 0;
1033 seqParams->InitVBVBufferFullnessInBit = 0;
1034 seqParams->VBVBufferSizeInBit = 0;
1035 }
1036
1037 encodeParams->uiSlcStructCaps = CODECHAL_SLICE_STRUCT_ARBITRARYMBSLICE;
1038
1039 ppsIdx = ((PCODEC_AVC_ENCODE_SLICE_PARAMS)(m_encodeCtx->pSliceParams))->pic_parameter_set_id;
1040 picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)m_encodeCtx->pPicParams + ppsIdx;
1041 spsIdx = picParams->seq_parameter_set_id;
1042 encodeParams->pSeqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)m_encodeCtx->pSeqParams + spsIdx;
1043 encodeParams->pPicParams = picParams;
1044
1045 encodeParams->pVuiParams = m_encodeCtx->pVuiParams;
1046 encodeParams->pSliceParams = m_encodeCtx->pSliceParams;
1047 encodeParams->pAVCQCParams = m_qcParams;
1048 encodeParams->pAVCRoundingParams = m_roundingParams;
1049
1050 // Sequence data
1051 encodeParams->bNewSeq = m_encodeCtx->bNewSeq;
1052 // VUI
1053 encodeParams->bNewVuiData = m_encodeCtx->bNewVuiData;
1054
1055 // Slice level data
1056 encodeParams->dwNumSlices = numSlices;
1057
1058 // IQmatrix params
1059 encodeParams->bNewQmatrixData = m_encodeCtx->bNewQmatrixData;
1060 encodeParams->bPicQuant = m_encodeCtx->bPicQuant;
1061 encodeParams->ppNALUnitParams = m_encodeCtx->ppNALUnitParams;
1062 encodeParams->pSeiData = m_encodeCtx->pSEIFromApp;
1063 encodeParams->pSeiParamBuffer = m_encodeCtx->pSEIFromApp->pSEIBuffer;
1064 encodeParams->dwSEIDataOffset = 0;
1065
1066 MOS_STATUS status = MOS_SecureMemcpy(&iqMatrixParams->ScalingList4x4,
1067 6 * 16 * sizeof(uint8_t),
1068 &m_scalingLists4x4,
1069 6 * 16 * sizeof(uint8_t));
1070 if (MOS_STATUS_SUCCESS != status)
1071 {
1072 DDI_CODEC_ASSERTMESSAGE("DDI:Failed to copy scaling list 4x4!");
1073 return VA_STATUS_ERROR_INVALID_PARAMETER;
1074 }
1075
1076 status = MOS_SecureMemcpy(&iqMatrixParams->ScalingList8x8,
1077 2 * 64 * sizeof(uint8_t),
1078 &m_scalingLists8x8,
1079 2 * 64 * sizeof(uint8_t));
1080 if (MOS_STATUS_SUCCESS != status)
1081 {
1082 DDI_CODEC_ASSERTMESSAGE("DDI:Failed to copy scaling list 8x8!");
1083 return VA_STATUS_ERROR_INVALID_PARAMETER;
1084 }
1085 encodeParams->pIQMatrixBuffer = iqMatrixParams;
1086
1087 status = MOS_SecureMemcpy(&iqWeightScaleLists->WeightScale4x4,
1088 (CODEC_AVC_WEIGHT_SCALE_4x4 * sizeof(uint32_t)),
1089 &m_weightScale4x4,
1090 (CODEC_AVC_WEIGHT_SCALE_4x4 * sizeof(uint32_t)));
1091 if (MOS_STATUS_SUCCESS != status)
1092 {
1093 DDI_CODEC_ASSERTMESSAGE("DDI:Failed to copy weight scale list 4x4!");
1094 return VA_STATUS_ERROR_INVALID_PARAMETER;
1095 }
1096
1097 status = MOS_SecureMemcpy(&iqWeightScaleLists->WeightScale8x8,
1098 (CODEC_AVC_WEIGHT_SCALE_8x8 * sizeof(uint32_t)),
1099 &m_weightScale8x8,
1100 (CODEC_AVC_WEIGHT_SCALE_8x8 * sizeof(uint32_t)));
1101 if (MOS_STATUS_SUCCESS != status)
1102 {
1103 DDI_CODEC_ASSERTMESSAGE("DDI:Failed to copy weight scale list 8x8!");
1104 return VA_STATUS_ERROR_INVALID_PARAMETER;
1105 }
1106 encodeParams->pIQWeightScaleLists = iqWeightScaleLists;
1107
1108 // whether driver need to pack slice header
1109 if (true == m_encodeCtx->bHavePackedSliceHdr)
1110 {
1111 encodeParams->bAcceleratorHeaderPackingCaps = false;
1112 }
1113 else
1114 {
1115 encodeParams->bAcceleratorHeaderPackingCaps = true;
1116 }
1117
1118 encodeParams->pBSBuffer = m_encodeCtx->pbsBuffer;
1119 encodeParams->pSlcHeaderData = (void *)m_encodeCtx->pSliceHeaderData;
1120
1121 if (m_encodeCtx->pCodecHal->IsApogeiosEnabled())
1122 {
1123 status = m_encodeCtx->pCodecHal->Execute(encodeParams);
1124 if (MOS_STATUS_SUCCESS != status)
1125 {
1126 DDI_CODEC_ASSERTMESSAGE("DDI:Failed in Codechal!");
1127 return VA_STATUS_ERROR_ENCODING_ERROR;
1128 }
1129 }
1130 else
1131 {
1132 DDI_CODEC_ASSERTMESSAGE("DDI:Failed in Codechal!");
1133 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
1134 }
1135
1136 return VA_STATUS_SUCCESS;
1137 }
1138
1139 VAStatus DdiEncodeAvc::ResetAtFrameLevel()
1140 {
1141 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1142
1143 // Assume there is only one SPS parameter
1144 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams);
1145 seqParams->bInitBRC = 0x0;
1146 seqParams->bResetBRC = 0x0;
1147
1148 m_encodeCtx->dwNumSlices = 0x0;
1149 m_encodeCtx->indexNALUnit = 0x0;
1150 m_encodeCtx->uiSliceHeaderCnt = 0x0;
1151
1152 // reset bsbuffer every frame
1153 m_encodeCtx->pbsBuffer->pCurrent = m_encodeCtx->pbsBuffer->pBase;
1154 m_encodeCtx->pbsBuffer->SliceOffset = 0x0;
1155 m_encodeCtx->pbsBuffer->BitOffset = 0x0;
1156 m_encodeCtx->pbsBuffer->BitSize = 0x0;
1157
1158 // clear the packed header information
1159 if (nullptr != m_encodeCtx->ppNALUnitParams)
1160 {
1161 MOS_ZeroMemory(m_encodeCtx->ppNALUnitParams[0], sizeof(CODECHAL_NAL_UNIT_PARAMS) * CODECHAL_ENCODE_AVC_MAX_NAL_TYPE);
1162 }
1163
1164 m_encodeCtx->bHavePackedSliceHdr = false;
1165 m_encodeCtx->bLastPackedHdrIsSlice = false;
1166 m_encodeCtx->bMbDisableSkipMapEnabled = false;
1167 m_encodeCtx->bMBQpEnable = false;
1168
1169 if (nullptr != m_roundingParams)
1170 {
1171 MOS_ZeroMemory(m_roundingParams, sizeof(CODECHAL_ENCODE_AVC_ROUNDING_PARAMS));
1172 }
1173
1174 return VA_STATUS_SUCCESS;
1175 }
1176
1177 VAStatus DdiEncodeAvc::Qmatrix(void *ptr)
1178 {
1179 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1180 DDI_CODEC_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER);
1181
1182 VAIQMatrixBufferH264 *qm = (VAIQMatrixBufferH264 *)ptr;
1183
1184 MOS_STATUS status = MOS_SecureMemcpy((void *)&m_scalingLists4x4,
1185 6 * 16 * sizeof(uint8_t),
1186 (void *)&qm->ScalingList4x4,
1187 6 * 16 * sizeof(uint8_t));
1188 if (MOS_STATUS_SUCCESS != status)
1189 {
1190 DDI_CODEC_ASSERTMESSAGE("Failed to copy QM scaling list 4x4.");
1191 return VA_STATUS_ERROR_INVALID_PARAMETER;
1192 }
1193
1194 status = MOS_SecureMemcpy((void *)&m_scalingLists8x8,
1195 2 * 64 * sizeof(uint8_t),
1196 (void *)&qm->ScalingList8x8,
1197 2 * 64 * sizeof(uint8_t));
1198 if (MOS_STATUS_SUCCESS != status)
1199 {
1200 DDI_CODEC_ASSERTMESSAGE("Failed to copy QM scaling list 8x8.");
1201 return VA_STATUS_ERROR_INVALID_PARAMETER;
1202 }
1203
1204 uint8_t idx1, idx2;
1205 // 4x4 block
1206 for (idx2 = 0; idx2 < 6; idx2++)
1207 {
1208 for (idx1 = 0; idx1 < 16; idx1++)
1209 {
1210 m_weightScale4x4[idx2][CODEC_AVC_Qmatrix_scan_4x4[idx1]] = qm->ScalingList4x4[idx2][idx1];
1211 }
1212 }
1213 // 8x8 block
1214 for (idx2 = 0; idx2 < 2; idx2++)
1215 {
1216 for (idx1 = 0; idx1 < 64; idx1++)
1217 {
1218 m_weightScale8x8[idx2][CODEC_AVC_Qmatrix_scan_8x8[idx1]] = qm->ScalingList8x8[idx2][idx1];
1219 }
1220 }
1221
1222 return VA_STATUS_SUCCESS;
1223 }
1224
1225 VAStatus DdiEncodeAvc::ParseSeqParams(void *ptr)
1226 {
1227 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1228 DDI_CODEC_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER);
1229
1230 VAEncSequenceParameterBufferH264 *seq = (VAEncSequenceParameterBufferH264 *)ptr;
1231 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)((uint8_t *)m_encodeCtx->pSeqParams + seq->seq_parameter_set_id * sizeof(CODEC_AVC_ENCODE_SEQUENCE_PARAMS));
1232
1233 if (seq->seq_parameter_set_id >= CODEC_AVC_MAX_SPS_NUM)
1234 {
1235 return VA_STATUS_ERROR_INVALID_PARAMETER;
1236 }
1237
1238 seqParams->FrameWidth = seq->picture_width_in_mbs * 16;
1239 seqParams->FrameHeight = seq->picture_height_in_mbs * 16;
1240 if ((seq->picture_width_in_mbs <= m_encodeCtx->wContextPicWidthInMB) && (seq->picture_height_in_mbs <= m_encodeCtx->wContextPicHeightInMB) && (seq->picture_width_in_mbs != m_encodeCtx->wOriPicWidthInMB) && (seq->picture_height_in_mbs != m_encodeCtx->wOriPicHeightInMB))
1241 {
1242 m_encodeCtx->wPicWidthInMB = seq->picture_width_in_mbs;
1243 m_encodeCtx->wPicHeightInMB = seq->picture_height_in_mbs;
1244 m_encodeCtx->wOriPicWidthInMB = m_encodeCtx->wPicWidthInMB;
1245 m_encodeCtx->wOriPicHeightInMB = m_encodeCtx->wPicHeightInMB;
1246 seqParams->bInitBRC = true;
1247 }
1248 else if ((seq->picture_width_in_mbs > m_encodeCtx->wContextPicWidthInMB) || (seq->picture_height_in_mbs > m_encodeCtx->wContextPicHeightInMB))
1249 {
1250 return VA_STATUS_ERROR_INVALID_PARAMETER;
1251 }
1252
1253 seqParams->Profile = GetAVCProfileFromVAProfile();
1254 seqParams->Level = seq->level_idc;
1255 seqParams->seq_parameter_set_id = seq->seq_parameter_set_id;
1256 seqParams->chroma_format_idc = 1;
1257
1258 seqParams->bit_depth_luma_minus8 = seq->bit_depth_luma_minus8;
1259 seqParams->bit_depth_chroma_minus8 = seq->bit_depth_chroma_minus8;
1260
1261 seqParams->GopPicSize = seq->intra_period ? seq->intra_period : seq->intra_idr_period;
1262 seqParams->GopRefDist = seq->ip_period;
1263 seqParams->RateControlMethod = VARC2HalRC(m_encodeCtx->uiRCMethod);
1264
1265 seqParams->TargetBitRate = seq->bits_per_second;
1266 seqParams->MaxBitRate = seq->bits_per_second;
1267 seqParams->MinBitRate = seq->bits_per_second;
1268 // fps it set to 30 by default, can be overwritten by misc paramter
1269 if(!seqParams->FramesPer100Sec)
1270 {
1271 seqParams->FramesPer100Sec = 3000;
1272 }
1273
1274
1275 // set default same as MSDK, can be overwritten by HRD params
1276 seqParams->InitVBVBufferFullnessInBit = seq->bits_per_second;
1277 seqParams->VBVBufferSizeInBit = seq->bits_per_second << 1;
1278
1279 seqParams->NumRefFrames = seq->max_num_ref_frames;
1280
1281 seqParams->log2_max_frame_num_minus4 = seq->seq_fields.bits.log2_max_frame_num_minus4;
1282 seqParams->pic_order_cnt_type = seq->seq_fields.bits.pic_order_cnt_type;
1283 seqParams->log2_max_pic_order_cnt_lsb_minus4 = seq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4;
1284 seqParams->num_ref_frames_in_pic_order_cnt_cycle = seq->num_ref_frames_in_pic_order_cnt_cycle;
1285 seqParams->delta_pic_order_always_zero_flag = seq->seq_fields.bits.delta_pic_order_always_zero_flag;
1286 seqParams->direct_8x8_inference_flag = seq->seq_fields.bits.direct_8x8_inference_flag;
1287 seqParams->vui_parameters_present_flag = seq->vui_parameters_present_flag;
1288 seqParams->frame_mbs_only_flag = seq->seq_fields.bits.frame_mbs_only_flag;
1289 seqParams->offset_for_non_ref_pic = seq->offset_for_non_ref_pic;
1290 seqParams->offset_for_top_to_bottom_field = seq->offset_for_top_to_bottom_field;
1291 for (uint8_t i = 0; i < seqParams->num_ref_frames_in_pic_order_cnt_cycle; i++)
1292 {
1293 seqParams->offset_for_ref_frame[i] = seq->offset_for_ref_frame[i];
1294 }
1295
1296 seqParams->frame_cropping_flag = seq->frame_cropping_flag;
1297 seqParams->frame_crop_bottom_offset = seq->frame_crop_bottom_offset;
1298 seqParams->frame_crop_left_offset = seq->frame_crop_left_offset;
1299 seqParams->frame_crop_right_offset = seq->frame_crop_right_offset;
1300 seqParams->frame_crop_top_offset = seq->frame_crop_top_offset;
1301
1302 // set up VUI parameter if rate control is enabled
1303 if (seqParams->vui_parameters_present_flag)
1304 {
1305 CODECHAL_ENCODE_AVC_VUI_PARAMS *vuiParam;
1306
1307 vuiParam = (CODECHAL_ENCODE_AVC_VUI_PARAMS *)m_encodeCtx->pVuiParams;
1308 vuiParam->nal_hrd_parameters_present_flag = 1;
1309 vuiParam->cpb_cnt_minus1 = 0;
1310 vuiParam->bit_rate_scale = 0;
1311 vuiParam->cpb_size_scale = 6;
1312 vuiParam->bit_rate_value_minus1[0] = MOS_ROUNDUP_SHIFT(seq->bits_per_second, 6 + vuiParam->bit_rate_scale) - 1;
1313 vuiParam->cpb_size_value_minus1[0] = MOS_ROUNDUP_DIVIDE(seqParams->VBVBufferSizeInBit, VuiKbps) - 1;
1314 vuiParam->cbr_flag = 0x0;
1315 vuiParam->initial_cpb_removal_delay_length_minus1 = 23;
1316 vuiParam->cpb_removal_delay_length_minus1 = 23;
1317 vuiParam->dpb_output_delay_length_minus1 = 23;
1318 vuiParam->time_offset_length = 24;
1319
1320 vuiParam->timing_info_present_flag = seq->vui_fields.bits.timing_info_present_flag;
1321 vuiParam->num_units_in_tick = seq->num_units_in_tick;
1322 vuiParam->time_scale = seq->time_scale;
1323 vuiParam->fixed_frame_rate_flag = 1;
1324
1325 vuiParam->bitstream_restriction_flag = seq->vui_fields.bits.bitstream_restriction_flag;
1326 vuiParam->motion_vectors_over_pic_boundaries_flag = 1;
1327 vuiParam->max_bytes_per_pic_denom = 2;
1328 vuiParam->max_bits_per_mb_denom = 1;
1329 vuiParam->max_dec_frame_buffering = seq->max_num_ref_frames + 1;
1330 vuiParam->num_reorder_frames = seq->max_num_ref_frames;
1331 vuiParam->log2_max_mv_length_horizontal = seq->vui_fields.bits.log2_max_mv_length_horizontal;
1332 vuiParam->log2_max_mv_length_vertical = seq->vui_fields.bits.log2_max_mv_length_vertical;
1333 }
1334
1335 return VA_STATUS_SUCCESS;
1336 }
1337
1338 VAStatus DdiEncodeAvc::ParsePicParams(
1339 PDDI_MEDIA_CONTEXT mediaCtx,
1340 void *ptr)
1341 {
1342 DDI_CODEC_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1343 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1344 DDI_CODEC_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER);
1345
1346 VAEncPictureParameterBufferH264 *pic = (VAEncPictureParameterBufferH264 *)ptr;
1347 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)((uint8_t *)m_encodeCtx->pPicParams + pic->pic_parameter_set_id * sizeof(CODEC_AVC_ENCODE_PIC_PARAMS));
1348 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
1349
1350 current_pic_parameter_set_id = pic->pic_parameter_set_id;
1351 current_seq_parameter_set_id = pic->seq_parameter_set_id;
1352 MOS_ZeroMemory(picParams, sizeof(CODEC_AVC_ENCODE_PIC_PARAMS));
1353
1354 PCODEC_AVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_AVC_ENCODE_SEQUENCE_PARAMS)((uint8_t *)m_encodeCtx->pSeqParams + pic->seq_parameter_set_id * sizeof(CODEC_AVC_ENCODE_SEQUENCE_PARAMS));
1355 if ((pic->seq_parameter_set_id >= CODEC_AVC_MAX_SPS_NUM) ||
1356 (pic->pic_parameter_set_id >= CODEC_AVC_MAX_PPS_NUM))
1357 {
1358 return VA_STATUS_ERROR_INVALID_PARAMETER;
1359 }
1360
1361 if (pic->CurrPic.flags == VA_PICTURE_H264_TOP_FIELD ||
1362 pic->CurrPic.flags == VA_PICTURE_H264_BOTTOM_FIELD)
1363 {
1364 picParams->FieldCodingFlag = 1;
1365 }
1366
1367 if (pic->CurrPic.picture_id != VA_INVALID_SURFACE)
1368 {
1369 DDI_CHK_RET(RegisterRTSurfaces(&(m_encodeCtx->RTtbl),
1370 MediaLibvaCommonNext::GetSurfaceFromVASurfaceID(mediaCtx,
1371 pic->CurrPic.picture_id)),
1372 "RegisterRTSurfaces failed!");
1373 }
1374
1375 // Curr Recon Pic
1376 SetupCodecPicture(mediaCtx, &(m_encodeCtx->RTtbl), &picParams->CurrReconstructedPic,pic->CurrPic, picParams->FieldCodingFlag, false, false);
1377 DDI_CODEC_RENDER_TARGET_TABLE *rtTbl = &(m_encodeCtx->RTtbl);
1378
1379 rtTbl->pCurrentReconTarget = MediaLibvaCommonNext::GetSurfaceFromVASurfaceID(mediaCtx, pic->CurrPic.picture_id);
1380 // The surface for reconstructed frame is not registered, return error to app
1381 if (nullptr == rtTbl->pCurrentReconTarget)
1382 {
1383 DDI_CODEC_ASSERTMESSAGE("invalid surface for reconstructed frame");
1384 return VA_STATUS_ERROR_INVALID_PARAMETER;
1385 }
1386
1387 // curr orig pic
1388 // Hard to understand here!
1389 picParams->CurrOriginalPic.FrameIdx = (uint8_t)GetRenderTargetID(rtTbl, rtTbl->pCurrentReconTarget);
1390 picParams->CurrOriginalPic.PicFlags = picParams->CurrReconstructedPic.PicFlags;
1391
1392 // The surface for reconstructed frame is not registered, return error to app
1393 if (picParams->CurrOriginalPic.FrameIdx == 0xFF)
1394 {
1395 DDI_CODEC_ASSERTMESSAGE("unregistered surface for reconstructed frame");
1396 return VA_STATUS_ERROR_INVALID_PARAMETER;
1397 }
1398
1399 // RefFrame List
1400 for (uint32_t i = 0; i < DDI_CODEC_NUM_MAX_REF_FRAME; i++)
1401 {
1402 if(pic->ReferenceFrames[i].picture_id!= VA_INVALID_SURFACE)
1403 {
1404 UpdateRegisteredRTSurfaceFlag(&(m_encodeCtx->RTtbl),
1405 MediaLibvaCommonNext::GetSurfaceFromVASurfaceID(mediaCtx,
1406 pic->ReferenceFrames[i].picture_id));
1407 }
1408 SetupCodecPicture(mediaCtx, &(m_encodeCtx->RTtbl), &(picParams->RefFrameList[i]), pic->ReferenceFrames[i], picParams->FieldCodingFlag, true, false);
1409 }
1410
1411 for (uint32_t i = 0; i < DDI_CODEC_NUM_MAX_REF_FRAME; i++)
1412 {
1413 picParams->FieldOrderCntList[i][0] = pic->ReferenceFrames[i].TopFieldOrderCnt;
1414 picParams->FieldOrderCntList[i][1] = pic->ReferenceFrames[i].BottomFieldOrderCnt;
1415 }
1416
1417 picParams->seq_parameter_set_id = pic->seq_parameter_set_id;
1418 picParams->pic_parameter_set_id = pic->pic_parameter_set_id;
1419 /* pic->coding_type; App is always setting this to 0 */
1420 picParams->CodingType = I_TYPE;
1421 picParams->second_chroma_qp_index_offset = pic->second_chroma_qp_index_offset;
1422 picParams->num_ref_idx_l0_active_minus1 = pic->num_ref_idx_l0_active_minus1;
1423 picParams->num_ref_idx_l1_active_minus1 = pic->num_ref_idx_l1_active_minus1;
1424 picParams->QpY = pic->pic_init_qp;
1425
1426 if (pic->CurrPic.flags == VA_PICTURE_H264_SHORT_TERM_REFERENCE ||
1427 pic->CurrPic.flags == VA_PICTURE_H264_LONG_TERM_REFERENCE)
1428 {
1429 picParams->UsedForReferenceFlags = 1;
1430 }
1431 picParams->CurrFieldOrderCnt[0] = pic->CurrPic.TopFieldOrderCnt;
1432 picParams->CurrFieldOrderCnt[1] = pic->CurrPic.BottomFieldOrderCnt;
1433 picParams->frame_num = pic->frame_num;
1434
1435 picParams->bLastPicInSeq = (pic->last_picture & H264_LAST_PICTURE_EOSEQ) ? 1 : 0;
1436 picParams->bLastPicInStream = (pic->last_picture & H264_LAST_PICTURE_EOSTREAM) ? 1 : 0;
1437 picParams->chroma_qp_index_offset = pic->chroma_qp_index_offset;
1438 picParams->bIdrPic = pic->pic_fields.bits.idr_pic_flag;
1439 picParams->RefPicFlag = pic->pic_fields.bits.reference_pic_flag;
1440 picParams->entropy_coding_mode_flag = pic->pic_fields.bits.entropy_coding_mode_flag;
1441 picParams->weighted_pred_flag = pic->pic_fields.bits.weighted_pred_flag;
1442 picParams->weighted_bipred_idc = pic->pic_fields.bits.weighted_bipred_idc;
1443 picParams->constrained_intra_pred_flag = pic->pic_fields.bits.constrained_intra_pred_flag;
1444 picParams->transform_8x8_mode_flag = pic->pic_fields.bits.transform_8x8_mode_flag;
1445 picParams->pic_order_present_flag = pic->pic_fields.bits.pic_order_present_flag;
1446 picParams->pic_scaling_matrix_present_flag = pic->pic_fields.bits.pic_scaling_matrix_present_flag;
1447 picParams->bDisplayFormatSwizzle = NeedDisplayFormatSwizzle(rtTbl->pCurrentRT);
1448 for (uint32_t i = 0; i < 12; i++)
1449 {
1450 picParams->pic_scaling_list_present_flag[i] = pic->pic_fields.bits.pic_scaling_matrix_present_flag;
1451 }
1452
1453 // pps, sps header packing in app
1454 //Slice header packing will be done in app where bDisableAccelaratorHeaderPacking flag is defaulted to 0
1455 //this flag shouldnt be true for slice header packing
1456 picParams->UserFlags.bDisableAcceleratorHeaderPacking = true;
1457 picParams->UserFlags.bUseRawPicForRef = false; //true;
1458
1459 //Reset it to zero now
1460 picParams->NumSlice = 0;
1461
1462 //Hardcode SFD mvThreshold to 80.
1463 picParams->dwZMvThreshold = 80;
1464
1465 DDI_MEDIA_BUFFER *buf = MediaLibvaCommonNext::GetBufferFromVABufferID(mediaCtx, pic->coded_buf);
1466 DDI_CODEC_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_PARAMETER);
1467
1468 // MSDK will re-use the buffer so need to remove before adding to status report again
1469 RemoveFromStatusReportQueue(buf);
1470 MediaLibvaCommonNext::MediaBufferToMosResource(buf, &(m_encodeCtx->resBitstreamBuffer));
1471
1472 return VA_STATUS_SUCCESS;
1473 }
1474
1475 VAStatus DdiEncodeAvc::ParseSlcParams(
1476 PDDI_MEDIA_CONTEXT mediaCtx,
1477 void *ptr,
1478 uint32_t numSlices)
1479 {
1480 DDI_CODEC_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1481 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1482 DDI_CODEC_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER);
1483
1484 VAEncSliceParameterBufferH264 *slc = (VAEncSliceParameterBufferH264 *)ptr;
1485 PCODEC_AVC_ENCODE_SLICE_PARAMS slcParams = (CODEC_AVC_ENCODE_SLICE_PARAMS *)m_encodeCtx->pSliceParams;
1486 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)((uint8_t *)m_encodeCtx->pPicParams + slc->pic_parameter_set_id * sizeof(CODEC_AVC_ENCODE_PIC_PARAMS));
1487 DDI_CODEC_CHK_NULL(slcParams, "nullptr slcParams", VA_STATUS_ERROR_INVALID_PARAMETER);
1488 DDI_CODEC_CHK_NULL(picParams, "nullptr picParams", VA_STATUS_ERROR_INVALID_PARAMETER);
1489
1490 uint8_t numSlcs = 0;
1491 uint32_t totalMBs = m_encodeCtx->wPicWidthInMB * m_encodeCtx->wPicHeightInMB;
1492 uint32_t usedMBs = 0;
1493 uint32_t mBScalerForField = 1; //related with frame/field coding
1494 uint32_t sliceHeightInMB = 1;
1495 uint32_t sliceActualMBs = 0;
1496
1497 if (slc->pic_parameter_set_id >= CODEC_AVC_MAX_PPS_NUM)
1498 {
1499 return VA_STATUS_ERROR_INVALID_PARAMETER;
1500 }
1501
1502 numSlcs = picParams->NumSlice;
1503
1504 if (numSlcs == 0)
1505 {
1506 picParams->CodingType = CodechalPicTypeFromVaSlcType(slc->slice_type);
1507 }
1508
1509 // Disable rolling intra refresh if not a P-type picture
1510 if ((picParams->EnableRollingIntraRefresh != ROLLING_I_DISABLED) && (picParams->CodingType != P_TYPE))
1511 {
1512 picParams->EnableRollingIntraRefresh = ROLLING_I_DISABLED;
1513 m_encodeCtx->uiIntraRefreshFrameCnt = 0;
1514 m_encodeCtx->uiIntraRefreshMBx = 0;
1515 m_encodeCtx->uiIntraRefreshMBy = 0;
1516 }
1517
1518 slcParams += numSlcs;
1519 MOS_ZeroMemory(
1520 slcParams,
1521 numSlices * sizeof(CODEC_AVC_ENCODE_SLICE_PARAMS));
1522
1523 if (numSlcs != 0)
1524 {
1525 slcParams--;
1526 usedMBs = (slcParams->NumMbsForSlice + slcParams->first_mb_in_slice) / mBScalerForField;
1527 if (usedMBs >= totalMBs)
1528 {
1529 return VA_STATUS_SUCCESS;
1530 }
1531 slcParams++;
1532 }
1533
1534 uint32_t slcCount;
1535 uint32_t i;
1536 for (slcCount = 0; slcCount < numSlices; slcCount++)
1537 {
1538 if (usedMBs >= totalMBs)
1539 break;
1540
1541 sliceActualMBs = slc->num_macroblocks;
1542 if (sliceActualMBs % m_encodeCtx->wPicWidthInMB) // If any slice is a partial row of macroblocks, then set flag to indicate sliceMapSurface is required.
1543 {
1544 m_arbitraryNumMbsInSlice = 1;
1545 }
1546
1547 if (sliceActualMBs + usedMBs > totalMBs)
1548 sliceActualMBs = totalMBs - usedMBs;
1549
1550 slcParams->NumMbsForSlice = sliceActualMBs * mBScalerForField;
1551 slcParams->first_mb_in_slice = usedMBs * mBScalerForField;
1552 usedMBs += sliceActualMBs;
1553
1554 slcParams->slice_type = slc->slice_type;
1555 slcParams->pic_parameter_set_id = slc->pic_parameter_set_id;
1556 slcParams->idr_pic_id = slc->idr_pic_id;
1557 slcParams->pic_order_cnt_lsb = slc->pic_order_cnt_lsb;
1558 slcParams->delta_pic_order_cnt_bottom = slc->delta_pic_order_cnt_bottom;
1559 slcParams->delta_pic_order_cnt[0] = slc->delta_pic_order_cnt[0];
1560 slcParams->delta_pic_order_cnt[1] = slc->delta_pic_order_cnt[1];
1561 slcParams->direct_spatial_mv_pred_flag = slc->direct_spatial_mv_pred_flag;
1562 slcParams->num_ref_idx_active_override_flag = slc->num_ref_idx_active_override_flag;
1563 slcParams->num_ref_idx_l0_active_minus1 = slc->num_ref_idx_l0_active_minus1;
1564 slcParams->num_ref_idx_l1_active_minus1 = slc->num_ref_idx_l1_active_minus1;
1565 slcParams->luma_log2_weight_denom = slc->luma_log2_weight_denom;
1566 slcParams->chroma_log2_weight_denom = slc->chroma_log2_weight_denom;
1567 slcParams->cabac_init_idc = slc->cabac_init_idc;
1568 slcParams->slice_qp_delta = slc->slice_qp_delta;
1569 slcParams->disable_deblocking_filter_idc = slc->disable_deblocking_filter_idc;
1570 slcParams->slice_alpha_c0_offset_div2 = slc->slice_alpha_c0_offset_div2;
1571 slcParams->slice_beta_offset_div2 = slc->slice_beta_offset_div2;
1572
1573 slcParams->num_ref_idx_l0_active_minus1_from_DDI = slcParams->num_ref_idx_l0_active_minus1;
1574 slcParams->num_ref_idx_l1_active_minus1_from_DDI = slcParams->num_ref_idx_l1_active_minus1;
1575 slcParams->slice_id = numSlcs + slcCount;
1576 slcParams->luma_weight_flag[0] = slc->luma_weight_l0_flag;
1577 slcParams->chroma_weight_flag[0] = slc->chroma_weight_l0_flag;
1578 slcParams->luma_weight_flag[1] = slc->luma_weight_l1_flag;
1579 slcParams->chroma_weight_flag[1] = slc->chroma_weight_l1_flag;
1580
1581 for (i = 0; i < 32; i++)
1582 {
1583 // list 0
1584 if ((slc->luma_weight_l0_flag) &&
1585 ((slc->luma_weight_l0[i] != 0) || (slc->luma_offset_l0[i] != 0)))
1586 {
1587 slcParams->Weights[0][i][0][0] = slc->luma_weight_l0[i]; // Y weight
1588 slcParams->Weights[0][i][0][1] = slc->luma_offset_l0[i]; // Y offset
1589 }
1590 else
1591 {
1592 slcParams->Weights[0][i][0][0] = 1 << slcParams->luma_log2_weight_denom; // Y weight
1593 slcParams->Weights[0][i][0][1] = 0; // Y offset
1594 }
1595
1596 if ((slc->chroma_weight_l0_flag) &&
1597 ((slc->chroma_weight_l0[i][0] != 0) || (slc->chroma_offset_l0[i][0] != 0) ||
1598 (slc->chroma_weight_l0[i][1] != 0) || (slc->chroma_offset_l0[i][1] != 0)))
1599 {
1600 slcParams->Weights[0][i][1][0] = slc->chroma_weight_l0[i][0]; // Cb weight
1601 slcParams->Weights[0][i][1][1] = slc->chroma_offset_l0[i][0]; // Cb offset
1602 slcParams->Weights[0][i][2][0] = slc->chroma_weight_l0[i][1]; // Cr weight
1603 slcParams->Weights[0][i][2][1] = slc->chroma_offset_l0[i][1]; // Cr offset
1604 }
1605 else
1606 {
1607 slcParams->Weights[0][i][1][0] = 1; // Cb weight
1608 slcParams->Weights[0][i][1][1] = 0; // Cb offset
1609 slcParams->Weights[0][i][2][0] = 1; // Cr weight
1610 slcParams->Weights[0][i][2][1] = 0; // Cr offset
1611 }
1612
1613 // list 1
1614 if ((slc->luma_weight_l1_flag) &&
1615 ((slc->luma_weight_l1[i] != 0) || (slc->luma_offset_l1[i] != 0)))
1616 {
1617 slcParams->Weights[1][i][0][0] = slc->luma_weight_l1[i]; // Y weight
1618 slcParams->Weights[1][i][0][1] = slc->luma_offset_l1[i]; // Y offset
1619 }
1620 else
1621 {
1622 slcParams->Weights[1][i][0][0] = 1 << slcParams->luma_log2_weight_denom; // Y weight
1623 slcParams->Weights[1][i][0][1] = 0; // Y offset
1624 }
1625
1626 if ((slc->chroma_weight_l1_flag) &&
1627 ((slc->chroma_weight_l1[i][0] != 0) || (slc->chroma_offset_l1[i][0] != 0) ||
1628 (slc->chroma_weight_l1[i][1] != 0) || (slc->chroma_offset_l1[i][1] != 0)))
1629 {
1630 slcParams->Weights[1][i][1][0] = slc->chroma_weight_l1[i][0]; // Cb weight
1631 slcParams->Weights[1][i][1][1] = slc->chroma_offset_l1[i][0]; // Cb offset
1632
1633 slcParams->Weights[1][i][2][0] = slc->chroma_weight_l1[i][1]; // Cr weight
1634 slcParams->Weights[1][i][2][1] = slc->chroma_offset_l1[i][1]; // Cr offset
1635 }
1636 else
1637 {
1638 slcParams->Weights[1][i][1][0] = 1; // Cb weight
1639 slcParams->Weights[1][i][1][1] = 0; // Cb offset
1640
1641 slcParams->Weights[1][i][2][0] = 1; // Cr weight
1642 slcParams->Weights[1][i][2][1] = 0; // Cr offset
1643 }
1644 }
1645
1646 for (i = 0; i < CODEC_MAX_NUM_REF_FIELD; i++)
1647 {
1648 SetupCodecPicture(mediaCtx, &(m_encodeCtx->RTtbl), &(slcParams->RefPicList[0][i]), slc->RefPicList0[i], picParams->FieldCodingFlag, false, true);
1649 GetSlcRefIdx(&(picParams->RefFrameList[0]), &(slcParams->RefPicList[0][i]));
1650 }
1651 for (i = 0; i < CODEC_MAX_NUM_REF_FIELD; i++)
1652 {
1653 SetupCodecPicture(mediaCtx, &(m_encodeCtx->RTtbl), &(slcParams->RefPicList[1][i]), slc->RefPicList1[i], picParams->FieldCodingFlag, false, true);
1654 GetSlcRefIdx(&(picParams->RefFrameList[0]), &(slcParams->RefPicList[1][i]));
1655 }
1656
1657 slc++;
1658 slcParams++;
1659 }
1660
1661 picParams->NumSlice += slcCount;
1662 if (picParams->NumSlice > ENCODE_AVC_MAX_SLICES_SUPPORTED)
1663 {
1664 DDI_CODEC_ASSERTMESSAGE("Number of slices exceeds max supported");
1665 return VA_STATUS_ERROR_INVALID_PARAMETER;
1666 }
1667
1668 m_encodeCtx->dwNumSlices = picParams->NumSlice;
1669
1670 return VA_STATUS_SUCCESS;
1671 }
1672
1673 VAStatus DdiEncodeAvc::FindNalUnitStartCodes(
1674 uint8_t * buf,
1675 uint32_t size,
1676 uint32_t * startCodesOffset,
1677 uint32_t * startCodesLength)
1678 {
1679 uint8_t i = 0;
1680
1681 while (((i + 3) < size) &&
1682 (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) &&
1683 (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0 || buf[i+3] != 0x01))
1684 {
1685 i++;
1686 }
1687
1688 if ((i + 3) == size)
1689 {
1690 if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01)
1691 {
1692 return VA_STATUS_ERROR_INVALID_BUFFER; //NALU start codes doesn't exit
1693 }
1694 else
1695 {
1696 *startCodesOffset = size - 3;
1697 *startCodesLength = 3;
1698 return VA_STATUS_SUCCESS;
1699 }
1700 }
1701
1702 if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01)
1703 {
1704 *startCodesOffset = i;
1705 *startCodesLength = 4;
1706 }
1707 else
1708 {
1709 *startCodesOffset = i;
1710 *startCodesLength = 3;
1711 }
1712
1713 return VA_STATUS_SUCCESS;
1714 }
1715
1716 VAStatus DdiEncodeAvc::ParsePackedHeaderParams(void *ptr)
1717 {
1718 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1719 DDI_CODEC_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER);
1720
1721 m_encodeCtx->bLastPackedHdrIsSlice = false;
1722 VAEncPackedHeaderParameterBuffer *encPackedHeaderParamBuf = (VAEncPackedHeaderParameterBuffer *)ptr;
1723 CODECHAL_ENCODE_AVC_NAL_UNIT_TYPE nalUnitType;
1724 if (encPackedHeaderParamBuf->type == VAEncPackedHeaderH264_SPS)
1725 {
1726 nalUnitType = CODECHAL_ENCODE_AVC_NAL_UT_SPS;
1727 m_newSeqHeader = 1;
1728 }
1729 else if (encPackedHeaderParamBuf->type == VAEncPackedHeaderH264_PPS)
1730 {
1731 nalUnitType = CODECHAL_ENCODE_AVC_NAL_UT_PPS;
1732 m_newPpsHeader = 1;
1733 }
1734 else if (encPackedHeaderParamBuf->type == VAEncPackedHeaderH264_Slice)
1735 {
1736 nalUnitType = CODECHAL_ENCODE_AVC_NAL_UT_SLICE;
1737 m_encodeCtx->bLastPackedHdrIsSlice = true;
1738 m_encodeCtx->bHavePackedSliceHdr = true;
1739
1740 // check the slice header number. Max supported is AVC_ENCODE_MAX_SLICES_SUPPORTED
1741 if (m_encodeCtx->uiSliceHeaderCnt >= ENCODE_AVC_MAX_SLICES_SUPPORTED)
1742 {
1743 DDI_CODEC_ASSERTMESSAGE("Number of slices exceeds max supported");
1744 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
1745 }
1746
1747 // get the packed header size
1748 m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].BitSize = encPackedHeaderParamBuf->bit_length;
1749 //don't know NALU start codes now, assign to 4 here when has_emulation_bytes is 0 and later will correct it
1750 m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].SkipEmulationByteCount = (encPackedHeaderParamBuf->has_emulation_bytes) ? (encPackedHeaderParamBuf->bit_length + 7) / 8 : 4;
1751 }
1752 else if (encPackedHeaderParamBuf->type == VAEncPackedHeaderRawData)
1753 {
1754 // currently RawData is packed nal unit beside SPS,PPS,SLICE HEADER etc.
1755 // use AUD just because we need a Type to distinguish with Slice header
1756 nalUnitType = CODECHAL_ENCODE_AVC_NAL_UT_AUD;
1757 }
1758 else
1759 {
1760 nalUnitType = CODECHAL_ENCODE_AVC_MAX_NAL_TYPE;
1761 }
1762
1763 if (encPackedHeaderParamBuf->type != VAEncPackedHeaderH264_Slice)
1764 {
1765 m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiNalUnitType = nalUnitType;
1766 m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->bInsertEmulationBytes = (encPackedHeaderParamBuf->has_emulation_bytes) ? false : true;
1767 //don't know NALU start codes now, assign to 4 here when has_emulation_bytes is 0 and later will correct it
1768 m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSkipEmulationCheckCount = (encPackedHeaderParamBuf->has_emulation_bytes) ? (encPackedHeaderParamBuf->bit_length + 7) / 8 : 4;
1769 m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSize = (encPackedHeaderParamBuf->bit_length + 7) / 8;
1770 m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiOffset = 0;
1771 }
1772
1773 return VA_STATUS_SUCCESS;
1774 }
1775
1776 AvcOutBits::AvcOutBits(uint8_t *pOutBits, uint32_t BitSize)
1777 {
1778 m_pOutBits = pOutBits;
1779 m_BitSize = BitSize;
1780 m_BitOffset = 0;
1781 }
1782
1783 inline uint32_t AvcOutBits::GetBitOffset()
1784 {
1785 return m_BitOffset;
1786 }
1787
1788 void AvcOutBits::PutBit(uint32_t v)
1789 {
1790 DDI_ASSERT(m_BitOffset + 1 <= m_BitSize);
1791
1792 uint32_t LeftOffset = m_BitOffset % 8;
1793 uint8_t *p = m_pOutBits + m_BitOffset / 8;
1794 (*p) |= ((v & 1) << (7 - LeftOffset));
1795
1796 m_BitOffset++;
1797 }
1798
1799 void AvcOutBits::PutBits(uint32_t v, uint32_t n)
1800 {
1801 DDI_ASSERT((n > 0) && (n <= 32));
1802
1803 uint32_t t = 0;
1804
1805 if (!(m_BitOffset % 8) && !(n % 8))
1806 {
1807 uint32_t nBytes = n / 8;
1808 uint8_t *p = m_pOutBits + m_BitOffset / 8;
1809
1810 while (nBytes-- > 0)
1811 (*p++) = (v >> nBytes) & 0xFF;
1812
1813 m_BitOffset += n;
1814 return;
1815 }
1816
1817 while (n-- > 0)
1818 PutBit((v >> n) & 1);
1819 }
1820
1821 AvcInBits::AvcInBits(uint8_t *pInBits, uint32_t BitSize)
1822 {
1823 m_pInBits = pInBits;
1824 m_BitSize = BitSize;
1825 m_BitOffset = 0;
1826 }
1827
1828 void AvcInBits::SkipBits(uint32_t n)
1829 {
1830 DDI_ASSERT(n > 0);
1831 DDI_ASSERT(m_BitOffset + n <= m_BitSize);
1832
1833 m_BitOffset += n;
1834 }
1835
1836 uint32_t AvcInBits::GetBit()
1837 {
1838 DDI_ASSERT(m_BitOffset + 1 <= m_BitSize);
1839
1840 uint32_t LeftOffset = m_BitOffset % 8;
1841 uint8_t const *p = m_pInBits + m_BitOffset / 8;
1842 uint32_t v = (*p >> (7 - LeftOffset)) & 1;
1843
1844 m_BitOffset++;
1845 return v;
1846 }
1847
1848 uint32_t AvcInBits::GetBits(uint32_t n)
1849 {
1850 DDI_ASSERT((n > 0) && (n <= 32));
1851
1852 uint32_t v = 0;
1853
1854 if (!(m_BitOffset % 8) && !(n % 8))
1855 {
1856 uint32_t nBytes = n / 8;
1857 uint8_t const *p = m_pInBits + m_BitOffset / 8;
1858
1859 while (nBytes-- > 0)
1860 v = (v << 8) | (*p++);
1861
1862 m_BitOffset += n;
1863 return v;
1864 }
1865
1866 while (n-- > 0)
1867 v = (v << 1) | GetBit();
1868
1869 return v;
1870 }
1871
1872 uint32_t AvcInBits::AvcInBits::GetUE()
1873 {
1874 uint32_t nZero = 0;
1875 while(!GetBit())
1876 nZero++;
1877
1878 return nZero ? ((1 << nZero) | GetBits(nZero)) - 1 : 0;
1879 }
1880
1881 inline uint32_t AvcInBits::GetBitOffset()
1882 {
1883 return m_BitOffset;
1884 }
1885
1886 inline void AvcInBits::ResetBitOffset()
1887 {
1888 m_BitOffset = 0;
1889 }
1890
1891 MOS_STATUS DdiEncodeAvc::CheckPackedSlcHeaderData(
1892 void *pInSlcHdr,
1893 uint32_t InBitSize,
1894 void **ppOutSlcHdr,
1895 uint32_t &OutBitSize)
1896 {
1897 MOS_STATUS status;
1898 uint32_t HdrBitSize = 0;
1899
1900 *ppOutSlcHdr = NULL;
1901 OutBitSize = 0;
1902
1903 if (0 == InBitSize || NULL == pInSlcHdr)
1904 return MOS_STATUS_SUCCESS;
1905
1906 AvcInBits InBits((uint8_t*)pInSlcHdr, InBitSize);
1907
1908 // Skip start code
1909 uint8_t StartCode = 0;
1910 while (1 != StartCode) {
1911 StartCode = InBits.GetBits(8);
1912 HdrBitSize += 8;
1913 }
1914
1915 uint32_t StartBitSize = HdrBitSize;
1916
1917 // Check NAL Unit type
1918 HdrBitSize += 8;
1919 InBits.SkipBits(1);
1920 InBits.SkipBits(2);
1921 uint32_t nalUnitType = InBits.GetBits(5);
1922 if (20 == nalUnitType)
1923 {
1924 // MVC enxtension
1925 InBits.SkipBits(24);
1926 HdrBitSize += 24;
1927 }
1928
1929 // find first_mb_in_slice
1930 uint32_t first_mb_in_slice = InBits.GetUE();
1931 if (0 == first_mb_in_slice)
1932 return MOS_STATUS_SUCCESS;
1933
1934 // Force first_mb_in_slice to 0 for AVC VDENC
1935 uint32_t LeftBitSize = InBitSize - InBits.GetBitOffset();
1936 OutBitSize = LeftBitSize + HdrBitSize + 1;
1937 *ppOutSlcHdr = MOS_AllocAndZeroMemory((OutBitSize + 7) / 8);
1938
1939 AvcOutBits OutBits((uint8_t*)(*ppOutSlcHdr), OutBitSize);
1940
1941 InBits.ResetBitOffset();
1942 OutBits.PutBits(InBits.GetBits(StartBitSize), StartBitSize);
1943 OutBits.PutBits(InBits.GetBits(8), 8);
1944 if (20 == nalUnitType)
1945 OutBits.PutBits(InBits.GetBits(24), 24);
1946
1947 // Replace first_mb_in_slice
1948 first_mb_in_slice = InBits.GetUE();
1949 OutBits.PutBit(0);
1950
1951 // Copy the left data
1952 while (LeftBitSize >= 32)
1953 {
1954 OutBits.PutBits(InBits.GetBits(32), 32);
1955 LeftBitSize -= 32;
1956 }
1957
1958 if (LeftBitSize)
1959 OutBits.PutBits(InBits.GetBits(LeftBitSize), LeftBitSize);
1960
1961 return MOS_STATUS_SUCCESS;
1962 }
1963
1964 VAStatus DdiEncodeAvc::ParsePackedHeaderData(void *ptr)
1965 {
1966 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1967 DDI_CODEC_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER);
1968
1969 BSBuffer *bsBuffer = m_encodeCtx->pbsBuffer;
1970 DDI_CODEC_CHK_NULL(bsBuffer, "nullptr bsBuffer", VA_STATUS_ERROR_INVALID_PARAMETER);
1971
1972 if ((m_encodeCtx->indexNALUnit == 0) && (m_encodeCtx->uiSliceHeaderCnt == 0))
1973 {
1974 *(bsBuffer->pBase) = 0;
1975 bsBuffer->pCurrent = bsBuffer->pBase;
1976 bsBuffer->SliceOffset = 0;
1977 bsBuffer->BitOffset = 0;
1978 bsBuffer->BitSize = 0;
1979 }
1980
1981 uint32_t hdrDataSize;
1982 if (true == m_encodeCtx->bLastPackedHdrIsSlice)
1983 {
1984 void *temp_ptr = NULL;
1985 uint32_t temp_size = 0;
1986
1987 MOS_STATUS status = CheckPackedSlcHeaderData(ptr,
1988 m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].BitSize,
1989 &temp_ptr, temp_size);
1990 if (MOS_STATUS_SUCCESS != status)
1991 {
1992 DDI_CODEC_ASSERTMESSAGE("DDI:packed slice header is not supported!");
1993 return VA_STATUS_ERROR_INVALID_PARAMETER;
1994 }
1995
1996 if (temp_size && temp_ptr)
1997 {
1998 m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].BitSize = temp_size;
1999 }
2000
2001 hdrDataSize = (m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].BitSize + 7) / 8;
2002
2003 status = MOS_SecureMemcpy(bsBuffer->pCurrent,
2004 bsBuffer->BufferSize - bsBuffer->SliceOffset,
2005 (uint8_t *)(temp_ptr ? temp_ptr : ptr),
2006 hdrDataSize);
2007
2008 if (temp_ptr)
2009 {
2010 MOS_FreeMemory(temp_ptr);
2011 temp_size = 0;
2012 temp_ptr = NULL;
2013 }
2014
2015 if (MOS_STATUS_SUCCESS != status)
2016 {
2017 DDI_CODEC_ASSERTMESSAGE("DDI:packed slice header size is too large to be supported!");
2018 return VA_STATUS_ERROR_INVALID_PARAMETER;
2019 }
2020
2021 m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].SliceOffset = bsBuffer->pCurrent - bsBuffer->pBase;
2022
2023 // correct SkipEmulationByteCount
2024 // according to LibVA principle, one packed header buffer should only contain one NALU,
2025 // so when has_emulation_bytes is 0, SkipEmulationByteCount only needs to skip the NALU start codes
2026 if (m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].SkipEmulationByteCount != hdrDataSize)
2027 {
2028 uint32_t startCodesOffset = 0;
2029 uint32_t startCodesLength = 0;
2030 VAStatus vaSts = VA_STATUS_SUCCESS;
2031 vaSts = FindNalUnitStartCodes((uint8_t *)ptr, hdrDataSize, &startCodesOffset, &startCodesLength);
2032 if (VA_STATUS_SUCCESS != vaSts)
2033 {
2034 DDI_CODEC_ASSERTMESSAGE("DDI: packed slice header doesn't include NAL unit start codes!");
2035 return vaSts;
2036 }
2037 m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].SkipEmulationByteCount = MOS_MIN(15, (startCodesOffset + startCodesLength));
2038 }
2039
2040 m_encodeCtx->uiSliceHeaderCnt++;
2041 m_encodeCtx->bLastPackedHdrIsSlice = false;
2042 }
2043 else
2044 {
2045 // copy sps and pps header data
2046 hdrDataSize = m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSize;
2047 MOS_STATUS status = MOS_SecureMemcpy(bsBuffer->pCurrent,
2048 bsBuffer->BufferSize - bsBuffer->SliceOffset,
2049 (uint8_t *)ptr,
2050 hdrDataSize);
2051 if (MOS_STATUS_SUCCESS != status)
2052 {
2053 DDI_CODEC_ASSERTMESSAGE("DDI:packed header size is too large to be supported!");
2054 return VA_STATUS_ERROR_INVALID_PARAMETER;
2055 }
2056
2057 // correct uiSkipEmulationCheckCount
2058 // according to LibVA principle, one packed header buffer should only contain one NALU,
2059 // so when has_emulation_bytes is 0, uiSkipEmulationCheckCount only needs to skip the NALU start codes
2060 if (m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSkipEmulationCheckCount != hdrDataSize)
2061 {
2062 uint32_t startCodesOffset = 0;
2063 uint32_t startCodesLength = 0;
2064 VAStatus vaSts = VA_STATUS_SUCCESS;
2065 vaSts = FindNalUnitStartCodes((uint8_t *)ptr, hdrDataSize, &startCodesOffset, &startCodesLength);
2066 if (VA_STATUS_SUCCESS != vaSts)
2067 {
2068 DDI_CODEC_ASSERTMESSAGE("DDI: packed header doesn't include NAL unit start codes!");
2069 return vaSts;
2070 }
2071 m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSkipEmulationCheckCount = MOS_MIN(15, (startCodesOffset + startCodesLength));
2072 }
2073
2074 m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiOffset = bsBuffer->pCurrent - bsBuffer->pBase;
2075 m_encodeCtx->indexNALUnit++;
2076 }
2077 bsBuffer->pCurrent += hdrDataSize;
2078 bsBuffer->SliceOffset += hdrDataSize;
2079 bsBuffer->BitSize += hdrDataSize * 8;
2080
2081 return VA_STATUS_SUCCESS;
2082 }
2083
2084 VAStatus DdiEncodeAvc::ParseMiscParams(void *ptr)
2085 {
2086 DDI_CODEC_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
2087 DDI_CODEC_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER);
2088
2089 VAEncMiscParameterBuffer *miscParamBuf = (VAEncMiscParameterBuffer *)ptr;
2090 DDI_CODEC_CHK_NULL(miscParamBuf->data, "nullptr miscParamBuf->data", VA_STATUS_ERROR_INVALID_PARAMETER);
2091
2092 VAStatus status = VA_STATUS_SUCCESS;
2093 switch ((int32_t)(miscParamBuf->type))
2094 {
2095 case VAEncMiscParameterTypeHRD:
2096 status = ParseMiscParamHRD((void *)miscParamBuf->data);
2097 break;
2098
2099 case VAEncMiscParameterTypeFrameRate:
2100 status = ParseMiscParamFR((void *)miscParamBuf->data);
2101 break;
2102
2103 case VAEncMiscParameterTypeRateControl:
2104 status = ParseMiscParamRC((void *)miscParamBuf->data);
2105 break;
2106
2107 case VAEncMiscParameterTypeEncQuality:
2108 status = ParseMiscParamEncQuality((void *)miscParamBuf->data);
2109 break;
2110
2111 case VAEncMiscParameterTypeQuantization:
2112 status = ParseMiscParamQuantization((void *)miscParamBuf->data);
2113 break;
2114
2115 case VAEncMiscParameterTypeRIR:
2116 status = ParseMiscParameterRIR((void *)miscParamBuf->data);
2117 break;
2118
2119 case VAEncMiscParameterTypeSkipFrame:
2120 status = ParseMiscParamSkipFrame((void *)miscParamBuf->data);
2121 break;
2122
2123 case VAEncMiscParameterTypeMaxFrameSize:
2124 status = ParseMiscParamMaxFrameSize((void *)miscParamBuf->data);
2125 break;
2126
2127 case VAEncMiscParameterTypeMultiPassFrameSize:
2128 status = ParseMiscParamMultiPassFrameSize((void *)miscParamBuf->data);
2129 break;
2130
2131 case VAEncMiscParameterTypeQualityLevel:
2132 status = ParseMiscParamQualityLevel((void *)miscParamBuf->data);
2133 break;
2134
2135 case VAEncMiscParameterTypeMaxSliceSize:
2136 status = ParseMiscParamMaxSliceSize((void *)miscParamBuf->data);
2137 break;
2138
2139 case VAEncMiscParameterTypeROI:
2140 status = ParseMiscParamROI((void *)miscParamBuf->data);
2141 break;
2142
2143 case VAEncMiscParameterTypeDirtyRect:
2144 status = ParseMiscParamDirtyROI((void *)miscParamBuf->data);
2145 break;
2146
2147 case VAEncMiscParameterTypeCustomRoundingControl:
2148 status = ParseMiscParamRounding((void *)miscParamBuf->data);
2149 break;
2150
2151 case VAEncMiscParameterTypeSubMbPartPel:
2152 status = ParseMiscParamSubMbPartPel((void *)miscParamBuf->data);
2153 break;
2154
2155 default:
2156 DDI_CODEC_ASSERTMESSAGE("unsupported misc parameter type.");
2157 status = VA_STATUS_ERROR_INVALID_PARAMETER;
2158 break;
2159 }
2160
2161 return status;
2162 }
2163
2164 void DdiEncodeAvc::GetSlcRefIdx(CODEC_PICTURE *picReference, CODEC_PICTURE *slcReference)
2165 {
2166 if (nullptr == picReference || nullptr == slcReference)
2167 {return;}
2168
2169 int32_t i = 0;
2170 if (slcReference->FrameIdx != CODEC_AVC_NUM_UNCOMPRESSED_SURFACE)
2171 {
2172 for (i = 0; i < CODEC_MAX_NUM_REF_FRAME; i++)
2173 {
2174 if (slcReference->FrameIdx == picReference[i].FrameIdx)
2175 {
2176 slcReference->FrameIdx = i;
2177 break;
2178 }
2179 }
2180 if (i == CODEC_MAX_NUM_REF_FRAME)
2181 {
2182 slcReference->FrameIdx = CODEC_AVC_NUM_UNCOMPRESSED_SURFACE;
2183 slcReference->PicFlags = PICTURE_INVALID;
2184 }
2185 }
2186 }
2187
2188 void DdiEncodeAvc::SetupCodecPicture(
2189 DDI_MEDIA_CONTEXT *mediaCtx,
2190 DDI_CODEC_RENDER_TARGET_TABLE *rtTbl,
2191 CODEC_PICTURE *codecHalPic,
2192 VAPictureH264 vaPic,
2193 bool fieldPicFlag,
2194 bool picReference,
2195 bool sliceReference)
2196 {
2197 if(vaPic.picture_id != DDI_CODEC_INVALID_FRAME_INDEX)
2198 {
2199 DDI_MEDIA_SURFACE *surface = MediaLibvaCommonNext::GetSurfaceFromVASurfaceID(mediaCtx, vaPic.picture_id);
2200 vaPic.frame_idx = GetRenderTargetID(rtTbl, surface);
2201 codecHalPic->FrameIdx = (uint8_t)vaPic.frame_idx;
2202 }
2203 else
2204 {
2205 vaPic.frame_idx = DDI_CODEC_INVALID_FRAME_INDEX;
2206 codecHalPic->FrameIdx = CODEC_AVC_NUM_UNCOMPRESSED_SURFACE - 1;
2207 }
2208
2209 if (picReference)
2210 {
2211 if (vaPic.frame_idx == DDI_CODEC_INVALID_FRAME_INDEX)
2212 {
2213 codecHalPic->PicFlags = PICTURE_INVALID;
2214 }
2215 else if ((vaPic.flags&VA_PICTURE_H264_LONG_TERM_REFERENCE) == VA_PICTURE_H264_LONG_TERM_REFERENCE)
2216 {
2217 codecHalPic->PicFlags = PICTURE_LONG_TERM_REFERENCE;
2218 }
2219 else
2220 {
2221 codecHalPic->PicFlags = PICTURE_SHORT_TERM_REFERENCE;
2222 }
2223 }
2224 else
2225 {
2226 if (fieldPicFlag)
2227 {
2228 if ((vaPic.flags&VA_PICTURE_H264_BOTTOM_FIELD) == VA_PICTURE_H264_BOTTOM_FIELD)
2229 {
2230 codecHalPic->PicFlags = PICTURE_BOTTOM_FIELD;
2231 }
2232 else
2233 {
2234 codecHalPic->PicFlags = PICTURE_TOP_FIELD;
2235 }
2236 }
2237 else
2238 {
2239 codecHalPic->PicFlags = PICTURE_FRAME;
2240 }
2241 }
2242
2243 if (sliceReference && (vaPic.picture_id == VA_INVALID_ID))//VA_INVALID_ID is used to indicate invalide picture in LIBVA.
2244 {
2245 codecHalPic->PicFlags = PICTURE_INVALID;
2246 }
2247 }
2248
2249 uint32_t DdiEncodeAvc::getSliceParameterBufferSize()
2250 {
2251 return sizeof(VAEncSliceParameterBufferH264);
2252 }
2253
2254 uint32_t DdiEncodeAvc::getSequenceParameterBufferSize()
2255 {
2256 return sizeof(VAEncSequenceParameterBufferH264);
2257 }
2258
2259 uint32_t DdiEncodeAvc::getPictureParameterBufferSize()
2260 {
2261 return sizeof(VAEncPictureParameterBufferH264);
2262 }
2263
2264 uint32_t DdiEncodeAvc::getQMatrixBufferSize()
2265 {
2266 return sizeof(VAIQMatrixBufferH264);
2267 }
2268
2269 void DdiEncodeAvc::ClearPicParams()
2270 {
2271 uint8_t ppsIdx = ((PCODEC_AVC_ENCODE_SLICE_PARAMS)(m_encodeCtx->pSliceParams))->pic_parameter_set_id;
2272 PCODEC_AVC_ENCODE_PIC_PARAMS picParams = (PCODEC_AVC_ENCODE_PIC_PARAMS)m_encodeCtx->pPicParams + ppsIdx;
2273
2274 if (picParams != nullptr && picParams->pDeltaQp != nullptr)
2275 {
2276 MOS_FreeMemory(picParams->pDeltaQp);
2277 picParams->pDeltaQp = nullptr;
2278 }
2279 }
2280
2281 CODECHAL_FUNCTION DdiEncodeAvc::GetEncodeCodecFunction(VAProfile profile, VAEntrypoint entrypoint, bool bVDEnc)
2282 {
2283 CODECHAL_FUNCTION codecFunction = CODECHAL_FUNCTION_INVALID;
2284 if (bVDEnc)
2285 {
2286 codecFunction = CODECHAL_FUNCTION_ENC_VDENC_PAK;
2287 }
2288 else
2289 {
2290 DDI_CODEC_ASSERTMESSAGE("Unsuported CODECHAL_FUNCTION");
2291 }
2292 return codecFunction;
2293 }
2294
2295 CODECHAL_MODE DdiEncodeAvc::GetEncodeCodecMode(
2296 VAProfile profile,
2297 VAEntrypoint entrypoint)
2298 {
2299 switch (profile)
2300 {
2301 case VAProfileH264Main:
2302 case VAProfileH264High:
2303 case VAProfileH264ConstrainedBaseline:
2304 return CODECHAL_ENCODE_MODE_AVC;
2305 default:
2306 DDI_CODEC_ASSERTMESSAGE("Unsuported CODECHAL_MODE");
2307 return CODECHAL_UNSUPPORTED_MODE;
2308 }
2309 }
2310
2311 }