0076a3cab3a947eb2c63fcf770ec8f14f08bfccf
[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 "gstgdkpixbufsink.h"
31 #include "pixbufscale.h"
32
33 GST_DEBUG_CATEGORY_STATIC (gst_gdk_pixbuf_debug);
34 #define GST_CAT_DEFAULT gst_gdk_pixbuf_debug
35
36 enum
37 {
38   ARG_0,
39 };
40
41 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
42     GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("image/png; "
46         /* "image/jpeg; " disabled because we can't handle MJPEG */
47         "image/gif; "
48         "image/x-icon; "
49         "application/x-navi-animation; "
50         "image/x-cmu-raster; "
51         "image/x-sun-raster; "
52         "image/x-pixmap; "
53         "image/tiff; "
54         "image/x-portable-anymap; "
55         "image/x-portable-bitmap; "
56         "image/x-portable-graymap; "
57         "image/x-portable-pixmap; "
58         "image/bmp; "
59         "image/x-bmp; "
60         "image/x-MS-bmp; "
61         "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga; "
62         "image/x-pcx; image/svg; image/svg+xml")
63     );
64
65 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
66     GST_STATIC_PAD_TEMPLATE ("src",
67     GST_PAD_SRC,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB") "; "
70         GST_VIDEO_CAPS_MAKE ("RGBA"))
71     );
72
73 static GstStateChangeReturn
74 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition);
75 static GstFlowReturn gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent,
76     GstBuffer * buffer);
77 static gboolean gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent,
78     GstEvent * event);
79
80 #ifdef enable_typefind
81 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
82 #endif
83
84 #define gst_gdk_pixbuf_parent_class parent_class
85 G_DEFINE_TYPE (GstGdkPixbuf, gst_gdk_pixbuf, GST_TYPE_ELEMENT);
86
87 static gboolean
88 gst_gdk_pixbuf_sink_setcaps (GstGdkPixbuf * filter, GstCaps * caps)
89 {
90   const GValue *framerate;
91   GstStructure *s;
92
93   s = gst_caps_get_structure (caps, 0);
94
95   if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
96     filter->in_fps_n = gst_value_get_fraction_numerator (framerate);
97     filter->in_fps_d = gst_value_get_fraction_denominator (framerate);
98     GST_DEBUG_OBJECT (filter, "got framerate of %d/%d fps => packetized mode",
99         filter->in_fps_n, filter->in_fps_d);
100   } else {
101     filter->in_fps_n = 0;
102     filter->in_fps_d = 1;
103     GST_DEBUG_OBJECT (filter, "no framerate, assuming single image");
104   }
105
106   return TRUE;
107 }
108
109 static GstCaps *
110 gst_gdk_pixbuf_get_capslist (GstCaps * filter)
111 {
112   GSList *slist;
113   GSList *slist0;
114   GstCaps *capslist = NULL;
115   GstCaps *return_caps = NULL;
116   GstCaps *tmpl_caps;
117
118   capslist = gst_caps_new_empty ();
119   slist0 = gdk_pixbuf_get_formats ();
120
121   for (slist = slist0; slist; slist = g_slist_next (slist)) {
122     GdkPixbufFormat *pixbuf_format;
123     char **mimetypes;
124     char **mimetype;
125
126     pixbuf_format = slist->data;
127     mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
128
129     for (mimetype = mimetypes; *mimetype; mimetype++) {
130       gst_caps_append_structure (capslist, gst_structure_new_empty (*mimetype));
131     }
132     g_strfreev (mimetypes);
133   }
134   g_slist_free (slist0);
135
136   tmpl_caps = gst_static_caps_get (&gst_gdk_pixbuf_sink_template.static_caps);
137   return_caps = gst_caps_intersect (capslist, tmpl_caps);
138
139   gst_caps_unref (tmpl_caps);
140   gst_caps_unref (capslist);
141
142   if (filter && return_caps) {
143     GstCaps *temp;
144
145     temp = gst_caps_intersect (return_caps, filter);
146     gst_caps_unref (return_caps);
147     return_caps = temp;
148   }
149
150   return return_caps;
151 }
152
153 static gboolean
154 gst_gdk_pixbuf_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
155 {
156   gboolean res;
157
158   switch (GST_QUERY_TYPE (query)) {
159     case GST_QUERY_CAPS:
160     {
161       GstCaps *filter, *caps;
162
163       gst_query_parse_caps (query, &filter);
164       caps = gst_gdk_pixbuf_get_capslist (filter);
165       gst_query_set_caps_result (query, caps);
166       gst_caps_unref (caps);
167
168       res = TRUE;
169       break;
170     }
171     default:
172       res = gst_pad_query_default (pad, parent, query);
173       break;
174   }
175   return res;
176 }
177
178
179 /* initialize the plugin's class */
180 static void
181 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
182 {
183   GstElementClass *gstelement_class;
184
185   gstelement_class = (GstElementClass *) klass;
186
187   gstelement_class->change_state =
188       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_change_state);
189
190   gst_element_class_add_pad_template (gstelement_class,
191       gst_static_pad_template_get (&gst_gdk_pixbuf_src_template));
192   gst_element_class_add_pad_template (gstelement_class,
193       gst_static_pad_template_get (&gst_gdk_pixbuf_sink_template));
194   gst_element_class_set_details_simple (gstelement_class,
195       "GdkPixbuf image decoder", "Codec/Decoder/Image",
196       "Decodes images in a video stream using GdkPixbuf",
197       "David A. Schleef <ds@schleef.org>, Renato Filho <renato.filho@indt.org.br>");
198 }
199
200 static void
201 gst_gdk_pixbuf_init (GstGdkPixbuf * filter)
202 {
203   filter->sinkpad =
204       gst_pad_new_from_static_template (&gst_gdk_pixbuf_sink_template, "sink");
205   gst_pad_set_query_function (filter->sinkpad,
206       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_query));
207   gst_pad_set_chain_function (filter->sinkpad,
208       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_chain));
209   gst_pad_set_event_function (filter->sinkpad,
210       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_event));
211   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
212
213   filter->srcpad =
214       gst_pad_new_from_static_template (&gst_gdk_pixbuf_src_template, "src");
215   gst_pad_use_fixed_caps (filter->srcpad);
216   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
217
218   filter->last_timestamp = GST_CLOCK_TIME_NONE;
219   filter->pixbuf_loader = NULL;
220 }
221
222 static gboolean
223 gst_gdk_pixbuf_setup_pool (GstGdkPixbuf * filter, GstVideoInfo * info)
224 {
225   GstCaps *target;
226   GstQuery *query;
227   GstBufferPool *pool = NULL;
228   guint size, min, max, prefix, padding, alignment;
229
230   target = gst_pad_get_current_caps (filter->srcpad);
231
232   /* try to get a bufferpool now */
233   /* find a pool for the negotiated caps now */
234   query = gst_query_new_allocation (target, TRUE);
235
236   if (gst_pad_peer_query (filter->srcpad, query)) {
237     /* we got configuration from our peer, parse them */
238     gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
239         &padding, &alignment, &pool);
240   } else {
241     size = info->size;
242     min = max = 0;
243     prefix = 0;
244     padding = 0;
245     alignment = 0;
246   }
247
248   if (pool == NULL) {
249     GstStructure *config;
250
251     /* we did not get a pool, make one ourselves then */
252     pool = gst_buffer_pool_new ();
253
254     config = gst_buffer_pool_get_config (pool);
255     gst_buffer_pool_config_set (config, target, size, min, max, prefix,
256         padding, alignment);
257     gst_buffer_pool_set_config (pool, config);
258   }
259
260   if (filter->pool)
261     gst_object_unref (filter->pool);
262   filter->pool = pool;
263
264   /* and activate */
265   gst_buffer_pool_set_active (filter->pool, TRUE);
266
267   gst_caps_unref (target);
268
269   return TRUE;
270 }
271
272 static GstFlowReturn
273 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
274 {
275   GstBuffer *outbuf;
276   GdkPixbuf *pixbuf;
277   int y;
278   guint8 *out_pix;
279   guint8 *in_pix;
280   int in_rowstride, out_rowstride;
281   GstFlowReturn ret;
282   GstCaps *caps = NULL;
283   gint width, height;
284   gint n_channels;
285   GstVideoFrame frame;
286
287   pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
288   if (pixbuf == NULL)
289     goto no_pixbuf;
290
291   width = gdk_pixbuf_get_width (pixbuf);
292   height = gdk_pixbuf_get_height (pixbuf);
293
294   if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN) {
295     GstVideoInfo info;
296     GstVideoFormat fmt;
297
298     GST_DEBUG ("Set size to %dx%d", width, height);
299
300     n_channels = gdk_pixbuf_get_n_channels (pixbuf);
301     switch (n_channels) {
302       case 3:
303         fmt = GST_VIDEO_FORMAT_RGB;
304         break;
305       case 4:
306         fmt = GST_VIDEO_FORMAT_RGBA;
307         break;
308       default:
309         goto channels_not_supported;
310     }
311
312
313     gst_video_info_init (&info);
314     gst_video_info_set_format (&info, fmt, width, height);
315     info.fps_n = filter->in_fps_n;
316     info.fps_d = filter->in_fps_d;
317     caps = gst_video_info_to_caps (&info);
318
319     filter->info = info;
320
321     gst_pad_set_caps (filter->srcpad, caps);
322     gst_caps_unref (caps);
323
324     gst_gdk_pixbuf_setup_pool (filter, &info);
325   }
326
327   ret = gst_buffer_pool_acquire_buffer (filter->pool, &outbuf, NULL);
328   if (ret != GST_FLOW_OK)
329     goto no_buffer;
330
331   GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
332   GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
333
334   in_pix = gdk_pixbuf_get_pixels (pixbuf);
335   in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
336
337   gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE);
338   out_pix = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
339   out_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0);
340
341   for (y = 0; y < height; y++) {
342     memcpy (out_pix, in_pix, width * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, 0));
343     in_pix += in_rowstride;
344     out_pix += out_rowstride;
345   }
346
347   gst_video_frame_unmap (&frame);
348
349   GST_DEBUG ("pushing... %d bytes", gst_buffer_get_size (outbuf));
350   ret = gst_pad_push (filter->srcpad, outbuf);
351
352   if (ret != GST_FLOW_OK)
353     GST_DEBUG_OBJECT (filter, "flow: %s", gst_flow_get_name (ret));
354
355   return ret;
356
357   /* ERRORS */
358 no_pixbuf:
359   {
360     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
361     return GST_FLOW_ERROR;
362   }
363 channels_not_supported:
364   {
365     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
366         ("%d channels not supported", n_channels));
367     return GST_FLOW_ERROR;
368   }
369 no_buffer:
370   {
371     GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
372     return ret;
373   }
374 }
375
376 static gboolean
377 gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
378 {
379   GstFlowReturn res = GST_FLOW_OK;
380   gboolean ret = TRUE, forward = TRUE;
381   GstGdkPixbuf *pixbuf;
382
383   pixbuf = GST_GDK_PIXBUF (parent);
384
385   switch (GST_EVENT_TYPE (event)) {
386     case GST_EVENT_CAPS:
387     {
388       GstCaps *caps;
389
390       gst_event_parse_caps (event, &caps);
391       ret = gst_gdk_pixbuf_sink_setcaps (pixbuf, caps);
392       forward = FALSE;
393       break;
394     }
395     case GST_EVENT_EOS:
396       if (pixbuf->pixbuf_loader != NULL) {
397         gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
398         res = gst_gdk_pixbuf_flush (pixbuf);
399         g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
400         pixbuf->pixbuf_loader = NULL;
401         /* as long as we don't have flow returns for event functions we need
402          * to post an error here, or the application might never know that
403          * things failed */
404         if (res != GST_FLOW_OK && res != GST_FLOW_FLUSHING) {
405           GST_ELEMENT_ERROR (pixbuf, STREAM, FAILED, (NULL),
406               ("Flow: %s", gst_flow_get_name (res)));
407           forward = FALSE;
408           ret = FALSE;
409         }
410       }
411       break;
412     case GST_EVENT_SEGMENT:
413     case GST_EVENT_FLUSH_STOP:
414       if (pixbuf->pixbuf_loader != NULL) {
415         gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
416         g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
417         pixbuf->pixbuf_loader = NULL;
418       }
419       break;
420     default:
421       break;
422   }
423   if (forward) {
424     ret = gst_pad_event_default (pad, parent, event);
425   } else {
426     gst_event_unref (event);
427   }
428   return ret;
429 }
430
431 static GstFlowReturn
432 gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
433 {
434   GstGdkPixbuf *filter;
435   GstFlowReturn ret = GST_FLOW_OK;
436   GError *error = NULL;
437   GstClockTime timestamp;
438   GstMapInfo map;
439
440   filter = GST_GDK_PIXBUF (parent);
441
442   timestamp = GST_BUFFER_TIMESTAMP (buf);
443
444   if (GST_CLOCK_TIME_IS_VALID (timestamp))
445     filter->last_timestamp = timestamp;
446
447   GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
448       GST_TIME_ARGS (timestamp));
449
450   if (filter->pixbuf_loader == NULL)
451     filter->pixbuf_loader = gdk_pixbuf_loader_new ();
452
453   gst_buffer_map (buf, &map, GST_MAP_READ);
454
455   GST_LOG_OBJECT (filter, "Writing buffer size %d", (gint) map.size);
456   if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, map.data, map.size,
457           &error))
458     goto error;
459
460   /* packetised mode? */
461   if (filter->in_fps_n != 0) {
462     gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
463     ret = gst_gdk_pixbuf_flush (filter);
464     g_object_unref (filter->pixbuf_loader);
465     filter->pixbuf_loader = NULL;
466   }
467
468   gst_buffer_unmap (buf, &map);
469   gst_buffer_unref (buf);
470
471   return ret;
472
473   /* ERRORS */
474 error:
475   {
476     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
477         ("gdk_pixbuf_loader_write error: %s", error->message));
478     g_error_free (error);
479     gst_buffer_unmap (buf, &map);
480     gst_buffer_unref (buf);
481     return GST_FLOW_ERROR;
482   }
483 }
484
485 static GstStateChangeReturn
486 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition)
487 {
488   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
489   GstGdkPixbuf *dec = GST_GDK_PIXBUF (element);
490
491   switch (transition) {
492     case GST_STATE_CHANGE_READY_TO_PAUSED:
493       /* default to single image mode, setcaps function might not be called */
494       dec->in_fps_n = 0;
495       dec->in_fps_d = 1;
496       gst_video_info_init (&dec->info);
497       break;
498     default:
499       break;
500   }
501
502   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
503   if (ret == GST_STATE_CHANGE_FAILURE)
504     return ret;
505
506   switch (transition) {
507     case GST_STATE_CHANGE_PAUSED_TO_READY:
508       dec->in_fps_n = 0;
509       dec->in_fps_d = 0;
510       if (dec->pool) {
511         gst_buffer_pool_set_active (dec->pool, FALSE);
512         gst_object_replace ((GstObject **) & dec->pool, NULL);
513       }
514       break;
515     default:
516       break;
517   }
518
519   return ret;
520 }
521
522 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
523
524 #ifdef enable_typefind
525 static void
526 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
527 {
528   guint8 *data;
529   GdkPixbufLoader *pixbuf_loader;
530   GdkPixbufFormat *format;
531
532   data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
533   if (data == NULL)
534     return;
535
536   GST_DEBUG ("creating new loader");
537
538   pixbuf_loader = gdk_pixbuf_loader_new ();
539
540   gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
541       NULL);
542
543   format = gdk_pixbuf_loader_get_format (pixbuf_loader);
544
545   if (format != NULL) {
546     GstCaps *caps;
547     gchar **p;
548     gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
549
550     for (p = mlist; *p; ++p) {
551       GST_DEBUG ("suggesting mime type %s", *p);
552       caps = gst_caps_new_simple (*p, NULL);
553       gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
554       gst_caps_free (caps);
555     }
556     g_strfreev (mlist);
557   }
558
559   GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
560   /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
561      to close a gzip that's not an svg; fixed upstream but no good way
562      to work around it */
563   gdk_pixbuf_loader_close (pixbuf_loader, NULL);
564   GST_DEBUG ("closed pixbuf loader");
565   g_object_unref (G_OBJECT (pixbuf_loader));
566 }
567 #endif
568
569 /* entry point to initialize the plug-in
570  * initialize the plug-in itself
571  * register the element factories and pad templates
572  * register the features
573  */
574 static gboolean
575 plugin_init (GstPlugin * plugin)
576 {
577   GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
578       "gdk pixbuf loader");
579
580   if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_SECONDARY,
581           GST_TYPE_GDK_PIXBUF))
582     return FALSE;
583
584 #ifdef enable_typefind
585   gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
586       gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
587 #endif
588
589   if (!gst_element_register (plugin, "gdkpixbufsink", GST_RANK_NONE,
590           GST_TYPE_GDK_PIXBUF_SINK))
591     return FALSE;
592
593   if (!pixbufscale_init (plugin))
594     return FALSE;
595
596   /* plugin initialisation succeeded */
597   return TRUE;
598 }
599
600
601 /* this is the structure that gst-register looks for
602  * so keep the name plugin_desc, or you cannot get your plug-in registered */
603 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
604     GST_VERSION_MINOR,
605     "gdkpixbuf",
606     "GdkPixbuf-based image decoder, scaler and sink",
607     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)