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 <gst/video/video.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include "gstgdkpixbuf.h"
30 #include "gstgdkpixbufsink.h"
31 #include "pixbufscale.h"
33 GST_DEBUG_CATEGORY_STATIC (gst_gdk_pixbuf_debug);
34 #define GST_CAT_DEFAULT gst_gdk_pixbuf_debug
39 ARG_SILENT /* FIXME 0.11: remove */
42 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("image/png; "
47 /* "image/jpeg; " disabled because we can't handle MJPEG */
50 "application/x-navi-animation; "
51 "image/x-cmu-raster; "
52 "image/x-sun-raster; "
55 "image/x-portable-anymap; "
56 "image/x-portable-bitmap; "
57 "image/x-portable-graymap; "
58 "image/x-portable-pixmap; "
62 "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga; "
63 "image/x-pcx; image/svg; image/svg+xml")
66 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
70 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB "; " GST_VIDEO_CAPS_RGBA)
73 static void gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
74 const GValue * value, GParamSpec * pspec);
75 static void gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
76 GValue * value, GParamSpec * pspec);
78 static GstStateChangeReturn
79 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition);
80 static GstFlowReturn gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buffer);
81 static gboolean gst_gdk_pixbuf_sink_event (GstPad * pad, GstEvent * event);
83 #ifdef enable_typefind
84 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
87 GST_BOILERPLATE (GstGdkPixbuf, gst_gdk_pixbuf, GstElement, GST_TYPE_ELEMENT);
90 gst_gdk_pixbuf_sink_setcaps (GstPad * pad, GstCaps * caps)
93 const GValue *framerate;
96 filter = GST_GDK_PIXBUF (GST_PAD_PARENT (pad));
97 s = gst_caps_get_structure (caps, 0);
99 if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
100 filter->framerate_numerator = gst_value_get_fraction_numerator (framerate);
101 filter->framerate_denominator =
102 gst_value_get_fraction_denominator (framerate);
103 GST_DEBUG_OBJECT (filter, "got framerate of %d/%d fps => packetized mode",
104 filter->framerate_numerator, filter->framerate_denominator);
106 filter->framerate_numerator = 0;
107 filter->framerate_denominator = 1;
108 GST_DEBUG_OBJECT (filter, "no framerate, assuming single image");
115 gst_gdk_pixbuf_get_capslist (void)
119 GstCaps *capslist = NULL;
120 GstCaps *return_caps = NULL;
123 capslist = gst_caps_new_empty ();
124 slist0 = gdk_pixbuf_get_formats ();
126 for (slist = slist0; slist; slist = g_slist_next (slist)) {
127 GdkPixbufFormat *pixbuf_format;
131 pixbuf_format = slist->data;
132 mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
134 for (mimetype = mimetypes; *mimetype; mimetype++) {
135 gst_caps_append_structure (capslist, gst_structure_new (*mimetype, NULL));
137 g_strfreev (mimetypes);
139 g_slist_free (slist0);
141 tmpl_caps = gst_static_caps_get (&gst_gdk_pixbuf_sink_template.static_caps);
142 return_caps = gst_caps_intersect (capslist, tmpl_caps);
144 gst_caps_unref (tmpl_caps);
145 gst_caps_unref (capslist);
150 gst_gdk_pixbuf_sink_getcaps (GstPad * pad)
152 return gst_gdk_pixbuf_get_capslist ();
156 gst_gdk_pixbuf_base_init (gpointer g_class)
158 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
160 gst_element_class_add_static_pad_template (element_class,
161 &gst_gdk_pixbuf_src_template);
162 gst_element_class_add_static_pad_template (element_class,
163 &gst_gdk_pixbuf_sink_template);
164 gst_element_class_set_details_simple (element_class,
165 "GdkPixbuf image decoder", "Codec/Decoder/Image",
166 "Decodes images in a video stream using GdkPixbuf",
167 "David A. Schleef <ds@schleef.org>, Renato Filho <renato.filho@indt.org.br>");
170 /* initialize the plugin's class */
172 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
174 GObjectClass *gobject_class;
175 GstElementClass *gstelement_class;
177 gobject_class = (GObjectClass *) klass;
178 gstelement_class = (GstElementClass *) klass;
180 gobject_class->set_property = gst_gdk_pixbuf_set_property;
181 gobject_class->get_property = gst_gdk_pixbuf_get_property;
183 g_object_class_install_property (gobject_class, ARG_SILENT,
184 g_param_spec_boolean ("silent", "Silent",
185 "Produce verbose output ? (deprecated)", FALSE,
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 gstelement_class->change_state =
189 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_change_state);
193 gst_gdk_pixbuf_init (GstGdkPixbuf * filter, GstGdkPixbufClass * klass)
196 gst_pad_new_from_static_template (&gst_gdk_pixbuf_sink_template, "sink");
197 gst_pad_set_setcaps_function (filter->sinkpad,
198 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_setcaps));
199 gst_pad_set_getcaps_function (filter->sinkpad,
200 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_getcaps));
201 gst_pad_set_chain_function (filter->sinkpad,
202 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_chain));
203 gst_pad_set_event_function (filter->sinkpad,
204 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_event));
205 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
208 gst_pad_new_from_static_template (&gst_gdk_pixbuf_src_template, "src");
209 gst_pad_use_fixed_caps (filter->srcpad);
210 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
212 filter->last_timestamp = GST_CLOCK_TIME_NONE;
213 filter->pixbuf_loader = NULL;
217 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
226 GstCaps *caps = NULL;
229 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
233 if (filter->image_size == 0) {
234 filter->width = gdk_pixbuf_get_width (pixbuf);
235 filter->height = gdk_pixbuf_get_height (pixbuf);
236 filter->rowstride = gdk_pixbuf_get_rowstride (pixbuf);
237 filter->image_size = filter->rowstride * filter->height;
239 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
240 switch (n_channels) {
242 caps = gst_caps_from_string (GST_VIDEO_CAPS_RGB);
245 caps = gst_caps_from_string (GST_VIDEO_CAPS_RGBA);
248 goto channels_not_supported;
251 gst_caps_set_simple (caps,
252 "width", G_TYPE_INT, filter->width,
253 "height", G_TYPE_INT, filter->height,
254 "framerate", GST_TYPE_FRACTION, filter->framerate_numerator,
255 filter->framerate_denominator, NULL);
257 GST_DEBUG ("Set size to %dx%d", filter->width, filter->height);
258 gst_pad_set_caps (filter->srcpad, caps);
259 gst_caps_unref (caps);
262 ret = gst_pad_alloc_buffer_and_set_caps (filter->srcpad,
263 GST_BUFFER_OFFSET_NONE,
264 filter->image_size, GST_PAD_CAPS (filter->srcpad), &outbuf);
266 if (ret != GST_FLOW_OK)
269 GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
270 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
272 in_pix = gdk_pixbuf_get_pixels (pixbuf);
273 in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
274 out_pix = GST_BUFFER_DATA (outbuf);
276 /* FIXME, last line might not have rowstride pixels */
277 for (y = 0; y < filter->height; y++) {
278 memcpy (out_pix, in_pix, filter->rowstride);
279 in_pix += in_rowstride;
280 out_pix += filter->rowstride;
283 GST_DEBUG ("pushing... %d bytes", GST_BUFFER_SIZE (outbuf));
284 ret = gst_pad_push (filter->srcpad, outbuf);
286 if (ret != GST_FLOW_OK)
287 GST_DEBUG_OBJECT (filter, "flow: %s", gst_flow_get_name (ret));
294 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
295 return GST_FLOW_ERROR;
297 channels_not_supported:
299 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
300 ("%d channels not supported", n_channels));
301 return GST_FLOW_ERROR;
305 GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
311 gst_gdk_pixbuf_sink_event (GstPad * pad, GstEvent * event)
313 GstFlowReturn res = GST_FLOW_OK;
315 GstGdkPixbuf *pixbuf;
317 pixbuf = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
319 switch (GST_EVENT_TYPE (event)) {
321 if (pixbuf->pixbuf_loader != NULL) {
322 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
323 res = gst_gdk_pixbuf_flush (pixbuf);
324 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
325 pixbuf->pixbuf_loader = NULL;
326 /* as long as we don't have flow returns for event functions we need
327 * to post an error here, or the application might never know that
329 if (res != GST_FLOW_OK && res != GST_FLOW_WRONG_STATE) {
330 GST_ELEMENT_ERROR (pixbuf, STREAM, FAILED, (NULL),
331 ("Flow: %s", gst_flow_get_name (res)));
335 case GST_EVENT_NEWSEGMENT:
336 case GST_EVENT_FLUSH_STOP:
337 if (pixbuf->pixbuf_loader != NULL) {
338 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
339 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
340 pixbuf->pixbuf_loader = NULL;
347 if (res == GST_FLOW_OK) {
348 ret = gst_pad_event_default (pad, event);
353 gst_object_unref (pixbuf);
359 gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buf)
361 GstGdkPixbuf *filter;
362 GstFlowReturn ret = GST_FLOW_OK;
363 GError *error = NULL;
364 GstClockTime timestamp;
368 filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
370 timestamp = GST_BUFFER_TIMESTAMP (buf);
372 if (GST_CLOCK_TIME_IS_VALID (timestamp))
373 filter->last_timestamp = timestamp;
375 GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
376 GST_TIME_ARGS (timestamp));
378 if (filter->pixbuf_loader == NULL)
379 filter->pixbuf_loader = gdk_pixbuf_loader_new ();
381 data = GST_BUFFER_DATA (buf);
382 size = GST_BUFFER_SIZE (buf);
384 GST_LOG_OBJECT (filter, "Writing buffer size %d", size);
385 if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, data, size, &error))
388 /* packetised mode? */
389 if (filter->framerate_numerator != 0) {
390 gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
391 ret = gst_gdk_pixbuf_flush (filter);
392 g_object_unref (filter->pixbuf_loader);
393 filter->pixbuf_loader = NULL;
396 gst_buffer_unref (buf);
397 gst_object_unref (filter);
404 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
405 ("gdk_pixbuf_loader_write error: %s", error->message));
406 g_error_free (error);
407 gst_buffer_unref (buf);
408 gst_object_unref (filter);
409 return GST_FLOW_ERROR;
414 gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
415 const GValue * value, GParamSpec * pspec)
419 /* filter->silent = g_value_get_boolean (value); */
422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
428 gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
429 GValue * value, GParamSpec * pspec)
433 /* g_value_set_boolean (value, filter->silent); */
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
441 static GstStateChangeReturn
442 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition)
444 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
445 GstGdkPixbuf *dec = GST_GDK_PIXBUF (element);
447 switch (transition) {
448 case GST_STATE_CHANGE_READY_TO_PAUSED:
449 /* default to single image mode, setcaps function might not be called */
450 dec->framerate_numerator = 0;
451 dec->framerate_denominator = 1;
457 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
458 if (ret == GST_STATE_CHANGE_FAILURE)
461 switch (transition) {
462 case GST_STATE_CHANGE_PAUSED_TO_READY:
463 dec->framerate_numerator = 0;
464 dec->framerate_denominator = 0;
473 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
475 #ifdef enable_typefind
477 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
480 GdkPixbufLoader *pixbuf_loader;
481 GdkPixbufFormat *format;
483 data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
487 GST_DEBUG ("creating new loader");
489 pixbuf_loader = gdk_pixbuf_loader_new ();
491 gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
494 format = gdk_pixbuf_loader_get_format (pixbuf_loader);
496 if (format != NULL) {
499 gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
501 for (p = mlist; *p; ++p) {
502 GST_DEBUG ("suggesting mime type %s", *p);
503 caps = gst_caps_new_simple (*p, NULL);
504 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
505 gst_caps_free (caps);
510 GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
511 /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
512 to close a gzip that's not an svg; fixed upstream but no good way
514 gdk_pixbuf_loader_close (pixbuf_loader, NULL);
515 GST_DEBUG ("closed pixbuf loader");
516 g_object_unref (G_OBJECT (pixbuf_loader));
520 /* entry point to initialize the plug-in
521 * initialize the plug-in itself
522 * register the element factories and pad templates
523 * register the features
526 plugin_init (GstPlugin * plugin)
528 GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
529 "gdk pixbuf loader");
531 if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_SECONDARY,
532 GST_TYPE_GDK_PIXBUF))
535 #ifdef enable_typefind
536 gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
537 gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
540 if (!gst_element_register (plugin, "gdkpixbufsink", GST_RANK_NONE,
541 GST_TYPE_GDK_PIXBUF_SINK))
544 if (!pixbufscale_init (plugin))
547 /* plugin initialisation succeeded */
552 /* this is the structure that gst-register looks for
553 * so keep the name plugin_desc, or you cannot get your plug-in registered */
554 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
557 "GdkPixbuf-based image decoder, scaler and sink",
558 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)