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