qsv: Update SDK version to v2022.2.4
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / qsv / libmfx / dispatcher / windows / mfx_load_dll.cpp
1 /*############################################################################
2   # Copyright (C) Intel Corporation
3   #
4   # SPDX-License-Identifier: MIT
5   ############################################################################*/
6
7 #include "windows/mfx_load_dll.h"
8 #include "windows/mfx_dispatcher.h"
9
10 #include <string.h>
11 #include <wchar.h>
12 #include <windows.h>
13
14 #if defined(_WIN64)
15 const wchar_t *const defaultDLLName[2]      = { L"libmfxhw64.dll", L"libvplswref64.dll" };
16 const wchar_t *const defaultAudioDLLName[2] = { L"libmfxaudiosw64.dll", L"libmfxaudiosw64.dll" };
17 const wchar_t *const defaultOneVPLDLLName   = { L"libmfx64-gen.dll" };
18
19 const wchar_t *const defaultPluginDLLName[2] = { L"mfxplugin64_hw.dll", L"mfxplugin64_sw.dll" };
20     #if defined(MEDIASDK_UWP_DISPATCHER)
21 const wchar_t *const IntelGFXAPIDLLName = { L"intel_gfx_api-x64.dll" };
22     #endif
23
24 #elif defined(_WIN32)
25 const wchar_t *const defaultDLLName[2] = { L"libmfxhw32.dll", L"libvplswref32.dll" };
26
27 const wchar_t *const defaultAudioDLLName[2] = { L"libmfxaudiosw32.dll", L"libmfxaudiosw32.dll" };
28
29 const wchar_t *const defaultOneVPLDLLName = { L"libmfx32-gen.dll" };
30
31 const wchar_t *const defaultPluginDLLName[2] = { L"mfxplugin32_hw.dll", L"mfxplugin32_sw.dll" };
32
33     #if defined(MEDIASDK_UWP_DISPATCHER)
34 const wchar_t *const IntelGFXAPIDLLName      = { L"intel_gfx_api-x86.dll" };
35     #endif
36
37 #endif // (defined(_WIN64))
38
39 namespace MFX {
40
41 mfxStatus mfx_get_default_dll_name(wchar_t *pPath, size_t pathSize, eMfxImplType implType) {
42     if (!pPath) {
43         return MFX_ERR_NULL_PTR;
44     }
45
46     // there are only 2 implementation with default DLL names
47 #if _MSC_VER >= 1400
48     return 0 == wcscpy_s(pPath, pathSize, defaultDLLName[implType & 1]) ? MFX_ERR_NONE
49                                                                         : MFX_ERR_UNKNOWN;
50 #else
51     wcscpy(pPath, defaultDLLName[implType & 1]);
52     return MFX_ERR_NONE;
53 #endif
54 } // mfxStatus mfx_get_default_dll_name(wchar_t *pPath, size_t pathSize, eMfxImplType implType)
55
56 mfxStatus mfx_get_default_onevpl_dll_name(wchar_t *pPath, size_t pathSize) {
57     if (!pPath) {
58         return MFX_ERR_NULL_PTR;
59     }
60
61     // there are only 2 implementation with default DLL names
62 #if _MSC_VER >= 1400
63     return 0 == wcscpy_s(pPath, pathSize, defaultOneVPLDLLName) ? MFX_ERR_NONE : MFX_ERR_UNKNOWN;
64 #else
65     wcscpy(pPath, defaultOneVPLDLLName);
66     return MFX_ERR_NONE;
67 #endif
68 } // mfxStatus mfx_get_default_onevpl_dll_name(wchar_t *pPath, size_t pathSize, eMfxImplType implType)
69
70 #if defined(MEDIASDK_UWP_DISPATCHER)
71 mfxStatus mfx_get_default_intel_gfx_api_dll_name(wchar_t *pPath, size_t pathSize) {
72     if (!pPath) {
73         return MFX_ERR_NULL_PTR;
74     }
75
76     #if _MSC_VER >= 1400
77     return 0 == wcscpy_s(pPath, pathSize, IntelGFXAPIDLLName) ? MFX_ERR_NONE : MFX_ERR_UNKNOWN;
78     #else
79     wcscpy(pPath, IntelGFXAPIDLLName);
80     return MFX_ERR_NONE;
81     #endif
82 } // mfx_get_default_intel_gfx_api_dll_name(wchar_t *pPath, size_t pathSize)
83 #endif
84
85 mfxStatus mfx_get_default_plugin_name(wchar_t *pPath, size_t pathSize, eMfxImplType implType) {
86     if (!pPath) {
87         return MFX_ERR_NULL_PTR;
88     }
89
90     // there are only 2 implementation with default DLL names
91 #if _MSC_VER >= 1400
92     return 0 == wcscpy_s(pPath, pathSize, defaultPluginDLLName[implType & 1]) ? MFX_ERR_NONE
93                                                                               : MFX_ERR_UNKNOWN;
94 #else
95     wcscpy(pPath, defaultPluginDLLName[implType & 1]);
96     return MFX_ERR_NONE;
97 #endif
98 }
99
100 mfxStatus mfx_get_default_audio_dll_name(wchar_t *pPath, size_t pathSize, eMfxImplType implType) {
101     if (!pPath) {
102         return MFX_ERR_NULL_PTR;
103     }
104
105     // there are only 2 implementation with default DLL names
106 #if _MSC_VER >= 1400
107     return 0 == wcscpy_s(pPath, pathSize, defaultAudioDLLName[implType & 1]) ? MFX_ERR_NONE
108                                                                              : MFX_ERR_UNKNOWN;
109 #else
110     wcscpy(pPath, defaultAudioDLLName[implType & 1]);
111     return MFX_ERR_NONE;
112 #endif
113 } // mfxStatus mfx_get_default_audio_dll_name(wchar_t *pPath, size_t pathSize, eMfxImplType implType)
114
115 mfxModuleHandle mfx_dll_load(const wchar_t *pFileName) {
116     mfxModuleHandle hModule = (mfxModuleHandle)0;
117
118     // check error(s)
119     if (NULL == pFileName) {
120         return NULL;
121     }
122 #if !defined(MEDIASDK_UWP_DISPATCHER)
123     // set the silent error mode
124     DWORD prevErrorMode = 0;
125     #if (_WIN32_WINNT >= 0x0600)
126     SetThreadErrorMode(SEM_FAILCRITICALERRORS, &prevErrorMode);
127     #else
128     prevErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
129     #endif
130 #endif // !defined(MEDIASDK_UWP_DISPATCHER)
131
132     // load the library's module
133 #if !defined(MEDIASDK_ARM_LOADER)
134     hModule = LoadLibraryExW(pFileName, NULL, 0);
135 #endif
136
137 #if !defined(MEDIASDK_UWP_DISPATCHER)
138     // set the previous error mode
139     #if (_WIN32_WINNT >= 0x0600)
140     SetThreadErrorMode(prevErrorMode, NULL);
141     #else
142     SetErrorMode(prevErrorMode);
143     #endif
144 #endif // !defined(MEDIASDK_UWP_DISPATCHER)
145
146     return hModule;
147
148 } // mfxModuleHandle mfx_dll_load(const wchar_t *pFileName)
149
150 mfxFunctionPointer mfx_dll_get_addr(mfxModuleHandle handle, const char *pFunctionName) {
151     if (NULL == handle) {
152         return NULL;
153     }
154
155     return (mfxFunctionPointer)GetProcAddress((HMODULE)handle, pFunctionName);
156 } // mfxFunctionPointer mfx_dll_get_addr(mfxModuleHandle handle, const char *pFunctionName)
157
158 bool mfx_dll_free(mfxModuleHandle handle) {
159     if (NULL == handle) {
160         return true;
161     }
162
163     BOOL bRes = FreeLibrary((HMODULE)handle);
164
165     return !!bRes;
166 } // bool mfx_dll_free(mfxModuleHandle handle)
167
168 #if !defined(MEDIASDK_UWP_DISPATCHER)
169 mfxModuleHandle mfx_get_dll_handle(const wchar_t *pFileName) {
170     mfxModuleHandle hModule = (mfxModuleHandle)0;
171
172     // check error(s)
173     if (NULL == pFileName) {
174         return NULL;
175     }
176
177     // set the silent error mode
178     DWORD prevErrorMode = 0;
179     #if (_WIN32_WINNT >= 0x0600)
180     SetThreadErrorMode(SEM_FAILCRITICALERRORS, &prevErrorMode);
181     #else
182     prevErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
183     #endif
184     // load the library's module
185     GetModuleHandleExW(0, pFileName, (HMODULE *)&hModule);
186     // set the previous error mode
187     #if (_WIN32_WINNT >= 0x0600)
188     SetThreadErrorMode(prevErrorMode, NULL);
189     #else
190     SetErrorMode(prevErrorMode);
191     #endif
192     return hModule;
193 }
194 #endif //!defined(MEDIASDK_UWP_DISPATCHER)
195
196 } // namespace MFX