2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2005-2012 David Schleef <ds@schleef.org>
4 * Copyright (C) 2012-2014 Matthew Waters <ystree00@gmail.com>
5 * Copyright (C) <2019> Seungha Yang <seungha.yang@navercorp.com>
6 * Copyright (C) <2019> Jeongki Kim <jeongki.kim@jeongki.kim>
7 * Copyright (C) 2020 Thibault Saunier <tsaunier@igalia.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
29 #include "gstd3d11convert.h"
30 #include "gstd3d11converter.h"
31 #include "gstd3d11videoprocessor.h"
32 #include "gstd3d11pluginutils.h"
34 GST_DEBUG_CATEGORY_STATIC (gst_d3d11_convert_debug);
35 #define GST_CAT_DEFAULT gst_d3d11_convert_debug
37 static GstStaticCaps sink_template_caps =
38 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
39 (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, GST_D3D11_SINK_FORMATS) "; "
40 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
41 (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY ","
42 GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION,
43 GST_D3D11_SINK_FORMATS));
45 static GstStaticCaps src_template_caps =
46 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
47 (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, GST_D3D11_SRC_FORMATS) "; "
48 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
49 (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY ","
50 GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION,
51 GST_D3D11_SRC_FORMATS));
53 #define DEFAULT_ADD_BORDERS TRUE
55 struct _GstD3D11BaseConvert
57 GstD3D11BaseFilter parent;
59 const GstD3D11Format *in_d3d11_format;
60 const GstD3D11Format *out_d3d11_format;
62 ID3D11Texture2D *in_texture[GST_VIDEO_MAX_PLANES];
63 ID3D11ShaderResourceView *shader_resource_view[GST_VIDEO_MAX_PLANES];
66 ID3D11Texture2D *out_texture[GST_VIDEO_MAX_PLANES];
67 ID3D11RenderTargetView *render_target_view[GST_VIDEO_MAX_PLANES];
68 guint num_output_view;
70 GstD3D11Converter *converter;
71 GstD3D11VideoProcessor *processor;
72 gboolean processor_in_use;
74 /* used for border rendering */
81 /* Updated by subclass */
86 * GstD3D11BaseConvert:
88 * A baseclass implementation for d3d11 convert elements
92 #define gst_d3d11_base_convert_parent_class parent_class
93 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstD3D11BaseConvert, gst_d3d11_base_convert,
94 GST_TYPE_D3D11_BASE_FILTER,
95 GST_DEBUG_CATEGORY_INIT (gst_d3d11_convert_debug, "d3d11convert", 0,
98 static void gst_d3d11_base_convert_dispose (GObject * object);
99 static GstCaps *gst_d3d11_base_convert_transform_caps (GstBaseTransform *
100 trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
101 static GstCaps *gst_d3d11_base_convert_fixate_caps (GstBaseTransform *
102 base, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
103 static gboolean gst_d3d11_base_convert_filter_meta (GstBaseTransform * trans,
104 GstQuery * query, GType api, const GstStructure * params);
106 gst_d3d11_base_convert_propose_allocation (GstBaseTransform * trans,
107 GstQuery * decide_query, GstQuery * query);
109 gst_d3d11_base_convert_decide_allocation (GstBaseTransform * trans,
112 static GstFlowReturn gst_d3d11_base_convert_transform (GstBaseTransform *
113 trans, GstBuffer * inbuf, GstBuffer * outbuf);
114 static gboolean gst_d3d11_base_convert_set_info (GstD3D11BaseFilter * filter,
115 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
116 GstVideoInfo * out_info);
118 /* copies the given caps */
120 gst_d3d11_base_convert_caps_remove_format_info (GstCaps * caps)
126 GstCapsFeatures *feature =
127 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY);
129 res = gst_caps_new_empty ();
131 n = gst_caps_get_size (caps);
132 for (i = 0; i < n; i++) {
133 st = gst_caps_get_structure (caps, i);
134 f = gst_caps_get_features (caps, i);
136 /* If this is already expressed by the existing caps
137 * skip this structure */
138 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
141 st = gst_structure_copy (st);
142 /* Only remove format info for the cases when we can actually convert */
143 if (!gst_caps_features_is_any (f)
144 && gst_caps_features_is_equal (f, feature)) {
145 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
149 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
151 gst_caps_features_free (feature);
157 gst_d3d11_base_convert_caps_rangify_size_info (GstCaps * caps)
163 GstCapsFeatures *feature =
164 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY);
166 res = gst_caps_new_empty ();
168 n = gst_caps_get_size (caps);
169 for (i = 0; i < n; i++) {
170 st = gst_caps_get_structure (caps, i);
171 f = gst_caps_get_features (caps, i);
173 /* If this is already expressed by the existing caps
174 * skip this structure */
175 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
178 st = gst_structure_copy (st);
179 /* Only remove format info for the cases when we can actually convert */
180 if (!gst_caps_features_is_any (f)
181 && gst_caps_features_is_equal (f, feature)) {
182 gst_structure_set (st, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
183 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
185 /* if pixel aspect ratio, make a range of it */
186 if (gst_structure_has_field (st, "pixel-aspect-ratio")) {
187 gst_structure_set (st, "pixel-aspect-ratio",
188 GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1, NULL);
192 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
194 gst_caps_features_free (feature);
200 gst_d3d11_base_convert_caps_remove_format_and_rangify_size_info (GstCaps * caps)
206 GstCapsFeatures *feature =
207 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY);
209 res = gst_caps_new_empty ();
211 n = gst_caps_get_size (caps);
212 for (i = 0; i < n; i++) {
213 st = gst_caps_get_structure (caps, i);
214 f = gst_caps_get_features (caps, i);
216 /* If this is already expressed by the existing caps
217 * skip this structure */
218 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
221 st = gst_structure_copy (st);
222 /* Only remove format info for the cases when we can actually convert */
223 if (!gst_caps_features_is_any (f)
224 && gst_caps_features_is_equal (f, feature)) {
225 gst_structure_set (st, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
226 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
227 /* if pixel aspect ratio, make a range of it */
228 if (gst_structure_has_field (st, "pixel-aspect-ratio")) {
229 gst_structure_set (st, "pixel-aspect-ratio",
230 GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1, NULL);
232 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
236 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
238 gst_caps_features_free (feature);
244 gst_d3d11_base_convert_class_init (GstD3D11BaseConvertClass * klass)
246 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
247 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
248 GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
249 GstD3D11BaseFilterClass *bfilter_class = GST_D3D11_BASE_FILTER_CLASS (klass);
252 gobject_class->dispose = gst_d3d11_base_convert_dispose;
254 caps = gst_d3d11_get_updated_template_caps (&sink_template_caps);
255 gst_element_class_add_pad_template (element_class,
256 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
257 gst_caps_unref (caps);
259 caps = gst_d3d11_get_updated_template_caps (&src_template_caps);
260 gst_element_class_add_pad_template (element_class,
261 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps));
262 gst_caps_unref (caps);
264 trans_class->passthrough_on_same_caps = TRUE;
266 trans_class->transform_caps =
267 GST_DEBUG_FUNCPTR (gst_d3d11_base_convert_transform_caps);
268 trans_class->fixate_caps =
269 GST_DEBUG_FUNCPTR (gst_d3d11_base_convert_fixate_caps);
270 trans_class->filter_meta =
271 GST_DEBUG_FUNCPTR (gst_d3d11_base_convert_filter_meta);
272 trans_class->propose_allocation =
273 GST_DEBUG_FUNCPTR (gst_d3d11_base_convert_propose_allocation);
274 trans_class->decide_allocation =
275 GST_DEBUG_FUNCPTR (gst_d3d11_base_convert_decide_allocation);
276 trans_class->transform = GST_DEBUG_FUNCPTR (gst_d3d11_base_convert_transform);
278 bfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_d3d11_base_convert_set_info);
280 gst_type_mark_as_plugin_api (GST_TYPE_D3D11_BASE_CONVERT,
281 (GstPluginAPIFlags) 0);
285 gst_d3d11_base_convert_init (GstD3D11BaseConvert * self)
287 self->add_borders = DEFAULT_ADD_BORDERS;
291 gst_d3d11_base_convert_clear_shader_resource (GstD3D11BaseConvert * self)
295 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
296 GST_D3D11_CLEAR_COM (self->shader_resource_view[i]);
297 GST_D3D11_CLEAR_COM (self->render_target_view[i]);
300 self->num_input_view = 0;
301 self->num_output_view = 0;
303 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
304 GST_D3D11_CLEAR_COM (self->in_texture[i]);
305 GST_D3D11_CLEAR_COM (self->out_texture[i]);
308 g_clear_pointer (&self->converter, gst_d3d11_converter_free);
309 g_clear_pointer (&self->processor, gst_d3d11_video_processor_free);
311 self->processor_in_use = FALSE;
315 gst_d3d11_base_convert_dispose (GObject * object)
317 GstD3D11BaseConvert *self = GST_D3D11_BASE_CONVERT (object);
319 gst_d3d11_base_convert_clear_shader_resource (self);
321 G_OBJECT_CLASS (parent_class)->dispose (object);
325 gst_d3d11_base_convert_transform_caps (GstBaseTransform *
326 trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter)
331 /* Get all possible caps that we can transform to */
332 tmp = gst_d3d11_base_convert_caps_remove_format_and_rangify_size_info (caps);
335 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
336 gst_caps_unref (tmp);
342 GST_DEBUG_OBJECT (trans, "transformed %" GST_PTR_FORMAT " into %"
343 GST_PTR_FORMAT, caps, result);
349 * This is an incomplete matrix of in formats and a score for the prefered output
352 * out: RGB24 RGB16 ARGB AYUV YUV444 YUV422 YUV420 YUV411 YUV410 PAL GRAY
354 * RGB24 0 2 1 2 2 3 4 5 6 7 8
355 * RGB16 1 0 1 2 2 3 4 5 6 7 8
356 * ARGB 2 3 0 1 4 5 6 7 8 9 10
357 * AYUV 3 4 1 0 2 5 6 7 8 9 10
358 * YUV444 2 4 3 1 0 5 6 7 8 9 10
359 * YUV422 3 5 4 2 1 0 6 7 8 9 10
360 * YUV420 4 6 5 3 2 1 0 7 8 9 10
361 * YUV411 4 6 5 3 2 1 7 0 8 9 10
362 * YUV410 6 8 7 5 4 3 2 1 0 9 10
363 * PAL 1 3 2 6 4 6 7 8 9 0 10
364 * GRAY 1 4 3 2 1 5 6 7 8 9 0
366 * PAL or GRAY are never prefered, if we can we would convert to PAL instead
368 * less subsampling is prefered and if any, preferably horizontal
369 * We would like to keep the alpha, even if we would need to to colorspace conversion
372 #define SCORE_FORMAT_CHANGE 1
373 #define SCORE_DEPTH_CHANGE 1
374 #define SCORE_ALPHA_CHANGE 1
375 #define SCORE_CHROMA_W_CHANGE 1
376 #define SCORE_CHROMA_H_CHANGE 1
377 #define SCORE_PALETTE_CHANGE 1
379 #define SCORE_COLORSPACE_LOSS 2 /* RGB <-> YUV */
380 #define SCORE_DEPTH_LOSS 4 /* change bit depth */
381 #define SCORE_ALPHA_LOSS 8 /* lose the alpha channel */
382 #define SCORE_CHROMA_W_LOSS 16 /* vertical subsample */
383 #define SCORE_CHROMA_H_LOSS 32 /* horizontal subsample */
384 #define SCORE_PALETTE_LOSS 64 /* convert to palette format */
385 #define SCORE_COLOR_LOSS 128 /* convert to GRAY */
387 #define COLORSPACE_MASK (GST_VIDEO_FORMAT_FLAG_YUV | \
388 GST_VIDEO_FORMAT_FLAG_RGB | GST_VIDEO_FORMAT_FLAG_GRAY)
389 #define ALPHA_MASK (GST_VIDEO_FORMAT_FLAG_ALPHA)
390 #define PALETTE_MASK (GST_VIDEO_FORMAT_FLAG_PALETTE)
392 /* calculate how much loss a conversion would be */
394 score_value (GstBaseTransform * base, const GstVideoFormatInfo * in_info,
395 const GValue * val, gint * min_loss, const GstVideoFormatInfo ** out_info)
398 const GstVideoFormatInfo *t_info;
399 guint in_flags, t_flags;
402 fname = g_value_get_string (val);
403 t_info = gst_video_format_get_info (gst_video_format_from_string (fname));
404 if (!t_info || t_info->format == GST_VIDEO_FORMAT_UNKNOWN)
407 /* accept input format immediately without loss */
408 if (in_info == t_info) {
414 loss = SCORE_FORMAT_CHANGE;
416 in_flags = GST_VIDEO_FORMAT_INFO_FLAGS (in_info);
417 in_flags &= ~GST_VIDEO_FORMAT_FLAG_LE;
418 in_flags &= ~GST_VIDEO_FORMAT_FLAG_COMPLEX;
419 in_flags &= ~GST_VIDEO_FORMAT_FLAG_UNPACK;
421 t_flags = GST_VIDEO_FORMAT_INFO_FLAGS (t_info);
422 t_flags &= ~GST_VIDEO_FORMAT_FLAG_LE;
423 t_flags &= ~GST_VIDEO_FORMAT_FLAG_COMPLEX;
424 t_flags &= ~GST_VIDEO_FORMAT_FLAG_UNPACK;
426 if ((t_flags & PALETTE_MASK) != (in_flags & PALETTE_MASK)) {
427 loss += SCORE_PALETTE_CHANGE;
428 if (t_flags & PALETTE_MASK)
429 loss += SCORE_PALETTE_LOSS;
432 if ((t_flags & COLORSPACE_MASK) != (in_flags & COLORSPACE_MASK)) {
433 loss += SCORE_COLORSPACE_LOSS;
434 if (t_flags & GST_VIDEO_FORMAT_FLAG_GRAY)
435 loss += SCORE_COLOR_LOSS;
438 if ((t_flags & ALPHA_MASK) != (in_flags & ALPHA_MASK)) {
439 loss += SCORE_ALPHA_CHANGE;
440 if (in_flags & ALPHA_MASK)
441 loss += SCORE_ALPHA_LOSS;
444 if ((in_info->h_sub[1]) != (t_info->h_sub[1])) {
445 loss += SCORE_CHROMA_H_CHANGE;
446 if ((in_info->h_sub[1]) < (t_info->h_sub[1]))
447 loss += SCORE_CHROMA_H_LOSS;
449 if ((in_info->w_sub[1]) != (t_info->w_sub[1])) {
450 loss += SCORE_CHROMA_W_CHANGE;
451 if ((in_info->w_sub[1]) < (t_info->w_sub[1]))
452 loss += SCORE_CHROMA_W_LOSS;
455 if ((in_info->bits) != (t_info->bits)) {
456 loss += SCORE_DEPTH_CHANGE;
457 if ((in_info->bits) > (t_info->bits))
458 loss += SCORE_DEPTH_LOSS + (in_info->bits - t_info->bits);
461 GST_DEBUG_OBJECT (base, "score %s -> %s = %d",
462 GST_VIDEO_FORMAT_INFO_NAME (in_info),
463 GST_VIDEO_FORMAT_INFO_NAME (t_info), loss);
465 if (loss < *min_loss) {
466 GST_DEBUG_OBJECT (base, "found new best %d", loss);
473 gst_d3d11_base_convert_fixate_format (GstBaseTransform * trans,
474 GstCaps * caps, GstCaps * result)
476 GstStructure *ins, *outs;
477 const gchar *in_format;
478 const GstVideoFormatInfo *in_info, *out_info = NULL;
479 gint min_loss = G_MAXINT;
482 ins = gst_caps_get_structure (caps, 0);
483 in_format = gst_structure_get_string (ins, "format");
488 GST_DEBUG_OBJECT (trans, "source format %s", in_format);
491 gst_video_format_get_info (gst_video_format_from_string (in_format));
495 outs = gst_caps_get_structure (result, 0);
497 capslen = gst_caps_get_size (result);
498 GST_DEBUG ("iterate %d structures", capslen);
499 for (i = 0; i < capslen; i++) {
501 const GValue *format;
503 tests = gst_caps_get_structure (result, i);
504 format = gst_structure_get_value (tests, "format");
506 /* should not happen */
510 if (GST_VALUE_HOLDS_LIST (format)) {
513 len = gst_value_list_get_size (format);
514 GST_DEBUG_OBJECT (trans, "have %d formats", len);
515 for (j = 0; j < len; j++) {
518 val = gst_value_list_get_value (format, j);
519 if (G_VALUE_HOLDS_STRING (val)) {
520 score_value (trans, in_info, val, &min_loss, &out_info);
525 } else if (G_VALUE_HOLDS_STRING (format)) {
526 score_value (trans, in_info, format, &min_loss, &out_info);
530 gst_structure_set (outs, "format", G_TYPE_STRING,
531 GST_VIDEO_FORMAT_INFO_NAME (out_info), NULL);
535 subsampling_unchanged (GstVideoInfo * in_info, GstVideoInfo * out_info)
538 const GstVideoFormatInfo *in_format, *out_format;
540 if (GST_VIDEO_INFO_N_COMPONENTS (in_info) !=
541 GST_VIDEO_INFO_N_COMPONENTS (out_info))
544 in_format = in_info->finfo;
545 out_format = out_info->finfo;
547 for (i = 0; i < GST_VIDEO_INFO_N_COMPONENTS (in_info); i++) {
548 if (GST_VIDEO_FORMAT_INFO_W_SUB (in_format,
549 i) != GST_VIDEO_FORMAT_INFO_W_SUB (out_format, i))
551 if (GST_VIDEO_FORMAT_INFO_H_SUB (in_format,
552 i) != GST_VIDEO_FORMAT_INFO_H_SUB (out_format, i))
560 transfer_colorimetry_from_input (GstBaseTransform * trans, GstCaps * in_caps,
563 GstStructure *out_caps_s = gst_caps_get_structure (out_caps, 0);
564 GstStructure *in_caps_s = gst_caps_get_structure (in_caps, 0);
565 gboolean have_colorimetry =
566 gst_structure_has_field (out_caps_s, "colorimetry");
567 gboolean have_chroma_site =
568 gst_structure_has_field (out_caps_s, "chroma-site");
570 /* If the output already has colorimetry and chroma-site, stop,
571 * otherwise try and transfer what we can from the input caps */
572 if (have_colorimetry && have_chroma_site)
576 GstVideoInfo in_info, out_info;
577 const GValue *in_colorimetry =
578 gst_structure_get_value (in_caps_s, "colorimetry");
580 if (!gst_video_info_from_caps (&in_info, in_caps)) {
581 GST_WARNING_OBJECT (trans,
582 "Failed to convert sink pad caps to video info");
585 if (!gst_video_info_from_caps (&out_info, out_caps)) {
586 GST_WARNING_OBJECT (trans,
587 "Failed to convert src pad caps to video info");
591 if (!have_colorimetry && in_colorimetry != NULL) {
592 if ((GST_VIDEO_INFO_IS_YUV (&out_info)
593 && GST_VIDEO_INFO_IS_YUV (&in_info))
594 || (GST_VIDEO_INFO_IS_RGB (&out_info)
595 && GST_VIDEO_INFO_IS_RGB (&in_info))
596 || (GST_VIDEO_INFO_IS_GRAY (&out_info)
597 && GST_VIDEO_INFO_IS_GRAY (&in_info))) {
598 /* Can transfer the colorimetry intact from the input if it has it */
599 gst_structure_set_value (out_caps_s, "colorimetry", in_colorimetry);
601 gchar *colorimetry_str;
603 /* Changing between YUV/RGB - forward primaries and transfer function, but use
604 * default range and matrix.
605 * the primaries is used for conversion between RGB and XYZ (CIE 1931 coordinate).
606 * the transfer function could be another reference (e.g., HDR)
608 out_info.colorimetry.primaries = in_info.colorimetry.primaries;
609 out_info.colorimetry.transfer = in_info.colorimetry.transfer;
612 gst_video_colorimetry_to_string (&out_info.colorimetry);
613 gst_caps_set_simple (out_caps, "colorimetry", G_TYPE_STRING,
614 colorimetry_str, NULL);
615 g_free (colorimetry_str);
619 /* Only YUV output needs chroma-site. If the input was also YUV and had the same chroma
620 * subsampling, transfer the siting. If the sub-sampling is changing, then the planes get
621 * scaled anyway so there's no real reason to prefer the input siting. */
622 if (!have_chroma_site && GST_VIDEO_INFO_IS_YUV (&out_info)) {
623 if (GST_VIDEO_INFO_IS_YUV (&in_info)) {
624 const GValue *in_chroma_site =
625 gst_structure_get_value (in_caps_s, "chroma-site");
626 if (in_chroma_site != NULL
627 && subsampling_unchanged (&in_info, &out_info))
628 gst_structure_set_value (out_caps_s, "chroma-site", in_chroma_site);
635 gst_d3d11_base_convert_get_fixed_format (GstBaseTransform * trans,
636 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
640 result = gst_caps_intersect (othercaps, caps);
641 if (gst_caps_is_empty (result)) {
642 gst_caps_unref (result);
643 result = gst_caps_copy (othercaps);
646 gst_d3d11_base_convert_fixate_format (trans, caps, result);
648 /* fixate remaining fields */
649 result = gst_caps_fixate (result);
651 if (direction == GST_PAD_SINK) {
652 if (gst_caps_is_subset (caps, result)) {
653 gst_caps_replace (&result, caps);
655 /* Try and preserve input colorimetry / chroma information */
656 transfer_colorimetry_from_input (trans, caps, result);
664 gst_d3d11_base_convert_fixate_size (GstBaseTransform * base,
665 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
667 GstStructure *ins, *outs;
668 const GValue *from_par, *to_par;
669 GValue fpar = G_VALUE_INIT, tpar = G_VALUE_INIT;
671 othercaps = gst_caps_truncate (othercaps);
672 othercaps = gst_caps_make_writable (othercaps);
673 ins = gst_caps_get_structure (caps, 0);
674 outs = gst_caps_get_structure (othercaps, 0);
676 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
677 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
679 /* If we're fixating from the sinkpad we always set the PAR and
680 * assume that missing PAR on the sinkpad means 1/1 and
681 * missing PAR on the srcpad means undefined
683 if (direction == GST_PAD_SINK) {
685 g_value_init (&fpar, GST_TYPE_FRACTION);
686 gst_value_set_fraction (&fpar, 1, 1);
690 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
691 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
696 g_value_init (&tpar, GST_TYPE_FRACTION);
697 gst_value_set_fraction (&tpar, 1, 1);
700 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
704 g_value_init (&fpar, GST_TYPE_FRACTION);
705 gst_value_set_fraction (&fpar, 1, 1);
710 /* we have both PAR but they might not be fixated */
712 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
714 gint from_dar_n, from_dar_d;
717 /* from_par should be fixed */
718 g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
720 from_par_n = gst_value_get_fraction_numerator (from_par);
721 from_par_d = gst_value_get_fraction_denominator (from_par);
723 gst_structure_get_int (ins, "width", &from_w);
724 gst_structure_get_int (ins, "height", &from_h);
726 gst_structure_get_int (outs, "width", &w);
727 gst_structure_get_int (outs, "height", &h);
729 /* if both width and height are already fixed, we can't do anything
730 * about it anymore */
734 GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
736 if (!gst_value_is_fixed (to_par)) {
737 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
738 from_par_n, from_par_d, w, h)) {
739 GST_DEBUG_OBJECT (base, "fixating to_par to %dx%d", n, d);
740 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
741 gst_structure_fixate_field_nearest_fraction (outs,
742 "pixel-aspect-ratio", n, d);
744 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
751 /* Calculate input DAR */
752 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
753 &from_dar_n, &from_dar_d)) {
754 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
755 ("Error calculating the output scaled size - integer overflow"));
759 GST_DEBUG_OBJECT (base, "Input DAR is %d/%d", from_dar_n, from_dar_d);
761 /* If either width or height are fixed there's not much we
762 * can do either except choosing a height or width and PAR
763 * that matches the DAR as good as possible
767 gint set_w, set_par_n, set_par_d;
769 GST_DEBUG_OBJECT (base, "height is fixed (%d)", h);
771 /* If the PAR is fixed too, there's not much to do
772 * except choosing the width that is nearest to the
773 * width with the same DAR */
774 if (gst_value_is_fixed (to_par)) {
775 to_par_n = gst_value_get_fraction_numerator (to_par);
776 to_par_d = gst_value_get_fraction_denominator (to_par);
778 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
780 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
781 to_par_n, &num, &den)) {
782 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
783 ("Error calculating the output scaled size - integer overflow"));
787 w = (guint) gst_util_uint64_scale_int_round (h, num, den);
788 gst_structure_fixate_field_nearest_int (outs, "width", w);
793 /* The PAR is not fixed and it's quite likely that we can set
794 * an arbitrary PAR. */
796 /* Check if we can keep the input width */
797 tmp = gst_structure_copy (outs);
798 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
799 gst_structure_get_int (tmp, "width", &set_w);
801 /* Might have failed but try to keep the DAR nonetheless by
802 * adjusting the PAR */
803 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
804 &to_par_n, &to_par_d)) {
805 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
806 ("Error calculating the output scaled size - integer overflow"));
807 gst_structure_free (tmp);
811 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
812 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
813 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
815 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
817 gst_structure_free (tmp);
819 /* Check if the adjusted PAR is accepted */
820 if (set_par_n == to_par_n && set_par_d == to_par_d) {
821 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
822 set_par_n != set_par_d)
823 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
824 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
829 /* Otherwise scale the width to the new PAR and check if the
830 * adjusted with is accepted. If all that fails we can't keep
832 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
833 set_par_n, &num, &den)) {
834 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
835 ("Error calculating the output scaled size - integer overflow"));
839 w = (guint) gst_util_uint64_scale_int_round (h, num, den);
840 gst_structure_fixate_field_nearest_int (outs, "width", w);
841 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
842 set_par_n != set_par_d)
843 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
844 set_par_n, set_par_d, NULL);
849 gint set_h, set_par_n, set_par_d;
851 GST_DEBUG_OBJECT (base, "width is fixed (%d)", w);
853 /* If the PAR is fixed too, there's not much to do
854 * except choosing the height that is nearest to the
855 * height with the same DAR */
856 if (gst_value_is_fixed (to_par)) {
857 to_par_n = gst_value_get_fraction_numerator (to_par);
858 to_par_d = gst_value_get_fraction_denominator (to_par);
860 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
862 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
863 to_par_n, &num, &den)) {
864 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
865 ("Error calculating the output scaled size - integer overflow"));
869 h = (guint) gst_util_uint64_scale_int_round (w, den, num);
870 gst_structure_fixate_field_nearest_int (outs, "height", h);
875 /* The PAR is not fixed and it's quite likely that we can set
876 * an arbitrary PAR. */
878 /* Check if we can keep the input height */
879 tmp = gst_structure_copy (outs);
880 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
881 gst_structure_get_int (tmp, "height", &set_h);
883 /* Might have failed but try to keep the DAR nonetheless by
884 * adjusting the PAR */
885 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
886 &to_par_n, &to_par_d)) {
887 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
888 ("Error calculating the output scaled size - integer overflow"));
889 gst_structure_free (tmp);
892 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
893 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
894 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
896 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
898 gst_structure_free (tmp);
900 /* Check if the adjusted PAR is accepted */
901 if (set_par_n == to_par_n && set_par_d == to_par_d) {
902 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
903 set_par_n != set_par_d)
904 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
905 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
910 /* Otherwise scale the height to the new PAR and check if the
911 * adjusted with is accepted. If all that fails we can't keep
913 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
914 set_par_n, &num, &den)) {
915 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
916 ("Error calculating the output scale sized - integer overflow"));
920 h = (guint) gst_util_uint64_scale_int_round (w, den, num);
921 gst_structure_fixate_field_nearest_int (outs, "height", h);
922 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
923 set_par_n != set_par_d)
924 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
925 set_par_n, set_par_d, NULL);
928 } else if (gst_value_is_fixed (to_par)) {
930 gint set_h, set_w, f_h, f_w;
932 to_par_n = gst_value_get_fraction_numerator (to_par);
933 to_par_d = gst_value_get_fraction_denominator (to_par);
935 /* Calculate scale factor for the PAR change */
936 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
937 to_par_d, &num, &den)) {
938 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
939 ("Error calculating the output scaled size - integer overflow"));
943 /* Try to keep the input height (because of interlacing) */
944 tmp = gst_structure_copy (outs);
945 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
946 gst_structure_get_int (tmp, "height", &set_h);
948 /* This might have failed but try to scale the width
949 * to keep the DAR nonetheless */
950 w = (guint) gst_util_uint64_scale_int_round (set_h, num, den);
951 gst_structure_fixate_field_nearest_int (tmp, "width", w);
952 gst_structure_get_int (tmp, "width", &set_w);
953 gst_structure_free (tmp);
955 /* We kept the DAR and the height is nearest to the original height */
957 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
958 G_TYPE_INT, set_h, NULL);
965 /* If the former failed, try to keep the input width at least */
966 tmp = gst_structure_copy (outs);
967 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
968 gst_structure_get_int (tmp, "width", &set_w);
970 /* This might have failed but try to scale the width
971 * to keep the DAR nonetheless */
972 h = (guint) gst_util_uint64_scale_int_round (set_w, den, num);
973 gst_structure_fixate_field_nearest_int (tmp, "height", h);
974 gst_structure_get_int (tmp, "height", &set_h);
975 gst_structure_free (tmp);
977 /* We kept the DAR and the width is nearest to the original width */
979 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
980 G_TYPE_INT, set_h, NULL);
984 /* If all this failed, keep the dimensions with the DAR that was closest
985 * to the correct DAR. This changes the DAR but there's not much else to
988 if (set_w * ABS (set_h - h) < ABS (f_w - w) * f_h) {
992 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
997 gint set_h, set_w, set_par_n, set_par_d, tmp2;
999 /* width, height and PAR are not fixed but passthrough is not possible */
1001 /* First try to keep the height and width as good as possible
1003 tmp = gst_structure_copy (outs);
1004 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
1005 gst_structure_get_int (tmp, "height", &set_h);
1006 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
1007 gst_structure_get_int (tmp, "width", &set_w);
1009 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
1010 &to_par_n, &to_par_d)) {
1011 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
1012 ("Error calculating the output scaled size - integer overflow"));
1013 gst_structure_free (tmp);
1017 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
1018 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
1019 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
1020 to_par_n, to_par_d);
1021 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
1023 gst_structure_free (tmp);
1025 if (set_par_n == to_par_n && set_par_d == to_par_d) {
1026 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1027 G_TYPE_INT, set_h, NULL);
1029 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1030 set_par_n != set_par_d)
1031 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1032 set_par_n, set_par_d, NULL);
1036 /* Otherwise try to scale width to keep the DAR with the set
1038 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
1039 set_par_n, &num, &den)) {
1040 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
1041 ("Error calculating the output scaled size - integer overflow"));
1045 w = (guint) gst_util_uint64_scale_int_round (set_h, num, den);
1046 tmp = gst_structure_copy (outs);
1047 gst_structure_fixate_field_nearest_int (tmp, "width", w);
1048 gst_structure_get_int (tmp, "width", &tmp2);
1049 gst_structure_free (tmp);
1052 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
1053 G_TYPE_INT, set_h, NULL);
1054 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1055 set_par_n != set_par_d)
1056 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1057 set_par_n, set_par_d, NULL);
1061 /* ... or try the same with the height */
1062 h = (guint) gst_util_uint64_scale_int_round (set_w, den, num);
1063 tmp = gst_structure_copy (outs);
1064 gst_structure_fixate_field_nearest_int (tmp, "height", h);
1065 gst_structure_get_int (tmp, "height", &tmp2);
1066 gst_structure_free (tmp);
1069 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1070 G_TYPE_INT, tmp2, NULL);
1071 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1072 set_par_n != set_par_d)
1073 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1074 set_par_n, set_par_d, NULL);
1078 /* If all fails we can't keep the DAR and take the nearest values
1079 * for everything from the first try */
1080 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1081 G_TYPE_INT, set_h, NULL);
1082 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1083 set_par_n != set_par_d)
1084 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1085 set_par_n, set_par_d, NULL);
1090 if (from_par == &fpar)
1091 g_value_unset (&fpar);
1092 if (to_par == &tpar)
1093 g_value_unset (&tpar);
1099 gst_d3d11_base_convert_fixate_caps (GstBaseTransform * base,
1100 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
1102 GstCaps *format = NULL;
1104 GST_DEBUG_OBJECT (base,
1105 "trying to fixate othercaps %" GST_PTR_FORMAT " based on caps %"
1106 GST_PTR_FORMAT, othercaps, caps);
1108 format = gst_d3d11_base_convert_get_fixed_format (base, direction, caps,
1111 if (gst_caps_is_empty (format)) {
1112 GST_ERROR_OBJECT (base, "Could not convert formats");
1116 /* convert mode is "all" or "size" here */
1118 gst_d3d11_base_convert_fixate_size (base, direction, caps, othercaps);
1120 if (gst_caps_get_size (othercaps) == 1) {
1122 const gchar *format_fields[] = { "format", "colorimetry", "chroma-site" };
1123 GstStructure *format_struct = gst_caps_get_structure (format, 0);
1124 GstStructure *fixated_struct;
1126 othercaps = gst_caps_make_writable (othercaps);
1127 fixated_struct = gst_caps_get_structure (othercaps, 0);
1129 for (i = 0; i < G_N_ELEMENTS (format_fields); i++) {
1130 if (gst_structure_has_field (format_struct, format_fields[i])) {
1131 gst_structure_set (fixated_struct, format_fields[i], G_TYPE_STRING,
1132 gst_structure_get_string (format_struct, format_fields[i]), NULL);
1134 gst_structure_remove_field (fixated_struct, format_fields[i]);
1138 gst_caps_unref (format);
1140 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
1146 gst_d3d11_base_convert_filter_meta (GstBaseTransform * trans,
1147 GstQuery * query, GType api, const GstStructure * params)
1149 /* This element cannot passthrough the crop meta, because it would convert the
1150 * wrong sub-region of the image, and worst, our output image may not be large
1151 * enough for the crop to be applied later */
1152 if (api == GST_VIDEO_CROP_META_API_TYPE)
1155 /* propose all other metadata upstream */
1160 gst_d3d11_base_convert_propose_allocation (GstBaseTransform * trans,
1161 GstQuery * decide_query, GstQuery * query)
1163 GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans);
1165 GstBufferPool *pool = NULL;
1168 GstStructure *config;
1170 GstD3D11AllocationParams *d3d11_params;
1171 const GstD3D11Format *d3d11_format;
1172 guint bind_flags = D3D11_BIND_SHADER_RESOURCE;
1173 DXGI_FORMAT dxgi_format = DXGI_FORMAT_UNKNOWN;
1176 ID3D11Device *device_handle;
1178 if (!GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
1179 decide_query, query))
1182 /* passthrough, we're done */
1183 if (decide_query == NULL)
1186 gst_query_parse_allocation (query, &caps, NULL);
1191 if (!gst_video_info_from_caps (&info, caps)) {
1192 GST_ERROR_OBJECT (filter, "Invalid caps %" GST_PTR_FORMAT, caps);
1196 d3d11_format = gst_d3d11_device_format_from_gst (filter->device,
1197 GST_VIDEO_INFO_FORMAT (&info));
1198 if (!d3d11_format) {
1199 GST_ERROR_OBJECT (filter, "Unknown format caps %" GST_PTR_FORMAT, caps);
1203 if (d3d11_format->dxgi_format == DXGI_FORMAT_UNKNOWN) {
1204 dxgi_format = d3d11_format->resource_format[0];
1206 dxgi_format = d3d11_format->dxgi_format;
1209 device_handle = gst_d3d11_device_get_device_handle (filter->device);
1210 hr = device_handle->CheckFormatSupport (dxgi_format, &supported);
1211 if (gst_d3d11_result (hr, filter->device) &&
1212 (supported & D3D11_FORMAT_SUPPORT_RENDER_TARGET) ==
1213 D3D11_FORMAT_SUPPORT_RENDER_TARGET) {
1214 bind_flags |= D3D11_BIND_RENDER_TARGET;
1217 n_pools = gst_query_get_n_allocation_pools (query);
1218 for (i = 0; i < n_pools; i++) {
1219 gst_query_parse_nth_allocation_pool (query, i, &pool, NULL, NULL, NULL);
1221 if (!GST_IS_D3D11_BUFFER_POOL (pool)) {
1222 gst_clear_object (&pool);
1224 GstD3D11BufferPool *dpool = GST_D3D11_BUFFER_POOL (pool);
1225 if (dpool->device != filter->device)
1226 gst_clear_object (&pool);
1232 pool = gst_d3d11_buffer_pool_new (filter->device);
1234 config = gst_buffer_pool_get_config (pool);
1235 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
1237 d3d11_params = gst_buffer_pool_config_get_d3d11_allocation_params (config);
1238 if (!d3d11_params) {
1239 d3d11_params = gst_d3d11_allocation_params_new (filter->device, &info,
1240 (GstD3D11AllocationFlags) 0, bind_flags);
1243 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&info); i++) {
1244 d3d11_params->desc[i].BindFlags |= bind_flags;
1248 gst_buffer_pool_config_set_d3d11_allocation_params (config, d3d11_params);
1249 gst_d3d11_allocation_params_free (d3d11_params);
1251 /* size will be updated by d3d11 buffer pool */
1252 gst_buffer_pool_config_set_params (config, caps, 0, 0, 0);
1254 if (!gst_buffer_pool_set_config (pool, config))
1257 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1258 gst_query_add_allocation_meta (query,
1259 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL);
1261 /* d3d11 buffer pool will update buffer size based on allocated texture,
1262 * get size from config again */
1263 config = gst_buffer_pool_get_config (pool);
1264 gst_buffer_pool_config_get_params (config, nullptr, &size, nullptr, nullptr);
1265 gst_structure_free (config);
1267 gst_query_add_allocation_pool (query, pool, size, 0, 0);
1269 gst_object_unref (pool);
1276 GST_ERROR_OBJECT (filter, "failed to set config");
1277 gst_object_unref (pool);
1283 gst_d3d11_base_convert_decide_allocation (GstBaseTransform * trans,
1286 GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans);
1287 GstCaps *outcaps = NULL;
1288 GstBufferPool *pool = NULL;
1289 guint size, min = 0, max = 0;
1290 GstStructure *config;
1291 GstD3D11AllocationParams *d3d11_params;
1292 gboolean update_pool = FALSE;
1295 const GstD3D11Format *d3d11_format;
1296 guint bind_flags = D3D11_BIND_RENDER_TARGET;
1297 DXGI_FORMAT dxgi_format = DXGI_FORMAT_UNKNOWN;
1300 ID3D11Device *device_handle;
1302 gst_query_parse_allocation (query, &outcaps, NULL);
1307 if (!gst_video_info_from_caps (&info, outcaps)) {
1308 GST_ERROR_OBJECT (filter, "Invalid caps %" GST_PTR_FORMAT, outcaps);
1312 d3d11_format = gst_d3d11_device_format_from_gst (filter->device,
1313 GST_VIDEO_INFO_FORMAT (&info));
1314 if (!d3d11_format) {
1315 GST_ERROR_OBJECT (filter, "Unknown format caps %" GST_PTR_FORMAT, outcaps);
1319 if (d3d11_format->dxgi_format == DXGI_FORMAT_UNKNOWN) {
1320 dxgi_format = d3d11_format->resource_format[0];
1322 dxgi_format = d3d11_format->dxgi_format;
1325 device_handle = gst_d3d11_device_get_device_handle (filter->device);
1326 hr = device_handle->CheckFormatSupport (dxgi_format, &supported);
1327 if (gst_d3d11_result (hr, filter->device) &&
1328 (supported & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) ==
1329 D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) {
1330 bind_flags |= D3D11_BIND_SHADER_RESOURCE;
1333 size = GST_VIDEO_INFO_SIZE (&info);
1335 if (gst_query_get_n_allocation_pools (query) > 0) {
1336 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1338 if (!GST_IS_D3D11_BUFFER_POOL (pool)) {
1339 gst_clear_object (&pool);
1341 GstD3D11BufferPool *dpool = GST_D3D11_BUFFER_POOL (pool);
1342 if (dpool->device != filter->device)
1343 gst_clear_object (&pool);
1351 pool = gst_d3d11_buffer_pool_new (filter->device);
1353 config = gst_buffer_pool_get_config (pool);
1354 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
1356 d3d11_params = gst_buffer_pool_config_get_d3d11_allocation_params (config);
1357 if (!d3d11_params) {
1358 d3d11_params = gst_d3d11_allocation_params_new (filter->device, &info,
1359 (GstD3D11AllocationFlags) 0, bind_flags);
1362 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&info); i++) {
1363 d3d11_params->desc[i].BindFlags |= bind_flags;
1367 gst_buffer_pool_config_set_d3d11_allocation_params (config, d3d11_params);
1368 gst_d3d11_allocation_params_free (d3d11_params);
1370 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
1371 gst_buffer_pool_set_config (pool, config);
1373 /* d3d11 buffer pool will update buffer size based on allocated texture,
1374 * get size from config again */
1375 config = gst_buffer_pool_get_config (pool);
1376 gst_buffer_pool_config_get_params (config, nullptr, &size, nullptr, nullptr);
1377 gst_structure_free (config);
1380 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
1382 gst_query_add_allocation_pool (query, pool, size, min, max);
1384 gst_object_unref (pool);
1386 return GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
1391 create_shader_input_resource (GstD3D11BaseConvert * self,
1392 GstD3D11Device * device, const GstD3D11Format * format, GstVideoInfo * info)
1394 D3D11_TEXTURE2D_DESC texture_desc;
1395 D3D11_SHADER_RESOURCE_VIEW_DESC view_desc;
1397 ID3D11Device *device_handle;
1398 ID3D11Texture2D *tex[GST_VIDEO_MAX_PLANES] = { NULL, };
1399 ID3D11ShaderResourceView *view[GST_VIDEO_MAX_PLANES] = { NULL, };
1402 if (self->num_input_view)
1405 memset (&texture_desc, 0, sizeof (texture_desc));
1406 memset (&view_desc, 0, sizeof (view_desc));
1408 device_handle = gst_d3d11_device_get_device_handle (device);
1410 texture_desc.MipLevels = 1;
1411 texture_desc.ArraySize = 1;
1412 texture_desc.SampleDesc.Count = 1;
1413 texture_desc.SampleDesc.Quality = 0;
1414 texture_desc.Usage = D3D11_USAGE_DEFAULT;
1415 texture_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
1417 if (format->dxgi_format == DXGI_FORMAT_UNKNOWN) {
1418 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1419 if (format->resource_format[i] == DXGI_FORMAT_UNKNOWN)
1422 texture_desc.Width = GST_VIDEO_INFO_COMP_WIDTH (info, i);
1423 texture_desc.Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
1424 texture_desc.Format = format->resource_format[i];
1426 hr = device_handle->CreateTexture2D (&texture_desc, NULL, &tex[i]);
1427 if (!gst_d3d11_result (hr, device)) {
1428 GST_ERROR_OBJECT (self, "Failed to create texture (0x%x)", (guint) hr);
1433 gboolean is_semiplanar = FALSE;
1435 if (format->dxgi_format == DXGI_FORMAT_NV12 ||
1436 format->dxgi_format == DXGI_FORMAT_P010 ||
1437 format->dxgi_format == DXGI_FORMAT_P016)
1438 is_semiplanar = TRUE;
1440 texture_desc.Width = GST_VIDEO_INFO_WIDTH (info);
1441 texture_desc.Height = GST_VIDEO_INFO_HEIGHT (info);
1442 texture_desc.Format = format->dxgi_format;
1444 /* semiplanar format resolution of should be even number */
1445 if (is_semiplanar) {
1446 texture_desc.Width = GST_ROUND_UP_2 (texture_desc.Width);
1447 texture_desc.Height = GST_ROUND_UP_2 (texture_desc.Height);
1450 hr = device_handle->CreateTexture2D (&texture_desc, NULL, &tex[0]);
1451 if (!gst_d3d11_result (hr, device)) {
1452 GST_ERROR_OBJECT (self, "Failed to create texture (0x%x)", (guint) hr);
1456 if (is_semiplanar) {
1462 view_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
1463 view_desc.Texture2D.MipLevels = 1;
1465 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1466 if (format->resource_format[i] == DXGI_FORMAT_UNKNOWN)
1469 view_desc.Format = format->resource_format[i];
1470 hr = device_handle->CreateShaderResourceView (tex[i], &view_desc, &view[i]);
1472 if (!gst_d3d11_result (hr, device)) {
1473 GST_ERROR_OBJECT (self,
1474 "Failed to create resource view (0x%x)", (guint) hr);
1479 self->num_input_view = i;
1481 GST_DEBUG_OBJECT (self,
1482 "%d shader resource view created", self->num_input_view);
1484 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1485 self->in_texture[i] = tex[i];
1486 self->shader_resource_view[i] = view[i];
1492 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1493 GST_D3D11_CLEAR_COM (view[i]);
1496 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1497 GST_D3D11_CLEAR_COM (tex[i]);
1503 /* 16.0 / 255.0 ~= 0.062745 */
1504 static const float luma_black_level_limited = 0.062745f;
1507 clear_rtv_color_rgb (GstD3D11BaseConvert * self,
1508 ID3D11DeviceContext * context_handle, ID3D11RenderTargetView * rtv,
1509 gboolean full_range)
1511 const FLOAT clear_color_full[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
1512 const FLOAT clear_color_limited[4] =
1513 { luma_black_level_limited, luma_black_level_limited,
1514 luma_black_level_limited, 1.0f
1516 const FLOAT *target;
1519 target = clear_color_full;
1521 target = clear_color_limited;
1523 context_handle->ClearRenderTargetView (rtv, target);
1527 clear_rtv_color_vuya (GstD3D11BaseConvert * self,
1528 ID3D11DeviceContext * context_handle, ID3D11RenderTargetView * rtv,
1529 gboolean full_range)
1531 const FLOAT clear_color_full[4] = { 0.5f, 0.5f, 0.0f, 1.0f };
1532 const FLOAT clear_color_limited[4] =
1533 { 0.5f, 0.5f, luma_black_level_limited, 1.0f };
1534 const FLOAT *target;
1537 target = clear_color_full;
1539 target = clear_color_limited;
1541 context_handle->ClearRenderTargetView (rtv, target);
1545 clear_rtv_color_luma (GstD3D11BaseConvert * self,
1546 ID3D11DeviceContext * context_handle, ID3D11RenderTargetView * rtv,
1547 gboolean full_range)
1549 const FLOAT clear_color_full[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
1550 const FLOAT clear_color_limited[4] =
1551 { luma_black_level_limited, luma_black_level_limited,
1552 luma_black_level_limited, 1.0f
1554 const FLOAT *target;
1557 target = clear_color_full;
1559 target = clear_color_limited;
1561 context_handle->ClearRenderTargetView (rtv, target);
1565 clear_rtv_color_chroma (GstD3D11BaseConvert * self,
1566 ID3D11DeviceContext * context_handle, ID3D11RenderTargetView * rtv)
1568 const FLOAT clear_color[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
1570 context_handle->ClearRenderTargetView (rtv, clear_color);
1574 clear_rtv_color_all (GstD3D11BaseConvert * self, GstVideoInfo * info,
1575 ID3D11DeviceContext * context_handle,
1576 ID3D11RenderTargetView * rtv[GST_VIDEO_MAX_PLANES])
1579 gboolean full_range = info->colorimetry.range == GST_VIDEO_COLOR_RANGE_0_255;
1581 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1585 if (GST_VIDEO_INFO_IS_RGB (info)) {
1586 clear_rtv_color_rgb (self, context_handle, rtv[i], full_range);
1588 if (GST_VIDEO_INFO_N_PLANES (info) == 1) {
1589 clear_rtv_color_vuya (self, context_handle, rtv[i], full_range);
1592 clear_rtv_color_luma (self, context_handle, rtv[i], full_range);
1594 clear_rtv_color_chroma (self, context_handle, rtv[i]);
1601 create_shader_output_resource (GstD3D11BaseConvert * self,
1602 GstD3D11Device * device, const GstD3D11Format * format, GstVideoInfo * info)
1604 D3D11_TEXTURE2D_DESC texture_desc;
1605 D3D11_RENDER_TARGET_VIEW_DESC view_desc;
1607 ID3D11Device *device_handle;
1608 ID3D11DeviceContext *context_handle;
1609 ID3D11Texture2D *tex[GST_VIDEO_MAX_PLANES] = { NULL, };
1610 ID3D11RenderTargetView *view[GST_VIDEO_MAX_PLANES] = { NULL, };
1613 if (self->num_output_view)
1616 memset (&texture_desc, 0, sizeof (texture_desc));
1617 memset (&view_desc, 0, sizeof (view_desc));
1619 device_handle = gst_d3d11_device_get_device_handle (device);
1620 context_handle = gst_d3d11_device_get_device_context_handle (device);
1622 texture_desc.MipLevels = 1;
1623 texture_desc.ArraySize = 1;
1624 texture_desc.SampleDesc.Count = 1;
1625 texture_desc.SampleDesc.Quality = 0;
1626 texture_desc.Usage = D3D11_USAGE_DEFAULT;
1627 texture_desc.BindFlags =
1628 D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
1630 if (format->dxgi_format == DXGI_FORMAT_UNKNOWN) {
1631 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1632 if (format->resource_format[i] == DXGI_FORMAT_UNKNOWN)
1635 texture_desc.Width = GST_VIDEO_INFO_COMP_WIDTH (info, i);
1636 texture_desc.Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
1637 texture_desc.Format = format->resource_format[i];
1639 hr = device_handle->CreateTexture2D (&texture_desc, NULL, &tex[i]);
1640 if (!gst_d3d11_result (hr, device)) {
1641 GST_ERROR_OBJECT (self, "Failed to create texture (0x%x)", (guint) hr);
1646 gboolean is_semiplanar = FALSE;
1648 if (format->dxgi_format == DXGI_FORMAT_NV12 ||
1649 format->dxgi_format == DXGI_FORMAT_P010 ||
1650 format->dxgi_format == DXGI_FORMAT_P016)
1651 is_semiplanar = TRUE;
1653 texture_desc.Width = GST_VIDEO_INFO_WIDTH (info);
1654 texture_desc.Height = GST_VIDEO_INFO_HEIGHT (info);
1655 texture_desc.Format = format->dxgi_format;
1657 /* semiplanar format resolution of should be even number */
1658 if (is_semiplanar) {
1659 texture_desc.Width = GST_ROUND_UP_2 (texture_desc.Width);
1660 texture_desc.Height = GST_ROUND_UP_2 (texture_desc.Height);
1663 hr = device_handle->CreateTexture2D (&texture_desc, NULL, &tex[0]);
1664 if (!gst_d3d11_result (hr, device)) {
1665 GST_ERROR_OBJECT (self, "Failed to create texture (0x%x)", (guint) hr);
1669 if (is_semiplanar) {
1675 view_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
1676 view_desc.Texture2D.MipSlice = 0;
1677 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1678 if (format->resource_format[i] == DXGI_FORMAT_UNKNOWN)
1681 view_desc.Format = format->resource_format[i];
1682 hr = device_handle->CreateRenderTargetView (tex[i], &view_desc, &view[i]);
1683 if (!gst_d3d11_result (hr, device)) {
1684 GST_ERROR_OBJECT (self,
1685 "Failed to create %dth render target view (0x%x)", i, (guint) hr);
1690 gst_d3d11_device_lock (device);
1691 clear_rtv_color_all (self, info, context_handle, view);
1692 gst_d3d11_device_unlock (device);
1694 self->num_output_view = i;
1696 GST_DEBUG_OBJECT (self, "%d render view created", self->num_output_view);
1698 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1699 self->out_texture[i] = tex[i];
1700 self->render_target_view[i] = view[i];
1706 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1707 GST_D3D11_CLEAR_COM (view[i]);
1710 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1711 GST_D3D11_CLEAR_COM (tex[i]);
1718 gst_d3d11_base_convert_set_info (GstD3D11BaseFilter * filter,
1719 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
1720 GstVideoInfo * out_info)
1722 GstD3D11BaseConvert *self = GST_D3D11_BASE_CONVERT (filter);
1723 const GstVideoInfo *unknown_info;
1724 gint from_dar_n, from_dar_d, to_dar_n, to_dar_d;
1725 D3D11_VIEWPORT view_port;
1726 gint border_offset_x = 0;
1727 gint border_offset_y = 0;
1729 if (gst_base_transform_is_passthrough (GST_BASE_TRANSFORM (filter)))
1732 if (!gst_util_fraction_multiply (in_info->width,
1733 in_info->height, in_info->par_n, in_info->par_d, &from_dar_n,
1735 from_dar_n = from_dar_d = -1;
1738 if (!gst_util_fraction_multiply (out_info->width,
1739 out_info->height, out_info->par_n, out_info->par_d, &to_dar_n,
1741 to_dar_n = to_dar_d = -1;
1744 self->borders_w = self->borders_h = 0;
1745 if (to_dar_n != from_dar_n || to_dar_d != from_dar_d) {
1746 if (self->add_borders) {
1747 gint n, d, to_h, to_w;
1749 if (from_dar_n != -1 && from_dar_d != -1
1750 && gst_util_fraction_multiply (from_dar_n, from_dar_d,
1751 out_info->par_d, out_info->par_n, &n, &d)) {
1752 to_h = gst_util_uint64_scale_int (out_info->width, d, n);
1753 if (to_h <= out_info->height) {
1754 self->borders_h = out_info->height - to_h;
1755 self->borders_w = 0;
1757 to_w = gst_util_uint64_scale_int (out_info->height, n, d);
1758 g_assert (to_w <= out_info->width);
1759 self->borders_h = 0;
1760 self->borders_w = out_info->width - to_w;
1763 GST_WARNING_OBJECT (self, "Can't calculate borders");
1766 GST_INFO_OBJECT (self, "Display aspect ratio update %d/%d -> %d/%d",
1767 from_dar_n, from_dar_d, to_dar_n, to_dar_d);
1771 gst_d3d11_base_convert_clear_shader_resource (self);
1773 GST_DEBUG_OBJECT (self, "Setup convert with format %s -> %s",
1774 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (in_info)),
1775 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (out_info)));
1777 /* if present, these must match */
1778 if (in_info->interlace_mode != out_info->interlace_mode)
1779 goto format_mismatch;
1781 if (in_info->width == out_info->width && in_info->height == out_info->height
1782 && in_info->finfo == out_info->finfo && self->borders_w == 0 &&
1783 self->borders_h == 0) {
1784 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), TRUE);
1787 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), FALSE);
1790 self->in_d3d11_format =
1791 gst_d3d11_device_format_from_gst (filter->device,
1792 GST_VIDEO_INFO_FORMAT (in_info));
1793 if (!self->in_d3d11_format) {
1794 unknown_info = in_info;
1795 goto format_unknown;
1798 self->out_d3d11_format =
1799 gst_d3d11_device_format_from_gst (filter->device,
1800 GST_VIDEO_INFO_FORMAT (out_info));
1801 if (!self->out_d3d11_format) {
1802 unknown_info = out_info;
1803 goto format_unknown;
1807 gst_d3d11_converter_new (filter->device, in_info, out_info, nullptr);
1809 if (!self->converter) {
1810 GST_ERROR_OBJECT (self, "couldn't set converter");
1814 /* If both input and output formats are native DXGI format */
1815 if (self->in_d3d11_format->dxgi_format != DXGI_FORMAT_UNKNOWN &&
1816 self->out_d3d11_format->dxgi_format != DXGI_FORMAT_UNKNOWN) {
1817 gboolean hardware = FALSE;
1818 GstD3D11VideoProcessor *processor = NULL;
1820 gst_d3d11_device_lock (filter->device);
1821 g_object_get (filter->device, "hardware", &hardware, NULL);
1823 processor = gst_d3d11_video_processor_new (filter->device,
1824 in_info->width, in_info->height, out_info->width, out_info->height);
1828 const GstDxgiColorSpace *in_color_space;
1829 const GstDxgiColorSpace *out_color_space;
1831 in_color_space = gst_d3d11_video_info_to_dxgi_color_space (in_info);
1832 out_color_space = gst_d3d11_video_info_to_dxgi_color_space (out_info);
1834 if (in_color_space && out_color_space) {
1835 DXGI_FORMAT in_dxgi_format = self->in_d3d11_format->dxgi_format;
1836 DXGI_FORMAT out_dxgi_format = self->out_d3d11_format->dxgi_format;
1837 DXGI_COLOR_SPACE_TYPE in_dxgi_color_space =
1838 (DXGI_COLOR_SPACE_TYPE) in_color_space->dxgi_color_space_type;
1839 DXGI_COLOR_SPACE_TYPE out_dxgi_color_space =
1840 (DXGI_COLOR_SPACE_TYPE) out_color_space->dxgi_color_space_type;
1842 if (!gst_d3d11_video_processor_check_format_conversion (processor,
1843 in_dxgi_format, in_dxgi_color_space, out_dxgi_format,
1844 out_dxgi_color_space)) {
1845 GST_DEBUG_OBJECT (self, "Conversion is not supported by device");
1846 gst_d3d11_video_processor_free (processor);
1849 GST_DEBUG_OBJECT (self, "video processor supports conversion");
1850 gst_d3d11_video_processor_set_input_dxgi_color_space (processor,
1851 in_dxgi_color_space);
1852 gst_d3d11_video_processor_set_output_dxgi_color_space (processor,
1853 out_dxgi_color_space);
1856 GST_WARNING_OBJECT (self,
1857 "Couldn't determine input and/or output dxgi colorspace");
1858 gst_d3d11_video_processor_free (processor);
1863 self->processor = processor;
1864 gst_d3d11_device_unlock (filter->device);
1867 GST_DEBUG_OBJECT (self, "from=%dx%d (par=%d/%d dar=%d/%d), size %"
1868 G_GSIZE_FORMAT " -> to=%dx%d (par=%d/%d dar=%d/%d borders=%d:%d), "
1869 "size %" G_GSIZE_FORMAT,
1870 in_info->width, in_info->height, in_info->par_n, in_info->par_d,
1871 from_dar_n, from_dar_d, in_info->size, out_info->width,
1872 out_info->height, out_info->par_n, out_info->par_d, to_dar_n, to_dar_d,
1873 self->borders_w, self->borders_h, out_info->size);
1875 self->in_rect.left = 0;
1876 self->in_rect.top = 0;
1877 self->in_rect.right = GST_VIDEO_INFO_WIDTH (in_info);
1878 self->in_rect.bottom = GST_VIDEO_INFO_HEIGHT (in_info);
1880 if (self->borders_w) {
1881 border_offset_x = self->borders_w / 2;
1882 self->out_rect.left = border_offset_x;
1883 self->out_rect.right = GST_VIDEO_INFO_WIDTH (out_info) - border_offset_x;
1885 self->out_rect.left = 0;
1886 self->out_rect.right = GST_VIDEO_INFO_WIDTH (out_info);
1889 if (self->borders_h) {
1890 border_offset_y = self->borders_h / 2;
1891 self->out_rect.top = border_offset_y;
1892 self->out_rect.bottom = GST_VIDEO_INFO_HEIGHT (out_info) - border_offset_y;
1894 self->out_rect.top = 0;
1895 self->out_rect.bottom = GST_VIDEO_INFO_HEIGHT (out_info);
1898 view_port.TopLeftX = border_offset_x;
1899 view_port.TopLeftY = border_offset_y;
1900 view_port.Width = GST_VIDEO_INFO_WIDTH (out_info) - self->borders_w;
1901 view_port.Height = GST_VIDEO_INFO_HEIGHT (out_info) - self->borders_h;
1902 view_port.MinDepth = 0.0f;
1903 view_port.MaxDepth = 1.0f;
1905 gst_d3d11_converter_update_viewport (self->converter, &view_port);
1912 GST_ERROR_OBJECT (self, "input and output formats do not match");
1917 GST_ERROR_OBJECT (self,
1918 "%s couldn't be converted to d3d11 format",
1919 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (unknown_info)));
1925 gst_d3d11_base_convert_prefer_video_processor (GstD3D11BaseConvert * self,
1926 GstBuffer * inbuf, GstBuffer * outbuf)
1928 GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (self);
1930 GstD3D11Memory *dmem;
1932 if (!self->processor) {
1933 GST_TRACE_OBJECT (self, "Processor is unavailable");
1937 if (gst_buffer_n_memory (inbuf) != 1 || gst_buffer_n_memory (outbuf) != 1) {
1938 GST_TRACE_OBJECT (self, "Num memory objects is mismatched, in: %d, out: %d",
1939 gst_buffer_n_memory (inbuf), gst_buffer_n_memory (outbuf));
1943 mem = gst_buffer_peek_memory (inbuf, 0);
1944 g_assert (gst_is_d3d11_memory (mem));
1946 dmem = (GstD3D11Memory *) mem;
1947 if (dmem->device != filter->device) {
1948 GST_TRACE_OBJECT (self, "Input memory belongs to different device");
1952 /* If we can use shader, and video processor was not used previously,
1953 * we prefer to use shader instead of video processor
1954 * because video processor implementation is vendor dependent
1955 * and not flexible */
1956 if (!self->processor_in_use &&
1957 gst_d3d11_memory_get_shader_resource_view_size (dmem)) {
1958 GST_TRACE_OBJECT (self, "SRV is available");
1962 if (!gst_d3d11_video_processor_get_input_view (self->processor, dmem)) {
1963 GST_TRACE_OBJECT (self, "PIV is unavailable");
1967 mem = gst_buffer_peek_memory (outbuf, 0);
1968 g_assert (gst_is_d3d11_memory (mem));
1970 dmem = (GstD3D11Memory *) mem;
1971 if (dmem->device != filter->device) {
1972 GST_TRACE_OBJECT (self, "Output memory belongs to different device");
1976 if (!gst_d3d11_video_processor_get_output_view (self->processor, dmem)) {
1977 GST_TRACE_OBJECT (self, "POV is unavailable");
1985 gst_d3d11_base_convert_transform_using_processor (GstD3D11BaseConvert * self,
1986 GstBuffer * inbuf, GstBuffer * outbuf)
1988 GstD3D11Memory *in_mem, *out_mem;
1989 ID3D11VideoProcessorInputView *piv;
1990 ID3D11VideoProcessorOutputView *pov;
1992 in_mem = (GstD3D11Memory *) gst_buffer_peek_memory (inbuf, 0);
1993 out_mem = (GstD3D11Memory *) gst_buffer_peek_memory (outbuf, 0);
1995 piv = gst_d3d11_video_processor_get_input_view (self->processor, in_mem);
1997 GST_ERROR_OBJECT (self, "ID3D11VideoProcessorInputView is unavailable");
2001 pov = gst_d3d11_video_processor_get_output_view (self->processor, out_mem);
2003 GST_ERROR_OBJECT (self, "ID3D11VideoProcessorOutputView is unavailable");
2007 /* Clear background color with black */
2008 if (self->borders_w || self->borders_h) {
2009 GstD3D11BaseFilter *bfilter = GST_D3D11_BASE_FILTER_CAST (self);
2010 ID3D11DeviceContext *context_handle =
2011 gst_d3d11_device_get_device_context_handle (bfilter->device);
2012 ID3D11RenderTargetView *render_view[GST_VIDEO_MAX_PLANES] = { NULL, };
2014 if (!gst_d3d11_buffer_get_render_target_view (outbuf, render_view)) {
2015 GST_ERROR_OBJECT (self, "ID3D11RenderTargetView is unavailable");
2019 gst_d3d11_device_lock (bfilter->device);
2020 clear_rtv_color_all (self, &bfilter->out_info, context_handle, render_view);
2021 gst_d3d11_device_unlock (bfilter->device);
2024 return gst_d3d11_video_processor_render (self->processor,
2025 &self->in_rect, piv, &self->out_rect, pov);
2028 static GstFlowReturn
2029 gst_d3d11_base_convert_transform (GstBaseTransform * trans,
2030 GstBuffer * inbuf, GstBuffer * outbuf)
2032 GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans);
2033 GstD3D11BaseConvert *self = GST_D3D11_BASE_CONVERT (trans);
2034 GstD3D11Device *device = filter->device;
2035 ID3D11Device *device_handle;
2036 ID3D11DeviceContext *context_handle;
2037 ID3D11ShaderResourceView *resource_view[GST_VIDEO_MAX_PLANES] = { NULL, };
2038 ID3D11RenderTargetView *render_view[GST_VIDEO_MAX_PLANES] = { NULL, };
2039 ID3D11RenderTargetView **target_rtv;
2041 gboolean copy_input = FALSE;
2042 gboolean copy_output = FALSE;
2043 GstMapInfo in_map[GST_VIDEO_MAX_PLANES];
2044 GstMapInfo out_map[GST_VIDEO_MAX_PLANES];
2046 device_handle = gst_d3d11_device_get_device_handle (device);
2047 context_handle = gst_d3d11_device_get_device_context_handle (device);
2049 if (!gst_d3d11_buffer_map (inbuf, device_handle, in_map, GST_MAP_READ)) {
2050 GST_ERROR_OBJECT (self, "Couldn't map input buffer");
2051 goto invalid_memory;
2054 if (!gst_d3d11_buffer_map (outbuf, device_handle, out_map, GST_MAP_WRITE)) {
2055 GST_ERROR_OBJECT (self, "Couldn't map output buffer");
2056 gst_d3d11_buffer_unmap (inbuf, in_map);
2057 goto invalid_memory;
2060 if (gst_d3d11_base_convert_prefer_video_processor (self, inbuf, outbuf)) {
2062 gst_d3d11_base_convert_transform_using_processor (self, inbuf, outbuf);
2065 GST_ERROR_OBJECT (self, "Couldn't convert using video processor");
2066 goto conversion_failed;
2069 self->processor_in_use = TRUE;
2071 GST_TRACE_OBJECT (self, "Conversion done by video processor");
2073 gst_d3d11_buffer_unmap (inbuf, in_map);
2074 gst_d3d11_buffer_unmap (outbuf, out_map);
2079 /* Ensure shader resource views */
2080 if (!gst_d3d11_buffer_get_shader_resource_view (inbuf, resource_view)) {
2081 if (!create_shader_input_resource (self, device,
2082 self->in_d3d11_format, &filter->in_info)) {
2083 GST_ERROR_OBJECT (self, "Failed to configure fallback input texture");
2084 goto fallback_failed;
2088 gst_d3d11_device_lock (device);
2089 for (i = 0; i < gst_buffer_n_memory (inbuf); i++) {
2090 GstD3D11Memory *mem =
2091 (GstD3D11Memory *) gst_buffer_peek_memory (inbuf, i);
2093 D3D11_BOX src_box = { 0, };
2094 D3D11_TEXTURE2D_DESC src_desc;
2095 D3D11_TEXTURE2D_DESC dst_desc;
2097 subidx = gst_d3d11_memory_get_subresource_index (mem);
2098 gst_d3d11_memory_get_texture_desc (mem, &src_desc);
2100 self->in_texture[i]->GetDesc (&dst_desc);
2106 src_box.right = MIN (src_desc.Width, dst_desc.Width);
2107 src_box.bottom = MIN (src_desc.Height, dst_desc.Height);
2109 context_handle->CopySubresourceRegion (self->in_texture[i], 0, 0, 0, 0,
2110 (ID3D11Resource *) in_map[i].data, subidx, &src_box);
2112 gst_d3d11_device_unlock (device);
2115 /* Ensure render target views */
2116 if (!gst_d3d11_buffer_get_render_target_view (outbuf, render_view)) {
2117 if (!create_shader_output_resource (self, device,
2118 self->out_d3d11_format, &filter->out_info)) {
2119 GST_ERROR_OBJECT (self, "Failed to configure fallback output texture");
2120 goto fallback_failed;
2126 /* If we need border, clear render target view first */
2128 target_rtv = self->render_target_view;
2130 target_rtv = render_view;
2133 /* We need to clear background color as our shader wouldn't touch border
2134 * area. Likely output texture was initialized with zeros which is fine for
2135 * RGB, but it's not black color in case of YUV */
2136 if (self->borders_w || self->borders_h) {
2137 gst_d3d11_device_lock (device);
2138 clear_rtv_color_all (self, &filter->out_info, context_handle, target_rtv);
2139 gst_d3d11_device_unlock (device);
2142 if (!gst_d3d11_converter_convert (self->converter,
2143 copy_input ? self->shader_resource_view : resource_view,
2144 target_rtv, NULL, NULL)) {
2145 goto conversion_failed;
2149 gst_d3d11_device_lock (device);
2150 for (i = 0; i < gst_buffer_n_memory (outbuf); i++) {
2151 GstD3D11Memory *mem =
2152 (GstD3D11Memory *) gst_buffer_peek_memory (outbuf, i);
2154 D3D11_BOX src_box = { 0, };
2155 D3D11_TEXTURE2D_DESC src_desc;
2156 D3D11_TEXTURE2D_DESC dst_desc;
2158 self->out_texture[i]->GetDesc (&src_desc);
2159 subidx = gst_d3d11_memory_get_subresource_index (mem);
2160 gst_d3d11_memory_get_texture_desc (mem, &dst_desc);
2166 src_box.right = MIN (src_desc.Width, dst_desc.Width);
2167 src_box.bottom = MIN (src_desc.Height, dst_desc.Height);
2169 context_handle->CopySubresourceRegion ((ID3D11Resource *) out_map[i].data,
2170 subidx, 0, 0, 0, self->out_texture[i], 0, &src_box);
2172 gst_d3d11_device_unlock (device);
2175 gst_d3d11_buffer_unmap (inbuf, in_map);
2176 gst_d3d11_buffer_unmap (outbuf, out_map);
2182 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL), ("Invalid memory"));
2183 return GST_FLOW_ERROR;
2187 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
2188 ("Couldn't prepare fallback memory"));
2189 gst_d3d11_buffer_unmap (inbuf, in_map);
2190 gst_d3d11_buffer_unmap (outbuf, out_map);
2192 return GST_FLOW_ERROR;
2196 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
2197 ("Couldn't convert texture"));
2198 gst_d3d11_buffer_unmap (inbuf, in_map);
2199 gst_d3d11_buffer_unmap (outbuf, out_map);
2201 return GST_FLOW_ERROR;
2206 gst_d3d11_base_convert_set_add_border (GstD3D11BaseConvert * self,
2207 gboolean add_border)
2209 gboolean prev = self->add_borders;
2211 self->add_borders = add_border;
2212 if (prev != self->add_borders)
2213 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM_CAST (self));
2217 * SECTION:element-d3d11convert
2218 * @title: d3d11convert
2219 * @short_description: A Direct3D11 based color conversion and video resizing element
2221 * This element resizes video frames and change color space.
2222 * By default the element will try to negotiate to the same size on the source
2223 * and sinkpad so that no scaling is needed.
2224 * It is therefore safe to insert this element in a pipeline to
2225 * get more robust behaviour without any cost if no scaling is needed.
2227 * ## Example launch line
2229 * gst-launch-1.0 videotestsrc ! video/x-raw,format=NV12 ! d3d11upload ! d3d11convert ! d3d11videosink
2231 * This will output a test video (generated in NV12 format) in a video
2232 * window. If the video sink selected does not support NV12
2233 * d3d11convert will automatically convert the video to a format understood
2234 * by the video sink.
2243 PROP_CONVERT_ADD_BORDERS,
2246 struct _GstD3D11Convert
2248 GstD3D11BaseConvert parent;
2251 G_DEFINE_TYPE (GstD3D11Convert, gst_d3d11_convert, GST_TYPE_D3D11_BASE_CONVERT);
2253 static void gst_d3d11_convert_set_property (GObject * object, guint prop_id,
2254 const GValue * value, GParamSpec * pspec);
2255 static void gst_d3d11_convert_get_property (GObject * object, guint prop_id,
2256 GValue * value, GParamSpec * pspec);
2259 gst_d3d11_convert_class_init (GstD3D11ConvertClass * klass)
2261 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
2262 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
2264 gobject_class->set_property = gst_d3d11_convert_set_property;
2265 gobject_class->get_property = gst_d3d11_convert_get_property;
2268 * GstD3D11Convert:add-borders:
2270 * Add black borders if necessary to keep the display aspect ratio
2274 g_object_class_install_property (gobject_class, PROP_CONVERT_ADD_BORDERS,
2275 g_param_spec_boolean ("add-borders", "Add Borders",
2276 "Add black borders if necessary to keep the display aspect ratio",
2277 DEFAULT_ADD_BORDERS, (GParamFlags) (GST_PARAM_MUTABLE_PLAYING |
2278 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
2280 gst_element_class_set_static_metadata (element_class,
2281 "Direct3D11 colorspace converter and scaler",
2282 "Filter/Converter/Scaler/Video/Hardware",
2283 "Resizes video and allow color conversion using Direct3D11",
2284 "Seungha Yang <seungha.yang@navercorp.com>, "
2285 "Jeongki Kim <jeongki.kim@jeongki.kim>");
2289 gst_d3d11_convert_init (GstD3D11Convert * self)
2294 gst_d3d11_convert_set_property (GObject * object, guint prop_id,
2295 const GValue * value, GParamSpec * pspec)
2297 GstD3D11BaseConvert *base = GST_D3D11_BASE_CONVERT (object);
2300 case PROP_CONVERT_ADD_BORDERS:
2301 gst_d3d11_base_convert_set_add_border (base, g_value_get_boolean (value));
2304 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2310 gst_d3d11_convert_get_property (GObject * object, guint prop_id,
2311 GValue * value, GParamSpec * pspec)
2313 GstD3D11BaseConvert *base = GST_D3D11_BASE_CONVERT (object);
2316 case PROP_CONVERT_ADD_BORDERS:
2317 g_value_set_boolean (value, base->add_borders);
2320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2326 * SECTION:element-d3d11colorconvert
2327 * @title: d3d11colorconvert
2329 * A Direct3D11 based color conversion element
2331 * ## Example launch line
2333 * gst-launch-1.0 videotestsrc ! video/x-raw,format=NV12 ! d3d11upload ! d3d11colorconvert ! d3d11download ! video/x-raw,format=RGBA ! fakesink
2335 * This will upload a test video (generated in NV12 format) to Direct3D11
2336 * memory space and convert it to RGBA format. Then a converted Direct3D11
2337 * frame will be downloaded to system memory space.
2342 struct _GstD3D11ColorConvert
2344 GstD3D11BaseConvert parent;
2347 static GstCaps *gst_d3d11_color_convert_transform_caps (GstBaseTransform *
2348 trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
2349 static GstCaps *gst_d3d11_color_convert_fixate_caps (GstBaseTransform * base,
2350 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
2352 G_DEFINE_TYPE (GstD3D11ColorConvert, gst_d3d11_color_convert,
2353 GST_TYPE_D3D11_BASE_CONVERT);
2356 gst_d3d11_color_convert_class_init (GstD3D11ColorConvertClass * klass)
2358 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
2359 GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
2361 gst_element_class_set_static_metadata (element_class,
2362 "Direct3D11 colorspace converter",
2363 "Filter/Converter/Video/Hardware",
2364 "Color conversion using Direct3D11",
2365 "Seungha Yang <seungha@centricular.com>");
2367 trans_class->transform_caps =
2368 GST_DEBUG_FUNCPTR (gst_d3d11_color_convert_transform_caps);
2369 trans_class->fixate_caps =
2370 GST_DEBUG_FUNCPTR (gst_d3d11_color_convert_fixate_caps);
2374 gst_d3d11_color_convert_init (GstD3D11ColorConvert * self)
2379 gst_d3d11_color_convert_transform_caps (GstBaseTransform *
2380 trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter)
2382 GstCaps *tmp, *tmp2;
2385 /* Get all possible caps that we can transform to */
2386 tmp = gst_d3d11_base_convert_caps_remove_format_info (caps);
2389 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
2390 gst_caps_unref (tmp);
2396 GST_DEBUG_OBJECT (trans, "transformed %" GST_PTR_FORMAT " into %"
2397 GST_PTR_FORMAT, caps, result);
2403 gst_d3d11_color_convert_fixate_caps (GstBaseTransform * base,
2404 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
2406 GstCaps *format = NULL;
2408 GST_DEBUG_OBJECT (base,
2409 "trying to fixate othercaps %" GST_PTR_FORMAT " based on caps %"
2410 GST_PTR_FORMAT, othercaps, caps);
2412 format = gst_d3d11_base_convert_get_fixed_format (base, direction, caps,
2414 gst_caps_unref (othercaps);
2416 if (gst_caps_is_empty (format)) {
2417 GST_ERROR_OBJECT (base, "Could not convert formats");
2419 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, format);
2426 * SECTION:element-d3d11scale
2427 * @title: d3d11scale
2429 * A Direct3D11 based video resizing element
2431 * ## Example launch line
2433 * gst-launch-1.0 videotestsrc ! video/x-raw,width=640,height=480 ! d3d11upload ! d3d11scale ! d3d11download ! video/x-raw,width=1280,height=720 ! fakesink
2435 * This will upload a 640x480 resolution test video to Direct3D11
2436 * memory space and resize it to 1280x720 resolution. Then a resized Direct3D11
2437 * frame will be downloaded to system memory space.
2446 PROP_SCALE_ADD_BORDERS,
2449 struct _GstD3D11Scale
2451 GstD3D11BaseConvert parent;
2454 static void gst_d3d11_scale_set_property (GObject * object, guint prop_id,
2455 const GValue * value, GParamSpec * pspec);
2456 static void gst_d3d11_scale_get_property (GObject * object, guint prop_id,
2457 GValue * value, GParamSpec * pspec);
2458 static GstCaps *gst_d3d11_scale_transform_caps (GstBaseTransform *
2459 trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
2460 static GstCaps *gst_d3d11_scale_fixate_caps (GstBaseTransform * base,
2461 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
2463 G_DEFINE_TYPE (GstD3D11Scale, gst_d3d11_scale, GST_TYPE_D3D11_BASE_CONVERT);
2466 gst_d3d11_scale_class_init (GstD3D11ScaleClass * klass)
2468 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
2469 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
2470 GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
2472 gobject_class->set_property = gst_d3d11_scale_set_property;
2473 gobject_class->get_property = gst_d3d11_scale_get_property;
2476 * GstD3D11Scale:add-borders:
2478 * Add black borders if necessary to keep the display aspect ratio
2482 g_object_class_install_property (gobject_class, PROP_SCALE_ADD_BORDERS,
2483 g_param_spec_boolean ("add-borders", "Add Borders",
2484 "Add black borders if necessary to keep the display aspect ratio",
2485 DEFAULT_ADD_BORDERS, (GParamFlags) (GST_PARAM_MUTABLE_PLAYING |
2486 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
2488 gst_element_class_set_static_metadata (element_class,
2489 "Direct3D11 scaler",
2490 "Filter/Converter/Video/Scaler/Hardware",
2491 "Resizes video using Direct3D11",
2492 "Seungha Yang <seungha@centricular.com>");
2494 trans_class->transform_caps =
2495 GST_DEBUG_FUNCPTR (gst_d3d11_scale_transform_caps);
2496 trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_d3d11_scale_fixate_caps);
2500 gst_d3d11_scale_init (GstD3D11Scale * self)
2505 gst_d3d11_scale_set_property (GObject * object, guint prop_id,
2506 const GValue * value, GParamSpec * pspec)
2508 GstD3D11BaseConvert *base = GST_D3D11_BASE_CONVERT (object);
2511 case PROP_CONVERT_ADD_BORDERS:
2512 gst_d3d11_base_convert_set_add_border (base, g_value_get_boolean (value));
2515 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2521 gst_d3d11_scale_get_property (GObject * object, guint prop_id,
2522 GValue * value, GParamSpec * pspec)
2524 GstD3D11BaseConvert *base = GST_D3D11_BASE_CONVERT (object);
2527 case PROP_CONVERT_ADD_BORDERS:
2528 g_value_set_boolean (value, base->add_borders);
2531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2537 gst_d3d11_scale_transform_caps (GstBaseTransform *
2538 trans, GstPadDirection direction, GstCaps * caps, GstCaps * filter)
2540 GstCaps *tmp, *tmp2;
2543 /* Get all possible caps that we can transform to */
2544 tmp = gst_d3d11_base_convert_caps_rangify_size_info (caps);
2547 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
2548 gst_caps_unref (tmp);
2554 GST_DEBUG_OBJECT (trans, "transformed %" GST_PTR_FORMAT " into %"
2555 GST_PTR_FORMAT, caps, result);
2561 gst_d3d11_scale_fixate_caps (GstBaseTransform * base,
2562 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
2564 GST_DEBUG_OBJECT (base,
2565 "trying to fixate othercaps %" GST_PTR_FORMAT " based on caps %"
2566 GST_PTR_FORMAT, othercaps, caps);
2569 gst_d3d11_base_convert_fixate_size (base, direction, caps, othercaps);
2571 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);