a907fab46f32daaf75746bdd3c319f6354b80580
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / d3d11 / gstd3d11videoprocessor.cpp
1 /* GStreamer
2  * Copyright (C) <2020> 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 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "gstd3d11videoprocessor.h"
25 #include "gstd3d11pluginutils.h"
26
27 #include <string.h>
28
29 GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_video_processor_debug);
30 #define GST_CAT_DEFAULT gst_d3d11_video_processor_debug
31
32 #if (GST_D3D11_HEADER_VERSION >= 1 && GST_D3D11_DXGI_HEADER_VERSION >= 4)
33 #define HAVE_VIDEO_CONTEXT_ONE
34 #endif
35
36 #if (GST_D3D11_HEADER_VERSION >= 4) && (GST_D3D11_DXGI_HEADER_VERSION >= 5)
37 #define HAVE_VIDEO_CONTEXT_TWO
38 #endif
39
40 struct _GstD3D11VideoProcessor
41 {
42   GstD3D11Device *device;
43
44   ID3D11VideoDevice *video_device;
45   ID3D11VideoContext *video_context;
46 #ifdef HAVE_VIDEO_CONTEXT_ONE
47   ID3D11VideoContext1 *video_context1;
48 #endif
49 #ifdef HAVE_VIDEO_CONTEXT_TWO
50   ID3D11VideoContext2 *video_context2;
51 #endif
52   ID3D11VideoProcessor *processor;
53   ID3D11VideoProcessorEnumerator *enumerator;
54 #ifdef HAVE_VIDEO_CONTEXT_ONE
55   ID3D11VideoProcessorEnumerator1 *enumerator1;
56 #endif
57   D3D11_VIDEO_PROCESSOR_CAPS processor_caps;
58 };
59
60 GstD3D11VideoProcessor *
61 gst_d3d11_video_processor_new (GstD3D11Device * device, guint in_width,
62     guint in_height, guint out_width, guint out_height)
63 {
64   GstD3D11VideoProcessor *self;
65   ID3D11VideoDevice *video_device;
66   ID3D11VideoContext *video_context;
67   HRESULT hr;
68   D3D11_VIDEO_PROCESSOR_CONTENT_DESC desc;
69
70   g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
71
72   video_device = gst_d3d11_device_get_video_device_handle (device);
73   if (!video_device) {
74     GST_WARNING_OBJECT (device, "ID3D11VideoDevice is not available");
75     return NULL;
76   }
77
78   video_context = gst_d3d11_device_get_video_context_handle (device);
79   if (!video_context) {
80     GST_WARNING_OBJECT (device, "ID3D11VideoContext is not availale");
81     return NULL;
82   }
83
84   memset (&desc, 0, sizeof (desc));
85
86   self = g_new0 (GstD3D11VideoProcessor, 1);
87   self->device = (GstD3D11Device *) gst_object_ref (device);
88
89   self->video_device = video_device;
90   video_device->AddRef ();
91
92   self->video_context = video_context;
93   video_context->AddRef ();
94
95   /* FIXME: Add support intelace */
96   desc.InputFrameFormat = D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE;
97   desc.InputWidth = in_width;
98   desc.InputHeight = in_height;
99   desc.OutputWidth = out_width;
100   desc.OutputHeight = out_height;
101   /* TODO: make option for this */
102   desc.Usage = D3D11_VIDEO_USAGE_PLAYBACK_NORMAL;
103
104   hr = self->video_device->CreateVideoProcessorEnumerator (&desc,
105       &self->enumerator);
106   if (!gst_d3d11_result (hr, device))
107     goto fail;
108 #ifdef HAVE_VIDEO_CONTEXT_ONE
109   hr = self->enumerator->QueryInterface (IID_PPV_ARGS (&self->enumerator1));
110   if (gst_d3d11_result (hr, device)) {
111     GST_DEBUG ("ID3D11VideoProcessorEnumerator1 interface available");
112   }
113 #endif
114
115   hr = self->enumerator->GetVideoProcessorCaps (&self->processor_caps);
116   if (!gst_d3d11_result (hr, device))
117     goto fail;
118
119   hr = self->video_device->CreateVideoProcessor (self->enumerator, 0,
120       &self->processor);
121   if (!gst_d3d11_result (hr, device))
122     goto fail;
123
124 #ifdef HAVE_VIDEO_CONTEXT_ONE
125   hr = self->video_context->
126       QueryInterface (IID_PPV_ARGS (&self->video_context1));
127   if (gst_d3d11_result (hr, device)) {
128     GST_DEBUG ("ID3D11VideoContext1 interface available");
129   }
130 #endif
131 #ifdef HAVE_VIDEO_CONTEXT_TWO
132   hr = self->video_context->
133       QueryInterface (IID_PPV_ARGS (&self->video_context2));
134   if (gst_d3d11_result (hr, device)) {
135     GST_DEBUG ("ID3D11VideoContext2 interface available");
136   }
137 #endif
138
139   /* Setting up default options */
140   gst_d3d11_device_lock (self->device);
141   /* We don't want auto processing by driver */
142   self->video_context->VideoProcessorSetStreamAutoProcessingMode
143       (self->processor, 0, FALSE);
144   gst_d3d11_device_unlock (self->device);
145
146   return self;
147
148 fail:
149   gst_d3d11_video_processor_free (self);
150
151   return NULL;
152 }
153
154 void
155 gst_d3d11_video_processor_free (GstD3D11VideoProcessor * processor)
156 {
157   g_return_if_fail (processor != NULL);
158
159   GST_D3D11_CLEAR_COM (processor->video_device);
160   GST_D3D11_CLEAR_COM (processor->video_context);
161 #ifdef HAVE_VIDEO_CONTEXT_ONE
162   GST_D3D11_CLEAR_COM (processor->video_context1);
163 #endif
164 #ifdef HAVE_VIDEO_CONTEXT_TWO
165   GST_D3D11_CLEAR_COM (processor->video_context2);
166 #endif
167   GST_D3D11_CLEAR_COM (processor->processor);
168   GST_D3D11_CLEAR_COM (processor->enumerator);
169 #ifdef HAVE_VIDEO_CONTEXT_ONE
170   GST_D3D11_CLEAR_COM (processor->enumerator1);
171 #endif
172
173   gst_clear_object (&processor->device);
174   g_free (processor);
175 }
176
177 static gboolean
178 gst_d3d11_video_processor_supports_format (GstD3D11VideoProcessor *
179     self, DXGI_FORMAT format, gboolean is_input)
180 {
181   HRESULT hr;
182   UINT flag = 0;
183
184   hr = self->enumerator->CheckVideoProcessorFormat (format, &flag);
185
186   if (!gst_d3d11_result (hr, self->device))
187     return FALSE;
188
189   if (is_input) {
190     /* D3D11_VIDEO_PROCESSOR_FORMAT_SUPPORT_INPUT, missing in mingw header */
191     if ((flag & 0x1) != 0)
192       return TRUE;
193   } else {
194     /* D3D11_VIDEO_PROCESSOR_FORMAT_SUPPORT_OUTPUT, missing in mingw header */
195     if ((flag & 0x2) != 0)
196       return TRUE;
197   }
198
199   return FALSE;
200 }
201
202 gboolean
203 gst_d3d11_video_processor_supports_input_format (GstD3D11VideoProcessor *
204     processor, DXGI_FORMAT format)
205 {
206   g_return_val_if_fail (processor != NULL, FALSE);
207
208   if (format == DXGI_FORMAT_UNKNOWN)
209     return FALSE;
210
211   return gst_d3d11_video_processor_supports_format (processor, format, TRUE);
212 }
213
214 gboolean
215 gst_d3d11_video_processor_supports_output_format (GstD3D11VideoProcessor *
216     processor, DXGI_FORMAT format)
217 {
218   g_return_val_if_fail (processor != NULL, FALSE);
219
220   if (format == DXGI_FORMAT_UNKNOWN)
221     return FALSE;
222
223   return gst_d3d11_video_processor_supports_format (processor, format, FALSE);
224 }
225
226 gboolean
227 gst_d3d11_video_processor_get_caps (GstD3D11VideoProcessor * processor,
228     D3D11_VIDEO_PROCESSOR_CAPS * caps)
229 {
230   g_return_val_if_fail (processor != NULL, FALSE);
231   g_return_val_if_fail (caps != NULL, FALSE);
232
233   *caps = processor->processor_caps;
234
235   return TRUE;
236 }
237
238 static void
239 video_processor_color_space_from_gst (GstD3D11VideoProcessor * self,
240     GstVideoColorimetry * color, D3D11_VIDEO_PROCESSOR_COLOR_SPACE * colorspace)
241 {
242   /* D3D11_VIDEO_PROCESSOR_DEVICE_CAPS_xvYCC */
243   UINT can_xvYCC = 2;
244
245   /* 0: playback, 1: video processing */
246   colorspace->Usage = 0;
247
248   if (color->range == GST_VIDEO_COLOR_RANGE_0_255) {
249     colorspace->RGB_Range = 0;
250     colorspace->Nominal_Range = D3D11_VIDEO_PROCESSOR_NOMINAL_RANGE_0_255;
251   } else {
252     /* 16-235 */
253     colorspace->RGB_Range = 1;
254     colorspace->Nominal_Range = D3D11_VIDEO_PROCESSOR_NOMINAL_RANGE_16_235;
255   }
256
257   if (color->matrix == GST_VIDEO_COLOR_MATRIX_BT601) {
258     colorspace->YCbCr_Matrix = 0;
259   } else {
260     /* BT.709, no other options (such as BT2020) */
261     colorspace->YCbCr_Matrix = 1;
262   }
263
264   if ((self->processor_caps.DeviceCaps & can_xvYCC) == can_xvYCC) {
265     colorspace->YCbCr_xvYCC = 1;
266   } else {
267     colorspace->YCbCr_xvYCC = 0;
268   }
269 }
270
271 gboolean
272 gst_d3d11_video_processor_set_input_color_space (GstD3D11VideoProcessor *
273     processor, GstVideoColorimetry * color)
274 {
275   D3D11_VIDEO_PROCESSOR_COLOR_SPACE color_space;
276
277   g_return_val_if_fail (processor != NULL, FALSE);
278   g_return_val_if_fail (color != NULL, FALSE);
279
280   video_processor_color_space_from_gst (processor, color, &color_space);
281
282   processor->video_context->VideoProcessorSetStreamColorSpace
283       (processor->processor, 0, &color_space);
284
285   return TRUE;
286 }
287
288 gboolean
289 gst_d3d11_video_processor_set_output_color_space (GstD3D11VideoProcessor *
290     processor, GstVideoColorimetry * color)
291 {
292   D3D11_VIDEO_PROCESSOR_COLOR_SPACE color_space;
293
294   g_return_val_if_fail (processor != NULL, FALSE);
295   g_return_val_if_fail (color != NULL, FALSE);
296
297   video_processor_color_space_from_gst (processor, color, &color_space);
298
299   processor->video_context->VideoProcessorSetOutputColorSpace
300       (processor->processor, &color_space);
301
302   return TRUE;
303 }
304
305 #if (GST_D3D11_DXGI_HEADER_VERSION >= 4)
306 gboolean
307 gst_d3d11_video_processor_check_format_conversion (GstD3D11VideoProcessor *
308     processor, DXGI_FORMAT in_format, DXGI_COLOR_SPACE_TYPE in_color_space,
309     DXGI_FORMAT out_format, DXGI_COLOR_SPACE_TYPE out_color_space)
310 {
311 #ifdef HAVE_VIDEO_CONTEXT_ONE
312   HRESULT hr;
313   BOOL supported = TRUE;
314
315   g_return_val_if_fail (processor != NULL, FALSE);
316
317   if (!processor->enumerator1)
318     return FALSE;
319
320   hr = processor->enumerator1->CheckVideoProcessorFormatConversion
321       (in_format, in_color_space, out_format, out_color_space, &supported);
322   if (!gst_d3d11_result (hr, processor->device)) {
323     GST_WARNING ("Failed to check conversion support");
324     return FALSE;
325   }
326
327   return supported;
328 #endif
329
330   return FALSE;
331 }
332
333 gboolean
334 gst_d3d11_video_processor_set_input_dxgi_color_space (GstD3D11VideoProcessor *
335     processor, DXGI_COLOR_SPACE_TYPE color_space)
336 {
337   g_return_val_if_fail (processor != NULL, FALSE);
338
339 #ifdef HAVE_VIDEO_CONTEXT_ONE
340   if (processor->video_context1) {
341     processor->video_context1->VideoProcessorSetStreamColorSpace1
342         (processor->processor, 0, color_space);
343     return TRUE;
344   }
345 #endif
346
347   return FALSE;
348 }
349
350 gboolean
351 gst_d3d11_video_processor_set_output_dxgi_color_space (GstD3D11VideoProcessor *
352     processor, DXGI_COLOR_SPACE_TYPE color_space)
353 {
354   g_return_val_if_fail (processor != NULL, FALSE);
355
356 #ifdef HAVE_VIDEO_CONTEXT_ONE
357   if (processor->video_context1) {
358     processor->video_context1->VideoProcessorSetOutputColorSpace1
359         (processor->processor, color_space);
360     return TRUE;
361   }
362 #endif
363
364   return FALSE;
365 }
366 #endif
367
368 #if (GST_D3D11_DXGI_HEADER_VERSION >= 5)
369 /* D3D11_VIDEO_PROCESSOR_FEATURE_CAPS_METADATA_HDR10
370  * missing in mingw header */
371 #define FEATURE_CAPS_METADATA_HDR10 (0x800)
372
373 gboolean
374 gst_d3d11_video_processor_set_input_hdr10_metadata (GstD3D11VideoProcessor *
375     processor, DXGI_HDR_METADATA_HDR10 * hdr10_meta)
376 {
377   g_return_val_if_fail (processor != NULL, FALSE);
378
379 #ifdef HAVE_VIDEO_CONTEXT_TWO
380   if (processor->video_context2 && (processor->processor_caps.FeatureCaps &
381           FEATURE_CAPS_METADATA_HDR10)) {
382     if (hdr10_meta) {
383       processor->video_context2->VideoProcessorSetStreamHDRMetaData
384           (processor->processor, 0,
385           DXGI_HDR_METADATA_TYPE_HDR10, sizeof (DXGI_HDR_METADATA_HDR10),
386           hdr10_meta);
387     } else {
388       processor->video_context2->VideoProcessorSetStreamHDRMetaData
389           (processor->processor, 0, DXGI_HDR_METADATA_TYPE_NONE, 0, NULL);
390     }
391
392     return TRUE;
393   }
394 #endif
395
396   return FALSE;
397 }
398
399 gboolean
400 gst_d3d11_video_processor_set_output_hdr10_metadata (GstD3D11VideoProcessor *
401     processor, DXGI_HDR_METADATA_HDR10 * hdr10_meta)
402 {
403   g_return_val_if_fail (processor != NULL, FALSE);
404
405 #ifdef HAVE_VIDEO_CONTEXT_TWO
406   if (processor->video_context2 && (processor->processor_caps.FeatureCaps &
407           FEATURE_CAPS_METADATA_HDR10)) {
408     if (hdr10_meta) {
409       processor->video_context2->VideoProcessorSetOutputHDRMetaData
410           (processor->processor, DXGI_HDR_METADATA_TYPE_HDR10,
411           sizeof (DXGI_HDR_METADATA_HDR10), hdr10_meta);
412     } else {
413       processor->video_context2->VideoProcessorSetOutputHDRMetaData
414           (processor->processor, DXGI_HDR_METADATA_TYPE_NONE, 0, NULL);
415     }
416
417     return TRUE;
418   }
419 #endif
420
421   return FALSE;
422 }
423 #endif
424
425 gboolean
426 gst_d3d11_video_processor_create_input_view (GstD3D11VideoProcessor * processor,
427     D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC * desc, ID3D11Resource * resource,
428     ID3D11VideoProcessorInputView ** view)
429 {
430   HRESULT hr;
431
432   g_return_val_if_fail (processor != NULL, FALSE);
433   g_return_val_if_fail (desc != NULL, FALSE);
434   g_return_val_if_fail (resource != NULL, FALSE);
435   g_return_val_if_fail (view != NULL, FALSE);
436
437   hr = processor->video_device->CreateVideoProcessorInputView (resource,
438       processor->enumerator, desc, view);
439   if (!gst_d3d11_result (hr, processor->device))
440     return FALSE;
441
442   return TRUE;
443 }
444
445 ID3D11VideoProcessorInputView *
446 gst_d3d11_video_processor_get_input_view (GstD3D11VideoProcessor * processor,
447     GstD3D11Memory * mem)
448 {
449   return gst_d3d11_memory_get_processor_input_view (mem,
450       processor->video_device, processor->enumerator);
451 }
452
453 gboolean
454 gst_d3d11_video_processor_create_output_view (GstD3D11VideoProcessor *
455     processor, D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC * desc,
456     ID3D11Resource * resource, ID3D11VideoProcessorOutputView ** view)
457 {
458   HRESULT hr;
459
460   g_return_val_if_fail (processor != NULL, FALSE);
461   g_return_val_if_fail (desc != NULL, FALSE);
462   g_return_val_if_fail (resource != NULL, FALSE);
463   g_return_val_if_fail (view != NULL, FALSE);
464
465   hr = processor->video_device->CreateVideoProcessorOutputView
466       (resource, processor->enumerator, desc, view);
467   if (!gst_d3d11_result (hr, processor->device))
468     return FALSE;
469
470   return TRUE;
471 }
472
473 ID3D11VideoProcessorOutputView *
474 gst_d3d11_video_processor_get_output_view (GstD3D11VideoProcessor *
475     processor, GstD3D11Memory * mem)
476 {
477   return gst_d3d11_memory_get_processor_output_view (mem,
478       processor->video_device, processor->enumerator);
479 }
480
481 gboolean
482 gst_d3d11_video_processor_render (GstD3D11VideoProcessor * processor,
483     RECT * in_rect, ID3D11VideoProcessorInputView * in_view,
484     RECT * out_rect, ID3D11VideoProcessorOutputView * out_view)
485 {
486   gboolean ret;
487
488   g_return_val_if_fail (processor != NULL, FALSE);
489   g_return_val_if_fail (in_view != NULL, FALSE);
490   g_return_val_if_fail (out_view != NULL, FALSE);
491
492   gst_d3d11_device_lock (processor->device);
493   ret = gst_d3d11_video_processor_render_unlocked (processor, in_rect, in_view,
494       out_rect, out_view);
495   gst_d3d11_device_unlock (processor->device);
496
497   return ret;
498 }
499
500 gboolean
501 gst_d3d11_video_processor_render_unlocked (GstD3D11VideoProcessor * processor,
502     RECT * in_rect, ID3D11VideoProcessorInputView * in_view,
503     RECT * out_rect, ID3D11VideoProcessorOutputView * out_view)
504 {
505   HRESULT hr;
506   D3D11_VIDEO_PROCESSOR_STREAM stream = { 0, };
507   ID3D11VideoContext *context;
508   ID3D11VideoProcessor *proc;
509
510   g_return_val_if_fail (processor != NULL, FALSE);
511   g_return_val_if_fail (in_view != NULL, FALSE);
512   g_return_val_if_fail (out_view != NULL, FALSE);
513
514   stream.Enable = TRUE;
515   stream.pInputSurface = in_view;
516   context = processor->video_context;
517   proc = processor->processor;
518
519   if (in_rect) {
520     context->VideoProcessorSetStreamSourceRect (proc, 0, TRUE, in_rect);
521   } else {
522     context->VideoProcessorSetStreamSourceRect (proc, 0, FALSE, NULL);
523   }
524
525   if (out_rect) {
526     context->VideoProcessorSetStreamDestRect (proc, 0, TRUE, out_rect);
527     context->VideoProcessorSetOutputTargetRect (proc, TRUE, out_rect);
528   } else {
529     context->VideoProcessorSetStreamDestRect (proc, 0, FALSE, NULL);
530     context->VideoProcessorSetOutputTargetRect (proc, FALSE, NULL);
531   }
532
533   hr = context->VideoProcessorBlt (proc, out_view, 0, 1, &stream);
534   if (!gst_d3d11_result (hr, processor->device))
535     return FALSE;
536
537   return TRUE;
538 }
539
540 gboolean
541 gst_d3d11_video_processor_check_bind_flags_for_input_view (guint bind_flags)
542 {
543   static const guint compatible_flags = (D3D11_BIND_DECODER |
544       D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_RENDER_TARGET |
545       D3D11_BIND_UNORDERED_ACCESS);
546
547   if (bind_flags == 0)
548     return TRUE;
549
550   if ((bind_flags & compatible_flags) != 0)
551     return TRUE;
552
553   return FALSE;
554 }
555
556 gboolean
557 gst_d3d11_video_processor_check_bind_flags_for_output_view (guint bind_flags)
558 {
559   if ((bind_flags & D3D11_BIND_RENDER_TARGET) == D3D11_BIND_RENDER_TARGET)
560     return TRUE;
561
562   return FALSE;
563 }