2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2012 Collabora Ltd.
4 * Author : Edward Hervey <edward@collabora.com>
6 * This library is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * Library General Public License for more details.
11 * You should have received a copy of the GNU Library General Public
12 * License along with this library; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 02111-1307, USA.
18 * SECTION:element-pngdec
20 * Decodes png images. If there is no framerate set on sink caps, it sends EOS
21 * after the first picture.
28 #include "gstpngdec.h"
32 #include <gst/video/video.h>
33 #include <gst/video/gstvideometa.h>
34 #include <gst/video/gstvideopool.h>
35 #include <gst/gst-i18n-plugin.h>
37 GST_DEBUG_CATEGORY_STATIC (pngdec_debug);
38 #define GST_CAT_DEFAULT pngdec_debug
40 static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
41 static gboolean gst_pngdec_reset (GstVideoDecoder * decoder, gboolean hard);
43 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
45 static gboolean gst_pngdec_start (GstVideoDecoder * decoder);
46 static gboolean gst_pngdec_stop (GstVideoDecoder * decoder);
47 static gboolean gst_pngdec_set_format (GstVideoDecoder * Decoder,
48 GstVideoCodecState * state);
49 static GstFlowReturn gst_pngdec_handle_frame (GstVideoDecoder * decoder,
50 GstVideoCodecFrame * frame);
51 static gboolean gst_pngdec_decide_allocation (GstVideoDecoder * decoder,
54 #define parent_class gst_pngdec_parent_class
55 G_DEFINE_TYPE (GstPngDec, gst_pngdec, GST_TYPE_VIDEO_DECODER);
57 static GstStaticPadTemplate gst_pngdec_src_pad_template =
58 GST_STATIC_PAD_TEMPLATE ("src",
61 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
62 ("{ RGBA, RGB, ARGB64, GRAY8, GRAY16_BE }"))
65 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
66 GST_STATIC_PAD_TEMPLATE ("sink",
69 GST_STATIC_CAPS ("image/png")
73 gst_pngdec_class_init (GstPngDecClass * klass)
75 GstElementClass *element_class = (GstElementClass *) klass;
76 GstVideoDecoderClass *vdec_class = (GstVideoDecoderClass *) klass;
78 gst_element_class_add_pad_template (element_class,
79 gst_static_pad_template_get (&gst_pngdec_src_pad_template));
80 gst_element_class_add_pad_template (element_class,
81 gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
82 gst_element_class_set_details_simple (element_class, "PNG image decoder",
83 "Codec/Decoder/Image",
84 "Decode a png video frame to a raw image",
85 "Wim Taymans <wim@fluendo.com>");
87 vdec_class->start = gst_pngdec_start;
88 vdec_class->reset = gst_pngdec_reset;
89 vdec_class->set_format = gst_pngdec_set_format;
90 vdec_class->handle_frame = gst_pngdec_handle_frame;
91 vdec_class->decide_allocation = gst_pngdec_decide_allocation;
93 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
97 gst_pngdec_init (GstPngDec * pngdec)
101 pngdec->endinfo = NULL;
103 pngdec->color_type = -1;
105 pngdec->image_ready = FALSE;
109 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
111 GST_ERROR ("%s", error_msg);
115 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
117 GST_WARNING ("%s", warning_msg);
121 user_info_callback (png_structp png_ptr, png_infop info)
123 GstPngDec *pngdec = NULL;
124 GstFlowReturn ret = GST_FLOW_OK;
126 GST_LOG ("info ready");
128 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
129 /* Generate the caps and configure */
130 ret = gst_pngdec_caps_create_and_set (pngdec);
131 if (ret != GST_FLOW_OK) {
135 /* Allocate output buffer */
137 gst_video_decoder_alloc_output_frame (GST_VIDEO_DECODER (pngdec),
138 pngdec->current_frame);
139 if (G_UNLIKELY (ret != GST_FLOW_OK))
140 GST_DEBUG_OBJECT (pngdec, "failed to acquire buffer");
147 gst_pngdec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
149 GstPngDec *pngdec = (GstPngDec *) decoder;
151 if (pngdec->input_state)
152 gst_video_codec_state_unref (pngdec->input_state);
153 pngdec->input_state = gst_video_codec_state_ref (state);
155 /* We'll set format later on */
161 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
162 png_uint_32 row_num, int pass)
164 GstPngDec *pngdec = NULL;
166 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
168 /* FIXME: implement interlaced pictures */
170 /* If buffer_out doesn't exist, it means buffer_alloc failed, which
171 * will already have set the return code */
172 if (GST_IS_BUFFER (pngdec->current_frame->output_buffer)) {
174 GstBuffer *buffer = pngdec->current_frame->output_buffer;
179 if (!gst_video_frame_map (&frame, &pngdec->output_state->info, buffer,
181 pngdec->ret = GST_FLOW_ERROR;
185 data = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
186 offset = row_num * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
187 GST_LOG ("got row %u, copying in buffer %p at offset %" G_GSIZE_FORMAT,
188 (guint) row_num, pngdec->current_frame->output_buffer, offset);
189 width = GST_ROUND_UP_4 (png_get_rowbytes (pngdec->png, pngdec->info));
190 memcpy (data + offset, new_row, width);
191 gst_video_frame_unmap (&frame);
192 pngdec->ret = GST_FLOW_OK;
197 user_end_callback (png_structp png_ptr, png_infop info)
199 GstPngDec *pngdec = NULL;
201 pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
203 GST_LOG_OBJECT (pngdec, "and we are done reading this image");
205 if (!pngdec->current_frame->output_buffer)
208 gst_buffer_unmap (pngdec->current_frame->input_buffer,
209 &pngdec->current_frame_map);
212 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (pngdec),
213 pngdec->current_frame);
215 pngdec->image_ready = TRUE;
220 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
222 GstFlowReturn ret = GST_FLOW_OK;
223 gint bpc = 0, color_type;
224 png_uint_32 width, height;
225 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
227 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
229 /* Get bits per channel */
230 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
233 color_type = png_get_color_type (pngdec->png, pngdec->info);
235 /* Add alpha channel if 16-bit depth, but not for GRAY images */
236 if ((bpc > 8) && (color_type != PNG_COLOR_TYPE_GRAY)) {
237 png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
238 png_set_swap (pngdec->png);
241 /* We used to have this HACK to reverse the outgoing bytes, but the problem
242 * that originally required the hack seems to have been in ffmpegcolorspace's
243 * RGBA descriptions. It doesn't seem needed now that's fixed, but might
244 * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
245 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
246 png_set_bgr (pngdec->png);
249 /* Gray scale with alpha channel converted to RGB */
250 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
251 GST_LOG_OBJECT (pngdec,
252 "converting grayscale png with alpha channel to RGB");
253 png_set_gray_to_rgb (pngdec->png);
256 /* Gray scale converted to upscaled to 8 bits */
257 if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
258 (color_type == PNG_COLOR_TYPE_GRAY)) {
259 if (bpc < 8) { /* Convert to 8 bits */
260 GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
261 #if PNG_LIBPNG_VER < 10400
262 png_set_gray_1_2_4_to_8 (pngdec->png);
264 png_set_expand_gray_1_2_4_to_8 (pngdec->png);
269 /* Palette converted to RGB */
270 if (color_type == PNG_COLOR_TYPE_PALETTE) {
271 GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
272 png_set_palette_to_rgb (pngdec->png);
275 /* Update the info structure */
276 png_read_update_info (pngdec->png, pngdec->info);
278 /* Get IHDR header again after transformation settings */
279 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
280 &bpc, &pngdec->color_type, NULL, NULL, NULL);
282 GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", (gint) width,
285 switch (pngdec->color_type) {
286 case PNG_COLOR_TYPE_RGB:
287 GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
289 format = GST_VIDEO_FORMAT_RGB;
291 case PNG_COLOR_TYPE_RGB_ALPHA:
292 GST_LOG_OBJECT (pngdec,
293 "we have an alpha channel, depth is 32 or 64 bits");
295 format = GST_VIDEO_FORMAT_RGBA;
297 format = GST_VIDEO_FORMAT_ARGB64;
299 case PNG_COLOR_TYPE_GRAY:
300 GST_LOG_OBJECT (pngdec,
301 "We have an gray image, depth is 8 or 16 (be) bits");
303 format = GST_VIDEO_FORMAT_GRAY8;
305 format = GST_VIDEO_FORMAT_GRAY16_BE;
311 if (format == GST_VIDEO_FORMAT_UNKNOWN) {
312 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
313 ("pngdec does not support this color type"));
314 ret = GST_FLOW_NOT_SUPPORTED;
318 /* Check if output state changed */
319 if (pngdec->output_state) {
320 GstVideoInfo *info = &pngdec->output_state->info;
322 if (width == GST_VIDEO_INFO_WIDTH (info) &&
323 height == GST_VIDEO_INFO_HEIGHT (info) &&
324 GST_VIDEO_INFO_FORMAT (info) == format) {
327 gst_video_codec_state_unref (pngdec->output_state);
330 pngdec->output_state =
331 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (pngdec), format,
332 width, height, pngdec->input_state);
333 GST_DEBUG ("Final %d %d", GST_VIDEO_INFO_WIDTH (&pngdec->output_state->info),
334 GST_VIDEO_INFO_HEIGHT (&pngdec->output_state->info));
341 gst_pngdec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
343 GstPngDec *pngdec = (GstPngDec *) decoder;
344 GstFlowReturn ret = GST_FLOW_OK;
346 GST_LOG_OBJECT (pngdec, "Got buffer, size=%u",
347 (guint) gst_buffer_get_size (frame->input_buffer));
349 /* Let libpng come back here on error */
350 if (setjmp (png_jmpbuf (pngdec->png))) {
351 GST_WARNING_OBJECT (pngdec, "error during decoding");
352 ret = GST_FLOW_ERROR;
356 pngdec->current_frame = frame;
358 /* Progressive loading of the PNG image */
359 if (!gst_buffer_map (frame->input_buffer, &pngdec->current_frame_map,
361 GST_WARNING_OBJECT (pngdec, "Failed to map input buffer");
362 ret = GST_FLOW_ERROR;
366 png_process_data (pngdec->png, pngdec->info,
367 pngdec->current_frame_map.data, pngdec->current_frame_map.size);
369 if (pngdec->image_ready) {
371 /* Reset ourselves for the next frame */
372 gst_pngdec_reset (decoder, TRUE);
373 GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
374 png_set_progressive_read_fn (pngdec->png, pngdec,
375 user_info_callback, user_endrow_callback, user_end_callback);
377 GST_LOG_OBJECT (pngdec, "sending EOS");
378 pngdec->ret = GST_FLOW_EOS;
380 pngdec->image_ready = FALSE;
382 /* An error happened and we have to unmap */
383 gst_buffer_unmap (pngdec->current_frame->input_buffer,
384 &pngdec->current_frame_map);
394 gst_pngdec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
397 GstStructure *config;
399 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
402 g_assert (gst_query_get_n_allocation_pools (query) > 0);
403 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
404 g_assert (pool != NULL);
406 config = gst_buffer_pool_get_config (pool);
407 if (gst_query_has_allocation_meta (query, GST_VIDEO_META_API_TYPE)) {
408 gst_buffer_pool_config_add_option (config,
409 GST_BUFFER_POOL_OPTION_VIDEO_META);
411 gst_buffer_pool_set_config (pool, config);
412 gst_object_unref (pool);
418 /* Clean up the libpng structures */
420 gst_pngdec_reset (GstVideoDecoder * decoder, gboolean hard)
422 gst_pngdec_stop (decoder);
423 gst_pngdec_start (decoder);
429 gst_pngdec_libpng_init (GstPngDec * pngdec)
431 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
433 GST_LOG ("init libpng structures");
435 /* initialize png struct stuff */
436 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
437 (png_voidp) NULL, user_error_fn, user_warning_fn);
439 if (pngdec->png == NULL)
442 pngdec->info = png_create_info_struct (pngdec->png);
443 if (pngdec->info == NULL)
446 pngdec->endinfo = png_create_info_struct (pngdec->png);
447 if (pngdec->endinfo == NULL)
450 png_set_progressive_read_fn (pngdec->png, pngdec,
451 user_info_callback, user_endrow_callback, user_end_callback);
458 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
459 ("Failed to initialize png structure"));
464 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
465 ("Failed to initialize info structure"));
470 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
471 ("Failed to initialize endinfo structure"));
477 gst_pngdec_start (GstVideoDecoder * decoder)
479 GstPngDec *pngdec = (GstPngDec *) decoder;
481 gst_pngdec_libpng_init (pngdec);
487 gst_pngdec_stop (GstVideoDecoder * decoder)
489 GstPngDec *pngdec = (GstPngDec *) decoder;
490 png_infopp info = NULL, endinfo = NULL;
492 GST_LOG ("cleaning up libpng structures");
495 info = &pngdec->info;
498 if (pngdec->endinfo) {
499 endinfo = &pngdec->endinfo;
503 png_destroy_read_struct (&(pngdec->png), info, endinfo);
506 pngdec->endinfo = NULL;
509 pngdec->color_type = -1;
511 if (pngdec->input_state) {
512 gst_video_codec_state_unref (pngdec->input_state);
513 pngdec->input_state = NULL;
515 if (pngdec->output_state) {
516 gst_video_codec_state_unref (pngdec->output_state);
517 pngdec->output_state = NULL;