Showing the changes to codec/decoder/core/src (1 of 5 files). Show all
Unified · Split
| @@ -297,6 +297,57 @@ bool FillDefaultSliceHeaderExt (PSliceHeaderExt pShExt, PNalUnitHeaderExt pNalEx · 296 unmodified lines | |||
| 297 | return true; | 297 | return true; |
| 298 | } | 298 | } |
| 299 | 299 | ||
| 300 | int32_t InitBsBuffer (PWelsDecoderContext pCtx) { | ||
| 301 | if (pCtx == NULL) | ||
| 302 | return ERR_INFO_INVALID_PTR; | ||
| 303 | |||
| 304 | pCtx->iMaxBsBufferSizeInByte = MIN_ACCESS_UNIT_CAPACITY * MAX_BUFFERED_NUM; | ||
| 305 | if ((pCtx->sRawData.pHead = static_cast<uint8_t*> (WelsMalloc (pCtx->iMaxBsBufferSizeInByte, | ||
| 306 | "pCtx->sRawData.pHead"))) == NULL) { | ||
| 307 | return ERR_INFO_OUT_OF_MEMORY; | ||
| 308 | } | ||
| 309 | pCtx->sRawData.pStartPos = pCtx->sRawData.pCurPos = pCtx->sRawData.pHead; | ||
| 310 | pCtx->sRawData.pEnd = pCtx->sRawData.pHead + pCtx->iMaxBsBufferSizeInByte; | ||
| 311 | return ERR_NONE; | ||
| 312 | } | ||
| 313 | |||
| 314 | int32_t ExpandBsBuffer (PWelsDecoderContext pCtx, const int kiSrcLen) { | ||
| 315 | if (pCtx == NULL) | ||
| 316 | return ERR_INFO_INVALID_PTR; | ||
| 317 | int32_t iExpandStepShift = 1; | ||
| 318 | int32_t iNewBuffLen = WELS_MAX ((kiSrcLen * MAX_BUFFERED_NUM), (pCtx->iMaxBsBufferSizeInByte << iExpandStepShift)); | ||
| 319 | //allocate new bs buffer | ||
| 320 | uint8_t* pNewBsBuff = static_cast<uint8_t*> (WelsMalloc (iNewBuffLen, "pCtx->sRawData.pHead")); | ||
| 321 | if (pNewBsBuff == NULL) | ||
| 322 | return ERR_INFO_OUT_OF_MEMORY; | ||
| 323 | |||
| 324 | //Copy current buffer status to new buffer | ||
| 325 | memcpy (pNewBsBuff, pCtx->sRawData.pHead, pCtx->iMaxBsBufferSizeInByte); | ||
| 326 | pCtx->iMaxBsBufferSizeInByte = iNewBuffLen; | ||
| 327 | pCtx->sRawData.pStartPos = pNewBsBuff + (pCtx->sRawData.pStartPos - pCtx->sRawData.pHead); | ||
| 328 | pCtx->sRawData.pCurPos = pNewBsBuff + (pCtx->sRawData.pCurPos - pCtx->sRawData.pHead); | ||
| 329 | pCtx->sRawData.pEnd = pNewBsBuff + iNewBuffLen; | ||
| 330 | WelsFree (pCtx->sRawData.pHead, "pCtx->sRawData.pHead"); | ||
| 331 | pCtx->sRawData.pHead = pNewBsBuff; | ||
| 332 | return ERR_NONE; | ||
| 333 | } | ||
| 334 | |||
| 335 | int32_t CheckBsBuffer (PWelsDecoderContext pCtx, const int32_t kiSrcLen) { | ||
| 336 | if (kiSrcLen > MAX_ACCESS_UNIT_CAPACITY) { //exceeds max allowed data | ||
| 337 | WelsLog (pCtx, WELS_LOG_WARNING, "Max AU size exceeded. Allowed size = %d, current size = %d", MAX_ACCESS_UNIT_CAPACITY, | ||
| 338 | kiSrcLen); | ||
| 339 | pCtx->iErrorCode |= dsBitstreamError; | ||
| 340 | return ERR_INFO_INVALID_ACCESS; | ||
| 341 | } else if (kiSrcLen > pCtx->iMaxBsBufferSizeInByte / | ||
| 342 | MAX_BUFFERED_NUM) { //may lead to buffer overwrite, prevent it by expanding buffer | ||
| 343 | if (ExpandBsBuffer (pCtx, kiSrcLen)) { | ||
| 344 | return ERR_INFO_OUT_OF_MEMORY; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | return ERR_NONE; | ||
| 349 | } | ||
| 350 | |||
| 300 | /* | 351 | /* |
| 301 | * WelsInitMemory | 352 | * WelsInitMemory |
| 302 | * Memory request for new introduced data | 353 | * Memory request for new introduced data |
| @@ -313,13 +364,8 @@ int32_t WelsInitMemory (PWelsDecoderContext pCtx) { · 10 unmodified lines | |||
| 313 | if (MemInitNalList (&pCtx->pAccessUnitList, MAX_NAL_UNIT_NUM_IN_AU) != 0) | 364 | if (MemInitNalList (&pCtx->pAccessUnitList, MAX_NAL_UNIT_NUM_IN_AU) != 0) |
| 314 | return ERR_INFO_OUT_OF_MEMORY; | 365 | return ERR_INFO_OUT_OF_MEMORY; |
| 315 | 366 | ||
| 316 | if ((pCtx->sRawData.pHead = static_cast<uint8_t*> (WelsMalloc (BS_BUFFER_SIZE, | 367 | if (InitBsBuffer (pCtx) != 0) |
| 317 | "pCtx->sRawData->pHead"))) == NULL) { | ||
| 318 | return ERR_INFO_OUT_OF_MEMORY; | 368 | return ERR_INFO_OUT_OF_MEMORY; |
| 319 | } | ||
| 320 | pCtx->sRawData.pStartPos = | ||
| 321 | pCtx->sRawData.pCurPos = pCtx->sRawData.pHead; | ||
| 322 | pCtx->sRawData.pEnd = pCtx->sRawData.pHead + BS_BUFFER_SIZE; | ||
| 323 | 369 | ||
| 324 | pCtx->uiTargetDqId = (uint8_t) - 1; | 370 | pCtx->uiTargetDqId = (uint8_t) - 1; |
| 325 | pCtx->bEndOfStreamFlag = false; | 371 | pCtx->bEndOfStreamFlag = false; |