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