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)
227 GstBufferPool *pool = NULL;
228 guint size, min, max, prefix, padding, alignment;
230 target = gst_pad_get_current_caps (filter->srcpad);
232 /* try to get a bufferpool now */
233 /* find a pool for the negotiated caps now */
234 query = gst_query_new_allocation (target, TRUE);
236 if (gst_pad_peer_query (filter->srcpad, query)) {
237 /* we got configuration from our peer, parse them */
238 gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
239 &padding, &alignment, &pool);
249 GstStructure *config;
251 /* we did not get a pool, make one ourselves then */
252 pool = gst_buffer_pool_new ();
254 config = gst_buffer_pool_get_config (pool);
255 gst_buffer_pool_config_set (config, target, size, min, max, prefix,
257 gst_buffer_pool_set_config (pool, config);
261 gst_object_unref (filter->pool);
265 gst_buffer_pool_set_active (filter->pool, TRUE);
267 gst_caps_unref (target);
273 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
280 int in_rowstride, out_rowstride;
282 GstCaps *caps = NULL;
287 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
291 width = gdk_pixbuf_get_width (pixbuf);
292 height = gdk_pixbuf_get_height (pixbuf);
294 if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN) {
298 GST_DEBUG ("Set size to %dx%d", width, height);
300 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
301 switch (n_channels) {
303 fmt = GST_VIDEO_FORMAT_RGB;
306 fmt = GST_VIDEO_FORMAT_RGBA;
309 goto channels_not_supported;
313 gst_video_info_init (&info);
314 gst_video_info_set_format (&info, fmt, width, height);
315 info.fps_n = filter->in_fps_n;
316 info.fps_d = filter->in_fps_d;
317 caps = gst_video_info_to_caps (&info);
321 gst_pad_set_caps (filter->srcpad, caps);
322 gst_caps_unref (caps);
324 gst_gdk_pixbuf_setup_pool (filter, &info);
327 ret = gst_buffer_pool_acquire_buffer (filter->pool, &outbuf, NULL);
328 if (ret != GST_FLOW_OK)
331 GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
332 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
334 in_pix = gdk_pixbuf_get_pixels (pixbuf);
335 in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
337 gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE);
338 out_pix = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
339 out_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0);
341 for (y = 0; y < height; y++) {
342 memcpy (out_pix, in_pix, width * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, 0));
343 in_pix += in_rowstride;
344 out_pix += out_rowstride;
347 gst_video_frame_unmap (&frame);
349 GST_DEBUG ("pushing... %d bytes", gst_buffer_get_size (outbuf));
350 ret = gst_pad_push (filter->srcpad, outbuf);
352 if (ret != GST_FLOW_OK)
353 GST_DEBUG_OBJECT (filter, "flow: %s", gst_flow_get_name (ret));
360 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
361 return GST_FLOW_ERROR;
363 channels_not_supported:
365 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
366 ("%d channels not supported", n_channels));
367 return GST_FLOW_ERROR;
371 GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
377 gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
379 GstFlowReturn res = GST_FLOW_OK;
380 gboolean ret = TRUE, forward = TRUE;
381 GstGdkPixbuf *pixbuf;
383 pixbuf = GST_GDK_PIXBUF (parent);
385 switch (GST_EVENT_TYPE (event)) {
390 gst_event_parse_caps (event, &caps);
391 ret = gst_gdk_pixbuf_sink_setcaps (pixbuf, caps);
396 if (pixbuf->pixbuf_loader != NULL) {
397 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
398 res = gst_gdk_pixbuf_flush (pixbuf);
399 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
400 pixbuf->pixbuf_loader = NULL;
401 /* as long as we don't have flow returns for event functions we need
402 * to post an error here, or the application might never know that
404 if (res != GST_FLOW_OK && res != GST_FLOW_FLUSHING) {
405 GST_ELEMENT_ERROR (pixbuf, STREAM, FAILED, (NULL),
406 ("Flow: %s", gst_flow_get_name (res)));
412 case GST_EVENT_SEGMENT:
413 case GST_EVENT_FLUSH_STOP:
414 if (pixbuf->pixbuf_loader != NULL) {
415 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
416 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
417 pixbuf->pixbuf_loader = NULL;
424 ret = gst_pad_event_default (pad, parent, event);
426 gst_event_unref (event);
432 gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
434 GstGdkPixbuf *filter;
435 GstFlowReturn ret = GST_FLOW_OK;
436 GError *error = NULL;
437 GstClockTime timestamp;
440 filter = GST_GDK_PIXBUF (parent);
442 timestamp = GST_BUFFER_TIMESTAMP (buf);
444 if (GST_CLOCK_TIME_IS_VALID (timestamp))
445 filter->last_timestamp = timestamp;
447 GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
448 GST_TIME_ARGS (timestamp));
450 if (filter->pixbuf_loader == NULL)
451 filter->pixbuf_loader = gdk_pixbuf_loader_new ();
453 gst_buffer_map (buf, &map, GST_MAP_READ);
455 GST_LOG_OBJECT (filter, "Writing buffer size %d", (gint) map.size);
456 if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, map.data, map.size,
460 /* packetised mode? */
461 if (filter->in_fps_n != 0) {
462 gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
463 ret = gst_gdk_pixbuf_flush (filter);
464 g_object_unref (filter->pixbuf_loader);
465 filter->pixbuf_loader = NULL;
468 gst_buffer_unmap (buf, &map);
469 gst_buffer_unref (buf);
476 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
477 ("gdk_pixbuf_loader_write error: %s", error->message));
478 g_error_free (error);
479 gst_buffer_unmap (buf, &map);
480 gst_buffer_unref (buf);
481 return GST_FLOW_ERROR;
485 static GstStateChangeReturn
486 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition)
488 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
489 GstGdkPixbuf *dec = GST_GDK_PIXBUF (element);
491 switch (transition) {
492 case GST_STATE_CHANGE_READY_TO_PAUSED:
493 /* default to single image mode, setcaps function might not be called */
496 gst_video_info_init (&dec->info);
502 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
503 if (ret == GST_STATE_CHANGE_FAILURE)
506 switch (transition) {
507 case GST_STATE_CHANGE_PAUSED_TO_READY:
511 gst_buffer_pool_set_active (dec->pool, FALSE);
512 gst_object_replace ((GstObject **) & dec->pool, NULL);
522 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
524 #ifdef enable_typefind
526 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
529 GdkPixbufLoader *pixbuf_loader;
530 GdkPixbufFormat *format;
532 data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
536 GST_DEBUG ("creating new loader");
538 pixbuf_loader = gdk_pixbuf_loader_new ();
540 gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
543 format = gdk_pixbuf_loader_get_format (pixbuf_loader);
545 if (format != NULL) {
548 gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
550 for (p = mlist; *p; ++p) {
551 GST_DEBUG ("suggesting mime type %s", *p);
552 caps = gst_caps_new_simple (*p, NULL);
553 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
554 gst_caps_free (caps);
559 GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
560 /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
561 to close a gzip that's not an svg; fixed upstream but no good way
563 gdk_pixbuf_loader_close (pixbuf_loader, NULL);
564 GST_DEBUG ("closed pixbuf loader");
565 g_object_unref (G_OBJECT (pixbuf_loader));
569 /* entry point to initialize the plug-in
570 * initialize the plug-in itself
571 * register the element factories and pad templates
572 * register the features
575 plugin_init (GstPlugin * plugin)
577 GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
578 "gdk pixbuf loader");
580 if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_SECONDARY,
581 GST_TYPE_GDK_PIXBUF))
584 #ifdef enable_typefind
585 gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
586 gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
589 if (!gst_element_register (plugin, "gdkpixbufsink", GST_RANK_NONE,
590 GST_TYPE_GDK_PIXBUF_SINK))
593 if (!pixbufscale_init (plugin))
596 /* plugin initialisation succeeded */
601 /* this is the structure that gst-register looks for
602 * so keep the name plugin_desc, or you cannot get your plug-in registered */
603 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
606 "GdkPixbuf-based image decoder, scaler and sink",
607 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)