cuda: Add convertscale element
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / nvcodec / gstcudascale.c
1 /* GStreamer
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:element-cudascale
24  * @title: cudascale
25  * @see_also: cudaconvert
26  *
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.
31  *
32  * This element supports some YUV formats which are are also supported by
33  * nvidia encoders and decoders.
34  *
35  * ## Example pipelines
36  * |[
37  * gst-launch-1.0 -v filesrc location=videotestsrc.mp4 ! qtdemux ! h264parse ! nvh264dec ! cudaconvert ! cudascale ! cudaconvert ! cudadownload ! autovideosink
38  * ]|
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
41  * |[
42  * gst-launch-1.0 -v filesrc location=videotestsrc.mp4 ! qtdemux ! h264parse ! nvh264dec ! cudaconvert ! cudascale ! cudaconvert ! cudadownload ! video/x-raw,width=100 ! autovideosink
43  * ]|
44  *  Decode an mp4/h264 and display the video with a width of 100.
45  *
46  * Since: 1.20
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #  include <config.h>
51 #endif
52
53 #include <gst/cuda/gstcudautils.h>
54
55 #include "gstcudascale.h"
56
57 GST_DEBUG_CATEGORY_STATIC (gst_cuda_scale_debug);
58 #define GST_CAT_DEFAULT gst_cuda_scale_debug
59
60 #define gst_cuda_scale_parent_class parent_class
61 G_DEFINE_TYPE (GstCudaScale, gst_cuda_scale, GST_TYPE_CUDA_BASE_FILTER);
62
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);
70
71 static void
72 gst_cuda_scale_class_init (GstCudaScaleClass * klass)
73 {
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);
78
79   gst_element_class_set_static_metadata (element_class,
80       "CUDA Video scaler",
81       "Filter/Converter/Video/Scaler/Hardware",
82       "Resizes Video using CUDA", "Seungha Yang <seungha.yang@navercorp.com>");
83
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);
87
88   btrans_class->set_info = GST_DEBUG_FUNCPTR (gst_cuda_scale_set_info);
89
90   GST_DEBUG_CATEGORY_INIT (gst_cuda_scale_debug,
91       "cudascale", 0, "Video Resize using CUDA");
92 }
93
94 static void
95 gst_cuda_scale_init (GstCudaScale * cuda)
96 {
97 }
98
99 static GstCaps *
100 gst_cuda_scale_transform_caps (GstBaseTransform * trans,
101     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
102 {
103   GstCaps *ret;
104   GstStructure *structure;
105   GstCapsFeatures *features;
106   gint i, n;
107
108   GST_DEBUG_OBJECT (trans,
109       "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps,
110       (direction == GST_PAD_SINK) ? "sink" : "src");
111
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);
117
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))
121       continue;
122
123     /* make copy */
124     structure = gst_structure_copy (structure);
125
126     gst_structure_set (structure, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
127         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
128
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);
133     }
134
135     gst_caps_append_structure_full (ret, structure,
136         gst_caps_features_copy (features));
137   }
138
139   if (filter) {
140     GstCaps *intersection;
141
142     intersection =
143         gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
144     gst_caps_unref (ret);
145     ret = intersection;
146   }
147
148   GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
149
150   return ret;
151 }
152
153 /* fork of gstvideoscale */
154 static GstCaps *
155 gst_cuda_scale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
156     GstCaps * caps, GstCaps * othercaps)
157 {
158   GstStructure *ins, *outs;
159   const GValue *from_par, *to_par;
160   GValue fpar = G_VALUE_INIT;
161   GValue tpar = G_VALUE_INIT;
162
163   othercaps = gst_caps_truncate (othercaps);
164   othercaps = gst_caps_make_writable (othercaps);
165
166   GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
167       " based on caps %" GST_PTR_FORMAT, othercaps, caps);
168
169   ins = gst_caps_get_structure (caps, 0);
170   outs = gst_caps_get_structure (othercaps, 0);
171
172   from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
173   to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
174
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
178    */
179   if (direction == GST_PAD_SINK) {
180     if (!from_par) {
181       g_value_init (&fpar, GST_TYPE_FRACTION);
182       gst_value_set_fraction (&fpar, 1, 1);
183       from_par = &fpar;
184     }
185     if (!to_par) {
186       g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
187       gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
188       to_par = &tpar;
189     }
190   } else {
191     if (!to_par) {
192       g_value_init (&tpar, GST_TYPE_FRACTION);
193       gst_value_set_fraction (&tpar, 1, 1);
194       to_par = &tpar;
195
196       gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
197           NULL);
198     }
199     if (!from_par) {
200       g_value_init (&fpar, GST_TYPE_FRACTION);
201       gst_value_set_fraction (&fpar, 1, 1);
202       from_par = &fpar;
203     }
204   }
205
206   /* we have both PAR but they might not be fixated */
207   {
208     gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
209     gint w = 0, h = 0;
210     gint from_dar_n, from_dar_d;
211     gint num, den;
212
213     /* from_par should be fixed */
214     g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
215
216     from_par_n = gst_value_get_fraction_numerator (from_par);
217     from_par_d = gst_value_get_fraction_denominator (from_par);
218
219     gst_structure_get_int (ins, "width", &from_w);
220     gst_structure_get_int (ins, "height", &from_h);
221
222     gst_structure_get_int (outs, "width", &w);
223     gst_structure_get_int (outs, "height", &h);
224
225     /* if both width and height are already fixed, we can't do anything
226      * about it anymore */
227     if (w && h) {
228       guint n, d;
229
230       GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
231           w, h);
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);
239           else if (n != d)
240             gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
241                 n, d, NULL);
242         }
243       }
244       goto done;
245     }
246
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"));
252       goto done;
253     }
254
255     GST_DEBUG_OBJECT (base, "Input DAR is %d/%d", from_dar_n, from_dar_d);
256
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
260      */
261     if (h) {
262       GstStructure *tmp;
263       gint set_w, set_par_n, set_par_d;
264
265       GST_DEBUG_OBJECT (base, "height is fixed (%d)", h);
266
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);
273
274         GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
275
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"));
280           goto done;
281         }
282
283         w = (guint) gst_util_uint64_scale_int_round (h, num, den);
284         gst_structure_fixate_field_nearest_int (outs, "width", w);
285
286         goto done;
287       }
288
289       /* The PAR is not fixed and it's quite likely that we can set
290        * an arbitrary PAR. */
291
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);
296
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);
304         goto done;
305       }
306
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",
310           to_par_n, to_par_d);
311       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
312           &set_par_d);
313       gst_structure_free (tmp);
314
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,
321               NULL);
322         goto done;
323       }
324
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
327        * the DAR */
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"));
332         goto done;
333       }
334
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);
341
342       goto done;
343     } else if (w) {
344       GstStructure *tmp;
345       gint set_h, set_par_n, set_par_d;
346
347       GST_DEBUG_OBJECT (base, "width is fixed (%d)", w);
348
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);
355
356         GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
357
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"));
362           goto done;
363         }
364
365         h = (guint) gst_util_uint64_scale_int_round (w, den, num);
366         gst_structure_fixate_field_nearest_int (outs, "height", h);
367
368         goto done;
369       }
370
371       /* The PAR is not fixed and it's quite likely that we can set
372        * an arbitrary PAR. */
373
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);
378
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);
386         goto done;
387       }
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",
391           to_par_n, to_par_d);
392       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
393           &set_par_d);
394       gst_structure_free (tmp);
395
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,
402               NULL);
403         goto done;
404       }
405
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
408        * the DAR */
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"));
413         goto done;
414       }
415
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);
422
423       goto done;
424     } else if (gst_value_is_fixed (to_par)) {
425       GstStructure *tmp;
426       gint set_h, set_w, f_h, f_w;
427
428       to_par_n = gst_value_get_fraction_numerator (to_par);
429       to_par_d = gst_value_get_fraction_denominator (to_par);
430
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"));
436         goto done;
437       }
438
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);
443
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);
450
451       /* We kept the DAR and the height is nearest to the original height */
452       if (set_w == w) {
453         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
454             G_TYPE_INT, set_h, NULL);
455         goto done;
456       }
457
458       f_h = set_h;
459       f_w = set_w;
460
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);
465
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);
472
473       /* We kept the DAR and the width is nearest to the original width */
474       if (set_h == h) {
475         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
476             G_TYPE_INT, set_h, NULL);
477         goto done;
478       }
479
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
482        * do here.
483        */
484       if (set_w * ABS (set_h - h) < ABS (f_w - w) * f_h) {
485         f_h = set_h;
486         f_w = set_w;
487       }
488       gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
489           f_h, NULL);
490       goto done;
491     } else {
492       GstStructure *tmp;
493       gint set_h, set_w, set_par_n, set_par_d, tmp2;
494
495       /* width, height and PAR are not fixed but passthrough is not possible */
496
497       /* First try to keep the height and width as good as possible
498        * and scale PAR */
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);
504
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);
510         goto done;
511       }
512
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",
516           to_par_n, to_par_d);
517       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
518           &set_par_d);
519       gst_structure_free (tmp);
520
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);
524
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);
529         goto done;
530       }
531
532       /* Otherwise try to scale width to keep the DAR with the set
533        * PAR and height */
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"));
538         goto done;
539       }
540
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);
546
547       if (tmp2 == w) {
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);
554         goto done;
555       }
556
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);
563
564       if (tmp2 == h) {
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);
571         goto done;
572       }
573
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);
582     }
583   }
584
585 done:
586   GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
587
588   if (from_par == &fpar)
589     g_value_unset (&fpar);
590   if (to_par == &tpar)
591     g_value_unset (&tpar);
592
593   return othercaps;
594 }
595
596 static gboolean
597 gst_cuda_scale_set_info (GstCudaBaseTransform * btrans, GstCaps * incaps,
598     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
599 {
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);
604   }
605
606   return GST_CUDA_BASE_TRANSFORM_CLASS (parent_class)->set_info (btrans,
607       incaps, in_info, outcaps, out_info);
608 }