2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Jan Schmidt <thaytan@mad.scientist.com>
4 * Copyright (C) <2004> Tim-Philipp Mueller <t.i.m@orange.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include <pixbufscale.h>
27 #include <gst/video/video.h>
28 #include <gdk-pixbuf/gdk-pixbuf.h>
30 #define ROUND_UP_2(x) (((x)+1)&~1)
31 #define ROUND_UP_4(x) (((x)+3)&~3)
32 #define ROUND_UP_8(x) (((x)+7)&~7)
34 /* These match the ones gstffmpegcolorspace uses (Tim) */
35 #define GST_RGB24_ROWSTRIDE(width) (ROUND_UP_4 ((width)*3))
36 #define GST_RGB24_SIZE(width,height) ((height)*GST_RGB24_ROWSTRIDE(width))
39 GST_DEBUG_CATEGORY_STATIC (pixbufscale_debug);
40 #define GST_CAT_DEFAULT pixbufscale_debug
42 /* elementfactory information */
43 static const GstElementDetails pixbufscale_details =
44 GST_ELEMENT_DETAILS ("GdkPixbuf image scaler",
45 "Filter/Effect/Video",
47 "Jan Schmidt <thaytan@mad.scientist.com>\n"
48 "Wim Taymans <wim.taymans@chello.be>\n"
49 "Renato Filho <renato.filho@indt.org.br>");
51 /* GstPixbufScale signals and args */
65 static GstStaticPadTemplate gst_pixbufscale_src_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
69 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
72 static GstStaticPadTemplate gst_pixbufscale_sink_template =
73 GST_STATIC_PAD_TEMPLATE ("sink",
76 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
79 #define GST_TYPE_PIXBUFSCALE_METHOD (gst_pixbufscale_method_get_type())
81 gst_pixbufscale_method_get_type (void)
83 static GType pixbufscale_method_type = 0;
84 static const GEnumValue pixbufscale_methods[] = {
85 {GST_PIXBUFSCALE_NEAREST, "0", "Nearest Neighbour"},
86 {GST_PIXBUFSCALE_TILES, "1", "Tiles"},
87 {GST_PIXBUFSCALE_BILINEAR, "2", "Bilinear"},
88 {GST_PIXBUFSCALE_HYPER, "3", "Hyper"},
92 if (!pixbufscale_method_type) {
93 pixbufscale_method_type =
94 g_enum_register_static ("GstPixbufScaleMethod", pixbufscale_methods);
96 return pixbufscale_method_type;
99 static void gst_pixbufscale_base_init (gpointer g_class);
100 static void gst_pixbufscale_class_init (GstPixbufScaleClass * klass);
101 static void gst_pixbufscale_init (GstPixbufScale * pixbufscale,
102 GstPixbufScaleClass * kclass);
103 static void gst_pixbufscale_set_property (GObject * object, guint prop_id,
104 const GValue * value, GParamSpec * pspec);
105 static void gst_pixbufscale_get_property (GObject * object, guint prop_id,
106 GValue * value, GParamSpec * pspec);
108 static GstCaps *gst_pixbufscale_transform_caps (GstBaseTransform * trans,
109 GstPadDirection direction, GstCaps * caps);
110 static gboolean gst_pixbufscale_set_caps (GstBaseTransform * trans,
111 GstCaps * in, GstCaps * out);
112 static gboolean gst_pixbufscale_get_unit_size (GstBaseTransform * trans,
113 GstCaps * caps, guint * size);
114 static void gst_pixbufscale_fixate_caps (GstBaseTransform * base,
115 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
116 static GstFlowReturn gst_pixbufscale_transform (GstBaseTransform * trans,
117 GstBuffer * in, GstBuffer * out);
118 static gboolean gst_pixbufscale_handle_src_event (GstPad * pad,
122 static gboolean parse_caps (GstCaps * caps, gint * width, gint * height);
124 GST_BOILERPLATE (GstPixbufScale, gst_pixbufscale, GstBaseTransform,
125 GST_TYPE_BASE_TRANSFORM);
128 gst_pixbufscale_base_init (gpointer g_class)
130 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
132 gst_element_class_set_details (element_class, &pixbufscale_details);
134 gst_element_class_add_pad_template (element_class,
135 gst_static_pad_template_get (&gst_pixbufscale_src_template));
136 gst_element_class_add_pad_template (element_class,
137 gst_static_pad_template_get (&gst_pixbufscale_sink_template));
140 gst_pixbufscale_class_init (GstPixbufScaleClass * klass)
142 GObjectClass *gobject_class;
143 GstBaseTransformClass *trans_class;
145 gobject_class = (GObjectClass *) klass;
146 trans_class = (GstBaseTransformClass *) klass;
148 gobject_class->set_property = gst_pixbufscale_set_property;
149 gobject_class->get_property = gst_pixbufscale_get_property;
151 g_object_class_install_property (gobject_class,
153 g_param_spec_enum ("method", "method", "method",
154 GST_TYPE_PIXBUFSCALE_METHOD, GST_PIXBUFSCALE_BILINEAR,
157 trans_class->transform_caps =
158 GST_DEBUG_FUNCPTR (gst_pixbufscale_transform_caps);
159 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_pixbufscale_set_caps);
160 trans_class->get_unit_size =
161 GST_DEBUG_FUNCPTR (gst_pixbufscale_get_unit_size);
162 trans_class->transform = GST_DEBUG_FUNCPTR (gst_pixbufscale_transform);
163 trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_pixbufscale_fixate_caps);
164 trans_class->passthrough_on_same_caps = TRUE;
166 parent_class = g_type_class_peek_parent (klass);
170 gst_pixbufscale_init (GstPixbufScale * pixbufscale,
171 GstPixbufScaleClass * kclass)
173 GstBaseTransform *trans;
175 GST_DEBUG_OBJECT (pixbufscale, "_init");
176 trans = GST_BASE_TRANSFORM (pixbufscale);
178 gst_pad_set_event_function (trans->srcpad, gst_pixbufscale_handle_src_event);
180 pixbufscale->method = GST_PIXBUFSCALE_TILES;
181 pixbufscale->gdk_method = GDK_INTERP_TILES;
185 gst_pixbufscale_set_property (GObject * object, guint prop_id,
186 const GValue * value, GParamSpec * pspec)
190 g_return_if_fail (GST_IS_PIXBUFSCALE (object));
191 src = GST_PIXBUFSCALE (object);
195 src->method = g_value_get_enum (value);
196 switch (src->method) {
197 case GST_PIXBUFSCALE_NEAREST:
198 src->gdk_method = GDK_INTERP_NEAREST;
200 case GST_PIXBUFSCALE_TILES:
201 src->gdk_method = GDK_INTERP_TILES;
203 case GST_PIXBUFSCALE_BILINEAR:
204 src->gdk_method = GDK_INTERP_BILINEAR;
206 case GST_PIXBUFSCALE_HYPER:
207 src->gdk_method = GDK_INTERP_HYPER;
217 gst_pixbufscale_get_property (GObject * object, guint prop_id, GValue * value,
222 g_return_if_fail (GST_IS_PIXBUFSCALE (object));
223 src = GST_PIXBUFSCALE (object);
227 g_value_set_enum (value, src->method);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237 gst_pixbufscale_transform_caps (GstBaseTransform * trans,
238 GstPadDirection direction, GstCaps * caps)
240 GstPixbufScale *pixbufscale;
244 pixbufscale = GST_PIXBUFSCALE (trans);
245 ret = gst_caps_copy (caps);
247 for (i = 0; i < gst_caps_get_size (ret); i++) {
248 GstStructure *structure = gst_caps_get_structure (ret, i);
250 gst_structure_set (structure,
251 "width", GST_TYPE_INT_RANGE, 16, 4096,
252 "height", GST_TYPE_INT_RANGE, 16, 4096, NULL);
253 gst_structure_remove_field (structure, "pixel-aspect-ratio");
256 GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
261 parse_caps (GstCaps * caps, gint * width, gint * height)
264 GstStructure *structure;
266 structure = gst_caps_get_structure (caps, 0);
267 ret = gst_structure_get_int (structure, "width", width);
268 ret &= gst_structure_get_int (structure, "height", height);
274 gst_pixbufscale_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
276 GstPixbufScale *pixbufscale;
279 pixbufscale = GST_PIXBUFSCALE (trans);
280 ret = parse_caps (in, &pixbufscale->from_width, &pixbufscale->from_height);
281 ret &= parse_caps (out, &pixbufscale->to_width, &pixbufscale->to_height);
285 pixbufscale->from_stride = GST_ROUND_UP_4 (pixbufscale->from_width * 3);
286 pixbufscale->from_buf_size =
287 pixbufscale->from_stride * pixbufscale->from_height;
289 pixbufscale->to_stride = GST_ROUND_UP_4 (pixbufscale->to_width * 3);
290 pixbufscale->to_buf_size = pixbufscale->to_stride * pixbufscale->to_height;
292 GST_DEBUG_OBJECT (pixbufscale, "from=%dx%d, size %d -> to=%dx%d, size %d",
293 pixbufscale->from_width, pixbufscale->from_height,
294 pixbufscale->from_buf_size, pixbufscale->to_width, pixbufscale->to_height,
295 pixbufscale->to_buf_size);
303 gst_pixbufscale_get_unit_size (GstBaseTransform * trans,
304 GstCaps * caps, guint * size)
306 GstPixbufScale *pixbufscale;
311 pixbufscale = GST_PIXBUFSCALE (trans);
313 if (!parse_caps (caps, &width, &height))
316 *size = GST_ROUND_UP_4 (width * 3) * height;
322 gst_pixbufscale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
323 GstCaps * caps, GstCaps * othercaps)
325 GstStructure *ins, *outs;
326 const GValue *from_par, *to_par;
328 g_return_if_fail (gst_caps_is_fixed (caps));
330 GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
331 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
333 ins = gst_caps_get_structure (caps, 0);
334 outs = gst_caps_get_structure (othercaps, 0);
336 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
337 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
339 if (from_par && to_par) {
340 GValue to_ratio = { 0, }; /* w/h of output video */
341 int from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
342 int count = 0, w = 0, h = 0, num, den;
344 /* if both width and height are already fixed, we can't do anything
345 * * about it anymore */
346 if (gst_structure_get_int (outs, "width", &w))
348 if (gst_structure_get_int (outs, "height", &h))
351 GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
356 gst_structure_get_int (ins, "width", &from_w);
357 gst_structure_get_int (ins, "height", &from_h);
358 from_par_n = gst_value_get_fraction_numerator (from_par);
359 from_par_d = gst_value_get_fraction_denominator (from_par);
360 to_par_n = gst_value_get_fraction_numerator (to_par);
361 to_par_d = gst_value_get_fraction_denominator (to_par);
363 g_value_init (&to_ratio, GST_TYPE_FRACTION);
364 gst_value_set_fraction (&to_ratio, from_w * from_par_n * to_par_d,
365 from_h * from_par_d * to_par_n);
366 num = gst_value_get_fraction_numerator (&to_ratio);
367 den = gst_value_get_fraction_denominator (&to_ratio);
368 GST_DEBUG_OBJECT (base,
369 "scaling input with %dx%d and PAR %d/%d to output PAR %d/%d",
370 from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d);
371 GST_DEBUG_OBJECT (base,
372 "resulting output should respect ratio of %d/%d", num, den);
374 /* now find a width x height that respects this display ratio.
375 * * prefer those that have one of w/h the same as the incoming video
376 * * using wd / hd = num / den */
378 /* start with same height, because of interlaced video */
379 /* check hd / den is an integer scale factor, and scale wd with the PAR */
380 if (from_h % den == 0) {
381 GST_DEBUG_OBJECT (base, "keeping video height");
384 } else if (from_w % num == 0) {
385 GST_DEBUG_OBJECT (base, "keeping video width");
389 GST_DEBUG_OBJECT (base, "approximating but keeping video height");
393 GST_DEBUG_OBJECT (base, "scaling to %dx%d", w, h);
395 gst_structure_fixate_field_nearest_int (outs, "width", w);
396 gst_structure_fixate_field_nearest_int (outs, "height", h);
400 if (gst_structure_get_int (ins, "width", &width)) {
401 if (gst_structure_has_field (outs, "width")) {
402 gst_structure_fixate_field_nearest_int (outs, "width", width);
405 if (gst_structure_get_int (ins, "height", &height)) {
406 if (gst_structure_has_field (outs, "height")) {
407 gst_structure_fixate_field_nearest_int (outs, "height", height);
412 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
416 gst_pixbufscale_transform (GstBaseTransform * trans,
417 GstBuffer * in, GstBuffer * out)
419 GstPixbufScale *scale;
420 GdkPixbuf *src_pixbuf, *dest_pixbuf;
422 scale = GST_PIXBUFSCALE (trans);
425 gdk_pixbuf_new_from_data (GST_BUFFER_DATA (in), GDK_COLORSPACE_RGB, FALSE,
426 8, scale->from_width, scale->from_height,
427 GST_RGB24_ROWSTRIDE (scale->from_width), NULL, NULL);
430 gdk_pixbuf_new_from_data (GST_BUFFER_DATA (out), GDK_COLORSPACE_RGB,
431 FALSE, 8, scale->to_width, scale->to_height,
432 GST_RGB24_ROWSTRIDE (scale->to_width), NULL, NULL);
434 gdk_pixbuf_scale (src_pixbuf, dest_pixbuf, 0, 0,
436 scale->to_height, 0, 0,
437 (double) scale->to_width / scale->from_width,
438 (double) scale->to_height / scale->from_height, scale->gdk_method);
440 g_object_unref (src_pixbuf);
441 g_object_unref (dest_pixbuf);
447 gst_pixbufscale_handle_src_event (GstPad * pad, GstEvent * event)
449 GstPixbufScale *pixbufscale;
452 GstStructure *structure;
454 pixbufscale = GST_PIXBUFSCALE (gst_pad_get_parent (pad));
456 GST_DEBUG_OBJECT (pixbufscale, "handling %s event",
457 GST_EVENT_TYPE_NAME (event));
459 switch (GST_EVENT_TYPE (event)) {
460 case GST_EVENT_NAVIGATION:
462 GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
464 structure = (GstStructure *) gst_event_get_structure (event);
465 if (gst_structure_get_double (structure, "pointer_x", &a)) {
466 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
467 a * pixbufscale->from_width / pixbufscale->to_width, NULL);
469 if (gst_structure_get_double (structure, "pointer_y", &a)) {
470 gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
471 a * pixbufscale->from_height / pixbufscale->to_height, NULL);
478 ret = gst_pad_event_default (pad, event);
480 gst_object_unref (pixbufscale);
486 pixbufscale_init (GstPlugin * plugin)
488 if (!gst_element_register (plugin, "gdkpixbufscale", GST_RANK_NONE,
489 GST_TYPE_PIXBUFSCALE))
492 GST_DEBUG_CATEGORY_INIT (pixbufscale_debug, "gdkpixbufscale", 0,
493 "pixbufscale element");