2004-07-16 10:56:31 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2005-10-15 16:48:55 +00:00
|
|
|
|
2004-07-16 10:56:31 +00:00
|
|
|
#include "gstpngdec.h"
|
2005-10-15 16:48:55 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2004-07-16 10:56:31 +00:00
|
|
|
#include <gst/video/video.h>
|
|
|
|
|
|
|
|
static GstElementDetails gst_pngdec_details = {
|
|
|
|
"PNG decoder",
|
|
|
|
"Codec/Decoder/Image",
|
|
|
|
"Decode a png video frame to a raw image",
|
|
|
|
"Wim Taymans <wim@fluendo.com>",
|
|
|
|
};
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
GST_DEBUG_CATEGORY (pngdec_debug);
|
|
|
|
#define GST_CAT_DEFAULT pngdec_debug
|
2004-07-16 10:56:31 +00:00
|
|
|
|
|
|
|
static void gst_pngdec_base_init (gpointer g_class);
|
|
|
|
static void gst_pngdec_class_init (GstPngDecClass * klass);
|
|
|
|
static void gst_pngdec_init (GstPngDec * pngdec);
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstBuffer * buf);
|
2004-07-16 10:56:31 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
|
|
|
|
static void
|
|
|
|
user_error_fn (png_structp png_ptr, png_const_charp error_msg)
|
|
|
|
{
|
|
|
|
g_warning ("%s", error_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
|
|
|
|
{
|
|
|
|
g_warning ("%s", warning_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_pngdec_get_type (void)
|
|
|
|
{
|
|
|
|
static GType pngdec_type = 0;
|
|
|
|
|
|
|
|
if (!pngdec_type) {
|
|
|
|
static const GTypeInfo pngdec_info = {
|
|
|
|
sizeof (GstPngDecClass),
|
|
|
|
gst_pngdec_base_init,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_pngdec_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstPngDec),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_pngdec_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
pngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngDec",
|
|
|
|
&pngdec_info, 0);
|
|
|
|
}
|
|
|
|
return pngdec_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_pngdec_src_pad_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2005-10-15 16:48:55 +00:00
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
|
2004-07-16 10:56:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_pngdec_sink_pad_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2005-01-07 10:27:20 +00:00
|
|
|
GST_STATIC_CAPS ("image/png, "
|
2004-07-16 10:56:31 +00:00
|
|
|
"width = (int) [ 16, 4096 ], "
|
|
|
|
"height = (int) [ 16, 4096 ], " "framerate = (double) [ 0.0, MAX ]")
|
|
|
|
);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pngdec_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&gst_pngdec_src_pad_template));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
|
|
|
|
gst_element_class_set_details (element_class, &gst_pngdec_details);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pngdec_class_init (GstPngDecClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
|
2004-07-16 10:56:31 +00:00
|
|
|
static void
|
|
|
|
gst_pngdec_init (GstPngDec * pngdec)
|
|
|
|
{
|
|
|
|
pngdec->sinkpad = gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&gst_pngdec_sink_pad_template), "sink");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);
|
|
|
|
|
|
|
|
pngdec->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&gst_pngdec_src_pad_template), "src");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);
|
|
|
|
|
|
|
|
gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
|
|
|
|
|
|
|
|
pngdec->png = NULL;
|
|
|
|
pngdec->info = NULL;
|
|
|
|
|
|
|
|
pngdec->color_type = -1;
|
|
|
|
pngdec->width = -1;
|
|
|
|
pngdec->height = -1;
|
2005-10-15 16:48:55 +00:00
|
|
|
pngdec->fps = 0.0;
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
|
|
|
|
{
|
|
|
|
GstPngDec *dec;
|
|
|
|
|
|
|
|
dec = GST_PNGDEC (png_ptr->io_ptr);
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
GST_LOG ("reading %d bytes of data at offset %d", length, dec->offset);
|
|
|
|
|
2004-07-16 10:56:31 +00:00
|
|
|
if (GST_BUFFER_SIZE (dec->buffer_in) < dec->offset + length) {
|
|
|
|
g_warning ("reading past end of buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (data, GST_BUFFER_DATA (dec->buffer_in) + dec->offset, length);
|
|
|
|
|
|
|
|
dec->offset += length;
|
|
|
|
}
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
#define ROUND_UP_4(x) (((x) + 3) & ~3)
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_pngdec_chain (GstPad * pad, GstBuffer * buf)
|
2004-07-16 10:56:31 +00:00
|
|
|
{
|
2005-10-15 16:48:55 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2004-07-16 10:56:31 +00:00
|
|
|
GstPngDec *pngdec;
|
|
|
|
png_uint_32 width, height;
|
|
|
|
gint depth, color;
|
|
|
|
png_bytep *rows, inp;
|
|
|
|
GstBuffer *out;
|
|
|
|
gint i;
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
GST_LOG ("chain");
|
|
|
|
|
2004-07-16 10:56:31 +00:00
|
|
|
pngdec = GST_PNGDEC (gst_pad_get_parent (pad));
|
|
|
|
|
|
|
|
if (!GST_PAD_IS_USABLE (pngdec->srcpad)) {
|
|
|
|
gst_buffer_unref (buf);
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = GST_FLOW_UNEXPECTED;
|
|
|
|
goto beach;
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pngdec->buffer_in = buf;
|
|
|
|
pngdec->offset = 0;
|
|
|
|
|
|
|
|
/* initialize png struct stuff */
|
|
|
|
pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
|
|
|
|
(png_voidp) NULL, user_error_fn, user_warning_fn);
|
|
|
|
|
|
|
|
if (pngdec->png == NULL) {
|
2005-01-09 01:40:14 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
|
|
|
|
("Failed to initialize png structure"));
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = GST_FLOW_UNEXPECTED;
|
|
|
|
goto beach;
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pngdec->info = png_create_info_struct (pngdec->png);
|
|
|
|
if (pngdec->info == NULL) {
|
2005-01-09 01:40:14 +00:00
|
|
|
gst_buffer_unref (buf);
|
2004-07-16 10:56:31 +00:00
|
|
|
png_destroy_read_struct (&(pngdec->png), (png_infopp) NULL,
|
|
|
|
(png_infopp) NULL);
|
2005-01-09 01:40:14 +00:00
|
|
|
GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
|
|
|
|
("Failed to initialize info structure"));
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = GST_FLOW_UNEXPECTED;
|
|
|
|
goto beach;
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pngdec->endinfo = png_create_info_struct (pngdec->png);
|
|
|
|
if (pngdec->endinfo == NULL) {
|
2005-01-09 01:40:14 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
png_destroy_read_struct (&pngdec->png, &pngdec->info, (png_infopp) NULL);
|
|
|
|
GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
|
|
|
|
("Failed to initialize endinfo structure"));
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = GST_FLOW_UNEXPECTED;
|
|
|
|
goto beach;
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* non-0 return is from a longjmp inside of libpng */
|
|
|
|
if (setjmp (pngdec->png->jmpbuf) != 0) {
|
2005-01-09 01:40:14 +00:00
|
|
|
gst_buffer_unref (buf);
|
2004-07-16 10:56:31 +00:00
|
|
|
png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
|
2005-01-09 01:40:14 +00:00
|
|
|
GST_ELEMENT_ERROR (pngdec, LIBRARY, FAILED, (NULL),
|
|
|
|
("returning from longjmp"));
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = GST_FLOW_UNEXPECTED;
|
|
|
|
goto beach;
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
png_set_read_fn (pngdec->png, pngdec, user_read_data);
|
|
|
|
|
|
|
|
png_read_info (pngdec->png, pngdec->info);
|
|
|
|
png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
|
|
|
|
&depth, &color, NULL, NULL, NULL);
|
|
|
|
|
2005-02-08 11:11:21 +00:00
|
|
|
if (pngdec->info->bit_depth != 8) {
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
|
|
|
|
GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
|
|
|
|
("pngdec only supports 8 bit images for now"));
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = GST_FLOW_UNEXPECTED;
|
|
|
|
goto beach;
|
2005-02-08 11:11:21 +00:00
|
|
|
}
|
|
|
|
|
2004-07-16 10:56:31 +00:00
|
|
|
if (pngdec->width != width ||
|
|
|
|
pngdec->height != height ||
|
|
|
|
pngdec->color_type != pngdec->info->color_type) {
|
2005-02-08 11:11:21 +00:00
|
|
|
GstCaps *caps, *templ, *res;
|
|
|
|
gboolean ret;
|
|
|
|
gint bpp;
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
GST_LOG ("this is a %dx%d PNG image", width, height);
|
2004-07-16 10:56:31 +00:00
|
|
|
pngdec->width = width;
|
|
|
|
pngdec->height = height;
|
|
|
|
pngdec->color_type = pngdec->info->color_type;
|
|
|
|
|
2005-02-08 11:11:21 +00:00
|
|
|
switch (pngdec->color_type) {
|
|
|
|
case PNG_COLOR_TYPE_RGB:
|
2005-10-15 16:48:55 +00:00
|
|
|
GST_LOG ("we have no alpha channel, depth is 24 bits");
|
2005-02-08 11:11:21 +00:00
|
|
|
bpp = 24;
|
|
|
|
break;
|
|
|
|
case PNG_COLOR_TYPE_RGB_ALPHA:
|
2005-10-15 16:48:55 +00:00
|
|
|
GST_LOG ("we have an alpha channel, depth is 32 bits");
|
2005-02-08 11:11:21 +00:00
|
|
|
bpp = 32;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
|
|
|
|
("pngdec does not support grayscale or paletted data yet"));
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = GST_FLOW_UNEXPECTED;
|
|
|
|
goto beach;
|
2005-02-08 11:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
caps = gst_caps_new_simple ("video/x-raw-rgb",
|
|
|
|
"width", G_TYPE_INT, width,
|
|
|
|
"height", G_TYPE_INT, height,
|
|
|
|
"bpp", G_TYPE_INT, bpp, "framerate", G_TYPE_DOUBLE, pngdec->fps, NULL);
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
templ = gst_caps_copy (gst_pad_template_get_caps
|
|
|
|
(gst_static_pad_template_get (&gst_pngdec_src_pad_template)));
|
|
|
|
|
2005-02-08 11:11:21 +00:00
|
|
|
res = gst_caps_intersect (templ, caps);
|
2005-10-15 16:48:55 +00:00
|
|
|
gst_caps_unref (caps);
|
|
|
|
gst_caps_unref (templ);
|
|
|
|
{
|
|
|
|
gchar *caps_str = gst_caps_to_string (res);
|
|
|
|
|
|
|
|
GST_LOG ("intersecting caps with template gave %s", caps_str);
|
|
|
|
g_free (caps_str);
|
|
|
|
}
|
|
|
|
ret = gst_pad_set_caps (pngdec->srcpad, res);
|
|
|
|
gst_caps_unref (res);
|
2005-02-08 11:11:21 +00:00
|
|
|
|
|
|
|
if (!ret) {
|
2005-01-09 01:40:14 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
|
2004-07-16 10:56:31 +00:00
|
|
|
GST_ELEMENT_ERROR (pngdec, CORE, NEGOTIATION, (NULL), (NULL));
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = GST_FLOW_UNEXPECTED;
|
|
|
|
goto beach;
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rows = (png_bytep *) g_malloc (sizeof (png_bytep) * height);
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
|
|
|
|
height * ROUND_UP_4 (pngdec->info->rowbytes),
|
|
|
|
GST_PAD_CAPS (pngdec->srcpad), &out);
|
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
goto beach;
|
|
|
|
}
|
2004-07-16 10:56:31 +00:00
|
|
|
|
|
|
|
inp = GST_BUFFER_DATA (out);
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
rows[i] = inp;
|
2005-10-15 16:48:55 +00:00
|
|
|
inp += ROUND_UP_4 (pngdec->info->rowbytes);
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
png_read_image (pngdec->png, rows);
|
|
|
|
free (rows);
|
|
|
|
|
|
|
|
png_destroy_info_struct (pngdec->png, &pngdec->info);
|
|
|
|
png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
|
|
|
|
|
|
|
|
pngdec->buffer_in = NULL;
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
|
2005-10-15 16:48:55 +00:00
|
|
|
/* Pushing */
|
configure.ac: Adding videomixer.
Original commit message from CVS:
2005-10-17 Julien MOUTTE <julien@moutte.net>
* configure.ac: Adding videomixer.
* ext/libpng/gstpngdec.c: (gst_pngdec_class_init),
(user_read_data), (gst_pngdec_chain): More debugging.
* gst/alpha/Makefile.am: Adding alphacolor
* gst/alpha/gstalphacolor.c: (gst_alpha_color_base_init),
(gst_alpha_color_class_init), (gst_alpha_color_init),
(gst_alpha_color_transform_caps), (gst_alpha_color_set_caps),
(transform_rgb), (transform_bgr),
(gst_alpha_color_transform_ip),
(plugin_init): Ported to 0.9 using in place base tranform.
* gst/videomixer/Makefile.am:
* gst/videomixer/videomixer.c: (gst_videomixer_pad_get_type),
(gst_videomixer_pad_class_init),
(gst_videomixer_pad_sink_setcaps),
(gst_videomixer_pad_link), (gst_videomixer_pad_unlink),
(gst_videomixer_pad_init), (gst_videomixer_class_init),
(gst_videomixer_init), (gst_videomixer_getcaps),
(gst_videomixer_request_new_pad), (gst_videomixer_fill_queues),
(gst_videomixer_blend_buffers), (gst_videomixer_update_queues),
(gst_videomixer_collected), (gst_videomixer_change_state):
Ported
to 0.9 using collectpads.
2005-10-17 08:46:30 +00:00
|
|
|
GST_LOG ("pushing our raw RGB frame of %d bytes", GST_BUFFER_SIZE (out));
|
2005-10-15 16:48:55 +00:00
|
|
|
ret = gst_pad_push (pngdec->srcpad, out);
|
|
|
|
|
|
|
|
beach:
|
|
|
|
return ret;
|
2004-07-16 10:56:31 +00:00
|
|
|
}
|