From 53b96fbbec95609eb15a6d378905acc024800d4e Mon Sep 17 00:00:00 2001 From: Dmitry Ermilov Date: Thu, 23 Sep 2021 19:06:21 +0300 Subject: [PATCH] Readonly frame access when file dumping Signed-off-by: Dmitry Ermilov --- .../sample_common/include/base_allocator.h | 85 +++++++++++++++++++ .../sample_common/include/d3d11_allocator.h | 85 ------------------- .../sample_common/src/general_allocator.cpp | 4 +- samples/sample_decode/src/pipeline_decode.cpp | 4 +- 4 files changed, 89 insertions(+), 89 deletions(-) diff --git a/samples/sample_common/include/base_allocator.h b/samples/sample_common/include/base_allocator.h index f01388e612..5e13a438d5 100644 --- a/samples/sample_common/include/base_allocator.h +++ b/samples/sample_common/include/base_allocator.h @@ -197,5 +197,90 @@ class MFXBufferAllocator : public mfxBufferAllocator static mfxStatus MFX_CDECL Free_(mfxHDL pthis, mfxMemId mid); }; +//application can provide either generic mid from surface or this wrapper +//wrapper distinguishes from generic mid by highest 1 bit +//if it set then remained pointer points to extended structure of memid +//64 bits system layout +/*----+-----------------------------------------------------------+ +|b63=1|63 bits remained for pointer to extended structure of memid| +|b63=0|63 bits from original mfxMemId | ++-----+----------------------------------------------------------*/ +//32 bits system layout +/*--+---+--------------------------------------------+ +|b31=1|31 bits remained for pointer to extended memid| +|b31=0|31 bits remained for surface pointer | ++---+---+-------------------------------------------*/ +class MFXReadWriteMid +{ + static const uintptr_t bits_offset = std::numeric_limits::digits - 1; + static const uintptr_t clear_mask = ~((uintptr_t)1 << bits_offset); +public: + enum + { + //if flag not set it means that read and write + not_set = 0, + reuse = 1, + read = 2, + write = 4, + }; + + //here mfxmemid might be as MFXReadWriteMid or mfxMemId memid + MFXReadWriteMid(mfxMemId mid, mfxU8 flag = not_set) + { + //setup mid + m_mid_to_report = (mfxMemId)((uintptr_t)&m_mid | ((uintptr_t)1 << bits_offset)); + if (0 != ((uintptr_t)mid >> bits_offset)) + { + //it points to extended structure + mfxMedIdEx* pMemIdExt = reinterpret_cast((uintptr_t)mid & clear_mask); + m_mid.pId = pMemIdExt->pId; + if (reuse == flag) + { + m_mid.read_write = pMemIdExt->read_write; + } + else + { + m_mid.read_write = flag; + } + } + else + { + m_mid.pId = mid; + if (reuse == flag) + m_mid.read_write = not_set; + else + m_mid.read_write = flag; + } + + } + bool isRead() const + { + return 0 != (m_mid.read_write & read) || !m_mid.read_write; + } + bool isWrite() const + { + return 0 != (m_mid.read_write & write) || !m_mid.read_write; + } + /// returns original memid without read write flags + mfxMemId raw() const + { + return m_mid.pId; + } + operator mfxMemId() const + { + return m_mid_to_report; + } + +private: + struct mfxMedIdEx + { + mfxMemId pId; + mfxU8 read_write; + }; + + mfxMedIdEx m_mid; + mfxMemId m_mid_to_report; +}; + #endif // __BASE_ALLOCATOR_H__ diff --git a/samples/sample_common/include/d3d11_allocator.h b/samples/sample_common/include/d3d11_allocator.h index d7af7b03ca..aa9615bd6b 100644 --- a/samples/sample_common/include/d3d11_allocator.h +++ b/samples/sample_common/include/d3d11_allocator.h @@ -27,91 +27,6 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include // for uintptr_t on Linux #endif -//application can provide either generic mid from surface or this wrapper -//wrapper distinguishes from generic mid by highest 1 bit -//if it set then remained pointer points to extended structure of memid -//64 bits system layout -/*----+-----------------------------------------------------------+ -|b63=1|63 bits remained for pointer to extended structure of memid| -|b63=0|63 bits from original mfxMemId | -+-----+----------------------------------------------------------*/ -//32 bits system layout -/*--+---+--------------------------------------------+ -|b31=1|31 bits remained for pointer to extended memid| -|b31=0|31 bits remained for surface pointer | -+---+---+-------------------------------------------*/ -//#pragma warning (disable:4293) -class MFXReadWriteMid -{ - static const uintptr_t bits_offset = std::numeric_limits::digits - 1; - static const uintptr_t clear_mask = ~((uintptr_t)1 << bits_offset); -public: - enum - { - //if flag not set it means that read and write - not_set = 0, - reuse = 1, - read = 2, - write = 4, - }; - //here mfxmemid might be as MFXReadWriteMid or mfxMemId memid - MFXReadWriteMid(mfxMemId mid, mfxU8 flag = not_set) - { - //setup mid - m_mid_to_report = (mfxMemId)((uintptr_t)&m_mid | ((uintptr_t)1 << bits_offset)); - if (0 != ((uintptr_t)mid >> bits_offset)) - { - //it points to extended structure - mfxMedIdEx * pMemIdExt = reinterpret_cast((uintptr_t)mid & clear_mask); - m_mid.pId = pMemIdExt->pId; - if (reuse == flag) - { - m_mid.read_write = pMemIdExt->read_write; - } - else - { - m_mid.read_write = flag; - } - } - else - { - m_mid.pId = mid; - if (reuse == flag) - m_mid.read_write = not_set; - else - m_mid.read_write = flag; - } - - } - bool isRead() const - { - return 0 != (m_mid.read_write & read) || !m_mid.read_write; - } - bool isWrite() const - { - return 0 != (m_mid.read_write & write) || !m_mid.read_write; - } - /// returns original memid without read write flags - mfxMemId raw() const - { - return m_mid.pId; - } - operator mfxMemId() const - { - return m_mid_to_report; - } - -private: - struct mfxMedIdEx - { - mfxMemId pId; - mfxU8 read_write; - }; - - mfxMedIdEx m_mid; - mfxMemId m_mid_to_report; -}; - #if (defined(_WIN32) || defined(_WIN64)) #include diff --git a/samples/sample_common/src/general_allocator.cpp b/samples/sample_common/src/general_allocator.cpp index 258edfe709..4ac872e92e 100644 --- a/samples/sample_common/src/general_allocator.cpp +++ b/samples/sample_common/src/general_allocator.cpp @@ -94,7 +94,7 @@ mfxStatus GeneralAllocator::LockFrame(mfxMemId mid, mfxFrameData *ptr) if (isD3DMid(mid) && m_D3DAllocator.get()) return m_D3DAllocator.get()->Lock(m_D3DAllocator.get(), mid, ptr); else - return m_SYSAllocator.get()->Lock(m_SYSAllocator.get(),mid, ptr); + return m_SYSAllocator.get()->Lock(m_SYSAllocator.get(),mid, ptr); } mfxStatus GeneralAllocator::UnlockFrame(mfxMemId mid, mfxFrameData *ptr) { @@ -164,7 +164,7 @@ void GeneralAllocator::StoreFrameMids(bool isD3DFrames, mfxFrameAllocResponse bool GeneralAllocator::isD3DMid(mfxHDL mid) { std::map::iterator it; - it = m_Mids.find(mid); + it = m_Mids.find(MFXReadWriteMid(mid).raw()); if (it == m_Mids.end()) return false; // sys mem allocator will check validity of mid further else diff --git a/samples/sample_decode/src/pipeline_decode.cpp b/samples/sample_decode/src/pipeline_decode.cpp index e1c28cc769..93e4bf6a88 100644 --- a/samples/sample_decode/src/pipeline_decode.cpp +++ b/samples/sample_decode/src/pipeline_decode.cpp @@ -1614,11 +1614,11 @@ mfxStatus CDecodingPipeline::DeliverOutput(mfxFrameSurface1* frame) if (m_bExternalAlloc) { if (m_eWorkMode == MODE_FILE_DUMP) { - res = m_pGeneralAllocator->Lock(m_pGeneralAllocator->pthis, frame->Data.MemId, &(frame->Data)); + res = m_pGeneralAllocator->Lock(m_pGeneralAllocator->pthis, MFXReadWriteMid(frame->Data.MemId, MFXReadWriteMid::read), &(frame->Data)); if (MFX_ERR_NONE == res) { res = m_bOutI420 ? m_FileWriter.WriteNextFrameI420(frame) : m_FileWriter.WriteNextFrame(frame); - sts = m_pGeneralAllocator->Unlock(m_pGeneralAllocator->pthis, frame->Data.MemId, &(frame->Data)); + sts = m_pGeneralAllocator->Unlock(m_pGeneralAllocator->pthis, MFXReadWriteMid(frame->Data.MemId, MFXReadWriteMid::read), &(frame->Data)); } if ((MFX_ERR_NONE == res) && (MFX_ERR_NONE != sts)) { res = sts;