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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include <gst/video/video.h>
27 #include <gdk-pixbuf/gdk-pixbuf.h>
30 #include "gstgdkpixbufdec.h"
32 GST_DEBUG_CATEGORY_STATIC (gdkpixbufdec_debug);
33 #define GST_CAT_DEFAULT gdkpixbufdec_debug
35 static GstStaticPadTemplate gst_gdk_pixbuf_dec_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
39 GST_STATIC_CAPS ("image/png; "
40 /* "image/jpeg; " disabled because we can't handle MJPEG */
43 "application/x-navi-animation; "
44 "image/x-cmu-raster; "
45 "image/x-sun-raster; "
48 "image/x-portable-anymap; "
49 "image/x-portable-bitmap; "
50 "image/x-portable-graymap; "
51 "image/x-portable-pixmap; "
55 "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga; "
56 "image/x-pcx; image/svg; image/svg+xml")
59 static GstStaticPadTemplate gst_gdk_pixbuf_dec_src_template =
60 GST_STATIC_PAD_TEMPLATE ("src",
63 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB") "; "
64 GST_VIDEO_CAPS_MAKE ("RGBA"))
67 static GstStateChangeReturn
68 gst_gdk_pixbuf_dec_change_state (GstElement * element,
69 GstStateChange transition);
70 static GstFlowReturn gst_gdk_pixbuf_dec_chain (GstPad * pad, GstObject * parent,
72 static gboolean gst_gdk_pixbuf_dec_sink_event (GstPad * pad, GstObject * parent,
75 #define gst_gdk_pixbuf_dec_parent_class parent_class
76 G_DEFINE_TYPE (GstGdkPixbufDec, gst_gdk_pixbuf_dec, GST_TYPE_ELEMENT);
79 gst_gdk_pixbuf_dec_sink_setcaps (GstGdkPixbufDec * filter, GstCaps * caps)
81 const GValue *framerate;
84 s = gst_caps_get_structure (caps, 0);
86 if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
87 filter->in_fps_n = gst_value_get_fraction_numerator (framerate);
88 filter->in_fps_d = gst_value_get_fraction_denominator (framerate);
89 GST_DEBUG_OBJECT (filter, "got framerate of %d/%d fps => packetized mode",
90 filter->in_fps_n, filter->in_fps_d);
94 GST_DEBUG_OBJECT (filter, "no framerate, assuming single image");
101 gst_gdk_pixbuf_dec_get_capslist (GstCaps * filter)
105 GstCaps *capslist = NULL;
106 GstCaps *return_caps = NULL;
109 capslist = gst_caps_new_empty ();
110 slist0 = gdk_pixbuf_get_formats ();
112 for (slist = slist0; slist; slist = g_slist_next (slist)) {
113 GdkPixbufFormat *pixbuf_format;
117 pixbuf_format = slist->data;
118 mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
120 for (mimetype = mimetypes; *mimetype; mimetype++) {
121 gst_caps_append_structure (capslist, gst_structure_new_empty (*mimetype));
123 g_strfreev (mimetypes);
125 g_slist_free (slist0);
128 gst_static_caps_get (&gst_gdk_pixbuf_dec_sink_template.static_caps);
129 return_caps = gst_caps_intersect (capslist, tmpl_caps);
131 gst_caps_unref (tmpl_caps);
132 gst_caps_unref (capslist);
134 if (filter && return_caps) {
137 temp = gst_caps_intersect (return_caps, filter);
138 gst_caps_unref (return_caps);
146 gst_gdk_pixbuf_dec_sink_query (GstPad * pad, GstObject * parent,
151 switch (GST_QUERY_TYPE (query)) {
154 GstCaps *filter, *caps;
156 gst_query_parse_caps (query, &filter);
157 caps = gst_gdk_pixbuf_dec_get_capslist (filter);
158 gst_query_set_caps_result (query, caps);
159 gst_caps_unref (caps);
165 res = gst_pad_query_default (pad, parent, query);
172 /* initialize the plugin's class */
174 gst_gdk_pixbuf_dec_class_init (GstGdkPixbufDecClass * klass)
176 GstElementClass *gstelement_class;
178 gstelement_class = (GstElementClass *) klass;
180 gstelement_class->change_state =
181 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_dec_change_state);
183 gst_element_class_add_static_pad_template (gstelement_class,
184 &gst_gdk_pixbuf_dec_src_template);
185 gst_element_class_add_static_pad_template (gstelement_class,
186 &gst_gdk_pixbuf_dec_sink_template);
187 gst_element_class_set_static_metadata (gstelement_class,
188 "GdkPixbuf image decoder", "Codec/Decoder/Image",
189 "Decodes images in a video stream using GdkPixbuf",
190 "David A. Schleef <ds@schleef.org>, Renato Filho <renato.filho@indt.org.br>");
192 GST_DEBUG_CATEGORY_INIT (gdkpixbufdec_debug, "gdkpixbuf", 0,
193 "GdkPixbuf image decoder");
197 gst_gdk_pixbuf_dec_init (GstGdkPixbufDec * filter)
200 gst_pad_new_from_static_template (&gst_gdk_pixbuf_dec_sink_template,
202 gst_pad_set_query_function (filter->sinkpad,
203 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_dec_sink_query));
204 gst_pad_set_chain_function (filter->sinkpad,
205 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_dec_chain));
206 gst_pad_set_event_function (filter->sinkpad,
207 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_dec_sink_event));
208 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
211 gst_pad_new_from_static_template (&gst_gdk_pixbuf_dec_src_template,
213 gst_pad_use_fixed_caps (filter->srcpad);
214 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
216 filter->last_timestamp = GST_CLOCK_TIME_NONE;
217 filter->pixbuf_loader = NULL;
218 filter->packetized = FALSE;
222 gst_gdk_pixbuf_dec_setup_pool (GstGdkPixbufDec * filter, GstVideoInfo * info)
227 GstStructure *config;
228 guint size, min, max;
230 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);
252 gst_query_unref (query);
255 /* we did not get a pool, make one ourselves then */
256 pool = gst_buffer_pool_new ();
260 config = gst_buffer_pool_get_config (pool);
261 gst_buffer_pool_config_set_params (config, target, size, min, max);
262 gst_buffer_pool_set_config (pool, config);
265 gst_buffer_pool_set_active (filter->pool, FALSE);
266 gst_object_unref (filter->pool);
271 gst_buffer_pool_set_active (filter->pool, TRUE);
273 gst_caps_unref (target);
279 gst_gdk_pixbuf_dec_flush (GstGdkPixbufDec * filter)
286 int in_rowstride, out_rowstride;
288 GstCaps *caps = NULL;
293 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
297 width = gdk_pixbuf_get_width (pixbuf);
298 height = gdk_pixbuf_get_height (pixbuf);
300 if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN) {
305 GST_DEBUG ("Set size to %dx%d", width, height);
307 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
308 switch (n_channels) {
310 fmt = GST_VIDEO_FORMAT_RGB;
313 fmt = GST_VIDEO_FORMAT_RGBA;
316 goto channels_not_supported;
320 gst_video_info_init (&info);
321 gst_video_info_set_format (&info, fmt, width, height);
322 info.fps_n = filter->in_fps_n;
323 info.fps_d = filter->in_fps_d;
324 caps = gst_video_info_to_caps (&info);
328 gst_pad_set_caps (filter->srcpad, caps);
329 gst_caps_unref (caps);
331 gst_gdk_pixbuf_dec_setup_pool (filter, &info);
333 for (l = filter->pending_events; l; l = l->next)
334 gst_pad_push_event (filter->srcpad, l->data);
335 g_list_free (filter->pending_events);
336 filter->pending_events = NULL;
339 ret = gst_buffer_pool_acquire_buffer (filter->pool, &outbuf, NULL);
340 if (ret != GST_FLOW_OK)
343 GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
344 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
346 in_pix = gdk_pixbuf_get_pixels (pixbuf);
347 in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
349 gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE);
350 out_pix = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
351 out_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0);
353 for (y = 0; y < height; y++) {
354 memcpy (out_pix, in_pix, width * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, 0));
355 in_pix += in_rowstride;
356 out_pix += out_rowstride;
359 gst_video_frame_unmap (&frame);
361 GST_DEBUG ("pushing... %" G_GSIZE_FORMAT " bytes",
362 gst_buffer_get_size (outbuf));
363 ret = gst_pad_push (filter->srcpad, outbuf);
365 if (ret != GST_FLOW_OK)
366 GST_DEBUG_OBJECT (filter, "flow: %s", gst_flow_get_name (ret));
373 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
374 return GST_FLOW_ERROR;
376 channels_not_supported:
378 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
379 ("%d channels not supported", n_channels));
380 return GST_FLOW_ERROR;
384 GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
390 gst_gdk_pixbuf_dec_sink_event (GstPad * pad, GstObject * parent,
393 GstFlowReturn res = GST_FLOW_OK;
394 gboolean ret = TRUE, forward = TRUE;
395 GstGdkPixbufDec *pixbuf;
397 pixbuf = GST_GDK_PIXBUF_DEC (parent);
399 switch (GST_EVENT_TYPE (event)) {
404 gst_event_parse_caps (event, &caps);
405 ret = gst_gdk_pixbuf_dec_sink_setcaps (pixbuf, caps);
410 if (pixbuf->pixbuf_loader != NULL) {
411 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
412 res = gst_gdk_pixbuf_dec_flush (pixbuf);
413 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
414 pixbuf->pixbuf_loader = NULL;
415 /* as long as we don't have flow returns for event functions we need
416 * to post an error here, or the application might never know that
418 if (res != GST_FLOW_OK && res != GST_FLOW_FLUSHING
419 && res != GST_FLOW_EOS && res != GST_FLOW_NOT_LINKED) {
420 GST_ELEMENT_FLOW_ERROR (pixbuf, res);
426 case GST_EVENT_FLUSH_STOP:
427 g_list_free_full (pixbuf->pending_events,
428 (GDestroyNotify) gst_event_unref);
429 pixbuf->pending_events = NULL;
431 case GST_EVENT_SEGMENT:
433 const GstSegment *segment;
434 gst_event_parse_segment (event, &segment);
435 if (segment->format == GST_FORMAT_BYTES)
436 pixbuf->packetized = FALSE;
438 pixbuf->packetized = TRUE;
439 if (pixbuf->pixbuf_loader != NULL) {
440 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
441 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
442 pixbuf->pixbuf_loader = NULL;
450 if (!gst_pad_has_current_caps (pixbuf->srcpad) &&
451 GST_EVENT_IS_SERIALIZED (event)
452 && GST_EVENT_TYPE (event) > GST_EVENT_CAPS
453 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP
454 && GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
456 pixbuf->pending_events = g_list_prepend (pixbuf->pending_events, event);
458 ret = gst_pad_event_default (pad, parent, event);
461 gst_event_unref (event);
467 gst_gdk_pixbuf_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
469 GstGdkPixbufDec *filter;
470 GstFlowReturn ret = GST_FLOW_OK;
471 GError *error = NULL;
472 GstClockTime timestamp;
475 filter = GST_GDK_PIXBUF_DEC (parent);
477 timestamp = GST_BUFFER_TIMESTAMP (buf);
479 if (GST_CLOCK_TIME_IS_VALID (timestamp))
480 filter->last_timestamp = timestamp;
482 GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
483 GST_TIME_ARGS (timestamp));
485 if (filter->pixbuf_loader == NULL)
486 filter->pixbuf_loader = gdk_pixbuf_loader_new ();
488 gst_buffer_map (buf, &map, GST_MAP_READ);
490 GST_LOG_OBJECT (filter, "Writing buffer size %d", (gint) map.size);
491 if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, map.data, map.size,
495 if (filter->packetized == TRUE) {
496 gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
497 ret = gst_gdk_pixbuf_dec_flush (filter);
498 g_object_unref (filter->pixbuf_loader);
499 filter->pixbuf_loader = NULL;
502 gst_buffer_unmap (buf, &map);
503 gst_buffer_unref (buf);
510 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
511 ("gdk_pixbuf_loader_write error: %s", error->message));
512 g_error_free (error);
513 gst_buffer_unmap (buf, &map);
514 gst_buffer_unref (buf);
515 return GST_FLOW_ERROR;
519 static GstStateChangeReturn
520 gst_gdk_pixbuf_dec_change_state (GstElement * element,
521 GstStateChange transition)
523 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
524 GstGdkPixbufDec *dec = GST_GDK_PIXBUF_DEC (element);
526 switch (transition) {
527 case GST_STATE_CHANGE_READY_TO_PAUSED:
528 /* default to single image mode, setcaps function might not be called */
531 gst_video_info_init (&dec->info);
537 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
538 if (ret == GST_STATE_CHANGE_FAILURE)
541 switch (transition) {
542 case GST_STATE_CHANGE_PAUSED_TO_READY:
546 gst_buffer_pool_set_active (dec->pool, FALSE);
547 gst_object_replace ((GstObject **) & dec->pool, NULL);
549 g_list_free_full (dec->pending_events, (GDestroyNotify) gst_event_unref);
550 dec->pending_events = NULL;
551 if (dec->pixbuf_loader != NULL) {
552 gdk_pixbuf_loader_close (dec->pixbuf_loader, NULL);
553 g_object_unref (G_OBJECT (dec->pixbuf_loader));
554 dec->pixbuf_loader = NULL;