From a32585779058f574ead4ae7ecdb63a783684f5fb Mon Sep 17 00:00:00 2001 From: Nana Zhang Date: Mon, 4 Nov 2024 09:51:29 +0000 Subject: [PATCH 1/2] Remove memory blocking for decoder Signed-off-by: Nana Zhang --- .../src/mfx_c2_decoder_component.cpp | 24 ++----------- c2_utils/include/mfx_frame_pool_allocator.h | 1 - .../include/mfx_va_frame_pool_allocator.h | 12 +------ c2_utils/src/mfx_va_frame_pool_allocator.cpp | 36 +++++++------------ 4 files changed, 17 insertions(+), 56 deletions(-) diff --git a/c2_components/src/mfx_c2_decoder_component.cpp b/c2_components/src/mfx_c2_decoder_component.cpp index 0ad2f3a1..55c79f1f 100755 --- a/c2_components/src/mfx_c2_decoder_component.cpp +++ b/c2_components/src/mfx_c2_decoder_component.cpp @@ -1814,27 +1814,9 @@ c2_status_t MfxC2DecoderComponent::AllocateC2Block(uint32_t width, uint32_t heig C2MemoryUsage mem_usage = {m_consumerUsage, C2AndroidMemoryUsage::HW_CODEC_WRITE}; res = m_c2Allocator->fetchGraphicBlock(width, height, MfxFourCCToGralloc(fourcc), mem_usage, out_block); - if (res == C2_OK) { - auto hndl_deleter = [](native_handle_t *hndl) { - native_handle_delete(hndl); - hndl = nullptr; - }; - - std::unique_ptr hndl( - android::UnwrapNativeCodec2GrallocHandle((*out_block)->handle()), hndl_deleter); - - uint64_t id; - if (C2_OK != MfxGrallocInstance::getInstance()->GetBackingStore(hndl.get(), &id)) - return C2_CORRUPTED; - if(!m_vppConversion) { - if (m_allocator && !m_allocator->InCache(id)) { - res = C2_BLOCKING; - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - // If always fetch a nocached block, check if width or height have changed - // compare to when it was initialized. - MFX_DEBUG_TRACE_STREAM("fetchGraphicBlock a nocached block, please retune output blocks. id = " << id); - } - } + if (res == C2_BLOCKING) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + MFX_DEBUG_TRACE_MSG("fetchGraphicBlock blocking, wait and try again"); } } else if (m_mfxVideoParams.IOPattern == MFX_IOPATTERN_OUT_SYSTEM_MEMORY) { C2MemoryUsage mem_usage = {m_consumerUsage, C2MemoryUsage::CPU_WRITE}; diff --git a/c2_utils/include/mfx_frame_pool_allocator.h b/c2_utils/include/mfx_frame_pool_allocator.h index 9785e2f5..ab579aac 100755 --- a/c2_utils/include/mfx_frame_pool_allocator.h +++ b/c2_utils/include/mfx_frame_pool_allocator.h @@ -29,7 +29,6 @@ class MfxFramePoolAllocator virtual void SetC2Allocator(std::shared_ptr c2_allocator) = 0; virtual std::shared_ptr Alloc() = 0; virtual void Reset() = 0; - virtual bool InCache(uint64_t id) = 0; virtual void SetBufferCount(unsigned int cnt) = 0; virtual void SetConsumerUsage(uint64_t usage) = 0; diff --git a/c2_utils/include/mfx_va_frame_pool_allocator.h b/c2_utils/include/mfx_va_frame_pool_allocator.h index 2ed43833..459728fd 100755 --- a/c2_utils/include/mfx_va_frame_pool_allocator.h +++ b/c2_utils/include/mfx_va_frame_pool_allocator.h @@ -62,8 +62,8 @@ class MfxVaFramePoolAllocator : public MfxVaFrameAllocator, public MfxFramePoolA { MFX_DEBUG_TRACE_FUNC; m_pool = std::make_unique>(); - m_cachedBufferId.clear(); } + virtual void SetBufferCount(unsigned int cnt) override { MFX_DEBUG_TRACE_FUNC; @@ -76,14 +76,6 @@ class MfxVaFramePoolAllocator : public MfxVaFrameAllocator, public MfxFramePoolA m_consumerUsage = usage; } - bool InCache(uint64_t id) { - auto it = m_cachedBufferId.find(id); - if (it == m_cachedBufferId.end()){ - return false; - } - - return true; - } private: virtual mfxStatus AllocFrames(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response) override; @@ -96,8 +88,6 @@ class MfxVaFramePoolAllocator : public MfxVaFrameAllocator, public MfxFramePoolA std::unique_ptr> m_pool; - std::map m_cachedBufferId; - unsigned int m_uSuggestBufferCnt = 0; uint64_t m_consumerUsage = 0; diff --git a/c2_utils/src/mfx_va_frame_pool_allocator.cpp b/c2_utils/src/mfx_va_frame_pool_allocator.cpp index d8aae064..7c3717ed 100755 --- a/c2_utils/src/mfx_va_frame_pool_allocator.cpp +++ b/c2_utils/src/mfx_va_frame_pool_allocator.cpp @@ -48,18 +48,20 @@ mfxStatus MfxVaFramePoolAllocator::AllocFrames(mfxFrameAllocRequest *request, // output->maxDequeueBuffers = numOutputSlots + reorderDepth.value(0) + kRenderingDepth(3); // numOutputSlots = outputDelayValue(output_delay_) + kSmoothnessFactor(4); // buffers_count = output_delay_ + kSmoothnessFactor(4) + kRenderingDepth(3) - int max_buffers = m_uSuggestBufferCnt + kSmoothnessFactor + kRenderingDepth; + // int max_buffers = m_uSuggestBufferCnt + kSmoothnessFactor + kRenderingDepth; int min_buffers = MFX_MAX(request->NumFrameSuggested, MFX_MAX(request->NumFrameMin, 1)); - int opt_buffers = max_buffers; // optimal buffer count for better performance - if (max_buffers < request->NumFrameMin) return MFX_ERR_MEMORY_ALLOC; - - // For 4K or 8K videos, limit buffer count to save memory - if (IS_8K_VIDEO(request->Info.Width, request->Info.Height)) { - opt_buffers = MFX_MAX(min_buffers, 4) + kSmoothnessFactor; - } else if (IS_4K_VIDEO(request->Info.Width, request->Info.Height)) { - opt_buffers = MFX_MAX(min_buffers, 4) + kSmoothnessFactor + kRenderingDepth; - } - MFX_DEBUG_TRACE_I32(max_buffers); + int opt_buffers = min_buffers; + // int opt_buffers = max_buffers; // optimal buffer count for better performance + // if (max_buffers < request->NumFrameMin) return MFX_ERR_MEMORY_ALLOC; + + // // For 4K or 8K videos, limit buffer count to save memory + // if (IS_8K_VIDEO(request->Info.Width, request->Info.Height)) { + // opt_buffers = MFX_MAX(min_buffers, 4) + kSmoothnessFactor; + // } else if (IS_4K_VIDEO(request->Info.Width, request->Info.Height)) { + // opt_buffers = MFX_MAX(min_buffers, 4) + kSmoothnessFactor + kRenderingDepth; + // } + // MFX_DEBUG_TRACE_I32(max_buffers); + MFX_DEBUG_TRACE_I32(opt_buffers); MFX_DEBUG_TRACE_I32(request->NumFrameMin); MFX_DEBUG_TRACE_I32(request->NumFrameSuggested); @@ -113,19 +115,7 @@ mfxStatus MfxVaFramePoolAllocator::AllocFrames(mfxFrameAllocRequest *request, } while(res == C2_BLOCKING); if (res != C2_OK || !new_block) break; - uint64_t id; native_handle_t *hndl = android::UnwrapNativeCodec2GrallocHandle(new_block->handle()); - if (C2_OK != MfxGrallocInstance::getInstance()->GetBackingStore(hndl, &id)) { - mfx_res = MFX_ERR_INVALID_HANDLE; - break; - } - - m_cachedBufferId.emplace(id, i); - // if (C2_OK != res) { //TODO dead code not need. - // native_handle_delete(hndl); - // mfx_res = MFX_ERR_MEMORY_ALLOC; - // break; - // } // deep copy to have unique_ptr as m_pool required unique_ptr std::unique_ptr unique_block = std::make_unique(*new_block); From 4ac75e6429d1def841bf2313a99870f796720125 Mon Sep 17 00:00:00 2001 From: Nana Zhang Date: Wed, 6 Nov 2024 03:21:28 +0000 Subject: [PATCH 2/2] Revert buffer allocated number Signed-off-by: Nana Zhang --- c2_utils/src/mfx_va_frame_pool_allocator.cpp | 25 ++++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/c2_utils/src/mfx_va_frame_pool_allocator.cpp b/c2_utils/src/mfx_va_frame_pool_allocator.cpp index 7c3717ed..40eee4cb 100755 --- a/c2_utils/src/mfx_va_frame_pool_allocator.cpp +++ b/c2_utils/src/mfx_va_frame_pool_allocator.cpp @@ -48,20 +48,19 @@ mfxStatus MfxVaFramePoolAllocator::AllocFrames(mfxFrameAllocRequest *request, // output->maxDequeueBuffers = numOutputSlots + reorderDepth.value(0) + kRenderingDepth(3); // numOutputSlots = outputDelayValue(output_delay_) + kSmoothnessFactor(4); // buffers_count = output_delay_ + kSmoothnessFactor(4) + kRenderingDepth(3) - // int max_buffers = m_uSuggestBufferCnt + kSmoothnessFactor + kRenderingDepth; + int max_buffers = m_uSuggestBufferCnt + kSmoothnessFactor + kRenderingDepth; int min_buffers = MFX_MAX(request->NumFrameSuggested, MFX_MAX(request->NumFrameMin, 1)); - int opt_buffers = min_buffers; - // int opt_buffers = max_buffers; // optimal buffer count for better performance - // if (max_buffers < request->NumFrameMin) return MFX_ERR_MEMORY_ALLOC; - - // // For 4K or 8K videos, limit buffer count to save memory - // if (IS_8K_VIDEO(request->Info.Width, request->Info.Height)) { - // opt_buffers = MFX_MAX(min_buffers, 4) + kSmoothnessFactor; - // } else if (IS_4K_VIDEO(request->Info.Width, request->Info.Height)) { - // opt_buffers = MFX_MAX(min_buffers, 4) + kSmoothnessFactor + kRenderingDepth; - // } - // MFX_DEBUG_TRACE_I32(max_buffers); - + // int opt_buffers = min_buffers; + int opt_buffers = max_buffers; // optimal buffer count for better performance + if (max_buffers < request->NumFrameMin) return MFX_ERR_MEMORY_ALLOC; + + // For 4K or 8K videos, limit buffer count to save memory + if (IS_8K_VIDEO(request->Info.Width, request->Info.Height)) { + opt_buffers = MFX_MAX(min_buffers, 4) + kSmoothnessFactor; + } else if (IS_4K_VIDEO(request->Info.Width, request->Info.Height)) { + opt_buffers = MFX_MAX(min_buffers, 4) + kSmoothnessFactor + kRenderingDepth; + } + MFX_DEBUG_TRACE_I32(max_buffers); MFX_DEBUG_TRACE_I32(opt_buffers); MFX_DEBUG_TRACE_I32(request->NumFrameMin); MFX_DEBUG_TRACE_I32(request->NumFrameSuggested);