| 1 |
/* |
| 2 |
* Copyright (c) 2017, Intel Corporation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included |
| 12 |
* in all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
//! |
| 23 |
//! \file media_ddi_encode_hevc.cpp |
| 24 |
//! \brief HEVC class definition for DDI media encoder. |
| 25 |
//! |
| 26 |
|
| 27 |
#include "media_ddi_base.h" |
| 28 |
#include "media_ddi_encode_base.h" |
| 29 |
#include "media_ddi_encode_hevc.h" |
| 30 |
#include "media_libva_util.h" |
| 31 |
#include "media_ddi_encode_const.h" |
| 32 |
#include "media_ddi_factory.h" |
| 33 |
#include "codechal_encoder_base.h" |
| 34 |
#include "codec_def_encode_hevc_trace.h" |
| 35 |
|
| 36 |
namespace |
| 37 |
{ |
| 38 |
namespace APISticker |
| 39 |
{ |
| 40 |
#define APIMAP_ELEM(e) {#e, __LINE__ - START_LINE} |
| 41 |
constexpr uint32_t START_LINE = __LINE__ + 2; |
| 42 |
const std::map<std::string, uint32_t> APIMap = { |
| 43 |
APIMAP_ELEM(DdiEncodeHevc::ContextInitialize), |
| 44 |
APIMAP_ELEM(RenderPicture), |
| 45 |
APIMAP_ELEM(EncodeInCodecHal), |
| 46 |
APIMAP_ELEM(ResetAtFrameLevel), |
| 47 |
APIMAP_ELEM(ParseSeqParams), |
| 48 |
APIMAP_ELEM(ParsePicParams), |
| 49 |
APIMAP_ELEM(ParseSlcParams), |
| 50 |
APIMAP_ELEM(Qmatrix), |
| 51 |
APIMAP_ELEM(FindNalUnitStartCodes), |
| 52 |
APIMAP_ELEM(ParsePackedHeaderParams), |
| 53 |
APIMAP_ELEM(ParsePackedHeaderData), |
| 54 |
APIMAP_ELEM(ParseMiscParams), |
| 55 |
}; |
| 56 |
#undef APIMAP_ELEM |
| 57 |
|
| 58 |
inline void TraceEnter(const char *api) |
| 59 |
{ |
| 60 |
try |
| 61 |
{ |
| 62 |
auto data = APIMap.at(api); |
| 63 |
|
| 64 |
MOS_TraceEventExt( |
| 65 |
TR_KEY_ENCODE_EVENT_API_STICKER, |
| 66 |
MT_EVENT_LEVEL::ALWAYS, |
| 67 |
EVENT_ENCODE_API_STICKER_HEVC, |
| 68 |
EVENT_TYPE_START, |
| 69 |
&data, |
| 70 |
sizeof(data)); |
| 71 |
} |
| 72 |
catch (...) |
| 73 |
{ |
| 74 |
return; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
inline void TraceExit(const char *api, VAStatus ret) |
| 79 |
{ |
| 80 |
try |
| 81 |
{ |
| 82 |
uint32_t data[] = {APIMap.at(api), static_cast<uint32_t>(ret)}; |
| 83 |
|
| 84 |
MOS_TraceEventExt( |
| 85 |
TR_KEY_ENCODE_EVENT_API_STICKER, |
| 86 |
MT_EVENT_LEVEL::CRITICAL, |
| 87 |
EVENT_ENCODE_API_STICKER_HEVC, |
| 88 |
EVENT_TYPE_END, |
| 89 |
data, |
| 90 |
sizeof(data)); |
| 91 |
} |
| 92 |
catch (...) |
| 93 |
{ |
| 94 |
return; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
#define API_STICKER_TRACE_ENTER() APISticker::TraceEnter(__func__) |
| 99 |
#define API_STICKER_TRACE_EXIT(ret) APISticker::TraceExit(__func__, ret) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
extern template class MediaDdiFactoryNoArg<DdiEncodeBase>; |
| 104 |
|
| 105 |
static bool isEncodeHevcRegistered = |
| 106 |
MediaDdiFactoryNoArg<DdiEncodeBase>::RegisterCodec<DdiEncodeHevc>(ENCODE_ID_HEVC); |
| 107 |
|
| 108 |
DdiEncodeHevc::~DdiEncodeHevc() |
| 109 |
{ |
| 110 |
if (m_encodeCtx == nullptr) |
| 111 |
{ |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
MOS_FreeMemory(m_encodeCtx->pSeqParams); |
| 116 |
m_encodeCtx->pSeqParams = nullptr; |
| 117 |
|
| 118 |
MOS_FreeMemory(((PCODEC_HEVC_ENCODE_PICTURE_PARAMS)m_encodeCtx->pPicParams)->pDirtyRect); |
| 119 |
((PCODEC_HEVC_ENCODE_PICTURE_PARAMS)m_encodeCtx->pPicParams)->pDirtyRect = nullptr; |
| 120 |
MOS_FreeMemory(m_encodeCtx->pPicParams); |
| 121 |
m_encodeCtx->pPicParams = nullptr; |
| 122 |
|
| 123 |
if (m_encodeCtx->ppNALUnitParams) |
| 124 |
{ |
| 125 |
// Allocate one contiguous memory for the NALUnitParams buffers |
| 126 |
// only need to free one time |
| 127 |
MOS_FreeMemory(m_encodeCtx->ppNALUnitParams[0]); |
| 128 |
m_encodeCtx->ppNALUnitParams[0] = nullptr; |
| 129 |
|
| 130 |
MOS_FreeMemory(m_encodeCtx->ppNALUnitParams); |
| 131 |
m_encodeCtx->ppNALUnitParams = nullptr; |
| 132 |
} |
| 133 |
|
| 134 |
MOS_FreeMemory(m_encodeCtx->pSliceParams); |
| 135 |
m_encodeCtx->pSliceParams = nullptr; |
| 136 |
|
| 137 |
MOS_FreeMemory(m_encodeCtx->pEncodeStatusReport); |
| 138 |
m_encodeCtx->pEncodeStatusReport = nullptr; |
| 139 |
|
| 140 |
MOS_FreeMemory(m_encodeCtx->pSEIFromApp->pSEIBuffer); |
| 141 |
m_encodeCtx->pSEIFromApp->pSEIBuffer = nullptr; |
| 142 |
MOS_FreeMemory(m_encodeCtx->pSEIFromApp); |
| 143 |
m_encodeCtx->pSEIFromApp = nullptr; |
| 144 |
|
| 145 |
MOS_FreeMemory(m_encodeCtx->pSliceHeaderData); |
| 146 |
m_encodeCtx->pSliceHeaderData = nullptr; |
| 147 |
|
| 148 |
MOS_FreeMemory(m_encodeCtx->pQmatrixParams); |
| 149 |
m_encodeCtx->pQmatrixParams = nullptr; |
| 150 |
|
| 151 |
if (m_encodeCtx->pbsBuffer) |
| 152 |
{ |
| 153 |
MOS_FreeMemory(m_encodeCtx->pbsBuffer->pBase); |
| 154 |
m_encodeCtx->pbsBuffer->pBase = nullptr; |
| 155 |
|
| 156 |
MOS_FreeMemory(m_encodeCtx->pbsBuffer); |
| 157 |
m_encodeCtx->pbsBuffer = nullptr; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
VAStatus DdiEncodeHevc::ContextInitialize( |
| 162 |
CodechalSetting *codecHalSettings) |
| 163 |
{ |
| 164 |
API_STICKER_TRACE_ENTER(); |
| 165 |
|
| 166 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx.", VA_STATUS_ERROR_INVALID_CONTEXT); |
| 167 |
DDI_CHK_NULL(m_encodeCtx->pCpDdiInterface, "nullptr m_encodeCtx->pCpDdiInterface.", VA_STATUS_ERROR_INVALID_CONTEXT); |
| 168 |
DDI_CHK_NULL(codecHalSettings, "nullptr codecHalSettings.", VA_STATUS_ERROR_INVALID_CONTEXT); |
| 169 |
|
| 170 |
if (true == m_encodeCtx->bVdencActive) |
| 171 |
{ |
| 172 |
codecHalSettings->codecFunction = CODECHAL_FUNCTION_ENC_VDENC_PAK; |
| 173 |
codecHalSettings->disableUltraHME = false; |
| 174 |
codecHalSettings->disableSuperHME = false; |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
codecHalSettings->codecFunction = CODECHAL_FUNCTION_ENC_PAK; |
| 179 |
} |
| 180 |
codecHalSettings->height = m_encodeCtx->dworiFrameHeight; |
| 181 |
codecHalSettings->width = m_encodeCtx->dworiFrameWidth; |
| 182 |
codecHalSettings->mode = m_encodeCtx->wModeType; |
| 183 |
codecHalSettings->standard = CODECHAL_HEVC; |
| 184 |
|
| 185 |
if(m_encodeCtx->vaProfile==VAProfileHEVCMain || m_encodeCtx->vaProfile==VAProfileHEVCSccMain) |
| 186 |
{ |
| 187 |
codecHalSettings->chromaFormat = HCP_CHROMA_FORMAT_YUV420; |
| 188 |
codecHalSettings->lumaChromaDepth = CODECHAL_LUMA_CHROMA_DEPTH_8_BITS; |
| 189 |
} |
| 190 |
else if(m_encodeCtx->vaProfile==VAProfileHEVCMain10 || m_encodeCtx->vaProfile==VAProfileHEVCSccMain10) |
| 191 |
{ |
| 192 |
codecHalSettings->chromaFormat = HCP_CHROMA_FORMAT_YUV420; |
| 193 |
codecHalSettings->lumaChromaDepth = CODECHAL_LUMA_CHROMA_DEPTH_10_BITS; |
| 194 |
} |
| 195 |
else if(m_encodeCtx->vaProfile == VAProfileHEVCMain12) |
| 196 |
{ |
| 197 |
codecHalSettings->chromaFormat = HCP_CHROMA_FORMAT_YUV420; |
| 198 |
codecHalSettings->lumaChromaDepth = CODECHAL_LUMA_CHROMA_DEPTH_12_BITS; |
| 199 |
} |
| 200 |
else if(m_encodeCtx->vaProfile == VAProfileHEVCMain422_10) |
| 201 |
{ |
| 202 |
codecHalSettings->chromaFormat = HCP_CHROMA_FORMAT_YUV422; |
| 203 |
codecHalSettings->lumaChromaDepth = CODECHAL_LUMA_CHROMA_DEPTH_10_BITS; |
| 204 |
} |
| 205 |
else if(m_encodeCtx->vaProfile == VAProfileHEVCMain422_12) |
| 206 |
{ |
| 207 |
codecHalSettings->chromaFormat = HCP_CHROMA_FORMAT_YUV422; |
| 208 |
codecHalSettings->lumaChromaDepth = CODECHAL_LUMA_CHROMA_DEPTH_12_BITS; |
| 209 |
} |
| 210 |
else if (m_encodeCtx->vaProfile == VAProfileHEVCMain444 || m_encodeCtx->vaProfile==VAProfileHEVCSccMain444) |
| 211 |
{ |
| 212 |
codecHalSettings->chromaFormat = HCP_CHROMA_FORMAT_YUV444; |
| 213 |
codecHalSettings->lumaChromaDepth = CODECHAL_LUMA_CHROMA_DEPTH_8_BITS; |
| 214 |
} |
| 215 |
else if (m_encodeCtx->vaProfile == VAProfileHEVCMain444_10 || m_encodeCtx->vaProfile==VAProfileHEVCSccMain444_10) |
| 216 |
{ |
| 217 |
codecHalSettings->chromaFormat = HCP_CHROMA_FORMAT_YUV444; |
| 218 |
codecHalSettings->lumaChromaDepth = CODECHAL_LUMA_CHROMA_DEPTH_10_BITS; |
| 219 |
} |
| 220 |
else if (m_encodeCtx->vaProfile == VAProfileHEVCMain444_12) |
| 221 |
{ |
| 222 |
codecHalSettings->chromaFormat = HCP_CHROMA_FORMAT_YUV444; |
| 223 |
codecHalSettings->lumaChromaDepth = CODECHAL_LUMA_CHROMA_DEPTH_12_BITS; |
| 224 |
} |
| 225 |
codecHalSettings->isSCCEnabled = IsSccProfile(); |
| 226 |
|
| 227 |
VAStatus eStatus = VA_STATUS_SUCCESS; |
| 228 |
|
| 229 |
// Allocate sequence params |
| 230 |
m_encodeCtx->pSeqParams = (void *)MOS_AllocAndZeroMemory(sizeof(CODEC_HEVC_ENCODE_SEQUENCE_PARAMS)); |
| 231 |
DDI_CHK_NULL(m_encodeCtx->pSeqParams, "nullptr m_encodeCtx->pSeqParams.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 232 |
|
| 233 |
// Allocate picture params |
| 234 |
m_encodeCtx->pPicParams = (void *)MOS_AllocAndZeroMemory(sizeof(CODEC_HEVC_ENCODE_PICTURE_PARAMS)); |
| 235 |
DDI_CHK_NULL(m_encodeCtx->pPicParams, "nullptr m_encodeCtx->pPicParams.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 236 |
|
| 237 |
// Allocate NAL unit params |
| 238 |
m_encodeCtx->ppNALUnitParams = (PCODECHAL_NAL_UNIT_PARAMS *)MOS_AllocAndZeroMemory(sizeof(PCODECHAL_NAL_UNIT_PARAMS) * HEVC_MAX_NAL_UNIT_TYPE); |
| 239 |
DDI_CHK_NULL(m_encodeCtx->ppNALUnitParams, "nullptr m_encodeCtx->ppNALUnitParams.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 240 |
|
| 241 |
PCODECHAL_NAL_UNIT_PARAMS nalUnitParams = (CODECHAL_NAL_UNIT_PARAMS *)MOS_AllocAndZeroMemory(sizeof(CODECHAL_NAL_UNIT_PARAMS) * HEVC_MAX_NAL_UNIT_TYPE); |
| 242 |
DDI_CHK_NULL(nalUnitParams, "nullptr nalUnitParams.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 243 |
|
| 244 |
for (uint32_t i = 0; i < HEVC_MAX_NAL_UNIT_TYPE; i++) |
| 245 |
{ |
| 246 |
m_encodeCtx->ppNALUnitParams[i] = &(nalUnitParams[i]); |
| 247 |
} |
| 248 |
|
| 249 |
// Allocate SliceParams |
| 250 |
uint32_t picSizeInMb = m_encodeCtx->wPicHeightInMB * m_encodeCtx->wPicWidthInMB; |
| 251 |
// Supports one LCU per slice, with minimum LCU size of 16x16 |
| 252 |
m_encodeCtx->pSliceParams = (void *)MOS_AllocAndZeroMemory(picSizeInMb * sizeof(CODEC_HEVC_ENCODE_SLICE_PARAMS)); |
| 253 |
DDI_CHK_NULL(m_encodeCtx->pSliceParams, "nullptr m_encodeCtx->pSliceParams.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 254 |
|
| 255 |
// Allocate Encode Status Report |
| 256 |
m_encodeCtx->pEncodeStatusReport = (void *)MOS_AllocAndZeroMemory(CODECHAL_ENCODE_STATUS_NUM * sizeof(EncodeStatusReport)); |
| 257 |
DDI_CHK_NULL(m_encodeCtx->pEncodeStatusReport, "nullptr m_encodeCtx->pEncodeStatusReport.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 258 |
|
| 259 |
// Allocate SEI structure |
| 260 |
m_encodeCtx->pSEIFromApp = (CodechalEncodeSeiData *)MOS_AllocAndZeroMemory(sizeof(CodechalEncodeSeiData)); |
| 261 |
DDI_CHK_NULL(m_encodeCtx->pSEIFromApp, "nullptr m_encodeCtx->pSEIFromApp.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 262 |
|
| 263 |
// Allocate Qmatrix structure |
| 264 |
m_encodeCtx->pQmatrixParams = (void *)MOS_AllocAndZeroMemory(sizeof(CODECHAL_HEVC_IQ_MATRIX_PARAMS)); |
| 265 |
DDI_CHK_NULL(m_encodeCtx->pQmatrixParams, "nullptr QMatrixParams", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 266 |
|
| 267 |
// for slice header from application |
| 268 |
m_encodeCtx->pSliceHeaderData = (CODEC_ENCODER_SLCDATA *)MOS_AllocAndZeroMemory(picSizeInMb * sizeof(CODEC_ENCODER_SLCDATA)); |
| 269 |
DDI_CHK_NULL(m_encodeCtx->pSliceHeaderData, "nullptr m_encodeCtx->pSliceHeaderData.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 270 |
|
| 271 |
// Create the bit stream buffer to hold the packed headers from application |
| 272 |
m_encodeCtx->pbsBuffer = (BSBuffer *)MOS_AllocAndZeroMemory(sizeof(BSBuffer)); |
| 273 |
DDI_CHK_NULL(m_encodeCtx->pbsBuffer, "nullptr m_encodeCtx->pbsBuffer.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 274 |
|
| 275 |
m_encodeCtx->pbsBuffer->BufferSize = picSizeInMb * PACKED_HEADER_SIZE_PER_ROW; |
| 276 |
m_encodeCtx->pbsBuffer->pBase = (uint8_t *)MOS_AllocAndZeroMemory(m_encodeCtx->pbsBuffer->BufferSize); |
| 277 |
DDI_CHK_NULL(m_encodeCtx->pbsBuffer->pBase, "nullptr m_encodeCtx->pbsBuffer->pBase.", VA_STATUS_ERROR_ALLOCATION_FAILED); |
| 278 |
|
| 279 |
API_STICKER_TRACE_EXIT(eStatus); |
| 280 |
return eStatus; |
| 281 |
} |
| 282 |
|
| 283 |
VAStatus DdiEncodeHevc::RenderPicture(VADriverContextP ctx, VAContextID context, VABufferID *buffers, int32_t numBuffers) |
| 284 |
{ |
| 285 |
API_STICKER_TRACE_ENTER(); |
| 286 |
VAStatus vaStatus = VA_STATUS_SUCCESS; |
| 287 |
|
| 288 |
DDI_FUNCTION_ENTER(); |
| 289 |
|
| 290 |
DDI_CHK_NULL(ctx, "nullptr context", VA_STATUS_ERROR_INVALID_CONTEXT); |
| 291 |
|
| 292 |
DDI_MEDIA_CONTEXT *mediaCtx = DdiMedia_GetMediaContext(ctx); |
| 293 |
DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT); |
| 294 |
|
| 295 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_CONTEXT); |
| 296 |
|
| 297 |
for (int32_t i = 0; i < numBuffers; i++) |
| 298 |
{ |
| 299 |
DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, buffers[i]); |
| 300 |
DDI_CHK_NULL(buf, "Invalid buffer.", VA_STATUS_ERROR_INVALID_BUFFER); |
| 301 |
if (buf->uiType == VAEncMacroblockDisableSkipMapBufferType) |
| 302 |
{ |
| 303 |
DdiMedia_MediaBufferToMosResource(buf, &(m_encodeCtx->resPerMBSkipMapBuffer)); |
| 304 |
m_encodeCtx->bMbDisableSkipMapEnabled = true; |
| 305 |
continue; |
| 306 |
} |
| 307 |
uint32_t dataSize = buf->iSize; |
| 308 |
// can use internal function instead of DdiMedia_MapBuffer here? |
| 309 |
void *data = nullptr; |
| 310 |
DdiMedia_MapBuffer(ctx, buffers[i], &data); |
| 311 |
|
| 312 |
DDI_CHK_NULL(data, "nullptr data.", VA_STATUS_ERROR_INVALID_BUFFER); |
| 313 |
|
| 314 |
switch (buf->uiType) |
| 315 |
{ |
| 316 |
case VAQMatrixBufferType: |
| 317 |
DDI_CHK_STATUS(Qmatrix(data), VA_STATUS_ERROR_INVALID_BUFFER); |
| 318 |
break; |
| 319 |
|
| 320 |
case VAEncSequenceParameterBufferType: |
| 321 |
DDI_CHK_STATUS(ParseSeqParams(data), VA_STATUS_ERROR_INVALID_BUFFER); |
| 322 |
m_encodeCtx->bNewSeq = true; |
| 323 |
break; |
| 324 |
|
| 325 |
case VAEncPictureParameterBufferType: |
| 326 |
DDI_CHK_STATUS(ParsePicParams(mediaCtx, data), VA_STATUS_ERROR_INVALID_BUFFER); |
| 327 |
DDI_CHK_STATUS( |
| 328 |
AddToStatusReportQueue((void *)m_encodeCtx->resBitstreamBuffer.bo), |
| 329 |
VA_STATUS_ERROR_INVALID_BUFFER); |
| 330 |
break; |
| 331 |
|
| 332 |
case VAEncSliceParameterBufferType: |
| 333 |
{ |
| 334 |
uint32_t numSlices = buf->uiNumElements; |
| 335 |
DDI_CHK_STATUS(ParseSlcParams(mediaCtx, data, numSlices), VA_STATUS_ERROR_INVALID_BUFFER); |
| 336 |
break; |
| 337 |
} |
| 338 |
|
| 339 |
case VAEncPackedHeaderParameterBufferType: |
| 340 |
vaStatus = ParsePackedHeaderParams(data); |
| 341 |
break; |
| 342 |
|
| 343 |
case VAEncPackedHeaderDataBufferType: |
| 344 |
vaStatus = ParsePackedHeaderData(data); |
| 345 |
break; |
| 346 |
|
| 347 |
case VAEncMiscParameterBufferType: |
| 348 |
DDI_CHK_STATUS(ParseMiscParams(data), VA_STATUS_ERROR_INVALID_BUFFER); |
| 349 |
break; |
| 350 |
|
| 351 |
case VAEncQPBufferType: |
| 352 |
DdiMedia_MediaBufferToMosResource(buf, &m_encodeCtx->resMBQpBuffer); |
| 353 |
m_encodeCtx->bMBQpEnable = true; |
| 354 |
break; |
| 355 |
|
| 356 |
default: |
| 357 |
DDI_ASSERTMESSAGE("not supported buffer type."); |
| 358 |
break; |
| 359 |
} |
| 360 |
DdiMedia_UnmapBuffer(ctx, buffers[i]); |
| 361 |
} |
| 362 |
|
| 363 |
DDI_FUNCTION_EXIT(vaStatus); |
| 364 |
API_STICKER_TRACE_EXIT(vaStatus); |
| 365 |
return vaStatus; |
| 366 |
} |
| 367 |
|
| 368 |
VAStatus DdiEncodeHevc::EncodeInCodecHal(uint32_t numSlices) |
| 369 |
{ |
| 370 |
API_STICKER_TRACE_ENTER(); |
| 371 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 372 |
DDI_CHK_NULL(m_encodeCtx->pCodecHal, "nullptr m_encodeCtx->pCodecHal", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 373 |
|
| 374 |
DDI_CODEC_RENDER_TARGET_TABLE *rtTbl = &(m_encodeCtx->RTtbl); |
| 375 |
|
| 376 |
EncoderParams encodeParams; |
| 377 |
MOS_ZeroMemory(&encodeParams, sizeof(encodeParams)); |
| 378 |
|
| 379 |
if (m_encodeCtx->bVdencActive) |
| 380 |
{ |
| 381 |
encodeParams.ExecCodecFunction = CODECHAL_FUNCTION_ENC_VDENC_PAK; |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
encodeParams.ExecCodecFunction = CODECHAL_FUNCTION_ENC_PAK; |
| 386 |
} |
| 387 |
|
| 388 |
// Raw Surface |
| 389 |
MOS_SURFACE rawSurface; |
| 390 |
MOS_ZeroMemory(&rawSurface, sizeof(rawSurface)); |
| 391 |
rawSurface.dwOffset = 0; |
| 392 |
|
| 393 |
DdiMedia_MediaSurfaceToMosResource(rtTbl->pCurrentRT, &(rawSurface.OsResource)); |
| 394 |
|
| 395 |
// Recon Surface |
| 396 |
MOS_SURFACE reconSurface; |
| 397 |
MOS_ZeroMemory(&reconSurface, sizeof(reconSurface)); |
| 398 |
reconSurface.dwOffset = 0; |
| 399 |
|
| 400 |
DdiMedia_MediaSurfaceToMosResource(rtTbl->pCurrentReconTarget, &(reconSurface.OsResource)); |
| 401 |
|
| 402 |
//clear registered recon/ref surface flags |
| 403 |
DDI_CHK_RET(ClearRefList(&m_encodeCtx->RTtbl, true), "ClearRefList failed!"); |
| 404 |
|
| 405 |
// Bitstream surface |
| 406 |
MOS_RESOURCE bitstreamSurface; |
| 407 |
MOS_ZeroMemory(&bitstreamSurface, sizeof(bitstreamSurface)); |
| 408 |
bitstreamSurface = m_encodeCtx->resBitstreamBuffer; // in render picture |
| 409 |
bitstreamSurface.Format = Format_Buffer; |
| 410 |
|
| 411 |
encodeParams.psRawSurface = &rawSurface; |
| 412 |
encodeParams.psReconSurface = &reconSurface; |
| 413 |
encodeParams.presBitstreamBuffer = &bitstreamSurface; |
| 414 |
|
| 415 |
MOS_SURFACE mbQpSurface; |
| 416 |
if (m_encodeCtx->bMBQpEnable) |
| 417 |
{ |
| 418 |
// MBQp surface |
| 419 |
MOS_ZeroMemory(&mbQpSurface, sizeof(mbQpSurface)); |
| 420 |
mbQpSurface.Format = Format_Buffer_2D; |
| 421 |
mbQpSurface.dwOffset = 0; |
| 422 |
mbQpSurface.OsResource = m_encodeCtx->resMBQpBuffer; |
| 423 |
|
| 424 |
encodeParams.psMbQpDataSurface = &mbQpSurface; |
| 425 |
encodeParams.bMbQpDataEnabled = true; |
| 426 |
} |
| 427 |
|
| 428 |
PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS hevcSeqParams = (PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS)((uint8_t *)m_encodeCtx->pSeqParams); |
| 429 |
if (m_encodeCtx->bNewSeq) |
| 430 |
{ |
| 431 |
hevcSeqParams->TargetUsage = m_encodeCtx->targetUsage; |
| 432 |
} |
| 433 |
encodeParams.pSeqParams = m_encodeCtx->pSeqParams; |
| 434 |
encodeParams.pVuiParams = m_encodeCtx->pVuiParams; |
| 435 |
encodeParams.pPicParams = m_encodeCtx->pPicParams; |
| 436 |
encodeParams.pSliceParams = m_encodeCtx->pSliceParams; |
| 437 |
encodeParams.pIQMatrixBuffer = m_encodeCtx->pQmatrixParams; |
| 438 |
|
| 439 |
// Sequence data |
| 440 |
encodeParams.bNewSeq = m_encodeCtx->bNewSeq; |
| 441 |
|
| 442 |
// VUI |
| 443 |
encodeParams.bNewVuiData = m_encodeCtx->bNewVuiData; |
| 444 |
|
| 445 |
// Slice level data |
| 446 |
encodeParams.dwNumSlices = numSlices; |
| 447 |
|
| 448 |
// IQmatrix params |
| 449 |
encodeParams.bNewQmatrixData = m_encodeCtx->bNewQmatrixData; |
| 450 |
encodeParams.bPicQuant = m_encodeCtx->bPicQuant; |
| 451 |
encodeParams.ppNALUnitParams = m_encodeCtx->ppNALUnitParams; |
| 452 |
encodeParams.uiNumNalUnits = m_encodeCtx->indexNALUnit; |
| 453 |
encodeParams.pSeiData = m_encodeCtx->pSEIFromApp; |
| 454 |
encodeParams.pSeiParamBuffer = m_encodeCtx->pSEIFromApp->pSEIBuffer; |
| 455 |
encodeParams.dwSEIDataOffset = 0; |
| 456 |
|
| 457 |
// whether driver need to pack slice header |
| 458 |
if (m_encodeCtx->bHavePackedSliceHdr) |
| 459 |
{ |
| 460 |
encodeParams.bAcceleratorHeaderPackingCaps = false; |
| 461 |
} |
| 462 |
else |
| 463 |
{ |
| 464 |
encodeParams.bAcceleratorHeaderPackingCaps = true; |
| 465 |
} |
| 466 |
|
| 467 |
encodeParams.pBSBuffer = m_encodeCtx->pbsBuffer; |
| 468 |
encodeParams.pSlcHeaderData = (void *)m_encodeCtx->pSliceHeaderData; |
| 469 |
|
| 470 |
CodechalEncoderState *encoder = dynamic_cast<CodechalEncoderState *>(m_encodeCtx->pCodecHal); |
| 471 |
if(encoder != nullptr) |
| 472 |
{ |
| 473 |
encoder->m_mfeEncodeParams.submitIndex = 0; |
| 474 |
encoder->m_mfeEncodeParams.submitNumber = 1; //By default we only use one stream |
| 475 |
encoder->m_mfeEncodeParams.streamId = 0; |
| 476 |
} |
| 477 |
|
| 478 |
MOS_STATUS status = m_encodeCtx->pCodecHal->Execute(&encodeParams); |
| 479 |
if (MOS_STATUS_SUCCESS != status) |
| 480 |
{ |
| 481 |
DDI_ASSERTMESSAGE("DDI:Failed in Codechal!"); |
| 482 |
return VA_STATUS_ERROR_ENCODING_ERROR; |
| 483 |
} |
| 484 |
|
| 485 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 486 |
return VA_STATUS_SUCCESS; |
| 487 |
} |
| 488 |
|
| 489 |
VAStatus DdiEncodeHevc::ResetAtFrameLevel() |
| 490 |
{ |
| 491 |
API_STICKER_TRACE_ENTER(); |
| 492 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 493 |
|
| 494 |
// Assume there is only one SPS parameter |
| 495 |
PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS hevcSeqParams = (PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams); |
| 496 |
hevcSeqParams->bResetBRC = 0x0; |
| 497 |
|
| 498 |
m_encodeCtx->dwNumSlices = 0x0; |
| 499 |
m_encodeCtx->indexNALUnit = 0x0; |
| 500 |
m_encodeCtx->uiSliceHeaderCnt = 0x0; |
| 501 |
|
| 502 |
// reset bsbuffer every frame |
| 503 |
m_encodeCtx->pbsBuffer->pCurrent = m_encodeCtx->pbsBuffer->pBase; |
| 504 |
m_encodeCtx->pbsBuffer->SliceOffset = 0x0; |
| 505 |
m_encodeCtx->pbsBuffer->BitOffset = 0x0; |
| 506 |
m_encodeCtx->pbsBuffer->BitSize = 0x0; |
| 507 |
|
| 508 |
// clear the packed header information |
| 509 |
if (nullptr != m_encodeCtx->ppNALUnitParams) |
| 510 |
{ |
| 511 |
MOS_ZeroMemory(m_encodeCtx->ppNALUnitParams[0], sizeof(CODECHAL_NAL_UNIT_PARAMS) * HEVC_MAX_NAL_UNIT_TYPE); |
| 512 |
} |
| 513 |
|
| 514 |
m_encodeCtx->bHavePackedSliceHdr = false; |
| 515 |
m_encodeCtx->bLastPackedHdrIsSlice = false; |
| 516 |
m_encodeCtx->bMBQpEnable = false; |
| 517 |
|
| 518 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 519 |
return VA_STATUS_SUCCESS; |
| 520 |
} |
| 521 |
|
| 522 |
VAStatus DdiEncodeHevc::ParseSeqParams(void *ptr) |
| 523 |
{ |
| 524 |
API_STICKER_TRACE_ENTER(); |
| 525 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 526 |
DDI_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 527 |
|
| 528 |
VAEncSequenceParameterBufferHEVC *seqParams = (VAEncSequenceParameterBufferHEVC *)ptr; |
| 529 |
|
| 530 |
PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS hevcSeqParams = (PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS)((uint8_t *)m_encodeCtx->pSeqParams); |
| 531 |
DDI_CHK_NULL(hevcSeqParams, "nullptr hevcSeqParams", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 532 |
//MOS_ZeroMemory(hevcSeqParams, sizeof(CODEC_HEVC_ENCODE_SEQUENCE_PARAMS)); |
| 533 |
|
| 534 |
uint8_t log2MinCUSize = seqParams->log2_min_luma_coding_block_size_minus3 + 3; |
| 535 |
|
| 536 |
hevcSeqParams->wFrameWidthInMinCbMinus1 = (seqParams->pic_width_in_luma_samples >> log2MinCUSize) - 1; |
| 537 |
hevcSeqParams->wFrameHeightInMinCbMinus1 = (seqParams->pic_height_in_luma_samples >> log2MinCUSize) - 1; |
| 538 |
hevcSeqParams->general_profile_idc = seqParams->general_profile_idc; |
| 539 |
hevcSeqParams->Level = seqParams->general_level_idc / 3; // App sends (30*level), we track as (10*level) |
| 540 |
hevcSeqParams->general_tier_flag = seqParams->general_tier_flag; |
| 541 |
hevcSeqParams->GopPicSize = seqParams->intra_period; |
| 542 |
hevcSeqParams->GopRefDist = seqParams->ip_period; |
| 543 |
hevcSeqParams->chroma_format_idc = seqParams->seq_fields.bits.chroma_format_idc; |
| 544 |
|
| 545 |
hevcSeqParams->RateControlMethod = VARC2HalRC(m_encodeCtx->uiRCMethod); |
| 546 |
|
| 547 |
hevcSeqParams->TargetBitRate = MOS_ROUNDUP_DIVIDE(seqParams->bits_per_second, CODECHAL_ENCODE_BRC_KBPS); |
| 548 |
hevcSeqParams->MaxBitRate = MOS_ROUNDUP_DIVIDE(seqParams->bits_per_second, CODECHAL_ENCODE_BRC_KBPS); |
| 549 |
hevcSeqParams->MinBitRate = MOS_ROUNDUP_DIVIDE(seqParams->bits_per_second, CODECHAL_ENCODE_BRC_KBPS); |
| 550 |
// if didn't setting FrameRate, set to 30 by default, can be overwritten by misc paramter |
| 551 |
if (!hevcSeqParams->FrameRate.Numerator) { |
| 552 |
hevcSeqParams->FrameRate.Numerator = 3000; |
| 553 |
hevcSeqParams->FrameRate.Denominator = 100; |
| 554 |
} |
| 555 |
|
| 556 |
// set default same as application, can be overwritten by HRD params |
| 557 |
hevcSeqParams->InitVBVBufferFullnessInBit = (uint32_t)seqParams->bits_per_second; |
| 558 |
hevcSeqParams->VBVBufferSizeInBit = (uint32_t)seqParams->bits_per_second << 1; |
| 559 |
|
| 560 |
hevcSeqParams->scaling_list_enable_flag = seqParams->seq_fields.bits.scaling_list_enabled_flag; |
| 561 |
hevcSeqParams->sps_temporal_mvp_enable_flag = seqParams->seq_fields.bits.sps_temporal_mvp_enabled_flag; |
| 562 |
hevcSeqParams->strong_intra_smoothing_enable_flag = seqParams->seq_fields.bits.strong_intra_smoothing_enabled_flag; |
| 563 |
hevcSeqParams->amp_enabled_flag = seqParams->seq_fields.bits.amp_enabled_flag; |
| 564 |
hevcSeqParams->SAO_enabled_flag = seqParams->seq_fields.bits.sample_adaptive_offset_enabled_flag; |
| 565 |
hevcSeqParams->pcm_enabled_flag = seqParams->seq_fields.bits.pcm_enabled_flag; |
| 566 |
hevcSeqParams->pcm_loop_filter_disable_flag = seqParams->seq_fields.bits.pcm_loop_filter_disabled_flag; |
| 567 |
hevcSeqParams->LowDelayMode = seqParams->seq_fields.bits.low_delay_seq; |
| 568 |
hevcSeqParams->HierarchicalFlag = seqParams->seq_fields.bits.hierachical_flag; |
| 569 |
|
| 570 |
hevcSeqParams->log2_max_coding_block_size_minus3 = seqParams->log2_diff_max_min_luma_coding_block_size + |
| 571 |
seqParams->log2_min_luma_coding_block_size_minus3; |
| 572 |
hevcSeqParams->log2_min_coding_block_size_minus3 = seqParams->log2_min_luma_coding_block_size_minus3; |
| 573 |
hevcSeqParams->log2_max_transform_block_size_minus2 = seqParams->log2_diff_max_min_transform_block_size + |
| 574 |
seqParams->log2_min_transform_block_size_minus2; |
| 575 |
hevcSeqParams->log2_min_transform_block_size_minus2 = seqParams->log2_min_transform_block_size_minus2; |
| 576 |
hevcSeqParams->max_transform_hierarchy_depth_intra = seqParams->max_transform_hierarchy_depth_intra; |
| 577 |
hevcSeqParams->max_transform_hierarchy_depth_inter = seqParams->max_transform_hierarchy_depth_inter; |
| 578 |
hevcSeqParams->log2_min_PCM_cb_size_minus3 = seqParams->log2_min_pcm_luma_coding_block_size_minus3; |
| 579 |
hevcSeqParams->log2_max_PCM_cb_size_minus3 = seqParams->log2_max_pcm_luma_coding_block_size_minus3; |
| 580 |
hevcSeqParams->bit_depth_luma_minus8 = seqParams->seq_fields.bits.bit_depth_luma_minus8; |
| 581 |
hevcSeqParams->bit_depth_chroma_minus8 = seqParams->seq_fields.bits.bit_depth_chroma_minus8; |
| 582 |
|
| 583 |
if (m_codechalSettings->isSCCEnabled) |
| 584 |
{ |
| 585 |
hevcSeqParams->palette_mode_enabled_flag = seqParams->scc_fields.bits.palette_mode_enabled_flag; |
| 586 |
hevcSeqParams->motion_vector_resolution_control_idc = 0; |
| 587 |
} |
| 588 |
|
| 589 |
CodecDefEncodeHevcTrace::TraceDDI(*hevcSeqParams); |
| 590 |
|
| 591 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 592 |
return VA_STATUS_SUCCESS; |
| 593 |
} |
| 594 |
|
| 595 |
VAStatus DdiEncodeHevc::ParsePicParams( |
| 596 |
DDI_MEDIA_CONTEXT *mediaCtx, |
| 597 |
void *ptr) |
| 598 |
{ |
| 599 |
API_STICKER_TRACE_ENTER(); |
| 600 |
DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 601 |
DDI_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 602 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 603 |
|
| 604 |
VAEncPictureParameterBufferHEVC *picParams = (VAEncPictureParameterBufferHEVC *)ptr; |
| 605 |
|
| 606 |
PCODEC_HEVC_ENCODE_PICTURE_PARAMS hevcPicParams = (PCODEC_HEVC_ENCODE_PICTURE_PARAMS)((uint8_t *)m_encodeCtx->pPicParams); |
| 607 |
DDI_CHK_NULL(hevcPicParams, "nullptr hevcPicParams", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 608 |
MOS_ZeroMemory(hevcPicParams, sizeof(CODEC_HEVC_ENCODE_PICTURE_PARAMS)); |
| 609 |
|
| 610 |
PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS hevcSeqParams = (PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS)((uint8_t *)m_encodeCtx->pSeqParams); |
| 611 |
|
| 612 |
DDI_MEDIA_SURFACE *surface; |
| 613 |
if(picParams->decoded_curr_pic.picture_id != VA_INVALID_SURFACE) |
| 614 |
{ |
| 615 |
surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, picParams->decoded_curr_pic.picture_id); |
| 616 |
|
| 617 |
if(m_encodeCtx->vaProfile == VAProfileHEVCMain444 |
| 618 |
|| m_encodeCtx->vaProfile == VAProfileHEVCMain444_10 |
| 619 |
|| m_encodeCtx->vaProfile == VAProfileHEVCMain444_12 |
| 620 |
|| m_encodeCtx->vaProfile == VAProfileHEVCMain422_10 |
| 621 |
|| m_encodeCtx->vaProfile == VAProfileHEVCMain422_12 |
| 622 |
|| m_encodeCtx->vaProfile == VAProfileHEVCMain10 |
| 623 |
|| m_encodeCtx->vaProfile == VAProfileHEVCMain12 |
| 624 |
|| m_encodeCtx->vaProfile == VAProfileHEVCSccMain10 |
| 625 |
|| m_encodeCtx->vaProfile == VAProfileHEVCSccMain444 |
| 626 |
|| m_encodeCtx->vaProfile == VAProfileHEVCSccMain444_10) |
| 627 |
{ |
| 628 |
surface = DdiMedia_ReplaceSurfaceWithVariant(surface, m_encodeCtx->vaEntrypoint); |
| 629 |
} |
| 630 |
DDI_CHK_RET(RegisterRTSurfaces(&(m_encodeCtx->RTtbl),surface), "RegisterRTSurfaces failed!"); |
| 631 |
} |
| 632 |
|
| 633 |
// Curr Recon Pic |
| 634 |
SetupCodecPicture( |
| 635 |
mediaCtx, |
| 636 |
&(m_encodeCtx->RTtbl), |
| 637 |
&hevcPicParams->CurrReconstructedPic, |
| 638 |
picParams->decoded_curr_pic, |
| 639 |
false, |
| 640 |
false); |
| 641 |
|
| 642 |
DDI_CODEC_RENDER_TARGET_TABLE *rtTbl = &(m_encodeCtx->RTtbl); |
| 643 |
rtTbl->pCurrentReconTarget = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, picParams->decoded_curr_pic.picture_id); |
| 644 |
// The surface for reconstructed frame is not registered, return error to app |
| 645 |
if (nullptr == rtTbl->pCurrentReconTarget) |
| 646 |
{ |
| 647 |
DDI_ASSERTMESSAGE("invalid surface for reconstructed frame"); |
| 648 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 649 |
} |
| 650 |
|
| 651 |
// curr orig pic |
| 652 |
hevcPicParams->CurrOriginalPic.FrameIdx = (uint8_t)GetRenderTargetID(rtTbl, rtTbl->pCurrentReconTarget); |
| 653 |
hevcPicParams->CurrOriginalPic.PicFlags = hevcPicParams->CurrReconstructedPic.PicFlags; |
| 654 |
hevcPicParams->CurrOriginalPic.PicEntry = hevcPicParams->CurrReconstructedPic.PicEntry; |
| 655 |
hevcPicParams->CollocatedRefPicIndex = picParams->collocated_ref_pic_index; |
| 656 |
|
| 657 |
// RefFrame List |
| 658 |
for (uint32_t i = 0; i < numMaxRefFrame; i++) |
| 659 |
{ |
| 660 |
if(picParams->reference_frames[i].picture_id != VA_INVALID_SURFACE) |
| 661 |
{ |
| 662 |
DDI_CHK_RET(UpdateRegisteredRTSurfaceFlag(&(m_encodeCtx->RTtbl), DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, picParams->reference_frames[i].picture_id)), "RegisterRTSurfaces failed!"); |
| 663 |
} |
| 664 |
SetupCodecPicture( |
| 665 |
mediaCtx, |
| 666 |
&(m_encodeCtx->RTtbl), |
| 667 |
&(hevcPicParams->RefFrameList[i]), |
| 668 |
picParams->reference_frames[i], |
| 669 |
true, |
| 670 |
false); |
| 671 |
|
| 672 |
hevcPicParams->RefFramePOCList[i] = picParams->reference_frames[i].pic_order_cnt; |
| 673 |
} |
| 674 |
|
| 675 |
hevcPicParams->CurrPicOrderCnt = picParams->decoded_curr_pic.pic_order_cnt; |
| 676 |
|
| 677 |
/* picParams->coding_type; App is always setting this to 0 */ |
| 678 |
hevcPicParams->CodingType = picParams->pic_fields.bits.coding_type; |
| 679 |
|
| 680 |
/* CodingType may be updated in slice processing. Do below to save pyramid level info */ |
| 681 |
hevcPicParams->ppsCodingType = picParams->pic_fields.bits.coding_type; |
| 682 |
|
| 683 |
hevcPicParams->HierarchLevelPlus1 = picParams->hierarchical_level_plus1; |
| 684 |
|
| 685 |
/* Reset it to zero now */ |
| 686 |
hevcPicParams->NumSlices = 0; |
| 687 |
|
| 688 |
hevcPicParams->sign_data_hiding_flag = picParams->pic_fields.bits.sign_data_hiding_enabled_flag; |
| 689 |
hevcPicParams->constrained_intra_pred_flag = picParams->pic_fields.bits.constrained_intra_pred_flag; |
| 690 |
hevcPicParams->transform_skip_enabled_flag = picParams->pic_fields.bits.transform_skip_enabled_flag; |
| 691 |
hevcPicParams->transquant_bypass_enabled_flag = picParams->pic_fields.bits.transquant_bypass_enabled_flag; |
| 692 |
hevcPicParams->tiles_enabled_flag = picParams->pic_fields.bits.tiles_enabled_flag; |
| 693 |
hevcPicParams->cu_qp_delta_enabled_flag = picParams->pic_fields.bits.cu_qp_delta_enabled_flag; |
| 694 |
hevcPicParams->weighted_pred_flag = picParams->pic_fields.bits.weighted_pred_flag; |
| 695 |
hevcPicParams->weighted_bipred_flag = picParams->pic_fields.bits.weighted_bipred_flag; |
| 696 |
hevcPicParams->loop_filter_across_slices_flag = picParams->pic_fields.bits.pps_loop_filter_across_slices_enabled_flag; |
| 697 |
hevcPicParams->loop_filter_across_tiles_flag = picParams->pic_fields.bits.loop_filter_across_tiles_enabled_flag; |
| 698 |
hevcPicParams->scaling_list_data_present_flag = picParams->pic_fields.bits.scaling_list_data_present_flag; |
| 699 |
hevcPicParams->bLastPicInSeq = (picParams->last_picture & HEVC_LAST_PICTURE_EOSEQ) ? 1 : 0; |
| 700 |
hevcPicParams->bLastPicInStream = (picParams->last_picture & HEVC_LAST_PICTURE_EOSTREAM) ? 1 : 0; |
| 701 |
hevcPicParams->bUseRawPicForRef = false; |
| 702 |
hevcPicParams->bScreenContent = picParams->pic_fields.bits.screen_content_flag; |
| 703 |
hevcPicParams->bEmulationByteInsertion = true; |
| 704 |
|
| 705 |
hevcPicParams->QpY = picParams->pic_init_qp; |
| 706 |
hevcPicParams->diff_cu_qp_delta_depth = picParams->diff_cu_qp_delta_depth; |
| 707 |
hevcPicParams->pps_cb_qp_offset = picParams->pps_cb_qp_offset; |
| 708 |
hevcPicParams->pps_cr_qp_offset = picParams->pps_cr_qp_offset; |
| 709 |
hevcPicParams->num_tile_columns_minus1 = picParams->num_tile_columns_minus1; |
| 710 |
hevcPicParams->num_tile_rows_minus1 = picParams->num_tile_rows_minus1; |
| 711 |
hevcPicParams->log2_parallel_merge_level_minus2 = picParams->log2_parallel_merge_level_minus2; |
| 712 |
hevcPicParams->LcuMaxBitsizeAllowed = picParams->ctu_max_bitsize_allowed; |
| 713 |
hevcPicParams->bUsedAsRef = picParams->pic_fields.bits.reference_pic_flag; |
| 714 |
hevcPicParams->slice_pic_parameter_set_id = picParams->slice_pic_parameter_set_id; |
| 715 |
hevcPicParams->nal_unit_type = picParams->nal_unit_type; |
| 716 |
hevcPicParams->no_output_of_prior_pics_flag = picParams->pic_fields.bits.no_output_of_prior_pics_flag; |
| 717 |
#if VA_CHECK_VERSION(1, 10, 0) |
| 718 |
hevcPicParams->bEnableGPUWeightedPrediction = picParams->pic_fields.bits.enable_gpu_weighted_prediction; |
| 719 |
#endif |
| 720 |
hevcPicParams->bDisplayFormatSwizzle = NeedDisapayFormatSwizzle(rtTbl->pCurrentRT, rtTbl->pCurrentReconTarget); |
| 721 |
|
| 722 |
// Correct Input color space of Sequence parameter here |
| 723 |
hevcSeqParams->InputColorSpace = hevcPicParams->bDisplayFormatSwizzle ? ECOLORSPACE_P601 : ECOLORSPACE_P709; |
| 724 |
|
| 725 |
if (hevcPicParams->tiles_enabled_flag) |
| 726 |
{ |
| 727 |
uint16_t shift = hevcSeqParams->log2_max_coding_block_size_minus3 - |
| 728 |
hevcSeqParams->log2_min_coding_block_size_minus3; |
| 729 |
uint16_t frameWidthAligedInLCU = MOS_ROUNDUP_SHIFT((hevcSeqParams->wFrameWidthInMinCbMinus1 + 1), shift); |
| 730 |
uint16_t frameHeightAligedInLCU = MOS_ROUNDUP_SHIFT((hevcSeqParams->wFrameHeightInMinCbMinus1 + 1), shift); |
| 731 |
|
| 732 |
if(hevcPicParams->num_tile_columns_minus1 > CODECHAL_GET_ARRAY_LENGTH(picParams->column_width_minus1) || |
| 733 |
hevcPicParams->num_tile_rows_minus1 > CODECHAL_GET_ARRAY_LENGTH(picParams->row_height_minus1)) |
| 734 |
{ |
| 735 |
// app passed wrong parameters |
| 736 |
DDI_ASSERTMESSAGE("invalid tile parameters!"); |
| 737 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 738 |
} |
| 739 |
|
| 740 |
for(uint32_t i = 0; i < (uint32_t)(hevcPicParams->num_tile_columns_minus1); i++) |
| 741 |
{ |
| 742 |
hevcPicParams->tile_column_width[i] = picParams->column_width_minus1[i] + 1; |
| 743 |
frameWidthAligedInLCU -= hevcPicParams->tile_column_width[i]; |
| 744 |
} |
| 745 |
|
| 746 |
if(frameWidthAligedInLCU > 0) |
| 747 |
{ |
| 748 |
hevcPicParams->tile_column_width[hevcPicParams->num_tile_columns_minus1] = frameWidthAligedInLCU; |
| 749 |
} |
| 750 |
else |
| 751 |
{ |
| 752 |
// app passed wrong parameters |
| 753 |
DDI_ASSERTMESSAGE("invalid tile parameters!"); |
| 754 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 755 |
} |
| 756 |
|
| 757 |
for(uint32_t i = 0; i < (uint32_t)(hevcPicParams->num_tile_rows_minus1); i++) |
| 758 |
{ |
| 759 |
hevcPicParams->tile_row_height[i] = picParams->row_height_minus1[i] + 1; |
| 760 |
frameHeightAligedInLCU -= hevcPicParams->tile_row_height[i]; |
| 761 |
} |
| 762 |
|
| 763 |
if(frameHeightAligedInLCU > 0) |
| 764 |
{ |
| 765 |
hevcPicParams->tile_row_height[hevcPicParams->num_tile_rows_minus1] = frameHeightAligedInLCU; |
| 766 |
} |
| 767 |
else |
| 768 |
{ |
| 769 |
// app passed wrong parameters |
| 770 |
DDI_ASSERTMESSAGE("invalid tile parameters!"); |
| 771 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
if (m_codechalSettings->isSCCEnabled) |
| 776 |
{ |
| 777 |
// SCC |
| 778 |
hevcPicParams->pps_curr_pic_ref_enabled_flag = picParams->scc_fields.bits.pps_curr_pic_ref_enabled_flag; |
| 779 |
hevcPicParams->residual_adaptive_colour_transform_enabled_flag = 0; |
| 780 |
hevcPicParams->pps_slice_act_qp_offsets_present_flag = 0; |
| 781 |
// GEN12 HW only can support the initial palette size 0 |
| 782 |
hevcPicParams->PredictorPaletteSize = 0; |
| 783 |
} |
| 784 |
|
| 785 |
DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, picParams->coded_buf); |
| 786 |
if (nullptr == buf) |
| 787 |
{ |
| 788 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 789 |
} |
| 790 |
|
| 791 |
//Application may re-use the buffer so need to remove before adding to status report again |
| 792 |
RemoveFromStatusReportQueue(buf); |
| 793 |
DdiMedia_MediaBufferToMosResource(buf, &(m_encodeCtx->resBitstreamBuffer)); |
| 794 |
|
| 795 |
CodecDefEncodeHevcTrace::TraceDDI(*hevcPicParams); |
| 796 |
|
| 797 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 798 |
return VA_STATUS_SUCCESS; |
| 799 |
} |
| 800 |
|
| 801 |
VAStatus DdiEncodeHevc::ParseSlcParams( |
| 802 |
DDI_MEDIA_CONTEXT *mediaCtx, |
| 803 |
void *ptr, |
| 804 |
uint32_t numSlices) |
| 805 |
{ |
| 806 |
API_STICKER_TRACE_ENTER(); |
| 807 |
DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 808 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 809 |
DDI_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 810 |
|
| 811 |
VAEncSliceParameterBufferHEVC *vaEncSlcParamsHEVC = (VAEncSliceParameterBufferHEVC *)ptr; |
| 812 |
|
| 813 |
PCODEC_HEVC_ENCODE_SLICE_PARAMS hevcSlcParams = (PCODEC_HEVC_ENCODE_SLICE_PARAMS)m_encodeCtx->pSliceParams; |
| 814 |
DDI_CHK_NULL(hevcSlcParams, "nullptr hevcSlcParams", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 815 |
|
| 816 |
PCODEC_HEVC_ENCODE_PICTURE_PARAMS hevcPicParams = (PCODEC_HEVC_ENCODE_PICTURE_PARAMS)(m_encodeCtx->pPicParams); |
| 817 |
DDI_CHK_NULL(hevcPicParams, "nullptr hevcPicParams", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 818 |
|
| 819 |
hevcPicParams->CodingType = CodechalPicTypeFromVaSlcType(vaEncSlcParamsHEVC->slice_type); |
| 820 |
|
| 821 |
if (vaEncSlcParamsHEVC->slice_segment_address == 0) |
| 822 |
{ |
| 823 |
hevcPicParams->NumSlices = 0; |
| 824 |
} |
| 825 |
else |
| 826 |
{ |
| 827 |
hevcSlcParams += hevcPicParams->NumSlices; |
| 828 |
} |
| 829 |
|
| 830 |
MOS_ZeroMemory( |
| 831 |
hevcSlcParams, |
| 832 |
numSlices * sizeof(CODEC_HEVC_ENCODE_SLICE_PARAMS)); |
| 833 |
|
| 834 |
for (uint32_t slcCount = 0; slcCount < numSlices; slcCount++, hevcSlcParams++) |
| 835 |
{ |
| 836 |
hevcSlcParams->slice_segment_address = vaEncSlcParamsHEVC->slice_segment_address; |
| 837 |
hevcSlcParams->NumLCUsInSlice = vaEncSlcParamsHEVC->num_ctu_in_slice; |
| 838 |
|
| 839 |
hevcSlcParams->num_ref_idx_l0_active_minus1 = vaEncSlcParamsHEVC->num_ref_idx_l0_active_minus1; |
| 840 |
hevcSlcParams->num_ref_idx_l1_active_minus1 = vaEncSlcParamsHEVC->num_ref_idx_l1_active_minus1; |
| 841 |
hevcSlcParams->bLastSliceOfPic = vaEncSlcParamsHEVC->slice_fields.bits.last_slice_of_pic_flag; |
| 842 |
hevcSlcParams->dependent_slice_segment_flag = vaEncSlcParamsHEVC->slice_fields.bits.dependent_slice_segment_flag; |
| 843 |
hevcSlcParams->slice_temporal_mvp_enable_flag = vaEncSlcParamsHEVC->slice_fields.bits.slice_temporal_mvp_enabled_flag; |
| 844 |
hevcSlcParams->slice_type = vaEncSlcParamsHEVC->slice_type; |
| 845 |
hevcSlcParams->slice_sao_luma_flag = vaEncSlcParamsHEVC->slice_fields.bits.slice_sao_luma_flag; |
| 846 |
hevcSlcParams->slice_sao_chroma_flag = vaEncSlcParamsHEVC->slice_fields.bits.slice_sao_chroma_flag; |
| 847 |
hevcSlcParams->mvd_l1_zero_flag = vaEncSlcParamsHEVC->slice_fields.bits.mvd_l1_zero_flag; |
| 848 |
hevcSlcParams->cabac_init_flag = vaEncSlcParamsHEVC->slice_fields.bits.cabac_init_flag; |
| 849 |
hevcSlcParams->slice_deblocking_filter_disable_flag = vaEncSlcParamsHEVC->slice_fields.bits.slice_deblocking_filter_disabled_flag; |
| 850 |
hevcSlcParams->collocated_from_l0_flag = vaEncSlcParamsHEVC->slice_fields.bits.collocated_from_l0_flag; |
| 851 |
|
| 852 |
hevcSlcParams->slice_qp_delta = vaEncSlcParamsHEVC->slice_qp_delta; |
| 853 |
hevcSlcParams->slice_cb_qp_offset = vaEncSlcParamsHEVC->slice_cb_qp_offset; |
| 854 |
hevcSlcParams->slice_cr_qp_offset = vaEncSlcParamsHEVC->slice_cr_qp_offset; |
| 855 |
hevcSlcParams->beta_offset_div2 = vaEncSlcParamsHEVC->slice_beta_offset_div2; |
| 856 |
hevcSlcParams->tc_offset_div2 = vaEncSlcParamsHEVC->slice_tc_offset_div2; |
| 857 |
hevcSlcParams->MaxNumMergeCand = vaEncSlcParamsHEVC->max_num_merge_cand; |
| 858 |
hevcSlcParams->luma_log2_weight_denom = vaEncSlcParamsHEVC->luma_log2_weight_denom; |
| 859 |
hevcSlcParams->delta_chroma_log2_weight_denom = vaEncSlcParamsHEVC->delta_chroma_log2_weight_denom; |
| 860 |
#if VA_CHECK_VERSION(1, 10, 0) |
| 861 |
hevcSlcParams->PredWeightTableBitOffset = vaEncSlcParamsHEVC->pred_weight_table_bit_offset; |
| 862 |
hevcSlcParams->PredWeightTableBitLength = vaEncSlcParamsHEVC->pred_weight_table_bit_length; |
| 863 |
#endif |
| 864 |
hevcSlcParams->slice_id = hevcPicParams->NumSlices + slcCount; |
| 865 |
hevcSlcParams->BitLengthSliceHeaderStartingPortion = 40; |
| 866 |
|
| 867 |
hevcSlcParams->bLastSliceOfPic = (slcCount == numSlices - 1) ? 1 : 0; |
| 868 |
// If either stored slice count or current count in this buffer are non-zero, then verify |
| 869 |
// that previous slice is not marked as last slice (it will be marked as last if it was last |
| 870 |
// slice in previous buffer, which is not necessarily last slice in picture). |
| 871 |
if (slcCount || hevcPicParams->NumSlices) |
| 872 |
{ |
| 873 |
hevcSlcParams--; |
| 874 |
hevcSlcParams->bLastSliceOfPic = false; |
| 875 |
hevcSlcParams++; |
| 876 |
} |
| 877 |
|
| 878 |
for (uint32_t i = 0; i < numMaxRefFrame; i++) |
| 879 |
{ |
| 880 |
// list 0 |
| 881 |
hevcSlcParams->luma_offset[0][i] = vaEncSlcParamsHEVC->luma_offset_l0[i]; |
| 882 |
hevcSlcParams->delta_luma_weight[0][i] = vaEncSlcParamsHEVC->delta_luma_weight_l0[i]; |
| 883 |
|
| 884 |
hevcSlcParams->chroma_offset[0][i][0] = MOS_CLAMP_MIN_MAX(vaEncSlcParamsHEVC->chroma_offset_l0[i][0], minChromaOffset, maxChromaOffset); |
| 885 |
hevcSlcParams->delta_chroma_weight[0][i][0] = vaEncSlcParamsHEVC->delta_chroma_weight_l0[i][0]; |
| 886 |
|
| 887 |
hevcSlcParams->chroma_offset[0][i][1] = MOS_CLAMP_MIN_MAX(vaEncSlcParamsHEVC->chroma_offset_l0[i][1], minChromaOffset, maxChromaOffset); |
| 888 |
hevcSlcParams->delta_chroma_weight[0][i][1] = vaEncSlcParamsHEVC->delta_chroma_weight_l0[i][1]; |
| 889 |
|
| 890 |
// list 1 |
| 891 |
hevcSlcParams->luma_offset[1][i] = vaEncSlcParamsHEVC->luma_offset_l1[i]; |
| 892 |
hevcSlcParams->delta_luma_weight[1][i] = vaEncSlcParamsHEVC->delta_luma_weight_l1[i]; |
| 893 |
|
| 894 |
hevcSlcParams->chroma_offset[1][i][0] = MOS_CLAMP_MIN_MAX(vaEncSlcParamsHEVC->chroma_offset_l1[i][0], minChromaOffset, maxChromaOffset); |
| 895 |
hevcSlcParams->delta_chroma_weight[1][i][0] = vaEncSlcParamsHEVC->delta_chroma_weight_l1[i][0]; |
| 896 |
|
| 897 |
hevcSlcParams->chroma_offset[1][i][1] = MOS_CLAMP_MIN_MAX(vaEncSlcParamsHEVC->chroma_offset_l1[i][1], minChromaOffset, maxChromaOffset); |
| 898 |
hevcSlcParams->delta_chroma_weight[1][i][1] = vaEncSlcParamsHEVC->delta_chroma_weight_l1[i][1]; |
| 899 |
} |
| 900 |
for (uint32_t i = 0; i < numMaxRefFrame; i++) |
| 901 |
{ |
| 902 |
if(i > hevcSlcParams->num_ref_idx_l0_active_minus1) |
| 903 |
{ |
| 904 |
hevcSlcParams->RefPicList[0][i].FrameIdx = CODECHAL_NUM_UNCOMPRESSED_SURFACE_HEVC; |
| 905 |
hevcSlcParams->RefPicList[0][i].PicFlags = PICTURE_INVALID; |
| 906 |
hevcSlcParams->RefPicList[0][i].PicEntry = 0xFF; |
| 907 |
} |
| 908 |
else |
| 909 |
{ |
| 910 |
SetupCodecPicture( |
| 911 |
mediaCtx, |
| 912 |
&(m_encodeCtx->RTtbl), |
| 913 |
&(hevcSlcParams->RefPicList[0][i]), |
| 914 |
vaEncSlcParamsHEVC->ref_pic_list0[i], |
| 915 |
false, |
| 916 |
true); |
| 917 |
GetSlcRefIdx(&(hevcPicParams->RefFrameList[0]), &(hevcSlcParams->RefPicList[0][i])); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
for (uint32_t i = 0; i < numMaxRefFrame; i++) |
| 922 |
{ |
| 923 |
if(i > hevcSlcParams->num_ref_idx_l1_active_minus1) |
| 924 |
{ |
| 925 |
hevcSlcParams->RefPicList[1][i].FrameIdx = CODECHAL_NUM_UNCOMPRESSED_SURFACE_HEVC; |
| 926 |
hevcSlcParams->RefPicList[1][i].PicFlags = PICTURE_INVALID; |
| 927 |
hevcSlcParams->RefPicList[1][i].PicEntry = 0xFF; |
| 928 |
} |
| 929 |
else |
| 930 |
{ |
| 931 |
SetupCodecPicture( |
| 932 |
mediaCtx, |
| 933 |
&(m_encodeCtx->RTtbl), |
| 934 |
&(hevcSlcParams->RefPicList[1][i]), |
| 935 |
vaEncSlcParamsHEVC->ref_pic_list1[i], |
| 936 |
false, |
| 937 |
true); |
| 938 |
GetSlcRefIdx(&(hevcPicParams->RefFrameList[0]), &(hevcSlcParams->RefPicList[1][i])); |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
CodecDefEncodeHevcTrace::TraceDDI(*hevcSlcParams); |
| 943 |
|
| 944 |
vaEncSlcParamsHEVC++; |
| 945 |
} |
| 946 |
|
| 947 |
hevcPicParams->NumSlices += numSlices; |
| 948 |
m_encodeCtx->dwNumSlices = hevcPicParams->NumSlices; |
| 949 |
|
| 950 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 951 |
return VA_STATUS_SUCCESS; |
| 952 |
} |
| 953 |
|
| 954 |
VAStatus DdiEncodeHevc::Qmatrix(void *ptr) |
| 955 |
{ |
| 956 |
API_STICKER_TRACE_ENTER(); |
| 957 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 958 |
DDI_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 959 |
|
| 960 |
PCODECHAL_HEVC_IQ_MATRIX_PARAMS QmatrixParams = (PCODECHAL_HEVC_IQ_MATRIX_PARAMS)(m_encodeCtx->pQmatrixParams); |
| 961 |
DDI_CHK_NULL(QmatrixParams, "nullptr hevcPicParams", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 962 |
|
| 963 |
VAQMatrixBufferHEVC *vaQMatrixHEVC = (VAQMatrixBufferHEVC *)ptr; |
| 964 |
|
| 965 |
for (uint8_t j = 0; j < 2; j++) |
| 966 |
{ |
| 967 |
for (uint8_t i = 0; i < 3; i++) |
| 968 |
{ |
| 969 |
MOS_SecureMemcpy((void *)&QmatrixParams->ucScalingLists0[3 * j + i][0], sizeof(QmatrixParams->ucScalingLists0[3 * j + i]), |
| 970 |
(void *)&vaQMatrixHEVC->scaling_lists_4x4[i][j][0], sizeof(vaQMatrixHEVC->scaling_lists_4x4[i][j])); |
| 971 |
MOS_SecureMemcpy((void *)&QmatrixParams->ucScalingLists1[3 * j + i][0], sizeof(QmatrixParams->ucScalingLists1[3 * j + i]), |
| 972 |
(void *)&vaQMatrixHEVC->scaling_lists_8x8[i][j][0], sizeof(vaQMatrixHEVC->scaling_lists_8x8[i][j])); |
| 973 |
MOS_SecureMemcpy((void *)&QmatrixParams->ucScalingLists2[3 * j + i][0], sizeof(QmatrixParams->ucScalingLists2[3 * j + i]), |
| 974 |
(void *)&vaQMatrixHEVC->scaling_lists_16x16[i][j][0], sizeof(vaQMatrixHEVC->scaling_lists_16x16[i][j])); |
| 975 |
QmatrixParams->ucScalingListDCCoefSizeID2[3 * j + i] = vaQMatrixHEVC->scaling_list_dc_16x16[i][j]; |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
MOS_SecureMemcpy((void *)&QmatrixParams->ucScalingLists3[0][0], sizeof(QmatrixParams->ucScalingLists3), |
| 980 |
(void *)&vaQMatrixHEVC->scaling_lists_32x32[0][0], sizeof(vaQMatrixHEVC->scaling_lists_32x32)); |
| 981 |
MOS_SecureMemcpy((void *)&QmatrixParams->ucScalingListDCCoefSizeID3[0], sizeof(QmatrixParams->ucScalingListDCCoefSizeID3), |
| 982 |
(void *)&vaQMatrixHEVC->scaling_list_dc_32x32[0], sizeof(QmatrixParams->ucScalingListDCCoefSizeID3)); |
| 983 |
|
| 984 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 985 |
return VA_STATUS_SUCCESS; |
| 986 |
} |
| 987 |
|
| 988 |
VAStatus DdiEncodeHevc::FindNalUnitStartCodes( |
| 989 |
uint8_t * buf, |
| 990 |
uint32_t size, |
| 991 |
uint32_t * startCodesOffset, |
| 992 |
uint32_t * startCodesLength) |
| 993 |
{ |
| 994 |
API_STICKER_TRACE_ENTER(); |
| 995 |
uint8_t i = 0; |
| 996 |
|
| 997 |
while (((i + 3) < size) && |
| 998 |
(buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) && |
| 999 |
(buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0 || buf[i+3] != 0x01)) |
| 1000 |
{ |
| 1001 |
i++; |
| 1002 |
} |
| 1003 |
|
| 1004 |
if ((i + 3) == size) |
| 1005 |
{ |
| 1006 |
if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) |
| 1007 |
{ |
| 1008 |
return VA_STATUS_ERROR_INVALID_BUFFER; //NALU start codes doesn't exit |
| 1009 |
} |
| 1010 |
else |
| 1011 |
{ |
| 1012 |
*startCodesOffset = size - 3; |
| 1013 |
*startCodesLength = 3; |
| 1014 |
return VA_STATUS_SUCCESS; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) |
| 1019 |
{ |
| 1020 |
*startCodesOffset = i; |
| 1021 |
*startCodesLength = 4; |
| 1022 |
} |
| 1023 |
else |
| 1024 |
{ |
| 1025 |
*startCodesOffset = i; |
| 1026 |
*startCodesLength = 3; |
| 1027 |
} |
| 1028 |
|
| 1029 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 1030 |
return VA_STATUS_SUCCESS; |
| 1031 |
} |
| 1032 |
|
| 1033 |
VAStatus DdiEncodeHevc::ParsePackedHeaderParams(void *ptr) |
| 1034 |
{ |
| 1035 |
API_STICKER_TRACE_ENTER(); |
| 1036 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx.", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1037 |
DDI_CHK_NULL(ptr, "nullptr ptr.", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1038 |
|
| 1039 |
VAEncPackedHeaderParameterBuffer *encPackedHeaderParamBuf = (VAEncPackedHeaderParameterBuffer *)ptr; |
| 1040 |
m_encodeCtx->bLastPackedHdrIsSlice = false; |
| 1041 |
if (encPackedHeaderParamBuf->type == VAEncPackedHeaderHEVC_Slice) |
| 1042 |
{ |
| 1043 |
m_encodeCtx->bLastPackedHdrIsSlice = true; |
| 1044 |
m_encodeCtx->bHavePackedSliceHdr = true; |
| 1045 |
|
| 1046 |
// Current minimum LCU size for HEVC is 16x16. Minimum 1 LCU per slice. |
| 1047 |
if (m_encodeCtx->uiSliceHeaderCnt >= m_encodeCtx->wPicHeightInMB * m_encodeCtx->wPicWidthInMB) |
| 1048 |
{ |
| 1049 |
return VA_STATUS_ERROR_MAX_NUM_EXCEEDED; |
| 1050 |
} |
| 1051 |
|
| 1052 |
// get the packed header size |
| 1053 |
m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].BitSize = encPackedHeaderParamBuf->bit_length; |
| 1054 |
// don't know NALU start codes now, assign to 4 here when has_emulation_bytes is 0 and later will correct it |
| 1055 |
m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].SkipEmulationByteCount = (encPackedHeaderParamBuf->has_emulation_bytes) ? (encPackedHeaderParamBuf->bit_length + 7) / 8 : 4; |
| 1056 |
} |
| 1057 |
else |
| 1058 |
{ |
| 1059 |
/* App does not indicate the type, using PPS in general for headers */ |
| 1060 |
m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiNalUnitType = HEVC_NAL_UT_PPS; |
| 1061 |
m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->bInsertEmulationBytes = (encPackedHeaderParamBuf->has_emulation_bytes) ? false : true; |
| 1062 |
// don't know NALU start codes now, assign to 4 here when has_emulation_bytes is 0 and later will correct it |
| 1063 |
m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSkipEmulationCheckCount = (encPackedHeaderParamBuf->has_emulation_bytes) ? (encPackedHeaderParamBuf->bit_length + 7) / 8 : 4; |
| 1064 |
m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSize = (encPackedHeaderParamBuf->bit_length + 7) / 8; |
| 1065 |
m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiOffset = 0; |
| 1066 |
} |
| 1067 |
|
| 1068 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 1069 |
return VA_STATUS_SUCCESS; |
| 1070 |
} |
| 1071 |
|
| 1072 |
VAStatus DdiEncodeHevc::ParsePackedHeaderData(void *ptr) |
| 1073 |
{ |
| 1074 |
API_STICKER_TRACE_ENTER(); |
| 1075 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx.", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1076 |
DDI_CHK_NULL(ptr, "nullptr ptr.", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1077 |
|
| 1078 |
BSBuffer *bsBuffer = m_encodeCtx->pbsBuffer; |
| 1079 |
DDI_CHK_NULL(bsBuffer, "nullptr bsBuffer.", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1080 |
|
| 1081 |
if (m_encodeCtx->indexNALUnit == 0 && m_encodeCtx->uiSliceHeaderCnt == 0) |
| 1082 |
{ |
| 1083 |
bsBuffer->pCurrent = bsBuffer->pBase; |
| 1084 |
bsBuffer->SliceOffset = 0; |
| 1085 |
bsBuffer->BitOffset = 0; |
| 1086 |
bsBuffer->BitSize = 0; |
| 1087 |
} |
| 1088 |
|
| 1089 |
uint32_t hdrDataSize = 0; |
| 1090 |
MOS_STATUS eStatus = MOS_STATUS_SUCCESS; |
| 1091 |
if (true == m_encodeCtx->bLastPackedHdrIsSlice) |
| 1092 |
{ |
| 1093 |
hdrDataSize = (m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].BitSize + 7) / 8; |
| 1094 |
|
| 1095 |
eStatus = MOS_SecureMemcpy(bsBuffer->pCurrent, bsBuffer->BufferSize - bsBuffer->SliceOffset, (uint8_t *)ptr, hdrDataSize); |
| 1096 |
if (MOS_STATUS_SUCCESS != eStatus) |
| 1097 |
{ |
| 1098 |
DDI_ASSERTMESSAGE("DDI:packed slice header size is too large to be supported!"); |
| 1099 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1100 |
} |
| 1101 |
|
| 1102 |
m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].SliceOffset = bsBuffer->pCurrent - bsBuffer->pBase; |
| 1103 |
|
| 1104 |
// correct SkipEmulationByteCount |
| 1105 |
// according to LibVA principle, one packed header buffer should only contain one NALU, |
| 1106 |
// so when has_emulation_bytes is 0, SkipEmulationByteCount only needs to skip the NALU start codes |
| 1107 |
if (m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].SkipEmulationByteCount != hdrDataSize) |
| 1108 |
{ |
| 1109 |
uint32_t startCodesOffset = 0; |
| 1110 |
uint32_t startCodesLength = 0; |
| 1111 |
VAStatus vaSts = VA_STATUS_SUCCESS; |
| 1112 |
vaSts = FindNalUnitStartCodes((uint8_t *)ptr, hdrDataSize, &startCodesOffset, &startCodesLength); |
| 1113 |
if (VA_STATUS_SUCCESS != vaSts) |
| 1114 |
{ |
| 1115 |
DDI_ASSERTMESSAGE("DDI: packed slice header doesn't include NAL unit start codes!"); |
| 1116 |
return vaSts; |
| 1117 |
} |
| 1118 |
m_encodeCtx->pSliceHeaderData[m_encodeCtx->uiSliceHeaderCnt].SkipEmulationByteCount = MOS_MIN(15, (startCodesOffset + startCodesLength)); |
| 1119 |
} |
| 1120 |
|
| 1121 |
m_encodeCtx->uiSliceHeaderCnt++; |
| 1122 |
m_encodeCtx->bLastPackedHdrIsSlice = false; |
| 1123 |
} |
| 1124 |
else |
| 1125 |
{ |
| 1126 |
// copy sps and pps header data |
| 1127 |
hdrDataSize = m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSize; |
| 1128 |
|
| 1129 |
eStatus = MOS_SecureMemcpy(bsBuffer->pCurrent, bsBuffer->BufferSize - bsBuffer->SliceOffset, (uint8_t *)ptr, hdrDataSize); |
| 1130 |
if (MOS_STATUS_SUCCESS != eStatus) |
| 1131 |
{ |
| 1132 |
DDI_ASSERTMESSAGE("DDI:packed header size is too large to be supported!"); |
| 1133 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1134 |
} |
| 1135 |
|
| 1136 |
// correct uiSkipEmulationCheckCount |
| 1137 |
// according to LibVA principle, one packed header buffer should only contain one NALU, |
| 1138 |
// so when has_emulation_bytes is 0, uiSkipEmulationCheckCount only needs to skip the NALU start codes |
| 1139 |
if (m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSkipEmulationCheckCount != hdrDataSize) |
| 1140 |
{ |
| 1141 |
uint32_t startCodesOffset = 0; |
| 1142 |
uint32_t startCodesLength = 0; |
| 1143 |
VAStatus vaSts = VA_STATUS_SUCCESS; |
| 1144 |
vaSts = FindNalUnitStartCodes((uint8_t *)ptr, hdrDataSize, &startCodesOffset, &startCodesLength); |
| 1145 |
if (VA_STATUS_SUCCESS != vaSts) |
| 1146 |
{ |
| 1147 |
DDI_ASSERTMESSAGE("DDI: packed header doesn't include NAL unit start codes!"); |
| 1148 |
return vaSts; |
| 1149 |
} |
| 1150 |
m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiSkipEmulationCheckCount = MOS_MIN(15, (startCodesOffset + startCodesLength)); |
| 1151 |
} |
| 1152 |
|
| 1153 |
m_encodeCtx->ppNALUnitParams[m_encodeCtx->indexNALUnit]->uiOffset = bsBuffer->pCurrent - bsBuffer->pBase; |
| 1154 |
m_encodeCtx->indexNALUnit++; |
| 1155 |
} |
| 1156 |
|
| 1157 |
bsBuffer->pCurrent += hdrDataSize; |
| 1158 |
bsBuffer->SliceOffset += hdrDataSize; |
| 1159 |
bsBuffer->BitSize += hdrDataSize * 8; |
| 1160 |
|
| 1161 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 1162 |
return VA_STATUS_SUCCESS; |
| 1163 |
} |
| 1164 |
|
| 1165 |
VAStatus DdiEncodeHevc::ParseMiscParams(void *ptr) |
| 1166 |
{ |
| 1167 |
API_STICKER_TRACE_ENTER(); |
| 1168 |
DDI_CHK_NULL(m_encodeCtx, "nullptr m_encodeCtx", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1169 |
DDI_CHK_NULL(ptr, "nullptr ptr", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1170 |
DDI_CHK_NULL(m_encodeCtx->pSeqParams, "nullptr m_encodeCtx->pSeqParams", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1171 |
DDI_CHK_NULL(m_encodeCtx->pPicParams, "nullptr m_encodeCtx->pPicParams", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1172 |
|
| 1173 |
PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS seqParams = (PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS)(m_encodeCtx->pSeqParams); |
| 1174 |
PCODEC_HEVC_ENCODE_PICTURE_PARAMS picParams = (PCODEC_HEVC_ENCODE_PICTURE_PARAMS)(m_encodeCtx->pPicParams); |
| 1175 |
|
| 1176 |
VAEncMiscParameterBuffer *miscParamBuf = (VAEncMiscParameterBuffer *)ptr; |
| 1177 |
DDI_CHK_NULL(miscParamBuf->data, "nullptr miscParamBuf->data", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1178 |
|
| 1179 |
switch ((int32_t)(miscParamBuf->type)) |
| 1180 |
{ |
| 1181 |
case VAEncMiscParameterTypeQualityLevel: |
| 1182 |
{ |
| 1183 |
VAEncMiscParameterBufferQualityLevel *vaEncMiscParamQualityLevel = (VAEncMiscParameterBufferQualityLevel *)miscParamBuf->data; |
| 1184 |
m_encodeCtx->targetUsage = (uint8_t)vaEncMiscParamQualityLevel->quality_level; |
| 1185 |
|
| 1186 |
#ifdef _FULL_OPEN_SOURCE |
| 1187 |
if(seqParams->TargetUsage >= 1 && seqParams->TargetUsage <= 2) |
| 1188 |
{ |
| 1189 |
seqParams->TargetUsage = 4; |
| 1190 |
} |
| 1191 |
else if(seqParams->TargetUsage >= 3 && seqParams->TargetUsage <= 5) |
| 1192 |
{ |
| 1193 |
seqParams->TargetUsage = 7; |
| 1194 |
} |
| 1195 |
else |
| 1196 |
{ |
| 1197 |
DDI_ASSERTMESSAGE("unsupported target usage in HEVC encoder."); |
| 1198 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1199 |
} |
| 1200 |
#endif |
| 1201 |
|
| 1202 |
// HEVC only supports TU=1, 4, and 7 |
| 1203 |
if (1 != m_encodeCtx->targetUsage && 4 != m_encodeCtx->targetUsage && 7 != m_encodeCtx->targetUsage) |
| 1204 |
{ |
| 1205 |
DDI_ASSERTMESSAGE("unsupported target usage in HEVC encoder."); |
| 1206 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1207 |
} |
| 1208 |
break; |
| 1209 |
} |
| 1210 |
case VAEncMiscParameterTypeHRD: |
| 1211 |
{ |
| 1212 |
VAEncMiscParameterHRD *vaEncMiscParamHRD = (VAEncMiscParameterHRD *)miscParamBuf->data; |
| 1213 |
seqParams->VBVBufferSizeInBit = vaEncMiscParamHRD->buffer_size; |
| 1214 |
seqParams->InitVBVBufferFullnessInBit = vaEncMiscParamHRD->initial_buffer_fullness; |
| 1215 |
break; |
| 1216 |
} |
| 1217 |
case VAEncMiscParameterTypeFrameRate: |
| 1218 |
{ |
| 1219 |
VAEncMiscParameterFrameRate *vaEncMiscParamFR = (VAEncMiscParameterFrameRate *)miscParamBuf->data; |
| 1220 |
uint32_t vaFRnumerator = vaEncMiscParamFR->framerate & 0xffff; |
| 1221 |
|
| 1222 |
#if (VA_MAJOR_VERSION < 1) |
| 1223 |
uint32_t vaFRDenominator = 100; |
| 1224 |
#else |
| 1225 |
uint32_t vaFRDenominator = (vaEncMiscParamFR->framerate >> 16) & 0xffff; |
| 1226 |
#endif |
| 1227 |
|
| 1228 |
if(vaFRDenominator == 0) |
| 1229 |
{ |
| 1230 |
vaFRDenominator = 1; |
| 1231 |
} |
| 1232 |
// Pass frame rate value to seqParams and set BRC reset flag and bNewSeq to true while dynamic BRC occures |
| 1233 |
seqParams->FrameRate.Numerator = vaFRnumerator; |
| 1234 |
seqParams->FrameRate.Denominator = vaFRDenominator; |
| 1235 |
if(m_previousFRvalue != 0) |
| 1236 |
{ |
| 1237 |
uint16_t seqFRvalue = (uint16_t)(seqParams->FrameRate.Numerator / seqParams->FrameRate.Denominator); |
| 1238 |
if(m_previousFRvalue != seqFRvalue) |
| 1239 |
{ |
| 1240 |
seqParams->bResetBRC = 0x1; |
| 1241 |
m_encodeCtx->bNewSeq = true; |
| 1242 |
} |
| 1243 |
} |
| 1244 |
m_previousFRvalue = (uint16_t)(vaFRnumerator / vaFRDenominator); |
| 1245 |
break; |
| 1246 |
} |
| 1247 |
case VAEncMiscParameterTypeRateControl: |
| 1248 |
{ |
| 1249 |
// Assume only one SPS here, modify when enable multiple SPS support |
| 1250 |
VAEncMiscParameterRateControl *vaEncMiscParamRC = (VAEncMiscParameterRateControl *)miscParamBuf->data; |
| 1251 |
|
| 1252 |
if(vaEncMiscParamRC->bits_per_second > 0) |
| 1253 |
{ |
| 1254 |
seqParams->TargetBitRate = MOS_ROUNDUP_DIVIDE(vaEncMiscParamRC->bits_per_second, CODECHAL_ENCODE_BRC_KBPS); |
| 1255 |
} |
| 1256 |
if (VA_RC_CQP != m_encodeCtx->uiRCMethod && (VA_RC_MB & m_encodeCtx->uiRCMethod) && vaEncMiscParamRC->rc_flags.bits.mb_rate_control <= mbBrcDisabled) |
| 1257 |
{ |
| 1258 |
seqParams->MBBRC = vaEncMiscParamRC->rc_flags.bits.mb_rate_control; |
| 1259 |
} |
| 1260 |
else |
| 1261 |
{ |
| 1262 |
seqParams->MBBRC = mbBrcDisabled; |
| 1263 |
} |
| 1264 |
//enable parallelBRC for Android and Linux |
| 1265 |
seqParams->ParallelBRC = vaEncMiscParamRC->rc_flags.bits.enable_parallel_brc; |
| 1266 |
|
| 1267 |
if (true == m_encodeCtx->bVdencActive) |
| 1268 |
{ |
| 1269 |
picParams->BRCMaxQp = (vaEncMiscParamRC->max_qp == 0) ? 51 : (uint8_t)CodecHal_Clip3(1, 51, (int8_t)vaEncMiscParamRC->max_qp); |
| 1270 |
picParams->BRCMinQp = (vaEncMiscParamRC->min_qp == 0) ? 1 : (uint8_t)CodecHal_Clip3(1, picParams->BRCMaxQp, (int8_t)vaEncMiscParamRC->min_qp); |
| 1271 |
#if VA_CHECK_VERSION(1, 10, 0) |
| 1272 |
picParams->TargetFrameSize = vaEncMiscParamRC->target_frame_size; |
| 1273 |
#endif |
| 1274 |
} |
| 1275 |
else |
| 1276 |
{ |
| 1277 |
picParams->BRCMaxQp = (uint8_t)CodecHal_Clip3(0, 51, (int8_t)vaEncMiscParamRC->max_qp); //use 0 to ignore |
| 1278 |
picParams->BRCMinQp = (uint8_t)CodecHal_Clip3(0, picParams->BRCMaxQp, (int8_t)vaEncMiscParamRC->min_qp); //use 0 to ignore |
| 1279 |
} |
| 1280 |
|
| 1281 |
if (VA_RC_NONE == m_encodeCtx->uiRCMethod || VA_RC_CQP == m_encodeCtx->uiRCMethod) |
| 1282 |
{ |
| 1283 |
seqParams->RateControlMethod = RATECONTROL_CQP; |
| 1284 |
seqParams->MBBRC = 0; |
| 1285 |
} |
| 1286 |
else if (VA_RC_CBR & m_encodeCtx->uiRCMethod ) |
| 1287 |
{ |
| 1288 |
seqParams->MaxBitRate = seqParams->TargetBitRate; |
| 1289 |
seqParams->MinBitRate = seqParams->TargetBitRate; |
| 1290 |
seqParams->RateControlMethod = RATECONTROL_CBR; |
| 1291 |
} |
| 1292 |
else if (VA_RC_ICQ & m_encodeCtx->uiRCMethod) |
| 1293 |
{ |
| 1294 |
seqParams->ICQQualityFactor = vaEncMiscParamRC->ICQ_quality_factor; |
| 1295 |
seqParams->RateControlMethod = RATECONTROL_ICQ; |
| 1296 |
seqParams->MBBRC = 1; |
| 1297 |
} |
| 1298 |
else if(VA_RC_VCM & m_encodeCtx->uiRCMethod) |
| 1299 |
{ |
| 1300 |
seqParams->RateControlMethod = RATECONTROL_VCM; |
| 1301 |
seqParams->MBBRC = 0; |
| 1302 |
} |
| 1303 |
else if(VA_RC_VBR & m_encodeCtx->uiRCMethod) |
| 1304 |
{ |
| 1305 |
seqParams->RateControlMethod = RATECONTROL_VBR; |
| 1306 |
} |
| 1307 |
else if (VA_RC_QVBR & m_encodeCtx->uiRCMethod) |
| 1308 |
{ |
| 1309 |
seqParams->RateControlMethod = RATECONTROL_QVBR; |
| 1310 |
seqParams->ICQQualityFactor = vaEncMiscParamRC->quality_factor; |
| 1311 |
} |
| 1312 |
else |
| 1313 |
{ |
| 1314 |
DDI_ASSERTMESSAGE("invalid RC method."); |
| 1315 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1316 |
} |
| 1317 |
if((RATECONTROL_VCM == seqParams->RateControlMethod) |
| 1318 |
|| (RATECONTROL_VBR == seqParams->RateControlMethod) |
| 1319 |
|| (RATECONTROL_CBR == seqParams->RateControlMethod) |
| 1320 |
|| (RATECONTROL_QVBR == seqParams->RateControlMethod)) |
| 1321 |
{ |
| 1322 |
seqParams->MaxBitRate = seqParams->TargetBitRate; |
| 1323 |
seqParams->MinBitRate = seqParams->TargetBitRate * (2 * vaEncMiscParamRC->target_percentage - 100) / 100; |
| 1324 |
seqParams->TargetBitRate = seqParams->TargetBitRate * vaEncMiscParamRC->target_percentage / 100; |
| 1325 |
|
| 1326 |
if ((m_encodeCtx->uiTargetBitRate != seqParams->TargetBitRate) || |
| 1327 |
(m_encodeCtx->uiMaxBitRate != seqParams->MaxBitRate)) |
| 1328 |
{ |
| 1329 |
if ((m_encodeCtx->uiTargetBitRate != 0) && (m_encodeCtx->uiMaxBitRate != 0)) |
| 1330 |
{ |
| 1331 |
seqParams->bResetBRC = 0x1; |
| 1332 |
m_encodeCtx->bNewSeq = true; |
| 1333 |
} |
| 1334 |
m_encodeCtx->uiTargetBitRate = seqParams->TargetBitRate; |
| 1335 |
m_encodeCtx->uiMaxBitRate = seqParams->MaxBitRate; |
| 1336 |
} |
| 1337 |
} |
| 1338 |
#ifndef ANDROID |
| 1339 |
seqParams->FrameSizeTolerance = static_cast<ENCODE_FRAMESIZE_TOLERANCE>(vaEncMiscParamRC->rc_flags.bits.frame_tolerance_mode); |
| 1340 |
#endif |
| 1341 |
break; |
| 1342 |
} |
| 1343 |
case VAEncMiscParameterTypeParallelBRC: |
| 1344 |
{ |
| 1345 |
VAEncMiscParameterParallelRateControl *vaEncMiscParameterParallel = (VAEncMiscParameterParallelRateControl *)miscParamBuf->data; |
| 1346 |
|
| 1347 |
seqParams->NumOfBInGop[0] = vaEncMiscParameterParallel->num_b_in_gop[0]; |
| 1348 |
seqParams->NumOfBInGop[1] = vaEncMiscParameterParallel->num_b_in_gop[1]; |
| 1349 |
seqParams->NumOfBInGop[2] = vaEncMiscParameterParallel->num_b_in_gop[2]; |
| 1350 |
|
| 1351 |
break; |
| 1352 |
} |
| 1353 |
case VAEncMiscParameterTypeRIR: |
| 1354 |
{ |
| 1355 |
VAEncMiscParameterRIR *vaEncMiscParamRIR = (VAEncMiscParameterRIR *)miscParamBuf->data; |
| 1356 |
picParams->bEnableRollingIntraRefresh = vaEncMiscParamRIR->rir_flags.value & 0x3; //only lower two bits are valid |
| 1357 |
|
| 1358 |
// Set for all frames since pic type is not known yet. Disable in slice params if its I or B type. |
| 1359 |
if ((ROLLING_I_COLUMN == picParams->bEnableRollingIntraRefresh) || (ROLLING_I_ROW == picParams->bEnableRollingIntraRefresh)) |
| 1360 |
{ |
| 1361 |
picParams->IntraInsertionLocation = (uint16_t)vaEncMiscParamRIR->intra_insertion_location; |
| 1362 |
picParams->IntraInsertionSize = (uint8_t)vaEncMiscParamRIR->intra_insert_size; |
| 1363 |
picParams->QpDeltaForInsertedIntra = vaEncMiscParamRIR->qp_delta_for_inserted_intra; |
| 1364 |
} |
| 1365 |
else if ((ROLLING_I_ROW | ROLLING_I_COLUMN) == picParams->bEnableRollingIntraRefresh) // cannot have row and column rolling |
| 1366 |
{ |
| 1367 |
picParams->bEnableRollingIntraRefresh = ROLLING_I_DISABLED; |
| 1368 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1369 |
} |
| 1370 |
|
| 1371 |
break; |
| 1372 |
} |
| 1373 |
case VAEncMiscParameterTypeROI: |
| 1374 |
{ |
| 1375 |
VAEncMiscParameterBufferROI *vaEncMiscParamROI = (VAEncMiscParameterBufferROI *)miscParamBuf->data; |
| 1376 |
uint8_t blockSize = (m_encodeCtx->bVdencActive) ? vdencRoiBlockSize : CODECHAL_MACROBLOCK_WIDTH; |
| 1377 |
|
| 1378 |
uint32_t frameWidth = (seqParams->wFrameWidthInMinCbMinus1 + 1) << (seqParams->log2_min_coding_block_size_minus3 + 3); |
| 1379 |
uint32_t frameHeight = (seqParams->wFrameHeightInMinCbMinus1 + 1) << (seqParams->log2_min_coding_block_size_minus3 + 3); |
| 1380 |
uint16_t rightBorder = (uint16_t)CODECHAL_GET_WIDTH_IN_BLOCKS(frameWidth, blockSize) - 1; |
| 1381 |
uint16_t bottomBorder = (uint16_t)CODECHAL_GET_HEIGHT_IN_BLOCKS(frameHeight, blockSize) - 1; |
| 1382 |
|
| 1383 |
if(vaEncMiscParamROI->num_roi > CODECHAL_ENCODE_HEVC_MAX_NUM_ROI) |
| 1384 |
{ |
| 1385 |
DDI_ASSERTMESSAGE("The number of ROI rect is greater than %d", CODECHAL_ENCODE_HEVC_MAX_NUM_ROI); |
| 1386 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1387 |
} |
| 1388 |
|
| 1389 |
if (vaEncMiscParamROI->num_roi) |
| 1390 |
{ |
| 1391 |
for (uint32_t i = 0; i < vaEncMiscParamROI->num_roi; ++i) |
| 1392 |
{ |
| 1393 |
auto &pic_param_roi = picParams->ROI[i]; |
| 1394 |
auto &va_roi = vaEncMiscParamROI->roi[i]; |
| 1395 |
|
| 1396 |
pic_param_roi.Top = va_roi.roi_rectangle.y; |
| 1397 |
pic_param_roi.Left = va_roi.roi_rectangle.x; |
| 1398 |
pic_param_roi.Right = va_roi.roi_rectangle.x + va_roi.roi_rectangle.width - 1; |
| 1399 |
pic_param_roi.Bottom = va_roi.roi_rectangle.y+va_roi.roi_rectangle.height - 1; |
| 1400 |
|
| 1401 |
pic_param_roi.PriorityLevelOrDQp = va_roi.roi_value; |
| 1402 |
|
| 1403 |
if( pic_param_roi.Left > pic_param_roi.Right){ |
| 1404 |
DDI_ASSERTMESSAGE("ROI left > ROI Right."); |
| 1405 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1406 |
} |
| 1407 |
|
| 1408 |
// check frame boundary |
| 1409 |
if( pic_param_roi.Top > pic_param_roi.Bottom){ |
| 1410 |
DDI_ASSERTMESSAGE("ROI Top > ROI Bottom."); |
| 1411 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1412 |
} |
| 1413 |
|
| 1414 |
// Convert from pixel units to block size units |
| 1415 |
pic_param_roi.Left /= blockSize; |
| 1416 |
pic_param_roi.Right /= blockSize; |
| 1417 |
pic_param_roi.Top /= blockSize; |
| 1418 |
pic_param_roi.Bottom /= blockSize; |
| 1419 |
|
| 1420 |
// check frame boundary |
| 1421 |
pic_param_roi.Left = pic_param_roi.Left > rightBorder ? rightBorder : pic_param_roi.Left; |
| 1422 |
pic_param_roi.Right = pic_param_roi.Right > rightBorder ? rightBorder : pic_param_roi.Right; |
| 1423 |
pic_param_roi.Top = pic_param_roi.Top > bottomBorder ? bottomBorder : pic_param_roi.Top; |
| 1424 |
pic_param_roi.Bottom = pic_param_roi.Bottom > bottomBorder ? bottomBorder : pic_param_roi.Bottom; |
| 1425 |
|
| 1426 |
// DDI defination for right and bottom parameters is inclusive whereas |
| 1427 |
// the defination in BRC kernel input is exclusive. So adding 1 to right & bottom. |
| 1428 |
pic_param_roi.Right += 1; |
| 1429 |
pic_param_roi.Bottom += 1; |
| 1430 |
|
| 1431 |
} |
| 1432 |
picParams->NumROI = vaEncMiscParamROI->num_roi; |
| 1433 |
} |
| 1434 |
#ifndef ANDROID |
| 1435 |
// support DeltaQP based ROI by default |
| 1436 |
seqParams->ROIValueInDeltaQP = vaEncMiscParamROI->roi_flags.bits.roi_value_is_qp_delta; |
| 1437 |
if(picParams->NumROI != 0 && seqParams->ROIValueInDeltaQP == 0) |
| 1438 |
{ |
| 1439 |
DDI_ASSERTMESSAGE("ROI does not support priority level now."); |
| 1440 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1441 |
} |
| 1442 |
#endif |
| 1443 |
break; |
| 1444 |
} |
| 1445 |
case VAEncMiscParameterTypeDirtyRect: |
| 1446 |
{ |
| 1447 |
VAEncMiscParameterBufferDirtyRect *vaEncMiscDirtyRect = (VAEncMiscParameterBufferDirtyRect *)miscParamBuf->data; |
| 1448 |
|
| 1449 |
uint8_t blockSize = (m_encodeCtx->bVdencActive) ? vdencRoiBlockSize : CODECHAL_MACROBLOCK_WIDTH; |
| 1450 |
uint32_t rightBorder = CODECHAL_GET_WIDTH_IN_BLOCKS(m_encodeCtx->dwFrameWidth, blockSize) - 1; |
| 1451 |
uint32_t bottomBorder = CODECHAL_GET_HEIGHT_IN_BLOCKS(m_encodeCtx->dwFrameHeight, blockSize) - 1; |
| 1452 |
|
| 1453 |
DDI_CHK_NULL(vaEncMiscDirtyRect->roi_rectangle, "nullptr dirty rect", VA_STATUS_ERROR_INVALID_PARAMETER); |
| 1454 |
if(vaEncMiscDirtyRect->num_roi_rectangle > ENCODE_VDENC_HEVC_MAX_DIRTYRECT_G10) |
| 1455 |
{ |
| 1456 |
DDI_ASSERTMESSAGE("The number of dirty rect is greater than %d", ENCODE_VDENC_HEVC_MAX_DIRTYRECT_G10); |
| 1457 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1458 |
} |
| 1459 |
picParams->pDirtyRect = m_pDirtyRect; |
| 1460 |
|
| 1461 |
if (vaEncMiscDirtyRect->num_roi_rectangle > m_numDirtyRects) |
| 1462 |
{ |
| 1463 |
picParams->pDirtyRect = (PCODEC_ROI)MOS_ReallocMemory(picParams->pDirtyRect, vaEncMiscDirtyRect->num_roi_rectangle * sizeof(CODEC_ROI)); |
| 1464 |
if (!picParams->pDirtyRect) |
| 1465 |
{ |
| 1466 |
picParams->NumDirtyRects = 0; |
| 1467 |
DDI_ASSERTMESSAGE("Allocation of pDirtyRect failed!"); |
| 1468 |
return VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 1469 |
} |
| 1470 |
m_pDirtyRect = picParams->pDirtyRect; |
| 1471 |
m_numDirtyRects = vaEncMiscDirtyRect->num_roi_rectangle; |
| 1472 |
} |
| 1473 |
|
| 1474 |
if(vaEncMiscDirtyRect->num_roi_rectangle > 0) |
| 1475 |
{ |
| 1476 |
MOS_ZeroMemory(picParams->pDirtyRect, vaEncMiscDirtyRect->num_roi_rectangle * sizeof(CODEC_ROI)); |
| 1477 |
picParams->NumDirtyRects = 0; |
| 1478 |
|
| 1479 |
for (int32_t i = 0; i < vaEncMiscDirtyRect->num_roi_rectangle; i++) |
| 1480 |
{ |
| 1481 |
auto &rect = picParams->pDirtyRect[i]; |
| 1482 |
auto &va_rect = vaEncMiscDirtyRect->roi_rectangle[i]; |
| 1483 |
rect.Left = va_rect.x; |
| 1484 |
rect.Top = va_rect.y; |
| 1485 |
rect.Right = va_rect.x + va_rect.width - 1; |
| 1486 |
rect.Bottom = va_rect.y + va_rect.height - 1; |
| 1487 |
|
| 1488 |
// Convert from pixel units to block units |
| 1489 |
rect.Left /= blockSize; |
| 1490 |
rect.Right /= blockSize; |
| 1491 |
rect.Top /= blockSize; |
| 1492 |
rect.Bottom /= blockSize; |
| 1493 |
|
| 1494 |
// Check and adjust if rect not within frame boundaries |
| 1495 |
rect.Left = MOS_MIN(rect.Left, rightBorder); |
| 1496 |
rect.Right = MOS_MIN(rect.Right, rightBorder); |
| 1497 |
rect.Top = MOS_MIN(rect.Top, bottomBorder); |
| 1498 |
rect.Bottom = MOS_MIN(rect.Bottom, bottomBorder); |
| 1499 |
if (rect.Left > rect.Right || rect.Top > rect.Bottom) |
| 1500 |
{ |
| 1501 |
DDI_ASSERTMESSAGE("Invalid rect region parameter."); |
| 1502 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1503 |
} |
| 1504 |
picParams->NumDirtyRects ++; |
| 1505 |
} |
| 1506 |
} |
| 1507 |
break; |
| 1508 |
} |
| 1509 |
case VAEncMiscParameterTypeSkipFrame: |
| 1510 |
{ |
| 1511 |
VAEncMiscParameterSkipFrame *vaEncMiscParamSkipFrame = (VAEncMiscParameterSkipFrame *)miscParamBuf->data; |
| 1512 |
// populate skipped frame params from DDI |
| 1513 |
if (FRAME_SKIP_NORMAL != vaEncMiscParamSkipFrame->skip_frame_flag) |
| 1514 |
{ |
| 1515 |
DDI_ASSERTMESSAGE("unsupported misc parameter type."); |
| 1516 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1517 |
} |
| 1518 |
picParams->SkipFrameFlag = vaEncMiscParamSkipFrame->skip_frame_flag; |
| 1519 |
picParams->NumSkipFrames = vaEncMiscParamSkipFrame->num_skip_frames; |
| 1520 |
picParams->SizeSkipFrames = vaEncMiscParamSkipFrame->size_skip_frames; |
| 1521 |
break; |
| 1522 |
} |
| 1523 |
case VAEncMiscParameterTypeMaxSliceSize: |
| 1524 |
{ |
| 1525 |
VAEncMiscParameterMaxSliceSize *vaEncMiscParamMaxSliceSize = (VAEncMiscParameterMaxSliceSize *)miscParamBuf->data; |
| 1526 |
m_encodeCtx->EnableSliceLevelRateCtrl = true; |
| 1527 |
seqParams->SliceSizeControl = (vaEncMiscParamMaxSliceSize->max_slice_size > 0) ? true : false;; |
| 1528 |
picParams->MaxSliceSizeInBytes = vaEncMiscParamMaxSliceSize->max_slice_size; |
| 1529 |
break; |
| 1530 |
} |
| 1531 |
case VAEncMiscParameterTypeEncQuality: |
| 1532 |
{ |
| 1533 |
VAEncMiscParameterEncQuality *vaEncMiscParamPrivate = (VAEncMiscParameterEncQuality *)miscParamBuf->data; |
| 1534 |
picParams->bUseRawPicForRef = vaEncMiscParamPrivate->useRawPicForRef; |
| 1535 |
break; |
| 1536 |
} |
| 1537 |
case VAEncMiscParameterTypeMaxFrameSize: |
| 1538 |
{ |
| 1539 |
VAEncMiscParameterBufferMaxFrameSize *vaEncData = (VAEncMiscParameterBufferMaxFrameSize *)miscParamBuf->data; |
| 1540 |
seqParams->UserMaxIFrameSize = vaEncData->max_frame_size>>3; // convert to bytes |
| 1541 |
seqParams->UserMaxPBFrameSize = vaEncData->max_frame_size>>3; // convert to bytes |
| 1542 |
break; |
| 1543 |
} |
| 1544 |
default: |
| 1545 |
DDI_ASSERTMESSAGE("unsupported misc parameter type."); |
| 1546 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 1547 |
} |
| 1548 |
|
| 1549 |
API_STICKER_TRACE_EXIT(VA_STATUS_SUCCESS); |
| 1550 |
return VA_STATUS_SUCCESS; |
| 1551 |
} |
| 1552 |
|
| 1553 |
uint8_t DdiEncodeHevc::CodechalPicTypeFromVaSlcType(uint8_t vaSliceType) |
| 1554 |
{ |
| 1555 |
switch (vaSliceType) |
| 1556 |
{ |
| 1557 |
case sliceTypeI: |
| 1558 |
return I_TYPE; |
| 1559 |
case sliceTypeP: |
| 1560 |
if (m_codechalSettings->isSCCEnabled) |
| 1561 |
{ |
| 1562 |
PCODEC_HEVC_ENCODE_PICTURE_PARAMS hevcPicParams = (PCODEC_HEVC_ENCODE_PICTURE_PARAMS)((uint8_t *)m_encodeCtx->pPicParams); |
| 1563 |
if (hevcPicParams->CodingType == I_TYPE && hevcPicParams->pps_curr_pic_ref_enabled_flag) |
| 1564 |
{ |
| 1565 |
return I_TYPE; |
| 1566 |
} |
| 1567 |
} |
| 1568 |
return P_TYPE; |
| 1569 |
case sliceTypeB: |
| 1570 |
return B_TYPE; |
| 1571 |
default: |
| 1572 |
return 0; |
| 1573 |
} |
| 1574 |
} |
| 1575 |
|
| 1576 |
void DdiEncodeHevc::GetSlcRefIdx(CODEC_PICTURE *picReference, CODEC_PICTURE *slcReference) |
| 1577 |
{ |
| 1578 |
if (nullptr == picReference || nullptr == slcReference) |
| 1579 |
{ |
| 1580 |
return; |
| 1581 |
} |
| 1582 |
|
| 1583 |
int32_t i = 0; |
| 1584 |
if (CODECHAL_NUM_UNCOMPRESSED_SURFACE_HEVC != slcReference->FrameIdx) |
| 1585 |
{ |
| 1586 |
for (i = 0; i < numMaxRefFrame; i++) |
| 1587 |
{ |
| 1588 |
if (slcReference->FrameIdx == picReference[i].FrameIdx) |
| 1589 |
{ |
| 1590 |
slcReference->FrameIdx = i; |
| 1591 |
slcReference->PicEntry = i; |
| 1592 |
break; |
| 1593 |
} |
| 1594 |
} |
| 1595 |
if (numMaxRefFrame == i) |
| 1596 |
{ |
| 1597 |
slcReference->FrameIdx = CODECHAL_NUM_UNCOMPRESSED_SURFACE_HEVC; |
| 1598 |
slcReference->PicFlags = PICTURE_INVALID; |
| 1599 |
slcReference->PicEntry = 0xFF; |
| 1600 |
} |
| 1601 |
} |
| 1602 |
} |
| 1603 |
|
| 1604 |
void DdiEncodeHevc::SetupCodecPicture( |
| 1605 |
DDI_MEDIA_CONTEXT *mediaCtx, |
| 1606 |
DDI_CODEC_RENDER_TARGET_TABLE *rtTbl, |
| 1607 |
CODEC_PICTURE *codecHalPic, |
| 1608 |
VAPictureHEVC vaPicHEVC, |
| 1609 |
bool picReference, |
| 1610 |
bool sliceReference) |
| 1611 |
{ |
| 1612 |
DDI_UNUSED(sliceReference); |
| 1613 |
|
| 1614 |
if (DDI_CODEC_INVALID_FRAME_INDEX != vaPicHEVC.picture_id) |
| 1615 |
{ |
| 1616 |
codecHalPic->FrameIdx = GetRenderTargetID(rtTbl, DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, vaPicHEVC.picture_id)); |
| 1617 |
codecHalPic->PicEntry = codecHalPic->FrameIdx; |
| 1618 |
} |
| 1619 |
else |
| 1620 |
{ |
| 1621 |
codecHalPic->FrameIdx = CODECHAL_NUM_UNCOMPRESSED_SURFACE_HEVC; |
| 1622 |
codecHalPic->PicFlags = PICTURE_INVALID; |
| 1623 |
codecHalPic->PicEntry = 0xFF; |
| 1624 |
} |
| 1625 |
|
| 1626 |
if (PICTURE_INVALID != codecHalPic->PicFlags) |
| 1627 |
{ |
| 1628 |
codecHalPic->PicFlags = PICTURE_FRAME; |
| 1629 |
if (picReference) |
| 1630 |
{ |
| 1631 |
if ((vaPicHEVC.flags & VA_PICTURE_HEVC_LONG_TERM_REFERENCE) == VA_PICTURE_HEVC_LONG_TERM_REFERENCE) |
| 1632 |
{ |
| 1633 |
codecHalPic->PicFlags = (CODEC_PICTURE_FLAG)(codecHalPic->PicFlags | PICTURE_LONG_TERM_REFERENCE); |
| 1634 |
codecHalPic->PicEntry |= 0x80; |
| 1635 |
} |
| 1636 |
else |
| 1637 |
{ |
| 1638 |
codecHalPic->PicFlags = (CODEC_PICTURE_FLAG)(codecHalPic->PicFlags | PICTURE_SHORT_TERM_REFERENCE); |
| 1639 |
} |
| 1640 |
} |
| 1641 |
} |
| 1642 |
} |
| 1643 |
|
| 1644 |
uint32_t DdiEncodeHevc::getSliceParameterBufferSize() |
| 1645 |
{ |
| 1646 |
return sizeof(VAEncSliceParameterBufferHEVC); |
| 1647 |
} |
| 1648 |
|
| 1649 |
uint32_t DdiEncodeHevc::getSequenceParameterBufferSize() |
| 1650 |
{ |
| 1651 |
return sizeof(VAEncSequenceParameterBufferHEVC); |
| 1652 |
} |
| 1653 |
|
| 1654 |
uint32_t DdiEncodeHevc::getPictureParameterBufferSize() |
| 1655 |
{ |
| 1656 |
return sizeof(VAEncPictureParameterBufferHEVC); |
| 1657 |
} |
| 1658 |
|
| 1659 |
uint32_t DdiEncodeHevc::getQMatrixBufferSize() |
| 1660 |
{ |
| 1661 |
return sizeof(VAQMatrixBufferHEVC); |
| 1662 |
} |
| 1663 |
|
| 1664 |
bool DdiEncodeHevc::IsSccProfile() |
| 1665 |
{ |
| 1666 |
return (m_encodeCtx->vaProfile==VAProfileHEVCSccMain |
| 1667 |
|| m_encodeCtx->vaProfile==VAProfileHEVCSccMain10 |
| 1668 |
|| m_encodeCtx->vaProfile==VAProfileHEVCSccMain444 |
| 1669 |
|| m_encodeCtx->vaProfile==VAProfileHEVCSccMain444_10); |
| 1670 |
} |