-base: port elements to new video caps
[platform/upstream/gstreamer.git] / gst / videoscale / gstvideoscale.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2005 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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:element-videoscale
23  * @see_also: videorate, videoconvert
24  *
25  * This element resizes video frames. By default the element will try to
26  * negotiate to the same size on the source and sinkpad so that no scaling
27  * is needed. It is therefore safe to insert this element in a pipeline to
28  * get more robust behaviour without any cost if no scaling is needed.
29  *
30  * This element supports a wide range of color spaces including various YUV and
31  * RGB formats and is therefore generally able to operate anywhere in a
32  * pipeline.
33  *
34  * <refsect2>
35  * <title>Example pipelines</title>
36  * |[
37  * gst-launch -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videoconvert ! videoscale ! ximagesink
38  * ]| Decode an Ogg/Theora and display the video using ximagesink. Since
39  * ximagesink cannot perform scaling, the video scaling will be performed by
40  * videoscale when you resize the video window.
41  * To create the test Ogg/Theora file refer to the documentation of theoraenc.
42  * |[
43  * gst-launch -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videoscale ! video/x-raw, width=50 ! xvimagesink
44  * ]| Decode an Ogg/Theora and display the video using xvimagesink with a width
45  * of 50.
46  * </refsect2>
47  *
48  * Last reviewed on 2006-03-02 (0.10.4)
49  */
50
51 /* 
52  * Formulas for PAR, DAR, width and height relations:
53  *
54  * dar_n   w   par_n
55  * ----- = - * -----
56  * dar_d   h   par_d
57  *
58  * par_n    h   dar_n
59  * ----- =  - * -----
60  * par_d    w   dar_d
61  * 
62  *         dar_n   par_d
63  * w = h * ----- * -----
64  *         dar_d   par_n
65  *
66  *         dar_d   par_n
67  * h = w * ----- * -----
68  *         dar_n   par_d
69  */
70
71 #ifdef HAVE_CONFIG_H
72 #include "config.h"
73 #endif
74
75 #include <string.h>
76
77 #include <math.h>
78
79 #include <gst/video/video.h>
80
81 #include "gstvideoscale.h"
82 #include "gstvideoscaleorc.h"
83 #include "vs_image.h"
84 #include "vs_4tap.h"
85 #include "vs_fill_borders.h"
86
87 /* debug variable definition */
88 GST_DEBUG_CATEGORY (video_scale_debug);
89
90 #define DEFAULT_PROP_METHOD       GST_VIDEO_SCALE_BILINEAR
91 #define DEFAULT_PROP_ADD_BORDERS  FALSE
92
93 enum
94 {
95   PROP_0,
96   PROP_METHOD,
97   PROP_ADD_BORDERS
98       /* FILL ME */
99 };
100
101 #undef GST_VIDEO_SIZE_RANGE
102 #define GST_VIDEO_SIZE_RANGE "(int) [ 1, 32767]"
103
104 #define GST_VIDEO_FORMATS "{ \"I420\", \"YV12\", \"YUY2\", \"UYVY\", \"AYUV\", \"RGBx\", " \
105     "\"BGRx\", \"xRGB\", \"xBGR\", \"RGBA\", \"BGRA\", \"ARGB\", \"ABGR\", \"RGB\", " \
106     "\"BGR\", \"Y41B\", \"Y42B\", \"YVYU\", \"Y444\", \"GRAY8\", \"GRAY16_BE\", \"GRAY16_LE\", " \
107     "\"v308\", \"Y800\", \"Y16\", \"RGB16\", \"RGB15\", \"ARGB64\", \"AYUV64\" } "
108
109
110 static GstStaticCaps gst_video_scale_format_caps[] = {
111   GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS))
112 };
113
114 #define GST_TYPE_VIDEO_SCALE_METHOD (gst_video_scale_method_get_type())
115 static GType
116 gst_video_scale_method_get_type (void)
117 {
118   static GType video_scale_method_type = 0;
119
120   static const GEnumValue video_scale_methods[] = {
121     {GST_VIDEO_SCALE_NEAREST, "Nearest Neighbour", "nearest-neighbour"},
122     {GST_VIDEO_SCALE_BILINEAR, "Bilinear", "bilinear"},
123     {GST_VIDEO_SCALE_4TAP, "4-tap", "4-tap"},
124     {0, NULL, NULL},
125   };
126
127   if (!video_scale_method_type) {
128     video_scale_method_type =
129         g_enum_register_static ("GstVideoScaleMethod", video_scale_methods);
130   }
131   return video_scale_method_type;
132 }
133
134 static GstCaps *
135 gst_video_scale_get_capslist (void)
136 {
137   static GstCaps *caps = NULL;
138   static volatile gsize inited = 0;
139
140   if (g_once_init_enter (&inited)) {
141     gint i;
142
143     g_assert (caps == NULL);
144
145     caps = gst_caps_new_empty ();
146     for (i = 0; i < G_N_ELEMENTS (gst_video_scale_format_caps); i++)
147       gst_caps_append (caps,
148           gst_caps_make_writable
149           (gst_static_caps_get (&gst_video_scale_format_caps[i])));
150     g_once_init_leave (&inited, 1);
151   }
152
153   return caps;
154 }
155
156 static GstPadTemplate *
157 gst_video_scale_src_template_factory (void)
158 {
159   return gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
160       gst_video_scale_get_capslist ());
161 }
162
163 static GstPadTemplate *
164 gst_video_scale_sink_template_factory (void)
165 {
166   return gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
167       gst_video_scale_get_capslist ());
168 }
169
170
171 static void gst_video_scale_finalize (GstVideoScale * videoscale);
172 static gboolean gst_video_scale_src_event (GstBaseTransform * trans,
173     GstEvent * event);
174
175 /* base transform vmethods */
176 static GstCaps *gst_video_scale_transform_caps (GstBaseTransform * trans,
177     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
178 static gboolean gst_video_scale_set_caps (GstBaseTransform * trans,
179     GstCaps * in, GstCaps * out);
180 static gboolean gst_video_scale_get_unit_size (GstBaseTransform * trans,
181     GstCaps * caps, gsize * size);
182 static GstFlowReturn gst_video_scale_transform (GstBaseTransform * trans,
183     GstBuffer * in, GstBuffer * out);
184 static void gst_video_scale_fixate_caps (GstBaseTransform * base,
185     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
186
187 static void gst_video_scale_set_property (GObject * object, guint prop_id,
188     const GValue * value, GParamSpec * pspec);
189 static void gst_video_scale_get_property (GObject * object, guint prop_id,
190     GValue * value, GParamSpec * pspec);
191
192 #define gst_video_scale_parent_class parent_class
193 G_DEFINE_TYPE (GstVideoScale, gst_video_scale, GST_TYPE_VIDEO_FILTER);
194
195 static void
196 gst_video_scale_class_init (GstVideoScaleClass * klass)
197 {
198   GObjectClass *gobject_class = (GObjectClass *) klass;
199   GstElementClass *element_class = (GstElementClass *) klass;
200   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
201
202   gobject_class->finalize = (GObjectFinalizeFunc) gst_video_scale_finalize;
203   gobject_class->set_property = gst_video_scale_set_property;
204   gobject_class->get_property = gst_video_scale_get_property;
205
206   g_object_class_install_property (gobject_class, PROP_METHOD,
207       g_param_spec_enum ("method", "method", "method",
208           GST_TYPE_VIDEO_SCALE_METHOD, DEFAULT_PROP_METHOD,
209           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210
211   g_object_class_install_property (gobject_class, PROP_ADD_BORDERS,
212       g_param_spec_boolean ("add-borders", "Add Borders",
213           "Add black borders if necessary to keep the display aspect ratio",
214           DEFAULT_PROP_ADD_BORDERS,
215           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216
217   gst_element_class_set_details_simple (element_class,
218       "Video scaler", "Filter/Converter/Video/Scaler",
219       "Resizes video", "Wim Taymans <wim.taymans@chello.be>");
220
221   gst_element_class_add_pad_template (element_class,
222       gst_video_scale_sink_template_factory ());
223   gst_element_class_add_pad_template (element_class,
224       gst_video_scale_src_template_factory ());
225
226   trans_class->transform_caps =
227       GST_DEBUG_FUNCPTR (gst_video_scale_transform_caps);
228   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_scale_set_caps);
229   trans_class->get_unit_size =
230       GST_DEBUG_FUNCPTR (gst_video_scale_get_unit_size);
231   trans_class->transform = GST_DEBUG_FUNCPTR (gst_video_scale_transform);
232   trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_video_scale_fixate_caps);
233   trans_class->src_event = GST_DEBUG_FUNCPTR (gst_video_scale_src_event);
234 }
235
236 static void
237 gst_video_scale_init (GstVideoScale * videoscale)
238 {
239   videoscale->tmp_buf = NULL;
240   videoscale->method = DEFAULT_PROP_METHOD;
241   videoscale->add_borders = DEFAULT_PROP_ADD_BORDERS;
242 }
243
244 static void
245 gst_video_scale_finalize (GstVideoScale * videoscale)
246 {
247   if (videoscale->tmp_buf)
248     g_free (videoscale->tmp_buf);
249
250   G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (videoscale));
251 }
252
253 static void
254 gst_video_scale_set_property (GObject * object, guint prop_id,
255     const GValue * value, GParamSpec * pspec)
256 {
257   GstVideoScale *vscale = GST_VIDEO_SCALE (object);
258
259   switch (prop_id) {
260     case PROP_METHOD:
261       GST_OBJECT_LOCK (vscale);
262       vscale->method = g_value_get_enum (value);
263       GST_OBJECT_UNLOCK (vscale);
264       break;
265     case PROP_ADD_BORDERS:
266       GST_OBJECT_LOCK (vscale);
267       vscale->add_borders = g_value_get_boolean (value);
268       GST_OBJECT_UNLOCK (vscale);
269       gst_base_transform_reconfigure (GST_BASE_TRANSFORM_CAST (vscale));
270       break;
271     default:
272       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273       break;
274   }
275 }
276
277 static void
278 gst_video_scale_get_property (GObject * object, guint prop_id, GValue * value,
279     GParamSpec * pspec)
280 {
281   GstVideoScale *vscale = GST_VIDEO_SCALE (object);
282
283   switch (prop_id) {
284     case PROP_METHOD:
285       GST_OBJECT_LOCK (vscale);
286       g_value_set_enum (value, vscale->method);
287       GST_OBJECT_UNLOCK (vscale);
288       break;
289     case PROP_ADD_BORDERS:
290       GST_OBJECT_LOCK (vscale);
291       g_value_set_boolean (value, vscale->add_borders);
292       GST_OBJECT_UNLOCK (vscale);
293       break;
294     default:
295       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296       break;
297   }
298 }
299
300 static GstCaps *
301 gst_video_scale_transform_caps (GstBaseTransform * trans,
302     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
303 {
304   GstCaps *ret;
305   GstStructure *structure;
306   gint i, n;
307
308   GST_DEBUG_OBJECT (trans,
309       "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps,
310       (direction == GST_PAD_SINK) ? "sink" : "src");
311
312   ret = gst_caps_new_empty ();
313   n = gst_caps_get_size (caps);
314   for (i = 0; i < n; i++) {
315     structure = gst_caps_get_structure (caps, i);
316
317     /* If this is already expressed by the existing caps
318      * skip this structure */
319     if (i > 0 && gst_caps_is_subset_structure (ret, structure))
320       continue;
321
322     structure = gst_structure_copy (structure);
323     gst_structure_set (structure,
324         "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
325         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
326
327     /* if pixel aspect ratio, make a range of it */
328     if (gst_structure_has_field (structure, "pixel-aspect-ratio")) {
329       gst_structure_set (structure, "pixel-aspect-ratio",
330           GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1, NULL);
331     }
332     gst_caps_append_structure (ret, structure);
333   }
334
335   if (filter) {
336     GstCaps *intersection;
337
338     intersection =
339         gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
340     gst_caps_unref (ret);
341     ret = intersection;
342   }
343
344   GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
345
346   return ret;
347 }
348
349 static gboolean
350 gst_video_scale_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
351 {
352   GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
353   gboolean ret;
354   gint from_dar_n, from_dar_d, to_dar_n, to_dar_d;
355   gint from_par_n, from_par_d, to_par_n, to_par_d;
356
357   ret =
358       gst_video_format_parse_caps (in, &videoscale->format,
359       &videoscale->from_width, &videoscale->from_height);
360   ret &=
361       gst_video_format_parse_caps (out, NULL, &videoscale->to_width,
362       &videoscale->to_height);
363   if (!ret)
364     goto done;
365
366   videoscale->src_size = gst_video_format_get_size (videoscale->format,
367       videoscale->from_width, videoscale->from_height);
368   videoscale->dest_size = gst_video_format_get_size (videoscale->format,
369       videoscale->to_width, videoscale->to_height);
370
371   if (!gst_video_parse_caps_pixel_aspect_ratio (in, &from_par_n, &from_par_d))
372     from_par_n = from_par_d = 1;
373   if (!gst_video_parse_caps_pixel_aspect_ratio (out, &to_par_n, &to_par_d))
374     to_par_n = to_par_d = 1;
375
376   if (!gst_util_fraction_multiply (videoscale->from_width,
377           videoscale->from_height, from_par_n, from_par_d, &from_dar_n,
378           &from_dar_d)) {
379     from_dar_n = from_dar_d = -1;
380   }
381
382   if (!gst_util_fraction_multiply (videoscale->to_width,
383           videoscale->to_height, to_par_n, to_par_d, &to_dar_n, &to_dar_d)) {
384     to_dar_n = to_dar_d = -1;
385   }
386
387   videoscale->borders_w = videoscale->borders_h = 0;
388   if (to_dar_n != from_dar_n || to_dar_d != from_dar_d) {
389     if (videoscale->add_borders) {
390       gint n, d, to_h, to_w;
391
392       if (from_dar_n != -1 && from_dar_d != -1
393           && gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
394               to_par_d, &n, &d)) {
395         to_h = gst_util_uint64_scale_int (videoscale->to_width, d, n);
396         if (to_h <= videoscale->to_height) {
397           videoscale->borders_h = videoscale->to_height - to_h;
398           videoscale->borders_w = 0;
399         } else {
400           to_w = gst_util_uint64_scale_int (videoscale->to_height, n, d);
401           g_assert (to_w <= videoscale->to_width);
402           videoscale->borders_h = 0;
403           videoscale->borders_w = videoscale->to_width - to_w;
404         }
405       } else {
406         GST_WARNING_OBJECT (videoscale, "Can't calculate borders");
407       }
408     } else {
409       GST_WARNING_OBJECT (videoscale, "Can't keep DAR!");
410     }
411   }
412
413   if (videoscale->tmp_buf)
414     g_free (videoscale->tmp_buf);
415   videoscale->tmp_buf = g_malloc (videoscale->to_width * 8 * 4);
416
417   gst_base_transform_set_passthrough (trans,
418       (videoscale->from_width == videoscale->to_width
419           && videoscale->from_height == videoscale->to_height));
420
421   GST_DEBUG_OBJECT (videoscale, "from=%dx%d (par=%d/%d dar=%d/%d), size %d "
422       "-> to=%dx%d (par=%d/%d dar=%d/%d borders=%d:%d), size %d",
423       videoscale->from_width, videoscale->from_height, from_par_n, from_par_d,
424       from_dar_n, from_dar_d, videoscale->src_size, videoscale->to_width,
425       videoscale->to_height, to_par_n, to_par_d, to_dar_n, to_dar_d,
426       videoscale->borders_w, videoscale->borders_h, videoscale->dest_size);
427
428 done:
429   return ret;
430 }
431
432 static gboolean
433 gst_video_scale_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
434     gsize * size)
435 {
436   GstVideoFormat format;
437   gint width, height;
438
439   if (!gst_video_format_parse_caps (caps, &format, &width, &height))
440     return FALSE;
441
442   *size = gst_video_format_get_size (format, width, height);
443
444   return TRUE;
445 }
446
447 static void
448 gst_video_scale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
449     GstCaps * caps, GstCaps * othercaps)
450 {
451   GstStructure *ins, *outs;
452   const GValue *from_par, *to_par;
453   GValue fpar = { 0, }, tpar = {
454   0,};
455
456   g_return_if_fail (gst_caps_is_fixed (caps));
457
458   GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
459       " based on caps %" GST_PTR_FORMAT, othercaps, caps);
460
461   ins = gst_caps_get_structure (caps, 0);
462   outs = gst_caps_get_structure (othercaps, 0);
463
464   from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
465   to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
466
467   /* If we're fixating from the sinkpad we always set the PAR and
468    * assume that missing PAR on the sinkpad means 1/1 and
469    * missing PAR on the srcpad means undefined
470    */
471   if (direction == GST_PAD_SINK) {
472     if (!from_par) {
473       g_value_init (&fpar, GST_TYPE_FRACTION);
474       gst_value_set_fraction (&fpar, 1, 1);
475       from_par = &fpar;
476     }
477     if (!to_par) {
478       g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
479       gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
480       to_par = &tpar;
481     }
482   } else {
483     if (!to_par) {
484       g_value_init (&tpar, GST_TYPE_FRACTION);
485       gst_value_set_fraction (&tpar, 1, 1);
486       to_par = &tpar;
487
488       gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
489           NULL);
490     }
491     if (!from_par) {
492       g_value_init (&fpar, GST_TYPE_FRACTION);
493       gst_value_set_fraction (&fpar, 1, 1);
494       from_par = &fpar;
495     }
496   }
497
498   /* we have both PAR but they might not be fixated */
499   {
500     gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
501     gint w = 0, h = 0;
502     gint from_dar_n, from_dar_d;
503     gint num, den;
504
505     /* from_par should be fixed */
506     g_return_if_fail (gst_value_is_fixed (from_par));
507
508     from_par_n = gst_value_get_fraction_numerator (from_par);
509     from_par_d = gst_value_get_fraction_denominator (from_par);
510
511     gst_structure_get_int (ins, "width", &from_w);
512     gst_structure_get_int (ins, "height", &from_h);
513
514     gst_structure_get_int (outs, "width", &w);
515     gst_structure_get_int (outs, "height", &h);
516
517     /* if both width and height are already fixed, we can't do anything
518      * about it anymore */
519     if (w && h) {
520       guint n, d;
521
522       GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
523           w, h);
524       if (!gst_value_is_fixed (to_par)) {
525         if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
526                 from_par_n, from_par_d, w, h)) {
527           GST_DEBUG_OBJECT (base, "fixating to_par to %dx%d", n, d);
528           if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
529             gst_structure_fixate_field_nearest_fraction (outs,
530                 "pixel-aspect-ratio", n, d);
531           else if (n != d)
532             gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
533                 n, d, NULL);
534         }
535       }
536       goto done;
537     }
538
539     /* Calculate input DAR */
540     if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
541             &from_dar_n, &from_dar_d)) {
542       GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
543           ("Error calculating the output scaled size - integer overflow"));
544       goto done;
545     }
546
547     GST_DEBUG_OBJECT (base, "Input DAR is %d/%d", from_dar_n, from_dar_d);
548
549     /* If either width or height are fixed there's not much we
550      * can do either except choosing a height or width and PAR
551      * that matches the DAR as good as possible
552      */
553     if (h) {
554       GstStructure *tmp;
555       gint set_w, set_par_n, set_par_d;
556
557       GST_DEBUG_OBJECT (base, "height is fixed (%d)", h);
558
559       /* If the PAR is fixed too, there's not much to do
560        * except choosing the width that is nearest to the
561        * width with the same DAR */
562       if (gst_value_is_fixed (to_par)) {
563         to_par_n = gst_value_get_fraction_numerator (to_par);
564         to_par_d = gst_value_get_fraction_denominator (to_par);
565
566         GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
567
568         if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
569                 to_par_n, &num, &den)) {
570           GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
571               ("Error calculating the output scaled size - integer overflow"));
572           goto done;
573         }
574
575         w = (guint) gst_util_uint64_scale_int (h, num, den);
576         gst_structure_fixate_field_nearest_int (outs, "width", w);
577
578         goto done;
579       }
580
581       /* The PAR is not fixed and it's quite likely that we can set
582        * an arbitrary PAR. */
583
584       /* Check if we can keep the input width */
585       tmp = gst_structure_copy (outs);
586       gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
587       gst_structure_get_int (tmp, "width", &set_w);
588
589       /* Might have failed but try to keep the DAR nonetheless by
590        * adjusting the PAR */
591       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
592               &to_par_n, &to_par_d)) {
593         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
594             ("Error calculating the output scaled size - integer overflow"));
595         gst_structure_free (tmp);
596         goto done;
597       }
598
599       if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
600         gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
601       gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
602           to_par_n, to_par_d);
603       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
604           &set_par_d);
605       gst_structure_free (tmp);
606
607       /* Check if the adjusted PAR is accepted */
608       if (set_par_n == to_par_n && set_par_d == to_par_d) {
609         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
610             set_par_n != set_par_d)
611           gst_structure_set (outs, "width", G_TYPE_INT, set_w,
612               "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
613               NULL);
614         goto done;
615       }
616
617       /* Otherwise scale the width to the new PAR and check if the
618        * adjusted with is accepted. If all that fails we can't keep
619        * the DAR */
620       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
621               set_par_n, &num, &den)) {
622         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
623             ("Error calculating the output scaled size - integer overflow"));
624         goto done;
625       }
626
627       w = (guint) gst_util_uint64_scale_int (h, num, den);
628       gst_structure_fixate_field_nearest_int (outs, "width", w);
629       if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
630           set_par_n != set_par_d)
631         gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
632             set_par_n, set_par_d, NULL);
633
634       goto done;
635     } else if (w) {
636       GstStructure *tmp;
637       gint set_h, set_par_n, set_par_d;
638
639       GST_DEBUG_OBJECT (base, "width is fixed (%d)", w);
640
641       /* If the PAR is fixed too, there's not much to do
642        * except choosing the height that is nearest to the
643        * height with the same DAR */
644       if (gst_value_is_fixed (to_par)) {
645         to_par_n = gst_value_get_fraction_numerator (to_par);
646         to_par_d = gst_value_get_fraction_denominator (to_par);
647
648         GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
649
650         if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
651                 to_par_n, &num, &den)) {
652           GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
653               ("Error calculating the output scaled size - integer overflow"));
654           goto done;
655         }
656
657         h = (guint) gst_util_uint64_scale_int (w, den, num);
658         gst_structure_fixate_field_nearest_int (outs, "height", h);
659
660         goto done;
661       }
662
663       /* The PAR is not fixed and it's quite likely that we can set
664        * an arbitrary PAR. */
665
666       /* Check if we can keep the input height */
667       tmp = gst_structure_copy (outs);
668       gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
669       gst_structure_get_int (tmp, "height", &set_h);
670
671       /* Might have failed but try to keep the DAR nonetheless by
672        * adjusting the PAR */
673       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
674               &to_par_n, &to_par_d)) {
675         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
676             ("Error calculating the output scaled size - integer overflow"));
677         gst_structure_free (tmp);
678         goto done;
679       }
680       if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
681         gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
682       gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
683           to_par_n, to_par_d);
684       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
685           &set_par_d);
686       gst_structure_free (tmp);
687
688       /* Check if the adjusted PAR is accepted */
689       if (set_par_n == to_par_n && set_par_d == to_par_d) {
690         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
691             set_par_n != set_par_d)
692           gst_structure_set (outs, "height", G_TYPE_INT, set_h,
693               "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
694               NULL);
695         goto done;
696       }
697
698       /* Otherwise scale the height to the new PAR and check if the
699        * adjusted with is accepted. If all that fails we can't keep
700        * the DAR */
701       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
702               set_par_n, &num, &den)) {
703         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
704             ("Error calculating the output scaled size - integer overflow"));
705         goto done;
706       }
707
708       h = (guint) gst_util_uint64_scale_int (w, den, num);
709       gst_structure_fixate_field_nearest_int (outs, "height", h);
710       if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
711           set_par_n != set_par_d)
712         gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
713             set_par_n, set_par_d, NULL);
714
715       goto done;
716     } else if (gst_value_is_fixed (to_par)) {
717       GstStructure *tmp;
718       gint set_h, set_w, f_h, f_w;
719
720       to_par_n = gst_value_get_fraction_numerator (to_par);
721       to_par_d = gst_value_get_fraction_denominator (to_par);
722
723       /* Calculate scale factor for the PAR change */
724       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
725               to_par_d, &num, &den)) {
726         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
727             ("Error calculating the output scaled size - integer overflow"));
728         goto done;
729       }
730
731       /* Try to keep the input height (because of interlacing) */
732       tmp = gst_structure_copy (outs);
733       gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
734       gst_structure_get_int (tmp, "height", &set_h);
735
736       /* This might have failed but try to scale the width
737        * to keep the DAR nonetheless */
738       w = (guint) gst_util_uint64_scale_int (set_h, num, den);
739       gst_structure_fixate_field_nearest_int (tmp, "width", w);
740       gst_structure_get_int (tmp, "width", &set_w);
741       gst_structure_free (tmp);
742
743       /* We kept the DAR and the height is nearest to the original height */
744       if (set_w == w) {
745         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
746             G_TYPE_INT, set_h, NULL);
747         goto done;
748       }
749
750       f_h = set_h;
751       f_w = set_w;
752
753       /* If the former failed, try to keep the input width at least */
754       tmp = gst_structure_copy (outs);
755       gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
756       gst_structure_get_int (tmp, "width", &set_w);
757
758       /* This might have failed but try to scale the width
759        * to keep the DAR nonetheless */
760       h = (guint) gst_util_uint64_scale_int (set_w, den, num);
761       gst_structure_fixate_field_nearest_int (tmp, "height", h);
762       gst_structure_get_int (tmp, "height", &set_h);
763       gst_structure_free (tmp);
764
765       /* We kept the DAR and the width is nearest to the original width */
766       if (set_h == h) {
767         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
768             G_TYPE_INT, set_h, NULL);
769         goto done;
770       }
771
772       /* If all this failed, keep the height that was nearest to the orignal
773        * height and the nearest possible width. This changes the DAR but
774        * there's not much else to do here.
775        */
776       gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
777           f_h, NULL);
778       goto done;
779     } else {
780       GstStructure *tmp;
781       gint set_h, set_w, set_par_n, set_par_d, tmp2;
782
783       /* width, height and PAR are not fixed but passthrough is not possible */
784
785       /* First try to keep the height and width as good as possible
786        * and scale PAR */
787       tmp = gst_structure_copy (outs);
788       gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
789       gst_structure_get_int (tmp, "height", &set_h);
790       gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
791       gst_structure_get_int (tmp, "width", &set_w);
792
793       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
794               &to_par_n, &to_par_d)) {
795         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
796             ("Error calculating the output scaled size - integer overflow"));
797         goto done;
798       }
799
800       if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
801         gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
802       gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
803           to_par_n, to_par_d);
804       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
805           &set_par_d);
806       gst_structure_free (tmp);
807
808       if (set_par_n == to_par_n && set_par_d == to_par_d) {
809         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
810             G_TYPE_INT, set_h, NULL);
811
812         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
813             set_par_n != set_par_d)
814           gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
815               set_par_n, set_par_d, NULL);
816         goto done;
817       }
818
819       /* Otherwise try to scale width to keep the DAR with the set
820        * PAR and height */
821       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
822               set_par_n, &num, &den)) {
823         GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
824             ("Error calculating the output scaled size - integer overflow"));
825         goto done;
826       }
827
828       w = (guint) gst_util_uint64_scale_int (set_h, num, den);
829       tmp = gst_structure_copy (outs);
830       gst_structure_fixate_field_nearest_int (tmp, "width", w);
831       gst_structure_get_int (tmp, "width", &tmp2);
832       gst_structure_free (tmp);
833
834       if (tmp2 == w) {
835         gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
836             G_TYPE_INT, set_h, NULL);
837         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
838             set_par_n != set_par_d)
839           gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
840               set_par_n, set_par_d, NULL);
841         goto done;
842       }
843
844       /* ... or try the same with the height */
845       h = (guint) gst_util_uint64_scale_int (set_w, den, num);
846       tmp = gst_structure_copy (outs);
847       gst_structure_fixate_field_nearest_int (tmp, "height", h);
848       gst_structure_get_int (tmp, "height", &tmp2);
849       gst_structure_free (tmp);
850
851       if (tmp2 == h) {
852         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
853             G_TYPE_INT, tmp2, NULL);
854         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
855             set_par_n != set_par_d)
856           gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
857               set_par_n, set_par_d, NULL);
858         goto done;
859       }
860
861       /* If all fails we can't keep the DAR and take the nearest values
862        * for everything from the first try */
863       gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
864           G_TYPE_INT, set_h, NULL);
865       if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
866           set_par_n != set_par_d)
867         gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
868             set_par_n, set_par_d, NULL);
869     }
870   }
871
872 done:
873   GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
874
875   if (from_par == &fpar)
876     g_value_unset (&fpar);
877   if (to_par == &tpar)
878     g_value_unset (&tpar);
879 }
880
881 static void
882 gst_video_scale_setup_vs_image (VSImage * image, GstVideoFormat format,
883     gint component, gint width, gint height, gint b_w, gint b_h, uint8_t * data)
884 {
885   image->real_width =
886       gst_video_format_get_component_width (format, component, width);
887   image->real_height =
888       gst_video_format_get_component_height (format, component, height);
889   image->width =
890       gst_video_format_get_component_width (format, component, MAX (1,
891           width - b_w));
892   image->height =
893       gst_video_format_get_component_height (format, component, MAX (1,
894           height - b_h));
895   image->stride = gst_video_format_get_row_stride (format, component, width);
896
897   image->border_top = (image->real_height - image->height) / 2;
898   image->border_bottom = image->real_height - image->height - image->border_top;
899
900   if (format == GST_VIDEO_FORMAT_YUY2 || format == GST_VIDEO_FORMAT_YVYU
901       || format == GST_VIDEO_FORMAT_UYVY) {
902     g_assert (component == 0);
903
904     image->border_left = (image->real_width - image->width) / 2;
905
906     if (image->border_left % 2 == 1)
907       image->border_left--;
908     image->border_right = image->real_width - image->width - image->border_left;
909   } else {
910     image->border_left = (image->real_width - image->width) / 2;
911     image->border_right = image->real_width - image->width - image->border_left;
912   }
913
914   if (format == GST_VIDEO_FORMAT_I420
915       || format == GST_VIDEO_FORMAT_YV12
916       || format == GST_VIDEO_FORMAT_Y444
917       || format == GST_VIDEO_FORMAT_Y42B || format == GST_VIDEO_FORMAT_Y41B) {
918     image->real_pixels = data + gst_video_format_get_component_offset (format,
919         component, width, height);
920   } else {
921     g_assert (component == 0);
922     image->real_pixels = data;
923   }
924
925   image->pixels =
926       image->real_pixels + image->border_top * image->stride +
927       image->border_left * gst_video_format_get_pixel_stride (format,
928       component);
929 }
930
931 static const guint8 *
932 _get_black_for_format (GstVideoFormat format)
933 {
934   static const guint8 black[][4] = {
935     {255, 0, 0, 0},             /*  0 = ARGB, ABGR, xRGB, xBGR */
936     {0, 0, 0, 255},             /*  1 = RGBA, BGRA, RGBx, BGRx */
937     {255, 16, 128, 128},        /*  2 = AYUV */
938     {0, 0, 0, 0},               /*  3 = RGB and BGR */
939     {16, 128, 128, 0},          /*  4 = v301 */
940     {16, 128, 16, 128},         /*  5 = YUY2, YUYV */
941     {128, 16, 128, 16},         /*  6 = UYVY */
942     {16, 0, 0, 0},              /*  7 = Y */
943     {0, 0, 0, 0}                /*  8 = RGB565, RGB666 */
944   };
945
946   switch (format) {
947     case GST_VIDEO_FORMAT_ARGB:
948     case GST_VIDEO_FORMAT_ABGR:
949     case GST_VIDEO_FORMAT_xRGB:
950     case GST_VIDEO_FORMAT_xBGR:
951     case GST_VIDEO_FORMAT_ARGB64:
952       return black[0];
953     case GST_VIDEO_FORMAT_RGBA:
954     case GST_VIDEO_FORMAT_BGRA:
955     case GST_VIDEO_FORMAT_RGBx:
956     case GST_VIDEO_FORMAT_BGRx:
957       return black[1];
958     case GST_VIDEO_FORMAT_AYUV:
959     case GST_VIDEO_FORMAT_AYUV64:
960       return black[2];
961     case GST_VIDEO_FORMAT_RGB:
962     case GST_VIDEO_FORMAT_BGR:
963       return black[3];
964     case GST_VIDEO_FORMAT_v308:
965       return black[4];
966     case GST_VIDEO_FORMAT_YUY2:
967     case GST_VIDEO_FORMAT_YVYU:
968       return black[5];
969     case GST_VIDEO_FORMAT_UYVY:
970       return black[6];
971     case GST_VIDEO_FORMAT_Y800:
972     case GST_VIDEO_FORMAT_GRAY8:
973       return black[7];
974     case GST_VIDEO_FORMAT_GRAY16_LE:
975     case GST_VIDEO_FORMAT_GRAY16_BE:
976     case GST_VIDEO_FORMAT_Y16:
977       return NULL;              /* Handled by the caller */
978     case GST_VIDEO_FORMAT_I420:
979     case GST_VIDEO_FORMAT_YV12:
980     case GST_VIDEO_FORMAT_Y444:
981     case GST_VIDEO_FORMAT_Y42B:
982     case GST_VIDEO_FORMAT_Y41B:
983       return black[4];          /* Y, U, V, 0 */
984     case GST_VIDEO_FORMAT_RGB16:
985     case GST_VIDEO_FORMAT_RGB15:
986       return black[8];
987     default:
988       return NULL;
989   }
990 }
991
992 static GstFlowReturn
993 gst_video_scale_transform (GstBaseTransform * trans, GstBuffer * in,
994     GstBuffer * out)
995 {
996   GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
997   GstFlowReturn ret = GST_FLOW_OK;
998   VSImage dest = { NULL, };
999   VSImage src = { NULL, };
1000   VSImage dest_u = { NULL, };
1001   VSImage dest_v = { NULL, };
1002   VSImage src_u = { NULL, };
1003   VSImage src_v = { NULL, };
1004   gint method;
1005   const guint8 *black = _get_black_for_format (videoscale->format);
1006   gboolean add_borders;
1007   guint8 *in_data, *out_data;
1008   gsize in_size, out_size;
1009
1010   GST_OBJECT_LOCK (videoscale);
1011   method = videoscale->method;
1012   add_borders = videoscale->add_borders;
1013   GST_OBJECT_UNLOCK (videoscale);
1014
1015   if (videoscale->from_width == 1) {
1016     method = GST_VIDEO_SCALE_NEAREST;
1017   }
1018   if (method == GST_VIDEO_SCALE_4TAP &&
1019       (videoscale->from_width < 4 || videoscale->from_height < 4)) {
1020     method = GST_VIDEO_SCALE_BILINEAR;
1021   }
1022
1023   in_data = gst_buffer_map (in, &in_size, NULL, GST_MAP_READ);
1024   out_data = gst_buffer_map (out, &out_size, NULL, GST_MAP_WRITE);
1025
1026   gst_video_scale_setup_vs_image (&src, videoscale->format, 0,
1027       videoscale->from_width, videoscale->from_height, 0, 0, in_data);
1028   gst_video_scale_setup_vs_image (&dest, videoscale->format, 0,
1029       videoscale->to_width, videoscale->to_height, videoscale->borders_w,
1030       videoscale->borders_h, out_data);
1031
1032   if (videoscale->format == GST_VIDEO_FORMAT_I420
1033       || videoscale->format == GST_VIDEO_FORMAT_YV12
1034       || videoscale->format == GST_VIDEO_FORMAT_Y444
1035       || videoscale->format == GST_VIDEO_FORMAT_Y42B
1036       || videoscale->format == GST_VIDEO_FORMAT_Y41B) {
1037     gst_video_scale_setup_vs_image (&src_u, videoscale->format, 1,
1038         videoscale->from_width, videoscale->from_height, 0, 0, in_data);
1039     gst_video_scale_setup_vs_image (&src_v, videoscale->format, 2,
1040         videoscale->from_width, videoscale->from_height, 0, 0, in_data);
1041     gst_video_scale_setup_vs_image (&dest_u, videoscale->format, 1,
1042         videoscale->to_width, videoscale->to_height, videoscale->borders_w,
1043         videoscale->borders_h, out_data);
1044     gst_video_scale_setup_vs_image (&dest_v, videoscale->format, 2,
1045         videoscale->to_width, videoscale->to_height, videoscale->borders_w,
1046         videoscale->borders_h, out_data);
1047   }
1048
1049   switch (videoscale->format) {
1050     case GST_VIDEO_FORMAT_RGBx:
1051     case GST_VIDEO_FORMAT_xRGB:
1052     case GST_VIDEO_FORMAT_BGRx:
1053     case GST_VIDEO_FORMAT_xBGR:
1054     case GST_VIDEO_FORMAT_RGBA:
1055     case GST_VIDEO_FORMAT_ARGB:
1056     case GST_VIDEO_FORMAT_BGRA:
1057     case GST_VIDEO_FORMAT_ABGR:
1058     case GST_VIDEO_FORMAT_AYUV:
1059       if (add_borders)
1060         vs_fill_borders_RGBA (&dest, black);
1061       switch (method) {
1062         case GST_VIDEO_SCALE_NEAREST:
1063           vs_image_scale_nearest_RGBA (&dest, &src, videoscale->tmp_buf);
1064           break;
1065         case GST_VIDEO_SCALE_BILINEAR:
1066           vs_image_scale_linear_RGBA (&dest, &src, videoscale->tmp_buf);
1067           break;
1068         case GST_VIDEO_SCALE_4TAP:
1069           vs_image_scale_4tap_RGBA (&dest, &src, videoscale->tmp_buf);
1070           break;
1071         default:
1072           goto unknown_mode;
1073       }
1074       break;
1075     case GST_VIDEO_FORMAT_ARGB64:
1076     case GST_VIDEO_FORMAT_AYUV64:
1077       if (add_borders)
1078         vs_fill_borders_AYUV64 (&dest, black);
1079       switch (method) {
1080         case GST_VIDEO_SCALE_NEAREST:
1081           vs_image_scale_nearest_AYUV64 (&dest, &src, videoscale->tmp_buf);
1082           break;
1083         case GST_VIDEO_SCALE_BILINEAR:
1084           vs_image_scale_linear_AYUV64 (&dest, &src, videoscale->tmp_buf);
1085           break;
1086         case GST_VIDEO_SCALE_4TAP:
1087           vs_image_scale_4tap_AYUV64 (&dest, &src, videoscale->tmp_buf);
1088           break;
1089         default:
1090           goto unknown_mode;
1091       }
1092       break;
1093     case GST_VIDEO_FORMAT_RGB:
1094     case GST_VIDEO_FORMAT_BGR:
1095     case GST_VIDEO_FORMAT_v308:
1096       if (add_borders)
1097         vs_fill_borders_RGB (&dest, black);
1098       switch (method) {
1099         case GST_VIDEO_SCALE_NEAREST:
1100           vs_image_scale_nearest_RGB (&dest, &src, videoscale->tmp_buf);
1101           break;
1102         case GST_VIDEO_SCALE_BILINEAR:
1103           vs_image_scale_linear_RGB (&dest, &src, videoscale->tmp_buf);
1104           break;
1105         case GST_VIDEO_SCALE_4TAP:
1106           vs_image_scale_4tap_RGB (&dest, &src, videoscale->tmp_buf);
1107           break;
1108         default:
1109           goto unknown_mode;
1110       }
1111       break;
1112     case GST_VIDEO_FORMAT_YUY2:
1113     case GST_VIDEO_FORMAT_YVYU:
1114       if (add_borders)
1115         vs_fill_borders_YUYV (&dest, black);
1116       switch (method) {
1117         case GST_VIDEO_SCALE_NEAREST:
1118           vs_image_scale_nearest_YUYV (&dest, &src, videoscale->tmp_buf);
1119           break;
1120         case GST_VIDEO_SCALE_BILINEAR:
1121           vs_image_scale_linear_YUYV (&dest, &src, videoscale->tmp_buf);
1122           break;
1123         case GST_VIDEO_SCALE_4TAP:
1124           vs_image_scale_4tap_YUYV (&dest, &src, videoscale->tmp_buf);
1125           break;
1126         default:
1127           goto unknown_mode;
1128       }
1129       break;
1130     case GST_VIDEO_FORMAT_UYVY:
1131       if (add_borders)
1132         vs_fill_borders_UYVY (&dest, black);
1133       switch (method) {
1134         case GST_VIDEO_SCALE_NEAREST:
1135           vs_image_scale_nearest_UYVY (&dest, &src, videoscale->tmp_buf);
1136           break;
1137         case GST_VIDEO_SCALE_BILINEAR:
1138           vs_image_scale_linear_UYVY (&dest, &src, videoscale->tmp_buf);
1139           break;
1140         case GST_VIDEO_SCALE_4TAP:
1141           vs_image_scale_4tap_UYVY (&dest, &src, videoscale->tmp_buf);
1142           break;
1143         default:
1144           goto unknown_mode;
1145       }
1146       break;
1147     case GST_VIDEO_FORMAT_Y800:
1148     case GST_VIDEO_FORMAT_GRAY8:
1149       if (add_borders)
1150         vs_fill_borders_Y (&dest, black);
1151       switch (method) {
1152         case GST_VIDEO_SCALE_NEAREST:
1153           vs_image_scale_nearest_Y (&dest, &src, videoscale->tmp_buf);
1154           break;
1155         case GST_VIDEO_SCALE_BILINEAR:
1156           vs_image_scale_linear_Y (&dest, &src, videoscale->tmp_buf);
1157           break;
1158         case GST_VIDEO_SCALE_4TAP:
1159           vs_image_scale_4tap_Y (&dest, &src, videoscale->tmp_buf);
1160           break;
1161         default:
1162           goto unknown_mode;
1163       }
1164       break;
1165     case GST_VIDEO_FORMAT_GRAY16_LE:
1166     case GST_VIDEO_FORMAT_GRAY16_BE:
1167     case GST_VIDEO_FORMAT_Y16:
1168       if (add_borders)
1169         vs_fill_borders_Y16 (&dest, 0);
1170       switch (method) {
1171         case GST_VIDEO_SCALE_NEAREST:
1172           vs_image_scale_nearest_Y16 (&dest, &src, videoscale->tmp_buf);
1173           break;
1174         case GST_VIDEO_SCALE_BILINEAR:
1175           vs_image_scale_linear_Y16 (&dest, &src, videoscale->tmp_buf);
1176           break;
1177         case GST_VIDEO_SCALE_4TAP:
1178           vs_image_scale_4tap_Y16 (&dest, &src, videoscale->tmp_buf);
1179           break;
1180         default:
1181           goto unknown_mode;
1182       }
1183       break;
1184     case GST_VIDEO_FORMAT_I420:
1185     case GST_VIDEO_FORMAT_YV12:
1186     case GST_VIDEO_FORMAT_Y444:
1187     case GST_VIDEO_FORMAT_Y42B:
1188     case GST_VIDEO_FORMAT_Y41B:
1189       if (add_borders) {
1190         vs_fill_borders_Y (&dest, black);
1191         vs_fill_borders_Y (&dest_u, black + 1);
1192         vs_fill_borders_Y (&dest_v, black + 2);
1193       }
1194       switch (method) {
1195         case GST_VIDEO_SCALE_NEAREST:
1196           vs_image_scale_nearest_Y (&dest, &src, videoscale->tmp_buf);
1197           vs_image_scale_nearest_Y (&dest_u, &src_u, videoscale->tmp_buf);
1198           vs_image_scale_nearest_Y (&dest_v, &src_v, videoscale->tmp_buf);
1199           break;
1200         case GST_VIDEO_SCALE_BILINEAR:
1201           vs_image_scale_linear_Y (&dest, &src, videoscale->tmp_buf);
1202           vs_image_scale_linear_Y (&dest_u, &src_u, videoscale->tmp_buf);
1203           vs_image_scale_linear_Y (&dest_v, &src_v, videoscale->tmp_buf);
1204           break;
1205         case GST_VIDEO_SCALE_4TAP:
1206           vs_image_scale_4tap_Y (&dest, &src, videoscale->tmp_buf);
1207           vs_image_scale_4tap_Y (&dest_u, &src_u, videoscale->tmp_buf);
1208           vs_image_scale_4tap_Y (&dest_v, &src_v, videoscale->tmp_buf);
1209           break;
1210         default:
1211           goto unknown_mode;
1212       }
1213       break;
1214     case GST_VIDEO_FORMAT_RGB16:
1215       if (add_borders)
1216         vs_fill_borders_RGB565 (&dest, black);
1217       switch (method) {
1218         case GST_VIDEO_SCALE_NEAREST:
1219           vs_image_scale_nearest_RGB565 (&dest, &src, videoscale->tmp_buf);
1220           break;
1221         case GST_VIDEO_SCALE_BILINEAR:
1222           vs_image_scale_linear_RGB565 (&dest, &src, videoscale->tmp_buf);
1223           break;
1224         case GST_VIDEO_SCALE_4TAP:
1225           vs_image_scale_4tap_RGB565 (&dest, &src, videoscale->tmp_buf);
1226           break;
1227         default:
1228           goto unknown_mode;
1229       }
1230       break;
1231     case GST_VIDEO_FORMAT_RGB15:
1232       if (add_borders)
1233         vs_fill_borders_RGB555 (&dest, black);
1234       switch (method) {
1235         case GST_VIDEO_SCALE_NEAREST:
1236           vs_image_scale_nearest_RGB555 (&dest, &src, videoscale->tmp_buf);
1237           break;
1238         case GST_VIDEO_SCALE_BILINEAR:
1239           vs_image_scale_linear_RGB555 (&dest, &src, videoscale->tmp_buf);
1240           break;
1241         case GST_VIDEO_SCALE_4TAP:
1242           vs_image_scale_4tap_RGB555 (&dest, &src, videoscale->tmp_buf);
1243           break;
1244         default:
1245           goto unknown_mode;
1246       }
1247       break;
1248     default:
1249       goto unsupported;
1250   }
1251
1252   GST_LOG_OBJECT (videoscale, "pushing buffer of %d bytes",
1253       gst_buffer_get_size (out));
1254
1255 done:
1256   gst_buffer_unmap (in, in_data, in_size);
1257   gst_buffer_unmap (out, out_data, out_size);
1258
1259   return ret;
1260
1261   /* ERRORS */
1262 unsupported:
1263   {
1264     GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1265         ("Unsupported format %d for scaling method %d",
1266             videoscale->format, method));
1267     ret = GST_FLOW_ERROR;
1268     goto done;
1269   }
1270 unknown_mode:
1271   {
1272     GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1273         ("Unknown scaling method %d", videoscale->method));
1274     ret = GST_FLOW_ERROR;
1275     goto done;
1276   }
1277 }
1278
1279 static gboolean
1280 gst_video_scale_src_event (GstBaseTransform * trans, GstEvent * event)
1281 {
1282   GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
1283   gboolean ret;
1284   gdouble a;
1285   GstStructure *structure;
1286
1287   GST_DEBUG_OBJECT (videoscale, "handling %s event",
1288       GST_EVENT_TYPE_NAME (event));
1289
1290   switch (GST_EVENT_TYPE (event)) {
1291     case GST_EVENT_NAVIGATION:
1292       event =
1293           GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
1294
1295       structure = (GstStructure *) gst_event_get_structure (event);
1296       if (gst_structure_get_double (structure, "pointer_x", &a)) {
1297         gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
1298             a * videoscale->from_width / videoscale->to_width, NULL);
1299       }
1300       if (gst_structure_get_double (structure, "pointer_y", &a)) {
1301         gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
1302             a * videoscale->from_height / videoscale->to_height, NULL);
1303       }
1304       break;
1305     default:
1306       break;
1307   }
1308
1309   ret = GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
1310
1311   return ret;
1312 }
1313
1314 static gboolean
1315 plugin_init (GstPlugin * plugin)
1316 {
1317   gst_videoscale_orc_init ();
1318
1319   if (!gst_element_register (plugin, "videoscale", GST_RANK_NONE,
1320           GST_TYPE_VIDEO_SCALE))
1321     return FALSE;
1322
1323   GST_DEBUG_CATEGORY_INIT (video_scale_debug, "videoscale", 0,
1324       "videoscale element");
1325
1326   vs_4tap_init ();
1327
1328   return TRUE;
1329 }
1330
1331 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1332     GST_VERSION_MINOR,
1333     "videoscale",
1334     "Resizes video", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
1335     GST_PACKAGE_ORIGIN)