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.
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_pad_template (gstelement_class,
184 gst_static_pad_template_get (&gst_gdk_pixbuf_dec_src_template));
185 gst_element_class_add_pad_template (gstelement_class,
186 gst_static_pad_template_get (&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;
221 gst_gdk_pixbuf_dec_setup_pool (GstGdkPixbufDec * filter, GstVideoInfo * info)
226 GstStructure *config;
227 guint size, min, max;
229 target = gst_pad_get_current_caps (filter->srcpad);
231 /* try to get a bufferpool now */
232 /* find a pool for the negotiated caps now */
233 query = gst_query_new_allocation (target, TRUE);
235 if (!gst_pad_peer_query (filter->srcpad, query)) {
236 /* not a problem, we use the query defaults */
237 GST_DEBUG_OBJECT (filter, "ALLOCATION query failed");
240 if (gst_query_get_n_allocation_pools (query) > 0) {
241 /* we got configuration from our peer, parse them */
242 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
250 /* we did not get a pool, make one ourselves then */
251 pool = gst_buffer_pool_new ();
255 config = gst_buffer_pool_get_config (pool);
256 gst_buffer_pool_config_set_params (config, target, size, min, max);
257 gst_buffer_pool_set_config (pool, config);
260 gst_buffer_pool_set_active (filter->pool, FALSE);
261 gst_object_unref (filter->pool);
266 gst_buffer_pool_set_active (filter->pool, TRUE);
268 gst_caps_unref (target);
274 gst_gdk_pixbuf_dec_flush (GstGdkPixbufDec * filter)
281 int in_rowstride, out_rowstride;
283 GstCaps *caps = NULL;
288 pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
292 width = gdk_pixbuf_get_width (pixbuf);
293 height = gdk_pixbuf_get_height (pixbuf);
295 if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN) {
299 GST_DEBUG ("Set size to %dx%d", width, height);
301 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
302 switch (n_channels) {
304 fmt = GST_VIDEO_FORMAT_RGB;
307 fmt = GST_VIDEO_FORMAT_RGBA;
310 goto channels_not_supported;
314 gst_video_info_init (&info);
315 gst_video_info_set_format (&info, fmt, width, height);
316 info.fps_n = filter->in_fps_n;
317 info.fps_d = filter->in_fps_d;
318 caps = gst_video_info_to_caps (&info);
322 gst_pad_set_caps (filter->srcpad, caps);
323 gst_caps_unref (caps);
325 gst_gdk_pixbuf_dec_setup_pool (filter, &info);
328 ret = gst_buffer_pool_acquire_buffer (filter->pool, &outbuf, NULL);
329 if (ret != GST_FLOW_OK)
332 GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
333 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
335 in_pix = gdk_pixbuf_get_pixels (pixbuf);
336 in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
338 gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE);
339 out_pix = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
340 out_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0);
342 for (y = 0; y < height; y++) {
343 memcpy (out_pix, in_pix, width * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, 0));
344 in_pix += in_rowstride;
345 out_pix += out_rowstride;
348 gst_video_frame_unmap (&frame);
350 GST_DEBUG ("pushing... %d bytes", gst_buffer_get_size (outbuf));
351 ret = gst_pad_push (filter->srcpad, outbuf);
353 if (ret != GST_FLOW_OK)
354 GST_DEBUG_OBJECT (filter, "flow: %s", gst_flow_get_name (ret));
361 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
362 return GST_FLOW_ERROR;
364 channels_not_supported:
366 GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
367 ("%d channels not supported", n_channels));
368 return GST_FLOW_ERROR;
372 GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
378 gst_gdk_pixbuf_dec_sink_event (GstPad * pad, GstObject * parent,
381 GstFlowReturn res = GST_FLOW_OK;
382 gboolean ret = TRUE, forward = TRUE;
383 GstGdkPixbufDec *pixbuf;
385 pixbuf = GST_GDK_PIXBUF_DEC (parent);
387 switch (GST_EVENT_TYPE (event)) {
392 gst_event_parse_caps (event, &caps);
393 ret = gst_gdk_pixbuf_dec_sink_setcaps (pixbuf, caps);
398 if (pixbuf->pixbuf_loader != NULL) {
399 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
400 res = gst_gdk_pixbuf_dec_flush (pixbuf);
401 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
402 pixbuf->pixbuf_loader = NULL;
403 /* as long as we don't have flow returns for event functions we need
404 * to post an error here, or the application might never know that
406 if (res != GST_FLOW_OK && res != GST_FLOW_FLUSHING) {
407 GST_ELEMENT_ERROR (pixbuf, STREAM, FAILED, (NULL),
408 ("Flow: %s", gst_flow_get_name (res)));
414 case GST_EVENT_SEGMENT:
415 case GST_EVENT_FLUSH_STOP:
416 if (pixbuf->pixbuf_loader != NULL) {
417 gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
418 g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
419 pixbuf->pixbuf_loader = NULL;
426 ret = gst_pad_event_default (pad, parent, event);
428 gst_event_unref (event);
434 gst_gdk_pixbuf_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
436 GstGdkPixbufDec *filter;
437 GstFlowReturn ret = GST_FLOW_OK;
438 GError *error = NULL;
439 GstClockTime timestamp;
442 filter = GST_GDK_PIXBUF_DEC (parent);
444 timestamp = GST_BUFFER_TIMESTAMP (buf);
446 if (GST_CLOCK_TIME_IS_VALID (timestamp))
447 filter->last_timestamp = timestamp;
449 GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
450 GST_TIME_ARGS (timestamp));
452 if (filter->pixbuf_loader == NULL)
453 filter->pixbuf_loader = gdk_pixbuf_loader_new ();
455 gst_buffer_map (buf, &map, GST_MAP_READ);
457 GST_LOG_OBJECT (filter, "Writing buffer size %d", (gint) map.size);
458 if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, map.data, map.size,
462 /* packetised mode? *//* FIXME: shouln't this be fps_d != 0, since 0/1
463 * might be packetised mode but variable framerate */
464 if (filter->in_fps_n != 0) {
465 gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
466 ret = gst_gdk_pixbuf_dec_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_dec_change_state (GstElement * element,
490 GstStateChange transition)
492 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
493 GstGdkPixbufDec *dec = GST_GDK_PIXBUF_DEC (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);