static void user_info_callback (png_structp png_ptr, png_infop info) { GstPngDec *pngdec = NULL; GstFlowReturn ret; GST_LOG ("info ready"); pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr)); /* Generate the caps and configure */ ret = gst_pngdec_caps_create_and_set (pngdec); if (ret != GST_FLOW_OK) { goto beach; } /* Allocate output buffer */ ret = gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (pngdec), pngdec->current_frame); if (G_UNLIKELY (ret != GST_FLOW_OK)) GST_DEBUG_OBJECT (pngdec, "failed to acquire buffer"); beach: pngdec->ret = ret; }
static void user_endrow_callback (png_structp png_ptr, png_bytep new_row, png_uint_32 row_num, int pass) { GstPngDec *pngdec = NULL; pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr)); /* If buffer_out doesn't exist, it means buffer_alloc failed, which * will already have set the return code */ if (new_row && GST_IS_BUFFER (pngdec->current_frame->output_buffer)) { GstVideoFrame frame; GstBuffer *buffer = pngdec->current_frame->output_buffer; size_t offset; guint8 *data; if (!gst_video_frame_map (&frame, &pngdec->output_state->info, buffer, GST_MAP_WRITE)) { pngdec->ret = GST_FLOW_ERROR; return; } data = GST_VIDEO_FRAME_COMP_DATA (&frame, 0); offset = row_num * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0); GST_LOG ("got row %u at pass %d, copying in buffer %p at offset %" G_GSIZE_FORMAT, (guint) row_num, pass, pngdec->current_frame->output_buffer, offset); png_progressive_combine_row (pngdec->png, data + offset, new_row); gst_video_frame_unmap (&frame); pngdec->ret = GST_FLOW_OK; } else pngdec->ret = GST_FLOW_OK; }
static GstStateChangeReturn gst_pngdec_change_state (GstElement * element, GstStateChange transition) { GstStateChangeReturn ret; GstPngDec *pngdec; pngdec = GST_PNGDEC (element); switch (transition) { case GST_STATE_CHANGE_READY_TO_PAUSED: gst_pngdec_libpng_init (pngdec); pngdec->need_newsegment = TRUE; pngdec->framed = FALSE; pngdec->ret = GST_FLOW_OK; gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED); break; default: break; } ret = parent_class->change_state (element, transition); if (ret != GST_STATE_CHANGE_SUCCESS) return ret; switch (transition) { case GST_STATE_CHANGE_PAUSED_TO_READY: gst_pngdec_libpng_clear (pngdec); break; default: break; } return ret; }
/* this function gets called when we activate ourselves in push mode. */ static gboolean gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active) { GstPngDec *pngdec; pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad)); pngdec->ret = GST_FLOW_OK; if (active) { /* Let libpng come back here on error */ if (setjmp (png_jmpbuf (pngdec->png))) goto setup_failed; GST_LOG ("setting up progressive loading callbacks"); png_set_progressive_read_fn (pngdec->png, pngdec, user_info_callback, user_endrow_callback, user_end_callback); } return TRUE; setup_failed: { GST_LOG ("failed setting up libpng jmpbuf"); gst_pngdec_libpng_clear (pngdec); return FALSE; } }
static gboolean gst_pngdec_sink_setcaps (GstPad * pad, GstCaps * caps) { GstStructure *s; GstPngDec *pngdec; gint num, denom; pngdec = GST_PNGDEC (gst_pad_get_parent (pad)); s = gst_caps_get_structure (caps, 0); if (gst_structure_get_fraction (s, "framerate", &num, &denom)) { GST_DEBUG_OBJECT (pngdec, "framed input"); pngdec->framed = TRUE; pngdec->fps_n = num; pngdec->fps_d = denom; } else { GST_DEBUG_OBJECT (pngdec, "single picture input"); pngdec->framed = FALSE; pngdec->fps_n = 0; pngdec->fps_d = 1; } gst_object_unref (pngdec); return TRUE; }
static void user_endrow_callback (png_structp png_ptr, png_bytep new_row, png_uint_32 row_num, int pass) { GstPngDec *pngdec = NULL; pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr)); /* FIXME: implement interlaced pictures */ /* If buffer_out doesn't exist, it means buffer_alloc failed, which * will already have set the return code */ if (GST_IS_BUFFER (pngdec->current_frame->output_buffer)) { GstVideoFrame frame; GstBuffer *buffer = pngdec->current_frame->output_buffer; size_t offset; gint width; guint8 *data; if (!gst_video_frame_map (&frame, &pngdec->output_state->info, buffer, GST_MAP_WRITE)) { pngdec->ret = GST_FLOW_ERROR; return; } data = GST_VIDEO_FRAME_COMP_DATA (&frame, 0); offset = row_num * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0); GST_LOG ("got row %u, copying in buffer %p at offset %" G_GSIZE_FORMAT, (guint) row_num, pngdec->current_frame->output_buffer, offset); width = GST_ROUND_UP_4 (png_get_rowbytes (pngdec->png, pngdec->info)); memcpy (data + offset, new_row, width); gst_video_frame_unmap (&frame); pngdec->ret = GST_FLOW_OK; } }
static void user_end_callback (png_structp png_ptr, png_infop info) { GstPngDec *pngdec = NULL; pngdec = GST_PNGDEC (png_ptr->io_ptr); GST_LOG_OBJECT (pngdec, "and we are done reading this image"); if (!pngdec->buffer_out) return; if (GST_CLOCK_TIME_IS_VALID (pngdec->in_timestamp)) GST_BUFFER_TIMESTAMP (pngdec->buffer_out) = pngdec->in_timestamp; if (GST_CLOCK_TIME_IS_VALID (pngdec->in_duration)) GST_BUFFER_DURATION (pngdec->buffer_out) = pngdec->in_duration; /* buffer clipping */ if (buffer_clip (pngdec, pngdec->buffer_out)) { /* Push our buffer and then EOS if needed */ GST_LOG_OBJECT (pngdec, "pushing buffer with ts=%" GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (pngdec->buffer_out))); pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out); } else { GST_LOG_OBJECT (pngdec, "dropped decoded buffer"); gst_buffer_unref (pngdec->buffer_out); } pngdec->buffer_out = NULL; pngdec->image_ready = TRUE; }
static void user_read_data (png_structp png_ptr, png_bytep data, png_size_t length) { GstPngDec *pngdec; GstBuffer *buffer; GstFlowReturn ret = GST_FLOW_OK; pngdec = GST_PNGDEC (png_ptr->io_ptr); GST_LOG ("reading %" G_GSIZE_FORMAT " bytes of data at offset %d", length, pngdec->offset); ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer); if ((ret != GST_FLOW_OK) || (GST_BUFFER_SIZE (buffer) != length)) goto pause; memcpy (data, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer)); gst_buffer_unref (buffer); pngdec->offset += length; return; pause: GST_INFO_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret)); gst_pad_pause_task (pngdec->sinkpad); if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) { gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ()); GST_ELEMENT_ERROR (pngdec, STREAM, FAILED, (_("Internal data stream error.")), ("stream stopped, reason %s", gst_flow_get_name (ret))); } }
static void user_info_callback (png_structp png_ptr, png_infop info) { GstPngDec *pngdec = NULL; GstFlowReturn ret = GST_FLOW_OK; size_t buffer_size; GstBuffer *buffer = NULL; pngdec = GST_PNGDEC (png_ptr->io_ptr); GST_LOG ("info ready"); /* Generate the caps and configure */ ret = gst_pngdec_caps_create_and_set (pngdec); if (ret != GST_FLOW_OK) { goto beach; } /* Allocate output buffer */ pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info); buffer_size = pngdec->height * GST_ROUND_UP_4 (pngdec->rowbytes); ret = gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE, buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer); if (ret != GST_FLOW_OK) { goto beach; } pngdec->buffer_out = buffer; beach: pngdec->ret = ret; }
static gboolean gst_pngdec_sink_event (GstPad * pad, GstEvent * event) { GstPngDec *pngdec; gboolean res; pngdec = GST_PNGDEC (gst_pad_get_parent (pad)); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_NEWSEGMENT:{ gdouble rate, arate; gboolean update; gint64 start, stop, position; GstFormat fmt; gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt, &start, &stop, &position); gst_segment_set_newsegment_full (&pngdec->segment, update, rate, arate, fmt, start, stop, position); GST_LOG_OBJECT (pngdec, "NEWSEGMENT (%s)", gst_format_get_name (fmt)); if (fmt == GST_FORMAT_TIME) { pngdec->need_newsegment = FALSE; res = gst_pad_push_event (pngdec->srcpad, event); } else { gst_event_unref (event); res = TRUE; } break; } case GST_EVENT_FLUSH_STOP: { gst_pngdec_libpng_clear (pngdec); gst_pngdec_libpng_init (pngdec); png_set_progressive_read_fn (pngdec->png, pngdec, user_info_callback, user_endrow_callback, user_end_callback); pngdec->ret = GST_FLOW_OK; gst_segment_init (&pngdec->segment, GST_FORMAT_UNDEFINED); res = gst_pad_push_event (pngdec->srcpad, event); break; } case GST_EVENT_EOS: { GST_LOG_OBJECT (pngdec, "EOS"); gst_pngdec_libpng_clear (pngdec); pngdec->ret = GST_FLOW_UNEXPECTED; res = gst_pad_push_event (pngdec->srcpad, event); break; } default: res = gst_pad_push_event (pngdec->srcpad, event); break; } gst_object_unref (pngdec); return res; }
static void user_read_data (png_structp png_ptr, png_bytep data, png_size_t length) { GstPngDec *pngdec; GstBuffer *buffer; GstFlowReturn ret = GST_FLOW_OK; guint size; pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr)); GST_LOG ("reading %" G_GSIZE_FORMAT " bytes of data at offset %d", length, pngdec->offset); ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer); if (ret != GST_FLOW_OK) goto pause; size = GST_BUFFER_SIZE (buffer); if (size != length) goto short_buffer; memcpy (data, GST_BUFFER_DATA (buffer), size); gst_buffer_unref (buffer); pngdec->offset += length; return; /* ERRORS */ pause: { GST_INFO_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret)); gst_pad_pause_task (pngdec->sinkpad); if (ret == GST_FLOW_UNEXPECTED) { gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ()); } else if (ret < GST_FLOW_UNEXPECTED || ret == GST_FLOW_NOT_LINKED) { GST_ELEMENT_ERROR (pngdec, STREAM, FAILED, (_("Internal data stream error.")), ("stream stopped, reason %s", gst_flow_get_name (ret))); gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ()); } png_error (png_ptr, "Internal data stream error."); return; } short_buffer: { gst_buffer_unref (buffer); GST_ELEMENT_ERROR (pngdec, STREAM, FAILED, (_("Internal data stream error.")), ("Read %u, needed %" G_GSIZE_FORMAT "bytes", size, length)); ret = GST_FLOW_ERROR; goto pause; } }
/* this function gets called when we activate ourselves in pull mode. * We can perform random access to the resource and we start a task * to start reading */ static gboolean gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active) { GstPngDec *pngdec; pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad)); if (active) { return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task, sinkpad); } else { return gst_pad_stop_task (sinkpad); } }
static void user_end_callback (png_structp png_ptr, png_infop info) { GstPngDec *pngdec = NULL; pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr)); GST_LOG_OBJECT (pngdec, "and we are done reading this image"); if (!pngdec->current_frame->output_buffer) return; gst_buffer_unmap (pngdec->current_frame->input_buffer, &pngdec->current_frame_map); pngdec->ret = gst_video_decoder_finish_frame (GST_VIDEO_DECODER (pngdec), pngdec->current_frame); pngdec->image_ready = TRUE; }
static void user_endrow_callback (png_structp png_ptr, png_bytep new_row, png_uint_32 row_num, int pass) { GstPngDec *pngdec = NULL; pngdec = GST_PNGDEC (png_ptr->io_ptr); /* FIXME: implement interlaced pictures */ /* If buffer_out doesn't exist, it means buffer_alloc failed, which * will already have set the return code */ if (GST_IS_BUFFER (pngdec->buffer_out)) { size_t offset = row_num * pngdec->rowbytes; GST_LOG ("got row %u, copying in buffer %p at offset %" G_GSIZE_FORMAT, (guint) row_num, pngdec->buffer_out, offset); memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row, pngdec->rowbytes); pngdec->ret = GST_FLOW_OK; } }
static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstBuffer * buffer) { GstPngDec *pngdec; GstFlowReturn ret = GST_FLOW_OK; pngdec = GST_PNGDEC (gst_pad_get_parent (pad)); GST_LOG_OBJECT (pngdec, "Got buffer, size=%u", GST_BUFFER_SIZE (buffer)); if (G_UNLIKELY (!pngdec->setup)) goto not_configured; /* Something is going wrong in our callbacks */ ret = pngdec->ret; if (G_UNLIKELY (ret != GST_FLOW_OK)) { GST_WARNING_OBJECT (pngdec, "we have a pending return code of %d", ret); goto beach; } /* Let libpng come back here on error */ if (setjmp (png_jmpbuf (pngdec->png))) { GST_WARNING_OBJECT (pngdec, "error during decoding"); ret = GST_FLOW_ERROR; goto beach; } pngdec->in_timestamp = GST_BUFFER_TIMESTAMP (buffer); pngdec->in_duration = GST_BUFFER_DURATION (buffer); /* Progressive loading of the PNG image */ png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer)); if (pngdec->image_ready) { if (pngdec->framed) { /* Reset ourselves for the next frame */ gst_pngdec_libpng_clear (pngdec); gst_pngdec_libpng_init (pngdec); GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame"); png_set_progressive_read_fn (pngdec->png, pngdec, user_info_callback, user_endrow_callback, user_end_callback); } else { GST_LOG_OBJECT (pngdec, "sending EOS"); pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ()); } pngdec->image_ready = FALSE; } /* grab new return code */ ret = pngdec->ret; /* And release the buffer */ gst_buffer_unref (buffer); beach: gst_object_unref (pngdec); return ret; /* ERRORS */ not_configured: { GST_LOG_OBJECT (pngdec, "we are not configured yet"); ret = GST_FLOW_WRONG_STATE; goto beach; } }
static void gst_pngdec_task (GstPad * pad) { GstPngDec *pngdec; GstBuffer *buffer = NULL; size_t buffer_size = 0; gint i = 0; png_bytep *rows, inp; png_uint_32 rowbytes; GstFlowReturn ret = GST_FLOW_OK; pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad)); GST_LOG_OBJECT (pngdec, "read frame"); /* Let libpng come back here on error */ if (setjmp (png_jmpbuf (pngdec->png))) { ret = GST_FLOW_ERROR; goto pause; } /* Set reading callback */ png_set_read_fn (pngdec->png, pngdec, user_read_data); /* Read info */ png_read_info (pngdec->png, pngdec->info); /* Generate the caps and configure */ ret = gst_pngdec_caps_create_and_set (pngdec); if (ret != GST_FLOW_OK) { goto pause; } /* Allocate output buffer */ rowbytes = png_get_rowbytes (pngdec->png, pngdec->info); if (rowbytes > (G_MAXUINT32 - 3) || pngdec->height > G_MAXUINT32 / rowbytes) { ret = GST_FLOW_ERROR; goto pause; } rowbytes = GST_ROUND_UP_4 (rowbytes); buffer_size = pngdec->height * rowbytes; ret = gst_pad_alloc_buffer_and_set_caps (pngdec->srcpad, GST_BUFFER_OFFSET_NONE, buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer); if (ret != GST_FLOW_OK) goto pause; rows = (png_bytep *) g_malloc (sizeof (png_bytep) * pngdec->height); inp = GST_BUFFER_DATA (buffer); for (i = 0; i < pngdec->height; i++) { rows[i] = inp; inp += rowbytes; } /* Read the actual picture */ png_read_image (pngdec->png, rows); g_free (rows); /* Push the raw RGB frame */ ret = gst_pad_push (pngdec->srcpad, buffer); if (ret != GST_FLOW_OK) goto pause; /* And we are done */ gst_pad_pause_task (pngdec->sinkpad); gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ()); return; pause: { GST_INFO_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret)); gst_pad_pause_task (pngdec->sinkpad); if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) { GST_ELEMENT_ERROR (pngdec, STREAM, FAILED, (_("Internal data stream error.")), ("stream stopped, reason %s", gst_flow_get_name (ret))); gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ()); } } }