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