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.
21 #include "gstpngdec.h"
22 #include <gst/video/video.h>
24 static GstElementDetails gst_pngdec_details = {
26 "Codec/Decoder/Image",
27 "Decode a png video frame to a raw image",
28 "Wim Taymans <wim@fluendo.com>",
32 /* Filter signals and args */
44 static void gst_pngdec_base_init (gpointer g_class);
45 static void gst_pngdec_class_init (GstPngDecClass * klass);
46 static void gst_pngdec_init (GstPngDec * pngdec);
48 static void gst_pngdec_chain (GstPad * pad, GstData * _data);
50 static GstCaps *gst_pngdec_src_getcaps (GstPad * pad);
52 static GstElementClass *parent_class = NULL;
56 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
58 g_warning ("%s", error_msg);
62 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
64 g_warning ("%s", warning_msg);
69 gst_pngdec_get_type (void)
71 static GType pngdec_type = 0;
74 static const GTypeInfo pngdec_info = {
75 sizeof (GstPngDecClass),
78 (GClassInitFunc) gst_pngdec_class_init,
83 (GInstanceInitFunc) gst_pngdec_init,
86 pngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngDec",
92 static GstStaticPadTemplate gst_pngdec_src_pad_template =
93 GST_STATIC_PAD_TEMPLATE ("src",
96 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
99 static GstStaticPadTemplate gst_pngdec_sink_pad_template =
100 GST_STATIC_PAD_TEMPLATE ("sink",
103 GST_STATIC_CAPS ("image/png, "
104 "width = (int) [ 16, 4096 ], "
105 "height = (int) [ 16, 4096 ], " "framerate = (double) [ 0.0, MAX ]")
109 gst_pngdec_base_init (gpointer g_class)
111 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
113 gst_element_class_add_pad_template (element_class,
114 gst_static_pad_template_get (&gst_pngdec_src_pad_template));
115 gst_element_class_add_pad_template (element_class,
116 gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
117 gst_element_class_set_details (element_class, &gst_pngdec_details);
121 gst_pngdec_class_init (GstPngDecClass * klass)
123 GObjectClass *gobject_class;
124 GstElementClass *gstelement_class;
126 gobject_class = (GObjectClass *) klass;
127 gstelement_class = (GstElementClass *) klass;
129 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
133 static GstPadLinkReturn
134 gst_pngdec_sinklink (GstPad * pad, const GstCaps * caps)
137 GstStructure *structure;
139 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
141 structure = gst_caps_get_structure (caps, 0);
142 gst_structure_get_double (structure, "framerate", &pngdec->fps);
148 gst_pngdec_init (GstPngDec * pngdec)
150 pngdec->sinkpad = gst_pad_new_from_template (gst_static_pad_template_get
151 (&gst_pngdec_sink_pad_template), "sink");
152 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
154 pngdec->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
155 (&gst_pngdec_src_pad_template), "src");
156 gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
158 gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
159 gst_pad_set_link_function (pngdec->sinkpad, gst_pngdec_sinklink);
161 gst_pad_set_getcaps_function (pngdec->srcpad, gst_pngdec_src_getcaps);
166 pngdec->color_type = -1;
173 gst_pngdec_src_getcaps (GstPad * pad)
178 GstPadTemplate *templ;
181 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
182 templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);
183 caps = gst_caps_copy (gst_pad_template_get_caps (templ));
185 if (pngdec->color_type != -1) {
186 GstCaps *to_inter = NULL;
188 switch (pngdec->color_type) {
189 case PNG_COLOR_TYPE_RGB:
190 to_inter = gst_caps_new_simple ("video/x-raw-rgb",
191 "bpp", G_TYPE_INT, 24, NULL);
193 case PNG_COLOR_TYPE_RGB_ALPHA:
194 to_inter = gst_caps_new_simple ("video/x-raw-rgb",
195 "bpp", G_TYPE_INT, 32, NULL);
197 case PNG_COLOR_TYPE_GRAY:
198 case PNG_COLOR_TYPE_PALETTE:
199 case PNG_COLOR_TYPE_GRAY_ALPHA:
201 GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
202 ("pngdec does not support grayscale or paletted data yet"));
205 inter = gst_caps_intersect (caps, to_inter);
206 gst_caps_free (caps);
207 gst_caps_free (to_inter);
211 for (i = 0; i < gst_caps_get_size (caps); i++) {
212 GstStructure *structure = gst_caps_get_structure (caps, i);
214 if (pngdec->width != -1) {
215 gst_structure_set (structure, "width", G_TYPE_INT, pngdec->width, NULL);
218 if (pngdec->height != -1) {
219 gst_structure_set (structure, "height", G_TYPE_INT, pngdec->height, NULL);
222 if (pngdec->fps != -1) {
223 gst_structure_set (structure,
224 "framerate", G_TYPE_DOUBLE, pngdec->fps, NULL);
232 user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
236 dec = GST_PNGDEC (png_ptr->io_ptr);
238 if (GST_BUFFER_SIZE (dec->buffer_in) < dec->offset + length) {
239 g_warning ("reading past end of buffer");
242 memcpy (data, GST_BUFFER_DATA (dec->buffer_in) + dec->offset, length);
244 dec->offset += length;
248 gst_pngdec_chain (GstPad * pad, GstData * _data)
250 GstBuffer *buf = GST_BUFFER (_data);
252 png_uint_32 width, height;
254 png_bytep *rows, inp;
258 pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
260 if (!GST_PAD_IS_USABLE (pngdec->srcpad)) {
261 gst_buffer_unref (buf);
265 pngdec->buffer_in = buf;
268 /* initialize png struct stuff */
269 pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
270 (png_voidp) NULL, user_error_fn, user_warning_fn);
272 /* FIXME: better error handling */
273 if (pngdec->png == NULL) {
274 g_warning ("Failed to initialize png structure");
278 pngdec->info = png_create_info_struct (pngdec->png);
279 if (pngdec->info == NULL) {
280 g_warning ("Failed to initialize info structure");
281 png_destroy_read_struct (&(pngdec->png), (png_infopp) NULL,
286 pngdec->endinfo = png_create_info_struct (pngdec->png);
287 if (pngdec->endinfo == NULL) {
288 g_warning ("Failed to initialize endinfo structure");
292 /* non-0 return is from a longjmp inside of libpng */
293 if (setjmp (pngdec->png->jmpbuf) != 0) {
294 GST_DEBUG ("returning from longjmp");
295 png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
299 png_set_read_fn (pngdec->png, pngdec, user_read_data);
301 png_read_info (pngdec->png, pngdec->info);
302 png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
303 &depth, &color, NULL, NULL, NULL);
305 GST_LOG ("color type: %d\n", pngdec->info->color_type);
307 if (pngdec->width != width ||
308 pngdec->height != height ||
309 pngdec->color_type != pngdec->info->color_type) {
310 pngdec->width = width;
311 pngdec->height = height;
312 pngdec->color_type = pngdec->info->color_type;
314 if (GST_PAD_LINK_FAILED (gst_pad_renegotiate (pngdec->srcpad))) {
315 GST_ELEMENT_ERROR (pngdec, CORE, NEGOTIATION, (NULL), (NULL));
320 rows = (png_bytep *) g_malloc (sizeof (png_bytep) * height);
323 gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
326 inp = GST_BUFFER_DATA (out);
327 for (i = 0; i < height; i++) {
332 png_read_image (pngdec->png, rows);
335 png_destroy_info_struct (pngdec->png, &pngdec->info);
336 png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
338 pngdec->buffer_in = NULL;
339 gst_buffer_unref (buf);
341 gst_pad_push (pngdec->srcpad, GST_DATA (out));