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