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