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
41 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("image/png; "
46 /* "image/jpeg; " disabled because we can't handle MJPEG */
49 "application/x-navi-animation; "
50 "image/x-cmu-raster; "
51 "image/x-sun-raster; "
54 "image/x-portable-anymap; "
55 "image/x-portable-bitmap; "
56 "image/x-portable-graymap; "
57 "image/x-portable-pixmap; "
61 "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga; "
62 "image/x-pcx; image/svg; image/svg+xml")
65 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
69 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB") "; "
70 GST_VIDEO_CAPS_MAKE ("RGBA"))
73 static GstStateChangeReturn
74 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition);
75 static GstFlowReturn gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent,
77 static gboolean gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent,
80 #ifdef enable_typefind
81 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
84 #define gst_gdk_pixbuf_parent_class parent_class
85 G_DEFINE_TYPE (GstGdkPixbuf, gst_gdk_pixbuf, GST_TYPE_ELEMENT);
88 gst_gdk_pixbuf_sink_setcaps (GstGdkPixbuf * filter, GstCaps * caps)
90 const GValue *framerate;
93 s = gst_caps_get_structure (caps, 0);
95 if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
96 filter->in_fps_n = gst_value_get_fraction_numerator (framerate);
97 filter->in_fps_d = gst_value_get_fraction_denominator (framerate);
98 GST_DEBUG_OBJECT (filter, "got framerate of %d/%d fps => packetized mode",
99 filter->in_fps_n, filter->in_fps_d);
101 filter->in_fps_n = 0;
102 filter->in_fps_d = 1;
103 GST_DEBUG_OBJECT (filter, "no framerate, assuming single image");
110 gst_gdk_pixbuf_get_capslist (GstCaps * filter)
114 GstCaps *capslist = NULL;
115 GstCaps *return_caps = NULL;
118 capslist = gst_caps_new_empty ();
119 slist0 = gdk_pixbuf_get_formats ();
121 for (slist = slist0; slist; slist = g_slist_next (slist)) {
122 GdkPixbufFormat *pixbuf_format;
126 pixbuf_format = slist->data;
127 mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
129 for (mimetype = mimetypes; *mimetype; mimetype++) {
130 gst_caps_append_structure (capslist, gst_structure_new_empty (*mimetype));
132 g_strfreev (mimetypes);
134 g_slist_free (slist0);
136 tmpl_caps = gst_static_caps_get (&gst_gdk_pixbuf_sink_template.static_caps);
137 return_caps = gst_caps_intersect (capslist, tmpl_caps);
139 gst_caps_unref (tmpl_caps);
140 gst_caps_unref (capslist);
142 if (filter && return_caps) {
145 temp = gst_caps_intersect (return_caps, filter);
146 gst_caps_unref (return_caps);
154 gst_gdk_pixbuf_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
158 switch (GST_QUERY_TYPE (query)) {
161 GstCaps *filter, *caps;
163 gst_query_parse_caps (query, &filter);
164 caps = gst_gdk_pixbuf_get_capslist (filter);
165 gst_query_set_caps_result (query, caps);
166 gst_caps_unref (caps);
172 res = gst_pad_query_default (pad, parent, query);
179 /* initialize the plugin's class */
181 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
183 GstElementClass *gstelement_class;
185 gstelement_class = (GstElementClass *) klass;
187 gstelement_class->change_state =
188 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_change_state);
190 gst_element_class_add_pad_template (gstelement_class,
191 gst_static_pad_template_get (&gst_gdk_pixbuf_src_template));
192 gst_element_class_add_pad_template (gstelement_class,
193 gst_static_pad_template_get (&gst_gdk_pixbuf_sink_template));
194 gst_element_class_set_details_simple (gstelement_class,
195 "GdkPixbuf image decoder", "Codec/Decoder/Image",
196 "Decodes images in a video stream using GdkPixbuf",
197 "David A. Schleef <ds@schleef.org>, Renato Filho <renato.filho@indt.org.br>");
201 gst_gdk_pixbuf_init (GstGdkPixbuf * filter)
204 gst_pad_new_from_static_template (&gst_gdk_pixbuf_sink_template, "sink");
205 gst_pad_set_query_function (filter->sinkpad,
206 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_query));
207 gst_pad_set_chain_function (filter->sinkpad,
208 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_chain));
209 gst_pad_set_event_function (filter->sinkpad,
210 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_event));
211 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
214 gst_pad_new_from_static_template (&gst_gdk_pixbuf_src_template, "src");
215 gst_pad_use_fixed_caps (filter->srcpad);
216 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
218 filter->last_timestamp = GST_CLOCK_TIME_NONE;
219 filter->pixbuf_loader = NULL;
223 gst_gdk_pixbuf_setup_pool (GstGdkPixbuf * filter, GstVideoInfo * info)
228 GstStructure *config;
229 guint size, min, max;
231 target = gst_pad_get_current_caps (filter->srcpad);
233 /* try to get a bufferpool now */
234 /* find a pool for the negotiated caps now */
235 query = gst_query_new_allocation (target, TRUE);
237 if (!gst_pad_peer_query (filter->srcpad, query)) {
238 /* not a problem, we use the query defaults */
239 GST_DEBUG_OBJECT (filter, "ALLOCATION query failed");
242 if (gst_query_get_n_allocation_pools (query) > 0) {
243 /* we got configuration from our peer, parse them */
244 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
252 /* we did not get a pool, make one ourselves then */
253 pool = gst_buffer_pool_new ();
257 config = gst_buffer_pool_get_config (pool);
258 gst_buffer_pool_config_set (config, target, size, min, max, 0, 0, 0);
259 gst_buffer_pool_set_config (pool, config);
262 gst_buffer_pool_set_active (filter->pool, FALSE);
263 gst_object_unref (filter->pool);
268 gst_buffer_pool_set_active (filter->pool, TRUE);
270 gst_caps_unref (target);
276 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
283 int in_rowstride, out_rowstride;
285 GstCaps *caps = NULL;
290 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
294 width = gdk_pixbuf_get_width (pixbuf);
295 height = gdk_pixbuf_get_height (pixbuf);
297 if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN) {
301 GST_DEBUG ("Set size to %dx%d", width, height);
303 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
304 switch (n_channels) {
306 fmt = GST_VIDEO_FORMAT_RGB;
309 fmt = GST_VIDEO_FORMAT_RGBA;
312 goto channels_not_supported;
316 gst_video_info_init (&info);
317 gst_video_info_set_format (&info, fmt, width, height);
318 info.fps_n = filter->in_fps_n;
319 info.fps_d = filter->in_fps_d;
320 caps = gst_video_info_to_caps (&info);
324 gst_pad_set_caps (filter->srcpad, caps);
325 gst_caps_unref (caps);
327 gst_gdk_pixbuf_setup_pool (filter, &info);
330 ret = gst_buffer_pool_acquire_buffer (filter->pool, &outbuf, NULL);
331 if (ret != GST_FLOW_OK)
334 GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
335 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
337 in_pix = gdk_pixbuf_get_pixels (pixbuf);
338 in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
340 gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE);
341 out_pix = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
342 out_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0);
344 for (y = 0; y < height; y++) {
345 memcpy (out_pix, in_pix, width * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, 0));
346 in_pix += in_rowstride;
347 out_pix += out_rowstride;
350 gst_video_frame_unmap (&frame);
352 GST_DEBUG ("pushing... %d bytes", gst_buffer_get_size (outbuf));
353 ret = gst_pad_push (filter->srcpad, outbuf);
355 if (ret != GST_FLOW_OK)
356 GST_DEBUG_OBJECT (filter, "flow: %s", gst_flow_get_name (ret));
363 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
364 return GST_FLOW_ERROR;
366 channels_not_supported:
368 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
369 ("%d channels not supported", n_channels));
370 return GST_FLOW_ERROR;
374 GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
380 gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
382 GstFlowReturn res = GST_FLOW_OK;
383 gboolean ret = TRUE, forward = TRUE;
384 GstGdkPixbuf *pixbuf;
386 pixbuf = GST_GDK_PIXBUF (parent);
388 switch (GST_EVENT_TYPE (event)) {
393 gst_event_parse_caps (event, &caps);
394 ret = gst_gdk_pixbuf_sink_setcaps (pixbuf, caps);
399 if (pixbuf->pixbuf_loader != NULL) {
400 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
401 res = gst_gdk_pixbuf_flush (pixbuf);
402 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
403 pixbuf->pixbuf_loader = NULL;
404 /* as long as we don't have flow returns for event functions we need
405 * to post an error here, or the application might never know that
407 if (res != GST_FLOW_OK && res != GST_FLOW_FLUSHING) {
408 GST_ELEMENT_ERROR (pixbuf, STREAM, FAILED, (NULL),
409 ("Flow: %s", gst_flow_get_name (res)));
415 case GST_EVENT_SEGMENT:
416 case GST_EVENT_FLUSH_STOP:
417 if (pixbuf->pixbuf_loader != NULL) {
418 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
419 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
420 pixbuf->pixbuf_loader = NULL;
427 ret = gst_pad_event_default (pad, parent, event);
429 gst_event_unref (event);
435 gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
437 GstGdkPixbuf *filter;
438 GstFlowReturn ret = GST_FLOW_OK;
439 GError *error = NULL;
440 GstClockTime timestamp;
443 filter = GST_GDK_PIXBUF (parent);
445 timestamp = GST_BUFFER_TIMESTAMP (buf);
447 if (GST_CLOCK_TIME_IS_VALID (timestamp))
448 filter->last_timestamp = timestamp;
450 GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
451 GST_TIME_ARGS (timestamp));
453 if (filter->pixbuf_loader == NULL)
454 filter->pixbuf_loader = gdk_pixbuf_loader_new ();
456 gst_buffer_map (buf, &map, GST_MAP_READ);
458 GST_LOG_OBJECT (filter, "Writing buffer size %d", (gint) map.size);
459 if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, map.data, map.size,
463 /* packetised mode? */
464 if (filter->in_fps_n != 0) {
465 gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
466 ret = gst_gdk_pixbuf_flush (filter);
467 g_object_unref (filter->pixbuf_loader);
468 filter->pixbuf_loader = NULL;
471 gst_buffer_unmap (buf, &map);
472 gst_buffer_unref (buf);
479 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
480 ("gdk_pixbuf_loader_write error: %s", error->message));
481 g_error_free (error);
482 gst_buffer_unmap (buf, &map);
483 gst_buffer_unref (buf);
484 return GST_FLOW_ERROR;
488 static GstStateChangeReturn
489 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition)
491 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
492 GstGdkPixbuf *dec = GST_GDK_PIXBUF (element);
494 switch (transition) {
495 case GST_STATE_CHANGE_READY_TO_PAUSED:
496 /* default to single image mode, setcaps function might not be called */
499 gst_video_info_init (&dec->info);
505 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
506 if (ret == GST_STATE_CHANGE_FAILURE)
509 switch (transition) {
510 case GST_STATE_CHANGE_PAUSED_TO_READY:
514 gst_buffer_pool_set_active (dec->pool, FALSE);
515 gst_object_replace ((GstObject **) & dec->pool, NULL);
525 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
527 #ifdef enable_typefind
529 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
532 GdkPixbufLoader *pixbuf_loader;
533 GdkPixbufFormat *format;
535 data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
539 GST_DEBUG ("creating new loader");
541 pixbuf_loader = gdk_pixbuf_loader_new ();
543 gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
546 format = gdk_pixbuf_loader_get_format (pixbuf_loader);
548 if (format != NULL) {
551 gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
553 for (p = mlist; *p; ++p) {
554 GST_DEBUG ("suggesting mime type %s", *p);
555 caps = gst_caps_new_simple (*p, NULL);
556 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
557 gst_caps_free (caps);
562 GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
563 /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
564 to close a gzip that's not an svg; fixed upstream but no good way
566 gdk_pixbuf_loader_close (pixbuf_loader, NULL);
567 GST_DEBUG ("closed pixbuf loader");
568 g_object_unref (G_OBJECT (pixbuf_loader));
572 /* entry point to initialize the plug-in
573 * initialize the plug-in itself
574 * register the element factories and pad templates
575 * register the features
578 plugin_init (GstPlugin * plugin)
580 GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
581 "gdk pixbuf loader");
583 if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_SECONDARY,
584 GST_TYPE_GDK_PIXBUF))
587 #ifdef enable_typefind
588 gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
589 gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
592 if (!gst_element_register (plugin, "gdkpixbufsink", GST_RANK_NONE,
593 GST_TYPE_GDK_PIXBUF_SINK))
596 if (!pixbufscale_init (plugin))
599 /* plugin initialisation succeeded */
604 /* this is the structure that gst-register looks for
605 * so keep the name plugin_desc, or you cannot get your plug-in registered */
606 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
609 "GdkPixbuf-based image decoder, scaler and sink",
610 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)