2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2005-2012 David Schleef <ds@schleef.org>
4 * Copyright (C) <2019> Seungha Yang <seungha.yang@navercorp.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-cudascale
25 * @see_also: cudaconvert
27 * This element resizes video frames. By default the element will try to
28 * negotiate to the same size on the source and sinkpad so that no scaling
29 * is needed. It is therefore safe to insert this element in a pipeline to
30 * get more robust behaviour without any cost if no scaling is needed.
32 * This element supports some YUV formats which are are also supported by
33 * nvidia encoders and decoders.
35 * ## Example pipelines
37 * gst-launch-1.0 -v filesrc location=videotestsrc.mp4 ! qtdemux ! h264parse ! nvh264dec ! cudaconvert ! cudascale ! cudaconvert ! cudadownload ! autovideosink
39 * Decode a mp4/h264 and display the video. If the video sink chosen
40 * cannot perform scaling, the video scaling will be performed by cudascale
42 * gst-launch-1.0 -v filesrc location=videotestsrc.mp4 ! qtdemux ! h264parse ! nvh264dec ! cudaconvert ! cudascale ! cudaconvert ! cudadownload ! video/x-raw,width=100 ! autovideosink
44 * Decode an mp4/h264 and display the video with a width of 100.
53 #include <gst/cuda/gstcudautils.h>
55 #include "gstcudascale.h"
57 GST_DEBUG_CATEGORY_STATIC (gst_cuda_scale_debug);
58 #define GST_CAT_DEFAULT gst_cuda_scale_debug
60 #define gst_cuda_scale_parent_class parent_class
61 G_DEFINE_TYPE (GstCudaScale, gst_cuda_scale, GST_TYPE_CUDA_BASE_FILTER);
63 static GstCaps *gst_cuda_scale_transform_caps (GstBaseTransform * trans,
64 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
65 static GstCaps *gst_cuda_scale_fixate_caps (GstBaseTransform * base,
66 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
67 static gboolean gst_cuda_scale_set_info (GstCudaBaseTransform * filter,
68 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
69 GstVideoInfo * out_info);
72 gst_cuda_scale_class_init (GstCudaScaleClass * klass)
74 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
75 GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
76 GstCudaBaseTransformClass *btrans_class =
77 GST_CUDA_BASE_TRANSFORM_CLASS (klass);
79 gst_element_class_set_static_metadata (element_class,
81 "Filter/Converter/Video/Scaler/Hardware",
82 "Resizes Video using CUDA", "Seungha Yang <seungha.yang@navercorp.com>");
84 trans_class->transform_caps =
85 GST_DEBUG_FUNCPTR (gst_cuda_scale_transform_caps);
86 trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_cuda_scale_fixate_caps);
88 btrans_class->set_info = GST_DEBUG_FUNCPTR (gst_cuda_scale_set_info);
90 GST_DEBUG_CATEGORY_INIT (gst_cuda_scale_debug,
91 "cudascale", 0, "Video Resize using CUDA");
95 gst_cuda_scale_init (GstCudaScale * cuda)
100 gst_cuda_scale_transform_caps (GstBaseTransform * trans,
101 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
104 GstStructure *structure;
105 GstCapsFeatures *features;
108 GST_DEBUG_OBJECT (trans,
109 "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps,
110 (direction == GST_PAD_SINK) ? "sink" : "src");
112 ret = gst_caps_new_empty ();
113 n = gst_caps_get_size (caps);
114 for (i = 0; i < n; i++) {
115 structure = gst_caps_get_structure (caps, i);
116 features = gst_caps_get_features (caps, i);
118 /* If this is already expressed by the existing caps
119 * skip this structure */
120 if (i > 0 && gst_caps_is_subset_structure_full (ret, structure, features))
124 structure = gst_structure_copy (structure);
126 gst_structure_set (structure, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
127 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
129 /* if pixel aspect ratio, make a range of it */
130 if (gst_structure_has_field (structure, "pixel-aspect-ratio")) {
131 gst_structure_set (structure, "pixel-aspect-ratio",
132 GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1, NULL);
135 gst_caps_append_structure_full (ret, structure,
136 gst_caps_features_copy (features));
140 GstCaps *intersection;
143 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
144 gst_caps_unref (ret);
148 GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
153 /* fork of gstvideoscale */
155 gst_cuda_scale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
156 GstCaps * caps, GstCaps * othercaps)
158 GstStructure *ins, *outs;
159 const GValue *from_par, *to_par;
160 GValue fpar = G_VALUE_INIT;
161 GValue tpar = G_VALUE_INIT;
163 othercaps = gst_caps_truncate (othercaps);
164 othercaps = gst_caps_make_writable (othercaps);
166 GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
167 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
169 ins = gst_caps_get_structure (caps, 0);
170 outs = gst_caps_get_structure (othercaps, 0);
172 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
173 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
175 /* If we're fixating from the sinkpad we always set the PAR and
176 * assume that missing PAR on the sinkpad means 1/1 and
177 * missing PAR on the srcpad means undefined
179 if (direction == GST_PAD_SINK) {
181 g_value_init (&fpar, GST_TYPE_FRACTION);
182 gst_value_set_fraction (&fpar, 1, 1);
186 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
187 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
192 g_value_init (&tpar, GST_TYPE_FRACTION);
193 gst_value_set_fraction (&tpar, 1, 1);
196 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
200 g_value_init (&fpar, GST_TYPE_FRACTION);
201 gst_value_set_fraction (&fpar, 1, 1);
206 /* we have both PAR but they might not be fixated */
208 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
210 gint from_dar_n, from_dar_d;
213 /* from_par should be fixed */
214 g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
216 from_par_n = gst_value_get_fraction_numerator (from_par);
217 from_par_d = gst_value_get_fraction_denominator (from_par);
219 gst_structure_get_int (ins, "width", &from_w);
220 gst_structure_get_int (ins, "height", &from_h);
222 gst_structure_get_int (outs, "width", &w);
223 gst_structure_get_int (outs, "height", &h);
225 /* if both width and height are already fixed, we can't do anything
226 * about it anymore */
230 GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
232 if (!gst_value_is_fixed (to_par)) {
233 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
234 from_par_n, from_par_d, w, h)) {
235 GST_DEBUG_OBJECT (base, "fixating to_par to %dx%d", n, d);
236 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
237 gst_structure_fixate_field_nearest_fraction (outs,
238 "pixel-aspect-ratio", n, d);
240 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
247 /* Calculate input DAR */
248 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
249 &from_dar_n, &from_dar_d)) {
250 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
251 ("Error calculating the output scaled size - integer overflow"));
255 GST_DEBUG_OBJECT (base, "Input DAR is %d/%d", from_dar_n, from_dar_d);
257 /* If either width or height are fixed there's not much we
258 * can do either except choosing a height or width and PAR
259 * that matches the DAR as good as possible
263 gint set_w, set_par_n, set_par_d;
265 GST_DEBUG_OBJECT (base, "height is fixed (%d)", h);
267 /* If the PAR is fixed too, there's not much to do
268 * except choosing the width that is nearest to the
269 * width with the same DAR */
270 if (gst_value_is_fixed (to_par)) {
271 to_par_n = gst_value_get_fraction_numerator (to_par);
272 to_par_d = gst_value_get_fraction_denominator (to_par);
274 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
276 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
277 to_par_n, &num, &den)) {
278 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
279 ("Error calculating the output scaled size - integer overflow"));
283 w = (guint) gst_util_uint64_scale_int_round (h, num, den);
284 gst_structure_fixate_field_nearest_int (outs, "width", w);
289 /* The PAR is not fixed and it's quite likely that we can set
290 * an arbitrary PAR. */
292 /* Check if we can keep the input width */
293 tmp = gst_structure_copy (outs);
294 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
295 gst_structure_get_int (tmp, "width", &set_w);
297 /* Might have failed but try to keep the DAR nonetheless by
298 * adjusting the PAR */
299 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
300 &to_par_n, &to_par_d)) {
301 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
302 ("Error calculating the output scaled size - integer overflow"));
303 gst_structure_free (tmp);
307 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
308 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
309 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
311 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
313 gst_structure_free (tmp);
315 /* Check if the adjusted PAR is accepted */
316 if (set_par_n == to_par_n && set_par_d == to_par_d) {
317 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
318 set_par_n != set_par_d)
319 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
320 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
325 /* Otherwise scale the width to the new PAR and check if the
326 * adjusted with is accepted. If all that fails we can't keep
328 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
329 set_par_n, &num, &den)) {
330 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
331 ("Error calculating the output scaled size - integer overflow"));
335 w = (guint) gst_util_uint64_scale_int_round (h, num, den);
336 gst_structure_fixate_field_nearest_int (outs, "width", w);
337 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
338 set_par_n != set_par_d)
339 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
340 set_par_n, set_par_d, NULL);
345 gint set_h, set_par_n, set_par_d;
347 GST_DEBUG_OBJECT (base, "width is fixed (%d)", w);
349 /* If the PAR is fixed too, there's not much to do
350 * except choosing the height that is nearest to the
351 * height with the same DAR */
352 if (gst_value_is_fixed (to_par)) {
353 to_par_n = gst_value_get_fraction_numerator (to_par);
354 to_par_d = gst_value_get_fraction_denominator (to_par);
356 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
358 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
359 to_par_n, &num, &den)) {
360 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
361 ("Error calculating the output scaled size - integer overflow"));
365 h = (guint) gst_util_uint64_scale_int_round (w, den, num);
366 gst_structure_fixate_field_nearest_int (outs, "height", h);
371 /* The PAR is not fixed and it's quite likely that we can set
372 * an arbitrary PAR. */
374 /* Check if we can keep the input height */
375 tmp = gst_structure_copy (outs);
376 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
377 gst_structure_get_int (tmp, "height", &set_h);
379 /* Might have failed but try to keep the DAR nonetheless by
380 * adjusting the PAR */
381 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
382 &to_par_n, &to_par_d)) {
383 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
384 ("Error calculating the output scaled size - integer overflow"));
385 gst_structure_free (tmp);
388 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
389 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
390 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
392 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
394 gst_structure_free (tmp);
396 /* Check if the adjusted PAR is accepted */
397 if (set_par_n == to_par_n && set_par_d == to_par_d) {
398 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
399 set_par_n != set_par_d)
400 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
401 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
406 /* Otherwise scale the height to the new PAR and check if the
407 * adjusted with is accepted. If all that fails we can't keep
409 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
410 set_par_n, &num, &den)) {
411 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
412 ("Error calculating the output scaled size - integer overflow"));
416 h = (guint) gst_util_uint64_scale_int_round (w, den, num);
417 gst_structure_fixate_field_nearest_int (outs, "height", h);
418 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
419 set_par_n != set_par_d)
420 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
421 set_par_n, set_par_d, NULL);
424 } else if (gst_value_is_fixed (to_par)) {
426 gint set_h, set_w, f_h, f_w;
428 to_par_n = gst_value_get_fraction_numerator (to_par);
429 to_par_d = gst_value_get_fraction_denominator (to_par);
431 /* Calculate scale factor for the PAR change */
432 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
433 to_par_d, &num, &den)) {
434 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
435 ("Error calculating the output scaled size - integer overflow"));
439 /* Try to keep the input height (because of interlacing) */
440 tmp = gst_structure_copy (outs);
441 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
442 gst_structure_get_int (tmp, "height", &set_h);
444 /* This might have failed but try to scale the width
445 * to keep the DAR nonetheless */
446 w = (guint) gst_util_uint64_scale_int_round (set_h, num, den);
447 gst_structure_fixate_field_nearest_int (tmp, "width", w);
448 gst_structure_get_int (tmp, "width", &set_w);
449 gst_structure_free (tmp);
451 /* We kept the DAR and the height is nearest to the original height */
453 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
454 G_TYPE_INT, set_h, NULL);
461 /* If the former failed, try to keep the input width at least */
462 tmp = gst_structure_copy (outs);
463 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
464 gst_structure_get_int (tmp, "width", &set_w);
466 /* This might have failed but try to scale the width
467 * to keep the DAR nonetheless */
468 h = (guint) gst_util_uint64_scale_int_round (set_w, den, num);
469 gst_structure_fixate_field_nearest_int (tmp, "height", h);
470 gst_structure_get_int (tmp, "height", &set_h);
471 gst_structure_free (tmp);
473 /* We kept the DAR and the width is nearest to the original width */
475 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
476 G_TYPE_INT, set_h, NULL);
480 /* If all this failed, keep the dimensions with the DAR that was closest
481 * to the correct DAR. This changes the DAR but there's not much else to
484 if (set_w * ABS (set_h - h) < ABS (f_w - w) * f_h) {
488 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
493 gint set_h, set_w, set_par_n, set_par_d, tmp2;
495 /* width, height and PAR are not fixed but passthrough is not possible */
497 /* First try to keep the height and width as good as possible
499 tmp = gst_structure_copy (outs);
500 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
501 gst_structure_get_int (tmp, "height", &set_h);
502 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
503 gst_structure_get_int (tmp, "width", &set_w);
505 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
506 &to_par_n, &to_par_d)) {
507 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
508 ("Error calculating the output scaled size - integer overflow"));
509 gst_structure_free (tmp);
513 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
514 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
515 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
517 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
519 gst_structure_free (tmp);
521 if (set_par_n == to_par_n && set_par_d == to_par_d) {
522 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
523 G_TYPE_INT, set_h, NULL);
525 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
526 set_par_n != set_par_d)
527 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
528 set_par_n, set_par_d, NULL);
532 /* Otherwise try to scale width to keep the DAR with the set
534 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
535 set_par_n, &num, &den)) {
536 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
537 ("Error calculating the output scaled size - integer overflow"));
541 w = (guint) gst_util_uint64_scale_int_round (set_h, num, den);
542 tmp = gst_structure_copy (outs);
543 gst_structure_fixate_field_nearest_int (tmp, "width", w);
544 gst_structure_get_int (tmp, "width", &tmp2);
545 gst_structure_free (tmp);
548 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
549 G_TYPE_INT, set_h, NULL);
550 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
551 set_par_n != set_par_d)
552 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
553 set_par_n, set_par_d, NULL);
557 /* ... or try the same with the height */
558 h = (guint) gst_util_uint64_scale_int_round (set_w, den, num);
559 tmp = gst_structure_copy (outs);
560 gst_structure_fixate_field_nearest_int (tmp, "height", h);
561 gst_structure_get_int (tmp, "height", &tmp2);
562 gst_structure_free (tmp);
565 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
566 G_TYPE_INT, tmp2, NULL);
567 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
568 set_par_n != set_par_d)
569 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
570 set_par_n, set_par_d, NULL);
574 /* If all fails we can't keep the DAR and take the nearest values
575 * for everything from the first try */
576 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
577 G_TYPE_INT, set_h, NULL);
578 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
579 set_par_n != set_par_d)
580 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
581 set_par_n, set_par_d, NULL);
586 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
588 if (from_par == &fpar)
589 g_value_unset (&fpar);
591 g_value_unset (&tpar);
597 gst_cuda_scale_set_info (GstCudaBaseTransform * btrans, GstCaps * incaps,
598 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
600 if (GST_VIDEO_INFO_WIDTH (in_info) == GST_VIDEO_INFO_WIDTH (out_info) &&
601 GST_VIDEO_INFO_HEIGHT (in_info) == GST_VIDEO_INFO_HEIGHT (out_info) &&
602 GST_VIDEO_INFO_FORMAT (in_info) == GST_VIDEO_INFO_FORMAT (out_info)) {
603 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (btrans), TRUE);
606 return GST_CUDA_BASE_TRANSFORM_CLASS (parent_class)->set_info (btrans,
607 incaps, in_info, outcaps, out_info);