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 "gstgdkpixbufoverlay.h"
31 #include "gstgdkpixbufsink.h"
32 #include "pixbufscale.h"
34 GST_DEBUG_CATEGORY_STATIC (gst_gdk_pixbuf_debug);
35 #define GST_CAT_DEFAULT gst_gdk_pixbuf_debug
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_MAKE ("RGB") "; "
71 GST_VIDEO_CAPS_MAKE ("RGBA"))
74 static GstStateChangeReturn
75 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition);
76 static GstFlowReturn gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent,
78 static gboolean gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent,
81 #ifdef enable_typefind
82 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
85 #define gst_gdk_pixbuf_parent_class parent_class
86 G_DEFINE_TYPE (GstGdkPixbuf, gst_gdk_pixbuf, GST_TYPE_ELEMENT);
89 gst_gdk_pixbuf_sink_setcaps (GstGdkPixbuf * filter, GstCaps * caps)
91 const GValue *framerate;
94 s = gst_caps_get_structure (caps, 0);
96 if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
97 filter->in_fps_n = gst_value_get_fraction_numerator (framerate);
98 filter->in_fps_d = gst_value_get_fraction_denominator (framerate);
99 GST_DEBUG_OBJECT (filter, "got framerate of %d/%d fps => packetized mode",
100 filter->in_fps_n, filter->in_fps_d);
102 filter->in_fps_n = 0;
103 filter->in_fps_d = 1;
104 GST_DEBUG_OBJECT (filter, "no framerate, assuming single image");
111 gst_gdk_pixbuf_get_capslist (GstCaps * filter)
115 GstCaps *capslist = NULL;
116 GstCaps *return_caps = NULL;
119 capslist = gst_caps_new_empty ();
120 slist0 = gdk_pixbuf_get_formats ();
122 for (slist = slist0; slist; slist = g_slist_next (slist)) {
123 GdkPixbufFormat *pixbuf_format;
127 pixbuf_format = slist->data;
128 mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
130 for (mimetype = mimetypes; *mimetype; mimetype++) {
131 gst_caps_append_structure (capslist, gst_structure_new_empty (*mimetype));
133 g_strfreev (mimetypes);
135 g_slist_free (slist0);
137 tmpl_caps = gst_static_caps_get (&gst_gdk_pixbuf_sink_template.static_caps);
138 return_caps = gst_caps_intersect (capslist, tmpl_caps);
140 gst_caps_unref (tmpl_caps);
141 gst_caps_unref (capslist);
143 if (filter && return_caps) {
146 temp = gst_caps_intersect (return_caps, filter);
147 gst_caps_unref (return_caps);
155 gst_gdk_pixbuf_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
159 switch (GST_QUERY_TYPE (query)) {
162 GstCaps *filter, *caps;
164 gst_query_parse_caps (query, &filter);
165 caps = gst_gdk_pixbuf_get_capslist (filter);
166 gst_query_set_caps_result (query, caps);
167 gst_caps_unref (caps);
173 res = gst_pad_query_default (pad, parent, query);
180 /* initialize the plugin's class */
182 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
184 GstElementClass *gstelement_class;
186 gstelement_class = (GstElementClass *) klass;
188 gstelement_class->change_state =
189 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_change_state);
191 gst_element_class_add_pad_template (gstelement_class,
192 gst_static_pad_template_get (&gst_gdk_pixbuf_src_template));
193 gst_element_class_add_pad_template (gstelement_class,
194 gst_static_pad_template_get (&gst_gdk_pixbuf_sink_template));
195 gst_element_class_set_details_simple (gstelement_class,
196 "GdkPixbuf image decoder", "Codec/Decoder/Image",
197 "Decodes images in a video stream using GdkPixbuf",
198 "David A. Schleef <ds@schleef.org>, Renato Filho <renato.filho@indt.org.br>");
202 gst_gdk_pixbuf_init (GstGdkPixbuf * filter)
205 gst_pad_new_from_static_template (&gst_gdk_pixbuf_sink_template, "sink");
206 gst_pad_set_query_function (filter->sinkpad,
207 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_query));
208 gst_pad_set_chain_function (filter->sinkpad,
209 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_chain));
210 gst_pad_set_event_function (filter->sinkpad,
211 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_event));
212 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
215 gst_pad_new_from_static_template (&gst_gdk_pixbuf_src_template, "src");
216 gst_pad_use_fixed_caps (filter->srcpad);
217 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
219 filter->last_timestamp = GST_CLOCK_TIME_NONE;
220 filter->pixbuf_loader = NULL;
224 gst_gdk_pixbuf_setup_pool (GstGdkPixbuf * filter, GstVideoInfo * info)
229 GstStructure *config;
230 guint size, min, max;
232 target = gst_pad_get_current_caps (filter->srcpad);
234 /* try to get a bufferpool now */
235 /* find a pool for the negotiated caps now */
236 query = gst_query_new_allocation (target, TRUE);
238 if (!gst_pad_peer_query (filter->srcpad, query)) {
239 /* not a problem, we use the query defaults */
240 GST_DEBUG_OBJECT (filter, "ALLOCATION query failed");
243 if (gst_query_get_n_allocation_pools (query) > 0) {
244 /* we got configuration from our peer, parse them */
245 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
253 /* we did not get a pool, make one ourselves then */
254 pool = gst_buffer_pool_new ();
258 config = gst_buffer_pool_get_config (pool);
259 gst_buffer_pool_config_set_params (config, target, size, min, max);
260 gst_buffer_pool_set_config (pool, config);
263 gst_buffer_pool_set_active (filter->pool, FALSE);
264 gst_object_unref (filter->pool);
269 gst_buffer_pool_set_active (filter->pool, TRUE);
271 gst_caps_unref (target);
277 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
284 int in_rowstride, out_rowstride;
286 GstCaps *caps = NULL;
291 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
295 width = gdk_pixbuf_get_width (pixbuf);
296 height = gdk_pixbuf_get_height (pixbuf);
298 if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN) {
302 GST_DEBUG ("Set size to %dx%d", width, height);
304 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
305 switch (n_channels) {
307 fmt = GST_VIDEO_FORMAT_RGB;
310 fmt = GST_VIDEO_FORMAT_RGBA;
313 goto channels_not_supported;
317 gst_video_info_init (&info);
318 gst_video_info_set_format (&info, fmt, width, height);
319 info.fps_n = filter->in_fps_n;
320 info.fps_d = filter->in_fps_d;
321 caps = gst_video_info_to_caps (&info);
325 gst_pad_set_caps (filter->srcpad, caps);
326 gst_caps_unref (caps);
328 gst_gdk_pixbuf_setup_pool (filter, &info);
331 ret = gst_buffer_pool_acquire_buffer (filter->pool, &outbuf, NULL);
332 if (ret != GST_FLOW_OK)
335 GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
336 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
338 in_pix = gdk_pixbuf_get_pixels (pixbuf);
339 in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
341 gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE);
342 out_pix = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
343 out_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0);
345 for (y = 0; y < height; y++) {
346 memcpy (out_pix, in_pix, width * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, 0));
347 in_pix += in_rowstride;
348 out_pix += out_rowstride;
351 gst_video_frame_unmap (&frame);
353 GST_DEBUG ("pushing... %d bytes", gst_buffer_get_size (outbuf));
354 ret = gst_pad_push (filter->srcpad, outbuf);
356 if (ret != GST_FLOW_OK)
357 GST_DEBUG_OBJECT (filter, "flow: %s", gst_flow_get_name (ret));
364 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
365 return GST_FLOW_ERROR;
367 channels_not_supported:
369 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
370 ("%d channels not supported", n_channels));
371 return GST_FLOW_ERROR;
375 GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
381 gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
383 GstFlowReturn res = GST_FLOW_OK;
384 gboolean ret = TRUE, forward = TRUE;
385 GstGdkPixbuf *pixbuf;
387 pixbuf = GST_GDK_PIXBUF (parent);
389 switch (GST_EVENT_TYPE (event)) {
394 gst_event_parse_caps (event, &caps);
395 ret = gst_gdk_pixbuf_sink_setcaps (pixbuf, caps);
400 if (pixbuf->pixbuf_loader != NULL) {
401 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
402 res = gst_gdk_pixbuf_flush (pixbuf);
403 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
404 pixbuf->pixbuf_loader = NULL;
405 /* as long as we don't have flow returns for event functions we need
406 * to post an error here, or the application might never know that
408 if (res != GST_FLOW_OK && res != GST_FLOW_FLUSHING) {
409 GST_ELEMENT_ERROR (pixbuf, STREAM, FAILED, (NULL),
410 ("Flow: %s", gst_flow_get_name (res)));
416 case GST_EVENT_SEGMENT:
417 case GST_EVENT_FLUSH_STOP:
418 if (pixbuf->pixbuf_loader != NULL) {
419 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
420 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
421 pixbuf->pixbuf_loader = NULL;
428 ret = gst_pad_event_default (pad, parent, event);
430 gst_event_unref (event);
436 gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
438 GstGdkPixbuf *filter;
439 GstFlowReturn ret = GST_FLOW_OK;
440 GError *error = NULL;
441 GstClockTime timestamp;
444 filter = GST_GDK_PIXBUF (parent);
446 timestamp = GST_BUFFER_TIMESTAMP (buf);
448 if (GST_CLOCK_TIME_IS_VALID (timestamp))
449 filter->last_timestamp = timestamp;
451 GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
452 GST_TIME_ARGS (timestamp));
454 if (filter->pixbuf_loader == NULL)
455 filter->pixbuf_loader = gdk_pixbuf_loader_new ();
457 gst_buffer_map (buf, &map, GST_MAP_READ);
459 GST_LOG_OBJECT (filter, "Writing buffer size %d", (gint) map.size);
460 if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, map.data, map.size,
464 /* packetised mode? */
465 if (filter->in_fps_n != 0) {
466 gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
467 ret = gst_gdk_pixbuf_flush (filter);
468 g_object_unref (filter->pixbuf_loader);
469 filter->pixbuf_loader = NULL;
472 gst_buffer_unmap (buf, &map);
473 gst_buffer_unref (buf);
480 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
481 ("gdk_pixbuf_loader_write error: %s", error->message));
482 g_error_free (error);
483 gst_buffer_unmap (buf, &map);
484 gst_buffer_unref (buf);
485 return GST_FLOW_ERROR;
489 static GstStateChangeReturn
490 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition)
492 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
493 GstGdkPixbuf *dec = GST_GDK_PIXBUF (element);
495 switch (transition) {
496 case GST_STATE_CHANGE_READY_TO_PAUSED:
497 /* default to single image mode, setcaps function might not be called */
500 gst_video_info_init (&dec->info);
506 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
507 if (ret == GST_STATE_CHANGE_FAILURE)
510 switch (transition) {
511 case GST_STATE_CHANGE_PAUSED_TO_READY:
515 gst_buffer_pool_set_active (dec->pool, FALSE);
516 gst_object_replace ((GstObject **) & dec->pool, NULL);
526 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
528 #ifdef enable_typefind
530 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
533 GdkPixbufLoader *pixbuf_loader;
534 GdkPixbufFormat *format;
536 data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
540 GST_DEBUG ("creating new loader");
542 pixbuf_loader = gdk_pixbuf_loader_new ();
544 gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
547 format = gdk_pixbuf_loader_get_format (pixbuf_loader);
549 if (format != NULL) {
552 gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
554 for (p = mlist; *p; ++p) {
555 GST_DEBUG ("suggesting mime type %s", *p);
556 caps = gst_caps_new_simple (*p, NULL);
557 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
558 gst_caps_free (caps);
563 GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
564 /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
565 to close a gzip that's not an svg; fixed upstream but no good way
567 gdk_pixbuf_loader_close (pixbuf_loader, NULL);
568 GST_DEBUG ("closed pixbuf loader");
569 g_object_unref (G_OBJECT (pixbuf_loader));
573 /* entry point to initialize the plug-in
574 * initialize the plug-in itself
575 * register the element factories and pad templates
576 * register the features
579 plugin_init (GstPlugin * plugin)
581 GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
582 "gdk pixbuf loader");
584 if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_SECONDARY,
585 GST_TYPE_GDK_PIXBUF))
588 #ifdef enable_typefind
589 gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
590 gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
593 if (!gst_element_register (plugin, "gdkpixbufoverlay", GST_RANK_NONE,
594 GST_TYPE_GDK_PIXBUF_OVERLAY))
597 if (!gst_element_register (plugin, "gdkpixbufsink", GST_RANK_NONE,
598 GST_TYPE_GDK_PIXBUF_SINK))
601 if (!pixbufscale_init (plugin))
604 /* plugin initialisation succeeded */
609 /* this is the structure that gst-register looks for
610 * so keep the name plugin_desc, or you cannot get your plug-in registered */
611 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
614 "GdkPixbuf-based image decoder, scaler and sink",
615 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)