Merge branch 'master' into 0.11
[platform/upstream/gstreamer.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 <gst/video/video.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <string.h>
28
29 #include "gstgdkpixbuf.h"
30 #include "gstgdkpixbufoverlay.h"
31 #include "gstgdkpixbufsink.h"
32 #include "pixbufscale.h"
33
34 GST_DEBUG_CATEGORY_STATIC (gst_gdk_pixbuf_debug);
35 #define GST_CAT_DEFAULT gst_gdk_pixbuf_debug
36
37 enum
38 {
39   ARG_0,
40 };
41
42 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
43     GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("image/png; "
47         /* "image/jpeg; " disabled because we can't handle MJPEG */
48         "image/gif; "
49         "image/x-icon; "
50         "application/x-navi-animation; "
51         "image/x-cmu-raster; "
52         "image/x-sun-raster; "
53         "image/x-pixmap; "
54         "image/tiff; "
55         "image/x-portable-anymap; "
56         "image/x-portable-bitmap; "
57         "image/x-portable-graymap; "
58         "image/x-portable-pixmap; "
59         "image/bmp; "
60         "image/x-bmp; "
61         "image/x-MS-bmp; "
62         "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga; "
63         "image/x-pcx; image/svg; image/svg+xml")
64     );
65
66 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
67     GST_STATIC_PAD_TEMPLATE ("src",
68     GST_PAD_SRC,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB") "; "
71         GST_VIDEO_CAPS_MAKE ("RGBA"))
72     );
73
74 static GstStateChangeReturn
75 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition);
76 static GstFlowReturn gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent,
77     GstBuffer * buffer);
78 static gboolean gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent,
79     GstEvent * event);
80
81 #ifdef enable_typefind
82 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
83 #endif
84
85 #define gst_gdk_pixbuf_parent_class parent_class
86 G_DEFINE_TYPE (GstGdkPixbuf, gst_gdk_pixbuf, GST_TYPE_ELEMENT);
87
88 static gboolean
89 gst_gdk_pixbuf_sink_setcaps (GstGdkPixbuf * filter, GstCaps * caps)
90 {
91   const GValue *framerate;
92   GstStructure *s;
93
94   s = gst_caps_get_structure (caps, 0);
95
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);
101   } else {
102     filter->in_fps_n = 0;
103     filter->in_fps_d = 1;
104     GST_DEBUG_OBJECT (filter, "no framerate, assuming single image");
105   }
106
107   return TRUE;
108 }
109
110 static GstCaps *
111 gst_gdk_pixbuf_get_capslist (GstCaps * filter)
112 {
113   GSList *slist;
114   GSList *slist0;
115   GstCaps *capslist = NULL;
116   GstCaps *return_caps = NULL;
117   GstCaps *tmpl_caps;
118
119   capslist = gst_caps_new_empty ();
120   slist0 = gdk_pixbuf_get_formats ();
121
122   for (slist = slist0; slist; slist = g_slist_next (slist)) {
123     GdkPixbufFormat *pixbuf_format;
124     char **mimetypes;
125     char **mimetype;
126
127     pixbuf_format = slist->data;
128     mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
129
130     for (mimetype = mimetypes; *mimetype; mimetype++) {
131       gst_caps_append_structure (capslist, gst_structure_new_empty (*mimetype));
132     }
133     g_strfreev (mimetypes);
134   }
135   g_slist_free (slist0);
136
137   tmpl_caps = gst_static_caps_get (&gst_gdk_pixbuf_sink_template.static_caps);
138   return_caps = gst_caps_intersect (capslist, tmpl_caps);
139
140   gst_caps_unref (tmpl_caps);
141   gst_caps_unref (capslist);
142
143   if (filter && return_caps) {
144     GstCaps *temp;
145
146     temp = gst_caps_intersect (return_caps, filter);
147     gst_caps_unref (return_caps);
148     return_caps = temp;
149   }
150
151   return return_caps;
152 }
153
154 static gboolean
155 gst_gdk_pixbuf_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
156 {
157   gboolean res;
158
159   switch (GST_QUERY_TYPE (query)) {
160     case GST_QUERY_CAPS:
161     {
162       GstCaps *filter, *caps;
163
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);
168
169       res = TRUE;
170       break;
171     }
172     default:
173       res = gst_pad_query_default (pad, parent, query);
174       break;
175   }
176   return res;
177 }
178
179
180 /* initialize the plugin's class */
181 static void
182 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
183 {
184   GstElementClass *gstelement_class;
185
186   gstelement_class = (GstElementClass *) klass;
187
188   gstelement_class->change_state =
189       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_change_state);
190
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>");
199 }
200
201 static void
202 gst_gdk_pixbuf_init (GstGdkPixbuf * filter)
203 {
204   filter->sinkpad =
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);
213
214   filter->srcpad =
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);
218
219   filter->last_timestamp = GST_CLOCK_TIME_NONE;
220   filter->pixbuf_loader = NULL;
221 }
222
223 static gboolean
224 gst_gdk_pixbuf_setup_pool (GstGdkPixbuf * filter, GstVideoInfo * info)
225 {
226   GstCaps *target;
227   GstQuery *query;
228   GstBufferPool *pool;
229   GstStructure *config;
230   guint size, min, max;
231
232   target = gst_pad_get_current_caps (filter->srcpad);
233
234   /* try to get a bufferpool now */
235   /* find a pool for the negotiated caps now */
236   query = gst_query_new_allocation (target, TRUE);
237
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");
241   }
242
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);
246   } else {
247     pool = NULL;
248     size = info->size;
249     min = max = 0;
250   }
251
252   if (pool == NULL) {
253     /* we did not get a pool, make one ourselves then */
254     pool = gst_buffer_pool_new ();
255   }
256
257   /* and configure */
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);
261
262   if (filter->pool) {
263     gst_buffer_pool_set_active (filter->pool, FALSE);
264     gst_object_unref (filter->pool);
265   }
266   filter->pool = pool;
267
268   /* and activate */
269   gst_buffer_pool_set_active (filter->pool, TRUE);
270
271   gst_caps_unref (target);
272
273   return TRUE;
274 }
275
276 static GstFlowReturn
277 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
278 {
279   GstBuffer *outbuf;
280   GdkPixbuf *pixbuf;
281   int y;
282   guint8 *out_pix;
283   guint8 *in_pix;
284   int in_rowstride, out_rowstride;
285   GstFlowReturn ret;
286   GstCaps *caps = NULL;
287   gint width, height;
288   gint n_channels;
289   GstVideoFrame frame;
290
291   pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
292   if (pixbuf == NULL)
293     goto no_pixbuf;
294
295   width = gdk_pixbuf_get_width (pixbuf);
296   height = gdk_pixbuf_get_height (pixbuf);
297
298   if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN) {
299     GstVideoInfo info;
300     GstVideoFormat fmt;
301
302     GST_DEBUG ("Set size to %dx%d", width, height);
303
304     n_channels = gdk_pixbuf_get_n_channels (pixbuf);
305     switch (n_channels) {
306       case 3:
307         fmt = GST_VIDEO_FORMAT_RGB;
308         break;
309       case 4:
310         fmt = GST_VIDEO_FORMAT_RGBA;
311         break;
312       default:
313         goto channels_not_supported;
314     }
315
316
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);
322
323     filter->info = info;
324
325     gst_pad_set_caps (filter->srcpad, caps);
326     gst_caps_unref (caps);
327
328     gst_gdk_pixbuf_setup_pool (filter, &info);
329   }
330
331   ret = gst_buffer_pool_acquire_buffer (filter->pool, &outbuf, NULL);
332   if (ret != GST_FLOW_OK)
333     goto no_buffer;
334
335   GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
336   GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
337
338   in_pix = gdk_pixbuf_get_pixels (pixbuf);
339   in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
340
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);
344
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;
349   }
350
351   gst_video_frame_unmap (&frame);
352
353   GST_DEBUG ("pushing... %d bytes", gst_buffer_get_size (outbuf));
354   ret = gst_pad_push (filter->srcpad, outbuf);
355
356   if (ret != GST_FLOW_OK)
357     GST_DEBUG_OBJECT (filter, "flow: %s", gst_flow_get_name (ret));
358
359   return ret;
360
361   /* ERRORS */
362 no_pixbuf:
363   {
364     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
365     return GST_FLOW_ERROR;
366   }
367 channels_not_supported:
368   {
369     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
370         ("%d channels not supported", n_channels));
371     return GST_FLOW_ERROR;
372   }
373 no_buffer:
374   {
375     GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
376     return ret;
377   }
378 }
379
380 static gboolean
381 gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
382 {
383   GstFlowReturn res = GST_FLOW_OK;
384   gboolean ret = TRUE, forward = TRUE;
385   GstGdkPixbuf *pixbuf;
386
387   pixbuf = GST_GDK_PIXBUF (parent);
388
389   switch (GST_EVENT_TYPE (event)) {
390     case GST_EVENT_CAPS:
391     {
392       GstCaps *caps;
393
394       gst_event_parse_caps (event, &caps);
395       ret = gst_gdk_pixbuf_sink_setcaps (pixbuf, caps);
396       forward = FALSE;
397       break;
398     }
399     case GST_EVENT_EOS:
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
407          * things failed */
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)));
411           forward = FALSE;
412           ret = FALSE;
413         }
414       }
415       break;
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;
422       }
423       break;
424     default:
425       break;
426   }
427   if (forward) {
428     ret = gst_pad_event_default (pad, parent, event);
429   } else {
430     gst_event_unref (event);
431   }
432   return ret;
433 }
434
435 static GstFlowReturn
436 gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
437 {
438   GstGdkPixbuf *filter;
439   GstFlowReturn ret = GST_FLOW_OK;
440   GError *error = NULL;
441   GstClockTime timestamp;
442   GstMapInfo map;
443
444   filter = GST_GDK_PIXBUF (parent);
445
446   timestamp = GST_BUFFER_TIMESTAMP (buf);
447
448   if (GST_CLOCK_TIME_IS_VALID (timestamp))
449     filter->last_timestamp = timestamp;
450
451   GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
452       GST_TIME_ARGS (timestamp));
453
454   if (filter->pixbuf_loader == NULL)
455     filter->pixbuf_loader = gdk_pixbuf_loader_new ();
456
457   gst_buffer_map (buf, &map, GST_MAP_READ);
458
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,
461           &error))
462     goto error;
463
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;
470   }
471
472   gst_buffer_unmap (buf, &map);
473   gst_buffer_unref (buf);
474
475   return ret;
476
477   /* ERRORS */
478 error:
479   {
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;
486   }
487 }
488
489 static GstStateChangeReturn
490 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition)
491 {
492   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
493   GstGdkPixbuf *dec = GST_GDK_PIXBUF (element);
494
495   switch (transition) {
496     case GST_STATE_CHANGE_READY_TO_PAUSED:
497       /* default to single image mode, setcaps function might not be called */
498       dec->in_fps_n = 0;
499       dec->in_fps_d = 1;
500       gst_video_info_init (&dec->info);
501       break;
502     default:
503       break;
504   }
505
506   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
507   if (ret == GST_STATE_CHANGE_FAILURE)
508     return ret;
509
510   switch (transition) {
511     case GST_STATE_CHANGE_PAUSED_TO_READY:
512       dec->in_fps_n = 0;
513       dec->in_fps_d = 0;
514       if (dec->pool) {
515         gst_buffer_pool_set_active (dec->pool, FALSE);
516         gst_object_replace ((GstObject **) & dec->pool, NULL);
517       }
518       break;
519     default:
520       break;
521   }
522
523   return ret;
524 }
525
526 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
527
528 #ifdef enable_typefind
529 static void
530 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
531 {
532   guint8 *data;
533   GdkPixbufLoader *pixbuf_loader;
534   GdkPixbufFormat *format;
535
536   data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
537   if (data == NULL)
538     return;
539
540   GST_DEBUG ("creating new loader");
541
542   pixbuf_loader = gdk_pixbuf_loader_new ();
543
544   gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
545       NULL);
546
547   format = gdk_pixbuf_loader_get_format (pixbuf_loader);
548
549   if (format != NULL) {
550     GstCaps *caps;
551     gchar **p;
552     gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
553
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);
559     }
560     g_strfreev (mlist);
561   }
562
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
566      to work around it */
567   gdk_pixbuf_loader_close (pixbuf_loader, NULL);
568   GST_DEBUG ("closed pixbuf loader");
569   g_object_unref (G_OBJECT (pixbuf_loader));
570 }
571 #endif
572
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
577  */
578 static gboolean
579 plugin_init (GstPlugin * plugin)
580 {
581   GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
582       "gdk pixbuf loader");
583
584   if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_SECONDARY,
585           GST_TYPE_GDK_PIXBUF))
586     return FALSE;
587
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);
591 #endif
592
593   if (!gst_element_register (plugin, "gdkpixbufoverlay", GST_RANK_NONE,
594           GST_TYPE_GDK_PIXBUF_OVERLAY))
595     return FALSE;
596
597   if (!gst_element_register (plugin, "gdkpixbufsink", GST_RANK_NONE,
598           GST_TYPE_GDK_PIXBUF_SINK))
599     return FALSE;
600
601   if (!pixbufscale_init (plugin))
602     return FALSE;
603
604   /* plugin initialisation succeeded */
605   return TRUE;
606 }
607
608
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,
612     GST_VERSION_MINOR,
613     "gdkpixbuf",
614     "GdkPixbuf-based image decoder, scaler and sink",
615     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)