Showing the changes to codec/encoder/core/src/slice_multi_threading.cpp (1 of 16 files). Show all
Unified · Split
| @@ -347,6 +347,7 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 346 unmodified lines | ||
| 347 | 347 | int32_t iIdx = 0; |
| 348 | 348 | int32_t iSliceBsBufferSize = 0; |
| 349 | 349 | int16_t iMaxSliceNum = 1; |
| 350 | + int32_t iReturn = ENC_RETURN_SUCCESS; | |
| 350 | 351 | |
| 351 | 352 | if (NULL == ppCtx || NULL == pCodingParam || NULL == *ppCtx || iCountBsLen <= 0) |
| 352 | 353 | return 1; |
| @@ -492,8 +493,6 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 139 unmodified lines | ||
| 492 | 493 | WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pCountBsSizeInPartition), FreeMemorySvc (ppCtx)) |
| 493 | 494 | #endif//PACKING_ONE_SLICE_PER_LAYER |
| 494 | 495 | |
| 495 | − WelsMutexInit (&pSmt->mutexSliceNumUpdate); | |
| 496 | − | |
| 497 | 496 | (*ppCtx)->pSliceBs = (SWelsSliceBs*)pMa->WelsMalloc (sizeof (SWelsSliceBs) * iMaxSliceNum, "pSliceBs"); |
| 498 | 497 | WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pSliceBs), FreeMemorySvc (ppCtx)) |
| 499 | 498 | |
| @@ -502,7 +501,7 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 2 unmodified lines | ||
| 502 | 501 | iSliceBsBufferSize = iTargetSpatialBsSize; |
| 503 | 502 | iIdx = 0; |
| 504 | 503 | while (iIdx < iMaxSliceNum) { |
| 505 | − pSliceB->pBsBuffer = (uint8_t*)pMa->WelsMalloc (iSliceBsBufferSize, "pSliceB->pBsBuffer"); | |
| 504 | + pSliceB->pBsBuffer = (uint8_t*)pMa->WelsMalloc (iSliceBsBufferSize, "pSliceB->pBsBuffer"); | |
| 506 | 505 | |
| 507 | 506 | WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSliceB->pBsBuffer), FreeMemorySvc (ppCtx)) |
| 508 | 507 | pSliceB->uiSize = iSliceBsBufferSize; |
| @@ -519,6 +518,12 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 10 unmodified lines | ||
| 519 | 518 | ++ iIdx; |
| 520 | 519 | } |
| 521 | 520 | |
| 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 | + | |
| 522 | 527 | #if defined(ENABLE_TRACE_MT) |
| 523 | 528 | WelsLog ((*ppCtx), WELS_LOG_INFO, "RequestMtResource(), iThreadNum=%d, iCountSliceNum= %d\n", pPara->iCountThreadsNum, |
| 524 | 529 | iMaxSliceNum); |
| @@ -610,7 +615,9 @@ void ReleaseMtResource (sWelsEncCtx** ppCtx) { · 85 unmodified lines | ||
| 610 | 615 | pSmt->pCountBsSizeInPartition = NULL; |
| 611 | 616 | } |
| 612 | 617 | #endif//PACKING_ONE_SLICE_PER_LAYER |
| 618 | + | |
| 613 | 619 | WelsMutexDestroy (&pSmt->mutexSliceNumUpdate); |
| 620 | + WelsMutexDestroy (&((*ppCtx)->mutexEncoderError)); | |
| 614 | 621 | |
| 615 | 622 | if (pSmt->pThreadPEncCtx != NULL) { |
| 616 | 623 | pMa->WelsFree (pSmt->pThreadPEncCtx, "pThreadPEncCtx"); |
| @@ -766,25 +773,32 @@ int32_t AppendSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, const int32 · 149 unmodified lines | ||
| 766 | 773 | return iLayerSize; |
| 767 | 774 | } |
| 768 | 775 | |
| 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) { | |
| 770 | 777 | SWelsSliceBs* pSliceBs = &pCtx->pSliceBs[iSliceIdx]; |
| 771 | 778 | SNalUnitHeaderExt* pNalHdrExt = &pCtx->pCurDqLayer->sLayerInfo.sNalHeaderExt; |
| 772 | 779 | uint8_t* pDst = pFrameBsBuffer; |
| 773 | 780 | int32_t pNalLen[2]; |
| 774 | − int32_t iSliceSize = 0; | |
| 775 | 781 | const int32_t kiNalCnt = pSliceBs->iNalIndex; |
| 776 | 782 | int32_t iNalIdx = 0; |
| 783 | + int32_t iNalSize = 0; | |
| 777 | 784 | #if !defined(PACKING_ONE_SLICE_PER_LAYER) |
| 778 | 785 | const int32_t iFirstSlice = (iSliceIdx == 0); |
| 779 | 786 | int32_t iNalBase = iFirstSlice ? 0 : pLbi->iNalCount; |
| 780 | 787 | #else |
| 781 | 788 | int32_t iNalBase = 0; |
| 782 | 789 | #endif//!PACKING_ONE_SLICE_PER_LAYER |
| 790 | + int32_t iReturn = ENC_RETURN_SUCCESS; | |
| 791 | + const int32_t kiWrittenLength = pCtx->iPosBsBuffer; | |
| 792 | + iSliceSize = 0; | |
| 783 | 793 | |
| 784 | 794 | 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; | |
| 788 | 802 | |
| 789 | 803 | ++ iNalIdx; |
| 790 | 804 | } |
| @@ -811,31 +825,37 @@ int32_t WriteSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, uint8_t* pFr · 20 unmodified lines | ||
| 811 | 825 | pLbi->iNalCount = kiNalCnt; |
| 812 | 826 | #endif//PACKING_ONE_SLICE_PER_LAYER |
| 813 | 827 | |
| 814 | − return iSliceSize; | |
| 828 | + return ENC_RETURN_SUCCESS; | |
| 815 | 829 | } |
| 816 | 830 | |
| 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) { | |
| 818 | 832 | SWelsSliceBs* pSliceBs = &pCtx->pSliceBs[iSliceIdx]; |
| 819 | 833 | SNalUnitHeaderExt* pNalHdrExt = &pCtx->pCurDqLayer->sLayerInfo.sNalHeaderExt; |
| 820 | 834 | uint8_t* pDst = pSliceBsBuf; |
| 821 | 835 | int32_t* pNalLen = &pSliceBs->iNalLen[0]; |
| 822 | − int32_t iSliceSize = 0; | |
| 823 | 836 | const int32_t kiNalCnt = pSliceBs->iNalIndex; |
| 824 | 837 | 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; | |
| 825 | 841 | |
| 842 | + iSliceSize = 0; | |
| 826 | 843 | assert (kiNalCnt <= 2); |
| 827 | 844 | if (kiNalCnt > 2) |
| 828 | 845 | return 0; |
| 829 | 846 | |
| 830 | 847 | 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; | |
| 834 | 854 | ++ iNalIdx; |
| 835 | 855 | } |
| 836 | 856 | pSliceBs->uiBsPos = iSliceSize; |
| 837 | 857 | |
| 838 | − return iSliceSize; | |
| 858 | + return iReturn; | |
| 839 | 859 | } |
| 840 | 860 | |
| 841 | 861 | #if defined(DYNAMIC_SLICE_ASSIGN) && defined(TRY_SLICING_BALANCE) |
| @@ -901,6 +921,7 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 59 unmodified lines | ||
| 901 | 921 | bool bNeedPrefix = false; |
| 902 | 922 | EWelsNalUnitType eNalType = NAL_UNIT_UNSPEC_0; |
| 903 | 923 | EWelsNalRefIdc eNalRefIdc = NRI_PRI_LOWEST; |
| 924 | + int32_t iReturn = ENC_RETURN_SUCCESS; | |
| 904 | 925 | |
| 905 | 926 | if (NULL == pPrivateData) |
| 906 | 927 | WELS_THREAD_ROUTINE_RETURN (1); |
| @@ -985,25 +1006,47 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 78 unmodified lines | ||
| 985 | 1006 | |
| 986 | 1007 | WelsLoadNalForSlice (pSliceBs, eNalType, eNalRefIdc); |
| 987 | 1008 | |
| 988 | − WelsCodeOneSlice (pEncPEncCtx, iSliceIdx, eNalType); | |
| 1009 | + iReturn = WelsCodeOneSlice (pEncPEncCtx, iSliceIdx, eNalType); | |
| 1010 | + if (ENC_RETURN_SUCCESS!=iReturn) { | |
| 1011 | + uiThrdRet = iReturn; | |
| 1012 | + break; | |
| 1013 | + } | |
| 989 | 1014 | |
| 990 | 1015 | WelsUnloadNalForSlice (pSliceBs); |
| 991 | 1016 | |
| 992 | 1017 | #if !defined(PACKING_ONE_SLICE_PER_LAYER) |
| 993 | 1018 | if (0 == iSliceIdx) { |
| 994 | 1019 | 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 | + } | |
| 996 | 1025 | pEncPEncCtx->iPosBsBuffer += iSliceSize; |
| 997 | 1026 | } 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 | + } | |
| 999 | 1034 | #else// PACKING_ONE_SLICE_PER_LAYER |
| 1000 | 1035 | if (0 == iSliceIdx) { |
| 1001 | 1036 | 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 | + } | |
| 1003 | 1042 | pEncPEncCtx->iPosBsBuffer += iSliceSize; |
| 1004 | 1043 | } else { |
| 1005 | 1044 | 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 | + } | |
| 1007 | 1050 | pSliceBs->uiBsPos += iSliceSize; |
| 1008 | 1051 | } |
| 1009 | 1052 | #endif//!PACKING_ONE_SLICE_PER_LAYER |
| @@ -1101,7 +1144,11 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 91 unmodified lines | ||
| 1101 | 1144 | |
| 1102 | 1145 | WelsLoadNalForSlice (pSliceBs, eNalType, eNalRefIdc); |
| 1103 | 1146 | |
| 1104 | − WelsCodeOneSlice (pEncPEncCtx, iSliceIdx, eNalType); | |
| 1147 | + iReturn = WelsCodeOneSlice (pEncPEncCtx, iSliceIdx, eNalType); | |
| 1148 | + if (ENC_RETURN_SUCCESS!=iReturn) { | |
| 1149 | + uiThrdRet = iReturn; | |
| 1150 | + break; | |
| 1151 | + } | |
| 1105 | 1152 | |
| 1106 | 1153 | WelsUnloadNalForSlice (pSliceBs); |
| 1107 | 1154 | |
| @@ -1109,20 +1156,38 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 1 unmodified line | ||
| 1109 | 1156 | if (0 == kiPartitionId) { |
| 1110 | 1157 | if (0 == iSliceIdx) |
| 1111 | 1158 | 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 | + } | |
| 1113 | 1164 | pEncPEncCtx->iPosBsBuffer += iSliceSize; |
| 1114 | 1165 | } 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 | + } | |
| 1116 | 1173 | #else// PACKING_ONE_SLICE_PER_LAYER |
| 1117 | 1174 | pLbiPacking = pLbi + (iSliceIdx - kiPartitionId); |
| 1118 | 1175 | |
| 1119 | 1176 | if (0 == kiPartitionId) { |
| 1120 | 1177 | 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 | + } | |
| 1122 | 1183 | pEncPEncCtx->iPosBsBuffer += iSliceSize; |
| 1123 | 1184 | } else { |
| 1124 | 1185 | 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 | + } | |
| 1126 | 1191 | pSliceBs->uiBsPos += iSliceSize; |
| 1127 | 1192 | } |
| 1128 | 1193 | pEncPEncCtx->pSliceThreading->pCountBsSizeInPartition[kiPartitionId] += iSliceSize; |
| @@ -1197,6 +1262,11 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 68 unmodified lines | ||
| 1197 | 1262 | WelsEventSignal (&pEncPEncCtx->pSliceThreading->pFinSliceCodingEvent[iEventIdx]); // notify to mother encoding threading |
| 1198 | 1263 | #endif//WIN32 |
| 1199 | 1264 | |
| 1265 | + //sync multi-threading error | |
| 1266 | + WelsMutexLock (&pEncPEncCtx->mutexEncoderError); | |
| 1267 | + if (uiThrdRet) pEncPEncCtx->iEncoderError |= uiThrdRet; | |
| 1268 | + WelsMutexUnlock (&pEncPEncCtx->mutexEncoderError); | |
| 1269 | + | |
| 1200 | 1270 | WELS_THREAD_ROUTINE_RETURN (uiThrdRet); |
| 1201 | 1271 | } |
| 1202 | 1272 | |
| @@ -1235,6 +1305,7 @@ int32_t CreateSliceThreads (sWelsEncCtx* pCtx) { · 32 unmodified lines | ||
| 1235 | 1305 | &pCtx->pSliceThreading->pThreadPEncCtx[iIdx], 0); |
| 1236 | 1306 | #endif//__GNUC__ |
| 1237 | 1307 | #endif//#if defined(DYNAMIC_SLICE_ASSIGN) && defined(TRY_SLICING_BALANCE) |
| 1308 | + | |
| 1238 | 1309 | ++ iIdx; |
| 1239 | 1310 | } |
| 1240 | 1311 | #if defined(ENABLE_TRACE_MT) |