update for allocation query changes
[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;
228   GstStructure *config;
229   guint size, min, max;
230
231   target = gst_pad_get_current_caps (filter->srcpad);
232
233   /* try to get a bufferpool now */
234   /* find a pool for the negotiated caps now */
235   query = gst_query_new_allocation (target, TRUE);
236
237   if (!gst_pad_peer_query (filter->srcpad, query)) {
238     /* not a problem, we use the query defaults */
239     GST_DEBUG_OBJECT (filter, "ALLOCATION query failed");
240   }
241
242   if (gst_query_get_n_allocation_pools (query) > 0) {
243     /* we got configuration from our peer, parse them */
244     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
245   } else {
246     pool = NULL;
247     size = info->size;
248     min = max = 0;
249   }
250
251   if (pool == NULL) {
252     /* we did not get a pool, make one ourselves then */
253     pool = gst_buffer_pool_new ();
254   }
255
256   /* and configure */
257   config = gst_buffer_pool_get_config (pool);
258   gst_buffer_pool_config_set (config, target, size, min, max, 0, 0, 0);
259   gst_buffer_pool_set_config (pool, config);
260
261   if (filter->pool) {
262     gst_buffer_pool_set_active (filter->pool, FALSE);
263     gst_object_unref (filter->pool);
264   }
265   filter->pool = pool;
266
267   /* and activate */
268   gst_buffer_pool_set_active (filter->pool, TRUE);
269
270   gst_caps_unref (target);
271
272   return TRUE;
273 }
274
275 static GstFlowReturn
276 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
277 {
278   GstBuffer *outbuf;
279   GdkPixbuf *pixbuf;
280   int y;
281   guint8 *out_pix;
282   guint8 *in_pix;
283   int in_rowstride, out_rowstride;
284   GstFlowReturn ret;
285   GstCaps *caps = NULL;
286   gint width, height;
287   gint n_channels;
288   GstVideoFrame frame;
289
290   pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
291   if (pixbuf == NULL)
292     goto no_pixbuf;
293
294   width = gdk_pixbuf_get_width (pixbuf);
295   height = gdk_pixbuf_get_height (pixbuf);
296
297   if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN) {
298     GstVideoInfo info;
299     GstVideoFormat fmt;
300
301     GST_DEBUG ("Set size to %dx%d", width, height);
302
303     n_channels = gdk_pixbuf_get_n_channels (pixbuf);
304     switch (n_channels) {
305       case 3:
306         fmt = GST_VIDEO_FORMAT_RGB;
307         break;
308       case 4:
309         fmt = GST_VIDEO_FORMAT_RGBA;
310         break;
311       default:
312         goto channels_not_supported;
313     }
314
315
316     gst_video_info_init (&info);
317     gst_video_info_set_format (&info, fmt, width, height);
318     info.fps_n = filter->in_fps_n;
319     info.fps_d = filter->in_fps_d;
320     caps = gst_video_info_to_caps (&info);
321
322     filter->info = info;
323
324     gst_pad_set_caps (filter->srcpad, caps);
325     gst_caps_unref (caps);
326
327     gst_gdk_pixbuf_setup_pool (filter, &info);
328   }
329
330   ret = gst_buffer_pool_acquire_buffer (filter->pool, &outbuf, NULL);
331   if (ret != GST_FLOW_OK)
332     goto no_buffer;
333
334   GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
335   GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
336
337   in_pix = gdk_pixbuf_get_pixels (pixbuf);
338   in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
339
340   gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE);
341   out_pix = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
342   out_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0);
343
344   for (y = 0; y < height; y++) {
345     memcpy (out_pix, in_pix, width * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, 0));
346     in_pix += in_rowstride;
347     out_pix += out_rowstride;
348   }
349
350   gst_video_frame_unmap (&frame);
351
352   GST_DEBUG ("pushing... %d bytes", gst_buffer_get_size (outbuf));
353   ret = gst_pad_push (filter->srcpad, outbuf);
354
355   if (ret != GST_FLOW_OK)
356     GST_DEBUG_OBJECT (filter, "flow: %s", gst_flow_get_name (ret));
357
358   return ret;
359
360   /* ERRORS */
361 no_pixbuf:
362   {
363     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL), ("error geting pixbuf"));
364     return GST_FLOW_ERROR;
365   }
366 channels_not_supported:
367   {
368     GST_ELEMENT_ERROR (filter, STREAM, DECODE, (NULL),
369         ("%d channels not supported", n_channels));
370     return GST_FLOW_ERROR;
371   }
372 no_buffer:
373   {
374     GST_DEBUG ("Failed to create outbuffer - %s", gst_flow_get_name (ret));
375     return ret;
376   }
377 }
378
379 static gboolean
380 gst_gdk_pixbuf_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
381 {
382   GstFlowReturn res = GST_FLOW_OK;
383   gboolean ret = TRUE, forward = TRUE;
384   GstGdkPixbuf *pixbuf;
385
386   pixbuf = GST_GDK_PIXBUF (parent);
387
388   switch (GST_EVENT_TYPE (event)) {
389     case GST_EVENT_CAPS:
390     {
391       GstCaps *caps;
392
393       gst_event_parse_caps (event, &caps);
394       ret = gst_gdk_pixbuf_sink_setcaps (pixbuf, caps);
395       forward = FALSE;
396       break;
397     }
398     case GST_EVENT_EOS:
399       if (pixbuf->pixbuf_loader != NULL) {
400         gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
401         res = gst_gdk_pixbuf_flush (pixbuf);
402         g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
403         pixbuf->pixbuf_loader = NULL;
404         /* as long as we don't have flow returns for event functions we need
405          * to post an error here, or the application might never know that
406          * things failed */
407         if (res != GST_FLOW_OK && res != GST_FLOW_FLUSHING) {
408           GST_ELEMENT_ERROR (pixbuf, STREAM, FAILED, (NULL),
409               ("Flow: %s", gst_flow_get_name (res)));
410           forward = FALSE;
411           ret = FALSE;
412         }
413       }
414       break;
415     case GST_EVENT_SEGMENT:
416     case GST_EVENT_FLUSH_STOP:
417       if (pixbuf->pixbuf_loader != NULL) {
418         gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
419         g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
420         pixbuf->pixbuf_loader = NULL;
421       }
422       break;
423     default:
424       break;
425   }
426   if (forward) {
427     ret = gst_pad_event_default (pad, parent, event);
428   } else {
429     gst_event_unref (event);
430   }
431   return ret;
432 }
433
434 static GstFlowReturn
435 gst_gdk_pixbuf_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
436 {
437   GstGdkPixbuf *filter;
438   GstFlowReturn ret = GST_FLOW_OK;
439   GError *error = NULL;
440   GstClockTime timestamp;
441   GstMapInfo map;
442
443   filter = GST_GDK_PIXBUF (parent);
444
445   timestamp = GST_BUFFER_TIMESTAMP (buf);
446
447   if (GST_CLOCK_TIME_IS_VALID (timestamp))
448     filter->last_timestamp = timestamp;
449
450   GST_LOG_OBJECT (filter, "buffer with ts: %" GST_TIME_FORMAT,
451       GST_TIME_ARGS (timestamp));
452
453   if (filter->pixbuf_loader == NULL)
454     filter->pixbuf_loader = gdk_pixbuf_loader_new ();
455
456   gst_buffer_map (buf, &map, GST_MAP_READ);
457
458   GST_LOG_OBJECT (filter, "Writing buffer size %d", (gint) map.size);
459   if (!gdk_pixbuf_loader_write (filter->pixbuf_loader, map.data, map.size,
460           &error))
461     goto error;
462
463   /* packetised mode? */
464   if (filter->in_fps_n != 0) {
465     gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
466     ret = gst_gdk_pixbuf_flush (filter);
467     g_object_unref (filter->pixbuf_loader);
468     filter->pixbuf_loader = NULL;
469   }
470
471   gst_buffer_unmap (buf, &map);
472   gst_buffer_unref (buf);
473
474   return ret;
475
476   /* ERRORS */
477 error:
478   {
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;
485   }
486 }
487
488 static GstStateChangeReturn
489 gst_gdk_pixbuf_change_state (GstElement * element, GstStateChange transition)
490 {
491   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
492   GstGdkPixbuf *dec = GST_GDK_PIXBUF (element);
493
494   switch (transition) {
495     case GST_STATE_CHANGE_READY_TO_PAUSED:
496       /* default to single image mode, setcaps function might not be called */
497       dec->in_fps_n = 0;
498       dec->in_fps_d = 1;
499       gst_video_info_init (&dec->info);
500       break;
501     default:
502       break;
503   }
504
505   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
506   if (ret == GST_STATE_CHANGE_FAILURE)
507     return ret;
508
509   switch (transition) {
510     case GST_STATE_CHANGE_PAUSED_TO_READY:
511       dec->in_fps_n = 0;
512       dec->in_fps_d = 0;
513       if (dec->pool) {
514         gst_buffer_pool_set_active (dec->pool, FALSE);
515         gst_object_replace ((GstObject **) & dec->pool, NULL);
516       }
517       break;
518     default:
519       break;
520   }
521
522   return ret;
523 }
524
525 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
526
527 #ifdef enable_typefind
528 static void
529 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
530 {
531   guint8 *data;
532   GdkPixbufLoader *pixbuf_loader;
533   GdkPixbufFormat *format;
534
535   data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
536   if (data == NULL)
537     return;
538
539   GST_DEBUG ("creating new loader");
540
541   pixbuf_loader = gdk_pixbuf_loader_new ();
542
543   gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
544       NULL);
545
546   format = gdk_pixbuf_loader_get_format (pixbuf_loader);
547
548   if (format != NULL) {
549     GstCaps *caps;
550     gchar **p;
551     gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
552
553     for (p = mlist; *p; ++p) {
554       GST_DEBUG ("suggesting mime type %s", *p);
555       caps = gst_caps_new_simple (*p, NULL);
556       gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
557       gst_caps_free (caps);
558     }
559     g_strfreev (mlist);
560   }
561
562   GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
563   /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
564      to close a gzip that's not an svg; fixed upstream but no good way
565      to work around it */
566   gdk_pixbuf_loader_close (pixbuf_loader, NULL);
567   GST_DEBUG ("closed pixbuf loader");
568   g_object_unref (G_OBJECT (pixbuf_loader));
569 }
570 #endif
571
572 /* entry point to initialize the plug-in
573  * initialize the plug-in itself
574  * register the element factories and pad templates
575  * register the features
576  */
577 static gboolean
578 plugin_init (GstPlugin * plugin)
579 {
580   GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
581       "gdk pixbuf loader");
582
583   if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_SECONDARY,
584           GST_TYPE_GDK_PIXBUF))
585     return FALSE;
586
587 #ifdef enable_typefind
588   gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
589       gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
590 #endif
591
592   if (!gst_element_register (plugin, "gdkpixbufsink", GST_RANK_NONE,
593           GST_TYPE_GDK_PIXBUF_SINK))
594     return FALSE;
595
596   if (!pixbufscale_init (plugin))
597     return FALSE;
598
599   /* plugin initialisation succeeded */
600   return TRUE;
601 }
602
603
604 /* this is the structure that gst-register looks for
605  * so keep the name plugin_desc, or you cannot get your plug-in registered */
606 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
607     GST_VERSION_MINOR,
608     "gdkpixbuf",
609     "GdkPixbuf-based image decoder, scaler and sink",
610     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)