gst-indent
[platform/upstream/gst-plugins-good.git] / ext / gdk_pixbuf / gstgdkpixbuf.c
1 /*
2  * gstgdkpixbuf.c
3  * GStreamer
4  * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
5  * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <gst/gst.h>
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <gst/video/video.h>
29 #include <string.h>
30
31 #include "gstgdkpixbuf.h"
32
33 GST_DEBUG_CATEGORY_STATIC (gst_gdk_pixbuf_debug);
34 #define GST_CAT_DEFAULT gst_gdk_pixbuf_debug
35
36 static GstElementDetails plugin_details = {
37   "GdkPixbuf image decoder",
38   "Codec/Decoder/Image",
39   "Decodes images in a video stream using GdkPixbuf",
40   "David A. Schleef <ds@schleef.org>",
41 };
42
43 /* Filter signals and args */
44 enum
45 {
46   /* FILL ME */
47   LAST_SIGNAL
48 };
49
50 enum
51 {
52   ARG_0,
53   ARG_SILENT
54 };
55
56 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
57     GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("image/png; "
61         "image/jpeg; "
62         "image/gif; "
63         "image/x-icon; "
64         "application/x-navi-animation; "
65         "image/x-cmu-raster; "
66         "image/x-sun-raster; "
67         "image/x-pixmap; "
68         "image/tiff; "
69         "image/x-portable-anymap; "
70         "image/x-portable-bitmap; "
71         "image/x-portable-graymap; "
72         "image/x-portable-pixmap; "
73         "image/bmp; "
74         "image/x-bmp; "
75         "image/x-MS-bmp; "
76         "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga")
77     );
78
79 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
80 GST_STATIC_PAD_TEMPLATE ("src",
81     GST_PAD_SRC,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
84     );
85
86 static void gst_gdk_pixbuf_base_init (gpointer g_class);
87 static void gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass);
88 static void gst_gdk_pixbuf_init (GstGdkPixbuf * filter);
89
90 static void gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
91     const GValue * value, GParamSpec * pspec);
92 static void gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
93     GValue * value, GParamSpec * pspec);
94
95 static void gst_gdk_pixbuf_chain (GstPad * pad, GstData * _data);
96
97 #ifdef enable_typefind
98 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
99 #endif
100
101 static GstElementClass *parent_class = NULL;
102
103 static GstPadLinkReturn
104 gst_gdk_pixbuf_sink_link (GstPad * pad, const GstCaps * caps)
105 {
106   GstGdkPixbuf *filter;
107
108   filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
109   g_return_val_if_fail (filter != NULL, GST_PAD_LINK_REFUSED);
110   g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter), GST_PAD_LINK_REFUSED);
111
112   filter->framerate = 1.0;
113   gst_structure_get_double (gst_caps_get_structure (caps, 0), "framerate",
114       &filter->framerate);
115
116   return GST_PAD_LINK_OK;
117 }
118
119 #if GDK_PIXBUF_MAJOR == 2 && GDK_PIXBUF_MINOR < 2
120 /* gdk-pixbuf prior to 2.2 didn't have gdk_pixbuf_get_formats().
121  * These are just the formats that gdk-pixbuf is known to support.
122  * But maybe not -- it may have been compiled without an external
123  * library. */
124 static GstCaps *
125 gst_gdk_pixbuf_get_capslist (void)
126 {
127   return gst_caps_copy (gst_static_caps_get (&gst_gdk_pixbuf_sink_template.
128           static_caps));
129 }
130 #else
131 static GstCaps *
132 gst_gdk_pixbuf_get_capslist (void)
133 {
134   GSList *slist;
135   GSList *slist0;
136   GdkPixbufFormat *pixbuf_format;
137   char **mimetypes;
138   char **mimetype;
139   GstCaps *capslist = NULL;
140
141   capslist = gst_caps_new_empty ();
142   slist0 = gdk_pixbuf_get_formats ();
143
144   for (slist = slist0; slist; slist = g_slist_next (slist)) {
145     pixbuf_format = slist->data;
146     mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
147     for (mimetype = mimetypes; *mimetype; mimetype++) {
148       gst_caps_append_structure (capslist, gst_structure_new (*mimetype, NULL));
149     }
150     g_free (mimetypes);
151   }
152   g_slist_free (slist0);
153
154   return capslist;
155 }
156 #endif
157
158 static GstCaps *
159 gst_gdk_pixbuf_sink_getcaps (GstPad * pad)
160 {
161   GstGdkPixbuf *filter;
162
163   filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
164   g_return_val_if_fail (filter != NULL, NULL);
165   g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter), NULL);
166
167   return gst_gdk_pixbuf_get_capslist ();
168 }
169
170 GType
171 gst_gdk_pixbuf_get_type (void)
172 {
173   static GType plugin_type = 0;
174
175   if (!plugin_type) {
176     static const GTypeInfo plugin_info = {
177       sizeof (GstGdkPixbufClass),
178       gst_gdk_pixbuf_base_init,
179       NULL,
180       (GClassInitFunc) gst_gdk_pixbuf_class_init,
181       NULL,
182       NULL,
183       sizeof (GstGdkPixbuf),
184       0,
185       (GInstanceInitFunc) gst_gdk_pixbuf_init,
186     };
187     plugin_type = g_type_register_static (GST_TYPE_ELEMENT,
188         "GstGdkPixbuf", &plugin_info, 0);
189   }
190   return plugin_type;
191 }
192
193 static void
194 gst_gdk_pixbuf_base_init (gpointer g_class)
195 {
196   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
197
198   gst_element_class_add_pad_template (element_class,
199       gst_static_pad_template_get (&gst_gdk_pixbuf_src_template));
200   gst_element_class_add_pad_template (element_class,
201       gst_static_pad_template_get (&gst_gdk_pixbuf_sink_template));
202   gst_element_class_set_details (element_class, &plugin_details);
203 }
204
205 /* initialize the plugin's class */
206 static void
207 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
208 {
209   GObjectClass *gobject_class;
210   GstElementClass *gstelement_class;
211
212   gobject_class = (GObjectClass *) klass;
213   gstelement_class = (GstElementClass *) klass;
214
215   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
216
217   g_object_class_install_property (gobject_class, ARG_SILENT,
218       g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
219           FALSE, G_PARAM_READWRITE));
220
221   gobject_class->set_property = gst_gdk_pixbuf_set_property;
222   gobject_class->get_property = gst_gdk_pixbuf_get_property;
223 }
224
225 static void
226 gst_gdk_pixbuf_init (GstGdkPixbuf * filter)
227 {
228   filter->sinkpad =
229       gst_pad_new_from_template (gst_static_pad_template_get
230       (&gst_gdk_pixbuf_sink_template), "sink");
231   gst_pad_set_link_function (filter->sinkpad, gst_gdk_pixbuf_sink_link);
232   gst_pad_set_getcaps_function (filter->sinkpad, gst_gdk_pixbuf_sink_getcaps);
233
234   filter->srcpad =
235       gst_pad_new_from_template (gst_static_pad_template_get
236       (&gst_gdk_pixbuf_src_template), "src");
237   gst_pad_use_explicit_caps (filter->srcpad);
238
239   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
240   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
241   gst_pad_set_chain_function (filter->sinkpad, gst_gdk_pixbuf_chain);
242
243   GST_FLAG_SET (GST_ELEMENT (filter), GST_ELEMENT_EVENT_AWARE);
244 }
245
246 static void
247 gst_gdk_pixbuf_chain (GstPad * pad, GstData * _data)
248 {
249   GstBuffer *buf = GST_BUFFER (_data);
250   GstGdkPixbuf *filter;
251   GError *error = NULL;
252   gboolean push_buffer = FALSE;
253   gboolean dump_buffer = FALSE;
254   gboolean got_eos = FALSE;
255
256   GST_DEBUG ("gst_gdk_pixbuf_chain");
257
258   g_return_if_fail (GST_IS_PAD (pad));
259   g_return_if_fail (buf != NULL);
260
261   filter = GST_GDK_PIXBUF (GST_OBJECT_PARENT (pad));
262   g_return_if_fail (GST_IS_GDK_PIXBUF (filter));
263
264   if (GST_IS_EVENT (_data)) {
265     GstEvent *event = GST_EVENT (buf);
266
267     switch (GST_EVENT_TYPE (event)) {
268       case GST_EVENT_EOS:
269         push_buffer = TRUE;
270         got_eos = TRUE;
271         break;
272       case GST_EVENT_DISCONTINUOUS:
273         dump_buffer = TRUE;
274         break;
275       default:
276         gst_pad_event_default (pad, event);
277         return;
278     }
279   }
280
281   if (filter->last_timestamp != GST_BUFFER_TIMESTAMP (buf)) {
282     push_buffer = TRUE;
283   }
284
285   if (push_buffer) {
286     if (filter->pixbuf_loader != NULL) {
287       GstBuffer *outbuf;
288       GdkPixbuf *pixbuf;
289       GError *error = NULL;
290
291       if (!gdk_pixbuf_loader_close (filter->pixbuf_loader, &error)) {
292         GST_ELEMENT_ERROR (filter, LIBRARY, SHUTDOWN, (NULL), (error->message));
293         g_error_free (error);
294         return;
295       }
296
297       pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
298
299       if (filter->image_size == 0) {
300         GstCaps *caps;
301
302         filter->width = gdk_pixbuf_get_width (pixbuf);
303         filter->height = gdk_pixbuf_get_height (pixbuf);
304         filter->rowstride = gdk_pixbuf_get_rowstride (pixbuf);
305         filter->image_size = filter->rowstride * filter->height;
306
307         caps = gst_caps_copy (gst_pad_get_pad_template_caps (filter->srcpad));
308         gst_caps_set_simple (caps,
309             "width", G_TYPE_INT, filter->width,
310             "height", G_TYPE_INT, filter->height,
311             "framerate", G_TYPE_DOUBLE, filter->framerate, NULL);
312
313         gst_pad_set_explicit_caps (filter->srcpad, caps);
314       }
315
316       outbuf = gst_pad_alloc_buffer (filter->srcpad, GST_BUFFER_OFFSET_NONE,
317           filter->image_size);
318       GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
319       GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
320
321       memcpy (GST_BUFFER_DATA (outbuf), gdk_pixbuf_get_pixels (pixbuf),
322           filter->image_size);
323
324       gst_pad_push (filter->srcpad, GST_DATA (outbuf));
325
326       g_object_unref (G_OBJECT (filter->pixbuf_loader));
327       filter->pixbuf_loader = NULL;
328       dump_buffer = FALSE;
329     }
330   }
331
332   if (dump_buffer) {
333     if (filter->pixbuf_loader != NULL) {
334       gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
335       g_object_unref (G_OBJECT (filter->pixbuf_loader));
336       filter->pixbuf_loader = NULL;
337     }
338   }
339
340   if (GST_IS_BUFFER (_data)) {
341     if (filter->pixbuf_loader == NULL) {
342       filter->pixbuf_loader = gdk_pixbuf_loader_new ();
343       filter->last_timestamp = GST_BUFFER_TIMESTAMP (buf);
344     }
345
346     gdk_pixbuf_loader_write (filter->pixbuf_loader, GST_BUFFER_DATA (buf),
347         GST_BUFFER_SIZE (buf), &error);
348     gst_buffer_unref (buf);
349   }
350
351   if (got_eos) {
352     gst_pad_event_default (pad, GST_EVENT (_data));
353   }
354 }
355
356 static void
357 gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
358     const GValue * value, GParamSpec * pspec)
359 {
360   GstGdkPixbuf *filter;
361
362   g_return_if_fail (GST_IS_GDK_PIXBUF (object));
363   filter = GST_GDK_PIXBUF (object);
364
365   switch (prop_id) {
366     case ARG_SILENT:
367       //filter->silent = g_value_get_boolean (value);
368       break;
369     default:
370       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371       break;
372   }
373 }
374
375 static void
376 gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
377     GValue * value, GParamSpec * pspec)
378 {
379   GstGdkPixbuf *filter;
380
381   g_return_if_fail (GST_IS_GDK_PIXBUF (object));
382   filter = GST_GDK_PIXBUF (object);
383
384   switch (prop_id) {
385     case ARG_SILENT:
386       //g_value_set_boolean (value, filter->silent);
387       break;
388     default:
389       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
390       break;
391   }
392 }
393
394 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
395
396 #ifdef enable_typefind
397 static void
398 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
399 {
400   guint8 *data;
401   GdkPixbufLoader *pixbuf_loader;
402   GdkPixbufFormat *format;
403
404   data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
405   if (data == NULL)
406     return;
407
408   GST_DEBUG ("creating new loader");
409
410   pixbuf_loader = gdk_pixbuf_loader_new ();
411
412   gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
413       NULL);
414
415   format = gdk_pixbuf_loader_get_format (pixbuf_loader);
416
417   if (format != NULL) {
418     GstCaps *caps;
419     gchar **p;
420     gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
421
422     for (p = mlist; *p; ++p) {
423       GST_DEBUG ("suggesting mime type %s", *p);
424       caps = gst_caps_new_simple (*p, NULL);
425       gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
426       gst_caps_free (caps);
427     }
428     g_strfreev (mlist);
429   }
430
431   GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
432   /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
433      to close a gzip that's not an svg; fixed upstream but no good way
434      to work around it */
435   gdk_pixbuf_loader_close (pixbuf_loader, NULL);
436   GST_DEBUG ("closed pixbuf loader");
437   g_object_unref (G_OBJECT (pixbuf_loader));
438 }
439 #endif
440
441 /* entry point to initialize the plug-in
442  * initialize the plug-in itself
443  * register the element factories and pad templates
444  * register the features
445  */
446 static gboolean
447 plugin_init (GstPlugin * plugin)
448 {
449   GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
450       "gdk pixbuf loader");
451
452   if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_NONE,
453           GST_TYPE_GDK_PIXBUF))
454     return FALSE;
455
456 #ifdef enable_typefind
457   gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
458       gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
459 #endif
460
461   /* plugin initialisation succeeded */
462   return TRUE;
463 }
464
465 /* this is the structure that gst-register looks for
466  * so keep the name plugin_desc, or you cannot get your plug-in registered */
467 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
468     GST_VERSION_MINOR,
469     "gdkpixbuf",
470     "GDK Pixbuf decoder", plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)