ext/libpng/: mime fixage.
[platform/upstream/gst-plugins-good.git] / ext / libpng / gstpngdec.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is distributed in the hope that it will be useful,
5  * but WITHOUT ANY WARRANTY; without even the implied warranty of
6  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7  * Library General Public License for more details.
8  *
9  * You should have received a copy of the GNU Library General Public
10  * License along with this library; if not, write to the
11  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
12  * Boston, MA 02111-1307, USA.
13  *
14  */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 #include <string.h>
20 #include <gst/gst.h>
21 #include "gstpngdec.h"
22 #include <gst/video/video.h>
23
24 static GstElementDetails gst_pngdec_details = {
25   "PNG decoder",
26   "Codec/Decoder/Image",
27   "Decode a png video frame to a raw image",
28   "Wim Taymans <wim@fluendo.com>",
29 };
30
31
32 /* Filter signals and args */
33 enum
34 {
35   /* FILL ME */
36   LAST_SIGNAL
37 };
38
39 enum
40 {
41   ARG_0
42 };
43
44 static void gst_pngdec_base_init (gpointer g_class);
45 static void gst_pngdec_class_init (GstPngDecClass * klass);
46 static void gst_pngdec_init (GstPngDec * pngdec);
47
48 static void gst_pngdec_chain (GstPad * pad, GstData * _data);
49
50 static GstCaps *gst_pngdec_src_getcaps (GstPad * pad);
51
52 static GstElementClass *parent_class = NULL;
53
54
55 static void
56 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
57 {
58   g_warning ("%s", error_msg);
59 }
60
61 static void
62 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
63 {
64   g_warning ("%s", warning_msg);
65 }
66
67
68 GType
69 gst_pngdec_get_type (void)
70 {
71   static GType pngdec_type = 0;
72
73   if (!pngdec_type) {
74     static const GTypeInfo pngdec_info = {
75       sizeof (GstPngDecClass),
76       gst_pngdec_base_init,
77       NULL,
78       (GClassInitFunc) gst_pngdec_class_init,
79       NULL,
80       NULL,
81       sizeof (GstPngDec),
82       0,
83       (GInstanceInitFunc) gst_pngdec_init,
84     };
85
86     pngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngDec",
87         &pngdec_info, 0);
88   }
89   return pngdec_type;
90 }
91
92 static GstStaticPadTemplate gst_pngdec_src_pad_template =
93     GST_STATIC_PAD_TEMPLATE ("src",
94     GST_PAD_SRC,
95     GST_PAD_ALWAYS,
96     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
97     );
98
99 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
100 GST_STATIC_PAD_TEMPLATE ("sink",
101     GST_PAD_SINK,
102     GST_PAD_ALWAYS,
103     GST_STATIC_CAPS ("image/png, "
104         "width = (int) [ 16, 4096 ], "
105         "height = (int) [ 16, 4096 ], " "framerate = (double) [ 0.0, MAX ]")
106     );
107
108 static void
109 gst_pngdec_base_init (gpointer g_class)
110 {
111   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
112
113   gst_element_class_add_pad_template (element_class,
114       gst_static_pad_template_get (&gst_pngdec_src_pad_template));
115   gst_element_class_add_pad_template (element_class,
116       gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
117   gst_element_class_set_details (element_class, &gst_pngdec_details);
118 }
119
120 static void
121 gst_pngdec_class_init (GstPngDecClass * klass)
122 {
123   GObjectClass *gobject_class;
124   GstElementClass *gstelement_class;
125
126   gobject_class = (GObjectClass *) klass;
127   gstelement_class = (GstElementClass *) klass;
128
129   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
130 }
131
132
133 static GstPadLinkReturn
134 gst_pngdec_sinklink (GstPad * pad, const GstCaps * caps)
135 {
136   GstPngDec *pngdec;
137   GstStructure *structure;
138
139   pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
140
141   structure = gst_caps_get_structure (caps, 0);
142   gst_structure_get_double (structure, "framerate", &pngdec->fps);
143
144   return TRUE;
145 }
146
147 static void
148 gst_pngdec_init (GstPngDec * pngdec)
149 {
150   pngdec->sinkpad = gst_pad_new_from_template (gst_static_pad_template_get
151       (&gst_pngdec_sink_pad_template), "sink");
152   gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
153
154   pngdec->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
155       (&gst_pngdec_src_pad_template), "src");
156   gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
157
158   gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
159   gst_pad_set_link_function (pngdec->sinkpad, gst_pngdec_sinklink);
160
161   gst_pad_set_getcaps_function (pngdec->srcpad, gst_pngdec_src_getcaps);
162
163   pngdec->png = NULL;
164   pngdec->info = NULL;
165
166   pngdec->color_type = -1;
167   pngdec->width = -1;
168   pngdec->height = -1;
169   pngdec->fps = -1;
170 }
171
172 static GstCaps *
173 gst_pngdec_src_getcaps (GstPad * pad)
174 {
175   GstPngDec *pngdec;
176   GstCaps *caps;
177   gint i;
178   GstPadTemplate *templ;
179   GstCaps *inter;
180
181   pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
182   templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
183   caps = gst_caps_copy (gst_pad_template_get_caps (templ));
184
185   if (pngdec->color_type != -1) {
186     GstCaps *to_inter = NULL;
187
188     switch (pngdec->color_type) {
189       case PNG_COLOR_TYPE_RGB:
190         to_inter = gst_caps_new_simple ("video/x-raw-rgb",
191             "bpp", G_TYPE_INT, 24, NULL);
192         break;
193       case PNG_COLOR_TYPE_RGB_ALPHA:
194         to_inter = gst_caps_new_simple ("video/x-raw-rgb",
195             "bpp", G_TYPE_INT, 32, NULL);
196         break;
197       case PNG_COLOR_TYPE_GRAY:
198       case PNG_COLOR_TYPE_PALETTE:
199       case PNG_COLOR_TYPE_GRAY_ALPHA:
200       default:
201         GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
202             ("pngdec does not support grayscale or paletted data yet"));
203         break;
204     }
205     inter = gst_caps_intersect (caps, to_inter);
206     gst_caps_free (caps);
207     gst_caps_free (to_inter);
208     caps = inter;
209   }
210
211   for (i = 0; i < gst_caps_get_size (caps); i++) {
212     GstStructure *structure = gst_caps_get_structure (caps, i);
213
214     if (pngdec->width != -1) {
215       gst_structure_set (structure, "width", G_TYPE_INT, pngdec->width, NULL);
216     }
217
218     if (pngdec->height != -1) {
219       gst_structure_set (structure, "height", G_TYPE_INT, pngdec->height, NULL);
220     }
221
222     if (pngdec->fps != -1) {
223       gst_structure_set (structure,
224           "framerate", G_TYPE_DOUBLE, pngdec->fps, NULL);
225     }
226   }
227
228   return caps;
229 }
230
231 static void
232 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
233 {
234   GstPngDec *dec;
235
236   dec = GST_PNGDEC (png_ptr->io_ptr);
237
238   if (GST_BUFFER_SIZE (dec->buffer_in) < dec->offset + length) {
239     g_warning ("reading past end of buffer");
240   }
241
242   memcpy (data, GST_BUFFER_DATA (dec->buffer_in) + dec->offset, length);
243
244   dec->offset += length;
245 }
246
247 static void
248 gst_pngdec_chain (GstPad * pad, GstData * _data)
249 {
250   GstBuffer *buf = GST_BUFFER (_data);
251   GstPngDec *pngdec;
252   png_uint_32 width, height;
253   gint depth, color;
254   png_bytep *rows, inp;
255   GstBuffer *out;
256   gint i;
257
258   pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
259
260   if (!GST_PAD_IS_USABLE (pngdec->srcpad)) {
261     gst_buffer_unref (buf);
262     return;
263   }
264
265   pngdec->buffer_in = buf;
266   pngdec->offset = 0;
267
268   /* initialize png struct stuff */
269   pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
270       (png_voidp) NULL, user_error_fn, user_warning_fn);
271
272   /* FIXME: better error handling */
273   if (pngdec->png == NULL) {
274     g_warning ("Failed to initialize png structure");
275     return;
276   }
277
278   pngdec->info = png_create_info_struct (pngdec->png);
279   if (pngdec->info == NULL) {
280     g_warning ("Failed to initialize info structure");
281     png_destroy_read_struct (&(pngdec->png), (png_infopp) NULL,
282         (png_infopp) NULL);
283     return;
284   }
285
286   pngdec->endinfo = png_create_info_struct (pngdec->png);
287   if (pngdec->endinfo == NULL) {
288     g_warning ("Failed to initialize endinfo structure");
289     return;
290   }
291
292   /* non-0 return is from a longjmp inside of libpng */
293   if (setjmp (pngdec->png->jmpbuf) != 0) {
294     GST_DEBUG ("returning from longjmp");
295     png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
296     return;
297   }
298
299   png_set_read_fn (pngdec->png, pngdec, user_read_data);
300
301   png_read_info (pngdec->png, pngdec->info);
302   png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
303       &depth, &color, NULL, NULL, NULL);
304
305   GST_LOG ("color type: %d\n", pngdec->info->color_type);
306
307   if (pngdec->width != width ||
308       pngdec->height != height ||
309       pngdec->color_type != pngdec->info->color_type) {
310     pngdec->width = width;
311     pngdec->height = height;
312     pngdec->color_type = pngdec->info->color_type;
313
314     if (GST_PAD_LINK_FAILED (gst_pad_renegotiate (pngdec->srcpad))) {
315       GST_ELEMENT_ERROR (pngdec, CORE, NEGOTIATION, (NULL), (NULL));
316       return;
317     }
318   }
319
320   rows = (png_bytep *) g_malloc (sizeof (png_bytep) * height);
321
322   out =
323       gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
324       width * height * 4);
325
326   inp = GST_BUFFER_DATA (out);
327   for (i = 0; i < height; i++) {
328     rows[i] = inp;
329     inp += width * 4;
330   }
331
332   png_read_image (pngdec->png, rows);
333   free (rows);
334
335   png_destroy_info_struct (pngdec->png, &pngdec->info);
336   png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
337
338   pngdec->buffer_in = NULL;
339   gst_buffer_unref (buf);
340
341   gst_pad_push (pngdec->srcpad, GST_DATA (out));
342 }