2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2005-2012 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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-videoscale
23 * @see_also: videorate, videoconvert
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 ! 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.
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
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/gstvideometa.h>
80 #include <gst/video/gstvideopool.h>
82 #include "gstvideoscale.h"
83 #include "gstvideoscaleorc.h"
86 #include "vs_fill_borders.h"
88 /* debug variable definition */
89 GST_DEBUG_CATEGORY (video_scale_debug);
90 GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE);
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
112 #undef GST_VIDEO_SIZE_RANGE
113 #define GST_VIDEO_SIZE_RANGE "(int) [ 1, 32767]"
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
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 } "
133 static GstStaticCaps gst_video_scale_format_caps =
134 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS));
136 #define GST_TYPE_VIDEO_SCALE_METHOD (gst_video_scale_method_get_type())
138 gst_video_scale_method_get_type (void)
140 static GType video_scale_method_type = 0;
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"},
150 if (!video_scale_method_type) {
151 video_scale_method_type =
152 g_enum_register_static ("GstVideoScaleMethod", video_scale_methods);
154 return video_scale_method_type;
158 gst_video_scale_get_capslist (void)
160 static GstCaps *caps = NULL;
161 static volatile gsize inited = 0;
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);
170 static GstPadTemplate *
171 gst_video_scale_src_template_factory (void)
173 return gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
174 gst_video_scale_get_capslist ());
177 static GstPadTemplate *
178 gst_video_scale_sink_template_factory (void)
180 return gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
181 gst_video_scale_get_capslist ());
185 static void gst_video_scale_finalize (GstVideoScale * videoscale);
186 static gboolean gst_video_scale_src_event (GstBaseTransform * trans,
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);
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);
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);
206 static GstFlowReturn do_scale (GstVideoFilter * filter, VSImage dest[4],
209 #define gst_video_scale_parent_class parent_class
210 G_DEFINE_TYPE (GstVideoScale, gst_video_scale, GST_TYPE_VIDEO_FILTER);
213 gst_video_scale_class_init (GstVideoScaleClass * klass)
215 GObjectClass *gobject_class = (GObjectClass *) klass;
216 GstElementClass *element_class = (GstElementClass *) klass;
217 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
218 GstVideoFilterClass *filter_class = (GstVideoFilterClass *) klass;
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;
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));
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));
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));
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));
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)",
249 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
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));
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));
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>");
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 ());
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);
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);
285 gst_video_scale_init (GstVideoScale * videoscale)
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;
298 gst_video_scale_finalize (GstVideoScale * videoscale)
300 if (videoscale->tmp_buf)
301 g_free (videoscale->tmp_buf);
303 G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (videoscale));
307 gst_video_scale_set_property (GObject * object, guint prop_id,
308 const GValue * value, GParamSpec * pspec)
310 GstVideoScale *vscale = GST_VIDEO_SCALE (object);
314 GST_OBJECT_LOCK (vscale);
315 vscale->method = g_value_get_enum (value);
316 GST_OBJECT_UNLOCK (vscale);
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));
325 GST_OBJECT_LOCK (vscale);
326 vscale->sharpness = g_value_get_double (value);
327 GST_OBJECT_UNLOCK (vscale);
330 GST_OBJECT_LOCK (vscale);
331 vscale->sharpen = g_value_get_double (value);
332 GST_OBJECT_UNLOCK (vscale);
335 GST_OBJECT_LOCK (vscale);
336 vscale->dither = g_value_get_boolean (value);
337 GST_OBJECT_UNLOCK (vscale);
340 GST_OBJECT_LOCK (vscale);
341 vscale->submethod = g_value_get_int (value);
342 GST_OBJECT_UNLOCK (vscale);
345 GST_OBJECT_LOCK (vscale);
346 vscale->envelope = g_value_get_double (value);
347 GST_OBJECT_UNLOCK (vscale);
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 gst_video_scale_get_property (GObject * object, guint prop_id, GValue * value,
359 GstVideoScale *vscale = GST_VIDEO_SCALE (object);
363 GST_OBJECT_LOCK (vscale);
364 g_value_set_enum (value, vscale->method);
365 GST_OBJECT_UNLOCK (vscale);
367 case PROP_ADD_BORDERS:
368 GST_OBJECT_LOCK (vscale);
369 g_value_set_boolean (value, vscale->add_borders);
370 GST_OBJECT_UNLOCK (vscale);
373 GST_OBJECT_LOCK (vscale);
374 g_value_set_double (value, vscale->sharpness);
375 GST_OBJECT_UNLOCK (vscale);
378 GST_OBJECT_LOCK (vscale);
379 g_value_set_double (value, vscale->sharpen);
380 GST_OBJECT_UNLOCK (vscale);
383 GST_OBJECT_LOCK (vscale);
384 g_value_set_boolean (value, vscale->dither);
385 GST_OBJECT_UNLOCK (vscale);
388 GST_OBJECT_LOCK (vscale);
389 g_value_set_int (value, vscale->submethod);
390 GST_OBJECT_UNLOCK (vscale);
393 GST_OBJECT_LOCK (vscale);
394 g_value_set_double (value, vscale->envelope);
395 GST_OBJECT_UNLOCK (vscale);
398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
404 get_formats_filter (GstVideoScaleMethod method)
407 case GST_VIDEO_SCALE_NEAREST:
408 case GST_VIDEO_SCALE_BILINEAR:
410 case GST_VIDEO_SCALE_4TAP:
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);
421 case GST_VIDEO_SCALE_LANCZOS:
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);
431 g_assert_not_reached ();
438 gst_video_scale_transform_caps (GstBaseTransform * trans,
439 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
441 GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
442 GstVideoScaleMethod method;
443 GstCaps *ret, *mfilter;
444 GstStructure *structure;
447 GST_DEBUG_OBJECT (trans,
448 "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps,
449 (direction == GST_PAD_SINK) ? "sink" : "src");
451 GST_OBJECT_LOCK (videoscale);
452 method = videoscale->method;
453 GST_OBJECT_UNLOCK (videoscale);
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);
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);
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))
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);
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);
484 gst_caps_append_structure (ret, structure);
488 GstCaps *intersection;
491 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
492 gst_caps_unref (ret);
496 gst_caps_unref (caps);
497 GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
503 gst_video_scale_set_info (GstVideoFilter * filter, GstCaps * in,
504 GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
506 GstVideoScale *videoscale = GST_VIDEO_SCALE (filter);
507 gint from_dar_n, from_dar_d, to_dar_n, to_dar_d;
509 if (!gst_util_fraction_multiply (in_info->width,
510 in_info->height, in_info->par_n, in_info->par_d, &from_dar_n,
512 from_dar_n = from_dar_d = -1;
515 if (!gst_util_fraction_multiply (out_info->width,
516 out_info->height, out_info->par_n, out_info->par_d, &to_dar_n,
518 to_dar_n = to_dar_d = -1;
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;
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;
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;
540 GST_WARNING_OBJECT (videoscale, "Can't calculate borders");
543 GST_WARNING_OBJECT (videoscale, "Can't keep DAR!");
547 if (videoscale->tmp_buf)
548 g_free (videoscale->tmp_buf);
549 videoscale->tmp_buf = g_malloc (out_info->width * sizeof (guint64) * 4);
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);
555 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, filter, "setup videoscaling");
556 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), FALSE);
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);
571 gst_video_scale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
572 GstCaps * caps, GstCaps * othercaps)
574 GstStructure *ins, *outs;
575 const GValue *from_par, *to_par;
576 GValue fpar = { 0, }, tpar = {
579 othercaps = gst_caps_truncate (othercaps);
580 othercaps = gst_caps_make_writable (othercaps);
582 GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
583 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
585 ins = gst_caps_get_structure (caps, 0);
586 outs = gst_caps_get_structure (othercaps, 0);
588 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
589 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
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
595 if (direction == GST_PAD_SINK) {
597 g_value_init (&fpar, GST_TYPE_FRACTION);
598 gst_value_set_fraction (&fpar, 1, 1);
602 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
603 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
608 g_value_init (&tpar, GST_TYPE_FRACTION);
609 gst_value_set_fraction (&tpar, 1, 1);
612 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
616 g_value_init (&fpar, GST_TYPE_FRACTION);
617 gst_value_set_fraction (&fpar, 1, 1);
622 /* we have both PAR but they might not be fixated */
624 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
626 gint from_dar_n, from_dar_d;
629 /* from_par should be fixed */
630 g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
632 from_par_n = gst_value_get_fraction_numerator (from_par);
633 from_par_d = gst_value_get_fraction_denominator (from_par);
635 gst_structure_get_int (ins, "width", &from_w);
636 gst_structure_get_int (ins, "height", &from_h);
638 gst_structure_get_int (outs, "width", &w);
639 gst_structure_get_int (outs, "height", &h);
641 /* if both width and height are already fixed, we can't do anything
642 * about it anymore */
646 GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
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);
656 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
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"));
671 GST_DEBUG_OBJECT (base, "Input DAR is %d/%d", from_dar_n, from_dar_d);
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
679 gint set_w, set_par_n, set_par_d;
681 GST_DEBUG_OBJECT (base, "height is fixed (%d)", h);
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);
690 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
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"));
699 w = (guint) gst_util_uint64_scale_int (h, num, den);
700 gst_structure_fixate_field_nearest_int (outs, "width", w);
705 /* The PAR is not fixed and it's quite likely that we can set
706 * an arbitrary PAR. */
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);
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);
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",
727 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
729 gst_structure_free (tmp);
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,
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
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"));
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);
761 gint set_h, set_par_n, set_par_d;
763 GST_DEBUG_OBJECT (base, "width is fixed (%d)", w);
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);
772 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
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"));
781 h = (guint) gst_util_uint64_scale_int (w, den, num);
782 gst_structure_fixate_field_nearest_int (outs, "height", h);
787 /* The PAR is not fixed and it's quite likely that we can set
788 * an arbitrary PAR. */
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);
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);
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",
808 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
810 gst_structure_free (tmp);
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,
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
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"));
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);
840 } else if (gst_value_is_fixed (to_par)) {
842 gint set_h, set_w, f_h, f_w;
844 to_par_n = gst_value_get_fraction_numerator (to_par);
845 to_par_d = gst_value_get_fraction_denominator (to_par);
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"));
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);
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);
867 /* We kept the DAR and the height is nearest to the original height */
869 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
870 G_TYPE_INT, set_h, NULL);
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);
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);
889 /* We kept the DAR and the width is nearest to the original width */
891 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
892 G_TYPE_INT, set_h, NULL);
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.
900 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
905 gint set_h, set_w, set_par_n, set_par_d, tmp2;
907 /* width, height and PAR are not fixed but passthrough is not possible */
909 /* First try to keep the height and width as good as possible
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);
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"));
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",
928 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
930 gst_structure_free (tmp);
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);
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);
943 /* Otherwise try to scale width to keep the DAR with the set
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"));
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);
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);
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);
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);
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);
997 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
999 if (from_par == &fpar)
1000 g_value_unset (&fpar);
1001 if (to_par == &tpar)
1002 g_value_unset (&tpar);
1008 gst_video_scale_setup_vs_image (VSImage * image, GstVideoFrame * frame,
1009 gint component, gint b_w, gint b_h, gboolean interlaced, gint field)
1011 GstVideoFormat format;
1014 format = GST_VIDEO_FRAME_FORMAT (frame);
1015 width = GST_VIDEO_FRAME_WIDTH (frame);
1016 height = GST_VIDEO_FRAME_HEIGHT (frame);
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));
1026 image->real_height /= 2;
1030 image->border_top = (image->real_height - image->height) / 2;
1031 image->border_bottom = image->real_height - image->height - image->border_top;
1033 if (format == GST_VIDEO_FORMAT_YUY2 || format == GST_VIDEO_FORMAT_YVYU
1034 || format == GST_VIDEO_FORMAT_UYVY) {
1035 g_assert (component == 0);
1037 image->border_left = (image->real_width - image->width) / 2;
1039 if (image->border_left % 2 == 1)
1040 image->border_left--;
1041 image->border_right = image->real_width - image->width - image->border_left;
1043 image->border_left = (image->real_width - image->width) / 2;
1044 image->border_right = image->real_width - image->width - image->border_left;
1047 image->real_pixels = GST_VIDEO_FRAME_PLANE_DATA (frame, component);
1048 image->stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, component);
1052 image->real_pixels += image->stride;
1057 image->real_pixels + image->border_top * image->stride +
1058 image->border_left * GST_VIDEO_FRAME_COMP_PSTRIDE (frame, component);
1062 static const guint8 *
1063 _get_black_for_format (GstVideoFormat format)
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 */
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:
1084 case GST_VIDEO_FORMAT_RGBA:
1085 case GST_VIDEO_FORMAT_BGRA:
1086 case GST_VIDEO_FORMAT_RGBx:
1087 case GST_VIDEO_FORMAT_BGRx:
1089 case GST_VIDEO_FORMAT_AYUV:
1090 case GST_VIDEO_FORMAT_AYUV64:
1092 case GST_VIDEO_FORMAT_RGB:
1093 case GST_VIDEO_FORMAT_BGR:
1095 case GST_VIDEO_FORMAT_v308:
1097 case GST_VIDEO_FORMAT_YUY2:
1098 case GST_VIDEO_FORMAT_YVYU:
1100 case GST_VIDEO_FORMAT_UYVY:
1102 case GST_VIDEO_FORMAT_GRAY8:
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:
1122 static GstFlowReturn
1123 gst_video_scale_transform_frame (GstVideoFilter * filter,
1124 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
1126 GstVideoScale *videoscale = GST_VIDEO_SCALE (filter);
1127 GstFlowReturn ret = GST_FLOW_OK;
1128 VSImage dest[4] = { {NULL,}, };
1129 VSImage src[4] = { {NULL,}, };
1131 gboolean interlaced;
1133 interlaced = GST_VIDEO_FRAME_IS_INTERLACED (in_frame);
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);
1140 ret = do_scale (filter, dest, src);
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,
1146 gst_video_scale_setup_vs_image (&dest[i], out_frame, i,
1147 videoscale->borders_w, videoscale->borders_h, interlaced, 1);
1149 ret = do_scale (filter, dest, src);
1154 static GstFlowReturn
1155 do_scale (GstVideoFilter * filter, VSImage dest[4], VSImage src[4])
1157 GstVideoScale *videoscale = GST_VIDEO_SCALE (filter);
1158 GstFlowReturn ret = GST_FLOW_OK;
1160 const guint8 *black;
1161 GstVideoFormat format;
1162 gboolean add_borders;
1164 GST_OBJECT_LOCK (videoscale);
1165 method = videoscale->method;
1166 add_borders = videoscale->add_borders;
1167 GST_OBJECT_UNLOCK (videoscale);
1169 format = GST_VIDEO_INFO_FORMAT (&filter->in_info);
1170 black = _get_black_for_format (format);
1172 if (filter->in_info.width == 1) {
1173 method = GST_VIDEO_SCALE_NEAREST;
1175 if (method == GST_VIDEO_SCALE_4TAP &&
1176 (filter->in_info.width < 4 || filter->in_info.height < 4)) {
1177 method = GST_VIDEO_SCALE_BILINEAR;
1180 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, filter,
1181 "doing videoscale format %s", GST_VIDEO_INFO_NAME (&filter->in_info));
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:
1194 vs_fill_borders_RGBA (&dest[0], black);
1196 case GST_VIDEO_SCALE_NEAREST:
1197 vs_image_scale_nearest_RGBA (&dest[0], &src[0], videoscale->tmp_buf);
1199 case GST_VIDEO_SCALE_BILINEAR:
1200 vs_image_scale_linear_RGBA (&dest[0], &src[0], videoscale->tmp_buf);
1202 case GST_VIDEO_SCALE_4TAP:
1203 vs_image_scale_4tap_RGBA (&dest[0], &src[0], videoscale->tmp_buf);
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);
1214 case GST_VIDEO_FORMAT_ARGB64:
1215 case GST_VIDEO_FORMAT_AYUV64:
1217 vs_fill_borders_AYUV64 (&dest[0], black);
1219 case GST_VIDEO_SCALE_NEAREST:
1220 vs_image_scale_nearest_AYUV64 (&dest[0], &src[0],
1221 videoscale->tmp_buf);
1223 case GST_VIDEO_SCALE_BILINEAR:
1224 vs_image_scale_linear_AYUV64 (&dest[0], &src[0], videoscale->tmp_buf);
1226 case GST_VIDEO_SCALE_4TAP:
1227 vs_image_scale_4tap_AYUV64 (&dest[0], &src[0], videoscale->tmp_buf);
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);
1238 case GST_VIDEO_FORMAT_RGB:
1239 case GST_VIDEO_FORMAT_BGR:
1240 case GST_VIDEO_FORMAT_v308:
1242 vs_fill_borders_RGB (&dest[0], black);
1244 case GST_VIDEO_SCALE_NEAREST:
1245 vs_image_scale_nearest_RGB (&dest[0], &src[0], videoscale->tmp_buf);
1247 case GST_VIDEO_SCALE_BILINEAR:
1248 vs_image_scale_linear_RGB (&dest[0], &src[0], videoscale->tmp_buf);
1250 case GST_VIDEO_SCALE_4TAP:
1251 vs_image_scale_4tap_RGB (&dest[0], &src[0], videoscale->tmp_buf);
1257 case GST_VIDEO_FORMAT_YUY2:
1258 case GST_VIDEO_FORMAT_YVYU:
1260 vs_fill_borders_YUYV (&dest[0], black);
1262 case GST_VIDEO_SCALE_NEAREST:
1263 vs_image_scale_nearest_YUYV (&dest[0], &src[0], videoscale->tmp_buf);
1265 case GST_VIDEO_SCALE_BILINEAR:
1266 vs_image_scale_linear_YUYV (&dest[0], &src[0], videoscale->tmp_buf);
1268 case GST_VIDEO_SCALE_4TAP:
1269 vs_image_scale_4tap_YUYV (&dest[0], &src[0], videoscale->tmp_buf);
1275 case GST_VIDEO_FORMAT_UYVY:
1277 vs_fill_borders_UYVY (&dest[0], black);
1279 case GST_VIDEO_SCALE_NEAREST:
1280 vs_image_scale_nearest_UYVY (&dest[0], &src[0], videoscale->tmp_buf);
1282 case GST_VIDEO_SCALE_BILINEAR:
1283 vs_image_scale_linear_UYVY (&dest[0], &src[0], videoscale->tmp_buf);
1285 case GST_VIDEO_SCALE_4TAP:
1286 vs_image_scale_4tap_UYVY (&dest[0], &src[0], videoscale->tmp_buf);
1292 case GST_VIDEO_FORMAT_GRAY8:
1294 vs_fill_borders_Y (&dest[0], black);
1296 case GST_VIDEO_SCALE_NEAREST:
1297 vs_image_scale_nearest_Y (&dest[0], &src[0], videoscale->tmp_buf);
1299 case GST_VIDEO_SCALE_BILINEAR:
1300 vs_image_scale_linear_Y (&dest[0], &src[0], videoscale->tmp_buf);
1302 case GST_VIDEO_SCALE_4TAP:
1303 vs_image_scale_4tap_Y (&dest[0], &src[0], videoscale->tmp_buf);
1309 case GST_VIDEO_FORMAT_GRAY16_LE:
1310 case GST_VIDEO_FORMAT_GRAY16_BE:
1312 vs_fill_borders_Y16 (&dest[0], 0);
1314 case GST_VIDEO_SCALE_NEAREST:
1315 vs_image_scale_nearest_Y16 (&dest[0], &src[0], videoscale->tmp_buf);
1317 case GST_VIDEO_SCALE_BILINEAR:
1318 vs_image_scale_linear_Y16 (&dest[0], &src[0], videoscale->tmp_buf);
1320 case GST_VIDEO_SCALE_4TAP:
1321 vs_image_scale_4tap_Y16 (&dest[0], &src[0], videoscale->tmp_buf);
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:
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);
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);
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);
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);
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);
1368 case GST_VIDEO_FORMAT_NV12:
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);
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);
1382 case GST_VIDEO_FORMAT_RGB16:
1384 vs_fill_borders_RGB565 (&dest[0], black);
1386 case GST_VIDEO_SCALE_NEAREST:
1387 vs_image_scale_nearest_RGB565 (&dest[0], &src[0],
1388 videoscale->tmp_buf);
1390 case GST_VIDEO_SCALE_BILINEAR:
1391 vs_image_scale_linear_RGB565 (&dest[0], &src[0], videoscale->tmp_buf);
1393 case GST_VIDEO_SCALE_4TAP:
1394 vs_image_scale_4tap_RGB565 (&dest[0], &src[0], videoscale->tmp_buf);
1400 case GST_VIDEO_FORMAT_RGB15:
1402 vs_fill_borders_RGB555 (&dest[0], black);
1404 case GST_VIDEO_SCALE_NEAREST:
1405 vs_image_scale_nearest_RGB555 (&dest[0], &src[0],
1406 videoscale->tmp_buf);
1408 case GST_VIDEO_SCALE_BILINEAR:
1409 vs_image_scale_linear_RGB555 (&dest[0], &src[0], videoscale->tmp_buf);
1411 case GST_VIDEO_SCALE_4TAP:
1412 vs_image_scale_4tap_RGB555 (&dest[0], &src[0], videoscale->tmp_buf);
1427 GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1428 ("Unsupported format %d for scaling method %d", format, method));
1429 return GST_FLOW_ERROR;
1433 GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1434 ("Unknown scaling method %d", videoscale->method));
1435 return GST_FLOW_ERROR;
1440 gst_video_scale_src_event (GstBaseTransform * trans, GstEvent * event)
1442 GstVideoScale *videoscale = GST_VIDEO_SCALE_CAST (trans);
1443 GstVideoFilter *filter = GST_VIDEO_FILTER_CAST (trans);
1446 GstStructure *structure;
1448 GST_DEBUG_OBJECT (videoscale, "handling %s event",
1449 GST_EVENT_TYPE_NAME (event));
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) {
1456 GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
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);
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);
1473 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
1479 plugin_init (GstPlugin * plugin)
1481 if (!gst_element_register (plugin, "videoscale", GST_RANK_NONE,
1482 GST_TYPE_VIDEO_SCALE))
1485 GST_DEBUG_CATEGORY_INIT (video_scale_debug, "videoscale", 0,
1486 "videoscale element");
1487 GST_DEBUG_CATEGORY_GET (GST_CAT_PERFORMANCE, "GST_PERFORMANCE");
1494 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1497 "Resizes video", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,