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 /* FIXME remove this after -good depends on -base-0.10.33 */
86 #ifdef GST_VIDEO_CAPS_ARGB_64
87 #define CAPS GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_ARGB_64
89 #define CAPS GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB
92 static GstStaticPadTemplate gst_pngdec_src_pad_template =
93 GST_STATIC_PAD_TEMPLATE ("src",
96 GST_STATIC_CAPS (CAPS)
99 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
100 GST_STATIC_PAD_TEMPLATE ("sink",
103 GST_STATIC_CAPS ("image/png")
107 gst_pngdec_base_init (gpointer g_class)
109 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
111 gst_element_class_add_pad_template (element_class,
112 gst_static_pad_template_get (&gst_pngdec_src_pad_template));
113 gst_element_class_add_pad_template (element_class,
114 gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
115 gst_element_class_set_details_simple (element_class, "PNG image decoder",
116 "Codec/Decoder/Image",
117 "Decode a png video frame to a raw image",
118 "Wim Taymans <wim@fluendo.com>");
122 gst_pngdec_class_init (GstPngDecClass * klass)
124 GstElementClass *gstelement_class;
126 gstelement_class = (GstElementClass *) klass;
128 parent_class = g_type_class_peek_parent (klass);
130 gstelement_class->change_state = gst_pngdec_change_state;
132 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
136 gst_pngdec_init (GstPngDec * pngdec)
139 gst_pad_new_from_static_template (&gst_pngdec_sink_pad_template, "sink");
140 gst_pad_set_activate_function (pngdec->sinkpad, gst_pngdec_sink_activate);
141 gst_pad_set_activatepush_function (pngdec->sinkpad,
142 gst_pngdec_sink_activate_push);
143 gst_pad_set_activatepull_function (pngdec->sinkpad,
144 gst_pngdec_sink_activate_pull);
145 gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
146 gst_pad_set_event_function (pngdec->sinkpad, gst_pngdec_sink_event);
147 gst_pad_set_setcaps_function (pngdec->sinkpad, gst_pngdec_sink_setcaps);
148 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
151 gst_pad_new_from_static_template (&gst_pngdec_src_pad_template, "src");
152 gst_pad_use_fixed_caps (pngdec->srcpad);
153 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
155 pngdec->buffer_out = NULL;
158 pngdec->endinfo = NULL;
159 pngdec->setup = FALSE;
161 pngdec->color_type = -1;
168 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
169 pngdec->in_duration = GST_CLOCK_TIME_NONE;
171 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
173 pngdec->image_ready = FALSE;
177 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
179 GST_ERROR ("%s", error_msg);
183 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
185 GST_WARNING ("%s", warning_msg);
189 user_info_callback (png_structp png_ptr, png_infop info)
191 GstPngDec *pngdec = NULL;
192 GstFlowReturn ret = GST_FLOW_OK;
194 GstBuffer *buffer = NULL;
196 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
198 GST_LOG ("info ready");
200 /* Generate the caps and configure */
201 ret = gst_pngdec_caps_create_and_set (pngdec);
202 if (ret != GST_FLOW_OK) {
206 /* Allocate output buffer */
207 pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
208 if (pngdec->rowbytes > (G_MAXUINT32 - 3)
209 || pngdec->height > G_MAXUINT32 / pngdec->rowbytes) {
210 ret = GST_FLOW_ERROR;
213 pngdec->rowbytes = GST_ROUND_UP_4 (pngdec->rowbytes);
214 buffer_size = pngdec->height * pngdec->rowbytes;
217 gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
218 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
219 if (ret != GST_FLOW_OK) {
223 pngdec->buffer_out = buffer;
230 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
231 png_uint_32 row_num, int pass)
233 GstPngDec *pngdec = NULL;
235 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
237 /* FIXME: implement interlaced pictures */
239 /* If buffer_out doesn't exist, it means buffer_alloc failed, which
240 * will already have set the return code */
241 if (GST_IS_BUFFER (pngdec->buffer_out)) {
242 size_t offset = row_num * pngdec->rowbytes;
244 GST_LOG ("got row %u, copying in buffer %p at offset %" G_GSIZE_FORMAT,
245 (guint) row_num, pngdec->buffer_out, offset);
246 memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row,
248 pngdec->ret = GST_FLOW_OK;
253 buffer_clip (GstPngDec * dec, GstBuffer * buffer)
256 gint64 cstart, cstop;
259 if ((!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) ||
260 (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) ||
261 (dec->segment.format != GST_FORMAT_TIME))
264 cstart = GST_BUFFER_TIMESTAMP (buffer);
265 cstop = GST_BUFFER_DURATION (buffer);
267 if ((res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
268 cstart, cstart + cstop, &cstart, &cstop))) {
269 GST_BUFFER_TIMESTAMP (buffer) = cstart;
270 GST_BUFFER_DURATION (buffer) = cstop - cstart;
278 user_end_callback (png_structp png_ptr, png_infop info)
280 GstPngDec *pngdec = NULL;
282 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
284 GST_LOG_OBJECT (pngdec, "and we are done reading this image");
286 if (!pngdec->buffer_out)
289 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_timestamp))
290 GST_BUFFER_TIMESTAMP (pngdec->buffer_out) = pngdec->in_timestamp;
291 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_duration))
292 GST_BUFFER_DURATION (pngdec->buffer_out) = pngdec->in_duration;
294 /* buffer clipping */
295 if (buffer_clip (pngdec, pngdec->buffer_out)) {
296 /* Push our buffer and then EOS if needed */
297 GST_LOG_OBJECT (pngdec, "pushing buffer with ts=%" GST_TIME_FORMAT,
298 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (pngdec->buffer_out)));
300 pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
302 GST_LOG_OBJECT (pngdec, "dropped decoded buffer");
303 gst_buffer_unref (pngdec->buffer_out);
305 pngdec->buffer_out = NULL;
306 pngdec->image_ready = TRUE;
310 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
314 GstFlowReturn ret = GST_FLOW_OK;
317 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
319 GST_LOG ("reading %" G_GSIZE_FORMAT " bytes of data at offset %d", length,
322 ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
323 if (ret != GST_FLOW_OK)
326 size = GST_BUFFER_SIZE (buffer);
331 memcpy (data, GST_BUFFER_DATA (buffer), size);
333 gst_buffer_unref (buffer);
335 pngdec->offset += length;
342 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
343 gst_flow_get_name (ret));
344 gst_pad_pause_task (pngdec->sinkpad);
345 if (ret == GST_FLOW_UNEXPECTED) {
346 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
347 } else if (ret < GST_FLOW_UNEXPECTED || ret == GST_FLOW_NOT_LINKED) {
348 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
349 (_("Internal data stream error.")),
350 ("stream stopped, reason %s", gst_flow_get_name (ret)));
351 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
353 png_error (png_ptr, "Internal data stream error.");
358 gst_buffer_unref (buffer);
359 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
360 (_("Internal data stream error.")),
361 ("Read %u, needed %" G_GSIZE_FORMAT "bytes", size, length));
362 ret = GST_FLOW_ERROR;
368 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
370 GstFlowReturn ret = GST_FLOW_OK;
371 GstCaps *caps = NULL, *res = NULL;
372 GstPadTemplate *templ = NULL;
373 gint bpc = 0, color_type;
374 png_uint_32 width, height;
376 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
378 /* Get bits per channel */
379 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
381 /* Add alpha channel if 16-bit depth */
382 png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
383 png_set_swap (pngdec->png);
387 color_type = png_get_color_type (pngdec->png, pngdec->info);
390 /* We used to have this HACK to reverse the outgoing bytes, but the problem
391 * that originally required the hack seems to have been in ffmpegcolorspace's
392 * RGBA descriptions. It doesn't seem needed now that's fixed, but might
393 * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
394 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
395 png_set_bgr (pngdec->png);
398 /* Gray scale converted to RGB and upscaled to 8 bits */
399 if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
400 (color_type == PNG_COLOR_TYPE_GRAY)) {
401 GST_LOG_OBJECT (pngdec, "converting grayscale png to RGB");
402 png_set_gray_to_rgb (pngdec->png);
403 if (bpc < 8) { /* Convert to 8 bits */
404 GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
405 #if PNG_LIBPNG_VER < 10400
406 png_set_gray_1_2_4_to_8 (pngdec->png);
408 png_set_expand_gray_1_2_4_to_8 (pngdec->png);
413 /* Palette converted to RGB */
414 if (color_type == PNG_COLOR_TYPE_PALETTE) {
415 GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
416 png_set_palette_to_rgb (pngdec->png);
419 /* Update the info structure */
420 png_read_update_info (pngdec->png, pngdec->info);
422 /* Get IHDR header again after transformation settings */
424 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
425 &bpc, &pngdec->color_type, NULL, NULL, NULL);
427 pngdec->width = width;
428 pngdec->height = height;
430 GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", pngdec->width,
433 switch (pngdec->color_type) {
434 case PNG_COLOR_TYPE_RGB:
435 GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
436 pngdec->bpp = 3 * bpc;
438 case PNG_COLOR_TYPE_RGB_ALPHA:
439 GST_LOG_OBJECT (pngdec, "we have an alpha channel, depth is 32 bits");
440 pngdec->bpp = 4 * bpc;
443 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
444 ("pngdec does not support this color type"));
445 ret = GST_FLOW_NOT_SUPPORTED;
449 caps = gst_caps_new_simple ("video/x-raw-rgb",
450 "width", G_TYPE_INT, pngdec->width,
451 "height", G_TYPE_INT, pngdec->height,
452 "bpp", G_TYPE_INT, pngdec->bpp,
453 "framerate", GST_TYPE_FRACTION, pngdec->fps_n, pngdec->fps_d, NULL);
455 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
457 res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
459 gst_caps_unref (caps);
460 gst_object_unref (templ);
462 if (!gst_pad_set_caps (pngdec->srcpad, res))
463 ret = GST_FLOW_NOT_NEGOTIATED;
465 GST_DEBUG_OBJECT (pngdec, "our caps %" GST_PTR_FORMAT, res);
467 gst_caps_unref (res);
469 /* Push a newsegment event */
470 if (pngdec->need_newsegment) {
471 gst_pad_push_event (pngdec->srcpad,
472 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
473 pngdec->need_newsegment = FALSE;
481 gst_pngdec_task (GstPad * pad)
484 GstBuffer *buffer = NULL;
485 size_t buffer_size = 0;
487 png_bytep *rows, inp;
488 png_uint_32 rowbytes;
489 GstFlowReturn ret = GST_FLOW_OK;
491 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
493 GST_LOG_OBJECT (pngdec, "read frame");
495 /* Let libpng come back here on error */
496 if (setjmp (png_jmpbuf (pngdec->png))) {
497 ret = GST_FLOW_ERROR;
501 /* Set reading callback */
502 png_set_read_fn (pngdec->png, pngdec, user_read_data);
505 png_read_info (pngdec->png, pngdec->info);
507 /* Generate the caps and configure */
508 ret = gst_pngdec_caps_create_and_set (pngdec);
509 if (ret != GST_FLOW_OK) {
513 /* Allocate output buffer */
514 rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
515 if (rowbytes > (G_MAXUINT32 - 3) || pngdec->height > G_MAXUINT32 / rowbytes) {
516 ret = GST_FLOW_ERROR;
519 rowbytes = GST_ROUND_UP_4 (rowbytes);
520 buffer_size = pngdec->height * rowbytes;
522 gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
523 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
524 if (ret != GST_FLOW_OK)
527 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height);
529 inp = GST_BUFFER_DATA (buffer);
531 for (i = 0; i < pngdec->height; i++) {
536 /* Read the actual picture */
537 png_read_image (pngdec->png, rows);
540 /* Push the raw RGB frame */
541 ret = gst_pad_push (pngdec->srcpad, buffer);
542 if (ret != GST_FLOW_OK)
545 /* And we are done */
546 gst_pad_pause_task (pngdec->sinkpad);
547 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
552 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
553 gst_flow_get_name (ret));
554 gst_pad_pause_task (pngdec->sinkpad);
555 if (ret == GST_FLOW_UNEXPECTED) {
556 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
557 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
558 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
559 (_("Internal data stream error.")),
560 ("stream stopped, reason %s", gst_flow_get_name (ret)));
561 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
567 gst_pngdec_chain (GstPad * pad, GstBuffer * buffer)
570 GstFlowReturn ret = GST_FLOW_OK;
572 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
574 GST_LOG_OBJECT (pngdec, "Got buffer, size=%u", GST_BUFFER_SIZE (buffer));
576 if (G_UNLIKELY (!pngdec->setup))
579 /* Something is going wrong in our callbacks */
581 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
582 GST_WARNING_OBJECT (pngdec, "we have a pending return code of %d", ret);
586 /* Let libpng come back here on error */
587 if (setjmp (png_jmpbuf (pngdec->png))) {
588 GST_WARNING_OBJECT (pngdec, "error during decoding");
589 ret = GST_FLOW_ERROR;
593 pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
594 pngdec->in_duration = GST_BUFFER_DURATION (buffer);
596 /* Progressive loading of the PNG image */
597 png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer),
598 GST_BUFFER_SIZE (buffer));
600 if (pngdec->image_ready) {
601 if (pngdec->framed) {
602 /* Reset ourselves for the next frame */
603 gst_pngdec_libpng_clear (pngdec);
604 gst_pngdec_libpng_init (pngdec);
605 GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
606 png_set_progressive_read_fn (pngdec->png, pngdec,
607 user_info_callback, user_endrow_callback, user_end_callback);
609 GST_LOG_OBJECT (pngdec, "sending EOS");
610 pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
612 pngdec->image_ready = FALSE;
615 /* grab new return code */
618 /* And release the buffer */
619 gst_buffer_unref (buffer);
622 gst_object_unref (pngdec);
629 GST_LOG_OBJECT (pngdec, "we are not configured yet");
630 ret = GST_FLOW_WRONG_STATE;
636 gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps)
642 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
644 s = gst_caps_get_structure (caps, 0);
645 if (gst_structure_get_fraction (s, "framerate", &num, &denom)) {
646 GST_DEBUG_OBJECT (pngdec, "framed input");
647 pngdec->framed = TRUE;
649 pngdec->fps_d = denom;
651 GST_DEBUG_OBJECT (pngdec, "single picture input");
652 pngdec->framed = FALSE;
657 gst_object_unref (pngdec);
662 gst_pngdec_sink_event (GstPad * pad, GstEvent * event)
667 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
669 switch (GST_EVENT_TYPE (event)) {
670 case GST_EVENT_NEWSEGMENT:{
673 gint64 start, stop, position;
676 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt,
677 &start, &stop, &position);
679 gst_segment_set_newsegment_full (&pngdec->segment, update, rate, arate,
680 fmt, start, stop, position);
682 GST_LOG_OBJECT (pngdec, "NEWSEGMENT (%s)", gst_format_get_name (fmt));
684 if (fmt == GST_FORMAT_TIME) {
685 pngdec->need_newsegment = FALSE;
686 res = gst_pad_push_event (pngdec->srcpad, event);
688 gst_event_unref (event);
693 case GST_EVENT_FLUSH_STOP:
695 gst_pngdec_libpng_clear (pngdec);
696 gst_pngdec_libpng_init (pngdec);
697 png_set_progressive_read_fn (pngdec->png, pngdec,
698 user_info_callback, user_endrow_callback, user_end_callback);
699 pngdec->ret = GST_FLOW_OK;
700 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
701 res = gst_pad_push_event (pngdec->srcpad, event);
706 GST_LOG_OBJECT (pngdec, "EOS");
707 gst_pngdec_libpng_clear (pngdec);
708 pngdec->ret = GST_FLOW_UNEXPECTED;
709 res = gst_pad_push_event (pngdec->srcpad, event);
713 res = gst_pad_push_event (pngdec->srcpad, event);
717 gst_object_unref (pngdec);
722 /* Clean up the libpng structures */
724 gst_pngdec_libpng_clear (GstPngDec * pngdec)
726 png_infopp info = NULL, endinfo = NULL;
728 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
730 GST_LOG ("cleaning up libpng structures");
733 info = &pngdec->info;
736 if (pngdec->endinfo) {
737 endinfo = &pngdec->endinfo;
741 png_destroy_read_struct (&(pngdec->png), info, endinfo);
744 pngdec->endinfo = NULL;
747 pngdec->bpp = pngdec->color_type = pngdec->height = pngdec->width = -1;
749 pngdec->rowbytes = 0;
750 pngdec->buffer_out = NULL;
752 pngdec->setup = FALSE;
754 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
755 pngdec->in_duration = GST_CLOCK_TIME_NONE;
761 gst_pngdec_libpng_init (GstPngDec * pngdec)
763 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
768 GST_LOG ("init libpng structures");
770 /* initialize png struct stuff */
771 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
772 (png_voidp) NULL, user_error_fn, user_warning_fn);
774 if (pngdec->png == NULL)
777 pngdec->info = png_create_info_struct (pngdec->png);
778 if (pngdec->info == NULL)
781 pngdec->endinfo = png_create_info_struct (pngdec->png);
782 if (pngdec->endinfo == NULL)
785 pngdec->setup = TRUE;
792 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
793 ("Failed to initialize png structure"));
798 gst_pngdec_libpng_clear (pngdec);
799 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
800 ("Failed to initialize info structure"));
805 gst_pngdec_libpng_clear (pngdec);
806 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
807 ("Failed to initialize endinfo structure"));
812 static GstStateChangeReturn
813 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
815 GstStateChangeReturn ret;
818 pngdec = GST_PNGDEC (element);
820 switch (transition) {
821 case GST_STATE_CHANGE_READY_TO_PAUSED:
822 gst_pngdec_libpng_init (pngdec);
823 pngdec->need_newsegment = TRUE;
824 pngdec->framed = FALSE;
825 pngdec->ret = GST_FLOW_OK;
826 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
832 ret = parent_class->change_state (element, transition);
833 if (ret != GST_STATE_CHANGE_SUCCESS)
836 switch (transition) {
837 case GST_STATE_CHANGE_PAUSED_TO_READY:
838 gst_pngdec_libpng_clear (pngdec);
847 /* this function gets called when we activate ourselves in push mode. */
849 gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active)
853 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
855 pngdec->ret = GST_FLOW_OK;
858 /* Let libpng come back here on error */
859 if (setjmp (png_jmpbuf (pngdec->png)))
862 GST_LOG ("setting up progressive loading callbacks");
863 png_set_progressive_read_fn (pngdec->png, pngdec,
864 user_info_callback, user_endrow_callback, user_end_callback);
870 GST_LOG ("failed setting up libpng jmpbuf");
871 gst_pngdec_libpng_clear (pngdec);
876 /* this function gets called when we activate ourselves in pull mode.
877 * We can perform random access to the resource and we start a task
878 * to start reading */
880 gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
883 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
886 return gst_pad_stop_task (sinkpad);
890 /* this function is called when the pad is activated and should start
893 * We check if we can do random access to decide if we work push or
897 gst_pngdec_sink_activate (GstPad * sinkpad)
899 if (gst_pad_check_pull_range (sinkpad)) {
900 return gst_pad_activate_pull (sinkpad, TRUE);
902 return gst_pad_activate_push (sinkpad, TRUE);