History

put pSliceBsBuffer to thread buffer rather than per slice, so as to save memory usage

Sijia Chen committed 11 years ago

Commit 43ca6db1 parent bee0d7d4 files · +71 −33Browse files

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

Unified · Split

codec/encoder/core/src/slice_multi_threading.cpp+68−32
@@ -289,20 +289,50 @@ void DynamicAdjustSlicing (sWelsEncCtx* pCtx, · 288 unmodified lines
289289 }
290290 }
291291
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+}
292323
293324 int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingParam, const int32_t iCountBsLen,
294325 const int32_t iTargetSpatialBsSize) {
295326 CMemoryAlign* pMa = NULL;
296327 SWelsSvcCodingParam* pPara = NULL;
297328 SSliceThreading* pSmt = NULL;
298− SWelsSliceBs* pSliceB = NULL;
299− uint8_t* pBsBase = NULL;
300329 int32_t iNumSpatialLayers = 0;
301330 int32_t iThreadNum = 0;
302331 int32_t iIdx = 0;
303− int32_t iSliceBsBufferSize = 0;
304332 int16_t iMaxSliceNum = 1;
305333 int32_t iReturn = ENC_RETURN_SUCCESS;
334+ bool bDynamicSlice = false;
335+ uint32_t uiMaxSliceSizeConstraint = 0;
306336
307337 if (NULL == ppCtx || NULL == pCodingParam || NULL == *ppCtx || iCountBsLen <= 0)
308338 return 1;
@@ -342,6 +372,13 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 33 unmodified lines
342372 pSmt->pSliceConsumeTime[iIdx] = NULL;
343373 pSmt->pSliceComplexRatio[iIdx] = NULL;
344374 }
375+
376+ if (pMso->uiSliceMode == SM_DYN_SLICE) {
377+ bDynamicSlice = true;
378+ if (uiMaxSliceSizeConstraint < pMso->sSliceArgument.uiSliceSizeConstraint) {
379+ uiMaxSliceSizeConstraint = pMso->sSliceArgument.uiSliceSizeConstraint;
380+ }
381+ }
345382 ++ iIdx;
346383 }
347384 // 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
391428 MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pReadySliceCodingEvent%d = 0x%p named(%s) ret%d err%d", iIdx,
392429 (void*)pSmt->pReadySliceCodingEvent[iIdx], name, err, errno);
393430
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+
394435 ++ iIdx;
395436 }
437+ for (; iIdx < MAX_THREADS_NUM; iIdx++) {
438+ pSmt->pThreadBsBuffer[iIdx] = NULL;
439+ }
396440
397441 WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace);
398442 err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name);
399443 MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno);
400444
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))
425450
426451 iReturn = WelsMutexInit (&pSmt->mutexSliceNumUpdate);
427452 WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx))
@@ -486,14 +511,19 @@ void ReleaseMtResource (sWelsEncCtx** ppCtx) { · 58 unmodified lines
486511 pSmt->pThreadPEncCtx = NULL;
487512 }
488513
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+
489521 pSliceB = (*ppCtx)->pSliceBs;
490522 iIdx = 0;
491523 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;
497527 ++ iIdx;
498528 ++ pSliceB;
499529 }
@@ -863,6 +893,7 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 363 unmodified lines
863893 iEventIdx);
864894 }
865895
896+ SetOneSliceBsBufferUnderMultithread (pEncPEncCtx, kiPartitionId, iSliceIdx);
866897 pSlice = &pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx];
867898 pSliceBs = &pEncPEncCtx->pSliceBs[iSliceIdx];
868899
@@ -1028,6 +1059,7 @@ int32_t FiredSliceThreads (sWelsEncCtx* pCtx, SSliceThreadPrivateData* pPriData, · 159 unmodified lines
10281059 while (iIdx < kiEventCnt) {
10291060 pPriData[iIdx].pLayerBs = pLbi;
10301061 pPriData[iIdx].iSliceIndex = iIdx;
1062+ SetOneSliceBsBufferUnderMultithread (pCtx, iIdx, iIdx);
10311063 if (pEventsList[iIdx])
10321064 WelsEventSignal (&pEventsList[iIdx]);
10331065 if (pMasterEventsList[iIdx])
@@ -1178,5 +1210,9 @@ void TrackSliceConsumeTime (sWelsEncCtx* pCtx, int32_t* pDidList, const int32_t · 144 unmodified lines
11781210 }
11791211 #endif//#if defined(MT_DEBUG)
11801212
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+}
11811217 }
11821218

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