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, padding, 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,
351 &padding, &alignment, &pool);
352 size = MAX (size, info->size);
354 GST_DEBUG_OBJECT (dec, "didn't get downstream ALLOCATION hints");
363 /* we did not get a pool, make one ourselves then */
364 pool = gst_buffer_pool_new ();
368 gst_object_unref (dec->pool);
371 config = gst_buffer_pool_get_config (pool);
372 gst_buffer_pool_config_set (config, caps, size, min, max, prefix, padding,
374 /* just set the option, if the pool can support it we will transparently use
375 * it through the video info API. We could also see if the pool support this
376 * option and only activate it then. */
377 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
379 gst_buffer_pool_set_config (pool, config);
381 gst_buffer_pool_set_active (pool, TRUE);
383 gst_query_unref (query);
389 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
391 GstFlowReturn ret = GST_FLOW_OK;
392 GstCaps *caps = NULL, *res = NULL;
393 GstPadTemplate *templ = NULL;
394 gint bpc = 0, color_type;
395 png_uint_32 width, height;
396 GstVideoFormat format;
397 GstVideoInfo vinfo = { 0, };
399 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
401 /* Get bits per channel */
402 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
404 /* Add alpha channel if 16-bit depth */
405 png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
406 png_set_swap (pngdec->png);
410 color_type = png_get_color_type (pngdec->png, pngdec->info);
413 /* We used to have this HACK to reverse the outgoing bytes, but the problem
414 * that originally required the hack seems to have been in ffmpegcolorspace's
415 * RGBA descriptions. It doesn't seem needed now that's fixed, but might
416 * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
417 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
418 png_set_bgr (pngdec->png);
421 /* Gray scale converted to RGB and upscaled to 8 bits */
422 if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
423 (color_type == PNG_COLOR_TYPE_GRAY)) {
424 GST_LOG_OBJECT (pngdec, "converting grayscale png to RGB");
425 png_set_gray_to_rgb (pngdec->png);
426 if (bpc < 8) { /* Convert to 8 bits */
427 GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
428 #if PNG_LIBPNG_VER < 10400
429 png_set_gray_1_2_4_to_8 (pngdec->png);
431 png_set_expand_gray_1_2_4_to_8 (pngdec->png);
436 /* Palette converted to RGB */
437 if (color_type == PNG_COLOR_TYPE_PALETTE) {
438 GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
439 png_set_palette_to_rgb (pngdec->png);
442 /* Update the info structure */
443 png_read_update_info (pngdec->png, pngdec->info);
445 /* Get IHDR header again after transformation settings */
447 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
448 &bpc, &pngdec->color_type, NULL, NULL, NULL);
450 pngdec->width = width;
451 pngdec->height = height;
453 GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", pngdec->width,
456 switch (pngdec->color_type) {
457 case PNG_COLOR_TYPE_RGB:
458 GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
459 format = GST_VIDEO_FORMAT_RGB;
461 case PNG_COLOR_TYPE_RGB_ALPHA:
462 GST_LOG_OBJECT (pngdec, "we have an alpha channel, depth is 32 bits");
463 format = GST_VIDEO_FORMAT_RGBA;
466 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
467 ("pngdec does not support this color type"));
468 ret = GST_FLOW_NOT_SUPPORTED;
472 gst_video_info_set_format (&vinfo, format, pngdec->width, pngdec->height);
473 vinfo.fps_n = pngdec->fps_n;
474 vinfo.fps_d = pngdec->fps_d;
478 if (memcmp (&vinfo, &pngdec->vinfo, sizeof (vinfo)) == 0) {
479 GST_DEBUG_OBJECT (pngdec, "video info unchanged, skip negotiation");
484 pngdec->vinfo = vinfo;
486 caps = gst_video_info_to_caps (&pngdec->vinfo);
488 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
490 res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
492 gst_caps_unref (caps);
493 gst_object_unref (templ);
495 if (!gst_pad_set_caps (pngdec->srcpad, res))
496 ret = GST_FLOW_NOT_NEGOTIATED;
498 /* clear pending reconfigure */
499 gst_pad_check_reconfigure (pngdec->srcpad);
501 GST_DEBUG_OBJECT (pngdec, "our caps %" GST_PTR_FORMAT, res);
502 gst_pngdec_negotiate_pool (pngdec, res, &pngdec->vinfo);
504 gst_caps_unref (res);
506 /* Push a newsegment event */
507 if (pngdec->need_newsegment) {
508 gst_segment_init (&pngdec->segment, GST_FORMAT_TIME);
509 gst_pad_push_event (pngdec->srcpad,
510 gst_event_new_segment (&pngdec->segment));
511 pngdec->need_newsegment = FALSE;
519 gst_pngdec_task (GstPad * pad)
522 GstBuffer *buffer = NULL;
524 png_bytep *rows, inp = NULL;
525 GstFlowReturn ret = GST_FLOW_OK;
528 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
530 GST_LOG_OBJECT (pngdec, "read frame");
532 /* Let libpng come back here on error */
533 if (setjmp (png_jmpbuf (pngdec->png))) {
534 ret = GST_FLOW_ERROR;
538 /* Set reading callback */
539 png_set_read_fn (pngdec->png, pngdec, user_read_data);
542 png_read_info (pngdec->png, pngdec->info);
547 /* Generate the caps and configure */
548 ret = gst_pngdec_caps_create_and_set (pngdec);
549 if (ret != GST_FLOW_OK) {
553 /* Allocate output buffer */
554 g_assert (pngdec->pool);
555 ret = gst_buffer_pool_acquire_buffer (pngdec->pool, &buffer, NULL);
556 if (ret != GST_FLOW_OK)
559 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height);
561 if (!gst_video_frame_map (&frame, &pngdec->vinfo, buffer, GST_MAP_WRITE))
564 inp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
566 for (i = 0; i < pngdec->height; i++) {
568 inp += GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
571 /* Read the actual picture */
572 png_read_image (pngdec->png, rows);
575 gst_video_frame_unmap (&frame);
578 /* Push the raw RGB frame */
579 ret = gst_pad_push (pngdec->srcpad, buffer);
581 if (ret != GST_FLOW_OK)
584 /* And we are done */
585 gst_pad_pause_task (pngdec->sinkpad);
586 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
592 gst_video_frame_unmap (&frame);
594 gst_buffer_unref (buffer);
595 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
596 gst_flow_get_name (ret));
597 gst_pad_pause_task (pngdec->sinkpad);
598 if (ret == GST_FLOW_EOS) {
599 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
600 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
601 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
602 (_("Internal data stream error.")),
603 ("stream stopped, reason %s", gst_flow_get_name (ret)));
604 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
609 GST_DEBUG_OBJECT (pngdec, "could not map video frame");
610 ret = GST_FLOW_ERROR;
616 gst_pngdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
619 GstFlowReturn ret = GST_FLOW_OK;
620 GstMapInfo map = GST_MAP_INFO_INIT;
622 pngdec = GST_PNGDEC (parent);
624 if (G_UNLIKELY (!pngdec->setup))
627 /* Something is going wrong in our callbacks */
629 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
630 GST_WARNING_OBJECT (pngdec, "we have a pending return code of %d", ret);
634 /* Let libpng come back here on error */
635 if (setjmp (png_jmpbuf (pngdec->png))) {
636 GST_WARNING_OBJECT (pngdec, "error during decoding");
637 ret = GST_FLOW_ERROR;
641 pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
642 pngdec->in_duration = GST_BUFFER_DURATION (buffer);
644 gst_buffer_map (buffer, &map, GST_MAP_READ);
646 GST_LOG_OBJECT (pngdec, "Got buffer, size=%d", (gint) map.size);
648 /* Progressive loading of the PNG image */
649 png_process_data (pngdec->png, pngdec->info, map.data, map.size);
651 if (pngdec->image_ready) {
652 if (pngdec->framed) {
653 /* Reset ourselves for the next frame */
654 gst_pngdec_libpng_clear (pngdec);
655 gst_pngdec_libpng_init (pngdec);
656 GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
657 png_set_progressive_read_fn (pngdec->png, pngdec,
658 user_info_callback, user_endrow_callback, user_end_callback);
660 GST_LOG_OBJECT (pngdec, "sending EOS");
661 pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
663 pngdec->image_ready = FALSE;
666 /* grab new return code */
670 if (G_LIKELY (map.data))
671 gst_buffer_unmap (buffer, &map);
673 /* And release the buffer */
674 gst_buffer_unref (buffer);
681 GST_LOG_OBJECT (pngdec, "we are not configured yet");
682 ret = GST_FLOW_FLUSHING;
688 gst_pngdec_sink_setcaps (GstPngDec * pngdec, GstCaps * caps)
693 s = gst_caps_get_structure (caps, 0);
694 if (gst_structure_get_fraction (s, "framerate", &num, &denom)) {
695 GST_DEBUG_OBJECT (pngdec, "framed input");
696 pngdec->framed = TRUE;
698 pngdec->fps_d = denom;
700 GST_DEBUG_OBJECT (pngdec, "single picture input");
701 pngdec->framed = FALSE;
710 gst_pngdec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
715 pngdec = GST_PNGDEC (parent);
717 switch (GST_EVENT_TYPE (event)) {
718 case GST_EVENT_SEGMENT:{
719 gst_event_copy_segment (event, &pngdec->segment);
721 GST_LOG_OBJECT (pngdec, "SEGMENT %" GST_SEGMENT_FORMAT, &pngdec->segment);
723 if (pngdec->segment.format == GST_FORMAT_TIME) {
724 pngdec->need_newsegment = FALSE;
725 res = gst_pad_push_event (pngdec->srcpad, event);
727 gst_event_unref (event);
732 case GST_EVENT_FLUSH_STOP:
734 gst_pngdec_libpng_clear (pngdec);
735 gst_pngdec_libpng_init (pngdec);
736 png_set_progressive_read_fn (pngdec->png, pngdec,
737 user_info_callback, user_endrow_callback, user_end_callback);
738 pngdec->ret = GST_FLOW_OK;
739 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
740 res = gst_pad_push_event (pngdec->srcpad, event);
745 GST_LOG_OBJECT (pngdec, "EOS");
746 gst_pngdec_libpng_clear (pngdec);
747 pngdec->ret = GST_FLOW_EOS;
748 res = gst_pad_push_event (pngdec->srcpad, event);
755 gst_event_parse_caps (event, &caps);
756 res = gst_pngdec_sink_setcaps (pngdec, caps);
757 gst_event_unref (event);
761 res = gst_pad_push_event (pngdec->srcpad, event);
769 /* Clean up the libpng structures */
771 gst_pngdec_libpng_clear (GstPngDec * pngdec)
773 png_infopp info = NULL, endinfo = NULL;
775 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
777 GST_LOG ("cleaning up libpng structures");
780 info = &pngdec->info;
783 if (pngdec->endinfo) {
784 endinfo = &pngdec->endinfo;
788 png_destroy_read_struct (&(pngdec->png), info, endinfo);
791 pngdec->endinfo = NULL;
794 pngdec->color_type = pngdec->height = pngdec->width = -1;
796 pngdec->buffer_out = NULL;
798 pngdec->setup = FALSE;
800 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
801 pngdec->in_duration = GST_CLOCK_TIME_NONE;
807 gst_pngdec_libpng_init (GstPngDec * pngdec)
809 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
814 GST_LOG ("init libpng structures");
816 /* initialize png struct stuff */
817 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
818 (png_voidp) NULL, user_error_fn, user_warning_fn);
820 if (pngdec->png == NULL)
823 pngdec->info = png_create_info_struct (pngdec->png);
824 if (pngdec->info == NULL)
827 pngdec->endinfo = png_create_info_struct (pngdec->png);
828 if (pngdec->endinfo == NULL)
831 pngdec->setup = TRUE;
838 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
839 ("Failed to initialize png structure"));
844 gst_pngdec_libpng_clear (pngdec);
845 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
846 ("Failed to initialize info structure"));
851 gst_pngdec_libpng_clear (pngdec);
852 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
853 ("Failed to initialize endinfo structure"));
858 static GstStateChangeReturn
859 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
861 GstStateChangeReturn ret;
864 pngdec = GST_PNGDEC (element);
866 switch (transition) {
867 case GST_STATE_CHANGE_READY_TO_PAUSED:
868 gst_pngdec_libpng_init (pngdec);
869 pngdec->need_newsegment = TRUE;
870 pngdec->framed = FALSE;
871 pngdec->ret = GST_FLOW_OK;
872 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
878 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
879 if (ret != GST_STATE_CHANGE_SUCCESS)
882 switch (transition) {
883 case GST_STATE_CHANGE_PAUSED_TO_READY:
884 gst_pngdec_libpng_clear (pngdec);
886 gst_object_unref (pngdec->pool);
895 /* this function gets called when we activate ourselves in pull mode.
896 * We can perform random access to the resource and we start a task
897 * to start reading */
899 gst_pngdec_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
900 GstPadMode mode, gboolean active)
902 GstPngDec *pngdec = GST_PNGDEC (parent);
906 case GST_PAD_MODE_PULL:
908 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
911 res = gst_pad_stop_task (sinkpad);
914 case GST_PAD_MODE_PUSH:
915 GST_DEBUG_OBJECT (pngdec, "activating push/chain function");
917 pngdec->ret = GST_FLOW_OK;
919 /* Let libpng come back here on error */
920 if (setjmp (png_jmpbuf (pngdec->png)))
923 GST_LOG_OBJECT (pngdec, "setting up progressive loading callbacks");
924 png_set_progressive_read_fn (pngdec->png, pngdec,
925 user_info_callback, user_endrow_callback, user_end_callback);
927 GST_DEBUG_OBJECT (pngdec, "deactivating push/chain function");
939 GST_LOG_OBJECT (pngdec, "failed setting up libpng jmpbuf");
940 gst_pngdec_libpng_clear (pngdec);
945 /* this function is called when the pad is activated and should start
948 * We check if we can do random access to decide if we work push or
952 gst_pngdec_sink_activate (GstPad * sinkpad, GstObject * parent)
957 query = gst_query_new_scheduling ();
959 if (!gst_pad_peer_query (sinkpad, query)) {
960 gst_query_unref (query);
964 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
965 gst_query_unref (query);
970 GST_DEBUG_OBJECT (sinkpad, "activating pull");
971 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
975 GST_DEBUG_OBJECT (sinkpad, "activating push");
976 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);