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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, 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 FALSE
94 #define DEFAULT_PROP_SHARPNESS 1.0
95 #define DEFAULT_PROP_SHARPEN 0.0
96 #define DEFAULT_PROP_DITHER FALSE
97 #define DEFAULT_PROP_SUBMETHOD 1
98 #define DEFAULT_PROP_ENVELOPE 2.0
112 #undef GST_VIDEO_SIZE_RANGE
113 #define GST_VIDEO_SIZE_RANGE "(int) [ 1, 32767]"
115 #define GST_VIDEO_FORMATS "{ I420, YV12, YUY2, UYVY, AYUV, RGBx, " \
116 "BGRx, xRGB, xBGR, RGBA, BGRA, ARGB, ABGR, RGB, " \
117 "BGR, Y41B, Y42B, YVYU, Y444, GRAY8, GRAY16_BE, GRAY16_LE, " \
118 "v308, Y800, Y16, RGB16, RGB15, ARGB64, AYUV64, NV12 } "
121 static GstStaticCaps gst_video_scale_format_caps[] = {
122 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS))
125 #define GST_TYPE_VIDEO_SCALE_METHOD (gst_video_scale_method_get_type())
127 gst_video_scale_method_get_type (void)
129 static GType video_scale_method_type = 0;
131 static const GEnumValue video_scale_methods[] = {
132 {GST_VIDEO_SCALE_NEAREST, "Nearest Neighbour", "nearest-neighbour"},
133 {GST_VIDEO_SCALE_BILINEAR, "Bilinear", "bilinear"},
134 {GST_VIDEO_SCALE_4TAP, "4-tap", "4-tap"},
135 {GST_VIDEO_SCALE_LANCZOS, "Lanczos", "lanczos"},
139 if (!video_scale_method_type) {
140 video_scale_method_type =
141 g_enum_register_static ("GstVideoScaleMethod", video_scale_methods);
143 return video_scale_method_type;
147 gst_video_scale_get_capslist (void)
149 static GstCaps *caps = NULL;
150 static volatile gsize inited = 0;
152 if (g_once_init_enter (&inited)) {
155 g_assert (caps == NULL);
157 caps = gst_caps_new_empty ();
158 for (i = 0; i < G_N_ELEMENTS (gst_video_scale_format_caps); i++)
159 gst_caps_append (caps,
160 gst_caps_make_writable
161 (gst_static_caps_get (&gst_video_scale_format_caps[i])));
162 g_once_init_leave (&inited, 1);
168 static GstPadTemplate *
169 gst_video_scale_src_template_factory (void)
171 return gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
172 gst_video_scale_get_capslist ());
175 static GstPadTemplate *
176 gst_video_scale_sink_template_factory (void)
178 return gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
179 gst_video_scale_get_capslist ());
183 static void gst_video_scale_finalize (GstVideoScale * videoscale);
184 static gboolean gst_video_scale_src_event (GstBaseTransform * trans,
187 /* base transform vmethods */
188 static GstCaps *gst_video_scale_transform_caps (GstBaseTransform * trans,
189 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
190 static GstCaps *gst_video_scale_fixate_caps (GstBaseTransform * base,
191 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
193 static gboolean gst_video_scale_set_info (GstVideoFilter * filter,
194 GstCaps * in, GstVideoInfo * in_info, GstCaps * out,
195 GstVideoInfo * out_info);
196 static GstFlowReturn gst_video_scale_transform_frame (GstVideoFilter * filter,
197 GstVideoFrame * in, GstVideoFrame * out);
199 static void gst_video_scale_set_property (GObject * object, guint prop_id,
200 const GValue * value, GParamSpec * pspec);
201 static void gst_video_scale_get_property (GObject * object, guint prop_id,
202 GValue * value, GParamSpec * pspec);
204 #define gst_video_scale_parent_class parent_class
205 G_DEFINE_TYPE (GstVideoScale, gst_video_scale, GST_TYPE_VIDEO_FILTER);
208 gst_video_scale_class_init (GstVideoScaleClass * klass)
210 GObjectClass *gobject_class = (GObjectClass *) klass;
211 GstElementClass *element_class = (GstElementClass *) klass;
212 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
213 GstVideoFilterClass *filter_class = (GstVideoFilterClass *) klass;
215 gobject_class->finalize = (GObjectFinalizeFunc) gst_video_scale_finalize;
216 gobject_class->set_property = gst_video_scale_set_property;
217 gobject_class->get_property = gst_video_scale_get_property;
219 g_object_class_install_property (gobject_class, PROP_METHOD,
220 g_param_spec_enum ("method", "method", "method",
221 GST_TYPE_VIDEO_SCALE_METHOD, DEFAULT_PROP_METHOD,
222 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_ADD_BORDERS,
225 g_param_spec_boolean ("add-borders", "Add Borders",
226 "Add black borders if necessary to keep the display aspect ratio",
227 DEFAULT_PROP_ADD_BORDERS,
228 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_SHARPNESS,
231 g_param_spec_double ("sharpness", "Sharpness",
232 "Sharpness of filter", 0.0, 2.0, DEFAULT_PROP_SHARPNESS,
233 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (gobject_class, PROP_SHARPEN,
236 g_param_spec_double ("sharpen", "Sharpen",
237 "Sharpening", 0.0, 1.0, DEFAULT_PROP_SHARPEN,
238 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property (gobject_class, PROP_DITHER,
241 g_param_spec_boolean ("dither", "Dither",
242 "Add dither (only used for Lanczos method)",
244 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247 /* I am hiding submethod for now, since it's poorly named, poorly
248 * documented, and will probably just get people into trouble. */
249 g_object_class_install_property (gobject_class, PROP_SUBMETHOD,
250 g_param_spec_int ("submethod", "submethod",
251 "submethod", 0, 3, DEFAULT_PROP_SUBMETHOD,
252 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (gobject_class, PROP_ENVELOPE,
256 g_param_spec_double ("envelope", "Envelope",
257 "Size of filter envelope", 0.0, 5.0, DEFAULT_PROP_ENVELOPE,
258 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 gst_element_class_set_details_simple (element_class,
261 "Video scaler", "Filter/Converter/Video/Scaler",
262 "Resizes video", "Wim Taymans <wim.taymans@chello.be>");
264 gst_element_class_add_pad_template (element_class,
265 gst_video_scale_sink_template_factory ());
266 gst_element_class_add_pad_template (element_class,
267 gst_video_scale_src_template_factory ());
269 trans_class->transform_caps =
270 GST_DEBUG_FUNCPTR (gst_video_scale_transform_caps);
271 trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_video_scale_fixate_caps);
272 trans_class->src_event = GST_DEBUG_FUNCPTR (gst_video_scale_src_event);
274 filter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_scale_set_info);
275 filter_class->transform_frame =
276 GST_DEBUG_FUNCPTR (gst_video_scale_transform_frame);
280 gst_video_scale_init (GstVideoScale * videoscale)
282 videoscale->tmp_buf = NULL;
283 videoscale->method = DEFAULT_PROP_METHOD;
284 videoscale->add_borders = DEFAULT_PROP_ADD_BORDERS;
285 videoscale->submethod = DEFAULT_PROP_SUBMETHOD;
286 videoscale->sharpness = DEFAULT_PROP_SHARPNESS;
287 videoscale->sharpen = DEFAULT_PROP_SHARPEN;
288 videoscale->dither = DEFAULT_PROP_DITHER;
289 videoscale->envelope = DEFAULT_PROP_ENVELOPE;
293 gst_video_scale_finalize (GstVideoScale * videoscale)
295 if (videoscale->tmp_buf)
296 g_free (videoscale->tmp_buf);
298 G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (videoscale));
302 gst_video_scale_set_property (GObject * object, guint prop_id,
303 const GValue * value, GParamSpec * pspec)
305 GstVideoScale *vscale = GST_VIDEO_SCALE (object);
309 GST_OBJECT_LOCK (vscale);
310 vscale->method = g_value_get_enum (value);
311 GST_OBJECT_UNLOCK (vscale);
313 case PROP_ADD_BORDERS:
314 GST_OBJECT_LOCK (vscale);
315 vscale->add_borders = g_value_get_boolean (value);
316 GST_OBJECT_UNLOCK (vscale);
317 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM_CAST (vscale));
320 GST_OBJECT_LOCK (vscale);
321 vscale->sharpness = g_value_get_double (value);
322 GST_OBJECT_UNLOCK (vscale);
325 GST_OBJECT_LOCK (vscale);
326 vscale->sharpen = g_value_get_double (value);
327 GST_OBJECT_UNLOCK (vscale);
330 GST_OBJECT_LOCK (vscale);
331 vscale->dither = g_value_get_boolean (value);
332 GST_OBJECT_UNLOCK (vscale);
335 GST_OBJECT_LOCK (vscale);
336 vscale->submethod = g_value_get_int (value);
337 GST_OBJECT_UNLOCK (vscale);
340 GST_OBJECT_LOCK (vscale);
341 vscale->envelope = g_value_get_double (value);
342 GST_OBJECT_UNLOCK (vscale);
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 gst_video_scale_get_property (GObject * object, guint prop_id, GValue * value,
354 GstVideoScale *vscale = GST_VIDEO_SCALE (object);
358 GST_OBJECT_LOCK (vscale);
359 g_value_set_enum (value, vscale->method);
360 GST_OBJECT_UNLOCK (vscale);
362 case PROP_ADD_BORDERS:
363 GST_OBJECT_LOCK (vscale);
364 g_value_set_boolean (value, vscale->add_borders);
365 GST_OBJECT_UNLOCK (vscale);
368 GST_OBJECT_LOCK (vscale);
369 g_value_set_double (value, vscale->sharpness);
370 GST_OBJECT_UNLOCK (vscale);
373 GST_OBJECT_LOCK (vscale);
374 g_value_set_double (value, vscale->sharpen);
375 GST_OBJECT_UNLOCK (vscale);
378 GST_OBJECT_LOCK (vscale);
379 g_value_set_boolean (value, vscale->dither);
380 GST_OBJECT_UNLOCK (vscale);
383 GST_OBJECT_LOCK (vscale);
384 g_value_set_int (value, vscale->submethod);
385 GST_OBJECT_UNLOCK (vscale);
388 GST_OBJECT_LOCK (vscale);
389 g_value_set_double (value, vscale->envelope);
390 GST_OBJECT_UNLOCK (vscale);
393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398 #define NEAREST (1 << GST_VIDEO_SCALE_NEAREST)
399 #define BILINEAR (1 << GST_VIDEO_SCALE_BILINEAR)
400 #define FOURTAP (1 << GST_VIDEO_SCALE_4TAP)
401 #define LANCZOS (1 << GST_VIDEO_SCALE_LANCZOS)
403 /* or we could just do lookups via table[format] if we could be bothered.. */
406 GstVideoFormat format;
408 } formats_methods_table[] = {
410 GST_VIDEO_FORMAT_RGBx, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
411 GST_VIDEO_FORMAT_xRGB, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
412 GST_VIDEO_FORMAT_BGRx, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
413 GST_VIDEO_FORMAT_xBGR, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
414 GST_VIDEO_FORMAT_RGBA, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
415 GST_VIDEO_FORMAT_ARGB, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
416 GST_VIDEO_FORMAT_BGRA, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
417 GST_VIDEO_FORMAT_ABGR, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
418 GST_VIDEO_FORMAT_AYUV, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
419 GST_VIDEO_FORMAT_ARGB64, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
420 GST_VIDEO_FORMAT_AYUV64, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
421 GST_VIDEO_FORMAT_RGB, NEAREST | BILINEAR | FOURTAP}, {
422 GST_VIDEO_FORMAT_BGR, NEAREST | BILINEAR | FOURTAP}, {
423 GST_VIDEO_FORMAT_v308, NEAREST | BILINEAR | FOURTAP}, {
424 GST_VIDEO_FORMAT_YUY2, NEAREST | BILINEAR | FOURTAP}, {
425 GST_VIDEO_FORMAT_YVYU, NEAREST | BILINEAR | FOURTAP}, {
426 GST_VIDEO_FORMAT_UYVY, NEAREST | BILINEAR | FOURTAP}, {
427 GST_VIDEO_FORMAT_Y800, NEAREST | BILINEAR | FOURTAP}, {
428 GST_VIDEO_FORMAT_GRAY8, NEAREST | BILINEAR | FOURTAP}, {
429 GST_VIDEO_FORMAT_GRAY16_LE, NEAREST | BILINEAR | FOURTAP}, {
430 GST_VIDEO_FORMAT_GRAY16_BE, NEAREST | BILINEAR | FOURTAP}, {
431 GST_VIDEO_FORMAT_Y16, NEAREST | BILINEAR | FOURTAP}, {
432 GST_VIDEO_FORMAT_I420, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
433 GST_VIDEO_FORMAT_YV12, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
434 GST_VIDEO_FORMAT_Y444, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
435 GST_VIDEO_FORMAT_Y42B, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
436 GST_VIDEO_FORMAT_Y41B, NEAREST | BILINEAR | FOURTAP | LANCZOS}, {
437 GST_VIDEO_FORMAT_NV12, NEAREST | BILINEAR}, {
438 GST_VIDEO_FORMAT_RGB16, NEAREST | BILINEAR | FOURTAP}, {
439 GST_VIDEO_FORMAT_RGB15, NEAREST | BILINEAR | FOURTAP}
443 gst_video_scale_format_supported_for_method (GstVideoFormat format,
444 GstVideoScaleMethod method)
448 for (i = 0; i < G_N_ELEMENTS (formats_methods_table); ++i) {
449 if (formats_methods_table[i].format == format)
450 return ((formats_methods_table[i].methods & (1 << method)) != 0);
456 gst_video_scale_transform_supported (GstVideoScale * videoscale,
457 GstVideoScaleMethod method, GstStructure * structure)
461 gboolean supported = TRUE;
465 /* we support these methods for all formats */
466 if (method == GST_VIDEO_SCALE_NEAREST || method == GST_VIDEO_SCALE_BILINEAR)
469 /* we need fixed caps if we want to use gst_video_parse_caps() */
470 s = gst_structure_new (gst_structure_get_name (structure),
471 "width", G_TYPE_INT, 1, "height", G_TYPE_INT, 1, NULL);
473 if ((val = gst_structure_get_value (structure, "format"))) {
474 gst_structure_set_value (s, "format", val);
476 if ((val = gst_structure_get_value (structure, "endianness")))
477 gst_structure_set_value (s, "endianness", val);
478 if ((val = gst_structure_get_value (structure, "red_mask")))
479 gst_structure_set_value (s, "red_mask", val);
480 if ((val = gst_structure_get_value (structure, "blue_mask")))
481 gst_structure_set_value (s, "blue_mask", val);
482 if ((val = gst_structure_get_value (structure, "green_mask")))
483 gst_structure_set_value (s, "green_mask", val);
484 if ((val = gst_structure_get_value (structure, "alpha_mask")))
485 gst_structure_set_value (s, "alpha_mask", val);
486 if ((val = gst_structure_get_value (structure, "depth")))
487 gst_structure_set_value (s, "depth", val);
488 if ((val = gst_structure_get_value (structure, "bpp")))
489 gst_structure_set_value (s, "bpp", val);
491 c = gst_caps_new_full (s, NULL);
493 gst_video_info_init (&info);
494 if (!gst_video_info_from_caps (&info, c)) {
495 GST_ERROR_OBJECT (videoscale, "couldn't parse %" GST_PTR_FORMAT, c);
496 } else if (!gst_video_scale_format_supported_for_method (info.finfo->format,
500 GST_LOG_OBJECT (videoscale, "method %d %ssupported for format %d",
501 method, (supported) ? "" : "not ", info.finfo->format);
508 gst_video_scale_transform_caps (GstBaseTransform * trans,
509 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
511 GstVideoScale *videoscale = GST_VIDEO_SCALE (trans);
512 GstVideoScaleMethod method;
514 GstStructure *structure;
517 GST_DEBUG_OBJECT (trans,
518 "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps,
519 (direction == GST_PAD_SINK) ? "sink" : "src");
521 GST_OBJECT_LOCK (videoscale);
522 method = videoscale->method;
523 GST_OBJECT_UNLOCK (videoscale);
525 ret = gst_caps_new_empty ();
526 n = gst_caps_get_size (caps);
527 for (i = 0; i < n; i++) {
528 structure = gst_caps_get_structure (caps, i);
530 /* If this is already expressed by the existing caps
531 * skip this structure */
532 if (i > 0 && gst_caps_is_subset_structure (ret, structure))
535 if (!gst_video_scale_transform_supported (videoscale, method, structure))
536 goto format_not_supported;
538 structure = gst_structure_copy (structure);
539 gst_structure_set (structure,
540 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
541 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
543 /* if pixel aspect ratio, make a range of it */
544 if (gst_structure_has_field (structure, "pixel-aspect-ratio")) {
545 gst_structure_set (structure, "pixel-aspect-ratio",
546 GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1, NULL);
548 gst_caps_append_structure (ret, structure);
552 GstCaps *intersection;
555 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
556 gst_caps_unref (ret);
562 GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
566 format_not_supported:
568 gst_structure_free (structure);
569 gst_caps_unref (ret);
570 ret = gst_caps_new_empty ();
576 gst_video_scale_set_info (GstVideoFilter * filter, GstCaps * in,
577 GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
579 GstVideoScale *videoscale = GST_VIDEO_SCALE (filter);
580 gint from_dar_n, from_dar_d, to_dar_n, to_dar_d;
582 if (!gst_util_fraction_multiply (in_info->width,
583 in_info->height, out_info->par_n, out_info->par_d, &from_dar_n,
585 from_dar_n = from_dar_d = -1;
588 if (!gst_util_fraction_multiply (out_info->width,
589 out_info->height, out_info->par_n, out_info->par_d, &to_dar_n,
591 to_dar_n = to_dar_d = -1;
594 videoscale->borders_w = videoscale->borders_h = 0;
595 if (to_dar_n != from_dar_n || to_dar_d != from_dar_d) {
596 if (videoscale->add_borders) {
597 gint n, d, to_h, to_w;
599 if (from_dar_n != -1 && from_dar_d != -1
600 && gst_util_fraction_multiply (from_dar_n, from_dar_d,
601 out_info->par_n, out_info->par_d, &n, &d)) {
602 to_h = gst_util_uint64_scale_int (out_info->width, d, n);
603 if (to_h <= out_info->height) {
604 videoscale->borders_h = out_info->height - to_h;
605 videoscale->borders_w = 0;
607 to_w = gst_util_uint64_scale_int (out_info->height, n, d);
608 g_assert (to_w <= out_info->width);
609 videoscale->borders_h = 0;
610 videoscale->borders_w = out_info->width - to_w;
613 GST_WARNING_OBJECT (videoscale, "Can't calculate borders");
616 GST_WARNING_OBJECT (videoscale, "Can't keep DAR!");
620 if (videoscale->tmp_buf)
621 g_free (videoscale->tmp_buf);
622 videoscale->tmp_buf = g_malloc (out_info->width * 8 * 4);
624 if (in_info->width == out_info->width && in_info->height == out_info->height) {
625 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), TRUE);
627 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, filter, "setup videoscaling");
628 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), FALSE);
631 GST_DEBUG_OBJECT (videoscale, "from=%dx%d (par=%d/%d dar=%d/%d), size %"
632 G_GSIZE_FORMAT " -> to=%dx%d (par=%d/%d dar=%d/%d borders=%d:%d), "
633 "size %" G_GSIZE_FORMAT,
634 in_info->width, in_info->height, out_info->par_n, out_info->par_d,
635 from_dar_n, from_dar_d, in_info->size, out_info->width,
636 out_info->height, out_info->par_n, out_info->par_d, to_dar_n, to_dar_d,
637 videoscale->borders_w, videoscale->borders_h, out_info->size);
643 gst_video_scale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
644 GstCaps * caps, GstCaps * othercaps)
646 GstStructure *ins, *outs;
647 const GValue *from_par, *to_par;
648 GValue fpar = { 0, }, tpar = {
651 othercaps = gst_caps_make_writable (othercaps);
652 gst_caps_truncate (othercaps);
654 GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
655 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
657 ins = gst_caps_get_structure (caps, 0);
658 outs = gst_caps_get_structure (othercaps, 0);
660 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
661 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
663 /* If we're fixating from the sinkpad we always set the PAR and
664 * assume that missing PAR on the sinkpad means 1/1 and
665 * missing PAR on the srcpad means undefined
667 if (direction == GST_PAD_SINK) {
669 g_value_init (&fpar, GST_TYPE_FRACTION);
670 gst_value_set_fraction (&fpar, 1, 1);
674 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
675 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
680 g_value_init (&tpar, GST_TYPE_FRACTION);
681 gst_value_set_fraction (&tpar, 1, 1);
684 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
688 g_value_init (&fpar, GST_TYPE_FRACTION);
689 gst_value_set_fraction (&fpar, 1, 1);
694 /* we have both PAR but they might not be fixated */
696 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
698 gint from_dar_n, from_dar_d;
701 /* from_par should be fixed */
702 g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
704 from_par_n = gst_value_get_fraction_numerator (from_par);
705 from_par_d = gst_value_get_fraction_denominator (from_par);
707 gst_structure_get_int (ins, "width", &from_w);
708 gst_structure_get_int (ins, "height", &from_h);
710 gst_structure_get_int (outs, "width", &w);
711 gst_structure_get_int (outs, "height", &h);
713 /* if both width and height are already fixed, we can't do anything
714 * about it anymore */
718 GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
720 if (!gst_value_is_fixed (to_par)) {
721 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
722 from_par_n, from_par_d, w, h)) {
723 GST_DEBUG_OBJECT (base, "fixating to_par to %dx%d", n, d);
724 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
725 gst_structure_fixate_field_nearest_fraction (outs,
726 "pixel-aspect-ratio", n, d);
728 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
735 /* Calculate input DAR */
736 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
737 &from_dar_n, &from_dar_d)) {
738 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
739 ("Error calculating the output scaled size - integer overflow"));
743 GST_DEBUG_OBJECT (base, "Input DAR is %d/%d", from_dar_n, from_dar_d);
745 /* If either width or height are fixed there's not much we
746 * can do either except choosing a height or width and PAR
747 * that matches the DAR as good as possible
751 gint set_w, set_par_n, set_par_d;
753 GST_DEBUG_OBJECT (base, "height is fixed (%d)", h);
755 /* If the PAR is fixed too, there's not much to do
756 * except choosing the width that is nearest to the
757 * width with the same DAR */
758 if (gst_value_is_fixed (to_par)) {
759 to_par_n = gst_value_get_fraction_numerator (to_par);
760 to_par_d = gst_value_get_fraction_denominator (to_par);
762 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
764 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
765 to_par_n, &num, &den)) {
766 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
767 ("Error calculating the output scaled size - integer overflow"));
771 w = (guint) gst_util_uint64_scale_int (h, num, den);
772 gst_structure_fixate_field_nearest_int (outs, "width", w);
777 /* The PAR is not fixed and it's quite likely that we can set
778 * an arbitrary PAR. */
780 /* Check if we can keep the input width */
781 tmp = gst_structure_copy (outs);
782 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
783 gst_structure_get_int (tmp, "width", &set_w);
785 /* Might have failed but try to keep the DAR nonetheless by
786 * adjusting the PAR */
787 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
788 &to_par_n, &to_par_d)) {
789 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
790 ("Error calculating the output scaled size - integer overflow"));
791 gst_structure_free (tmp);
795 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
796 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
797 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
799 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
801 gst_structure_free (tmp);
803 /* Check if the adjusted PAR is accepted */
804 if (set_par_n == to_par_n && set_par_d == to_par_d) {
805 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
806 set_par_n != set_par_d)
807 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
808 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
813 /* Otherwise scale the width to the new PAR and check if the
814 * adjusted with is accepted. If all that fails we can't keep
816 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
817 set_par_n, &num, &den)) {
818 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
819 ("Error calculating the output scaled size - integer overflow"));
823 w = (guint) gst_util_uint64_scale_int (h, num, den);
824 gst_structure_fixate_field_nearest_int (outs, "width", w);
825 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
826 set_par_n != set_par_d)
827 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
828 set_par_n, set_par_d, NULL);
833 gint set_h, set_par_n, set_par_d;
835 GST_DEBUG_OBJECT (base, "width is fixed (%d)", w);
837 /* If the PAR is fixed too, there's not much to do
838 * except choosing the height that is nearest to the
839 * height with the same DAR */
840 if (gst_value_is_fixed (to_par)) {
841 to_par_n = gst_value_get_fraction_numerator (to_par);
842 to_par_d = gst_value_get_fraction_denominator (to_par);
844 GST_DEBUG_OBJECT (base, "PAR is fixed %d/%d", to_par_n, to_par_d);
846 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
847 to_par_n, &num, &den)) {
848 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
849 ("Error calculating the output scaled size - integer overflow"));
853 h = (guint) gst_util_uint64_scale_int (w, den, num);
854 gst_structure_fixate_field_nearest_int (outs, "height", h);
859 /* The PAR is not fixed and it's quite likely that we can set
860 * an arbitrary PAR. */
862 /* Check if we can keep the input height */
863 tmp = gst_structure_copy (outs);
864 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
865 gst_structure_get_int (tmp, "height", &set_h);
867 /* Might have failed but try to keep the DAR nonetheless by
868 * adjusting the PAR */
869 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
870 &to_par_n, &to_par_d)) {
871 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
872 ("Error calculating the output scaled size - integer overflow"));
873 gst_structure_free (tmp);
876 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
877 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
878 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
880 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
882 gst_structure_free (tmp);
884 /* Check if the adjusted PAR is accepted */
885 if (set_par_n == to_par_n && set_par_d == to_par_d) {
886 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
887 set_par_n != set_par_d)
888 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
889 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
894 /* Otherwise scale the height to the new PAR and check if the
895 * adjusted with is accepted. If all that fails we can't keep
897 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
898 set_par_n, &num, &den)) {
899 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
900 ("Error calculating the output scaled size - integer overflow"));
904 h = (guint) gst_util_uint64_scale_int (w, den, num);
905 gst_structure_fixate_field_nearest_int (outs, "height", h);
906 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
907 set_par_n != set_par_d)
908 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
909 set_par_n, set_par_d, NULL);
912 } else if (gst_value_is_fixed (to_par)) {
914 gint set_h, set_w, f_h, f_w;
916 to_par_n = gst_value_get_fraction_numerator (to_par);
917 to_par_d = gst_value_get_fraction_denominator (to_par);
919 /* Calculate scale factor for the PAR change */
920 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
921 to_par_d, &num, &den)) {
922 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
923 ("Error calculating the output scaled size - integer overflow"));
927 /* Try to keep the input height (because of interlacing) */
928 tmp = gst_structure_copy (outs);
929 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
930 gst_structure_get_int (tmp, "height", &set_h);
932 /* This might have failed but try to scale the width
933 * to keep the DAR nonetheless */
934 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
935 gst_structure_fixate_field_nearest_int (tmp, "width", w);
936 gst_structure_get_int (tmp, "width", &set_w);
937 gst_structure_free (tmp);
939 /* We kept the DAR and the height is nearest to the original height */
941 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
942 G_TYPE_INT, set_h, NULL);
949 /* If the former failed, try to keep the input width at least */
950 tmp = gst_structure_copy (outs);
951 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
952 gst_structure_get_int (tmp, "width", &set_w);
954 /* This might have failed but try to scale the width
955 * to keep the DAR nonetheless */
956 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
957 gst_structure_fixate_field_nearest_int (tmp, "height", h);
958 gst_structure_get_int (tmp, "height", &set_h);
959 gst_structure_free (tmp);
961 /* We kept the DAR and the width is nearest to the original width */
963 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
964 G_TYPE_INT, set_h, NULL);
968 /* If all this failed, keep the height that was nearest to the orignal
969 * height and the nearest possible width. This changes the DAR but
970 * there's not much else to do here.
972 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
977 gint set_h, set_w, set_par_n, set_par_d, tmp2;
979 /* width, height and PAR are not fixed but passthrough is not possible */
981 /* First try to keep the height and width as good as possible
983 tmp = gst_structure_copy (outs);
984 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
985 gst_structure_get_int (tmp, "height", &set_h);
986 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
987 gst_structure_get_int (tmp, "width", &set_w);
989 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
990 &to_par_n, &to_par_d)) {
991 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
992 ("Error calculating the output scaled size - integer overflow"));
996 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
997 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
998 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
1000 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
1002 gst_structure_free (tmp);
1004 if (set_par_n == to_par_n && set_par_d == to_par_d) {
1005 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1006 G_TYPE_INT, set_h, NULL);
1008 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1009 set_par_n != set_par_d)
1010 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1011 set_par_n, set_par_d, NULL);
1015 /* Otherwise try to scale width to keep the DAR with the set
1017 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
1018 set_par_n, &num, &den)) {
1019 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
1020 ("Error calculating the output scaled size - integer overflow"));
1024 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
1025 tmp = gst_structure_copy (outs);
1026 gst_structure_fixate_field_nearest_int (tmp, "width", w);
1027 gst_structure_get_int (tmp, "width", &tmp2);
1028 gst_structure_free (tmp);
1031 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
1032 G_TYPE_INT, set_h, NULL);
1033 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1034 set_par_n != set_par_d)
1035 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1036 set_par_n, set_par_d, NULL);
1040 /* ... or try the same with the height */
1041 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
1042 tmp = gst_structure_copy (outs);
1043 gst_structure_fixate_field_nearest_int (tmp, "height", h);
1044 gst_structure_get_int (tmp, "height", &tmp2);
1045 gst_structure_free (tmp);
1048 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1049 G_TYPE_INT, tmp2, NULL);
1050 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1051 set_par_n != set_par_d)
1052 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1053 set_par_n, set_par_d, NULL);
1057 /* If all fails we can't keep the DAR and take the nearest values
1058 * for everything from the first try */
1059 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
1060 G_TYPE_INT, set_h, NULL);
1061 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
1062 set_par_n != set_par_d)
1063 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1064 set_par_n, set_par_d, NULL);
1069 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
1071 if (from_par == &fpar)
1072 g_value_unset (&fpar);
1073 if (to_par == &tpar)
1074 g_value_unset (&tpar);
1080 gst_video_scale_setup_vs_image (VSImage * image, GstVideoFrame * frame,
1081 gint component, gint b_w, gint b_h)
1083 GstVideoFormat format;
1086 format = GST_VIDEO_FRAME_FORMAT (frame);
1087 width = GST_VIDEO_FRAME_WIDTH (frame);
1088 height = GST_VIDEO_FRAME_HEIGHT (frame);
1090 image->real_width = GST_VIDEO_FRAME_COMP_WIDTH (frame, component);
1091 image->real_height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, component);
1092 image->width = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (frame->info.finfo,
1093 component, MAX (1, width - b_w));
1094 image->height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (frame->info.finfo,
1095 component, MAX (1, height - b_h));
1097 image->border_top = (image->real_height - image->height) / 2;
1098 image->border_bottom = image->real_height - image->height - image->border_top;
1100 if (format == GST_VIDEO_FORMAT_YUY2 || format == GST_VIDEO_FORMAT_YVYU
1101 || format == GST_VIDEO_FORMAT_UYVY) {
1102 g_assert (component == 0);
1104 image->border_left = (image->real_width - image->width) / 2;
1106 if (image->border_left % 2 == 1)
1107 image->border_left--;
1108 image->border_right = image->real_width - image->width - image->border_left;
1110 image->border_left = (image->real_width - image->width) / 2;
1111 image->border_right = image->real_width - image->width - image->border_left;
1114 image->real_pixels = frame->data[component];
1115 image->stride = frame->info.stride[component];
1118 image->real_pixels + image->border_top * image->stride +
1119 image->border_left * GST_VIDEO_FRAME_COMP_PSTRIDE (frame, component);
1122 static const guint8 *
1123 _get_black_for_format (GstVideoFormat format)
1125 static const guint8 black[][4] = {
1126 {255, 0, 0, 0}, /* 0 = ARGB, ABGR, xRGB, xBGR */
1127 {0, 0, 0, 255}, /* 1 = RGBA, BGRA, RGBx, BGRx */
1128 {255, 16, 128, 128}, /* 2 = AYUV */
1129 {0, 0, 0, 0}, /* 3 = RGB and BGR */
1130 {16, 128, 128, 0}, /* 4 = v301 */
1131 {16, 128, 16, 128}, /* 5 = YUY2, YUYV */
1132 {128, 16, 128, 16}, /* 6 = UYVY */
1133 {16, 0, 0, 0}, /* 7 = Y */
1134 {0, 0, 0, 0} /* 8 = RGB565, RGB666 */
1138 case GST_VIDEO_FORMAT_ARGB:
1139 case GST_VIDEO_FORMAT_ABGR:
1140 case GST_VIDEO_FORMAT_xRGB:
1141 case GST_VIDEO_FORMAT_xBGR:
1142 case GST_VIDEO_FORMAT_ARGB64:
1144 case GST_VIDEO_FORMAT_RGBA:
1145 case GST_VIDEO_FORMAT_BGRA:
1146 case GST_VIDEO_FORMAT_RGBx:
1147 case GST_VIDEO_FORMAT_BGRx:
1149 case GST_VIDEO_FORMAT_AYUV:
1150 case GST_VIDEO_FORMAT_AYUV64:
1152 case GST_VIDEO_FORMAT_RGB:
1153 case GST_VIDEO_FORMAT_BGR:
1155 case GST_VIDEO_FORMAT_v308:
1157 case GST_VIDEO_FORMAT_YUY2:
1158 case GST_VIDEO_FORMAT_YVYU:
1160 case GST_VIDEO_FORMAT_UYVY:
1162 case GST_VIDEO_FORMAT_Y800:
1163 case GST_VIDEO_FORMAT_GRAY8:
1165 case GST_VIDEO_FORMAT_GRAY16_LE:
1166 case GST_VIDEO_FORMAT_GRAY16_BE:
1167 case GST_VIDEO_FORMAT_Y16:
1168 return NULL; /* Handled by the caller */
1169 case GST_VIDEO_FORMAT_I420:
1170 case GST_VIDEO_FORMAT_YV12:
1171 case GST_VIDEO_FORMAT_Y444:
1172 case GST_VIDEO_FORMAT_Y42B:
1173 case GST_VIDEO_FORMAT_Y41B:
1174 case GST_VIDEO_FORMAT_NV12:
1175 return black[4]; /* Y, U, V, 0 */
1176 case GST_VIDEO_FORMAT_RGB16:
1177 case GST_VIDEO_FORMAT_RGB15:
1184 static GstFlowReturn
1185 gst_video_scale_transform_frame (GstVideoFilter * filter,
1186 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
1188 GstVideoScale *videoscale = GST_VIDEO_SCALE (filter);
1189 GstFlowReturn ret = GST_FLOW_OK;
1190 VSImage dest[4] = { {NULL,}, };
1191 VSImage src[4] = { {NULL,}, };
1193 const guint8 *black;
1194 gboolean add_borders;
1195 GstVideoFormat format;
1198 GST_OBJECT_LOCK (videoscale);
1199 method = videoscale->method;
1200 add_borders = videoscale->add_borders;
1201 GST_OBJECT_UNLOCK (videoscale);
1203 format = GST_VIDEO_INFO_FORMAT (&filter->in_info);
1204 black = _get_black_for_format (format);
1206 if (filter->in_info.width == 1) {
1207 method = GST_VIDEO_SCALE_NEAREST;
1209 if (method == GST_VIDEO_SCALE_4TAP &&
1210 (filter->in_info.width < 4 || filter->in_info.height < 4)) {
1211 method = GST_VIDEO_SCALE_BILINEAR;
1214 for (i = 0; i < GST_VIDEO_FRAME_N_PLANES (in_frame); i++) {
1215 gst_video_scale_setup_vs_image (&src[i], in_frame, i, 0, 0);
1216 gst_video_scale_setup_vs_image (&dest[i], out_frame, i,
1217 videoscale->borders_w, videoscale->borders_h);
1220 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, filter,
1221 "doing videoscale format %s", GST_VIDEO_INFO_NAME (&filter->in_info));
1224 case GST_VIDEO_FORMAT_RGBx:
1225 case GST_VIDEO_FORMAT_xRGB:
1226 case GST_VIDEO_FORMAT_BGRx:
1227 case GST_VIDEO_FORMAT_xBGR:
1228 case GST_VIDEO_FORMAT_RGBA:
1229 case GST_VIDEO_FORMAT_ARGB:
1230 case GST_VIDEO_FORMAT_BGRA:
1231 case GST_VIDEO_FORMAT_ABGR:
1232 case GST_VIDEO_FORMAT_AYUV:
1234 vs_fill_borders_RGBA (&dest[0], black);
1236 case GST_VIDEO_SCALE_NEAREST:
1237 vs_image_scale_nearest_RGBA (&dest[0], &src[0], videoscale->tmp_buf);
1239 case GST_VIDEO_SCALE_BILINEAR:
1240 vs_image_scale_linear_RGBA (&dest[0], &src[0], videoscale->tmp_buf);
1242 case GST_VIDEO_SCALE_4TAP:
1243 vs_image_scale_4tap_RGBA (&dest[0], &src[0], videoscale->tmp_buf);
1245 case GST_VIDEO_SCALE_LANCZOS:
1246 vs_image_scale_lanczos_AYUV (&dest[0], &src[0], videoscale->tmp_buf,
1247 videoscale->sharpness, videoscale->dither, videoscale->submethod,
1248 videoscale->envelope, videoscale->sharpen);
1254 case GST_VIDEO_FORMAT_ARGB64:
1255 case GST_VIDEO_FORMAT_AYUV64:
1257 vs_fill_borders_AYUV64 (&dest[0], black);
1259 case GST_VIDEO_SCALE_NEAREST:
1260 vs_image_scale_nearest_AYUV64 (&dest[0], &src[0],
1261 videoscale->tmp_buf);
1263 case GST_VIDEO_SCALE_BILINEAR:
1264 vs_image_scale_linear_AYUV64 (&dest[0], &src[0], videoscale->tmp_buf);
1266 case GST_VIDEO_SCALE_4TAP:
1267 vs_image_scale_4tap_AYUV64 (&dest[0], &src[0], videoscale->tmp_buf);
1269 case GST_VIDEO_SCALE_LANCZOS:
1270 vs_image_scale_lanczos_AYUV64 (&dest[0], &src[0], videoscale->tmp_buf,
1271 videoscale->sharpness, videoscale->dither, videoscale->submethod,
1272 videoscale->envelope, videoscale->sharpen);
1278 case GST_VIDEO_FORMAT_RGB:
1279 case GST_VIDEO_FORMAT_BGR:
1280 case GST_VIDEO_FORMAT_v308:
1282 vs_fill_borders_RGB (&dest[0], black);
1284 case GST_VIDEO_SCALE_NEAREST:
1285 vs_image_scale_nearest_RGB (&dest[0], &src[0], videoscale->tmp_buf);
1287 case GST_VIDEO_SCALE_BILINEAR:
1288 vs_image_scale_linear_RGB (&dest[0], &src[0], videoscale->tmp_buf);
1290 case GST_VIDEO_SCALE_4TAP:
1291 vs_image_scale_4tap_RGB (&dest[0], &src[0], videoscale->tmp_buf);
1297 case GST_VIDEO_FORMAT_YUY2:
1298 case GST_VIDEO_FORMAT_YVYU:
1300 vs_fill_borders_YUYV (&dest[0], black);
1302 case GST_VIDEO_SCALE_NEAREST:
1303 vs_image_scale_nearest_YUYV (&dest[0], &src[0], videoscale->tmp_buf);
1305 case GST_VIDEO_SCALE_BILINEAR:
1306 vs_image_scale_linear_YUYV (&dest[0], &src[0], videoscale->tmp_buf);
1308 case GST_VIDEO_SCALE_4TAP:
1309 vs_image_scale_4tap_YUYV (&dest[0], &src[0], videoscale->tmp_buf);
1315 case GST_VIDEO_FORMAT_UYVY:
1317 vs_fill_borders_UYVY (&dest[0], black);
1319 case GST_VIDEO_SCALE_NEAREST:
1320 vs_image_scale_nearest_UYVY (&dest[0], &src[0], videoscale->tmp_buf);
1322 case GST_VIDEO_SCALE_BILINEAR:
1323 vs_image_scale_linear_UYVY (&dest[0], &src[0], videoscale->tmp_buf);
1325 case GST_VIDEO_SCALE_4TAP:
1326 vs_image_scale_4tap_UYVY (&dest[0], &src[0], videoscale->tmp_buf);
1332 case GST_VIDEO_FORMAT_Y800:
1333 case GST_VIDEO_FORMAT_GRAY8:
1335 vs_fill_borders_Y (&dest[0], black);
1337 case GST_VIDEO_SCALE_NEAREST:
1338 vs_image_scale_nearest_Y (&dest[0], &src[0], videoscale->tmp_buf);
1340 case GST_VIDEO_SCALE_BILINEAR:
1341 vs_image_scale_linear_Y (&dest[0], &src[0], videoscale->tmp_buf);
1343 case GST_VIDEO_SCALE_4TAP:
1344 vs_image_scale_4tap_Y (&dest[0], &src[0], videoscale->tmp_buf);
1350 case GST_VIDEO_FORMAT_GRAY16_LE:
1351 case GST_VIDEO_FORMAT_GRAY16_BE:
1352 case GST_VIDEO_FORMAT_Y16:
1354 vs_fill_borders_Y16 (&dest[0], 0);
1356 case GST_VIDEO_SCALE_NEAREST:
1357 vs_image_scale_nearest_Y16 (&dest[0], &src[0], videoscale->tmp_buf);
1359 case GST_VIDEO_SCALE_BILINEAR:
1360 vs_image_scale_linear_Y16 (&dest[0], &src[0], videoscale->tmp_buf);
1362 case GST_VIDEO_SCALE_4TAP:
1363 vs_image_scale_4tap_Y16 (&dest[0], &src[0], videoscale->tmp_buf);
1369 case GST_VIDEO_FORMAT_I420:
1370 case GST_VIDEO_FORMAT_YV12:
1371 case GST_VIDEO_FORMAT_Y444:
1372 case GST_VIDEO_FORMAT_Y42B:
1373 case GST_VIDEO_FORMAT_Y41B:
1375 vs_fill_borders_Y (&dest[0], black);
1376 vs_fill_borders_Y (&dest[1], black + 1);
1377 vs_fill_borders_Y (&dest[2], black + 2);
1380 case GST_VIDEO_SCALE_NEAREST:
1381 vs_image_scale_nearest_Y (&dest[0], &src[0], videoscale->tmp_buf);
1382 vs_image_scale_nearest_Y (&dest[1], &src[1], videoscale->tmp_buf);
1383 vs_image_scale_nearest_Y (&dest[2], &src[2], videoscale->tmp_buf);
1385 case GST_VIDEO_SCALE_BILINEAR:
1386 vs_image_scale_linear_Y (&dest[0], &src[0], videoscale->tmp_buf);
1387 vs_image_scale_linear_Y (&dest[1], &src[1], videoscale->tmp_buf);
1388 vs_image_scale_linear_Y (&dest[2], &src[2], videoscale->tmp_buf);
1390 case GST_VIDEO_SCALE_4TAP:
1391 vs_image_scale_4tap_Y (&dest[0], &src[0], videoscale->tmp_buf);
1392 vs_image_scale_4tap_Y (&dest[1], &src[1], videoscale->tmp_buf);
1393 vs_image_scale_4tap_Y (&dest[2], &src[2], videoscale->tmp_buf);
1395 case GST_VIDEO_SCALE_LANCZOS:
1396 vs_image_scale_lanczos_Y (&dest[0], &src[0], videoscale->tmp_buf,
1397 videoscale->sharpness, videoscale->dither, videoscale->submethod,
1398 videoscale->envelope, videoscale->sharpen);
1399 vs_image_scale_lanczos_Y (&dest[1], &src[1], videoscale->tmp_buf,
1400 videoscale->sharpness, videoscale->dither, videoscale->submethod,
1401 videoscale->envelope, videoscale->sharpen);
1402 vs_image_scale_lanczos_Y (&dest[2], &src[2], videoscale->tmp_buf,
1403 videoscale->sharpness, videoscale->dither, videoscale->submethod,
1404 videoscale->envelope, videoscale->sharpen);
1410 case GST_VIDEO_FORMAT_NV12:
1412 case GST_VIDEO_SCALE_NEAREST:
1413 vs_image_scale_nearest_Y (&dest[0], &src[0], videoscale->tmp_buf);
1414 vs_image_scale_nearest_NV12 (&dest[1], &src[1], videoscale->tmp_buf);
1416 case GST_VIDEO_SCALE_BILINEAR:
1417 vs_image_scale_linear_Y (&dest[0], &src[0], videoscale->tmp_buf);
1418 vs_image_scale_linear_NV12 (&dest[1], &src[1], videoscale->tmp_buf);
1424 case GST_VIDEO_FORMAT_RGB16:
1426 vs_fill_borders_RGB565 (&dest[0], black);
1428 case GST_VIDEO_SCALE_NEAREST:
1429 vs_image_scale_nearest_RGB565 (&dest[0], &src[0],
1430 videoscale->tmp_buf);
1432 case GST_VIDEO_SCALE_BILINEAR:
1433 vs_image_scale_linear_RGB565 (&dest[0], &src[0], videoscale->tmp_buf);
1435 case GST_VIDEO_SCALE_4TAP:
1436 vs_image_scale_4tap_RGB565 (&dest[0], &src[0], videoscale->tmp_buf);
1442 case GST_VIDEO_FORMAT_RGB15:
1444 vs_fill_borders_RGB555 (&dest[0], black);
1446 case GST_VIDEO_SCALE_NEAREST:
1447 vs_image_scale_nearest_RGB555 (&dest[0], &src[0],
1448 videoscale->tmp_buf);
1450 case GST_VIDEO_SCALE_BILINEAR:
1451 vs_image_scale_linear_RGB555 (&dest[0], &src[0], videoscale->tmp_buf);
1453 case GST_VIDEO_SCALE_4TAP:
1454 vs_image_scale_4tap_RGB555 (&dest[0], &src[0], videoscale->tmp_buf);
1469 GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1470 ("Unsupported format %d for scaling method %d", format, method));
1471 return GST_FLOW_ERROR;
1475 GST_ELEMENT_ERROR (videoscale, STREAM, NOT_IMPLEMENTED, (NULL),
1476 ("Unknown scaling method %d", videoscale->method));
1477 return GST_FLOW_ERROR;
1482 gst_video_scale_src_event (GstBaseTransform * trans, GstEvent * event)
1484 GstVideoScale *videoscale = GST_VIDEO_SCALE_CAST (trans);
1485 GstVideoFilter *filter = GST_VIDEO_FILTER_CAST (trans);
1488 GstStructure *structure;
1490 GST_DEBUG_OBJECT (videoscale, "handling %s event",
1491 GST_EVENT_TYPE_NAME (event));
1493 switch (GST_EVENT_TYPE (event)) {
1494 case GST_EVENT_NAVIGATION:
1496 GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
1498 structure = (GstStructure *) gst_event_get_structure (event);
1499 if (gst_structure_get_double (structure, "pointer_x", &a)) {
1500 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
1501 a * filter->in_info.width / filter->out_info.width, NULL);
1503 if (gst_structure_get_double (structure, "pointer_y", &a)) {
1504 gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
1505 a * filter->in_info.height / filter->out_info.height, NULL);
1512 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
1518 plugin_init (GstPlugin * plugin)
1520 gst_videoscale_orc_init ();
1522 if (!gst_element_register (plugin, "videoscale", GST_RANK_NONE,
1523 GST_TYPE_VIDEO_SCALE))
1526 GST_DEBUG_CATEGORY_INIT (video_scale_debug, "videoscale", 0,
1527 "videoscale element");
1528 GST_DEBUG_CATEGORY_GET (GST_CAT_PERFORMANCE, "GST_PERFORMANCE");
1535 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1538 "Resizes video", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,