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