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 /* debug variable definition */
40 GST_DEBUG_CATEGORY (pixbufscale_debug);
41 #define GST_CAT_DEFAULT pixbufscale_debug
43 /* elementfactory information */
44 static GstElementDetails pixbufscale_details =
45 GST_ELEMENT_DETAILS ("Gdk Pixbuf scaler",
46 "Filter/Effect/Video",
48 "Jan Schmidt <thaytan@mad.scientist.com>\nWim Taymans <wim.taymans@chello.be>");
50 /* GstPixbufScale signals and args */
64 static GstStaticPadTemplate gst_pixbufscale_src_template =
65 GST_STATIC_PAD_TEMPLATE ("src",
68 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
71 static GstStaticPadTemplate gst_pixbufscale_sink_template =
72 GST_STATIC_PAD_TEMPLATE ("sink",
75 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
78 #define GST_TYPE_PIXBUFSCALE_METHOD (gst_pixbufscale_method_get_type())
80 gst_pixbufscale_method_get_type (void)
82 static GType pixbufscale_method_type = 0;
83 static GEnumValue pixbufscale_methods[] = {
84 {GST_PIXBUFSCALE_NEAREST, "0", "Nearest Neighbour"},
85 {GST_PIXBUFSCALE_TILES, "1", "Tiles"},
86 {GST_PIXBUFSCALE_BILINEAR, "2", "Bilinear"},
87 {GST_PIXBUFSCALE_HYPER, "3", "Hyper"},
91 if (!pixbufscale_method_type) {
92 pixbufscale_method_type =
93 g_enum_register_static ("GstPixbufScaleMethod", pixbufscale_methods);
95 return pixbufscale_method_type;
98 static void gst_pixbufscale_base_init (gpointer g_class);
99 static void gst_pixbufscale_class_init (GstPixbufScaleClass * klass);
100 static void gst_pixbufscale_init (GstPixbufScale * pixbufscale);
101 static gboolean gst_pixbufscale_handle_src_event (GstPad * pad,
104 static void gst_pixbufscale_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_pixbufscale_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
109 static void gst_pixbufscale_chain (GstPad * pad, GstData * _data);
111 static GstElementClass *parent_class = NULL;
114 gst_pixbufscale_get_type (void)
116 static GType pixbufscale_type = 0;
118 if (!pixbufscale_type) {
119 static const GTypeInfo pixbufscale_info = {
120 sizeof (GstPixbufScaleClass),
121 gst_pixbufscale_base_init,
123 (GClassInitFunc) gst_pixbufscale_class_init,
126 sizeof (GstPixbufScale),
128 (GInstanceInitFunc) gst_pixbufscale_init,
132 g_type_register_static (GST_TYPE_ELEMENT, "GstPixbufScale",
133 &pixbufscale_info, 0);
135 return pixbufscale_type;
139 gst_pixbufscale_base_init (gpointer g_class)
141 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
143 gst_element_class_set_details (element_class, &pixbufscale_details);
145 gst_element_class_add_pad_template (element_class,
146 gst_static_pad_template_get (&gst_pixbufscale_src_template));
147 gst_element_class_add_pad_template (element_class,
148 gst_static_pad_template_get (&gst_pixbufscale_sink_template));
151 gst_pixbufscale_class_init (GstPixbufScaleClass * klass)
153 GObjectClass *gobject_class;
154 GstElementClass *gstelement_class;
156 gobject_class = (GObjectClass *) klass;
157 gstelement_class = (GstElementClass *) klass;
159 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_METHOD,
160 g_param_spec_enum ("method", "method", "method",
161 GST_TYPE_PIXBUFSCALE_METHOD, GST_PIXBUFSCALE_BILINEAR,
164 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
166 gobject_class->set_property = gst_pixbufscale_set_property;
167 gobject_class->get_property = gst_pixbufscale_get_property;
171 gst_pixbufscale_getcaps (GstPad * pad)
173 GstPixbufScale *pixbufscale;
179 pixbufscale = GST_PIXBUFSCALE (gst_pad_get_parent (pad));
181 otherpad = (pad == pixbufscale->srcpad) ? pixbufscale->sinkpad :
183 othercaps = gst_pad_get_allowed_caps (otherpad);
185 caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
186 gst_caps_free (othercaps);
188 for (i = 0; i < gst_caps_get_size (caps); i++) {
189 GstStructure *structure = gst_caps_get_structure (caps, i);
191 gst_structure_set (structure,
192 "width", GST_TYPE_INT_RANGE, 16, 4096,
193 "height", GST_TYPE_INT_RANGE, 16, 4096, NULL);
194 gst_structure_remove_field (structure, "pixel-aspect-ratio");
197 GST_DEBUG ("getcaps are: %" GST_PTR_FORMAT, caps);
201 static GstPadLinkReturn
202 gst_pixbufscale_link (GstPad * pad, const GstCaps * caps)
204 GstPixbufScale *pixbufscale;
205 GstPadLinkReturn ret;
207 GstStructure *structure;
210 GST_DEBUG ("gst_pixbufscale_link %s\n", gst_caps_to_string (caps));
211 pixbufscale = GST_PIXBUFSCALE (gst_pad_get_parent (pad));
213 otherpad = (pad == pixbufscale->srcpad) ? pixbufscale->sinkpad :
216 structure = gst_caps_get_structure (caps, 0);
217 ret = gst_structure_get_int (structure, "width", &width);
218 ret &= gst_structure_get_int (structure, "height", &height);
220 ret = gst_pad_try_set_caps (otherpad, caps);
221 if (ret == GST_PAD_LINK_OK) {
222 /* cool, we can use passthru */
224 pixbufscale->to_width = width;
225 pixbufscale->to_height = height;
226 pixbufscale->from_width = width;
227 pixbufscale->from_height = height;
229 pixbufscale->from_buf_size = GST_RGB24_SIZE (width, height);
230 pixbufscale->to_buf_size = GST_RGB24_SIZE (width, height);
231 pixbufscale->from_stride = GST_RGB24_ROWSTRIDE (width);
232 pixbufscale->to_stride = GST_RGB24_ROWSTRIDE (width);
234 pixbufscale->inited = TRUE;
236 return GST_PAD_LINK_OK;
239 if (gst_pad_is_negotiated (otherpad)) {
240 GstCaps *newcaps = gst_caps_copy (caps);
242 if (pad == pixbufscale->srcpad) {
243 gst_caps_set_simple (newcaps,
244 "width", G_TYPE_INT, pixbufscale->from_width,
245 "height", G_TYPE_INT, pixbufscale->from_height, NULL);
247 gst_caps_set_simple (newcaps,
248 "width", G_TYPE_INT, pixbufscale->to_width,
249 "height", G_TYPE_INT, pixbufscale->to_height, NULL);
251 ret = gst_pad_try_set_caps (otherpad, newcaps);
252 if (GST_PAD_LINK_FAILED (ret)) {
253 return GST_PAD_LINK_REFUSED;
257 pixbufscale->passthru = FALSE;
259 if (pad == pixbufscale->srcpad) {
260 pixbufscale->to_width = width;
261 pixbufscale->to_height = height;
263 pixbufscale->from_width = width;
264 pixbufscale->from_height = height;
267 if (gst_pad_is_negotiated (otherpad)) {
268 pixbufscale->from_buf_size =
269 GST_RGB24_SIZE (pixbufscale->from_width, pixbufscale->from_height);
270 pixbufscale->to_buf_size =
271 GST_RGB24_SIZE (pixbufscale->to_width, pixbufscale->to_height);
272 pixbufscale->from_stride = GST_RGB24_ROWSTRIDE (pixbufscale->from_width);
273 pixbufscale->to_stride = GST_RGB24_ROWSTRIDE (pixbufscale->to_width);
274 pixbufscale->inited = TRUE;
277 return GST_PAD_LINK_OK;
281 gst_pixbufscale_init (GstPixbufScale * pixbufscale)
283 GST_DEBUG_OBJECT (pixbufscale, "_init");
284 pixbufscale->sinkpad =
285 gst_pad_new_from_template (gst_static_pad_template_get
286 (&gst_pixbufscale_sink_template), "sink");
287 gst_element_add_pad (GST_ELEMENT (pixbufscale), pixbufscale->sinkpad);
288 gst_pad_set_chain_function (pixbufscale->sinkpad, gst_pixbufscale_chain);
289 gst_pad_set_link_function (pixbufscale->sinkpad, gst_pixbufscale_link);
290 gst_pad_set_getcaps_function (pixbufscale->sinkpad, gst_pixbufscale_getcaps);
292 pixbufscale->srcpad =
293 gst_pad_new_from_template (gst_static_pad_template_get
294 (&gst_pixbufscale_src_template), "src");
295 gst_element_add_pad (GST_ELEMENT (pixbufscale), pixbufscale->srcpad);
296 gst_pad_set_event_function (pixbufscale->srcpad,
297 gst_pixbufscale_handle_src_event);
298 gst_pad_set_link_function (pixbufscale->srcpad, gst_pixbufscale_link);
299 gst_pad_set_getcaps_function (pixbufscale->srcpad, gst_pixbufscale_getcaps);
301 pixbufscale->inited = FALSE;
303 pixbufscale->method = GST_PIXBUFSCALE_TILES;
304 pixbufscale->gdk_method = GDK_INTERP_TILES;
308 gst_pixbufscale_handle_src_event (GstPad * pad, GstEvent * event)
310 GstPixbufScale *pixbufscale;
312 GstStructure *structure;
315 pixbufscale = GST_PIXBUFSCALE (gst_pad_get_parent (pad));
317 switch (GST_EVENT_TYPE (event)) {
318 case GST_EVENT_NAVIGATION:
319 structure = gst_structure_copy (event->event_data.structure.structure);
320 if (gst_structure_get_double (event->event_data.structure.structure,
322 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
323 a * pixbufscale->from_width / pixbufscale->to_width, NULL);
325 if (gst_structure_get_double (event->event_data.structure.structure,
327 gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
328 a * pixbufscale->from_height / pixbufscale->to_height, NULL);
330 gst_event_unref (event);
331 new_event = gst_event_new (GST_EVENT_NAVIGATION);
332 new_event->event_data.structure.structure = structure;
333 return gst_pad_event_default (pad, new_event);
336 return gst_pad_event_default (pad, event);
342 pixbufscale_scale (GstPixbufScale * scale, unsigned char *dest,
345 GdkPixbuf *src_pixbuf, *dest_pixbuf;
347 src_pixbuf = gdk_pixbuf_new_from_data
348 (src, GDK_COLORSPACE_RGB, FALSE,
349 8, scale->from_width, scale->from_height,
350 GST_RGB24_ROWSTRIDE (scale->from_width), NULL, NULL);
351 dest_pixbuf = gdk_pixbuf_new_from_data
352 (dest, GDK_COLORSPACE_RGB, FALSE,
353 8, scale->to_width, scale->to_height,
354 GST_RGB24_ROWSTRIDE (scale->to_width), NULL, NULL);
355 gdk_pixbuf_scale (src_pixbuf, dest_pixbuf, 0, 0, scale->to_width,
356 scale->to_height, 0, 0,
357 (double) scale->to_width / scale->from_width,
358 (double) scale->to_height / scale->from_height, scale->gdk_method);
360 dest_pixbuf = gdk_pixbuf_scale_simple
361 (src_pixbuf, scale->to_width, scale->to_height, scale->gdk_method);
363 g_object_unref (src_pixbuf);
364 g_object_unref (dest_pixbuf);
368 gst_pixbufscale_chain (GstPad * pad, GstData * _data)
370 GstBuffer *buf = GST_BUFFER (_data);
371 GstPixbufScale *pixbufscale;
376 g_return_if_fail (pad != NULL);
377 g_return_if_fail (GST_IS_PAD (pad));
378 g_return_if_fail (buf != NULL);
380 pixbufscale = GST_PIXBUFSCALE (gst_pad_get_parent (pad));
381 g_return_if_fail (pixbufscale->inited);
383 data = GST_BUFFER_DATA (buf);
384 size = GST_BUFFER_SIZE (buf);
386 if (pixbufscale->passthru) {
387 GST_LOG_OBJECT (pixbufscale, "passing through buffer of %ld bytes in '%s'",
388 size, GST_OBJECT_NAME (pixbufscale));
389 gst_pad_push (pixbufscale->srcpad, GST_DATA (buf));
393 GST_LOG_OBJECT (pixbufscale, "got buffer of %ld bytes in '%s'", size,
394 GST_OBJECT_NAME (pixbufscale));
395 GST_LOG_OBJECT (pixbufscale,
396 "size=%ld from=%dx%d to=%dx%d fromsize=%ld (should be %d) tosize=%d",
397 size, pixbufscale->from_width, pixbufscale->from_height,
398 pixbufscale->to_width, pixbufscale->to_height, size,
399 pixbufscale->from_buf_size, pixbufscale->to_buf_size);
401 if (size != pixbufscale->from_buf_size) {
402 GST_ERROR ("Incoming RGB data is %d bytes (expected: %d bytes) (%dx%d)\n",
403 size, pixbufscale->from_buf_size, pixbufscale->from_width,
404 pixbufscale->from_height);
408 outbuf = gst_pad_alloc_buffer (pixbufscale->srcpad,
409 GST_BUFFER_OFFSET_NONE, pixbufscale->to_buf_size);
411 gst_buffer_stamp (outbuf, buf);
413 pixbufscale_scale (pixbufscale, GST_BUFFER_DATA (outbuf), data);
415 GST_DEBUG_OBJECT (pixbufscale, "pushing buffer of %d bytes in '%s'",
416 GST_BUFFER_SIZE (outbuf), GST_OBJECT_NAME (pixbufscale));
418 gst_pad_push (pixbufscale->srcpad, GST_DATA (outbuf));
420 gst_buffer_unref (buf);
424 gst_pixbufscale_set_property (GObject * object, guint prop_id,
425 const GValue * value, GParamSpec * pspec)
429 g_return_if_fail (GST_IS_PIXBUFSCALE (object));
430 src = GST_PIXBUFSCALE (object);
434 src->method = g_value_get_enum (value);
435 switch (src->method) {
436 case GST_PIXBUFSCALE_NEAREST:
437 src->gdk_method = GDK_INTERP_NEAREST;
439 case GST_PIXBUFSCALE_TILES:
440 src->gdk_method = GDK_INTERP_TILES;
442 case GST_PIXBUFSCALE_BILINEAR:
443 src->gdk_method = GDK_INTERP_BILINEAR;
445 case GST_PIXBUFSCALE_HYPER:
446 src->gdk_method = GDK_INTERP_HYPER;
456 gst_pixbufscale_get_property (GObject * object, guint prop_id, GValue * value,
461 g_return_if_fail (GST_IS_PIXBUFSCALE (object));
462 src = GST_PIXBUFSCALE (object);
466 g_value_set_enum (value, src->method);
469 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
476 pixbufscale_init (GstPlugin * plugin)
478 if (!gst_element_register (plugin, "gdkpixbufscale", GST_RANK_NONE,
479 GST_TYPE_PIXBUFSCALE))
482 GST_DEBUG_CATEGORY_INIT (pixbufscale_debug, "gdkpixbufscale", 0,
483 "pixbufscale element");