99d937bd86ea8e1072f386880296317ddaabdffc
[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   /* FILL ME */
46   LAST_SIGNAL
47 };
48
49 enum {
50   ARG_0,
51   ARG_SILENT
52 };
53
54 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
55 GST_STATIC_PAD_TEMPLATE (
56   "sink",
57   GST_PAD_SINK,
58   GST_PAD_ALWAYS,
59   GST_STATIC_CAPS (
60     "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; "
77     "image/x-bitmap; "
78     "image/x-tga")
79 );
80
81 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
82 GST_STATIC_PAD_TEMPLATE (
83   "src",
84   GST_PAD_SRC,
85   GST_PAD_ALWAYS,
86   GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
87 );
88
89 static void     gst_gdk_pixbuf_base_init (gpointer g_class);
90 static void     gst_gdk_pixbuf_class_init       (GstGdkPixbufClass *klass);
91 static void     gst_gdk_pixbuf_init     (GstGdkPixbuf *filter);
92
93 static void     gst_gdk_pixbuf_set_property(GObject *object, guint prop_id,
94                                                  const GValue *value,
95                                                  GParamSpec *pspec);
96 static void     gst_gdk_pixbuf_get_property(GObject *object, guint prop_id,
97                                                  GValue *value,
98                                                  GParamSpec *pspec);
99
100 static void     gst_gdk_pixbuf_chain    (GstPad *pad, GstData *_data);
101 #ifdef enable_typefind
102 static void     gst_gdk_pixbuf_type_find (GstTypeFind *tf, gpointer ignore);
103 #endif
104
105 static GstElementClass *parent_class = NULL;
106
107 static GstPadLinkReturn
108 gst_gdk_pixbuf_sink_link (GstPad *pad, const GstCaps *caps)
109 {
110   GstGdkPixbuf *filter;
111
112   filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
113   g_return_val_if_fail (filter != NULL, GST_PAD_LINK_REFUSED);
114   g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter),
115                         GST_PAD_LINK_REFUSED);
116
117   filter->framerate = 1.0;
118   gst_structure_get_double (gst_caps_get_structure (caps, 0), "framerate",
119       &filter->framerate);
120
121   return GST_PAD_LINK_OK;
122 }
123
124 #if GDK_PIXBUF_MAJOR == 2 && GDK_PIXBUF_MINOR < 2
125 /* gdk-pixbuf prior to 2.2 didn't have gdk_pixbuf_get_formats().
126  * These are just the formats that gdk-pixbuf is known to support.
127  * But maybe not -- it may have been compiled without an external
128  * library. */
129 static GstCaps *gst_gdk_pixbuf_get_capslist(void)
130 {
131   return gst_caps_copy (gst_static_caps_get (
132         &gst_gdk_pixbuf_sink_template.static_caps));
133 }
134 #else
135 static GstCaps *gst_gdk_pixbuf_get_capslist(void)
136 {
137   GSList *slist;
138   GSList *slist0;
139   GdkPixbufFormat *pixbuf_format;
140   char **mimetypes;
141   char **mimetype;
142   GstCaps *capslist = NULL;
143
144   capslist = gst_caps_new_empty ();
145   slist0 = gdk_pixbuf_get_formats();
146
147   for(slist = slist0;slist;slist=g_slist_next(slist)){
148     pixbuf_format = slist->data;
149     mimetypes = gdk_pixbuf_format_get_mime_types(pixbuf_format);
150     for(mimetype = mimetypes; *mimetype; mimetype++){
151       gst_caps_append_structure (capslist,
152           gst_structure_new (*mimetype,NULL));
153     }
154     g_free(mimetypes);
155   }
156   g_slist_free(slist0);
157
158   return capslist;
159 }
160 #endif
161
162 static GstCaps *gst_gdk_pixbuf_sink_getcaps(GstPad *pad)
163 {
164   GstGdkPixbuf *filter;
165
166   filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
167   g_return_val_if_fail (filter != NULL, NULL);
168   g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter), NULL);
169
170   return gst_gdk_pixbuf_get_capslist();
171 }
172
173 GType
174 gst_gdk_pixbuf_get_type (void)
175 {
176   static GType plugin_type = 0;
177
178   if (!plugin_type)
179   {
180     static const GTypeInfo plugin_info =
181     {
182       sizeof (GstGdkPixbufClass),
183       gst_gdk_pixbuf_base_init,
184       NULL,
185       (GClassInitFunc) gst_gdk_pixbuf_class_init,
186       NULL,
187       NULL,
188       sizeof (GstGdkPixbuf),
189       0,
190       (GInstanceInitFunc) gst_gdk_pixbuf_init,
191     };
192     plugin_type = g_type_register_static (GST_TYPE_ELEMENT,
193                                           "GstGdkPixbuf",
194                                           &plugin_info, 0);
195   }
196   return plugin_type;
197 }
198
199 static void
200 gst_gdk_pixbuf_base_init (gpointer g_class)
201 {
202   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
203
204   gst_element_class_add_pad_template (element_class,
205       gst_static_pad_template_get( &gst_gdk_pixbuf_src_template));
206   gst_element_class_add_pad_template (element_class,
207       gst_static_pad_template_get( &gst_gdk_pixbuf_sink_template));
208   gst_element_class_set_details (element_class, &plugin_details);
209 }
210
211 /* initialize the plugin's class */
212 static void
213 gst_gdk_pixbuf_class_init (GstGdkPixbufClass *klass)
214 {
215   GObjectClass *gobject_class;
216   GstElementClass *gstelement_class;
217
218   gobject_class = (GObjectClass*) klass;
219   gstelement_class = (GstElementClass*) klass;
220
221   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
222
223   g_object_class_install_property (gobject_class, ARG_SILENT,
224     g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
225                           FALSE, G_PARAM_READWRITE));
226
227   gobject_class->set_property = gst_gdk_pixbuf_set_property;
228   gobject_class->get_property = gst_gdk_pixbuf_get_property;
229 }
230
231 static void
232 gst_gdk_pixbuf_init (GstGdkPixbuf *filter)
233 {
234   filter->sinkpad = gst_pad_new_from_template (
235       gst_static_pad_template_get( &gst_gdk_pixbuf_sink_template), "sink");
236   gst_pad_set_link_function (filter->sinkpad, gst_gdk_pixbuf_sink_link);
237   gst_pad_set_getcaps_function (filter->sinkpad, gst_gdk_pixbuf_sink_getcaps);
238
239   filter->srcpad = gst_pad_new_from_template (
240       gst_static_pad_template_get( &gst_gdk_pixbuf_src_template), "src");
241   gst_pad_use_explicit_caps (filter->srcpad);
242
243   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
244   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
245   gst_pad_set_chain_function (filter->sinkpad, gst_gdk_pixbuf_chain);
246
247   GST_FLAG_SET (GST_ELEMENT (filter), GST_ELEMENT_EVENT_AWARE);
248 }
249
250 static void
251 gst_gdk_pixbuf_chain (GstPad *pad, GstData *_data)
252 {
253   GstBuffer *buf = GST_BUFFER (_data);
254   GstGdkPixbuf *filter;
255   GError *error = NULL;
256   gboolean push_buffer = FALSE;
257   gboolean dump_buffer = FALSE;
258   gboolean got_eos = FALSE;
259
260   GST_DEBUG ("gst_gdk_pixbuf_chain");
261
262   g_return_if_fail (GST_IS_PAD (pad));
263   g_return_if_fail (buf != NULL);
264
265   filter = GST_GDK_PIXBUF (GST_OBJECT_PARENT (pad));
266   g_return_if_fail (GST_IS_GDK_PIXBUF (filter));
267
268   if (GST_IS_EVENT (_data)) {
269     GstEvent *event = GST_EVENT (buf);
270
271     switch (GST_EVENT_TYPE (event)) {
272       case GST_EVENT_EOS:
273         push_buffer = TRUE;
274         got_eos = TRUE;
275         break;
276       case GST_EVENT_DISCONTINUOUS:
277         dump_buffer = TRUE;
278         break;
279       default:
280         gst_pad_event_default (pad, event);
281         return;
282     }
283   }
284
285   if (filter->last_timestamp != GST_BUFFER_TIMESTAMP (buf)) {
286     push_buffer = TRUE;
287   }
288
289   if (push_buffer) {
290     if (filter->pixbuf_loader != NULL) {
291       GstBuffer *outbuf;
292       GdkPixbuf *pixbuf;
293       GError *error = NULL;
294
295       if (!gdk_pixbuf_loader_close (filter->pixbuf_loader, &error)) {
296         GST_ELEMENT_ERROR (filter, LIBRARY, SHUTDOWN, (NULL), (error->message));
297         g_error_free (error);
298         return;
299       }
300
301       pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
302
303       if(filter->image_size == 0){
304         GstCaps *caps;
305
306         filter->width = gdk_pixbuf_get_width(pixbuf);
307         filter->height = gdk_pixbuf_get_height(pixbuf);
308         filter->rowstride = gdk_pixbuf_get_rowstride(pixbuf);
309         filter->image_size = filter->rowstride * filter->height;
310
311         caps = gst_caps_copy (gst_pad_get_pad_template_caps (filter->srcpad));
312         gst_caps_set_simple (caps,
313             "width", G_TYPE_INT, filter->width,
314             "height", G_TYPE_INT, filter->height,
315             "framerate", G_TYPE_DOUBLE, filter->framerate, NULL);
316
317         gst_pad_set_explicit_caps (filter->srcpad, caps);
318       }
319
320       outbuf = gst_pad_alloc_buffer (filter->srcpad, GST_BUFFER_OFFSET_NONE,
321           filter->image_size);
322       GST_BUFFER_TIMESTAMP(outbuf) = GST_BUFFER_TIMESTAMP(buf);
323       GST_BUFFER_DURATION(outbuf) = GST_BUFFER_DURATION(buf);
324       
325       memcpy(GST_BUFFER_DATA(outbuf), gdk_pixbuf_get_pixels(pixbuf),
326           filter->image_size);
327
328       gst_pad_push (filter->srcpad, GST_DATA (outbuf));
329
330       g_object_unref(G_OBJECT(filter->pixbuf_loader));
331       filter->pixbuf_loader = NULL;
332       dump_buffer = FALSE;
333     }
334   }
335
336   if (dump_buffer) {
337     if (filter->pixbuf_loader != NULL) {
338       gdk_pixbuf_loader_close (filter->pixbuf_loader, NULL);
339       g_object_unref(G_OBJECT(filter->pixbuf_loader));
340       filter->pixbuf_loader = NULL;
341     }
342   }
343
344   if (GST_IS_BUFFER (_data)) {
345     if (filter->pixbuf_loader == NULL) {
346       filter->pixbuf_loader = gdk_pixbuf_loader_new();
347       filter->last_timestamp = GST_BUFFER_TIMESTAMP (buf);
348     }
349     
350     gdk_pixbuf_loader_write(filter->pixbuf_loader, GST_BUFFER_DATA(buf),
351         GST_BUFFER_SIZE(buf), &error);
352     gst_buffer_unref (buf);
353   }
354
355   if (got_eos) {
356     gst_pad_event_default (pad, GST_EVENT (_data));
357   }
358 }
359
360 static void
361 gst_gdk_pixbuf_set_property (GObject *object, guint prop_id,
362                                   const GValue *value, GParamSpec *pspec)
363 {
364   GstGdkPixbuf *filter;
365
366   g_return_if_fail (GST_IS_GDK_PIXBUF (object));
367   filter = GST_GDK_PIXBUF (object);
368
369   switch (prop_id)
370   {
371   case ARG_SILENT:
372     //filter->silent = g_value_get_boolean (value);
373     break;
374   default:
375     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376     break;
377   }
378 }
379
380 static void
381 gst_gdk_pixbuf_get_property (GObject *object, guint prop_id,
382                                   GValue *value, GParamSpec *pspec)
383 {
384   GstGdkPixbuf *filter;
385
386   g_return_if_fail (GST_IS_GDK_PIXBUF (object));
387   filter = GST_GDK_PIXBUF (object);
388
389   switch (prop_id) {
390   case ARG_SILENT:
391     //g_value_set_boolean (value, filter->silent);
392     break;
393   default:
394     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
395     break;
396   }
397 }
398
399 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
400
401 #ifdef enable_typefind
402 static void
403 gst_gdk_pixbuf_type_find (GstTypeFind *tf, gpointer ignore)
404 {
405   guint8 *data;
406   GdkPixbufLoader *pixbuf_loader;
407   GdkPixbufFormat *format;
408
409   data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
410   if (data == NULL) return;
411
412   GST_DEBUG ("creating new loader");
413
414   pixbuf_loader = gdk_pixbuf_loader_new();
415   
416   gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
417       NULL);
418   
419   format = gdk_pixbuf_loader_get_format (pixbuf_loader);
420
421   if (format != NULL) {
422     GstCaps *caps;
423     gchar **p;
424     gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
425
426     for (p = mlist; *p; ++p)
427     {
428       GST_DEBUG ("suggesting mime type %s", *p);
429       caps = gst_caps_new_simple (*p, NULL);
430       gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
431       gst_caps_free (caps);
432     }
433     g_strfreev (mlist);
434   }
435
436   GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
437   /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
438      to close a gzip that's not an svg; fixed upstream but no good way
439      to work around it */
440   gdk_pixbuf_loader_close (pixbuf_loader, NULL);
441   GST_DEBUG ("closed pixbuf loader");
442   g_object_unref (G_OBJECT (pixbuf_loader));
443 }
444 #endif
445
446 /* entry point to initialize the plug-in
447  * initialize the plug-in itself
448  * register the element factories and pad templates
449  * register the features
450  */
451 static gboolean
452 plugin_init (GstPlugin *plugin)
453 {
454   GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0, "gdk pixbuf loader");
455
456   if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_NONE, GST_TYPE_GDK_PIXBUF))
457     return FALSE;
458
459 #ifdef enable_typefind
460   gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
461                           gst_gdk_pixbuf_type_find, NULL,
462                           GST_CAPS_ANY, NULL);
463 #endif
464
465   /* plugin initialisation succeeded */
466   return TRUE;
467 }
468
469 /* this is the structure that gst-register looks for
470  * so keep the name plugin_desc, or you cannot get your plug-in registered */
471 GST_PLUGIN_DEFINE (
472   GST_VERSION_MAJOR,
473   GST_VERSION_MINOR,
474   "gdkpixbuf",
475   "GDK Pixbuf decoder",
476   plugin_init,
477   VERSION,
478   "LGPL",
479   GST_PACKAGE,
480   GST_ORIGIN)