History

Squashed merge from writenal_refactor2

sijchen committed 12 years ago

Commit e45e8591 parent a921bc616 files · +431 −263Browse files

Showing the changes to codec/encoder/core/src/slice_multi_threading.cpp (1 of 16 files). Show all

Unified · Split

codec/encoder/core/src/slice_multi_threading.cpp+96−25
@@ -347,6 +347,7 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 346 unmodified lines
347347 int32_t iIdx = 0;
348348 int32_t iSliceBsBufferSize = 0;
349349 int16_t iMaxSliceNum = 1;
350+ int32_t iReturn = ENC_RETURN_SUCCESS;
350351
351352 if (NULL == ppCtx || NULL == pCodingParam || NULL == *ppCtx || iCountBsLen <= 0)
352353 return 1;
@@ -492,8 +493,6 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 139 unmodified lines
492493 WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pCountBsSizeInPartition), FreeMemorySvc (ppCtx))
493494 #endif//PACKING_ONE_SLICE_PER_LAYER
494495
495− WelsMutexInit (&pSmt->mutexSliceNumUpdate);
496−
497496 (*ppCtx)->pSliceBs = (SWelsSliceBs*)pMa->WelsMalloc (sizeof (SWelsSliceBs) * iMaxSliceNum, "pSliceBs");
498497 WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pSliceBs), FreeMemorySvc (ppCtx))
499498
@@ -502,7 +501,7 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 2 unmodified lines
502501 iSliceBsBufferSize = iTargetSpatialBsSize;
503502 iIdx = 0;
504503 while (iIdx < iMaxSliceNum) {
505− pSliceB->pBsBuffer = (uint8_t*)pMa->WelsMalloc (iSliceBsBufferSize, "pSliceB->pBsBuffer");
504+ pSliceB->pBsBuffer = (uint8_t*)pMa->WelsMalloc (iSliceBsBufferSize, "pSliceB->pBsBuffer");
506505
507506 WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSliceB->pBsBuffer), FreeMemorySvc (ppCtx))
508507 pSliceB->uiSize = iSliceBsBufferSize;
@@ -519,6 +518,12 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 10 unmodified lines
519518 ++ iIdx;
520519 }
521520
521+ iReturn = WelsMutexInit (&pSmt->mutexSliceNumUpdate);
522+ WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx))
523+
524+ iReturn = WelsMutexInit (&(*ppCtx)->mutexEncoderError);
525+ WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx))
526+
522527 #if defined(ENABLE_TRACE_MT)
523528 WelsLog ((*ppCtx), WELS_LOG_INFO, "RequestMtResource(), iThreadNum=%d, iCountSliceNum= %d\n", pPara->iCountThreadsNum,
524529 iMaxSliceNum);
@@ -610,7 +615,9 @@ void ReleaseMtResource (sWelsEncCtx** ppCtx) { · 85 unmodified lines
610615 pSmt->pCountBsSizeInPartition = NULL;
611616 }
612617 #endif//PACKING_ONE_SLICE_PER_LAYER
618+
613619 WelsMutexDestroy (&pSmt->mutexSliceNumUpdate);
620+ WelsMutexDestroy (&((*ppCtx)->mutexEncoderError));
614621
615622 if (pSmt->pThreadPEncCtx != NULL) {
616623 pMa->WelsFree (pSmt->pThreadPEncCtx, "pThreadPEncCtx");
@@ -766,25 +773,32 @@ int32_t AppendSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, const int32 · 149 unmodified lines
766773 return iLayerSize;
767774 }
768775
769−int32_t WriteSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, uint8_t* pFrameBsBuffer, const int32_t iSliceIdx) {
776+int32_t WriteSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, uint8_t* pFrameBsBuffer, const int32_t iSliceIdx, int32_t& iSliceSize) {
770777 SWelsSliceBs* pSliceBs = &pCtx->pSliceBs[iSliceIdx];
771778 SNalUnitHeaderExt* pNalHdrExt = &pCtx->pCurDqLayer->sLayerInfo.sNalHeaderExt;
772779 uint8_t* pDst = pFrameBsBuffer;
773780 int32_t pNalLen[2];
774− int32_t iSliceSize = 0;
775781 const int32_t kiNalCnt = pSliceBs->iNalIndex;
776782 int32_t iNalIdx = 0;
783+ int32_t iNalSize = 0;
777784 #if !defined(PACKING_ONE_SLICE_PER_LAYER)
778785 const int32_t iFirstSlice = (iSliceIdx == 0);
779786 int32_t iNalBase = iFirstSlice ? 0 : pLbi->iNalCount;
780787 #else
781788 int32_t iNalBase = 0;
782789 #endif//!PACKING_ONE_SLICE_PER_LAYER
790+ int32_t iReturn = ENC_RETURN_SUCCESS;
791+ const int32_t kiWrittenLength = pCtx->iPosBsBuffer;
792+ iSliceSize = 0;
783793
784794 while (iNalIdx < kiNalCnt) {
785− iSliceSize += WelsEncodeNalExt (&pSliceBs->sNalList[iNalIdx], pNalHdrExt, pDst, &pNalLen[iNalIdx]);
786− pDst += pNalLen[iNalIdx];
787− pLbi->iNalLengthInByte[iNalBase + iNalIdx] = pNalLen[iNalIdx];
795+ iNalSize = 0;
796+ iReturn = WelsEncodeNal (&pSliceBs->sNalList[iNalIdx], pNalHdrExt, pCtx->iFrameBsSize-kiWrittenLength-iSliceSize, pDst, &iNalSize);
797+ WELS_VERIFY_RETURN_IFNEQ(iReturn, ENC_RETURN_SUCCESS)
798+ pNalLen[iNalIdx] = iNalSize;
799+ iSliceSize += iNalSize;
800+ pDst += iNalSize;
801+ pLbi->iNalLengthInByte[iNalBase + iNalIdx] = iNalSize;
788802
789803 ++ iNalIdx;
790804 }
@@ -811,31 +825,37 @@ int32_t WriteSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, uint8_t* pFr · 20 unmodified lines
811825 pLbi->iNalCount = kiNalCnt;
812826 #endif//PACKING_ONE_SLICE_PER_LAYER
813827
814− return iSliceSize;
828+ return ENC_RETURN_SUCCESS;
815829 }
816830
817−int32_t WriteSliceBs (sWelsEncCtx* pCtx, uint8_t* pSliceBsBuf, const int32_t iSliceIdx) {
831+int32_t WriteSliceBs (sWelsEncCtx* pCtx, uint8_t* pSliceBsBuf, const int32_t iSliceIdx, int32_t& iSliceSize) {
818832 SWelsSliceBs* pSliceBs = &pCtx->pSliceBs[iSliceIdx];
819833 SNalUnitHeaderExt* pNalHdrExt = &pCtx->pCurDqLayer->sLayerInfo.sNalHeaderExt;
820834 uint8_t* pDst = pSliceBsBuf;
821835 int32_t* pNalLen = &pSliceBs->iNalLen[0];
822− int32_t iSliceSize = 0;
823836 const int32_t kiNalCnt = pSliceBs->iNalIndex;
824837 int32_t iNalIdx = 0;
838+ int32_t iNalSize = 0;
839+ int32_t iReturn = ENC_RETURN_SUCCESS;
840+ const int32_t kiWrittenLength = pSliceBs->sBsWrite.pBufPtr - pSliceBs->sBsWrite.pBuf;
825841
842+ iSliceSize = 0;
826843 assert (kiNalCnt <= 2);
827844 if (kiNalCnt > 2)
828845 return 0;
829846
830847 while (iNalIdx < kiNalCnt) {
831− iSliceSize += WelsEncodeNalExt (&pSliceBs->sNalList[iNalIdx], pNalHdrExt, pDst, &pNalLen[iNalIdx]);
832− pDst += pNalLen[iNalIdx];
833−
848+ iNalSize = 0;
849+ iReturn = WelsEncodeNal (&pSliceBs->sNalList[iNalIdx], pNalHdrExt, pSliceBs->uiSize-kiWrittenLength-iSliceSize, pDst, &iNalSize);
850+ WELS_VERIFY_RETURN_IFNEQ(iReturn, ENC_RETURN_SUCCESS)
851+ pNalLen[iNalIdx] = iNalSize;
852+ iSliceSize += iNalSize;
853+ pDst += iNalSize;
834854 ++ iNalIdx;
835855 }
836856 pSliceBs->uiBsPos = iSliceSize;
837857
838− return iSliceSize;
858+ return iReturn;
839859 }
840860
841861 #if defined(DYNAMIC_SLICE_ASSIGN) && defined(TRY_SLICING_BALANCE)
@@ -901,6 +921,7 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 59 unmodified lines
901921 bool bNeedPrefix = false;
902922 EWelsNalUnitType eNalType = NAL_UNIT_UNSPEC_0;
903923 EWelsNalRefIdc eNalRefIdc = NRI_PRI_LOWEST;
924+ int32_t iReturn = ENC_RETURN_SUCCESS;
904925
905926 if (NULL == pPrivateData)
906927 WELS_THREAD_ROUTINE_RETURN (1);
@@ -985,25 +1006,47 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 78 unmodified lines
9851006
9861007 WelsLoadNalForSlice (pSliceBs, eNalType, eNalRefIdc);
9871008
988− WelsCodeOneSlice (pEncPEncCtx, iSliceIdx, eNalType);
1009+ iReturn = WelsCodeOneSlice (pEncPEncCtx, iSliceIdx, eNalType);
1010+ if (ENC_RETURN_SUCCESS!=iReturn) {
1011+ uiThrdRet = iReturn;
1012+ break;
1013+ }
9891014
9901015 WelsUnloadNalForSlice (pSliceBs);
9911016
9921017 #if !defined(PACKING_ONE_SLICE_PER_LAYER)
9931018 if (0 == iSliceIdx) {
9941019 pLbi->pBsBuf = pEncPEncCtx->pFrameBs + pEncPEncCtx->iPosBsBuffer;
995− iSliceSize = WriteSliceToFrameBs (pEncPEncCtx, pLbi, pLbi->pBsBuf, iSliceIdx);
1020+ iReturn = WriteSliceToFrameBs (pEncPEncCtx, pLbi, pLbi->pBsBuf, iSliceIdx, iSliceSize);
1021+ if (ENC_RETURN_SUCCESS!=iReturn) {
1022+ uiThrdRet = iReturn;
1023+ break;
1024+ }
9961025 pEncPEncCtx->iPosBsBuffer += iSliceSize;
9971026 } else
998− iSliceSize = WriteSliceBs (pEncPEncCtx, pSliceBs->pBs, iSliceIdx);
1027+ {
1028+ iReturn = WriteSliceBs (pEncPEncCtx, pSliceBs->pBs, iSliceIdx, iSliceSize);
1029+ if (ENC_RETURN_SUCCESS!=iReturn) {
1030+ uiThrdRet = iReturn;
1031+ break;
1032+ }
1033+ }
9991034 #else// PACKING_ONE_SLICE_PER_LAYER
10001035 if (0 == iSliceIdx) {
10011036 pLbi->pBsBuf = pEncPEncCtx->pFrameBs + pEncPEncCtx->iPosBsBuffer;
1002− iSliceSize = WriteSliceToFrameBs (pEncPEncCtx, pLbi, pLbi->pBsBuf, iSliceIdx);
1037+ iReturn = WriteSliceToFrameBs (pEncPEncCtx, pLbi, pLbi->pBsBuf, iSliceIdx, &iSliceSize);
1038+ if (ENC_RETURN_SUCCESS!=iReturn) {
1039+ uiThrdRet = iReturn;
1040+ break;
1041+ }
10031042 pEncPEncCtx->iPosBsBuffer += iSliceSize;
10041043 } else {
10051044 pLbi->pBsBuf = pSliceBs->bs + pSliceBs->uiBsPos;
1006− iSliceSize = WriteSliceToFrameBs (pEncPEncCtx, pLbi, pLbi->pBsBuf, iSliceIdx);
1045+ iReturn = WriteSliceToFrameBs (pEncPEncCtx, pLbi, pLbi->pBsBuf, iSliceIdx, &iSliceSize);
1046+ if (ENC_RETURN_SUCCESS!=iReturn) {
1047+ uiThrdRet = iReturn;
1048+ break;
1049+ }
10071050 pSliceBs->uiBsPos += iSliceSize;
10081051 }
10091052 #endif//!PACKING_ONE_SLICE_PER_LAYER
@@ -1101,7 +1144,11 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 91 unmodified lines
11011144
11021145 WelsLoadNalForSlice (pSliceBs, eNalType, eNalRefIdc);
11031146
1104− WelsCodeOneSlice (pEncPEncCtx, iSliceIdx, eNalType);
1147+ iReturn = WelsCodeOneSlice (pEncPEncCtx, iSliceIdx, eNalType);
1148+ if (ENC_RETURN_SUCCESS!=iReturn) {
1149+ uiThrdRet = iReturn;
1150+ break;
1151+ }
11051152
11061153 WelsUnloadNalForSlice (pSliceBs);
11071154
@@ -1109,20 +1156,38 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 1 unmodified line
11091156 if (0 == kiPartitionId) {
11101157 if (0 == iSliceIdx)
11111158 pLbi->pBsBuf = pEncPEncCtx->pFrameBs + pEncPEncCtx->iPosBsBuffer;
1112− iSliceSize = WriteSliceToFrameBs (pEncPEncCtx, pLbi, pEncPEncCtx->pFrameBs + pEncPEncCtx->iPosBsBuffer, iSliceIdx);
1159+ iReturn = WriteSliceToFrameBs (pEncPEncCtx, pLbi, pEncPEncCtx->pFrameBs + pEncPEncCtx->iPosBsBuffer, iSliceIdx, iSliceSize);
1160+ if (ENC_RETURN_SUCCESS!=iReturn) {
1161+ uiThrdRet = iReturn;
1162+ break;
1163+ }
11131164 pEncPEncCtx->iPosBsBuffer += iSliceSize;
11141165 } else
1115− iSliceSize = WriteSliceBs (pEncPEncCtx, pSliceBs->pBs, iSliceIdx);
1166+ {
1167+ iSliceSize = WriteSliceBs (pEncPEncCtx, pSliceBs->pBs, iSliceIdx, iSliceSize);
1168+ if (ENC_RETURN_SUCCESS!=iReturn) {
1169+ uiThrdRet = iReturn;
1170+ break;
1171+ }
1172+ }
11161173 #else// PACKING_ONE_SLICE_PER_LAYER
11171174 pLbiPacking = pLbi + (iSliceIdx - kiPartitionId);
11181175
11191176 if (0 == kiPartitionId) {
11201177 pLbiPacking->pBsBuf = pEncPEncCtx->pFrameBs + pEncPEncCtx->iPosBsBuffer;
1121− iSliceSize = WriteSliceToFrameBs (pEncPEncCtx, pLbiPacking, pLbiPacking->pBsBuf, iSliceIdx);
1178+ iReturn = WriteSliceToFrameBs (pEncPEncCtx, pLbiPacking, pLbiPacking->pBsBuf, iSliceIdx, iSliceSize);
1179+ if (ENC_RETURN_SUCCESS!=iReturn) {
1180+ uiThrdRet = iReturn;
1181+ break;
1182+ }
11221183 pEncPEncCtx->iPosBsBuffer += iSliceSize;
11231184 } else {
11241185 pLbiPacking->pBsBuf = pSliceBs->bs + pSliceBs->uiBsPos;
1125− iSliceSize = WriteSliceToFrameBs (pEncPEncCtx, pLbiPacking, pLbiPacking->pBsBuf, iSliceIdx);
1186+ iReturn = WriteSliceToFrameBs (pEncPEncCtx, pLbiPacking, pLbiPacking->pBsBuf, iSliceIdx, iSliceSize);
1187+ if (ENC_RETURN_SUCCESS!=iReturn) {
1188+ uiThrdRet = iReturn;
1189+ break;
1190+ }
11261191 pSliceBs->uiBsPos += iSliceSize;
11271192 }
11281193 pEncPEncCtx->pSliceThreading->pCountBsSizeInPartition[kiPartitionId] += iSliceSize;
@@ -1197,6 +1262,11 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 68 unmodified lines
11971262 WelsEventSignal (&pEncPEncCtx->pSliceThreading->pFinSliceCodingEvent[iEventIdx]); // notify to mother encoding threading
11981263 #endif//WIN32
11991264
1265+ //sync multi-threading error
1266+ WelsMutexLock (&pEncPEncCtx->mutexEncoderError);
1267+ if (uiThrdRet) pEncPEncCtx->iEncoderError |= uiThrdRet;
1268+ WelsMutexUnlock (&pEncPEncCtx->mutexEncoderError);
1269+
12001270 WELS_THREAD_ROUTINE_RETURN (uiThrdRet);
12011271 }
12021272
@@ -1235,6 +1305,7 @@ int32_t CreateSliceThreads (sWelsEncCtx* pCtx) { · 32 unmodified lines
12351305 &pCtx->pSliceThreading->pThreadPEncCtx[iIdx], 0);
12361306 #endif//__GNUC__
12371307 #endif//#if defined(DYNAMIC_SLICE_ASSIGN) && defined(TRY_SLICING_BALANCE)
1308+
12381309 ++ iIdx;
12391310 }
12401311 #if defined(ENABLE_TRACE_MT)

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