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 */
54 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
55 GST_STATIC_PAD_TEMPLATE (
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; "
81 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
82 GST_STATIC_PAD_TEMPLATE (
86 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
89 static void gst_gdk_pixbuf_base_init (gpointer g_class);
90 static void gst_gdk_pixbuf_class_init (GstGdkPixbufClass *klass);
91 static void gst_gdk_pixbuf_init (GstGdkPixbuf *filter);
93 static void gst_gdk_pixbuf_set_property(GObject *object, guint prop_id,
96 static void gst_gdk_pixbuf_get_property(GObject *object, guint prop_id,
100 static void gst_gdk_pixbuf_chain (GstPad *pad, GstData *_data);
101 #ifdef enable_typefind
102 static void gst_gdk_pixbuf_type_find (GstTypeFind *tf, gpointer ignore);
105 static GstElementClass *parent_class = NULL;
107 static GstPadLinkReturn
108 gst_gdk_pixbuf_sink_link (GstPad *pad, const GstCaps *caps)
110 GstGdkPixbuf *filter;
112 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
113 g_return_val_if_fail (filter != NULL, GST_PAD_LINK_REFUSED);
114 g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter),
115 GST_PAD_LINK_REFUSED);
117 filter->framerate = 1.0;
118 gst_structure_get_double (gst_caps_get_structure (caps, 0), "framerate",
121 return GST_PAD_LINK_OK;
124 #if GDK_PIXBUF_MAJOR == 2 && GDK_PIXBUF_MINOR < 2
125 /* gdk-pixbuf prior to 2.2 didn't have gdk_pixbuf_get_formats().
126 * These are just the formats that gdk-pixbuf is known to support.
127 * But maybe not -- it may have been compiled without an external
129 static GstCaps *gst_gdk_pixbuf_get_capslist(void)
131 return gst_caps_copy (gst_static_caps_get (
132 &gst_gdk_pixbuf_sink_template.static_caps));
135 static GstCaps *gst_gdk_pixbuf_get_capslist(void)
139 GdkPixbufFormat *pixbuf_format;
142 GstCaps *capslist = NULL;
144 capslist = gst_caps_new_empty ();
145 slist0 = gdk_pixbuf_get_formats();
147 for(slist = slist0;slist;slist=g_slist_next(slist)){
148 pixbuf_format = slist->data;
149 mimetypes = gdk_pixbuf_format_get_mime_types(pixbuf_format);
150 for(mimetype = mimetypes; *mimetype; mimetype++){
151 gst_caps_append_structure (capslist,
152 gst_structure_new (*mimetype,NULL));
156 g_slist_free(slist0);
162 static GstCaps *gst_gdk_pixbuf_sink_getcaps(GstPad *pad)
164 GstGdkPixbuf *filter;
166 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
167 g_return_val_if_fail (filter != NULL, NULL);
168 g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter), NULL);
170 return gst_gdk_pixbuf_get_capslist();
174 gst_gdk_pixbuf_get_type (void)
176 static GType plugin_type = 0;
180 static const GTypeInfo plugin_info =
182 sizeof (GstGdkPixbufClass),
183 gst_gdk_pixbuf_base_init,
185 (GClassInitFunc) gst_gdk_pixbuf_class_init,
188 sizeof (GstGdkPixbuf),
190 (GInstanceInitFunc) gst_gdk_pixbuf_init,
192 plugin_type = g_type_register_static (GST_TYPE_ELEMENT,
200 gst_gdk_pixbuf_base_init (gpointer g_class)
202 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
204 gst_element_class_add_pad_template (element_class,
205 gst_static_pad_template_get( &gst_gdk_pixbuf_src_template));
206 gst_element_class_add_pad_template (element_class,
207 gst_static_pad_template_get( &gst_gdk_pixbuf_sink_template));
208 gst_element_class_set_details (element_class, &plugin_details);
211 /* initialize the plugin's class */
213 gst_gdk_pixbuf_class_init (GstGdkPixbufClass *klass)
215 GObjectClass *gobject_class;
216 GstElementClass *gstelement_class;
218 gobject_class = (GObjectClass*) klass;
219 gstelement_class = (GstElementClass*) klass;
221 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
223 g_object_class_install_property (gobject_class, ARG_SILENT,
224 g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
225 FALSE, G_PARAM_READWRITE));
227 gobject_class->set_property = gst_gdk_pixbuf_set_property;
228 gobject_class->get_property = gst_gdk_pixbuf_get_property;
232 gst_gdk_pixbuf_init (GstGdkPixbuf *filter)
234 filter->sinkpad = gst_pad_new_from_template (
235 gst_static_pad_template_get( &gst_gdk_pixbuf_sink_template), "sink");
236 gst_pad_set_link_function (filter->sinkpad, gst_gdk_pixbuf_sink_link);
237 gst_pad_set_getcaps_function (filter->sinkpad, gst_gdk_pixbuf_sink_getcaps);
239 filter->srcpad = gst_pad_new_from_template (
240 gst_static_pad_template_get( &gst_gdk_pixbuf_src_template), "src");
241 gst_pad_use_explicit_caps (filter->srcpad);
243 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
244 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
245 gst_pad_set_chain_function (filter->sinkpad, gst_gdk_pixbuf_chain);
247 GST_FLAG_SET (GST_ELEMENT (filter), GST_ELEMENT_EVENT_AWARE);
251 gst_gdk_pixbuf_chain (GstPad *pad, GstData *_data)
253 GstBuffer *buf = GST_BUFFER (_data);
254 GstGdkPixbuf *filter;
255 GError *error = NULL;
256 gboolean push_buffer = FALSE;
257 gboolean dump_buffer = FALSE;
258 gboolean got_eos = FALSE;
260 GST_DEBUG ("gst_gdk_pixbuf_chain");
262 g_return_if_fail (GST_IS_PAD (pad));
263 g_return_if_fail (buf != NULL);
265 filter = GST_GDK_PIXBUF (GST_OBJECT_PARENT (pad));
266 g_return_if_fail (GST_IS_GDK_PIXBUF (filter));
268 if (GST_IS_EVENT (_data)) {
269 GstEvent *event = GST_EVENT (buf);
271 switch (GST_EVENT_TYPE (event)) {
276 case GST_EVENT_DISCONTINUOUS:
280 gst_pad_event_default (pad, event);
285 if (filter->last_timestamp != GST_BUFFER_TIMESTAMP (buf)) {
290 if (filter->pixbuf_loader != NULL) {
293 GError *error = NULL;
295 if (!gdk_pixbuf_loader_close (filter->pixbuf_loader, &error)) {
296 GST_ELEMENT_ERROR (filter, LIBRARY, SHUTDOWN, (NULL), (error->message));
297 g_error_free (error);
301 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
303 if(filter->image_size == 0){
306 filter->width = gdk_pixbuf_get_width(pixbuf);
307 filter->height = gdk_pixbuf_get_height(pixbuf);
308 filter->rowstride = gdk_pixbuf_get_rowstride(pixbuf);
309 filter->image_size = filter->rowstride * filter->height;
311 caps = gst_caps_copy (gst_pad_get_pad_template_caps (filter->srcpad));
312 gst_caps_set_simple (caps,
313 "width", G_TYPE_INT, filter->width,
314 "height", G_TYPE_INT, filter->height,
315 "framerate", G_TYPE_DOUBLE, filter->framerate, NULL);
317 gst_pad_set_explicit_caps (filter->srcpad, caps);
320 outbuf = gst_pad_alloc_buffer (filter->srcpad, GST_BUFFER_OFFSET_NONE,
322 GST_BUFFER_TIMESTAMP(outbuf) = GST_BUFFER_TIMESTAMP(buf);
323 GST_BUFFER_DURATION(outbuf) = GST_BUFFER_DURATION(buf);
325 memcpy(GST_BUFFER_DATA(outbuf), gdk_pixbuf_get_pixels(pixbuf),
328 gst_pad_push (filter->srcpad, GST_DATA (outbuf));
330 g_object_unref(G_OBJECT(filter->pixbuf_loader));
331 filter->pixbuf_loader = NULL;
337 if (filter->pixbuf_loader != NULL) {
338 gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
339 g_object_unref(G_OBJECT(filter->pixbuf_loader));
340 filter->pixbuf_loader = NULL;
344 if (GST_IS_BUFFER (_data)) {
345 if (filter->pixbuf_loader == NULL) {
346 filter->pixbuf_loader = gdk_pixbuf_loader_new();
347 filter->last_timestamp = GST_BUFFER_TIMESTAMP (buf);
350 gdk_pixbuf_loader_write(filter->pixbuf_loader, GST_BUFFER_DATA(buf),
351 GST_BUFFER_SIZE(buf), &error);
352 gst_buffer_unref (buf);
356 gst_pad_event_default (pad, GST_EVENT (_data));
361 gst_gdk_pixbuf_set_property (GObject *object, guint prop_id,
362 const GValue *value, GParamSpec *pspec)
364 GstGdkPixbuf *filter;
366 g_return_if_fail (GST_IS_GDK_PIXBUF (object));
367 filter = GST_GDK_PIXBUF (object);
372 //filter->silent = g_value_get_boolean (value);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 gst_gdk_pixbuf_get_property (GObject *object, guint prop_id,
382 GValue *value, GParamSpec *pspec)
384 GstGdkPixbuf *filter;
386 g_return_if_fail (GST_IS_GDK_PIXBUF (object));
387 filter = GST_GDK_PIXBUF (object);
391 //g_value_set_boolean (value, filter->silent);
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
399 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
401 #ifdef enable_typefind
403 gst_gdk_pixbuf_type_find (GstTypeFind *tf, gpointer ignore)
406 GdkPixbufLoader *pixbuf_loader;
407 GdkPixbufFormat *format;
409 data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
410 if (data == NULL) return;
412 GST_DEBUG ("creating new loader");
414 pixbuf_loader = gdk_pixbuf_loader_new();
416 gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
419 format = gdk_pixbuf_loader_get_format (pixbuf_loader);
421 if (format != NULL) {
424 gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
426 for (p = mlist; *p; ++p)
428 GST_DEBUG ("suggesting mime type %s", *p);
429 caps = gst_caps_new_simple (*p, NULL);
430 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
431 gst_caps_free (caps);
436 GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
437 /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
438 to close a gzip that's not an svg; fixed upstream but no good way
440 gdk_pixbuf_loader_close (pixbuf_loader, NULL);
441 GST_DEBUG ("closed pixbuf loader");
442 g_object_unref (G_OBJECT (pixbuf_loader));
446 /* entry point to initialize the plug-in
447 * initialize the plug-in itself
448 * register the element factories and pad templates
449 * register the features
452 plugin_init (GstPlugin *plugin)
454 GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0, "gdk pixbuf loader");
456 if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_NONE, GST_TYPE_GDK_PIXBUF))
459 #ifdef enable_typefind
460 gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
461 gst_gdk_pixbuf_type_find, NULL,
465 /* plugin initialisation succeeded */
469 /* this is the structure that gst-register looks for
470 * so keep the name plugin_desc, or you cannot get your plug-in registered */
475 "GDK Pixbuf decoder",