1 /* GStreamer GdkPixbuf-based image decoder
2 * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <gst/video/video.h>
29 #include "gstgdkpixbuf.h"
31 GST_DEBUG_CATEGORY_STATIC (gst_gdk_pixbuf_debug);
32 #define GST_CAT_DEFAULT gst_gdk_pixbuf_debug
34 static const GstElementDetails plugin_details =
35 GST_ELEMENT_DETAILS ("GdkPixbuf image decoder",
36 "Codec/Decoder/Image",
37 "Decodes images in a video stream using GdkPixbuf",
38 "David A. Schleef <ds@schleef.org>, Renato Filho <renato.filho@indt.org.br>");
43 ARG_SILENT /* FIXME 0.11: remove */
46 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
47 GST_STATIC_PAD_TEMPLATE ("sink",
50 GST_STATIC_CAPS ("image/png; "
51 /* "image/jpeg; " disabled because we can't handle MJPEG */
54 "application/x-navi-animation; "
55 "image/x-cmu-raster; "
56 "image/x-sun-raster; "
59 "image/x-portable-anymap; "
60 "image/x-portable-bitmap; "
61 "image/x-portable-graymap; "
62 "image/x-portable-pixmap; "
66 "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga; "
67 "image/x-pcx; image/svg; image/svg+xml")
70 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
71 GST_STATIC_PAD_TEMPLATE ("src",
74 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB "; " GST_VIDEO_CAPS_RGBA)
77 gboolean pixbufscale_init (GstPlugin * plugin);
79 static void gst_gdk_pixbuf_base_init (gpointer g_class);
80 static void gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass);
81 static void gst_gdk_pixbuf_init (GstGdkPixbuf * filter,
82 GstGdkPixbufClass * klass);
84 static void gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
86 static void gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
89 static GstFlowReturn gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buffer);
90 static gboolean gst_gdk_pixbuf_sink_event (GstPad * pad, GstEvent * event);
92 #ifdef enable_typefind
93 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
96 GST_BOILERPLATE (GstGdkPixbuf, gst_gdk_pixbuf, GstElement, GST_TYPE_ELEMENT);
99 gst_gdk_pixbuf_sink_setcaps (GstPad * pad, GstCaps * caps)
101 GstGdkPixbuf *filter;
102 const GValue *framerate;
105 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
106 s = gst_caps_get_structure (caps, 0);
108 filter->framerate_numerator = 0;
109 filter->framerate_denominator = 1;
110 if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
111 filter->framerate_numerator = gst_value_get_fraction_numerator (framerate);
112 filter->framerate_denominator =
113 gst_value_get_fraction_denominator (framerate);
114 GST_DEBUG ("got framerate of %d/%d fps => packetized mode",
115 filter->framerate_numerator, filter->framerate_denominator);
117 gst_object_unref (filter);
123 gst_gdk_pixbuf_get_capslist (void)
127 GstCaps *capslist = NULL;
128 GstCaps *return_caps = NULL;
130 capslist = gst_caps_new_empty ();
131 slist0 = gdk_pixbuf_get_formats ();
133 for (slist = slist0; slist; slist = g_slist_next (slist)) {
134 GdkPixbufFormat *pixbuf_format;
138 pixbuf_format = slist->data;
139 mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
141 for (mimetype = mimetypes; *mimetype; mimetype++) {
142 gst_caps_append_structure (capslist, gst_structure_new (*mimetype, NULL));
144 g_strfreev (mimetypes);
146 g_slist_free (slist0);
148 return_caps = gst_caps_intersect (capslist,
149 gst_static_caps_get (&gst_gdk_pixbuf_sink_template.static_caps));
151 gst_caps_unref (capslist);
156 gst_gdk_pixbuf_sink_getcaps (GstPad * pad)
158 return gst_gdk_pixbuf_get_capslist ();
162 gst_gdk_pixbuf_base_init (gpointer g_class)
164 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
166 gst_element_class_add_pad_template (element_class,
167 gst_static_pad_template_get (&gst_gdk_pixbuf_src_template));
168 gst_element_class_add_pad_template (element_class,
169 gst_static_pad_template_get (&gst_gdk_pixbuf_sink_template));
170 gst_element_class_set_details (element_class, &plugin_details);
173 /* initialize the plugin's class */
175 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
177 GObjectClass *gobject_class;
178 GstElementClass *gstelement_class;
180 gobject_class = (GObjectClass *) klass;
181 gstelement_class = (GstElementClass *) klass;
183 gobject_class->set_property = gst_gdk_pixbuf_set_property;
184 gobject_class->get_property = gst_gdk_pixbuf_get_property;
186 g_object_class_install_property (gobject_class, ARG_SILENT,
187 g_param_spec_boolean ("silent", "Silent",
188 "Produce verbose output ? (deprecated)", FALSE, G_PARAM_READWRITE));
192 gst_gdk_pixbuf_init (GstGdkPixbuf * filter, GstGdkPixbufClass * klass)
195 gst_pad_new_from_static_template (&gst_gdk_pixbuf_sink_template, "sink");
196 gst_pad_set_setcaps_function (filter->sinkpad,
197 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_setcaps));
198 gst_pad_set_getcaps_function (filter->sinkpad,
199 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_getcaps));
200 gst_pad_set_chain_function (filter->sinkpad,
201 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_chain));
202 gst_pad_set_event_function (filter->sinkpad,
203 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_event));
204 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
207 gst_pad_new_from_static_template (&gst_gdk_pixbuf_src_template, "src");
208 gst_pad_use_fixed_caps (filter->srcpad);
209 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
211 filter->last_timestamp = GST_CLOCK_TIME_NONE;
212 filter->pixbuf_loader = NULL;
216 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
225 GstCaps *caps = NULL;
228 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
232 if (filter->image_size == 0) {
233 filter->width = gdk_pixbuf_get_width (pixbuf);
234 filter->height = gdk_pixbuf_get_height (pixbuf);
235 filter->rowstride = gdk_pixbuf_get_rowstride (pixbuf);
236 filter->image_size = filter->rowstride * filter->height;
238 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
239 switch (n_channels) {
241 caps = gst_caps_from_string (GST_VIDEO_CAPS_RGB);
244 caps = gst_caps_from_string (GST_VIDEO_CAPS_RGBA);
247 goto channels_not_supported;
250 gst_caps_set_simple (caps,
251 "width", G_TYPE_INT, filter->width,
252 "height", G_TYPE_INT, filter->height,
253 "framerate", GST_TYPE_FRACTION, filter->framerate_numerator,
254 filter->framerate_denominator, NULL);
256 GST_DEBUG ("Set size to %dx%d", filter->width, filter->height);
257 gst_pad_set_caps (filter->srcpad, caps);
258 gst_caps_unref (caps);
261 ret = gst_pad_alloc_buffer_and_set_caps (filter->srcpad,
262 GST_BUFFER_OFFSET_NONE,
263 filter->image_size, GST_PAD_CAPS (filter->srcpad), &outbuf);
265 if (ret != GST_FLOW_OK)
268 GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
269 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
271 in_pix = gdk_pixbuf_get_pixels (pixbuf);
272 in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
273 out_pix = GST_BUFFER_DATA (outbuf);
275 /* FIXME, last line might not have rowstride pixels */
276 for (y = 0; y < filter->height; y++) {
277 memcpy (out_pix, in_pix, filter->rowstride);
278 in_pix += in_rowstride;
279 out_pix += filter->rowstride;
282 GST_DEBUG ("pushing... %d bytes", GST_BUFFER_SIZE (outbuf));
283 return gst_pad_push (filter->srcpad, outbuf);
288 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
289 return GST_FLOW_ERROR;
291 channels_not_supported:
293 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
294 ("%d channels not supported", n_channels));
295 return GST_FLOW_ERROR;
299 GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
305 gst_gdk_pixbuf_sink_event (GstPad * pad, GstEvent * event)
307 GstFlowReturn res = GST_FLOW_OK;
309 GstGdkPixbuf *pixbuf;
311 pixbuf = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
313 switch (GST_EVENT_TYPE (event)) {
315 if (pixbuf->pixbuf_loader != NULL) {
316 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
317 res = gst_gdk_pixbuf_flush (pixbuf);
318 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
319 pixbuf->pixbuf_loader = NULL;
322 case GST_EVENT_NEWSEGMENT:
323 case GST_EVENT_FLUSH_STOP:
324 if (pixbuf->pixbuf_loader != NULL) {
325 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
326 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
327 pixbuf->pixbuf_loader = NULL;
334 if (res == GST_FLOW_OK) {
335 ret = gst_pad_event_default (pad, event);
340 gst_object_unref (pixbuf);
346 gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buf)
348 GstGdkPixbuf *filter;
349 GstFlowReturn ret = GST_FLOW_OK;
350 GError *error = NULL;
351 GstClockTime timestamp;
355 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
357 timestamp = GST_BUFFER_TIMESTAMP (buf);
359 if (GST_CLOCK_TIME_IS_VALID (timestamp))
360 filter->last_timestamp = timestamp;
362 GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
363 GST_TIME_ARGS (timestamp));
365 if (filter->pixbuf_loader == NULL)
366 filter->pixbuf_loader = gdk_pixbuf_loader_new ();
368 data = GST_BUFFER_DATA (buf);
369 size = GST_BUFFER_SIZE (buf);
371 GST_LOG_OBJECT (filter, "Writing buffer size %d", size);
372 if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, data, size, &error))
375 /* packetised mode? */
376 if (filter->framerate_numerator != 0) {
377 gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
378 ret = gst_gdk_pixbuf_flush (filter);
379 g_object_unref (filter->pixbuf_loader);
380 filter->pixbuf_loader = NULL;
383 gst_object_unref (filter);
390 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
391 ("gdk_pixbuf_loader_write error: %s", error->message));
392 g_error_free (error);
393 gst_object_unref (filter);
394 return GST_FLOW_ERROR;
399 gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
400 const GValue * value, GParamSpec * pspec)
402 GstGdkPixbuf *filter;
404 filter = GST_GDK_PIXBUF (object);
408 /* filter->silent = g_value_get_boolean (value); */
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
417 gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
418 GValue * value, GParamSpec * pspec)
420 GstGdkPixbuf *filter;
422 filter = GST_GDK_PIXBUF (object);
426 /* g_value_set_boolean (value, filter->silent); */
429 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
434 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
436 #ifdef enable_typefind
438 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
441 GdkPixbufLoader *pixbuf_loader;
442 GdkPixbufFormat *format;
444 data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
448 GST_DEBUG ("creating new loader");
450 pixbuf_loader = gdk_pixbuf_loader_new ();
452 gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
455 format = gdk_pixbuf_loader_get_format (pixbuf_loader);
457 if (format != NULL) {
460 gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
462 for (p = mlist; *p; ++p) {
463 GST_DEBUG ("suggesting mime type %s", *p);
464 caps = gst_caps_new_simple (*p, NULL);
465 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
466 gst_caps_free (caps);
471 GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
472 /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
473 to close a gzip that's not an svg; fixed upstream but no good way
475 gdk_pixbuf_loader_close (pixbuf_loader, NULL);
476 GST_DEBUG ("closed pixbuf loader");
477 g_object_unref (G_OBJECT (pixbuf_loader));
481 /* entry point to initialize the plug-in
482 * initialize the plug-in itself
483 * register the element factories and pad templates
484 * register the features
487 plugin_init (GstPlugin * plugin)
489 GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
490 "gdk pixbuf loader");
492 if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_MARGINAL,
493 GST_TYPE_GDK_PIXBUF))
496 #ifdef enable_typefind
497 gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
498 gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
501 if (!pixbufscale_init (plugin))
504 /* plugin initialisation succeeded */
509 /* this is the structure that gst-register looks for
510 * so keep the name plugin_desc, or you cannot get your plug-in registered */
511 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
514 "GDK Pixbuf decoder & scaler",
515 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)