ext/libpng/gstpngdec.*: Handle more than one frame if the content is framed, like...
[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 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 (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
163 static void
164 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
165 {
166   GST_ERROR ("%s", error_msg);
167 }
168
169 static void
170 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
171 {
172   GST_WARNING ("%s", warning_msg);
173 }
174
175 static void
176 user_info_callback (png_structp png_ptr, png_infop info)
177 {
178   GstPngDec *pngdec = NULL;
179   GstFlowReturn ret = GST_FLOW_OK;
180   size_t buffer_size;
181   GstBuffer *buffer = NULL;
182
183   pngdec = GST_PNGDEC (png_ptr->io_ptr);
184
185   GST_LOG ("info ready");
186
187   /* Generate the caps and configure */
188   ret = gst_pngdec_caps_create_and_set (pngdec);
189   if (ret != GST_FLOW_OK) {
190     goto beach;
191   }
192
193   /* Allocate output buffer */
194   pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
195   buffer_size = pngdec->height * GST_ROUND_UP_4 (pngdec->rowbytes);
196   ret =
197       gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
198       buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
199   if (ret != GST_FLOW_OK) {
200     goto beach;
201   }
202
203   pngdec->buffer_out = buffer;
204
205 beach:
206   pngdec->ret = ret;
207 }
208
209 static void
210 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
211     png_uint_32 row_num, int pass)
212 {
213   GstPngDec *pngdec = NULL;
214
215   pngdec = GST_PNGDEC (png_ptr->io_ptr);
216
217   /* FIXME: implement interlaced pictures */
218
219   if (GST_IS_BUFFER (pngdec->buffer_out)) {
220     size_t offset = row_num * GST_ROUND_UP_4 (pngdec->rowbytes);
221
222     GST_LOG ("got row %d, copying in buffer %p at offset %d", row_num,
223         pngdec->buffer_out, offset);
224     memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row,
225         pngdec->rowbytes);
226     pngdec->ret = GST_FLOW_OK;
227   } else {
228     GST_LOG ("we don't have any output buffer to write this row !");
229     pngdec->ret = GST_FLOW_ERROR;
230   }
231 }
232
233 static void
234 user_end_callback (png_structp png_ptr, png_infop info)
235 {
236   GstPngDec *pngdec = NULL;
237
238   pngdec = GST_PNGDEC (png_ptr->io_ptr);
239
240   GST_LOG_OBJECT (pngdec, "and we are done reading this image");
241
242   if (GST_CLOCK_TIME_IS_VALID (pngdec->in_timestamp))
243     GST_BUFFER_TIMESTAMP (pngdec->buffer_out) = pngdec->in_timestamp;
244   if (GST_CLOCK_TIME_IS_VALID (pngdec->in_duration))
245     GST_BUFFER_DURATION (pngdec->buffer_out) = pngdec->in_duration;
246
247   /* Push our buffer and then EOS if needed */
248   GST_LOG_OBJECT (pngdec, "pushing buffer with ts=%" GST_TIME_FORMAT,
249       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (pngdec->buffer_out)));
250
251   pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
252   pngdec->buffer_out = NULL;
253
254   if (pngdec->framed) {
255     /* Reset ourselves for the next frame */
256     gst_pngdec_libpng_clear (pngdec);
257     gst_pngdec_libpng_init (pngdec);
258     GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
259     png_set_progressive_read_fn (pngdec->png, pngdec,
260         user_info_callback, user_endrow_callback, user_end_callback);
261   } else {
262     GST_LOG_OBJECT (pngdec, "sending EOS");
263     pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
264   }
265 }
266
267 static void
268 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
269 {
270   GstPngDec *pngdec;
271   GstBuffer *buffer;
272   GstFlowReturn ret = GST_FLOW_OK;
273
274   pngdec = GST_PNGDEC (png_ptr->io_ptr);
275
276   GST_LOG ("reading %d bytes of data at offset %d", length, pngdec->offset);
277
278   ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
279   if ((ret != GST_FLOW_OK) || (GST_BUFFER_SIZE (buffer) != length))
280     goto pause;
281
282   memcpy (data, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
283
284   gst_buffer_unref (buffer);
285
286   pngdec->offset += length;
287
288   return;
289
290 pause:
291   GST_LOG_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret));
292   gst_pad_pause_task (pngdec->sinkpad);
293   if (GST_FLOW_IS_FATAL (ret)) {
294     gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
295     GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
296         (_("Internal data stream error.")),
297         ("stream stopped, reason %s", gst_flow_get_name (ret)));
298   }
299 }
300
301 static GstFlowReturn
302 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
303 {
304   GstFlowReturn ret = GST_FLOW_OK;
305   GstCaps *caps = NULL, *res = NULL;
306   GstPadTemplate *templ = NULL;
307   gint bpc = 0, color_type;
308   png_uint_32 width, height;
309
310   g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
311
312   /* Get bits per channel */
313   bpc = png_get_bit_depth (pngdec->png, pngdec->info);
314
315   /* We don't handle 16 bits per color, strip down to 8 */
316   if (bpc == 16) {
317     GST_LOG_OBJECT (pngdec,
318         "this is a 16 bits per channel PNG image, strip down to 8 bits");
319     png_set_strip_16 (pngdec->png);
320   }
321
322   /* Get Color type */
323   color_type = png_get_color_type (pngdec->png, pngdec->info);
324
325   /* HACK: The doc states that it's RGBA but apparently it's not... */
326   if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
327     png_set_bgr (pngdec->png);
328
329   /* Gray scale converted to RGB and upscaled to 8 bits */
330   if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
331       (color_type == PNG_COLOR_TYPE_GRAY)) {
332     GST_LOG_OBJECT (pngdec, "converting grayscale png to RGB");
333     png_set_gray_to_rgb (pngdec->png);
334     if (bpc < 8) {              /* Convert to 8 bits */
335       GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
336       png_set_gray_1_2_4_to_8 (pngdec->png);
337     }
338   }
339
340   /* Palette converted to RGB */
341   if (color_type == PNG_COLOR_TYPE_PALETTE) {
342     GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
343     png_set_palette_to_rgb (pngdec->png);
344   }
345
346   /* Update the info structure */
347   png_read_update_info (pngdec->png, pngdec->info);
348
349   /* Get IHDR header again after transformation settings */
350
351   png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
352       &bpc, &pngdec->color_type, NULL, NULL, NULL);
353
354   pngdec->width = width;
355   pngdec->height = height;
356
357   GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", pngdec->width,
358       pngdec->height);
359
360   switch (pngdec->color_type) {
361     case PNG_COLOR_TYPE_RGB:
362       GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
363       pngdec->bpp = 24;
364       break;
365     case PNG_COLOR_TYPE_RGB_ALPHA:
366       GST_LOG_OBJECT (pngdec, "we have an alpha channel, depth is 32 bits");
367       pngdec->bpp = 32;
368       break;
369     default:
370       GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
371           ("pngdec does not support this color type"));
372       ret = GST_FLOW_ERROR;
373       goto beach;
374   }
375
376   caps = gst_caps_new_simple ("video/x-raw-rgb",
377       "width", G_TYPE_INT, pngdec->width,
378       "height", G_TYPE_INT, pngdec->height,
379       "bpp", G_TYPE_INT, pngdec->bpp,
380       "framerate", GST_TYPE_FRACTION, pngdec->fps_n, pngdec->fps_d, NULL);
381
382   templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
383
384   res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
385
386   gst_caps_unref (caps);
387   gst_object_unref (templ);
388
389   if (!gst_pad_set_caps (pngdec->srcpad, res)) {
390     ret = GST_FLOW_ERROR;
391   }
392
393   GST_DEBUG_OBJECT (pngdec, "our caps %" GST_PTR_FORMAT, res);
394
395   gst_caps_unref (res);
396
397   /* Push a newsegment event */
398   if (pngdec->need_newsegment) {
399     gst_pad_push_event (pngdec->srcpad,
400         gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
401     pngdec->need_newsegment = FALSE;
402   }
403
404 beach:
405   return ret;
406 }
407
408 static void
409 gst_pngdec_task (GstPad * pad)
410 {
411   GstPngDec *pngdec;
412   GstBuffer *buffer = NULL;
413   size_t buffer_size = 0;
414   gint i = 0;
415   png_bytep *rows, inp;
416   png_uint_32 rowbytes;
417   GstFlowReturn ret = GST_FLOW_OK;
418
419   pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
420
421   /* Let libpng come back here on error */
422   if (setjmp (png_jmpbuf (pngdec->png))) {
423     ret = GST_FLOW_ERROR;
424     goto pause;
425   }
426
427   /* Set reading callback */
428   png_set_read_fn (pngdec->png, pngdec, user_read_data);
429
430   /* Read info */
431   png_read_info (pngdec->png, pngdec->info);
432
433   /* Generate the caps and configure */
434   ret = gst_pngdec_caps_create_and_set (pngdec);
435   if (ret != GST_FLOW_OK) {
436     goto pause;
437   }
438
439   /* Allocate output buffer */
440   rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
441   buffer_size = pngdec->height * GST_ROUND_UP_4 (rowbytes);
442   ret =
443       gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
444       buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
445   if (ret != GST_FLOW_OK) {
446     goto pause;
447   }
448
449   rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height);
450
451   inp = GST_BUFFER_DATA (buffer);
452
453   for (i = 0; i < pngdec->height; i++) {
454     rows[i] = inp;
455     inp += GST_ROUND_UP_4 (rowbytes);
456   }
457
458   /* Read the actual picture */
459   png_read_image (pngdec->png, rows);
460   free (rows);
461
462   /* Push the raw RGB frame */
463   ret = gst_pad_push (pngdec->srcpad, buffer);
464   if (ret != GST_FLOW_OK) {
465     goto pause;
466   }
467
468   /* And we are done */
469   gst_pad_pause_task (pngdec->sinkpad);
470   gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
471   return;
472
473 pause:
474   GST_LOG_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret));
475   gst_pad_pause_task (pngdec->sinkpad);
476   if (GST_FLOW_IS_FATAL (ret)) {
477     gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
478     GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
479         (_("Internal data stream error.")),
480         ("stream stopped, reason %s", gst_flow_get_name (ret)));
481   }
482 }
483
484 static GstFlowReturn
485 gst_pngdec_chain (GstPad * pad, GstBuffer * buffer)
486 {
487   GstPngDec *pngdec;
488   GstFlowReturn ret = GST_FLOW_OK;
489
490   pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
491
492   GST_LOG_OBJECT (pngdec, "Got buffer, size=%u", GST_BUFFER_SIZE (buffer));
493
494   if (!pngdec->setup) {
495     GST_LOG ("we are not configured yet");
496     ret = GST_FLOW_WRONG_STATE;
497     goto beach;
498   }
499
500   /* Something is going wrong in our callbacks */
501   if (pngdec->ret != GST_FLOW_OK) {
502     ret = pngdec->ret;
503     goto beach;
504   }
505
506   /* Let libpng come back here on error */
507   if (setjmp (png_jmpbuf (pngdec->png))) {
508     GST_WARNING ("error during decoding");
509     ret = GST_FLOW_ERROR;
510     goto beach;
511   }
512
513   pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
514   pngdec->in_duration = GST_BUFFER_DURATION (buffer);
515
516   /* Progressive loading of the PNG image */
517   png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer),
518       GST_BUFFER_SIZE (buffer));
519
520   /* And release the buffer */
521   gst_buffer_unref (buffer);
522
523 beach:
524   return ret;
525 }
526
527 static gboolean
528 gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps)
529 {
530   GstStructure *s;
531   GstPngDec *pngdec;
532   gint num, denom;
533
534   pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
535
536   s = gst_caps_get_structure (caps, 0);
537   if (gst_structure_get_fraction (s, "framerate", &num, &denom)) {
538     GST_DEBUG_OBJECT (pngdec, "framed input");
539     pngdec->framed = TRUE;
540     pngdec->fps_n = num;
541     pngdec->fps_d = denom;
542   } else {
543     pngdec->framed = FALSE;
544     pngdec->fps_n = 0;
545     pngdec->fps_d = 1;
546   }
547
548   gst_object_unref (pngdec);
549   return TRUE;
550 }
551
552 static gboolean
553 gst_pngdec_sink_event (GstPad * pad, GstEvent * event)
554 {
555   GstPngDec *pngdec;
556   gboolean res;
557
558   pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
559
560   switch (GST_EVENT_TYPE (event)) {
561     case GST_EVENT_NEWSEGMENT:{
562       GstFormat fmt;
563
564       gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL);
565       GST_LOG_OBJECT (pngdec, "NEWSEGMENT (%s)", gst_format_get_name (fmt));
566       if (fmt == GST_FORMAT_TIME) {
567         pngdec->need_newsegment = FALSE;
568         res = gst_pad_event_default (pad, event);
569       } else {
570         gst_event_unref (event);
571         res = TRUE;
572       }
573       break;
574     }
575     case GST_EVENT_EOS:
576       GST_LOG_OBJECT (pngdec, "EOS");
577       gst_pngdec_libpng_clear (pngdec);
578       res = gst_pad_event_default (pad, event);
579       break;
580     default:
581       res = gst_pad_event_default (pad, event);
582       break;
583   }
584
585   gst_object_unref (pngdec);
586   return res;
587 }
588
589
590 /* Clean up the libpng structures */
591 static gboolean
592 gst_pngdec_libpng_clear (GstPngDec * pngdec)
593 {
594   png_infopp info = NULL, endinfo = NULL;
595
596   g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
597
598   GST_LOG ("cleaning up libpng structures");
599
600   if (pngdec->info) {
601     info = &pngdec->info;
602   }
603
604   if (pngdec->endinfo) {
605     endinfo = &pngdec->endinfo;
606   }
607
608   if (pngdec->png) {
609     png_destroy_read_struct (&(pngdec->png), info, endinfo);
610     pngdec->png = NULL;
611     pngdec->info = NULL;
612     pngdec->endinfo = NULL;
613   }
614
615   pngdec->bpp = pngdec->color_type = pngdec->height = pngdec->width = -1;
616   pngdec->offset = 0;
617   pngdec->rowbytes = 0;
618   pngdec->buffer_out = NULL;
619
620   pngdec->setup = FALSE;
621
622   pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
623   pngdec->in_duration = GST_CLOCK_TIME_NONE;
624
625   return TRUE;
626 }
627
628 static gboolean
629 gst_pngdec_libpng_init (GstPngDec * pngdec)
630 {
631   g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
632
633   if (pngdec->setup) {
634     goto beach;
635   }
636
637   /* initialize png struct stuff */
638   pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
639       (png_voidp) NULL, user_error_fn, user_warning_fn);
640
641   if (pngdec->png == NULL) {
642     GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
643         ("Failed to initialize png structure"));
644     goto beach;
645   }
646
647   pngdec->info = png_create_info_struct (pngdec->png);
648   if (pngdec->info == NULL) {
649     gst_pngdec_libpng_clear (pngdec);
650     GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
651         ("Failed to initialize info structure"));
652     goto beach;
653   }
654
655   pngdec->endinfo = png_create_info_struct (pngdec->png);
656   if (pngdec->endinfo == NULL) {
657     gst_pngdec_libpng_clear (pngdec);
658     GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
659         ("Failed to initialize endinfo structure"));
660     goto beach;
661   }
662
663   pngdec->setup = TRUE;
664
665 beach:
666   return pngdec->setup;
667 }
668
669 static GstStateChangeReturn
670 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
671 {
672   GstStateChangeReturn ret;
673   GstPngDec *pngdec;
674
675   pngdec = GST_PNGDEC (element);
676
677   switch (transition) {
678     case GST_STATE_CHANGE_READY_TO_PAUSED:
679       gst_pngdec_libpng_init (pngdec);
680       pngdec->need_newsegment = TRUE;
681       pngdec->framed = FALSE;
682       break;
683     default:
684       break;
685   }
686
687   ret = parent_class->change_state (element, transition);
688   if (ret != GST_STATE_CHANGE_SUCCESS)
689     return ret;
690
691   switch (transition) {
692     case GST_STATE_CHANGE_PAUSED_TO_READY:
693       gst_pngdec_libpng_clear (pngdec);
694       break;
695     default:
696       break;
697   }
698
699   return ret;
700 }
701
702 /* this function gets called when we activate ourselves in push mode. */
703 static gboolean
704 gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active)
705 {
706   GstPngDec *pngdec;
707
708   pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
709
710   pngdec->ret = GST_FLOW_OK;
711
712   if (active) {
713     /* Let libpng come back here on error */
714     if (setjmp (png_jmpbuf (pngdec->png))) {
715       GST_LOG ("failed setting up libpng jumb");
716       gst_pngdec_libpng_clear (pngdec);
717       return FALSE;
718     }
719
720     GST_LOG ("setting up progressive loading callbacks");
721     png_set_progressive_read_fn (pngdec->png, pngdec,
722         user_info_callback, user_endrow_callback, user_end_callback);
723   }
724
725   return TRUE;
726 }
727
728 /* this function gets called when we activate ourselves in pull mode.
729  * We can perform  random access to the resource and we start a task
730  * to start reading */
731 static gboolean
732 gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
733 {
734   GstPngDec *pngdec;
735
736   pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
737
738   if (active) {
739     return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
740         sinkpad);
741   } else {
742     return gst_pad_stop_task (sinkpad);
743   }
744 }
745
746 /* this function is called when the pad is activated and should start
747  * processing data.
748  *
749  * We check if we can do random access to decide if we work push or
750  * pull based.
751  */
752 static gboolean
753 gst_pngdec_sink_activate (GstPad * sinkpad)
754 {
755   if (gst_pad_check_pull_range (sinkpad)) {
756     return gst_pad_activate_pull (sinkpad, TRUE);
757   } else {
758     return gst_pad_activate_push (sinkpad, TRUE);
759   }
760 }