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 = {
28 "Codec/Decoder/Image",
29 "Decode a png video frame to a raw image",
30 "Wim Taymans <wim@fluendo.com>",
33 GST_DEBUG_CATEGORY (pngdec_debug);
34 #define GST_CAT_DEFAULT pngdec_debug
36 static void gst_pngdec_base_init (gpointer g_class);
37 static void gst_pngdec_class_init (GstPngDecClass * klass);
38 static void gst_pngdec_init (GstPngDec * pngdec);
40 static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
41 static gboolean gst_pngdec_libpng_clear (GstPngDec * pngdec);
43 static GstStateChangeReturn gst_pngdec_change_state (GstElement * element,
44 GstStateChange transition);
46 static gboolean gst_pngdec_sink_activate_push (GstPad * sinkpad,
48 static gboolean gst_pngdec_sink_activate_pull (GstPad * sinkpad,
50 static gboolean gst_pngdec_sink_activate (GstPad * sinkpad);
52 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
54 static void gst_pngdec_task (GstPad * pad);
55 static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstBuffer * buffer);
56 static gboolean gst_pngdec_sink_event (GstPad * pad, GstEvent * event);
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_ref (GST_TYPE_ELEMENT);
121 gstelement_class->change_state = gst_pngdec_change_state;
123 GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
128 gst_pngdec_init (GstPngDec * pngdec)
130 pngdec->sinkpad = gst_pad_new_from_template (gst_static_pad_template_get
131 (&gst_pngdec_sink_pad_template), "sink");
132 gst_pad_set_activate_function (pngdec->sinkpad, gst_pngdec_sink_activate);
133 gst_pad_set_activatepush_function (pngdec->sinkpad,
134 gst_pngdec_sink_activate_push);
135 gst_pad_set_activatepull_function (pngdec->sinkpad,
136 gst_pngdec_sink_activate_pull);
137 gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
138 gst_pad_set_event_function (pngdec->sinkpad, gst_pngdec_sink_event);
139 gst_pad_use_fixed_caps (pngdec->sinkpad);
140 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
142 pngdec->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
143 (&gst_pngdec_src_pad_template), "src");
144 gst_pad_use_fixed_caps (pngdec->srcpad);
145 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
147 /* gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain); */
149 pngdec->buffer_out = NULL;
152 pngdec->endinfo = NULL;
153 pngdec->setup = FALSE;
155 pngdec->color_type = -1;
163 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
165 GST_ERROR ("%s", error_msg);
169 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
171 GST_WARNING ("%s", warning_msg);
175 user_info_callback (png_structp png_ptr, png_infop info)
177 GstPngDec *pngdec = NULL;
178 GstFlowReturn ret = GST_FLOW_OK;
180 png_uint_32 width, height;
182 GstBuffer *buffer = NULL;
184 pngdec = GST_PNGDEC (png_ptr->io_ptr);
186 GST_LOG ("info ready");
188 /* Get bits per channel */
189 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
191 /* We don't handle 16 bits per color, strip down to 8 */
193 GST_LOG ("this is a 16 bits per channel PNG image, strip down to 8 bits");
194 png_set_strip_16 (pngdec->png);
197 /* Update the info structure */
198 png_read_update_info (pngdec->png, pngdec->info);
200 /* Get IHDR header again after transformation settings */
202 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
203 &bpc, &pngdec->color_type, NULL, NULL, NULL);
205 pngdec->width = width;
206 pngdec->height = height;
208 /* Generate the caps and configure */
209 ret = gst_pngdec_caps_create_and_set (pngdec);
210 if (ret != GST_FLOW_OK) {
214 /* Allocate output buffer */
215 pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
216 buffer_size = pngdec->height * GST_ROUND_UP_4 (pngdec->rowbytes);
217 ret = gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
218 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
219 if (ret != GST_FLOW_OK) {
223 pngdec->buffer_out = buffer;
230 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
231 png_uint_32 row_num, int pass)
233 GstPngDec *pngdec = NULL;
235 pngdec = GST_PNGDEC (png_ptr->io_ptr);
237 /* FIXME: implement interlaced pictures */
239 if (GST_IS_BUFFER (pngdec->buffer_out)) {
240 size_t offset = row_num * GST_ROUND_UP_4 (pngdec->rowbytes);
242 GST_LOG ("got row %d, copying in buffer %p at offset %d", row_num,
243 pngdec->buffer_out, offset);
244 memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row,
246 pngdec->ret = GST_FLOW_OK;
248 GST_LOG ("we don't have any output buffer to write this row !");
249 pngdec->ret = GST_FLOW_ERROR;
254 user_end_callback (png_structp png_ptr, png_infop info)
256 GstPngDec *pngdec = NULL;
258 pngdec = GST_PNGDEC (png_ptr->io_ptr);
260 GST_LOG ("and we are done reading this image, pushing it and setting eos");
262 /* Push our buffer and then EOS */
263 pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
264 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;
308 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
310 GST_LOG ("this is a %dx%d PNG image", pngdec->width, pngdec->height);
312 switch (pngdec->color_type) {
313 case PNG_COLOR_TYPE_RGB:
314 GST_LOG ("we have no alpha channel, depth is 24 bits");
317 case PNG_COLOR_TYPE_RGB_ALPHA:
318 GST_LOG ("we have an alpha channel, depth is 32 bits");
322 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
323 ("pngdec does not support grayscale or paletted data yet"));
324 ret = GST_FLOW_ERROR;
328 caps = gst_caps_new_simple ("video/x-raw-rgb",
329 "width", G_TYPE_INT, pngdec->width,
330 "height", G_TYPE_INT, pngdec->height,
331 "bpp", G_TYPE_INT, pngdec->bpp,
332 "framerate", G_TYPE_DOUBLE, pngdec->fps, NULL);
334 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
336 res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));
338 gst_caps_unref (caps);
340 if (!gst_pad_set_caps (pngdec->srcpad, res)) {
341 ret = GST_FLOW_ERROR;
344 GST_LOG ("our caps %s", gst_caps_to_string (res));
346 gst_caps_unref (res);
353 gst_pngdec_task (GstPad * pad)
356 GstBuffer *buffer = NULL;
357 size_t buffer_size = 0;
359 png_bytep *rows, inp;
360 png_uint_32 width, height, rowbytes;
361 GstFlowReturn ret = GST_FLOW_OK;
363 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
365 /* Let libpng come back here on error */
366 if (setjmp (png_jmpbuf (pngdec->png))) {
367 ret = GST_FLOW_ERROR;
371 /* Set reading callback */
372 png_set_read_fn (pngdec->png, pngdec, user_read_data);
375 png_read_info (pngdec->png, pngdec->info);
377 /* Get bits per channel */
378 bpc = png_get_bit_depth (pngdec->png, pngdec->info);
380 /* We don't handle 16 bits per color, strip down to 8 */
382 GST_LOG ("this is a 16 bits per channel PNG image, strip down to 8 bits");
383 png_set_strip_16 (pngdec->png);
386 /* Update the info structure */
387 png_read_update_info (pngdec->png, pngdec->info);
389 /* Get IHDR header again after transformation settings */
391 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
392 &bpc, &pngdec->color_type, NULL, NULL, NULL);
394 pngdec->width = width;
395 pngdec->height = height;
397 /* Generate the caps and configure */
398 ret = gst_pngdec_caps_create_and_set (pngdec);
399 if (ret != GST_FLOW_OK) {
403 /* Allocate output buffer */
404 rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
405 buffer_size = pngdec->height * GST_ROUND_UP_4 (rowbytes);
406 ret = gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
407 buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
408 if (ret != GST_FLOW_OK) {
412 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * height);
414 inp = GST_BUFFER_DATA (buffer);
416 for (i = 0; i < height; i++) {
418 inp += GST_ROUND_UP_4 (rowbytes);
421 /* Read the actual picture */
422 png_read_image (pngdec->png, rows);
425 /* Push the raw RGB frame */
426 ret = gst_pad_push (pngdec->srcpad, buffer);
427 if (ret != GST_FLOW_OK) {
431 /* And we are done */
432 gst_pad_pause_task (pngdec->sinkpad);
433 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
437 GST_LOG_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret));
438 gst_pad_pause_task (pngdec->sinkpad);
439 if (GST_FLOW_IS_FATAL (ret)) {
440 gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
441 GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
442 (_("Internal data stream error.")),
443 ("stream stopped, reason %s", gst_flow_get_name (ret)));
448 gst_pngdec_chain (GstPad * pad, GstBuffer * buffer)
451 GstFlowReturn ret = GST_FLOW_OK;
453 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
455 if (!pngdec->setup) {
456 GST_LOG ("we are not configured yet");
457 ret = GST_FLOW_WRONG_STATE;
461 /* Something is going wrong in our callbacks */
462 if (pngdec->ret != GST_FLOW_OK) {
467 /* Let libpng come back here on error */
468 if (setjmp (png_jmpbuf (pngdec->png))) {
469 ret = GST_FLOW_ERROR;
473 /* Progressive loading of the PNG image */
474 png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer),
475 GST_BUFFER_SIZE (buffer));
477 /* And release the buffer */
478 gst_buffer_unref (buffer);
485 gst_pngdec_sink_event (GstPad * pad, GstEvent * event)
489 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));
491 switch (GST_EVENT_TYPE (event)) {
493 gst_pngdec_libpng_clear (pngdec);
495 return gst_pad_event_default (pad, event);
500 /* Clean up the libpng structures */
502 gst_pngdec_libpng_clear (GstPngDec * pngdec)
504 png_infopp info = NULL, endinfo = NULL;
506 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
508 GST_LOG ("cleaning up libpng structures");
511 info = &pngdec->info;
514 if (pngdec->endinfo) {
515 endinfo = &pngdec->endinfo;
519 png_destroy_read_struct (&(pngdec->png), info, endinfo);
522 pngdec->endinfo = NULL;
525 pngdec->bpp = pngdec->color_type = pngdec->height = pngdec->width = -1;
527 pngdec->rowbytes = 0;
528 pngdec->buffer_out = NULL;
530 pngdec->setup = FALSE;
536 gst_pngdec_libpng_init (GstPngDec * pngdec)
538 g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
544 /* initialize png struct stuff */
545 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
546 (png_voidp) NULL, user_error_fn, user_warning_fn);
548 if (pngdec->png == NULL) {
549 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
550 ("Failed to initialize png structure"));
554 pngdec->info = png_create_info_struct (pngdec->png);
555 if (pngdec->info == NULL) {
556 gst_pngdec_libpng_clear (pngdec);
557 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
558 ("Failed to initialize info structure"));
562 pngdec->endinfo = png_create_info_struct (pngdec->png);
563 if (pngdec->endinfo == NULL) {
564 gst_pngdec_libpng_clear (pngdec);
565 GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
566 ("Failed to initialize endinfo structure"));
570 pngdec->setup = TRUE;
573 return pngdec->setup;
576 static GstStateChangeReturn
577 gst_pngdec_change_state (GstElement * element, GstStateChange transition)
579 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
580 GstPngDec *pngdec = NULL;
582 pngdec = GST_PNGDEC (element);
584 switch (transition) {
585 case GST_STATE_CHANGE_READY_TO_PAUSED:
586 gst_pngdec_libpng_init (pngdec);
588 case GST_STATE_CHANGE_PAUSED_TO_READY:
589 gst_pngdec_libpng_clear (pngdec);
595 ret = parent_class->change_state (element, transition);
600 /* this function gets called when we activate ourselves in push mode. */
602 gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active)
606 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
608 pngdec->ret = GST_FLOW_OK;
611 /* Let libpng come back here on error */
612 if (setjmp (png_jmpbuf (pngdec->png))) {
613 GST_LOG ("failed setting up libpng jumb");
614 gst_pngdec_libpng_clear (pngdec);
618 GST_LOG ("setting up progressive loading callbacks");
619 png_set_progressive_read_fn (pngdec->png, pngdec,
620 user_info_callback, user_endrow_callback, user_end_callback);
626 /* this function gets called when we activate ourselves in pull mode.
627 * We can perform random access to the resource and we start a task
628 * to start reading */
630 gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
634 pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
637 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
640 return gst_pad_stop_task (sinkpad);
644 /* this function is called when the pad is activated and should start
647 * We check if we can do random access to decide if we work push or
651 gst_pngdec_sink_activate (GstPad * sinkpad)
653 if (gst_pad_check_pull_range (sinkpad)) {
654 return gst_pad_activate_pull (sinkpad, TRUE);
656 return gst_pad_activate_push (sinkpad, TRUE);