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.
|
|
|
|
*
|
|
|
|
*/
|
2009-01-28 15:46:06 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-pngenc
|
|
|
|
*
|
|
|
|
* Encodes png images.
|
|
|
|
*/
|
2002-11-18 22:29:38 +00:00
|
|
|
|
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"
|
2005-01-09 16:30:15 +00:00
|
|
|
#include <zlib.h>
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2006-06-22 19:31:04 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (pngenc_debug);
|
2005-11-18 18:19:21 +00:00
|
|
|
#define GST_CAT_DEFAULT pngenc_debug
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
/* Filter signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2012-01-24 17:24:13 +00:00
|
|
|
#define DEFAULT_SNAPSHOT FALSE
|
2005-12-06 19:44:58 +00:00
|
|
|
/* #define DEFAULT_NEWMEDIA FALSE */
|
|
|
|
#define DEFAULT_COMPRESSION_LEVEL 6
|
2004-07-27 09:59:17 +00:00
|
|
|
|
2002-11-18 22:29:38 +00:00
|
|
|
enum
|
|
|
|
{
|
2004-07-27 09:59:17 +00:00
|
|
|
ARG_0,
|
2004-07-30 09:35:09 +00:00
|
|
|
ARG_SNAPSHOT,
|
2005-08-02 11:42:33 +00:00
|
|
|
/* ARG_NEWMEDIA, */
|
2005-01-09 16:30:15 +00:00
|
|
|
ARG_COMPRESSION_LEVEL
|
2002-11-18 22:29:38 +00:00
|
|
|
};
|
|
|
|
|
2005-01-07 10:27:20 +00:00
|
|
|
static GstStaticPadTemplate pngenc_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("image/png, "
|
2011-10-16 18:30:25 +00:00
|
|
|
"width = (int) [ 16, 1000000 ], "
|
|
|
|
"height = (int) [ 16, 1000000 ], " "framerate = " GST_VIDEO_FPS_RANGE)
|
2005-01-07 10:27:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate pngenc_sink_template =
|
2012-01-24 17:21:08 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
2005-01-07 10:27:20 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-03-22 10:53:24 +00:00
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBA, RGB, GRAY8, GRAY16_BE }"))
|
2005-01-07 10:27:20 +00:00
|
|
|
);
|
2004-01-03 13:14:20 +00:00
|
|
|
|
2005-08-02 11:42:33 +00:00
|
|
|
/* static GstElementClass *parent_class = NULL; */
|
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
G_DEFINE_TYPE (GstPngEnc, gst_pngenc, GST_TYPE_ELEMENT);
|
2005-08-02 11:42:33 +00:00
|
|
|
|
|
|
|
static void gst_pngenc_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_pngenc_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
static GstFlowReturn gst_pngenc_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * data);
|
|
|
|
static gboolean gst_pngenc_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static void
|
|
|
|
user_error_fn (png_structp png_ptr, png_const_charp error_msg)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
g_warning ("%s", error_msg);
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static void
|
|
|
|
user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
g_warning ("%s", warning_msg);
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_pngenc_class_init (GstPngEncClass * klass)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2012-01-24 17:21:08 +00:00
|
|
|
GstElementClass *element_class;
|
2002-11-18 22:29:38 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2012-01-24 17:21:08 +00:00
|
|
|
element_class = (GstElementClass *) klass;
|
2004-07-27 09:59:17 +00:00
|
|
|
|
2005-08-02 11:42:33 +00:00
|
|
|
gobject_class->get_property = gst_pngenc_get_property;
|
|
|
|
gobject_class->set_property = gst_pngenc_set_property;
|
|
|
|
|
2004-07-27 09:59:17 +00:00
|
|
|
g_object_class_install_property (gobject_class, ARG_SNAPSHOT,
|
|
|
|
g_param_spec_boolean ("snapshot", "Snapshot",
|
2004-07-30 09:35:09 +00:00
|
|
|
"Send EOS after encoding a frame, useful for snapshots",
|
2010-10-13 13:25:15 +00:00
|
|
|
DEFAULT_SNAPSHOT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2004-07-30 09:35:09 +00:00
|
|
|
|
2005-08-02 11:42:33 +00:00
|
|
|
/* g_object_class_install_property (gobject_class, ARG_NEWMEDIA, */
|
|
|
|
/* g_param_spec_boolean ("newmedia", "newmedia", */
|
|
|
|
/* "Send new media discontinuity after encoding each frame", */
|
2010-10-13 13:25:15 +00:00
|
|
|
/* DEFAULT_NEWMEDIA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); */
|
2004-07-27 09:59:17 +00:00
|
|
|
|
2010-10-13 13:25:15 +00:00
|
|
|
g_object_class_install_property (gobject_class, ARG_COMPRESSION_LEVEL,
|
2005-01-09 16:30:15 +00:00
|
|
|
g_param_spec_uint ("compression-level", "compression-level",
|
|
|
|
"PNG compression level",
|
|
|
|
Z_NO_COMPRESSION, Z_BEST_COMPRESSION,
|
2010-10-13 13:25:15 +00:00
|
|
|
DEFAULT_COMPRESSION_LEVEL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2005-11-18 18:19:21 +00:00
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
gst_element_class_add_pad_template
|
|
|
|
(element_class, gst_static_pad_template_get (&pngenc_sink_template));
|
|
|
|
gst_element_class_add_pad_template
|
|
|
|
(element_class, gst_static_pad_template_get (&pngenc_src_template));
|
2012-04-09 23:51:41 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class, "PNG image encoder",
|
2012-01-24 17:21:08 +00:00
|
|
|
"Codec/Encoder/Image",
|
|
|
|
"Encode a video frame to a .png image",
|
|
|
|
"Jeremy SIMON <jsimon13@yahoo.fr>");
|
|
|
|
|
2005-11-18 18:19:21 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (pngenc_debug, "pngenc", 0, "PNG image encoder");
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-02 11:42:33 +00:00
|
|
|
static gboolean
|
2012-01-24 17:21:08 +00:00
|
|
|
gst_pngenc_setcaps (GstPngEnc * pngenc, GstCaps * caps)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
2011-10-16 17:41:28 +00:00
|
|
|
int fps_n, fps_d;
|
2005-01-09 01:40:14 +00:00
|
|
|
GstCaps *pcaps;
|
2011-10-16 17:41:28 +00:00
|
|
|
gboolean ret;
|
2012-01-24 17:21:08 +00:00
|
|
|
GstVideoInfo info;
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
ret = gst_video_info_from_caps (&info, caps);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2011-10-16 17:41:28 +00:00
|
|
|
if (G_UNLIKELY (!ret))
|
|
|
|
goto done;
|
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
pngenc->info = info;
|
|
|
|
|
|
|
|
switch (GST_VIDEO_INFO_FORMAT (&info)) {
|
2011-10-16 17:41:28 +00:00
|
|
|
case GST_VIDEO_FORMAT_RGBA:
|
|
|
|
pngenc->png_color_type = PNG_COLOR_TYPE_RGBA;
|
2012-03-22 10:53:24 +00:00
|
|
|
pngenc->depth = 8;
|
2011-10-16 17:41:28 +00:00
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_RGB:
|
|
|
|
pngenc->png_color_type = PNG_COLOR_TYPE_RGB;
|
2012-03-22 10:53:24 +00:00
|
|
|
pngenc->depth = 8;
|
2011-10-16 17:41:28 +00:00
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_GRAY8:
|
|
|
|
pngenc->png_color_type = PNG_COLOR_TYPE_GRAY;
|
2012-03-22 10:53:24 +00:00
|
|
|
pngenc->depth = 8;
|
|
|
|
break;
|
2012-03-13 22:08:38 +00:00
|
|
|
case GST_VIDEO_FORMAT_GRAY16_BE:
|
2011-10-16 17:41:28 +00:00
|
|
|
pngenc->png_color_type = PNG_COLOR_TYPE_GRAY;
|
2012-03-22 10:53:24 +00:00
|
|
|
pngenc->depth = 16;
|
2011-10-16 17:41:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = FALSE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
pngenc->width = GST_VIDEO_INFO_WIDTH (&info);
|
|
|
|
pngenc->height = GST_VIDEO_INFO_HEIGHT (&info);
|
|
|
|
fps_n = GST_VIDEO_INFO_FPS_N (&info);
|
|
|
|
fps_d = GST_VIDEO_INFO_FPS_D (&info);
|
|
|
|
|
2011-10-16 18:30:25 +00:00
|
|
|
if (G_UNLIKELY (pngenc->width < 16 || pngenc->width > 1000000 ||
|
|
|
|
pngenc->height < 16 || pngenc->height > 1000000)) {
|
2011-10-16 17:41:28 +00:00
|
|
|
ret = FALSE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2006-10-27 17:10:42 +00:00
|
|
|
pcaps = gst_caps_new_simple ("image/png",
|
|
|
|
"width", G_TYPE_INT, pngenc->width,
|
2011-10-16 17:41:28 +00:00
|
|
|
"height", G_TYPE_INT, pngenc->height,
|
|
|
|
"framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
|
2006-10-27 17:10:42 +00:00
|
|
|
|
|
|
|
ret = gst_pad_set_caps (pngenc->srcpad, pcaps);
|
close #333784 unref the result of gst_pad_get_parent() by: Christophe Fergeau.
Original commit message from CVS:
* ext/cairo/gsttextoverlay.c: (gst_text_overlay_setcaps):
* ext/esd/esdmon.c: (gst_esdmon_get):
* ext/flac/gstflactag.c: (gst_flac_tag_chain):
* ext/gdk_pixbuf/gstgdkpixbuf.c: (gst_gdk_pixbuf_sink_setcaps),
(gst_gdk_pixbuf_sink_getcaps):
* ext/jpeg/gstjpegenc.c: (gst_jpegenc_getcaps),
(gst_jpegenc_setcaps):
* ext/jpeg/gstsmokedec.c: (gst_smokedec_chain):
* ext/jpeg/gstsmokeenc.c: (gst_smokeenc_getcaps),
(gst_smokeenc_setcaps):
* ext/libmng/gstmngdec.c: (gst_mngdec_sinklink),
(gst_mngdec_src_getcaps):
* ext/libmng/gstmngenc.c: (gst_mngenc_sinklink),
(gst_mngenc_chain):
* ext/libpng/gstpngenc.c: (gst_pngenc_setcaps):
* ext/mikmod/gstmikmod.c: (gst_mikmod_srclink):
* ext/speex/gstspeexdec.c: (speex_dec_convert),
(speex_dec_src_event), (speex_dec_chain):
* gst/avi/gstavimux.c: (gst_avimux_vidsinkconnect),
(gst_avimux_audsinkconnect), (gst_avimux_handle_event):
* gst/debug/negotiation.c: (gst_negotiation_getcaps),
(gst_negotiation_pad_link), (gst_negotiation_chain):
* gst/flx/gstflxdec.c: (gst_flxdec_src_query_handler),
(gst_flxdec_chain):
* gst/interleave/deinterleave.c: (deinterleave_sink_link),
(deinterleave_chain):
* gst/law/mulaw-encode.c: (mulawenc_setcaps):
* gst/median/gstmedian.c: (gst_median_link):
* gst/monoscope/gstmonoscope.c: (gst_monoscope_srcconnect),
(gst_monoscope_chain):
* gst/rtp/gstrtpL16pay.c: (gst_rtpL16pay_sinkconnect):
* gst/wavenc/gstwavenc.c: (gst_wavenc_sink_setcaps):
* sys/osxaudio/gstosxaudiosink.c: (gst_osxaudiosink_chain):
* sys/osxaudio/gstosxaudiosrc.c: (gst_osxaudiosrc_get):
close #333784 unref the result of gst_pad_get_parent()
by: Christophe Fergeau.
2006-03-13 15:49:08 +00:00
|
|
|
|
2006-10-27 17:10:42 +00:00
|
|
|
gst_caps_unref (pcaps);
|
2011-10-16 17:41:28 +00:00
|
|
|
|
|
|
|
/* Fall-through. */
|
|
|
|
done:
|
|
|
|
if (G_UNLIKELY (!ret)) {
|
|
|
|
pngenc->width = 0;
|
|
|
|
pngenc->height = 0;
|
|
|
|
}
|
|
|
|
|
2005-01-09 01:40:14 +00:00
|
|
|
return ret;
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-24 17:21:08 +00:00
|
|
|
gst_pngenc_init (GstPngEnc * pngenc)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
2005-08-02 11:42:33 +00:00
|
|
|
/* sinkpad */
|
2006-03-15 16:17:12 +00:00
|
|
|
pngenc->sinkpad = gst_pad_new_from_static_template
|
|
|
|
(&pngenc_sink_template, "sink");
|
2012-01-24 17:21:08 +00:00
|
|
|
gst_pad_set_chain_function (pngenc->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pngenc_chain));
|
|
|
|
gst_pad_set_event_function (pngenc->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pngenc_sink_event));
|
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
|
|
|
|
2005-08-02 11:42:33 +00:00
|
|
|
/* srcpad */
|
2006-03-15 16:17:12 +00:00
|
|
|
pngenc->srcpad = gst_pad_new_from_static_template
|
|
|
|
(&pngenc_src_template, "src");
|
2012-01-24 17:21:08 +00:00
|
|
|
gst_pad_use_fixed_caps (pngenc->srcpad);
|
2002-11-18 22:29:38 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->srcpad);
|
|
|
|
|
2005-08-02 11:42:33 +00:00
|
|
|
/* init settings */
|
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
|
|
|
|
2004-07-27 09:59:17 +00:00
|
|
|
pngenc->snapshot = DEFAULT_SNAPSHOT;
|
2005-08-02 11:42:33 +00:00
|
|
|
/* pngenc->newmedia = FALSE; */
|
|
|
|
pngenc->compression_level = DEFAULT_COMPRESSION_LEVEL;
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
2004-07-27 09:59:17 +00:00
|
|
|
static void
|
2008-11-20 13:46:47 +00:00
|
|
|
user_flush_data (png_structp png_ptr G_GNUC_UNUSED)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-07-27 09:59:17 +00:00
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
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
|
|
|
GstPngEnc *pngenc;
|
2012-01-25 11:41:42 +00:00
|
|
|
GstMapInfo map;
|
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
|
|
|
|
2012-01-25 11:41:42 +00:00
|
|
|
gst_buffer_map (pngenc->buffer_out, &map, GST_MAP_WRITE);
|
|
|
|
if (pngenc->written + length >= map.size) {
|
|
|
|
gst_buffer_unmap (pngenc->buffer_out, &map);
|
2008-01-29 18:43:32 +00:00
|
|
|
GST_ERROR_OBJECT (pngenc, "output buffer bigger than the input buffer!?");
|
2010-03-12 13:44:30 +00:00
|
|
|
png_error (png_ptr, "output buffer bigger than the input buffer!?");
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2008-01-29 18:43:32 +00:00
|
|
|
/* never reached */
|
|
|
|
return;
|
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
GST_DEBUG_OBJECT (pngenc, "writing %u bytes", (guint) length);
|
|
|
|
|
2012-01-25 11:41:42 +00:00
|
|
|
memcpy (map.data + pngenc->written, data, length);
|
|
|
|
gst_buffer_unmap (pngenc->buffer_out, &map);
|
2008-01-29 18:43:32 +00:00
|
|
|
pngenc->written += length;
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
2005-08-02 11:42:33 +00:00
|
|
|
static GstFlowReturn
|
2012-01-24 17:21:08 +00:00
|
|
|
gst_pngenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
2002-11-18 22:29:38 +00:00
|
|
|
{
|
|
|
|
GstPngEnc *pngenc;
|
2003-01-20 23:32:39 +00:00
|
|
|
gint row_index;
|
2011-10-16 18:25:41 +00:00
|
|
|
png_byte **row_pointers;
|
2005-08-02 11:42:33 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2008-01-29 18:43:32 +00:00
|
|
|
GstBuffer *encoded_buf = NULL;
|
2012-01-24 17:21:08 +00:00
|
|
|
GstVideoFrame frame;
|
2003-01-20 23:32:39 +00:00
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
pngenc = GST_PNGENC (parent);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2005-11-18 18:19:21 +00:00
|
|
|
GST_DEBUG_OBJECT (pngenc, "BEGINNING");
|
|
|
|
|
2011-10-16 18:05:28 +00:00
|
|
|
if (G_UNLIKELY (pngenc->width <= 0 || pngenc->height <= 0)) {
|
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
2012-01-24 17:21:08 +00:00
|
|
|
goto exit;
|
2011-10-16 18:05:28 +00:00
|
|
|
}
|
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
if (!gst_video_frame_map (&frame, &pngenc->info, buf, GST_MAP_READ)) {
|
2011-10-16 17:44:27 +00:00
|
|
|
GST_ELEMENT_ERROR (pngenc, STREAM, FORMAT, (NULL),
|
2012-01-24 17:21:08 +00:00
|
|
|
("Failed to map video frame, caps problem?"));
|
2011-10-16 17:44:27 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2012-01-24 17:21:08 +00:00
|
|
|
goto exit;
|
2011-10-16 17:44:27 +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,
|
2004-03-14 22:34:33 +00:00
|
|
|
(png_voidp) NULL, user_error_fn, user_warning_fn);
|
2005-01-09 01:40:14 +00:00
|
|
|
if (pngenc->png_struct_ptr == NULL) {
|
|
|
|
GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
|
|
|
|
("Failed to initialize png structure"));
|
2005-08-02 11:42:33 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto done;
|
2005-01-09 01:40:14 +00:00
|
|
|
}
|
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);
|
2004-03-14 22:34:33 +00:00
|
|
|
if (!pngenc->png_info_ptr) {
|
2005-01-09 01:40:14 +00:00
|
|
|
png_destroy_write_struct (&(pngenc->png_struct_ptr), (png_infopp) NULL);
|
|
|
|
GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
|
|
|
|
("Failed to initialize the png info structure"));
|
2005-08-02 11:42:33 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto done;
|
2003-01-20 23:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* non-0 return is from a longjmp inside of libpng */
|
2010-03-13 20:19:35 +00:00
|
|
|
if (setjmp (png_jmpbuf (pngenc->png_struct_ptr)) != 0) {
|
2003-01-20 23:32:39 +00:00
|
|
|
png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
|
2005-01-09 01:40:14 +00:00
|
|
|
GST_ELEMENT_ERROR (pngenc, LIBRARY, FAILED, (NULL),
|
|
|
|
("returning from longjmp"));
|
2005-08-02 11:42:33 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto done;
|
2003-01-20 23:32:39 +00:00
|
|
|
}
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
png_set_filter (pngenc->png_struct_ptr, 0,
|
2004-03-14 22:34:33 +00:00
|
|
|
PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
|
2005-01-09 16:30:15 +00:00
|
|
|
png_set_compression_level (pngenc->png_struct_ptr, pngenc->compression_level);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
png_set_IHDR (pngenc->png_struct_ptr,
|
|
|
|
pngenc->png_info_ptr,
|
|
|
|
pngenc->width,
|
|
|
|
pngenc->height,
|
2012-03-13 22:08:38 +00:00
|
|
|
pngenc->depth,
|
2011-10-16 17:41:28 +00:00
|
|
|
pngenc->png_color_type,
|
2004-03-14 22:34:33 +00:00
|
|
|
PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2003-01-20 23:32:39 +00:00
|
|
|
png_set_write_fn (pngenc->png_struct_ptr, pngenc,
|
2004-03-14 22:34:33 +00:00
|
|
|
(png_rw_ptr) user_write_data, user_flush_data);
|
2003-01-20 23:32:39 +00:00
|
|
|
|
2011-10-16 18:30:25 +00:00
|
|
|
row_pointers = g_new (png_byte *, pngenc->height);
|
2011-10-16 18:25:41 +00:00
|
|
|
|
2006-10-27 17:10:42 +00:00
|
|
|
for (row_index = 0; row_index < pngenc->height; row_index++) {
|
2012-01-24 17:21:08 +00:00
|
|
|
row_pointers[row_index] = GST_VIDEO_FRAME_COMP_DATA (&frame, 0) +
|
|
|
|
(row_index * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0));
|
2006-10-27 17:10:42 +00:00
|
|
|
}
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2008-01-29 18:43:32 +00:00
|
|
|
/* allocate the output buffer */
|
|
|
|
pngenc->buffer_out =
|
2012-01-24 17:21:08 +00:00
|
|
|
gst_buffer_new_and_alloc (pngenc->height * pngenc->width);
|
2008-01-29 18:43:32 +00:00
|
|
|
pngenc->written = 0;
|
|
|
|
|
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
|
|
|
|
2011-10-16 18:30:25 +00:00
|
|
|
g_free (row_pointers);
|
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
GST_DEBUG_OBJECT (pngenc, "written %d", pngenc->written);
|
|
|
|
|
|
|
|
encoded_buf =
|
|
|
|
gst_buffer_copy_region (pngenc->buffer_out, GST_BUFFER_COPY_MEMORY,
|
|
|
|
0, pngenc->written);
|
2008-01-29 18:43:32 +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);
|
2012-01-24 17:21:08 +00:00
|
|
|
|
|
|
|
GST_BUFFER_TIMESTAMP (encoded_buf) = GST_BUFFER_TIMESTAMP (buf);
|
|
|
|
GST_BUFFER_DURATION (encoded_buf) = GST_BUFFER_DURATION (buf);
|
2002-11-18 22:29:38 +00:00
|
|
|
|
2008-01-29 18:43:32 +00:00
|
|
|
if ((ret = gst_pad_push (pngenc->srcpad, encoded_buf)) != GST_FLOW_OK)
|
2005-08-02 11:42:33 +00:00
|
|
|
goto done;
|
2003-06-07 00:17:53 +00:00
|
|
|
|
2004-07-27 09:59:17 +00:00
|
|
|
if (pngenc->snapshot) {
|
2006-01-23 09:59:03 +00:00
|
|
|
GstEvent *event;
|
|
|
|
|
2005-11-18 18:19:21 +00:00
|
|
|
GST_DEBUG_OBJECT (pngenc, "snapshot mode, sending EOS");
|
2004-07-27 09:59:17 +00:00
|
|
|
/* send EOS event, since a frame has been pushed out */
|
2006-01-23 09:59:03 +00:00
|
|
|
event = gst_event_new_eos ();
|
2005-08-02 11:42:33 +00:00
|
|
|
|
2006-05-16 17:20:04 +00:00
|
|
|
gst_pad_push_event (pngenc->srcpad, event);
|
2012-01-24 17:21:08 +00:00
|
|
|
ret = GST_FLOW_EOS;
|
2004-07-27 09:59:17 +00:00
|
|
|
}
|
2006-10-27 17:10:42 +00:00
|
|
|
|
2005-08-02 11:42:33 +00:00
|
|
|
done:
|
2012-01-24 17:21:08 +00:00
|
|
|
gst_video_frame_unmap (&frame);
|
|
|
|
exit:
|
|
|
|
gst_buffer_unref (buf);
|
2005-11-18 18:19:21 +00:00
|
|
|
GST_DEBUG_OBJECT (pngenc, "END, ret:%d", ret);
|
|
|
|
|
2008-01-29 18:43:32 +00:00
|
|
|
if (pngenc->buffer_out != NULL) {
|
|
|
|
gst_buffer_unref (pngenc->buffer_out);
|
|
|
|
pngenc->buffer_out = NULL;
|
|
|
|
}
|
|
|
|
|
2005-08-02 11:42:33 +00:00
|
|
|
return ret;
|
2004-07-27 09:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-01-24 17:21:08 +00:00
|
|
|
static gboolean
|
|
|
|
gst_pngenc_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstPngEnc *enc;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
enc = GST_PNGENC (parent);
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
gst_event_parse_caps (event, &caps);
|
|
|
|
res = gst_pngenc_setcaps (enc, caps);
|
|
|
|
gst_event_unref (event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
res = gst_pad_push_event (enc->srcpad, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2004-07-27 09:59:17 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pngenc_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstPngEnc *pngenc;
|
|
|
|
|
|
|
|
pngenc = GST_PNGENC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_SNAPSHOT:
|
|
|
|
g_value_set_boolean (value, pngenc->snapshot);
|
|
|
|
break;
|
2005-08-02 11:42:33 +00:00
|
|
|
/* case ARG_NEWMEDIA: */
|
|
|
|
/* g_value_set_boolean (value, pngenc->newmedia); */
|
|
|
|
/* break; */
|
2005-01-09 16:30:15 +00:00
|
|
|
case ARG_COMPRESSION_LEVEL:
|
|
|
|
g_value_set_uint (value, pngenc->compression_level);
|
|
|
|
break;
|
2004-07-27 09:59:17 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pngenc_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstPngEnc *pngenc;
|
|
|
|
|
|
|
|
pngenc = GST_PNGENC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_SNAPSHOT:
|
|
|
|
pngenc->snapshot = g_value_get_boolean (value);
|
|
|
|
break;
|
2005-08-02 11:42:33 +00:00
|
|
|
/* case ARG_NEWMEDIA: */
|
|
|
|
/* pngenc->newmedia = g_value_get_boolean (value); */
|
|
|
|
/* break; */
|
2005-01-09 16:30:15 +00:00
|
|
|
case ARG_COMPRESSION_LEVEL:
|
|
|
|
pngenc->compression_level = g_value_get_uint (value);
|
|
|
|
break;
|
2004-07-27 09:59:17 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2002-11-18 22:29:38 +00:00
|
|
|
}
|