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/video/gstvideometa.h>
32 #include <gst/video/gstvideopool.h>
33 #include <gst/gst-i18n-plugin.h>
35 GST_DEBUG_CATEGORY_STATIC (pngdec_debug);
36 #define GST_CAT_DEFAULT pngdec_debug
38 static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
39 static gboolean gst_pngdec_libpng_clear (GstPngDec * pngdec);
41 static GstStateChangeReturn gst_pngdec_change_state (GstElement * element,
42 GstStateChange transition);
44 static gboolean gst_pngdec_sink_activate_mode (GstPad * sinkpad,
45 GstObject * parent, GstPadMode mode, gboolean active);
46 static gboolean gst_pngdec_sink_activate (GstPad * sinkpad, GstObject * parent);
48 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
50 static void gst_pngdec_task (GstPad * pad);
51 static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstObject * parent,
53 static gboolean gst_pngdec_sink_event (GstPad * pad, GstObject * parent,
55 static gboolean gst_pngdec_sink_setcaps (GstPngDec * pngdec, GstCaps * caps);
57 static GstFlowReturn gst_pngdec_negotiate_pool (GstPngDec * dec,
58 GstCaps * caps, GstVideoInfo * info);
60 static GstStaticPadTemplate gst_pngdec_src_pad_template =
61 GST_STATIC_PAD_TEMPLATE ("src",
64 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBA, RGB }"))
67 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
68 GST_STATIC_PAD_TEMPLATE ("sink",
71 GST_STATIC_CAPS ("image/png")
74 #define gst_pngdec_parent_class parent_class
75 G_DEFINE_TYPE (GstPngDec, gst_pngdec, GST_TYPE_ELEMENT);
78 gst_pngdec_class_init (GstPngDecClass * klass)
80 GstElementClass *gstelement_class;
82 gstelement_class = (GstElementClass *) klass;
84 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_pngdec_change_state);
86 gst_element_class_add_pad_template (gstelement_class,
87 gst_static_pad_template_get (&gst_pngdec_src_pad_template));
88 gst_element_class_add_pad_template (gstelement_class,
89 gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
90 gst_element_class_set_details_simple (gstelement_class, "PNG image decoder",
91 "Codec/Decoder/Image",
92 "Decode a png video frame to a raw image",
93 "Wim Taymans <wim@fluendo.com>");
95 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
99 gst_pngdec_init (GstPngDec * pngdec)
102 gst_pad_new_from_static_template (&gst_pngdec_sink_pad_template, "sink");
103 gst_pad_set_activate_function (pngdec->sinkpad,
104 GST_DEBUG_FUNCPTR (gst_pngdec_sink_activate));
105 gst_pad_set_activatemode_function (pngdec->sinkpad,
106 GST_DEBUG_FUNCPTR (gst_pngdec_sink_activate_mode));
107 gst_pad_set_chain_function (pngdec->sinkpad,
108 GST_DEBUG_FUNCPTR (gst_pngdec_chain));
109 gst_pad_set_event_function (pngdec->sinkpad,
110 GST_DEBUG_FUNCPTR (gst_pngdec_sink_event));
111 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
114 gst_pad_new_from_static_template (&gst_pngdec_src_pad_template, "src");
115 gst_pad_use_fixed_caps (pngdec->srcpad);
116 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
118 pngdec->buffer_out = NULL;
121 pngdec->endinfo = NULL;
122 pngdec->setup = FALSE;
124 pngdec->color_type = -1;
130 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
131 pngdec->in_duration = GST_CLOCK_TIME_NONE;
133 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
135 pngdec->image_ready = FALSE;
139 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
141 GST_ERROR ("%s", error_msg);
145 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
147 GST_WARNING ("%s", warning_msg);
151 user_info_callback (png_structp png_ptr, png_infop info)
153 GstPngDec *pngdec = NULL;
154 GstFlowReturn ret = GST_FLOW_OK;
155 GstBuffer *buffer = NULL;
157 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
159 GST_LOG ("info ready");
161 /* Generate the caps and configure */
162 ret = gst_pngdec_caps_create_and_set (pngdec);
163 if (ret != GST_FLOW_OK) {
167 if (gst_pad_check_reconfigure (pngdec->srcpad)) {
170 caps = gst_pad_get_current_caps (pngdec->srcpad);
171 gst_pngdec_negotiate_pool (pngdec, caps, &pngdec->vinfo);
172 gst_caps_unref (caps);
175 /* Allocate output buffer */
176 g_assert (pngdec->pool);
177 ret = gst_buffer_pool_acquire_buffer (pngdec->pool, &buffer, NULL);
178 if (ret != GST_FLOW_OK) {
179 GST_DEBUG_OBJECT (pngdec, "failed to acquire buffer");
180 ret = GST_FLOW_ERROR;
184 pngdec->buffer_out = buffer;
191 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
192 png_uint_32 row_num, int pass)
194 GstPngDec *pngdec = NULL;
196 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
198 /* FIXME: implement interlaced pictures */
200 /* If buffer_out doesn't exist, it means buffer_alloc failed, which
201 * will already have set the return code */
202 if (GST_IS_BUFFER (pngdec->buffer_out)) {
204 GstBuffer *buffer = pngdec->buffer_out;
209 if (!gst_video_frame_map (&frame, &pngdec->vinfo, buffer, GST_MAP_WRITE)) {
210 pngdec->ret = GST_FLOW_ERROR;
214 data = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
215 offset = row_num * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
216 GST_LOG ("got row %u, copying in buffer %p at offset %" G_GSIZE_FORMAT,
217 (guint) row_num, pngdec->buffer_out, offset);
218 width = GST_ROUND_UP_4 (png_get_rowbytes (pngdec->png, pngdec->info));
219 memcpy (data + offset, new_row, width);
220 gst_video_frame_unmap (&frame);
221 pngdec->ret = GST_FLOW_OK;
226 buffer_clip (GstPngDec * dec, GstBuffer * buffer)
229 guint64 cstart, cstop;
231 if ((!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) ||
232 (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buffer))) ||
233 (dec->segment.format != GST_FORMAT_TIME))
236 cstart = GST_BUFFER_TIMESTAMP (buffer);
237 cstop = GST_BUFFER_DURATION (buffer);
239 if ((res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
240 cstart, cstart + cstop, &cstart, &cstop))) {
241 GST_BUFFER_TIMESTAMP (buffer) = cstart;
242 GST_BUFFER_DURATION (buffer) = cstop - cstart;
250 user_end_callback (png_structp png_ptr, png_infop info)
252 GstPngDec *pngdec = NULL;
254 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
256 GST_LOG_OBJECT (pngdec, "and we are done reading this image");
258 if (!pngdec->buffer_out)
261 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_timestamp))
262 GST_BUFFER_TIMESTAMP (pngdec->buffer_out) = pngdec->in_timestamp;
263 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_duration))
264 GST_BUFFER_DURATION (pngdec->buffer_out) = pngdec->in_duration;
266 /* buffer clipping */
267 if (buffer_clip (pngdec, pngdec->buffer_out)) {
268 /* Push our buffer and then EOS if needed */
269 GST_LOG_OBJECT (pngdec, "pushing buffer with ts=%" GST_TIME_FORMAT,
270 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (pngdec->buffer_out)));
272 pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
274 GST_LOG_OBJECT (pngdec, "dropped decoded buffer");
275 gst_buffer_unref (pngdec->buffer_out);
277 pngdec->buffer_out = NULL;
278 pngdec->image_ready = TRUE;
282 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
286 GstFlowReturn ret = GST_FLOW_OK;
289 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
291 GST_LOG ("reading %" G_GSIZE_FORMAT " bytes of data at offset %d", length,
294 ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
295 if (ret != GST_FLOW_OK)
298 size = gst_buffer_get_size (buffer);
303 gst_buffer_extract (buffer, 0, data, size);
304 gst_buffer_unref (buffer);
306 pngdec->offset += length;
313 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
314 gst_flow_get_name (ret));
315 gst_pad_pause_task (pngdec->sinkpad);
316 if (ret == GST_FLOW_EOS) {
317 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
318 } else if (ret < GST_FLOW_EOS || ret == GST_FLOW_NOT_LINKED) {
319 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
320 (_("Internal data stream error.")),
321 ("stream stopped, reason %s", gst_flow_get_name (ret)));
322 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
324 png_error (png_ptr, "Internal data stream error.");
329 gst_buffer_unref (buffer);
330 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
331 (_("Internal data stream error.")),
332 ("Read %u, needed %" G_GSIZE_FORMAT "bytes", size, length));
333 ret = GST_FLOW_ERROR;
339 gst_pngdec_negotiate_pool (GstPngDec * dec, GstCaps * caps, GstVideoInfo * info)
343 guint size, min, max;
344 GstStructure *config;
346 /* find a pool for the negotiated caps now */
347 query = gst_query_new_allocation (caps, TRUE);
349 if (!gst_pad_peer_query (dec->srcpad, query)) {
350 GST_DEBUG_OBJECT (dec, "didn't get downstream ALLOCATION hints");
353 if (gst_query_get_n_allocation_pools (query) > 0) {
354 /* we got configuration from our peer, parse them */
355 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
356 size = MAX (size, info->size);
364 /* we did not get a pool, make one ourselves then */
365 pool = gst_video_buffer_pool_new ();
369 gst_buffer_pool_set_active (dec->pool, TRUE);
370 gst_object_unref (dec->pool);
374 config = gst_buffer_pool_get_config (pool);
375 gst_buffer_pool_config_set (config, caps, size, min, max, 0, 0, 0);
377 if (gst_query_has_allocation_meta (query, GST_VIDEO_META_API_TYPE)) {
378 /* just set the option, if the pool can support it we will transparently use
379 * it through the video info API. We could also see if the pool support this
380 * option and only activate it then. */
381 gst_buffer_pool_config_add_option (config,
382 GST_BUFFER_POOL_OPTION_VIDEO_META);
384 gst_buffer_pool_set_config (pool, config);
387 gst_buffer_pool_set_active (pool, TRUE);
389 gst_query_unref (query);
395 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
397 GstFlowReturn ret = GST_FLOW_OK;
398 GstCaps *caps = NULL, *res = NULL;
399 GstPadTemplate *templ = NULL;
400 gint bpc = 0, color_type;
401 png_uint_32 width, height;
402 GstVideoFormat format;
403 GstVideoInfo vinfo = { 0, };
405 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
407 /* Get bits per channel */
408 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
410 /* Add alpha channel if 16-bit depth */
411 png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
412 png_set_swap (pngdec->png);
416 color_type = png_get_color_type (pngdec->png, pngdec->info);
419 /* We used to have this HACK to reverse the outgoing bytes, but the problem
420 * that originally required the hack seems to have been in ffmpegcolorspace's
421 * RGBA descriptions. It doesn't seem needed now that's fixed, but might
422 * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
423 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
424 png_set_bgr (pngdec->png);
427 /* Gray scale converted to RGB and upscaled to 8 bits */
428 if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
429 (color_type == PNG_COLOR_TYPE_GRAY)) {
430 GST_LOG_OBJECT (pngdec, "converting grayscale png to RGB");
431 png_set_gray_to_rgb (pngdec->png);
432 if (bpc < 8) { /* Convert to 8 bits */
433 GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
434 #if PNG_LIBPNG_VER < 10400
435 png_set_gray_1_2_4_to_8 (pngdec->png);
437 png_set_expand_gray_1_2_4_to_8 (pngdec->png);
442 /* Palette converted to RGB */
443 if (color_type == PNG_COLOR_TYPE_PALETTE) {
444 GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
445 png_set_palette_to_rgb (pngdec->png);
448 /* Update the info structure */
449 png_read_update_info (pngdec->png, pngdec->info);
451 /* Get IHDR header again after transformation settings */
453 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
454 &bpc, &pngdec->color_type, NULL, NULL, NULL);
456 pngdec->width = width;
457 pngdec->height = height;
459 GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", pngdec->width,
462 switch (pngdec->color_type) {
463 case PNG_COLOR_TYPE_RGB:
464 GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
465 format = GST_VIDEO_FORMAT_RGB;
467 case PNG_COLOR_TYPE_RGB_ALPHA:
468 GST_LOG_OBJECT (pngdec, "we have an alpha channel, depth is 32 bits");
469 format = GST_VIDEO_FORMAT_RGBA;
472 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
473 ("pngdec does not support this color type"));
474 ret = GST_FLOW_NOT_SUPPORTED;
478 gst_video_info_set_format (&vinfo, format, pngdec->width, pngdec->height);
479 vinfo.fps_n = pngdec->fps_n;
480 vinfo.fps_d = pngdec->fps_d;
484 if (memcmp (&vinfo, &pngdec->vinfo, sizeof (vinfo)) == 0) {
485 GST_DEBUG_OBJECT (pngdec, "video info unchanged, skip negotiation");
490 pngdec->vinfo = vinfo;
492 caps = gst_video_info_to_caps (&pngdec->vinfo);
494 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
496 res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
498 gst_caps_unref (caps);
499 gst_object_unref (templ);
501 if (!gst_pad_set_caps (pngdec->srcpad, res))
502 ret = GST_FLOW_NOT_NEGOTIATED;
504 /* clear pending reconfigure */
505 gst_pad_check_reconfigure (pngdec->srcpad);
507 GST_DEBUG_OBJECT (pngdec, "our caps %" GST_PTR_FORMAT, res);
508 gst_pngdec_negotiate_pool (pngdec, res, &pngdec->vinfo);
510 gst_caps_unref (res);
512 /* Push a newsegment event */
513 if (pngdec->need_newsegment) {
514 gst_segment_init (&pngdec->segment, GST_FORMAT_TIME);
515 gst_pad_push_event (pngdec->srcpad,
516 gst_event_new_segment (&pngdec->segment));
517 pngdec->need_newsegment = FALSE;
525 gst_pngdec_task (GstPad * pad)
528 GstBuffer *buffer = NULL;
530 png_bytep *rows, inp = NULL;
531 GstFlowReturn ret = GST_FLOW_OK;
534 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
536 GST_LOG_OBJECT (pngdec, "read frame");
538 /* Let libpng come back here on error */
539 if (setjmp (png_jmpbuf (pngdec->png))) {
540 ret = GST_FLOW_ERROR;
544 /* Set reading callback */
545 png_set_read_fn (pngdec->png, pngdec, user_read_data);
548 png_read_info (pngdec->png, pngdec->info);
553 /* Generate the caps and configure */
554 ret = gst_pngdec_caps_create_and_set (pngdec);
555 if (ret != GST_FLOW_OK) {
559 /* Allocate output buffer */
560 g_assert (pngdec->pool);
561 ret = gst_buffer_pool_acquire_buffer (pngdec->pool, &buffer, NULL);
562 if (ret != GST_FLOW_OK)
565 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height);
567 if (!gst_video_frame_map (&frame, &pngdec->vinfo, buffer, GST_MAP_WRITE))
570 inp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
572 for (i = 0; i < pngdec->height; i++) {
574 inp += GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
577 /* Read the actual picture */
578 png_read_image (pngdec->png, rows);
581 gst_video_frame_unmap (&frame);
584 /* Push the raw RGB frame */
585 ret = gst_pad_push (pngdec->srcpad, buffer);
587 if (ret != GST_FLOW_OK)
590 /* And we are done */
591 gst_pad_pause_task (pngdec->sinkpad);
592 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
598 gst_video_frame_unmap (&frame);
600 gst_buffer_unref (buffer);
601 GST_INFO_OBJECT (pngdec, "pausing task, reason %s",
602 gst_flow_get_name (ret));
603 gst_pad_pause_task (pngdec->sinkpad);
604 if (ret == GST_FLOW_EOS) {
605 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
606 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
607 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
608 (_("Internal data stream error.")),
609 ("stream stopped, reason %s", gst_flow_get_name (ret)));
610 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
615 GST_DEBUG_OBJECT (pngdec, "could not map video frame");
616 ret = GST_FLOW_ERROR;
622 gst_pngdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
625 GstFlowReturn ret = GST_FLOW_OK;
626 GstMapInfo map = GST_MAP_INFO_INIT;
628 pngdec = GST_PNGDEC (parent);
630 if (G_UNLIKELY (!pngdec->setup))
633 /* Something is going wrong in our callbacks */
635 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
636 GST_WARNING_OBJECT (pngdec, "we have a pending return code of %d", ret);
640 /* Let libpng come back here on error */
641 if (setjmp (png_jmpbuf (pngdec->png))) {
642 GST_WARNING_OBJECT (pngdec, "error during decoding");
643 ret = GST_FLOW_ERROR;
647 pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
648 pngdec->in_duration = GST_BUFFER_DURATION (buffer);
650 gst_buffer_map (buffer, &map, GST_MAP_READ);
652 GST_LOG_OBJECT (pngdec, "Got buffer, size=%d", (gint) map.size);
654 /* Progressive loading of the PNG image */
655 png_process_data (pngdec->png, pngdec->info, map.data, map.size);
657 if (pngdec->image_ready) {
658 if (pngdec->framed) {
659 /* Reset ourselves for the next frame */
660 gst_pngdec_libpng_clear (pngdec);
661 gst_pngdec_libpng_init (pngdec);
662 GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
663 png_set_progressive_read_fn (pngdec->png, pngdec,
664 user_info_callback, user_endrow_callback, user_end_callback);
666 GST_LOG_OBJECT (pngdec, "sending EOS");
667 pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
669 pngdec->image_ready = FALSE;
672 /* grab new return code */
676 if (G_LIKELY (map.data))
677 gst_buffer_unmap (buffer, &map);
679 /* And release the buffer */
680 gst_buffer_unref (buffer);
687 GST_LOG_OBJECT (pngdec, "we are not configured yet");
688 ret = GST_FLOW_FLUSHING;
694 gst_pngdec_sink_setcaps (GstPngDec * pngdec, GstCaps * caps)
699 s = gst_caps_get_structure (caps, 0);
700 if (gst_structure_get_fraction (s, "framerate", &num, &denom)) {
701 GST_DEBUG_OBJECT (pngdec, "framed input");
702 pngdec->framed = TRUE;
704 pngdec->fps_d = denom;
706 GST_DEBUG_OBJECT (pngdec, "single picture input");
707 pngdec->framed = FALSE;
716 gst_pngdec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
721 pngdec = GST_PNGDEC (parent);
723 switch (GST_EVENT_TYPE (event)) {
724 case GST_EVENT_SEGMENT:{
725 gst_event_copy_segment (event, &pngdec->segment);
727 GST_LOG_OBJECT (pngdec, "SEGMENT %" GST_SEGMENT_FORMAT, &pngdec->segment);
729 if (pngdec->segment.format == GST_FORMAT_TIME) {
730 pngdec->need_newsegment = FALSE;
731 res = gst_pad_push_event (pngdec->srcpad, event);
733 gst_event_unref (event);
738 case GST_EVENT_FLUSH_STOP:
740 gst_pngdec_libpng_clear (pngdec);
741 gst_pngdec_libpng_init (pngdec);
742 png_set_progressive_read_fn (pngdec->png, pngdec,
743 user_info_callback, user_endrow_callback, user_end_callback);
744 pngdec->ret = GST_FLOW_OK;
745 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
746 res = gst_pad_push_event (pngdec->srcpad, event);
751 GST_LOG_OBJECT (pngdec, "EOS");
752 gst_pngdec_libpng_clear (pngdec);
753 pngdec->ret = GST_FLOW_EOS;
754 res = gst_pad_push_event (pngdec->srcpad, event);
761 gst_event_parse_caps (event, &caps);
762 res = gst_pngdec_sink_setcaps (pngdec, caps);
763 gst_event_unref (event);
767 res = gst_pad_push_event (pngdec->srcpad, event);
775 /* Clean up the libpng structures */
777 gst_pngdec_libpng_clear (GstPngDec * pngdec)
779 png_infopp info = NULL, endinfo = NULL;
781 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
783 GST_LOG ("cleaning up libpng structures");
786 info = &pngdec->info;
789 if (pngdec->endinfo) {
790 endinfo = &pngdec->endinfo;
794 png_destroy_read_struct (&(pngdec->png), info, endinfo);
797 pngdec->endinfo = NULL;
800 pngdec->color_type = pngdec->height = pngdec->width = -1;
802 pngdec->buffer_out = NULL;
804 pngdec->setup = FALSE;
806 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
807 pngdec->in_duration = GST_CLOCK_TIME_NONE;
813 gst_pngdec_libpng_init (GstPngDec * pngdec)
815 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
820 GST_LOG ("init libpng structures");
822 /* initialize png struct stuff */
823 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
824 (png_voidp) NULL, user_error_fn, user_warning_fn);
826 if (pngdec->png == NULL)
829 pngdec->info = png_create_info_struct (pngdec->png);
830 if (pngdec->info == NULL)
833 pngdec->endinfo = png_create_info_struct (pngdec->png);
834 if (pngdec->endinfo == NULL)
837 pngdec->setup = TRUE;
844 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
845 ("Failed to initialize png structure"));
850 gst_pngdec_libpng_clear (pngdec);
851 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
852 ("Failed to initialize info structure"));
857 gst_pngdec_libpng_clear (pngdec);
858 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
859 ("Failed to initialize endinfo structure"));
864 static GstStateChangeReturn
865 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
867 GstStateChangeReturn ret;
870 pngdec = GST_PNGDEC (element);
872 switch (transition) {
873 case GST_STATE_CHANGE_READY_TO_PAUSED:
874 gst_pngdec_libpng_init (pngdec);
875 pngdec->need_newsegment = TRUE;
876 pngdec->framed = FALSE;
877 pngdec->ret = GST_FLOW_OK;
878 gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED);
884 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
885 if (ret != GST_STATE_CHANGE_SUCCESS)
888 switch (transition) {
889 case GST_STATE_CHANGE_PAUSED_TO_READY:
890 gst_pngdec_libpng_clear (pngdec);
892 gst_object_unref (pngdec->pool);
901 /* this function gets called when we activate ourselves in pull mode.
902 * We can perform random access to the resource and we start a task
903 * to start reading */
905 gst_pngdec_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
906 GstPadMode mode, gboolean active)
908 GstPngDec *pngdec = GST_PNGDEC (parent);
912 case GST_PAD_MODE_PULL:
914 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
917 res = gst_pad_stop_task (sinkpad);
920 case GST_PAD_MODE_PUSH:
921 GST_DEBUG_OBJECT (pngdec, "activating push/chain function");
923 pngdec->ret = GST_FLOW_OK;
925 /* Let libpng come back here on error */
926 if (setjmp (png_jmpbuf (pngdec->png)))
929 GST_LOG_OBJECT (pngdec, "setting up progressive loading callbacks");
930 png_set_progressive_read_fn (pngdec->png, pngdec,
931 user_info_callback, user_endrow_callback, user_end_callback);
933 GST_DEBUG_OBJECT (pngdec, "deactivating push/chain function");
945 GST_LOG_OBJECT (pngdec, "failed setting up libpng jmpbuf");
946 gst_pngdec_libpng_clear (pngdec);
951 /* this function is called when the pad is activated and should start
954 * We check if we can do random access to decide if we work push or
958 gst_pngdec_sink_activate (GstPad * sinkpad, GstObject * parent)
963 query = gst_query_new_scheduling ();
965 if (!gst_pad_peer_query (sinkpad, query)) {
966 gst_query_unref (query);
970 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
971 gst_query_unref (query);
976 GST_DEBUG_OBJECT (sinkpad, "activating pull");
977 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
981 GST_DEBUG_OBJECT (sinkpad, "activating push");
982 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);