ivfparse: port to baseparse.

https://bugzilla.gnome.org/show_bug.cgi?id=710855

Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
This commit is contained in:
Gwenole Beauchesne 2014-03-04 15:46:58 +01:00
parent bf6959000f
commit 3d0ce67fcd
2 changed files with 201 additions and 167 deletions

View file

@ -51,6 +51,9 @@
#include "gstivfparse.h" #include "gstivfparse.h"
#define IVF_FILE_HEADER_SIZE 32
#define IVF_FRAME_HEADER_SIZE 12
GST_DEBUG_CATEGORY_STATIC (gst_ivf_parse_debug); GST_DEBUG_CATEGORY_STATIC (gst_ivf_parse_debug);
#define GST_CAT_DEFAULT gst_ivf_parse_debug #define GST_CAT_DEFAULT gst_ivf_parse_debug
@ -67,48 +70,51 @@ static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
GST_STATIC_CAPS ("ANY") GST_STATIC_CAPS ("ANY")
); );
GST_BOILERPLATE (GstIvfParse, gst_ivf_parse, GstElement, GST_TYPE_ELEMENT); #define gst_ivf_parse_parent_class parent_class
G_DEFINE_TYPE (GstIvfParse, gst_ivf_parse, GST_TYPE_BASE_PARSE);
static void gst_ivf_parse_dispose (GObject * object); static void gst_ivf_parse_finalize (GObject * object);
static GstFlowReturn gst_ivf_parse_chain (GstPad * pad, GstBuffer * buf); static gboolean gst_ivf_parse_start (GstBaseParse * parse);
static gboolean gst_ivf_parse_stop (GstBaseParse * parse);
/* GObject vmethod implementations */ static GstFlowReturn
gst_ivf_parse_handle_frame (GstBaseParse * parse,
static void GstBaseParseFrame * frame, gint * skipsize);
gst_ivf_parse_base_init (gpointer gclass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
gst_element_class_set_static_metadata (element_class,
"IVF parser",
"Codec/Demuxer",
"Demuxes a IVF stream", "Philip Jägenstedt <philipj@opera.com>");
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&src_factory));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sink_factory));
}
/* initialize the ivfparse's class */ /* initialize the ivfparse's class */
static void static void
gst_ivf_parse_class_init (GstIvfParseClass * klass) gst_ivf_parse_class_init (GstIvfParseClass * klass)
{ {
GObjectClass *gobject_class; GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseParseClass *gstbaseparse_class;
gobject_class = (GObjectClass *) klass; gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbaseparse_class = (GstBaseParseClass *) klass;
gobject_class->dispose = gst_ivf_parse_dispose; gobject_class->finalize = gst_ivf_parse_finalize;
gstbaseparse_class->start = gst_ivf_parse_start;
gstbaseparse_class->stop = gst_ivf_parse_stop;
gstbaseparse_class->handle_frame = gst_ivf_parse_handle_frame;
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&src_factory));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&sink_factory));
gst_element_class_set_static_metadata (gstelement_class,
"IVF parser", "Codec/Demuxer",
"Demuxes a IVF stream", "Philip Jägenstedt <philipj@opera.com>");
/* debug category for filtering log messages */
GST_DEBUG_CATEGORY_INIT (gst_ivf_parse_debug, "ivfparse", 0, "IVF parser");
} }
static void static void
gst_ivf_parse_reset (GstIvfParse * ivf) gst_ivf_parse_reset (GstIvfParse * ivf)
{ {
if (ivf->adapter) {
gst_adapter_clear (ivf->adapter);
g_object_unref (ivf->adapter);
ivf->adapter = NULL;
}
ivf->state = GST_IVF_PARSE_START; ivf->state = GST_IVF_PARSE_START;
ivf->rate_num = 0; ivf->rate_num = 0;
ivf->rate_den = 0; ivf->rate_den = 0;
@ -120,84 +126,77 @@ gst_ivf_parse_reset (GstIvfParse * ivf)
* initialize instance structure * initialize instance structure
*/ */
static void static void
gst_ivf_parse_init (GstIvfParse * ivf, GstIvfParseClass * gclass) gst_ivf_parse_init (GstIvfParse * ivf)
{ {
/* sink pad */
ivf->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
gst_pad_set_chain_function (ivf->sinkpad,
GST_DEBUG_FUNCPTR (gst_ivf_parse_chain));
/* src pad */
ivf->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
gst_pad_use_fixed_caps (ivf->srcpad);
gst_element_add_pad (GST_ELEMENT (ivf), ivf->sinkpad);
gst_element_add_pad (GST_ELEMENT (ivf), ivf->srcpad);
/* reset */
gst_ivf_parse_reset (ivf); gst_ivf_parse_reset (ivf);
} }
static void static void
gst_ivf_parse_dispose (GObject * object) gst_ivf_parse_finalize (GObject * object)
{ {
GstIvfParse *ivf = GST_IVF_PARSE (object); GstIvfParse *const ivf = GST_IVF_PARSE (object);
GST_DEBUG_OBJECT (ivf, "disposing"); GST_DEBUG_OBJECT (ivf, "finalizing");
gst_ivf_parse_reset (ivf); gst_ivf_parse_reset (ivf);
G_OBJECT_CLASS (parent_class)->dispose (object); G_OBJECT_CLASS (parent_class)->finalize (object);
} }
/* GstElement vmethod implementations */ static gboolean
gst_ivf_parse_start (GstBaseParse * parse)
/* chain function
* this function does the actual processing
*/
static GstFlowReturn
gst_ivf_parse_chain (GstPad * pad, GstBuffer * buf)
{ {
GstIvfParse *ivf = GST_IVF_PARSE (GST_OBJECT_PARENT (pad)); GstIvfParse *const ivf = GST_IVF_PARSE (parse);
gboolean res;
/* lazy creation of the adapter */ gst_ivf_parse_reset (ivf);
if (G_UNLIKELY (ivf->adapter == NULL)) {
ivf->adapter = gst_adapter_new (); /* Minimal file header size needed at start */
gst_base_parse_set_min_frame_size (parse, IVF_FILE_HEADER_SIZE);
/* No sync code to detect frame boundaries */
gst_base_parse_set_syncable (parse, FALSE);
return TRUE;
} }
GST_LOG_OBJECT (ivf, "Pushing buffer of size %u to adapter", static gboolean
GST_BUFFER_SIZE (buf)); gst_ivf_parse_stop (GstBaseParse * parse)
{
GstIvfParse *const ivf = GST_IVF_PARSE (parse);
gst_adapter_push (ivf->adapter, buf); /* adapter takes ownership of buf */ gst_ivf_parse_reset (ivf);
res = GST_FLOW_OK; return TRUE;
}
switch (ivf->state) { static GstFlowReturn
case GST_IVF_PARSE_START: gst_ivf_parse_handle_frame_start (GstIvfParse * ivf, GstBaseParseFrame * frame,
if (gst_adapter_available (ivf->adapter) >= 32) { gint * skipsize)
{
GstBuffer *const buffer = frame->buffer;
GstMapInfo map;
GstFlowReturn ret = GST_FLOW_OK;
GstCaps *caps; GstCaps *caps;
const guint8 *data = gst_adapter_peek (ivf->adapter, 32); gst_buffer_map (buffer, &map, GST_MAP_READ);
guint32 magic = GST_READ_UINT32_LE (data); if (map.size >= IVF_FILE_HEADER_SIZE) {
guint16 version = GST_READ_UINT16_LE (data + 4); guint32 magic = GST_READ_UINT32_LE (map.data);
guint16 header_size = GST_READ_UINT16_LE (data + 6); guint16 version = GST_READ_UINT16_LE (map.data + 4);
guint32 fourcc = GST_READ_UINT32_LE (data + 8); guint16 header_size = GST_READ_UINT16_LE (map.data + 6);
guint16 width = GST_READ_UINT16_LE (data + 12); guint32 fourcc = GST_READ_UINT32_LE (map.data + 8);
guint16 height = GST_READ_UINT16_LE (data + 14); guint16 width = GST_READ_UINT16_LE (map.data + 12);
guint32 rate_num = GST_READ_UINT32_LE (data + 16); guint16 height = GST_READ_UINT16_LE (map.data + 14);
guint32 rate_den = GST_READ_UINT32_LE (data + 20); guint32 rate_num = GST_READ_UINT32_LE (map.data + 16);
guint32 rate_den = GST_READ_UINT32_LE (map.data + 20);
#ifndef GST_DISABLE_GST_DEBUG #ifndef GST_DISABLE_GST_DEBUG
guint32 num_frames = GST_READ_UINT32_LE (data + 24); guint32 num_frames = GST_READ_UINT32_LE (map.data + 24);
#endif #endif
/* last 4 bytes unused */
gst_adapter_flush (ivf->adapter, 32);
if (magic != GST_MAKE_FOURCC ('D', 'K', 'I', 'F') || if (magic != GST_MAKE_FOURCC ('D', 'K', 'I', 'F') ||
version != 0 || header_size != 32 || version != 0 || header_size != 32 ||
fourcc != GST_MAKE_FOURCC ('V', 'P', '8', '0')) { fourcc != GST_MAKE_FOURCC ('V', 'P', '8', '0')) {
GST_ELEMENT_ERROR (ivf, STREAM, WRONG_TYPE, (NULL), (NULL)); GST_ELEMENT_ERROR (ivf, STREAM, WRONG_TYPE, (NULL), (NULL));
return GST_FLOW_ERROR; ret = GST_FLOW_ERROR;
goto end;
} }
/* create src pad caps */ /* create src pad caps */
@ -209,81 +208,120 @@ gst_ivf_parse_chain (GstPad * pad, GstBuffer * buf)
GST_LOG_OBJECT (ivf, "Stream has %d frames", num_frames); GST_LOG_OBJECT (ivf, "Stream has %d frames", num_frames);
gst_pad_set_caps (ivf->srcpad, caps); gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (ivf), caps);
gst_caps_unref (caps); gst_caps_unref (caps);
/* keep framerate in instance for convenience */ /* keep framerate in instance for convenience */
ivf->rate_num = rate_num; ivf->rate_num = rate_num;
ivf->rate_den = rate_den; ivf->rate_den = rate_den;
gst_pad_push_event (ivf->srcpad, gst_event_new_new_segment (FALSE, 1.0,
GST_FORMAT_TIME, 0, -1, 0));
/* move along */ /* move along */
ivf->state = GST_IVF_PARSE_DATA; ivf->state = GST_IVF_PARSE_DATA;
gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (ivf),
IVF_FRAME_HEADER_SIZE);
*skipsize = IVF_FILE_HEADER_SIZE;
} else { } else {
GST_LOG_OBJECT (ivf, "Header data not yet available."); GST_LOG_OBJECT (ivf, "Header data not yet available.");
break; *skipsize = 0;
} }
/* fall through */ end:
gst_buffer_unmap (buffer, &map);
return ret;
}
case GST_IVF_PARSE_DATA: static GstFlowReturn
while (gst_adapter_available (ivf->adapter) > 12) { gst_ivf_parse_handle_frame_data (GstIvfParse * ivf, GstBaseParseFrame * frame,
const guint8 *data = gst_adapter_peek (ivf->adapter, 12); gint * skipsize)
guint32 frame_size = GST_READ_UINT32_LE (data); {
guint64 frame_pts = GST_READ_UINT64_LE (data + 4); GstBuffer *const buffer = frame->buffer;
GstMapInfo map;
GstFlowReturn ret = GST_FLOW_OK;
GstBuffer *out_buffer;
gst_buffer_map (buffer, &map, GST_MAP_READ);
if (map.size >= IVF_FILE_HEADER_SIZE) {
guint32 frame_size = GST_READ_UINT32_LE (map.data);
guint64 frame_pts = GST_READ_UINT64_LE (map.data + 4);
GST_LOG_OBJECT (ivf, GST_LOG_OBJECT (ivf,
"Read frame header: size %u, pts %" G_GUINT64_FORMAT, frame_size, "Read frame header: size %u, pts %" G_GUINT64_FORMAT, frame_size,
frame_pts); frame_pts);
if (gst_adapter_available (ivf->adapter) >= 12 + frame_size) { if (map.size < IVF_FRAME_HEADER_SIZE + frame_size) {
GstBuffer *frame; gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (ivf),
IVF_FRAME_HEADER_SIZE + frame_size);
gst_buffer_unmap (buffer, &map);
*skipsize = 0;
goto end;
}
gst_adapter_flush (ivf->adapter, 12); gst_buffer_unmap (buffer, &map);
frame = gst_adapter_take_buffer (ivf->adapter, frame_size); /* Eventually, we would need the buffer memory in a merged state anyway */
gst_buffer_set_caps (frame, GST_PAD_CAPS (ivf->srcpad)); out_buffer = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_FLAGS |
GST_BUFFER_TIMESTAMP (frame) = GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_META |
GST_BUFFER_COPY_MEMORY | GST_BUFFER_COPY_MERGE,
IVF_FRAME_HEADER_SIZE, frame_size);
if (!out_buffer) {
GST_ERROR_OBJECT (ivf, "Failed to copy frame buffer");
ret = GST_FLOW_ERROR;
*skipsize = IVF_FRAME_HEADER_SIZE + frame_size;
goto end;
}
gst_buffer_replace (&frame->out_buffer, out_buffer);
gst_buffer_unref (out_buffer);
GST_BUFFER_TIMESTAMP (out_buffer) =
gst_util_uint64_scale_int (GST_SECOND * frame_pts, ivf->rate_den, gst_util_uint64_scale_int (GST_SECOND * frame_pts, ivf->rate_den,
ivf->rate_num); ivf->rate_num);
GST_BUFFER_DURATION (frame) = GST_BUFFER_DURATION (out_buffer) =
gst_util_uint64_scale_int (GST_SECOND, ivf->rate_den, gst_util_uint64_scale_int (GST_SECOND, ivf->rate_den, ivf->rate_num);
ivf->rate_num);
GST_DEBUG_OBJECT (ivf, "Pushing frame of size %u, ts %" GST_DEBUG_OBJECT (ivf, "Pushing frame of size %u, ts %"
GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", off %"
G_GUINT64_FORMAT ", off_end %" G_GUINT64_FORMAT, G_GUINT64_FORMAT ", off_end %" G_GUINT64_FORMAT,
GST_BUFFER_SIZE (frame), frame_size,
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (frame)), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (out_buffer)),
GST_TIME_ARGS (GST_BUFFER_DURATION (frame)), GST_TIME_ARGS (GST_BUFFER_DURATION (out_buffer)),
GST_BUFFER_OFFSET (frame), GST_BUFFER_OFFSET_END (frame)); GST_BUFFER_OFFSET (out_buffer), GST_BUFFER_OFFSET_END (out_buffer));
res = gst_pad_push (ivf->srcpad, frame); ret = gst_base_parse_finish_frame (GST_BASE_PARSE_CAST (ivf), frame,
if (res != GST_FLOW_OK) IVF_FRAME_HEADER_SIZE + frame_size);
break; *skipsize = 0;
} else { } else {
GST_LOG_OBJECT (ivf, "Frame data not yet available."); GST_LOG_OBJECT (ivf, "Frame data not yet available.");
break; gst_buffer_unmap (buffer, &map);
*skipsize = 0;
} }
}
break;
end:
return ret;
}
static GstFlowReturn
gst_ivf_parse_handle_frame (GstBaseParse * parse,
GstBaseParseFrame * frame, gint * skipsize)
{
GstIvfParse *const ivf = GST_IVF_PARSE (parse);
switch (ivf->state) {
case GST_IVF_PARSE_START:
return gst_ivf_parse_handle_frame_start (ivf, frame, skipsize);
case GST_IVF_PARSE_DATA:
return gst_ivf_parse_handle_frame_data (ivf, frame, skipsize);
default: default:
g_return_val_if_reached (GST_FLOW_ERROR); break;
} }
return res; g_assert_not_reached ();
return GST_FLOW_ERROR;
} }
/* entry point to initialize the plug-in */ /* entry point to initialize the plug-in */
static gboolean static gboolean
ivfparse_init (GstPlugin * ivfparse) ivfparse_init (GstPlugin * ivfparse)
{ {
/* debug category for filtering log messages */
GST_DEBUG_CATEGORY_INIT (gst_ivf_parse_debug, "ivfparse", 0, "IVF parser");
/* register parser element */ /* register parser element */
if (!gst_element_register (ivfparse, "ivfparse", GST_RANK_PRIMARY, if (!gst_element_register (ivfparse, "ivfparse", GST_RANK_PRIMARY,
GST_TYPE_IVF_PARSE)) GST_TYPE_IVF_PARSE))

View file

@ -23,7 +23,7 @@
#define __GST_IVF_PARSE_H__ #define __GST_IVF_PARSE_H__
#include <gst/gst.h> #include <gst/gst.h>
#include <gst/base/gstadapter.h> #include <gst/base/gstbaseparse.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -48,11 +48,7 @@ typedef struct _GstIvfParseClass GstIvfParseClass;
struct _GstIvfParse struct _GstIvfParse
{ {
GstElement element; GstBaseParse baseparse;
GstPad *sinkpad, *srcpad;
GstAdapter *adapter;
GstIvfParseState state; GstIvfParseState state;
@ -63,7 +59,7 @@ struct _GstIvfParse
struct _GstIvfParseClass struct _GstIvfParseClass
{ {
GstElementClass parent_class; GstBaseParseClass parent_class;
}; };
GType gst_ivf_parse_get_type (void); GType gst_ivf_parse_get_type (void);