plugins: uddate gst_type_mark_as_plugin_api() calls
[platform/upstream/gstreamer.git] / gst / videoscale / gstvideoscale.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2005-2012 David Schleef <ds@schleef.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:element-videoscale
23  * @title: videoscale
24  * @see_also: videorate, videoconvert
25  *
26  * This element resizes video frames. By default the element will try to
27  * negotiate to the same size on the source and sinkpad so that no scaling
28  * is needed. It is therefore safe to insert this element in a pipeline to
29  * get more robust behaviour without any cost if no scaling is needed.
30  *
31  * This element supports a wide range of color spaces including various YUV and
32  * RGB formats and is therefore generally able to operate anywhere in a
33  * pipeline.
34  *
35  * ## Example pipelines
36  * |[
37  * gst-launch-1.0 -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videoconvert ! videoscale ! autovideosink
38  * ]|
39  *  Decode an Ogg/Theora and display the video. If the video sink chosen
40  * cannot perform scaling, the video scaling will be performed by videoscale
41  * when you resize the video window.
42  * To create the test Ogg/Theora file refer to the documentation of theoraenc.
43  * |[
44  * gst-launch-1.0 -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videoconvert ! videoscale ! video/x-raw,width=100 ! autovideosink
45  * ]|
46  *  Decode an Ogg/Theora and display the video with a width of 100.
47  *
48  */
49
50 /* 
51  * Formulas for PAR, DAR, width and height relations:
52  *
53  * dar_n   w   par_n
54  * ----- = - * -----
55  * dar_d   h   par_d
56  *
57  * par_n    h   dar_n
58  * ----- =  - * -----
59  * par_d    w   dar_d
60  * 
61  *         dar_n   par_d
62  * w = h * ----- * -----
63  *         dar_d   par_n
64  *
65  *         dar_d   par_n
66  * h = w * ----- * -----
67  *         dar_n   par_d
68  */
69
70 #ifdef HAVE_CONFIG_H
71 #include "config.h"
72 #endif
73
74 #include <string.h>
75
76 #include <math.h>
77
78 #include <gst/video/gstvideometa.h>
79 #include <gst/video/gstvideopool.h>
80
81 #include "gstvideoscale.h"
82
83 #define GST_CAT_DEFAULT video_scale_debug
84 GST_DEBUG_CATEGORY_STATIC (video_scale_debug);
85 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
86
87 #define DEFAULT_PROP_METHOD       GST_VIDEO_SCALE_BILINEAR
88 #define DEFAULT_PROP_ADD_BORDERS  TRUE
89 #define DEFAULT_PROP_SHARPNESS    1.0
90 #define DEFAULT_PROP_SHARPEN      0.0
91 #define DEFAULT_PROP_DITHER       FALSE
92 #define DEFAULT_PROP_SUBMETHOD    1
93 #define DEFAULT_PROP_ENVELOPE     2.0
94 #define DEFAULT_PROP_GAMMA_DECODE FALSE
95 #define DEFAULT_PROP_N_THREADS    1
96
97 enum
98 {
99   PROP_0,
100   PROP_METHOD,
101   PROP_ADD_BORDERS,
102   PROP_SHARPNESS,
103   PROP_SHARPEN,
104   PROP_DITHER,
105   PROP_SUBMETHOD,
106   PROP_ENVELOPE,
107   PROP_GAMMA_DECODE,
108   PROP_N_THREADS
109 };
110
111 #undef GST_VIDEO_SIZE_RANGE
112 #define GST_VIDEO_SIZE_RANGE "(int) [ 1, 32767]"
113
114 /* FIXME: add v210 support
115  * FIXME: add v216 support
116  * FIXME: add UYVP support
117  * FIXME: add A420 support
118  * FIXME: add YUV9 support
119  * FIXME: add YVU9 support
120  * FIXME: add IYU1 support
121  * FIXME: add r210 support
122  */
123
124 #define GST_VIDEO_FORMATS GST_VIDEO_FORMATS_ALL
125
126 static GstStaticCaps gst_video_scale_format_caps =
127     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS) ";"
128     GST_VIDEO_CAPS_MAKE_WITH_FEATURES ("ANY", GST_VIDEO_FORMATS));
129
130 GQuark _size_quark;
131 GQuark _scale_quark;
132
133 #define GST_TYPE_VIDEO_SCALE_METHOD (gst_video_scale_method_get_type())
134 static GType
135 gst_video_scale_method_get_type (void)
136 {
137   static GType video_scale_method_type = 0;
138
139   static const GEnumValue video_scale_methods[] = {
140     {GST_VIDEO_SCALE_NEAREST, "Nearest Neighbour", "nearest-neighbour"},
141     {GST_VIDEO_SCALE_BILINEAR, "Bilinear (2-tap)", "bilinear"},
142     {GST_VIDEO_SCALE_4TAP, "4-tap Sinc", "4-tap"},
143     {GST_VIDEO_SCALE_LANCZOS, "Lanczos", "lanczos"},
144     {GST_VIDEO_SCALE_BILINEAR2, "Bilinear (multi-tap)", "bilinear2"},
145     {GST_VIDEO_SCALE_SINC, "Sinc (multi-tap)", "sinc"},
146     {GST_VIDEO_SCALE_HERMITE, "Hermite (multi-tap)", "hermite"},
147     {GST_VIDEO_SCALE_SPLINE, "Spline (multi-tap)", "spline"},
148     {GST_VIDEO_SCALE_CATROM, "Catmull-Rom (multi-tap)", "catrom"},
149     {GST_VIDEO_SCALE_MITCHELL, "Mitchell (multi-tap)", "mitchell"},
150     {0, NULL, NULL},
151   };
152
153   if (!video_scale_method_type) {
154     video_scale_method_type =
155         g_enum_register_static ("GstVideoScaleMethod", video_scale_methods);
156   }
157   return video_scale_method_type;
158 }
159
160 static GstCaps *
161 gst_video_scale_get_capslist (void)
162 {
163   static GstCaps *caps = NULL;
164   static volatile gsize inited = 0;
165
166   if (g_once_init_enter (&inited)) {
167     caps = gst_static_caps_get (&gst_video_scale_format_caps);
168     g_once_init_leave (&inited, 1);
169   }
170   return caps;
171 }
172
173 static GstPadTemplate *
174 gst_video_scale_src_template_factory (void)
175 {
176   return gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
177       gst_video_scale_get_capslist ());
178 }
179
180 static GstPadTemplate *
181 gst_video_scale_sink_template_factory (void)
182 {
183   return gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
184       gst_video_scale_get_capslist ());
185 }
186
187
188 static void gst_video_scale_finalize (GstVideoScale * videoscale);
189 static gboolean gst_video_scale_src_event (GstBaseTransform * trans,
190     GstEvent * event);
191
192 /* base transform vmethods */
193 static GstCaps *gst_video_scale_transform_caps (GstBaseTransform * trans,
194     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
195 static GstCaps *gst_video_scale_fixate_caps (GstBaseTransform * base,
196     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
197 static gboolean gst_video_scale_transform_meta (GstBaseTransform * trans,
198     GstBuffer * inbuf, GstMeta * meta, GstBuffer * outbuf);
199
200 static gboolean gst_video_scale_set_info (GstVideoFilter * filter,
201     GstCaps * in, GstVideoInfo * in_info, GstCaps * out,
202     GstVideoInfo * out_info);
203 static GstFlowReturn gst_video_scale_transform_frame (GstVideoFilter * filter,
204     GstVideoFrame * in, GstVideoFrame * out);
205
206 static void gst_video_scale_set_property (GObject * object, guint prop_id,
207     const GValue * value, GParamSpec * pspec);
208 static void gst_video_scale_get_property (GObject * object, guint prop_id,
209     GValue * value, GParamSpec * pspec);
210
211 #define gst_video_scale_parent_class parent_class
212 G_DEFINE_TYPE (GstVideoScale, gst_video_scale, GST_TYPE_VIDEO_FILTER);
213
214 static void
215 gst_video_scale_class_init (GstVideoScaleClass * klass)
216 {
217   GObjectClass *gobject_class = (GObjectClass *) klass;
218   GstElementClass *element_class = (GstElementClass *) klass;
219   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
220   GstVideoFilterClass *filter_class = (GstVideoFilterClass *) klass;
221
222   gobject_class->finalize = (GObjectFinalizeFunc) gst_video_scale_finalize;
223   gobject_class->set_property = gst_video_scale_set_property;
224   gobject_class->get_property = gst_video_scale_get_property;
225
226   g_object_class_install_property (gobject_class, PROP_METHOD,
227       g_param_spec_enum ("method", "method", "method",
228           GST_TYPE_VIDEO_SCALE_METHOD, DEFAULT_PROP_METHOD,
229           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230
231   g_object_class_install_property (gobject_class, PROP_ADD_BORDERS,
232       g_param_spec_boolean ("add-borders", "Add Borders",
233           "Add black borders if necessary to keep the display aspect ratio",
234           DEFAULT_PROP_ADD_BORDERS,
235           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236
237   g_object_class_install_property (gobject_class, PROP_SHARPNESS,
238       g_param_spec_double ("sharpness", "Sharpness",
239           "Sharpness of filter", 0.5, 1.5, DEFAULT_PROP_SHARPNESS,
240           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241
242   g_object_class_install_property (gobject_class, PROP_SHARPEN,
243       g_param_spec_double ("sharpen", "Sharpen",
244           "Sharpening", 0.0, 1.0, DEFAULT_PROP_SHARPEN,
245           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246
247   g_object_class_install_property (gobject_class, PROP_DITHER,
248       g_param_spec_boolean ("dither", "Dither",
249           "Add dither (only used for Lanczos method)",
250           DEFAULT_PROP_DITHER,
251           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252
253 #if 0
254   /* I am hiding submethod for now, since it's poorly named, poorly
255    * documented, and will probably just get people into trouble. */
256   g_object_class_install_property (gobject_class, PROP_SUBMETHOD,
257       g_param_spec_int ("submethod", "submethod",
258           "submethod", 0, 3, DEFAULT_PROP_SUBMETHOD,
259           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 #endif
261
262   g_object_class_install_property (gobject_class, PROP_ENVELOPE,
263       g_param_spec_double ("envelope", "Envelope",
264           "Size of filter envelope", 1.0, 5.0, DEFAULT_PROP_ENVELOPE,
265           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266
267   g_object_class_install_property (gobject_class, PROP_GAMMA_DECODE,
268       g_param_spec_boolean ("gamma-decode", "Gamma Decode",
269           "Decode gamma before scaling", DEFAULT_PROP_GAMMA_DECODE,
270           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271
272   g_object_class_install_property (gobject_class, PROP_N_THREADS,
273       g_param_spec_uint ("n-threads", "Threads",
274           "Maximum number of threads to use", 0, G_MAXUINT,
275           DEFAULT_PROP_N_THREADS,
276           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277
278   gst_element_class_set_static_metadata (element_class,
279       "Video scaler", "Filter/Converter/Video/Scaler",
280       "Resizes video", "Wim Taymans <wim.taymans@gmail.com>");
281
282   gst_element_class_add_pad_template (element_class,
283       gst_video_scale_sink_template_factory ());
284   gst_element_class_add_pad_template (element_class,
285       gst_video_scale_src_template_factory ());
286
287   trans_class->transform_caps =
288       GST_DEBUG_FUNCPTR (gst_video_scale_transform_caps);
289   trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_video_scale_fixate_caps);
290   trans_class->src_event = GST_DEBUG_FUNCPTR (gst_video_scale_src_event);
291   trans_class->transform_meta =
292       GST_DEBUG_FUNCPTR (gst_video_scale_transform_meta);
293
294   filter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_scale_set_info);
295   filter_class->transform_frame =
296       GST_DEBUG_FUNCPTR (gst_video_scale_transform_frame);
297
298   _size_quark = g_quark_from_static_string (GST_META_TAG_VIDEO_SIZE_STR);
299   _scale_quark = gst_video_meta_transform_scale_get_quark ();
300
301   gst_type_mark_as_plugin_api (GST_TYPE_VIDEO_SCALE_METHOD, 0);
302 }
303
304 static void
305 gst_video_scale_init (GstVideoScale * videoscale)
306 {
307   videoscale->method = DEFAULT_PROP_METHOD;
308   videoscale->add_borders = DEFAULT_PROP_ADD_BORDERS;
309   videoscale->submethod = DEFAULT_PROP_SUBMETHOD;
310   videoscale->sharpness = DEFAULT_PROP_SHARPNESS;
311   videoscale->sharpen = DEFAULT_PROP_SHARPEN;
312   videoscale->dither = DEFAULT_PROP_DITHER;
313   videoscale->envelope = DEFAULT_PROP_ENVELOPE;
314   videoscale->gamma_decode = DEFAULT_PROP_GAMMA_DECODE;
315   videoscale->n_threads = DEFAULT_PROP_N_THREADS;
316 }
317
318 static void
319 gst_video_scale_finalize (GstVideoScale * videoscale)
320 {
321   if (videoscale->convert)
322     gst_video_converter_free (videoscale->convert);
323
324   G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (videoscale));
325 }
326
327 static void
328 gst_video_scale_set_property (GObject * object, guint prop_id,
329     const GValue * value, GParamSpec * pspec)
330 {
331   GstVideoScale *vscale = GST_VIDEO_SCALE (object);
332
333   switch (prop_id) {
334     case PROP_METHOD:
335       GST_OBJECT_LOCK (vscale);
336       vscale->method = g_value_get_enum (value);
337       GST_OBJECT_UNLOCK (vscale);
338       break;
339     case PROP_ADD_BORDERS:
340       GST_OBJECT_LOCK (vscale);
341       vscale->add_borders = g_value_get_boolean (value);
342       GST_OBJECT_UNLOCK (vscale);
343       gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM_CAST (vscale));
344       break;
345     case PROP_SHARPNESS:
346       GST_OBJECT_LOCK (vscale);
347       vscale->sharpness = g_value_get_double (value);
348       GST_OBJECT_UNLOCK (vscale);
349       break;
350     case PROP_SHARPEN:
351       GST_OBJECT_LOCK (vscale);
352       vscale->sharpen = g_value_get_double (value);
353       GST_OBJECT_UNLOCK (vscale);
354       break;
355     case PROP_DITHER:
356       GST_OBJECT_LOCK (vscale);
357       vscale->dither = g_value_get_boolean (value);
358       GST_OBJECT_UNLOCK (vscale);
359       break;
360     case PROP_SUBMETHOD:
361       GST_OBJECT_LOCK (vscale);
362       vscale->submethod = g_value_get_int (value);
363       GST_OBJECT_UNLOCK (vscale);
364       break;
365     case PROP_ENVELOPE:
366       GST_OBJECT_LOCK (vscale);
367       vscale->envelope = g_value_get_double (value);
368       GST_OBJECT_UNLOCK (vscale);
369       break;
370     case PROP_GAMMA_DECODE:
371       GST_OBJECT_LOCK (vscale);
372       vscale->gamma_decode = g_value_get_boolean (value);
373       GST_OBJECT_UNLOCK (vscale);
374       break;
375     case PROP_N_THREADS:
376       GST_OBJECT_LOCK (vscale);
377       vscale->n_threads = g_value_get_uint (value);
378       GST_OBJECT_UNLOCK (vscale);
379       break;
380     default:
381       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382       break;
383   }
384 }
385
386 static void
387 gst_video_scale_get_property (GObject * object, guint prop_id, GValue * value,
388     GParamSpec * pspec)
389 {
390   GstVideoScale *vscale = GST_VIDEO_SCALE (object);
391
392   switch (prop_id) {
393     case PROP_METHOD:
394       GST_OBJECT_LOCK (vscale);
395       g_value_set_enum (value, vscale->method);
396       GST_OBJECT_UNLOCK (vscale);
397       break;
398     case PROP_ADD_BORDERS:
399       GST_OBJECT_LOCK (vscale);
400       g_value_set_boolean (value, vscale->add_borders);
401       GST_OBJECT_UNLOCK (vscale);
402       break;
403     case PROP_SHARPNESS:
404       GST_OBJECT_LOCK (vscale);
405       g_value_set_double (value, vscale->sharpness);
406       GST_OBJECT_UNLOCK (vscale);
407       break;
408     case PROP_SHARPEN:
409       GST_OBJECT_LOCK (vscale);
410       g_value_set_double (value, vscale->sharpen);
411       GST_OBJECT_UNLOCK (vscale);
412       break;
413     case PROP_DITHER:
414       GST_OBJECT_LOCK (vscale);
415       g_value_set_boolean (value, vscale->dither);
416       GST_OBJECT_UNLOCK (vscale);
417       break;
418     case PROP_SUBMETHOD:
419       GST_OBJECT_LOCK (vscale);
420       g_value_set_int (value, vscale->submethod);
421       GST_OBJECT_UNLOCK (vscale);
422       break;
423     case PROP_ENVELOPE:
424       GST_OBJECT_LOCK (vscale);
425       g_value_set_double (value, vscale->envelope);
426       GST_OBJECT_UNLOCK (vscale);
427       break;
428     case PROP_GAMMA_DECODE:
429       GST_OBJECT_LOCK (vscale);
430       g_value_set_boolean (value, vscale->gamma_decode);
431       GST_OBJECT_UNLOCK (vscale);
432       break;
433     case PROP_N_THREADS:
434       GST_OBJECT_LOCK (vscale);
435       g_value_set_uint (value, vscale->n_threads);
436       GST_OBJECT_UNLOCK (vscale);
437       break;
438     default:
439       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440       break;
441   }
442 }
443
444 static GstCaps *
445 gst_video_scale_transform_caps (GstBaseTransform * trans,
446     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
447 {
448   GstCaps *ret;
449   GstStructure *structure;
450   GstCapsFeatures *features;
451   gint i, n;
452
453   GST_DEBUG_OBJECT (trans,
454       "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps,
455       (direction == GST_PAD_SINK) ? "sink" : "src");
456
457   ret = gst_caps_new_empty ();
458   n = gst_caps_get_size (caps);
459   for (i = 0; i < n; i++) {
460     structure = gst_caps_get_structure (caps, i);
461     features = gst_caps_get_features (caps, i);
462
463     /* If this is already expressed by the existing caps
464      * skip this structure */
465     if (i > 0 && gst_caps_is_subset_structure_full (ret, structure, features))
466       continue;
467
468     /* make copy */
469     structure = gst_structure_copy (structure);
470
471     /* If the features are non-sysmem we can only do passthrough */
472     if (!gst_caps_features_is_any (features)
473         && gst_caps_features_is_equal (features,
474             GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY)) {
475       gst_structure_set (structure, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
476           "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
477
478       /* if pixel aspect ratio, make a range of it */
479       if (gst_structure_has_field (structure, "pixel-aspect-ratio")) {
480         gst_structure_set (structure, "pixel-aspect-ratio",
481             GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1, NULL);
482       }
483     }
484     gst_caps_append_structure_full (ret, structure,
485         gst_caps_features_copy (features));
486   }
487
488   if (filter) {
489     GstCaps *intersection;
490
491     intersection =
492         gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
493     gst_caps_unref (ret);
494     ret = intersection;
495   }
496
497   GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
498
499   return ret;
500 }
501
502 static gboolean
503 gst_video_scale_transform_meta (GstBaseTransform * trans, GstBuffer * inbuf,
504     GstMeta * meta, GstBuffer * outbuf)
505 {
506   GstVideoFilter *videofilter = GST_VIDEO_FILTER (trans);
507   const GstMetaInfo *info = meta->info;
508   const gchar *const *tags;
509   const gchar *const *curr = NULL;
510   gboolean should_copy = TRUE;
511   const gchar *const valid_tags[] = { GST_META_TAG_VIDEO_STR,
512     GST_META_TAG_VIDEO_COLORSPACE_STR,
513     GST_META_TAG_VIDEO_ORIENTATION_STR,
514     GST_META_TAG_VIDEO_SIZE_STR
515   };
516
517   tags = gst_meta_api_type_get_tags (info->api);
518
519   /* No specific tags, we are good to copy */
520   if (!tags) {
521     return TRUE;
522   }
523
524   /* We are only changing size, we can preserve other metas tagged as
525      orientation and colorspace */
526   for (curr = tags; *curr; ++curr) {
527
528     /* We dont handle any other tag */
529     if (!g_strv_contains (valid_tags, *curr)) {
530       should_copy = FALSE;
531       break;
532     }
533   }
534
535   /* Cant handle the tags in this meta, let the parent class handle it */
536   if (!should_copy) {
537     return GST_BASE_TRANSFORM_CLASS (parent_class)->transform_meta (trans,
538         inbuf, meta, outbuf);
539   }
540
541   /* This meta is size sensitive, try to transform it accordingly */
542   if (gst_meta_api_type_has_tag (info->api, _size_quark)) {
543     GstVideoMetaTransform trans =
544         { &videofilter->in_info, &videofilter->out_info };
545
546     return info->transform_func (outbuf, meta, inbuf, _scale_quark, &trans);
547   }
548
549   /* No need to transform, we can safely copy this meta */
550   return TRUE;
551 }
552
553 static gboolean
554 gst_video_scale_set_info (GstVideoFilter * filter, GstCaps * in,
555     GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
556 {
557   GstVideoScale *videoscale = GST_VIDEO_SCALE (filter);
558   gint from_dar_n, from_dar_d, to_dar_n, to_dar_d;
559
560   if (!gst_util_fraction_multiply (in_info->width,
561           in_info->height, in_info->par_n, in_info->par_d, &from_dar_n,
562           &from_dar_d)) {
563     from_dar_n = from_dar_d = -1;
564   }
565
566   if (!gst_util_fraction_multiply (out_info->width,
567           out_info->height, out_info->par_n, out_info->par_d, &to_dar_n,
568           &to_dar_d)) {
569     to_dar_n = to_dar_d = -1;
570   }
571
572   videoscale->borders_w = videoscale->borders_h = 0;
573   if (to_dar_n != from_dar_n || to_dar_d != from_dar_d) {
574     if (videoscale->add_borders) {
575       gint n, d, to_h, to_w;
576
577       if (from_dar_n != -1 && from_dar_d != -1
578           && gst_util_fraction_multiply (from_dar_n, from_dar_d,
579               out_info->par_d, out_info->par_n, &n, &d)) {
580         to_h = gst_util_uint64_scale_int (out_info->width, d, n);
581         if (to_h <= out_info->height) {
582           videoscale->borders_h = out_info->height - to_h;
583           videoscale->borders_w = 0;
584         } else {
585           to_w = gst_util_uint64_scale_int (out_info->height, n, d);
586           g_assert (to_w <= out_info->width);
587           videoscale->borders_h = 0;
588           videoscale->borders_w = out_info->width - to_w;
589         }
590       } else {
591         GST_WARNING_OBJECT (videoscale, "Can't calculate borders");
592       }
593     } else {
594       GST_WARNING_OBJECT (videoscale, "Can't keep DAR!");
595     }
596   }
597
598   if (in_info->width == out_info->width && in_info->height == out_info->height
599       && videoscale->borders_w == 0 && videoscale->borders_h == 0) {
600     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), TRUE);
601   } else {
602     GstStructure *options;
603     GST_CAT_DEBUG_OBJECT (CAT_PERFORMANCE, filter, "setup videoscaling");
604     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), FALSE);
605
606     options = gst_structure_new_empty ("videoscale");
607
608     switch (videoscale->method) {
609       case GST_VIDEO_SCALE_NEAREST:
610         gst_structure_set (options,
611             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
612             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_NEAREST,
613             NULL);
614         break;
615       case GST_VIDEO_SCALE_BILINEAR:
616         gst_structure_set (options,
617             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
618             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_LINEAR,
619             GST_VIDEO_RESAMPLER_OPT_MAX_TAPS, G_TYPE_INT, 2, NULL);
620         break;
621       case GST_VIDEO_SCALE_4TAP:
622         gst_structure_set (options,
623             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
624             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_SINC,
625             GST_VIDEO_RESAMPLER_OPT_MAX_TAPS, G_TYPE_INT, 4, NULL);
626         break;
627       case GST_VIDEO_SCALE_LANCZOS:
628         gst_structure_set (options,
629             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
630             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_LANCZOS,
631             NULL);
632         break;
633       case GST_VIDEO_SCALE_BILINEAR2:
634         gst_structure_set (options,
635             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
636             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_LINEAR,
637             NULL);
638         break;
639       case GST_VIDEO_SCALE_SINC:
640         gst_structure_set (options,
641             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
642             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_SINC,
643             NULL);
644         break;
645       case GST_VIDEO_SCALE_HERMITE:
646         gst_structure_set (options,
647             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
648             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_CUBIC,
649             GST_VIDEO_RESAMPLER_OPT_CUBIC_B, G_TYPE_DOUBLE, (gdouble) 0.0,
650             GST_VIDEO_RESAMPLER_OPT_CUBIC_C, G_TYPE_DOUBLE, (gdouble) 0.0,
651             NULL);
652         break;
653       case GST_VIDEO_SCALE_SPLINE:
654         gst_structure_set (options,
655             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
656             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_CUBIC,
657             GST_VIDEO_RESAMPLER_OPT_CUBIC_B, G_TYPE_DOUBLE, (gdouble) 1.0,
658             GST_VIDEO_RESAMPLER_OPT_CUBIC_C, G_TYPE_DOUBLE, (gdouble) 0.0,
659             NULL);
660         break;
661       case GST_VIDEO_SCALE_CATROM:
662         gst_structure_set (options,
663             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
664             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_CUBIC,
665             GST_VIDEO_RESAMPLER_OPT_CUBIC_B, G_TYPE_DOUBLE, (gdouble) 0.0,
666             GST_VIDEO_RESAMPLER_OPT_CUBIC_C, G_TYPE_DOUBLE, (gdouble) 0.5,
667             NULL);
668         break;
669       case GST_VIDEO_SCALE_MITCHELL:
670         gst_structure_set (options,
671             GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
672             GST_TYPE_VIDEO_RESAMPLER_METHOD, GST_VIDEO_RESAMPLER_METHOD_CUBIC,
673             GST_VIDEO_RESAMPLER_OPT_CUBIC_B, G_TYPE_DOUBLE, (gdouble) 1.0 / 3.0,
674             GST_VIDEO_RESAMPLER_OPT_CUBIC_C, G_TYPE_DOUBLE, (gdouble) 1.0 / 3.0,
675             NULL);
676         break;
677     }
678     gst_structure_set (options,
679         GST_VIDEO_RESAMPLER_OPT_ENVELOPE, G_TYPE_DOUBLE, videoscale->envelope,
680         GST_VIDEO_RESAMPLER_OPT_SHARPNESS, G_TYPE_DOUBLE, videoscale->sharpness,
681         GST_VIDEO_RESAMPLER_OPT_SHARPEN, G_TYPE_DOUBLE, videoscale->sharpen,
682         GST_VIDEO_CONVERTER_OPT_DEST_X, G_TYPE_INT, videoscale->borders_w / 2,
683         GST_VIDEO_CONVERTER_OPT_DEST_Y, G_TYPE_INT, videoscale->borders_h / 2,
684         GST_VIDEO_CONVERTER_OPT_DEST_WIDTH, G_TYPE_INT,
685         out_info->width - videoscale->borders_w,
686         GST_VIDEO_CONVERTER_OPT_DEST_HEIGHT, G_TYPE_INT,
687         out_info->height - videoscale->borders_h,
688         GST_VIDEO_CONVERTER_OPT_MATRIX_MODE, GST_TYPE_VIDEO_MATRIX_MODE,
689         GST_VIDEO_MATRIX_MODE_NONE, GST_VIDEO_CONVERTER_OPT_DITHER_METHOD,
690         GST_TYPE_VIDEO_DITHER_METHOD, GST_VIDEO_DITHER_NONE,
691         GST_VIDEO_CONVERTER_OPT_CHROMA_MODE, GST_TYPE_VIDEO_CHROMA_MODE,
692         GST_VIDEO_CHROMA_MODE_NONE,
693         GST_VIDEO_CONVERTER_OPT_THREADS, G_TYPE_UINT, videoscale->n_threads,
694         NULL);
695
696     if (videoscale->gamma_decode) {
697       gst_structure_set (options,
698           GST_VIDEO_CONVERTER_OPT_GAMMA_MODE, GST_TYPE_VIDEO_GAMMA_MODE,
699           GST_VIDEO_GAMMA_MODE_REMAP, NULL);
700     }
701
702     if (videoscale->convert)
703       gst_video_converter_free (videoscale->convert);
704     videoscale->convert = gst_video_converter_new (in_info, out_info, options);
705   }
706
707   GST_DEBUG_OBJECT (videoscale, "from=%dx%d (par=%d/%d dar=%d/%d), size %"
708       G_GSIZE_FORMAT " -> to=%dx%d (par=%d/%d dar=%d/%d borders=%d:%d), "
709       "size %" G_GSIZE_FORMAT,
710       in_info->width, in_info->height, in_info->par_n, in_info->par_d,
711       from_dar_n, from_dar_d, in_info->size, out_info->width,
712       out_info->height, out_info->par_n, out_info->par_d, to_dar_n, to_dar_d,
713       videoscale->borders_w, videoscale->borders_h, out_info->size);
714
715   return TRUE;
716 }
717
718 static GstCaps *
719 gst_video_scale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
720     GstCaps * caps, GstCaps * othercaps)
721 {
722   GstStructure *ins, *outs;
723   const GValue *from_par, *to_par;
724   GValue fpar = { 0, };
725   GValue tpar = { 0, };
726
727   othercaps = gst_caps_truncate (othercaps);
728   othercaps = gst_caps_make_writable (othercaps);
729
730   GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
731       " based on caps %" GST_PTR_FORMAT, othercaps, caps);
732
733   ins = gst_caps_get_structure (caps, 0);
734   outs = gst_caps_get_structure (othercaps, 0);
735
736   from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
737   to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
738
739   /* If we're fixating from the sinkpad we always set the PAR and
740    * assume that missing PAR on the sinkpad means 1/1 and
741    * missing PAR on the srcpad means undefined
742    */
743   if (direction == GST_PAD_SINK) {
744     if (!from_par) {
745       g_value_init (&fpar, GST_TYPE_FRACTION);
746       gst_value_set_fraction (&fpar, 1, 1);
747       from_par = &fpar;
748     }
749     if (!to_par) {
750       g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
751       gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
752       to_par = &tpar;
753     }
754   } else {
755     if (!to_par) {
756       g_value_init (&tpar, GST_TYPE_FRACTION);
757       gst_value_set_fraction (&tpar, 1, 1);
758       to_par = &tpar;
759
760       gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
761           NULL);
762     }
763     if (!from_par) {
764       g_value_init (&fpar, GST_TYPE_FRACTION);
765       gst_value_set_fraction (&fpar, 1, 1);
766       from_par = &fpar;
767     }
768   }
769
770   /* we have both PAR but they might not be fixated */
771   {
772     gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
773     gint w = 0, h = 0;
774     gint from_dar_n, from_dar_d;
775     gint num, den;
776
777     /* from_par should be fixed */
778     g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
779
780     from_par_n = gst_value_get_fraction_numerator (from_par);
781     from_par_d = gst_value_get_fraction_denominator (from_par);
782
783     gst_structure_get_int (ins, "width", &from_w);
784     gst_structure_get_int (ins, "height", &from_h);
785
786     gst_structure_get_int (outs, "width", &w);
787     gst_structure_get_int (outs, "height", &h);
788
789     /* if both width and height are already fixed, we can't do anything
790      * about it anymore */
791     if (w && h) {
792       guint n, d;
793
794       GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
795           w, h);
796       if (!gst_value_is_fixed (to_par)) {
797         if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
798                 from_par_n, from_par_d, w, h)) {
799           GST_DEBUG_OBJECT (base, "fixating to_par to %dx%d", n, d);
800           if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
801             gst_structure_fixate_field_nearest_fraction (outs,
802                 "pixel-aspect-ratio", n, d);
803           else if (n != d)
804             gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
805                 n, d, NULL);
806         }
807       }
808       goto done;
809     }
810
811     /* Calculate input DAR */
812     if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
813             &from_dar_n, &from_dar_d)) {
814       GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
815           ("Error calculating the output scaled size - integer overflow"));
816       goto done;
817     }
818
819     GST_DEBUG_OBJECT (base, "Input DAR is %d/%d", from_dar_n, from_dar_d);
820
821     /* If either width or height are fixed there's not much we
822      * can do either except choosing a height or width and PAR
823      * that matches the DAR as good as possible
824      */
825     if (h) {
826       GstStructure *tmp;
827       gint set_w, set_par_n, set_par_d;
828
829       GST_DEBUG_OBJECT (base, "height is fixed (%d)", h);
830
831       /* If the PAR is fixed too, there's not much to do
832        * except choosing the width that is nearest to the
833        * width with the same DAR */
834       if (gst_value_is_fixed (to_par)) {
835         to_par_n = gst_value_get_fraction_numerator (to_par);
836         to_par_d = gst_value_get_fraction_denominator (to_par);
837
838         GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
839
840         if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
841                 to_par_n, &num, &den)) {
842           GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
843               ("Error calculating the output scaled size - integer overflow"));
844           goto done;
845         }
846
847         w = (guint) gst_util_uint64_scale_int_round (h, num, den);
848         gst_structure_fixate_field_nearest_int (outs, "width", w);
849
850         goto done;
851       }
852
853       /* The PAR is not fixed and it's quite likely that we can set
854        * an arbitrary PAR. */
855
856       /* Check if we can keep the input width */
857       tmp = gst_structure_copy (outs);
858       gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
859       gst_structure_get_int (tmp, "width", &set_w);
860
861       /* Might have failed but try to keep the DAR nonetheless by
862        * adjusting the PAR */
863       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
864               &to_par_n, &to_par_d)) {
865         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
866             ("Error calculating the output scaled size - integer overflow"));
867         gst_structure_free (tmp);
868         goto done;
869       }
870
871       if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
872         gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
873       gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
874           to_par_n, to_par_d);
875       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
876           &set_par_d);
877       gst_structure_free (tmp);
878
879       /* Check if the adjusted PAR is accepted */
880       if (set_par_n == to_par_n && set_par_d == to_par_d) {
881         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
882             set_par_n != set_par_d)
883           gst_structure_set (outs, "width", G_TYPE_INT, set_w,
884               "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
885               NULL);
886         goto done;
887       }
888
889       /* Otherwise scale the width to the new PAR and check if the
890        * adjusted with is accepted. If all that fails we can't keep
891        * the DAR */
892       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
893               set_par_n, &num, &den)) {
894         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
895             ("Error calculating the output scaled size - integer overflow"));
896         goto done;
897       }
898
899       w = (guint) gst_util_uint64_scale_int_round (h, num, den);
900       gst_structure_fixate_field_nearest_int (outs, "width", w);
901       if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
902           set_par_n != set_par_d)
903         gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
904             set_par_n, set_par_d, NULL);
905
906       goto done;
907     } else if (w) {
908       GstStructure *tmp;
909       gint set_h, set_par_n, set_par_d;
910
911       GST_DEBUG_OBJECT (base, "width is fixed (%d)", w);
912
913       /* If the PAR is fixed too, there's not much to do
914        * except choosing the height that is nearest to the
915        * height with the same DAR */
916       if (gst_value_is_fixed (to_par)) {
917         to_par_n = gst_value_get_fraction_numerator (to_par);
918         to_par_d = gst_value_get_fraction_denominator (to_par);
919
920         GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
921
922         if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
923                 to_par_n, &num, &den)) {
924           GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
925               ("Error calculating the output scaled size - integer overflow"));
926           goto done;
927         }
928
929         h = (guint) gst_util_uint64_scale_int_round (w, den, num);
930         gst_structure_fixate_field_nearest_int (outs, "height", h);
931
932         goto done;
933       }
934
935       /* The PAR is not fixed and it's quite likely that we can set
936        * an arbitrary PAR. */
937
938       /* Check if we can keep the input height */
939       tmp = gst_structure_copy (outs);
940       gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
941       gst_structure_get_int (tmp, "height", &set_h);
942
943       /* Might have failed but try to keep the DAR nonetheless by
944        * adjusting the PAR */
945       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
946               &to_par_n, &to_par_d)) {
947         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
948             ("Error calculating the output scaled size - integer overflow"));
949         gst_structure_free (tmp);
950         goto done;
951       }
952       if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
953         gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
954       gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
955           to_par_n, to_par_d);
956       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
957           &set_par_d);
958       gst_structure_free (tmp);
959
960       /* Check if the adjusted PAR is accepted */
961       if (set_par_n == to_par_n && set_par_d == to_par_d) {
962         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
963             set_par_n != set_par_d)
964           gst_structure_set (outs, "height", G_TYPE_INT, set_h,
965               "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
966               NULL);
967         goto done;
968       }
969
970       /* Otherwise scale the height to the new PAR and check if the
971        * adjusted with is accepted. If all that fails we can't keep
972        * the DAR */
973       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
974               set_par_n, &num, &den)) {
975         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
976             ("Error calculating the output scaled size - integer overflow"));
977         goto done;
978       }
979
980       h = (guint) gst_util_uint64_scale_int_round (w, den, num);
981       gst_structure_fixate_field_nearest_int (outs, "height", h);
982       if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
983           set_par_n != set_par_d)
984         gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
985             set_par_n, set_par_d, NULL);
986
987       goto done;
988     } else if (gst_value_is_fixed (to_par)) {
989       GstStructure *tmp;
990       gint set_h, set_w, f_h, f_w;
991
992       to_par_n = gst_value_get_fraction_numerator (to_par);
993       to_par_d = gst_value_get_fraction_denominator (to_par);
994
995       /* Calculate scale factor for the PAR change */
996       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
997               to_par_d, &num, &den)) {
998         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
999             ("Error calculating the output scaled size - integer overflow"));
1000         goto done;
1001       }
1002
1003       /* Try to keep the input height (because of interlacing) */
1004       tmp = gst_structure_copy (outs);
1005       gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
1006       gst_structure_get_int (tmp, "height", &set_h);
1007
1008       /* This might have failed but try to scale the width
1009        * to keep the DAR nonetheless */
1010       w = (guint) gst_util_uint64_scale_int_round (set_h, num, den);
1011       gst_structure_fixate_field_nearest_int (tmp, "width", w);
1012       gst_structure_get_int (tmp, "width", &set_w);
1013       gst_structure_free (tmp);
1014
1015       /* We kept the DAR and the height is nearest to the original height */
1016       if (set_w == w) {
1017         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1018             G_TYPE_INT, set_h, NULL);
1019         goto done;
1020       }
1021
1022       f_h = set_h;
1023       f_w = set_w;
1024
1025       /* If the former failed, try to keep the input width at least */
1026       tmp = gst_structure_copy (outs);
1027       gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
1028       gst_structure_get_int (tmp, "width", &set_w);
1029
1030       /* This might have failed but try to scale the width
1031        * to keep the DAR nonetheless */
1032       h = (guint) gst_util_uint64_scale_int_round (set_w, den, num);
1033       gst_structure_fixate_field_nearest_int (tmp, "height", h);
1034       gst_structure_get_int (tmp, "height", &set_h);
1035       gst_structure_free (tmp);
1036
1037       /* We kept the DAR and the width is nearest to the original width */
1038       if (set_h == h) {
1039         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1040             G_TYPE_INT, set_h, NULL);
1041         goto done;
1042       }
1043
1044       /* If all this failed, keep the dimensions with the DAR that was closest
1045        * to the correct DAR. This changes the DAR but there's not much else to
1046        * do here.
1047        */
1048       if (set_w * ABS (set_h - h) < ABS (f_w - w) * f_h) {
1049         f_h = set_h;
1050         f_w = set_w;
1051       }
1052       gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
1053           f_h, NULL);
1054       goto done;
1055     } else {
1056       GstStructure *tmp;
1057       gint set_h, set_w, set_par_n, set_par_d, tmp2;
1058
1059       /* width, height and PAR are not fixed but passthrough is not possible */
1060
1061       /* First try to keep the height and width as good as possible
1062        * and scale PAR */
1063       tmp = gst_structure_copy (outs);
1064       gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
1065       gst_structure_get_int (tmp, "height", &set_h);
1066       gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
1067       gst_structure_get_int (tmp, "width", &set_w);
1068
1069       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
1070               &to_par_n, &to_par_d)) {
1071         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
1072             ("Error calculating the output scaled size - integer overflow"));
1073         gst_structure_free (tmp);
1074         goto done;
1075       }
1076
1077       if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
1078         gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
1079       gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
1080           to_par_n, to_par_d);
1081       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
1082           &set_par_d);
1083       gst_structure_free (tmp);
1084
1085       if (set_par_n == to_par_n && set_par_d == to_par_d) {
1086         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1087             G_TYPE_INT, set_h, NULL);
1088
1089         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1090             set_par_n != set_par_d)
1091           gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1092               set_par_n, set_par_d, NULL);
1093         goto done;
1094       }
1095
1096       /* Otherwise try to scale width to keep the DAR with the set
1097        * PAR and height */
1098       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
1099               set_par_n, &num, &den)) {
1100         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
1101             ("Error calculating the output scaled size - integer overflow"));
1102         goto done;
1103       }
1104
1105       w = (guint) gst_util_uint64_scale_int_round (set_h, num, den);
1106       tmp = gst_structure_copy (outs);
1107       gst_structure_fixate_field_nearest_int (tmp, "width", w);
1108       gst_structure_get_int (tmp, "width", &tmp2);
1109       gst_structure_free (tmp);
1110
1111       if (tmp2 == w) {
1112         gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
1113             G_TYPE_INT, set_h, NULL);
1114         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1115             set_par_n != set_par_d)
1116           gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1117               set_par_n, set_par_d, NULL);
1118         goto done;
1119       }
1120
1121       /* ... or try the same with the height */
1122       h = (guint) gst_util_uint64_scale_int_round (set_w, den, num);
1123       tmp = gst_structure_copy (outs);
1124       gst_structure_fixate_field_nearest_int (tmp, "height", h);
1125       gst_structure_get_int (tmp, "height", &tmp2);
1126       gst_structure_free (tmp);
1127
1128       if (tmp2 == h) {
1129         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1130             G_TYPE_INT, tmp2, NULL);
1131         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1132             set_par_n != set_par_d)
1133           gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1134               set_par_n, set_par_d, NULL);
1135         goto done;
1136       }
1137
1138       /* If all fails we can't keep the DAR and take the nearest values
1139        * for everything from the first try */
1140       gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1141           G_TYPE_INT, set_h, NULL);
1142       if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1143           set_par_n != set_par_d)
1144         gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1145             set_par_n, set_par_d, NULL);
1146     }
1147   }
1148
1149 done:
1150   GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
1151
1152   if (from_par == &fpar)
1153     g_value_unset (&fpar);
1154   if (to_par == &tpar)
1155     g_value_unset (&tpar);
1156
1157   return othercaps;
1158 }
1159
1160 #define GET_LINE(frame, line) \
1161     (gpointer)(((guint8*)(GST_VIDEO_FRAME_PLANE_DATA (frame, 0))) + \
1162      GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0) * (line))
1163
1164 static GstFlowReturn
1165 gst_video_scale_transform_frame (GstVideoFilter * filter,
1166     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
1167 {
1168   GstVideoScale *videoscale = GST_VIDEO_SCALE_CAST (filter);
1169   GstFlowReturn ret = GST_FLOW_OK;
1170
1171   GST_CAT_DEBUG_OBJECT (CAT_PERFORMANCE, filter, "doing video scaling");
1172
1173   gst_video_converter_frame (videoscale->convert, in_frame, out_frame);
1174
1175   return ret;
1176 }
1177
1178 static gboolean
1179 gst_video_scale_src_event (GstBaseTransform * trans, GstEvent * event)
1180 {
1181   GstVideoScale *videoscale = GST_VIDEO_SCALE_CAST (trans);
1182   GstVideoFilter *filter = GST_VIDEO_FILTER_CAST (trans);
1183   gboolean ret;
1184   gdouble a;
1185   GstStructure *structure;
1186
1187   GST_DEBUG_OBJECT (videoscale, "handling %s event",
1188       GST_EVENT_TYPE_NAME (event));
1189
1190   switch (GST_EVENT_TYPE (event)) {
1191     case GST_EVENT_NAVIGATION:
1192       if (filter->in_info.width != filter->out_info.width ||
1193           filter->in_info.height != filter->out_info.height) {
1194         event =
1195             GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
1196
1197         structure = (GstStructure *) gst_event_get_structure (event);
1198         if (gst_structure_get_double (structure, "pointer_x", &a)) {
1199           gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
1200               a * filter->in_info.width / filter->out_info.width, NULL);
1201         }
1202         if (gst_structure_get_double (structure, "pointer_y", &a)) {
1203           gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
1204               a * filter->in_info.height / filter->out_info.height, NULL);
1205         }
1206       }
1207       break;
1208     default:
1209       break;
1210   }
1211
1212   ret = GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
1213
1214   return ret;
1215 }
1216
1217 static gboolean
1218 plugin_init (GstPlugin * plugin)
1219 {
1220   if (!gst_element_register (plugin, "videoscale", GST_RANK_NONE,
1221           GST_TYPE_VIDEO_SCALE))
1222     return FALSE;
1223
1224   GST_DEBUG_CATEGORY_INIT (video_scale_debug, "videoscale", 0,
1225       "videoscale element");
1226   GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
1227
1228   return TRUE;
1229 }
1230
1231 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1232     GST_VERSION_MINOR,
1233     videoscale,
1234     "Resizes video", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
1235     GST_PACKAGE_ORIGIN)