History

enable dynamic AU size in decoder

huili2 committed 12 years ago

Commit bed18c21 parent 7de6eb25 files · +81 −14Browse files

Showing the changes to codec/decoder (5 of 5 files). Show all

Unified · Split

5 files
  1. codec/decoder/core/inc/decoder_context.h +1 −0
  2. codec/decoder/core/inc/decoder_core.h +23 −1
  3. codec/decoder/core/inc/wels_const.h +3 −2
  4. codec/decoder/core/src/decoder_core.cpp +52 −6
  5. codec/decoder/plus/src/welsDecoderExt.cpp +2 −5
codec/decoder/core/inc/decoder_context.h+1−0
@@ -233,6 +233,7 @@ typedef struct TagWelsDecoderContext { · 232 unmodified lines
233233 SVlcTable sVlcTable; // vlc table
234234
235235 SBitStringAux sBs;
236+ int32_t iMaxBsBufferSizeInByte; //actual memory size for BS buffer
236237
237238 /* Global memory external */
238239
codec/decoder/core/inc/decoder_core.h+23−1
@@ -48,6 +48,28 @@ · 47 unmodified lines
4848 #include "codec_def.h"
4949
5050 namespace WelsDec {
51+/*
52+ * InitBsBuffer
53+ * Memory allocation for Bitstream Buffer
54+ * return:
55+ * 0 - success; otherwise returned error_no defined in error_no.h.
56+ */
57+int32_t InitBsBuffer (PWelsDecoderContext pCtx);
58+
59+/*
60+ * ExpandBsBuffer
61+ * Expand current BS buffer and copy its content
62+ * new buffer size will consider input size as a reference
63+ * return:
64+ * 0 - success; otherwise returned error_no defined in error_no.h.
65+ */
66+int32_t ExpandBsBuffer (PWelsDecoderContext pCtx, const int32_t kiSrcLen);
67+
68+/*
69+ * CheckBsBuffer
70+ * Check if current buffer size is enough
71+ */
72+int32_t CheckBsBuffer (PWelsDecoderContext pCtx, const int32_t kiSrcLen);
5173
5274 /*
5375 * WelsInitMemory
@@ -56,7 +78,7 @@ namespace WelsDec { · 2 unmodified lines
5678 * rbsp_au_buffer, cur_dq_layer_ptr and ref_dq_layer_ptr in MB info cache.
5779 * return:
5880 * 0 - success; otherwise returned error_no defined in error_no.h.
59−*/
81+ */
6082 int32_t WelsInitMemory (PWelsDecoderContext pCtx);
6183
6284 /*
codec/decoder/core/inc/wels_const.h+3−2
@@ -92,8 +92,9 @@ · 91 unmodified lines
9292 #define LAYER_NUM_EXCHANGEABLE 1
9393
9494 #define MAX_NAL_UNIT_NUM_IN_AU 32 // predefined maximal number of NAL Units in an access unit
95−#define MAX_ACCESS_UNIT_CAPACITY 1048576 // Maximal AU capacity in bytes: (1<<20) = 1024 KB predefined
96−#define BS_BUFFER_SIZE (MAX_ACCESS_UNIT_CAPACITY * 3) //for delay case, keep three AU size to prevent buffer overwrite
95+#define MIN_ACCESS_UNIT_CAPACITY 1048576 // Min AU capacity in bytes: (1<<20) = 1024 KB predefined
96+#define MAX_BUFFERED_NUM 3 //mamixum stored number of AU|packet to prevent overwrite
97+#define MAX_ACCESS_UNIT_CAPACITY 7077888 //Maximum AU size in bytes for level 5.2 for single frame
9798 #define MAX_MACROBLOCK_CAPACITY 5000 //Maximal legal MB capacity, 15000 bits is enough
9899
99100 #endif//WELS_CONSTANCE_H__
codec/decoder/core/src/decoder_core.cpp+52−6
@@ -297,6 +297,57 @@ bool FillDefaultSliceHeaderExt (PSliceHeaderExt pShExt, PNalUnitHeaderExt pNalEx · 296 unmodified lines
297297 return true;
298298 }
299299
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+
300351 /*
301352 * WelsInitMemory
302353 * Memory request for new introduced data
@@ -313,13 +364,8 @@ int32_t WelsInitMemory (PWelsDecoderContext pCtx) { · 10 unmodified lines
313364 if (MemInitNalList (&pCtx->pAccessUnitList, MAX_NAL_UNIT_NUM_IN_AU) != 0)
314365 return ERR_INFO_OUT_OF_MEMORY;
315366
316− if ((pCtx->sRawData.pHead = static_cast<uint8_t*> (WelsMalloc (BS_BUFFER_SIZE,
317− "pCtx->sRawData->pHead"))) == NULL) {
367+ if (InitBsBuffer (pCtx) != 0)
318368 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;
323369
324370 pCtx->uiTargetDqId = (uint8_t) - 1;
325371 pCtx->bEndOfStreamFlag = false;
codec/decoder/plus/src/welsDecoderExt.cpp+2−5
@@ -49,6 +49,7 @@ · 48 unmodified lines
4949
5050 //#include "macros.h"
5151 #include "decoder.h"
52+#include "decoder_core.h"
5253
5354 extern "C" {
5455 #include "decoder_core.h"
@@ -322,11 +323,7 @@ DECODING_STATE CWelsDecoder::DecodeFrame2 (const unsigned char* kpSrc, · 267 unmodified lines
322323 const int kiSrcLen,
323324 unsigned char** ppDst,
324325 SBufferInfo* pDstInfo) {
325− if (kiSrcLen > MAX_ACCESS_UNIT_CAPACITY - MAX_MACROBLOCK_CAPACITY) {//prevent from residual reading overflow
326− m_pDecContext->iErrorCode |= dsOutOfMemory;
327− IWelsTrace::WelsVTrace (m_pTrace, IWelsTrace::WELS_LOG_INFO,
328− "max AU size exceeded. Allowed size = %d, current size = %d",
329− MAX_ACCESS_UNIT_CAPACITY - MAX_MACROBLOCK_CAPACITY, kiSrcLen);
326+ if (CheckBsBuffer (m_pDecContext, kiSrcLen)) {
330327 return dsOutOfMemory;
331328 }
332329 if (kiSrcLen > 0 && kpSrc != NULL) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t