1 // Copyright (c) 2017-2020 Intel Corporation 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in all 11 // copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 // SOFTWARE. 20 21 #ifndef __LIBMFX_CORE_H__ 22 #define __LIBMFX_CORE_H__ 23 24 #include <map> 25 26 #include "umc_mutex.h" 27 #include "libmfx_allocator.h" 28 #include "mfxvideo.h" 29 #include "mfxvideo++int.h" 30 #include "mfx_ext_buffers.h" 31 #include "fast_copy.h" 32 #include "libmfx_core_interface.h" 33 34 #include <memory> 35 36 37 class mfx_UMC_FrameAllocator; 38 39 // Virtual table size for CommonCORE should be considered fixed. 40 // Otherwise binary compatibility with already released plugins would be broken. 41 42 class CommonCORE : public VideoCORE 43 { 44 public: 45 46 friend class FactoryCORE; 47 ~CommonCORE()48 virtual ~CommonCORE() override { Close(); } 49 50 virtual mfxStatus GetHandle(mfxHandleType type, mfxHDL *handle) override; 51 virtual mfxStatus SetHandle(mfxHandleType type, mfxHDL handle) override; 52 53 virtual mfxStatus SetBufferAllocator(mfxBufferAllocator *) override; 54 virtual mfxStatus SetFrameAllocator(mfxFrameAllocator *allocator) override; 55 56 // Utility functions for memory access 57 virtual mfxStatus AllocBuffer(mfxU32 nbytes, mfxU16 type, mfxMemId *mid) override; 58 virtual mfxStatus LockBuffer(mfxMemId mid, mfxU8 **ptr) override; 59 virtual mfxStatus UnlockBuffer(mfxMemId mid) override; 60 virtual mfxStatus FreeBuffer(mfxMemId mid) override; 61 62 // DEPRECATED CheckHandle()63 virtual mfxStatus CheckHandle() override { return MFX_ERR_NONE; } 64 65 virtual mfxStatus GetFrameHDL(mfxMemId mid, mfxHDL *handle, bool ExtendedSearch = true) override; 66 67 virtual mfxStatus AllocFrames(mfxFrameAllocRequest *request, 68 mfxFrameAllocResponse *response, bool isNeedCopy = true) override; 69 70 virtual mfxStatus AllocFrames(mfxFrameAllocRequest *request, 71 mfxFrameAllocResponse *response, 72 mfxFrameSurface1 **pOpaqueSurface, 73 mfxU32 NumOpaqueSurface) override; 74 75 virtual mfxStatus LockFrame(mfxMemId mid, mfxFrameData *ptr) override; 76 virtual mfxStatus UnlockFrame(mfxMemId mid, mfxFrameData *ptr = nullptr) override; 77 virtual mfxStatus FreeFrames(mfxFrameAllocResponse *response, bool ExtendedSearch = true) override; 78 79 virtual mfxStatus LockExternalFrame(mfxMemId mid, mfxFrameData *ptr, bool ExtendedSearch = true) override; 80 virtual mfxStatus GetExternalFrameHDL(mfxMemId mid, mfxHDL *handle, bool ExtendedSearch = true) override; 81 virtual mfxStatus UnlockExternalFrame(mfxMemId mid, mfxFrameData *ptr=0, bool ExtendedSearch = true) override; 82 83 virtual mfxMemId MapIdx(mfxMemId mid) override; 84 85 // Get original Surface corresponding to OpaqueSurface 86 virtual mfxFrameSurface1* GetNativeSurface(mfxFrameSurface1 *pOpqSurface, bool ExtendedSearch = true) override; 87 // Get OpaqueSurface corresponding to Original 88 virtual mfxFrameSurface1* GetOpaqSurface(mfxMemId mid, bool ExtendedSearch = true) override; 89 90 // Increment Surface lock caring about opaq 91 virtual mfxStatus IncreaseReference(mfxFrameData *ptr, bool ExtendedSearch = true) override; 92 // Decrement Surface lock caring about opaq 93 virtual mfxStatus DecreaseReference(mfxFrameData *ptr, bool ExtendedSearch = true) override; 94 95 // no care about surface, opaq and all round. Just increasing reference 96 virtual mfxStatus IncreasePureReference(mfxU16 &Locked) override; 97 // no care about surface, opaq and all round. Just decreasing reference 98 virtual mfxStatus DecreasePureReference(mfxU16 &Locked) override; 99 100 // Get Video Accelerator. GetVA(mfxHDL * phdl,mfxU16)101 virtual void GetVA(mfxHDL* phdl, mfxU16) override { *phdl = nullptr; } CreateVA(mfxVideoParam *,mfxFrameAllocRequest *,mfxFrameAllocResponse *,UMC::FrameAllocator *)102 virtual mfxStatus CreateVA(mfxVideoParam *, mfxFrameAllocRequest *, mfxFrameAllocResponse *, UMC::FrameAllocator *) override { MFX_RETURN(MFX_ERR_UNSUPPORTED); } 103 // Get the current working adapter's number GetAdapterNumber()104 virtual mfxU32 GetAdapterNumber() override { return 0; } 105 // GetPlatformType()106 virtual eMFXPlatform GetPlatformType() override { return MFX_PLATFORM_SOFTWARE; } 107 108 // Get Video Processing GetVideoProcessing(mfxHDL * phdl)109 virtual void GetVideoProcessing(mfxHDL* phdl) override { *phdl = 0; } CreateVideoProcessing(mfxVideoParam *)110 virtual mfxStatus CreateVideoProcessing(mfxVideoParam *) override { MFX_RETURN(MFX_ERR_UNSUPPORTED); } 111 112 // Get the current number of working threads GetNumWorkingThreads()113 virtual mfxU32 GetNumWorkingThreads() override { return m_numThreadsAvailable; } 114 virtual void INeedMoreThreadsInside(const void *pComponent) override; 115 116 virtual mfxStatus DoFastCopy(mfxFrameSurface1 *pDst, mfxFrameSurface1 *pSrc) override; 117 virtual mfxStatus DoFastCopyExtended(mfxFrameSurface1 *pDst, mfxFrameSurface1 *pSrc) override; 118 119 virtual mfxStatus DoFastCopyWrapper(mfxFrameSurface1 *pDst, mfxU16 dstMemType, mfxFrameSurface1 *pSrc, mfxU16 srcMemType) override; 120 121 // DEPRECATED IsFastCopyEnabled()122 virtual bool IsFastCopyEnabled() override { return true; } 123 124 virtual bool IsExternalFrameAllocator() const override; GetHWType()125 virtual eMFXHWType GetHWType() override { return MFX_HW_UNKNOWN; } 126 127 virtual mfxStatus CopyFrame(mfxFrameSurface1 *dst, mfxFrameSurface1 *src) override; 128 CopyBuffer(mfxU8 *,mfxU32,mfxFrameSurface1 *)129 virtual mfxStatus CopyBuffer(mfxU8 *, mfxU32, mfxFrameSurface1 *) override { MFX_RETURN(MFX_ERR_UNKNOWN); } 130 CopyFrameEx(mfxFrameSurface1 * pDst,mfxU16 dstMemType,mfxFrameSurface1 * pSrc,mfxU16 srcMemType)131 virtual mfxStatus CopyFrameEx(mfxFrameSurface1 *pDst, mfxU16 dstMemType, mfxFrameSurface1 *pSrc, mfxU16 srcMemType) override 132 { 133 return DoFastCopyWrapper(pDst, dstMemType, pSrc, srcMemType); 134 } 135 IsGuidSupported(const GUID,mfxVideoParam *,bool)136 virtual mfxStatus IsGuidSupported(const GUID, mfxVideoParam *, bool) override { return MFX_ERR_NONE; } 137 138 virtual bool CheckOpaqueRequest(mfxFrameAllocRequest *request, mfxFrameSurface1 **pOpaqueSurface, mfxU32 NumOpaqueSurface, bool ExtendedSearch = true) override; 139 GetVAType()140 virtual eMFXVAType GetVAType() const override { return MFX_HW_NO; } 141 142 virtual bool SetCoreId(mfxU32 Id) override; 143 virtual void* QueryCoreInterface(const MFX_GUID &guid) override; 144 GetSession()145 virtual mfxSession GetSession() override { return m_session; } 146 147 virtual void SetWrapper(void* pWrp) override; 148 149 virtual mfxU16 GetAutoAsyncDepth() override; 150 IsCompatibleForOpaq()151 virtual bool IsCompatibleForOpaq() override { return true; } 152 153 // keep frame response structure describing plug-in memory surfaces 154 void AddPluginAllocResponse(mfxFrameAllocResponse& response); 155 156 // get response which corresponds required conditions: same mids and number 157 mfxFrameAllocResponse* GetPluginAllocResponse(mfxFrameAllocResponse& temp_response); 158 159 // non-virtual QueryPlatform, as we should not change vtable 160 mfxStatus QueryPlatform(mfxPlatform* platform); 161 162 protected: 163 164 CommonCORE(const mfxU32 numThreadsAvailable, const mfxSession session = nullptr); 165 166 class API_1_19_Adapter : public IVideoCore_API_1_19 167 { 168 public: API_1_19_Adapter(CommonCORE * core)169 API_1_19_Adapter(CommonCORE * core) : m_core(core) {} 170 virtual mfxStatus QueryPlatform(mfxPlatform* platform); 171 172 private: 173 CommonCORE *m_core; 174 }; 175 176 177 virtual mfxStatus DefaultAllocFrames(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response); 178 mfxFrameAllocator* GetAllocatorAndMid(mfxMemId& mid); 179 mfxBaseWideFrameAllocator* GetAllocatorByReq(mfxU16 type) const; 180 virtual void Close(); 181 mfxStatus FreeMidArray(mfxFrameAllocator* pAlloc, mfxFrameAllocResponse *response); 182 mfxStatus RegisterMids(mfxFrameAllocResponse *response, mfxU16 memType, bool IsDefaultAlloc, mfxBaseWideFrameAllocator* pAlloc = 0); 183 mfxStatus CheckTimingLog(); 184 185 bool GetUniqID(mfxMemId& mId); 186 virtual mfxStatus InternalFreeFrames(mfxFrameAllocResponse *response); IsEqual(const mfxFrameAllocResponse & resp1,const mfxFrameAllocResponse & resp2)187 bool IsEqual (const mfxFrameAllocResponse &resp1, const mfxFrameAllocResponse &resp2) const 188 { 189 if (resp1.NumFrameActual != resp2.NumFrameActual) 190 return false; 191 192 for (mfxU32 i=0; i < resp1.NumFrameActual; i++) 193 { 194 if (resp1.mids[i] != resp2.mids[i]) 195 return false; 196 } 197 return true; 198 }; 199 200 //function checks if surfaces already allocated and mapped and request is consistent. Fill response if surfaces are correct 201 virtual bool IsOpaqSurfacesAlreadyMapped(mfxFrameSurface1 **pOpaqueSurface, mfxU32 NumOpaqueSurface, mfxFrameAllocResponse *response, bool ExtendedSearch = true) override; 202 203 typedef struct 204 { 205 mfxMemId InternalMid; 206 bool isDefaultMem; 207 mfxU16 memType; 208 209 } MemDesc; 210 211 typedef std::map<mfxMemId, MemDesc> CorrespTbl; 212 typedef std::map<mfxMemId, mfxBaseWideFrameAllocator*> AllocQueue; 213 typedef std::map<mfxMemId*, mfxMemId*> MemIDMap; 214 215 typedef std::map<mfxFrameSurface1*, mfxFrameSurface1> OpqTbl; 216 typedef std::map<mfxMemId, mfxFrameSurface1*> OpqTbl_MemId; 217 typedef std::map<mfxFrameData*, mfxFrameSurface1*> OpqTbl_FrameData; 218 typedef std::map<mfxFrameAllocResponse*, mfxU32> RefCtrTbl; 219 220 221 CorrespTbl m_CTbl; 222 AllocQueue m_AllocatorQueue; 223 MemIDMap m_RespMidQ; 224 OpqTbl m_OpqTbl; 225 OpqTbl_MemId m_OpqTbl_MemId; 226 OpqTbl_FrameData m_OpqTbl_FrameData; 227 RefCtrTbl m_RefCtrTbl; 228 229 // Number of available threads 230 const 231 mfxU32 m_numThreadsAvailable; 232 // Handler to the owning session 233 const 234 mfxSession m_session; 235 236 // Common I/F 237 mfxWideBufferAllocator m_bufferAllocator; 238 mfxBaseWideFrameAllocator m_FrameAllocator; 239 240 mfxU32 m_NumAllocators; 241 mfxHDL m_hdl; 242 243 mfxHDL m_DXVA2DecodeHandle; 244 245 mfxHDL m_D3DDecodeHandle; 246 mfxHDL m_D3DEncodeHandle; 247 mfxHDL m_D3DVPPHandle; 248 249 bool m_bSetExtBufAlloc; 250 bool m_bSetExtFrameAlloc; 251 252 std::unique_ptr<mfxMemId[]> m_pMemId; 253 std::unique_ptr<mfxBaseWideFrameAllocator> m_pcAlloc; 254 255 std::unique_ptr<FastCopy> m_pFastCopy; 256 bool m_bUseExtManager; 257 UMC::Mutex m_guard; 258 259 bool m_bIsOpaqMode; 260 261 mfxU32 m_CoreId; 262 263 mfx_UMC_FrameAllocator* m_pWrp; 264 265 EncodeHWCaps m_encode_caps; 266 EncodeHWCaps m_encode_mbprocrate; 267 268 std::vector<mfxFrameAllocResponse> m_PlugInMids; 269 270 API_1_19_Adapter m_API_1_19; 271 272 273 mfxU16 m_deviceId; 274 275 CommonCORE & operator = (const CommonCORE &) = delete; 276 }; 277 278 mfxStatus CoreDoSWFastCopy(mfxFrameSurface1 & dst, const mfxFrameSurface1 & src, int copyFlag); 279 280 #endif 281