| 1 |
/* |
| 2 |
* Copyright (c) 2022-2023, 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_decode_jpeg_specific.cpp |
| 24 |
//! \brief JPEG class definition for DDI media decoder |
| 25 |
//! |
| 26 |
|
| 27 |
#include "media_libva_util_next.h" |
| 28 |
#include "media_libva_interface_next.h" |
| 29 |
#include "ddi_decode_jpeg_specific.h" |
| 30 |
#include "mos_solo_generic.h" |
| 31 |
#include "codechal_decode_jpeg.h" |
| 32 |
|
| 33 |
typedef enum _DDI_DECODE_JPEG_BUFFER_STATE |
| 34 |
{ |
| 35 |
BUFFER_UNLOADED = 0, |
| 36 |
BUFFER_LOADED = 1, |
| 37 |
} DDI_DECODE_JPEG_BUFFER_STATE; |
| 38 |
|
| 39 |
namespace decode |
| 40 |
{ |
| 41 |
|
| 42 |
#define DDI_DECODE_JPEG_MAXIMUM_HUFFMAN_TABLE 2 |
| 43 |
#define DDI_DECODE_JPEG_MAXIMUM_QMATRIX_NUM 4 |
| 44 |
#define DDI_DECODE_JPEG_MAXIMUM_QMATRIX_ENTRIES 64 |
| 45 |
#define DDI_DECODE_JPEG_SLICE_PARAM_BUF_NUM 0x4 // According to JPEG SPEC, max slice per frame is 4 |
| 46 |
|
| 47 |
static const uint32_t zigzag_order[64] = |
| 48 |
{ |
| 49 |
0, 1, 8, 16, 9, 2, 3, 10, |
| 50 |
17, 24, 32, 25, 18, 11, 4, 5, |
| 51 |
12, 19, 26, 33, 40, 48, 41, 34, |
| 52 |
27, 20, 13, 6, 7, 14, 21, 28, |
| 53 |
35, 42, 49, 56, 57, 50, 43, 36, |
| 54 |
29, 22, 15, 23, 30, 37, 44, 51, |
| 55 |
58, 59, 52, 45, 38, 31, 39, 46, |
| 56 |
53, 60, 61, 54, 47, 55, 62, 63 |
| 57 |
}; |
| 58 |
|
| 59 |
VAStatus DdiDecodeJpeg::ParseSliceParams( |
| 60 |
DDI_MEDIA_CONTEXT *mediaCtx, |
| 61 |
VASliceParameterBufferJPEGBaseline *slcParam, |
| 62 |
uint32_t numSlices) |
| 63 |
{ |
| 64 |
CodecDecodeJpegScanParameter *jpegSliceParam = |
| 65 |
(CodecDecodeJpegScanParameter *)(m_decodeCtx->DecodeParams.m_sliceParams); |
| 66 |
|
| 67 |
CodecDecodeJpegPicParams *picParam = (CodecDecodeJpegPicParams *)(m_decodeCtx->DecodeParams.m_picParams); |
| 68 |
|
| 69 |
if ((jpegSliceParam == nullptr) || |
| 70 |
(picParam == nullptr) || |
| 71 |
(slcParam == nullptr)) |
| 72 |
{ |
| 73 |
DDI_CODEC_ASSERTMESSAGE("Invalid Parameter for Parsing JPEG Slice parameter\n"); |
| 74 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 75 |
} |
| 76 |
|
| 77 |
jpegSliceParam->NumScans += numSlices; |
| 78 |
picParam->m_totalScans += numSlices; |
| 79 |
if (picParam->m_totalScans == 1 && slcParam[0].num_components > 1) |
| 80 |
{ |
| 81 |
picParam->m_interleavedData = 1; |
| 82 |
} |
| 83 |
|
| 84 |
uint32_t j, i; |
| 85 |
int32_t startIdx = m_numScans; |
| 86 |
for (j = 0; j < numSlices; j++) |
| 87 |
{ |
| 88 |
if (j + startIdx >= jpegNumComponent || |
| 89 |
slcParam[j].num_components > jpegNumComponent) |
| 90 |
{ |
| 91 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 92 |
} |
| 93 |
|
| 94 |
for (i = 0; i < slcParam[j].num_components; i++) |
| 95 |
{ |
| 96 |
jpegSliceParam->ScanHeader[j + startIdx].ComponentSelector[i] = slcParam[j].components[i].component_selector; |
| 97 |
jpegSliceParam->ScanHeader[j + startIdx].DcHuffTblSelector[i] = slcParam[j].components[i].dc_table_selector; |
| 98 |
jpegSliceParam->ScanHeader[j + startIdx].AcHuffTblSelector[i] = slcParam[j].components[i].ac_table_selector; |
| 99 |
} |
| 100 |
jpegSliceParam->ScanHeader[j + startIdx].NumComponents = slcParam[j].num_components; |
| 101 |
jpegSliceParam->ScanHeader[j + startIdx].RestartInterval = slcParam[j].restart_interval; |
| 102 |
jpegSliceParam->ScanHeader[j + startIdx].MCUCount = slcParam[j].num_mcus; |
| 103 |
jpegSliceParam->ScanHeader[j + startIdx].ScanHoriPosition = slcParam[j].slice_horizontal_position; |
| 104 |
jpegSliceParam->ScanHeader[j + startIdx].ScanVertPosition = slcParam[j].slice_vertical_position; |
| 105 |
jpegSliceParam->ScanHeader[j + startIdx].DataOffset = slcParam[j].slice_data_offset; |
| 106 |
jpegSliceParam->ScanHeader[j + startIdx].DataLength = slcParam[j].slice_data_size; |
| 107 |
} |
| 108 |
|
| 109 |
return VA_STATUS_SUCCESS; |
| 110 |
} |
| 111 |
|
| 112 |
VAStatus DdiDecodeJpeg::ParsePicParams( |
| 113 |
DDI_MEDIA_CONTEXT *mediaCtx, |
| 114 |
VAPictureParameterBufferJPEGBaseline *picParam) |
| 115 |
{ |
| 116 |
CodecDecodeJpegPicParams *jpegPicParam = (CodecDecodeJpegPicParams *)(m_decodeCtx->DecodeParams.m_picParams); |
| 117 |
|
| 118 |
if ((jpegPicParam == nullptr) || |
| 119 |
(picParam == nullptr)) |
| 120 |
{ |
| 121 |
DDI_CODEC_ASSERTMESSAGE("Null Parameter for Parsing JPEG Picture parameter\n"); |
| 122 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 123 |
} |
| 124 |
|
| 125 |
jpegPicParam->m_frameWidth = picParam->picture_width; |
| 126 |
jpegPicParam->m_frameHeight = picParam->picture_height; |
| 127 |
jpegPicParam->m_numCompInFrame = picParam->num_components; |
| 128 |
|
| 129 |
switch (picParam->rotation) |
| 130 |
{ |
| 131 |
case VA_ROTATION_NONE: |
| 132 |
jpegPicParam->m_rotation = jpegRotation0; |
| 133 |
break; |
| 134 |
case VA_ROTATION_90: |
| 135 |
jpegPicParam->m_rotation = jpegRotation90; |
| 136 |
break; |
| 137 |
case VA_ROTATION_180: |
| 138 |
jpegPicParam->m_rotation = jpegRotation180; |
| 139 |
break; |
| 140 |
case VA_ROTATION_270: |
| 141 |
jpegPicParam->m_rotation = jpegRotation270; |
| 142 |
break; |
| 143 |
default: |
| 144 |
/* For the other rotation type, the rotation is disabled. */ |
| 145 |
jpegPicParam->m_rotation = jpegRotation0; |
| 146 |
break; |
| 147 |
; |
| 148 |
} |
| 149 |
|
| 150 |
if (jpegPicParam->m_numCompInFrame == 1) |
| 151 |
{ |
| 152 |
jpegPicParam->m_chromaType = jpegYUV400; |
| 153 |
} |
| 154 |
else if (jpegPicParam->m_numCompInFrame == 3) |
| 155 |
{ |
| 156 |
int32_t h1 = picParam->components[0].h_sampling_factor; |
| 157 |
int32_t h2 = picParam->components[1].h_sampling_factor; |
| 158 |
int32_t h3 = picParam->components[2].h_sampling_factor; |
| 159 |
int32_t v1 = picParam->components[0].v_sampling_factor; |
| 160 |
int32_t v2 = picParam->components[1].v_sampling_factor; |
| 161 |
int32_t v3 = picParam->components[2].v_sampling_factor; |
| 162 |
|
| 163 |
if (h1 == 2 && h2 == 1 && h3 == 1 && |
| 164 |
v1 == 2 && v2 == 1 && v3 == 1) |
| 165 |
{ |
| 166 |
jpegPicParam->m_chromaType = jpegYUV420; |
| 167 |
} |
| 168 |
else if (h1 == 2 && h2 == 1 && h3 == 1 && |
| 169 |
v1 == 1 && v2 == 1 && v3 == 1) |
| 170 |
{ |
| 171 |
jpegPicParam->m_chromaType = jpegYUV422H2Y; |
| 172 |
} |
| 173 |
else if (h1 == 1 && h2 == 1 && h3 == 1 && |
| 174 |
v1 == 1 && v2 == 1 && v3 == 1) |
| 175 |
{ |
| 176 |
switch (picParam->color_space) |
| 177 |
{ |
| 178 |
case 0: // YUV |
| 179 |
jpegPicParam->m_chromaType = jpegYUV444; |
| 180 |
break; |
| 181 |
case 1: // RGB |
| 182 |
jpegPicParam->m_chromaType = jpegRGB; |
| 183 |
break; |
| 184 |
case 2: // BGR |
| 185 |
jpegPicParam->m_chromaType = jpegBGR; |
| 186 |
break; |
| 187 |
default: |
| 188 |
/* For the other type, the default YUV444 is used */ |
| 189 |
jpegPicParam->m_chromaType = jpegYUV444; |
| 190 |
break; |
| 191 |
; |
| 192 |
} |
| 193 |
} |
| 194 |
else if (h1 == 4 && h2 == 1 && h3 == 1 && |
| 195 |
v1 == 1 && v2 == 1 && v3 == 1) |
| 196 |
{ |
| 197 |
jpegPicParam->m_chromaType = jpegYUV411; |
| 198 |
} |
| 199 |
else if (h1 == 1 && h2 == 1 && h3 == 1 && |
| 200 |
v1 == 2 && v2 == 1 && v3 == 1) |
| 201 |
{ |
| 202 |
jpegPicParam->m_chromaType = jpegYUV422V2Y; |
| 203 |
} |
| 204 |
else if (h1 == 2 && h2 == 1 && h3 == 1 && |
| 205 |
v1 == 2 && v2 == 2 && v3 == 2) |
| 206 |
{ |
| 207 |
jpegPicParam->m_chromaType = jpegYUV422H4Y; |
| 208 |
} |
| 209 |
else if (h1 == 2 && h2 == 2 && h3 == 2 && |
| 210 |
v1 == 2 && v2 == 1 && v3 == 1) |
| 211 |
{ |
| 212 |
jpegPicParam->m_chromaType = jpegYUV422V4Y; |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
DDI_CODEC_NORMALMESSAGE("Unsupported sampling factor in JPEG Picture parameter\n"); |
| 217 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
memset(jpegPicParam->m_componentIdentifier, 0, jpegNumComponent); |
| 222 |
memset(jpegPicParam->m_quantTableSelector, 0, jpegNumComponent); |
| 223 |
|
| 224 |
if (picParam->num_components > jpegNumComponent) |
| 225 |
{ |
| 226 |
DDI_CODEC_NORMALMESSAGE("Unsupported component num in JPEG Picture parameter\n"); |
| 227 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 228 |
} |
| 229 |
for (int32_t i = 0; i < picParam->num_components; i++) |
| 230 |
{ |
| 231 |
jpegPicParam->m_componentIdentifier[i] = picParam->components[i].component_id; |
| 232 |
jpegPicParam->m_quantTableSelector[i] = picParam->components[i].quantiser_table_selector; |
| 233 |
} |
| 234 |
|
| 235 |
return VA_STATUS_SUCCESS; |
| 236 |
} |
| 237 |
|
| 238 |
VAStatus DdiDecodeJpeg::ParseHuffmanTbl( |
| 239 |
DDI_MEDIA_CONTEXT *mediaCtx, |
| 240 |
VAHuffmanTableBufferJPEGBaseline *huffmanTbl) |
| 241 |
{ |
| 242 |
PCODECHAL_DECODE_JPEG_HUFFMAN_TABLE jpegHuffTbl = (PCODECHAL_DECODE_JPEG_HUFFMAN_TABLE)(m_decodeCtx->DecodeParams.m_huffmanTable); |
| 243 |
int32_t sumBITS = 0; |
| 244 |
|
| 245 |
if ((jpegHuffTbl == nullptr) || |
| 246 |
(huffmanTbl == nullptr)) |
| 247 |
{ |
| 248 |
DDI_CODEC_ASSERTMESSAGE("Null Parameter for Parsing JPEG Huffman Tableparameter\n"); |
| 249 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 250 |
} |
| 251 |
|
| 252 |
memset(jpegHuffTbl, 0, sizeof(CODECHAL_DECODE_JPEG_HUFFMAN_TABLE)); |
| 253 |
|
| 254 |
for (int32_t i = 0; i < DDI_DECODE_JPEG_MAXIMUM_HUFFMAN_TABLE; i++) |
| 255 |
{ |
| 256 |
if (huffmanTbl->load_huffman_table[i] == BUFFER_LOADED) |
| 257 |
{ |
| 258 |
sumBITS = 0; |
| 259 |
for (int32_t j = 0; j < JPEG_NUM_HUFF_TABLE_DC_BITS; j++) |
| 260 |
{ |
| 261 |
sumBITS += huffmanTbl->huffman_table[i].num_dc_codes[j]; |
| 262 |
} |
| 263 |
|
| 264 |
if (sumBITS > JPEG_NUM_HUFF_TABLE_DC_HUFFVAL) |
| 265 |
{ |
| 266 |
DDI_CODEC_ASSERTMESSAGE("Huffman table DC entries number is out of HW limitation."); |
| 267 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 268 |
} |
| 269 |
|
| 270 |
// the size of jpegHuffTbl->HuffTable[i].DC_BITS is 12 (defined in driver) |
| 271 |
// the size of huffmanTbl->huffman_table[i].num_dc_codes is 16 (defined in libva) |
| 272 |
// it is using the size of "DC_BITS" for solve the overflow |
| 273 |
MOS_SecureMemcpy(jpegHuffTbl->HuffTable[i].DC_BITS, |
| 274 |
sizeof(jpegHuffTbl->HuffTable[i].DC_BITS), |
| 275 |
huffmanTbl->huffman_table[i].num_dc_codes, |
| 276 |
sizeof(jpegHuffTbl->HuffTable[i].DC_BITS)); |
| 277 |
MOS_SecureMemcpy(jpegHuffTbl->HuffTable[i].DC_HUFFVAL, |
| 278 |
sizeof(jpegHuffTbl->HuffTable[i].DC_HUFFVAL), |
| 279 |
huffmanTbl->huffman_table[i].dc_values, |
| 280 |
sizeof(huffmanTbl->huffman_table[i].dc_values)); |
| 281 |
MOS_SecureMemcpy(jpegHuffTbl->HuffTable[i].AC_BITS, |
| 282 |
sizeof(jpegHuffTbl->HuffTable[i].AC_BITS), |
| 283 |
huffmanTbl->huffman_table[i].num_ac_codes, |
| 284 |
sizeof(huffmanTbl->huffman_table[i].num_ac_codes)); |
| 285 |
MOS_SecureMemcpy(jpegHuffTbl->HuffTable[i].AC_HUFFVAL, |
| 286 |
sizeof(jpegHuffTbl->HuffTable[i].AC_HUFFVAL), |
| 287 |
huffmanTbl->huffman_table[i].ac_values, |
| 288 |
sizeof(huffmanTbl->huffman_table[i].ac_values)); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
return VA_STATUS_SUCCESS; |
| 293 |
} |
| 294 |
|
| 295 |
///////////////////////////////////////////////////////////////////////////////////////// |
| 296 |
// |
| 297 |
// Function: JpegQMatrixDecode |
| 298 |
// Description: Parse the QMatrix table from VAAPI, and load the valid Qmatrix to the Buffer used by |
| 299 |
// CodecHal |
| 300 |
// |
| 301 |
///////////////////////////////////////////////////////////////////////////////////////// |
| 302 |
VAStatus DdiDecodeJpeg::ParseIQMatrix( |
| 303 |
DDI_MEDIA_CONTEXT *mediaCtx, |
| 304 |
VAIQMatrixBufferJPEGBaseline *matrix) |
| 305 |
{ |
| 306 |
CodecJpegQuantMatrix *jpegQMatrix = (CodecJpegQuantMatrix *)(m_decodeCtx->DecodeParams.m_iqMatrixBuffer); |
| 307 |
|
| 308 |
if ((matrix == nullptr) || (jpegQMatrix == nullptr)) |
| 309 |
{ |
| 310 |
DDI_CODEC_ASSERTMESSAGE("Null Parameter for Parsing JPEG IQMatrix \n"); |
| 311 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 312 |
} |
| 313 |
|
| 314 |
memset(jpegQMatrix, 0, sizeof(CodecJpegQuantMatrix)); |
| 315 |
|
| 316 |
int32_t idx = 0, idx2 = 0; |
| 317 |
for (idx = 0; idx < DDI_DECODE_JPEG_MAXIMUM_QMATRIX_NUM; idx++) |
| 318 |
{ |
| 319 |
if (matrix->load_quantiser_table[idx] == BUFFER_LOADED) |
| 320 |
{ |
| 321 |
for (idx2 = 0; idx2 < DDI_DECODE_JPEG_MAXIMUM_QMATRIX_ENTRIES; idx2++) |
| 322 |
{ |
| 323 |
jpegQMatrix->m_quantMatrix[idx][zigzag_order[idx2]] = matrix->quantiser_table[idx][idx2]; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
return VA_STATUS_SUCCESS; |
| 329 |
} |
| 330 |
|
| 331 |
VAStatus DdiDecodeJpeg::SetBufferRendered(VABufferID bufferID) |
| 332 |
{ |
| 333 |
DDI_CODEC_COM_BUFFER_MGR *bufMgr = &(m_decodeCtx->BufMgr); |
| 334 |
|
| 335 |
if (bufMgr == nullptr) |
| 336 |
{ |
| 337 |
DDI_CODEC_ASSERTMESSAGE("Null Parameter for Parsing Data buffer for JPEG\n"); |
| 338 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 339 |
} |
| 340 |
|
| 341 |
bool renderFlag = false; |
| 342 |
for (uint32_t i = 0; i < bufMgr->dwNumSliceData; i++) |
| 343 |
{ |
| 344 |
// Depend on the ID we tracked, if application want to rendered one of them |
| 345 |
// we set some flags |
| 346 |
if (bufMgr->pSliceData[i].vaBufferId == bufferID) |
| 347 |
{ |
| 348 |
if (bufMgr->pSliceData[i].bRendered == false) |
| 349 |
{ |
| 350 |
// set the rendered flags. which will be used when EndPicture. |
| 351 |
bufMgr->pSliceData[i].bRendered = true; |
| 352 |
// calculate the size of a bunch of rendered buffers. for endpicture to allocate appropriate memory size of GPU. |
| 353 |
bufMgr->dwSizeOfRenderedSliceData += bufMgr->pSliceData[i].uiLength; |
| 354 |
// keep record the render order, so that we can calculate the offset correctly when endpicture. |
| 355 |
// in this array we save the buffer index in pSliceData which render in sequence. if application create buffers like: 4,3,5,1,2. |
| 356 |
// but only render 2,3,5. the content in this array is: [4,1,2], which is the index of created buffer. |
| 357 |
bufMgr->pRenderedOrder[bufMgr->dwNumOfRenderedSliceData] = i; |
| 358 |
bufMgr->dwNumOfRenderedSliceData++; |
| 359 |
} |
| 360 |
renderFlag = true; |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if (renderFlag) |
| 366 |
{ |
| 367 |
return VA_STATUS_SUCCESS; |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 371 |
return VA_STATUS_ERROR_INVALID_BUFFER; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
VAStatus DdiDecodeJpeg::RenderPicture( |
| 376 |
VADriverContextP ctx, |
| 377 |
VAContextID context, |
| 378 |
VABufferID *buffers, |
| 379 |
int32_t numBuffers) |
| 380 |
{ |
| 381 |
DDI_CODEC_FUNC_ENTER; |
| 382 |
|
| 383 |
VAStatus va = VA_STATUS_SUCCESS; |
| 384 |
PDDI_MEDIA_CONTEXT mediaCtx = GetMediaContext(ctx); |
| 385 |
|
| 386 |
void *data = nullptr; |
| 387 |
for (int32_t i = 0; i < numBuffers; i++) |
| 388 |
{ |
| 389 |
if (!buffers || (buffers[i] == VA_INVALID_ID)) |
| 390 |
{ |
| 391 |
return VA_STATUS_ERROR_INVALID_BUFFER; |
| 392 |
} |
| 393 |
|
| 394 |
DDI_MEDIA_BUFFER *buf = MediaLibvaCommonNext::GetBufferFromVABufferID(mediaCtx, buffers[i]); |
| 395 |
if (nullptr == buf) |
| 396 |
{ |
| 397 |
return VA_STATUS_ERROR_INVALID_BUFFER; |
| 398 |
} |
| 399 |
|
| 400 |
uint32_t dataSize = buf->iSize; |
| 401 |
MediaLibvaInterfaceNext::MapBuffer(ctx, buffers[i], &data); |
| 402 |
|
| 403 |
if (data == nullptr) |
| 404 |
{ |
| 405 |
return VA_STATUS_ERROR_INVALID_BUFFER; |
| 406 |
} |
| 407 |
|
| 408 |
switch ((int32_t)buf->uiType) |
| 409 |
{ |
| 410 |
case VASliceDataBufferType: |
| 411 |
{ |
| 412 |
DDI_CODEC_CHK_RET(SetBufferRendered(buffers[i]),"SetBufferRendered failed!"); |
| 413 |
m_decodeCtx->DecodeParams.m_dataSize += dataSize; |
| 414 |
break; |
| 415 |
} |
| 416 |
case VASliceParameterBufferType: |
| 417 |
{ |
| 418 |
if (buf->uiNumElements == 0) |
| 419 |
{ |
| 420 |
return VA_STATUS_ERROR_INVALID_BUFFER; |
| 421 |
} |
| 422 |
|
| 423 |
uint32_t numSlices = buf->uiNumElements; |
| 424 |
|
| 425 |
if ((m_numScans + numSlices) > jpegNumComponent) |
| 426 |
{ |
| 427 |
DDI_CODEC_NORMALMESSAGE("the total number of JPEG scans are beyond the supported num(4)\n"); |
| 428 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 429 |
} |
| 430 |
DDI_CODEC_CHK_RET(AllocSliceParamContext(numSlices),"AllocSliceParamContext failed!"); |
| 431 |
VASliceParameterBufferJPEGBaseline *slcInfo = (VASliceParameterBufferJPEGBaseline *)data; |
| 432 |
DDI_CODEC_CHK_RET(ParseSliceParams(mediaCtx, slcInfo, numSlices),"ParseSliceParams failed!"); |
| 433 |
m_decodeCtx->BufMgr.pNumOfRenderedSliceParaForOneBuffer[m_decodeCtx->BufMgr.dwNumOfRenderedSlicePara] = numSlices; |
| 434 |
m_decodeCtx->BufMgr.dwNumOfRenderedSlicePara++; |
| 435 |
|
| 436 |
m_decodeCtx->DecodeParams.m_numSlices += numSlices; |
| 437 |
m_numScans += numSlices; |
| 438 |
m_groupIndex++; |
| 439 |
break; |
| 440 |
} |
| 441 |
case VAIQMatrixBufferType: |
| 442 |
{ |
| 443 |
VAIQMatrixBufferJPEGBaseline *imxBuf = (VAIQMatrixBufferJPEGBaseline *)data; |
| 444 |
DDI_CODEC_CHK_RET(ParseIQMatrix(mediaCtx, imxBuf),"ParseIQMatrix failed!"); |
| 445 |
break; |
| 446 |
} |
| 447 |
case VAPictureParameterBufferType: |
| 448 |
{ |
| 449 |
VAPictureParameterBufferJPEGBaseline *picParam = (VAPictureParameterBufferJPEGBaseline *)data; |
| 450 |
DDI_CODEC_CHK_RET(ParsePicParams(mediaCtx, picParam),"ParsePicParams failed!"); |
| 451 |
break; |
| 452 |
} |
| 453 |
case VAHuffmanTableBufferType: |
| 454 |
{ |
| 455 |
VAHuffmanTableBufferJPEGBaseline *huffTbl = (VAHuffmanTableBufferJPEGBaseline *)data; |
| 456 |
DDI_CODEC_CHK_RET(ParseHuffmanTbl(mediaCtx, huffTbl),"ParseHuffmanTbl failed!"); |
| 457 |
break; |
| 458 |
} |
| 459 |
case VAProcPipelineParameterBufferType: |
| 460 |
{ |
| 461 |
DDI_CODEC_NORMALMESSAGE("ProcPipeline is not supported for JPEGBaseline decoding\n"); |
| 462 |
break; |
| 463 |
} |
| 464 |
case VADecodeStreamoutBufferType: |
| 465 |
{ |
| 466 |
MediaLibvaCommonNext::MediaBufferToMosResource(buf, &m_decodeCtx->BufMgr.resExternalStreamOutBuffer); |
| 467 |
m_streamOutEnabled = true; |
| 468 |
break; |
| 469 |
} |
| 470 |
default: |
| 471 |
va = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE; |
| 472 |
break; |
| 473 |
} |
| 474 |
MediaLibvaInterfaceNext::UnmapBuffer(ctx, buffers[i]); |
| 475 |
} |
| 476 |
|
| 477 |
return va; |
| 478 |
} |
| 479 |
|
| 480 |
VAStatus DdiDecodeJpeg::BeginPicture( |
| 481 |
VADriverContextP ctx, |
| 482 |
VAContextID context, |
| 483 |
VASurfaceID renderTarget) |
| 484 |
{ |
| 485 |
VAStatus vaStatus = DdiDecodeBase::BeginPicture(ctx, context, renderTarget); |
| 486 |
|
| 487 |
if (vaStatus != VA_STATUS_SUCCESS) |
| 488 |
{ |
| 489 |
return vaStatus; |
| 490 |
} |
| 491 |
|
| 492 |
if (m_jpegBitstreamBuf) |
| 493 |
{ |
| 494 |
MediaLibvaUtilNext::FreeBuffer(m_jpegBitstreamBuf); |
| 495 |
MOS_FreeMemory(m_jpegBitstreamBuf); |
| 496 |
m_jpegBitstreamBuf = nullptr; |
| 497 |
} |
| 498 |
|
| 499 |
CodecDecodeJpegScanParameter *jpegSliceParam = |
| 500 |
(CodecDecodeJpegScanParameter *)(m_decodeCtx->DecodeParams.m_sliceParams); |
| 501 |
jpegSliceParam->NumScans = 0; |
| 502 |
|
| 503 |
CodecDecodeJpegPicParams *picParam = (CodecDecodeJpegPicParams *)(m_decodeCtx->DecodeParams.m_picParams); |
| 504 |
picParam->m_totalScans = 0; |
| 505 |
|
| 506 |
m_numScans = 0; |
| 507 |
return vaStatus; |
| 508 |
} |
| 509 |
|
| 510 |
bool DdiDecodeJpeg::CheckFormat(MOS_FORMAT format) |
| 511 |
{ |
| 512 |
bool isSupported = false; |
| 513 |
CodecDecodeJpegPicParams *jpegPicParam = (CodecDecodeJpegPicParams *)(m_decodeCtx->DecodeParams.m_picParams); |
| 514 |
switch (jpegPicParam->m_chromaType) |
| 515 |
{ |
| 516 |
case jpegYUV400: |
| 517 |
if (format == Format_400P || format == Format_A8R8G8B8) |
| 518 |
{ |
| 519 |
isSupported = true; |
| 520 |
} |
| 521 |
break; |
| 522 |
case jpegYUV420: |
| 523 |
if (format == Format_NV12 || format == Format_YUY2 || format == Format_UYVY || format == Format_IMC3 || format == Format_A8R8G8B8) |
| 524 |
{ |
| 525 |
isSupported = true; |
| 526 |
} |
| 527 |
break; |
| 528 |
case jpegYUV411: |
| 529 |
if (format == Format_411P) |
| 530 |
{ |
| 531 |
isSupported = true; |
| 532 |
} |
| 533 |
break; |
| 534 |
case jpegYUV422H2Y: |
| 535 |
case jpegYUV422H4Y: |
| 536 |
if (format == Format_NV12 || format == Format_YUY2 || format == Format_UYVY || format == Format_422H || format == Format_A8R8G8B8) |
| 537 |
{ |
| 538 |
isSupported = true; |
| 539 |
} |
| 540 |
break; |
| 541 |
case jpegYUV422V2Y: |
| 542 |
case jpegYUV422V4Y: |
| 543 |
if (format == Format_NV12 || format == Format_422V || format == Format_A8R8G8B8) |
| 544 |
{ |
| 545 |
isSupported = true; |
| 546 |
} |
| 547 |
break; |
| 548 |
case jpegYUV444: |
| 549 |
if (format == Format_444P || format == Format_A8R8G8B8) |
| 550 |
{ |
| 551 |
isSupported = true; |
| 552 |
} |
| 553 |
break; |
| 554 |
case jpegRGB: |
| 555 |
case jpegBGR: |
| 556 |
if (format == Format_RGBP || format == Format_BGRP ||format == Format_A8R8G8B8) |
| 557 |
{ |
| 558 |
isSupported = true; |
| 559 |
} |
| 560 |
break; |
| 561 |
default: |
| 562 |
break; |
| 563 |
} |
| 564 |
|
| 565 |
return isSupported; |
| 566 |
} |
| 567 |
|
| 568 |
VAStatus DdiDecodeJpeg::InitDecodeParams( |
| 569 |
VADriverContextP ctx, |
| 570 |
VAContextID context) |
| 571 |
{ |
| 572 |
/* skip the mediaCtx check as it is checked in caller */ |
| 573 |
PDDI_MEDIA_CONTEXT mediaCtx; |
| 574 |
mediaCtx = GetMediaContext(ctx); |
| 575 |
DDI_CODEC_CHK_RET(DecodeCombineBitstream(mediaCtx),"DecodeCombineBitstream failed!"); |
| 576 |
DDI_CODEC_COM_BUFFER_MGR *bufMgr = &(m_decodeCtx->BufMgr); |
| 577 |
bufMgr->dwNumSliceControl = 0; |
| 578 |
memset(&m_destSurface, 0, sizeof(MOS_SURFACE)); |
| 579 |
m_destSurface.dwOffset = 0; |
| 580 |
DDI_CODEC_RENDER_TARGET_TABLE *rtTbl = &(m_decodeCtx->RTtbl); |
| 581 |
|
| 582 |
if ((rtTbl == nullptr) || (rtTbl->pCurrentRT == nullptr)) |
| 583 |
{ |
| 584 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 585 |
} |
| 586 |
return VA_STATUS_SUCCESS; |
| 587 |
} |
| 588 |
|
| 589 |
VAStatus DdiDecodeJpeg::SetDecodeParams() |
| 590 |
{ |
| 591 |
DDI_CODEC_COM_BUFFER_MGR *bufMgr = &(m_decodeCtx->BufMgr); |
| 592 |
|
| 593 |
// we do not support mismatched usecase. |
| 594 |
if ((bufMgr->dwNumOfRenderedSlicePara != bufMgr->dwNumOfRenderedSliceData) || |
| 595 |
(bufMgr->dwNumOfRenderedSlicePara == 0)) |
| 596 |
{ |
| 597 |
DDI_CODEC_NORMALMESSAGE("DDI: Unsupported buffer mismatch usage!\n"); |
| 598 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 599 |
} |
| 600 |
|
| 601 |
// Allocate GPU Buffer description keeper. |
| 602 |
m_jpegBitstreamBuf = (DDI_MEDIA_BUFFER *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_BUFFER)); |
| 603 |
if (m_jpegBitstreamBuf == nullptr) |
| 604 |
{ |
| 605 |
DDI_CODEC_ASSERTMESSAGE("DDI: Allocate Jpeg Media Buffer Failed!\n"); |
| 606 |
return VA_STATUS_ERROR_UNKNOWN; |
| 607 |
} |
| 608 |
|
| 609 |
m_jpegBitstreamBuf->iSize = bufMgr->dwSizeOfRenderedSliceData; |
| 610 |
m_jpegBitstreamBuf->uiNumElements = bufMgr->dwNumOfRenderedSliceData; |
| 611 |
m_jpegBitstreamBuf->uiType = VASliceDataBufferType; |
| 612 |
m_jpegBitstreamBuf->format = Media_Format_Buffer; |
| 613 |
m_jpegBitstreamBuf->uiOffset = 0; |
| 614 |
m_jpegBitstreamBuf->bCFlushReq = false; |
| 615 |
m_jpegBitstreamBuf->pMediaCtx = m_decodeCtx->pMediaCtx; |
| 616 |
|
| 617 |
// Create GPU buffer |
| 618 |
VAStatus va = MediaLibvaUtilNext::CreateBuffer(m_jpegBitstreamBuf, m_decodeCtx->pMediaCtx->pDrmBufMgr); |
| 619 |
if (va != VA_STATUS_SUCCESS) |
| 620 |
{ |
| 621 |
MediaLibvaUtilNext::FreeBuffer(m_jpegBitstreamBuf); |
| 622 |
MOS_FreeMemory(m_jpegBitstreamBuf); |
| 623 |
m_jpegBitstreamBuf = nullptr; |
| 624 |
return va; |
| 625 |
} |
| 626 |
|
| 627 |
// For the first time you call MediaLibvaUtilNext::LockBuffer for a fresh GPU memory, it will map GPU address to a virtual address. |
| 628 |
// and then, MediaLibvaUtilNext::LockBuffer is acutally to increase the reference count. so we used here for 2 resaons: |
| 629 |
// |
| 630 |
// 1. Map GPU address to virtual at the beginning when we combine the CPU -> GPU. |
| 631 |
uint8_t *mappedBuf = (uint8_t *)MediaLibvaUtilNext::LockBuffer(m_jpegBitstreamBuf, MOS_LOCKFLAG_WRITEONLY); |
| 632 |
|
| 633 |
if (mappedBuf == nullptr) |
| 634 |
{ |
| 635 |
MediaLibvaUtilNext::FreeBuffer(m_jpegBitstreamBuf); |
| 636 |
MOS_FreeMemory(m_jpegBitstreamBuf); |
| 637 |
m_jpegBitstreamBuf = nullptr; |
| 638 |
return VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 639 |
} |
| 640 |
|
| 641 |
// get the JPEG Slice Header Params for offset recaculated. |
| 642 |
CodecDecodeJpegScanParameter *sliceParam = |
| 643 |
(CodecDecodeJpegScanParameter *)(m_decodeCtx->DecodeParams.m_sliceParams); |
| 644 |
|
| 645 |
uint32_t bufOffset = 0; |
| 646 |
int32_t orderSlicePara = 0; |
| 647 |
int32_t orderSliceData = 0; |
| 648 |
for (uint32_t i = 0; i < bufMgr->dwNumOfRenderedSliceData; i++) |
| 649 |
{ |
| 650 |
// get the rendered slice data index in rendered order. |
| 651 |
int32_t renderedBufIdx = bufMgr->pRenderedOrder[i]; |
| 652 |
if (bufMgr->pSliceData[renderedBufIdx].bRendered) |
| 653 |
{ |
| 654 |
MOS_SecureMemcpy((void *)(mappedBuf + bufOffset), |
| 655 |
bufMgr->pSliceData[renderedBufIdx].uiLength, |
| 656 |
bufMgr->pSliceData[renderedBufIdx].pBaseAddress, |
| 657 |
bufMgr->pSliceData[renderedBufIdx].uiLength); |
| 658 |
|
| 659 |
// since we assume application must make sure ONE slice parameter buffer ONE slice data buffer, so we recaculate header offset here. |
| 660 |
for (int32_t j = 0; j < bufMgr->pNumOfRenderedSliceParaForOneBuffer[orderSliceData]; j++) |
| 661 |
{ |
| 662 |
sliceParam->ScanHeader[orderSlicePara].DataOffset += bufOffset; |
| 663 |
orderSlicePara++; |
| 664 |
} |
| 665 |
bufOffset += bufMgr->pSliceData[renderedBufIdx].uiLength; |
| 666 |
bufMgr->pNumOfRenderedSliceParaForOneBuffer[orderSliceData] = 0; |
| 667 |
orderSliceData++; |
| 668 |
bufMgr->pSliceData[renderedBufIdx].bRendered = false; |
| 669 |
} |
| 670 |
} |
| 671 |
MediaLibvaUtilNext::UnlockBuffer(m_jpegBitstreamBuf); |
| 672 |
MediaLibvaCommonNext::MediaBufferToMosResource(m_jpegBitstreamBuf, &(bufMgr->resBitstreamBuffer)); |
| 673 |
bufMgr->dwNumOfRenderedSliceData = 0; |
| 674 |
bufMgr->dwNumOfRenderedSlicePara = 0; |
| 675 |
bufMgr->dwSizeOfRenderedSliceData = 0; |
| 676 |
|
| 677 |
m_destSurface.dwOffset = 0; |
| 678 |
m_destSurface.Format = Format_NV12; |
| 679 |
|
| 680 |
CodecDecodeJpegPicParams *jpegPicParam = (CodecDecodeJpegPicParams *)(m_decodeCtx->DecodeParams.m_picParams); |
| 681 |
if ((m_decodeCtx->RTtbl.pCurrentRT->format == Media_Format_NV12) |
| 682 |
&&(jpegPicParam->m_chromaType == jpegYUV444)) |
| 683 |
{ |
| 684 |
UnRegisterRTSurfaces(&(m_decodeCtx->RTtbl), m_decodeCtx->RTtbl.pCurrentRT); |
| 685 |
m_decodeCtx->RTtbl.pCurrentRT = MediaLibvaCommonNext::ReplaceSurfaceWithNewFormat(m_decodeCtx->RTtbl.pCurrentRT, Media_Format_444P); |
| 686 |
RegisterRTSurfaces(&(m_decodeCtx->RTtbl), m_decodeCtx->RTtbl.pCurrentRT); |
| 687 |
} |
| 688 |
if (m_decodeCtx->RTtbl.pCurrentRT != nullptr) |
| 689 |
{ |
| 690 |
MediaLibvaCommonNext::MediaSurfaceToMosResource((&(m_decodeCtx->RTtbl))->pCurrentRT, &(m_destSurface.OsResource)); |
| 691 |
} |
| 692 |
|
| 693 |
if (!CheckFormat(m_destSurface.OsResource.Format)) |
| 694 |
{ |
| 695 |
DDI_ASSERTMESSAGE("Surface fomrat of decoded surface is inconsistent with Codec bitstream!\n"); |
| 696 |
return VA_STATUS_ERROR_INVALID_PARAMETER; |
| 697 |
} |
| 698 |
|
| 699 |
(&m_decodeCtx->DecodeParams)->m_destSurface = &m_destSurface; |
| 700 |
|
| 701 |
(&m_decodeCtx->DecodeParams)->m_deblockSurface = nullptr; |
| 702 |
|
| 703 |
(&m_decodeCtx->DecodeParams)->m_dataBuffer = &bufMgr->resBitstreamBuffer; |
| 704 |
(&m_decodeCtx->DecodeParams)->m_bitStreamBufData = bufMgr->pBitstreamBuffer; |
| 705 |
Mos_Solo_OverrideBufferSize((&m_decodeCtx->DecodeParams)->m_dataSize, (&m_decodeCtx->DecodeParams)->m_dataBuffer); |
| 706 |
|
| 707 |
(&m_decodeCtx->DecodeParams)->m_bitplaneBuffer = nullptr; |
| 708 |
|
| 709 |
if (m_streamOutEnabled) |
| 710 |
{ |
| 711 |
(&m_decodeCtx->DecodeParams)->m_streamOutEnabled = true; |
| 712 |
(&m_decodeCtx->DecodeParams)->m_externalStreamOutBuffer = &bufMgr->resExternalStreamOutBuffer; |
| 713 |
} |
| 714 |
else |
| 715 |
{ |
| 716 |
(&m_decodeCtx->DecodeParams)->m_streamOutEnabled = false; |
| 717 |
(&m_decodeCtx->DecodeParams)->m_externalStreamOutBuffer = nullptr; |
| 718 |
} |
| 719 |
return VA_STATUS_SUCCESS; |
| 720 |
} |
| 721 |
|
| 722 |
VAStatus DdiDecodeJpeg::AllocSliceParamContext( |
| 723 |
uint32_t numSlices) |
| 724 |
{ |
| 725 |
uint32_t baseSize = sizeof(CodecDecodeJpegScanParameter); |
| 726 |
|
| 727 |
if (m_sliceParamBufNum < (m_decodeCtx->DecodeParams.m_numSlices + numSlices)) |
| 728 |
{ |
| 729 |
// in order to avoid that the buffer is reallocated multi-times, |
| 730 |
// extra 10 slices are added. |
| 731 |
uint32_t extraSlices = numSlices + 10; |
| 732 |
|
| 733 |
m_decodeCtx->DecodeParams.m_sliceParams = realloc(m_decodeCtx->DecodeParams.m_sliceParams, |
| 734 |
baseSize * (m_sliceParamBufNum + extraSlices)); |
| 735 |
|
| 736 |
if (m_decodeCtx->DecodeParams.m_sliceParams == nullptr) |
| 737 |
{ |
| 738 |
return VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 739 |
} |
| 740 |
|
| 741 |
memset((void *)((uint8_t *)m_decodeCtx->DecodeParams.m_sliceParams + baseSize * m_sliceParamBufNum), 0, baseSize * extraSlices); |
| 742 |
m_sliceParamBufNum += extraSlices; |
| 743 |
} |
| 744 |
|
| 745 |
return VA_STATUS_SUCCESS; |
| 746 |
} |
| 747 |
|
| 748 |
void DdiDecodeJpeg::DestroyContext( |
| 749 |
VADriverContextP ctx) |
| 750 |
{ |
| 751 |
FreeResourceBuffer(); |
| 752 |
// explicitly call the base function to do the further clean-up |
| 753 |
DdiDecodeBase::DestroyContext(ctx); |
| 754 |
return; |
| 755 |
} |
| 756 |
|
| 757 |
uint8_t* DdiDecodeJpeg::GetPicParamBuf( |
| 758 |
DDI_CODEC_COM_BUFFER_MGR *bufMgr) |
| 759 |
{ |
| 760 |
return (uint8_t*)(&(bufMgr->Codec_Param.Codec_Param_JPEG.PicParamJPEG)); |
| 761 |
} |
| 762 |
|
| 763 |
VAStatus DdiDecodeJpeg::AllocBsBuffer( |
| 764 |
DDI_CODEC_COM_BUFFER_MGR *bufMgr, |
| 765 |
DDI_MEDIA_BUFFER *buf) |
| 766 |
{ |
| 767 |
// Allocate JPEG slice data memory from CPU. |
| 768 |
uint8_t *bsAddr = nullptr; |
| 769 |
uint32_t index = 0; |
| 770 |
|
| 771 |
index = bufMgr->dwNumSliceData; |
| 772 |
|
| 773 |
/* the pSliceData needs to be reallocated in order to contain more SliceDataBuf */ |
| 774 |
if (index >= bufMgr->m_maxNumSliceData) |
| 775 |
{ |
| 776 |
/* In theroy it can resize the m_maxNumSliceData one by one. But in order to |
| 777 |
* avoid calling realloc frequently, it will try to allocate 10 to hold more |
| 778 |
* SliceDataBuf. This is only for the optimized purpose. |
| 779 |
*/ |
| 780 |
int32_t reallocSize = bufMgr->m_maxNumSliceData + 10; |
| 781 |
|
| 782 |
bufMgr->pSliceData = (DDI_CODEC_BITSTREAM_BUFFER_INFO *)realloc(bufMgr->pSliceData, sizeof(bufMgr->pSliceData[0]) * reallocSize); |
| 783 |
|
| 784 |
if (bufMgr->pSliceData == nullptr) |
| 785 |
{ |
| 786 |
DDI_CODEC_ASSERTMESSAGE("fail to reallocate pSliceData for JPEG\n."); |
| 787 |
return VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 788 |
} |
| 789 |
memset((void *)(bufMgr->pSliceData + bufMgr->m_maxNumSliceData), 0, |
| 790 |
sizeof(bufMgr->pSliceData[0]) * 10); |
| 791 |
|
| 792 |
bufMgr->m_maxNumSliceData += 10; |
| 793 |
} |
| 794 |
|
| 795 |
bsAddr = (uint8_t*)MOS_AllocAndZeroMemory(buf->iSize); |
| 796 |
if (bsAddr == 0) |
| 797 |
{ |
| 798 |
return VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 799 |
} |
| 800 |
|
| 801 |
buf->pData = bsAddr; |
| 802 |
buf->format = Media_Format_CPU; |
| 803 |
buf->bCFlushReq = false; |
| 804 |
buf->uiOffset = 0; |
| 805 |
bufMgr->pSliceData[index].uiLength = buf->iSize; |
| 806 |
bufMgr->pSliceData[index].uiOffset = buf->uiOffset; |
| 807 |
bufMgr->pSliceData[index].pBaseAddress = buf->pData; |
| 808 |
bufMgr->dwNumSliceData ++; |
| 809 |
return VA_STATUS_SUCCESS; |
| 810 |
} |
| 811 |
|
| 812 |
VAStatus DdiDecodeJpeg::AllocSliceControlBuffer( |
| 813 |
DDI_MEDIA_BUFFER *buf) |
| 814 |
{ |
| 815 |
DDI_CODEC_COM_BUFFER_MGR *bufMgr = nullptr; |
| 816 |
uint32_t availSize = 0; |
| 817 |
uint32_t newSize = 0; |
| 818 |
|
| 819 |
bufMgr = &(m_decodeCtx->BufMgr); |
| 820 |
availSize = m_sliceCtrlBufNum - bufMgr->dwNumSliceControl; |
| 821 |
|
| 822 |
if (availSize < buf->uiNumElements) |
| 823 |
{ |
| 824 |
newSize = sizeof(VASliceParameterBufferJPEGBaseline) * (m_sliceCtrlBufNum - availSize + buf->uiNumElements); |
| 825 |
bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG = (VASliceParameterBufferJPEGBaseline *)realloc(bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG, newSize); |
| 826 |
if (bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG == nullptr) |
| 827 |
{ |
| 828 |
return VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 829 |
} |
| 830 |
MOS_ZeroMemory(bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG + m_sliceCtrlBufNum, sizeof(VASliceParameterBufferJPEGBaseline) * (buf->uiNumElements - availSize)); |
| 831 |
m_sliceCtrlBufNum = m_sliceCtrlBufNum - availSize + buf->uiNumElements; |
| 832 |
} |
| 833 |
buf->pData = (uint8_t*)bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG; |
| 834 |
buf->uiOffset = sizeof(VASliceParameterBufferJPEGBaseline) * bufMgr->dwNumSliceControl; |
| 835 |
|
| 836 |
bufMgr->dwNumSliceControl += buf->uiNumElements; |
| 837 |
|
| 838 |
return VA_STATUS_SUCCESS; |
| 839 |
} |
| 840 |
|
| 841 |
void DdiDecodeJpeg::ContextInit( |
| 842 |
int32_t picWidth, |
| 843 |
int32_t picHeight) |
| 844 |
{ |
| 845 |
// call the function in base class to initialize it. |
| 846 |
DdiDecodeBase::ContextInit(picWidth, picHeight); |
| 847 |
|
| 848 |
m_decodeCtx->wMode = CODECHAL_DECODE_MODE_JPEG; |
| 849 |
|
| 850 |
return; |
| 851 |
} |
| 852 |
|
| 853 |
VAStatus DdiDecodeJpeg::InitResourceBuffer() |
| 854 |
{ |
| 855 |
VAStatus vaStatus = VA_STATUS_SUCCESS; |
| 856 |
DDI_CODEC_COM_BUFFER_MGR *bufMgr = &(m_decodeCtx->BufMgr); |
| 857 |
|
| 858 |
bufMgr->pSliceData = nullptr; |
| 859 |
bufMgr->ui64BitstreamOrder = 0; |
| 860 |
bufMgr->dwMaxBsSize = m_width * m_height * 3 / 2; |
| 861 |
|
| 862 |
bufMgr->dwNumSliceData = 0; |
| 863 |
bufMgr->dwNumSliceControl = 0; |
| 864 |
|
| 865 |
m_sliceCtrlBufNum = DDI_DECODE_JPEG_SLICE_PARAM_BUF_NUM; |
| 866 |
bufMgr->m_maxNumSliceData = DDI_DECODE_JPEG_SLICE_PARAM_BUF_NUM; |
| 867 |
bufMgr->pSliceData = (DDI_CODEC_BITSTREAM_BUFFER_INFO *)MOS_AllocAndZeroMemory(sizeof(bufMgr->pSliceData[0]) * DDI_DECODE_JPEG_SLICE_PARAM_BUF_NUM); |
| 868 |
if (bufMgr->pSliceData == nullptr) |
| 869 |
{ |
| 870 |
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 871 |
FreeResourceBuffer(); |
| 872 |
return vaStatus; |
| 873 |
} |
| 874 |
bufMgr->dwNumOfRenderedSlicePara = 0; |
| 875 |
bufMgr->dwNumOfRenderedSliceData = 0; |
| 876 |
bufMgr->pNumOfRenderedSliceParaForOneBuffer = (int32_t *)MOS_AllocAndZeroMemory(sizeof(bufMgr->pNumOfRenderedSliceParaForOneBuffer[0]) * m_sliceCtrlBufNum); |
| 877 |
bufMgr->pRenderedOrder = (int32_t *)MOS_AllocAndZeroMemory(sizeof(bufMgr->pRenderedOrder[0]) * m_sliceCtrlBufNum); |
| 878 |
bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG = (VASliceParameterBufferJPEGBaseline *)MOS_AllocAndZeroMemory(sizeof(VASliceParameterBufferJPEGBaseline) * m_sliceCtrlBufNum); |
| 879 |
if (bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG == nullptr) |
| 880 |
{ |
| 881 |
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 882 |
FreeResourceBuffer(); |
| 883 |
return vaStatus; |
| 884 |
} |
| 885 |
|
| 886 |
return VA_STATUS_SUCCESS; |
| 887 |
} |
| 888 |
|
| 889 |
void DdiDecodeJpeg::FreeResourceBuffer() |
| 890 |
{ |
| 891 |
DDI_CODEC_COM_BUFFER_MGR *bufMgr = &(m_decodeCtx->BufMgr); |
| 892 |
|
| 893 |
if (bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG) |
| 894 |
{ |
| 895 |
MOS_FreeMemory(bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG); |
| 896 |
bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG = nullptr; |
| 897 |
} |
| 898 |
bufMgr->dwNumOfRenderedSlicePara = 0; |
| 899 |
bufMgr->dwNumOfRenderedSliceData = 0; |
| 900 |
MOS_FreeMemory(bufMgr->pNumOfRenderedSliceParaForOneBuffer); |
| 901 |
bufMgr->pNumOfRenderedSliceParaForOneBuffer = nullptr; |
| 902 |
MOS_FreeMemory(bufMgr->pRenderedOrder); |
| 903 |
bufMgr->pRenderedOrder = nullptr; |
| 904 |
|
| 905 |
for (uint32_t i = 0; i < bufMgr->dwNumSliceData && (bufMgr->pSliceData != nullptr); i++) |
| 906 |
{ |
| 907 |
if (bufMgr->pSliceData[i].pBaseAddress != nullptr) |
| 908 |
{ |
| 909 |
MOS_FreeMemory(bufMgr->pSliceData[i].pBaseAddress); |
| 910 |
bufMgr->pSliceData[i].pBaseAddress = nullptr; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
bufMgr->dwNumSliceData = 0; |
| 915 |
|
| 916 |
if (m_jpegBitstreamBuf) |
| 917 |
{ |
| 918 |
MediaLibvaUtilNext::FreeBuffer(m_jpegBitstreamBuf); |
| 919 |
MOS_FreeMemory(m_jpegBitstreamBuf); |
| 920 |
m_jpegBitstreamBuf = nullptr; |
| 921 |
} |
| 922 |
|
| 923 |
// free decode bitstream buffer object |
| 924 |
MOS_FreeMemory(bufMgr->pSliceData); |
| 925 |
bufMgr->pSliceData = nullptr; |
| 926 |
|
| 927 |
return; |
| 928 |
} |
| 929 |
|
| 930 |
VAStatus DdiDecodeJpeg::CodecHalInit( |
| 931 |
DDI_MEDIA_CONTEXT *mediaCtx, |
| 932 |
void *ptr) |
| 933 |
{ |
| 934 |
VAStatus vaStatus = VA_STATUS_SUCCESS; |
| 935 |
MOS_CONTEXT *mosCtx = (MOS_CONTEXT *)ptr; |
| 936 |
|
| 937 |
CODECHAL_FUNCTION codecFunction = CODECHAL_FUNCTION_DECODE; |
| 938 |
m_decodeCtx->pCpDdiInterfaceNext->SetCpParams(m_ddiDecodeAttr->componentData.data.encryptType, m_codechalSettings); |
| 939 |
|
| 940 |
CODECHAL_STANDARD_INFO standardInfo; |
| 941 |
memset(&standardInfo, 0, sizeof(standardInfo)); |
| 942 |
|
| 943 |
standardInfo.CodecFunction = codecFunction; |
| 944 |
standardInfo.Mode = (CODECHAL_MODE)m_decodeCtx->wMode; |
| 945 |
|
| 946 |
m_codechalSettings->codecFunction = codecFunction; |
| 947 |
m_codechalSettings->width = m_width; |
| 948 |
m_codechalSettings->height = m_height; |
| 949 |
m_codechalSettings->intelEntrypointInUse = false; |
| 950 |
|
| 951 |
m_codechalSettings->lumaChromaDepth = CODECHAL_LUMA_CHROMA_DEPTH_8_BITS; |
| 952 |
|
| 953 |
m_codechalSettings->shortFormatInUse = m_decodeCtx->bShortFormatInUse; |
| 954 |
|
| 955 |
m_codechalSettings->mode = CODECHAL_DECODE_MODE_JPEG; |
| 956 |
m_codechalSettings->standard = CODECHAL_JPEG; |
| 957 |
|
| 958 |
#ifdef _DECODE_PROCESSING_SUPPORTED |
| 959 |
m_codechalSettings->sfcEnablingHinted = true; |
| 960 |
#endif |
| 961 |
|
| 962 |
m_decodeCtx->DecodeParams.m_iqMatrixBuffer = MOS_AllocAndZeroMemory(sizeof(CodecJpegQuantMatrix)); |
| 963 |
if (m_decodeCtx->DecodeParams.m_iqMatrixBuffer == nullptr) |
| 964 |
{ |
| 965 |
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 966 |
FreeResource(); |
| 967 |
return vaStatus; |
| 968 |
} |
| 969 |
m_decodeCtx->DecodeParams.m_picParams = MOS_AllocAndZeroMemory(sizeof(CodecDecodeJpegPicParams)); |
| 970 |
if (m_decodeCtx->DecodeParams.m_picParams == nullptr) |
| 971 |
{ |
| 972 |
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 973 |
FreeResource(); |
| 974 |
return vaStatus; |
| 975 |
} |
| 976 |
|
| 977 |
m_decodeCtx->DecodeParams.m_huffmanTable = (void*)MOS_AllocAndZeroMemory(sizeof(CODECHAL_DECODE_JPEG_HUFFMAN_TABLE)); |
| 978 |
if (!m_decodeCtx->DecodeParams.m_huffmanTable) |
| 979 |
{ |
| 980 |
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 981 |
FreeResource(); |
| 982 |
return vaStatus; |
| 983 |
} |
| 984 |
|
| 985 |
m_sliceParamBufNum = DDI_DECODE_JPEG_SLICE_PARAM_BUF_NUM; |
| 986 |
m_decodeCtx->DecodeParams.m_sliceParams = (void *)MOS_AllocAndZeroMemory(m_sliceParamBufNum * sizeof(CodecDecodeJpegScanParameter)); |
| 987 |
|
| 988 |
if (m_decodeCtx->DecodeParams.m_sliceParams == nullptr) |
| 989 |
{ |
| 990 |
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 991 |
FreeResource(); |
| 992 |
return vaStatus; |
| 993 |
} |
| 994 |
|
| 995 |
vaStatus = CreateCodecHal(mediaCtx, |
| 996 |
ptr, |
| 997 |
&standardInfo); |
| 998 |
|
| 999 |
if (vaStatus != VA_STATUS_SUCCESS) |
| 1000 |
{ |
| 1001 |
FreeResource(); |
| 1002 |
return vaStatus; |
| 1003 |
} |
| 1004 |
|
| 1005 |
if (InitResourceBuffer() != VA_STATUS_SUCCESS) |
| 1006 |
{ |
| 1007 |
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED; |
| 1008 |
FreeResource(); |
| 1009 |
return vaStatus; |
| 1010 |
} |
| 1011 |
|
| 1012 |
return vaStatus; |
| 1013 |
} |
| 1014 |
|
| 1015 |
void DdiDecodeJpeg::FreeResource() |
| 1016 |
{ |
| 1017 |
FreeResourceBuffer(); |
| 1018 |
|
| 1019 |
if (m_decodeCtx->pCodecHal) |
| 1020 |
{ |
| 1021 |
m_decodeCtx->pCodecHal->Destroy(); |
| 1022 |
MOS_Delete(m_decodeCtx->pCodecHal); |
| 1023 |
m_decodeCtx->pCodecHal = nullptr; |
| 1024 |
} |
| 1025 |
|
| 1026 |
MOS_FreeMemory(m_decodeCtx->DecodeParams.m_iqMatrixBuffer); |
| 1027 |
m_decodeCtx->DecodeParams.m_iqMatrixBuffer = nullptr; |
| 1028 |
MOS_FreeMemory(m_decodeCtx->DecodeParams.m_picParams); |
| 1029 |
m_decodeCtx->DecodeParams.m_picParams = nullptr; |
| 1030 |
MOS_FreeMemory(m_decodeCtx->DecodeParams.m_huffmanTable); |
| 1031 |
m_decodeCtx->DecodeParams.m_huffmanTable = nullptr; |
| 1032 |
MOS_FreeMemory(m_decodeCtx->DecodeParams.m_sliceParams); |
| 1033 |
m_decodeCtx->DecodeParams.m_sliceParams = nullptr; |
| 1034 |
|
| 1035 |
return; |
| 1036 |
} |
| 1037 |
|
| 1038 |
} // namespace decode |