2009-07-01 17:55:12 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2008 David Schleef <ds@schleef.org>
|
2011-03-25 08:28:24 +00:00
|
|
|
* 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>
|
2009-07-01 17:55:12 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-03-25 08:28:24 +00:00
|
|
|
/**
|
|
|
|
* 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>
|
2011-08-12 10:06:23 +00:00
|
|
|
* Provide source pad caps before pushing the first buffer
|
2011-03-25 08:28:24 +00:00
|
|
|
* </para></listitem>
|
|
|
|
* <listitem><para>
|
|
|
|
* Accept data in @handle_frame and provide encoded results to
|
|
|
|
* @gst_base_video_encoder_finish_frame.
|
|
|
|
* </para></listitem>
|
|
|
|
* </itemizedlist>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstbasevideoencoder.h"
|
2011-07-09 09:40:17 +00:00
|
|
|
#include "gstbasevideoutils.h"
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-07-19 10:50:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-06-02 23:01:28 +00:00
|
|
|
GST_DEBUG_CATEGORY (basevideoencoder_debug);
|
|
|
|
#define GST_CAT_DEFAULT basevideoencoder_debug
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
static void gst_base_video_encoder_finalize (GObject * object);
|
|
|
|
|
|
|
|
static gboolean gst_base_video_encoder_sink_setcaps (GstPad * pad,
|
|
|
|
GstCaps * caps);
|
2011-08-12 10:25:03 +00:00
|
|
|
static GstCaps *gst_base_video_encoder_sink_getcaps (GstPad * pad);
|
2010-09-20 02:33:40 +00:00
|
|
|
static gboolean gst_base_video_encoder_src_event (GstPad * pad,
|
|
|
|
GstEvent * event);
|
2009-07-01 17:55:12 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
2011-03-25 14:29:34 +00:00
|
|
|
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);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_base_video_encoder_base_init (gpointer g_class)
|
|
|
|
{
|
2010-06-02 23:01:28 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (basevideoencoder_debug, "basevideoencoder", 0,
|
|
|
|
"Base Video Encoder");
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
gstelement_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_base_video_encoder_change_state);
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2011-03-23 07:35:51 +00:00
|
|
|
static void
|
|
|
|
gst_base_video_encoder_reset (GstBaseVideoEncoder * base_video_encoder)
|
|
|
|
{
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_encoder);
|
|
|
|
|
2011-03-24 07:17:52 +00:00
|
|
|
base_video_encoder->presentation_frame_number = 0;
|
|
|
|
base_video_encoder->distance_from_sync = 0;
|
|
|
|
base_video_encoder->force_keyframe = FALSE;
|
|
|
|
|
2011-03-23 21:17:49 +00:00
|
|
|
base_video_encoder->drained = TRUE;
|
2011-03-24 07:17:52 +00:00
|
|
|
base_video_encoder->min_latency = 0;
|
|
|
|
base_video_encoder->max_latency = 0;
|
|
|
|
|
2011-03-23 07:35:51 +00:00
|
|
|
if (base_video_encoder->force_keyunit_event) {
|
|
|
|
gst_event_unref (base_video_encoder->force_keyunit_event);
|
|
|
|
base_video_encoder->force_keyunit_event = NULL;
|
|
|
|
}
|
2011-07-20 07:25:28 +00:00
|
|
|
|
|
|
|
g_list_foreach (base_video_encoder->current_frame_events,
|
|
|
|
(GFunc) gst_event_unref, NULL);
|
|
|
|
g_list_free (base_video_encoder->current_frame_events);
|
|
|
|
base_video_encoder->current_frame_events = NULL;
|
2011-08-18 08:02:50 +00:00
|
|
|
|
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_encoder);
|
2011-03-23 07:35:51 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
static void
|
|
|
|
gst_base_video_encoder_init (GstBaseVideoEncoder * base_video_encoder,
|
|
|
|
GstBaseVideoEncoderClass * klass)
|
|
|
|
{
|
|
|
|
GstPad *pad;
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_DEBUG_OBJECT (base_video_encoder, "gst_base_video_encoder_init");
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
pad = GST_BASE_VIDEO_CODEC_SINK_PAD (base_video_encoder);
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
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));
|
2011-08-12 10:25:03 +00:00
|
|
|
gst_pad_set_getcaps_function (pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_base_video_encoder_sink_getcaps));
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
pad = GST_BASE_VIDEO_CODEC_SRC_PAD (base_video_encoder);
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
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));
|
2011-04-24 23:00:00 +00:00
|
|
|
|
|
|
|
base_video_encoder->a.at_eos = FALSE;
|
2011-03-24 07:21:03 +00:00
|
|
|
|
|
|
|
/* encoder is expected to do so */
|
|
|
|
base_video_encoder->sink_clipping = TRUE;
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2011-03-23 21:17:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-20 07:09:25 +00:00
|
|
|
if (enc_class->reset) {
|
2011-03-23 21:17:49 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "requesting subclass to finish");
|
2011-07-20 07:09:25 +00:00
|
|
|
ret = enc_class->reset (enc);
|
2011-03-23 21:17:49 +00:00
|
|
|
}
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
static gboolean
|
|
|
|
gst_base_video_encoder_sink_setcaps (GstPad * pad, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstBaseVideoEncoder *base_video_encoder;
|
|
|
|
GstBaseVideoEncoderClass *base_video_encoder_class;
|
2010-06-03 00:54:42 +00:00
|
|
|
GstStructure *structure;
|
2011-07-19 10:50:43 +00:00
|
|
|
GstVideoState *state, tmp_state;
|
2010-06-01 22:54:51 +00:00
|
|
|
gboolean ret;
|
2011-07-19 10:50:43 +00:00
|
|
|
gboolean changed = FALSE;
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-03-24 07:23:27 +00:00
|
|
|
/* subclass should do something here ... */
|
|
|
|
g_return_val_if_fail (base_video_encoder_class->set_format != NULL, FALSE);
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_DEBUG_OBJECT (base_video_encoder, "setcaps %" GST_PTR_FORMAT, caps);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_encoder);
|
|
|
|
|
2010-10-10 00:36:07 +00:00
|
|
|
state = &GST_BASE_VIDEO_CODEC (base_video_encoder)->state;
|
2011-07-19 10:50:43 +00:00
|
|
|
memset (&tmp_state, 0, sizeof (tmp_state));
|
|
|
|
|
|
|
|
tmp_state.caps = gst_caps_ref (caps);
|
2010-06-03 00:54:42 +00:00
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
|
2011-07-19 10:50:43 +00:00
|
|
|
ret =
|
|
|
|
gst_video_format_parse_caps (caps, &tmp_state.format, &tmp_state.width,
|
|
|
|
&tmp_state.height);
|
2011-03-24 07:23:27 +00:00
|
|
|
if (!ret)
|
|
|
|
goto exit;
|
2010-06-03 00:54:42 +00:00
|
|
|
|
2011-07-19 10:50:43 +00:00
|
|
|
changed = (tmp_state.format != state->format
|
|
|
|
|| tmp_state.width != state->width || tmp_state.height != state->height);
|
2010-06-03 00:54:42 +00:00
|
|
|
|
2011-07-19 10:50:43 +00:00
|
|
|
if (!gst_video_parse_caps_framerate (caps, &tmp_state.fps_n,
|
|
|
|
&tmp_state.fps_d)) {
|
|
|
|
tmp_state.fps_n = 0;
|
|
|
|
tmp_state.fps_d = 1;
|
2011-03-24 07:23:27 +00:00
|
|
|
}
|
2011-07-19 10:50:43 +00:00
|
|
|
changed = changed || (tmp_state.fps_n != state->fps_n
|
|
|
|
|| tmp_state.fps_d != state->fps_d);
|
2010-06-03 00:54:42 +00:00
|
|
|
|
2011-07-19 10:50:43 +00:00
|
|
|
if (!gst_video_parse_caps_pixel_aspect_ratio (caps, &tmp_state.par_n,
|
|
|
|
&tmp_state.par_d)) {
|
|
|
|
tmp_state.par_n = 1;
|
|
|
|
tmp_state.par_d = 1;
|
2011-03-24 07:23:27 +00:00
|
|
|
}
|
2011-07-19 10:50:43 +00:00
|
|
|
changed = changed || (tmp_state.par_n != state->par_n
|
|
|
|
|| tmp_state.par_d != state->par_d);
|
|
|
|
|
|
|
|
tmp_state.have_interlaced =
|
|
|
|
gst_structure_get_boolean (structure, "interlaced",
|
|
|
|
&tmp_state.interlaced);
|
|
|
|
changed = changed || (tmp_state.have_interlaced != state->have_interlaced
|
|
|
|
|| tmp_state.interlaced != state->interlaced);
|
|
|
|
|
|
|
|
tmp_state.bytes_per_picture =
|
|
|
|
gst_video_format_get_size (tmp_state.format, tmp_state.width,
|
|
|
|
tmp_state.height);
|
|
|
|
tmp_state.clean_width = tmp_state.width;
|
|
|
|
tmp_state.clean_height = tmp_state.height;
|
|
|
|
tmp_state.clean_offset_left = 0;
|
|
|
|
tmp_state.clean_offset_top = 0;
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-03-24 07:23:27 +00:00
|
|
|
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)
|
2011-07-19 10:50:43 +00:00
|
|
|
ret =
|
|
|
|
base_video_encoder_class->set_format (base_video_encoder, &tmp_state);
|
|
|
|
if (ret) {
|
|
|
|
gst_caps_replace (&state->caps, NULL);
|
|
|
|
*state = tmp_state;
|
|
|
|
}
|
2011-03-24 07:23:27 +00:00
|
|
|
} else {
|
|
|
|
/* no need to stir things up */
|
|
|
|
GST_DEBUG_OBJECT (base_video_encoder,
|
|
|
|
"new video format identical to configured format");
|
2011-07-19 10:50:43 +00:00
|
|
|
gst_caps_unref (tmp_state.caps);
|
2011-03-24 07:23:27 +00:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
2009-10-05 23:16:34 +00:00
|
|
|
|
2011-03-24 07:23:27 +00:00
|
|
|
exit:
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_encoder);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
if (!ret) {
|
|
|
|
GST_WARNING_OBJECT (base_video_encoder, "rejected caps %" GST_PTR_FORMAT,
|
|
|
|
caps);
|
|
|
|
}
|
|
|
|
|
2011-08-18 08:02:50 +00:00
|
|
|
g_object_unref (base_video_encoder);
|
|
|
|
|
2010-06-01 22:54:51 +00:00
|
|
|
return ret;
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2011-08-12 10:25:03 +00:00
|
|
|
static GstCaps *
|
|
|
|
gst_base_video_encoder_sink_getcaps (GstPad * pad)
|
|
|
|
{
|
|
|
|
GstBaseVideoEncoder *base_video_encoder;
|
|
|
|
const GstCaps *templ_caps;
|
|
|
|
GstCaps *allowed;
|
|
|
|
GstCaps *fcaps, *filter_caps;
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
base_video_encoder = GST_BASE_VIDEO_ENCODER (gst_pad_get_parent (pad));
|
|
|
|
|
|
|
|
/* FIXME: Allow subclass to override this? */
|
|
|
|
|
|
|
|
/* Allow downstream to specify width/height/framerate/PAR constraints
|
|
|
|
* and forward them upstream for video converters to handle
|
|
|
|
*/
|
|
|
|
templ_caps =
|
|
|
|
gst_pad_get_pad_template_caps (GST_BASE_VIDEO_CODEC_SINK_PAD
|
|
|
|
(base_video_encoder));
|
|
|
|
allowed =
|
|
|
|
gst_pad_get_allowed_caps (GST_BASE_VIDEO_CODEC_SRC_PAD
|
|
|
|
(base_video_encoder));
|
|
|
|
if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
|
|
|
|
fcaps = gst_caps_copy (templ_caps);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (base_video_encoder, "template caps %" GST_PTR_FORMAT,
|
|
|
|
templ_caps);
|
|
|
|
GST_LOG_OBJECT (base_video_encoder, "allowed caps %" GST_PTR_FORMAT, allowed);
|
|
|
|
|
|
|
|
filter_caps = gst_caps_new_empty ();
|
|
|
|
|
|
|
|
for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
|
|
|
|
GQuark q_name =
|
|
|
|
gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
|
|
|
|
|
|
|
|
for (j = 0; j < gst_caps_get_size (allowed); j++) {
|
|
|
|
const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
|
|
|
|
const GValue *val;
|
|
|
|
GstStructure *s;
|
|
|
|
|
|
|
|
s = gst_structure_id_empty_new (q_name);
|
|
|
|
if ((val = gst_structure_get_value (allowed_s, "width")))
|
|
|
|
gst_structure_set_value (s, "width", val);
|
|
|
|
if ((val = gst_structure_get_value (allowed_s, "height")))
|
|
|
|
gst_structure_set_value (s, "height", val);
|
|
|
|
if ((val = gst_structure_get_value (allowed_s, "framerate")))
|
|
|
|
gst_structure_set_value (s, "framerate", val);
|
|
|
|
if ((val = gst_structure_get_value (allowed_s, "pixel-aspect-ratio")))
|
|
|
|
gst_structure_set_value (s, "pixel-aspect-ratio", val);
|
|
|
|
|
|
|
|
gst_caps_merge_structure (filter_caps, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fcaps = gst_caps_intersect (filter_caps, templ_caps);
|
|
|
|
gst_caps_unref (filter_caps);
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
gst_caps_replace (&allowed, NULL);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (base_video_encoder, "Returning caps %" GST_PTR_FORMAT, fcaps);
|
|
|
|
|
2011-08-25 18:56:58 +00:00
|
|
|
g_object_unref (base_video_encoder);
|
2011-08-12 10:25:03 +00:00
|
|
|
return fcaps;
|
|
|
|
}
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
static void
|
|
|
|
gst_base_video_encoder_finalize (GObject * object)
|
|
|
|
{
|
2011-04-14 15:49:18 +00:00
|
|
|
GST_DEBUG_OBJECT (object, "finalize");
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-03-23 07:49:48 +00:00
|
|
|
gst_base_video_encoder_sink_eventfunc (GstBaseVideoEncoder * base_video_encoder,
|
|
|
|
GstEvent * event)
|
2009-07-01 17:55:12 +00:00
|
|
|
{
|
2011-07-20 07:09:25 +00:00
|
|
|
GstBaseVideoEncoderClass *base_video_encoder_class;
|
2009-07-01 17:55:12 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
2011-07-20 07:09:25 +00:00
|
|
|
base_video_encoder_class =
|
|
|
|
GST_BASE_VIDEO_ENCODER_GET_CLASS (base_video_encoder);
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_EOS:
|
|
|
|
{
|
2011-07-20 07:09:25 +00:00
|
|
|
GstFlowReturn flow_ret;
|
|
|
|
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_encoder);
|
2011-04-24 23:00:00 +00:00
|
|
|
base_video_encoder->a.at_eos = TRUE;
|
2011-07-20 07:09:25 +00:00
|
|
|
|
|
|
|
if (base_video_encoder_class->finish) {
|
|
|
|
flow_ret = base_video_encoder_class->finish (base_video_encoder);
|
|
|
|
} else {
|
|
|
|
flow_ret = GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (flow_ret == GST_BASE_VIDEO_ENCODER_FLOW_DROPPED);
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_encoder);
|
2009-07-01 17:55:12 +00:00
|
|
|
break;
|
2011-03-23 07:49:48 +00:00
|
|
|
}
|
2009-07-01 17:55:12 +00:00
|
|
|
case GST_EVENT_NEWSEGMENT:
|
|
|
|
{
|
|
|
|
gboolean update;
|
|
|
|
double rate;
|
|
|
|
double applied_rate;
|
|
|
|
GstFormat format;
|
|
|
|
gint64 start;
|
|
|
|
gint64 stop;
|
|
|
|
gint64 position;
|
|
|
|
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_encoder);
|
2011-03-24 13:16:12 +00:00
|
|
|
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));
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-03-23 07:49:48 +00:00
|
|
|
if (format != GST_FORMAT_TIME) {
|
|
|
|
GST_DEBUG_OBJECT (base_video_encoder, "received non TIME newsegment");
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_encoder);
|
2011-03-23 07:49:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-04-24 23:00:00 +00:00
|
|
|
base_video_encoder->a.at_eos = FALSE;
|
2011-03-24 13:16:12 +00:00
|
|
|
|
2010-10-10 00:36:07 +00:00
|
|
|
gst_segment_set_newsegment_full (&GST_BASE_VIDEO_CODEC
|
|
|
|
(base_video_encoder)->segment, update, rate, applied_rate, format,
|
|
|
|
start, stop, position);
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_encoder);
|
2009-07-01 17:55:12 +00:00
|
|
|
break;
|
2011-03-23 07:49:48 +00:00
|
|
|
}
|
2010-09-20 02:33:40 +00:00
|
|
|
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;
|
2011-03-23 07:35:51 +00:00
|
|
|
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);
|
2010-09-20 02:33:40 +00:00
|
|
|
GST_OBJECT_UNLOCK (base_video_encoder);
|
|
|
|
gst_event_unref (event);
|
2011-03-23 07:49:48 +00:00
|
|
|
ret = TRUE;
|
2010-09-20 02:33:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-07-01 17:55:12 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2011-03-23 07:49:48 +00:00
|
|
|
}
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-03-23 07:49:48 +00:00
|
|
|
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);
|
|
|
|
|
2011-07-20 07:25:28 +00:00
|
|
|
if (!handled) {
|
|
|
|
/* Forward non-serialized events and EOS/FLUSH_STOP immediately.
|
|
|
|
* For EOS this is required because no buffer or serialized event
|
|
|
|
* will come after EOS and nothing could trigger another
|
|
|
|
* _finish_frame() call. *
|
|
|
|
* If the subclass handles sending of EOS manually it can return
|
|
|
|
* _DROPPED from ::finish() and all other subclasses should have
|
|
|
|
* decoded/flushed all remaining data before this
|
|
|
|
*
|
|
|
|
* For FLUSH_STOP this is required because it is expected
|
|
|
|
* to be forwarded immediately and no buffers are queued anyway.
|
|
|
|
*/
|
|
|
|
if (!GST_EVENT_IS_SERIALIZED (event)
|
|
|
|
|| GST_EVENT_TYPE (event) == GST_EVENT_EOS
|
|
|
|
|| GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
|
|
|
|
ret = gst_pad_push_event (enc->base_video_codec.srcpad, event);
|
|
|
|
} else {
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (enc);
|
2011-07-20 07:25:28 +00:00
|
|
|
enc->current_frame_events =
|
|
|
|
g_list_prepend (enc->current_frame_events, event);
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (enc);
|
2011-07-20 07:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-23 07:49:48 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (enc, "event handled");
|
|
|
|
|
|
|
|
gst_object_unref (enc);
|
|
|
|
return ret;
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-20 02:33:40 +00:00
|
|
|
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));
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_LOG_OBJECT (base_video_encoder, "handling event: %" GST_PTR_FORMAT,
|
|
|
|
event);
|
|
|
|
|
2010-09-20 02:33:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
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));
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
2009-07-01 17:55:12 +00:00
|
|
|
case GST_QUERY_CONVERT:
|
|
|
|
{
|
2011-03-28 09:15:49 +00:00
|
|
|
GstBaseVideoCodec *codec = GST_BASE_VIDEO_CODEC (enc);
|
2009-07-01 17:55:12 +00:00
|
|
|
GstFormat src_fmt, dest_fmt;
|
|
|
|
gint64 src_val, dest_val;
|
|
|
|
|
|
|
|
gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
|
2011-03-28 09:15:49 +00:00
|
|
|
res = gst_base_video_encoded_video_convert (&codec->state,
|
|
|
|
codec->bytes, codec->time, src_fmt, src_val, &dest_fmt, &dest_val);
|
2009-07-01 17:55:12 +00:00
|
|
|
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);
|
2011-03-24 13:16:12 +00:00
|
|
|
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));
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_OBJECT_LOCK (enc);
|
2009-07-01 17:55:12 +00:00
|
|
|
min_latency += enc->min_latency;
|
|
|
|
if (max_latency != GST_CLOCK_TIME_NONE) {
|
|
|
|
max_latency += enc->max_latency;
|
|
|
|
}
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_OBJECT_UNLOCK (enc);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
gst_query_set_latency (query, live, min_latency, max_latency);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = gst_pad_query_default (pad, query);
|
2011-03-24 13:16:12 +00:00
|
|
|
}
|
2009-07-01 17:55:12 +00:00
|
|
|
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;
|
2011-10-04 12:27:32 +00:00
|
|
|
GstVideoFrameState *frame;
|
2011-03-24 13:10:07 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
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);
|
|
|
|
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_encoder);
|
|
|
|
|
2011-03-21 16:44:17 +00:00
|
|
|
if (!GST_PAD_CAPS (pad)) {
|
2011-08-18 08:02:50 +00:00
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
goto done;
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
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)));
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-04-24 23:00:00 +00:00
|
|
|
if (base_video_encoder->a.at_eos) {
|
2011-08-18 08:02:50 +00:00
|
|
|
ret = GST_FLOW_UNEXPECTED;
|
|
|
|
goto done;
|
2011-04-24 23:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
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;
|
|
|
|
|
2010-10-10 00:36:07 +00:00
|
|
|
if (!gst_segment_clip (&GST_BASE_VIDEO_CODEC (base_video_encoder)->segment,
|
2009-07-01 17:55:12 +00:00
|
|
|
GST_FORMAT_TIME, start, stop, &clip_start, &clip_stop)) {
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_DEBUG_OBJECT (base_video_encoder,
|
|
|
|
"clipping to segment dropped frame");
|
2009-07-01 17:55:12 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-27 14:36:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-06-01 22:54:51 +00:00
|
|
|
frame =
|
|
|
|
gst_base_video_codec_new_frame (GST_BASE_VIDEO_CODEC
|
|
|
|
(base_video_encoder));
|
2011-07-20 07:25:28 +00:00
|
|
|
frame->events = base_video_encoder->current_frame_events;
|
|
|
|
base_video_encoder->current_frame_events = NULL;
|
2009-07-01 17:55:12 +00:00
|
|
|
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++;
|
2011-03-23 07:35:51 +00:00
|
|
|
frame->force_keyframe = base_video_encoder->force_keyframe;
|
|
|
|
base_video_encoder->force_keyframe = FALSE;
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2010-10-10 00:36:07 +00:00
|
|
|
GST_BASE_VIDEO_CODEC (base_video_encoder)->frames =
|
|
|
|
g_list_append (GST_BASE_VIDEO_CODEC (base_video_encoder)->frames, frame);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-03-24 07:23:27 +00:00
|
|
|
/* new data, more finish needed */
|
|
|
|
base_video_encoder->drained = FALSE;
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_LOG_OBJECT (base_video_encoder, "passing frame pfn %d to subclass",
|
|
|
|
frame->presentation_frame_number);
|
|
|
|
|
2011-03-24 13:10:07 +00:00
|
|
|
ret = klass->handle_frame (base_video_encoder, frame);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
done:
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_encoder);
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
g_object_unref (base_video_encoder);
|
|
|
|
|
2011-03-24 13:10:07 +00:00
|
|
|
return ret;
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2011-03-23 07:35:51 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
gst_base_video_encoder_reset (base_video_encoder);
|
2011-03-24 08:21:04 +00:00
|
|
|
gst_base_video_encoder_reset (base_video_encoder);
|
|
|
|
if (base_video_encoder_class->start) {
|
|
|
|
base_video_encoder_class->start (base_video_encoder);
|
|
|
|
}
|
2011-03-23 07:35:51 +00:00
|
|
|
break;
|
2009-07-01 17:55:12 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2011-03-23 07:35:51 +00:00
|
|
|
gst_base_video_encoder_reset (base_video_encoder);
|
2009-10-05 23:16:34 +00:00
|
|
|
if (base_video_encoder_class->stop) {
|
|
|
|
base_video_encoder_class->stop (base_video_encoder);
|
|
|
|
}
|
2009-07-01 17:55:12 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-25 08:28:24 +00:00
|
|
|
/**
|
|
|
|
* gst_base_video_encoder_finish_frame:
|
|
|
|
* @base_video_encoder: a #GstBaseVideoEncoder
|
2011-10-04 12:27:32 +00:00
|
|
|
* @frame: an encoded #GstVideoFrameState
|
2011-03-25 08:28:24 +00:00
|
|
|
*
|
|
|
|
* @frame must have a valid encoded data buffer, whose metadata fields
|
2011-07-21 05:44:10 +00:00
|
|
|
* are then appropriately set according to frame data or no buffer at
|
|
|
|
* all if the frame should be dropped.
|
2011-03-25 08:28:24 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2009-07-01 17:55:12 +00:00
|
|
|
GstFlowReturn
|
|
|
|
gst_base_video_encoder_finish_frame (GstBaseVideoEncoder * base_video_encoder,
|
2011-10-04 12:27:32 +00:00
|
|
|
GstVideoFrameState * frame)
|
2009-07-01 17:55:12 +00:00
|
|
|
{
|
2011-07-21 05:44:10 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2009-07-01 17:55:12 +00:00
|
|
|
GstBaseVideoEncoderClass *base_video_encoder_class;
|
2011-07-20 07:25:28 +00:00
|
|
|
GList *l;
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
base_video_encoder_class =
|
|
|
|
GST_BASE_VIDEO_ENCODER_GET_CLASS (base_video_encoder);
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_LOG_OBJECT (base_video_encoder,
|
|
|
|
"finish frame fpn %d", frame->presentation_frame_number);
|
|
|
|
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_encoder);
|
|
|
|
|
2011-07-20 07:25:28 +00:00
|
|
|
/* Push all pending events that arrived before this frame */
|
|
|
|
for (l = base_video_encoder->base_video_codec.frames; l; l = l->next) {
|
2011-10-04 12:27:32 +00:00
|
|
|
GstVideoFrameState *tmp = l->data;
|
2011-07-20 07:25:28 +00:00
|
|
|
|
|
|
|
if (tmp->events) {
|
|
|
|
GList *k;
|
|
|
|
|
|
|
|
for (k = g_list_last (tmp->events); k; k = k->prev)
|
|
|
|
gst_pad_push_event (GST_BASE_VIDEO_CODEC_SRC_PAD (base_video_encoder),
|
|
|
|
k->data);
|
|
|
|
g_list_free (tmp->events);
|
|
|
|
tmp->events = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp == frame)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-20 02:33:40 +00:00
|
|
|
if (frame->force_keyframe) {
|
|
|
|
GstClockTime stream_time;
|
|
|
|
GstClockTime running_time;
|
2011-03-23 07:35:51 +00:00
|
|
|
GstEvent *ev;
|
2010-09-20 02:33:40 +00:00
|
|
|
|
2010-10-10 00:36:07 +00:00
|
|
|
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);
|
2010-09-20 02:33:40 +00:00
|
|
|
|
2011-03-23 07:35:51 +00:00
|
|
|
/* 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,
|
2010-09-20 02:33:40 +00:00
|
|
|
"timestamp", G_TYPE_UINT64, frame->presentation_timestamp,
|
|
|
|
"stream-time", G_TYPE_UINT64, stream_time,
|
|
|
|
"running-time", G_TYPE_UINT64, running_time, NULL);
|
|
|
|
|
2011-03-23 07:35:51 +00:00
|
|
|
gst_pad_push_event (GST_BASE_VIDEO_CODEC_SRC_PAD (base_video_encoder), ev);
|
2010-09-20 02:33:40 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 05:44:10 +00:00
|
|
|
/* no buffer data means this frame is skipped/dropped */
|
|
|
|
if (!frame->src_buffer) {
|
|
|
|
GST_DEBUG_OBJECT (base_video_encoder, "skipping frame %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (frame->presentation_timestamp));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_buffer_set_caps (GST_BUFFER (frame->src_buffer),
|
|
|
|
GST_PAD_CAPS (GST_BASE_VIDEO_CODEC_SRC_PAD (base_video_encoder)));
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
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);
|
|
|
|
}
|
2011-07-21 05:44:10 +00:00
|
|
|
frame->src_buffer = NULL;
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-07-21 05:44:10 +00:00
|
|
|
done:
|
2011-03-29 13:41:55 +00:00
|
|
|
/* handed out */
|
2011-07-21 05:44:10 +00:00
|
|
|
GST_BASE_VIDEO_CODEC (base_video_encoder)->frames =
|
|
|
|
g_list_remove (GST_BASE_VIDEO_CODEC (base_video_encoder)->frames, frame);
|
|
|
|
|
2009-10-05 20:33:22 +00:00
|
|
|
gst_base_video_codec_free_frame (frame);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_encoder);
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-25 08:28:24 +00:00
|
|
|
/**
|
|
|
|
* gst_base_video_encoder_get_state:
|
|
|
|
* @base_video_encoder: a #GstBaseVideoEncoder
|
|
|
|
*
|
|
|
|
* Returns: #GstVideoState describing format of video data.
|
|
|
|
*/
|
2009-07-01 17:55:12 +00:00
|
|
|
const GstVideoState *
|
|
|
|
gst_base_video_encoder_get_state (GstBaseVideoEncoder * base_video_encoder)
|
|
|
|
{
|
2010-10-10 00:36:07 +00:00
|
|
|
return &GST_BASE_VIDEO_CODEC (base_video_encoder)->state;
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 08:28:24 +00:00
|
|
|
/**
|
|
|
|
* gst_base_video_encoder_set_latency:
|
|
|
|
* @base_video_encoder: a #GstBaseVideoEncoder
|
|
|
|
* @min_latency: minimum latency
|
|
|
|
* @max_latency: maximum latency
|
|
|
|
*
|
|
|
|
* Informs baseclass of encoding latency.
|
|
|
|
*/
|
2009-07-01 17:55:12 +00:00
|
|
|
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);
|
|
|
|
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_OBJECT_LOCK (base_video_encoder);
|
2009-07-01 17:55:12 +00:00
|
|
|
base_video_encoder->min_latency = min_latency;
|
|
|
|
base_video_encoder->max_latency = max_latency;
|
2011-03-24 13:16:12 +00:00
|
|
|
GST_OBJECT_UNLOCK (base_video_encoder);
|
2010-06-01 22:54:51 +00:00
|
|
|
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (base_video_encoder),
|
|
|
|
gst_message_new_latency (GST_OBJECT_CAST (base_video_encoder)));
|
2009-07-01 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 08:28:24 +00:00
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*/
|
2009-07-01 17:55:12 +00:00
|
|
|
void
|
|
|
|
gst_base_video_encoder_set_latency_fields (GstBaseVideoEncoder *
|
|
|
|
base_video_encoder, int n_fields)
|
|
|
|
{
|
|
|
|
gint64 latency;
|
|
|
|
|
2011-08-26 09:50:38 +00:00
|
|
|
/* 0 numerator is used for "don't know" */
|
|
|
|
if (GST_BASE_VIDEO_CODEC (base_video_encoder)->state.fps_n == 0)
|
|
|
|
return;
|
|
|
|
|
2009-07-01 17:55:12 +00:00
|
|
|
latency = gst_util_uint64_scale (n_fields,
|
2010-10-10 00:36:07 +00:00
|
|
|
GST_BASE_VIDEO_CODEC (base_video_encoder)->state.fps_d * GST_SECOND,
|
|
|
|
2 * GST_BASE_VIDEO_CODEC (base_video_encoder)->state.fps_n);
|
2009-07-01 17:55:12 +00:00
|
|
|
|
|
|
|
gst_base_video_encoder_set_latency (base_video_encoder, latency, latency);
|
|
|
|
|
|
|
|
}
|
2009-10-02 23:35:09 +00:00
|
|
|
|
2011-03-25 08:28:24 +00:00
|
|
|
/**
|
|
|
|
* gst_base_video_encoder_get_oldest_frame:
|
|
|
|
* @base_video_encoder: a #GstBaseVideoEncoder
|
|
|
|
*
|
2011-10-04 12:27:32 +00:00
|
|
|
* Returns: oldest unfinished pending #GstVideoFrameState
|
2011-03-25 08:28:24 +00:00
|
|
|
*/
|
2011-10-04 12:27:32 +00:00
|
|
|
GstVideoFrameState *
|
2010-06-01 22:54:51 +00:00
|
|
|
gst_base_video_encoder_get_oldest_frame (GstBaseVideoEncoder *
|
|
|
|
base_video_encoder)
|
2009-10-02 23:35:09 +00:00
|
|
|
{
|
|
|
|
GList *g;
|
|
|
|
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_LOCK (base_video_encoder);
|
2010-10-10 00:36:07 +00:00
|
|
|
g = g_list_first (GST_BASE_VIDEO_CODEC (base_video_encoder)->frames);
|
2011-08-18 08:02:50 +00:00
|
|
|
GST_BASE_VIDEO_CODEC_STREAM_UNLOCK (base_video_encoder);
|
2009-10-02 23:35:09 +00:00
|
|
|
|
2010-06-01 22:54:51 +00:00
|
|
|
if (g == NULL)
|
|
|
|
return NULL;
|
2011-10-04 12:27:32 +00:00
|
|
|
return (GstVideoFrameState *) (g->data);
|
2009-10-02 23:35:09 +00:00
|
|
|
}
|
2011-03-25 08:28:24 +00:00
|
|
|
|
|
|
|
/* FIXME there could probably be more of these;
|
|
|
|
* get by presentation_number, by presentation_time ? */
|