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)
59 #include <gst/video/video.h>
61 #include "gstvideoscale.h"
66 /* debug variable definition */
67 GST_DEBUG_CATEGORY (video_scale_debug);
69 #define DEFAULT_PROP_METHOD GST_VIDEO_SCALE_BILINEAR
78 #undef GST_VIDEO_SIZE_RANGE
79 #define GST_VIDEO_SIZE_RANGE "(int) [ 1, 32767]"
81 static GstStaticCaps gst_video_scale_format_caps[] = {
82 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA),
83 GST_STATIC_CAPS (GST_VIDEO_CAPS_ARGB),
84 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRA),
85 GST_STATIC_CAPS (GST_VIDEO_CAPS_ABGR),
86 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV")),
87 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx),
88 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB),
89 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx),
90 GST_STATIC_CAPS (GST_VIDEO_CAPS_xBGR),
91 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y444")),
92 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("v308")),
93 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB),
94 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGR),
95 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y42B")),
96 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("YUY2")),
97 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("YVYU")),
98 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("UYVY")),
99 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")),
100 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("YV12")),
101 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y41B")),
102 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB_16),
103 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB_15),
104 GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY16 ("BYTE_ORDER")),
105 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y16 ")),
106 GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY8),
107 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y800")),
108 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("Y8 ")),
109 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("GREY"))
112 #define GST_TYPE_VIDEO_SCALE_METHOD (gst_video_scale_method_get_type())
114 gst_video_scale_method_get_type (void)
116 static GType video_scale_method_type = 0;
118 static const GEnumValue video_scale_methods[] = {
119 {GST_VIDEO_SCALE_NEAREST, "Nearest Neighbour", "nearest-neighbour"},
120 {GST_VIDEO_SCALE_BILINEAR, "Bilinear", "bilinear"},
121 {GST_VIDEO_SCALE_4TAP, "4-tap", "4-tap"},
125 if (!video_scale_method_type) {
126 video_scale_method_type =
127 g_enum_register_static ("GstVideoScaleMethod", video_scale_methods);
129 return video_scale_method_type;
133 gst_video_scale_get_capslist (void)
135 static GstCaps *caps = NULL;
136 static volatile gsize inited = 0;
138 if (g_once_init_enter (&inited)) {
141 g_assert (caps == NULL);
143 caps = gst_caps_new_empty ();
144 for (i = 0; i < G_N_ELEMENTS (gst_video_scale_format_caps); i++)
145 gst_caps_append (caps,
146 gst_caps_make_writable
147 (gst_static_caps_get (&gst_video_scale_format_caps[i])));
148 g_once_init_leave (&inited, 1);
154 static GstPadTemplate *
155 gst_video_scale_src_template_factory (void)
157 return gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
158 gst_caps_ref (gst_video_scale_get_capslist ()));
161 static GstPadTemplate *
162 gst_video_scale_sink_template_factory (void)
164 return gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
165 gst_caps_ref (gst_video_scale_get_capslist ()));
169 static void gst_video_scale_finalize (GstVideoScale * videoscale);
170 static gboolean gst_video_scale_src_event (GstBaseTransform * trans,
173 /* base transform vmethods */
174 static GstCaps *gst_video_scale_transform_caps (GstBaseTransform * trans,
175 GstPadDirection direction, GstCaps * caps);
176 static gboolean gst_video_scale_set_caps (GstBaseTransform * trans,
177 GstCaps * in, GstCaps * out);
178 static gboolean gst_video_scale_get_unit_size (GstBaseTransform * trans,
179 GstCaps * caps, guint * size);
180 static GstFlowReturn gst_video_scale_transform (GstBaseTransform * trans,
181 GstBuffer * in, GstBuffer * out);
182 static void gst_video_scale_fixate_caps (GstBaseTransform * base,
183 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
185 static void gst_video_scale_set_property (GObject * object, guint prop_id,
186 const GValue * value, GParamSpec * pspec);
187 static void gst_video_scale_get_property (GObject * object, guint prop_id,
188 GValue * value, GParamSpec * pspec);
190 GST_BOILERPLATE (GstVideoScale, gst_video_scale, GstVideoFilter,
191 GST_TYPE_VIDEO_FILTER);
194 gst_video_scale_base_init (gpointer g_class)
196 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
198 gst_element_class_set_details_simple (element_class,
199 "Video scaler", "Filter/Effect/Video",
200 "Resizes video", "Wim Taymans <wim.taymans@chello.be>");
202 gst_element_class_add_pad_template (element_class,
203 gst_video_scale_sink_template_factory ());
204 gst_element_class_add_pad_template (element_class,
205 gst_video_scale_src_template_factory ());
209 gst_video_scale_class_init (GstVideoScaleClass * klass)
211 GObjectClass *gobject_class = (GObjectClass *) klass;
212 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
214 gobject_class->finalize = (GObjectFinalizeFunc) gst_video_scale_finalize;
215 gobject_class->set_property = gst_video_scale_set_property;
216 gobject_class->get_property = gst_video_scale_get_property;
218 g_object_class_install_property (gobject_class, PROP_METHOD,
219 g_param_spec_enum ("method", "method", "method",
220 GST_TYPE_VIDEO_SCALE_METHOD, DEFAULT_PROP_METHOD,
221 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 trans_class->transform_caps =
224 GST_DEBUG_FUNCPTR (gst_video_scale_transform_caps);
225 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_scale_set_caps);
226 trans_class->get_unit_size =
227 GST_DEBUG_FUNCPTR (gst_video_scale_get_unit_size);
228 trans_class->transform = GST_DEBUG_FUNCPTR (gst_video_scale_transform);
229 trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_video_scale_fixate_caps);
230 trans_class->src_event = GST_DEBUG_FUNCPTR (gst_video_scale_src_event);
234 gst_video_scale_init (GstVideoScale * videoscale, GstVideoScaleClass * klass)
236 videoscale->tmp_buf = NULL;
237 videoscale->method = DEFAULT_PROP_METHOD;
241 gst_video_scale_finalize (GstVideoScale * videoscale)
243 if (videoscale->tmp_buf)
244 g_free (videoscale->tmp_buf);
246 G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (videoscale));
250 gst_video_scale_set_property (GObject * object, guint prop_id,
251 const GValue * value, GParamSpec * pspec)
253 GstVideoScale *vscale = GST_VIDEO_SCALE (object);
257 GST_OBJECT_LOCK (vscale);
258 vscale->method = g_value_get_enum (value);
259 GST_OBJECT_UNLOCK (vscale);
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 gst_video_scale_get_property (GObject * object, guint prop_id, GValue * value,
271 GstVideoScale *vscale = GST_VIDEO_SCALE (object);
275 GST_OBJECT_LOCK (vscale);
276 g_value_set_enum (value, vscale->method);
277 GST_OBJECT_UNLOCK (vscale);
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 gst_video_scale_transform_caps (GstBaseTransform * trans,
287 GstPadDirection direction, GstCaps * caps)
290 GstStructure *structure;
292 /* this function is always called with a simple caps */
293 g_return_val_if_fail (GST_CAPS_IS_SIMPLE (caps), NULL);
295 ret = gst_caps_copy (caps);
296 structure = gst_structure_copy (gst_caps_get_structure (ret, 0));
298 gst_structure_set (structure,
299 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
300 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
302 /* if pixel aspect ratio, make a range of it */
303 if (gst_structure_has_field (structure, "pixel-aspect-ratio")) {
304 gst_structure_set (structure,
305 "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
308 gst_caps_merge_structure (ret, gst_structure_copy (structure));
309 gst_structure_free (structure);
311 GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
317 gst_video_scale_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
319 GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
321 gint from_dar_n, from_dar_d, to_dar_n, to_dar_d;
322 gint from_par_n, from_par_d, to_par_n, to_par_d;
325 gst_video_format_parse_caps (in, &videoscale->format,
326 &videoscale->from_width, &videoscale->from_height);
328 gst_video_format_parse_caps (out, NULL, &videoscale->to_width,
329 &videoscale->to_height);
330 ret &= gst_video_format_parse_caps_interlaced (in, &videoscale->interlaced);
335 videoscale->src_size = gst_video_format_get_size (videoscale->format,
336 videoscale->from_width, videoscale->from_height);
337 videoscale->dest_size = gst_video_format_get_size (videoscale->format,
338 videoscale->to_width, videoscale->to_height);
340 videoscale->src.width =
341 gst_video_format_get_component_width (videoscale->format, 0,
342 videoscale->from_width);
343 videoscale->src.height =
344 gst_video_format_get_component_height (videoscale->format, 0,
345 videoscale->from_height);
346 videoscale->src.stride =
347 gst_video_format_get_row_stride (videoscale->format, 0,
348 videoscale->from_width);
350 videoscale->dest.width =
351 gst_video_format_get_component_width (videoscale->format, 0,
352 videoscale->to_width);
353 videoscale->dest.height =
354 gst_video_format_get_component_height (videoscale->format, 0,
355 videoscale->to_height);
356 videoscale->dest.stride =
357 gst_video_format_get_row_stride (videoscale->format, 0,
358 videoscale->to_width);
360 if (videoscale->tmp_buf)
361 g_free (videoscale->tmp_buf);
363 videoscale->tmp_buf =
364 g_malloc (videoscale->dest.stride * 4 * (videoscale->interlaced ? 2 : 1));
366 if (!gst_video_parse_caps_pixel_aspect_ratio (in, &from_par_n, &from_par_d))
367 from_par_n = from_par_d = 1;
368 if (!gst_video_parse_caps_pixel_aspect_ratio (out, &to_par_n, &to_par_d))
369 to_par_n = to_par_d = 1;
371 if (!gst_util_fraction_multiply (videoscale->from_width,
372 videoscale->from_height, from_par_n, from_par_d, &from_dar_n,
374 from_dar_n = from_dar_d = -1;
377 if (!gst_util_fraction_multiply (videoscale->to_width, videoscale->to_height,
378 to_par_n, to_par_d, &to_dar_n, &to_dar_d)) {
379 to_dar_n = to_dar_d = -1;
382 if (to_dar_n != from_dar_n || to_dar_d != from_dar_d)
383 GST_WARNING_OBJECT (videoscale, "Can't keep DAR!");
385 gst_base_transform_set_passthrough (trans,
386 (videoscale->from_width == videoscale->to_width
387 && videoscale->from_height == videoscale->to_height));
389 GST_DEBUG_OBJECT (videoscale, "from=%dx%d (par=%d/%d dar=%d/%d), size %d "
390 "-> to=%dx%d (par=%d/%d dar=%d/%d), size %d",
391 videoscale->from_width, videoscale->from_height, from_par_n, from_par_d,
392 from_dar_n, from_dar_d, videoscale->src_size, videoscale->to_width,
393 videoscale->to_height, to_par_n, to_par_d, to_dar_n, to_dar_d,
394 videoscale->dest_size);
401 gst_video_scale_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
404 GstVideoFormat format;
407 if (!gst_video_format_parse_caps (caps, &format, &width, &height))
410 *size = gst_video_format_get_size (format, width, height);
416 gst_video_scale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
417 GstCaps * caps, GstCaps * othercaps)
419 GstStructure *ins, *outs;
420 const GValue *from_par, *to_par;
421 GValue fpar = { 0, }, tpar = {
424 g_return_if_fail (gst_caps_is_fixed (caps));
426 GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
427 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
429 ins = gst_caps_get_structure (caps, 0);
430 outs = gst_caps_get_structure (othercaps, 0);
432 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
433 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
435 /* If we're fixating from the sinkpad we always set the PAR and
436 * assume that missing PAR on the sinkpad means 1/1 and
437 * missing PAR on the srcpad means undefined
439 if (direction == GST_PAD_SINK) {
441 g_value_init (&fpar, GST_TYPE_FRACTION);
442 gst_value_set_fraction (&fpar, 1, 1);
446 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
447 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
450 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1,
451 G_MAXINT, G_MAXINT, 1, NULL);
455 g_value_init (&tpar, GST_TYPE_FRACTION);
456 gst_value_set_fraction (&tpar, 1, 1);
459 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
463 g_value_init (&fpar, GST_TYPE_FRACTION_RANGE);
464 gst_value_set_fraction_range_full (&fpar, 1, G_MAXINT, G_MAXINT, 1);
469 /* we have both PAR but they might not be fixated */
471 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
473 gint from_dar_n, from_dar_d;
476 /* from_par should be fixed */
477 g_return_if_fail (gst_value_is_fixed (from_par));
479 from_par_n = gst_value_get_fraction_numerator (from_par);
480 from_par_d = gst_value_get_fraction_denominator (from_par);
482 gst_structure_get_int (ins, "width", &from_w);
483 gst_structure_get_int (ins, "height", &from_h);
485 /* if both width and height are already fixed, we can't do anything
486 * about it anymore */
487 if (gst_structure_get_int (outs, "width", &w)
488 && gst_structure_get_int (outs, "height", &h)) {
491 GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
493 if (!gst_value_is_fixed (to_par)) {
494 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
495 from_par_n, from_par_d, w, h)) {
496 GST_DEBUG_OBJECT (base, "fixating to_par to %dx%d", n, d);
497 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
498 gst_structure_fixate_field_nearest_fraction (outs,
499 "pixel-aspect-ratio", n, d);
501 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
508 /* Calculate input DAR */
509 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
510 &from_dar_n, &from_dar_d)) {
511 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
512 ("Error calculating the output scaled size - integer overflow"));
516 GST_DEBUG_OBJECT (base, "Input DAR is %d/%d", from_dar_n, from_dar_d);
518 /* If either width or height are fixed there's not much we
519 * can do either except choosing a height or width and PAR
520 * that matches the DAR as good as possible
524 gint set_w, set_par_n, set_par_d;
526 GST_DEBUG_OBJECT (base, "height is fixed (%d)", h);
528 /* If the PAR is fixed too, there's not much to do
529 * except choosing the width that is nearest to the
530 * width with the same DAR */
531 if (gst_value_is_fixed (to_par)) {
532 to_par_n = gst_value_get_fraction_numerator (to_par);
533 to_par_d = gst_value_get_fraction_denominator (to_par);
535 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
537 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
538 to_par_n, &num, &den)) {
539 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
540 ("Error calculating the output scaled size - integer overflow"));
544 w = (guint) gst_util_uint64_scale_int (h, num, den);
545 gst_structure_fixate_field_nearest_int (outs, "width", w);
550 /* The PAR is not fixed and it's quite likely that we can set
551 * an arbitrary PAR. */
553 /* Check if we can keep the input width */
554 tmp = gst_structure_copy (outs);
555 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
556 gst_structure_get_int (tmp, "width", &set_w);
558 /* Might have failed but try to keep the DAR nonetheless by
559 * adjusting the PAR */
560 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
561 &to_par_n, &to_par_d)) {
562 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
563 ("Error calculating the output scaled size - integer overflow"));
564 gst_structure_free (tmp);
567 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
569 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
571 gst_structure_free (tmp);
573 /* Check if the adjusted PAR is accepted */
574 if (set_par_n == to_par_n && set_par_d == to_par_d) {
575 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
576 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
581 /* Otherwise scale the width to the new PAR and check if the
582 * adjusted with is accepted. If all that fails we can't keep
584 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
585 set_par_n, &num, &den)) {
586 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
587 ("Error calculating the output scaled size - integer overflow"));
591 w = (guint) gst_util_uint64_scale_int (h, num, den);
592 gst_structure_fixate_field_nearest_int (outs, "width", w);
593 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
594 set_par_n, set_par_d, NULL);
599 gint set_h, set_par_n, set_par_d;
601 GST_DEBUG_OBJECT (base, "width is fixed (%d)", w);
603 /* If the PAR is fixed too, there's not much to do
604 * except choosing the height that is nearest to the
605 * height with the same DAR */
606 if (gst_value_is_fixed (to_par)) {
607 to_par_n = gst_value_get_fraction_numerator (to_par);
608 to_par_d = gst_value_get_fraction_denominator (to_par);
610 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
612 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
613 to_par_n, &num, &den)) {
614 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
615 ("Error calculating the output scaled size - integer overflow"));
619 h = (guint) gst_util_uint64_scale_int (w, den, num);
620 gst_structure_fixate_field_nearest_int (outs, "height", h);
625 /* The PAR is not fixed and it's quite likely that we can set
626 * an arbitrary PAR. */
628 /* Check if we can keep the input height */
629 tmp = gst_structure_copy (outs);
630 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
631 gst_structure_get_int (tmp, "height", &set_h);
633 /* Might have failed but try to keep the DAR nonetheless by
634 * adjusting the PAR */
635 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
636 &to_par_n, &to_par_d)) {
637 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
638 ("Error calculating the output scaled size - integer overflow"));
639 gst_structure_free (tmp);
642 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
644 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
646 gst_structure_free (tmp);
648 /* Check if the adjusted PAR is accepted */
649 if (set_par_n == to_par_n && set_par_d == to_par_d) {
650 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
651 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
656 /* Otherwise scale the height to the new PAR and check if the
657 * adjusted with is accepted. If all that fails we can't keep
659 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
660 set_par_n, &num, &den)) {
661 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
662 ("Error calculating the output scaled size - integer overflow"));
666 h = (guint) gst_util_uint64_scale_int (w, den, num);
667 gst_structure_fixate_field_nearest_int (outs, "height", h);
668 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
669 set_par_n, set_par_d, NULL);
672 } else if (gst_value_is_fixed (to_par)) {
674 gint set_h, set_w, f_h, f_w;
676 to_par_n = gst_value_get_fraction_numerator (to_par);
677 to_par_d = gst_value_get_fraction_denominator (to_par);
679 /* Calculate scale factor for the PAR change */
680 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
681 to_par_d, &num, &den)) {
682 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
683 ("Error calculating the output scaled size - integer overflow"));
687 /* Try to keep the input height (because of interlacing) */
688 tmp = gst_structure_copy (outs);
689 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
690 gst_structure_get_int (tmp, "height", &set_h);
692 /* This might have failed but try to scale the width
693 * to keep the DAR nonetheless */
694 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
695 gst_structure_fixate_field_nearest_int (tmp, "width", w);
696 gst_structure_get_int (tmp, "width", &set_w);
697 gst_structure_free (tmp);
699 /* We kept the DAR and the height is nearest to the original height */
701 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
702 G_TYPE_INT, set_h, NULL);
709 /* If the former failed, try to keep the input width at least */
710 tmp = gst_structure_copy (outs);
711 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
712 gst_structure_get_int (tmp, "width", &set_w);
714 /* This might have failed but try to scale the width
715 * to keep the DAR nonetheless */
716 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
717 gst_structure_fixate_field_nearest_int (tmp, "height", h);
718 gst_structure_get_int (tmp, "height", &set_h);
719 gst_structure_free (tmp);
721 /* We kept the DAR and the width is nearest to the original width */
723 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
724 G_TYPE_INT, set_h, NULL);
728 /* If all this failed, keep the height that was nearest to the orignal
729 * height and the nearest possible width. This changes the DAR but
730 * there's not much else to do here.
732 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
737 gint set_h, set_w, set_par_n, set_par_d, tmp2;
739 /* width, height and PAR are not fixed but passthrough is not possible */
741 /* First try to keep the height and width as good as possible
743 tmp = gst_structure_copy (outs);
744 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
745 gst_structure_get_int (tmp, "height", &set_h);
746 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
747 gst_structure_get_int (tmp, "width", &set_w);
749 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
750 &to_par_n, &to_par_d)) {
751 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
752 ("Error calculating the output scaled size - integer overflow"));
756 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
758 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
760 gst_structure_free (tmp);
762 if (set_par_n == to_par_n && set_par_d == to_par_d) {
763 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
764 G_TYPE_INT, set_h, "pixel-aspect-ratio", GST_TYPE_FRACTION,
765 set_par_n, set_par_d, NULL);
769 /* Otherwise try to scale width to keep the DAR with the set
771 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
772 set_par_n, &num, &den)) {
773 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
774 ("Error calculating the output scaled size - integer overflow"));
778 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
779 tmp = gst_structure_copy (outs);
780 gst_structure_fixate_field_nearest_int (tmp, "width", w);
781 gst_structure_get_int (tmp, "width", &tmp2);
782 gst_structure_free (tmp);
785 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
786 G_TYPE_INT, set_h, "pixel-aspect-ratio", GST_TYPE_FRACTION,
787 set_par_n, set_par_d, NULL);
791 /* ... or try the same with the height */
792 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
793 tmp = gst_structure_copy (outs);
794 gst_structure_fixate_field_nearest_int (tmp, "height", h);
795 gst_structure_get_int (tmp, "height", &tmp2);
796 gst_structure_free (tmp);
799 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
800 G_TYPE_INT, tmp2, "pixel-aspect-ratio", GST_TYPE_FRACTION,
801 set_par_n, set_par_d, NULL);
805 /* If all fails we can't keep the DAR and take the nearest values
806 * for everything from the first try */
807 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
808 G_TYPE_INT, set_h, "pixel-aspect-ratio", GST_TYPE_FRACTION,
809 set_par_n, set_par_d, NULL);
814 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
816 if (from_par == &fpar)
817 g_value_unset (&fpar);
819 g_value_unset (&tpar);
823 gst_video_scale_prepare_image (gint format, GstBuffer * buf,
824 VSImage * img, VSImage * img_u, VSImage * img_v, gint step,
830 case GST_VIDEO_FORMAT_I420:
831 case GST_VIDEO_FORMAT_YV12:
832 case GST_VIDEO_FORMAT_Y444:
833 case GST_VIDEO_FORMAT_Y42B:
834 case GST_VIDEO_FORMAT_Y41B:
836 GST_BUFFER_DATA (buf) + gst_video_format_get_component_offset (format,
837 1, img->width, img->height);
839 GST_BUFFER_DATA (buf) + gst_video_format_get_component_offset (format,
840 2, img->width, img->height);
842 gst_video_format_get_component_height (format, 1, img->height);
844 gst_video_format_get_component_height (format, 2, img->height);
846 gst_video_format_get_component_width (format, 1, img->width);
848 gst_video_format_get_component_width (format, 2, img->width);
849 img_u->stride = gst_video_format_get_row_stride (format, 1, img->width);
850 img_v->stride = gst_video_format_get_row_stride (format, 2, img->width);
852 if (interlaced && step == 1) {
853 img_v->pixels += img_v->stride;
854 img_u->pixels += img_u->stride;
858 img_u->height = (img_u->height / 2) + ((step == 0
859 && img_u->height % 2 == 1) ? 1 : 0);
861 img_v->height = (img_v->height / 2) + ((step == 0
862 && img_v->height % 2 == 1) ? 1 : 0);
868 GST_BUFFER_DATA (buf) + GST_ROUND_UP_4 (img->width) * img->height;
869 img_u->height = img->height;
870 img_u->width = GST_ROUND_UP_2 (img->width) / 2;
871 img_u->stride = GST_ROUND_UP_8 (img->width) / 2;
872 memcpy (img_v, img_u, sizeof (*img_v));
874 GST_BUFFER_DATA (buf) + (GST_ROUND_UP_4 (img->width) +
875 (GST_ROUND_UP_8 (img->width) / 2)) * img->height;
877 if (interlaced && step == 1) {
878 img_v->pixels += img_v->stride;
879 img_u->pixels += img_u->stride;
883 img_u->height = (img_u->height / 2) + ((step == 0
884 && img_u->height % 2 == 1) ? 1 : 0);
886 img_v->height = (img_v->height / 2) + ((step == 0
887 && img_v->height % 2 == 1) ? 1 : 0);
898 gst_video_scale_transform (GstBaseTransform * trans, GstBuffer * in,
901 GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
902 GstFlowReturn ret = GST_FLOW_OK;
903 VSImage dest = videoscale->dest;
904 VSImage src = videoscale->src;
905 VSImage dest_u = { NULL, };
906 VSImage dest_v = { NULL, };
907 VSImage src_u = { NULL, };
908 VSImage src_v = { NULL, };
911 gboolean interlaced = videoscale->interlaced;
913 GST_OBJECT_LOCK (videoscale);
914 method = videoscale->method;
915 GST_OBJECT_UNLOCK (videoscale);
917 src.pixels = GST_BUFFER_DATA (in);
918 dest.pixels = GST_BUFFER_DATA (out);
920 /* For interlaced content we have to run two times with half height
921 * and doubled stride */
929 if (src.height < 4 && method == GST_VIDEO_SCALE_4TAP)
930 method = GST_VIDEO_SCALE_BILINEAR;
932 for (step = 0; step < (interlaced ? 2 : 1); step++) {
933 gst_video_scale_prepare_image (videoscale->format, in, &videoscale->src,
934 &src_u, &src_v, step, interlaced);
935 gst_video_scale_prepare_image (videoscale->format, out, &videoscale->dest,
936 &dest_u, &dest_v, step, interlaced);
938 if (step == 0 && interlaced) {
939 if (videoscale->from_height % 2 == 1) {
943 if (videoscale->to_height % 2 == 1) {
946 } else if (step == 1 && interlaced) {
947 if (videoscale->from_height % 2 == 1) {
951 if (videoscale->to_height % 2 == 1) {
954 src.pixels += (src.stride / 2);
955 dest.pixels += (dest.stride / 2);
959 case GST_VIDEO_SCALE_NEAREST:
960 GST_LOG_OBJECT (videoscale, "doing nearest scaling");
961 switch (videoscale->format) {
962 case GST_VIDEO_FORMAT_RGBx:
963 case GST_VIDEO_FORMAT_xRGB:
964 case GST_VIDEO_FORMAT_BGRx:
965 case GST_VIDEO_FORMAT_xBGR:
966 case GST_VIDEO_FORMAT_RGBA:
967 case GST_VIDEO_FORMAT_ARGB:
968 case GST_VIDEO_FORMAT_BGRA:
969 case GST_VIDEO_FORMAT_ABGR:
970 case GST_VIDEO_FORMAT_AYUV:
971 vs_image_scale_nearest_RGBA (&dest, &src, videoscale->tmp_buf);
973 case GST_VIDEO_FORMAT_RGB:
974 case GST_VIDEO_FORMAT_BGR:
975 case GST_VIDEO_FORMAT_v308:
976 vs_image_scale_nearest_RGB (&dest, &src, videoscale->tmp_buf);
978 case GST_VIDEO_FORMAT_YUY2:
979 case GST_VIDEO_FORMAT_YVYU:
980 vs_image_scale_nearest_YUYV (&dest, &src, videoscale->tmp_buf);
982 case GST_VIDEO_FORMAT_UYVY:
983 vs_image_scale_nearest_UYVY (&dest, &src, videoscale->tmp_buf);
985 case GST_VIDEO_FORMAT_Y800:
986 case GST_VIDEO_FORMAT_GRAY8:
987 vs_image_scale_nearest_Y (&dest, &src, videoscale->tmp_buf);
989 case GST_VIDEO_FORMAT_GRAY16_LE:
990 case GST_VIDEO_FORMAT_GRAY16_BE:
991 case GST_VIDEO_FORMAT_Y16:
992 vs_image_scale_nearest_Y16 (&dest, &src, videoscale->tmp_buf);
994 case GST_VIDEO_FORMAT_I420:
995 case GST_VIDEO_FORMAT_YV12:
996 case GST_VIDEO_FORMAT_Y444:
997 case GST_VIDEO_FORMAT_Y42B:
998 case GST_VIDEO_FORMAT_Y41B:
999 vs_image_scale_nearest_Y (&dest, &src, videoscale->tmp_buf);
1000 vs_image_scale_nearest_Y (&dest_u, &src_u, videoscale->tmp_buf);
1001 vs_image_scale_nearest_Y (&dest_v, &src_v, videoscale->tmp_buf);
1003 case GST_VIDEO_FORMAT_RGB16:
1004 vs_image_scale_nearest_RGB565 (&dest, &src, videoscale->tmp_buf);
1006 case GST_VIDEO_FORMAT_RGB15:
1007 vs_image_scale_nearest_RGB555 (&dest, &src, videoscale->tmp_buf);
1013 case GST_VIDEO_SCALE_BILINEAR:
1014 GST_LOG_OBJECT (videoscale, "doing bilinear scaling");
1015 switch (videoscale->format) {
1016 case GST_VIDEO_FORMAT_RGBx:
1017 case GST_VIDEO_FORMAT_xRGB:
1018 case GST_VIDEO_FORMAT_BGRx:
1019 case GST_VIDEO_FORMAT_xBGR:
1020 case GST_VIDEO_FORMAT_RGBA:
1021 case GST_VIDEO_FORMAT_ARGB:
1022 case GST_VIDEO_FORMAT_BGRA:
1023 case GST_VIDEO_FORMAT_ABGR:
1024 case GST_VIDEO_FORMAT_AYUV:
1025 vs_image_scale_linear_RGBA (&dest, &src, videoscale->tmp_buf);
1027 case GST_VIDEO_FORMAT_RGB:
1028 case GST_VIDEO_FORMAT_BGR:
1029 case GST_VIDEO_FORMAT_v308:
1030 vs_image_scale_linear_RGB (&dest, &src, videoscale->tmp_buf);
1032 case GST_VIDEO_FORMAT_YUY2:
1033 case GST_VIDEO_FORMAT_YVYU:
1034 vs_image_scale_linear_YUYV (&dest, &src, videoscale->tmp_buf);
1036 case GST_VIDEO_FORMAT_UYVY:
1037 vs_image_scale_linear_UYVY (&dest, &src, videoscale->tmp_buf);
1039 case GST_VIDEO_FORMAT_Y800:
1040 case GST_VIDEO_FORMAT_GRAY8:
1041 vs_image_scale_linear_Y (&dest, &src, videoscale->tmp_buf);
1043 case GST_VIDEO_FORMAT_GRAY16_LE:
1044 case GST_VIDEO_FORMAT_GRAY16_BE:
1045 vs_image_scale_linear_Y16 (&dest, &src, videoscale->tmp_buf);
1047 case GST_VIDEO_FORMAT_I420:
1048 case GST_VIDEO_FORMAT_YV12:
1049 case GST_VIDEO_FORMAT_Y444:
1050 case GST_VIDEO_FORMAT_Y42B:
1051 case GST_VIDEO_FORMAT_Y41B:
1052 vs_image_scale_linear_Y (&dest, &src, videoscale->tmp_buf);
1053 vs_image_scale_linear_Y (&dest_u, &src_u, videoscale->tmp_buf);
1054 vs_image_scale_linear_Y (&dest_v, &src_v, videoscale->tmp_buf);
1056 case GST_VIDEO_FORMAT_RGB16:
1057 vs_image_scale_linear_RGB565 (&dest, &src, videoscale->tmp_buf);
1059 case GST_VIDEO_FORMAT_RGB15:
1060 vs_image_scale_linear_RGB555 (&dest, &src, videoscale->tmp_buf);
1066 case GST_VIDEO_SCALE_4TAP:
1067 GST_LOG_OBJECT (videoscale, "doing 4tap scaling");
1069 switch (videoscale->format) {
1070 case GST_VIDEO_FORMAT_RGBx:
1071 case GST_VIDEO_FORMAT_xRGB:
1072 case GST_VIDEO_FORMAT_BGRx:
1073 case GST_VIDEO_FORMAT_xBGR:
1074 case GST_VIDEO_FORMAT_RGBA:
1075 case GST_VIDEO_FORMAT_ARGB:
1076 case GST_VIDEO_FORMAT_BGRA:
1077 case GST_VIDEO_FORMAT_ABGR:
1078 case GST_VIDEO_FORMAT_AYUV:
1079 vs_image_scale_4tap_RGBA (&dest, &src, videoscale->tmp_buf);
1081 case GST_VIDEO_FORMAT_RGB:
1082 case GST_VIDEO_FORMAT_BGR:
1083 case GST_VIDEO_FORMAT_v308:
1084 vs_image_scale_4tap_RGB (&dest, &src, videoscale->tmp_buf);
1086 case GST_VIDEO_FORMAT_YUY2:
1087 case GST_VIDEO_FORMAT_YVYU:
1088 vs_image_scale_4tap_YUYV (&dest, &src, videoscale->tmp_buf);
1090 case GST_VIDEO_FORMAT_UYVY:
1091 vs_image_scale_4tap_UYVY (&dest, &src, videoscale->tmp_buf);
1093 case GST_VIDEO_FORMAT_Y800:
1094 case GST_VIDEO_FORMAT_GRAY8:
1095 vs_image_scale_4tap_Y (&dest, &src, videoscale->tmp_buf);
1097 case GST_VIDEO_FORMAT_GRAY16_LE:
1098 case GST_VIDEO_FORMAT_GRAY16_BE:
1099 vs_image_scale_4tap_Y16 (&dest, &src, videoscale->tmp_buf);
1101 case GST_VIDEO_FORMAT_I420:
1102 case GST_VIDEO_FORMAT_YV12:
1103 case GST_VIDEO_FORMAT_Y444:
1104 case GST_VIDEO_FORMAT_Y42B:
1105 case GST_VIDEO_FORMAT_Y41B:
1106 vs_image_scale_4tap_Y (&dest, &src, videoscale->tmp_buf);
1107 vs_image_scale_4tap_Y (&dest_u, &src_u, videoscale->tmp_buf);
1108 vs_image_scale_4tap_Y (&dest_v, &src_v, videoscale->tmp_buf);
1110 case GST_VIDEO_FORMAT_RGB16:
1111 vs_image_scale_4tap_RGB565 (&dest, &src, videoscale->tmp_buf);
1113 case GST_VIDEO_FORMAT_RGB15:
1114 vs_image_scale_4tap_RGB555 (&dest, &src, videoscale->tmp_buf);
1126 GST_LOG_OBJECT (videoscale, "pushing buffer of %d bytes",
1127 GST_BUFFER_SIZE (out));
1134 GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1135 ("Unsupported format %d for scaling method %d",
1136 videoscale->format, method));
1137 return GST_FLOW_ERROR;
1141 GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1142 ("Unknown scaling method %d", videoscale->method));
1143 return GST_FLOW_ERROR;
1148 gst_video_scale_src_event (GstBaseTransform * trans, GstEvent * event)
1150 GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
1153 GstStructure *structure;
1155 GST_DEBUG_OBJECT (videoscale, "handling %s event",
1156 GST_EVENT_TYPE_NAME (event));
1158 switch (GST_EVENT_TYPE (event)) {
1159 case GST_EVENT_NAVIGATION:
1161 GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
1163 structure = (GstStructure *) gst_event_get_structure (event);
1164 if (gst_structure_get_double (structure, "pointer_x", &a)) {
1165 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
1166 a * videoscale->from_width / videoscale->to_width, NULL);
1168 if (gst_structure_get_double (structure, "pointer_y", &a)) {
1169 gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
1170 a * videoscale->from_height / videoscale->to_height, NULL);
1177 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
1183 plugin_init (GstPlugin * plugin)
1185 if (!gst_element_register (plugin, "videoscale", GST_RANK_NONE,
1186 GST_TYPE_VIDEO_SCALE))
1189 GST_DEBUG_CATEGORY_INIT (video_scale_debug, "videoscale", 0,
1190 "videoscale element");
1197 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1200 "Resizes video", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,