Showing the changes to codec/encoder/core/src/svc_encode_slice.cpp (1 of 14 files). Show all
Unified · Split
| @@ -640,9 +640,9 @@ TRY_REENCODING: · 639 unmodified lines | ||
| 640 | 640 | |
| 641 | 641 | if (DynSlcJudgeSliceBoundaryStepBack (pEncCtx, pSlice, pSliceCtx, pCurMb, &sDss)) { //islice |
| 642 | 642 | pEncCtx->pFuncList->pfStashPopMBStatus (&sDss, pSlice); |
| 643 | − pCurLayer->pLastCodedMbIdxOfPartition[kiPartitionId] = iCurMbIdx - | |
| 644 | − 1; // update pLastCodedMbIdxOfPartition, need to -1 due to stepping back | |
| 645 | − ++ pCurLayer->pNumSliceCodedOfPartition[kiPartitionId]; | |
| 643 | + pCurLayer->LastCodedMbIdxOfPartition[kiPartitionId] = iCurMbIdx - | |
| 644 | + 1; // update LastCodedMbIdxOfPartition, need to -1 due to stepping back | |
| 645 | + ++ pCurLayer->NumSliceCodedOfPartition[kiPartitionId]; | |
| 646 | 646 | |
| 647 | 647 | break; |
| 648 | 648 | } |
| @@ -661,9 +661,10 @@ TRY_REENCODING: · 12 unmodified lines | ||
| 661 | 661 | iNextMbIdx = WelsGetNextMbOfSlice (pCurLayer, iCurMbIdx); |
| 662 | 662 | //whether all of MB in current pSlice encoded or not |
| 663 | 663 | if (iNextMbIdx == -1 || iNextMbIdx >= kiTotalNumMb || iNumMbCoded >= kiTotalNumMb) { |
| 664 | − pSlice->iCountMbNumInSlice = iCurMbIdx - pCurLayer->pLastCodedMbIdxOfPartition[kiPartitionId]; | |
| 665 | − pCurLayer->pLastCodedMbIdxOfPartition[kiPartitionId] = | |
| 666 | − iCurMbIdx; // update pLastCodedMbIdxOfPartition, finish coding, use iCurMbIdx directly | |
| 664 | + pSlice->iCountMbNumInSlice = iCurMbIdx - pCurLayer->LastCodedMbIdxOfPartition[kiPartitionId]; | |
| 665 | + pCurLayer->LastCodedMbIdxOfPartition[kiPartitionId] = iCurMbIdx; | |
| 666 | + ++ pCurLayer->NumSliceCodedOfPartition[kiPartitionId]; | |
| 667 | + | |
| 667 | 668 | break; |
| 668 | 669 | } |
| 669 | 670 | } |
| @@ -754,10 +755,11 @@ static const PWelsSliceHeaderWriteFunc g_pWelsWriteSliceHeader[2] = { // 0: for · 84 unmodified lines | ||
| 754 | 755 | |
| 755 | 756 | //Allocate slice's MB cache buffer |
| 756 | 757 | int32_t AllocMbCacheAligned (SMbCache* pMbCache, CMemoryAlign* pMa) { |
| 757 | − pMbCache->pCoeffLevel = (int16_t*)pMa->WelsMallocz (MB_COEFF_LIST_SIZE * sizeof (int16_t), "pMbCache->pCoeffLevel"); | |
| 758 | − WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pCoeffLevel)); | |
| 759 | 758 | pMbCache->pMemPredMb = (uint8_t*)pMa->WelsMallocz (2 * 256 * sizeof (uint8_t), "pMbCache->pMemPredMb"); |
| 760 | 759 | WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pMemPredMb)); |
| 760 | + | |
| 761 | + pMbCache->pCoeffLevel = (int16_t*)pMa->WelsMallocz (MB_COEFF_LIST_SIZE * sizeof (int16_t), "pMbCache->pCoeffLevel"); | |
| 762 | + WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pCoeffLevel)); | |
| 761 | 763 | pMbCache->pSkipMb = (uint8_t*)pMa->WelsMallocz (384 * sizeof (uint8_t), "pMbCache->pSkipMb"); |
| 762 | 764 | WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pSkipMb)); |
| 763 | 765 | pMbCache->pMemPredBlk4 = (uint8_t*)pMa->WelsMallocz (2 * 16 * sizeof (uint8_t), "pMbCache->pMemPredBlk4"); |
| @@ -895,7 +897,7 @@ int32_t InitSliceBsBuffer (SSlice* pSlice, · 131 unmodified lines | ||
| 895 | 897 | |
| 896 | 898 | if (bIndependenceBsBuffer) { |
| 897 | 899 | pSlice->pSliceBsa = &pSlice->sSliceBs.sBsWrite; |
| 898 | − pSlice->sSliceBs.pBs = (uint8_t*)pMa->WelsMalloc (iMaxSliceBufferSize, "SliceBs"); | |
| 900 | + pSlice->sSliceBs.pBs = (uint8_t*)pMa->WelsMalloc (iMaxSliceBufferSize, "sSliceBs.pBs"); | |
| 899 | 901 | if (NULL == pSlice->sSliceBs.pBs) { |
| 900 | 902 | return ENC_RETURN_MEMALLOCERR; |
| 901 | 903 | } |
| @@ -979,15 +981,20 @@ int32_t InitSliceList (sWelsEncCtx* pCtx, · 77 unmodified lines | ||
| 979 | 981 | } |
| 980 | 982 | |
| 981 | 983 | int32_t InitAllSlicesInThread (sWelsEncCtx* pCtx) { |
| 982 | − SDqLayer* pDqLayer = pCtx->pCurDqLayer; | |
| 983 | − int32_t iSliceIdx = 0; | |
| 984 | + SDqLayer* pCurDqLayer = pCtx->pCurDqLayer; | |
| 985 | + int32_t iSliceIdx = 0; | |
| 986 | + int32_t iTheadIdx = 0; | |
| 984 | 987 | |
| 985 | − for( ; iSliceIdx < pDqLayer->iMaxSliceNum; iSliceIdx++) { | |
| 986 | − if(NULL == pDqLayer->ppSliceInLayer[iSliceIdx]) { | |
| 988 | + for( ; iSliceIdx < pCurDqLayer->iMaxSliceNum; iSliceIdx++) { | |
| 989 | + if(NULL == pCurDqLayer->ppSliceInLayer[iSliceIdx]) { | |
| 987 | 990 | return ENC_RETURN_UNEXPECTED; |
| 988 | 991 | } |
| 989 | 992 | |
| 990 | − pDqLayer->ppSliceInLayer[iSliceIdx]->iSliceIdx = -1; | |
| 993 | + pCurDqLayer->ppSliceInLayer[iSliceIdx]->iSliceIdx = -1; | |
| 994 | + } | |
| 995 | + | |
| 996 | + for( ; iTheadIdx < pCtx->iActiveThreadsNum; iTheadIdx++ ) { | |
| 997 | + pCurDqLayer->sSliceThreadInfo.iEncodedSliceNumInThread[iTheadIdx] = 0; | |
| 991 | 998 | } |
| 992 | 999 | |
| 993 | 1000 | return ENC_RETURN_SUCCESS; |
| @@ -1000,17 +1007,13 @@ int32_t InitOneSliceInThread (sWelsEncCtx* pCtx, · 6 unmodified lines | ||
| 1000 | 1007 | const int32_t kiSliceIdx) { |
| 1001 | 1008 | SDqLayer* pDqLayer = pCtx->pCurDqLayer; |
| 1002 | 1009 | const int32_t kiCodedNumInThread = pDqLayer->sSliceThreadInfo.iEncodedSliceNumInThread[kiThreadIdx]; |
| 1003 | − const int32_t kiMaxSliceNumInThread = pDqLayer->sSliceThreadInfo.iMaxSliceNumInThread[kiThreadIdx]; | |
| 1004 | − int32_t iRet = 0; | |
| 1005 | 1010 | |
| 1006 | − if (kiCodedNumInThread >= kiMaxSliceNumInThread) { | |
| 1007 | − iRet = ReallocateSliceInThread(pCtx, pDqLayer, kiDlayerIdx, kiThreadIdx); | |
| 1008 | − if (ENC_RETURN_SUCCESS != iRet) { | |
| 1009 | − return iRet; | |
| 1010 | − } | |
| 1011 | − } | |
| 1011 | + assert(kiCodedNumInThread < pDqLayer->sSliceThreadInfo.iMaxSliceNumInThread[kiThreadIdx] -1 ); | |
| 1012 | + | |
| 1013 | + pSlice = &pDqLayer->sSliceThreadInfo.pSliceInThread [kiThreadIdx][kiCodedNumInThread]; | |
| 1014 | + pSlice->iSliceIdx = kiSliceIdx; | |
| 1015 | + pSlice->uiThreadIdx = kiThreadIdx; | |
| 1012 | 1016 | |
| 1013 | − pSlice = pDqLayer->sSliceThreadInfo.pSliceInThread [kiThreadIdx] + kiCodedNumInThread; | |
| 1014 | 1017 | // Initialize slice bs buffer info |
| 1015 | 1018 | pSlice->sSliceBs.uiBsPos = 0; |
| 1016 | 1019 | pSlice->sSliceBs.iNalIndex = 0; |
| @@ -1030,6 +1033,7 @@ int32_t InitSliceThreadInfo (sWelsEncCtx* pCtx, · 13 unmodified lines | ||
| 1030 | 1033 | int32_t iIdx = 0; |
| 1031 | 1034 | int32_t iRet = 0; |
| 1032 | 1035 | |
| 1036 | + assert (iThreadNum > 0); | |
| 1033 | 1037 | iMaxSliceNumInThread = pDqLayer->iMaxSliceNum; |
| 1034 | 1038 | while (iIdx < iThreadNum) { |
| 1035 | 1039 | pSliceThreadInfo->iMaxSliceNumInThread[iIdx] = iMaxSliceNumInThread; |
| @@ -1078,7 +1082,10 @@ int32_t InitSliceInLayer (sWelsEncCtx* pCtx, · 42 unmodified lines | ||
| 1078 | 1082 | return ENC_RETURN_MEMALLOCERR; |
| 1079 | 1083 | } |
| 1080 | 1084 | |
| 1081 | − pDqLayer->iMaxSliceNum = iMaxSliceNum * pCtx->iActiveThreadsNum; | |
| 1085 | + pDqLayer->iMaxSliceNum = 0; | |
| 1086 | + for(iThreadIdx = 0; iThreadIdx < pCtx->iActiveThreadsNum; iThreadIdx++ ) { | |
| 1087 | + pDqLayer->iMaxSliceNum += pDqLayer->sSliceThreadInfo.iMaxSliceNumInThread[iThreadIdx]; | |
| 1088 | + } | |
| 1082 | 1089 | |
| 1083 | 1090 | pDqLayer->ppSliceInLayer = (SSlice**)pMa->WelsMallocz (sizeof (SSlice*) * pDqLayer->iMaxSliceNum, "ppSliceInLayer"); |
| 1084 | 1091 | if (NULL == pDqLayer->ppSliceInLayer) { |
| @@ -1120,14 +1127,14 @@ void InitSliceHeadWithBase (SSlice* pSlice, SSlice* pBaseSlice) { · 35 unmodified lines | ||
| 1120 | 1127 | return; |
| 1121 | 1128 | } |
| 1122 | 1129 | |
| 1123 | − SSliceHeaderExt* pBaseSHExt = &pBaseSlice->sSliceHeaderExt; | |
| 1124 | − SSliceHeaderExt* pSHExt = &pSlice->sSliceHeaderExt; | |
| 1130 | + SSliceHeaderExt* pBaseSHExt = &pBaseSlice->sSliceHeaderExt; | |
| 1131 | + SSliceHeaderExt* pSHExt = &pSlice->sSliceHeaderExt; | |
| 1125 | 1132 | |
| 1126 | − pSlice->bSliceHeaderExtFlag = pBaseSlice->bSliceHeaderExtFlag; | |
| 1127 | − pSHExt->sSliceHeader.iPpsId = pBaseSHExt->sSliceHeader.iPpsId; | |
| 1128 | − pSHExt->sSliceHeader.pPps = pBaseSHExt->sSliceHeader.pPps; | |
| 1129 | − pSHExt->sSliceHeader.iSpsId = pBaseSHExt->sSliceHeader.iSpsId; | |
| 1130 | − pSHExt->sSliceHeader.pSps = pBaseSHExt->sSliceHeader.pSps; | |
| 1133 | + pSlice->bSliceHeaderExtFlag = pBaseSlice->bSliceHeaderExtFlag; | |
| 1134 | + pSHExt->sSliceHeader.iPpsId = pBaseSHExt->sSliceHeader.iPpsId; | |
| 1135 | + pSHExt->sSliceHeader.pPps = pBaseSHExt->sSliceHeader.pPps; | |
| 1136 | + pSHExt->sSliceHeader.iSpsId = pBaseSHExt->sSliceHeader.iSpsId; | |
| 1137 | + pSHExt->sSliceHeader.pSps = pBaseSHExt->sSliceHeader.pSps; | |
| 1131 | 1138 | } |
| 1132 | 1139 | |
| 1133 | 1140 | void InitSliceRefInfoWithBase (SSlice* pSlice, SSlice* pBaseSlice, const uint8_t kuiRefCount) { |
| @@ -1181,28 +1188,41 @@ int32_t ReallocateSliceList (sWelsEncCtx* pCtx, · 47 unmodified lines | ||
| 1181 | 1188 | return ENC_RETURN_INVALIDINPUT; |
| 1182 | 1189 | } |
| 1183 | 1190 | |
| 1184 | − pNewSliceList = (SSlice*)pMA->WelsMallocz (sizeof (SSlice) * kiMaxSliceNumNew, "Slice"); | |
| 1191 | + pNewSliceList = (SSlice*)pMA->WelsMallocz (sizeof (SSlice) * kiMaxSliceNumNew, "pSliceInThread"); | |
| 1185 | 1192 | if (NULL == pNewSliceList) { |
| 1186 | 1193 | WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR, "CWelsH264SVCEncoder::ReallocateSliceList: pNewSliceList is NULL"); |
| 1187 | 1194 | return ENC_RETURN_MEMALLOCERR; |
| 1188 | 1195 | } |
| 1189 | 1196 | |
| 1190 | 1197 | memcpy (pNewSliceList, pSliceList, sizeof (SSlice) * kiMaxSliceNumOld); |
| 1191 | − iSliceIdx = kiMaxSliceNumOld; | |
| 1192 | − pBaseSlice = &pSliceList[0]; | |
| 1198 | + | |
| 1199 | + //update Bs writer | |
| 1200 | + for (iSliceIdx = 0; iSliceIdx < kiMaxSliceNumOld; iSliceIdx++) { | |
| 1201 | + pSlice = pNewSliceList + iSliceIdx; | |
| 1202 | + if (NULL == pSlice) { | |
| 1203 | + FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "pSliceInThread"); | |
| 1204 | + return ENC_RETURN_MEMALLOCERR; | |
| 1205 | + } | |
| 1206 | + | |
| 1207 | + if (bIndependenceBsBuffer) { | |
| 1208 | + pSlice->pSliceBsa = &pSlice->sSliceBs.sBsWrite; | |
| 1209 | + } | |
| 1210 | + } | |
| 1211 | + | |
| 1212 | + pBaseSlice = &pSliceList[0]; | |
| 1193 | 1213 | if (NULL == pBaseSlice) { |
| 1194 | 1214 | FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "ReallocateSliceList()::InitSliceBsBuffer()"); |
| 1195 | 1215 | return ENC_RETURN_MEMALLOCERR; |
| 1196 | 1216 | } |
| 1197 | 1217 | |
| 1198 | − for (; iSliceIdx < kiMaxSliceNumNew; iSliceIdx++) { | |
| 1218 | + for (iSliceIdx = kiMaxSliceNumOld; iSliceIdx < kiMaxSliceNumNew; iSliceIdx++) { | |
| 1199 | 1219 | pSlice = pNewSliceList + iSliceIdx; |
| 1200 | 1220 | if (NULL == pSlice) { |
| 1201 | − FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "ReallocateSliceList()::InitSliceBsBuffer()"); | |
| 1221 | + FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "pSliceInThread"); | |
| 1202 | 1222 | return ENC_RETURN_MEMALLOCERR; |
| 1203 | 1223 | } |
| 1204 | 1224 | |
| 1205 | − pSlice->iSliceIdx = iSliceIdx; | |
| 1225 | + pSlice->iSliceIdx = -1; | |
| 1206 | 1226 | pSlice->uiThreadIdx = 0; |
| 1207 | 1227 | pSlice->iCountMbNumInSlice = 0; |
| 1208 | 1228 | pSlice->sSliceHeaderExt.sSliceHeader.iFirstMbInSlice = 0; |
| @@ -1213,13 +1233,13 @@ int32_t ReallocateSliceList (sWelsEncCtx* pCtx, · 4 unmodified lines | ||
| 1213 | 1233 | iMaxSliceBufferSize, |
| 1214 | 1234 | pMA); |
| 1215 | 1235 | if (ENC_RETURN_SUCCESS != iRet) { |
| 1216 | − FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "ReallocateSliceList()::InitSliceBsBuffer()"); | |
| 1236 | + FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "pSliceInThread"); | |
| 1217 | 1237 | return iRet; |
| 1218 | 1238 | } |
| 1219 | 1239 | |
| 1220 | 1240 | iRet = AllocateSliceMBBuffer (pSlice, pMA); |
| 1221 | 1241 | if (ENC_RETURN_SUCCESS != iRet) { |
| 1222 | − FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "ReallocateSliceList()::InitSliceBsBuffer()"); | |
| 1242 | + FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "pSliceInThread"); | |
| 1223 | 1243 | return iRet; |
| 1224 | 1244 | } |
| 1225 | 1245 | |
| @@ -1228,12 +1248,12 @@ int32_t ReallocateSliceList (sWelsEncCtx* pCtx, · 2 unmodified lines | ||
| 1228 | 1248 | |
| 1229 | 1249 | iRet = InitSliceRC (pSlice, pCtx->iGlobalQp); |
| 1230 | 1250 | if (ENC_RETURN_SUCCESS != iRet) { |
| 1231 | − FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "ReallocateSliceList()::InitSliceBsBuffer()"); | |
| 1251 | + FreeSliceBuffer(pNewSliceList, kiMaxSliceNumNew, pMA, "pSliceInThread"); | |
| 1232 | 1252 | return iRet; |
| 1233 | 1253 | } |
| 1234 | 1254 | } |
| 1235 | 1255 | |
| 1236 | − pMA->WelsFree (pSliceList, "Slice"); | |
| 1256 | + pMA->WelsFree (pSliceList, "pSliceInThread"); | |
| 1237 | 1257 | pSliceList = pNewSliceList; |
| 1238 | 1258 | |
| 1239 | 1259 | return ENC_RETURN_SUCCESS; |
| @@ -1290,6 +1310,8 @@ int32_t ReallocateSliceInThread (sWelsEncCtx* pCtx, · 50 unmodified lines | ||
| 1290 | 1310 | return iRet; |
| 1291 | 1311 | } |
| 1292 | 1312 | |
| 1313 | + pDqLayer->sSliceThreadInfo.iMaxSliceNumInThread[kiThreadIndex] = iMaxSliceNumUpdate; | |
| 1314 | + | |
| 1293 | 1315 | return ENC_RETURN_SUCCESS; |
| 1294 | 1316 | } |
| 1295 | 1317 | |
| @@ -1299,11 +1321,11 @@ int32_t ExtendLayerBuffer(sWelsEncCtx* pCtx, · 3 unmodified lines | ||
| 1299 | 1321 | CMemoryAlign* pMA = pCtx->pMemAlign; |
| 1300 | 1322 | SDqLayer* pCurLayer = pCtx->pCurDqLayer; |
| 1301 | 1323 | SSlice** ppSlice = NULL; |
| 1302 | − int32_t* pFirstMbIdxofSlice = NULL; | |
| 1324 | + int32_t* pFirstMbIdxOfSlice = NULL; | |
| 1303 | 1325 | int32_t* pCountMbNumInSlice = NULL; |
| 1304 | 1326 | |
| 1305 | 1327 | // update for ppsliceInlayer |
| 1306 | − ppSlice = (SSlice**)pMA->WelsMallocz (sizeof (SSlice*) * kiMaxSliceNumNew, "ppSlice"); | |
| 1328 | + ppSlice = (SSlice**)pMA->WelsMallocz (sizeof (SSlice*) * kiMaxSliceNumNew, "ppSliceInLayer"); | |
| 1307 | 1329 | if (NULL == ppSlice) { |
| 1308 | 1330 | WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR, "CWelsH264SVCEncoder::ExtendLayerBuffer: ppSlice is NULL"); |
| 1309 | 1331 | return ENC_RETURN_MEMALLOCERR; |
| @@ -1312,15 +1334,15 @@ int32_t ExtendLayerBuffer(sWelsEncCtx* pCtx, · 2 unmodified lines | ||
| 1312 | 1334 | pCurLayer->ppSliceInLayer = ppSlice; |
| 1313 | 1335 | |
| 1314 | 1336 | // update for pFirstMbIdxInSlice |
| 1315 | − pFirstMbIdxofSlice = (int32_t*)pMA->WelsMallocz (sizeof (int32_t*) * kiMaxSliceNumNew, "pFirstMbIdxofSlice"); | |
| 1316 | − if (NULL == pFirstMbIdxofSlice) { | |
| 1317 | − WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR, "CWelsH264SVCEncoder::ExtendLayerBuffer: pFirstMbIdxofSlice is NULL"); | |
| 1337 | + pFirstMbIdxOfSlice = (int32_t*)pMA->WelsMallocz (sizeof (int32_t*) * kiMaxSliceNumNew, "pFirstMbIdxOfSlice"); | |
| 1338 | + if (NULL == pFirstMbIdxOfSlice) { | |
| 1339 | + WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR, "CWelsH264SVCEncoder::ExtendLayerBuffer: pFirstMbIdxOfSlice is NULL"); | |
| 1318 | 1340 | return ENC_RETURN_MEMALLOCERR; |
| 1319 | 1341 | } |
| 1320 | − memset (pFirstMbIdxofSlice, 0, sizeof(int32_t) * kiMaxSliceNumNew); | |
| 1321 | − memcpy (pFirstMbIdxofSlice, pCurLayer->pFirstMbIdxOfSlice, sizeof (int32_t) * kiMaxSliceNumOld); | |
| 1322 | − pMA->WelsFree (pCurLayer->pFirstMbIdxOfSlice, "pFirstMbIdxofSlice"); | |
| 1323 | − pCurLayer->pFirstMbIdxOfSlice = pFirstMbIdxofSlice; | |
| 1342 | + memset (pFirstMbIdxOfSlice, 0, sizeof(int32_t) * kiMaxSliceNumNew); | |
| 1343 | + memcpy (pFirstMbIdxOfSlice, pCurLayer->pFirstMbIdxOfSlice, sizeof (int32_t) * kiMaxSliceNumOld); | |
| 1344 | + pMA->WelsFree (pCurLayer->pFirstMbIdxOfSlice, "pFirstMbIdxOfSlice"); | |
| 1345 | + pCurLayer->pFirstMbIdxOfSlice = pFirstMbIdxOfSlice; | |
| 1324 | 1346 | |
| 1325 | 1347 | // update for pCountMbNumInSlice |
| 1326 | 1348 | pCountMbNumInSlice = (int32_t*)pMA->WelsMallocz (sizeof (int32_t*) * kiMaxSliceNumNew, "pCountMbNumInSlice"); |
| @@ -1405,10 +1427,10 @@ static inline int32_t CheckAllSliceBuffer(SDqLayer* pCurLayer, const int32_t kiC · 78 unmodified lines | ||
| 1405 | 1427 | return ENC_RETURN_SUCCESS; |
| 1406 | 1428 | } |
| 1407 | 1429 | |
| 1408 | − | |
| 1409 | −int32_t ReOrderSliceInLayer (SDqLayer* pCurLayer, | |
| 1410 | − const int32_t kiThreadNum, | |
| 1411 | − const int32_t kiPartitionNum) { | |
| 1430 | +int32_t ReOrderSliceInLayer (sWelsEncCtx* pCtx, | |
| 1431 | + const SliceModeEnum kuiSliceMode, | |
| 1432 | + const int32_t kiThreadNum) { | |
| 1433 | + SDqLayer* pCurLayer = pCtx->pCurDqLayer; | |
| 1412 | 1434 | SSlice* pSliceInThread = NULL; |
| 1413 | 1435 | int32_t iThreadIdx = 0; |
| 1414 | 1436 | int32_t iPartitionIdx = 0; |
| @@ -1419,12 +1441,19 @@ int32_t ReOrderSliceInLayer (SDqLayer* pCurLayer, · 4 unmodified lines | ||
| 1419 | 1441 | int32_t iActualSliceIdx = 0; |
| 1420 | 1442 | int32_t iNonUsedBufferNum = 0; |
| 1421 | 1443 | int32_t iUsedSliceNum = 0; |
| 1444 | + | |
| 1445 | + int32_t iPartitionNum = 0; | |
| 1422 | 1446 | int32_t aiPartitionOffset[MAX_THREADS_NUM] = {0}; |
| 1423 | 1447 | |
| 1424 | − //for non-dynamic slice mode, kiPartitionNum = 1, iPartitionOffset = 0 | |
| 1425 | − for(iPartitionIdx = 0; iPartitionIdx < kiPartitionNum; iPartitionIdx++) { | |
| 1448 | + //for non-dynamic slice mode, iPartitionNum = 1, iPartitionOffset = 0 | |
| 1449 | + iPartitionNum = (SM_SIZELIMITED_SLICE == kuiSliceMode) ? kiThreadNum : 1; | |
| 1450 | + for(iPartitionIdx = 0; iPartitionIdx < iPartitionNum; iPartitionIdx++) { | |
| 1426 | 1451 | aiPartitionOffset[iPartitionIdx] = iEncodeSliceNum; |
| 1427 | − iEncodeSliceNum += pCurLayer->pNumSliceCodedOfPartition[iPartitionIdx]; | |
| 1452 | + if ( SM_SIZELIMITED_SLICE == kuiSliceMode) { | |
| 1453 | + iEncodeSliceNum += pCurLayer->NumSliceCodedOfPartition[iPartitionIdx]; | |
| 1454 | + } else { | |
| 1455 | + iEncodeSliceNum = pCurLayer->sSliceEncCtx.iSliceNumInFrame; | |
| 1456 | + } | |
| 1428 | 1457 | } |
| 1429 | 1458 | |
| 1430 | 1459 | if( iEncodeSliceNum != pCurLayer->sSliceEncCtx.iSliceNumInFrame) { |
| @@ -1443,8 +1472,9 @@ int32_t ReOrderSliceInLayer (SDqLayer* pCurLayer, · 12 unmodified lines | ||
| 1443 | 1472 | } |
| 1444 | 1473 | |
| 1445 | 1474 | if( -1 != pSliceInThread->iSliceIdx) { |
| 1446 | − iPartitionID = pSliceInThread->iSliceIdx % kiPartitionNum; | |
| 1447 | − iActualSliceIdx = aiPartitionOffset[iPartitionID] + pSliceInThread->iSliceIdx / kiPartitionNum; | |
| 1475 | + iPartitionID = pSliceInThread->iSliceIdx % iPartitionNum; | |
| 1476 | + iActualSliceIdx = aiPartitionOffset[iPartitionID] + pSliceInThread->iSliceIdx / iPartitionNum; | |
| 1477 | + pSliceInThread->iSliceIdx = iActualSliceIdx; | |
| 1448 | 1478 | pCurLayer->ppSliceInLayer[iActualSliceIdx] = pSliceInThread; |
| 1449 | 1479 | iUsedSliceNum ++; |
| 1450 | 1480 | } else { |
| @@ -1466,6 +1496,29 @@ int32_t ReOrderSliceInLayer (SDqLayer* pCurLayer, · 15 unmodified lines | ||
| 1466 | 1496 | return ENC_RETURN_SUCCESS; |
| 1467 | 1497 | } |
| 1468 | 1498 | |
| 1499 | +int32_t GetCurLayerNalCount(const SDqLayer* pCurDq, const int32_t kiCodedSliceNum) { | |
| 1500 | + int32_t iTotalNalCount = 0; | |
| 1501 | + int32_t iSliceIdx = 0; | |
| 1502 | + SWelsSliceBs* pSliceBs = NULL; | |
| 1503 | + for ( ; iSliceIdx < kiCodedSliceNum; iSliceIdx++ ) { | |
| 1504 | + pSliceBs = &pCurDq->ppSliceInLayer[iSliceIdx]->sSliceBs; | |
| 1505 | + if (pSliceBs != NULL && pSliceBs->uiBsPos > 0) { | |
| 1506 | + iTotalNalCount += pSliceBs->iNalIndex; | |
| 1507 | + } | |
| 1508 | + } | |
| 1509 | + | |
| 1510 | + return iTotalNalCount; | |
| 1511 | +} | |
| 1512 | + | |
| 1513 | +int32_t GetTotalCodedNalCount(SFrameBSInfo* pFbi) { | |
| 1514 | + int32_t iTotalCodedNalCount = 0; | |
| 1515 | + for (int32_t iNalIdx = 0; iNalIdx < MAX_LAYER_NUM_OF_FRAME; iNalIdx++) { | |
| 1516 | + iTotalCodedNalCount += pFbi->sLayerInfo[iNalIdx].iNalCount; | |
| 1517 | + } | |
| 1518 | + | |
| 1519 | + return iTotalCodedNalCount; | |
| 1520 | +} | |
| 1521 | + | |
| 1469 | 1522 | int32_t FrameBsRealloc (sWelsEncCtx* pCtx, |
| 1470 | 1523 | SFrameBSInfo* pFrameBsInfo, |
| 1471 | 1524 | SLayerBSInfo* pLayerBsInfo, |
| @@ -1508,39 +1561,45 @@ int32_t FrameBsRealloc (sWelsEncCtx* pCtx, · 36 unmodified lines | ||
| 1508 | 1561 | int32_t SliceLayerInfoUpdate (sWelsEncCtx* pCtx, |
| 1509 | 1562 | SFrameBSInfo* pFrameBsInfo, |
| 1510 | 1563 | SLayerBSInfo* pLayerBsInfo, |
| 1511 | − const int32_t kiPartitionNum) { | |
| 1512 | − SDqLayer* pCurLayer = pCtx->pCurDqLayer; | |
| 1513 | − int32_t iMaxSliceNum = 0; | |
| 1514 | − int32_t iThreadIdx = 0; | |
| 1515 | − int32_t iRet = 0; | |
| 1516 | − int32_t iThreadNum = 1; //TODO: should be equal to pCurLayer->iMaxSliceNum; | |
| 1564 | + const SliceModeEnum kuiSliceMode) { | |
| 1565 | + int32_t iMaxSliceNum = 0; | |
| 1566 | + int32_t iCodedSliceNum = 0; | |
| 1567 | + int32_t iCodedNalCount = 0; | |
| 1568 | + int32_t iRet = 0; | |
| 1517 | 1569 | |
| 1518 | − for ( ; iThreadIdx < iThreadNum; iThreadIdx++) { | |
| 1519 | − iMaxSliceNum += pCurLayer->sSliceThreadInfo.iMaxSliceNumInThread[iThreadIdx]; | |
| 1570 | + for ( int32_t iThreadIdx = 0; iThreadIdx < pCtx->iActiveThreadsNum; iThreadIdx++) { | |
| 1571 | + iMaxSliceNum += pCtx->pCurDqLayer->sSliceThreadInfo.iMaxSliceNumInThread[iThreadIdx]; | |
| 1520 | 1572 | } |
| 1521 | 1573 | |
| 1522 | 1574 | //reallocate ppSliceInLayer if total encoded slice num exceed max slice num |
| 1523 | − if (iMaxSliceNum > pCurLayer->iMaxSliceNum) { | |
| 1524 | − iRet = FrameBsRealloc (pCtx, pFrameBsInfo, pLayerBsInfo, pCtx->pCurDqLayer->iMaxSliceNum); | |
| 1525 | − if(ENC_RETURN_SUCCESS != iRet) { | |
| 1526 | − return iRet; | |
| 1527 | − } | |
| 1528 | − | |
| 1575 | + if (iMaxSliceNum > pCtx->pCurDqLayer->iMaxSliceNum) { | |
| 1529 | 1576 | iRet = ExtendLayerBuffer(pCtx, pCtx->pCurDqLayer->iMaxSliceNum, iMaxSliceNum); |
| 1530 | 1577 | if (ENC_RETURN_SUCCESS != iRet) { |
| 1531 | 1578 | return iRet; |
| 1532 | 1579 | } |
| 1533 | − pCurLayer->iMaxSliceNum = iMaxSliceNum; | |
| 1580 | + pCtx->pCurDqLayer->iMaxSliceNum = iMaxSliceNum; | |
| 1534 | 1581 | } |
| 1535 | 1582 | |
| 1536 | 1583 | //update ppSliceInLayer based on pSliceInThread, reordering based on slice index |
| 1537 | − iRet = ReOrderSliceInLayer (pCurLayer, iThreadNum, kiPartitionNum); | |
| 1584 | + iRet = ReOrderSliceInLayer (pCtx, kuiSliceMode, pCtx->iActiveThreadsNum); | |
| 1538 | 1585 | if (ENC_RETURN_SUCCESS != iRet) { |
| 1539 | 1586 | WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR, |
| 1540 | 1587 | "CWelsH264SVCEncoder::SliceLayerInfoUpdate: ReOrderSliceInLayer failed"); |
| 1541 | 1588 | return iRet; |
| 1542 | 1589 | } |
| 1543 | 1590 | |
| 1591 | + //Extend NalList buffer if exceed | |
| 1592 | + iCodedSliceNum = GetCurrentSliceNum (pCtx->pCurDqLayer); | |
| 1593 | + pLayerBsInfo->iNalCount = GetCurLayerNalCount(pCtx->pCurDqLayer, iCodedSliceNum); | |
| 1594 | + iCodedNalCount = GetTotalCodedNalCount(pFrameBsInfo); | |
| 1595 | + | |
| 1596 | + if( iCodedNalCount > pCtx->pOut->iCountNals) { | |
| 1597 | + iRet = FrameBsRealloc (pCtx, pFrameBsInfo, pLayerBsInfo, pCtx->pCurDqLayer->iMaxSliceNum); | |
| 1598 | + if(ENC_RETURN_SUCCESS != iRet) { | |
| 1599 | + return iRet; | |
| 1600 | + } | |
| 1601 | + } | |
| 1602 | + | |
| 1544 | 1603 | return ENC_RETURN_SUCCESS; |
| 1545 | 1604 | } |
| 1546 | 1605 | |
| @@ -1602,7 +1661,8 @@ void UpdateMbNeighbourInfoForNextSlice (SDqLayer* pCurDq, · 55 unmodified lines | ||
| 1602 | 1661 | void AddSliceBoundary (sWelsEncCtx* pEncCtx, SSlice* pCurSlice, SSliceCtx* pSliceCtx, SMB* pCurMb, |
| 1603 | 1662 | int32_t iFirstMbIdxOfNextSlice, const int32_t kiLastMbIdxInPartition) { |
| 1604 | 1663 | SDqLayer* pCurLayer = pEncCtx->pCurDqLayer; |
| 1605 | − SSlice* pSliceInThread = pCurLayer->sSliceThreadInfo.pSliceInThread[0]; | |
| 1664 | + SSlice* pSliceInThread = pCurLayer->sSliceThreadInfo.pSliceInThread[pCurSlice->uiThreadIdx]; | |
| 1665 | + int32_t iCodedSliceNum = pCurLayer->sSliceThreadInfo.iEncodedSliceNumInThread[pCurSlice->uiThreadIdx]; | |
| 1606 | 1666 | int32_t iCurMbIdx = pCurMb->iMbXY; |
| 1607 | 1667 | uint16_t iCurSliceIdc = pSliceCtx->pOverallMbMap[ iCurMbIdx ]; |
| 1608 | 1668 | const int32_t kiSliceIdxStep = pEncCtx->iActiveThreadsNum; |
| @@ -1615,7 +1675,11 @@ void AddSliceBoundary (sWelsEncCtx* pEncCtx, SSlice* pCurSlice, SSliceCtx* pSlic · 6 unmodified lines | ||
| 1615 | 1675 | pCurSlice->sSliceHeaderExt.uiNumMbsInSlice = 1 + iCurMbIdx - pCurSlice->sSliceHeaderExt.sSliceHeader.iFirstMbInSlice; |
| 1616 | 1676 | |
| 1617 | 1677 | //pNextSlice pointer/initialization |
| 1618 | − pNextSlice = &pSliceInThread[ iNextSliceIdc ]; | |
| 1678 | + if(pEncCtx->iActiveThreadsNum > 1) { | |
| 1679 | + pNextSlice = &pSliceInThread[ iCodedSliceNum + 1 ]; | |
| 1680 | + } else { | |
| 1681 | + pNextSlice = &pSliceInThread[ iNextSliceIdc ]; | |
| 1682 | + } | |
| 1619 | 1683 | |
| 1620 | 1684 | #if _DEBUG |
| 1621 | 1685 | assert (NULL != pNextSlice); |
| @@ -1643,7 +1707,7 @@ bool DynSlcJudgeSliceBoundaryStepBack (void* pCtx, void* pSlice, SSliceCtx* pSli · 21 unmodified lines | ||
| 1643 | 1707 | int32_t iPosBitOffset = 0; |
| 1644 | 1708 | const int32_t kiActiveThreadsNum = pEncCtx->iActiveThreadsNum; |
| 1645 | 1709 | const int32_t kiPartitaionId = pCurSlice->iSliceIdx % kiActiveThreadsNum; |
| 1646 | − const int32_t kiEndMbIdxOfPartition = pEncCtx->pCurDqLayer->pEndMbIdxOfPartition[kiPartitaionId]; | |
| 1710 | + const int32_t kiEndMbIdxOfPartition = pEncCtx->pCurDqLayer->EndMbIdxOfPartition[kiPartitaionId]; | |
| 1647 | 1711 | const int32_t kiMaxSliceNum = pEncCtx->pCurDqLayer->sSliceThreadInfo.iMaxSliceNumInThread[pCurSlice->uiThreadIdx]; |
| 1648 | 1712 | const bool kbCurMbNotFirstMbOfCurSlice = ((iCurMbIdx > 0) && (pSliceCtx->pOverallMbMap[iCurMbIdx] == |
| 1649 | 1713 | pSliceCtx->pOverallMbMap[iCurMbIdx - 1])); |
| @@ -1676,37 +1740,14 @@ bool DynSlcJudgeSliceBoundaryStepBack (void* pCtx, void* pSlice, SSliceCtx* pSli · 26 unmodified lines | ||
| 1676 | 1740 | // as iMaxSliceNum is always equal to iMaxSliceNumConstraint in origin design |
| 1677 | 1741 | // and will also extend when reallocated, |
| 1678 | 1742 | // tmp change is: iMaxSliceNumConstraint is alway set to be MAXSLICENUM, will not change even reallocate |
| 1679 | − const bool kbSliceNumNotExceedConstraint = pSliceCtx->iSliceNumInFrame < kiMaxSliceNum; | |
| 1680 | − const bool kbSliceIdxNotExceedConstraint = ((int) pCurSlice->iSliceIdx + kiActiveThreadsNum) < kiMaxSliceNum; | |
| 1681 | − const bool kbSliceNumReachConstraint = (pSliceCtx->iSliceNumInFrame == kiMaxSliceNum); | |
| 1682 | − | |
| 1683 | − //DYNAMIC_SLICING_ONE_THREAD: judge jump_avoiding_pack_exceed | |
| 1684 | − if (kbSliceNumNotExceedConstraint && kbSliceIdxNotExceedConstraint) {//able to add new pSlice | |
| 1743 | + AddSliceBoundary (pEncCtx, pCurSlice, pSliceCtx, pCurMb, iCurMbIdx, kiEndMbIdxOfPartition); | |
| 1744 | + ++ pSliceCtx->iSliceNumInFrame; | |
| 1685 | 1745 | |
| 1686 | − AddSliceBoundary (pEncCtx, pCurSlice, pSliceCtx, pCurMb, iCurMbIdx, kiEndMbIdxOfPartition); | |
| 1687 | − | |
| 1688 | − ++ pSliceCtx->iSliceNumInFrame; | |
| 1689 | − | |
| 1690 | − if (pEncCtx->pSvcParam->iMultipleThreadIdc > 1) { | |
| 1691 | − WelsMutexUnlock (&pEncCtx->pSliceThreading->mutexSliceNumUpdate); | |
| 1692 | − } | |
| 1693 | − | |
| 1694 | − return true; | |
| 1695 | − } | |
| 1696 | 1746 | if (pEncCtx->pSvcParam->iMultipleThreadIdc > 1) { |
| 1697 | 1747 | WelsMutexUnlock (&pEncCtx->pSliceThreading->mutexSliceNumUpdate); |
| 1698 | 1748 | } |
| 1699 | 1749 | |
| 1700 | − if ((kbSliceNumReachConstraint || !kbSliceIdxNotExceedConstraint) | |
| 1701 | − && kbCurMbNotLastMbOfCurPartition | |
| 1702 | − && JUMPPACKETSIZE_JUDGE (uiLen, iCurMbIdx, | |
| 1703 | − pSliceCtx->uiSliceSizeConstraint - ((kiEndMbIdxOfPartition - iCurMbIdx) << | |
| 1704 | − (pCurSlice->uiAssumeLog2BytePerMb) //assume each MB consumes these byte under largest QP | |
| 1705 | − )) | |
| 1706 | − ) { | |
| 1707 | − // to minimize the impact under the risk of exceeding the size constraint when pSlice num reaches constraint | |
| 1708 | − pCurSlice->bDynamicSlicingSliceSizeCtrlFlag = true; | |
| 1709 | − } | |
| 1750 | + return true; | |
| 1710 | 1751 | } |
| 1711 | 1752 | |
| 1712 | 1753 | return false; |
| @@ -1903,9 +1944,9 @@ TRY_REENCODING: · 190 unmodified lines | ||
| 1903 | 1944 | sDss.iCurrentPos = pEncCtx->pFuncList->pfGetBsPosition (pSlice); |
| 1904 | 1945 | if (DynSlcJudgeSliceBoundaryStepBack (pEncCtx, pSlice, pSliceCtx, pCurMb, &sDss)) { |
| 1905 | 1946 | pSlice->iMbSkipRun = pEncCtx->pFuncList->pfStashPopMBStatus (&sDss, pSlice); |
| 1906 | − pCurLayer->pLastCodedMbIdxOfPartition[kiPartitionId] = iCurMbIdx - | |
| 1907 | − 1; // update pLastCodedMbIdxOfPartition, need to -1 due to stepping back | |
| 1908 | − ++ pCurLayer->pNumSliceCodedOfPartition[kiPartitionId]; | |
| 1947 | + pCurLayer->LastCodedMbIdxOfPartition[kiPartitionId] = iCurMbIdx - | |
| 1948 | + 1; // update LastCodedMbIdxOfPartition, need to -1 due to stepping back | |
| 1949 | + ++ pCurLayer->NumSliceCodedOfPartition[kiPartitionId]; | |
| 1909 | 1950 | |
| 1910 | 1951 | break; |
| 1911 | 1952 | } |
| @@ -1926,8 +1967,9 @@ TRY_REENCODING: · 14 unmodified lines | ||
| 1926 | 1967 | iNextMbIdx = WelsGetNextMbOfSlice (pCurLayer, iCurMbIdx); |
| 1927 | 1968 | //whether all of MB in current pSlice encoded or not |
| 1928 | 1969 | if (iNextMbIdx == -1 || iNextMbIdx >= kiTotalNumMb || iNumMbCoded >= kiTotalNumMb) { |
| 1929 | − pCurLayer->pLastCodedMbIdxOfPartition[kiPartitionId] = | |
| 1930 | − iCurMbIdx; // update pLastCodedMbIdxOfPartition, finish coding, use pCurMb_idx directly | |
| 1970 | + pCurLayer->LastCodedMbIdxOfPartition[kiPartitionId] = iCurMbIdx; | |
| 1971 | + ++ pCurLayer->NumSliceCodedOfPartition[kiPartitionId]; | |
| 1972 | + | |
| 1931 | 1973 | break; |
| 1932 | 1974 | } |
| 1933 | 1975 | } |