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>",
43 /* Filter signals and args */
56 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("image/png; "
64 "application/x-navi-animation; "
65 "image/x-cmu-raster; "
66 "image/x-sun-raster; "
69 "image/x-portable-anymap; "
70 "image/x-portable-bitmap; "
71 "image/x-portable-graymap; "
72 "image/x-portable-pixmap; "
76 "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga")
79 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
80 GST_STATIC_PAD_TEMPLATE ("src",
83 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
86 static void gst_gdk_pixbuf_base_init (gpointer g_class);
87 static void gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass);
88 static void gst_gdk_pixbuf_init (GstGdkPixbuf * filter);
90 static void gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
91 const GValue * value, GParamSpec * pspec);
92 static void gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
93 GValue * value, GParamSpec * pspec);
95 static void gst_gdk_pixbuf_chain (GstPad * pad, GstData * _data);
97 #ifdef enable_typefind
98 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
101 static GstElementClass *parent_class = NULL;
103 static GstPadLinkReturn
104 gst_gdk_pixbuf_sink_link (GstPad * pad, const GstCaps * caps)
106 GstGdkPixbuf *filter;
108 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
109 g_return_val_if_fail (filter != NULL, GST_PAD_LINK_REFUSED);
110 g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter), GST_PAD_LINK_REFUSED);
112 filter->framerate = 1.0;
113 gst_structure_get_double (gst_caps_get_structure (caps, 0), "framerate",
116 return GST_PAD_LINK_OK;
119 #if GDK_PIXBUF_MAJOR == 2 && GDK_PIXBUF_MINOR < 2
120 /* gdk-pixbuf prior to 2.2 didn't have gdk_pixbuf_get_formats().
121 * These are just the formats that gdk-pixbuf is known to support.
122 * But maybe not -- it may have been compiled without an external
125 gst_gdk_pixbuf_get_capslist (void)
127 return gst_caps_copy (gst_static_caps_get (&gst_gdk_pixbuf_sink_template.
132 gst_gdk_pixbuf_get_capslist (void)
136 GdkPixbufFormat *pixbuf_format;
139 GstCaps *capslist = NULL;
141 capslist = gst_caps_new_empty ();
142 slist0 = gdk_pixbuf_get_formats ();
144 for (slist = slist0; slist; slist = g_slist_next (slist)) {
145 pixbuf_format = slist->data;
146 mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
147 for (mimetype = mimetypes; *mimetype; mimetype++) {
148 gst_caps_append_structure (capslist, gst_structure_new (*mimetype, NULL));
152 g_slist_free (slist0);
159 gst_gdk_pixbuf_sink_getcaps (GstPad * pad)
161 GstGdkPixbuf *filter;
163 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
164 g_return_val_if_fail (filter != NULL, NULL);
165 g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter), NULL);
167 return gst_gdk_pixbuf_get_capslist ();
171 gst_gdk_pixbuf_get_type (void)
173 static GType plugin_type = 0;
176 static const GTypeInfo plugin_info = {
177 sizeof (GstGdkPixbufClass),
178 gst_gdk_pixbuf_base_init,
180 (GClassInitFunc) gst_gdk_pixbuf_class_init,
183 sizeof (GstGdkPixbuf),
185 (GInstanceInitFunc) gst_gdk_pixbuf_init,
187 plugin_type = g_type_register_static (GST_TYPE_ELEMENT,
188 "GstGdkPixbuf", &plugin_info, 0);
194 gst_gdk_pixbuf_base_init (gpointer g_class)
196 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
198 gst_element_class_add_pad_template (element_class,
199 gst_static_pad_template_get (&gst_gdk_pixbuf_src_template));
200 gst_element_class_add_pad_template (element_class,
201 gst_static_pad_template_get (&gst_gdk_pixbuf_sink_template));
202 gst_element_class_set_details (element_class, &plugin_details);
205 /* initialize the plugin's class */
207 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
209 GObjectClass *gobject_class;
210 GstElementClass *gstelement_class;
212 gobject_class = (GObjectClass *) klass;
213 gstelement_class = (GstElementClass *) klass;
215 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
217 g_object_class_install_property (gobject_class, ARG_SILENT,
218 g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
219 FALSE, G_PARAM_READWRITE));
221 gobject_class->set_property = gst_gdk_pixbuf_set_property;
222 gobject_class->get_property = gst_gdk_pixbuf_get_property;
226 gst_gdk_pixbuf_init (GstGdkPixbuf * filter)
229 gst_pad_new_from_template (gst_static_pad_template_get
230 (&gst_gdk_pixbuf_sink_template), "sink");
231 gst_pad_set_link_function (filter->sinkpad, gst_gdk_pixbuf_sink_link);
232 gst_pad_set_getcaps_function (filter->sinkpad, gst_gdk_pixbuf_sink_getcaps);
235 gst_pad_new_from_template (gst_static_pad_template_get
236 (&gst_gdk_pixbuf_src_template), "src");
237 gst_pad_use_explicit_caps (filter->srcpad);
239 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
240 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
241 gst_pad_set_chain_function (filter->sinkpad, gst_gdk_pixbuf_chain);
243 GST_FLAG_SET (GST_ELEMENT (filter), GST_ELEMENT_EVENT_AWARE);
247 gst_gdk_pixbuf_chain (GstPad * pad, GstData * _data)
249 GstBuffer *buf = GST_BUFFER (_data);
250 GstGdkPixbuf *filter;
251 GError *error = NULL;
252 gboolean push_buffer = FALSE;
253 gboolean dump_buffer = FALSE;
254 gboolean got_eos = FALSE;
256 GST_DEBUG ("gst_gdk_pixbuf_chain");
258 g_return_if_fail (GST_IS_PAD (pad));
259 g_return_if_fail (buf != NULL);
261 filter = GST_GDK_PIXBUF (GST_OBJECT_PARENT (pad));
262 g_return_if_fail (GST_IS_GDK_PIXBUF (filter));
264 if (GST_IS_EVENT (_data)) {
265 GstEvent *event = GST_EVENT (buf);
267 switch (GST_EVENT_TYPE (event)) {
272 case GST_EVENT_DISCONTINUOUS:
276 gst_pad_event_default (pad, event);
281 if (filter->last_timestamp != GST_BUFFER_TIMESTAMP (buf)) {
286 if (filter->pixbuf_loader != NULL) {
289 GError *error = NULL;
291 if (!gdk_pixbuf_loader_close (filter->pixbuf_loader, &error)) {
292 GST_ELEMENT_ERROR (filter, LIBRARY, SHUTDOWN, (NULL), (error->message));
293 g_error_free (error);
297 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
299 if (filter->image_size == 0) {
302 filter->width = gdk_pixbuf_get_width (pixbuf);
303 filter->height = gdk_pixbuf_get_height (pixbuf);
304 filter->rowstride = gdk_pixbuf_get_rowstride (pixbuf);
305 filter->image_size = filter->rowstride * filter->height;
307 caps = gst_caps_copy (gst_pad_get_pad_template_caps (filter->srcpad));
308 gst_caps_set_simple (caps,
309 "width", G_TYPE_INT, filter->width,
310 "height", G_TYPE_INT, filter->height,
311 "framerate", G_TYPE_DOUBLE, filter->framerate, NULL);
313 gst_pad_set_explicit_caps (filter->srcpad, caps);
316 outbuf = gst_pad_alloc_buffer (filter->srcpad, GST_BUFFER_OFFSET_NONE,
318 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
319 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
321 memcpy (GST_BUFFER_DATA (outbuf), gdk_pixbuf_get_pixels (pixbuf),
324 gst_pad_push (filter->srcpad, GST_DATA (outbuf));
326 g_object_unref (G_OBJECT (filter->pixbuf_loader));
327 filter->pixbuf_loader = NULL;
333 if (filter->pixbuf_loader != NULL) {
334 gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
335 g_object_unref (G_OBJECT (filter->pixbuf_loader));
336 filter->pixbuf_loader = NULL;
340 if (GST_IS_BUFFER (_data)) {
341 if (filter->pixbuf_loader == NULL) {
342 filter->pixbuf_loader = gdk_pixbuf_loader_new ();
343 filter->last_timestamp = GST_BUFFER_TIMESTAMP (buf);
346 gdk_pixbuf_loader_write (filter->pixbuf_loader, GST_BUFFER_DATA (buf),
347 GST_BUFFER_SIZE (buf), &error);
348 gst_buffer_unref (buf);
352 gst_pad_event_default (pad, GST_EVENT (_data));
357 gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
358 const GValue * value, GParamSpec * pspec)
360 GstGdkPixbuf *filter;
362 g_return_if_fail (GST_IS_GDK_PIXBUF (object));
363 filter = GST_GDK_PIXBUF (object);
367 //filter->silent = g_value_get_boolean (value);
370 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376 gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
377 GValue * value, GParamSpec * pspec)
379 GstGdkPixbuf *filter;
381 g_return_if_fail (GST_IS_GDK_PIXBUF (object));
382 filter = GST_GDK_PIXBUF (object);
386 //g_value_set_boolean (value, filter->silent);
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
394 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
396 #ifdef enable_typefind
398 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
401 GdkPixbufLoader *pixbuf_loader;
402 GdkPixbufFormat *format;
404 data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
408 GST_DEBUG ("creating new loader");
410 pixbuf_loader = gdk_pixbuf_loader_new ();
412 gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
415 format = gdk_pixbuf_loader_get_format (pixbuf_loader);
417 if (format != NULL) {
420 gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
422 for (p = mlist; *p; ++p) {
423 GST_DEBUG ("suggesting mime type %s", *p);
424 caps = gst_caps_new_simple (*p, NULL);
425 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
426 gst_caps_free (caps);
431 GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
432 /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
433 to close a gzip that's not an svg; fixed upstream but no good way
435 gdk_pixbuf_loader_close (pixbuf_loader, NULL);
436 GST_DEBUG ("closed pixbuf loader");
437 g_object_unref (G_OBJECT (pixbuf_loader));
441 /* entry point to initialize the plug-in
442 * initialize the plug-in itself
443 * register the element factories and pad templates
444 * register the features
447 plugin_init (GstPlugin * plugin)
449 GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
450 "gdk pixbuf loader");
452 if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_NONE,
453 GST_TYPE_GDK_PIXBUF))
456 #ifdef enable_typefind
457 gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
458 gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
461 /* plugin initialisation succeeded */
465 /* this is the structure that gst-register looks for
466 * so keep the name plugin_desc, or you cannot get your plug-in registered */
467 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
470 "GDK Pixbuf decoder", plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)