basevideo: Add video encoder/decoder base classes from gst-plugins-bad

This commit is contained in:
Sebastian Dröge 2011-06-27 09:37:03 +02:00
parent 3dba85ea14
commit 93a675b570
7 changed files with 3884 additions and 0 deletions

186
omx/gstbasevideocodec.c Normal file
View file

@ -0,0 +1,186 @@
/* GStreamer
* Copyright (C) 2006 David Schleef <ds@schleef.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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
#include "gstbasevideocodec.h"
#include <string.h>
#include <math.h>
GST_DEBUG_CATEGORY (basevideocodec_debug);
#define GST_CAT_DEFAULT basevideocodec_debug
/* GstBaseVideoCodec signals and args */
enum
{
LAST_SIGNAL
};
enum
{
ARG_0
};
static void gst_base_video_codec_finalize (GObject * object);
static GstStateChangeReturn gst_base_video_codec_change_state (GstElement *
element, GstStateChange transition);
GST_BOILERPLATE (GstBaseVideoCodec, gst_base_video_codec, GstElement,
GST_TYPE_ELEMENT);
static void
gst_base_video_codec_base_init (gpointer g_class)
{
GST_DEBUG_CATEGORY_INIT (basevideocodec_debug, "basevideocodec", 0,
"Base Video Codec");
}
static void
gst_base_video_codec_class_init (GstBaseVideoCodecClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *element_class;
gobject_class = G_OBJECT_CLASS (klass);
element_class = GST_ELEMENT_CLASS (klass);
gobject_class->finalize = gst_base_video_codec_finalize;
element_class->change_state = gst_base_video_codec_change_state;
}
static void
gst_base_video_codec_init (GstBaseVideoCodec * base_video_codec,
GstBaseVideoCodecClass * klass)
{
GstPadTemplate *pad_template;
GST_DEBUG_OBJECT (base_video_codec, "gst_base_video_codec_init");
pad_template =
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
g_return_if_fail (pad_template != NULL);
base_video_codec->sinkpad = gst_pad_new_from_template (pad_template, "sink");
gst_element_add_pad (GST_ELEMENT (base_video_codec),
base_video_codec->sinkpad);
pad_template =
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
g_return_if_fail (pad_template != NULL);
base_video_codec->srcpad = gst_pad_new_from_template (pad_template, "src");
gst_element_add_pad (GST_ELEMENT (base_video_codec),
base_video_codec->srcpad);
gst_segment_init (&base_video_codec->segment, GST_FORMAT_TIME);
}
static void
gst_base_video_codec_reset (GstBaseVideoCodec * base_video_codec)
{
GList *g;
GST_DEBUG_OBJECT (base_video_codec, "reset");
for (g = base_video_codec->frames; g; g = g_list_next (g)) {
gst_base_video_codec_free_frame ((GstVideoFrame *) g->data);
}
g_list_free (base_video_codec->frames);
base_video_codec->frames = NULL;
base_video_codec->bytes = 0;
base_video_codec->time = 0;
}
static void
gst_base_video_codec_finalize (GObject * object)
{
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static GstStateChangeReturn
gst_base_video_codec_change_state (GstElement * element,
GstStateChange transition)
{
GstBaseVideoCodec *base_video_codec = GST_BASE_VIDEO_CODEC (element);
GstStateChangeReturn ret;
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_base_video_codec_reset (base_video_codec);
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
break;
default:
break;
}
ret = parent_class->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
gst_base_video_codec_reset (base_video_codec);
break;
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
return ret;
}
GstVideoFrame *
gst_base_video_codec_new_frame (GstBaseVideoCodec * base_video_codec)
{
GstVideoFrame *frame;
frame = g_malloc0 (sizeof (GstVideoFrame));
frame->system_frame_number = base_video_codec->system_frame_number;
base_video_codec->system_frame_number++;
return frame;
}
void
gst_base_video_codec_free_frame (GstVideoFrame * frame)
{
if (frame->sink_buffer) {
gst_buffer_unref (frame->sink_buffer);
}
if (frame->src_buffer) {
gst_buffer_unref (frame->src_buffer);
}
g_free (frame);
}

185
omx/gstbasevideocodec.h Normal file
View file

@ -0,0 +1,185 @@
/* GStreamer
* Copyright (C) 2008 David Schleef <ds@schleef.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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.
*/
#ifndef _GST_BASE_VIDEO_CODEC_H_
#define _GST_BASE_VIDEO_CODEC_H_
#ifndef GST_USE_UNSTABLE_API
#warning "GstBaseVideoCodec is unstable API and may change in future."
#warning "You can define GST_USE_UNSTABLE_API to avoid this warning."
#endif
#include <gst/gst.h>
#include <gst/base/gstadapter.h>
#include <gst/video/video.h>
G_BEGIN_DECLS
#define GST_TYPE_BASE_VIDEO_CODEC \
(gst_base_video_codec_get_type())
#define GST_BASE_VIDEO_CODEC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BASE_VIDEO_CODEC,GstBaseVideoCodec))
#define GST_BASE_VIDEO_CODEC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BASE_VIDEO_CODEC,GstBaseVideoCodecClass))
#define GST_BASE_VIDEO_CODEC_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_BASE_VIDEO_CODEC,GstBaseVideoCodecClass))
#define GST_IS_BASE_VIDEO_CODEC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BASE_VIDEO_CODEC))
#define GST_IS_BASE_VIDEO_CODEC_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BASE_VIDEO_CODEC))
/**
* GST_BASE_VIDEO_CODEC_SINK_NAME:
*
* The name of the templates for the sink pad.
*/
#define GST_BASE_VIDEO_CODEC_SINK_NAME "sink"
/**
* GST_BASE_VIDEO_CODEC_SRC_NAME:
*
* The name of the templates for the source pad.
*/
#define GST_BASE_VIDEO_CODEC_SRC_NAME "src"
/**
* GST_BASE_VIDEO_CODEC_SRC_PAD:
* @obj: base video codec instance
*
* Gives the pointer to the source #GstPad object of the element.
*/
#define GST_BASE_VIDEO_CODEC_SRC_PAD(obj) (((GstBaseVideoCodec *) (obj))->srcpad)
/**
* GST_BASE_VIDEO_CODEC_SINK_PAD:
* @obj: base video codec instance
*
* Gives the pointer to the sink #GstPad object of the element.
*/
#define GST_BASE_VIDEO_CODEC_SINK_PAD(obj) (((GstBaseVideoCodec *) (obj))->sinkpad)
/**
* GST_BASE_VIDEO_CODEC_FLOW_NEED_DATA:
*
*/
#define GST_BASE_VIDEO_CODEC_FLOW_NEED_DATA GST_FLOW_CUSTOM_SUCCESS
typedef struct _GstVideoState GstVideoState;
typedef struct _GstVideoFrame GstVideoFrame;
typedef struct _GstBaseVideoCodec GstBaseVideoCodec;
typedef struct _GstBaseVideoCodecClass GstBaseVideoCodecClass;
struct _GstVideoState
{
GstVideoFormat format;
int width, height;
int fps_n, fps_d;
int par_n, par_d;
gboolean have_interlaced;
gboolean interlaced;
gboolean top_field_first;
int clean_width, clean_height;
int clean_offset_left, clean_offset_top;
int bytes_per_picture;
GstBuffer *codec_data;
};
struct _GstVideoFrame
{
GstClockTime decode_timestamp;
GstClockTime presentation_timestamp;
GstClockTime presentation_duration;
gint system_frame_number;
gint decode_frame_number;
gint presentation_frame_number;
int distance_from_sync;
gboolean is_sync_point;
gboolean is_eos;
GstBuffer *sink_buffer;
GstBuffer *src_buffer;
int field_index;
int n_fields;
void *coder_hook;
GstClockTime deadline;
gboolean force_keyframe;
};
struct _GstBaseVideoCodec
{
GstElement element;
/*< private >*/
GstPad *sinkpad;
GstPad *srcpad;
guint64 system_frame_number;
GList *frames;
GstVideoState state;
GstSegment segment;
gdouble proportion;
GstClockTime earliest_time;
gboolean discont;
gint64 bytes;
gint64 time;
/* FIXME before moving to base */
void *padding[GST_PADDING_LARGE];
};
struct _GstBaseVideoCodecClass
{
GstElementClass element_class;
/* FIXME before moving to base */
void *padding[GST_PADDING_LARGE];
};
GType gst_base_video_codec_get_type (void);
GstVideoFrame * gst_base_video_codec_new_frame (GstBaseVideoCodec *base_video_codec);
void gst_base_video_codec_free_frame (GstVideoFrame *frame);
gboolean gst_base_video_rawvideo_convert (GstVideoState *state,
GstFormat src_format, gint64 src_value,
GstFormat * dest_format, gint64 *dest_value);
gboolean gst_base_video_encoded_video_convert (GstVideoState * state,
gint64 bytes, gint64 time, GstFormat src_format,
gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
GstClockTime gst_video_state_get_timestamp (const GstVideoState *state,
GstSegment *segment, int frame_number);
G_END_DECLS
#endif

1988
omx/gstbasevideodecoder.c Normal file

File diff suppressed because it is too large Load diff

267
omx/gstbasevideodecoder.h Normal file
View file

@ -0,0 +1,267 @@
/* GStreamer
* Copyright (C) 2008 David Schleef <ds@schleef.org>
* Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
* Copyright (C) 2011 Nokia Corporation. All rights reserved.
* Contact: Stefan Kost <stefan.kost@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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.
*/
#ifndef _GST_BASE_VIDEO_DECODER_H_
#define _GST_BASE_VIDEO_DECODER_H_
#ifndef GST_USE_UNSTABLE_API
#warning "GstBaseVideoDecoder is unstable API and may change in future."
#warning "You can define GST_USE_UNSTABLE_API to avoid this warning."
#endif
#include "gstbasevideocodec.h"
G_BEGIN_DECLS
#define GST_TYPE_BASE_VIDEO_DECODER \
(gst_base_video_decoder_get_type())
#define GST_BASE_VIDEO_DECODER(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BASE_VIDEO_DECODER,GstBaseVideoDecoder))
#define GST_BASE_VIDEO_DECODER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BASE_VIDEO_DECODER,GstBaseVideoDecoderClass))
#define GST_BASE_VIDEO_DECODER_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_BASE_VIDEO_DECODER,GstBaseVideoDecoderClass))
#define GST_IS_BASE_VIDEO_DECODER(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BASE_VIDEO_DECODER))
#define GST_IS_BASE_VIDEO_DECODER_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BASE_VIDEO_DECODER))
/**
* GST_BASE_VIDEO_DECODER_SINK_NAME:
*
* The name of the templates for the sink pad.
*/
#define GST_BASE_VIDEO_DECODER_SINK_NAME "sink"
/**
* GST_BASE_VIDEO_DECODER_SRC_NAME:
*
* The name of the templates for the source pad.
*/
#define GST_BASE_VIDEO_DECODER_SRC_NAME "src"
/**
* GST_BASE_VIDEO_DECODER_FLOW_NEED_DATA:
*
* Returned while parsing to indicate more data is needed.
**/
#define GST_BASE_VIDEO_DECODER_FLOW_NEED_DATA GST_FLOW_CUSTOM_SUCCESS
typedef struct _GstBaseVideoDecoder GstBaseVideoDecoder;
typedef struct _GstBaseVideoDecoderClass GstBaseVideoDecoderClass;
/* do not use this one, use macro below */
GstFlowReturn _gst_base_video_decoder_error (GstBaseVideoDecoder *dec, gint weight,
GQuark domain, gint code,
gchar *txt, gchar *debug,
const gchar *file, const gchar *function,
gint line);
/**
* GST_BASE_VIDEO_DECODER_ERROR:
* @el: the base video decoder element that generates the error
* @weight: element defined weight of the error, added to error count
* @domain: like CORE, LIBRARY, RESOURCE or STREAM (see #gstreamer-GstGError)
* @code: error code defined for that domain (see #gstreamer-GstGError)
* @text: the message to display (format string and args enclosed in
* parentheses)
* @debug: debugging information for the message (format string and args
* enclosed in parentheses)
* @ret: variable to receive return value
*
* Utility function that audio decoder elements can use in case they encountered
* a data processing error that may be fatal for the current "data unit" but
* need not prevent subsequent decoding. Such errors are counted and if there
* are too many, as configured in the context's max_errors, the pipeline will
* post an error message and the application will be requested to stop further
* media processing. Otherwise, it is considered a "glitch" and only a warning
* is logged. In either case, @ret is set to the proper value to
* return to upstream/caller (indicating either GST_FLOW_ERROR or GST_FLOW_OK).
*/
#define GST_BASE_AUDIO_DECODER_ERROR(el, w, domain, code, text, debug, ret) \
G_STMT_START { \
gchar *__txt = _gst_element_error_printf text; \
gchar *__dbg = _gst_element_error_printf debug; \
GstBaseVideoDecoder *dec = GST_BASE_VIDEO_DECODER (el); \
ret = _gst_base_video_decoder_error (dec, w, GST_ ## domain ## _ERROR, \
GST_ ## domain ## _ERROR_ ## code, __txt, __dbg, __FILE__, \
GST_FUNCTION, __LINE__); \
} G_STMT_END
/**
* GstBaseVideoDecoder:
*
* The opaque #GstBaseVideoDecoder data structure.
*/
struct _GstBaseVideoDecoder
{
GstBaseVideoCodec base_video_codec;
/*< protected >*/
gboolean sink_clipping;
gboolean do_byte_time;
gboolean packetized;
gint max_errors;
/* parse tracking */
/* input data */
GstAdapter *input_adapter;
/* assembles current frame */
GstAdapter *output_adapter;
/*< private >*/
/* FIXME move to real private part ?
* (and introduce a context ?) */
/* ... being tracked here;
* only available during parsing */
/* FIXME remove and add parameter to method */
GstVideoFrame *current_frame;
/* relative offset of input data */
guint64 input_offset;
/* relative offset of frame */
guint64 frame_offset;
/* tracking ts and offsets */
GList *timestamps;
/* whether parsing is in sync */
gboolean have_sync;
/* maybe sort-of protected ? */
/* combine to yield (presentation) ts */
GstClockTime timestamp_offset;
int field_index;
/* last outgoing ts */
GstClockTime last_timestamp;
gint error_count;
/* reverse playback */
/* collect input */
GList *gather;
/* to-be-parsed */
GList *parse;
/* collected parsed frames */
GList *parse_gather;
/* frames to be handled == decoded */
GList *decode;
/* collected output */
GList *queued;
gboolean process;
/* no comment ... */
guint64 base_picture_number;
int reorder_depth;
int distance_from_sync;
/* FIXME before moving to base */
void *padding[GST_PADDING_LARGE];
};
/**
* GstBaseAudioDecoderClass:
* @start: Optional.
* Called when the element starts processing.
* Allows opening external resources.
* @stop: Optional.
* Called when the element stops processing.
* Allows closing external resources.
* @set_format: Notifies subclass of incoming data format (caps).
* @scan_for_sync: Optional.
* Allows subclass to obtain sync for subsequent parsing
* by custom means (above an beyond scanning for specific
* marker and mask).
* @parse_data: Required for non-packetized input.
* Allows chopping incoming data into manageable units (frames)
* for subsequent decoding.
* @reset: Optional.
* Allows subclass (codec) to perform post-seek semantics reset.
* @handle_frame: Provides input data frame to subclass.
* @finish: Optional.
* Called to request subclass to dispatch any pending remaining
* data (e.g. at EOS).
*
* Subclasses can override any of the available virtual methods or not, as
* needed. At minimum @handle_frame needs to be overridden, and @set_format
* and likely as well. If non-packetized input is supported or expected,
* @parse needs to be overridden as well.
*/
struct _GstBaseVideoDecoderClass
{
GstBaseVideoCodecClass base_video_codec_class;
gboolean (*start) (GstBaseVideoDecoder *coder);
gboolean (*stop) (GstBaseVideoDecoder *coder);
int (*scan_for_sync) (GstBaseVideoDecoder *decoder, gboolean at_eos,
int offset, int n);
GstFlowReturn (*parse_data) (GstBaseVideoDecoder *decoder, gboolean at_eos);
gboolean (*set_format) (GstBaseVideoDecoder *coder, GstVideoState * state);
gboolean (*reset) (GstBaseVideoDecoder *coder);
GstFlowReturn (*finish) (GstBaseVideoDecoder *coder);
GstFlowReturn (*handle_frame) (GstBaseVideoDecoder *coder, GstVideoFrame *frame);
/*< private >*/
guint32 capture_mask;
guint32 capture_pattern;
/* FIXME before moving to base */
void *padding[GST_PADDING_LARGE];
};
void gst_base_video_decoder_class_set_capture_pattern (GstBaseVideoDecoderClass *klass,
guint32 mask, guint32 pattern);
GstVideoFrame *gst_base_video_decoder_get_frame (GstBaseVideoDecoder *coder,
int frame_number);
GstVideoFrame *gst_base_video_decoder_get_oldest_frame (GstBaseVideoDecoder *coder);
void gst_base_video_decoder_add_to_frame (GstBaseVideoDecoder *base_video_decoder,
int n_bytes);
void gst_base_video_decoder_lost_sync (GstBaseVideoDecoder *base_video_decoder);
GstFlowReturn gst_base_video_decoder_have_frame (GstBaseVideoDecoder *base_video_decoder);
void gst_base_video_decoder_set_sync_point (GstBaseVideoDecoder *base_video_decoder);
gboolean gst_base_video_decoder_set_src_caps (GstBaseVideoDecoder *base_video_decoder);
GstBuffer *gst_base_video_decoder_alloc_src_buffer (GstBaseVideoDecoder * base_video_decoder);
GstFlowReturn gst_base_video_decoder_alloc_src_frame (GstBaseVideoDecoder *base_video_decoder,
GstVideoFrame *frame);
GstVideoState *gst_base_video_decoder_get_state (GstBaseVideoDecoder *base_video_decoder);
GstClockTimeDiff gst_base_video_decoder_get_max_decode_time (
GstBaseVideoDecoder *base_video_decoder,
GstVideoFrame *frame);
GstFlowReturn gst_base_video_decoder_finish_frame (GstBaseVideoDecoder *base_video_decoder,
GstVideoFrame *frame);
GType gst_base_video_decoder_get_type (void);
G_END_DECLS
#endif

917
omx/gstbasevideoencoder.c Normal file
View file

@ -0,0 +1,917 @@
/* GStreamer
* Copyright (C) 2008 David Schleef <ds@schleef.org>
* Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
* Copyright (C) 2011 Nokia Corporation. All rights reserved.
* Contact: Stefan Kost <stefan.kost@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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.
*/
/**
* SECTION:gstbasevideoencoder
* @short_description: Base class for video encoders
* @see_also: #GstBaseTransform
*
* This base class is for video encoders turning raw video into
* encoded video data.
*
* GstBaseVideoEncoder and subclass should cooperate as follows.
* <orderedlist>
* <listitem>
* <itemizedlist><title>Configuration</title>
* <listitem><para>
* Initially, GstBaseVideoEncoder calls @start when the encoder element
* is activated, which allows subclass to perform any global setup.
* </para></listitem>
* <listitem><para>
* GstBaseVideoEncoder calls @set_format to inform subclass of the format
* of input video data that it is about to receive. Subclass should
* setup for encoding and configure base class as appropriate
* (e.g. latency). While unlikely, it might be called more than once,
* if changing input parameters require reconfiguration. Baseclass
* will ensure that processing of current configuration is finished.
* </para></listitem>
* <listitem><para>
* GstBaseVideoEncoder calls @stop at end of all processing.
* </para></listitem>
* </itemizedlist>
* </listitem>
* <listitem>
* <itemizedlist>
* <title>Data processing</title>
* <listitem><para>
* Base class collects input data and metadata into a frame and hands
* this to subclass' @handle_frame.
* </para></listitem>
* <listitem><para>
* If codec processing results in encoded data, subclass should call
* @gst_base_video_encoder_finish_frame to have encoded data pushed
* downstream.
* </para></listitem>
* <listitem><para>
* If implemented, baseclass calls subclass @shape_output which then sends
* data downstream in desired form. Otherwise, it is sent as-is.
* </para></listitem>
* <listitem><para>
* GstBaseVideoEncoderClass will handle both srcpad and sinkpad events.
* Sink events will be passed to subclass if @event callback has been
* provided.
* </para></listitem>
* </itemizedlist>
* </listitem>
* <listitem>
* <itemizedlist><title>Shutdown phase</title>
* <listitem><para>
* GstBaseVideoEncoder class calls @stop to inform the subclass that data
* parsing will be stopped.
* </para></listitem>
* </itemizedlist>
* </listitem>
* </orderedlist>
*
* Subclass is responsible for providing pad template caps for
* source and sink pads. The pads need to be named "sink" and "src". It should
* also be able to provide fixed src pad caps in @getcaps by the time it calls
* @gst_base_video_encoder_finish_frame.
*
* Things that subclass need to take care of:
* <itemizedlist>
* <listitem><para>Provide pad templates</para></listitem>
* <listitem><para>
* Provide source pad caps in @get_caps.
* </para></listitem>
* <listitem><para>
* Accept data in @handle_frame and provide encoded results to
* @gst_base_video_encoder_finish_frame.
* </para></listitem>
* </itemizedlist>
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstbasevideoencoder.h"
GST_DEBUG_CATEGORY (basevideoencoder_debug);
#define GST_CAT_DEFAULT basevideoencoder_debug
static void gst_base_video_encoder_finalize (GObject * object);
static gboolean gst_base_video_encoder_sink_setcaps (GstPad * pad,
GstCaps * caps);
static gboolean gst_base_video_encoder_src_event (GstPad * pad,
GstEvent * event);
static gboolean gst_base_video_encoder_sink_event (GstPad * pad,
GstEvent * event);
static GstFlowReturn gst_base_video_encoder_chain (GstPad * pad,
GstBuffer * buf);
static GstStateChangeReturn gst_base_video_encoder_change_state (GstElement *
element, GstStateChange transition);
static const GstQueryType *gst_base_video_encoder_get_query_types (GstPad *
pad);
static gboolean gst_base_video_encoder_src_query (GstPad * pad,
GstQuery * query);
static void
_do_init (GType object_type)
{
const GInterfaceInfo preset_interface_info = {
NULL, /* interface_init */
NULL, /* interface_finalize */
NULL /* interface_data */
};
g_type_add_interface_static (object_type, GST_TYPE_PRESET,
&preset_interface_info);
}
GST_BOILERPLATE_FULL (GstBaseVideoEncoder, gst_base_video_encoder,
GstBaseVideoCodec, GST_TYPE_BASE_VIDEO_CODEC, _do_init);
static void
gst_base_video_encoder_base_init (gpointer g_class)
{
GST_DEBUG_CATEGORY_INIT (basevideoencoder_debug, "basevideoencoder", 0,
"Base Video Encoder");
}
static void
gst_base_video_encoder_class_init (GstBaseVideoEncoderClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
gobject_class = G_OBJECT_CLASS (klass);
gstelement_class = GST_ELEMENT_CLASS (klass);
gobject_class->finalize = gst_base_video_encoder_finalize;
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_base_video_encoder_change_state);
parent_class = g_type_class_peek_parent (klass);
}
static void
gst_base_video_encoder_reset (GstBaseVideoEncoder * base_video_encoder)
{
base_video_encoder->presentation_frame_number = 0;
base_video_encoder->distance_from_sync = 0;
base_video_encoder->force_keyframe = FALSE;
base_video_encoder->set_output_caps = FALSE;
base_video_encoder->drained = TRUE;
base_video_encoder->min_latency = 0;
base_video_encoder->max_latency = 0;
if (base_video_encoder->force_keyunit_event) {
gst_event_unref (base_video_encoder->force_keyunit_event);
base_video_encoder->force_keyunit_event = NULL;
}
}
static void
gst_base_video_encoder_init (GstBaseVideoEncoder * base_video_encoder,
GstBaseVideoEncoderClass * klass)
{
GstPad *pad;
GST_DEBUG_OBJECT (base_video_encoder, "gst_base_video_encoder_init");
pad = GST_BASE_VIDEO_CODEC_SINK_PAD (base_video_encoder);
gst_pad_set_chain_function (pad,
GST_DEBUG_FUNCPTR (gst_base_video_encoder_chain));
gst_pad_set_event_function (pad,
GST_DEBUG_FUNCPTR (gst_base_video_encoder_sink_event));
gst_pad_set_setcaps_function (pad,
GST_DEBUG_FUNCPTR (gst_base_video_encoder_sink_setcaps));
pad = GST_BASE_VIDEO_CODEC_SRC_PAD (base_video_encoder);
gst_pad_set_query_type_function (pad,
GST_DEBUG_FUNCPTR (gst_base_video_encoder_get_query_types));
gst_pad_set_query_function (pad,
GST_DEBUG_FUNCPTR (gst_base_video_encoder_src_query));
gst_pad_set_event_function (pad,
GST_DEBUG_FUNCPTR (gst_base_video_encoder_src_event));
base_video_encoder->a.at_eos = FALSE;
/* encoder is expected to do so */
base_video_encoder->sink_clipping = TRUE;
}
static gboolean
gst_base_video_encoder_drain (GstBaseVideoEncoder * enc)
{
GstBaseVideoCodec *codec;
GstBaseVideoEncoderClass *enc_class;
gboolean ret = TRUE;
codec = GST_BASE_VIDEO_CODEC (enc);
enc_class = GST_BASE_VIDEO_ENCODER_GET_CLASS (enc);
GST_DEBUG_OBJECT (enc, "draining");
if (enc->drained) {
GST_DEBUG_OBJECT (enc, "already drained");
return TRUE;
}
if (enc_class->finish) {
GST_DEBUG_OBJECT (enc, "requesting subclass to finish");
ret = enc_class->finish (enc);
}
/* everything should be away now */
if (codec->frames) {
/* not fatal/impossible though if subclass/codec eats stuff */
GST_WARNING_OBJECT (enc, "still %d frames left after draining",
g_list_length (codec->frames));
#if 0
/* FIXME should do this, but subclass may come up with it later on ?
* and would then need refcounting or so on frames */
g_list_foreach (codec->frames,
(GFunc) gst_base_video_codec_free_frame, NULL);
#endif
}
return ret;
}
static gboolean
gst_base_video_encoder_sink_setcaps (GstPad * pad, GstCaps * caps)
{
GstBaseVideoEncoder *base_video_encoder;
GstBaseVideoEncoderClass *base_video_encoder_class;
GstStructure *structure;
GstVideoState *state;
gboolean ret;
gboolean changed = FALSE, u, v;
GstVideoFormat fmt;
gint w, h, num, den;
base_video_encoder = GST_BASE_VIDEO_ENCODER (gst_pad_get_parent (pad));
base_video_encoder_class =
GST_BASE_VIDEO_ENCODER_GET_CLASS (base_video_encoder);
/* subclass should do something here ... */
g_return_val_if_fail (base_video_encoder_class->set_format != NULL, FALSE);
GST_DEBUG_OBJECT (base_video_encoder, "setcaps %" GST_PTR_FORMAT, caps);
state = &GST_BASE_VIDEO_CODEC (base_video_encoder)->state;
structure = gst_caps_get_structure (caps, 0);
ret = gst_video_format_parse_caps (caps, &fmt, &w, &h);
if (!ret)
goto exit;
if (fmt != state->format || w != state->width || h != state->height) {
changed = TRUE;
state->format = fmt;
state->width = w;
state->height = h;
}
num = 0;
den = 1;
gst_video_parse_caps_framerate (caps, &num, &den);
if (den == 0) {
num = 0;
den = 1;
}
if (num != state->fps_n || den != state->fps_d) {
changed = TRUE;
state->fps_n = num;
state->fps_d = den;
}
num = 0;
den = 1;
gst_video_parse_caps_pixel_aspect_ratio (caps, &num, &den);
if (den == 0) {
num = 0;
den = 1;
}
if (num != state->par_n || den != state->par_d) {
changed = TRUE;
state->par_n = num;
state->par_d = den;
}
u = gst_structure_get_boolean (structure, "interlaced", &v);
if (u != state->have_interlaced || v != state->interlaced) {
changed = TRUE;
state->have_interlaced = u;
state->interlaced = v;
}
state->bytes_per_picture =
gst_video_format_get_size (state->format, state->width, state->height);
state->clean_width = state->width;
state->clean_height = state->height;
state->clean_offset_left = 0;
state->clean_offset_top = 0;
if (changed) {
/* arrange draining pending frames */
gst_base_video_encoder_drain (base_video_encoder);
/* and subclass should be ready to configure format at any time around */
if (base_video_encoder_class->set_format)
ret = base_video_encoder_class->set_format (base_video_encoder, state);
} else {
/* no need to stir things up */
GST_DEBUG_OBJECT (base_video_encoder,
"new video format identical to configured format");
ret = TRUE;
}
exit:
g_object_unref (base_video_encoder);
if (!ret) {
GST_WARNING_OBJECT (base_video_encoder, "rejected caps %" GST_PTR_FORMAT,
caps);
}
return ret;
}
static void
gst_base_video_encoder_finalize (GObject * object)
{
GST_DEBUG_OBJECT (object, "finalize");
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static gboolean
gst_base_video_encoder_sink_eventfunc (GstBaseVideoEncoder * base_video_encoder,
GstEvent * event)
{
gboolean ret = FALSE;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_EOS:
{
base_video_encoder->a.at_eos = TRUE;
gst_base_video_encoder_drain (base_video_encoder);
break;
}
case GST_EVENT_NEWSEGMENT:
{
gboolean update;
double rate;
double applied_rate;
GstFormat format;
gint64 start;
gint64 stop;
gint64 position;
gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
&format, &start, &stop, &position);
GST_DEBUG_OBJECT (base_video_encoder, "newseg rate %g, applied rate %g, "
"format %d, start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
", pos = %" GST_TIME_FORMAT, rate, applied_rate, format,
GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
GST_TIME_ARGS (position));
if (format != GST_FORMAT_TIME) {
GST_DEBUG_OBJECT (base_video_encoder, "received non TIME newsegment");
break;
}
base_video_encoder->a.at_eos = FALSE;
gst_segment_set_newsegment_full (&GST_BASE_VIDEO_CODEC
(base_video_encoder)->segment, update, rate, applied_rate, format,
start, stop, position);
break;
}
case GST_EVENT_CUSTOM_DOWNSTREAM:
{
const GstStructure *s;
s = gst_event_get_structure (event);
if (gst_structure_has_name (s, "GstForceKeyUnit")) {
GST_OBJECT_LOCK (base_video_encoder);
base_video_encoder->force_keyframe = TRUE;
if (base_video_encoder->force_keyunit_event)
gst_event_unref (base_video_encoder->force_keyunit_event);
base_video_encoder->force_keyunit_event = gst_event_copy (event);
GST_OBJECT_UNLOCK (base_video_encoder);
gst_event_unref (event);
ret = TRUE;
}
break;
}
default:
break;
}
return ret;
}
static gboolean
gst_base_video_encoder_sink_event (GstPad * pad, GstEvent * event)
{
GstBaseVideoEncoder *enc;
GstBaseVideoEncoderClass *klass;
gboolean handled = FALSE;
gboolean ret = TRUE;
enc = GST_BASE_VIDEO_ENCODER (gst_pad_get_parent (pad));
klass = GST_BASE_VIDEO_ENCODER_GET_CLASS (enc);
GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
GST_EVENT_TYPE_NAME (event));
if (klass->event)
handled = klass->event (enc, event);
if (!handled)
handled = gst_base_video_encoder_sink_eventfunc (enc, event);
if (!handled)
ret = gst_pad_event_default (pad, event);
GST_DEBUG_OBJECT (enc, "event handled");
gst_object_unref (enc);
return ret;
}
static gboolean
gst_base_video_encoder_src_event (GstPad * pad, GstEvent * event)
{
GstBaseVideoEncoder *base_video_encoder;
gboolean ret = FALSE;
base_video_encoder = GST_BASE_VIDEO_ENCODER (gst_pad_get_parent (pad));
GST_LOG_OBJECT (base_video_encoder, "handling event: %" GST_PTR_FORMAT,
event);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_CUSTOM_UPSTREAM:
{
const GstStructure *s;
s = gst_event_get_structure (event);
if (gst_structure_has_name (s, "GstForceKeyUnit")) {
GST_OBJECT_LOCK (base_video_encoder);
base_video_encoder->force_keyframe = TRUE;
GST_OBJECT_UNLOCK (base_video_encoder);
gst_event_unref (event);
ret = TRUE;
} else {
ret =
gst_pad_push_event (GST_BASE_VIDEO_CODEC_SINK_PAD
(base_video_encoder), event);
}
break;
}
default:
ret =
gst_pad_push_event (GST_BASE_VIDEO_CODEC_SINK_PAD
(base_video_encoder), event);
break;
}
gst_object_unref (base_video_encoder);
return ret;
}
static const GstQueryType *
gst_base_video_encoder_get_query_types (GstPad * pad)
{
static const GstQueryType query_types[] = {
GST_QUERY_CONVERT,
GST_QUERY_LATENCY,
0
};
return query_types;
}
static gboolean
gst_base_video_encoder_src_query (GstPad * pad, GstQuery * query)
{
GstBaseVideoEncoder *enc;
gboolean res;
GstPad *peerpad;
enc = GST_BASE_VIDEO_ENCODER (gst_pad_get_parent (pad));
peerpad = gst_pad_get_peer (GST_BASE_VIDEO_CODEC_SINK_PAD (enc));
GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONVERT:
{
GstBaseVideoCodec *codec = GST_BASE_VIDEO_CODEC (enc);
GstFormat src_fmt, dest_fmt;
gint64 src_val, dest_val;
gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
res = gst_base_video_encoded_video_convert (&codec->state,
codec->bytes, codec->time, src_fmt, src_val, &dest_fmt, &dest_val);
if (!res)
goto error;
gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
break;
}
case GST_QUERY_LATENCY:
{
gboolean live;
GstClockTime min_latency, max_latency;
res = gst_pad_query (peerpad, query);
if (res) {
gst_query_parse_latency (query, &live, &min_latency, &max_latency);
GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
GST_OBJECT_LOCK (enc);
min_latency += enc->min_latency;
if (max_latency != GST_CLOCK_TIME_NONE) {
max_latency += enc->max_latency;
}
GST_OBJECT_UNLOCK (enc);
gst_query_set_latency (query, live, min_latency, max_latency);
}
}
break;
default:
res = gst_pad_query_default (pad, query);
}
gst_object_unref (peerpad);
gst_object_unref (enc);
return res;
error:
GST_DEBUG_OBJECT (enc, "query failed");
gst_object_unref (peerpad);
gst_object_unref (enc);
return res;
}
static GstFlowReturn
gst_base_video_encoder_chain (GstPad * pad, GstBuffer * buf)
{
GstBaseVideoEncoder *base_video_encoder;
GstBaseVideoEncoderClass *klass;
GstVideoFrame *frame;
GstFlowReturn ret = GST_FLOW_OK;
base_video_encoder = GST_BASE_VIDEO_ENCODER (gst_pad_get_parent (pad));
klass = GST_BASE_VIDEO_ENCODER_GET_CLASS (base_video_encoder);
g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
if (!GST_PAD_CAPS (pad)) {
return GST_FLOW_NOT_NEGOTIATED;
}
GST_LOG_OBJECT (base_video_encoder,
"received buffer of size %d with ts %" GST_TIME_FORMAT
", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buf),
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
if (base_video_encoder->a.at_eos) {
return GST_FLOW_UNEXPECTED;
}
if (base_video_encoder->sink_clipping) {
gint64 start = GST_BUFFER_TIMESTAMP (buf);
gint64 stop = start + GST_BUFFER_DURATION (buf);
gint64 clip_start;
gint64 clip_stop;
if (!gst_segment_clip (&GST_BASE_VIDEO_CODEC (base_video_encoder)->segment,
GST_FORMAT_TIME, start, stop, &clip_start, &clip_stop)) {
GST_DEBUG_OBJECT (base_video_encoder,
"clipping to segment dropped frame");
goto done;
}
}
if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))) {
GST_LOG_OBJECT (base_video_encoder, "marked discont");
GST_BASE_VIDEO_CODEC (base_video_encoder)->discont = TRUE;
}
frame =
gst_base_video_codec_new_frame (GST_BASE_VIDEO_CODEC
(base_video_encoder));
frame->sink_buffer = buf;
frame->presentation_timestamp = GST_BUFFER_TIMESTAMP (buf);
frame->presentation_duration = GST_BUFFER_DURATION (buf);
frame->presentation_frame_number =
base_video_encoder->presentation_frame_number;
base_video_encoder->presentation_frame_number++;
frame->force_keyframe = base_video_encoder->force_keyframe;
base_video_encoder->force_keyframe = FALSE;
GST_BASE_VIDEO_CODEC (base_video_encoder)->frames =
g_list_append (GST_BASE_VIDEO_CODEC (base_video_encoder)->frames, frame);
/* new data, more finish needed */
base_video_encoder->drained = FALSE;
GST_LOG_OBJECT (base_video_encoder, "passing frame pfn %d to subclass",
frame->presentation_frame_number);
ret = klass->handle_frame (base_video_encoder, frame);
done:
g_object_unref (base_video_encoder);
return ret;
}
static GstStateChangeReturn
gst_base_video_encoder_change_state (GstElement * element,
GstStateChange transition)
{
GstBaseVideoEncoder *base_video_encoder;
GstBaseVideoEncoderClass *base_video_encoder_class;
GstStateChangeReturn ret;
base_video_encoder = GST_BASE_VIDEO_ENCODER (element);
base_video_encoder_class = GST_BASE_VIDEO_ENCODER_GET_CLASS (element);
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_base_video_encoder_reset (base_video_encoder);
gst_base_video_encoder_reset (base_video_encoder);
if (base_video_encoder_class->start) {
base_video_encoder_class->start (base_video_encoder);
}
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:
gst_base_video_encoder_reset (base_video_encoder);
if (base_video_encoder_class->stop) {
base_video_encoder_class->stop (base_video_encoder);
}
break;
default:
break;
}
return ret;
}
/**
* gst_base_video_encoder_finish_frame:
* @base_video_encoder: a #GstBaseVideoEncoder
* @frame: an encoded #GstVideoFrame
*
* @frame must have a valid encoded data buffer, whose metadata fields
* are then appropriately set according to frame data.
* It is subsequently pushed downstream or provided to @shape_output.
* In any case, the frame is considered finished and released.
*
* Returns: a #GstFlowReturn resulting from sending data downstream
*/
GstFlowReturn
gst_base_video_encoder_finish_frame (GstBaseVideoEncoder * base_video_encoder,
GstVideoFrame * frame)
{
GstFlowReturn ret;
GstBaseVideoEncoderClass *base_video_encoder_class;
g_return_val_if_fail (frame->src_buffer != NULL, GST_FLOW_ERROR);
base_video_encoder_class =
GST_BASE_VIDEO_ENCODER_GET_CLASS (base_video_encoder);
GST_LOG_OBJECT (base_video_encoder,
"finish frame fpn %d", frame->presentation_frame_number);
if (frame->is_sync_point) {
GST_LOG_OBJECT (base_video_encoder, "key frame");
base_video_encoder->distance_from_sync = 0;
GST_BUFFER_FLAG_UNSET (frame->src_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
} else {
GST_BUFFER_FLAG_SET (frame->src_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
}
frame->distance_from_sync = base_video_encoder->distance_from_sync;
base_video_encoder->distance_from_sync++;
frame->decode_frame_number = frame->system_frame_number - 1;
if (frame->decode_frame_number < 0) {
frame->decode_timestamp = 0;
} else {
frame->decode_timestamp = gst_util_uint64_scale (frame->decode_frame_number,
GST_SECOND * GST_BASE_VIDEO_CODEC (base_video_encoder)->state.fps_d,
GST_BASE_VIDEO_CODEC (base_video_encoder)->state.fps_n);
}
GST_BUFFER_TIMESTAMP (frame->src_buffer) = frame->presentation_timestamp;
GST_BUFFER_DURATION (frame->src_buffer) = frame->presentation_duration;
GST_BUFFER_OFFSET (frame->src_buffer) = frame->decode_timestamp;
/* update rate estimate */
GST_BASE_VIDEO_CODEC (base_video_encoder)->bytes +=
GST_BUFFER_SIZE (frame->src_buffer);
if (GST_CLOCK_TIME_IS_VALID (frame->presentation_duration)) {
GST_BASE_VIDEO_CODEC (base_video_encoder)->time +=
frame->presentation_duration;
} else {
/* better none than nothing valid */
GST_BASE_VIDEO_CODEC (base_video_encoder)->time = GST_CLOCK_TIME_NONE;
}
if (G_UNLIKELY (GST_BASE_VIDEO_CODEC (base_video_encoder)->discont)) {
GST_LOG_OBJECT (base_video_encoder, "marking discont");
GST_BUFFER_FLAG_SET (frame->src_buffer, GST_BUFFER_FLAG_DISCONT);
GST_BASE_VIDEO_CODEC (base_video_encoder)->discont = FALSE;
}
GST_BASE_VIDEO_CODEC (base_video_encoder)->frames =
g_list_remove (GST_BASE_VIDEO_CODEC (base_video_encoder)->frames, frame);
/* FIXME get rid of this ?
* seems a roundabout way that adds little benefit to simply get
* and subsequently set. subclass is adult enough to set_caps itself ...
* so simply check/ensure/assert that src pad caps are set by now */
if (!base_video_encoder->set_output_caps) {
GstCaps *caps;
if (base_video_encoder_class->get_caps) {
caps = base_video_encoder_class->get_caps (base_video_encoder);
} else {
caps = gst_caps_new_simple ("video/unknown", NULL);
}
GST_DEBUG_OBJECT (base_video_encoder, "src caps %" GST_PTR_FORMAT, caps);
gst_pad_set_caps (GST_BASE_VIDEO_CODEC_SRC_PAD (base_video_encoder), caps);
gst_caps_unref (caps);
base_video_encoder->set_output_caps = TRUE;
}
gst_buffer_set_caps (GST_BUFFER (frame->src_buffer),
GST_PAD_CAPS (GST_BASE_VIDEO_CODEC_SRC_PAD (base_video_encoder)));
if (frame->force_keyframe) {
GstClockTime stream_time;
GstClockTime running_time;
GstEvent *ev;
running_time =
gst_segment_to_running_time (&GST_BASE_VIDEO_CODEC
(base_video_encoder)->segment, GST_FORMAT_TIME,
frame->presentation_timestamp);
stream_time =
gst_segment_to_stream_time (&GST_BASE_VIDEO_CODEC
(base_video_encoder)->segment, GST_FORMAT_TIME,
frame->presentation_timestamp);
/* re-use upstream event if any so it also conveys any additional
* info upstream arranged in there */
GST_OBJECT_LOCK (base_video_encoder);
if (base_video_encoder->force_keyunit_event) {
ev = base_video_encoder->force_keyunit_event;
base_video_encoder->force_keyunit_event = NULL;
} else {
ev = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
gst_structure_new ("GstForceKeyUnit", NULL));
}
GST_OBJECT_UNLOCK (base_video_encoder);
gst_structure_set (ev->structure,
"timestamp", G_TYPE_UINT64, frame->presentation_timestamp,
"stream-time", G_TYPE_UINT64, stream_time,
"running-time", G_TYPE_UINT64, running_time, NULL);
gst_pad_push_event (GST_BASE_VIDEO_CODEC_SRC_PAD (base_video_encoder), ev);
}
if (base_video_encoder_class->shape_output) {
ret = base_video_encoder_class->shape_output (base_video_encoder, frame);
} else {
ret =
gst_pad_push (GST_BASE_VIDEO_CODEC_SRC_PAD (base_video_encoder),
frame->src_buffer);
}
/* handed out */
frame->src_buffer = NULL;
gst_base_video_codec_free_frame (frame);
return ret;
}
/**
* gst_base_video_encoder_get_state:
* @base_video_encoder: a #GstBaseVideoEncoder
*
* Returns: #GstVideoState describing format of video data.
*/
const GstVideoState *
gst_base_video_encoder_get_state (GstBaseVideoEncoder * base_video_encoder)
{
return &GST_BASE_VIDEO_CODEC (base_video_encoder)->state;
}
/**
* gst_base_video_encoder_set_latency:
* @base_video_encoder: a #GstBaseVideoEncoder
* @min_latency: minimum latency
* @max_latency: maximum latency
*
* Informs baseclass of encoding latency.
*/
void
gst_base_video_encoder_set_latency (GstBaseVideoEncoder * base_video_encoder,
GstClockTime min_latency, GstClockTime max_latency)
{
g_return_if_fail (min_latency >= 0);
g_return_if_fail (max_latency >= min_latency);
GST_OBJECT_LOCK (base_video_encoder);
base_video_encoder->min_latency = min_latency;
base_video_encoder->max_latency = max_latency;
GST_OBJECT_UNLOCK (base_video_encoder);
gst_element_post_message (GST_ELEMENT_CAST (base_video_encoder),
gst_message_new_latency (GST_OBJECT_CAST (base_video_encoder)));
}
/**
* gst_base_video_encoder_set_latency_fields:
* @base_video_encoder: a #GstBaseVideoEncoder
* @fields: latency in fields
*
* Informs baseclass of encoding latency in terms of fields (both min
* and max latency).
*/
void
gst_base_video_encoder_set_latency_fields (GstBaseVideoEncoder *
base_video_encoder, int n_fields)
{
gint64 latency;
latency = gst_util_uint64_scale (n_fields,
GST_BASE_VIDEO_CODEC (base_video_encoder)->state.fps_d * GST_SECOND,
2 * GST_BASE_VIDEO_CODEC (base_video_encoder)->state.fps_n);
gst_base_video_encoder_set_latency (base_video_encoder, latency, latency);
}
/**
* gst_base_video_encoder_get_oldest_frame:
* @base_video_encoder: a #GstBaseVideoEncoder
*
* Returns: oldest unfinished pending #GstVideoFrame
*/
GstVideoFrame *
gst_base_video_encoder_get_oldest_frame (GstBaseVideoEncoder *
base_video_encoder)
{
GList *g;
g = g_list_first (GST_BASE_VIDEO_CODEC (base_video_encoder)->frames);
if (g == NULL)
return NULL;
return (GstVideoFrame *) (g->data);
}
/* FIXME there could probably be more of these;
* get by presentation_number, by presentation_time ? */

182
omx/gstbasevideoencoder.h Normal file
View file

@ -0,0 +1,182 @@
/* GStreamer
* Copyright (C) 2008 David Schleef <ds@schleef.org>
* Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
* Copyright (C) 2011 Nokia Corporation. All rights reserved.
* Contact: Stefan Kost <stefan.kost@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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.
*/
#ifndef _GST_BASE_VIDEO_ENCODER_H_
#define _GST_BASE_VIDEO_ENCODER_H_
#ifndef GST_USE_UNSTABLE_API
#warning "GstBaseVideoEncoder is unstable API and may change in future."
#warning "You can define GST_USE_UNSTABLE_API to avoid this warning."
#endif
#include "gstbasevideocodec.h"
G_BEGIN_DECLS
#define GST_TYPE_BASE_VIDEO_ENCODER \
(gst_base_video_encoder_get_type())
#define GST_BASE_VIDEO_ENCODER(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BASE_VIDEO_ENCODER,GstBaseVideoEncoder))
#define GST_BASE_VIDEO_ENCODER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BASE_VIDEO_ENCODER,GstBaseVideoEncoderClass))
#define GST_BASE_VIDEO_ENCODER_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_BASE_VIDEO_ENCODER,GstBaseVideoEncoderClass))
#define GST_IS_BASE_VIDEO_ENCODER(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BASE_VIDEO_ENCODER))
#define GST_IS_BASE_VIDEO_ENCODER_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BASE_VIDEO_ENCODER))
/**
* GST_BASE_VIDEO_ENCODER_SINK_NAME:
*
* The name of the templates for the sink pad.
*/
#define GST_BASE_VIDEO_ENCODER_SINK_NAME "sink"
/**
* GST_BASE_VIDEO_ENCODER_SRC_NAME:
*
* The name of the templates for the source pad.
*/
#define GST_BASE_VIDEO_ENCODER_SRC_NAME "src"
typedef struct _GstBaseVideoEncoder GstBaseVideoEncoder;
typedef struct _GstBaseVideoEncoderClass GstBaseVideoEncoderClass;
/**
* GstBaseVideoEncoder:
* @element: the parent element.
*
* The opaque #GstBaseVideoEncoder data structure.
*/
struct _GstBaseVideoEncoder
{
GstBaseVideoCodec base_video_codec;
/*< protected >*/
gboolean sink_clipping;
guint64 presentation_frame_number;
int distance_from_sync;
gboolean force_keyframe;
/*< private >*/
/* FIXME move to real private part ?
* (and introduce a context ?) */
gboolean set_output_caps;
gboolean drained;
gint64 min_latency;
gint64 max_latency;
GstEvent *force_keyunit_event;
union {
void *padding;
gboolean at_eos;
} a;
/* FIXME before moving to base */
void *padding[GST_PADDING_LARGE-1];
};
/**
* GstBaseVideoEncoderClass:
* @start: Optional.
* Called when the element starts processing.
* Allows opening external resources.
* @stop: Optional.
* Called when the element stops processing.
* Allows closing external resources.
* @set_format: Optional.
* Notifies subclass of incoming data format.
* GstVideoState fields have already been
* set according to provided caps.
* @handle_frame: Provides input frame to subclass.
* @finish: Optional.
* Called to request subclass to dispatch any pending remaining
* data (e.g. at EOS).
* @shape_output: Optional.
* Allows subclass to push frame downstream in whatever
* shape or form it deems appropriate. If not provided,
* provided encoded frame data is simply pushed downstream.
* @event: Optional.
* Event handler on the sink pad. This function should return
* TRUE if the event was handled and should be discarded
* (i.e. not unref'ed).
* @getcaps: Optional, but recommended.
* Provides src pad caps to baseclass.
*
* Subclasses can override any of the available virtual methods or not, as
* needed. At minimum @handle_frame needs to be overridden, and @set_format
* and @get_caps are likely needed as well.
*/
struct _GstBaseVideoEncoderClass
{
GstBaseVideoCodecClass base_video_codec_class;
/*< public >*/
/* virtual methods for subclasses */
gboolean (*start) (GstBaseVideoEncoder *coder);
gboolean (*stop) (GstBaseVideoEncoder *coder);
gboolean (*set_format) (GstBaseVideoEncoder *coder,
GstVideoState *state);
GstFlowReturn (*handle_frame) (GstBaseVideoEncoder *coder,
GstVideoFrame *frame);
gboolean (*finish) (GstBaseVideoEncoder *coder);
GstFlowReturn (*shape_output) (GstBaseVideoEncoder *coder,
GstVideoFrame *frame);
gboolean (*event) (GstBaseVideoEncoder *coder,
GstEvent *event);
GstCaps * (*get_caps) (GstBaseVideoEncoder *coder);
/*< private >*/
/* FIXME before moving to base */
gpointer _gst_reserved[GST_PADDING_LARGE];
};
GType gst_base_video_encoder_get_type (void);
const GstVideoState* gst_base_video_encoder_get_state (GstBaseVideoEncoder *coder);
GstVideoFrame* gst_base_video_encoder_get_oldest_frame (GstBaseVideoEncoder *coder);
GstFlowReturn gst_base_video_encoder_finish_frame (GstBaseVideoEncoder *base_video_encoder,
GstVideoFrame *frame);
void gst_base_video_encoder_set_latency (GstBaseVideoEncoder *base_video_encoder,
GstClockTime min_latency, GstClockTime max_latency);
void gst_base_video_encoder_set_latency_fields (GstBaseVideoEncoder *base_video_encoder,
int n_fields);
G_END_DECLS
#endif

159
omx/gstbasevideoutils.c Normal file
View file

@ -0,0 +1,159 @@
/* GStreamer
* Copyright (C) 2008 David Schleef <ds@schleef.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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
#include "gstbasevideocodec.h"
#include <string.h>
GST_DEBUG_CATEGORY_EXTERN (basevideocodec_debug);
#define GST_CAT_DEFAULT basevideocodec_debug
gboolean
gst_base_video_rawvideo_convert (GstVideoState * state,
GstFormat src_format, gint64 src_value,
GstFormat * dest_format, gint64 * dest_value)
{
gboolean res = FALSE;
g_return_val_if_fail (dest_format != NULL, FALSE);
g_return_val_if_fail (dest_value != NULL, FALSE);
if (src_format == *dest_format || src_value == 0 || src_value == -1) {
*dest_value = src_value;
return TRUE;
}
if (src_format == GST_FORMAT_BYTES &&
*dest_format == GST_FORMAT_DEFAULT && state->bytes_per_picture != 0) {
/* convert bytes to frames */
*dest_value = gst_util_uint64_scale_int (src_value, 1,
state->bytes_per_picture);
res = TRUE;
} else if (src_format == GST_FORMAT_DEFAULT &&
*dest_format == GST_FORMAT_BYTES && state->bytes_per_picture != 0) {
/* convert bytes to frames */
*dest_value = src_value * state->bytes_per_picture;
res = TRUE;
} else if (src_format == GST_FORMAT_DEFAULT &&
*dest_format == GST_FORMAT_TIME && state->fps_n != 0) {
/* convert frames to time */
/* FIXME add segment time? */
*dest_value = gst_util_uint64_scale (src_value,
GST_SECOND * state->fps_d, state->fps_n);
res = TRUE;
} else if (src_format == GST_FORMAT_TIME &&
*dest_format == GST_FORMAT_DEFAULT && state->fps_d != 0) {
/* convert time to frames */
/* FIXME subtract segment time? */
*dest_value = gst_util_uint64_scale (src_value, state->fps_n,
GST_SECOND * state->fps_d);
res = TRUE;
} else if (src_format == GST_FORMAT_TIME &&
*dest_format == GST_FORMAT_BYTES && state->fps_d != 0 &&
state->bytes_per_picture != 0) {
/* convert time to frames */
/* FIXME subtract segment time? */
*dest_value = gst_util_uint64_scale (src_value,
state->fps_n * state->bytes_per_picture, GST_SECOND * state->fps_d);
res = TRUE;
} else if (src_format == GST_FORMAT_BYTES &&
*dest_format == GST_FORMAT_TIME && state->fps_n != 0 &&
state->bytes_per_picture != 0) {
/* convert frames to time */
/* FIXME add segment time? */
*dest_value = gst_util_uint64_scale (src_value,
GST_SECOND * state->fps_d, state->fps_n * state->bytes_per_picture);
res = TRUE;
}
return res;
}
gboolean
gst_base_video_encoded_video_convert (GstVideoState * state,
gint64 bytes, gint64 time, GstFormat src_format,
gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
{
gboolean res = FALSE;
g_return_val_if_fail (dest_format != NULL, FALSE);
g_return_val_if_fail (dest_value != NULL, FALSE);
if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
src_value == -1)) {
if (dest_value)
*dest_value = src_value;
return TRUE;
}
if (bytes <= 0 || time <= 0) {
GST_DEBUG ("not enough metadata yet to convert");
goto exit;
}
switch (src_format) {
case GST_FORMAT_BYTES:
switch (*dest_format) {
case GST_FORMAT_TIME:
*dest_value = gst_util_uint64_scale (src_value, time, bytes);
res = TRUE;
break;
default:
res = FALSE;
}
break;
case GST_FORMAT_TIME:
switch (*dest_format) {
case GST_FORMAT_BYTES:
*dest_value = gst_util_uint64_scale (src_value, bytes, time);
res = TRUE;
break;
default:
res = FALSE;
}
break;
default:
GST_DEBUG ("unhandled conversion from %d to %d", src_format,
*dest_format);
res = FALSE;
}
exit:
return res;
}
GstClockTime
gst_video_state_get_timestamp (const GstVideoState * state,
GstSegment * segment, int frame_number)
{
if (frame_number < 0) {
return segment->start -
(gint64) gst_util_uint64_scale (-frame_number,
state->fps_d * GST_SECOND, state->fps_n);
} else {
return segment->start +
gst_util_uint64_scale (frame_number,
state->fps_d * GST_SECOND, state->fps_n);
}
}