Showing the changes to codec/encoder/core/src/slice_multi_threading.cpp (1 of 4 files). Show all
Unified · Split
| @@ -289,20 +289,50 @@ void DynamicAdjustSlicing (sWelsEncCtx* pCtx, · 288 unmodified lines | |||
| 289 | } | 289 | } |
| 290 | } | 290 | } |
| 291 | 291 | ||
| 292 | int32_t SetMultiSliceBuffer (sWelsEncCtx** ppCtx, CMemoryAlign* pMa, SSliceThreading* pSmt, | ||
| 293 | int32_t iMaxSliceNum, int32_t iSlice1Len, int32_t iSlice0Len, bool bDynamicSlice) { | ||
| 294 | (*ppCtx)->pSliceBs = (SWelsSliceBs*)pMa->WelsMalloc (sizeof (SWelsSliceBs) * iMaxSliceNum, "pSliceBs"); | ||
| 295 | if (NULL == (*ppCtx)->pSliceBs) { | ||
| 296 | return ENC_RETURN_MEMALLOCERR; | ||
| 297 | } | ||
| 298 | if (iSlice0Len <= 0) { | ||
| 299 | return ENC_RETURN_UNEXPECTED; | ||
| 300 | } | ||
| 301 | //slice 0 | ||
| 302 | (*ppCtx)->pSliceBs[0].uiSize = iSlice0Len; | ||
| 303 | (*ppCtx)->pSliceBs[0].pBs = (*ppCtx)->pFrameBs; | ||
| 304 | (*ppCtx)->pSliceBs[0].uiBsPos = 0; | ||
| 305 | (*ppCtx)->pSliceBs[0].pBsBuffer = pSmt->pThreadBsBuffer[0]; | ||
| 306 | if ((iMaxSliceNum == 1) && (!bDynamicSlice)) { | ||
| 307 | return ENC_RETURN_SUCCESS; | ||
| 308 | } | ||
| 309 | //slice >0 | ||
| 310 | if (iSlice1Len <= 0) { | ||
| 311 | return ENC_RETURN_UNEXPECTED; | ||
| 312 | } | ||
| 313 | for (int32_t k = 1; k < iMaxSliceNum; k++) { | ||
| 314 | (*ppCtx)->pSliceBs[k].uiSize = iSlice1Len; | ||
| 315 | (*ppCtx)->pSliceBs[k].pBs = (*ppCtx)->pSliceBs[k - 1].pBs + (*ppCtx)->pSliceBs[k - 1].uiSize; | ||
| 316 | } | ||
| 317 | if ((*ppCtx)->iFrameBsSize < (iSlice0Len + (iMaxSliceNum - 1)*iSlice1Len)) { | ||
| 318 | return ENC_RETURN_MEMALLOCERR; | ||
| 319 | } | ||
| 320 | return ENC_RETURN_SUCCESS; | ||
| 321 | |||
| 322 | } | ||
| 292 | 323 | ||
| 293 | int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingParam, const int32_t iCountBsLen, | 324 | int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingParam, const int32_t iCountBsLen, |
| 294 | const int32_t iTargetSpatialBsSize) { | 325 | const int32_t iTargetSpatialBsSize) { |
| 295 | CMemoryAlign* pMa = NULL; | 326 | CMemoryAlign* pMa = NULL; |
| 296 | SWelsSvcCodingParam* pPara = NULL; | 327 | SWelsSvcCodingParam* pPara = NULL; |
| 297 | SSliceThreading* pSmt = NULL; | 328 | SSliceThreading* pSmt = NULL; |
| 298 | SWelsSliceBs* pSliceB = NULL; | ||
| 299 | uint8_t* pBsBase = NULL; | ||
| 300 | int32_t iNumSpatialLayers = 0; | 329 | int32_t iNumSpatialLayers = 0; |
| 301 | int32_t iThreadNum = 0; | 330 | int32_t iThreadNum = 0; |
| 302 | int32_t iIdx = 0; | 331 | int32_t iIdx = 0; |
| 303 | int32_t iSliceBsBufferSize = 0; | ||
| 304 | int16_t iMaxSliceNum = 1; | 332 | int16_t iMaxSliceNum = 1; |
| 305 | int32_t iReturn = ENC_RETURN_SUCCESS; | 333 | int32_t iReturn = ENC_RETURN_SUCCESS; |
| 334 | bool bDynamicSlice = false; | ||
| 335 | uint32_t uiMaxSliceSizeConstraint = 0; | ||
| 306 | 336 | ||
| 307 | if (NULL == ppCtx || NULL == pCodingParam || NULL == *ppCtx || iCountBsLen <= 0) | 337 | if (NULL == ppCtx || NULL == pCodingParam || NULL == *ppCtx || iCountBsLen <= 0) |
| 308 | return 1; | 338 | return 1; |
| @@ -342,6 +372,13 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 33 unmodified lines | |||
| 342 | pSmt->pSliceConsumeTime[iIdx] = NULL; | 372 | pSmt->pSliceConsumeTime[iIdx] = NULL; |
| 343 | pSmt->pSliceComplexRatio[iIdx] = NULL; | 373 | pSmt->pSliceComplexRatio[iIdx] = NULL; |
| 344 | } | 374 | } |
| 375 | |||
| 376 | if (pMso->uiSliceMode == SM_DYN_SLICE) { | ||
| 377 | bDynamicSlice = true; | ||
| 378 | if (uiMaxSliceSizeConstraint < pMso->sSliceArgument.uiSliceSizeConstraint) { | ||
| 379 | uiMaxSliceSizeConstraint = pMso->sSliceArgument.uiSliceSizeConstraint; | ||
| 380 | } | ||
| 381 | } | ||
| 345 | ++ iIdx; | 382 | ++ iIdx; |
| 346 | } | 383 | } |
| 347 | // NULL for pSliceConsumeTime[iIdx]: iIdx from iNumSpatialLayers to MAX_DEPENDENCY_LAYERS | 384 | // NULL for pSliceConsumeTime[iIdx]: iIdx from iNumSpatialLayers to MAX_DEPENDENCY_LAYERS |
| @@ -391,37 +428,25 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 43 unmodified lines | |||
| 391 | MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pReadySliceCodingEvent%d = 0x%p named(%s) ret%d err%d", iIdx, | 428 | MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pReadySliceCodingEvent%d = 0x%p named(%s) ret%d err%d", iIdx, |
| 392 | (void*)pSmt->pReadySliceCodingEvent[iIdx], name, err, errno); | 429 | (void*)pSmt->pReadySliceCodingEvent[iIdx], name, err, errno); |
| 393 | 430 | ||
| 431 | |||
| 432 | pSmt->pThreadBsBuffer[iIdx] = (uint8_t*)pMa->WelsMalloc (iTargetSpatialBsSize, "pSmt->pThreadBsBuffer"); | ||
| 433 | WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pThreadBsBuffer[iIdx]), FreeMemorySvc (ppCtx)) | ||
| 434 | |||
| 394 | ++ iIdx; | 435 | ++ iIdx; |
| 395 | } | 436 | } |
| 437 | for (; iIdx < MAX_THREADS_NUM; iIdx++) { | ||
| 438 | pSmt->pThreadBsBuffer[iIdx] = NULL; | ||
| 439 | } | ||
| 396 | 440 | ||
| 397 | WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace); | 441 | WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace); |
| 398 | err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name); | 442 | err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name); |
| 399 | MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno); | 443 | MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno); |
| 400 | 444 | ||
| 401 | (*ppCtx)->pSliceBs = (SWelsSliceBs*)pMa->WelsMalloc (sizeof (SWelsSliceBs) * iMaxSliceNum, "pSliceBs"); | 445 | iReturn = SetMultiSliceBuffer (ppCtx, pMa, pSmt, iMaxSliceNum, |
| 402 | WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pSliceBs), FreeMemorySvc (ppCtx)) | 446 | iTargetSpatialBsSize, //TODO: may use uiMaxSliceSizeConstraint<<1 when bDynamicSlice, but need more twist |
| 403 | 447 | iCountBsLen, | |
| 404 | pBsBase = (*ppCtx)->pFrameBs + iCountBsLen; | 448 | bDynamicSlice); |
| 405 | pSliceB = (*ppCtx)->pSliceBs; | 449 | WELS_VERIFY_RETURN_PROC_IF (iReturn, (ENC_RETURN_SUCCESS != iReturn), FreeMemorySvc (ppCtx)) |
| 406 | iSliceBsBufferSize = iTargetSpatialBsSize; | ||
| 407 | iIdx = 0; | ||
| 408 | while (iIdx < iMaxSliceNum) { | ||
| 409 | pSliceB->pBsBuffer = (uint8_t*)pMa->WelsMalloc (iSliceBsBufferSize, "pSliceB->pBsBuffer"); | ||
| 410 | |||
| 411 | WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSliceB->pBsBuffer), FreeMemorySvc (ppCtx)) | ||
| 412 | pSliceB->uiSize = iSliceBsBufferSize; | ||
| 413 | |||
| 414 | if (iIdx > 0) { | ||
| 415 | pSliceB->pBs = pBsBase; | ||
| 416 | pSliceB->uiBsPos = 0; | ||
| 417 | pBsBase += iSliceBsBufferSize; | ||
| 418 | } else { | ||
| 419 | pSliceB->pBs = NULL; | ||
| 420 | pSliceB->uiBsPos = 0; | ||
| 421 | } | ||
| 422 | ++ pSliceB; | ||
| 423 | ++ iIdx; | ||
| 424 | } | ||
| 425 | 450 | ||
| 426 | iReturn = WelsMutexInit (&pSmt->mutexSliceNumUpdate); | 451 | iReturn = WelsMutexInit (&pSmt->mutexSliceNumUpdate); |
| 427 | WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx)) | 452 | WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx)) |
| @@ -486,14 +511,19 @@ void ReleaseMtResource (sWelsEncCtx** ppCtx) { · 58 unmodified lines | |||
| 486 | pSmt->pThreadPEncCtx = NULL; | 511 | pSmt->pThreadPEncCtx = NULL; |
| 487 | } | 512 | } |
| 488 | 513 | ||
| 514 | for (int i = 0; i < MAX_THREADS_NUM; i++) { | ||
| 515 | if (pSmt->pThreadBsBuffer[i]) { | ||
| 516 | pMa->WelsFree (pSmt->pThreadBsBuffer[i], "pSmt->pThreadBsBuffer"); | ||
| 517 | pSmt->pThreadBsBuffer[i] = NULL; | ||
| 518 | } | ||
| 519 | } | ||
| 520 | |||
| 489 | pSliceB = (*ppCtx)->pSliceBs; | 521 | pSliceB = (*ppCtx)->pSliceBs; |
| 490 | iIdx = 0; | 522 | iIdx = 0; |
| 491 | while (pSliceB != NULL && iIdx < uiSliceNum) { | 523 | while (pSliceB != NULL && iIdx < uiSliceNum) { |
| 492 | if (pSliceB->pBsBuffer) { | 524 | pSliceB->pBsBuffer = NULL; |
| 493 | pMa->WelsFree (pSliceB->pBsBuffer, "pSliceB->pBsBuffer"); | 525 | pSliceB->uiSize = 0; |
| 494 | pSliceB->pBsBuffer = NULL; | 526 | pSliceB->uiBsPos = 0; |
| 495 | pSliceB->uiSize = 0; | ||
| 496 | } | ||
| 497 | ++ iIdx; | 527 | ++ iIdx; |
| 498 | ++ pSliceB; | 528 | ++ pSliceB; |
| 499 | } | 529 | } |
| @@ -863,6 +893,7 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 363 unmodified lines | |||
| 863 | iEventIdx); | 893 | iEventIdx); |
| 864 | } | 894 | } |
| 865 | 895 | ||
| 896 | SetOneSliceBsBufferUnderMultithread (pEncPEncCtx, kiPartitionId, iSliceIdx); | ||
| 866 | pSlice = &pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx]; | 897 | pSlice = &pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx]; |
| 867 | pSliceBs = &pEncPEncCtx->pSliceBs[iSliceIdx]; | 898 | pSliceBs = &pEncPEncCtx->pSliceBs[iSliceIdx]; |
| 868 | 899 | ||
| @@ -1028,6 +1059,7 @@ int32_t FiredSliceThreads (sWelsEncCtx* pCtx, SSliceThreadPrivateData* pPriData, · 159 unmodified lines | |||
| 1028 | while (iIdx < kiEventCnt) { | 1059 | while (iIdx < kiEventCnt) { |
| 1029 | pPriData[iIdx].pLayerBs = pLbi; | 1060 | pPriData[iIdx].pLayerBs = pLbi; |
| 1030 | pPriData[iIdx].iSliceIndex = iIdx; | 1061 | pPriData[iIdx].iSliceIndex = iIdx; |
| 1062 | SetOneSliceBsBufferUnderMultithread (pCtx, iIdx, iIdx); | ||
| 1031 | if (pEventsList[iIdx]) | 1063 | if (pEventsList[iIdx]) |
| 1032 | WelsEventSignal (&pEventsList[iIdx]); | 1064 | WelsEventSignal (&pEventsList[iIdx]); |
| 1033 | if (pMasterEventsList[iIdx]) | 1065 | if (pMasterEventsList[iIdx]) |
| @@ -1178,5 +1210,9 @@ void TrackSliceConsumeTime (sWelsEncCtx* pCtx, int32_t* pDidList, const int32_t · 144 unmodified lines | |||
| 1178 | } | 1210 | } |
| 1179 | #endif//#if defined(MT_DEBUG) | 1211 | #endif//#if defined(MT_DEBUG) |
| 1180 | 1212 | ||
| 1213 | void SetOneSliceBsBufferUnderMultithread (sWelsEncCtx* pCtx, const int32_t kiThreadIdx, const int32_t iSliceIdx) { | ||
| 1214 | pCtx->pSliceBs[iSliceIdx].pBsBuffer = pCtx->pSliceThreading->pThreadBsBuffer[kiThreadIdx]; | ||
| 1215 | pCtx->pSliceBs[iSliceIdx].uiBsPos = 0; | ||
| 1216 | } | ||
| 1181 | } | 1217 | } |
| 1182 | 1218 | ||