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>
25 static GstElementDetails gst_pngdec_details = {
27 "Codec/Decoder/Image",
28 "Decode a png video frame to a raw image",
29 "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);
57 static GstElementClass *parent_class = NULL;
60 gst_pngdec_get_type (void)
62 static GType pngdec_type = 0;
65 static const GTypeInfo pngdec_info = {
66 sizeof (GstPngDecClass),
69 (GClassInitFunc) gst_pngdec_class_init,
74 (GInstanceInitFunc) gst_pngdec_init,
77 pngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngDec",
83 static GstStaticPadTemplate gst_pngdec_src_pad_template =
84 GST_STATIC_PAD_TEMPLATE ("src",
87 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
90 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
91 GST_STATIC_PAD_TEMPLATE ("sink",
94 GST_STATIC_CAPS ("image/png")
98 gst_pngdec_base_init (gpointer g_class)
100 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
102 gst_element_class_add_pad_template (element_class,
103 gst_static_pad_template_get (&gst_pngdec_src_pad_template));
104 gst_element_class_add_pad_template (element_class,
105 gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
106 gst_element_class_set_details (element_class, &gst_pngdec_details);
110 gst_pngdec_class_init (GstPngDecClass * klass)
112 GObjectClass *gobject_class;
113 GstElementClass *gstelement_class;
115 gobject_class = (GObjectClass *) klass;
116 gstelement_class = (GstElementClass *) klass;
118 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
120 gstelement_class->change_state = gst_pngdec_change_state;
122 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
127 gst_pngdec_init (GstPngDec * pngdec)
129 pngdec->sinkpad = gst_pad_new_from_template (gst_static_pad_template_get
130 (&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_use_fixed_caps (pngdec->sinkpad);
139 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
141 pngdec->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
142 (&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 /* gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain); */
148 pngdec->buffer_out = NULL;
151 pngdec->endinfo = NULL;
152 pngdec->setup = FALSE;
154 pngdec->color_type = -1;
162 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
164 GST_ERROR ("%s", error_msg);
168 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
170 GST_WARNING ("%s", warning_msg);
174 user_info_callback (png_structp png_ptr, png_infop info)
176 GstPngDec *pngdec = NULL;
177 GstFlowReturn ret = GST_FLOW_OK;
179 png_uint_32 width, height;
181 GstBuffer *buffer = NULL;
183 pngdec = GST_PNGDEC (png_ptr->io_ptr);
185 GST_LOG ("info ready");
187 /* Get bits per channel */
188 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
190 /* We don't handle 16 bits per color, strip down to 8 */
192 GST_LOG ("this is a 16 bits per channel PNG image, strip down to 8 bits");
193 png_set_strip_16 (pngdec->png);
196 /* Update the info structure */
197 png_read_update_info (pngdec->png, pngdec->info);
199 /* Get IHDR header again after transformation settings */
201 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
202 &bpc, &pngdec->color_type, NULL, NULL, NULL);
204 pngdec->width = width;
205 pngdec->height = height;
207 /* Generate the caps and configure */
208 ret = gst_pngdec_caps_create_and_set (pngdec);
209 if (ret != GST_FLOW_OK) {
213 /* Allocate output buffer */
214 pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
215 buffer_size = pngdec->height * GST_ROUND_UP_4 (pngdec->rowbytes);
216 ret = gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
217 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
218 if (ret != GST_FLOW_OK) {
222 pngdec->buffer_out = buffer;
229 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
230 png_uint_32 row_num, int pass)
232 GstPngDec *pngdec = NULL;
234 pngdec = GST_PNGDEC (png_ptr->io_ptr);
236 /* FIXME: implement interlaced pictures */
238 if (GST_IS_BUFFER (pngdec->buffer_out)) {
239 size_t offset = row_num * GST_ROUND_UP_4 (pngdec->rowbytes);
241 GST_LOG ("got row %d, copying in buffer %p at offset %d", row_num,
242 pngdec->buffer_out, offset);
243 memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row,
245 pngdec->ret = GST_FLOW_OK;
247 GST_LOG ("we don't have any output buffer to write this row !");
248 pngdec->ret = GST_FLOW_ERROR;
253 user_end_callback (png_structp png_ptr, png_infop info)
255 GstPngDec *pngdec = NULL;
257 pngdec = GST_PNGDEC (png_ptr->io_ptr);
259 GST_LOG ("and we are done reading this image, pushing it and setting eos");
261 /* Push our buffer and then EOS */
262 pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
263 pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
267 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
271 GstFlowReturn ret = GST_FLOW_OK;
273 pngdec = GST_PNGDEC (png_ptr->io_ptr);
275 GST_LOG ("reading %d bytes of data at offset %d", length, pngdec->offset);
277 ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
278 if ((ret != GST_FLOW_OK) || (GST_BUFFER_SIZE (buffer) != length))
281 memcpy (data, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
283 gst_buffer_unref (buffer);
285 pngdec->offset += length;
290 GST_LOG_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret));
291 gst_pad_pause_task (pngdec->sinkpad);
292 if (GST_FLOW_IS_FATAL (ret)) {
293 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
294 GST_ELEMENT_ERROR (pngdec, STREAM, STOPPED,
295 (NULL), ("stream stopped, reason %s", gst_flow_get_name (ret)));
300 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
302 GstFlowReturn ret = GST_FLOW_OK;
303 GstCaps *caps = NULL, *res = NULL;
304 GstPadTemplate *templ = NULL;
306 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
308 GST_LOG ("this is a %dx%d PNG image", pngdec->width, pngdec->height);
310 switch (pngdec->color_type) {
311 case PNG_COLOR_TYPE_RGB:
312 GST_LOG ("we have no alpha channel, depth is 24 bits");
315 case PNG_COLOR_TYPE_RGB_ALPHA:
316 GST_LOG ("we have an alpha channel, depth is 32 bits");
320 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
321 ("pngdec does not support grayscale or paletted data yet"));
322 ret = GST_FLOW_ERROR;
326 caps = gst_caps_new_simple ("video/x-raw-rgb",
327 "width", G_TYPE_INT, pngdec->width,
328 "height", G_TYPE_INT, pngdec->height,
329 "bpp", G_TYPE_INT, pngdec->bpp,
330 "framerate", G_TYPE_DOUBLE, pngdec->fps, NULL);
332 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
334 res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
336 gst_caps_unref (caps);
338 if (!gst_pad_set_caps (pngdec->srcpad, res)) {
339 ret = GST_FLOW_ERROR;
342 GST_LOG ("our caps %s", gst_caps_to_string (res));
344 gst_caps_unref (res);
351 gst_pngdec_task (GstPad * pad)
354 GstBuffer *buffer = NULL;
355 size_t buffer_size = 0;
357 png_bytep *rows, inp;
358 png_uint_32 width, height, rowbytes;
359 GstFlowReturn ret = GST_FLOW_OK;
361 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
363 /* Let libpng come back here on error */
364 if (setjmp (png_jmpbuf (pngdec->png))) {
365 ret = GST_FLOW_ERROR;
369 /* Set reading callback */
370 png_set_read_fn (pngdec->png, pngdec, user_read_data);
373 png_read_info (pngdec->png, pngdec->info);
375 /* Get bits per channel */
376 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
378 /* We don't handle 16 bits per color, strip down to 8 */
380 GST_LOG ("this is a 16 bits per channel PNG image, strip down to 8 bits");
381 png_set_strip_16 (pngdec->png);
384 /* Update the info structure */
385 png_read_update_info (pngdec->png, pngdec->info);
387 /* Get IHDR header again after transformation settings */
389 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
390 &bpc, &pngdec->color_type, NULL, NULL, NULL);
392 pngdec->width = width;
393 pngdec->height = height;
395 /* Generate the caps and configure */
396 ret = gst_pngdec_caps_create_and_set (pngdec);
397 if (ret != GST_FLOW_OK) {
401 /* Allocate output buffer */
402 rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
403 buffer_size = pngdec->height * GST_ROUND_UP_4 (rowbytes);
404 ret = gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
405 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
406 if (ret != GST_FLOW_OK) {
410 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * height);
412 inp = GST_BUFFER_DATA (buffer);
414 for (i = 0; i < height; i++) {
416 inp += GST_ROUND_UP_4 (rowbytes);
419 /* Read the actual picture */
420 png_read_image (pngdec->png, rows);
423 /* Push the raw RGB frame */
424 ret = gst_pad_push (pngdec->srcpad, buffer);
425 if (ret != GST_FLOW_OK) {
429 /* And we are done */
430 gst_pad_pause_task (pngdec->sinkpad);
431 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
435 GST_LOG_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret));
436 gst_pad_pause_task (pngdec->sinkpad);
437 if (GST_FLOW_IS_FATAL (ret)) {
438 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
439 GST_ELEMENT_ERROR (pngdec, STREAM, STOPPED,
440 (NULL), ("stream stopped, reason %s", gst_flow_get_name (ret)));
445 gst_pngdec_chain (GstPad * pad, GstBuffer * buffer)
448 GstFlowReturn ret = GST_FLOW_OK;
450 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
452 if (!pngdec->setup) {
453 GST_LOG ("we are not configured yet");
454 ret = GST_FLOW_WRONG_STATE;
458 /* Something is going wrong in our callbacks */
459 if (pngdec->ret != GST_FLOW_OK) {
464 /* Let libpng come back here on error */
465 if (setjmp (png_jmpbuf (pngdec->png))) {
466 ret = GST_FLOW_ERROR;
470 /* Progressive loading of the PNG image */
471 png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer),
472 GST_BUFFER_SIZE (buffer));
474 /* And release the buffer */
475 gst_buffer_unref (buffer);
482 gst_pngdec_sink_event (GstPad * pad, GstEvent * event)
486 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
488 switch (GST_EVENT_TYPE (event)) {
490 gst_pngdec_libpng_clear (pngdec);
492 return gst_pad_event_default (pad, event);
497 /* Clean up the libpng structures */
499 gst_pngdec_libpng_clear (GstPngDec * pngdec)
501 png_infopp info = NULL, endinfo = NULL;
503 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
505 GST_LOG ("cleaning up libpng structures");
508 info = &pngdec->info;
511 if (pngdec->endinfo) {
512 endinfo = &pngdec->endinfo;
516 png_destroy_read_struct (&(pngdec->png), info, endinfo);
519 pngdec->endinfo = NULL;
522 pngdec->bpp = pngdec->color_type = pngdec->height = pngdec->width = -1;
524 pngdec->rowbytes = 0;
525 pngdec->buffer_out = NULL;
527 pngdec->setup = FALSE;
533 gst_pngdec_libpng_init (GstPngDec * pngdec)
535 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
541 /* initialize png struct stuff */
542 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
543 (png_voidp) NULL, user_error_fn, user_warning_fn);
545 if (pngdec->png == NULL) {
546 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
547 ("Failed to initialize png structure"));
551 pngdec->info = png_create_info_struct (pngdec->png);
552 if (pngdec->info == NULL) {
553 gst_pngdec_libpng_clear (pngdec);
554 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
555 ("Failed to initialize info structure"));
559 pngdec->endinfo = png_create_info_struct (pngdec->png);
560 if (pngdec->endinfo == NULL) {
561 gst_pngdec_libpng_clear (pngdec);
562 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
563 ("Failed to initialize endinfo structure"));
567 pngdec->setup = TRUE;
570 return pngdec->setup;
573 static GstStateChangeReturn
574 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
576 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
577 GstPngDec *pngdec = NULL;
579 pngdec = GST_PNGDEC (element);
581 switch (transition) {
582 case GST_STATE_CHANGE_READY_TO_PAUSED:
583 gst_pngdec_libpng_init (pngdec);
585 case GST_STATE_CHANGE_PAUSED_TO_READY:
586 gst_pngdec_libpng_clear (pngdec);
592 ret = parent_class->change_state (element, transition);
597 /* this function gets called when we activate ourselves in push mode. */
599 gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active)
603 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
605 pngdec->ret = GST_FLOW_OK;
608 /* Let libpng come back here on error */
609 if (setjmp (png_jmpbuf (pngdec->png))) {
610 GST_LOG ("failed setting up libpng jumb");
611 gst_pngdec_libpng_clear (pngdec);
615 GST_LOG ("setting up progressive loading callbacks");
616 png_set_progressive_read_fn (pngdec->png, pngdec,
617 user_info_callback, user_endrow_callback, user_end_callback);
623 /* this function gets called when we activate ourselves in pull mode.
624 * We can perform random access to the resource and we start a task
625 * to start reading */
627 gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
631 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
634 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
637 return gst_pad_stop_task (sinkpad);
641 /* this function is called when the pad is activated and should start
644 * We check if we can do random access to decide if we work push or
648 gst_pngdec_sink_activate (GstPad * sinkpad)
650 if (gst_pad_check_pull_range (sinkpad)) {
651 return gst_pad_activate_pull (sinkpad, TRUE);
653 return gst_pad_activate_push (sinkpad, TRUE);