2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
16 * SECTION:element-pngdec
18 * Decodes png images. If there is no framerate set on sink caps, it sends EOS
19 * after the first picture.
26 #include "gstpngdec.h"
30 #include <gst/video/video.h>
31 #include <gst/gst-i18n-plugin.h>
33 static const GstElementDetails gst_pngdec_details =
34 GST_ELEMENT_DETAILS ("PNG image decoder",
35 "Codec/Decoder/Image",
36 "Decode a png video frame to a raw image",
37 "Wim Taymans <wim@fluendo.com>");
39 GST_DEBUG_CATEGORY_STATIC (pngdec_debug);
40 #define GST_CAT_DEFAULT pngdec_debug
42 static void gst_pngdec_base_init (gpointer g_class);
43 static void gst_pngdec_class_init (GstPngDecClass * klass);
44 static void gst_pngdec_init (GstPngDec * pngdec);
46 static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
47 static gboolean gst_pngdec_libpng_clear (GstPngDec * pngdec);
49 static GstStateChangeReturn gst_pngdec_change_state (GstElement * element,
50 GstStateChange transition);
52 static gboolean gst_pngdec_sink_activate_push (GstPad * sinkpad,
54 static gboolean gst_pngdec_sink_activate_pull (GstPad * sinkpad,
56 static gboolean gst_pngdec_sink_activate (GstPad * sinkpad);
58 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
60 static void gst_pngdec_task (GstPad * pad);
61 static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstBuffer * buffer);
62 static gboolean gst_pngdec_sink_event (GstPad * pad, GstEvent * event);
63 static gboolean gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps);
65 static GstElementClass *parent_class = NULL;
68 gst_pngdec_get_type (void)
70 static GType pngdec_type = 0;
73 static const GTypeInfo pngdec_info = {
74 sizeof (GstPngDecClass),
77 (GClassInitFunc) gst_pngdec_class_init,
82 (GInstanceInitFunc) gst_pngdec_init,
85 pngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngDec",
91 static GstStaticPadTemplate gst_pngdec_src_pad_template =
92 GST_STATIC_PAD_TEMPLATE ("src",
95 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
98 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
99 GST_STATIC_PAD_TEMPLATE ("sink",
102 GST_STATIC_CAPS ("image/png")
106 gst_pngdec_base_init (gpointer g_class)
108 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&gst_pngdec_src_pad_template));
112 gst_element_class_add_pad_template (element_class,
113 gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
114 gst_element_class_set_details (element_class, &gst_pngdec_details);
118 gst_pngdec_class_init (GstPngDecClass * klass)
120 GstElementClass *gstelement_class;
122 gstelement_class = (GstElementClass *) klass;
124 parent_class = g_type_class_peek_parent (klass);
126 gstelement_class->change_state = gst_pngdec_change_state;
128 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
132 gst_pngdec_init (GstPngDec * pngdec)
135 gst_pad_new_from_static_template (&gst_pngdec_sink_pad_template, "sink");
136 gst_pad_set_activate_function (pngdec->sinkpad, gst_pngdec_sink_activate);
137 gst_pad_set_activatepush_function (pngdec->sinkpad,
138 gst_pngdec_sink_activate_push);
139 gst_pad_set_activatepull_function (pngdec->sinkpad,
140 gst_pngdec_sink_activate_pull);
141 gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
142 gst_pad_set_event_function (pngdec->sinkpad, gst_pngdec_sink_event);
143 gst_pad_set_setcaps_function (pngdec->sinkpad, gst_pngdec_sink_setcaps);
144 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
147 gst_pad_new_from_static_template (&gst_pngdec_src_pad_template, "src");
148 gst_pad_use_fixed_caps (pngdec->srcpad);
149 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
151 pngdec->buffer_out = NULL;
154 pngdec->endinfo = NULL;
155 pngdec->setup = FALSE;
157 pngdec->color_type = -1;
164 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
165 pngdec->in_duration = GST_CLOCK_TIME_NONE;
167 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
169 pngdec->image_ready = FALSE;
173 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
175 GST_ERROR ("%s", error_msg);
179 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
181 GST_WARNING ("%s", warning_msg);
185 user_info_callback (png_structp png_ptr, png_infop info)
187 GstPngDec *pngdec = NULL;
188 GstFlowReturn ret = GST_FLOW_OK;
190 GstBuffer *buffer = NULL;
192 pngdec = GST_PNGDEC (png_ptr->io_ptr);
194 GST_LOG ("info ready");
196 /* Generate the caps and configure */
197 ret = gst_pngdec_caps_create_and_set (pngdec);
198 if (ret != GST_FLOW_OK) {
202 /* Allocate output buffer */
203 pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
204 buffer_size = pngdec->height * GST_ROUND_UP_4 (pngdec->rowbytes);
206 gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
207 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
208 if (ret != GST_FLOW_OK) {
212 pngdec->buffer_out = buffer;
219 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
220 png_uint_32 row_num, int pass)
222 GstPngDec *pngdec = NULL;
224 pngdec = GST_PNGDEC (png_ptr->io_ptr);
226 /* FIXME: implement interlaced pictures */
228 /* If buffer_out doesn't exist, it means buffer_alloc failed, which
229 * will already have set the return code */
230 if (GST_IS_BUFFER (pngdec->buffer_out)) {
231 size_t offset = row_num * GST_ROUND_UP_4 (pngdec->rowbytes);
233 GST_LOG ("got row %u, copying in buffer %p at offset %" G_GSIZE_FORMAT,
234 (guint) row_num, pngdec->buffer_out, offset);
235 memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row,
237 pngdec->ret = GST_FLOW_OK;
242 buffer_clip (GstPngDec * dec, GstBuffer * buffer)
245 gint64 cstart, cstop;
248 if ((!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) ||
249 (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) ||
250 (dec->segment.format != GST_FORMAT_TIME))
253 cstart = GST_BUFFER_TIMESTAMP (buffer);
254 cstop = GST_BUFFER_DURATION (buffer);
256 if ((res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
257 cstart, cstart + cstop, &cstart, &cstop))) {
258 GST_BUFFER_TIMESTAMP (buffer) = cstart;
259 GST_BUFFER_DURATION (buffer) = cstop - cstart;
267 user_end_callback (png_structp png_ptr, png_infop info)
269 GstPngDec *pngdec = NULL;
271 pngdec = GST_PNGDEC (png_ptr->io_ptr);
273 GST_LOG_OBJECT (pngdec, "and we are done reading this image");
275 if (!pngdec->buffer_out)
278 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_timestamp))
279 GST_BUFFER_TIMESTAMP (pngdec->buffer_out) = pngdec->in_timestamp;
280 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_duration))
281 GST_BUFFER_DURATION (pngdec->buffer_out) = pngdec->in_duration;
283 /* buffer clipping */
284 if (buffer_clip (pngdec, pngdec->buffer_out)) {
285 /* Push our buffer and then EOS if needed */
286 GST_LOG_OBJECT (pngdec, "pushing buffer with ts=%" GST_TIME_FORMAT,
287 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (pngdec->buffer_out)));
289 pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
291 GST_LOG_OBJECT (pngdec, "dropped decoded buffer");
292 gst_buffer_unref (pngdec->buffer_out);
294 pngdec->buffer_out = NULL;
295 pngdec->image_ready = TRUE;
299 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
303 GstFlowReturn ret = GST_FLOW_OK;
306 pngdec = GST_PNGDEC (png_ptr->io_ptr);
308 GST_LOG ("reading %" G_GSIZE_FORMAT " bytes of data at offset %d", length,
311 ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
312 if (ret != GST_FLOW_OK)
315 size = GST_BUFFER_SIZE (buffer);
320 memcpy (data, GST_BUFFER_DATA (buffer), size);
322 gst_buffer_unref (buffer);
324 pngdec->offset += length;
331 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
332 gst_flow_get_name (ret));
333 gst_pad_pause_task (pngdec->sinkpad);
334 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
335 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
336 (_("Internal data stream error.")),
337 ("stream stopped, reason %s", gst_flow_get_name (ret)));
338 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
344 gst_buffer_unref (buffer);
345 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
346 (_("Internal data stream error.")),
347 ("Read %u, needed %u bytes", size, length));
348 ret = GST_FLOW_ERROR;
354 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
356 GstFlowReturn ret = GST_FLOW_OK;
357 GstCaps *caps = NULL, *res = NULL;
358 GstPadTemplate *templ = NULL;
359 gint bpc = 0, color_type;
360 png_uint_32 width, height;
362 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
364 /* Get bits per channel */
365 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
367 /* We don't handle 16 bits per color, strip down to 8 */
369 GST_LOG_OBJECT (pngdec,
370 "this is a 16 bits per channel PNG image, strip down to 8 bits");
371 png_set_strip_16 (pngdec->png);
375 color_type = png_get_color_type (pngdec->png, pngdec->info);
378 /* We used to have this HACK to reverse the outgoing bytes, but the problem
379 * that originally required the hack seems to have been in ffmpegcolorspace's
380 * RGBA descriptions. It doesn't seem needed now that's fixed, but might
381 * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
382 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
383 png_set_bgr (pngdec->png);
386 /* Gray scale converted to RGB and upscaled to 8 bits */
387 if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
388 (color_type == PNG_COLOR_TYPE_GRAY)) {
389 GST_LOG_OBJECT (pngdec, "converting grayscale png to RGB");
390 png_set_gray_to_rgb (pngdec->png);
391 if (bpc < 8) { /* Convert to 8 bits */
392 GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
393 png_set_gray_1_2_4_to_8 (pngdec->png);
397 /* Palette converted to RGB */
398 if (color_type == PNG_COLOR_TYPE_PALETTE) {
399 GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
400 png_set_palette_to_rgb (pngdec->png);
403 /* Update the info structure */
404 png_read_update_info (pngdec->png, pngdec->info);
406 /* Get IHDR header again after transformation settings */
408 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
409 &bpc, &pngdec->color_type, NULL, NULL, NULL);
411 pngdec->width = width;
412 pngdec->height = height;
414 GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", pngdec->width,
417 switch (pngdec->color_type) {
418 case PNG_COLOR_TYPE_RGB:
419 GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
422 case PNG_COLOR_TYPE_RGB_ALPHA:
423 GST_LOG_OBJECT (pngdec, "we have an alpha channel, depth is 32 bits");
427 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
428 ("pngdec does not support this color type"));
429 ret = GST_FLOW_NOT_SUPPORTED;
433 caps = gst_caps_new_simple ("video/x-raw-rgb",
434 "width", G_TYPE_INT, pngdec->width,
435 "height", G_TYPE_INT, pngdec->height,
436 "bpp", G_TYPE_INT, pngdec->bpp,
437 "framerate", GST_TYPE_FRACTION, pngdec->fps_n, pngdec->fps_d, NULL);
439 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
441 res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
443 gst_caps_unref (caps);
444 gst_object_unref (templ);
446 if (!gst_pad_set_caps (pngdec->srcpad, res))
447 ret = GST_FLOW_NOT_NEGOTIATED;
449 GST_DEBUG_OBJECT (pngdec, "our caps %" GST_PTR_FORMAT, res);
451 gst_caps_unref (res);
453 /* Push a newsegment event */
454 if (pngdec->need_newsegment) {
455 gst_pad_push_event (pngdec->srcpad,
456 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
457 pngdec->need_newsegment = FALSE;
465 gst_pngdec_task (GstPad * pad)
468 GstBuffer *buffer = NULL;
469 size_t buffer_size = 0;
471 png_bytep *rows, inp;
472 png_uint_32 rowbytes;
473 GstFlowReturn ret = GST_FLOW_OK;
475 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
477 GST_LOG_OBJECT (pngdec, "read frame");
479 /* Let libpng come back here on error */
480 if (setjmp (png_jmpbuf (pngdec->png))) {
481 ret = GST_FLOW_ERROR;
485 /* Set reading callback */
486 png_set_read_fn (pngdec->png, pngdec, user_read_data);
489 png_read_info (pngdec->png, pngdec->info);
491 /* Generate the caps and configure */
492 ret = gst_pngdec_caps_create_and_set (pngdec);
493 if (ret != GST_FLOW_OK) {
497 /* Allocate output buffer */
498 rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
499 buffer_size = pngdec->height * GST_ROUND_UP_4 (rowbytes);
501 gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
502 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
503 if (ret != GST_FLOW_OK)
506 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height);
508 inp = GST_BUFFER_DATA (buffer);
510 for (i = 0; i < pngdec->height; i++) {
512 inp += GST_ROUND_UP_4 (rowbytes);
515 /* Read the actual picture */
516 png_read_image (pngdec->png, rows);
519 /* Push the raw RGB frame */
520 ret = gst_pad_push (pngdec->srcpad, buffer);
521 if (ret != GST_FLOW_OK)
524 /* And we are done */
525 gst_pad_pause_task (pngdec->sinkpad);
526 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
531 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
532 gst_flow_get_name (ret));
533 gst_pad_pause_task (pngdec->sinkpad);
534 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
535 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
536 (_("Internal data stream error.")),
537 ("stream stopped, reason %s", gst_flow_get_name (ret)));
538 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
544 gst_pngdec_chain (GstPad * pad, GstBuffer * buffer)
547 GstFlowReturn ret = GST_FLOW_OK;
549 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
551 GST_LOG_OBJECT (pngdec, "Got buffer, size=%u", GST_BUFFER_SIZE (buffer));
553 if (G_UNLIKELY (!pngdec->setup))
556 /* Something is going wrong in our callbacks */
558 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
559 GST_WARNING_OBJECT (pngdec, "we have a pending return code of %d", ret);
563 /* Let libpng come back here on error */
564 if (setjmp (png_jmpbuf (pngdec->png))) {
565 GST_WARNING_OBJECT (pngdec, "error during decoding");
566 ret = GST_FLOW_ERROR;
570 pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
571 pngdec->in_duration = GST_BUFFER_DURATION (buffer);
573 /* Progressive loading of the PNG image */
574 png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer),
575 GST_BUFFER_SIZE (buffer));
577 if (pngdec->image_ready) {
578 if (pngdec->framed) {
579 /* Reset ourselves for the next frame */
580 gst_pngdec_libpng_clear (pngdec);
581 gst_pngdec_libpng_init (pngdec);
582 GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
583 png_set_progressive_read_fn (pngdec->png, pngdec,
584 user_info_callback, user_endrow_callback, user_end_callback);
586 GST_LOG_OBJECT (pngdec, "sending EOS");
587 pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
589 pngdec->image_ready = FALSE;
592 /* grab new return code */
595 /* And release the buffer */
596 gst_buffer_unref (buffer);
599 gst_object_unref (pngdec);
606 GST_LOG_OBJECT (pngdec, "we are not configured yet");
607 ret = GST_FLOW_WRONG_STATE;
613 gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps)
619 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
621 s = gst_caps_get_structure (caps, 0);
622 if (gst_structure_get_fraction (s, "framerate", &num, &denom)) {
623 GST_DEBUG_OBJECT (pngdec, "framed input");
624 pngdec->framed = TRUE;
626 pngdec->fps_d = denom;
628 GST_DEBUG_OBJECT (pngdec, "single picture input");
629 pngdec->framed = FALSE;
634 gst_object_unref (pngdec);
639 gst_pngdec_sink_event (GstPad * pad, GstEvent * event)
644 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
646 switch (GST_EVENT_TYPE (event)) {
647 case GST_EVENT_NEWSEGMENT:{
650 gint64 start, stop, position;
653 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt,
654 &start, &stop, &position);
656 gst_segment_set_newsegment_full (&pngdec->segment, update, rate, arate,
657 fmt, start, stop, position);
659 GST_LOG_OBJECT (pngdec, "NEWSEGMENT (%s)", gst_format_get_name (fmt));
661 if (fmt == GST_FORMAT_TIME) {
662 pngdec->need_newsegment = FALSE;
663 res = gst_pad_push_event (pngdec->srcpad, event);
665 gst_event_unref (event);
670 case GST_EVENT_FLUSH_STOP:
672 gst_pngdec_libpng_clear (pngdec);
673 gst_pngdec_libpng_init (pngdec);
674 png_set_progressive_read_fn (pngdec->png, pngdec,
675 user_info_callback, user_endrow_callback, user_end_callback);
676 pngdec->ret = GST_FLOW_OK;
677 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
678 res = gst_pad_push_event (pngdec->srcpad, event);
683 GST_LOG_OBJECT (pngdec, "EOS");
684 gst_pngdec_libpng_clear (pngdec);
685 pngdec->ret = GST_FLOW_UNEXPECTED;
686 res = gst_pad_push_event (pngdec->srcpad, event);
690 res = gst_pad_push_event (pngdec->srcpad, event);
694 gst_object_unref (pngdec);
699 /* Clean up the libpng structures */
701 gst_pngdec_libpng_clear (GstPngDec * pngdec)
703 png_infopp info = NULL, endinfo = NULL;
705 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
707 GST_LOG ("cleaning up libpng structures");
710 info = &pngdec->info;
713 if (pngdec->endinfo) {
714 endinfo = &pngdec->endinfo;
718 png_destroy_read_struct (&(pngdec->png), info, endinfo);
721 pngdec->endinfo = NULL;
724 pngdec->bpp = pngdec->color_type = pngdec->height = pngdec->width = -1;
726 pngdec->rowbytes = 0;
727 pngdec->buffer_out = NULL;
729 pngdec->setup = FALSE;
731 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
732 pngdec->in_duration = GST_CLOCK_TIME_NONE;
738 gst_pngdec_libpng_init (GstPngDec * pngdec)
740 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
745 GST_LOG ("init libpng structures");
747 /* initialize png struct stuff */
748 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
749 (png_voidp) NULL, user_error_fn, user_warning_fn);
751 if (pngdec->png == NULL)
754 pngdec->info = png_create_info_struct (pngdec->png);
755 if (pngdec->info == NULL)
758 pngdec->endinfo = png_create_info_struct (pngdec->png);
759 if (pngdec->endinfo == NULL)
762 pngdec->setup = TRUE;
769 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
770 ("Failed to initialize png structure"));
775 gst_pngdec_libpng_clear (pngdec);
776 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
777 ("Failed to initialize info structure"));
782 gst_pngdec_libpng_clear (pngdec);
783 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
784 ("Failed to initialize endinfo structure"));
789 static GstStateChangeReturn
790 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
792 GstStateChangeReturn ret;
795 pngdec = GST_PNGDEC (element);
797 switch (transition) {
798 case GST_STATE_CHANGE_READY_TO_PAUSED:
799 gst_pngdec_libpng_init (pngdec);
800 pngdec->need_newsegment = TRUE;
801 pngdec->framed = FALSE;
802 pngdec->ret = GST_FLOW_OK;
803 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
809 ret = parent_class->change_state (element, transition);
810 if (ret != GST_STATE_CHANGE_SUCCESS)
813 switch (transition) {
814 case GST_STATE_CHANGE_PAUSED_TO_READY:
815 gst_pngdec_libpng_clear (pngdec);
824 /* this function gets called when we activate ourselves in push mode. */
826 gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active)
830 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
832 pngdec->ret = GST_FLOW_OK;
835 /* Let libpng come back here on error */
836 if (setjmp (png_jmpbuf (pngdec->png)))
839 GST_LOG ("setting up progressive loading callbacks");
840 png_set_progressive_read_fn (pngdec->png, pngdec,
841 user_info_callback, user_endrow_callback, user_end_callback);
847 GST_LOG ("failed setting up libpng jmpbuf");
848 gst_pngdec_libpng_clear (pngdec);
853 /* this function gets called when we activate ourselves in pull mode.
854 * We can perform random access to the resource and we start a task
855 * to start reading */
857 gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
861 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
864 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
867 return gst_pad_stop_task (sinkpad);
871 /* this function is called when the pad is activated and should start
874 * We check if we can do random access to decide if we work push or
878 gst_pngdec_sink_activate (GstPad * sinkpad)
880 if (gst_pad_check_pull_range (sinkpad)) {
881 return gst_pad_activate_pull (sinkpad, TRUE);
883 return gst_pad_activate_push (sinkpad, TRUE);