2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2012 Collabora Ltd.
4 * Author : Edward Hervey <edward@collabora.com>
5 * Copyright (C) 2013 Collabora Ltd.
6 * Author : Sebastian Dröge <sebastian.droege@collabora.co.uk>
7 * Olivier Crete <olivier.crete@collabora.com>
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-pngdec
23 * Decodes png images. If there is no framerate set on sink caps, it sends EOS
24 * after the first picture.
31 #include "gstpngdec.h"
35 #include <gst/base/gstbytereader.h>
36 #include <gst/video/video.h>
37 #include <gst/video/gstvideometa.h>
38 #include <gst/video/gstvideopool.h>
39 #include <gst/gst-i18n-plugin.h>
41 GST_DEBUG_CATEGORY_STATIC (pngdec_debug);
42 #define GST_CAT_DEFAULT pngdec_debug
44 static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
46 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
48 static gboolean gst_pngdec_start (GstVideoDecoder * decoder);
49 static gboolean gst_pngdec_stop (GstVideoDecoder * decoder);
50 static gboolean gst_pngdec_flush (GstVideoDecoder * decoder);
51 static gboolean gst_pngdec_set_format (GstVideoDecoder * Decoder,
52 GstVideoCodecState * state);
53 static GstFlowReturn gst_pngdec_parse (GstVideoDecoder * decoder,
54 GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos);
55 static GstFlowReturn gst_pngdec_handle_frame (GstVideoDecoder * decoder,
56 GstVideoCodecFrame * frame);
57 static gboolean gst_pngdec_decide_allocation (GstVideoDecoder * decoder,
59 static gboolean gst_pngdec_sink_event (GstVideoDecoder * bdec,
62 #define parent_class gst_pngdec_parent_class
63 G_DEFINE_TYPE (GstPngDec, gst_pngdec, GST_TYPE_VIDEO_DECODER);
65 static GstStaticPadTemplate gst_pngdec_src_pad_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
69 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
70 ("{ RGBA, RGB, ARGB64, GRAY8, GRAY16_BE }"))
73 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_STATIC_CAPS ("image/png")
81 gst_pngdec_class_init (GstPngDecClass * klass)
83 GstElementClass *element_class = (GstElementClass *) klass;
84 GstVideoDecoderClass *vdec_class = (GstVideoDecoderClass *) klass;
86 gst_element_class_add_static_pad_template (element_class,
87 &gst_pngdec_src_pad_template);
88 gst_element_class_add_static_pad_template (element_class,
89 &gst_pngdec_sink_pad_template);
90 gst_element_class_set_static_metadata (element_class, "PNG image decoder",
91 "Codec/Decoder/Image", "Decode a png video frame to a raw image",
92 "Wim Taymans <wim@fluendo.com>");
94 vdec_class->start = gst_pngdec_start;
95 vdec_class->stop = gst_pngdec_stop;
96 vdec_class->flush = gst_pngdec_flush;
97 vdec_class->set_format = gst_pngdec_set_format;
98 vdec_class->parse = gst_pngdec_parse;
99 vdec_class->handle_frame = gst_pngdec_handle_frame;
100 vdec_class->decide_allocation = gst_pngdec_decide_allocation;
101 vdec_class->sink_event = gst_pngdec_sink_event;
103 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
107 gst_pngdec_init (GstPngDec * pngdec)
111 pngdec->endinfo = NULL;
113 pngdec->color_type = -1;
115 pngdec->image_ready = FALSE;
116 pngdec->read_data = 0;
118 gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
120 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (pngdec));
124 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
126 GST_ERROR ("%s", error_msg);
130 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
132 GST_WARNING ("%s", warning_msg);
136 user_info_callback (png_structp png_ptr, png_infop info)
138 GstPngDec *pngdec = NULL;
141 GST_LOG ("info ready");
143 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
144 /* Generate the caps and configure */
145 ret = gst_pngdec_caps_create_and_set (pngdec);
146 if (ret != GST_FLOW_OK) {
150 /* Allocate output buffer */
152 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (pngdec),
153 pngdec->current_frame);
154 if (G_UNLIKELY (ret != GST_FLOW_OK))
155 GST_DEBUG_OBJECT (pngdec, "failed to acquire buffer");
162 gst_pngdec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
164 GstPngDec *pngdec = (GstPngDec *) decoder;
166 if (pngdec->input_state)
167 gst_video_codec_state_unref (pngdec->input_state);
168 pngdec->input_state = gst_video_codec_state_ref (state);
170 /* We'll set format later on */
176 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
177 png_uint_32 row_num, int pass)
179 GstPngDec *pngdec = NULL;
181 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
183 /* If buffer_out doesn't exist, it means buffer_alloc failed, which
184 * will already have set the return code */
185 if (new_row && GST_IS_BUFFER (pngdec->current_frame->output_buffer)) {
187 GstBuffer *buffer = pngdec->current_frame->output_buffer;
191 if (!gst_video_frame_map (&frame, &pngdec->output_state->info, buffer,
193 pngdec->ret = GST_FLOW_ERROR;
197 data = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
198 offset = row_num * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
199 GST_LOG ("got row %u at pass %d, copying in buffer %p at offset %"
200 G_GSIZE_FORMAT, (guint) row_num, pass,
201 pngdec->current_frame->output_buffer, offset);
202 png_progressive_combine_row (pngdec->png, data + offset, new_row);
203 gst_video_frame_unmap (&frame);
204 pngdec->ret = GST_FLOW_OK;
206 pngdec->ret = GST_FLOW_OK;
210 user_end_callback (png_structp png_ptr, png_infop info)
212 GstPngDec *pngdec = NULL;
214 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
216 GST_LOG_OBJECT (pngdec, "and we are done reading this image");
218 if (!pngdec->current_frame->output_buffer)
221 gst_buffer_unmap (pngdec->current_frame->input_buffer,
222 &pngdec->current_frame_map);
225 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (pngdec),
226 pngdec->current_frame);
228 pngdec->image_ready = TRUE;
233 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
235 GstFlowReturn ret = GST_FLOW_OK;
236 gint bpc = 0, color_type;
237 png_uint_32 width, height;
238 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
240 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
242 /* Get bits per channel */
243 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
246 color_type = png_get_color_type (pngdec->png, pngdec->info);
248 /* Add alpha channel if 16-bit depth, but not for GRAY images */
249 if ((bpc > 8) && (color_type != PNG_COLOR_TYPE_GRAY)) {
250 png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
251 png_set_swap (pngdec->png);
254 /* We used to have this HACK to reverse the outgoing bytes, but the problem
255 * that originally required the hack seems to have been in videoconvert's
256 * RGBA descriptions. It doesn't seem needed now that's fixed, but might
257 * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
258 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
259 png_set_bgr (pngdec->png);
262 /* Gray scale with alpha channel converted to RGB */
263 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
264 GST_LOG_OBJECT (pngdec,
265 "converting grayscale png with alpha channel to RGB");
266 png_set_gray_to_rgb (pngdec->png);
269 /* Gray scale converted to upscaled to 8 bits */
270 if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
271 (color_type == PNG_COLOR_TYPE_GRAY)) {
272 if (bpc < 8) { /* Convert to 8 bits */
273 GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
274 #if PNG_LIBPNG_VER < 10400
275 png_set_gray_1_2_4_to_8 (pngdec->png);
277 png_set_expand_gray_1_2_4_to_8 (pngdec->png);
282 /* Palette converted to RGB */
283 if (color_type == PNG_COLOR_TYPE_PALETTE) {
284 GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
285 png_set_palette_to_rgb (pngdec->png);
288 png_set_interlace_handling (pngdec->png);
290 /* Update the info structure */
291 png_read_update_info (pngdec->png, pngdec->info);
293 /* Get IHDR header again after transformation settings */
294 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
295 &bpc, &pngdec->color_type, NULL, NULL, NULL);
297 GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", (gint) width,
300 switch (pngdec->color_type) {
301 case PNG_COLOR_TYPE_RGB:
302 GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
304 format = GST_VIDEO_FORMAT_RGB;
306 case PNG_COLOR_TYPE_RGB_ALPHA:
307 GST_LOG_OBJECT (pngdec,
308 "we have an alpha channel, depth is 32 or 64 bits");
310 format = GST_VIDEO_FORMAT_RGBA;
312 format = GST_VIDEO_FORMAT_ARGB64;
314 case PNG_COLOR_TYPE_GRAY:
315 GST_LOG_OBJECT (pngdec,
316 "We have an gray image, depth is 8 or 16 (be) bits");
318 format = GST_VIDEO_FORMAT_GRAY8;
320 format = GST_VIDEO_FORMAT_GRAY16_BE;
326 if (format == GST_VIDEO_FORMAT_UNKNOWN) {
327 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
328 ("pngdec does not support this color type"));
329 ret = GST_FLOW_NOT_SUPPORTED;
333 /* Check if output state changed */
334 if (pngdec->output_state) {
335 GstVideoInfo *info = &pngdec->output_state->info;
337 if (width == GST_VIDEO_INFO_WIDTH (info) &&
338 height == GST_VIDEO_INFO_HEIGHT (info) &&
339 GST_VIDEO_INFO_FORMAT (info) == format) {
342 gst_video_codec_state_unref (pngdec->output_state);
345 pngdec->output_state =
346 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (pngdec), format,
347 width, height, pngdec->input_state);
348 gst_video_decoder_negotiate (GST_VIDEO_DECODER (pngdec));
349 GST_DEBUG ("Final %d %d", GST_VIDEO_INFO_WIDTH (&pngdec->output_state->info),
350 GST_VIDEO_INFO_HEIGHT (&pngdec->output_state->info));
357 gst_pngdec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
359 GstPngDec *pngdec = (GstPngDec *) decoder;
360 GstFlowReturn ret = GST_FLOW_OK;
362 GST_LOG_OBJECT (pngdec, "Got buffer, size=%u",
363 (guint) gst_buffer_get_size (frame->input_buffer));
365 /* Let libpng come back here on error */
366 if (setjmp (png_jmpbuf (pngdec->png))) {
367 GST_WARNING_OBJECT (pngdec, "error during decoding");
368 ret = GST_FLOW_ERROR;
372 pngdec->current_frame = frame;
374 /* Progressive loading of the PNG image */
375 if (!gst_buffer_map (frame->input_buffer, &pngdec->current_frame_map,
377 GST_WARNING_OBJECT (pngdec, "Failed to map input buffer");
378 ret = GST_FLOW_ERROR;
382 png_process_data (pngdec->png, pngdec->info,
383 pngdec->current_frame_map.data, pngdec->current_frame_map.size);
385 if (pngdec->image_ready) {
386 /* Reset ourselves for the next frame */
387 gst_pngdec_flush (decoder);
388 GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
389 png_set_progressive_read_fn (pngdec->png, pngdec,
390 user_info_callback, user_endrow_callback, user_end_callback);
391 pngdec->image_ready = FALSE;
393 /* An error happened and we have to unmap */
394 gst_buffer_unmap (pngdec->current_frame->input_buffer,
395 &pngdec->current_frame_map);
404 /* Based on pngparse */
405 #define PNG_SIGNATURE G_GUINT64_CONSTANT (0x89504E470D0A1A0A)
408 gst_pngdec_parse (GstVideoDecoder * decoder, GstVideoCodecFrame * frame,
409 GstAdapter * adapter, gboolean at_eos)
412 GstByteReader reader;
416 GstPngDec *pngdec = (GstPngDec *) decoder;
418 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
420 /* FIXME : The overhead of using scan_uint32 is massive */
422 size = gst_adapter_available (adapter);
423 GST_DEBUG ("Parsing PNG image data (%" G_GSIZE_FORMAT " bytes)", size);
428 data = gst_adapter_map (adapter, size);
429 gst_byte_reader_init (&reader, data, size);
431 if (pngdec->read_data == 0) {
432 if (!gst_byte_reader_peek_uint64_be (&reader, &signature))
435 if (signature != PNG_SIGNATURE) {
439 offset = gst_byte_reader_masked_scan_uint32 (&reader, 0xffffffff,
440 0x89504E47, 0, gst_byte_reader_get_remaining (&reader));
443 gst_adapter_flush (adapter,
444 gst_byte_reader_get_remaining (&reader) - 4);
448 if (!gst_byte_reader_skip (&reader, offset))
451 if (!gst_byte_reader_peek_uint64_be (&reader, &signature))
454 if (signature == PNG_SIGNATURE) {
455 /* We're skipping, go out, we'll be back */
456 gst_adapter_flush (adapter, gst_byte_reader_get_pos (&reader));
459 if (!gst_byte_reader_skip (&reader, 4))
463 pngdec->read_data = 8;
466 if (!gst_byte_reader_skip (&reader, pngdec->read_data))
473 if (!gst_byte_reader_get_uint32_be (&reader, &length))
475 if (!gst_byte_reader_get_uint32_le (&reader, &code))
478 if (!gst_byte_reader_skip (&reader, length + 4))
481 if (code == GST_MAKE_FOURCC ('I', 'E', 'N', 'D')) {
482 /* Have complete frame */
483 toadd = gst_byte_reader_get_pos (&reader);
484 GST_DEBUG_OBJECT (decoder, "Have complete frame of size %" G_GSIZE_FORMAT,
486 pngdec->read_data = 0;
487 goto have_full_frame;
489 pngdec->read_data += length + 12;
492 g_assert_not_reached ();
493 return GST_FLOW_ERROR;
496 return GST_VIDEO_DECODER_FLOW_NEED_DATA;
500 gst_video_decoder_add_to_frame (decoder, toadd);
501 return gst_video_decoder_have_frame (decoder);
505 gst_pngdec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
507 GstBufferPool *pool = NULL;
508 GstStructure *config;
510 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
513 if (gst_query_get_n_allocation_pools (query) > 0)
514 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
519 config = gst_buffer_pool_get_config (pool);
520 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
521 gst_buffer_pool_config_add_option (config,
522 GST_BUFFER_POOL_OPTION_VIDEO_META);
524 gst_buffer_pool_set_config (pool, config);
525 gst_object_unref (pool);
531 gst_pngdec_sink_event (GstVideoDecoder * bdec, GstEvent * event)
533 const GstSegment *segment;
535 if (GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT)
538 gst_event_parse_segment (event, &segment);
540 if (segment->format == GST_FORMAT_TIME)
541 gst_video_decoder_set_packetized (bdec, TRUE);
543 gst_video_decoder_set_packetized (bdec, FALSE);
546 return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (bdec, event);
550 gst_pngdec_libpng_init (GstPngDec * pngdec)
552 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
554 GST_LOG ("init libpng structures");
556 /* initialize png struct stuff */
557 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
558 (png_voidp) NULL, user_error_fn, user_warning_fn);
560 if (pngdec->png == NULL)
563 pngdec->info = png_create_info_struct (pngdec->png);
564 if (pngdec->info == NULL)
567 pngdec->endinfo = png_create_info_struct (pngdec->png);
568 if (pngdec->endinfo == NULL)
571 png_set_progressive_read_fn (pngdec->png, pngdec,
572 user_info_callback, user_endrow_callback, user_end_callback);
579 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
580 ("Failed to initialize png structure"));
585 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
586 ("Failed to initialize info structure"));
591 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
592 ("Failed to initialize endinfo structure"));
599 gst_pngdec_libpng_clear (GstPngDec * pngdec)
601 png_infopp info = NULL, endinfo = NULL;
603 GST_LOG ("cleaning up libpng structures");
606 info = &pngdec->info;
609 if (pngdec->endinfo) {
610 endinfo = &pngdec->endinfo;
614 png_destroy_read_struct (&(pngdec->png), info, endinfo);
617 pngdec->endinfo = NULL;
620 pngdec->color_type = -1;
621 pngdec->read_data = 0;
625 gst_pngdec_start (GstVideoDecoder * decoder)
627 GstPngDec *pngdec = (GstPngDec *) decoder;
629 gst_video_decoder_set_packetized (GST_VIDEO_DECODER (pngdec), FALSE);
630 gst_pngdec_libpng_init (pngdec);
636 gst_pngdec_stop (GstVideoDecoder * decoder)
638 GstPngDec *pngdec = (GstPngDec *) decoder;
640 gst_pngdec_libpng_clear (pngdec);
642 if (pngdec->input_state) {
643 gst_video_codec_state_unref (pngdec->input_state);
644 pngdec->input_state = NULL;
646 if (pngdec->output_state) {
647 gst_video_codec_state_unref (pngdec->output_state);
648 pngdec->output_state = NULL;
654 /* Clean up the libpng structures */
656 gst_pngdec_flush (GstVideoDecoder * decoder)
658 gst_pngdec_libpng_clear ((GstPngDec *) decoder);
659 gst_pngdec_libpng_init ((GstPngDec *) decoder);