From c08c0f85eb12f45a40f17a60ac33e00312799d18 Mon Sep 17 00:00:00 2001 From: ganyang Date: Wed, 14 May 2014 16:00:36 +0800 Subject: [PATCH] This refine is to remove slice number limitation in the future. The changes contains: 1. add pNalLen in Structure SWelsEncoderOutput to store each nal length 2. rename iNalLengthInByte[MAX_NAL_UNITS_IN_LAYER] to pNalLengthInByte in Structure SLayerBSInfo, the pointer point to pNalLen, like pBSBuf point to pFrameBS. --- codec/api/svc/codec_app_def.h | 2 +- codec/console/enc/src/welsenc.cpp | 2 +- codec/encoder/core/inc/nal_encap.h | 1 + codec/encoder/core/src/encoder_ext.cpp | 51 +++++++++---------- .../core/src/slice_multi_threading.cpp | 6 +-- codec/encoder/plus/src/welsEncoderExt.cpp | 2 +- test/api/decode_encode_test.cpp | 2 +- test/api/encoder_test.cpp | 2 +- 8 files changed, 34 insertions(+), 34 deletions(-) diff --git a/codec/api/svc/codec_app_def.h b/codec/api/svc/codec_app_def.h index b45f21f3..ddf31d51 100644 --- a/codec/api/svc/codec_app_def.h +++ b/codec/api/svc/codec_app_def.h @@ -347,7 +347,7 @@ typedef struct { unsigned char uiLayerType; int iNalCount; // Count number of NAL coded already - int iNalLengthInByte[MAX_NAL_UNITS_IN_LAYER]; // Length of NAL size in byte from 0 to iNalCount-1 + int* pNalLengthInByte; // Length of NAL size in byte from 0 to iNalCount-1 unsigned char* pBsBuf; // Buffer of bitstream contained } SLayerBSInfo, *PLayerBSInfo; diff --git a/codec/console/enc/src/welsenc.cpp b/codec/console/enc/src/welsenc.cpp index 56ae0ea8..95562148 100644 --- a/codec/console/enc/src/welsenc.cpp +++ b/codec/console/enc/src/welsenc.cpp @@ -782,7 +782,7 @@ int ProcessEncoding(ISVCEncoder* pPtrEnc, int argc, char** argv,bool bConfigFile int iLayerSize = 0; int iNalIdx = pLayerBsInfo->iNalCount - 1; do { - iLayerSize += pLayerBsInfo->iNalLengthInByte[iNalIdx]; + iLayerSize += pLayerBsInfo->pNalLengthInByte[iNalIdx]; -- iNalIdx; } while (iNalIdx >= 0); #if defined(COMPARE_DATA) diff --git a/codec/encoder/core/inc/nal_encap.h b/codec/encoder/core/inc/nal_encap.h index 792152cc..ab7e6235 100644 --- a/codec/encoder/core/inc/nal_encap.h +++ b/codec/encoder/core/inc/nal_encap.h @@ -71,6 +71,7 @@ typedef struct TagWelsEncoderOutput { // SWelsNalRaw raw_nals[MAX_DEPENDENCY_LAYER*2+MAX_DEPENDENCY_LAYER*MAX_QUALITY_LEVEL]; // AVC: max up to SPS+PPS+max_slice_idc (2 + 8) for FMO; SWelsNalRaw* sNalList; // nal list, adaptive for AVC/SVC in case single slice, multiple slices or fmo + int32_t* pNalLen; int32_t iCountNals; // count number of NAL in list // SVC: num_sps (MAX_D) + num_pps (MAX_D) + num_vcl (MAX_D * MAX_Q) int32_t iNalIndex; // coding NAL currently, 0 based diff --git a/codec/encoder/core/src/encoder_ext.cpp b/codec/encoder/core/src/encoder_ext.cpp index 82a02f32..b2ff11fa 100644 --- a/codec/encoder/core/src/encoder_ext.cpp +++ b/codec/encoder/core/src/encoder_ext.cpp @@ -1303,6 +1303,8 @@ int32_t RequestMemorySvc (sWelsEncCtx** ppCtx) { (*ppCtx)->pOut->uiSize = iCountBsLen; (*ppCtx)->pOut->sNalList = (SWelsNalRaw*)pMa->WelsMalloc (iCountNals * sizeof (SWelsNalRaw), "pOut->sNalList"); WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut->sNalList), FreeMemorySvc (ppCtx)) + (*ppCtx)->pOut->pNalLen = (int32_t*)pMa->WelsMallocz(iCountNals * sizeof (int32_t), "pOut->pNalLen"); + WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut->pNalLen), FreeMemorySvc(ppCtx)) (*ppCtx)->pOut->iCountNals = iCountNals; (*ppCtx)->pOut->iNalIndex = 0; @@ -1497,6 +1499,11 @@ void FreeMemorySvc (sWelsEncCtx** ppCtx) { pMa->WelsFree (pCtx->pOut->sNalList, "pOut->sNalList"); pCtx->pOut->sNalList = NULL; } + // NALs len + if (NULL != pCtx->pOut->pNalLen) { + pMa->WelsFree (pCtx->pOut->pNalLen, "pOut->pNalLen"); + pCtx->pOut->pNalLen = NULL; + } pMa->WelsFree (pCtx->pOut, "SWelsEncoderOutput"); pCtx->pOut = NULL; } @@ -2733,7 +2740,6 @@ static inline int32_t AddPrefixNal (sWelsEncCtx* pCtx, iPayloadSize = pNalLen[*pNalIdxInLayer]; pCtx->iPosBsBuffer += iPayloadSize; - pLayerBsInfo->iNalLengthInByte[*pNalIdxInLayer] = iPayloadSize; (*pNalIdxInLayer) ++; } else { // No Prefix NAL Unit RBSP syntax here, but need add NAL Unit Header extension @@ -2750,7 +2756,6 @@ static inline int32_t AddPrefixNal (sWelsEncCtx* pCtx, iPayloadSize = pNalLen[*pNalIdxInLayer]; pCtx->iPosBsBuffer += iPayloadSize; - pLayerBsInfo->iNalLengthInByte[*pNalIdxInLayer] = iPayloadSize; (*pNalIdxInLayer) ++; } @@ -2816,13 +2821,13 @@ int32_t ForceCodingIDR (sWelsEncCtx* pCtx) { int32_t WelsEncoderEncodeParameterSets (sWelsEncCtx* pCtx, void* pDst) { SFrameBSInfo* pFbi = (SFrameBSInfo*)pDst; SLayerBSInfo* pLayerBsInfo = &pFbi->sLayerInfo[0]; - int32_t iNalLen[128] = {0}; int32_t iCountNal = 0; pLayerBsInfo->pBsBuf = pCtx->pFrameBs; + pLayerBsInfo->pNalLengthInByte = pCtx->pOut->pNalLen; InitBits (&pCtx->pOut->sBsWrite, pCtx->pOut->pBsBuffer, pCtx->pOut->uiSize); - int32_t iReturn = WelsWriteParameterSets (pCtx, &iNalLen[0], &iCountNal); + int32_t iReturn = WelsWriteParameterSets (pCtx, &pLayerBsInfo->pNalLengthInByte[0], &iCountNal); WELS_VERIFY_RETURN_IFNEQ (iReturn, ENC_RETURN_SUCCESS) pLayerBsInfo->uiPriorityId = 0; @@ -2831,9 +2836,6 @@ int32_t WelsEncoderEncodeParameterSets (sWelsEncCtx* pCtx, void* pDst) { pLayerBsInfo->uiQualityId = 0; pLayerBsInfo->uiLayerType = NON_VIDEO_CODING_LAYER; pLayerBsInfo->iNalCount = iCountNal; - for (int32_t iNalIndex = 0; iNalIndex < iCountNal; ++ iNalIndex) { - pLayerBsInfo->iNalLengthInByte[iNalIndex] = iNalLen[iNalIndex]; - } pCtx->eLastNalPriority = NRI_PRI_HIGHEST; pFbi->iLayerNum = 1; @@ -2865,7 +2867,6 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour int32_t iSpatialNum = 0; // available count number of spatial layers due to frame size changed in this given frame int32_t iSpatialIdx = 0; // iIndex of spatial layers due to frame size changed in this given frame int32_t iFrameSize = 0; - int32_t iNalLen[128] = {0}; int32_t iNalIdxInLayer = 0; int32_t iCountNal = 0; EVideoFrameType eFrameType = videoFrameTypeInvalid; @@ -2909,13 +2910,14 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour pCtx->uiTemporalId = iCurTid; pLayerBsInfo->pBsBuf = pCtx->pFrameBs ; + pLayerBsInfo->pNalLengthInByte = pCtx->pOut->pNalLen; if (eFrameType == videoFrameTypeIDR) { ++ pCtx->sPSOVector.uiIdrPicId; //if ( pSvcParam->bEnableSSEI ) // write parameter sets bitstream here - pCtx->iEncoderError = WelsWriteParameterSets (pCtx, &iNalLen[0], &iCountNal); + pCtx->iEncoderError = WelsWriteParameterSets (pCtx, &pLayerBsInfo->pNalLengthInByte[0], &iCountNal); WELS_VERIFY_RETURN_IFNEQ (pCtx->iEncoderError, ENC_RETURN_SUCCESS) pLayerBsInfo->uiPriorityId = 0; @@ -2924,12 +2926,10 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour pLayerBsInfo->uiQualityId = 0; pLayerBsInfo->uiLayerType = NON_VIDEO_CODING_LAYER; pLayerBsInfo->iNalCount = iCountNal; - for (int32_t iNalIndex = 0; iNalIndex < iCountNal; ++ iNalIndex) { - pLayerBsInfo->iNalLengthInByte[iNalIndex] = iNalLen[iNalIndex]; - } ++ pLayerBsInfo; pLayerBsInfo->pBsBuf = pCtx->pFrameBs + pCtx->iPosBsBuffer; + pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + iCountNal; ++ iLayerNum; } @@ -3050,7 +3050,7 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour int32_t iPayloadSize = 0; if (pCtx->bNeedPrefixNalFlag) { - pCtx->iEncoderError = AddPrefixNal (pCtx, pLayerBsInfo, &iNalLen[0], &iNalIdxInLayer, eNalType, eNalRefIdc, + pCtx->iEncoderError = AddPrefixNal (pCtx, pLayerBsInfo, &pLayerBsInfo->pNalLengthInByte[0], &iNalIdxInLayer, eNalType, eNalRefIdc, iPayloadSize); WELS_VERIFY_RETURN_IFNEQ (pCtx->iEncoderError, ENC_RETURN_SUCCESS) iLayerSize += iPayloadSize; @@ -3067,9 +3067,9 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour &pCtx->pCurDqLayer->sLayerInfo.sNalHeaderExt, pCtx->iFrameBsSize - pCtx->iPosBsBuffer, pCtx->pFrameBs + pCtx->iPosBsBuffer, - &iNalLen[iNalIdxInLayer]); + &pLayerBsInfo->pNalLengthInByte[iNalIdxInLayer]); WELS_VERIFY_RETURN_IFNEQ (pCtx->iEncoderError, ENC_RETURN_SUCCESS) - iSliceSize = iNalLen[iNalIdxInLayer]; + iSliceSize = pLayerBsInfo->pNalLengthInByte[iNalIdxInLayer]; iLayerSize += iSliceSize; pCtx->iPosBsBuffer += iSliceSize; @@ -3078,7 +3078,6 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour pLayerBsInfo->uiTemporalId = iCurTid; pLayerBsInfo->uiQualityId = 0; pLayerBsInfo->uiPriorityId = 0; - pLayerBsInfo->iNalLengthInByte[iNalIdxInLayer] = iSliceSize; pLayerBsInfo->iNalCount = ++ iNalIdxInLayer; } // for dynamic slicing single threading.. @@ -3221,7 +3220,7 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour int32_t iSliceSize = 0; int32_t iPayloadSize = 0; if (bNeedPrefix) { - pCtx->iEncoderError = AddPrefixNal (pCtx, pLayerBsInfo, &iNalLen[0], &iNalIdxInLayer, eNalType, eNalRefIdc, + pCtx->iEncoderError = AddPrefixNal (pCtx, pLayerBsInfo, &pLayerBsInfo->pNalLengthInByte[0], &iNalIdxInLayer, eNalType, eNalRefIdc, iPayloadSize); WELS_VERIFY_RETURN_IFNEQ (pCtx->iEncoderError, ENC_RETURN_SUCCESS) iLayerSize += iPayloadSize; @@ -3236,13 +3235,12 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour pCtx->iEncoderError = WelsEncodeNal (&pCtx->pOut->sNalList[pCtx->pOut->iNalIndex - 1], &pCtx->pCurDqLayer->sLayerInfo.sNalHeaderExt, pCtx->iFrameBsSize - pCtx->iPosBsBuffer, - pCtx->pFrameBs + pCtx->iPosBsBuffer, &iNalLen[iNalIdxInLayer]); + pCtx->pFrameBs + pCtx->iPosBsBuffer, &pLayerBsInfo->pNalLengthInByte[iNalIdxInLayer]); WELS_VERIFY_RETURN_IFNEQ (pCtx->iEncoderError, ENC_RETURN_SUCCESS) - iSliceSize = iNalLen[iNalIdxInLayer]; + iSliceSize = pLayerBsInfo->pNalLengthInByte[iNalIdxInLayer]; pCtx->iPosBsBuffer += iSliceSize; iLayerSize += iSliceSize; - pLayerBsInfo->iNalLengthInByte[iNalIdxInLayer] = iSliceSize; #if defined(SLICE_INFO_OUTPUT) fprintf (stderr, @@ -3374,10 +3372,12 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour } #endif//STAT_OUTPUT + iCountNal = pLayerBsInfo->iNalCount; ++ iLayerNum; ++ pLayerBsInfo; pLayerBsInfo->pBsBuf = pCtx->pFrameBs + pCtx->iPosBsBuffer; + pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + iCountNal; if (pSvcParam->iPaddingFlag && pCtx->pWelsSvcRc[pCtx->uiDependencyId].iPaddingSize > 0) { int32_t iPaddingNalSize = 0; @@ -3401,9 +3401,10 @@ int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSour pLayerBsInfo->uiQualityId = 0; pLayerBsInfo->uiLayerType = NON_VIDEO_CODING_LAYER; pLayerBsInfo->iNalCount = 1; - pLayerBsInfo->iNalLengthInByte[0] = iPaddingNalSize; + pLayerBsInfo->pNalLengthInByte[0] = iPaddingNalSize; ++ pLayerBsInfo; pLayerBsInfo->pBsBuf = pCtx->pFrameBs + pCtx->iPosBsBuffer; + pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + 1; ++ iLayerNum; } @@ -3657,7 +3658,6 @@ int32_t WelsCodeOnePicPartition (sWelsEncCtx* pCtx, SDqLayer* pCurLayer = pCtx->pCurDqLayer; SSliceCtx* pSliceCtx = pCurLayer->pSliceEncCtx; - int32_t iNalLen[MAX_NAL_UNITS_IN_LAYER] = {0}; int32_t iNalIdxInLayer = *pNalIdxInLayer; int32_t iSliceIdx = iStartSliceIdx; const int32_t kiSliceStep = pCtx->iActiveThreadsNum; @@ -3688,7 +3688,7 @@ int32_t WelsCodeOnePicPartition (sWelsEncCtx* pCtx, } if (kbNeedPrefix) { - iReturn = AddPrefixNal (pCtx, pLayerBsInfo, &iNalLen[0], &iNalIdxInLayer, keNalType, keNalRefIdc, iPayloadSize); + iReturn = AddPrefixNal (pCtx, pLayerBsInfo, &pLayerBsInfo->pNalLengthInByte[0], &iNalIdxInLayer, keNalType, keNalRefIdc, iPayloadSize); WELS_VERIFY_RETURN_IFNEQ (iReturn, ENC_RETURN_SUCCESS) iPartitionBsSize += iPayloadSize; } @@ -3702,13 +3702,12 @@ int32_t WelsCodeOnePicPartition (sWelsEncCtx* pCtx, &pCtx->pCurDqLayer->sLayerInfo.sNalHeaderExt, pCtx->iFrameBsSize - pCtx->iPosBsBuffer, pCtx->pFrameBs + pCtx->iPosBsBuffer, - &iNalLen[iNalIdxInLayer]); + &pLayerBsInfo->pNalLengthInByte[iNalIdxInLayer]); WELS_VERIFY_RETURN_IFNEQ (iReturn, ENC_RETURN_SUCCESS) - iSliceSize = iNalLen[iNalIdxInLayer]; + iSliceSize = pLayerBsInfo->pNalLengthInByte[iNalIdxInLayer]; pCtx->iPosBsBuffer += iSliceSize; iPartitionBsSize += iSliceSize; - pLayerBsInfo->iNalLengthInByte[iNalIdxInLayer] = iSliceSize; #if defined(SLICE_INFO_OUTPUT) fprintf (stderr, diff --git a/codec/encoder/core/src/slice_multi_threading.cpp b/codec/encoder/core/src/slice_multi_threading.cpp index 2340a2b9..de9fb737 100644 --- a/codec/encoder/core/src/slice_multi_threading.cpp +++ b/codec/encoder/core/src/slice_multi_threading.cpp @@ -548,7 +548,7 @@ int32_t AppendSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, const int32 iLayerSize += pSliceBs->uiBsPos; while (iNalIdx < iCountNal) { - pLbi->iNalLengthInByte[iNalIdxBase + iNalIdx] = pSliceBs->iNalLen[iNalIdx]; + pLbi->pNalLengthInByte[iNalIdxBase + iNalIdx] = pSliceBs->iNalLen[iNalIdx]; ++ iNalIdx; } pLbi->iNalCount += iCountNal; @@ -580,7 +580,7 @@ int32_t AppendSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, const int32 iLayerSize += pSliceBs->uiBsPos; while (iNalIdx < iCountNal) { - pLbi->iNalLengthInByte[iNalIdxBase + iNalIdx] = pSliceBs->iNalLen[iNalIdx]; + pLbi->pNalLengthInByte[iNalIdxBase + iNalIdx] = pSliceBs->iNalLen[iNalIdx]; ++ iNalIdx; } pLbi->iNalCount += iCountNal; @@ -621,7 +621,7 @@ int32_t WriteSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, uint8_t* pFr WELS_VERIFY_RETURN_IFNEQ (iReturn, ENC_RETURN_SUCCESS) iSliceSize += iNalSize; pDst += iNalSize; - pLbi->iNalLengthInByte[iNalBase + iNalIdx] = iNalSize; + pLbi->pNalLengthInByte[iNalBase + iNalIdx] = iNalSize; ++ iNalIdx; } diff --git a/codec/encoder/plus/src/welsEncoderExt.cpp b/codec/encoder/plus/src/welsEncoderExt.cpp index 01a6440c..66f0be33 100644 --- a/codec/encoder/plus/src/welsEncoderExt.cpp +++ b/codec/encoder/plus/src/welsEncoderExt.cpp @@ -479,7 +479,7 @@ int CWelsH264SVCEncoder::EncodeFrameInternal (const SSourcePicture* pSrcPic, SF iCurLayerBits = 0; for (j = 0; j < pLayer->iNalCount; j++) { - iCurLayerBits += pLayer->iNalLengthInByte[j]; + iCurLayerBits += pLayer->pNalLengthInByte[j]; } total_bits += iCurLayerBits; if (m_pFileBs != NULL) diff --git a/test/api/decode_encode_test.cpp b/test/api/decode_encode_test.cpp index d835f14f..192d5fa9 100644 --- a/test/api/decode_encode_test.cpp +++ b/test/api/decode_encode_test.cpp @@ -11,7 +11,7 @@ static void UpdateHashFromFrame(const SFrameBSInfo& info, SHA1Context* ctx) { const SLayerBSInfo& layerInfo = info.sLayerInfo[i]; int layerSize = 0; for (int j = 0; j < layerInfo.iNalCount; ++j) { - layerSize += layerInfo.iNalLengthInByte[j]; + layerSize += layerInfo.pNalLengthInByte[j]; } SHA1Input(ctx, layerInfo.pBsBuf, layerSize); } diff --git a/test/api/encoder_test.cpp b/test/api/encoder_test.cpp index fdcba008..dbc197a6 100644 --- a/test/api/encoder_test.cpp +++ b/test/api/encoder_test.cpp @@ -7,7 +7,7 @@ static void UpdateHashFromFrame(const SFrameBSInfo& info, SHA1Context* ctx) { const SLayerBSInfo& layerInfo = info.sLayerInfo[i]; int layerSize = 0; for (int j = 0; j < layerInfo.iNalCount; ++j) { - layerSize += layerInfo.iNalLengthInByte[j]; + layerSize += layerInfo.pNalLengthInByte[j]; } SHA1Input(ctx, layerInfo.pBsBuf, layerSize); }