close #333784 unref the result of gst_pad_get_parent() by: Christophe Fergeau.
[platform/upstream/gstreamer.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>, Renato Filho <renato.filho@indt.org.br>",
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
57
58 static GstStaticPadTemplate gst_gdk_pixbuf_sink_template =
59     GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("image/png; "
63         "image/jpeg; "
64         "image/gif; "
65         "image/x-icon; "
66         "application/x-navi-animation; "
67         "image/x-cmu-raster; "
68         "image/x-sun-raster; "
69         "image/x-pixmap; "
70         "image/tiff; "
71         "image/x-portable-anymap; "
72         "image/x-portable-bitmap; "
73         "image/x-portable-graymap; "
74         "image/x-portable-pixmap; "
75         "image/bmp; "
76         "image/x-bmp; "
77         "image/x-MS-bmp; "
78         "image/vnd.wap.wbmp; " "image/x-bitmap; " "image/x-tga; "
79         "image/x-pcx; image/svg; image/svg+xml")
80     );
81
82 static GstStaticPadTemplate gst_gdk_pixbuf_src_template =
83 GST_STATIC_PAD_TEMPLATE ("src",
84     GST_PAD_SRC,
85     GST_PAD_ALWAYS,
86     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
87     );
88
89 gboolean pixbufscale_init (GstPlugin * plugin);
90
91 static void gst_gdk_pixbuf_base_init (gpointer g_class);
92 static void gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass);
93 static void gst_gdk_pixbuf_init (GstGdkPixbuf * filter,
94     GstGdkPixbufClass * klass);
95
96 static void gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
97     const GValue * value, GParamSpec * pspec);
98 static void gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
99     GValue * value, GParamSpec * pspec);
100
101 static GstFlowReturn gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buffer);
102 static gboolean gst_gdk_pixbuf_event (GstPad * pad, GstEvent * event);
103
104 #ifdef enable_typefind
105 static void gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore);
106 #endif
107
108
109 GST_BOILERPLATE (GstGdkPixbuf, gst_gdk_pixbuf, GstElement, GST_TYPE_ELEMENT)
110
111      static gboolean gst_gdk_pixbuf_sink_setcaps (GstPad * pad, GstCaps * caps)
112 {
113   GstGdkPixbuf *filter;
114   const GValue *framerate;
115   GstStructure *s;
116
117   filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
118   s = gst_caps_get_structure (caps, 0);
119
120   filter->framerate_numerator = 0;
121   filter->framerate_denominator = 1;
122   if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
123     filter->framerate_numerator = gst_value_get_fraction_numerator (framerate);
124     filter->framerate_denominator =
125         gst_value_get_fraction_denominator (framerate);
126     GST_DEBUG ("got framerate of %d/%d fps => packetized mode",
127         filter->framerate_numerator, filter->framerate_denominator);
128   }
129   gst_object_unref (filter);
130   return TRUE;
131 }
132
133
134 #if GDK_PIXBUF_MAJOR == 2 && GDK_PIXBUF_MINOR < 2
135 /* gdk-pixbuf prior to 2.2 didn't have gdk_pixbuf_get_formats().
136  * These are just the formats that gdk-pixbuf is known to support.
137  * But maybe not -- it may have been compiled without an external
138  * library. */
139
140 static GstCaps *
141 gst_gdk_pixbuf_get_capslist (void)
142 {
143   return gst_caps_copy (gst_static_caps_get (&gst_gdk_pixbuf_sink_template.
144           static_caps));
145 }
146 #else
147 static GstCaps *
148 gst_gdk_pixbuf_get_capslist (void)
149 {
150   GSList *slist;
151   GSList *slist0;
152   GdkPixbufFormat *pixbuf_format;
153   char **mimetypes;
154   char **mimetype;
155   GstCaps *capslist = NULL;
156   GstCaps *return_caps = NULL;
157
158   capslist = gst_caps_new_empty ();
159   slist0 = gdk_pixbuf_get_formats ();
160
161   for (slist = slist0; slist; slist = g_slist_next (slist)) {
162     pixbuf_format = slist->data;
163     mimetypes = gdk_pixbuf_format_get_mime_types (pixbuf_format);
164     for (mimetype = mimetypes; *mimetype; mimetype++) {
165       gst_caps_append_structure (capslist, gst_structure_new (*mimetype, NULL));
166     }
167     g_free (mimetypes);
168   }
169   g_slist_free (slist0);
170
171   return_caps = gst_caps_intersect (capslist,
172       gst_static_caps_get (&gst_gdk_pixbuf_sink_template.static_caps));
173
174   gst_caps_unref (capslist);
175   return return_caps;
176 }
177 #endif
178
179 static GstCaps *
180 gst_gdk_pixbuf_sink_getcaps (GstPad * pad)
181 {
182   GstGdkPixbuf *filter;
183
184   filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
185   g_return_val_if_fail (filter != NULL, NULL);
186   g_return_val_if_fail (GST_IS_GDK_PIXBUF (filter), NULL);
187   gst_object_unref (filter);
188
189   return gst_gdk_pixbuf_get_capslist ();
190 }
191
192 static void
193 gst_gdk_pixbuf_base_init (gpointer g_class)
194 {
195   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
196
197   gst_element_class_add_pad_template (element_class,
198       gst_static_pad_template_get (&gst_gdk_pixbuf_src_template));
199   gst_element_class_add_pad_template (element_class,
200       gst_static_pad_template_get (&gst_gdk_pixbuf_sink_template));
201   gst_element_class_set_details (element_class, &plugin_details);
202 }
203
204 /* initialize the plugin's class */
205 static void
206 gst_gdk_pixbuf_class_init (GstGdkPixbufClass * klass)
207 {
208   GObjectClass *gobject_class;
209   GstElementClass *gstelement_class;
210
211   gobject_class = (GObjectClass *) klass;
212   gstelement_class = (GstElementClass *) klass;
213
214   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
215
216   gobject_class->set_property = gst_gdk_pixbuf_set_property;
217   gobject_class->get_property = gst_gdk_pixbuf_get_property;
218
219
220   g_object_class_install_property (gobject_class, ARG_SILENT,
221       g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
222           FALSE, G_PARAM_READWRITE));
223
224 }
225
226 static void
227 gst_gdk_pixbuf_init (GstGdkPixbuf * filter, GstGdkPixbufClass * klass)
228 {
229   filter->sinkpad =
230       gst_pad_new_from_template (gst_static_pad_template_get
231       (&gst_gdk_pixbuf_sink_template), "sink");
232   gst_pad_set_setcaps_function (filter->sinkpad, gst_gdk_pixbuf_sink_setcaps);
233   gst_pad_set_getcaps_function (filter->sinkpad, gst_gdk_pixbuf_sink_getcaps);
234
235   gst_pad_set_chain_function (filter->sinkpad,
236       (GstPadChainFunction) gst_gdk_pixbuf_chain);
237
238   gst_pad_set_event_function (filter->sinkpad, gst_gdk_pixbuf_event);
239
240
241   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
242
243
244   filter->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
245       (&gst_gdk_pixbuf_src_template), "src");
246
247   gst_pad_use_fixed_caps (filter->srcpad);
248   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
249
250   filter->last_timestamp = GST_CLOCK_TIME_NONE;
251   filter->pixbuf_loader = NULL;
252 }
253
254 static GstFlowReturn
255 gst_gdk_pixbuf_flush (GstGdkPixbuf * filter)
256 {
257   GstBuffer *outbuf;
258   GdkPixbuf *pixbuf;
259   int y;
260   guint8 *out_pix;
261   guint8 *in_pix;
262   int in_rowstride;
263   GstFlowReturn ret;
264   GstCaps *caps = NULL;
265
266   pixbuf = gdk_pixbuf_loader_get_pixbuf (filter->pixbuf_loader);
267   if (pixbuf == NULL) {
268     GST_DEBUG ("error geting pixbuf");
269     return GST_FLOW_ERROR;
270   }
271
272   if (filter->image_size == 0) {
273     filter->width = gdk_pixbuf_get_width (pixbuf);
274     filter->height = gdk_pixbuf_get_height (pixbuf);
275     filter->rowstride = gdk_pixbuf_get_rowstride (pixbuf);
276     filter->image_size = filter->rowstride * filter->height;
277
278     if (gdk_pixbuf_get_rowstride (pixbuf) / filter->width == 4) {
279       caps = gst_caps_from_string (GST_VIDEO_CAPS_RGBA);
280     } else if (gdk_pixbuf_get_rowstride (pixbuf) / filter->width == 3) {
281       caps = gst_caps_from_string (GST_VIDEO_CAPS_RGB);
282     } else {
283       GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, (NULL),
284           ("Bpp %d not supported", gdk_pixbuf_get_bits_per_sample (pixbuf)));
285       return GST_FLOW_ERROR;
286     }
287     gst_caps_set_simple (caps,
288         "width", G_TYPE_INT, filter->width,
289         "height", G_TYPE_INT, filter->height,
290         "framerate", GST_TYPE_FRACTION, filter->framerate_numerator,
291         filter->framerate_denominator, NULL);
292
293     GST_DEBUG ("Set size to %dx%d", filter->width, filter->height);
294     gst_pad_set_caps (filter->srcpad, caps);
295     gst_caps_unref (caps);
296   }
297
298   ret = gst_pad_alloc_buffer_and_set_caps (filter->srcpad,
299       GST_BUFFER_OFFSET_NONE,
300       filter->image_size, GST_PAD_CAPS (filter->srcpad), &outbuf);
301
302   if (ret != GST_FLOW_OK) {
303     GST_DEBUG ("Failed to create outbuffer - %d", ret);
304     return GST_FLOW_ERROR;
305   }
306
307   gst_caps_unref (caps);
308   caps = gst_pad_get_negotiated_caps (filter->srcpad);
309   GST_DEBUG ("Caps negotiated %s", gst_caps_to_string (caps));
310   gst_caps_unref (caps);
311
312   GST_BUFFER_TIMESTAMP (outbuf) = filter->last_timestamp;
313   GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
314
315   in_pix = gdk_pixbuf_get_pixels (pixbuf);
316   in_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
317   out_pix = GST_BUFFER_DATA (outbuf);
318
319   for (y = 0; y < filter->height; y++) {
320     memcpy (out_pix, in_pix, filter->rowstride);
321     in_pix += in_rowstride;
322     out_pix += filter->rowstride;
323   }
324
325   GST_DEBUG ("pushing... %d bytes", GST_BUFFER_SIZE (outbuf));
326   return gst_pad_push (filter->srcpad, outbuf);
327 }
328
329 static gboolean
330 gst_gdk_pixbuf_event (GstPad * pad, GstEvent * event)
331 {
332   GstFlowReturn res = GST_FLOW_OK;
333   gboolean ret = TRUE;
334   GstGdkPixbuf *pixbuf;
335
336   pixbuf = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
337
338   switch (GST_EVENT_TYPE (event)) {
339     case GST_EVENT_EOS:
340       gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
341       res = gst_gdk_pixbuf_flush (pixbuf);
342       g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
343       break;
344     case GST_EVENT_NEWSEGMENT:
345     case GST_EVENT_FLUSH_STOP:
346       if (pixbuf->pixbuf_loader != NULL) {
347         gdk_pixbuf_loader_close (pixbuf->pixbuf_loader, NULL);
348         g_object_unref (G_OBJECT (pixbuf->pixbuf_loader));
349         pixbuf->pixbuf_loader = NULL;
350       }
351       break;
352     default:
353       break;
354   }
355
356   if (res == GST_FLOW_OK) {
357     ret = gst_pad_event_default (pad, event);
358   } else {
359     ret = FALSE;
360   }
361
362   gst_object_unref (pixbuf);
363
364   return ret;
365 }
366
367 static GstFlowReturn
368 gst_gdk_pixbuf_chain (GstPad * pad, GstBuffer * buf)
369 {
370   GstGdkPixbuf *filter;
371   GstFlowReturn ret = GST_FLOW_OK;
372   GError *error = NULL;
373   GstClockTime timestamp;
374
375   GST_DEBUG ("gst_gdk_pixbuf_chain");
376   filter = GST_GDK_PIXBUF (gst_pad_get_parent (pad));
377
378   timestamp = GST_BUFFER_TIMESTAMP (buf);
379
380   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
381     filter->last_timestamp = timestamp;
382   }
383
384   if (filter->pixbuf_loader == NULL) {
385     filter->pixbuf_loader = gdk_pixbuf_loader_new ();
386   }
387
388   GST_DEBUG ("Writing buffer size %d", GST_BUFFER_SIZE (buf));
389   if (gdk_pixbuf_loader_write (filter->pixbuf_loader, GST_BUFFER_DATA (buf),
390           GST_BUFFER_SIZE (buf), &error) == FALSE) {
391     GST_DEBUG ("gst_gdk_pixbuf_chain ERROR: %s", error->message);
392     ret = GST_FLOW_ERROR;
393     goto need_more_data;
394   }
395
396 need_more_data:
397   gst_object_unref (filter);
398   return ret;
399 }
400
401 static void
402 gst_gdk_pixbuf_set_property (GObject * object, guint prop_id,
403     const GValue * value, GParamSpec * pspec)
404 {
405   GstGdkPixbuf *filter;
406
407   g_return_if_fail (GST_IS_GDK_PIXBUF (object));
408   filter = GST_GDK_PIXBUF (object);
409
410   switch (prop_id) {
411     case ARG_SILENT:
412       /* filter->silent = g_value_get_boolean (value); */
413       break;
414     default:
415       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
416       break;
417   }
418 }
419
420 static void
421 gst_gdk_pixbuf_get_property (GObject * object, guint prop_id,
422     GValue * value, GParamSpec * pspec)
423 {
424   GstGdkPixbuf *filter;
425
426   g_return_if_fail (GST_IS_GDK_PIXBUF (object));
427   filter = GST_GDK_PIXBUF (object);
428
429   switch (prop_id) {
430     case ARG_SILENT:
431       /* g_value_set_boolean (value, filter->silent); */
432       break;
433     default:
434       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435       break;
436   }
437 }
438
439 #define GST_GDK_PIXBUF_TYPE_FIND_SIZE 1024
440
441 #ifdef enable_typefind
442 static void
443 gst_gdk_pixbuf_type_find (GstTypeFind * tf, gpointer ignore)
444 {
445   guint8 *data;
446   GdkPixbufLoader *pixbuf_loader;
447   GdkPixbufFormat *format;
448
449   data = gst_type_find_peek (tf, 0, GST_GDK_PIXBUF_TYPE_FIND_SIZE);
450   if (data == NULL)
451     return;
452
453   GST_DEBUG ("creating new loader");
454
455   pixbuf_loader = gdk_pixbuf_loader_new ();
456
457   gdk_pixbuf_loader_write (pixbuf_loader, data, GST_GDK_PIXBUF_TYPE_FIND_SIZE,
458       NULL);
459
460   format = gdk_pixbuf_loader_get_format (pixbuf_loader);
461
462   if (format != NULL) {
463     GstCaps *caps;
464     gchar **p;
465     gchar **mlist = gdk_pixbuf_format_get_mime_types (format);
466
467     for (p = mlist; *p; ++p) {
468       GST_DEBUG ("suggesting mime type %s", *p);
469       caps = gst_caps_new_simple (*p, NULL);
470       gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, caps);
471       gst_caps_free (caps);
472     }
473     g_strfreev (mlist);
474   }
475
476   GST_DEBUG ("closing pixbuf loader, hope it doesn't hang ...");
477   /* librsvg 2.4.x has a bug where it triggers an endless loop in trying
478      to close a gzip that's not an svg; fixed upstream but no good way
479      to work around it */
480   gdk_pixbuf_loader_close (pixbuf_loader, NULL);
481   GST_DEBUG ("closed pixbuf loader");
482   g_object_unref (G_OBJECT (pixbuf_loader));
483 }
484 #endif
485
486 /* entry point to initialize the plug-in
487  * initialize the plug-in itself
488  * register the element factories and pad templates
489  * register the features
490  */
491 static gboolean
492 plugin_init (GstPlugin * plugin)
493 {
494   GST_DEBUG_CATEGORY_INIT (gst_gdk_pixbuf_debug, "gdkpixbuf", 0,
495       "gdk pixbuf loader");
496
497   if (!gst_element_register (plugin, "gdkpixbufdec", GST_RANK_MARGINAL,
498           GST_TYPE_GDK_PIXBUF))
499     return FALSE;
500
501 #ifdef enable_typefind
502   gst_type_find_register (plugin, "image/*", GST_RANK_MARGINAL,
503       gst_gdk_pixbuf_type_find, NULL, GST_CAPS_ANY, NULL);
504 #endif
505
506   if (!pixbufscale_init (plugin))
507     return FALSE;
508
509   /* plugin initialisation succeeded */
510   return TRUE;
511 }
512
513
514 /* this is the structure that gst-register looks for
515  * so keep the name plugin_desc, or you cannot get your plug-in registered */
516 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
517     GST_VERSION_MINOR,
518     "gdkpixbuf",
519     "GDK Pixbuf decoder & scaler",
520     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)