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