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.
20 #include "gstpngdec.h"
23 #include <gst/video/video.h>
24 #include <gst/gst-i18n-plugin.h>
26 static GstElementDetails gst_pngdec_details =
27 GST_ELEMENT_DETAILS ("PNG image decoder",
28 "Codec/Decoder/Image",
29 "Decode a png video frame to a raw image",
30 "Wim Taymans <wim@fluendo.com>");
32 GST_DEBUG_CATEGORY (pngdec_debug);
33 #define GST_CAT_DEFAULT pngdec_debug
35 static void gst_pngdec_base_init (gpointer g_class);
36 static void gst_pngdec_class_init (GstPngDecClass * klass);
37 static void gst_pngdec_init (GstPngDec * pngdec);
39 static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
40 static gboolean gst_pngdec_libpng_clear (GstPngDec * pngdec);
42 static GstStateChangeReturn gst_pngdec_change_state (GstElement * element,
43 GstStateChange transition);
45 static gboolean gst_pngdec_sink_activate_push (GstPad * sinkpad,
47 static gboolean gst_pngdec_sink_activate_pull (GstPad * sinkpad,
49 static gboolean gst_pngdec_sink_activate (GstPad * sinkpad);
51 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
53 static void gst_pngdec_task (GstPad * pad);
54 static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstBuffer * buffer);
55 static gboolean gst_pngdec_sink_event (GstPad * pad, GstEvent * event);
56 static gboolean gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps);
58 static GstElementClass *parent_class = NULL;
61 gst_pngdec_get_type (void)
63 static GType pngdec_type = 0;
66 static const GTypeInfo pngdec_info = {
67 sizeof (GstPngDecClass),
70 (GClassInitFunc) gst_pngdec_class_init,
75 (GInstanceInitFunc) gst_pngdec_init,
78 pngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngDec",
84 static GstStaticPadTemplate gst_pngdec_src_pad_template =
85 GST_STATIC_PAD_TEMPLATE ("src",
88 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
91 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
92 GST_STATIC_PAD_TEMPLATE ("sink",
95 GST_STATIC_CAPS ("image/png")
99 gst_pngdec_base_init (gpointer g_class)
101 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
103 gst_element_class_add_pad_template (element_class,
104 gst_static_pad_template_get (&gst_pngdec_src_pad_template));
105 gst_element_class_add_pad_template (element_class,
106 gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
107 gst_element_class_set_details (element_class, &gst_pngdec_details);
111 gst_pngdec_class_init (GstPngDecClass * klass)
113 GObjectClass *gobject_class;
114 GstElementClass *gstelement_class;
116 gobject_class = (GObjectClass *) klass;
117 gstelement_class = (GstElementClass *) klass;
119 parent_class = g_type_class_peek_parent (klass);
121 gstelement_class->change_state = gst_pngdec_change_state;
123 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
127 gst_pngdec_init (GstPngDec * pngdec)
130 gst_pad_new_from_static_template (&gst_pngdec_sink_pad_template, "sink");
131 gst_pad_set_activate_function (pngdec->sinkpad, gst_pngdec_sink_activate);
132 gst_pad_set_activatepush_function (pngdec->sinkpad,
133 gst_pngdec_sink_activate_push);
134 gst_pad_set_activatepull_function (pngdec->sinkpad,
135 gst_pngdec_sink_activate_pull);
136 gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
137 gst_pad_set_event_function (pngdec->sinkpad, gst_pngdec_sink_event);
138 gst_pad_set_setcaps_function (pngdec->sinkpad, gst_pngdec_sink_setcaps);
139 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
142 gst_pad_new_from_static_template (&gst_pngdec_src_pad_template, "src");
143 gst_pad_use_fixed_caps (pngdec->srcpad);
144 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
146 pngdec->buffer_out = NULL;
149 pngdec->endinfo = NULL;
150 pngdec->setup = FALSE;
152 pngdec->color_type = -1;
159 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
160 pngdec->in_duration = GST_CLOCK_TIME_NONE;
164 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
166 GST_ERROR ("%s", error_msg);
170 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
172 GST_WARNING ("%s", warning_msg);
176 user_info_callback (png_structp png_ptr, png_infop info)
178 GstPngDec *pngdec = NULL;
179 GstFlowReturn ret = GST_FLOW_OK;
181 GstBuffer *buffer = NULL;
183 pngdec = GST_PNGDEC (png_ptr->io_ptr);
185 GST_LOG ("info ready");
187 /* Generate the caps and configure */
188 ret = gst_pngdec_caps_create_and_set (pngdec);
189 if (ret != GST_FLOW_OK) {
193 /* Allocate output buffer */
194 pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
195 buffer_size = pngdec->height * GST_ROUND_UP_4 (pngdec->rowbytes);
197 gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
198 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
199 if (ret != GST_FLOW_OK) {
203 pngdec->buffer_out = buffer;
210 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
211 png_uint_32 row_num, int pass)
213 GstPngDec *pngdec = NULL;
215 pngdec = GST_PNGDEC (png_ptr->io_ptr);
217 /* FIXME: implement interlaced pictures */
219 if (GST_IS_BUFFER (pngdec->buffer_out)) {
220 size_t offset = row_num * GST_ROUND_UP_4 (pngdec->rowbytes);
222 GST_LOG ("got row %d, copying in buffer %p at offset %d", row_num,
223 pngdec->buffer_out, offset);
224 memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row,
226 pngdec->ret = GST_FLOW_OK;
228 GST_LOG ("we don't have any output buffer to write this row !");
229 pngdec->ret = GST_FLOW_ERROR;
234 user_end_callback (png_structp png_ptr, png_infop info)
236 GstPngDec *pngdec = NULL;
238 pngdec = GST_PNGDEC (png_ptr->io_ptr);
240 GST_LOG_OBJECT (pngdec, "and we are done reading this image");
242 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_timestamp))
243 GST_BUFFER_TIMESTAMP (pngdec->buffer_out) = pngdec->in_timestamp;
244 if (GST_CLOCK_TIME_IS_VALID (pngdec->in_duration))
245 GST_BUFFER_DURATION (pngdec->buffer_out) = pngdec->in_duration;
247 /* Push our buffer and then EOS if needed */
248 GST_LOG_OBJECT (pngdec, "pushing buffer with ts=%" GST_TIME_FORMAT,
249 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (pngdec->buffer_out)));
251 pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
252 pngdec->buffer_out = NULL;
254 if (pngdec->framed) {
255 /* Reset ourselves for the next frame */
256 gst_pngdec_libpng_clear (pngdec);
257 gst_pngdec_libpng_init (pngdec);
258 GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
259 png_set_progressive_read_fn (pngdec->png, pngdec,
260 user_info_callback, user_endrow_callback, user_end_callback);
262 GST_LOG_OBJECT (pngdec, "sending EOS");
263 pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
268 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
272 GstFlowReturn ret = GST_FLOW_OK;
274 pngdec = GST_PNGDEC (png_ptr->io_ptr);
276 GST_LOG ("reading %d bytes of data at offset %d", length, pngdec->offset);
278 ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
279 if ((ret != GST_FLOW_OK) || (GST_BUFFER_SIZE (buffer) != length))
282 memcpy (data, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
284 gst_buffer_unref (buffer);
286 pngdec->offset += length;
291 GST_LOG_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret));
292 gst_pad_pause_task (pngdec->sinkpad);
293 if (GST_FLOW_IS_FATAL (ret)) {
294 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
295 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
296 (_("Internal data stream error.")),
297 ("stream stopped, reason %s", gst_flow_get_name (ret)));
302 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
304 GstFlowReturn ret = GST_FLOW_OK;
305 GstCaps *caps = NULL, *res = NULL;
306 GstPadTemplate *templ = NULL;
307 gint bpc = 0, color_type;
308 png_uint_32 width, height;
310 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
312 /* Get bits per channel */
313 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
315 /* We don't handle 16 bits per color, strip down to 8 */
317 GST_LOG_OBJECT (pngdec,
318 "this is a 16 bits per channel PNG image, strip down to 8 bits");
319 png_set_strip_16 (pngdec->png);
323 color_type = png_get_color_type (pngdec->png, pngdec->info);
325 /* HACK: The doc states that it's RGBA but apparently it's not... */
326 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
327 png_set_bgr (pngdec->png);
329 /* Gray scale converted to RGB and upscaled to 8 bits */
330 if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
331 (color_type == PNG_COLOR_TYPE_GRAY)) {
332 GST_LOG_OBJECT (pngdec, "converting grayscale png to RGB");
333 png_set_gray_to_rgb (pngdec->png);
334 if (bpc < 8) { /* Convert to 8 bits */
335 GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
336 png_set_gray_1_2_4_to_8 (pngdec->png);
340 /* Palette converted to RGB */
341 if (color_type == PNG_COLOR_TYPE_PALETTE) {
342 GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
343 png_set_palette_to_rgb (pngdec->png);
346 /* Update the info structure */
347 png_read_update_info (pngdec->png, pngdec->info);
349 /* Get IHDR header again after transformation settings */
351 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
352 &bpc, &pngdec->color_type, NULL, NULL, NULL);
354 pngdec->width = width;
355 pngdec->height = height;
357 GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", pngdec->width,
360 switch (pngdec->color_type) {
361 case PNG_COLOR_TYPE_RGB:
362 GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
365 case PNG_COLOR_TYPE_RGB_ALPHA:
366 GST_LOG_OBJECT (pngdec, "we have an alpha channel, depth is 32 bits");
370 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
371 ("pngdec does not support this color type"));
372 ret = GST_FLOW_ERROR;
376 caps = gst_caps_new_simple ("video/x-raw-rgb",
377 "width", G_TYPE_INT, pngdec->width,
378 "height", G_TYPE_INT, pngdec->height,
379 "bpp", G_TYPE_INT, pngdec->bpp,
380 "framerate", GST_TYPE_FRACTION, pngdec->fps_n, pngdec->fps_d, NULL);
382 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
384 res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
386 gst_caps_unref (caps);
387 gst_object_unref (templ);
389 if (!gst_pad_set_caps (pngdec->srcpad, res)) {
390 ret = GST_FLOW_ERROR;
393 GST_DEBUG_OBJECT (pngdec, "our caps %" GST_PTR_FORMAT, res);
395 gst_caps_unref (res);
397 /* Push a newsegment event */
398 if (pngdec->need_newsegment) {
399 gst_pad_push_event (pngdec->srcpad,
400 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
401 pngdec->need_newsegment = FALSE;
409 gst_pngdec_task (GstPad * pad)
412 GstBuffer *buffer = NULL;
413 size_t buffer_size = 0;
415 png_bytep *rows, inp;
416 png_uint_32 rowbytes;
417 GstFlowReturn ret = GST_FLOW_OK;
419 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
421 /* Let libpng come back here on error */
422 if (setjmp (png_jmpbuf (pngdec->png))) {
423 ret = GST_FLOW_ERROR;
427 /* Set reading callback */
428 png_set_read_fn (pngdec->png, pngdec, user_read_data);
431 png_read_info (pngdec->png, pngdec->info);
433 /* Generate the caps and configure */
434 ret = gst_pngdec_caps_create_and_set (pngdec);
435 if (ret != GST_FLOW_OK) {
439 /* Allocate output buffer */
440 rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
441 buffer_size = pngdec->height * GST_ROUND_UP_4 (rowbytes);
443 gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
444 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
445 if (ret != GST_FLOW_OK) {
449 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height);
451 inp = GST_BUFFER_DATA (buffer);
453 for (i = 0; i < pngdec->height; i++) {
455 inp += GST_ROUND_UP_4 (rowbytes);
458 /* Read the actual picture */
459 png_read_image (pngdec->png, rows);
462 /* Push the raw RGB frame */
463 ret = gst_pad_push (pngdec->srcpad, buffer);
464 if (ret != GST_FLOW_OK) {
468 /* And we are done */
469 gst_pad_pause_task (pngdec->sinkpad);
470 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
474 GST_LOG_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret));
475 gst_pad_pause_task (pngdec->sinkpad);
476 if (GST_FLOW_IS_FATAL (ret)) {
477 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
478 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
479 (_("Internal data stream error.")),
480 ("stream stopped, reason %s", gst_flow_get_name (ret)));
485 gst_pngdec_chain (GstPad * pad, GstBuffer * buffer)
488 GstFlowReturn ret = GST_FLOW_OK;
490 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
492 GST_LOG_OBJECT (pngdec, "Got buffer, size=%u", GST_BUFFER_SIZE (buffer));
494 if (!pngdec->setup) {
495 GST_LOG ("we are not configured yet");
496 ret = GST_FLOW_WRONG_STATE;
500 /* Something is going wrong in our callbacks */
501 if (pngdec->ret != GST_FLOW_OK) {
506 /* Let libpng come back here on error */
507 if (setjmp (png_jmpbuf (pngdec->png))) {
508 GST_WARNING ("error during decoding");
509 ret = GST_FLOW_ERROR;
513 pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer);
514 pngdec->in_duration = GST_BUFFER_DURATION (buffer);
516 /* Progressive loading of the PNG image */
517 png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer),
518 GST_BUFFER_SIZE (buffer));
520 /* And release the buffer */
521 gst_buffer_unref (buffer);
528 gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps)
534 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
536 s = gst_caps_get_structure (caps, 0);
537 if (gst_structure_get_fraction (s, "framerate", &num, &denom)) {
538 GST_DEBUG_OBJECT (pngdec, "framed input");
539 pngdec->framed = TRUE;
541 pngdec->fps_d = denom;
543 pngdec->framed = FALSE;
548 gst_object_unref (pngdec);
553 gst_pngdec_sink_event (GstPad * pad, GstEvent * event)
558 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
560 switch (GST_EVENT_TYPE (event)) {
561 case GST_EVENT_NEWSEGMENT:{
564 gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL);
565 GST_LOG_OBJECT (pngdec, "NEWSEGMENT (%s)", gst_format_get_name (fmt));
566 if (fmt == GST_FORMAT_TIME) {
567 pngdec->need_newsegment = FALSE;
568 res = gst_pad_event_default (pad, event);
570 gst_event_unref (event);
576 GST_LOG_OBJECT (pngdec, "EOS");
577 gst_pngdec_libpng_clear (pngdec);
578 res = gst_pad_event_default (pad, event);
581 res = gst_pad_event_default (pad, event);
585 gst_object_unref (pngdec);
590 /* Clean up the libpng structures */
592 gst_pngdec_libpng_clear (GstPngDec * pngdec)
594 png_infopp info = NULL, endinfo = NULL;
596 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
598 GST_LOG ("cleaning up libpng structures");
601 info = &pngdec->info;
604 if (pngdec->endinfo) {
605 endinfo = &pngdec->endinfo;
609 png_destroy_read_struct (&(pngdec->png), info, endinfo);
612 pngdec->endinfo = NULL;
615 pngdec->bpp = pngdec->color_type = pngdec->height = pngdec->width = -1;
617 pngdec->rowbytes = 0;
618 pngdec->buffer_out = NULL;
620 pngdec->setup = FALSE;
622 pngdec->in_timestamp = GST_CLOCK_TIME_NONE;
623 pngdec->in_duration = GST_CLOCK_TIME_NONE;
629 gst_pngdec_libpng_init (GstPngDec * pngdec)
631 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
637 /* initialize png struct stuff */
638 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
639 (png_voidp) NULL, user_error_fn, user_warning_fn);
641 if (pngdec->png == NULL) {
642 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
643 ("Failed to initialize png structure"));
647 pngdec->info = png_create_info_struct (pngdec->png);
648 if (pngdec->info == NULL) {
649 gst_pngdec_libpng_clear (pngdec);
650 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
651 ("Failed to initialize info structure"));
655 pngdec->endinfo = png_create_info_struct (pngdec->png);
656 if (pngdec->endinfo == NULL) {
657 gst_pngdec_libpng_clear (pngdec);
658 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
659 ("Failed to initialize endinfo structure"));
663 pngdec->setup = TRUE;
666 return pngdec->setup;
669 static GstStateChangeReturn
670 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
672 GstStateChangeReturn ret;
675 pngdec = GST_PNGDEC (element);
677 switch (transition) {
678 case GST_STATE_CHANGE_READY_TO_PAUSED:
679 gst_pngdec_libpng_init (pngdec);
680 pngdec->need_newsegment = TRUE;
681 pngdec->framed = FALSE;
687 ret = parent_class->change_state (element, transition);
688 if (ret != GST_STATE_CHANGE_SUCCESS)
691 switch (transition) {
692 case GST_STATE_CHANGE_PAUSED_TO_READY:
693 gst_pngdec_libpng_clear (pngdec);
702 /* this function gets called when we activate ourselves in push mode. */
704 gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active)
708 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
710 pngdec->ret = GST_FLOW_OK;
713 /* Let libpng come back here on error */
714 if (setjmp (png_jmpbuf (pngdec->png))) {
715 GST_LOG ("failed setting up libpng jumb");
716 gst_pngdec_libpng_clear (pngdec);
720 GST_LOG ("setting up progressive loading callbacks");
721 png_set_progressive_read_fn (pngdec->png, pngdec,
722 user_info_callback, user_endrow_callback, user_end_callback);
728 /* this function gets called when we activate ourselves in pull mode.
729 * We can perform random access to the resource and we start a task
730 * to start reading */
732 gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
736 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
739 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
742 return gst_pad_stop_task (sinkpad);
746 /* this function is called when the pad is activated and should start
749 * We check if we can do random access to decide if we work push or
753 gst_pngdec_sink_activate (GstPad * sinkpad)
755 if (gst_pad_check_pull_range (sinkpad)) {
756 return gst_pad_activate_pull (sinkpad, TRUE);
758 return gst_pad_activate_push (sinkpad, TRUE);