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 GST_DEBUG_CATEGORY_STATIC (pngdec_debug);
34 #define GST_CAT_DEFAULT pngdec_debug
36 static void gst_pngdec_base_init (gpointer g_class);
37 static void gst_pngdec_class_init (GstPngDecClass * klass);
38 static void gst_pngdec_init (GstPngDec * pngdec);
40 static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
41 static gboolean gst_pngdec_libpng_clear (GstPngDec * pngdec);
43 static GstStateChangeReturn gst_pngdec_change_state (GstElement * element,
44 GstStateChange transition);
46 static gboolean gst_pngdec_sink_activate_push (GstPad * sinkpad,
48 static gboolean gst_pngdec_sink_activate_pull (GstPad * sinkpad,
50 static gboolean gst_pngdec_sink_activate (GstPad * sinkpad);
52 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
54 static void gst_pngdec_task (GstPad * pad);
55 static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstBuffer * buffer);
56 static gboolean gst_pngdec_sink_event (GstPad * pad, GstEvent * event);
57 static gboolean gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps);
59 static GstElementClass *parent_class = NULL;
62 gst_pngdec_get_type (void)
64 static GType pngdec_type = 0;
67 static const GTypeInfo pngdec_info = {
68 sizeof (GstPngDecClass),
71 (GClassInitFunc) gst_pngdec_class_init,
76 (GInstanceInitFunc) gst_pngdec_init,
79 pngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngDec",
85 static GstStaticPadTemplate gst_pngdec_src_pad_template =
86 GST_STATIC_PAD_TEMPLATE ("src",
89 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB ";"
90 GST_VIDEO_CAPS_ARGB_64)
93 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
94 GST_STATIC_PAD_TEMPLATE ("sink",
97 GST_STATIC_CAPS ("image/png")
101 gst_pngdec_base_init (gpointer g_class)
103 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
105 gst_element_class_add_static_pad_template (element_class,
106 &gst_pngdec_src_pad_template);
107 gst_element_class_add_static_pad_template (element_class,
108 &gst_pngdec_sink_pad_template);
109 gst_element_class_set_details_simple (element_class, "PNG image decoder",
110 "Codec/Decoder/Image",
111 "Decode a png video frame to a raw image",
112 "Wim Taymans <wim@fluendo.com>");
116 gst_pngdec_class_init (GstPngDecClass * klass)
118 GstElementClass *gstelement_class;
120 gstelement_class = (GstElementClass *) klass;
122 parent_class = g_type_class_peek_parent (klass);
124 gstelement_class->change_state = gst_pngdec_change_state;
126 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
130 gst_pngdec_init (GstPngDec * pngdec)
133 gst_pad_new_from_static_template (&gst_pngdec_sink_pad_template, "sink");
134 gst_pad_set_activate_function (pngdec->sinkpad, gst_pngdec_sink_activate);
135 gst_pad_set_activatepush_function (pngdec->sinkpad,
136 gst_pngdec_sink_activate_push);
137 gst_pad_set_activatepull_function (pngdec->sinkpad,
138 gst_pngdec_sink_activate_pull);
139 gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
140 gst_pad_set_event_function (pngdec->sinkpad, gst_pngdec_sink_event);
141 gst_pad_set_setcaps_function (pngdec->sinkpad, gst_pngdec_sink_setcaps);
142 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
145 gst_pad_new_from_static_template (&gst_pngdec_src_pad_template, "src");
146 gst_pad_use_fixed_caps (pngdec->srcpad);
147 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
149 pngdec->buffer_out = NULL;
152 pngdec->endinfo = NULL;
153 pngdec->setup = FALSE;
155 pngdec->color_type = -1;
162 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
163 pngdec->in_duration = GST_CLOCK_TIME_NONE;
165 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
167 pngdec->image_ready = FALSE;
171 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
173 GST_ERROR ("%s", error_msg);
177 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
179 GST_WARNING ("%s", warning_msg);
183 user_info_callback (png_structp png_ptr, png_infop info)
185 GstPngDec *pngdec = NULL;
186 GstFlowReturn ret = GST_FLOW_OK;
188 GstBuffer *buffer = NULL;
190 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
192 GST_LOG ("info ready");
194 /* Generate the caps and configure */
195 ret = gst_pngdec_caps_create_and_set (pngdec);
196 if (ret != GST_FLOW_OK) {
200 /* Allocate output buffer */
201 pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
202 if (pngdec->rowbytes > (G_MAXUINT32 - 3)
203 || pngdec->height > G_MAXUINT32 / pngdec->rowbytes) {
204 ret = GST_FLOW_ERROR;
207 pngdec->rowbytes = GST_ROUND_UP_4 (pngdec->rowbytes);
208 buffer_size = pngdec->height * pngdec->rowbytes;
211 gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
212 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
213 if (ret != GST_FLOW_OK) {
217 pngdec->buffer_out = buffer;
224 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
225 png_uint_32 row_num, int pass)
227 GstPngDec *pngdec = NULL;
229 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
231 /* FIXME: implement interlaced pictures */
233 /* If buffer_out doesn't exist, it means buffer_alloc failed, which
234 * will already have set the return code */
235 if (GST_IS_BUFFER (pngdec->buffer_out)) {
236 size_t offset = row_num * pngdec->rowbytes;
238 GST_LOG ("got row %u, copying in buffer %p at offset %" G_GSIZE_FORMAT,
239 (guint) row_num, pngdec->buffer_out, offset);
240 memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row,
242 pngdec->ret = GST_FLOW_OK;
247 buffer_clip (GstPngDec * dec, GstBuffer * buffer)
250 gint64 cstart, cstop;
253 if ((!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) ||
254 (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) ||
255 (dec->segment.format != GST_FORMAT_TIME))
258 cstart = GST_BUFFER_TIMESTAMP (buffer);
259 cstop = GST_BUFFER_DURATION (buffer);
261 if ((res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
262 cstart, cstart + cstop, &cstart, &cstop))) {
263 GST_BUFFER_TIMESTAMP (buffer) = cstart;
264 GST_BUFFER_DURATION (buffer) = cstop - cstart;
272 user_end_callback (png_structp png_ptr, png_infop info)
274 GstPngDec *pngdec = NULL;
276 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
278 GST_LOG_OBJECT (pngdec, "and we are done reading this image");
280 if (!pngdec->buffer_out)
283 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_timestamp))
284 GST_BUFFER_TIMESTAMP (pngdec->buffer_out) = pngdec->in_timestamp;
285 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_duration))
286 GST_BUFFER_DURATION (pngdec->buffer_out) = pngdec->in_duration;
288 /* buffer clipping */
289 if (buffer_clip (pngdec, pngdec->buffer_out)) {
290 /* Push our buffer and then EOS if needed */
291 GST_LOG_OBJECT (pngdec, "pushing buffer with ts=%" GST_TIME_FORMAT,
292 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (pngdec->buffer_out)));
294 pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
296 GST_LOG_OBJECT (pngdec, "dropped decoded buffer");
297 gst_buffer_unref (pngdec->buffer_out);
299 pngdec->buffer_out = NULL;
300 pngdec->image_ready = TRUE;
304 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
308 GstFlowReturn ret = GST_FLOW_OK;
311 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
313 GST_LOG ("reading %" G_GSIZE_FORMAT " bytes of data at offset %d", length,
316 ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
317 if (ret != GST_FLOW_OK)
320 size = GST_BUFFER_SIZE (buffer);
325 memcpy (data, GST_BUFFER_DATA (buffer), size);
327 gst_buffer_unref (buffer);
329 pngdec->offset += length;
336 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
337 gst_flow_get_name (ret));
338 gst_pad_pause_task (pngdec->sinkpad);
339 if (ret == GST_FLOW_UNEXPECTED) {
340 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
341 } else if (ret < GST_FLOW_UNEXPECTED || ret == GST_FLOW_NOT_LINKED) {
342 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
343 (_("Internal data stream error.")),
344 ("stream stopped, reason %s", gst_flow_get_name (ret)));
345 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
347 png_error (png_ptr, "Internal data stream error.");
352 gst_buffer_unref (buffer);
353 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
354 (_("Internal data stream error.")),
355 ("Read %u, needed %" G_GSIZE_FORMAT "bytes", size, length));
356 ret = GST_FLOW_ERROR;
362 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
364 GstFlowReturn ret = GST_FLOW_OK;
365 GstCaps *caps = NULL, *res = NULL;
366 GstPadTemplate *templ = NULL;
367 gint bpc = 0, color_type;
368 png_uint_32 width, height;
370 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
372 /* Get bits per channel */
373 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
375 /* Add alpha channel if 16-bit depth */
376 png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
377 png_set_swap (pngdec->png);
381 color_type = png_get_color_type (pngdec->png, pngdec->info);
384 /* We used to have this HACK to reverse the outgoing bytes, but the problem
385 * that originally required the hack seems to have been in ffmpegcolorspace's
386 * RGBA descriptions. It doesn't seem needed now that's fixed, but might
387 * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
388 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
389 png_set_bgr (pngdec->png);
392 /* Gray scale converted to RGB and upscaled to 8 bits */
393 if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
394 (color_type == PNG_COLOR_TYPE_GRAY)) {
395 GST_LOG_OBJECT (pngdec, "converting grayscale png to RGB");
396 png_set_gray_to_rgb (pngdec->png);
397 if (bpc < 8) { /* Convert to 8 bits */
398 GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
399 #if PNG_LIBPNG_VER < 10400
400 png_set_gray_1_2_4_to_8 (pngdec->png);
402 png_set_expand_gray_1_2_4_to_8 (pngdec->png);
407 /* Palette converted to RGB */
408 if (color_type == PNG_COLOR_TYPE_PALETTE) {
409 GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
410 png_set_palette_to_rgb (pngdec->png);
413 /* Update the info structure */
414 png_read_update_info (pngdec->png, pngdec->info);
416 /* Get IHDR header again after transformation settings */
418 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
419 &bpc, &pngdec->color_type, NULL, NULL, NULL);
421 pngdec->width = width;
422 pngdec->height = height;
424 GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", pngdec->width,
427 switch (pngdec->color_type) {
428 case PNG_COLOR_TYPE_RGB:
429 GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
430 pngdec->bpp = 3 * bpc;
432 case PNG_COLOR_TYPE_RGB_ALPHA:
433 GST_LOG_OBJECT (pngdec, "we have an alpha channel, depth is 32 bits");
434 pngdec->bpp = 4 * bpc;
437 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
438 ("pngdec does not support this color type"));
439 ret = GST_FLOW_NOT_SUPPORTED;
443 caps = gst_caps_new_simple ("video/x-raw-rgb",
444 "width", G_TYPE_INT, pngdec->width,
445 "height", G_TYPE_INT, pngdec->height,
446 "bpp", G_TYPE_INT, pngdec->bpp,
447 "framerate", GST_TYPE_FRACTION, pngdec->fps_n, pngdec->fps_d, NULL);
449 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
451 res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
453 gst_caps_unref (caps);
454 gst_object_unref (templ);
456 if (!gst_pad_set_caps (pngdec->srcpad, res))
457 ret = GST_FLOW_NOT_NEGOTIATED;
459 GST_DEBUG_OBJECT (pngdec, "our caps %" GST_PTR_FORMAT, res);
461 gst_caps_unref (res);
463 /* Push a newsegment event */
464 if (pngdec->need_newsegment) {
465 gst_pad_push_event (pngdec->srcpad,
466 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
467 pngdec->need_newsegment = FALSE;
475 gst_pngdec_task (GstPad * pad)
478 GstBuffer *buffer = NULL;
479 size_t buffer_size = 0;
481 png_bytep *rows, inp;
482 png_uint_32 rowbytes;
483 GstFlowReturn ret = GST_FLOW_OK;
485 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
487 GST_LOG_OBJECT (pngdec, "read frame");
489 /* Let libpng come back here on error */
490 if (setjmp (png_jmpbuf (pngdec->png))) {
491 ret = GST_FLOW_ERROR;
495 /* Set reading callback */
496 png_set_read_fn (pngdec->png, pngdec, user_read_data);
499 png_read_info (pngdec->png, pngdec->info);
501 /* Generate the caps and configure */
502 ret = gst_pngdec_caps_create_and_set (pngdec);
503 if (ret != GST_FLOW_OK) {
507 /* Allocate output buffer */
508 rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
509 if (rowbytes > (G_MAXUINT32 - 3) || pngdec->height > G_MAXUINT32 / rowbytes) {
510 ret = GST_FLOW_ERROR;
513 rowbytes = GST_ROUND_UP_4 (rowbytes);
514 buffer_size = pngdec->height * rowbytes;
516 gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
517 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
518 if (ret != GST_FLOW_OK)
521 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height);
523 inp = GST_BUFFER_DATA (buffer);
525 for (i = 0; i < pngdec->height; i++) {
530 /* Read the actual picture */
531 png_read_image (pngdec->png, rows);
534 /* Push the raw RGB frame */
535 ret = gst_pad_push (pngdec->srcpad, buffer);
536 if (ret != GST_FLOW_OK)
539 /* And we are done */
540 gst_pad_pause_task (pngdec->sinkpad);
541 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
546 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
547 gst_flow_get_name (ret));
548 gst_pad_pause_task (pngdec->sinkpad);
549 if (ret == GST_FLOW_UNEXPECTED) {
550 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
551 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
552 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
553 (_("Internal data stream error.")),
554 ("stream stopped, reason %s", gst_flow_get_name (ret)));
555 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
561 gst_pngdec_chain (GstPad * pad, GstBuffer * buffer)
564 GstFlowReturn ret = GST_FLOW_OK;
566 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
568 GST_LOG_OBJECT (pngdec, "Got buffer, size=%u", GST_BUFFER_SIZE (buffer));
570 if (G_UNLIKELY (!pngdec->setup))
573 /* Something is going wrong in our callbacks */
575 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
576 GST_WARNING_OBJECT (pngdec, "we have a pending return code of %d", ret);
580 /* Let libpng come back here on error */
581 if (setjmp (png_jmpbuf (pngdec->png))) {
582 GST_WARNING_OBJECT (pngdec, "error during decoding");
583 ret = GST_FLOW_ERROR;
587 pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
588 pngdec->in_duration = GST_BUFFER_DURATION (buffer);
590 /* Progressive loading of the PNG image */
591 png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer),
592 GST_BUFFER_SIZE (buffer));
594 if (pngdec->image_ready) {
595 if (pngdec->framed) {
596 /* Reset ourselves for the next frame */
597 gst_pngdec_libpng_clear (pngdec);
598 gst_pngdec_libpng_init (pngdec);
599 GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
600 png_set_progressive_read_fn (pngdec->png, pngdec,
601 user_info_callback, user_endrow_callback, user_end_callback);
603 GST_LOG_OBJECT (pngdec, "sending EOS");
604 pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
606 pngdec->image_ready = FALSE;
609 /* grab new return code */
612 /* And release the buffer */
613 gst_buffer_unref (buffer);
616 gst_object_unref (pngdec);
623 GST_LOG_OBJECT (pngdec, "we are not configured yet");
624 ret = GST_FLOW_WRONG_STATE;
630 gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps)
636 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
638 s = gst_caps_get_structure (caps, 0);
639 if (gst_structure_get_fraction (s, "framerate", &num, &denom)) {
640 GST_DEBUG_OBJECT (pngdec, "framed input");
641 pngdec->framed = TRUE;
643 pngdec->fps_d = denom;
645 GST_DEBUG_OBJECT (pngdec, "single picture input");
646 pngdec->framed = FALSE;
651 gst_object_unref (pngdec);
656 gst_pngdec_sink_event (GstPad * pad, GstEvent * event)
661 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
663 switch (GST_EVENT_TYPE (event)) {
664 case GST_EVENT_NEWSEGMENT:{
667 gint64 start, stop, position;
670 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt,
671 &start, &stop, &position);
673 gst_segment_set_newsegment_full (&pngdec->segment, update, rate, arate,
674 fmt, start, stop, position);
676 GST_LOG_OBJECT (pngdec, "NEWSEGMENT (%s)", gst_format_get_name (fmt));
678 if (fmt == GST_FORMAT_TIME) {
679 pngdec->need_newsegment = FALSE;
680 res = gst_pad_push_event (pngdec->srcpad, event);
682 gst_event_unref (event);
687 case GST_EVENT_FLUSH_STOP:
689 gst_pngdec_libpng_clear (pngdec);
690 gst_pngdec_libpng_init (pngdec);
691 png_set_progressive_read_fn (pngdec->png, pngdec,
692 user_info_callback, user_endrow_callback, user_end_callback);
693 pngdec->ret = GST_FLOW_OK;
694 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
695 res = gst_pad_push_event (pngdec->srcpad, event);
700 GST_LOG_OBJECT (pngdec, "EOS");
701 gst_pngdec_libpng_clear (pngdec);
702 pngdec->ret = GST_FLOW_UNEXPECTED;
703 res = gst_pad_push_event (pngdec->srcpad, event);
707 res = gst_pad_push_event (pngdec->srcpad, event);
711 gst_object_unref (pngdec);
716 /* Clean up the libpng structures */
718 gst_pngdec_libpng_clear (GstPngDec * pngdec)
720 png_infopp info = NULL, endinfo = NULL;
722 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
724 GST_LOG ("cleaning up libpng structures");
727 info = &pngdec->info;
730 if (pngdec->endinfo) {
731 endinfo = &pngdec->endinfo;
735 png_destroy_read_struct (&(pngdec->png), info, endinfo);
738 pngdec->endinfo = NULL;
741 pngdec->bpp = pngdec->color_type = pngdec->height = pngdec->width = -1;
743 pngdec->rowbytes = 0;
744 pngdec->buffer_out = NULL;
746 pngdec->setup = FALSE;
748 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
749 pngdec->in_duration = GST_CLOCK_TIME_NONE;
755 gst_pngdec_libpng_init (GstPngDec * pngdec)
757 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
762 GST_LOG ("init libpng structures");
764 /* initialize png struct stuff */
765 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
766 (png_voidp) NULL, user_error_fn, user_warning_fn);
768 if (pngdec->png == NULL)
771 pngdec->info = png_create_info_struct (pngdec->png);
772 if (pngdec->info == NULL)
775 pngdec->endinfo = png_create_info_struct (pngdec->png);
776 if (pngdec->endinfo == NULL)
779 pngdec->setup = TRUE;
786 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
787 ("Failed to initialize png structure"));
792 gst_pngdec_libpng_clear (pngdec);
793 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
794 ("Failed to initialize info structure"));
799 gst_pngdec_libpng_clear (pngdec);
800 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
801 ("Failed to initialize endinfo structure"));
806 static GstStateChangeReturn
807 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
809 GstStateChangeReturn ret;
812 pngdec = GST_PNGDEC (element);
814 switch (transition) {
815 case GST_STATE_CHANGE_READY_TO_PAUSED:
816 gst_pngdec_libpng_init (pngdec);
817 pngdec->need_newsegment = TRUE;
818 pngdec->framed = FALSE;
819 pngdec->ret = GST_FLOW_OK;
820 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
826 ret = parent_class->change_state (element, transition);
827 if (ret != GST_STATE_CHANGE_SUCCESS)
830 switch (transition) {
831 case GST_STATE_CHANGE_PAUSED_TO_READY:
832 gst_pngdec_libpng_clear (pngdec);
841 /* this function gets called when we activate ourselves in push mode. */
843 gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active)
847 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
849 pngdec->ret = GST_FLOW_OK;
852 /* Let libpng come back here on error */
853 if (setjmp (png_jmpbuf (pngdec->png)))
856 GST_LOG ("setting up progressive loading callbacks");
857 png_set_progressive_read_fn (pngdec->png, pngdec,
858 user_info_callback, user_endrow_callback, user_end_callback);
864 GST_LOG ("failed setting up libpng jmpbuf");
865 gst_pngdec_libpng_clear (pngdec);
870 /* this function gets called when we activate ourselves in pull mode.
871 * We can perform random access to the resource and we start a task
872 * to start reading */
874 gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
877 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
880 return gst_pad_stop_task (sinkpad);
884 /* this function is called when the pad is activated and should start
887 * We check if we can do random access to decide if we work push or
891 gst_pngdec_sink_activate (GstPad * sinkpad)
893 if (gst_pad_check_pull_range (sinkpad)) {
894 return gst_pad_activate_pull (sinkpad, TRUE);
896 return gst_pad_activate_push (sinkpad, TRUE);