d3d11: Update build-time dependency
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / mediafoundation / plugin.cpp
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:plugin-mediafoundation
23  *
24  * Microsoft MediaFoundation plugin.
25  *
26  * This plugin consists of various hardware/software video encoders
27  * software audio encoders, and video capture (from webcam) elements.
28  *
29  * GstMediaFoundation plugin supports H.264/AVC, H.265/HEVC, VP9, codecs for
30  * hardware-accelerate encoding.
31  *
32  * However, depending on the hardware it runs on, some elements might not be
33  * registered in case that underlying hardware doesn't support the feature.
34  *
35  * Moreover, depending on hardware vendor's MediaFoundation implementation,
36  * secendary GPU may not be usable. In that case, user could use vendor
37  * specific plugins, Intel Media SDK and NVCODEC plugins for example.
38  *
39  * For a system with multiple MediaFoundation compatible hardwares (i.e., GPU),
40  * there can be multiple plugin features having the same role.
41  * Also, there would be additional software video encoder element the system
42  * meets requirement.
43  *
44  * The naming rule for the non-primary encoder is `mf{codec}device{index}enc`
45  * where `index` is an arbitrary index number of hardware starting from 1.
46  *
47  * To get a list of all available elements, user can run
48  * ```sh
49  * gst-inspect-1.0.exe mediafoundation
50  * ```
51  *
52  * Since: 1.18
53  *
54  */
55
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59
60 #include "gstmfconfig.h"
61
62 #include <winapifamily.h>
63 #include <wrl.h>
64
65 #include <gst/gst.h>
66 #include <gst/video/video.h>
67 #include "gstmfvideosrc.h"
68 #include "gstmfdevice.h"
69 #include "gstmfutils.h"
70 #include "gstmfh264enc.h"
71 #include "gstmfh265enc.h"
72 #include "gstmfvp9enc.h"
73 #include "gstmfaacenc.h"
74 #include "gstmfmp3enc.h"
75 #include "gstmfaacdec.h"
76 #include "gstmfmp3dec.h"
77
78 #if GST_MF_HAVE_D3D11
79 #include <gst/d3d11/gstd3d11.h>
80 #include <d3d11_4.h>
81 #include <dxgi1_6.h>
82 #include <gstmfplatloader.h>
83 #endif
84
85 GST_DEBUG_CATEGORY (gst_mf_debug);
86 GST_DEBUG_CATEGORY (gst_mf_utils_debug);
87 GST_DEBUG_CATEGORY (gst_mf_source_object_debug);
88 GST_DEBUG_CATEGORY (gst_mf_transform_debug);
89 GST_DEBUG_CATEGORY (gst_mf_video_buffer_debug);
90 GST_DEBUG_CATEGORY (gst_mf_video_encoder_debug);
91
92 #define GST_CAT_DEFAULT gst_mf_debug
93
94 /* *INDENT-OFF* */
95 using namespace Microsoft::WRL;
96 /* *INDENT-ON* */
97
98 static void
99 plugin_deinit (gpointer data)
100 {
101   MFShutdown ();
102 }
103
104 #if GST_MF_HAVE_D3D11
105 static GList *
106 get_d3d11_devices (void)
107 {
108   GList *ret = nullptr;
109   guint i;
110   HRESULT hr;
111   ComPtr < IMFVideoSampleAllocatorEx > allocator;
112
113   /* Check whether we can use IMFVideoSampleAllocatorEx interface */
114   hr = GstMFCreateVideoSampleAllocatorEx (IID_IMFVideoSampleAllocatorEx,
115       &allocator);
116   if (!gst_mf_result (hr)) {
117     GST_DEBUG ("IMFVideoSampleAllocatorEx interface is unavailable");
118     return nullptr;
119   }
120
121   /* AMD seems supporting up to 12 cards, and 8 for NVIDIA */
122   for (i = 0; i < 12; i++) {
123     GstD3D11Device *device;
124     gboolean is_hardware = FALSE;
125     const GstD3D11Format *d3d11_format;
126     ID3D11Device *device_handle;
127     D3D11_FEATURE_DATA_D3D11_OPTIONS4 options = { 0, };
128     UINT supported = 0;
129
130     device = gst_d3d11_device_new (i, D3D11_CREATE_DEVICE_VIDEO_SUPPORT);
131
132     if (!device)
133       break;
134
135     g_object_get (device, "hardware", &is_hardware, nullptr);
136
137     if (!is_hardware) {
138       GST_DEBUG_OBJECT (device, "Given d3d11 device is not for hardware");
139       gst_object_unref (device);
140       continue;
141     }
142
143     /* device can support NV12 format? */
144     d3d11_format =
145         gst_d3d11_device_format_from_gst (device, GST_VIDEO_FORMAT_NV12);
146     if (!d3d11_format || d3d11_format->dxgi_format != DXGI_FORMAT_NV12) {
147       GST_DEBUG_OBJECT (device,
148           "Given d3d11 device cannot support NV12 format");
149       gst_object_unref (device);
150       continue;
151     }
152
153     /* device can support ExtendedNV12SharedTextureSupported?
154      *
155      * NOTE: we will make use of per encoder object d3d11 device without
156      * sharing it in a pipeline because MF needs D3D11_CREATE_DEVICE_VIDEO_SUPPORT
157      * but the flag doesn't used for the other our use cases.
158      * So we need texture sharing feature so that we can copy d3d11 texture into
159      * MF specific texture pool without download texture */
160
161     device_handle = gst_d3d11_device_get_device_handle (device);
162     hr = device_handle->CheckFeatureSupport (D3D11_FEATURE_D3D11_OPTIONS4,
163         &options, sizeof (options));
164     if (!gst_d3d11_result (hr, device) ||
165         !options.ExtendedNV12SharedTextureSupported) {
166       GST_DEBUG_OBJECT (device,
167           "Given d3d11 device cannot support NV12 format for shared texture");
168       gst_object_unref (device);
169       continue;
170     }
171
172     /* can we bind NV12 texture for encoder? */
173     hr = device_handle->CheckFormatSupport (DXGI_FORMAT_NV12, &supported);
174
175     if (!gst_d3d11_result (hr, device)) {
176       GST_DEBUG_OBJECT (device, "Couldn't query format support");
177       gst_object_unref (device);
178       continue;
179     } else if ((supported & D3D11_FORMAT_SUPPORT_VIDEO_ENCODER) == 0) {
180       GST_DEBUG_OBJECT (device, "We cannot bind NV12 format for encoding");
181       gst_object_unref (device);
182       continue;
183     }
184
185     ret = g_list_append (ret, device);
186   }
187
188   return ret;
189 }
190 #endif
191
192 static gboolean
193 plugin_init (GstPlugin * plugin)
194 {
195   HRESULT hr;
196   guint rank = GST_RANK_SECONDARY;
197   GList *device_list = nullptr;
198
199   GST_DEBUG_CATEGORY_INIT (gst_mf_debug, "mf", 0, "media foundation");
200   GST_DEBUG_CATEGORY_INIT (gst_mf_utils_debug,
201       "mfutils", 0, "media foundation utility functions");
202   GST_DEBUG_CATEGORY_INIT (gst_mf_source_object_debug,
203       "mfsourceobject", 0, "mfsourceobject");
204   GST_DEBUG_CATEGORY_INIT (gst_mf_transform_debug,
205       "mftransform", 0, "mftransform");
206   GST_DEBUG_CATEGORY_INIT (gst_mf_video_buffer_debug,
207       "mfvideobuffer", 0, "mfvideobuffer");
208   GST_DEBUG_CATEGORY_INIT (gst_mf_video_encoder_debug,
209       "mfvideoencoder", 0, "mfvideoencoder");
210
211   hr = MFStartup (MF_VERSION, MFSTARTUP_NOSOCKET);
212   if (!gst_mf_result (hr)) {
213     GST_WARNING ("MFStartup failure, hr: 0x%x", hr);
214     return TRUE;
215   }
216
217   /* mfvideosrc should be primary rank for UWP */
218 #if (GST_MF_WINAPI_APP && !GST_MF_WINAPI_DESKTOP)
219   rank = GST_RANK_PRIMARY + 1;
220 #endif
221
222   /* FIXME: In order to create MFT for a specific GPU, MFTEnum2() API is
223    * required API but it's desktop only.
224    * So, resulting MFT and D3D11 might not be compatible in case of multi-GPU
225    * environment on UWP. */
226 #if GST_MF_HAVE_D3D11
227   if (gst_mf_plat_load_library ())
228     device_list = get_d3d11_devices ();
229 #endif
230
231   gst_element_register (plugin, "mfvideosrc", rank, GST_TYPE_MF_VIDEO_SRC);
232   gst_device_provider_register (plugin, "mfdeviceprovider",
233       rank, GST_TYPE_MF_DEVICE_PROVIDER);
234
235   gst_mf_h264_enc_plugin_init (plugin, GST_RANK_SECONDARY, device_list);
236   gst_mf_h265_enc_plugin_init (plugin, GST_RANK_SECONDARY, device_list);
237   gst_mf_vp9_enc_plugin_init (plugin, GST_RANK_SECONDARY, device_list);
238
239   if (device_list)
240     g_list_free_full (device_list, gst_object_unref);
241
242   gst_mf_aac_enc_plugin_init (plugin, GST_RANK_SECONDARY);
243   gst_mf_mp3_enc_plugin_init (plugin, GST_RANK_SECONDARY);
244   gst_mf_aac_dec_plugin_init (plugin, GST_RANK_SECONDARY);
245   gst_mf_mp3_dec_plugin_init (plugin, GST_RANK_SECONDARY);
246
247   /* So that call MFShutdown() when this plugin is no more used
248    * (i.e., gst_deinit). Otherwise valgrind-like tools would complain
249    * about un-released media foundation resources.
250    *
251    * NOTE: MFStartup and MFShutdown can be called multiple times, but the number
252    * of each MFStartup and MFShutdown call should be identical. This rule is
253    * simliar to that of CoInitialize/CoUninitialize pair */
254   g_object_set_data_full (G_OBJECT (plugin),
255       "plugin-mediafoundation-shutdown", (gpointer) "shutdown-data",
256       (GDestroyNotify) plugin_deinit);
257
258   return TRUE;
259 }
260
261 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
262     GST_VERSION_MINOR,
263     mediafoundation,
264     "Microsoft Media Foundation plugin",
265     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)