4 * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
5 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <gst/video/video.h>
31 #include "gstgdkpixbuf.h"
33 GST_DEBUG_CATEGORY_STATIC (gst_gdk_pixbuf_debug);
34 #define GST_CAT_DEFAULT gst_gdk_pixbuf_debug
36 static GstElementDetails plugin_details = {
37 "GdkPixbuf image decoder",
38 "Codec/Decoder/Image",
39 "Decodes images in a video stream using GdkPixbuf",
40 "David A. Schleef <ds@schleef.org>, Renato Filho <renato.filho@indt.org.br>",
43 /* Filter signals and args */
58 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
59 GST_STATIC_PAD_TEMPLATE ("sink",
62 GST_STATIC_CAPS ("image/png; "
66 "application/x-navi-animation; "
67 "image/x-cmu-raster; "
68 "image/x-sun-raster; "
71 "image/x-portable-anymap; "
72 "image/x-portable-bitmap; "
73 "image/x-portable-graymap; "
74 "image/x-portable-pixmap; "
78 "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga; "
79 "image/x-pcx; image/svg; image/svg+xml")
82 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
83 GST_STATIC_PAD_TEMPLATE ("src",
86 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
89 gboolean pixbufscale_init (GstPlugin * plugin);
91 static void gst_gdk_pixbuf_base_init (gpointer g_class);
92 static void gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass);
93 static void gst_gdk_pixbuf_init (GstGdkPixbuf * filter,
94 GstGdkPixbufClass * klass);
96 static void gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
97 const GValue * value, GParamSpec * pspec);
98 static void gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
99 GValue * value, GParamSpec * pspec);
101 static GstFlowReturn gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buffer);
102 static gboolean gst_gdk_pixbuf_event (GstPad * pad, GstEvent * event);
104 #ifdef enable_typefind
105 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
109 GST_BOILERPLATE (GstGdkPixbuf, gst_gdk_pixbuf, GstElement, GST_TYPE_ELEMENT)
111 static gboolean gst_gdk_pixbuf_sink_setcaps (GstPad * pad, GstCaps * caps)
113 GstGdkPixbuf *filter;
114 const GValue *framerate;
117 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
118 s = gst_caps_get_structure (caps, 0);
120 filter->framerate_numerator = 0;
121 filter->framerate_denominator = 1;
122 if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
123 filter->framerate_numerator = gst_value_get_fraction_numerator (framerate);
124 filter->framerate_denominator =
125 gst_value_get_fraction_denominator (framerate);
126 GST_DEBUG ("got framerate of %d/%d fps => packetized mode",
127 filter->framerate_numerator, filter->framerate_denominator);
129 gst_object_unref (filter);
134 #if GDK_PIXBUF_MAJOR == 2 && GDK_PIXBUF_MINOR < 2
135 /* gdk-pixbuf prior to 2.2 didn't have gdk_pixbuf_get_formats().
136 * These are just the formats that gdk-pixbuf is known to support.
137 * But maybe not -- it may have been compiled without an external
141 gst_gdk_pixbuf_get_capslist (void)
143 return gst_caps_copy (gst_static_caps_get (&gst_gdk_pixbuf_sink_template.
148 gst_gdk_pixbuf_get_capslist (void)
152 GdkPixbufFormat *pixbuf_format;
155 GstCaps *capslist = NULL;
156 GstCaps *return_caps = NULL;
158 capslist = gst_caps_new_empty ();
159 slist0 = gdk_pixbuf_get_formats ();
161 for (slist = slist0; slist; slist = g_slist_next (slist)) {
162 pixbuf_format = slist->data;
163 mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
164 for (mimetype = mimetypes; *mimetype; mimetype++) {
165 gst_caps_append_structure (capslist, gst_structure_new (*mimetype, NULL));
169 g_slist_free (slist0);
171 return_caps = gst_caps_intersect (capslist,
172 gst_static_caps_get (&gst_gdk_pixbuf_sink_template.static_caps));
174 gst_caps_unref (capslist);
180 gst_gdk_pixbuf_sink_getcaps (GstPad * pad)
182 GstGdkPixbuf *filter;
184 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
185 g_return_val_if_fail (filter != NULL, NULL);
186 g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter), NULL);
187 gst_object_unref (filter);
189 return gst_gdk_pixbuf_get_capslist ();
193 gst_gdk_pixbuf_base_init (gpointer g_class)
195 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
197 gst_element_class_add_pad_template (element_class,
198 gst_static_pad_template_get (&gst_gdk_pixbuf_src_template));
199 gst_element_class_add_pad_template (element_class,
200 gst_static_pad_template_get (&gst_gdk_pixbuf_sink_template));
201 gst_element_class_set_details (element_class, &plugin_details);
204 /* initialize the plugin's class */
206 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
208 GObjectClass *gobject_class;
209 GstElementClass *gstelement_class;
211 gobject_class = (GObjectClass *) klass;
212 gstelement_class = (GstElementClass *) klass;
214 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
216 gobject_class->set_property = gst_gdk_pixbuf_set_property;
217 gobject_class->get_property = gst_gdk_pixbuf_get_property;
220 g_object_class_install_property (gobject_class, ARG_SILENT,
221 g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
222 FALSE, G_PARAM_READWRITE));
227 gst_gdk_pixbuf_init (GstGdkPixbuf * filter, GstGdkPixbufClass * klass)
230 gst_pad_new_from_template (gst_static_pad_template_get
231 (&gst_gdk_pixbuf_sink_template), "sink");
232 gst_pad_set_setcaps_function (filter->sinkpad, gst_gdk_pixbuf_sink_setcaps);
233 gst_pad_set_getcaps_function (filter->sinkpad, gst_gdk_pixbuf_sink_getcaps);
235 gst_pad_set_chain_function (filter->sinkpad,
236 (GstPadChainFunction) gst_gdk_pixbuf_chain);
238 gst_pad_set_event_function (filter->sinkpad, gst_gdk_pixbuf_event);
241 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
244 filter->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
245 (&gst_gdk_pixbuf_src_template), "src");
247 gst_pad_use_fixed_caps (filter->srcpad);
248 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
250 filter->last_timestamp = GST_CLOCK_TIME_NONE;
251 filter->pixbuf_loader = NULL;
255 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
264 GstCaps *caps = NULL;
266 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
267 if (pixbuf == NULL) {
268 GST_DEBUG ("error geting pixbuf");
269 return GST_FLOW_ERROR;
272 if (filter->image_size == 0) {
273 filter->width = gdk_pixbuf_get_width (pixbuf);
274 filter->height = gdk_pixbuf_get_height (pixbuf);
275 filter->rowstride = gdk_pixbuf_get_rowstride (pixbuf);
276 filter->image_size = filter->rowstride * filter->height;
278 if (gdk_pixbuf_get_rowstride (pixbuf) / filter->width == 4) {
279 caps = gst_caps_from_string (GST_VIDEO_CAPS_RGBA);
280 } else if (gdk_pixbuf_get_rowstride (pixbuf) / filter->width == 3) {
281 caps = gst_caps_from_string (GST_VIDEO_CAPS_RGB);
283 GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, (NULL),
284 ("Bpp %d not supported", gdk_pixbuf_get_bits_per_sample (pixbuf)));
285 return GST_FLOW_ERROR;
287 gst_caps_set_simple (caps,
288 "width", G_TYPE_INT, filter->width,
289 "height", G_TYPE_INT, filter->height,
290 "framerate", GST_TYPE_FRACTION, filter->framerate_numerator,
291 filter->framerate_denominator, NULL);
293 GST_DEBUG ("Set size to %dx%d", filter->width, filter->height);
294 gst_pad_set_caps (filter->srcpad, caps);
295 gst_caps_unref (caps);
298 ret = gst_pad_alloc_buffer_and_set_caps (filter->srcpad,
299 GST_BUFFER_OFFSET_NONE,
300 filter->image_size, GST_PAD_CAPS (filter->srcpad), &outbuf);
302 if (ret != GST_FLOW_OK) {
303 GST_DEBUG ("Failed to create outbuffer - %d", ret);
304 return GST_FLOW_ERROR;
307 gst_caps_unref (caps);
308 caps = gst_pad_get_negotiated_caps (filter->srcpad);
309 GST_DEBUG ("Caps negotiated %s", gst_caps_to_string (caps));
310 gst_caps_unref (caps);
312 GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
313 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
315 in_pix = gdk_pixbuf_get_pixels (pixbuf);
316 in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
317 out_pix = GST_BUFFER_DATA (outbuf);
319 for (y = 0; y < filter->height; y++) {
320 memcpy (out_pix, in_pix, filter->rowstride);
321 in_pix += in_rowstride;
322 out_pix += filter->rowstride;
325 GST_DEBUG ("pushing... %d bytes", GST_BUFFER_SIZE (outbuf));
326 return gst_pad_push (filter->srcpad, outbuf);
330 gst_gdk_pixbuf_event (GstPad * pad, GstEvent * event)
332 GstFlowReturn res = GST_FLOW_OK;
334 GstGdkPixbuf *pixbuf;
336 pixbuf = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
338 switch (GST_EVENT_TYPE (event)) {
340 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
341 res = gst_gdk_pixbuf_flush (pixbuf);
342 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
344 case GST_EVENT_NEWSEGMENT:
345 case GST_EVENT_FLUSH_STOP:
346 if (pixbuf->pixbuf_loader != NULL) {
347 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
348 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
349 pixbuf->pixbuf_loader = NULL;
356 if (res == GST_FLOW_OK) {
357 ret = gst_pad_event_default (pad, event);
362 gst_object_unref (pixbuf);
368 gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buf)
370 GstGdkPixbuf *filter;
371 GstFlowReturn ret = GST_FLOW_OK;
372 GError *error = NULL;
373 GstClockTime timestamp;
375 GST_DEBUG ("gst_gdk_pixbuf_chain");
376 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
378 timestamp = GST_BUFFER_TIMESTAMP (buf);
380 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
381 filter->last_timestamp = timestamp;
384 if (filter->pixbuf_loader == NULL) {
385 filter->pixbuf_loader = gdk_pixbuf_loader_new ();
388 GST_DEBUG ("Writing buffer size %d", GST_BUFFER_SIZE (buf));
389 if (gdk_pixbuf_loader_write (filter->pixbuf_loader, GST_BUFFER_DATA (buf),
390 GST_BUFFER_SIZE (buf), &error) == FALSE) {
391 GST_DEBUG ("gst_gdk_pixbuf_chain ERROR: %s", error->message);
392 ret = GST_FLOW_ERROR;
397 gst_object_unref (filter);
402 gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
403 const GValue * value, GParamSpec * pspec)
405 GstGdkPixbuf *filter;
407 g_return_if_fail (GST_IS_GDK_PIXBUF (object));
408 filter = GST_GDK_PIXBUF (object);
412 /* filter->silent = g_value_get_boolean (value); */
415 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
421 gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
422 GValue * value, GParamSpec * pspec)
424 GstGdkPixbuf *filter;
426 g_return_if_fail (GST_IS_GDK_PIXBUF (object));
427 filter = GST_GDK_PIXBUF (object);
431 /* g_value_set_boolean (value, filter->silent); */
434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
439 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
441 #ifdef enable_typefind
443 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
446 GdkPixbufLoader *pixbuf_loader;
447 GdkPixbufFormat *format;
449 data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
453 GST_DEBUG ("creating new loader");
455 pixbuf_loader = gdk_pixbuf_loader_new ();
457 gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
460 format = gdk_pixbuf_loader_get_format (pixbuf_loader);
462 if (format != NULL) {
465 gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
467 for (p = mlist; *p; ++p) {
468 GST_DEBUG ("suggesting mime type %s", *p);
469 caps = gst_caps_new_simple (*p, NULL);
470 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
471 gst_caps_free (caps);
476 GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
477 /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
478 to close a gzip that's not an svg; fixed upstream but no good way
480 gdk_pixbuf_loader_close (pixbuf_loader, NULL);
481 GST_DEBUG ("closed pixbuf loader");
482 g_object_unref (G_OBJECT (pixbuf_loader));
486 /* entry point to initialize the plug-in
487 * initialize the plug-in itself
488 * register the element factories and pad templates
489 * register the features
492 plugin_init (GstPlugin * plugin)
494 GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
495 "gdk pixbuf loader");
497 if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_MARGINAL,
498 GST_TYPE_GDK_PIXBUF))
501 #ifdef enable_typefind
502 gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
503 gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
506 if (!pixbufscale_init (plugin))
509 /* plugin initialisation succeeded */
514 /* this is the structure that gst-register looks for
515 * so keep the name plugin_desc, or you cannot get your plug-in registered */
516 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
519 "GDK Pixbuf decoder & scaler",
520 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)