upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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  * SECTION:element-pngdec
17  *
18  * Decodes png images. If there is no framerate set on sink caps, it sends EOS
19  * after the first picture.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "gstpngdec.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <gst/video/video.h>
31 #include <gst/gst-i18n-plugin.h>
32
33 GST_DEBUG_CATEGORY_STATIC (pngdec_debug);
34 #define GST_CAT_DEFAULT pngdec_debug
35
36 static void gst_pngdec_base_init (gpointer g_class);
37 static void gst_pngdec_class_init (GstPngDecClass * klass);
38 static void gst_pngdec_init (GstPngDec * pngdec);
39
40 static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
41 static gboolean gst_pngdec_libpng_clear (GstPngDec * pngdec);
42
43 static GstStateChangeReturn gst_pngdec_change_state (GstElement * element,
44     GstStateChange transition);
45
46 static gboolean gst_pngdec_sink_activate_push (GstPad * sinkpad,
47     gboolean active);
48 static gboolean gst_pngdec_sink_activate_pull (GstPad * sinkpad,
49     gboolean active);
50 static gboolean gst_pngdec_sink_activate (GstPad * sinkpad);
51
52 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
53
54 static void gst_pngdec_task (GstPad * pad);
55 static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstBuffer * buffer);
56 static gboolean gst_pngdec_sink_event (GstPad * pad, GstEvent * event);
57 static gboolean gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps);
58
59 static GstElementClass *parent_class = NULL;
60
61 GType
62 gst_pngdec_get_type (void)
63 {
64   static GType pngdec_type = 0;
65
66   if (!pngdec_type) {
67     static const GTypeInfo pngdec_info = {
68       sizeof (GstPngDecClass),
69       gst_pngdec_base_init,
70       NULL,
71       (GClassInitFunc) gst_pngdec_class_init,
72       NULL,
73       NULL,
74       sizeof (GstPngDec),
75       0,
76       (GInstanceInitFunc) gst_pngdec_init,
77     };
78
79     pngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngDec",
80         &pngdec_info, 0);
81   }
82   return pngdec_type;
83 }
84
85 static GstStaticPadTemplate gst_pngdec_src_pad_template =
86     GST_STATIC_PAD_TEMPLATE ("src",
87     GST_PAD_SRC,
88     GST_PAD_ALWAYS,
89     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB ";"
90         GST_VIDEO_CAPS_ARGB_64)
91     );
92
93 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
94 GST_STATIC_PAD_TEMPLATE ("sink",
95     GST_PAD_SINK,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS ("image/png")
98     );
99
100 static void
101 gst_pngdec_base_init (gpointer g_class)
102 {
103   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
104
105   gst_element_class_add_pad_template (element_class,
106       gst_static_pad_template_get (&gst_pngdec_src_pad_template));
107   gst_element_class_add_pad_template (element_class,
108       gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
109   gst_element_class_set_details_simple (element_class, "PNG image decoder",
110       "Codec/Decoder/Image",
111       "Decode a png video frame to a raw image",
112       "Wim Taymans <wim@fluendo.com>");
113 }
114
115 static void
116 gst_pngdec_class_init (GstPngDecClass * klass)
117 {
118   GstElementClass *gstelement_class;
119
120   gstelement_class = (GstElementClass *) klass;
121
122   parent_class = g_type_class_peek_parent (klass);
123
124   gstelement_class->change_state = gst_pngdec_change_state;
125
126   GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
127 }
128
129 static void
130 gst_pngdec_init (GstPngDec * pngdec)
131 {
132   pngdec->sinkpad =
133       gst_pad_new_from_static_template (&gst_pngdec_sink_pad_template, "sink");
134   gst_pad_set_activate_function (pngdec->sinkpad, gst_pngdec_sink_activate);
135   gst_pad_set_activatepush_function (pngdec->sinkpad,
136       gst_pngdec_sink_activate_push);
137   gst_pad_set_activatepull_function (pngdec->sinkpad,
138       gst_pngdec_sink_activate_pull);
139   gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
140   gst_pad_set_event_function (pngdec->sinkpad, gst_pngdec_sink_event);
141   gst_pad_set_setcaps_function (pngdec->sinkpad, gst_pngdec_sink_setcaps);
142   gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
143
144   pngdec->srcpad =
145       gst_pad_new_from_static_template (&gst_pngdec_src_pad_template, "src");
146   gst_pad_use_fixed_caps (pngdec->srcpad);
147   gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
148
149   pngdec->buffer_out = NULL;
150   pngdec->png = NULL;
151   pngdec->info = NULL;
152   pngdec->endinfo = NULL;
153   pngdec->setup = FALSE;
154
155   pngdec->color_type = -1;
156   pngdec->width = -1;
157   pngdec->height = -1;
158   pngdec->bpp = -1;
159   pngdec->fps_n = 0;
160   pngdec->fps_d = 1;
161
162   pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
163   pngdec->in_duration = GST_CLOCK_TIME_NONE;
164
165   gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
166
167   pngdec->image_ready = FALSE;
168 }
169
170 static void
171 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
172 {
173   GST_ERROR ("%s", error_msg);
174 }
175
176 static void
177 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
178 {
179   GST_WARNING ("%s", warning_msg);
180 }
181
182 static void
183 user_info_callback (png_structp png_ptr, png_infop info)
184 {
185   GstPngDec *pngdec = NULL;
186   GstFlowReturn ret = GST_FLOW_OK;
187   size_t buffer_size;
188   GstBuffer *buffer = NULL;
189
190   pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
191
192   GST_LOG ("info ready");
193
194   /* Generate the caps and configure */
195   ret = gst_pngdec_caps_create_and_set (pngdec);
196   if (ret != GST_FLOW_OK) {
197     goto beach;
198   }
199
200   /* Allocate output buffer */
201   pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
202   if (pngdec->rowbytes > (G_MAXUINT32 - 3)
203       || pngdec->height > G_MAXUINT32 / pngdec->rowbytes) {
204     ret = GST_FLOW_ERROR;
205     goto beach;
206   }
207   pngdec->rowbytes = GST_ROUND_UP_4 (pngdec->rowbytes);
208   buffer_size = pngdec->height * pngdec->rowbytes;
209
210   ret =
211       gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
212       buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
213   if (ret != GST_FLOW_OK) {
214     goto beach;
215   }
216
217   pngdec->buffer_out = buffer;
218
219 beach:
220   pngdec->ret = ret;
221 }
222
223 static void
224 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
225     png_uint_32 row_num, int pass)
226 {
227   GstPngDec *pngdec = NULL;
228
229   pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
230
231   /* FIXME: implement interlaced pictures */
232
233   /* If buffer_out doesn't exist, it means buffer_alloc failed, which 
234    * will already have set the return code */
235   if (GST_IS_BUFFER (pngdec->buffer_out)) {
236     size_t offset = row_num * pngdec->rowbytes;
237
238     GST_LOG ("got row %u, copying in buffer %p at offset %" G_GSIZE_FORMAT,
239         (guint) row_num, pngdec->buffer_out, offset);
240     memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row,
241         pngdec->rowbytes);
242     pngdec->ret = GST_FLOW_OK;
243   }
244 }
245
246 static gboolean
247 buffer_clip (GstPngDec * dec, GstBuffer * buffer)
248 {
249   gboolean res = TRUE;
250   gint64 cstart, cstop;
251
252
253   if ((!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) ||
254       (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) ||
255       (dec->segment.format != GST_FORMAT_TIME))
256     goto beach;
257
258   cstart = GST_BUFFER_TIMESTAMP (buffer);
259   cstop = GST_BUFFER_DURATION (buffer);
260
261   if ((res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
262               cstart, cstart + cstop, &cstart, &cstop))) {
263     GST_BUFFER_TIMESTAMP (buffer) = cstart;
264     GST_BUFFER_DURATION (buffer) = cstop - cstart;
265   }
266
267 beach:
268   return res;
269 }
270
271 static void
272 user_end_callback (png_structp png_ptr, png_infop info)
273 {
274   GstPngDec *pngdec = NULL;
275
276   pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
277
278   GST_LOG_OBJECT (pngdec, "and we are done reading this image");
279
280   if (!pngdec->buffer_out)
281     return;
282
283   if (GST_CLOCK_TIME_IS_VALID (pngdec->in_timestamp))
284     GST_BUFFER_TIMESTAMP (pngdec->buffer_out) = pngdec->in_timestamp;
285   if (GST_CLOCK_TIME_IS_VALID (pngdec->in_duration))
286     GST_BUFFER_DURATION (pngdec->buffer_out) = pngdec->in_duration;
287
288   /* buffer clipping */
289   if (buffer_clip (pngdec, pngdec->buffer_out)) {
290     /* Push our buffer and then EOS if needed */
291     GST_LOG_OBJECT (pngdec, "pushing buffer with ts=%" GST_TIME_FORMAT,
292         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (pngdec->buffer_out)));
293
294     pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
295   } else {
296     GST_LOG_OBJECT (pngdec, "dropped decoded buffer");
297     gst_buffer_unref (pngdec->buffer_out);
298   }
299   pngdec->buffer_out = NULL;
300   pngdec->image_ready = TRUE;
301 }
302
303 static void
304 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
305 {
306   GstPngDec *pngdec;
307   GstBuffer *buffer;
308   GstFlowReturn ret = GST_FLOW_OK;
309   guint size;
310
311   pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
312
313   GST_LOG ("reading %" G_GSIZE_FORMAT " bytes of data at offset %d", length,
314       pngdec->offset);
315
316   ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
317   if (ret != GST_FLOW_OK)
318     goto pause;
319
320   size = GST_BUFFER_SIZE (buffer);
321
322   if (size != length)
323     goto short_buffer;
324
325   memcpy (data, GST_BUFFER_DATA (buffer), size);
326
327   gst_buffer_unref (buffer);
328
329   pngdec->offset += length;
330
331   return;
332
333   /* ERRORS */
334 pause:
335   {
336     GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
337         gst_flow_get_name (ret));
338     gst_pad_pause_task (pngdec->sinkpad);
339     if (ret == GST_FLOW_UNEXPECTED) {
340       gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
341     } else if (ret < GST_FLOW_UNEXPECTED || ret == GST_FLOW_NOT_LINKED) {
342       GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
343           (_("Internal data stream error.")),
344           ("stream stopped, reason %s", gst_flow_get_name (ret)));
345       gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
346     }
347     png_error (png_ptr, "Internal data stream error.");
348     return;
349   }
350 short_buffer:
351   {
352     gst_buffer_unref (buffer);
353     GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
354         (_("Internal data stream error.")),
355         ("Read %u, needed %" G_GSIZE_FORMAT "bytes", size, length));
356     ret = GST_FLOW_ERROR;
357     goto pause;
358   }
359 }
360
361 static GstFlowReturn
362 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
363 {
364   GstFlowReturn ret = GST_FLOW_OK;
365   GstCaps *caps = NULL, *res = NULL;
366   GstPadTemplate *templ = NULL;
367   gint bpc = 0, color_type;
368   png_uint_32 width, height;
369
370   g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
371
372   /* Get bits per channel */
373   bpc = png_get_bit_depth (pngdec->png, pngdec->info);
374   if (bpc > 8) {
375     /* Add alpha channel if 16-bit depth */
376     png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
377     png_set_swap (pngdec->png);
378   }
379
380   /* Get Color type */
381   color_type = png_get_color_type (pngdec->png, pngdec->info);
382
383 #if 0
384   /* We used to have this HACK to reverse the outgoing bytes, but the problem
385    * that originally required the hack seems to have been in ffmpegcolorspace's
386    * RGBA descriptions. It doesn't seem needed now that's fixed, but might
387    * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
388   if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
389     png_set_bgr (pngdec->png);
390 #endif
391
392   /* Gray scale converted to RGB and upscaled to 8 bits */
393   if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
394       (color_type == PNG_COLOR_TYPE_GRAY)) {
395     GST_LOG_OBJECT (pngdec, "converting grayscale png to RGB");
396     png_set_gray_to_rgb (pngdec->png);
397     if (bpc < 8) {              /* Convert to 8 bits */
398       GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
399 #if PNG_LIBPNG_VER < 10400
400       png_set_gray_1_2_4_to_8 (pngdec->png);
401 #else
402       png_set_expand_gray_1_2_4_to_8 (pngdec->png);
403 #endif
404     }
405   }
406
407   /* Palette converted to RGB */
408   if (color_type == PNG_COLOR_TYPE_PALETTE) {
409     GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
410     png_set_palette_to_rgb (pngdec->png);
411   }
412
413   /* Update the info structure */
414   png_read_update_info (pngdec->png, pngdec->info);
415
416   /* Get IHDR header again after transformation settings */
417
418   png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
419       &bpc, &pngdec->color_type, NULL, NULL, NULL);
420
421   pngdec->width = width;
422   pngdec->height = height;
423
424   GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", pngdec->width,
425       pngdec->height);
426
427   switch (pngdec->color_type) {
428     case PNG_COLOR_TYPE_RGB:
429       GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
430       pngdec->bpp = 3 * bpc;
431       break;
432     case PNG_COLOR_TYPE_RGB_ALPHA:
433       GST_LOG_OBJECT (pngdec, "we have an alpha channel, depth is 32 bits");
434       pngdec->bpp = 4 * bpc;
435       break;
436     default:
437       GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
438           ("pngdec does not support this color type"));
439       ret = GST_FLOW_NOT_SUPPORTED;
440       goto beach;
441   }
442
443   caps = gst_caps_new_simple ("video/x-raw-rgb",
444       "width", G_TYPE_INT, pngdec->width,
445       "height", G_TYPE_INT, pngdec->height,
446       "bpp", G_TYPE_INT, pngdec->bpp,
447       "framerate", GST_TYPE_FRACTION, pngdec->fps_n, pngdec->fps_d, NULL);
448
449   templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
450
451   res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
452
453   gst_caps_unref (caps);
454   gst_object_unref (templ);
455
456   if (!gst_pad_set_caps (pngdec->srcpad, res))
457     ret = GST_FLOW_NOT_NEGOTIATED;
458
459   GST_DEBUG_OBJECT (pngdec, "our caps %" GST_PTR_FORMAT, res);
460
461   gst_caps_unref (res);
462
463   /* Push a newsegment event */
464   if (pngdec->need_newsegment) {
465     gst_pad_push_event (pngdec->srcpad,
466         gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
467     pngdec->need_newsegment = FALSE;
468   }
469
470 beach:
471   return ret;
472 }
473
474 static void
475 gst_pngdec_task (GstPad * pad)
476 {
477   GstPngDec *pngdec;
478   GstBuffer *buffer = NULL;
479   size_t buffer_size = 0;
480   gint i = 0;
481   png_bytep *rows, inp;
482   png_uint_32 rowbytes;
483   GstFlowReturn ret = GST_FLOW_OK;
484
485   pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
486
487   GST_LOG_OBJECT (pngdec, "read frame");
488
489   /* Let libpng come back here on error */
490   if (setjmp (png_jmpbuf (pngdec->png))) {
491     ret = GST_FLOW_ERROR;
492     goto pause;
493   }
494
495   /* Set reading callback */
496   png_set_read_fn (pngdec->png, pngdec, user_read_data);
497
498   /* Read info */
499   png_read_info (pngdec->png, pngdec->info);
500
501   /* Generate the caps and configure */
502   ret = gst_pngdec_caps_create_and_set (pngdec);
503   if (ret != GST_FLOW_OK) {
504     goto pause;
505   }
506
507   /* Allocate output buffer */
508   rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
509   if (rowbytes > (G_MAXUINT32 - 3) || pngdec->height > G_MAXUINT32 / rowbytes) {
510     ret = GST_FLOW_ERROR;
511     goto pause;
512   }
513   rowbytes = GST_ROUND_UP_4 (rowbytes);
514   buffer_size = pngdec->height * rowbytes;
515   ret =
516       gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
517       buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
518   if (ret != GST_FLOW_OK)
519     goto pause;
520
521   rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height);
522
523   inp = GST_BUFFER_DATA (buffer);
524
525   for (i = 0; i < pngdec->height; i++) {
526     rows[i] = inp;
527     inp += rowbytes;
528   }
529
530   /* Read the actual picture */
531   png_read_image (pngdec->png, rows);
532   g_free (rows);
533
534   /* Push the raw RGB frame */
535   ret = gst_pad_push (pngdec->srcpad, buffer);
536   if (ret != GST_FLOW_OK)
537     goto pause;
538
539   /* And we are done */
540   gst_pad_pause_task (pngdec->sinkpad);
541   gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
542   return;
543
544 pause:
545   {
546     GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
547         gst_flow_get_name (ret));
548     gst_pad_pause_task (pngdec->sinkpad);
549     if (ret == GST_FLOW_UNEXPECTED) {
550       gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
551     } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
552       GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
553           (_("Internal data stream error.")),
554           ("stream stopped, reason %s", gst_flow_get_name (ret)));
555       gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
556     }
557   }
558 }
559
560 static GstFlowReturn
561 gst_pngdec_chain (GstPad * pad, GstBuffer * buffer)
562 {
563   GstPngDec *pngdec;
564   GstFlowReturn ret = GST_FLOW_OK;
565
566   pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
567
568   GST_LOG_OBJECT (pngdec, "Got buffer, size=%u", GST_BUFFER_SIZE (buffer));
569
570   if (G_UNLIKELY (!pngdec->setup))
571     goto not_configured;
572
573   /* Something is going wrong in our callbacks */
574   ret = pngdec->ret;
575   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
576     GST_WARNING_OBJECT (pngdec, "we have a pending return code of %d", ret);
577     goto beach;
578   }
579
580   /* Let libpng come back here on error */
581   if (setjmp (png_jmpbuf (pngdec->png))) {
582     GST_WARNING_OBJECT (pngdec, "error during decoding");
583     ret = GST_FLOW_ERROR;
584     goto beach;
585   }
586
587   pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
588   pngdec->in_duration = GST_BUFFER_DURATION (buffer);
589
590   /* Progressive loading of the PNG image */
591   png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer),
592       GST_BUFFER_SIZE (buffer));
593
594   if (pngdec->image_ready) {
595     if (pngdec->framed) {
596       /* Reset ourselves for the next frame */
597       gst_pngdec_libpng_clear (pngdec);
598       gst_pngdec_libpng_init (pngdec);
599       GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
600       png_set_progressive_read_fn (pngdec->png, pngdec,
601           user_info_callback, user_endrow_callback, user_end_callback);
602     } else {
603       GST_LOG_OBJECT (pngdec, "sending EOS");
604       pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
605     }
606     pngdec->image_ready = FALSE;
607   }
608
609   /* grab new return code */
610   ret = pngdec->ret;
611
612   /* And release the buffer */
613   gst_buffer_unref (buffer);
614
615 beach:
616   gst_object_unref (pngdec);
617
618   return ret;
619
620   /* ERRORS */
621 not_configured:
622   {
623     GST_LOG_OBJECT (pngdec, "we are not configured yet");
624     ret = GST_FLOW_WRONG_STATE;
625     goto beach;
626   }
627 }
628
629 static gboolean
630 gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps)
631 {
632   GstStructure *s;
633   GstPngDec *pngdec;
634   gint num, denom;
635
636   pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
637
638   s = gst_caps_get_structure (caps, 0);
639   if (gst_structure_get_fraction (s, "framerate", &num, &denom)) {
640     GST_DEBUG_OBJECT (pngdec, "framed input");
641     pngdec->framed = TRUE;
642     pngdec->fps_n = num;
643     pngdec->fps_d = denom;
644   } else {
645     GST_DEBUG_OBJECT (pngdec, "single picture input");
646     pngdec->framed = FALSE;
647     pngdec->fps_n = 0;
648     pngdec->fps_d = 1;
649   }
650
651   gst_object_unref (pngdec);
652   return TRUE;
653 }
654
655 static gboolean
656 gst_pngdec_sink_event (GstPad * pad, GstEvent * event)
657 {
658   GstPngDec *pngdec;
659   gboolean res;
660
661   pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
662
663   switch (GST_EVENT_TYPE (event)) {
664     case GST_EVENT_NEWSEGMENT:{
665       gdouble rate, arate;
666       gboolean update;
667       gint64 start, stop, position;
668       GstFormat fmt;
669
670       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt,
671           &start, &stop, &position);
672
673       gst_segment_set_newsegment_full (&pngdec->segment, update, rate, arate,
674           fmt, start, stop, position);
675
676       GST_LOG_OBJECT (pngdec, "NEWSEGMENT (%s)", gst_format_get_name (fmt));
677
678       if (fmt == GST_FORMAT_TIME) {
679         pngdec->need_newsegment = FALSE;
680         res = gst_pad_push_event (pngdec->srcpad, event);
681       } else {
682         gst_event_unref (event);
683         res = TRUE;
684       }
685       break;
686     }
687     case GST_EVENT_FLUSH_STOP:
688     {
689       gst_pngdec_libpng_clear (pngdec);
690       gst_pngdec_libpng_init (pngdec);
691       png_set_progressive_read_fn (pngdec->png, pngdec,
692           user_info_callback, user_endrow_callback, user_end_callback);
693       pngdec->ret = GST_FLOW_OK;
694       gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
695       res = gst_pad_push_event (pngdec->srcpad, event);
696       break;
697     }
698     case GST_EVENT_EOS:
699     {
700       GST_LOG_OBJECT (pngdec, "EOS");
701       gst_pngdec_libpng_clear (pngdec);
702       pngdec->ret = GST_FLOW_UNEXPECTED;
703       res = gst_pad_push_event (pngdec->srcpad, event);
704       break;
705     }
706     default:
707       res = gst_pad_push_event (pngdec->srcpad, event);
708       break;
709   }
710
711   gst_object_unref (pngdec);
712   return res;
713 }
714
715
716 /* Clean up the libpng structures */
717 static gboolean
718 gst_pngdec_libpng_clear (GstPngDec * pngdec)
719 {
720   png_infopp info = NULL, endinfo = NULL;
721
722   g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
723
724   GST_LOG ("cleaning up libpng structures");
725
726   if (pngdec->info) {
727     info = &pngdec->info;
728   }
729
730   if (pngdec->endinfo) {
731     endinfo = &pngdec->endinfo;
732   }
733
734   if (pngdec->png) {
735     png_destroy_read_struct (&(pngdec->png), info, endinfo);
736     pngdec->png = NULL;
737     pngdec->info = NULL;
738     pngdec->endinfo = NULL;
739   }
740
741   pngdec->bpp = pngdec->color_type = pngdec->height = pngdec->width = -1;
742   pngdec->offset = 0;
743   pngdec->rowbytes = 0;
744   pngdec->buffer_out = NULL;
745
746   pngdec->setup = FALSE;
747
748   pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
749   pngdec->in_duration = GST_CLOCK_TIME_NONE;
750
751   return TRUE;
752 }
753
754 static gboolean
755 gst_pngdec_libpng_init (GstPngDec * pngdec)
756 {
757   g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
758
759   if (pngdec->setup)
760     return TRUE;
761
762   GST_LOG ("init libpng structures");
763
764   /* initialize png struct stuff */
765   pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
766       (png_voidp) NULL, user_error_fn, user_warning_fn);
767
768   if (pngdec->png == NULL)
769     goto init_failed;
770
771   pngdec->info = png_create_info_struct (pngdec->png);
772   if (pngdec->info == NULL)
773     goto info_failed;
774
775   pngdec->endinfo = png_create_info_struct (pngdec->png);
776   if (pngdec->endinfo == NULL)
777     goto endinfo_failed;
778
779   pngdec->setup = TRUE;
780
781   return TRUE;
782
783   /* ERRORS */
784 init_failed:
785   {
786     GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
787         ("Failed to initialize png structure"));
788     return FALSE;
789   }
790 info_failed:
791   {
792     gst_pngdec_libpng_clear (pngdec);
793     GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
794         ("Failed to initialize info structure"));
795     return FALSE;
796   }
797 endinfo_failed:
798   {
799     gst_pngdec_libpng_clear (pngdec);
800     GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
801         ("Failed to initialize endinfo structure"));
802     return FALSE;
803   }
804 }
805
806 static GstStateChangeReturn
807 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
808 {
809   GstStateChangeReturn ret;
810   GstPngDec *pngdec;
811
812   pngdec = GST_PNGDEC (element);
813
814   switch (transition) {
815     case GST_STATE_CHANGE_READY_TO_PAUSED:
816       gst_pngdec_libpng_init (pngdec);
817       pngdec->need_newsegment = TRUE;
818       pngdec->framed = FALSE;
819       pngdec->ret = GST_FLOW_OK;
820       gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
821       break;
822     default:
823       break;
824   }
825
826   ret = parent_class->change_state (element, transition);
827   if (ret != GST_STATE_CHANGE_SUCCESS)
828     return ret;
829
830   switch (transition) {
831     case GST_STATE_CHANGE_PAUSED_TO_READY:
832       gst_pngdec_libpng_clear (pngdec);
833       break;
834     default:
835       break;
836   }
837
838   return ret;
839 }
840
841 /* this function gets called when we activate ourselves in push mode. */
842 static gboolean
843 gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active)
844 {
845   GstPngDec *pngdec;
846
847   pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
848
849   pngdec->ret = GST_FLOW_OK;
850
851   if (active) {
852     /* Let libpng come back here on error */
853     if (setjmp (png_jmpbuf (pngdec->png)))
854       goto setup_failed;
855
856     GST_LOG ("setting up progressive loading callbacks");
857     png_set_progressive_read_fn (pngdec->png, pngdec,
858         user_info_callback, user_endrow_callback, user_end_callback);
859   }
860   return TRUE;
861
862 setup_failed:
863   {
864     GST_LOG ("failed setting up libpng jmpbuf");
865     gst_pngdec_libpng_clear (pngdec);
866     return FALSE;
867   }
868 }
869
870 /* this function gets called when we activate ourselves in pull mode.
871  * We can perform  random access to the resource and we start a task
872  * to start reading */
873 static gboolean
874 gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
875 {
876   if (active) {
877     return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
878         sinkpad);
879   } else {
880     return gst_pad_stop_task (sinkpad);
881   }
882 }
883
884 /* this function is called when the pad is activated and should start
885  * processing data.
886  *
887  * We check if we can do random access to decide if we work push or
888  * pull based.
889  */
890 static gboolean
891 gst_pngdec_sink_activate (GstPad * sinkpad)
892 {
893   if (gst_pad_check_pull_range (sinkpad)) {
894     return gst_pad_activate_pull (sinkpad, TRUE);
895   } else {
896     return gst_pad_activate_push (sinkpad, TRUE);
897   }
898 }