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 gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
37 static gboolean gst_pngdec_libpng_clear (GstPngDec * pngdec);
39 static GstStateChangeReturn gst_pngdec_change_state (GstElement * element,
40 GstStateChange transition);
42 static gboolean gst_pngdec_sink_activate_mode (GstPad * sinkpad,
43 GstObject * parent, GstPadMode mode, gboolean active);
44 static gboolean gst_pngdec_sink_activate (GstPad * sinkpad, GstObject * parent);
46 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
48 static void gst_pngdec_task (GstPad * pad);
49 static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstObject * parent,
51 static gboolean gst_pngdec_sink_event (GstPad * pad, GstObject * parent,
53 static gboolean gst_pngdec_sink_setcaps (GstPngDec * pngdec, GstCaps * caps);
55 static GstFlowReturn gst_pngdec_negotiate_pool (GstPngDec * dec,
56 GstCaps * caps, GstVideoInfo * info);
58 static GstStaticPadTemplate gst_pngdec_src_pad_template =
59 GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBA, RGB }"))
65 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
66 GST_STATIC_PAD_TEMPLATE ("sink",
69 GST_STATIC_CAPS ("image/png")
72 #define gst_pngdec_parent_class parent_class
73 G_DEFINE_TYPE (GstPngDec, gst_pngdec, GST_TYPE_ELEMENT);
76 gst_pngdec_class_init (GstPngDecClass * klass)
78 GstElementClass *gstelement_class;
80 gstelement_class = (GstElementClass *) klass;
82 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_pngdec_change_state);
84 gst_element_class_add_pad_template (gstelement_class,
85 gst_static_pad_template_get (&gst_pngdec_src_pad_template));
86 gst_element_class_add_pad_template (gstelement_class,
87 gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
88 gst_element_class_set_details_simple (gstelement_class, "PNG image decoder",
89 "Codec/Decoder/Image",
90 "Decode a png video frame to a raw image",
91 "Wim Taymans <wim@fluendo.com>");
93 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
97 gst_pngdec_init (GstPngDec * pngdec)
100 gst_pad_new_from_static_template (&gst_pngdec_sink_pad_template, "sink");
101 gst_pad_set_activate_function (pngdec->sinkpad,
102 GST_DEBUG_FUNCPTR (gst_pngdec_sink_activate));
103 gst_pad_set_activatemode_function (pngdec->sinkpad,
104 GST_DEBUG_FUNCPTR (gst_pngdec_sink_activate_mode));
105 gst_pad_set_chain_function (pngdec->sinkpad,
106 GST_DEBUG_FUNCPTR (gst_pngdec_chain));
107 gst_pad_set_event_function (pngdec->sinkpad,
108 GST_DEBUG_FUNCPTR (gst_pngdec_sink_event));
109 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
112 gst_pad_new_from_static_template (&gst_pngdec_src_pad_template, "src");
113 gst_pad_use_fixed_caps (pngdec->srcpad);
114 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
116 pngdec->buffer_out = NULL;
119 pngdec->endinfo = NULL;
120 pngdec->setup = FALSE;
122 pngdec->color_type = -1;
128 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
129 pngdec->in_duration = GST_CLOCK_TIME_NONE;
131 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
133 pngdec->image_ready = FALSE;
137 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
139 GST_ERROR ("%s", error_msg);
143 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
145 GST_WARNING ("%s", warning_msg);
149 user_info_callback (png_structp png_ptr, png_infop info)
151 GstPngDec *pngdec = NULL;
152 GstFlowReturn ret = GST_FLOW_OK;
153 GstBuffer *buffer = NULL;
155 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
157 GST_LOG ("info ready");
159 /* Generate the caps and configure */
160 ret = gst_pngdec_caps_create_and_set (pngdec);
161 if (ret != GST_FLOW_OK) {
165 if (gst_pad_check_reconfigure (pngdec->srcpad)) {
168 caps = gst_pad_get_current_caps (pngdec->srcpad);
169 gst_pngdec_negotiate_pool (pngdec, caps, &pngdec->vinfo);
170 gst_caps_unref (caps);
173 /* Allocate output buffer */
174 g_assert (pngdec->pool);
175 ret = gst_buffer_pool_acquire_buffer (pngdec->pool, &buffer, NULL);
176 if (ret != GST_FLOW_OK) {
177 GST_DEBUG_OBJECT (pngdec, "failed to acquire buffer");
178 ret = GST_FLOW_ERROR;
182 pngdec->buffer_out = buffer;
189 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
190 png_uint_32 row_num, int pass)
192 GstPngDec *pngdec = NULL;
194 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
196 /* FIXME: implement interlaced pictures */
198 /* If buffer_out doesn't exist, it means buffer_alloc failed, which
199 * will already have set the return code */
200 if (GST_IS_BUFFER (pngdec->buffer_out)) {
202 GstBuffer *buffer = pngdec->buffer_out;
207 if (!gst_video_frame_map (&frame, &pngdec->vinfo, buffer, GST_MAP_WRITE)) {
208 pngdec->ret = GST_FLOW_ERROR;
212 data = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
213 offset = row_num * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
214 GST_LOG ("got row %u, copying in buffer %p at offset %" G_GSIZE_FORMAT,
215 (guint) row_num, pngdec->buffer_out, offset);
216 width = GST_ROUND_UP_4 (png_get_rowbytes (pngdec->png, pngdec->info));
217 memcpy (data + offset, new_row, width);
218 gst_video_frame_unmap (&frame);
219 pngdec->ret = GST_FLOW_OK;
224 buffer_clip (GstPngDec * dec, GstBuffer * buffer)
227 guint64 cstart, cstop;
229 if ((!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) ||
230 (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) ||
231 (dec->segment.format != GST_FORMAT_TIME))
234 cstart = GST_BUFFER_TIMESTAMP (buffer);
235 cstop = GST_BUFFER_DURATION (buffer);
237 if ((res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
238 cstart, cstart + cstop, &cstart, &cstop))) {
239 GST_BUFFER_TIMESTAMP (buffer) = cstart;
240 GST_BUFFER_DURATION (buffer) = cstop - cstart;
248 user_end_callback (png_structp png_ptr, png_infop info)
250 GstPngDec *pngdec = NULL;
252 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
254 GST_LOG_OBJECT (pngdec, "and we are done reading this image");
256 if (!pngdec->buffer_out)
259 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_timestamp))
260 GST_BUFFER_TIMESTAMP (pngdec->buffer_out) = pngdec->in_timestamp;
261 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_duration))
262 GST_BUFFER_DURATION (pngdec->buffer_out) = pngdec->in_duration;
264 /* buffer clipping */
265 if (buffer_clip (pngdec, pngdec->buffer_out)) {
266 /* Push our buffer and then EOS if needed */
267 GST_LOG_OBJECT (pngdec, "pushing buffer with ts=%" GST_TIME_FORMAT,
268 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (pngdec->buffer_out)));
270 pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
272 GST_LOG_OBJECT (pngdec, "dropped decoded buffer");
273 gst_buffer_unref (pngdec->buffer_out);
275 pngdec->buffer_out = NULL;
276 pngdec->image_ready = TRUE;
280 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
284 GstFlowReturn ret = GST_FLOW_OK;
287 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
289 GST_LOG ("reading %" G_GSIZE_FORMAT " bytes of data at offset %d", length,
292 ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
293 if (ret != GST_FLOW_OK)
296 size = gst_buffer_get_size (buffer);
301 gst_buffer_extract (buffer, 0, data, size);
302 gst_buffer_unref (buffer);
304 pngdec->offset += length;
311 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
312 gst_flow_get_name (ret));
313 gst_pad_pause_task (pngdec->sinkpad);
314 if (ret == GST_FLOW_EOS) {
315 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
316 } else if (ret < GST_FLOW_EOS || ret == GST_FLOW_NOT_LINKED) {
317 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
318 (_("Internal data stream error.")),
319 ("stream stopped, reason %s", gst_flow_get_name (ret)));
320 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
322 png_error (png_ptr, "Internal data stream error.");
327 gst_buffer_unref (buffer);
328 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
329 (_("Internal data stream error.")),
330 ("Read %u, needed %" G_GSIZE_FORMAT "bytes", size, length));
331 ret = GST_FLOW_ERROR;
337 gst_pngdec_negotiate_pool (GstPngDec * dec, GstCaps * caps, GstVideoInfo * info)
340 GstBufferPool *pool = NULL;
341 guint size, min, max, prefix, alignment;
342 GstStructure *config;
344 /* find a pool for the negotiated caps now */
345 query = gst_query_new_allocation (caps, TRUE);
347 if (gst_pad_peer_query (dec->srcpad, query)) {
348 GST_DEBUG_OBJECT (dec, "got downstream ALLOCATION hints");
349 /* we got configuration from our peer, parse them */
350 gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
352 size = MAX (size, info->size);
354 GST_DEBUG_OBJECT (dec, "didn't get downstream ALLOCATION hints");
362 /* we did not get a pool, make one ourselves then */
363 pool = gst_buffer_pool_new ();
367 gst_object_unref (dec->pool);
370 config = gst_buffer_pool_get_config (pool);
371 gst_buffer_pool_config_set (config, caps, size, min, max, prefix, alignment);
372 /* just set the option, if the pool can support it we will transparently use
373 * it through the video info API. We could also see if the pool support this
374 * option and only activate it then. */
375 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
377 gst_buffer_pool_set_config (pool, config);
379 gst_buffer_pool_set_active (pool, TRUE);
381 gst_query_unref (query);
387 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
389 GstFlowReturn ret = GST_FLOW_OK;
390 GstCaps *caps = NULL, *res = NULL;
391 GstPadTemplate *templ = NULL;
392 gint bpc = 0, color_type;
393 png_uint_32 width, height;
394 GstVideoFormat format;
395 GstVideoInfo vinfo = { 0, };
397 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
399 /* Get bits per channel */
400 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
402 /* Add alpha channel if 16-bit depth */
403 png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
404 png_set_swap (pngdec->png);
408 color_type = png_get_color_type (pngdec->png, pngdec->info);
411 /* We used to have this HACK to reverse the outgoing bytes, but the problem
412 * that originally required the hack seems to have been in ffmpegcolorspace's
413 * RGBA descriptions. It doesn't seem needed now that's fixed, but might
414 * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
415 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
416 png_set_bgr (pngdec->png);
419 /* Gray scale converted to RGB and upscaled to 8 bits */
420 if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
421 (color_type == PNG_COLOR_TYPE_GRAY)) {
422 GST_LOG_OBJECT (pngdec, "converting grayscale png to RGB");
423 png_set_gray_to_rgb (pngdec->png);
424 if (bpc < 8) { /* Convert to 8 bits */
425 GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
426 #if PNG_LIBPNG_VER < 10400
427 png_set_gray_1_2_4_to_8 (pngdec->png);
429 png_set_expand_gray_1_2_4_to_8 (pngdec->png);
434 /* Palette converted to RGB */
435 if (color_type == PNG_COLOR_TYPE_PALETTE) {
436 GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
437 png_set_palette_to_rgb (pngdec->png);
440 /* Update the info structure */
441 png_read_update_info (pngdec->png, pngdec->info);
443 /* Get IHDR header again after transformation settings */
445 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
446 &bpc, &pngdec->color_type, NULL, NULL, NULL);
448 pngdec->width = width;
449 pngdec->height = height;
451 GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", pngdec->width,
454 switch (pngdec->color_type) {
455 case PNG_COLOR_TYPE_RGB:
456 GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
457 format = GST_VIDEO_FORMAT_RGB;
459 case PNG_COLOR_TYPE_RGB_ALPHA:
460 GST_LOG_OBJECT (pngdec, "we have an alpha channel, depth is 32 bits");
461 format = GST_VIDEO_FORMAT_RGBA;
464 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
465 ("pngdec does not support this color type"));
466 ret = GST_FLOW_NOT_SUPPORTED;
470 gst_video_info_set_format (&vinfo, format, pngdec->width, pngdec->height);
471 vinfo.fps_n = pngdec->fps_n;
472 vinfo.fps_d = pngdec->fps_d;
476 if (memcmp (&vinfo, &pngdec->vinfo, sizeof (vinfo)) == 0) {
477 GST_DEBUG_OBJECT (pngdec, "video info unchanged, skip negotiation");
482 pngdec->vinfo = vinfo;
484 caps = gst_video_info_to_caps (&pngdec->vinfo);
486 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
488 res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
490 gst_caps_unref (caps);
491 gst_object_unref (templ);
493 if (!gst_pad_set_caps (pngdec->srcpad, res))
494 ret = GST_FLOW_NOT_NEGOTIATED;
496 /* clear pending reconfigure */
497 gst_pad_check_reconfigure (pngdec->srcpad);
499 GST_DEBUG_OBJECT (pngdec, "our caps %" GST_PTR_FORMAT, res);
500 gst_pngdec_negotiate_pool (pngdec, res, &pngdec->vinfo);
502 gst_caps_unref (res);
504 /* Push a newsegment event */
505 if (pngdec->need_newsegment) {
506 gst_segment_init (&pngdec->segment, GST_FORMAT_TIME);
507 gst_pad_push_event (pngdec->srcpad,
508 gst_event_new_segment (&pngdec->segment));
509 pngdec->need_newsegment = FALSE;
517 gst_pngdec_task (GstPad * pad)
520 GstBuffer *buffer = NULL;
522 png_bytep *rows, inp = NULL;
523 GstFlowReturn ret = GST_FLOW_OK;
526 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
528 GST_LOG_OBJECT (pngdec, "read frame");
530 /* Let libpng come back here on error */
531 if (setjmp (png_jmpbuf (pngdec->png))) {
532 ret = GST_FLOW_ERROR;
536 /* Set reading callback */
537 png_set_read_fn (pngdec->png, pngdec, user_read_data);
540 png_read_info (pngdec->png, pngdec->info);
545 /* Generate the caps and configure */
546 ret = gst_pngdec_caps_create_and_set (pngdec);
547 if (ret != GST_FLOW_OK) {
551 /* Allocate output buffer */
552 g_assert (pngdec->pool);
553 ret = gst_buffer_pool_acquire_buffer (pngdec->pool, &buffer, NULL);
554 if (ret != GST_FLOW_OK)
557 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height);
559 if (!gst_video_frame_map (&frame, &pngdec->vinfo, buffer, GST_MAP_WRITE))
562 inp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
564 for (i = 0; i < pngdec->height; i++) {
566 inp += GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
569 /* Read the actual picture */
570 png_read_image (pngdec->png, rows);
573 gst_video_frame_unmap (&frame);
576 /* Push the raw RGB frame */
577 ret = gst_pad_push (pngdec->srcpad, buffer);
579 if (ret != GST_FLOW_OK)
582 /* And we are done */
583 gst_pad_pause_task (pngdec->sinkpad);
584 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
590 gst_video_frame_unmap (&frame);
592 gst_buffer_unref (buffer);
593 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
594 gst_flow_get_name (ret));
595 gst_pad_pause_task (pngdec->sinkpad);
596 if (ret == GST_FLOW_EOS) {
597 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
598 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
599 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
600 (_("Internal data stream error.")),
601 ("stream stopped, reason %s", gst_flow_get_name (ret)));
602 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
607 GST_DEBUG_OBJECT (pngdec, "could not map video frame");
608 ret = GST_FLOW_ERROR;
614 gst_pngdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
617 GstFlowReturn ret = GST_FLOW_OK;
618 GstMapInfo map = GST_MAP_INFO_INIT;
620 pngdec = GST_PNGDEC (parent);
622 if (G_UNLIKELY (!pngdec->setup))
625 /* Something is going wrong in our callbacks */
627 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
628 GST_WARNING_OBJECT (pngdec, "we have a pending return code of %d", ret);
632 /* Let libpng come back here on error */
633 if (setjmp (png_jmpbuf (pngdec->png))) {
634 GST_WARNING_OBJECT (pngdec, "error during decoding");
635 ret = GST_FLOW_ERROR;
639 pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
640 pngdec->in_duration = GST_BUFFER_DURATION (buffer);
642 gst_buffer_map (buffer, &map, GST_MAP_READ);
644 GST_LOG_OBJECT (pngdec, "Got buffer, size=%d", (gint) map.size);
646 /* Progressive loading of the PNG image */
647 png_process_data (pngdec->png, pngdec->info, map.data, map.size);
649 if (pngdec->image_ready) {
650 if (pngdec->framed) {
651 /* Reset ourselves for the next frame */
652 gst_pngdec_libpng_clear (pngdec);
653 gst_pngdec_libpng_init (pngdec);
654 GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
655 png_set_progressive_read_fn (pngdec->png, pngdec,
656 user_info_callback, user_endrow_callback, user_end_callback);
658 GST_LOG_OBJECT (pngdec, "sending EOS");
659 pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
661 pngdec->image_ready = FALSE;
664 /* grab new return code */
668 if (G_LIKELY (map.data))
669 gst_buffer_unmap (buffer, &map);
671 /* And release the buffer */
672 gst_buffer_unref (buffer);
679 GST_LOG_OBJECT (pngdec, "we are not configured yet");
680 ret = GST_FLOW_WRONG_STATE;
686 gst_pngdec_sink_setcaps (GstPngDec * pngdec, GstCaps * caps)
691 s = gst_caps_get_structure (caps, 0);
692 if (gst_structure_get_fraction (s, "framerate", &num, &denom)) {
693 GST_DEBUG_OBJECT (pngdec, "framed input");
694 pngdec->framed = TRUE;
696 pngdec->fps_d = denom;
698 GST_DEBUG_OBJECT (pngdec, "single picture input");
699 pngdec->framed = FALSE;
708 gst_pngdec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
713 pngdec = GST_PNGDEC (parent);
715 switch (GST_EVENT_TYPE (event)) {
716 case GST_EVENT_SEGMENT:{
717 gst_event_copy_segment (event, &pngdec->segment);
719 GST_LOG_OBJECT (pngdec, "SEGMENT %" GST_SEGMENT_FORMAT, &pngdec->segment);
721 if (pngdec->segment.format == GST_FORMAT_TIME) {
722 pngdec->need_newsegment = FALSE;
723 res = gst_pad_push_event (pngdec->srcpad, event);
725 gst_event_unref (event);
730 case GST_EVENT_FLUSH_STOP:
732 gst_pngdec_libpng_clear (pngdec);
733 gst_pngdec_libpng_init (pngdec);
734 png_set_progressive_read_fn (pngdec->png, pngdec,
735 user_info_callback, user_endrow_callback, user_end_callback);
736 pngdec->ret = GST_FLOW_OK;
737 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
738 res = gst_pad_push_event (pngdec->srcpad, event);
743 GST_LOG_OBJECT (pngdec, "EOS");
744 gst_pngdec_libpng_clear (pngdec);
745 pngdec->ret = GST_FLOW_EOS;
746 res = gst_pad_push_event (pngdec->srcpad, event);
753 gst_event_parse_caps (event, &caps);
754 res = gst_pngdec_sink_setcaps (pngdec, caps);
755 gst_event_unref (event);
759 res = gst_pad_push_event (pngdec->srcpad, event);
767 /* Clean up the libpng structures */
769 gst_pngdec_libpng_clear (GstPngDec * pngdec)
771 png_infopp info = NULL, endinfo = NULL;
773 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
775 GST_LOG ("cleaning up libpng structures");
778 info = &pngdec->info;
781 if (pngdec->endinfo) {
782 endinfo = &pngdec->endinfo;
786 png_destroy_read_struct (&(pngdec->png), info, endinfo);
789 pngdec->endinfo = NULL;
792 pngdec->color_type = pngdec->height = pngdec->width = -1;
794 pngdec->buffer_out = NULL;
796 pngdec->setup = FALSE;
798 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
799 pngdec->in_duration = GST_CLOCK_TIME_NONE;
805 gst_pngdec_libpng_init (GstPngDec * pngdec)
807 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
812 GST_LOG ("init libpng structures");
814 /* initialize png struct stuff */
815 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
816 (png_voidp) NULL, user_error_fn, user_warning_fn);
818 if (pngdec->png == NULL)
821 pngdec->info = png_create_info_struct (pngdec->png);
822 if (pngdec->info == NULL)
825 pngdec->endinfo = png_create_info_struct (pngdec->png);
826 if (pngdec->endinfo == NULL)
829 pngdec->setup = TRUE;
836 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
837 ("Failed to initialize png structure"));
842 gst_pngdec_libpng_clear (pngdec);
843 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
844 ("Failed to initialize info structure"));
849 gst_pngdec_libpng_clear (pngdec);
850 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
851 ("Failed to initialize endinfo structure"));
856 static GstStateChangeReturn
857 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
859 GstStateChangeReturn ret;
862 pngdec = GST_PNGDEC (element);
864 switch (transition) {
865 case GST_STATE_CHANGE_READY_TO_PAUSED:
866 gst_pngdec_libpng_init (pngdec);
867 pngdec->need_newsegment = TRUE;
868 pngdec->framed = FALSE;
869 pngdec->ret = GST_FLOW_OK;
870 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
876 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
877 if (ret != GST_STATE_CHANGE_SUCCESS)
880 switch (transition) {
881 case GST_STATE_CHANGE_PAUSED_TO_READY:
882 gst_pngdec_libpng_clear (pngdec);
884 gst_object_unref (pngdec->pool);
893 /* this function gets called when we activate ourselves in pull mode.
894 * We can perform random access to the resource and we start a task
895 * to start reading */
897 gst_pngdec_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
898 GstPadMode mode, gboolean active)
900 GstPngDec *pngdec = GST_PNGDEC (parent);
904 case GST_PAD_MODE_PULL:
906 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
909 res = gst_pad_stop_task (sinkpad);
912 case GST_PAD_MODE_PUSH:
913 GST_DEBUG_OBJECT (pngdec, "activating push/chain function");
915 pngdec->ret = GST_FLOW_OK;
917 /* Let libpng come back here on error */
918 if (setjmp (png_jmpbuf (pngdec->png)))
921 GST_LOG_OBJECT (pngdec, "setting up progressive loading callbacks");
922 png_set_progressive_read_fn (pngdec->png, pngdec,
923 user_info_callback, user_endrow_callback, user_end_callback);
925 GST_DEBUG_OBJECT (pngdec, "deactivating push/chain function");
937 GST_LOG_OBJECT (pngdec, "failed setting up libpng jmpbuf");
938 gst_pngdec_libpng_clear (pngdec);
943 /* this function is called when the pad is activated and should start
946 * We check if we can do random access to decide if we work push or
950 gst_pngdec_sink_activate (GstPad * sinkpad, GstObject * parent)
955 query = gst_query_new_scheduling ();
957 if (!gst_pad_peer_query (sinkpad, query)) {
958 gst_query_unref (query);
962 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
963 gst_query_unref (query);
968 GST_DEBUG_OBJECT (sinkpad, "activating pull");
969 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
973 GST_DEBUG_OBJECT (sinkpad, "activating push");
974 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);