Printf format fixes.
[platform/upstream/gst-plugins-good.git] / ext / gdk_pixbuf / gstgdkpixbuf.c
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>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <gst/gst.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <gst/video/video.h>
27 #include <string.h>
28
29 #include "gstgdkpixbuf.h"
30
31 GST_DEBUG_CATEGORY_STATIC (gst_gdk_pixbuf_debug);
32 #define GST_CAT_DEFAULT gst_gdk_pixbuf_debug
33
34 static const GstElementDetails plugin_details =
35 GST_ELEMENT_DETAILS ("GdkPixbuf image decoder",
36     "Codec/Decoder/Image",
37     "Decodes images in a video stream using GdkPixbuf",
38     "David A. Schleef <ds@schleef.org>, Renato Filho <renato.filho@indt.org.br>");
39
40 enum
41 {
42   ARG_0,
43   ARG_SILENT                    /* FIXME 0.11: remove */
44 };
45
46 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
47     GST_STATIC_PAD_TEMPLATE ("sink",
48     GST_PAD_SINK,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("image/png; "
51         /* "image/jpeg; " disabled because we can't handle MJPEG */
52         "image/gif; "
53         "image/x-icon; "
54         "application/x-navi-animation; "
55         "image/x-cmu-raster; "
56         "image/x-sun-raster; "
57         "image/x-pixmap; "
58         "image/tiff; "
59         "image/x-portable-anymap; "
60         "image/x-portable-bitmap; "
61         "image/x-portable-graymap; "
62         "image/x-portable-pixmap; "
63         "image/bmp; "
64         "image/x-bmp; "
65         "image/x-MS-bmp; "
66         "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga; "
67         "image/x-pcx; image/svg; image/svg+xml")
68     );
69
70 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
71     GST_STATIC_PAD_TEMPLATE ("src",
72     GST_PAD_SRC,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB "; " GST_VIDEO_CAPS_RGBA)
75     );
76
77 gboolean pixbufscale_init (GstPlugin * plugin);
78
79 static void gst_gdk_pixbuf_base_init (gpointer g_class);
80 static void gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass);
81 static void gst_gdk_pixbuf_init (GstGdkPixbuf * filter,
82     GstGdkPixbufClass * klass);
83
84 static void gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
85     const GValue * value, GParamSpec * pspec);
86 static void gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
87     GValue * value, GParamSpec * pspec);
88
89 static GstFlowReturn gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buffer);
90 static gboolean gst_gdk_pixbuf_sink_event (GstPad * pad, GstEvent * event);
91
92 #ifdef enable_typefind
93 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
94 #endif
95
96 GST_BOILERPLATE (GstGdkPixbuf, gst_gdk_pixbuf, GstElement, GST_TYPE_ELEMENT);
97
98 static gboolean
99 gst_gdk_pixbuf_sink_setcaps (GstPad * pad, GstCaps * caps)
100 {
101   GstGdkPixbuf *filter;
102   const GValue *framerate;
103   GstStructure *s;
104
105   filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
106   s = gst_caps_get_structure (caps, 0);
107
108   filter->framerate_numerator = 0;
109   filter->framerate_denominator = 1;
110   if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
111     filter->framerate_numerator = gst_value_get_fraction_numerator (framerate);
112     filter->framerate_denominator =
113         gst_value_get_fraction_denominator (framerate);
114     GST_DEBUG ("got framerate of %d/%d fps => packetized mode",
115         filter->framerate_numerator, filter->framerate_denominator);
116   }
117   gst_object_unref (filter);
118
119   return TRUE;
120 }
121
122 static GstCaps *
123 gst_gdk_pixbuf_get_capslist (void)
124 {
125   GSList *slist;
126   GSList *slist0;
127   GstCaps *capslist = NULL;
128   GstCaps *return_caps = NULL;
129
130   capslist = gst_caps_new_empty ();
131   slist0 = gdk_pixbuf_get_formats ();
132
133   for (slist = slist0; slist; slist = g_slist_next (slist)) {
134     GdkPixbufFormat *pixbuf_format;
135     char **mimetypes;
136     char **mimetype;
137
138     pixbuf_format = slist->data;
139     mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
140
141     for (mimetype = mimetypes; *mimetype; mimetype++) {
142       gst_caps_append_structure (capslist, gst_structure_new (*mimetype, NULL));
143     }
144     g_strfreev (mimetypes);
145   }
146   g_slist_free (slist0);
147
148   return_caps = gst_caps_intersect (capslist,
149       gst_static_caps_get (&gst_gdk_pixbuf_sink_template.static_caps));
150
151   gst_caps_unref (capslist);
152   return return_caps;
153 }
154
155 static GstCaps *
156 gst_gdk_pixbuf_sink_getcaps (GstPad * pad)
157 {
158   return gst_gdk_pixbuf_get_capslist ();
159 }
160
161 static void
162 gst_gdk_pixbuf_base_init (gpointer g_class)
163 {
164   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
165
166   gst_element_class_add_pad_template (element_class,
167       gst_static_pad_template_get (&gst_gdk_pixbuf_src_template));
168   gst_element_class_add_pad_template (element_class,
169       gst_static_pad_template_get (&gst_gdk_pixbuf_sink_template));
170   gst_element_class_set_details (element_class, &plugin_details);
171 }
172
173 /* initialize the plugin's class */
174 static void
175 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
176 {
177   GObjectClass *gobject_class;
178   GstElementClass *gstelement_class;
179
180   gobject_class = (GObjectClass *) klass;
181   gstelement_class = (GstElementClass *) klass;
182
183   gobject_class->set_property = gst_gdk_pixbuf_set_property;
184   gobject_class->get_property = gst_gdk_pixbuf_get_property;
185
186   g_object_class_install_property (gobject_class, ARG_SILENT,
187       g_param_spec_boolean ("silent", "Silent",
188           "Produce verbose output ? (deprecated)", FALSE, G_PARAM_READWRITE));
189 }
190
191 static void
192 gst_gdk_pixbuf_init (GstGdkPixbuf * filter, GstGdkPixbufClass * klass)
193 {
194   filter->sinkpad =
195       gst_pad_new_from_static_template (&gst_gdk_pixbuf_sink_template, "sink");
196   gst_pad_set_setcaps_function (filter->sinkpad,
197       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_setcaps));
198   gst_pad_set_getcaps_function (filter->sinkpad,
199       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_getcaps));
200   gst_pad_set_chain_function (filter->sinkpad,
201       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_chain));
202   gst_pad_set_event_function (filter->sinkpad,
203       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_event));
204   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
205
206   filter->srcpad =
207       gst_pad_new_from_static_template (&gst_gdk_pixbuf_src_template, "src");
208   gst_pad_use_fixed_caps (filter->srcpad);
209   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
210
211   filter->last_timestamp = GST_CLOCK_TIME_NONE;
212   filter->pixbuf_loader = NULL;
213 }
214
215 static GstFlowReturn
216 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
217 {
218   GstBuffer *outbuf;
219   GdkPixbuf *pixbuf;
220   int y;
221   guint8 *out_pix;
222   guint8 *in_pix;
223   int in_rowstride;
224   GstFlowReturn ret;
225   GstCaps *caps = NULL;
226   gint n_channels;
227
228   pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
229   if (pixbuf == NULL)
230     goto no_pixbuf;
231
232   if (filter->image_size == 0) {
233     filter->width = gdk_pixbuf_get_width (pixbuf);
234     filter->height = gdk_pixbuf_get_height (pixbuf);
235     filter->rowstride = gdk_pixbuf_get_rowstride (pixbuf);
236     filter->image_size = filter->rowstride * filter->height;
237
238     n_channels = gdk_pixbuf_get_n_channels (pixbuf);
239     switch (n_channels) {
240       case 3:
241         caps = gst_caps_from_string (GST_VIDEO_CAPS_RGB);
242         break;
243       case 4:
244         caps = gst_caps_from_string (GST_VIDEO_CAPS_RGBA);
245         break;
246       default:
247         goto channels_not_supported;
248     }
249
250     gst_caps_set_simple (caps,
251         "width", G_TYPE_INT, filter->width,
252         "height", G_TYPE_INT, filter->height,
253         "framerate", GST_TYPE_FRACTION, filter->framerate_numerator,
254         filter->framerate_denominator, NULL);
255
256     GST_DEBUG ("Set size to %dx%d", filter->width, filter->height);
257     gst_pad_set_caps (filter->srcpad, caps);
258     gst_caps_unref (caps);
259   }
260
261   ret = gst_pad_alloc_buffer_and_set_caps (filter->srcpad,
262       GST_BUFFER_OFFSET_NONE,
263       filter->image_size, GST_PAD_CAPS (filter->srcpad), &outbuf);
264
265   if (ret != GST_FLOW_OK)
266     goto no_buffer;
267
268   GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
269   GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
270
271   in_pix = gdk_pixbuf_get_pixels (pixbuf);
272   in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
273   out_pix = GST_BUFFER_DATA (outbuf);
274
275   /* FIXME, last line might not have rowstride pixels */
276   for (y = 0; y < filter->height; y++) {
277     memcpy (out_pix, in_pix, filter->rowstride);
278     in_pix += in_rowstride;
279     out_pix += filter->rowstride;
280   }
281
282   GST_DEBUG ("pushing... %d bytes", GST_BUFFER_SIZE (outbuf));
283   return gst_pad_push (filter->srcpad, outbuf);
284
285   /* ERRORS */
286 no_pixbuf:
287   {
288     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
289     return GST_FLOW_ERROR;
290   }
291 channels_not_supported:
292   {
293     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
294         ("%d channels not supported", n_channels));
295     return GST_FLOW_ERROR;
296   }
297 no_buffer:
298   {
299     GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
300     return ret;
301   }
302 }
303
304 static gboolean
305 gst_gdk_pixbuf_sink_event (GstPad * pad, GstEvent * event)
306 {
307   GstFlowReturn res = GST_FLOW_OK;
308   gboolean ret = TRUE;
309   GstGdkPixbuf *pixbuf;
310
311   pixbuf = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
312
313   switch (GST_EVENT_TYPE (event)) {
314     case GST_EVENT_EOS:
315       if (pixbuf->pixbuf_loader != NULL) {
316         gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
317         res = gst_gdk_pixbuf_flush (pixbuf);
318         g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
319         pixbuf->pixbuf_loader = NULL;
320       }
321       break;
322     case GST_EVENT_NEWSEGMENT:
323     case GST_EVENT_FLUSH_STOP:
324       if (pixbuf->pixbuf_loader != NULL) {
325         gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
326         g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
327         pixbuf->pixbuf_loader = NULL;
328       }
329       break;
330     default:
331       break;
332   }
333
334   if (res == GST_FLOW_OK) {
335     ret = gst_pad_event_default (pad, event);
336   } else {
337     ret = FALSE;
338   }
339
340   gst_object_unref (pixbuf);
341
342   return ret;
343 }
344
345 static GstFlowReturn
346 gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buf)
347 {
348   GstGdkPixbuf *filter;
349   GstFlowReturn ret = GST_FLOW_OK;
350   GError *error = NULL;
351   GstClockTime timestamp;
352   guint8 *data;
353   guint size;
354
355   filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
356
357   timestamp = GST_BUFFER_TIMESTAMP (buf);
358
359   if (GST_CLOCK_TIME_IS_VALID (timestamp))
360     filter->last_timestamp = timestamp;
361
362   GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
363       GST_TIME_ARGS (timestamp));
364
365   if (filter->pixbuf_loader == NULL)
366     filter->pixbuf_loader = gdk_pixbuf_loader_new ();
367
368   data = GST_BUFFER_DATA (buf);
369   size = GST_BUFFER_SIZE (buf);
370
371   GST_LOG_OBJECT (filter, "Writing buffer size %d", size);
372   if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, data, size, &error))
373     goto error;
374
375   /* packetised mode? */
376   if (filter->framerate_numerator != 0) {
377     gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
378     ret = gst_gdk_pixbuf_flush (filter);
379     g_object_unref (filter->pixbuf_loader);
380     filter->pixbuf_loader = NULL;
381   }
382
383   gst_object_unref (filter);
384
385   return ret;
386
387   /* ERRORS */
388 error:
389   {
390     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
391         ("gdk_pixbuf_loader_write error: %s", error->message));
392     g_error_free (error);
393     gst_object_unref (filter);
394     return GST_FLOW_ERROR;
395   }
396 }
397
398 static void
399 gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
400     const GValue * value, GParamSpec * pspec)
401 {
402   GstGdkPixbuf *filter;
403
404   filter = GST_GDK_PIXBUF (object);
405
406   switch (prop_id) {
407     case ARG_SILENT:
408       /* filter->silent = g_value_get_boolean (value); */
409       break;
410     default:
411       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412       break;
413   }
414 }
415
416 static void
417 gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
418     GValue * value, GParamSpec * pspec)
419 {
420   GstGdkPixbuf *filter;
421
422   filter = GST_GDK_PIXBUF (object);
423
424   switch (prop_id) {
425     case ARG_SILENT:
426       /* g_value_set_boolean (value, filter->silent); */
427       break;
428     default:
429       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
430       break;
431   }
432 }
433
434 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
435
436 #ifdef enable_typefind
437 static void
438 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
439 {
440   guint8 *data;
441   GdkPixbufLoader *pixbuf_loader;
442   GdkPixbufFormat *format;
443
444   data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
445   if (data == NULL)
446     return;
447
448   GST_DEBUG ("creating new loader");
449
450   pixbuf_loader = gdk_pixbuf_loader_new ();
451
452   gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
453       NULL);
454
455   format = gdk_pixbuf_loader_get_format (pixbuf_loader);
456
457   if (format != NULL) {
458     GstCaps *caps;
459     gchar **p;
460     gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
461
462     for (p = mlist; *p; ++p) {
463       GST_DEBUG ("suggesting mime type %s", *p);
464       caps = gst_caps_new_simple (*p, NULL);
465       gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
466       gst_caps_free (caps);
467     }
468     g_strfreev (mlist);
469   }
470
471   GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
472   /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
473      to close a gzip that's not an svg; fixed upstream but no good way
474      to work around it */
475   gdk_pixbuf_loader_close (pixbuf_loader, NULL);
476   GST_DEBUG ("closed pixbuf loader");
477   g_object_unref (G_OBJECT (pixbuf_loader));
478 }
479 #endif
480
481 /* entry point to initialize the plug-in
482  * initialize the plug-in itself
483  * register the element factories and pad templates
484  * register the features
485  */
486 static gboolean
487 plugin_init (GstPlugin * plugin)
488 {
489   GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
490       "gdk pixbuf loader");
491
492   if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_MARGINAL,
493           GST_TYPE_GDK_PIXBUF))
494     return FALSE;
495
496 #ifdef enable_typefind
497   gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
498       gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
499 #endif
500
501   if (!pixbufscale_init (plugin))
502     return FALSE;
503
504   /* plugin initialisation succeeded */
505   return TRUE;
506 }
507
508
509 /* this is the structure that gst-register looks for
510  * so keep the name plugin_desc, or you cannot get your plug-in registered */
511 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
512     GST_VERSION_MINOR,
513     "gdkpixbuf",
514     "GDK Pixbuf decoder & scaler",
515     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)