1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2012 Collabora Ltd.
4  *	Author : Edward Hervey <edward@collabora.com>
5  * Copyright (C) 2013 Collabora Ltd.
6  *	Author : Sebastian Dröge <sebastian.droege@collabora.co.uk>
7  *	         Olivier Crete <olivier.crete@collabora.com>
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20 /**
21  * SECTION:element-pngdec
22  *
23  * Decodes png images. If there is no framerate set on sink caps, it sends EOS
24  * after the first picture.
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include "gstpngdec.h"
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <gst/base/gstbytereader.h>
36 #include <gst/video/video.h>
37 #include <gst/video/gstvideometa.h>
38 #include <gst/video/gstvideopool.h>
39 #include <gst/gst-i18n-plugin.h>
40 
41 GST_DEBUG_CATEGORY_STATIC (pngdec_debug);
42 #define GST_CAT_DEFAULT pngdec_debug
43 
44 static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
45 
46 static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);
47 
48 static gboolean gst_pngdec_start (GstVideoDecoder * decoder);
49 static gboolean gst_pngdec_stop (GstVideoDecoder * decoder);
50 static gboolean gst_pngdec_flush (GstVideoDecoder * decoder);
51 static gboolean gst_pngdec_set_format (GstVideoDecoder * Decoder,
52     GstVideoCodecState * state);
53 static GstFlowReturn gst_pngdec_parse (GstVideoDecoder * decoder,
54     GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos);
55 static GstFlowReturn gst_pngdec_handle_frame (GstVideoDecoder * decoder,
56     GstVideoCodecFrame * frame);
57 static gboolean gst_pngdec_decide_allocation (GstVideoDecoder * decoder,
58     GstQuery * query);
59 static gboolean gst_pngdec_sink_event (GstVideoDecoder * bdec,
60     GstEvent * event);
61 
62 #define parent_class gst_pngdec_parent_class
63 G_DEFINE_TYPE (GstPngDec, gst_pngdec, GST_TYPE_VIDEO_DECODER);
64 
65 static GstStaticPadTemplate gst_pngdec_src_pad_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
67     GST_PAD_SRC,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
70         ("{ RGBA, RGB, ARGB64, GRAY8, GRAY16_BE }"))
71     );
72 
73 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
75     GST_PAD_SINK,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS ("image/png")
78     );
79 
80 static void
gst_pngdec_class_init(GstPngDecClass * klass)81 gst_pngdec_class_init (GstPngDecClass * klass)
82 {
83   GstElementClass *element_class = (GstElementClass *) klass;
84   GstVideoDecoderClass *vdec_class = (GstVideoDecoderClass *) klass;
85 
86   gst_element_class_add_static_pad_template (element_class,
87       &gst_pngdec_src_pad_template);
88   gst_element_class_add_static_pad_template (element_class,
89       &gst_pngdec_sink_pad_template);
90   gst_element_class_set_static_metadata (element_class, "PNG image decoder",
91       "Codec/Decoder/Image", "Decode a png video frame to a raw image",
92       "Wim Taymans <wim@fluendo.com>");
93 
94   vdec_class->start = gst_pngdec_start;
95   vdec_class->stop = gst_pngdec_stop;
96   vdec_class->flush = gst_pngdec_flush;
97   vdec_class->set_format = gst_pngdec_set_format;
98   vdec_class->parse = gst_pngdec_parse;
99   vdec_class->handle_frame = gst_pngdec_handle_frame;
100   vdec_class->decide_allocation = gst_pngdec_decide_allocation;
101   vdec_class->sink_event = gst_pngdec_sink_event;
102 
103   GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
104 }
105 
106 static void
gst_pngdec_init(GstPngDec * pngdec)107 gst_pngdec_init (GstPngDec * pngdec)
108 {
109   pngdec->png = NULL;
110   pngdec->info = NULL;
111   pngdec->endinfo = NULL;
112 
113   pngdec->color_type = -1;
114 
115   pngdec->image_ready = FALSE;
116   pngdec->read_data = 0;
117 
118   gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
119       (pngdec), TRUE);
120   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (pngdec));
121 }
122 
123 static void
user_error_fn(png_structp png_ptr,png_const_charp error_msg)124 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
125 {
126   GST_ERROR ("%s", error_msg);
127 }
128 
129 static void
user_warning_fn(png_structp png_ptr,png_const_charp warning_msg)130 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
131 {
132   GST_WARNING ("%s", warning_msg);
133 }
134 
135 static void
user_info_callback(png_structp png_ptr,png_infop info)136 user_info_callback (png_structp png_ptr, png_infop info)
137 {
138   GstPngDec *pngdec = NULL;
139   GstFlowReturn ret;
140 
141   GST_LOG ("info ready");
142 
143   pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
144   /* Generate the caps and configure */
145   ret = gst_pngdec_caps_create_and_set (pngdec);
146   if (ret != GST_FLOW_OK) {
147     goto beach;
148   }
149 
150   /* Allocate output buffer */
151   ret =
152       gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (pngdec),
153       pngdec->current_frame);
154   if (G_UNLIKELY (ret != GST_FLOW_OK))
155     GST_DEBUG_OBJECT (pngdec, "failed to acquire buffer");
156 
157 beach:
158   pngdec->ret = ret;
159 }
160 
161 static gboolean
gst_pngdec_set_format(GstVideoDecoder * decoder,GstVideoCodecState * state)162 gst_pngdec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
163 {
164   GstPngDec *pngdec = (GstPngDec *) decoder;
165 
166   if (pngdec->input_state)
167     gst_video_codec_state_unref (pngdec->input_state);
168   pngdec->input_state = gst_video_codec_state_ref (state);
169 
170   /* We'll set format later on */
171 
172   return TRUE;
173 }
174 
175 static void
user_endrow_callback(png_structp png_ptr,png_bytep new_row,png_uint_32 row_num,int pass)176 user_endrow_callback (png_structp png_ptr, png_bytep new_row,
177     png_uint_32 row_num, int pass)
178 {
179   GstPngDec *pngdec = NULL;
180 
181   pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
182 
183   /* If buffer_out doesn't exist, it means buffer_alloc failed, which
184    * will already have set the return code */
185   if (new_row && GST_IS_BUFFER (pngdec->current_frame->output_buffer)) {
186     GstVideoFrame frame;
187     GstBuffer *buffer = pngdec->current_frame->output_buffer;
188     size_t offset;
189     guint8 *data;
190 
191     if (!gst_video_frame_map (&frame, &pngdec->output_state->info, buffer,
192             GST_MAP_WRITE)) {
193       pngdec->ret = GST_FLOW_ERROR;
194       return;
195     }
196 
197     data = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
198     offset = row_num * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
199     GST_LOG ("got row %u at pass %d, copying in buffer %p at offset %"
200         G_GSIZE_FORMAT, (guint) row_num, pass,
201         pngdec->current_frame->output_buffer, offset);
202     png_progressive_combine_row (pngdec->png, data + offset, new_row);
203     gst_video_frame_unmap (&frame);
204     pngdec->ret = GST_FLOW_OK;
205   } else
206     pngdec->ret = GST_FLOW_OK;
207 }
208 
209 static void
user_end_callback(png_structp png_ptr,png_infop info)210 user_end_callback (png_structp png_ptr, png_infop info)
211 {
212   GstPngDec *pngdec = NULL;
213 
214   pngdec = GST_PNGDEC (png_get_io_ptr (png_ptr));
215 
216   GST_LOG_OBJECT (pngdec, "and we are done reading this image");
217 
218   if (!pngdec->current_frame->output_buffer)
219     return;
220 
221   gst_buffer_unmap (pngdec->current_frame->input_buffer,
222       &pngdec->current_frame_map);
223 
224   pngdec->ret =
225       gst_video_decoder_finish_frame (GST_VIDEO_DECODER (pngdec),
226       pngdec->current_frame);
227 
228   pngdec->image_ready = TRUE;
229 }
230 
231 
232 static GstFlowReturn
gst_pngdec_caps_create_and_set(GstPngDec * pngdec)233 gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
234 {
235   GstFlowReturn ret = GST_FLOW_OK;
236   gint bpc = 0, color_type;
237   png_uint_32 width, height;
238   GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
239 
240   g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
241 
242   /* Get bits per channel */
243   bpc = png_get_bit_depth (pngdec->png, pngdec->info);
244 
245   /* Get Color type */
246   color_type = png_get_color_type (pngdec->png, pngdec->info);
247 
248   /* Add alpha channel if 16-bit depth, but not for GRAY images */
249   if ((bpc > 8) && (color_type != PNG_COLOR_TYPE_GRAY)) {
250     png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
251     png_set_swap (pngdec->png);
252   }
253 #if 0
254   /* We used to have this HACK to reverse the outgoing bytes, but the problem
255    * that originally required the hack seems to have been in videoconvert's
256    * RGBA descriptions. It doesn't seem needed now that's fixed, but might
257    * still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
258   if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
259     png_set_bgr (pngdec->png);
260 #endif
261 
262   /* Gray scale with alpha channel converted to RGB */
263   if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
264     GST_LOG_OBJECT (pngdec,
265         "converting grayscale png with alpha channel to RGB");
266     png_set_gray_to_rgb (pngdec->png);
267   }
268 
269   /* Gray scale converted to upscaled to 8 bits */
270   if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
271       (color_type == PNG_COLOR_TYPE_GRAY)) {
272     if (bpc < 8) {              /* Convert to 8 bits */
273       GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
274 #if PNG_LIBPNG_VER < 10400
275       png_set_gray_1_2_4_to_8 (pngdec->png);
276 #else
277       png_set_expand_gray_1_2_4_to_8 (pngdec->png);
278 #endif
279     }
280   }
281 
282   /* Palette converted to RGB */
283   if (color_type == PNG_COLOR_TYPE_PALETTE) {
284     GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
285     png_set_palette_to_rgb (pngdec->png);
286   }
287 
288   png_set_interlace_handling (pngdec->png);
289 
290   /* Update the info structure */
291   png_read_update_info (pngdec->png, pngdec->info);
292 
293   /* Get IHDR header again after transformation settings */
294   png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
295       &bpc, &pngdec->color_type, NULL, NULL, NULL);
296 
297   GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", (gint) width,
298       (gint) height);
299 
300   switch (pngdec->color_type) {
301     case PNG_COLOR_TYPE_RGB:
302       GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
303       if (bpc == 8)
304         format = GST_VIDEO_FORMAT_RGB;
305       break;
306     case PNG_COLOR_TYPE_RGB_ALPHA:
307       GST_LOG_OBJECT (pngdec,
308           "we have an alpha channel, depth is 32 or 64 bits");
309       if (bpc == 8)
310         format = GST_VIDEO_FORMAT_RGBA;
311       else if (bpc == 16)
312         format = GST_VIDEO_FORMAT_ARGB64;
313       break;
314     case PNG_COLOR_TYPE_GRAY:
315       GST_LOG_OBJECT (pngdec,
316           "We have an gray image, depth is 8 or 16 (be) bits");
317       if (bpc == 8)
318         format = GST_VIDEO_FORMAT_GRAY8;
319       else if (bpc == 16)
320         format = GST_VIDEO_FORMAT_GRAY16_BE;
321       break;
322     default:
323       break;
324   }
325 
326   if (format == GST_VIDEO_FORMAT_UNKNOWN) {
327     GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
328         ("pngdec does not support this color type"));
329     ret = GST_FLOW_NOT_SUPPORTED;
330     goto beach;
331   }
332 
333   /* Check if output state changed */
334   if (pngdec->output_state) {
335     GstVideoInfo *info = &pngdec->output_state->info;
336 
337     if (width == GST_VIDEO_INFO_WIDTH (info) &&
338         height == GST_VIDEO_INFO_HEIGHT (info) &&
339         GST_VIDEO_INFO_FORMAT (info) == format) {
340       goto beach;
341     }
342     gst_video_codec_state_unref (pngdec->output_state);
343   }
344 #ifdef HAVE_LIBPNG_1_5
345   if ((pngdec->color_type & PNG_COLOR_MASK_COLOR)
346       && !(pngdec->color_type & PNG_COLOR_MASK_PALETTE)
347       && png_get_valid (pngdec->png, pngdec->info, PNG_INFO_iCCP)) {
348     png_charp icc_name;
349     png_bytep icc_profile;
350     int icc_compression_type;
351     png_uint_32 icc_proflen = 0;
352     png_uint_32 ret = png_get_iCCP (pngdec->png, pngdec->info, &icc_name,
353         &icc_compression_type, &icc_profile, &icc_proflen);
354 
355     if ((ret & PNG_INFO_iCCP)) {
356       gpointer gst_icc_prof = g_memdup (icc_profile, icc_proflen);
357       GstBuffer *tagbuffer = NULL;
358       GstSample *tagsample = NULL;
359       GstTagList *taglist = NULL;
360       GstStructure *info = NULL;
361       GstCaps *caps;
362 
363       GST_DEBUG_OBJECT (pngdec, "extracted ICC profile '%s' length=%i",
364           icc_name, (guint32) icc_proflen);
365 
366       tagbuffer = gst_buffer_new_wrapped (gst_icc_prof, icc_proflen);
367 
368       caps = gst_caps_new_empty_simple ("application/vnd.iccprofile");
369       info = gst_structure_new_empty ("application/vnd.iccprofile");
370 
371       if (icc_name)
372         gst_structure_set (info, "icc-name", G_TYPE_STRING, icc_name, NULL);
373 
374       tagsample = gst_sample_new (tagbuffer, caps, NULL, info);
375 
376       gst_buffer_unref (tagbuffer);
377       gst_caps_unref (caps);
378 
379       taglist = gst_tag_list_new_empty ();
380       gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, GST_TAG_ATTACHMENT,
381           tagsample, NULL);
382       gst_sample_unref (tagsample);
383 
384       gst_video_decoder_merge_tags (GST_VIDEO_DECODER (pngdec), taglist,
385           GST_TAG_MERGE_APPEND);
386       gst_tag_list_unref (taglist);
387     }
388   }
389 #endif
390 
391   pngdec->output_state =
392       gst_video_decoder_set_output_state (GST_VIDEO_DECODER (pngdec), format,
393       width, height, pngdec->input_state);
394   gst_video_decoder_negotiate (GST_VIDEO_DECODER (pngdec));
395   GST_DEBUG ("Final %d %d", GST_VIDEO_INFO_WIDTH (&pngdec->output_state->info),
396       GST_VIDEO_INFO_HEIGHT (&pngdec->output_state->info));
397 
398 beach:
399   return ret;
400 }
401 
402 static GstFlowReturn
gst_pngdec_handle_frame(GstVideoDecoder * decoder,GstVideoCodecFrame * frame)403 gst_pngdec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
404 {
405   GstPngDec *pngdec = (GstPngDec *) decoder;
406   GstFlowReturn ret = GST_FLOW_OK;
407 
408   GST_LOG_OBJECT (pngdec, "Got buffer, size=%u",
409       (guint) gst_buffer_get_size (frame->input_buffer));
410 
411   /* Let libpng come back here on error */
412   if (setjmp (png_jmpbuf (pngdec->png))) {
413     GST_WARNING_OBJECT (pngdec, "error during decoding");
414     ret = GST_FLOW_ERROR;
415     goto beach;
416   }
417 
418   pngdec->current_frame = frame;
419 
420   /* Progressive loading of the PNG image */
421   if (!gst_buffer_map (frame->input_buffer, &pngdec->current_frame_map,
422           GST_MAP_READ)) {
423     GST_WARNING_OBJECT (pngdec, "Failed to map input buffer");
424     ret = GST_FLOW_ERROR;
425     goto beach;
426   }
427 
428   png_process_data (pngdec->png, pngdec->info,
429       pngdec->current_frame_map.data, pngdec->current_frame_map.size);
430 
431   if (pngdec->image_ready) {
432     /* Reset ourselves for the next frame */
433     gst_pngdec_flush (decoder);
434     GST_LOG_OBJECT (pngdec, "setting up callbacks for next frame");
435     png_set_progressive_read_fn (pngdec->png, pngdec,
436         user_info_callback, user_endrow_callback, user_end_callback);
437     pngdec->image_ready = FALSE;
438   } else {
439     /* An error happened and we have to unmap */
440     gst_buffer_unmap (pngdec->current_frame->input_buffer,
441         &pngdec->current_frame_map);
442   }
443 
444   ret = pngdec->ret;
445 beach:
446 
447   return ret;
448 }
449 
450 /* Based on pngparse */
451 #define PNG_SIGNATURE G_GUINT64_CONSTANT (0x89504E470D0A1A0A)
452 
453 static GstFlowReturn
gst_pngdec_parse(GstVideoDecoder * decoder,GstVideoCodecFrame * frame,GstAdapter * adapter,gboolean at_eos)454 gst_pngdec_parse (GstVideoDecoder * decoder, GstVideoCodecFrame * frame,
455     GstAdapter * adapter, gboolean at_eos)
456 {
457   gsize toadd = 0;
458   GstByteReader reader;
459   gconstpointer data;
460   guint64 signature;
461   gsize size;
462   GstPngDec *pngdec = (GstPngDec *) decoder;
463 
464   GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
465 
466   /* FIXME : The overhead of using scan_uint32 is massive */
467 
468   size = gst_adapter_available (adapter);
469   GST_DEBUG ("Parsing PNG image data (%" G_GSIZE_FORMAT " bytes)", size);
470 
471   if (size < 8)
472     goto need_more_data;
473 
474   data = gst_adapter_map (adapter, size);
475   gst_byte_reader_init (&reader, data, size);
476 
477   if (pngdec->read_data == 0) {
478     if (!gst_byte_reader_peek_uint64_be (&reader, &signature))
479       goto need_more_data;
480 
481     if (signature != PNG_SIGNATURE) {
482       for (;;) {
483         guint offset;
484 
485         offset = gst_byte_reader_masked_scan_uint32 (&reader, 0xffffffff,
486             0x89504E47, 0, gst_byte_reader_get_remaining (&reader));
487 
488         if (offset == -1) {
489           gst_adapter_flush (adapter,
490               gst_byte_reader_get_remaining (&reader) - 4);
491           goto need_more_data;
492         }
493 
494         if (!gst_byte_reader_skip (&reader, offset))
495           goto need_more_data;
496 
497         if (!gst_byte_reader_peek_uint64_be (&reader, &signature))
498           goto need_more_data;
499 
500         if (signature == PNG_SIGNATURE) {
501           /* We're skipping, go out, we'll be back */
502           gst_adapter_flush (adapter, gst_byte_reader_get_pos (&reader));
503           goto need_more_data;
504         }
505         if (!gst_byte_reader_skip (&reader, 4))
506           goto need_more_data;
507       }
508     }
509     pngdec->read_data = 8;
510   }
511 
512   if (!gst_byte_reader_skip (&reader, pngdec->read_data))
513     goto need_more_data;
514 
515   for (;;) {
516     guint32 length;
517     guint32 code;
518 
519     if (!gst_byte_reader_get_uint32_be (&reader, &length))
520       goto need_more_data;
521     if (!gst_byte_reader_get_uint32_le (&reader, &code))
522       goto need_more_data;
523 
524     if (!gst_byte_reader_skip (&reader, length + 4))
525       goto need_more_data;
526 
527     if (code == GST_MAKE_FOURCC ('I', 'E', 'N', 'D')) {
528       /* Have complete frame */
529       toadd = gst_byte_reader_get_pos (&reader);
530       GST_DEBUG_OBJECT (decoder, "Have complete frame of size %" G_GSIZE_FORMAT,
531           toadd);
532       pngdec->read_data = 0;
533       goto have_full_frame;
534     } else
535       pngdec->read_data += length + 12;
536   }
537 
538   g_assert_not_reached ();
539   return GST_FLOW_ERROR;
540 
541 need_more_data:
542   return GST_VIDEO_DECODER_FLOW_NEED_DATA;
543 
544 have_full_frame:
545   if (toadd)
546     gst_video_decoder_add_to_frame (decoder, toadd);
547   return gst_video_decoder_have_frame (decoder);
548 }
549 
550 static gboolean
gst_pngdec_decide_allocation(GstVideoDecoder * bdec,GstQuery * query)551 gst_pngdec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
552 {
553   GstBufferPool *pool = NULL;
554   GstStructure *config;
555 
556   if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
557     return FALSE;
558 
559   if (gst_query_get_n_allocation_pools (query) > 0)
560     gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
561 
562   if (pool == NULL)
563     return FALSE;
564 
565   config = gst_buffer_pool_get_config (pool);
566   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
567     gst_buffer_pool_config_add_option (config,
568         GST_BUFFER_POOL_OPTION_VIDEO_META);
569   }
570   gst_buffer_pool_set_config (pool, config);
571   gst_object_unref (pool);
572 
573   return TRUE;
574 }
575 
576 static gboolean
gst_pngdec_sink_event(GstVideoDecoder * bdec,GstEvent * event)577 gst_pngdec_sink_event (GstVideoDecoder * bdec, GstEvent * event)
578 {
579   const GstSegment *segment;
580 
581   if (GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT)
582     goto done;
583 
584   gst_event_parse_segment (event, &segment);
585 
586   if (segment->format == GST_FORMAT_TIME)
587     gst_video_decoder_set_packetized (bdec, TRUE);
588   else
589     gst_video_decoder_set_packetized (bdec, FALSE);
590 
591 done:
592   return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (bdec, event);
593 }
594 
595 static gboolean
gst_pngdec_libpng_init(GstPngDec * pngdec)596 gst_pngdec_libpng_init (GstPngDec * pngdec)
597 {
598   g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);
599 
600   GST_LOG ("init libpng structures");
601 
602   /* initialize png struct stuff */
603   pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
604       (png_voidp) NULL, user_error_fn, user_warning_fn);
605 
606   if (pngdec->png == NULL)
607     goto init_failed;
608 
609   pngdec->info = png_create_info_struct (pngdec->png);
610   if (pngdec->info == NULL)
611     goto info_failed;
612 
613   pngdec->endinfo = png_create_info_struct (pngdec->png);
614   if (pngdec->endinfo == NULL)
615     goto endinfo_failed;
616 
617   png_set_progressive_read_fn (pngdec->png, pngdec,
618       user_info_callback, user_endrow_callback, user_end_callback);
619 
620   return TRUE;
621 
622   /* ERRORS */
623 init_failed:
624   {
625     GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
626         ("Failed to initialize png structure"));
627     return FALSE;
628   }
629 info_failed:
630   {
631     GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
632         ("Failed to initialize info structure"));
633     return FALSE;
634   }
635 endinfo_failed:
636   {
637     GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
638         ("Failed to initialize endinfo structure"));
639     return FALSE;
640   }
641 }
642 
643 
644 static void
gst_pngdec_libpng_clear(GstPngDec * pngdec)645 gst_pngdec_libpng_clear (GstPngDec * pngdec)
646 {
647   png_infopp info = NULL, endinfo = NULL;
648 
649   GST_LOG ("cleaning up libpng structures");
650 
651   if (pngdec->info) {
652     info = &pngdec->info;
653   }
654 
655   if (pngdec->endinfo) {
656     endinfo = &pngdec->endinfo;
657   }
658 
659   if (pngdec->png) {
660     png_destroy_read_struct (&(pngdec->png), info, endinfo);
661     pngdec->png = NULL;
662     pngdec->info = NULL;
663     pngdec->endinfo = NULL;
664   }
665 
666   pngdec->color_type = -1;
667   pngdec->read_data = 0;
668 }
669 
670 static gboolean
gst_pngdec_start(GstVideoDecoder * decoder)671 gst_pngdec_start (GstVideoDecoder * decoder)
672 {
673   GstPngDec *pngdec = (GstPngDec *) decoder;
674 
675   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (pngdec), FALSE);
676   gst_pngdec_libpng_init (pngdec);
677 
678   return TRUE;
679 }
680 
681 static gboolean
gst_pngdec_stop(GstVideoDecoder * decoder)682 gst_pngdec_stop (GstVideoDecoder * decoder)
683 {
684   GstPngDec *pngdec = (GstPngDec *) decoder;
685 
686   gst_pngdec_libpng_clear (pngdec);
687 
688   if (pngdec->input_state) {
689     gst_video_codec_state_unref (pngdec->input_state);
690     pngdec->input_state = NULL;
691   }
692   if (pngdec->output_state) {
693     gst_video_codec_state_unref (pngdec->output_state);
694     pngdec->output_state = NULL;
695   }
696 
697   return TRUE;
698 }
699 
700 /* Clean up the libpng structures */
701 static gboolean
gst_pngdec_flush(GstVideoDecoder * decoder)702 gst_pngdec_flush (GstVideoDecoder * decoder)
703 {
704   gst_pngdec_libpng_clear ((GstPngDec *) decoder);
705   gst_pngdec_libpng_init ((GstPngDec *) decoder);
706 
707   return TRUE;
708 }
709