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 /* GstPixbufScale signals and args */
56 static GstStaticPadTemplate gst_pixbufscale_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
60 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
63 static GstStaticPadTemplate gst_pixbufscale_sink_template =
64 GST_STATIC_PAD_TEMPLATE ("sink",
67 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
70 #define GST_TYPE_PIXBUFSCALE_METHOD (gst_pixbufscale_method_get_type())
72 gst_pixbufscale_method_get_type (void)
74 static GType pixbufscale_method_type = 0;
75 static const GEnumValue pixbufscale_methods[] = {
76 {GST_PIXBUFSCALE_NEAREST, "0", "Nearest Neighbour"},
77 {GST_PIXBUFSCALE_TILES, "1", "Tiles"},
78 {GST_PIXBUFSCALE_BILINEAR, "2", "Bilinear"},
79 {GST_PIXBUFSCALE_HYPER, "3", "Hyper"},
83 if (!pixbufscale_method_type) {
84 pixbufscale_method_type =
85 g_enum_register_static ("GstPixbufScaleMethod", pixbufscale_methods);
87 return pixbufscale_method_type;
90 static void gst_pixbufscale_set_property (GObject * object, guint prop_id,
91 const GValue * value, GParamSpec * pspec);
92 static void gst_pixbufscale_get_property (GObject * object, guint prop_id,
93 GValue * value, GParamSpec * pspec);
95 static GstCaps *gst_pixbufscale_transform_caps (GstBaseTransform * trans,
96 GstPadDirection direction, GstCaps * caps);
97 static gboolean gst_pixbufscale_set_caps (GstBaseTransform * trans,
98 GstCaps * in, GstCaps * out);
99 static gboolean gst_pixbufscale_get_unit_size (GstBaseTransform * trans,
100 GstCaps * caps, guint * size);
101 static void gst_pixbufscale_fixate_caps (GstBaseTransform * base,
102 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
103 static GstFlowReturn gst_pixbufscale_transform (GstBaseTransform * trans,
104 GstBuffer * in, GstBuffer * out);
105 static gboolean gst_pixbufscale_handle_src_event (GstPad * pad,
109 static gboolean parse_caps (GstCaps * caps, gint * width, gint * height);
111 GST_BOILERPLATE (GstPixbufScale, gst_pixbufscale, GstBaseTransform,
112 GST_TYPE_BASE_TRANSFORM);
115 gst_pixbufscale_base_init (gpointer g_class)
117 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
119 gst_element_class_set_details_simple (element_class, "GdkPixbuf image scaler",
120 "Filter/Effect/Video", "Resizes video",
121 "Jan Schmidt <thaytan@mad.scientist.com>, "
122 "Wim Taymans <wim.taymans@chello.be>, "
123 "Renato Filho <renato.filho@indt.org.br>");
125 gst_element_class_add_static_pad_template (element_class,
126 &gst_pixbufscale_src_template);
127 gst_element_class_add_static_pad_template (element_class,
128 &gst_pixbufscale_sink_template);
132 gst_pixbufscale_class_init (GstPixbufScaleClass * klass)
134 GObjectClass *gobject_class;
135 GstBaseTransformClass *trans_class;
137 gobject_class = (GObjectClass *) klass;
138 trans_class = (GstBaseTransformClass *) klass;
140 gobject_class->set_property = gst_pixbufscale_set_property;
141 gobject_class->get_property = gst_pixbufscale_get_property;
143 g_object_class_install_property (gobject_class,
145 g_param_spec_enum ("method", "method", "method",
146 GST_TYPE_PIXBUFSCALE_METHOD, GST_PIXBUFSCALE_BILINEAR,
147 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149 trans_class->transform_caps =
150 GST_DEBUG_FUNCPTR (gst_pixbufscale_transform_caps);
151 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_pixbufscale_set_caps);
152 trans_class->get_unit_size =
153 GST_DEBUG_FUNCPTR (gst_pixbufscale_get_unit_size);
154 trans_class->transform = GST_DEBUG_FUNCPTR (gst_pixbufscale_transform);
155 trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_pixbufscale_fixate_caps);
156 trans_class->passthrough_on_same_caps = TRUE;
158 parent_class = g_type_class_peek_parent (klass);
162 gst_pixbufscale_init (GstPixbufScale * pixbufscale,
163 GstPixbufScaleClass * kclass)
165 GstBaseTransform *trans;
167 GST_DEBUG_OBJECT (pixbufscale, "_init");
168 trans = GST_BASE_TRANSFORM (pixbufscale);
170 gst_pad_set_event_function (trans->srcpad, gst_pixbufscale_handle_src_event);
172 pixbufscale->method = GST_PIXBUFSCALE_TILES;
173 pixbufscale->gdk_method = GDK_INTERP_TILES;
177 gst_pixbufscale_set_property (GObject * object, guint prop_id,
178 const GValue * value, GParamSpec * pspec)
182 g_return_if_fail (GST_IS_PIXBUFSCALE (object));
183 src = GST_PIXBUFSCALE (object);
187 src->method = g_value_get_enum (value);
188 switch (src->method) {
189 case GST_PIXBUFSCALE_NEAREST:
190 src->gdk_method = GDK_INTERP_NEAREST;
192 case GST_PIXBUFSCALE_TILES:
193 src->gdk_method = GDK_INTERP_TILES;
195 case GST_PIXBUFSCALE_BILINEAR:
196 src->gdk_method = GDK_INTERP_BILINEAR;
198 case GST_PIXBUFSCALE_HYPER:
199 src->gdk_method = GDK_INTERP_HYPER;
209 gst_pixbufscale_get_property (GObject * object, guint prop_id, GValue * value,
214 g_return_if_fail (GST_IS_PIXBUFSCALE (object));
215 src = GST_PIXBUFSCALE (object);
219 g_value_set_enum (value, src->method);
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229 gst_pixbufscale_transform_caps (GstBaseTransform * trans,
230 GstPadDirection direction, GstCaps * caps)
235 ret = gst_caps_copy (caps);
237 for (i = 0; i < gst_caps_get_size (ret); i++) {
238 GstStructure *structure = gst_caps_get_structure (ret, i);
240 gst_structure_set (structure,
241 "width", GST_TYPE_INT_RANGE, 16, 4096,
242 "height", GST_TYPE_INT_RANGE, 16, 4096, NULL);
243 gst_structure_remove_field (structure, "pixel-aspect-ratio");
246 GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
251 parse_caps (GstCaps * caps, gint * width, gint * height)
254 GstStructure *structure;
256 structure = gst_caps_get_structure (caps, 0);
257 ret = gst_structure_get_int (structure, "width", width);
258 ret &= gst_structure_get_int (structure, "height", height);
264 gst_pixbufscale_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
266 GstPixbufScale *pixbufscale;
269 pixbufscale = GST_PIXBUFSCALE (trans);
270 ret = parse_caps (in, &pixbufscale->from_width, &pixbufscale->from_height);
271 ret &= parse_caps (out, &pixbufscale->to_width, &pixbufscale->to_height);
275 pixbufscale->from_stride = GST_ROUND_UP_4 (pixbufscale->from_width * 3);
276 pixbufscale->from_buf_size =
277 pixbufscale->from_stride * pixbufscale->from_height;
279 pixbufscale->to_stride = GST_ROUND_UP_4 (pixbufscale->to_width * 3);
280 pixbufscale->to_buf_size = pixbufscale->to_stride * pixbufscale->to_height;
282 GST_DEBUG_OBJECT (pixbufscale, "from=%dx%d, size %d -> to=%dx%d, size %d",
283 pixbufscale->from_width, pixbufscale->from_height,
284 pixbufscale->from_buf_size, pixbufscale->to_width, pixbufscale->to_height,
285 pixbufscale->to_buf_size);
293 gst_pixbufscale_get_unit_size (GstBaseTransform * trans,
294 GstCaps * caps, guint * size)
300 if (!parse_caps (caps, &width, &height))
303 *size = GST_ROUND_UP_4 (width * 3) * height;
309 gst_pixbufscale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
310 GstCaps * caps, GstCaps * othercaps)
312 GstStructure *ins, *outs;
313 const GValue *from_par, *to_par;
315 g_return_if_fail (gst_caps_is_fixed (caps));
317 GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
318 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
320 ins = gst_caps_get_structure (caps, 0);
321 outs = gst_caps_get_structure (othercaps, 0);
323 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
324 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
326 if (from_par && to_par) {
327 GValue to_ratio = { 0, }; /* w/h of output video */
328 int from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
329 int count = 0, w = 0, h = 0, num, den;
331 /* if both width and height are already fixed, we can't do anything
332 * * about it anymore */
333 if (gst_structure_get_int (outs, "width", &w))
335 if (gst_structure_get_int (outs, "height", &h))
338 GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
343 gst_structure_get_int (ins, "width", &from_w);
344 gst_structure_get_int (ins, "height", &from_h);
345 from_par_n = gst_value_get_fraction_numerator (from_par);
346 from_par_d = gst_value_get_fraction_denominator (from_par);
347 to_par_n = gst_value_get_fraction_numerator (to_par);
348 to_par_d = gst_value_get_fraction_denominator (to_par);
350 g_value_init (&to_ratio, GST_TYPE_FRACTION);
351 gst_value_set_fraction (&to_ratio, from_w * from_par_n * to_par_d,
352 from_h * from_par_d * to_par_n);
353 num = gst_value_get_fraction_numerator (&to_ratio);
354 den = gst_value_get_fraction_denominator (&to_ratio);
355 GST_DEBUG_OBJECT (base,
356 "scaling input with %dx%d and PAR %d/%d to output PAR %d/%d",
357 from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d);
358 GST_DEBUG_OBJECT (base,
359 "resulting output should respect ratio of %d/%d", num, den);
361 /* now find a width x height that respects this display ratio.
362 * * prefer those that have one of w/h the same as the incoming video
363 * * using wd / hd = num / den */
365 /* start with same height, because of interlaced video */
366 /* check hd / den is an integer scale factor, and scale wd with the PAR */
367 if (from_h % den == 0) {
368 GST_DEBUG_OBJECT (base, "keeping video height");
371 } else if (from_w % num == 0) {
372 GST_DEBUG_OBJECT (base, "keeping video width");
376 GST_DEBUG_OBJECT (base, "approximating but keeping video height");
380 GST_DEBUG_OBJECT (base, "scaling to %dx%d", w, h);
382 gst_structure_fixate_field_nearest_int (outs, "width", w);
383 gst_structure_fixate_field_nearest_int (outs, "height", h);
387 if (gst_structure_get_int (ins, "width", &width)) {
388 if (gst_structure_has_field (outs, "width")) {
389 gst_structure_fixate_field_nearest_int (outs, "width", width);
392 if (gst_structure_get_int (ins, "height", &height)) {
393 if (gst_structure_has_field (outs, "height")) {
394 gst_structure_fixate_field_nearest_int (outs, "height", height);
399 GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
403 gst_pixbufscale_transform (GstBaseTransform * trans,
404 GstBuffer * in, GstBuffer * out)
406 GstPixbufScale *scale;
407 GdkPixbuf *src_pixbuf, *dest_pixbuf;
409 scale = GST_PIXBUFSCALE (trans);
412 gdk_pixbuf_new_from_data (GST_BUFFER_DATA (in), GDK_COLORSPACE_RGB, FALSE,
413 8, scale->from_width, scale->from_height,
414 GST_RGB24_ROWSTRIDE (scale->from_width), NULL, NULL);
417 gdk_pixbuf_new_from_data (GST_BUFFER_DATA (out), GDK_COLORSPACE_RGB,
418 FALSE, 8, scale->to_width, scale->to_height,
419 GST_RGB24_ROWSTRIDE (scale->to_width), NULL, NULL);
421 gdk_pixbuf_scale (src_pixbuf, dest_pixbuf, 0, 0,
423 scale->to_height, 0, 0,
424 (double) scale->to_width / scale->from_width,
425 (double) scale->to_height / scale->from_height, scale->gdk_method);
427 g_object_unref (src_pixbuf);
428 g_object_unref (dest_pixbuf);
434 gst_pixbufscale_handle_src_event (GstPad * pad, GstEvent * event)
436 GstPixbufScale *pixbufscale;
439 GstStructure *structure;
441 pixbufscale = GST_PIXBUFSCALE (gst_pad_get_parent (pad));
443 GST_DEBUG_OBJECT (pixbufscale, "handling %s event",
444 GST_EVENT_TYPE_NAME (event));
446 switch (GST_EVENT_TYPE (event)) {
447 case GST_EVENT_NAVIGATION:
449 GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
451 structure = (GstStructure *) gst_event_get_structure (event);
452 if (gst_structure_get_double (structure, "pointer_x", &a)) {
453 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
454 a * pixbufscale->from_width / pixbufscale->to_width, NULL);
456 if (gst_structure_get_double (structure, "pointer_y", &a)) {
457 gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
458 a * pixbufscale->from_height / pixbufscale->to_height, NULL);
465 ret = gst_pad_event_default (pad, event);
467 gst_object_unref (pixbufscale);
473 pixbufscale_init (GstPlugin * plugin)
475 if (!gst_element_register (plugin, "gdkpixbufscale", GST_RANK_NONE,
476 GST_TYPE_PIXBUFSCALE))
479 GST_DEBUG_CATEGORY_INIT (pixbufscale_debug, "gdkpixbufscale", 0,
480 "pixbufscale element");