2002-11-18 22:29:38 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
*
|
|
|
|
* Filter:
|
2003-01-20 23:32:39 +00:00
|
|
|
* Copyright (C) 2000 Donald A. Graft
|
2002-11-18 22:29:38 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-06-29 19:46:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2002-11-18 22:29:38 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <gst/gst.h>
|
2002-11-22 22:25:03 +00:00
|
|
|
#include "gstpngenc.h"
|
2003-11-02 00:21:14 +00:00
|
|
|
#include <gst/video/video.h>
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
#define MAX_HEIGHT 4096
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
GstElementDetails gst_pngenc_details = {
|
2003-07-19 23:25:25 +00:00
|
|
|
"PNG encoder",
|
2003-11-16 22:02:22 +00:00
|
|
|
"Codec/Encoder/Image",
|
2003-07-08 17:19:52 +00:00
|
|
|
"Encode a video frame to a .png image",
|
2002-11-18 22:29:38 +00:00
|
|
|
"Jeremy SIMON <jsimon13@yahoo.fr>",
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Filter signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ARG_0
|
|
|
|
};
|
|
|
|
|
2003-11-02 00:21:14 +00:00
|
|
|
static void gst_pngenc_base_init (gpointer g_class);
|
2003-01-20 23:32:39 +00:00
|
|
|
static void gst_pngenc_class_init (GstPngEncClass *klass);
|
|
|
|
static void gst_pngenc_init (GstPngEnc *pngenc);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2003-10-08 16:08:18 +00:00
|
|
|
static void gst_pngenc_chain (GstPad *pad, GstData *_data);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
|
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
static void user_error_fn (png_structp png_ptr, png_const_charp error_msg)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
|
|
|
g_warning("%s", error_msg);
|
|
|
|
}
|
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
static void user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
|
|
|
g_warning("%s", warning_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GType gst_pngenc_get_type (void)
|
|
|
|
{
|
|
|
|
static GType pngenc_type = 0;
|
|
|
|
|
|
|
|
if (!pngenc_type) {
|
|
|
|
static const GTypeInfo pngenc_info = {
|
2003-11-02 00:21:14 +00:00
|
|
|
sizeof (GstPngEncClass),
|
|
|
|
gst_pngenc_base_init,
|
2002-11-18 22:29:38 +00:00
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_pngenc_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstPngEnc),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_pngenc_init,
|
|
|
|
};
|
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
pngenc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngEnc",
|
|
|
|
&pngenc_info, 0);
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
return pngenc_type;
|
|
|
|
}
|
|
|
|
|
2003-11-02 00:21:14 +00:00
|
|
|
static GstCaps*
|
|
|
|
png_caps_factory (void)
|
|
|
|
{
|
|
|
|
return gst_caps_new ( "png_png",
|
|
|
|
"video/x-png",
|
|
|
|
gst_props_new (
|
|
|
|
"width", GST_PROPS_INT_RANGE (16, 4096),
|
|
|
|
"height", GST_PROPS_INT_RANGE (16, 4096),
|
|
|
|
"framerate", GST_PROPS_FLOAT_RANGE (0, G_MAXFLOAT),
|
|
|
|
NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GstCaps*
|
|
|
|
raw_caps_factory (void)
|
|
|
|
{
|
|
|
|
return gst_caps_new ( "png_raw",
|
|
|
|
"video/x-raw-rgb",
|
|
|
|
GST_VIDEO_RGB_PAD_TEMPLATE_PROPS_24
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pngenc_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
GstCaps *raw_caps, *png_caps;
|
|
|
|
|
|
|
|
raw_caps = raw_caps_factory ();
|
|
|
|
png_caps = png_caps_factory ();
|
|
|
|
|
|
|
|
pngenc_sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
raw_caps, NULL);
|
|
|
|
|
|
|
|
pngenc_src_template = gst_pad_template_new ("src", GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
png_caps, NULL);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class, pngenc_sink_template);
|
|
|
|
gst_element_class_add_pad_template (element_class, pngenc_src_template);
|
|
|
|
gst_element_class_set_details (element_class, &gst_pngenc_details);
|
|
|
|
}
|
|
|
|
|
2002-11-18 22:29:38 +00:00
|
|
|
static void
|
2003-01-20 23:32:39 +00:00
|
|
|
gst_pngenc_class_init (GstPngEncClass *klass)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-10 13:38:32 +00:00
|
|
|
static GstPadLinkReturn
|
2003-01-20 23:32:39 +00:00
|
|
|
gst_pngenc_sinklink (GstPad *pad, GstCaps *caps)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
|
|
|
GstPngEnc *pngenc;
|
2003-07-06 20:49:52 +00:00
|
|
|
gfloat fps;
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
pngenc = GST_PNGENC (gst_pad_get_parent (pad));
|
|
|
|
|
|
|
|
if (!GST_CAPS_IS_FIXED (caps))
|
2003-01-10 10:22:25 +00:00
|
|
|
return GST_PAD_LINK_DELAYED;
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
gst_caps_get_int (caps, "width", &pngenc->width);
|
|
|
|
gst_caps_get_int (caps, "height", &pngenc->height);
|
2003-07-06 20:49:52 +00:00
|
|
|
gst_caps_get_float (caps, "framerate", &fps);
|
2002-11-18 22:29:38 +00:00
|
|
|
gst_caps_get_int (caps, "bpp", &pngenc->bpp);
|
|
|
|
|
2003-07-06 20:49:52 +00:00
|
|
|
caps = GST_CAPS_NEW ("png_src",
|
|
|
|
"video/x-png",
|
|
|
|
"framerate", GST_PROPS_FLOAT (fps),
|
|
|
|
"width", GST_PROPS_INT (pngenc->width),
|
|
|
|
"height", GST_PROPS_INT (pngenc->height));
|
|
|
|
|
2002-11-18 22:29:38 +00:00
|
|
|
return gst_pad_try_set_caps (pngenc->srcpad, caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pngenc_init (GstPngEnc * pngenc)
|
|
|
|
{
|
2002-11-22 22:25:03 +00:00
|
|
|
pngenc->sinkpad = gst_pad_new_from_template (pngenc_sink_template, "sink");
|
2002-11-18 22:29:38 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->sinkpad);
|
2003-01-20 23:32:39 +00:00
|
|
|
|
|
|
|
pngenc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
|
2002-11-18 22:29:38 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->srcpad);
|
|
|
|
|
|
|
|
gst_pad_set_chain_function (pngenc->sinkpad, gst_pngenc_chain);
|
2003-01-20 23:32:39 +00:00
|
|
|
gst_pad_set_link_function (pngenc->sinkpad, gst_pngenc_sinklink);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
pngenc->png_struct_ptr = NULL;
|
|
|
|
pngenc->png_info_ptr = NULL;
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-11-22 22:25:03 +00:00
|
|
|
void user_flush_data (png_structp png_ptr)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
|
|
|
GstPngEnc *pngenc;
|
|
|
|
|
2002-11-22 22:25:03 +00:00
|
|
|
pngenc = (GstPngEnc *) png_get_io_ptr (png_ptr);
|
2003-01-20 23:32:39 +00:00
|
|
|
|
2003-10-08 16:08:18 +00:00
|
|
|
gst_pad_push (pngenc->srcpad, GST_DATA (gst_event_new (GST_EVENT_FLUSH)));
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
2002-11-22 22:25:03 +00:00
|
|
|
|
|
|
|
void user_write_data (png_structp png_ptr, png_bytep data, png_uint_32 length)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
2003-01-20 23:32:39 +00:00
|
|
|
GstBuffer *buffer;
|
|
|
|
GstPngEnc *pngenc;
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2002-11-22 22:25:03 +00:00
|
|
|
pngenc = (GstPngEnc *) png_get_io_ptr (png_ptr);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
buffer = gst_buffer_new();
|
2002-11-22 22:25:03 +00:00
|
|
|
GST_BUFFER_DATA (buffer) = g_memdup (data, length);
|
2002-11-18 22:29:38 +00:00
|
|
|
GST_BUFFER_SIZE (buffer) = length;
|
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
if (pngenc->buffer_out)
|
|
|
|
{
|
2002-11-22 22:25:03 +00:00
|
|
|
pngenc->buffer_out = gst_buffer_merge (pngenc->buffer_out, buffer);
|
2003-01-20 23:32:39 +00:00
|
|
|
gst_buffer_unref (buffer);
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
pngenc->buffer_out = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-10-08 16:08:18 +00:00
|
|
|
gst_pngenc_chain (GstPad *pad, GstData *_data)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
2003-10-08 16:08:18 +00:00
|
|
|
GstBuffer *buf = GST_BUFFER (_data);
|
2002-11-18 22:29:38 +00:00
|
|
|
GstPngEnc *pngenc;
|
2003-01-20 23:32:39 +00:00
|
|
|
gint row_index;
|
|
|
|
png_byte *row_pointers[MAX_HEIGHT];
|
|
|
|
GstEvent *event;
|
|
|
|
|
|
|
|
pngenc = GST_PNGENC (gst_pad_get_parent (pad));
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
pngenc->buffer_out = NULL;
|
|
|
|
if (!GST_PAD_IS_USABLE (pngenc->srcpad))
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
|
|
|
gst_buffer_unref (buf);
|
2003-01-20 23:32:39 +00:00
|
|
|
return;
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
/* initialize png struct stuff */
|
|
|
|
pngenc->png_struct_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
|
|
|
|
(png_voidp) NULL, user_error_fn, user_warning_fn);
|
|
|
|
/* FIXME: better error handling */
|
|
|
|
if (pngenc->png_struct_ptr == NULL)
|
|
|
|
g_warning ("Failed to initialize png structure");
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
pngenc->png_info_ptr = png_create_info_struct (pngenc->png_struct_ptr);
|
|
|
|
if (!pngenc->png_info_ptr)
|
|
|
|
{
|
|
|
|
png_destroy_read_struct (&(pngenc->png_struct_ptr), (png_infopp) NULL,
|
|
|
|
(png_infopp) NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* non-0 return is from a longjmp inside of libpng */
|
|
|
|
if (setjmp (pngenc->png_struct_ptr->jmpbuf) != 0)
|
|
|
|
{
|
2003-06-29 19:46:12 +00:00
|
|
|
GST_DEBUG ("returning from longjmp");
|
2003-01-20 23:32:39 +00:00
|
|
|
png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
|
|
|
|
return;
|
|
|
|
}
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
png_set_filter (pngenc->png_struct_ptr, 0,
|
|
|
|
PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
|
2002-11-22 22:25:03 +00:00
|
|
|
png_set_compression_level (pngenc->png_struct_ptr, 9);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
png_set_IHDR(
|
|
|
|
pngenc->png_struct_ptr,
|
|
|
|
pngenc->png_info_ptr,
|
|
|
|
pngenc->width,
|
|
|
|
pngenc->height,
|
2003-01-20 23:32:39 +00:00
|
|
|
pngenc->bpp / 3,
|
2002-11-18 22:29:38 +00:00
|
|
|
PNG_COLOR_TYPE_RGB,
|
|
|
|
PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_DEFAULT,
|
|
|
|
PNG_FILTER_TYPE_DEFAULT
|
|
|
|
);
|
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
png_set_write_fn (pngenc->png_struct_ptr, pngenc,
|
|
|
|
(png_rw_ptr) user_write_data, user_flush_data);
|
|
|
|
|
|
|
|
for (row_index = 0; row_index < pngenc->height; row_index++)
|
|
|
|
row_pointers[row_index] = GST_BUFFER_DATA (buf) +
|
|
|
|
(pngenc->width * row_index * pngenc->bpp / 8);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2002-11-22 22:25:03 +00:00
|
|
|
png_write_info (pngenc->png_struct_ptr, pngenc->png_info_ptr);
|
|
|
|
png_write_image (pngenc->png_struct_ptr, row_pointers);
|
|
|
|
png_write_end (pngenc->png_struct_ptr, NULL);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2002-11-22 22:25:03 +00:00
|
|
|
user_flush_data (pngenc->png_struct_ptr);
|
2003-01-20 23:32:39 +00:00
|
|
|
|
2002-11-22 22:25:03 +00:00
|
|
|
png_destroy_info_struct (pngenc->png_struct_ptr, &pngenc->png_info_ptr);
|
2003-01-20 23:32:39 +00:00
|
|
|
png_destroy_write_struct (&pngenc->png_struct_ptr, (png_infopp) NULL);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2003-10-08 16:08:18 +00:00
|
|
|
gst_pad_push (pngenc->srcpad, GST_DATA (pngenc->buffer_out));
|
2003-06-07 00:17:53 +00:00
|
|
|
|
|
|
|
/* send EOS event, since a frame has been pushed out */
|
|
|
|
event = gst_event_new (GST_EVENT_EOS);
|
2003-10-08 16:08:18 +00:00
|
|
|
gst_pad_push (pngenc->srcpad, GST_DATA (event));
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
}
|