2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2005 David Schleef <ds@schleef.org>
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.
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.
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.
22 * SECTION:element-videoscale
23 * @see_also: videorate, ffmpegcolorspace
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.
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
35 * <title>Example pipelines</title>
37 * gst-launch -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! ffmpegcolorspace ! videoscale ! ximagesink
38 * ]| Decode an Ogg/Theora and display the video using ximagesink. Since
39 * ximagesink cannot perform scaling, the video scaling will be performed by
40 * videoscale when you resize the video window.
41 * To create the test Ogg/Theora file refer to the documentation of theoraenc.
43 * gst-launch -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videoscale ! video/x-raw-yuv, width=50 ! xvimagesink
44 * ]| Decode an Ogg/Theora and display the video using xvimagesink with a width
48 * Last reviewed on 2006-03-02 (0.10.4)
52 * Formulas for PAR, DAR, width and height relations:
63 * w = h * ----- * -----
67 * h = w * ----- * -----
79 #include <gst/video/video.h>
81 #include "gstvideoscale.h"
84 #include "vs_fill_borders.h"
86 /* debug variable definition */
87 GST_DEBUG_CATEGORY (video_scale_debug);
89 #define DEFAULT_PROP_METHOD GST_VIDEO_SCALE_BILINEAR
90 #define DEFAULT_PROP_ADD_BORDERS FALSE
100 #undef GST_VIDEO_SIZE_RANGE
101 #define GST_VIDEO_SIZE_RANGE "(int) [ 1, 32767]"
103 static GstStaticCaps gst_video_scale_format_caps[] = {
104 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA),
105 GST_STATIC_CAPS (GST_VIDEO_CAPS_ARGB),
106 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRA),
107 GST_STATIC_CAPS (GST_VIDEO_CAPS_ABGR),
108 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV")),
109 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx),
110 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB),
111 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx),
112 GST_STATIC_CAPS (GST_VIDEO_CAPS_xBGR),
113 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y444")),
114 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("v308")),
115 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB),
116 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGR),
117 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y42B")),
118 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("YUY2")),
119 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("YVYU")),
120 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("UYVY")),
121 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")),
122 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("YV12")),
123 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y41B")),
124 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB_16),
125 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB_15),
126 GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY16 ("BYTE_ORDER")),
127 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y16 ")),
128 GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY8),
129 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y800")),
130 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y8 ")),
131 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("GREY"))
134 #define GST_TYPE_VIDEO_SCALE_METHOD (gst_video_scale_method_get_type())
136 gst_video_scale_method_get_type (void)
138 static GType video_scale_method_type = 0;
140 static const GEnumValue video_scale_methods[] = {
141 {GST_VIDEO_SCALE_NEAREST, "Nearest Neighbour", "nearest-neighbour"},
142 {GST_VIDEO_SCALE_BILINEAR, "Bilinear", "bilinear"},
143 {GST_VIDEO_SCALE_4TAP, "4-tap", "4-tap"},
147 if (!video_scale_method_type) {
148 video_scale_method_type =
149 g_enum_register_static ("GstVideoScaleMethod", video_scale_methods);
151 return video_scale_method_type;
155 gst_video_scale_get_capslist (void)
157 static GstCaps *caps = NULL;
158 static volatile gsize inited = 0;
160 if (g_once_init_enter (&inited)) {
163 g_assert (caps == NULL);
165 caps = gst_caps_new_empty ();
166 for (i = 0; i < G_N_ELEMENTS (gst_video_scale_format_caps); i++)
167 gst_caps_append (caps,
168 gst_caps_make_writable
169 (gst_static_caps_get (&gst_video_scale_format_caps[i])));
170 g_once_init_leave (&inited, 1);
176 static GstPadTemplate *
177 gst_video_scale_src_template_factory (void)
179 return gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
180 gst_caps_ref (gst_video_scale_get_capslist ()));
183 static GstPadTemplate *
184 gst_video_scale_sink_template_factory (void)
186 return gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
187 gst_caps_ref (gst_video_scale_get_capslist ()));
191 static void gst_video_scale_finalize (GstVideoScale * videoscale);
192 static gboolean gst_video_scale_src_event (GstBaseTransform * trans,
195 /* base transform vmethods */
196 static GstCaps *gst_video_scale_transform_caps (GstBaseTransform * trans,
197 GstPadDirection direction, GstCaps * caps);
198 static gboolean gst_video_scale_set_caps (GstBaseTransform * trans,
199 GstCaps * in, GstCaps * out);
200 static gboolean gst_video_scale_get_unit_size (GstBaseTransform * trans,
201 GstCaps * caps, guint * size);
202 static GstFlowReturn gst_video_scale_transform (GstBaseTransform * trans,
203 GstBuffer * in, GstBuffer * out);
204 static void gst_video_scale_fixate_caps (GstBaseTransform * base,
205 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
207 static void gst_video_scale_set_property (GObject * object, guint prop_id,
208 const GValue * value, GParamSpec * pspec);
209 static void gst_video_scale_get_property (GObject * object, guint prop_id,
210 GValue * value, GParamSpec * pspec);
212 GST_BOILERPLATE (GstVideoScale, gst_video_scale, GstVideoFilter,
213 GST_TYPE_VIDEO_FILTER);
216 gst_video_scale_base_init (gpointer g_class)
218 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
220 gst_element_class_set_details_simple (element_class,
221 "Video scaler", "Filter/Effect/Video",
222 "Resizes video", "Wim Taymans <wim.taymans@chello.be>");
224 gst_element_class_add_pad_template (element_class,
225 gst_video_scale_sink_template_factory ());
226 gst_element_class_add_pad_template (element_class,
227 gst_video_scale_src_template_factory ());
231 gst_video_scale_class_init (GstVideoScaleClass * klass)
233 GObjectClass *gobject_class = (GObjectClass *) klass;
234 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
236 gobject_class->finalize = (GObjectFinalizeFunc) gst_video_scale_finalize;
237 gobject_class->set_property = gst_video_scale_set_property;
238 gobject_class->get_property = gst_video_scale_get_property;
240 g_object_class_install_property (gobject_class, PROP_METHOD,
241 g_param_spec_enum ("method", "method", "method",
242 GST_TYPE_VIDEO_SCALE_METHOD, DEFAULT_PROP_METHOD,
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245 g_object_class_install_property (gobject_class, PROP_ADD_BORDERS,
246 g_param_spec_boolean ("add-borders", "Add Borders",
247 "Add black borders if necessary to keep the display aspect ratio",
248 DEFAULT_PROP_ADD_BORDERS,
249 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
251 trans_class->transform_caps =
252 GST_DEBUG_FUNCPTR (gst_video_scale_transform_caps);
253 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_scale_set_caps);
254 trans_class->get_unit_size =
255 GST_DEBUG_FUNCPTR (gst_video_scale_get_unit_size);
256 trans_class->transform = GST_DEBUG_FUNCPTR (gst_video_scale_transform);
257 trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_video_scale_fixate_caps);
258 trans_class->src_event = GST_DEBUG_FUNCPTR (gst_video_scale_src_event);
262 gst_video_scale_init (GstVideoScale * videoscale, GstVideoScaleClass * klass)
264 videoscale->tmp_buf = NULL;
265 videoscale->method = DEFAULT_PROP_METHOD;
266 videoscale->add_borders = DEFAULT_PROP_ADD_BORDERS;
270 gst_video_scale_finalize (GstVideoScale * videoscale)
272 if (videoscale->tmp_buf)
273 g_free (videoscale->tmp_buf);
275 G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (videoscale));
279 gst_video_scale_set_property (GObject * object, guint prop_id,
280 const GValue * value, GParamSpec * pspec)
282 GstVideoScale *vscale = GST_VIDEO_SCALE (object);
286 GST_OBJECT_LOCK (vscale);
287 vscale->method = g_value_get_enum (value);
288 GST_OBJECT_UNLOCK (vscale);
290 case PROP_ADD_BORDERS:
291 GST_OBJECT_LOCK (vscale);
292 vscale->add_borders = g_value_get_boolean (value);
293 GST_OBJECT_UNLOCK (vscale);
294 gst_base_transform_reconfigure (GST_BASE_TRANSFORM_CAST (vscale));
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
303 gst_video_scale_get_property (GObject * object, guint prop_id, GValue * value,
306 GstVideoScale *vscale = GST_VIDEO_SCALE (object);
310 GST_OBJECT_LOCK (vscale);
311 g_value_set_enum (value, vscale->method);
312 GST_OBJECT_UNLOCK (vscale);
314 case PROP_ADD_BORDERS:
315 GST_OBJECT_LOCK (vscale);
316 g_value_set_boolean (value, vscale->add_borders);
317 GST_OBJECT_UNLOCK (vscale);
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
326 gst_video_scale_transform_caps (GstBaseTransform * trans,
327 GstPadDirection direction, GstCaps * caps)
330 GstStructure *structure;
332 /* this function is always called with a simple caps */
333 g_return_val_if_fail (GST_CAPS_IS_SIMPLE (caps), NULL);
335 ret = gst_caps_copy (caps);
336 structure = gst_structure_copy (gst_caps_get_structure (ret, 0));
338 gst_structure_set (structure,
339 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
340 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
342 /* if pixel aspect ratio, make a range of it */
343 if (gst_structure_has_field (structure, "pixel-aspect-ratio")) {
344 gst_structure_set (structure,
345 "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
348 gst_caps_merge_structure (ret, gst_structure_copy (structure));
349 gst_structure_free (structure);
351 GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
357 gst_video_scale_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
359 GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
361 gint from_dar_n, from_dar_d, to_dar_n, to_dar_d;
362 gint from_par_n, from_par_d, to_par_n, to_par_d;
365 gst_video_format_parse_caps (in, &videoscale->format,
366 &videoscale->from_width, &videoscale->from_height);
368 gst_video_format_parse_caps (out, NULL, &videoscale->to_width,
369 &videoscale->to_height);
373 videoscale->src_size = gst_video_format_get_size (videoscale->format,
374 videoscale->from_width, videoscale->from_height);
375 videoscale->dest_size = gst_video_format_get_size (videoscale->format,
376 videoscale->to_width, videoscale->to_height);
378 if (!gst_video_parse_caps_pixel_aspect_ratio (in, &from_par_n, &from_par_d))
379 from_par_n = from_par_d = 1;
380 if (!gst_video_parse_caps_pixel_aspect_ratio (out, &to_par_n, &to_par_d))
381 to_par_n = to_par_d = 1;
383 if (!gst_util_fraction_multiply (videoscale->from_width,
384 videoscale->from_height, from_par_n, from_par_d, &from_dar_n,
386 from_dar_n = from_dar_d = -1;
389 if (!gst_util_fraction_multiply (videoscale->to_width,
390 videoscale->to_height, to_par_n, to_par_d, &to_dar_n, &to_dar_d)) {
391 to_dar_n = to_dar_d = -1;
394 videoscale->borders_w = videoscale->borders_h = 0;
395 if (to_dar_n != from_dar_n || to_dar_d != from_dar_d) {
396 if (videoscale->add_borders) {
397 gint n, d, to_h, to_w;
399 if (from_dar_n != -1 && from_dar_d != -1
400 && gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
402 to_h = gst_util_uint64_scale_int (videoscale->to_width, d, n);
403 if (to_h <= videoscale->to_height) {
404 videoscale->borders_h = videoscale->to_height - to_h;
405 videoscale->borders_w = 0;
407 to_w = gst_util_uint64_scale_int (videoscale->to_height, n, d);
408 g_assert (to_w <= videoscale->to_width);
409 videoscale->borders_h = 0;
410 videoscale->borders_w = videoscale->to_width - to_w;
413 GST_WARNING_OBJECT (videoscale, "Can't calculate borders");
416 GST_WARNING_OBJECT (videoscale, "Can't keep DAR!");
420 if (videoscale->tmp_buf)
421 g_free (videoscale->tmp_buf);
422 videoscale->tmp_buf =
423 g_malloc (gst_video_format_get_row_stride (videoscale->format, 0,
424 videoscale->to_width) * 4);
426 gst_base_transform_set_passthrough (trans,
427 (videoscale->from_width == videoscale->to_width
428 && videoscale->from_height == videoscale->to_height));
430 GST_DEBUG_OBJECT (videoscale, "from=%dx%d (par=%d/%d dar=%d/%d), size %d "
431 "-> to=%dx%d (par=%d/%d dar=%d/%d borders=%d:%d), size %d",
432 videoscale->from_width, videoscale->from_height, from_par_n, from_par_d,
433 from_dar_n, from_dar_d, videoscale->src_size, videoscale->to_width,
434 videoscale->to_height, to_par_n, to_par_d, to_dar_n, to_dar_d,
435 videoscale->borders_w, videoscale->borders_h, videoscale->dest_size);
442 gst_video_scale_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
445 GstVideoFormat format;
448 if (!gst_video_format_parse_caps (caps, &format, &width, &height))
451 *size = gst_video_format_get_size (format, width, height);
457 gst_video_scale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
458 GstCaps * caps, GstCaps * othercaps)
460 GstStructure *ins, *outs;
461 const GValue *from_par, *to_par;
462 GValue fpar = { 0, }, tpar = {
465 g_return_if_fail (gst_caps_is_fixed (caps));
467 GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
468 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
470 ins = gst_caps_get_structure (caps, 0);
471 outs = gst_caps_get_structure (othercaps, 0);
473 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
474 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
476 /* If we're fixating from the sinkpad we always set the PAR and
477 * assume that missing PAR on the sinkpad means 1/1 and
478 * missing PAR on the srcpad means undefined
480 if (direction == GST_PAD_SINK) {
482 g_value_init (&fpar, GST_TYPE_FRACTION);
483 gst_value_set_fraction (&fpar, 1, 1);
487 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
488 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
491 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1,
492 G_MAXINT, G_MAXINT, 1, NULL);
496 g_value_init (&tpar, GST_TYPE_FRACTION);
497 gst_value_set_fraction (&tpar, 1, 1);
500 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
504 g_value_init (&fpar, GST_TYPE_FRACTION_RANGE);
505 gst_value_set_fraction_range_full (&fpar, 1, G_MAXINT, G_MAXINT, 1);
510 /* we have both PAR but they might not be fixated */
512 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
514 gint from_dar_n, from_dar_d;
517 /* from_par should be fixed */
518 g_return_if_fail (gst_value_is_fixed (from_par));
520 from_par_n = gst_value_get_fraction_numerator (from_par);
521 from_par_d = gst_value_get_fraction_denominator (from_par);
523 gst_structure_get_int (ins, "width", &from_w);
524 gst_structure_get_int (ins, "height", &from_h);
526 gst_structure_get_int (outs, "width", &w);
527 gst_structure_get_int (outs, "height", &h);
529 /* if both width and height are already fixed, we can't do anything
530 * about it anymore */
534 GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
536 if (!gst_value_is_fixed (to_par)) {
537 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
538 from_par_n, from_par_d, w, h)) {
539 GST_DEBUG_OBJECT (base, "fixating to_par to %dx%d", n, d);
540 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
541 gst_structure_fixate_field_nearest_fraction (outs,
542 "pixel-aspect-ratio", n, d);
544 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
551 /* Calculate input DAR */
552 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
553 &from_dar_n, &from_dar_d)) {
554 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
555 ("Error calculating the output scaled size - integer overflow"));
559 GST_DEBUG_OBJECT (base, "Input DAR is %d/%d", from_dar_n, from_dar_d);
561 /* If either width or height are fixed there's not much we
562 * can do either except choosing a height or width and PAR
563 * that matches the DAR as good as possible
567 gint set_w, set_par_n, set_par_d;
569 GST_DEBUG_OBJECT (base, "height is fixed (%d)", h);
571 /* If the PAR is fixed too, there's not much to do
572 * except choosing the width that is nearest to the
573 * width with the same DAR */
574 if (gst_value_is_fixed (to_par)) {
575 to_par_n = gst_value_get_fraction_numerator (to_par);
576 to_par_d = gst_value_get_fraction_denominator (to_par);
578 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
580 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
581 to_par_n, &num, &den)) {
582 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
583 ("Error calculating the output scaled size - integer overflow"));
587 w = (guint) gst_util_uint64_scale_int (h, num, den);
588 gst_structure_fixate_field_nearest_int (outs, "width", w);
593 /* The PAR is not fixed and it's quite likely that we can set
594 * an arbitrary PAR. */
596 /* Check if we can keep the input width */
597 tmp = gst_structure_copy (outs);
598 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
599 gst_structure_get_int (tmp, "width", &set_w);
601 /* Might have failed but try to keep the DAR nonetheless by
602 * adjusting the PAR */
603 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
604 &to_par_n, &to_par_d)) {
605 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
606 ("Error calculating the output scaled size - integer overflow"));
607 gst_structure_free (tmp);
610 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
612 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
614 gst_structure_free (tmp);
616 /* Check if the adjusted PAR is accepted */
617 if (set_par_n == to_par_n && set_par_d == to_par_d) {
618 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
619 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
624 /* Otherwise scale the width to the new PAR and check if the
625 * adjusted with is accepted. If all that fails we can't keep
627 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
628 set_par_n, &num, &den)) {
629 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
630 ("Error calculating the output scaled size - integer overflow"));
634 w = (guint) gst_util_uint64_scale_int (h, num, den);
635 gst_structure_fixate_field_nearest_int (outs, "width", w);
636 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
637 set_par_n, set_par_d, NULL);
642 gint set_h, set_par_n, set_par_d;
644 GST_DEBUG_OBJECT (base, "width is fixed (%d)", w);
646 /* If the PAR is fixed too, there's not much to do
647 * except choosing the height that is nearest to the
648 * height with the same DAR */
649 if (gst_value_is_fixed (to_par)) {
650 to_par_n = gst_value_get_fraction_numerator (to_par);
651 to_par_d = gst_value_get_fraction_denominator (to_par);
653 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
655 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
656 to_par_n, &num, &den)) {
657 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
658 ("Error calculating the output scaled size - integer overflow"));
662 h = (guint) gst_util_uint64_scale_int (w, den, num);
663 gst_structure_fixate_field_nearest_int (outs, "height", h);
668 /* The PAR is not fixed and it's quite likely that we can set
669 * an arbitrary PAR. */
671 /* Check if we can keep the input height */
672 tmp = gst_structure_copy (outs);
673 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
674 gst_structure_get_int (tmp, "height", &set_h);
676 /* Might have failed but try to keep the DAR nonetheless by
677 * adjusting the PAR */
678 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
679 &to_par_n, &to_par_d)) {
680 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
681 ("Error calculating the output scaled size - integer overflow"));
682 gst_structure_free (tmp);
685 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
687 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
689 gst_structure_free (tmp);
691 /* Check if the adjusted PAR is accepted */
692 if (set_par_n == to_par_n && set_par_d == to_par_d) {
693 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
694 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
699 /* Otherwise scale the height to the new PAR and check if the
700 * adjusted with is accepted. If all that fails we can't keep
702 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
703 set_par_n, &num, &den)) {
704 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
705 ("Error calculating the output scaled size - integer overflow"));
709 h = (guint) gst_util_uint64_scale_int (w, den, num);
710 gst_structure_fixate_field_nearest_int (outs, "height", h);
711 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
712 set_par_n, set_par_d, NULL);
715 } else if (gst_value_is_fixed (to_par)) {
717 gint set_h, set_w, f_h, f_w;
719 to_par_n = gst_value_get_fraction_numerator (to_par);
720 to_par_d = gst_value_get_fraction_denominator (to_par);
722 /* Calculate scale factor for the PAR change */
723 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
724 to_par_d, &num, &den)) {
725 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
726 ("Error calculating the output scaled size - integer overflow"));
730 /* Try to keep the input height (because of interlacing) */
731 tmp = gst_structure_copy (outs);
732 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
733 gst_structure_get_int (tmp, "height", &set_h);
735 /* This might have failed but try to scale the width
736 * to keep the DAR nonetheless */
737 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
738 gst_structure_fixate_field_nearest_int (tmp, "width", w);
739 gst_structure_get_int (tmp, "width", &set_w);
740 gst_structure_free (tmp);
742 /* We kept the DAR and the height is nearest to the original height */
744 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
745 G_TYPE_INT, set_h, NULL);
752 /* If the former failed, try to keep the input width at least */
753 tmp = gst_structure_copy (outs);
754 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
755 gst_structure_get_int (tmp, "width", &set_w);
757 /* This might have failed but try to scale the width
758 * to keep the DAR nonetheless */
759 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
760 gst_structure_fixate_field_nearest_int (tmp, "height", h);
761 gst_structure_get_int (tmp, "height", &set_h);
762 gst_structure_free (tmp);
764 /* We kept the DAR and the width is nearest to the original width */
766 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
767 G_TYPE_INT, set_h, NULL);
771 /* If all this failed, keep the height that was nearest to the orignal
772 * height and the nearest possible width. This changes the DAR but
773 * there's not much else to do here.
775 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
780 gint set_h, set_w, set_par_n, set_par_d, tmp2;
782 /* width, height and PAR are not fixed but passthrough is not possible */
784 /* First try to keep the height and width as good as possible
786 tmp = gst_structure_copy (outs);
787 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
788 gst_structure_get_int (tmp, "height", &set_h);
789 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
790 gst_structure_get_int (tmp, "width", &set_w);
792 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
793 &to_par_n, &to_par_d)) {
794 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
795 ("Error calculating the output scaled size - integer overflow"));
799 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
801 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
803 gst_structure_free (tmp);
805 if (set_par_n == to_par_n && set_par_d == to_par_d) {
806 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
807 G_TYPE_INT, set_h, "pixel-aspect-ratio", GST_TYPE_FRACTION,
808 set_par_n, set_par_d, NULL);
812 /* Otherwise try to scale width to keep the DAR with the set
814 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
815 set_par_n, &num, &den)) {
816 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
817 ("Error calculating the output scaled size - integer overflow"));
821 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
822 tmp = gst_structure_copy (outs);
823 gst_structure_fixate_field_nearest_int (tmp, "width", w);
824 gst_structure_get_int (tmp, "width", &tmp2);
825 gst_structure_free (tmp);
828 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
829 G_TYPE_INT, set_h, "pixel-aspect-ratio", GST_TYPE_FRACTION,
830 set_par_n, set_par_d, NULL);
834 /* ... or try the same with the height */
835 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
836 tmp = gst_structure_copy (outs);
837 gst_structure_fixate_field_nearest_int (tmp, "height", h);
838 gst_structure_get_int (tmp, "height", &tmp2);
839 gst_structure_free (tmp);
842 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
843 G_TYPE_INT, tmp2, "pixel-aspect-ratio", GST_TYPE_FRACTION,
844 set_par_n, set_par_d, NULL);
848 /* If all fails we can't keep the DAR and take the nearest values
849 * for everything from the first try */
850 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
851 G_TYPE_INT, set_h, "pixel-aspect-ratio", GST_TYPE_FRACTION,
852 set_par_n, set_par_d, NULL);
857 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
859 if (from_par == &fpar)
860 g_value_unset (&fpar);
862 g_value_unset (&tpar);
866 gst_video_scale_setup_vs_image (VSImage * image, GstVideoFormat format,
867 gint component, gint width, gint height, gint b_w, gint b_h, uint8_t * data)
870 gst_video_format_get_component_width (format, component, width);
872 gst_video_format_get_component_height (format, component, height);
874 gst_video_format_get_component_width (format, component, MAX (1,
877 gst_video_format_get_component_height (format, component, MAX (1,
879 image->stride = gst_video_format_get_row_stride (format, component, width);
881 image->border_top = (image->real_height - image->height) / 2;
882 image->border_bottom = image->real_height - image->height - image->border_top;
884 if (format == GST_VIDEO_FORMAT_YUY2 || format == GST_VIDEO_FORMAT_YVYU
885 || format == GST_VIDEO_FORMAT_UYVY) {
886 g_assert (component == 0);
888 image->border_left = (image->real_width - image->width) / 2;
890 if (image->border_left % 2 == 1)
891 image->border_left--;
892 image->border_right = image->real_width - image->width - image->border_left;
894 image->border_left = (image->real_width - image->width) / 2;
895 image->border_right = image->real_width - image->width - image->border_left;
898 if (format == GST_VIDEO_FORMAT_I420
899 || format == GST_VIDEO_FORMAT_YV12
900 || format == GST_VIDEO_FORMAT_Y444
901 || format == GST_VIDEO_FORMAT_Y42B || format == GST_VIDEO_FORMAT_Y41B) {
902 image->real_pixels = data + gst_video_format_get_component_offset (format,
903 component, width, height);
905 g_assert (component == 0);
906 image->real_pixels = data;
910 image->real_pixels + image->border_top * image->stride +
911 image->border_left * gst_video_format_get_pixel_stride (format,
915 static const guint8 *
916 _get_black_for_format (GstVideoFormat format)
918 static const guint8 black[][4] = {
919 {255, 0, 0, 0}, /* 0 = ARGB, ABGR, xRGB, xBGR */
920 {0, 0, 0, 255}, /* 1 = RGBA, BGRA, RGBx, BGRx */
921 {255, 16, 128, 128}, /* 2 = AYUV */
922 {0, 0, 0, 0}, /* 3 = RGB and BGR */
923 {16, 128, 128, 0}, /* 4 = v301 */
924 {16, 128, 16, 128}, /* 5 = YUY2, YUYV */
925 {128, 16, 128, 16}, /* 6 = UYVY */
926 {16, 0, 0, 0}, /* 7 = Y */
927 {0, 0, 0, 0} /* 8 = RGB565, RGB666 */
931 case GST_VIDEO_FORMAT_ARGB:
932 case GST_VIDEO_FORMAT_ABGR:
933 case GST_VIDEO_FORMAT_xRGB:
934 case GST_VIDEO_FORMAT_xBGR:
936 case GST_VIDEO_FORMAT_RGBA:
937 case GST_VIDEO_FORMAT_BGRA:
938 case GST_VIDEO_FORMAT_RGBx:
939 case GST_VIDEO_FORMAT_BGRx:
941 case GST_VIDEO_FORMAT_AYUV:
943 case GST_VIDEO_FORMAT_RGB:
944 case GST_VIDEO_FORMAT_BGR:
946 case GST_VIDEO_FORMAT_v308:
948 case GST_VIDEO_FORMAT_YUY2:
949 case GST_VIDEO_FORMAT_YVYU:
951 case GST_VIDEO_FORMAT_UYVY:
953 case GST_VIDEO_FORMAT_Y800:
954 case GST_VIDEO_FORMAT_GRAY8:
956 case GST_VIDEO_FORMAT_GRAY16_LE:
957 case GST_VIDEO_FORMAT_GRAY16_BE:
958 case GST_VIDEO_FORMAT_Y16:
959 return NULL; /* Handled by the caller */
960 case GST_VIDEO_FORMAT_I420:
961 case GST_VIDEO_FORMAT_YV12:
962 case GST_VIDEO_FORMAT_Y444:
963 case GST_VIDEO_FORMAT_Y42B:
964 case GST_VIDEO_FORMAT_Y41B:
965 return black[4]; /* Y, U, V, 0 */
966 case GST_VIDEO_FORMAT_RGB16:
967 case GST_VIDEO_FORMAT_RGB15:
975 gst_video_scale_transform (GstBaseTransform * trans, GstBuffer * in,
978 GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
979 GstFlowReturn ret = GST_FLOW_OK;
980 VSImage dest = { NULL, };
981 VSImage src = { NULL, };
982 VSImage dest_u = { NULL, };
983 VSImage dest_v = { NULL, };
984 VSImage src_u = { NULL, };
985 VSImage src_v = { NULL, };
987 const guint8 *black = _get_black_for_format (videoscale->format);
988 gboolean add_borders;
990 GST_OBJECT_LOCK (videoscale);
991 method = videoscale->method;
992 add_borders = videoscale->add_borders;
993 GST_OBJECT_UNLOCK (videoscale);
995 gst_video_scale_setup_vs_image (&src, videoscale->format, 0,
996 videoscale->from_width, videoscale->from_height, 0, 0,
997 GST_BUFFER_DATA (in));
998 gst_video_scale_setup_vs_image (&dest, videoscale->format, 0,
999 videoscale->to_width, videoscale->to_height, videoscale->borders_w,
1000 videoscale->borders_h, GST_BUFFER_DATA (out));
1002 if (videoscale->format == GST_VIDEO_FORMAT_I420
1003 || videoscale->format == GST_VIDEO_FORMAT_YV12
1004 || videoscale->format == GST_VIDEO_FORMAT_Y444
1005 || videoscale->format == GST_VIDEO_FORMAT_Y42B
1006 || videoscale->format == GST_VIDEO_FORMAT_Y41B) {
1007 gst_video_scale_setup_vs_image (&src_u, videoscale->format, 1,
1008 videoscale->from_width, videoscale->from_height, 0, 0,
1009 GST_BUFFER_DATA (in));
1010 gst_video_scale_setup_vs_image (&src_v, videoscale->format, 2,
1011 videoscale->from_width, videoscale->from_height, 0, 0,
1012 GST_BUFFER_DATA (in));
1013 gst_video_scale_setup_vs_image (&dest_u, videoscale->format, 1,
1014 videoscale->to_width, videoscale->to_height, videoscale->borders_w,
1015 videoscale->borders_h, GST_BUFFER_DATA (out));
1016 gst_video_scale_setup_vs_image (&dest_v, videoscale->format, 2,
1017 videoscale->to_width, videoscale->to_height, videoscale->borders_w,
1018 videoscale->borders_h, GST_BUFFER_DATA (out));
1021 switch (videoscale->format) {
1022 case GST_VIDEO_FORMAT_RGBx:
1023 case GST_VIDEO_FORMAT_xRGB:
1024 case GST_VIDEO_FORMAT_BGRx:
1025 case GST_VIDEO_FORMAT_xBGR:
1026 case GST_VIDEO_FORMAT_RGBA:
1027 case GST_VIDEO_FORMAT_ARGB:
1028 case GST_VIDEO_FORMAT_BGRA:
1029 case GST_VIDEO_FORMAT_ABGR:
1030 case GST_VIDEO_FORMAT_AYUV:
1032 vs_fill_borders_RGBA (&dest, black);
1034 case GST_VIDEO_SCALE_NEAREST:
1035 vs_image_scale_nearest_RGBA (&dest, &src, videoscale->tmp_buf);
1037 case GST_VIDEO_SCALE_BILINEAR:
1038 vs_image_scale_linear_RGBA (&dest, &src, videoscale->tmp_buf);
1040 case GST_VIDEO_SCALE_4TAP:
1041 vs_image_scale_4tap_RGBA (&dest, &src, videoscale->tmp_buf);
1047 case GST_VIDEO_FORMAT_RGB:
1048 case GST_VIDEO_FORMAT_BGR:
1049 case GST_VIDEO_FORMAT_v308:
1051 vs_fill_borders_RGB (&dest, black);
1053 case GST_VIDEO_SCALE_NEAREST:
1054 vs_image_scale_nearest_RGB (&dest, &src, videoscale->tmp_buf);
1056 case GST_VIDEO_SCALE_BILINEAR:
1057 vs_image_scale_linear_RGB (&dest, &src, videoscale->tmp_buf);
1059 case GST_VIDEO_SCALE_4TAP:
1060 vs_image_scale_4tap_RGB (&dest, &src, videoscale->tmp_buf);
1066 case GST_VIDEO_FORMAT_YUY2:
1067 case GST_VIDEO_FORMAT_YVYU:
1069 vs_fill_borders_YUYV (&dest, black);
1071 case GST_VIDEO_SCALE_NEAREST:
1072 vs_image_scale_nearest_YUYV (&dest, &src, videoscale->tmp_buf);
1074 case GST_VIDEO_SCALE_BILINEAR:
1075 vs_image_scale_linear_YUYV (&dest, &src, videoscale->tmp_buf);
1077 case GST_VIDEO_SCALE_4TAP:
1078 vs_image_scale_4tap_YUYV (&dest, &src, videoscale->tmp_buf);
1084 case GST_VIDEO_FORMAT_UYVY:
1086 vs_fill_borders_UYVY (&dest, black);
1088 case GST_VIDEO_SCALE_NEAREST:
1089 vs_image_scale_nearest_UYVY (&dest, &src, videoscale->tmp_buf);
1091 case GST_VIDEO_SCALE_BILINEAR:
1092 vs_image_scale_linear_UYVY (&dest, &src, videoscale->tmp_buf);
1094 case GST_VIDEO_SCALE_4TAP:
1095 vs_image_scale_4tap_UYVY (&dest, &src, videoscale->tmp_buf);
1101 case GST_VIDEO_FORMAT_Y800:
1102 case GST_VIDEO_FORMAT_GRAY8:
1104 vs_fill_borders_Y (&dest, black);
1106 case GST_VIDEO_SCALE_NEAREST:
1107 vs_image_scale_nearest_Y (&dest, &src, videoscale->tmp_buf);
1109 case GST_VIDEO_SCALE_BILINEAR:
1110 vs_image_scale_linear_Y (&dest, &src, videoscale->tmp_buf);
1112 case GST_VIDEO_SCALE_4TAP:
1113 vs_image_scale_4tap_Y (&dest, &src, videoscale->tmp_buf);
1119 case GST_VIDEO_FORMAT_GRAY16_LE:
1120 case GST_VIDEO_FORMAT_GRAY16_BE:
1121 case GST_VIDEO_FORMAT_Y16:
1123 vs_fill_borders_Y16 (&dest, 0);
1125 case GST_VIDEO_SCALE_NEAREST:
1126 vs_image_scale_nearest_Y16 (&dest, &src, videoscale->tmp_buf);
1128 case GST_VIDEO_SCALE_BILINEAR:
1129 vs_image_scale_linear_Y16 (&dest, &src, videoscale->tmp_buf);
1131 case GST_VIDEO_SCALE_4TAP:
1132 vs_image_scale_4tap_Y16 (&dest, &src, videoscale->tmp_buf);
1138 case GST_VIDEO_FORMAT_I420:
1139 case GST_VIDEO_FORMAT_YV12:
1140 case GST_VIDEO_FORMAT_Y444:
1141 case GST_VIDEO_FORMAT_Y42B:
1142 case GST_VIDEO_FORMAT_Y41B:
1144 vs_fill_borders_Y (&dest, black);
1145 vs_fill_borders_Y (&dest_u, black + 1);
1146 vs_fill_borders_Y (&dest_v, black + 2);
1149 case GST_VIDEO_SCALE_NEAREST:
1150 vs_image_scale_nearest_Y (&dest, &src, videoscale->tmp_buf);
1151 vs_image_scale_nearest_Y (&dest_u, &src_u, videoscale->tmp_buf);
1152 vs_image_scale_nearest_Y (&dest_v, &src_v, videoscale->tmp_buf);
1154 case GST_VIDEO_SCALE_BILINEAR:
1155 vs_image_scale_linear_Y (&dest, &src, videoscale->tmp_buf);
1156 vs_image_scale_linear_Y (&dest_u, &src_u, videoscale->tmp_buf);
1157 vs_image_scale_linear_Y (&dest_v, &src_v, videoscale->tmp_buf);
1159 case GST_VIDEO_SCALE_4TAP:
1160 vs_image_scale_4tap_Y (&dest, &src, videoscale->tmp_buf);
1161 vs_image_scale_4tap_Y (&dest_u, &src_u, videoscale->tmp_buf);
1162 vs_image_scale_4tap_Y (&dest_v, &src_v, videoscale->tmp_buf);
1168 case GST_VIDEO_FORMAT_RGB16:
1170 vs_fill_borders_RGB565 (&dest, black);
1172 case GST_VIDEO_SCALE_NEAREST:
1173 vs_image_scale_nearest_RGB565 (&dest, &src, videoscale->tmp_buf);
1175 case GST_VIDEO_SCALE_BILINEAR:
1176 vs_image_scale_linear_RGB565 (&dest, &src, videoscale->tmp_buf);
1178 case GST_VIDEO_SCALE_4TAP:
1179 vs_image_scale_4tap_RGB565 (&dest, &src, videoscale->tmp_buf);
1185 case GST_VIDEO_FORMAT_RGB15:
1187 vs_fill_borders_RGB555 (&dest, black);
1189 case GST_VIDEO_SCALE_NEAREST:
1190 vs_image_scale_nearest_RGB555 (&dest, &src, videoscale->tmp_buf);
1192 case GST_VIDEO_SCALE_BILINEAR:
1193 vs_image_scale_linear_RGB555 (&dest, &src, videoscale->tmp_buf);
1195 case GST_VIDEO_SCALE_4TAP:
1196 vs_image_scale_4tap_RGB555 (&dest, &src, videoscale->tmp_buf);
1206 GST_LOG_OBJECT (videoscale, "pushing buffer of %d bytes",
1207 GST_BUFFER_SIZE (out));
1214 GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1215 ("Unsupported format %d for scaling method %d",
1216 videoscale->format, method));
1217 return GST_FLOW_ERROR;
1221 GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1222 ("Unknown scaling method %d", videoscale->method));
1223 return GST_FLOW_ERROR;
1228 gst_video_scale_src_event (GstBaseTransform * trans, GstEvent * event)
1230 GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
1233 GstStructure *structure;
1235 GST_DEBUG_OBJECT (videoscale, "handling %s event",
1236 GST_EVENT_TYPE_NAME (event));
1238 switch (GST_EVENT_TYPE (event)) {
1239 case GST_EVENT_NAVIGATION:
1241 GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
1243 structure = (GstStructure *) gst_event_get_structure (event);
1244 if (gst_structure_get_double (structure, "pointer_x", &a)) {
1245 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
1246 a * videoscale->from_width / videoscale->to_width, NULL);
1248 if (gst_structure_get_double (structure, "pointer_y", &a)) {
1249 gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
1250 a * videoscale->from_height / videoscale->to_height, NULL);
1257 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
1263 plugin_init (GstPlugin * plugin)
1265 if (!gst_element_register (plugin, "videoscale", GST_RANK_NONE,
1266 GST_TYPE_VIDEO_SCALE))
1269 GST_DEBUG_CATEGORY_INIT (video_scale_debug, "videoscale", 0,
1270 "videoscale element");
1277 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1280 "Resizes video", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,