|
| 1 | +// |
| 2 | +// Notice Regarding Standards. AMD does not provide a license or sublicense to |
| 3 | +// any Intellectual Property Rights relating to any standards, including but not |
| 4 | +// limited to any audio and/or video codec technologies such as MPEG-2, MPEG-4; |
| 5 | +// AVC/H.264; HEVC/H.265; AAC decode/FFMPEG; AAC encode/FFMPEG; VC-1; and MP3 |
| 6 | +// (collectively, the “Media Technologies”). For clarity, you will pay any |
| 7 | +// royalties due for such third party technologies, which may include the Media |
| 8 | +// Technologies that are owed as a result of AMD providing the Software to you. |
| 9 | +// |
| 10 | +// MIT license |
| 11 | +// |
| 12 | +// Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved. |
| 13 | +// |
| 14 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 | +// of this software and associated documentation files (the "Software"), to deal |
| 16 | +// in the Software without restriction, including without limitation the rights |
| 17 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 18 | +// copies of the Software, and to permit persons to whom the Software is |
| 19 | +// furnished to do so, subject to the following conditions: |
| 20 | +// |
| 21 | +// The above copyright notice and this permission notice shall be included in |
| 22 | +// all copies or substantial portions of the Software. |
| 23 | +// |
| 24 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 25 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 26 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 27 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 28 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 29 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 30 | +// THE SOFTWARE. |
| 31 | +// |
| 32 | + |
| 33 | +#include "AMFFactory.h" |
| 34 | +#include "Thread.h" |
| 35 | + |
| 36 | +AMFFactoryHelper g_AMFFactory; |
| 37 | + |
| 38 | +extern "C" |
| 39 | +{ |
| 40 | + extern AMF_CORE_LINK AMF_RESULT AMF_CDECL_CALL AMFInit(amf_uint64 version, amf::AMFFactory **ppFactory); |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +//------------------------------------------------------------------------------------------------- |
| 45 | +AMFFactoryHelper::AMFFactoryHelper() : |
| 46 | +m_hDLLHandle(NULL), |
| 47 | +m_pFactory(NULL), |
| 48 | +m_pDebug(NULL), |
| 49 | +m_pTrace(NULL), |
| 50 | +m_AMFRuntimeVersion(0), |
| 51 | +m_iRefCount(0) |
| 52 | +{ |
| 53 | +} |
| 54 | +//------------------------------------------------------------------------------------------------- |
| 55 | +AMFFactoryHelper::~AMFFactoryHelper() |
| 56 | +{ |
| 57 | + Terminate(); |
| 58 | +} |
| 59 | +//------------------------------------------------------------------------------------------------- |
| 60 | +AMF_RESULT AMFFactoryHelper::Init() |
| 61 | +{ |
| 62 | +#ifndef AMF_CORE_STATIC |
| 63 | + if (m_hDLLHandle != NULL) |
| 64 | + { |
| 65 | + amf_atomic_inc(&m_iRefCount); |
| 66 | + return AMF_OK; |
| 67 | + } |
| 68 | + m_hDLLHandle = LoadLibraryW(AMF_DLL_NAME); |
| 69 | + if(m_hDLLHandle == NULL) |
| 70 | + { |
| 71 | + return AMF_FAIL; |
| 72 | + } |
| 73 | + |
| 74 | + AMFInit_Fn initFun = (AMFInit_Fn)::GetProcAddress(m_hDLLHandle, AMF_INIT_FUNCTION_NAME); |
| 75 | + if(initFun == NULL) |
| 76 | + { |
| 77 | + return AMF_FAIL; |
| 78 | + } |
| 79 | + AMF_RESULT res = initFun(AMF_FULL_VERSION, &m_pFactory); |
| 80 | + if(res != AMF_OK) |
| 81 | + { |
| 82 | + return res; |
| 83 | + } |
| 84 | + AMFQueryVersion_Fn versionFun = (AMFQueryVersion_Fn)::GetProcAddress(m_hDLLHandle, AMF_QUERY_VERSION_FUNCTION_NAME); |
| 85 | + if(versionFun == NULL) |
| 86 | + { |
| 87 | + return AMF_FAIL; |
| 88 | + } |
| 89 | + res = versionFun(&m_AMFRuntimeVersion); |
| 90 | + if(res != AMF_OK) |
| 91 | + { |
| 92 | + return res; |
| 93 | + } |
| 94 | +#else |
| 95 | + AMF_RESULT res = AMFInit(AMF_FULL_VERSION, &m_pFactory); |
| 96 | + if (res != AMF_OK) |
| 97 | + { |
| 98 | + return res; |
| 99 | + } |
| 100 | + m_AMFRuntimeVersion = AMF_FULL_VERSION; |
| 101 | +#endif |
| 102 | + m_pFactory->GetTrace(&m_pTrace); |
| 103 | + m_pFactory->GetDebug(&m_pDebug); |
| 104 | + |
| 105 | + amf_atomic_inc(&m_iRefCount); |
| 106 | + return AMF_OK; |
| 107 | +} |
| 108 | +//------------------------------------------------------------------------------------------------- |
| 109 | +AMF_RESULT AMFFactoryHelper::Terminate() |
| 110 | +{ |
| 111 | + if(m_hDLLHandle != NULL) |
| 112 | + { |
| 113 | + amf_atomic_dec(&m_iRefCount); |
| 114 | + if(m_iRefCount == 0) |
| 115 | + { |
| 116 | + FreeLibrary(m_hDLLHandle); |
| 117 | + m_hDLLHandle = NULL; |
| 118 | + m_pFactory= NULL; |
| 119 | + m_pDebug = NULL; |
| 120 | + m_pTrace = NULL; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return AMF_OK; |
| 125 | +} |
| 126 | +//------------------------------------------------------------------------------------------------- |
| 127 | +amf::AMFFactory* AMFFactoryHelper::GetFactory() |
| 128 | +{ |
| 129 | + return m_pFactory; |
| 130 | +} |
| 131 | +//------------------------------------------------------------------------------------------------- |
| 132 | +amf::AMFDebug* AMFFactoryHelper::GetDebug() |
| 133 | +{ |
| 134 | + return m_pDebug; |
| 135 | +} |
| 136 | +//------------------------------------------------------------------------------------------------- |
| 137 | +amf::AMFTrace* AMFFactoryHelper::GetTrace() |
| 138 | +{ |
| 139 | + return m_pTrace; |
| 140 | +} |
| 141 | +//------------------------------------------------------------------------------------------------- |
| 142 | +amf_uint64 AMFFactoryHelper::AMFQueryVersion() |
| 143 | +{ |
| 144 | + return m_AMFRuntimeVersion; |
| 145 | +} |
| 146 | + |
| 147 | +//------------------------------------------------------------------------------------------------- |
| 148 | +AMF_RESULT AMFFactoryHelper::LoadExternalComponent(amf::AMFContext* pContext, const wchar_t* dll, const char* function, void* reserved, amf::AMFComponent** ppComponent) |
| 149 | +{ |
| 150 | + // check passed in parameters |
| 151 | + if (!pContext || !dll || !function) |
| 152 | + { |
| 153 | + return AMF_INVALID_ARG; |
| 154 | + } |
| 155 | + |
| 156 | + // check if DLL has already been loaded |
| 157 | + HMODULE hDll = NULL; |
| 158 | + for (std::vector<ComponentHolder>::iterator it = m_extComponents.begin(); it != m_extComponents.end(); ++it) |
| 159 | + { |
| 160 | + if (wcsicmp(it->m_DLL.c_str(), dll) == 0) |
| 161 | + { |
| 162 | + if (it->m_hDLLHandle != NULL) |
| 163 | + { |
| 164 | + hDll = it->m_hDLLHandle; |
| 165 | + amf_atomic_inc(&it->m_iRefCount); |
| 166 | + break; |
| 167 | + } |
| 168 | + |
| 169 | + return AMF_UNEXPECTED; |
| 170 | + } |
| 171 | + } |
| 172 | + // DLL wasn't loaded before so load it now and |
| 173 | + // add it to the internal list |
| 174 | + if (hDll == NULL) |
| 175 | + { |
| 176 | + ComponentHolder component; |
| 177 | + component.m_iRefCount = 0; |
| 178 | + component.m_hDLLHandle = NULL; |
| 179 | + component.m_DLL = dll; |
| 180 | + |
| 181 | + hDll = LoadLibraryW(dll); |
| 182 | + if (hDll == NULL) |
| 183 | + return AMF_FAIL; |
| 184 | + |
| 185 | + // since LoadLibrary succeeded add the information |
| 186 | + // into the internal list so we can properly free |
| 187 | + // the DLL later on, even if we fail to get the |
| 188 | + // required information from it... |
| 189 | + component.m_hDLLHandle = hDll; |
| 190 | + amf_atomic_inc(&component.m_iRefCount); |
| 191 | + m_extComponents.push_back(component); |
| 192 | + } |
| 193 | + |
| 194 | + // look for function we want in the dll we just loaded |
| 195 | + typedef AMF_RESULT(AMF_CDECL_CALL *AMFCreateComponentFunc)(amf::AMFContext*, void* reserved, amf::AMFComponent**); |
| 196 | + AMFCreateComponentFunc initFn = (AMFCreateComponentFunc)::GetProcAddress(hDll, function); |
| 197 | + if (initFn == NULL) |
| 198 | + return AMF_FAIL; |
| 199 | + |
| 200 | + return initFn(pContext, reserved, ppComponent); |
| 201 | +} |
| 202 | +//------------------------------------------------------------------------------------------------- |
| 203 | +AMF_RESULT AMFFactoryHelper::UnLoadExternalComponent(const wchar_t* dll) |
| 204 | +{ |
| 205 | + if (!dll) |
| 206 | + { |
| 207 | + return AMF_INVALID_ARG; |
| 208 | + } |
| 209 | + for (std::vector<ComponentHolder>::iterator it = m_extComponents.begin(); it != m_extComponents.end(); ++it) |
| 210 | + { |
| 211 | + if (wcsicmp(it->m_DLL.c_str(), dll) == 0) |
| 212 | + { |
| 213 | + if (it->m_hDLLHandle == NULL) |
| 214 | + { |
| 215 | + return AMF_UNEXPECTED; |
| 216 | + } |
| 217 | + amf_atomic_dec(&it->m_iRefCount); |
| 218 | + if (it->m_iRefCount == 0) |
| 219 | + { |
| 220 | + FreeLibrary(it->m_hDLLHandle); |
| 221 | + m_extComponents.erase(it); |
| 222 | + } |
| 223 | + break; |
| 224 | + } |
| 225 | + } |
| 226 | + return AMF_OK; |
| 227 | +} |
| 228 | +//------------------------------------------------------------------------------------------------- |
| 229 | +//------------------------------------------------------------------------------------------------- |
| 230 | +//------------------------------------------------------------------------------------------------- |
| 231 | + |
0 commit comments