aaff1fe88455194cd05244e961d1b7f1ecdcf43b
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / d3d11 / plugin.cpp
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:plugin-d3d11
22  *
23  * Microsoft Direct3D11 plugin.
24  *
25  * This plugin consists of various video filter, screen capture source,
26  * video sink, and video decoder elements.
27  *
28  * GstD3D11 plugin supports H.264/AVC, H.265/HEVC, VP8, VP9, H.262/MPEG-2 video,
29  * and AV1 codecs for decoding as well as hardware-accelerated video
30  * deinterlacing. Note that minimum required OS version for video decoder and
31  * deinterlacing elements is Windows 8.
32  *
33  * Plugin feature names of decoders:
34  * - d3d11h264dec
35  * - d3d11h265dec
36  * - d3d11vp8dec
37  * - d3d11vp9dec
38  * - d3d11mpeg2dec
39  * - d3d11av1dec
40  *
41  * Similar to the video decoder case, deinterlacing element would be registered
42  * only if its supported by hardware with the feature name `d3d11deinterlace`
43  *
44  * However, depending on the hardware it runs on, some elements might not be
45  * registered in case that underlying hardware doesn't support the feature.
46  * For a system with multiple Direct3D11 compatible hardwares (i.e., GPU),
47  * there can be multiple plugin features having the same role.
48  * The naming rule for the non-primary decoder element is
49  * `d3d11{codec}device{index}dec` where `index` is an arbitrary index number of
50  * hardware starting from 1.
51  *
52  * To get a list of all available elements, user can run
53  * ```sh
54  * gst-inspect-1.0.exe d3d11
55  * ```
56  *
57  */
58
59 #ifdef HAVE_CONFIG_H
60 #include "config.h"
61 #endif
62
63 #include <gst/gst.h>
64 #include <gst/d3d11/gstd3d11.h>
65 #include "gstd3d11videosink.h"
66 #include "gstd3d11upload.h"
67 #include "gstd3d11download.h"
68 #include "gstd3d11convert.h"
69 #include "gstd3d11shader.h"
70 #include "gstd3d11compositor.h"
71 #include "gstd3d11compositorbin.h"
72 #ifdef HAVE_DXVA_H
73 #include "gstd3d11h264dec.h"
74 #include "gstd3d11h265dec.h"
75 #include "gstd3d11vp9dec.h"
76 #include "gstd3d11vp8dec.h"
77 #include "gstd3d11mpeg2dec.h"
78 #include "gstd3d11av1dec.h"
79 #endif
80 #ifdef HAVE_DXGI_DESKTOP_DUP
81 #include "gstd3d11screencapturesrc.h"
82 #include "gstd3d11screencapturedevice.h"
83 #endif
84 #ifdef HAVE_D3D11_VIDEO_PROC
85 #include "gstd3d11deinterlace.h"
86 #endif
87
88 GST_DEBUG_CATEGORY (gst_d3d11_debug);
89 GST_DEBUG_CATEGORY (gst_d3d11_shader_debug);
90 GST_DEBUG_CATEGORY (gst_d3d11_converter_debug);
91 GST_DEBUG_CATEGORY (gst_d3d11_plugin_utils_debug);
92 GST_DEBUG_CATEGORY (gst_d3d11_format_debug);
93 GST_DEBUG_CATEGORY (gst_d3d11_device_debug);
94 GST_DEBUG_CATEGORY (gst_d3d11_overlay_compositor_debug);
95 GST_DEBUG_CATEGORY (gst_d3d11_window_debug);
96 GST_DEBUG_CATEGORY (gst_d3d11_video_processor_debug);
97 GST_DEBUG_CATEGORY (gst_d3d11_compositor_debug);
98
99 #ifdef HAVE_DXVA_H
100 GST_DEBUG_CATEGORY (gst_d3d11_decoder_debug);
101 GST_DEBUG_CATEGORY (gst_d3d11_h264_dec_debug);
102 GST_DEBUG_CATEGORY (gst_d3d11_h265_dec_debug);
103 GST_DEBUG_CATEGORY (gst_d3d11_vp9_dec_debug);
104 GST_DEBUG_CATEGORY (gst_d3d11_vp8_dec_debug);
105 GST_DEBUG_CATEGORY (gst_d3d11_mpeg2_dec_debug);
106 GST_DEBUG_CATEGORY (gst_d3d11_av1_dec_debug);
107 #endif
108
109 #ifdef HAVE_DXGI_DESKTOP_DUP
110 GST_DEBUG_CATEGORY (gst_d3d11_screen_capture_debug);
111 GST_DEBUG_CATEGORY (gst_d3d11_screen_capture_device_debug);
112 #endif
113
114 #ifdef HAVE_D3D11_VIDEO_PROC
115 GST_DEBUG_CATEGORY (gst_d3d11_deinterlace_debug);
116 #endif
117
118 #define GST_CAT_DEFAULT gst_d3d11_debug
119
120 static gboolean
121 plugin_init (GstPlugin * plugin)
122 {
123   GstRank video_sink_rank = GST_RANK_PRIMARY;
124   D3D_FEATURE_LEVEL max_feature_level = D3D_FEATURE_LEVEL_9_3;
125   guint i;
126
127   GST_DEBUG_CATEGORY_INIT (gst_d3d11_debug, "d3d11", 0, "direct3d 11 plugin");
128   GST_DEBUG_CATEGORY_INIT (gst_d3d11_shader_debug,
129       "d3d11shader", 0, "d3d11shader");
130   GST_DEBUG_CATEGORY_INIT (gst_d3d11_converter_debug,
131       "d3d11converter", 0, "d3d11converter");
132   GST_DEBUG_CATEGORY_INIT (gst_d3d11_plugin_utils_debug,
133       "d3d11pluginutils", 0, "d3d11 plugin utility functions");
134   GST_DEBUG_CATEGORY_INIT (gst_d3d11_overlay_compositor_debug,
135       "d3d11overlaycompositor", 0, "d3d11overlaycompositor");
136   GST_DEBUG_CATEGORY_INIT (gst_d3d11_window_debug,
137       "d3d11window", 0, "d3d11window");
138   GST_DEBUG_CATEGORY_INIT (gst_d3d11_video_processor_debug,
139       "d3d11videoprocessor", 0, "d3d11videoprocessor");
140   GST_DEBUG_CATEGORY_INIT (gst_d3d11_compositor_debug,
141       "d3d11compositor", 0, "d3d11compositor element");
142
143   if (!gst_d3d11_shader_init ()) {
144     GST_WARNING ("Cannot initialize d3d11 shader");
145     return TRUE;
146   }
147 #ifdef HAVE_DXVA_H
148   /* DXVA2 API is availble since Windows 8 */
149   if (gst_d3d11_is_windows_8_or_greater ()) {
150     GST_DEBUG_CATEGORY_INIT (gst_d3d11_decoder_debug,
151         "d3d11decoder", 0, "Direct3D11 Video Decoder object");
152     GST_DEBUG_CATEGORY_INIT (gst_d3d11_h264_dec_debug,
153         "d3d11h264dec", 0, "Direct3D11 H.264 Video Decoder");
154     GST_DEBUG_CATEGORY_INIT (gst_d3d11_vp9_dec_debug,
155         "d3d11vp9dec", 0, "Direct3D11 VP9 Video Decoder");
156     GST_DEBUG_CATEGORY_INIT (gst_d3d11_h265_dec_debug,
157         "d3d11h265dec", 0, "Direct3D11 H.265 Video Decoder");
158     GST_DEBUG_CATEGORY_INIT (gst_d3d11_vp8_dec_debug,
159         "d3d11vp8dec", 0, "Direct3D11 VP8 Decoder");
160     GST_DEBUG_CATEGORY_INIT (gst_d3d11_mpeg2_dec_debug,
161         "d3d11mpeg2dec", 0, "Direct3D11 MPEG2 Decoder");
162     GST_DEBUG_CATEGORY_INIT (gst_d3d11_av1_dec_debug,
163         "d3d11av1dec", 0, "Direct3D11 AV1 Decoder");
164   }
165 #endif
166
167 #ifdef HAVE_D3D11_VIDEO_PROC
168   GST_DEBUG_CATEGORY_INIT (gst_d3d11_deinterlace_debug,
169       "d3d11deinterlace", 0, "Direct3D11 Deinterlacer");
170 #endif
171
172   /* Enumerate devices to register decoders per device and to get the highest
173    * feature level */
174   /* AMD seems supporting up to 12 cards, and 8 for NVIDIA */
175   for (i = 0; i < 12; i++) {
176     GstD3D11Device *device = NULL;
177     ID3D11Device *device_handle;
178     D3D_FEATURE_LEVEL feature_level;
179
180     device = gst_d3d11_device_new (i, D3D11_CREATE_DEVICE_BGRA_SUPPORT);
181     if (!device)
182       break;
183
184     device_handle = gst_d3d11_device_get_device_handle (device);
185     feature_level = device_handle->GetFeatureLevel ();
186
187     if (feature_level > max_feature_level)
188       max_feature_level = feature_level;
189
190 #ifdef HAVE_DXVA_H
191     /* DXVA2 API is availble since Windows 8 */
192     if (gst_d3d11_is_windows_8_or_greater () &&
193         gst_d3d11_device_get_video_device_handle (device)) {
194       gboolean legacy = gst_d3d11_decoder_util_is_legacy_device (device);
195
196       /* avdec_h264 has primary rank, make this higher than it */
197       gst_d3d11_h264_dec_register (plugin,
198           device, GST_RANK_PRIMARY + 1, legacy);
199       if (!legacy) {
200         gst_d3d11_h265_dec_register (plugin, device, GST_RANK_PRIMARY);
201         gst_d3d11_vp9_dec_register (plugin, device, GST_RANK_PRIMARY);
202         gst_d3d11_vp8_dec_register (plugin, device, GST_RANK_PRIMARY);
203         gst_d3d11_av1_dec_register (plugin, device, GST_RANK_PRIMARY);
204         gst_d3d11_mpeg2_dec_register (plugin, device, GST_RANK_SECONDARY);
205       }
206     }
207 #endif
208
209 #ifdef HAVE_D3D11_VIDEO_PROC
210     /* D3D11 video processor API is availble since Windows 8 */
211     if (gst_d3d11_is_windows_8_or_greater ()) {
212       gboolean hardware;
213
214       g_object_get (device, "hardware", &hardware, NULL);
215       if (hardware)
216         gst_d3d11_deinterlace_register (plugin, device, GST_RANK_MARGINAL);
217     }
218 #endif
219
220     gst_object_unref (device);
221   }
222
223   /* FIXME: Our shader code is not compatible with D3D_FEATURE_LEVEL_9_3
224    * or lower. So HLSL compiler cannot understand our shader code and
225    * therefore d3d11colorconverter cannot be configured.
226    *
227    * Known D3D_FEATURE_LEVEL_9_3 driver is
228    * "VirtualBox Graphics Adapter (WDDM)"
229    * ... and there might be some more old physical devices which don't support
230    * D3D_FEATURE_LEVEL_10_0.
231    */
232   if (max_feature_level < D3D_FEATURE_LEVEL_10_0)
233     video_sink_rank = GST_RANK_NONE;
234
235   gst_d3d11_plugin_utils_init (max_feature_level);
236
237   gst_element_register (plugin,
238       "d3d11upload", GST_RANK_NONE, GST_TYPE_D3D11_UPLOAD);
239   gst_element_register (plugin,
240       "d3d11download", GST_RANK_NONE, GST_TYPE_D3D11_DOWNLOAD);
241   gst_element_register (plugin,
242       "d3d11convert", GST_RANK_NONE, GST_TYPE_D3D11_CONVERT);
243   gst_element_register (plugin,
244       "d3d11colorconvert", GST_RANK_NONE, GST_TYPE_D3D11_COLOR_CONVERT);
245   gst_element_register (plugin,
246       "d3d11scale", GST_RANK_NONE, GST_TYPE_D3D11_SCALE);
247   gst_element_register (plugin,
248       "d3d11videosink", video_sink_rank, GST_TYPE_D3D11_VIDEO_SINK);
249
250   gst_element_register (plugin,
251       "d3d11compositorelement", GST_RANK_NONE, GST_TYPE_D3D11_COMPOSITOR);
252   gst_element_register (plugin,
253       "d3d11compositor", GST_RANK_SECONDARY, GST_TYPE_D3D11_COMPOSITOR_BIN);
254
255 #ifdef HAVE_DXGI_DESKTOP_DUP
256   if (gst_d3d11_is_windows_8_or_greater ()) {
257     GST_DEBUG_CATEGORY_INIT (gst_d3d11_screen_capture_debug,
258         "d3d11screencapturesrc", 0, "d3d11screencapturesrc");
259     GST_DEBUG_CATEGORY_INIT (gst_d3d11_screen_capture_device_debug,
260         "d3d11screencapturedevice", 0, "d3d11screencapturedevice");
261
262     gst_element_register (plugin,
263         "d3d11screencapturesrc", GST_RANK_NONE,
264         GST_TYPE_D3D11_SCREEN_CAPTURE_SRC);
265     gst_device_provider_register (plugin,
266         "d3d11screencapturedeviceprovider", GST_RANK_PRIMARY,
267         GST_TYPE_D3D11_SCREEN_CAPTURE_DEVICE_PROVIDER);
268   }
269 #endif
270
271   return TRUE;
272 }
273
274 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
275     GST_VERSION_MINOR,
276     d3d11,
277     "Direct3D11 plugin",
278     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)