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 | 324 | int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingParam, const int32_t iCountBsLen, |
| 294 | 325 | const int32_t iTargetSpatialBsSize) { |
| 295 | 326 | CMemoryAlign* pMa = NULL; |
| 296 | 327 | SWelsSvcCodingParam* pPara = NULL; |
| 297 | 328 | SSliceThreading* pSmt = NULL; |
| 298 | − SWelsSliceBs* pSliceB = NULL; | |
| 299 | − uint8_t* pBsBase = NULL; | |
| 300 | 329 | int32_t iNumSpatialLayers = 0; |
| 301 | 330 | int32_t iThreadNum = 0; |
| 302 | 331 | int32_t iIdx = 0; |
| 303 | − int32_t iSliceBsBufferSize = 0; | |
| 304 | 332 | int16_t iMaxSliceNum = 1; |
| 305 | 333 | int32_t iReturn = ENC_RETURN_SUCCESS; |
| 334 | + bool bDynamicSlice = false; | |
| 335 | + uint32_t uiMaxSliceSizeConstraint = 0; | |
| 306 | 336 | |
| 307 | 337 | if (NULL == ppCtx || NULL == pCodingParam || NULL == *ppCtx || iCountBsLen <= 0) |
| 308 | 338 | return 1; |
| @@ -342,6 +372,13 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 33 unmodified lines | ||
| 342 | 372 | pSmt->pSliceConsumeTime[iIdx] = NULL; |
| 343 | 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 | 382 | ++ iIdx; |
| 346 | 383 | } |
| 347 | 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 | 428 | MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pReadySliceCodingEvent%d = 0x%p named(%s) ret%d err%d", iIdx, |
| 392 | 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 | 435 | ++ iIdx; |
| 395 | 436 | } |
| 437 | + for (; iIdx < MAX_THREADS_NUM; iIdx++) { | |
| 438 | + pSmt->pThreadBsBuffer[iIdx] = NULL; | |
| 439 | + } | |
| 396 | 440 | |
| 397 | 441 | WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace); |
| 398 | 442 | err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name); |
| 399 | 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"); | |
| 402 | − WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pSliceBs), FreeMemorySvc (ppCtx)) | |
| 403 | − | |
| 404 | − pBsBase = (*ppCtx)->pFrameBs + iCountBsLen; | |
| 405 | − pSliceB = (*ppCtx)->pSliceBs; | |
| 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 | − } | |
| 445 | + iReturn = SetMultiSliceBuffer (ppCtx, pMa, pSmt, iMaxSliceNum, | |
| 446 | + iTargetSpatialBsSize, //TODO: may use uiMaxSliceSizeConstraint<<1 when bDynamicSlice, but need more twist | |
| 447 | + iCountBsLen, | |
| 448 | + bDynamicSlice); | |
| 449 | + WELS_VERIFY_RETURN_PROC_IF (iReturn, (ENC_RETURN_SUCCESS != iReturn), FreeMemorySvc (ppCtx)) | |
| 425 | 450 | |
| 426 | 451 | iReturn = WelsMutexInit (&pSmt->mutexSliceNumUpdate); |
| 427 | 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 | 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 | 521 | pSliceB = (*ppCtx)->pSliceBs; |
| 490 | 522 | iIdx = 0; |
| 491 | 523 | while (pSliceB != NULL && iIdx < uiSliceNum) { |
| 492 | − if (pSliceB->pBsBuffer) { | |
| 493 | − pMa->WelsFree (pSliceB->pBsBuffer, "pSliceB->pBsBuffer"); | |
| 494 | − pSliceB->pBsBuffer = NULL; | |
| 495 | − pSliceB->uiSize = 0; | |
| 496 | − } | |
| 524 | + pSliceB->pBsBuffer = NULL; | |
| 525 | + pSliceB->uiSize = 0; | |
| 526 | + pSliceB->uiBsPos = 0; | |
| 497 | 527 | ++ iIdx; |
| 498 | 528 | ++ pSliceB; |
| 499 | 529 | } |
| @@ -863,6 +893,7 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 363 unmodified lines | ||
| 863 | 893 | iEventIdx); |
| 864 | 894 | } |
| 865 | 895 | |
| 896 | + SetOneSliceBsBufferUnderMultithread (pEncPEncCtx, kiPartitionId, iSliceIdx); | |
| 866 | 897 | pSlice = &pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx]; |
| 867 | 898 | pSliceBs = &pEncPEncCtx->pSliceBs[iSliceIdx]; |
| 868 | 899 | |
| @@ -1028,6 +1059,7 @@ int32_t FiredSliceThreads (sWelsEncCtx* pCtx, SSliceThreadPrivateData* pPriData, · 159 unmodified lines | ||
| 1028 | 1059 | while (iIdx < kiEventCnt) { |
| 1029 | 1060 | pPriData[iIdx].pLayerBs = pLbi; |
| 1030 | 1061 | pPriData[iIdx].iSliceIndex = iIdx; |
| 1062 | + SetOneSliceBsBufferUnderMultithread (pCtx, iIdx, iIdx); | |
| 1031 | 1063 | if (pEventsList[iIdx]) |
| 1032 | 1064 | WelsEventSignal (&pEventsList[iIdx]); |
| 1033 | 1065 | if (pMasterEventsList[iIdx]) |
| @@ -1178,5 +1210,9 @@ void TrackSliceConsumeTime (sWelsEncCtx* pCtx, int32_t* pDidList, const int32_t · 144 unmodified lines | ||
| 1178 | 1210 | } |
| 1179 | 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 | |