png: Port to base video classes

Conflicts:

	ext/libpng/gstpngdec.c
	ext/libpng/gstpngdec.h
	ext/libpng/gstpngenc.c
	ext/libpng/gstpngenc.h

Reverted to 0.10, needs to be ported again.
This commit is contained in:
Edward Hervey 2012-04-07 09:52:09 +02:00 committed by Sebastian Dröge
parent 3986174aa9
commit 0b82806547
4 changed files with 266 additions and 885 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,7 @@
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
* Copyright (C) 2012 Collabora Ltd.
* Author : Edward Hervey <edward@collabora.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -22,9 +24,7 @@
#define __GST_PNGDEC_H__
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/video/gstvideopool.h>
#include <gst/video/gstvideodecoder.h>
#include <png.h>
G_BEGIN_DECLS
@ -40,45 +40,30 @@ typedef struct _GstPngDecClass GstPngDecClass;
struct _GstPngDec
{
GstElement element;
GstVideoDecoder parent;
GstPad *sinkpad, *srcpad;
GstVideoCodecState *input_state;
GstVideoCodecState *output_state;
GstVideoCodecFrame *current_frame;
gboolean need_newsegment;
GstFlowReturn ret;
/* Progressive */
GstBuffer *buffer_out;
GstFlowReturn ret;
png_uint_32 rowbytes;
/* Pull range */
gint offset;
png_structp png;
png_infop info;
png_infop endinfo;
gboolean setup;
GstVideoInfo vinfo;
gint width;
gint height;
gint color_type;
gint fps_n;
gint fps_d;
GstBufferPool *pool;
/* Chain */
gboolean framed;
GstClockTime in_timestamp;
GstClockTime in_duration;
GstSegment segment;
gboolean image_ready;
};
struct _GstPngDecClass
{
GstElementClass parent_class;
GstVideoDecoderClass parent_class;
};
GType gst_pngdec_get_type(void);

View file

@ -4,6 +4,9 @@
* Filter:
* Copyright (C) 2000 Donald A. Graft
*
* Copyright (C) 2012 Collabora Ltd.
* Author : Edward Hervey <edward@collabora.com>
*
* 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
@ -27,6 +30,7 @@
#include <string.h>
#include <gst/gst.h>
#include "gstpngenc.h"
#include <gst/video/video.h>
#include <zlib.h>
GST_DEBUG_CATEGORY_STATIC (pngenc_debug);
@ -39,15 +43,13 @@ enum
LAST_SIGNAL
};
#define DEFAULT_SNAPSHOT FALSE
/* #define DEFAULT_NEWMEDIA FALSE */
#define DEFAULT_SNAPSHOT TRUE
#define DEFAULT_COMPRESSION_LEVEL 6
enum
{
ARG_0,
ARG_SNAPSHOT,
/* ARG_NEWMEDIA, */
ARG_COMPRESSION_LEVEL
};
@ -61,25 +63,25 @@ GST_STATIC_PAD_TEMPLATE ("src",
);
static GstStaticPadTemplate pngenc_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBA, RGB, GRAY8, GRAY16_BE }"))
GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB ";"
GST_VIDEO_CAPS_GRAY8 ";" GST_VIDEO_CAPS_GRAY16 ("BIG_ENDIAN"))
);
/* static GstElementClass *parent_class = NULL; */
G_DEFINE_TYPE (GstPngEnc, gst_pngenc, GST_TYPE_ELEMENT);
GST_BOILERPLATE (GstPngEnc, gst_pngenc, GstVideoEncoder,
GST_TYPE_VIDEO_ENCODER);
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);
static GstFlowReturn gst_pngenc_chain (GstPad * pad, GstObject * parent,
GstBuffer * data);
static gboolean gst_pngenc_sink_event (GstPad * pad, GstObject * parent,
GstEvent * event);
static GstFlowReturn gst_pngenc_handle_frame (GstVideoEncoder * encoder,
GstVideoCodecFrame * frame);
static gboolean gst_pngenc_set_format (GstVideoEncoder * encoder,
GstVideoCodecState * state);
static void
user_error_fn (png_structp png_ptr, png_const_charp error_msg)
@ -93,14 +95,31 @@ user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
g_warning ("%s", warning_msg);
}
static void
gst_pngenc_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_static_pad_template
(element_class, &pngenc_sink_template);
gst_element_class_add_static_pad_template
(element_class, &pngenc_src_template);
gst_element_class_set_details_simple (element_class, "PNG image encoder",
"Codec/Encoder/Image",
"Encode a video frame to a .png image",
"Jeremy SIMON <jsimon13@yahoo.fr>");
}
static void
gst_pngenc_class_init (GstPngEncClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *element_class;
GstVideoEncoderClass *venc_class;
gobject_class = (GObjectClass *) klass;
element_class = (GstElementClass *) klass;
venc_class = (GstVideoEncoderClass *) klass;
parent_class = g_type_class_peek_parent (klass);
gobject_class->get_property = gst_pngenc_get_property;
gobject_class->set_property = gst_pngenc_set_property;
@ -110,11 +129,6 @@ gst_pngenc_class_init (GstPngEncClass * klass)
"Send EOS after encoding a frame, useful for snapshots",
DEFAULT_SNAPSHOT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/* g_object_class_install_property (gobject_class, ARG_NEWMEDIA, */
/* g_param_spec_boolean ("newmedia", "newmedia", */
/* "Send new media discontinuity after encoding each frame", */
/* DEFAULT_NEWMEDIA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); */
g_object_class_install_property (gobject_class, ARG_COMPRESSION_LEVEL,
g_param_spec_uint ("compression-level", "compression-level",
"PNG compression level",
@ -122,110 +136,71 @@ gst_pngenc_class_init (GstPngEncClass * klass)
DEFAULT_COMPRESSION_LEVEL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
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));
gst_element_class_set_static_metadata (element_class, "PNG image encoder",
"Codec/Encoder/Image",
"Encode a video frame to a .png image",
"Jeremy SIMON <jsimon13@yahoo.fr>");
venc_class->set_format = gst_pngenc_set_format;
venc_class->handle_frame = gst_pngenc_handle_frame;
GST_DEBUG_CATEGORY_INIT (pngenc_debug, "pngenc", 0, "PNG image encoder");
}
static gboolean
gst_pngenc_setcaps (GstPngEnc * pngenc, GstCaps * caps)
gst_pngenc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state)
{
int fps_n, fps_d;
GstCaps *pcaps;
gboolean ret;
GstVideoInfo info;
GstPngEnc *pngenc;
gboolean ret = TRUE;
GstVideoInfo *info;
GstVideoCodecState *output_state;
ret = gst_video_info_from_caps (&info, caps);
pngenc = GST_PNGENC (encoder);
info = &state->info;
if (G_UNLIKELY (!ret))
goto done;
pngenc->info = info;
switch (GST_VIDEO_INFO_FORMAT (&info)) {
switch (GST_VIDEO_INFO_FORMAT (info)) {
case GST_VIDEO_FORMAT_RGBA:
pngenc->png_color_type = PNG_COLOR_TYPE_RGBA;
pngenc->depth = 8;
break;
case GST_VIDEO_FORMAT_RGB:
pngenc->png_color_type = PNG_COLOR_TYPE_RGB;
pngenc->depth = 8;
break;
case GST_VIDEO_FORMAT_GRAY8:
pngenc->png_color_type = PNG_COLOR_TYPE_GRAY;
pngenc->depth = 8;
break;
case GST_VIDEO_FORMAT_GRAY16_BE:
pngenc->png_color_type = PNG_COLOR_TYPE_GRAY;
pngenc->depth = 16;
break;
default:
ret = FALSE;
goto done;
}
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);
if (G_UNLIKELY (pngenc->width < 16 || pngenc->width > 1000000 ||
pngenc->height < 16 || pngenc->height > 1000000)) {
ret = FALSE;
goto done;
switch (GST_VIDEO_INFO_FORMAT (info)) {
case GST_VIDEO_FORMAT_GRAY16_BE:
pngenc->depth = 16;
break;
default: /* GST_VIDEO_FORMAT_RGB and GST_VIDEO_FORMAT_GRAY8 */
pngenc->depth = 8;
break;
}
pcaps = gst_caps_new_simple ("image/png",
"width", G_TYPE_INT, pngenc->width,
"height", G_TYPE_INT, pngenc->height,
"framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
if (pngenc->input_state)
gst_video_codec_state_unref (pngenc->input_state);
pngenc->input_state = gst_video_codec_state_ref (state);
ret = gst_pad_set_caps (pngenc->srcpad, pcaps);
output_state =
gst_video_encoder_set_output_state (encoder,
gst_caps_new_simple ("image/png", NULL), state);
gst_video_codec_state_unref (output_state);
gst_caps_unref (pcaps);
/* Fall-through. */
done:
if (G_UNLIKELY (!ret)) {
pngenc->width = 0;
pngenc->height = 0;
}
return ret;
}
static void
gst_pngenc_init (GstPngEnc * pngenc)
gst_pngenc_init (GstPngEnc * pngenc, GstPngEncClass * g_class)
{
/* sinkpad */
pngenc->sinkpad = gst_pad_new_from_static_template
(&pngenc_sink_template, "sink");
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));
gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->sinkpad);
/* srcpad */
pngenc->srcpad = gst_pad_new_from_static_template
(&pngenc_src_template, "src");
gst_pad_use_fixed_caps (pngenc->srcpad);
gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->srcpad);
/* init settings */
pngenc->png_struct_ptr = NULL;
pngenc->png_info_ptr = NULL;
pngenc->snapshot = DEFAULT_SNAPSHOT;
/* pngenc->newmedia = FALSE; */
pngenc->compression_level = DEFAULT_COMPRESSION_LEVEL;
}
@ -238,13 +213,10 @@ static void
user_write_data (png_structp png_ptr, png_bytep data, png_uint_32 length)
{
GstPngEnc *pngenc;
GstMapInfo map;
pngenc = (GstPngEnc *) png_get_io_ptr (png_ptr);
gst_buffer_map (pngenc->buffer_out, &map, GST_MAP_WRITE);
if (pngenc->written + length >= map.size) {
gst_buffer_unmap (pngenc->buffer_out, &map);
if (pngenc->written + length >= GST_BUFFER_SIZE (pngenc->buffer_out)) {
GST_ERROR_OBJECT (pngenc, "output buffer bigger than the input buffer!?");
png_error (png_ptr, "output buffer bigger than the input buffer!?");
@ -252,66 +224,37 @@ user_write_data (png_structp png_ptr, png_bytep data, png_uint_32 length)
return;
}
GST_DEBUG_OBJECT (pngenc, "writing %u bytes", (guint) length);
memcpy (map.data + pngenc->written, data, length);
gst_buffer_unmap (pngenc->buffer_out, &map);
memcpy (GST_BUFFER_DATA (pngenc->buffer_out) + pngenc->written, data, length);
pngenc->written += length;
}
static GstFlowReturn
gst_pngenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
gst_pngenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
{
GstPngEnc *pngenc;
gint row_index;
png_byte **row_pointers;
GstFlowReturn ret = GST_FLOW_OK;
GstBuffer *encoded_buf = NULL;
GstVideoFrame frame;
GstVideoInfo *info;
pngenc = GST_PNGENC (parent);
pngenc = GST_PNGENC (encoder);
info = &pngenc->input_state->info;
GST_DEBUG_OBJECT (pngenc, "BEGINNING");
if (G_UNLIKELY (pngenc->width <= 0 || pngenc->height <= 0)) {
ret = GST_FLOW_NOT_NEGOTIATED;
goto exit;
}
if (!gst_video_frame_map (&frame, &pngenc->info, buf, GST_MAP_READ)) {
GST_ELEMENT_ERROR (pngenc, STREAM, FORMAT, (NULL),
("Failed to map video frame, caps problem?"));
ret = GST_FLOW_ERROR;
goto exit;
}
/* 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);
if (pngenc->png_struct_ptr == NULL) {
GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
("Failed to initialize png structure"));
ret = GST_FLOW_ERROR;
goto done;
}
if (pngenc->png_struct_ptr == NULL)
goto struct_init_fail;
pngenc->png_info_ptr = png_create_info_struct (pngenc->png_struct_ptr);
if (!pngenc->png_info_ptr) {
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"));
ret = GST_FLOW_ERROR;
goto done;
}
if (!pngenc->png_info_ptr)
goto png_info_fail;
/* non-0 return is from a longjmp inside of libpng */
if (setjmp (png_jmpbuf (pngenc->png_struct_ptr)) != 0) {
png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
GST_ELEMENT_ERROR (pngenc, LIBRARY, FAILED, (NULL),
("returning from longjmp"));
ret = GST_FLOW_ERROR;
goto done;
}
if (setjmp (png_jmpbuf (pngenc->png_struct_ptr)) != 0)
goto longjmp_fail;
png_set_filter (pngenc->png_struct_ptr, 0,
PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
@ -319,8 +262,8 @@ gst_pngenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
png_set_IHDR (pngenc->png_struct_ptr,
pngenc->png_info_ptr,
pngenc->width,
pngenc->height,
GST_VIDEO_INFO_WIDTH (info),
GST_VIDEO_INFO_HEIGHT (info),
pngenc->depth,
pngenc->png_color_type,
PNG_INTERLACE_NONE,
@ -329,16 +272,17 @@ gst_pngenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
png_set_write_fn (pngenc->png_struct_ptr, pngenc,
(png_rw_ptr) user_write_data, user_flush_data);
row_pointers = g_new (png_byte *, pngenc->height);
row_pointers = g_new (png_byte *, GST_VIDEO_INFO_HEIGHT (info));
for (row_index = 0; row_index < pngenc->height; row_index++) {
row_pointers[row_index] = GST_VIDEO_FRAME_COMP_DATA (&frame, 0) +
(row_index * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0));
for (row_index = 0; row_index < GST_VIDEO_INFO_HEIGHT (info); row_index++) {
row_pointers[row_index] = GST_BUFFER_DATA (frame->input_buffer) +
(row_index * GST_VIDEO_INFO_COMP_STRIDE (info, 0));
}
/* allocate the output buffer */
pngenc->buffer_out =
gst_buffer_new_and_alloc (pngenc->height * pngenc->width);
gst_buffer_new_and_alloc (GST_VIDEO_INFO_HEIGHT (info) *
GST_VIDEO_INFO_COMP_STRIDE (info, 0));
pngenc->written = 0;
png_write_info (pngenc->png_struct_ptr, pngenc->png_info_ptr);
@ -347,71 +291,52 @@ gst_pngenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
g_free (row_pointers);
GST_DEBUG_OBJECT (pngenc, "written %d", pngenc->written);
encoded_buf =
gst_buffer_copy_region (pngenc->buffer_out, GST_BUFFER_COPY_MEMORY,
0, pngenc->written);
png_destroy_info_struct (pngenc->png_struct_ptr, &pngenc->png_info_ptr);
png_destroy_write_struct (&pngenc->png_struct_ptr, (png_infopp) NULL);
GST_BUFFER_TIMESTAMP (encoded_buf) = GST_BUFFER_TIMESTAMP (buf);
GST_BUFFER_DURATION (encoded_buf) = GST_BUFFER_DURATION (buf);
/* Set final size and store */
GST_BUFFER_SIZE (pngenc->buffer_out) = pngenc->written;
frame->output_buffer = pngenc->buffer_out;
if ((ret = gst_pad_push (pngenc->srcpad, encoded_buf)) != GST_FLOW_OK)
pngenc->buffer_out = NULL;
if ((ret = gst_video_encoder_finish_frame (encoder, frame)) != GST_FLOW_OK)
goto done;
if (pngenc->snapshot) {
GstEvent *event;
GST_DEBUG_OBJECT (pngenc, "snapshot mode, sending EOS");
/* send EOS event, since a frame has been pushed out */
event = gst_event_new_eos ();
gst_pad_push_event (pngenc->srcpad, event);
ret = GST_FLOW_EOS;
}
if (pngenc->snapshot)
ret = GST_FLOW_UNEXPECTED;
done:
gst_video_frame_unmap (&frame);
exit:
gst_buffer_unref (buf);
GST_DEBUG_OBJECT (pngenc, "END, ret:%d", ret);
if (pngenc->buffer_out != NULL) {
gst_buffer_unref (pngenc->buffer_out);
pngenc->buffer_out = NULL;
}
return ret;
}
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;
/* ERRORS */
struct_init_fail:
{
GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
("Failed to initialize png structure"));
return GST_FLOW_ERROR;
}
png_info_fail:
{
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"));
return GST_FLOW_ERROR;
}
longjmp_fail:
{
png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
GST_ELEMENT_ERROR (pngenc, LIBRARY, FAILED, (NULL),
("returning from longjmp"));
return GST_FLOW_ERROR;
}
return res;
}
static void
gst_pngenc_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec)
@ -424,9 +349,6 @@ gst_pngenc_get_property (GObject * object,
case ARG_SNAPSHOT:
g_value_set_boolean (value, pngenc->snapshot);
break;
/* case ARG_NEWMEDIA: */
/* g_value_set_boolean (value, pngenc->newmedia); */
/* break; */
case ARG_COMPRESSION_LEVEL:
g_value_set_uint (value, pngenc->compression_level);
break;
@ -449,9 +371,6 @@ gst_pngenc_set_property (GObject * object,
case ARG_SNAPSHOT:
pngenc->snapshot = g_value_get_boolean (value);
break;
/* case ARG_NEWMEDIA: */
/* pngenc->newmedia = g_value_get_boolean (value); */
/* break; */
case ARG_COMPRESSION_LEVEL:
pngenc->compression_level = g_value_get_uint (value);
break;

View file

@ -1,5 +1,7 @@
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
* Copyright (C) 2012 Collabora Ltd.
* Author : Edward Hervey <edward@collabora.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -22,7 +24,7 @@
#define __GST_PNGENC_H__
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/video/gstvideoencoder.h>
#include <png.h>
#ifdef __cplusplus
@ -41,20 +43,17 @@ typedef struct _GstPngEncClass GstPngEncClass;
struct _GstPngEnc
{
GstElement element;
GstVideoEncoder parent;
GstPad *sinkpad, *srcpad;
GstVideoCodecState *input_state;
GstBuffer *buffer_out;
guint written;
png_structp png_struct_ptr;
png_infop png_info_ptr;
GstVideoInfo info;
gint png_color_type;
gint depth;
gint width;
gint height;
guint compression_level;
gboolean snapshot;
@ -63,7 +62,7 @@ struct _GstPngEnc
struct _GstPngEncClass
{
GstElementClass parent_class;
GstVideoEncoderClass parent_class;
};
GType gst_pngenc_get_type(void);