2012-03-07 09:18:49 +00:00
|
|
|
/* 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>
|
|
|
|
* Copyright (C) 2012 Collabora Ltd.
|
|
|
|
* Author : Edward Hervey <edward@collabora.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* 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
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2012-03-07 09:18:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gstvideoencoder
|
2017-01-23 19:36:11 +00:00
|
|
|
* @title: GstVideoEncoder
|
2012-03-07 09:18:49 +00:00
|
|
|
* @short_description: Base class for video encoders
|
|
|
|
*
|
|
|
|
* This base class is for video encoders turning raw video into
|
|
|
|
* encoded video data.
|
|
|
|
*
|
|
|
|
* GstVideoEncoder and subclass should cooperate as follows.
|
2017-01-23 19:36:11 +00:00
|
|
|
*
|
|
|
|
* ## Configuration
|
|
|
|
*
|
|
|
|
* * Initially, GstVideoEncoder calls @start when the encoder element
|
2012-03-07 09:18:49 +00:00
|
|
|
* is activated, which allows subclass to perform any global setup.
|
2017-01-23 19:36:11 +00:00
|
|
|
* * GstVideoEncoder calls @set_format to inform subclass of the format
|
2012-03-07 09:18:49 +00:00
|
|
|
* 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.
|
2017-01-23 19:36:11 +00:00
|
|
|
* * GstVideoEncoder calls @stop at end of all processing.
|
|
|
|
*
|
|
|
|
* ## Data processing
|
|
|
|
*
|
|
|
|
* * Base class collects input data and metadata into a frame and hands
|
2012-03-07 09:18:49 +00:00
|
|
|
* this to subclass' @handle_frame.
|
2017-01-23 19:36:11 +00:00
|
|
|
*
|
|
|
|
* * If codec processing results in encoded data, subclass should call
|
2012-03-07 09:18:49 +00:00
|
|
|
* @gst_video_encoder_finish_frame to have encoded data pushed
|
|
|
|
* downstream.
|
2017-01-23 19:36:11 +00:00
|
|
|
*
|
|
|
|
* * If implemented, baseclass calls subclass @pre_push just prior to
|
2012-03-07 09:18:49 +00:00
|
|
|
* pushing to allow subclasses to modify some metadata on the buffer.
|
|
|
|
* If it returns GST_FLOW_OK, the buffer is pushed downstream.
|
2017-01-23 19:36:11 +00:00
|
|
|
*
|
|
|
|
* * GstVideoEncoderClass will handle both srcpad and sinkpad events.
|
2012-03-07 09:18:49 +00:00
|
|
|
* Sink events will be passed to subclass if @event callback has been
|
|
|
|
* provided.
|
2017-01-23 19:36:11 +00:00
|
|
|
*
|
|
|
|
* ## Shutdown phase
|
|
|
|
*
|
|
|
|
* * GstVideoEncoder class calls @stop to inform the subclass that data
|
2012-03-07 09:18:49 +00:00
|
|
|
* parsing will be stopped.
|
|
|
|
*
|
|
|
|
* 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_video_encoder_finish_frame.
|
|
|
|
*
|
|
|
|
* Things that subclass need to take care of:
|
2017-01-23 19:36:11 +00:00
|
|
|
*
|
|
|
|
* * Provide pad templates
|
|
|
|
* * Provide source pad caps before pushing the first buffer
|
|
|
|
* * Accept data in @handle_frame and provide encoded results to
|
2012-03-07 09:18:49 +00:00
|
|
|
* @gst_video_encoder_finish_frame.
|
|
|
|
*
|
2017-10-25 09:41:02 +00:00
|
|
|
*
|
|
|
|
* The #GstVideoEncoder:qos property will enable the Quality-of-Service
|
|
|
|
* features of the encoder which gather statistics about the real-time
|
|
|
|
* performance of the downstream elements. If enabled, subclasses can
|
|
|
|
* use gst_video_encoder_get_max_encode_time() to check if input frames
|
|
|
|
* are already late and drop them right away to give a chance to the
|
|
|
|
* pipeline to catch up.
|
2012-03-07 09:18:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* TODO
|
|
|
|
*
|
|
|
|
* * Calculate actual latency based on input/output timestamp/frame_number
|
|
|
|
* and if it exceeds the recorded one, save it and emit a GST_MESSAGE_LATENCY
|
|
|
|
*/
|
|
|
|
|
2012-12-12 17:13:10 +00:00
|
|
|
#include <gst/video/video.h>
|
2012-03-07 09:18:49 +00:00
|
|
|
#include "gstvideoencoder.h"
|
|
|
|
#include "gstvideoutils.h"
|
2014-12-08 19:33:33 +00:00
|
|
|
#include "gstvideoutilsprivate.h"
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2012-04-24 20:42:59 +00:00
|
|
|
#include <gst/video/gstvideometa.h>
|
2012-08-08 13:28:52 +00:00
|
|
|
#include <gst/video/gstvideopool.h>
|
2012-04-24 20:42:59 +00:00
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY (videoencoder_debug);
|
|
|
|
#define GST_CAT_DEFAULT videoencoder_debug
|
|
|
|
|
2017-10-25 09:41:02 +00:00
|
|
|
/* properties */
|
|
|
|
|
|
|
|
#define DEFAULT_QOS FALSE
|
2020-06-03 18:46:38 +00:00
|
|
|
#define DEFAULT_MIN_FORCE_KEY_UNIT_INTERVAL 0
|
2017-10-25 09:41:02 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_QOS,
|
2020-06-03 18:46:38 +00:00
|
|
|
PROP_MIN_FORCE_KEY_UNIT_INTERVAL,
|
2017-10-25 09:41:02 +00:00
|
|
|
PROP_LAST
|
|
|
|
};
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
struct _GstVideoEncoderPrivate
|
|
|
|
{
|
|
|
|
guint64 presentation_frame_number;
|
|
|
|
int distance_from_sync;
|
|
|
|
|
|
|
|
/* FIXME : (and introduce a context ?) */
|
|
|
|
gboolean drained;
|
|
|
|
|
|
|
|
gint64 min_latency;
|
|
|
|
gint64 max_latency;
|
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
/* FIXME 2.0: Use a GQueue or similar, see GstVideoCodecFrame::events */
|
2012-03-07 09:18:49 +00:00
|
|
|
GList *current_frame_events;
|
|
|
|
|
|
|
|
GList *headers;
|
|
|
|
gboolean new_headers; /* Whether new headers were just set */
|
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
GQueue force_key_unit; /* List of pending forced keyunits */
|
2020-06-03 18:46:38 +00:00
|
|
|
GstClockTime min_force_key_unit_interval;
|
|
|
|
GstClockTime last_force_key_unit_request;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2012-07-26 16:36:53 +00:00
|
|
|
guint32 system_frame_number;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
GQueue frames; /* Protected with OBJECT_LOCK */
|
2012-03-07 09:18:49 +00:00
|
|
|
GstVideoCodecState *input_state;
|
|
|
|
GstVideoCodecState *output_state;
|
|
|
|
gboolean output_state_changed;
|
|
|
|
|
|
|
|
gint64 bytes;
|
|
|
|
gint64 time;
|
2012-07-23 07:07:02 +00:00
|
|
|
|
|
|
|
GstAllocator *allocator;
|
|
|
|
GstAllocationParams params;
|
2012-08-09 14:02:15 +00:00
|
|
|
|
2015-08-18 11:29:29 +00:00
|
|
|
/* upstream stream tags (global tags are passed through as-is) */
|
|
|
|
GstTagList *upstream_tags;
|
|
|
|
|
|
|
|
/* subclass tags */
|
2012-08-09 14:02:15 +00:00
|
|
|
GstTagList *tags;
|
2015-08-18 11:29:29 +00:00
|
|
|
GstTagMergeMode tags_merge_mode;
|
|
|
|
|
2012-08-09 14:02:15 +00:00
|
|
|
gboolean tags_changed;
|
2014-11-26 20:06:57 +00:00
|
|
|
|
|
|
|
GstClockTime min_pts;
|
2019-08-29 17:42:39 +00:00
|
|
|
/* adjustment needed on pts, dts, segment start and stop to accommodate
|
2014-11-26 20:06:57 +00:00
|
|
|
* min_pts */
|
|
|
|
GstClockTime time_adjustment;
|
2017-09-21 13:18:10 +00:00
|
|
|
|
|
|
|
/* QoS properties */
|
2017-10-25 09:41:02 +00:00
|
|
|
gint qos_enabled; /* ATOMIC */
|
2017-09-21 13:18:10 +00:00
|
|
|
gdouble proportion; /* OBJECT_LOCK */
|
|
|
|
GstClockTime earliest_time; /* OBJECT_LOCK */
|
|
|
|
GstClockTime qos_frame_duration; /* OBJECT_LOCK */
|
|
|
|
/* qos messages: frames dropped/processed */
|
|
|
|
guint dropped;
|
|
|
|
guint processed;
|
2012-03-07 09:18:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
|
|
|
|
struct _ForcedKeyUnitEvent
|
|
|
|
{
|
|
|
|
GstClockTime running_time;
|
|
|
|
gboolean pending; /* TRUE if this was requested already */
|
|
|
|
gboolean all_headers;
|
|
|
|
guint count;
|
2013-08-27 13:03:54 +00:00
|
|
|
guint32 frame_id;
|
2012-03-07 09:18:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
forced_key_unit_event_free (ForcedKeyUnitEvent * evt)
|
|
|
|
{
|
|
|
|
g_slice_free (ForcedKeyUnitEvent, evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ForcedKeyUnitEvent *
|
|
|
|
forced_key_unit_event_new (GstClockTime running_time, gboolean all_headers,
|
|
|
|
guint count)
|
|
|
|
{
|
|
|
|
ForcedKeyUnitEvent *evt = g_slice_new0 (ForcedKeyUnitEvent);
|
|
|
|
|
|
|
|
evt->running_time = running_time;
|
|
|
|
evt->all_headers = all_headers;
|
|
|
|
evt->count = count;
|
|
|
|
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:59:03 +00:00
|
|
|
static gint
|
|
|
|
forced_key_unit_event_compare (const ForcedKeyUnitEvent * a,
|
|
|
|
const ForcedKeyUnitEvent * b, gpointer user_data)
|
|
|
|
{
|
|
|
|
if (a->running_time == b->running_time)
|
|
|
|
return 0;
|
|
|
|
if (a->running_time == GST_CLOCK_TIME_NONE)
|
|
|
|
return -1;
|
|
|
|
if (b->running_time == GST_CLOCK_TIME_NONE)
|
|
|
|
return 1;
|
|
|
|
if (a->running_time < b->running_time)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
static GstElementClass *parent_class = NULL;
|
2018-06-23 19:33:16 +00:00
|
|
|
static gint private_offset = 0;
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
static void gst_video_encoder_class_init (GstVideoEncoderClass * klass);
|
|
|
|
static void gst_video_encoder_init (GstVideoEncoder * enc,
|
|
|
|
GstVideoEncoderClass * klass);
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
static void gst_video_encoder_finalize (GObject * object);
|
2016-01-19 22:22:35 +00:00
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
static gboolean gst_video_encoder_setcaps (GstVideoEncoder * enc,
|
|
|
|
GstCaps * caps);
|
|
|
|
static GstCaps *gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder,
|
|
|
|
GstCaps * filter);
|
|
|
|
static gboolean gst_video_encoder_src_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
static gboolean gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
static GstFlowReturn gst_video_encoder_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buf);
|
2012-03-07 09:18:49 +00:00
|
|
|
static GstStateChangeReturn gst_video_encoder_change_state (GstElement *
|
|
|
|
element, GstStateChange transition);
|
2012-04-24 17:35:24 +00:00
|
|
|
static gboolean gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
|
|
|
static gboolean gst_video_encoder_src_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
2012-03-07 09:18:49 +00:00
|
|
|
static GstVideoCodecFrame *gst_video_encoder_new_frame (GstVideoEncoder *
|
2012-06-27 14:45:41 +00:00
|
|
|
encoder, GstBuffer * buf, GstClockTime pts, GstClockTime dts,
|
|
|
|
GstClockTime duration);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2012-04-24 17:42:04 +00:00
|
|
|
static gboolean gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
|
|
|
|
GstEvent * event);
|
|
|
|
static gboolean gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
|
|
|
|
GstEvent * event);
|
2012-07-23 07:07:02 +00:00
|
|
|
static gboolean gst_video_encoder_decide_allocation_default (GstVideoEncoder *
|
|
|
|
encoder, GstQuery * query);
|
2012-04-24 20:42:59 +00:00
|
|
|
static gboolean gst_video_encoder_propose_allocation_default (GstVideoEncoder *
|
|
|
|
encoder, GstQuery * query);
|
2012-08-09 12:39:18 +00:00
|
|
|
static gboolean gst_video_encoder_negotiate_default (GstVideoEncoder * encoder);
|
2013-12-05 14:31:25 +00:00
|
|
|
static gboolean gst_video_encoder_negotiate_unlocked (GstVideoEncoder *
|
|
|
|
encoder);
|
2012-04-24 17:42:04 +00:00
|
|
|
|
2013-12-09 15:33:40 +00:00
|
|
|
static gboolean gst_video_encoder_sink_query_default (GstVideoEncoder * encoder,
|
|
|
|
GstQuery * query);
|
|
|
|
static gboolean gst_video_encoder_src_query_default (GstVideoEncoder * encoder,
|
|
|
|
GstQuery * query);
|
|
|
|
|
2015-06-29 11:59:25 +00:00
|
|
|
static gboolean gst_video_encoder_transform_meta_default (GstVideoEncoder *
|
|
|
|
encoder, GstVideoCodecFrame * frame, GstMeta * meta);
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
/* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
|
|
|
|
* method to get to the padtemplates */
|
|
|
|
GType
|
|
|
|
gst_video_encoder_get_type (void)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
2012-04-24 17:35:24 +00:00
|
|
|
static volatile gsize type = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&type)) {
|
|
|
|
GType _type;
|
|
|
|
static const GTypeInfo info = {
|
|
|
|
sizeof (GstVideoEncoderClass),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_video_encoder_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstVideoEncoder),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_video_encoder_init,
|
|
|
|
};
|
|
|
|
const GInterfaceInfo preset_interface_info = {
|
|
|
|
NULL, /* interface_init */
|
|
|
|
NULL, /* interface_finalize */
|
|
|
|
NULL /* interface_data */
|
|
|
|
};
|
|
|
|
|
|
|
|
_type = g_type_register_static (GST_TYPE_ELEMENT,
|
|
|
|
"GstVideoEncoder", &info, G_TYPE_FLAG_ABSTRACT);
|
2018-06-23 19:33:16 +00:00
|
|
|
private_offset =
|
|
|
|
g_type_add_instance_private (_type, sizeof (GstVideoEncoderPrivate));
|
2012-04-24 17:35:24 +00:00
|
|
|
g_type_add_interface_static (_type, GST_TYPE_PRESET,
|
|
|
|
&preset_interface_info);
|
|
|
|
g_once_init_leave (&type, _type);
|
|
|
|
}
|
|
|
|
return type;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 19:33:16 +00:00
|
|
|
static inline GstVideoEncoderPrivate *
|
|
|
|
gst_video_encoder_get_instance_private (GstVideoEncoder * self)
|
|
|
|
{
|
|
|
|
return (G_STRUCT_MEMBER_P (self, private_offset));
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:41:02 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstVideoEncoder *sink = GST_VIDEO_ENCODER (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_QOS:
|
|
|
|
gst_video_encoder_set_qos_enabled (sink, g_value_get_boolean (value));
|
|
|
|
break;
|
2020-06-03 18:46:38 +00:00
|
|
|
case PROP_MIN_FORCE_KEY_UNIT_INTERVAL:
|
|
|
|
gst_video_encoder_set_min_force_key_unit_interval (sink,
|
|
|
|
g_value_get_uint64 (value));
|
|
|
|
break;
|
2017-10-25 09:41:02 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_video_encoder_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstVideoEncoder *sink = GST_VIDEO_ENCODER (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_QOS:
|
|
|
|
g_value_set_boolean (value, gst_video_encoder_is_qos_enabled (sink));
|
|
|
|
break;
|
2020-06-03 18:46:38 +00:00
|
|
|
case PROP_MIN_FORCE_KEY_UNIT_INTERVAL:
|
|
|
|
g_value_set_uint64 (value,
|
|
|
|
gst_video_encoder_get_min_force_key_unit_interval (sink));
|
|
|
|
break;
|
2017-10-25 09:41:02 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_class_init (GstVideoEncoderClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
gstelement_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
|
|
|
|
"Base Video Encoder");
|
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
2018-06-23 19:33:16 +00:00
|
|
|
if (private_offset != 0)
|
|
|
|
g_type_class_adjust_private_offset (klass, &private_offset);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2017-10-25 09:41:02 +00:00
|
|
|
gobject_class->set_property = gst_video_encoder_set_property;
|
|
|
|
gobject_class->get_property = gst_video_encoder_get_property;
|
2012-03-07 09:18:49 +00:00
|
|
|
gobject_class->finalize = gst_video_encoder_finalize;
|
|
|
|
|
|
|
|
gstelement_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
|
2012-04-24 17:42:04 +00:00
|
|
|
|
|
|
|
klass->sink_event = gst_video_encoder_sink_event_default;
|
|
|
|
klass->src_event = gst_video_encoder_src_event_default;
|
2012-04-24 20:42:59 +00:00
|
|
|
klass->propose_allocation = gst_video_encoder_propose_allocation_default;
|
2012-07-23 07:07:02 +00:00
|
|
|
klass->decide_allocation = gst_video_encoder_decide_allocation_default;
|
2012-08-09 12:39:18 +00:00
|
|
|
klass->negotiate = gst_video_encoder_negotiate_default;
|
2013-12-09 15:33:40 +00:00
|
|
|
klass->sink_query = gst_video_encoder_sink_query_default;
|
|
|
|
klass->src_query = gst_video_encoder_src_query_default;
|
2015-06-29 11:59:25 +00:00
|
|
|
klass->transform_meta = gst_video_encoder_transform_meta_default;
|
2017-10-25 09:41:02 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_QOS,
|
|
|
|
g_param_spec_boolean ("qos", "Qos",
|
|
|
|
"Handle Quality-of-Service events from downstream", DEFAULT_QOS,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2020-06-03 18:46:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstVideoEncoder:min-force-key-unit-interval:
|
|
|
|
*
|
|
|
|
* Minimum interval between force-keyunit requests in nanoseconds. See
|
|
|
|
* gst_video_encoder_set_min_force_key_unit_interval() for more details.
|
|
|
|
*
|
|
|
|
* Since: 1.18
|
|
|
|
**/
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_MIN_FORCE_KEY_UNIT_INTERVAL,
|
|
|
|
g_param_spec_uint64 ("min-force-key-unit-interval",
|
|
|
|
"Minimum Force Keyunit Interval",
|
|
|
|
"Minimum interval between force-keyunit requests in nanoseconds", 0,
|
|
|
|
G_MAXUINT64, DEFAULT_MIN_FORCE_KEY_UNIT_INTERVAL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 21:45:10 +00:00
|
|
|
static GList *
|
|
|
|
_flush_events (GstPad * pad, GList * events)
|
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
|
|
|
|
for (tmp = events; tmp; tmp = tmp->next) {
|
2014-09-17 12:08:17 +00:00
|
|
|
if (GST_EVENT_TYPE (tmp->data) != GST_EVENT_EOS &&
|
|
|
|
GST_EVENT_TYPE (tmp->data) != GST_EVENT_SEGMENT &&
|
|
|
|
GST_EVENT_IS_STICKY (tmp->data)) {
|
2013-10-14 21:45:10 +00:00
|
|
|
gst_pad_store_sticky_event (pad, GST_EVENT_CAST (tmp->data));
|
|
|
|
}
|
2014-09-17 12:08:17 +00:00
|
|
|
gst_event_unref (tmp->data);
|
2013-10-14 21:45:10 +00:00
|
|
|
}
|
|
|
|
g_list_free (events);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 60, 0)
|
|
|
|
#define g_queue_clear_full queue_clear_full
|
|
|
|
static void
|
|
|
|
queue_clear_full (GQueue * queue, GDestroyNotify free_func)
|
|
|
|
{
|
|
|
|
gpointer data;
|
|
|
|
|
|
|
|
while ((data = g_queue_pop_head (queue)) != NULL)
|
|
|
|
free_func (data);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-25 08:42:56 +00:00
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_reset (GstVideoEncoder * encoder, gboolean hard)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
2013-07-25 08:42:56 +00:00
|
|
|
gboolean ret = TRUE;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
|
|
|
|
priv->presentation_frame_number = 0;
|
|
|
|
priv->distance_from_sync = 0;
|
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
g_queue_clear_full (&priv->force_key_unit,
|
|
|
|
(GDestroyNotify) forced_key_unit_event_free);
|
2020-06-03 18:46:38 +00:00
|
|
|
priv->last_force_key_unit_request = GST_CLOCK_TIME_NONE;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
priv->drained = TRUE;
|
|
|
|
|
2016-07-04 08:52:24 +00:00
|
|
|
GST_OBJECT_LOCK (encoder);
|
2012-03-07 09:18:49 +00:00
|
|
|
priv->bytes = 0;
|
|
|
|
priv->time = 0;
|
2016-07-04 08:52:24 +00:00
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2014-11-26 20:06:57 +00:00
|
|
|
priv->time_adjustment = GST_CLOCK_TIME_NONE;
|
|
|
|
|
2013-07-25 08:42:56 +00:00
|
|
|
if (hard) {
|
|
|
|
gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
|
|
|
|
gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
|
|
|
|
|
|
|
|
if (priv->input_state)
|
|
|
|
gst_video_codec_state_unref (priv->input_state);
|
|
|
|
priv->input_state = NULL;
|
|
|
|
if (priv->output_state)
|
|
|
|
gst_video_codec_state_unref (priv->output_state);
|
|
|
|
priv->output_state = NULL;
|
|
|
|
|
2015-08-18 11:29:29 +00:00
|
|
|
if (priv->upstream_tags) {
|
|
|
|
gst_tag_list_unref (priv->upstream_tags);
|
|
|
|
priv->upstream_tags = NULL;
|
|
|
|
}
|
2013-07-25 08:42:56 +00:00
|
|
|
if (priv->tags)
|
|
|
|
gst_tag_list_unref (priv->tags);
|
|
|
|
priv->tags = NULL;
|
2015-08-18 11:29:29 +00:00
|
|
|
priv->tags_merge_mode = GST_TAG_MERGE_APPEND;
|
2013-07-25 08:42:56 +00:00
|
|
|
priv->tags_changed = FALSE;
|
|
|
|
|
|
|
|
g_list_foreach (priv->headers, (GFunc) gst_event_unref, NULL);
|
|
|
|
g_list_free (priv->headers);
|
|
|
|
priv->headers = NULL;
|
|
|
|
priv->new_headers = FALSE;
|
2013-12-17 16:09:02 +00:00
|
|
|
|
|
|
|
if (priv->allocator) {
|
|
|
|
gst_object_unref (priv->allocator);
|
|
|
|
priv->allocator = NULL;
|
|
|
|
}
|
2013-10-14 21:45:10 +00:00
|
|
|
|
|
|
|
g_list_foreach (priv->current_frame_events, (GFunc) gst_event_unref, NULL);
|
|
|
|
g_list_free (priv->current_frame_events);
|
|
|
|
priv->current_frame_events = NULL;
|
|
|
|
|
2017-09-21 13:18:10 +00:00
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
priv->proportion = 0.5;
|
|
|
|
priv->earliest_time = GST_CLOCK_TIME_NONE;
|
|
|
|
priv->qos_frame_duration = 0;
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
|
|
|
priv->dropped = 0;
|
|
|
|
priv->processed = 0;
|
2013-10-14 21:45:10 +00:00
|
|
|
} else {
|
|
|
|
GList *l;
|
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
for (l = priv->frames.head; l; l = l->next) {
|
2013-10-14 21:45:10 +00:00
|
|
|
GstVideoCodecFrame *frame = l->data;
|
|
|
|
|
|
|
|
frame->events = _flush_events (encoder->srcpad, frame->events);
|
|
|
|
}
|
|
|
|
priv->current_frame_events = _flush_events (encoder->srcpad,
|
|
|
|
encoder->priv->current_frame_events);
|
2013-07-25 08:42:56 +00:00
|
|
|
}
|
2012-08-09 14:02:15 +00:00
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
g_queue_clear_full (&priv->frames,
|
|
|
|
(GDestroyNotify) gst_video_codec_frame_unref);
|
2015-03-27 05:39:43 +00:00
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
2013-07-25 08:42:56 +00:00
|
|
|
|
|
|
|
return ret;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 10:44:56 +00:00
|
|
|
/* Always call reset() in one way or another after this */
|
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_flush (GstVideoEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
if (klass->flush)
|
|
|
|
ret = klass->flush (encoder);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
|
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv;
|
|
|
|
GstPadTemplate *pad_template;
|
|
|
|
GstPad *pad;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
|
|
|
|
|
2018-06-23 19:33:16 +00:00
|
|
|
priv = encoder->priv = gst_video_encoder_get_instance_private (encoder);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
pad_template =
|
|
|
|
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
|
|
|
|
g_return_if_fail (pad_template != NULL);
|
|
|
|
|
|
|
|
encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
|
|
|
|
|
|
|
|
gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_encoder_chain));
|
|
|
|
gst_pad_set_event_function (pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_video_encoder_sink_event));
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_pad_set_query_function (pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_video_encoder_sink_query));
|
2012-03-07 09:18:49 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
|
|
|
|
|
|
|
|
pad_template =
|
|
|
|
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
|
|
|
|
g_return_if_fail (pad_template != NULL);
|
|
|
|
|
|
|
|
encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
|
|
|
|
|
|
|
|
gst_pad_set_query_function (pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_video_encoder_src_query));
|
|
|
|
gst_pad_set_event_function (pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_video_encoder_src_event));
|
|
|
|
gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
|
|
|
|
|
|
|
|
gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
|
|
|
|
gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
g_rec_mutex_init (&encoder->stream_lock);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
priv->headers = NULL;
|
|
|
|
priv->new_headers = FALSE;
|
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
g_queue_init (&priv->frames);
|
|
|
|
g_queue_init (&priv->force_key_unit);
|
|
|
|
|
2015-02-03 11:23:06 +00:00
|
|
|
priv->min_latency = 0;
|
2015-02-11 12:43:11 +00:00
|
|
|
priv->max_latency = 0;
|
2014-11-26 20:06:57 +00:00
|
|
|
priv->min_pts = GST_CLOCK_TIME_NONE;
|
|
|
|
priv->time_adjustment = GST_CLOCK_TIME_NONE;
|
2015-02-03 11:23:06 +00:00
|
|
|
|
2013-07-25 08:42:56 +00:00
|
|
|
gst_video_encoder_reset (encoder, TRUE);
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_set_headers:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
|
|
|
|
*
|
|
|
|
* Set the codec headers to be sent downstream whenever requested.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
|
|
|
|
{
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (video_encoder, "new headers %p", headers);
|
|
|
|
if (video_encoder->priv->headers) {
|
|
|
|
g_list_foreach (video_encoder->priv->headers, (GFunc) gst_buffer_unref,
|
|
|
|
NULL);
|
|
|
|
g_list_free (video_encoder->priv->headers);
|
|
|
|
}
|
|
|
|
video_encoder->priv->headers = headers;
|
|
|
|
video_encoder->priv->new_headers = TRUE;
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstVideoCodecState *
|
|
|
|
_new_output_state (GstCaps * caps, GstVideoCodecState * reference)
|
|
|
|
{
|
|
|
|
GstVideoCodecState *state;
|
|
|
|
|
|
|
|
state = g_slice_new0 (GstVideoCodecState);
|
|
|
|
state->ref_count = 1;
|
|
|
|
gst_video_info_init (&state->info);
|
2016-11-23 18:10:34 +00:00
|
|
|
|
|
|
|
if (!gst_video_info_set_format (&state->info, GST_VIDEO_FORMAT_ENCODED, 0, 0)) {
|
|
|
|
g_slice_free (GstVideoCodecState, state);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
state->caps = caps;
|
|
|
|
|
|
|
|
if (reference) {
|
|
|
|
GstVideoInfo *tgt, *ref;
|
|
|
|
|
|
|
|
tgt = &state->info;
|
|
|
|
ref = &reference->info;
|
|
|
|
|
|
|
|
/* Copy over extra fields from reference state */
|
|
|
|
tgt->interlace_mode = ref->interlace_mode;
|
|
|
|
tgt->flags = ref->flags;
|
|
|
|
tgt->width = ref->width;
|
|
|
|
tgt->height = ref->height;
|
|
|
|
tgt->chroma_site = ref->chroma_site;
|
|
|
|
tgt->colorimetry = ref->colorimetry;
|
|
|
|
tgt->par_n = ref->par_n;
|
|
|
|
tgt->par_d = ref->par_d;
|
|
|
|
tgt->fps_n = ref->fps_n;
|
|
|
|
tgt->fps_d = ref->fps_d;
|
2015-06-11 01:12:39 +00:00
|
|
|
|
2016-09-29 11:48:00 +00:00
|
|
|
GST_VIDEO_INFO_FIELD_ORDER (tgt) = GST_VIDEO_INFO_FIELD_ORDER (ref);
|
|
|
|
|
2015-06-11 01:12:39 +00:00
|
|
|
GST_VIDEO_INFO_MULTIVIEW_MODE (tgt) = GST_VIDEO_INFO_MULTIVIEW_MODE (ref);
|
|
|
|
GST_VIDEO_INFO_MULTIVIEW_FLAGS (tgt) = GST_VIDEO_INFO_MULTIVIEW_FLAGS (ref);
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstVideoCodecState *
|
|
|
|
_new_input_state (GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstVideoCodecState *state;
|
|
|
|
|
|
|
|
state = g_slice_new0 (GstVideoCodecState);
|
|
|
|
state->ref_count = 1;
|
|
|
|
gst_video_info_init (&state->info);
|
|
|
|
if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
|
|
|
|
goto parse_fail;
|
|
|
|
state->caps = gst_caps_ref (caps);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
|
|
|
parse_fail:
|
|
|
|
{
|
|
|
|
g_slice_free (GstVideoCodecState, state);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoderClass *encoder_class;
|
|
|
|
GstVideoCodecState *state;
|
2020-04-01 15:47:04 +00:00
|
|
|
gboolean ret = TRUE;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
|
|
|
|
|
2013-04-15 07:42:22 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
|
|
|
|
if (encoder->priv->input_state) {
|
|
|
|
GST_DEBUG_OBJECT (encoder,
|
|
|
|
"Checking if caps changed old %" GST_PTR_FORMAT " new %" GST_PTR_FORMAT,
|
|
|
|
encoder->priv->input_state->caps, caps);
|
|
|
|
if (gst_caps_is_equal (encoder->priv->input_state->caps, caps))
|
|
|
|
goto caps_not_changed;
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
state = _new_input_state (caps);
|
|
|
|
if (G_UNLIKELY (!state))
|
|
|
|
goto parse_fail;
|
|
|
|
|
2013-04-18 07:58:09 +00:00
|
|
|
if (encoder->priv->input_state
|
|
|
|
&& gst_video_info_is_equal (&state->info,
|
|
|
|
&encoder->priv->input_state->info)) {
|
|
|
|
gst_video_codec_state_unref (state);
|
|
|
|
goto caps_not_changed;
|
|
|
|
}
|
|
|
|
|
2013-08-14 14:55:55 +00:00
|
|
|
if (encoder_class->reset) {
|
|
|
|
GST_FIXME_OBJECT (encoder, "GstVideoEncoder::reset() is deprecated");
|
2015-03-09 11:25:41 +00:00
|
|
|
encoder_class->reset (encoder, TRUE);
|
2013-08-14 14:55:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 07:58:09 +00:00
|
|
|
/* and subclass should be ready to configure format at any time around */
|
2020-04-01 15:47:04 +00:00
|
|
|
if (encoder_class->set_format != NULL)
|
|
|
|
ret = encoder_class->set_format (encoder, state);
|
|
|
|
|
2013-04-18 07:58:09 +00:00
|
|
|
if (ret) {
|
|
|
|
if (encoder->priv->input_state)
|
|
|
|
gst_video_codec_state_unref (encoder->priv->input_state);
|
|
|
|
encoder->priv->input_state = state;
|
2012-03-07 09:18:49 +00:00
|
|
|
} else {
|
|
|
|
gst_video_codec_state_unref (state);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
2013-04-15 07:42:22 +00:00
|
|
|
caps_not_changed:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (encoder, "Caps did not change - ignore");
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ERRORS */
|
2012-03-07 09:18:49 +00:00
|
|
|
parse_fail:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (encoder, "Failed to parse caps");
|
2013-04-15 07:42:22 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
2012-03-07 09:18:49 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_proxy_getcaps:
|
|
|
|
* @enc: a #GstVideoEncoder
|
2014-12-17 22:14:38 +00:00
|
|
|
* @caps: (allow-none): initial caps
|
|
|
|
* @filter: (allow-none): filter caps
|
2012-03-07 09:18:49 +00:00
|
|
|
*
|
|
|
|
* Returns caps that express @caps (or sink template caps if @caps == NULL)
|
|
|
|
* restricted to resolution/format/... combinations supported by downstream
|
|
|
|
* elements (e.g. muxers).
|
|
|
|
*
|
2015-02-19 18:51:19 +00:00
|
|
|
* Returns: (transfer full): a #GstCaps owned by caller
|
2012-03-07 09:18:49 +00:00
|
|
|
*/
|
|
|
|
GstCaps *
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
|
|
|
|
GstCaps * filter)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
2014-12-08 19:33:33 +00:00
|
|
|
return __gst_video_element_proxy_getcaps (GST_ELEMENT_CAST (encoder),
|
|
|
|
GST_VIDEO_ENCODER_SINK_PAD (encoder),
|
|
|
|
GST_VIDEO_ENCODER_SRC_PAD (encoder), caps, filter);
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoderClass *klass;
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
if (klass->getcaps)
|
2012-04-24 17:35:24 +00:00
|
|
|
caps = klass->getcaps (encoder, filter);
|
2012-03-07 09:18:49 +00:00
|
|
|
else
|
2012-04-24 17:35:24 +00:00
|
|
|
caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2012-07-23 07:07:02 +00:00
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_decide_allocation_default (GstVideoEncoder * encoder,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
|
|
|
GstAllocator *allocator = NULL;
|
|
|
|
GstAllocationParams params;
|
|
|
|
gboolean update_allocator;
|
|
|
|
|
|
|
|
/* we got configuration from our peer or the decide_allocation method,
|
|
|
|
* parse them */
|
|
|
|
if (gst_query_get_n_allocation_params (query) > 0) {
|
|
|
|
/* try the allocator */
|
|
|
|
gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
|
|
|
|
update_allocator = TRUE;
|
|
|
|
} else {
|
|
|
|
allocator = NULL;
|
|
|
|
gst_allocation_params_init (¶ms);
|
|
|
|
update_allocator = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update_allocator)
|
|
|
|
gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
|
|
|
|
else
|
|
|
|
gst_query_add_allocation_param (query, allocator, ¶ms);
|
|
|
|
if (allocator)
|
|
|
|
gst_object_unref (allocator);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-04-24 20:42:59 +00:00
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_propose_allocation_default (GstVideoEncoder * encoder,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
2012-08-08 13:28:52 +00:00
|
|
|
GstCaps *caps;
|
|
|
|
GstVideoInfo info;
|
|
|
|
GstBufferPool *pool;
|
|
|
|
guint size;
|
|
|
|
|
|
|
|
gst_query_parse_allocation (query, &caps, NULL);
|
|
|
|
|
|
|
|
if (caps == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!gst_video_info_from_caps (&info, caps))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
size = GST_VIDEO_INFO_SIZE (&info);
|
|
|
|
|
|
|
|
if (gst_query_get_n_allocation_pools (query) == 0) {
|
|
|
|
GstStructure *structure;
|
|
|
|
GstAllocator *allocator = NULL;
|
2012-08-08 14:12:20 +00:00
|
|
|
GstAllocationParams params = { 0, 15, 0, 0 };
|
2012-08-08 13:28:52 +00:00
|
|
|
|
|
|
|
if (gst_query_get_n_allocation_params (query) > 0)
|
|
|
|
gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
|
|
|
|
else
|
|
|
|
gst_query_add_allocation_param (query, allocator, ¶ms);
|
|
|
|
|
|
|
|
pool = gst_video_buffer_pool_new ();
|
|
|
|
|
|
|
|
structure = gst_buffer_pool_get_config (pool);
|
|
|
|
gst_buffer_pool_config_set_params (structure, caps, size, 0, 0);
|
|
|
|
gst_buffer_pool_config_set_allocator (structure, allocator, ¶ms);
|
|
|
|
|
|
|
|
if (allocator)
|
|
|
|
gst_object_unref (allocator);
|
|
|
|
|
|
|
|
if (!gst_buffer_pool_set_config (pool, structure))
|
|
|
|
goto config_failed;
|
|
|
|
|
|
|
|
gst_query_add_allocation_pool (query, pool, size, 0, 0);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
|
|
|
|
}
|
|
|
|
|
2012-04-24 20:42:59 +00:00
|
|
|
return TRUE;
|
2012-08-08 13:28:52 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
config_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (encoder, "failed to set config");
|
|
|
|
gst_object_unref (pool);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-04-24 20:42:59 +00:00
|
|
|
}
|
2012-04-24 17:35:24 +00:00
|
|
|
|
|
|
|
static gboolean
|
2013-12-09 15:33:40 +00:00
|
|
|
gst_video_encoder_sink_query_default (GstVideoEncoder * encoder,
|
2012-04-24 17:35:24 +00:00
|
|
|
GstQuery * query)
|
|
|
|
{
|
2013-12-09 15:33:40 +00:00
|
|
|
GstPad *pad = GST_VIDEO_ENCODER_SINK_PAD (encoder);
|
2012-04-24 17:35:24 +00:00
|
|
|
gboolean res = FALSE;
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *filter, *caps;
|
|
|
|
|
|
|
|
gst_query_parse_caps (query, &filter);
|
|
|
|
caps = gst_video_encoder_sink_getcaps (encoder, filter);
|
|
|
|
gst_query_set_caps_result (query, caps);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-04 08:47:36 +00:00
|
|
|
case GST_QUERY_CONVERT:
|
|
|
|
{
|
|
|
|
GstFormat src_fmt, dest_fmt;
|
|
|
|
gint64 src_val, dest_val;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder, "convert query");
|
|
|
|
|
|
|
|
gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
if (encoder->priv->input_state != NULL)
|
|
|
|
res = __gst_video_rawvideo_convert (encoder->priv->input_state,
|
|
|
|
src_fmt, src_val, &dest_fmt, &dest_val);
|
|
|
|
else
|
|
|
|
res = FALSE;
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
if (!res)
|
|
|
|
goto error;
|
|
|
|
gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
|
|
|
|
break;
|
|
|
|
}
|
2012-04-24 20:42:59 +00:00
|
|
|
case GST_QUERY_ALLOCATION:
|
|
|
|
{
|
|
|
|
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
if (klass->propose_allocation)
|
|
|
|
res = klass->propose_allocation (encoder, query);
|
|
|
|
break;
|
|
|
|
}
|
2012-04-24 17:35:24 +00:00
|
|
|
default:
|
2013-12-09 15:33:40 +00:00
|
|
|
res = gst_pad_query_default (pad, GST_OBJECT (encoder), query);
|
2012-04-24 17:35:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
2016-07-04 08:47:36 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
GST_DEBUG_OBJECT (encoder, "query failed");
|
|
|
|
return res;
|
2012-04-24 17:35:24 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 15:33:40 +00:00
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
|
|
|
GstVideoEncoder *encoder;
|
|
|
|
GstVideoEncoderClass *encoder_class;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
encoder = GST_VIDEO_ENCODER (parent);
|
|
|
|
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder, "received query %d, %s", GST_QUERY_TYPE (query),
|
|
|
|
GST_QUERY_TYPE_NAME (query));
|
|
|
|
|
|
|
|
if (encoder_class->sink_query)
|
|
|
|
ret = encoder_class->sink_query (encoder, query);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstVideoEncoder *encoder;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (object, "finalize");
|
|
|
|
|
|
|
|
encoder = GST_VIDEO_ENCODER (object);
|
2012-04-24 17:35:24 +00:00
|
|
|
g_rec_mutex_clear (&encoder->stream_lock);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2012-07-23 07:07:02 +00:00
|
|
|
if (encoder->priv->allocator) {
|
|
|
|
gst_object_unref (encoder->priv->allocator);
|
|
|
|
encoder->priv->allocator = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
|
|
|
|
{
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2012-04-24 17:35:24 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
2012-04-24 17:35:24 +00:00
|
|
|
GstSegment segment;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_event_copy_segment (event, &segment);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
|
|
|
|
|
|
|
|
if (segment.format != GST_FORMAT_TIME) {
|
|
|
|
GST_DEBUG_OBJECT (encoder, "received non TIME segment");
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-26 20:06:57 +00:00
|
|
|
if (encoder->priv->time_adjustment != GST_CLOCK_TIME_NONE) {
|
|
|
|
segment.start += encoder->priv->time_adjustment;
|
2018-06-13 11:19:54 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (segment.position)) {
|
|
|
|
segment.position += encoder->priv->time_adjustment;
|
|
|
|
}
|
2014-11-26 20:06:57 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (segment.stop)) {
|
|
|
|
segment.stop += encoder->priv->time_adjustment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
encoder->output_segment = segment;
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
2014-11-26 20:06:57 +00:00
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
event = gst_event_new_segment (&encoder->output_segment);
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_pad_push_event (encoder->srcpad, event);
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:29:29 +00:00
|
|
|
static GstEvent *
|
|
|
|
gst_video_encoder_create_merged_tags_event (GstVideoEncoder * enc)
|
|
|
|
{
|
|
|
|
GstTagList *merged_tags;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (enc, "upstream : %" GST_PTR_FORMAT, enc->priv->upstream_tags);
|
|
|
|
GST_LOG_OBJECT (enc, "encoder : %" GST_PTR_FORMAT, enc->priv->tags);
|
|
|
|
GST_LOG_OBJECT (enc, "mode : %d", enc->priv->tags_merge_mode);
|
|
|
|
|
|
|
|
merged_tags =
|
|
|
|
gst_tag_list_merge (enc->priv->upstream_tags, enc->priv->tags,
|
|
|
|
enc->priv->tags_merge_mode);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (enc, "merged : %" GST_PTR_FORMAT, merged_tags);
|
|
|
|
|
|
|
|
if (merged_tags == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (gst_tag_list_is_empty (merged_tags)) {
|
|
|
|
gst_tag_list_unref (merged_tags);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_event_new_tag (merged_tags);
|
|
|
|
}
|
|
|
|
|
2014-01-28 18:30:35 +00:00
|
|
|
static inline void
|
|
|
|
gst_video_encoder_check_and_push_tags (GstVideoEncoder * encoder)
|
|
|
|
{
|
2015-08-18 11:29:29 +00:00
|
|
|
if (encoder->priv->tags_changed) {
|
|
|
|
GstEvent *tags_event;
|
|
|
|
|
|
|
|
tags_event = gst_video_encoder_create_merged_tags_event (encoder);
|
|
|
|
|
|
|
|
if (tags_event != NULL)
|
|
|
|
gst_video_encoder_push_event (encoder, tags_event);
|
|
|
|
|
2014-01-28 18:30:35 +00:00
|
|
|
encoder->priv->tags_changed = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
static gboolean
|
2012-04-24 17:42:04 +00:00
|
|
|
gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
|
|
|
|
GstEvent * event)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoderClass *encoder_class;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2012-04-24 17:35:24 +00:00
|
|
|
case GST_EVENT_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
gst_event_parse_caps (event, &caps);
|
2014-07-08 20:59:37 +00:00
|
|
|
ret = gst_video_encoder_setcaps (encoder, caps);
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_event_unref (event);
|
2012-04-24 17:42:04 +00:00
|
|
|
event = NULL;
|
2012-04-24 17:35:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
case GST_EVENT_EOS:
|
|
|
|
{
|
|
|
|
GstFlowReturn flow_ret;
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
|
|
|
|
if (encoder_class->finish) {
|
|
|
|
flow_ret = encoder_class->finish (encoder);
|
|
|
|
} else {
|
|
|
|
flow_ret = GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-28 18:30:35 +00:00
|
|
|
if (encoder->priv->current_frame_events) {
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = g_list_last (encoder->priv->current_frame_events); l;
|
|
|
|
l = g_list_previous (l)) {
|
|
|
|
GstEvent *event = GST_EVENT (l->data);
|
|
|
|
|
|
|
|
gst_video_encoder_push_event (encoder, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_list_free (encoder->priv->current_frame_events);
|
|
|
|
encoder->priv->current_frame_events = NULL;
|
|
|
|
|
|
|
|
gst_video_encoder_check_and_push_tags (encoder);
|
|
|
|
|
2012-04-24 17:42:04 +00:00
|
|
|
ret = (flow_ret == GST_FLOW_OK);
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
break;
|
|
|
|
}
|
2012-04-24 17:35:24 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
2012-04-24 17:35:24 +00:00
|
|
|
GstSegment segment;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_event_copy_segment (event, &segment);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
if (segment.format != GST_FORMAT_TIME) {
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
encoder->input_segment = segment;
|
2012-04-24 19:32:08 +00:00
|
|
|
ret = TRUE;
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_EVENT_CUSTOM_DOWNSTREAM:
|
|
|
|
{
|
|
|
|
if (gst_video_event_is_force_key_unit (event)) {
|
|
|
|
GstClockTime running_time;
|
|
|
|
gboolean all_headers;
|
|
|
|
guint count;
|
|
|
|
|
|
|
|
if (gst_video_event_parse_downstream_force_key_unit (event,
|
|
|
|
NULL, NULL, &running_time, &all_headers, &count)) {
|
|
|
|
ForcedKeyUnitEvent *fevt;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
fevt = forced_key_unit_event_new (running_time, all_headers, count);
|
2020-06-03 16:59:03 +00:00
|
|
|
g_queue_insert_sorted (&encoder->priv->force_key_unit, fevt,
|
|
|
|
(GCompareDataFunc) forced_key_unit_event_compare, NULL);
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder,
|
|
|
|
"force-key-unit event: running-time %" GST_TIME_FORMAT
|
|
|
|
", all_headers %d, count %u",
|
|
|
|
GST_TIME_ARGS (running_time), all_headers, count);
|
|
|
|
}
|
|
|
|
gst_event_unref (event);
|
2012-04-24 17:42:04 +00:00
|
|
|
event = NULL;
|
2012-03-07 09:18:49 +00:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-08-18 11:29:29 +00:00
|
|
|
case GST_EVENT_STREAM_START:
|
|
|
|
{
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
/* Flush upstream tags after a STREAM_START */
|
|
|
|
GST_DEBUG_OBJECT (encoder, "STREAM_START, clearing upstream tags");
|
|
|
|
if (encoder->priv->upstream_tags) {
|
|
|
|
gst_tag_list_unref (encoder->priv->upstream_tags);
|
|
|
|
encoder->priv->upstream_tags = NULL;
|
|
|
|
encoder->priv->tags_changed = TRUE;
|
|
|
|
}
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
break;
|
|
|
|
}
|
2012-08-09 14:19:32 +00:00
|
|
|
case GST_EVENT_TAG:
|
|
|
|
{
|
|
|
|
GstTagList *tags;
|
|
|
|
|
|
|
|
gst_event_parse_tag (event, &tags);
|
|
|
|
|
|
|
|
if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
|
2015-08-18 11:29:29 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
if (encoder->priv->upstream_tags != tags) {
|
|
|
|
tags = gst_tag_list_copy (tags);
|
|
|
|
|
|
|
|
/* FIXME: make generic based on GST_TAG_FLAG_ENCODED */
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_AUDIO_CODEC);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_VIDEO_CODEC);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_SUBTITLE_CODEC);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_BITRATE);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_NOMINAL_BITRATE);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_MAXIMUM_BITRATE);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_MINIMUM_BITRATE);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_ENCODER);
|
|
|
|
gst_tag_list_remove_tag (tags, GST_TAG_ENCODER_VERSION);
|
|
|
|
|
|
|
|
if (encoder->priv->upstream_tags)
|
|
|
|
gst_tag_list_unref (encoder->priv->upstream_tags);
|
|
|
|
encoder->priv->upstream_tags = tags;
|
|
|
|
GST_INFO_OBJECT (encoder, "upstream tags: %" GST_PTR_FORMAT, tags);
|
|
|
|
}
|
2012-08-09 14:19:32 +00:00
|
|
|
gst_event_unref (event);
|
2015-08-18 11:29:29 +00:00
|
|
|
event = gst_video_encoder_create_merged_tags_event (encoder);
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
2015-08-18 13:28:42 +00:00
|
|
|
if (!event)
|
|
|
|
ret = TRUE;
|
2012-08-09 14:19:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-07-25 08:46:04 +00:00
|
|
|
case GST_EVENT_FLUSH_STOP:{
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2013-08-15 10:44:56 +00:00
|
|
|
gst_video_encoder_flush (encoder);
|
2013-07-25 08:46:04 +00:00
|
|
|
gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
|
|
|
|
gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
|
|
|
|
gst_video_encoder_reset (encoder, FALSE);
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
break;
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-24 17:42:04 +00:00
|
|
|
/* 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 simply
|
|
|
|
* not chain up to the parent class' event handler
|
|
|
|
*
|
|
|
|
* For FLUSH_STOP this is required because it is expected
|
|
|
|
* to be forwarded immediately and no buffers are queued anyway.
|
|
|
|
*/
|
|
|
|
if (event) {
|
|
|
|
if (!GST_EVENT_IS_SERIALIZED (event)
|
|
|
|
|| GST_EVENT_TYPE (event) == GST_EVENT_EOS
|
|
|
|
|| GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
|
|
|
|
ret = gst_video_encoder_push_event (encoder, event);
|
|
|
|
} else {
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
encoder->priv->current_frame_events =
|
|
|
|
g_list_prepend (encoder->priv->current_frame_events, event);
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
2012-04-24 19:32:08 +00:00
|
|
|
ret = TRUE;
|
2012-04-24 17:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoder *enc;
|
|
|
|
GstVideoEncoderClass *klass;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
enc = GST_VIDEO_ENCODER (parent);
|
2012-03-07 09:18:49 +00:00
|
|
|
klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
|
|
|
|
GST_EVENT_TYPE_NAME (event));
|
|
|
|
|
|
|
|
if (klass->sink_event)
|
2012-04-24 17:42:04 +00:00
|
|
|
ret = klass->sink_event (enc, event);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-04-24 17:42:04 +00:00
|
|
|
gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
|
|
|
|
GstEvent * event)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
2012-04-24 17:42:04 +00:00
|
|
|
gboolean ret = FALSE;
|
2017-09-21 13:18:10 +00:00
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_CUSTOM_UPSTREAM:
|
|
|
|
{
|
|
|
|
if (gst_video_event_is_force_key_unit (event)) {
|
|
|
|
GstClockTime running_time;
|
|
|
|
gboolean all_headers;
|
|
|
|
guint count;
|
|
|
|
|
|
|
|
if (gst_video_event_parse_upstream_force_key_unit (event,
|
|
|
|
&running_time, &all_headers, &count)) {
|
|
|
|
ForcedKeyUnitEvent *fevt;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
fevt = forced_key_unit_event_new (running_time, all_headers, count);
|
2020-06-03 16:59:03 +00:00
|
|
|
g_queue_insert_sorted (&encoder->priv->force_key_unit, fevt,
|
|
|
|
(GCompareDataFunc) forced_key_unit_event_compare, NULL);
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder,
|
|
|
|
"force-key-unit event: running-time %" GST_TIME_FORMAT
|
|
|
|
", all_headers %d, count %u",
|
|
|
|
GST_TIME_ARGS (running_time), all_headers, count);
|
|
|
|
}
|
|
|
|
gst_event_unref (event);
|
2012-04-24 17:42:04 +00:00
|
|
|
event = NULL;
|
|
|
|
ret = TRUE;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-09-21 13:18:10 +00:00
|
|
|
case GST_EVENT_QOS:
|
|
|
|
{
|
|
|
|
GstQOSType type;
|
|
|
|
gdouble proportion;
|
|
|
|
GstClockTimeDiff diff;
|
|
|
|
GstClockTime timestamp;
|
|
|
|
|
2017-10-25 09:41:02 +00:00
|
|
|
if (!g_atomic_int_get (&priv->qos_enabled))
|
|
|
|
break;
|
|
|
|
|
2017-09-21 13:18:10 +00:00
|
|
|
gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
priv->proportion = proportion;
|
|
|
|
if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (timestamp))) {
|
|
|
|
if (G_UNLIKELY (diff > 0)) {
|
|
|
|
priv->earliest_time = timestamp + 2 * diff + priv->qos_frame_duration;
|
|
|
|
} else {
|
|
|
|
priv->earliest_time = timestamp + diff;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
priv->earliest_time = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder,
|
|
|
|
"got QoS %" GST_TIME_FORMAT ", %" GST_STIME_FORMAT ", %g",
|
|
|
|
GST_TIME_ARGS (timestamp), GST_STIME_ARGS (diff), proportion);
|
|
|
|
|
|
|
|
ret = gst_pad_push_event (encoder->sinkpad, event);
|
|
|
|
event = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-24 17:42:04 +00:00
|
|
|
if (event)
|
|
|
|
ret =
|
|
|
|
gst_pad_event_default (encoder->srcpad, GST_OBJECT_CAST (encoder),
|
|
|
|
event);
|
|
|
|
|
|
|
|
return ret;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoder *encoder;
|
|
|
|
GstVideoEncoderClass *klass;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
encoder = GST_VIDEO_ENCODER (parent);
|
2012-03-07 09:18:49 +00:00
|
|
|
klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
|
|
|
|
|
|
|
|
if (klass->src_event)
|
2012-04-24 17:42:04 +00:00
|
|
|
ret = klass->src_event (encoder, event);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-12-09 15:33:40 +00:00
|
|
|
gst_video_encoder_src_query_default (GstVideoEncoder * enc, GstQuery * query)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
2013-12-09 15:33:40 +00:00
|
|
|
GstPad *pad = GST_VIDEO_ENCODER_SRC_PAD (enc);
|
2012-03-07 09:18:49 +00:00
|
|
|
GstVideoEncoderPrivate *priv;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
priv = enc->priv;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CONVERT:
|
|
|
|
{
|
|
|
|
GstFormat src_fmt, dest_fmt;
|
|
|
|
gint64 src_val, dest_val;
|
|
|
|
|
|
|
|
gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
|
2016-07-04 08:52:24 +00:00
|
|
|
GST_OBJECT_LOCK (enc);
|
2012-03-07 09:18:49 +00:00
|
|
|
res =
|
2016-07-04 08:47:36 +00:00
|
|
|
__gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
|
2012-03-07 09:18:49 +00:00
|
|
|
src_val, &dest_fmt, &dest_val);
|
2016-07-04 08:52:24 +00:00
|
|
|
GST_OBJECT_UNLOCK (enc);
|
2012-03-07 09:18:49 +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;
|
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
res = gst_pad_peer_query (enc->sinkpad, query);
|
2012-03-07 09:18:49 +00:00
|
|
|
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 += priv->min_latency;
|
2015-02-11 12:43:11 +00:00
|
|
|
if (max_latency == GST_CLOCK_TIME_NONE
|
|
|
|
|| enc->priv->max_latency == GST_CLOCK_TIME_NONE)
|
|
|
|
max_latency = GST_CLOCK_TIME_NONE;
|
|
|
|
else
|
2012-04-25 11:52:51 +00:00
|
|
|
max_latency += enc->priv->max_latency;
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_OBJECT_UNLOCK (enc);
|
|
|
|
|
|
|
|
gst_query_set_latency (query, live, min_latency, max_latency);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-12-09 15:33:40 +00:00
|
|
|
res = gst_pad_query_default (pad, GST_OBJECT (enc), query);
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
|
|
|
|
error:
|
|
|
|
GST_DEBUG_OBJECT (enc, "query failed");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-12-09 15:33:40 +00:00
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstVideoEncoder *encoder;
|
|
|
|
GstVideoEncoderClass *encoder_class;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
encoder = GST_VIDEO_ENCODER (parent);
|
|
|
|
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder, "received query %d, %s", GST_QUERY_TYPE (query),
|
|
|
|
GST_QUERY_TYPE_NAME (query));
|
|
|
|
|
|
|
|
if (encoder_class->src_query)
|
|
|
|
ret = encoder_class->src_query (encoder, query);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
static GstVideoCodecFrame *
|
|
|
|
gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
|
2012-06-27 14:45:41 +00:00
|
|
|
GstClockTime pts, GstClockTime dts, GstClockTime duration)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
|
|
|
GstVideoCodecFrame *frame;
|
|
|
|
|
|
|
|
frame = g_slice_new0 (GstVideoCodecFrame);
|
|
|
|
|
|
|
|
frame->ref_count = 1;
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
frame->system_frame_number = priv->system_frame_number;
|
|
|
|
priv->system_frame_number++;
|
|
|
|
|
|
|
|
frame->presentation_frame_number = priv->presentation_frame_number;
|
|
|
|
priv->presentation_frame_number++;
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
frame->events = priv->current_frame_events;
|
|
|
|
priv->current_frame_events = NULL;
|
|
|
|
frame->input_buffer = buf;
|
2012-06-27 14:45:41 +00:00
|
|
|
frame->pts = pts;
|
|
|
|
frame->dts = dts;
|
2012-03-07 09:18:49 +00:00
|
|
|
frame->duration = duration;
|
2012-09-28 11:59:24 +00:00
|
|
|
frame->abidata.ABI.ts = pts;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2012-04-24 17:35:24 +00:00
|
|
|
gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoder *encoder;
|
|
|
|
GstVideoEncoderPrivate *priv;
|
|
|
|
GstVideoEncoderClass *klass;
|
|
|
|
GstVideoCodecFrame *frame;
|
2012-09-26 14:22:56 +00:00
|
|
|
GstClockTime pts, duration;
|
2012-03-07 09:18:49 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2012-06-27 14:45:41 +00:00
|
|
|
guint64 start, stop, cstart, cstop;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2012-04-24 17:35:24 +00:00
|
|
|
encoder = GST_VIDEO_ENCODER (parent);
|
2012-03-07 09:18:49 +00:00
|
|
|
priv = encoder->priv;
|
|
|
|
klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
|
|
|
|
|
2013-12-05 10:25:47 +00:00
|
|
|
if (!encoder->priv->input_state)
|
|
|
|
goto not_negotiated;
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
|
2012-06-27 14:45:41 +00:00
|
|
|
pts = GST_BUFFER_PTS (buf);
|
|
|
|
duration = GST_BUFFER_DURATION (buf);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (encoder,
|
2012-06-27 14:45:41 +00:00
|
|
|
"received buffer of size %" G_GSIZE_FORMAT " with PTS %" GST_TIME_FORMAT
|
2012-09-25 15:19:15 +00:00
|
|
|
", DTS %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
|
2012-09-26 14:22:56 +00:00
|
|
|
gst_buffer_get_size (buf), GST_TIME_ARGS (pts),
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_DTS (buf)), GST_TIME_ARGS (duration));
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2012-06-27 14:45:41 +00:00
|
|
|
start = pts;
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (duration))
|
|
|
|
stop = start + duration;
|
|
|
|
else
|
|
|
|
stop = GST_CLOCK_TIME_NONE;
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
/* Drop buffers outside of segment */
|
2012-09-25 15:19:15 +00:00
|
|
|
if (!gst_segment_clip (&encoder->input_segment,
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
|
|
|
|
GST_DEBUG_OBJECT (encoder, "clipping to segment dropped frame");
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2013-09-02 22:47:18 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (cstop))
|
|
|
|
duration = cstop - cstart;
|
|
|
|
else
|
|
|
|
duration = GST_CLOCK_TIME_NONE;
|
|
|
|
|
2014-11-26 20:06:57 +00:00
|
|
|
if (priv->min_pts != GST_CLOCK_TIME_NONE
|
|
|
|
&& priv->time_adjustment == GST_CLOCK_TIME_NONE) {
|
|
|
|
if (cstart < priv->min_pts) {
|
|
|
|
priv->time_adjustment = priv->min_pts - cstart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->time_adjustment != GST_CLOCK_TIME_NONE) {
|
|
|
|
cstart += priv->time_adjustment;
|
|
|
|
}
|
|
|
|
|
2012-09-26 14:22:56 +00:00
|
|
|
/* incoming DTS is not really relevant and does not make sense anyway,
|
|
|
|
* so pass along _NONE and maybe come up with something better later on */
|
|
|
|
frame = gst_video_encoder_new_frame (encoder, buf, cstart,
|
2013-09-02 22:47:18 +00:00
|
|
|
GST_CLOCK_TIME_NONE, duration);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
2020-06-03 11:37:00 +00:00
|
|
|
if (priv->force_key_unit.head) {
|
2012-03-07 09:18:49 +00:00
|
|
|
GList *l;
|
2020-06-03 18:46:38 +00:00
|
|
|
GstClockTime running_time;
|
|
|
|
gboolean throttled = FALSE, have_fevt = FALSE;
|
2020-06-03 17:17:06 +00:00
|
|
|
GQueue matching_fevt = G_QUEUE_INIT;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
running_time =
|
|
|
|
gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
|
2012-06-27 14:45:41 +00:00
|
|
|
cstart);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2020-06-03 18:46:38 +00:00
|
|
|
throttled = (priv->min_force_key_unit_interval != 0 &&
|
|
|
|
priv->min_force_key_unit_interval != GST_CLOCK_TIME_NONE &&
|
|
|
|
priv->last_force_key_unit_request != GST_CLOCK_TIME_NONE &&
|
|
|
|
priv->last_force_key_unit_request + priv->min_force_key_unit_interval >
|
|
|
|
running_time);
|
|
|
|
|
|
|
|
for (l = priv->force_key_unit.head; l && (!throttled || !have_fevt);
|
|
|
|
l = l->next) {
|
2020-06-03 17:17:06 +00:00
|
|
|
ForcedKeyUnitEvent *fevt = l->data;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
/* Skip pending keyunits */
|
2020-06-03 17:17:06 +00:00
|
|
|
if (fevt->pending)
|
2012-03-07 09:18:49 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Simple case, keyunit ASAP */
|
2020-06-03 17:17:06 +00:00
|
|
|
if (fevt->running_time == GST_CLOCK_TIME_NONE) {
|
2020-06-03 18:46:38 +00:00
|
|
|
have_fevt = TRUE;
|
|
|
|
if (!throttled)
|
|
|
|
g_queue_push_tail (&matching_fevt, fevt);
|
2020-06-03 17:17:06 +00:00
|
|
|
continue;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Event for before this frame */
|
2020-06-03 17:17:06 +00:00
|
|
|
if (fevt->running_time <= running_time) {
|
2020-06-03 18:46:38 +00:00
|
|
|
have_fevt = TRUE;
|
|
|
|
if (!throttled)
|
|
|
|
g_queue_push_tail (&matching_fevt, fevt);
|
2020-06-03 17:17:06 +00:00
|
|
|
continue;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
2020-06-03 17:17:06 +00:00
|
|
|
|
|
|
|
/* Otherwise all following events are in the future */
|
|
|
|
break;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 18:46:38 +00:00
|
|
|
if (throttled && have_fevt) {
|
|
|
|
GST_DEBUG_OBJECT (encoder,
|
|
|
|
"Not requesting a new key unit yet due to throttling (%"
|
|
|
|
GST_TIME_FORMAT " + %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (priv->last_force_key_unit_request),
|
|
|
|
GST_TIME_ARGS (priv->min_force_key_unit_interval),
|
|
|
|
GST_TIME_ARGS (running_time));
|
|
|
|
g_queue_clear (&matching_fevt);
|
|
|
|
}
|
|
|
|
|
2020-06-03 17:17:06 +00:00
|
|
|
if (matching_fevt.length > 0) {
|
|
|
|
ForcedKeyUnitEvent *fevt;
|
|
|
|
gboolean all_headers = FALSE;
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_DEBUG_OBJECT (encoder,
|
|
|
|
"Forcing a key unit at running time %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (running_time));
|
2020-06-03 17:17:06 +00:00
|
|
|
|
|
|
|
while ((fevt = g_queue_pop_head (&matching_fevt))) {
|
|
|
|
fevt->pending = TRUE;
|
|
|
|
fevt->frame_id = frame->system_frame_number;
|
|
|
|
if (fevt->all_headers)
|
|
|
|
all_headers = TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME (frame);
|
2020-06-03 17:17:06 +00:00
|
|
|
if (all_headers)
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME_HEADERS (frame);
|
2020-06-03 18:46:38 +00:00
|
|
|
priv->last_force_key_unit_request = running_time;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
g_queue_push_tail (&priv->frames, gst_video_codec_frame_ref (frame));
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
/* new data, more finish needed */
|
|
|
|
priv->drained = FALSE;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
|
|
|
|
frame->presentation_frame_number);
|
|
|
|
|
2017-09-21 13:18:10 +00:00
|
|
|
frame->deadline =
|
|
|
|
gst_segment_to_running_time (&encoder->input_segment, GST_FORMAT_TIME,
|
|
|
|
frame->pts);
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
ret = klass->handle_frame (encoder, frame);
|
|
|
|
|
|
|
|
done:
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return ret;
|
2012-07-26 12:28:26 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
not_negotiated:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (encoder, CORE, NEGOTIATION, (NULL),
|
|
|
|
("encoder not initialized"));
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstVideoEncoder *encoder;
|
|
|
|
GstVideoEncoderClass *encoder_class;
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
|
|
|
|
encoder = GST_VIDEO_ENCODER (element);
|
|
|
|
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
/* open device/library if needed */
|
|
|
|
if (encoder_class->open && !encoder_class->open (encoder))
|
|
|
|
goto open_failed;
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
2013-07-25 12:25:08 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
gst_video_encoder_reset (encoder, TRUE);
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
2014-12-22 10:38:20 +00:00
|
|
|
|
|
|
|
/* Initialize device/library if needed */
|
|
|
|
if (encoder_class->start && !encoder_class->start (encoder))
|
|
|
|
goto start_failed;
|
2012-03-07 09:18:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
2014-12-22 10:33:14 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:{
|
|
|
|
gboolean stopped = TRUE;
|
|
|
|
|
|
|
|
if (encoder_class->stop)
|
|
|
|
stopped = encoder_class->stop (encoder);
|
|
|
|
|
2013-07-25 12:25:08 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
gst_video_encoder_reset (encoder, TRUE);
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
2014-12-22 10:33:14 +00:00
|
|
|
|
|
|
|
if (!stopped)
|
2013-07-26 08:22:32 +00:00
|
|
|
goto stop_failed;
|
2012-03-07 09:18:49 +00:00
|
|
|
break;
|
2014-12-22 10:33:14 +00:00
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
/* close device/library if needed */
|
|
|
|
if (encoder_class->close && !encoder_class->close (encoder))
|
|
|
|
goto close_failed;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Errors */
|
|
|
|
|
|
|
|
open_failed:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
|
|
|
|
("Failed to open encoder"));
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
start_failed:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
|
|
|
|
("Failed to start encoder"));
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
stop_failed:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
|
|
|
|
("Failed to stop encoder"));
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
close_failed:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
|
|
|
|
("Failed to close encoder"));
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 12:39:18 +00:00
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_negotiate_default (GstVideoEncoder * encoder)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
2012-07-23 07:07:02 +00:00
|
|
|
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
GstAllocator *allocator;
|
|
|
|
GstAllocationParams params;
|
2013-08-23 17:20:03 +00:00
|
|
|
gboolean ret = TRUE;
|
2012-03-07 09:18:49 +00:00
|
|
|
GstVideoCodecState *state = encoder->priv->output_state;
|
|
|
|
GstVideoInfo *info = &state->info;
|
2012-07-23 08:04:48 +00:00
|
|
|
GstQuery *query = NULL;
|
2013-05-08 13:50:34 +00:00
|
|
|
GstVideoCodecFrame *frame;
|
2013-08-23 17:20:03 +00:00
|
|
|
GstCaps *prevcaps;
|
2016-09-29 11:57:02 +00:00
|
|
|
gchar *colorimetry;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (state->caps != NULL, FALSE);
|
|
|
|
|
|
|
|
if (encoder->priv->output_state_changed) {
|
2019-05-02 05:24:27 +00:00
|
|
|
GstCaps *incaps;
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
state->caps = gst_caps_make_writable (state->caps);
|
|
|
|
|
|
|
|
/* Fill caps */
|
|
|
|
gst_caps_set_simple (state->caps, "width", G_TYPE_INT, info->width,
|
|
|
|
"height", G_TYPE_INT, info->height,
|
|
|
|
"pixel-aspect-ratio", GST_TYPE_FRACTION,
|
|
|
|
info->par_n, info->par_d, NULL);
|
|
|
|
if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
|
|
|
|
/* variable fps with a max-framerate */
|
|
|
|
gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION, 0, 1,
|
|
|
|
"max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
|
|
|
|
} else {
|
|
|
|
/* no variable fps or no max-framerate */
|
|
|
|
gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION,
|
|
|
|
info->fps_n, info->fps_d, NULL);
|
|
|
|
}
|
|
|
|
if (state->codec_data)
|
|
|
|
gst_caps_set_simple (state->caps, "codec_data", GST_TYPE_BUFFER,
|
|
|
|
state->codec_data, NULL);
|
2015-06-11 01:12:39 +00:00
|
|
|
|
2016-09-29 11:48:00 +00:00
|
|
|
gst_caps_set_simple (state->caps, "interlace-mode", G_TYPE_STRING,
|
|
|
|
gst_video_interlace_mode_to_string (info->interlace_mode), NULL);
|
|
|
|
if (info->interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED &&
|
|
|
|
GST_VIDEO_INFO_FIELD_ORDER (info) != GST_VIDEO_FIELD_ORDER_UNKNOWN)
|
|
|
|
gst_caps_set_simple (state->caps, "field-order", G_TYPE_STRING,
|
|
|
|
gst_video_field_order_to_string (GST_VIDEO_INFO_FIELD_ORDER (info)),
|
|
|
|
NULL);
|
|
|
|
|
2016-09-29 11:57:02 +00:00
|
|
|
colorimetry = gst_video_colorimetry_to_string (&info->colorimetry);
|
|
|
|
if (colorimetry)
|
|
|
|
gst_caps_set_simple (state->caps, "colorimetry", G_TYPE_STRING,
|
|
|
|
colorimetry, NULL);
|
|
|
|
g_free (colorimetry);
|
|
|
|
|
|
|
|
if (info->chroma_site != GST_VIDEO_CHROMA_SITE_UNKNOWN)
|
|
|
|
gst_caps_set_simple (state->caps, "chroma-site", G_TYPE_STRING,
|
|
|
|
gst_video_chroma_to_string (info->chroma_site), NULL);
|
|
|
|
|
2015-06-11 01:12:39 +00:00
|
|
|
if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
|
|
|
|
const gchar *caps_mview_mode =
|
|
|
|
gst_video_multiview_mode_to_caps_string (GST_VIDEO_INFO_MULTIVIEW_MODE
|
|
|
|
(info));
|
|
|
|
|
|
|
|
gst_caps_set_simple (state->caps, "multiview-mode", G_TYPE_STRING,
|
|
|
|
caps_mview_mode, "multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET,
|
|
|
|
GST_VIDEO_INFO_MULTIVIEW_FLAGS (info), GST_FLAG_SET_MASK_EXACT, NULL);
|
|
|
|
}
|
2019-05-02 05:24:27 +00:00
|
|
|
|
|
|
|
incaps = gst_pad_get_current_caps (GST_VIDEO_ENCODER_SINK_PAD (encoder));
|
|
|
|
if (incaps) {
|
|
|
|
GstStructure *in_struct;
|
|
|
|
GstStructure *out_struct;
|
|
|
|
const gchar *s;
|
|
|
|
|
|
|
|
in_struct = gst_caps_get_structure (incaps, 0);
|
|
|
|
out_struct = gst_caps_get_structure (state->caps, 0);
|
|
|
|
|
|
|
|
/* forward upstream mastering display info and content light level
|
|
|
|
* if subclass didn't set */
|
|
|
|
if ((s = gst_structure_get_string (in_struct, "mastering-display-info"))
|
|
|
|
&& !gst_structure_has_field (out_struct, "mastering-display-info")) {
|
|
|
|
gst_caps_set_simple (state->caps, "mastering-display-info",
|
|
|
|
G_TYPE_STRING, s, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s = gst_structure_get_string (in_struct, "content-light-level")) &&
|
|
|
|
!gst_structure_has_field (out_struct, "content-light-level")) {
|
|
|
|
gst_caps_set_simple (state->caps,
|
|
|
|
"content-light-level", G_TYPE_STRING, s, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_caps_unref (incaps);
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
encoder->priv->output_state_changed = FALSE;
|
|
|
|
}
|
|
|
|
|
2016-03-31 13:31:31 +00:00
|
|
|
if (state->allocation_caps == NULL)
|
|
|
|
state->allocation_caps = gst_caps_ref (state->caps);
|
|
|
|
|
2013-05-08 13:50:34 +00:00
|
|
|
/* Push all pending pre-caps events of the oldest frame before
|
|
|
|
* setting caps */
|
2020-06-03 11:37:00 +00:00
|
|
|
frame = encoder->priv->frames.head ? encoder->priv->frames.head->data : NULL;
|
2013-05-09 13:42:49 +00:00
|
|
|
if (frame || encoder->priv->current_frame_events) {
|
|
|
|
GList **events, *l;
|
2013-05-08 13:50:34 +00:00
|
|
|
|
2013-05-09 13:42:49 +00:00
|
|
|
if (frame) {
|
|
|
|
events = &frame->events;
|
|
|
|
} else {
|
|
|
|
events = &encoder->priv->current_frame_events;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (l = g_list_last (*events); l;) {
|
2013-05-08 13:50:34 +00:00
|
|
|
GstEvent *event = GST_EVENT (l->data);
|
2013-05-09 13:42:49 +00:00
|
|
|
GList *tmp;
|
2013-05-08 13:50:34 +00:00
|
|
|
|
2013-06-27 14:33:15 +00:00
|
|
|
if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
|
|
|
|
gst_video_encoder_push_event (encoder, event);
|
|
|
|
tmp = l;
|
|
|
|
l = l->prev;
|
|
|
|
*events = g_list_delete_link (*events, tmp);
|
|
|
|
} else {
|
|
|
|
l = l->prev;
|
2013-05-08 13:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 17:20:03 +00:00
|
|
|
prevcaps = gst_pad_get_current_caps (encoder->srcpad);
|
|
|
|
if (!prevcaps || !gst_caps_is_equal (prevcaps, state->caps))
|
|
|
|
ret = gst_pad_set_caps (encoder->srcpad, state->caps);
|
2013-08-25 08:25:43 +00:00
|
|
|
else
|
|
|
|
ret = TRUE;
|
2013-08-23 17:20:03 +00:00
|
|
|
if (prevcaps)
|
|
|
|
gst_caps_unref (prevcaps);
|
|
|
|
|
2012-07-23 07:07:02 +00:00
|
|
|
if (!ret)
|
2012-07-23 08:04:48 +00:00
|
|
|
goto done;
|
2012-07-23 07:07:02 +00:00
|
|
|
|
2016-03-31 13:31:31 +00:00
|
|
|
query = gst_query_new_allocation (state->allocation_caps, TRUE);
|
2012-07-23 07:07:02 +00:00
|
|
|
if (!gst_pad_peer_query (encoder->srcpad, query)) {
|
|
|
|
GST_DEBUG_OBJECT (encoder, "didn't get downstream ALLOCATION hints");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (klass->decide_allocation != NULL);
|
|
|
|
ret = klass->decide_allocation (encoder, query);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
|
|
|
|
query);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
goto no_decide_allocation;
|
|
|
|
|
|
|
|
/* we got configuration from our peer or the decide_allocation method,
|
|
|
|
* parse them */
|
|
|
|
if (gst_query_get_n_allocation_params (query) > 0) {
|
|
|
|
gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
|
|
|
|
} else {
|
|
|
|
allocator = NULL;
|
|
|
|
gst_allocation_params_init (¶ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoder->priv->allocator)
|
|
|
|
gst_object_unref (encoder->priv->allocator);
|
|
|
|
encoder->priv->allocator = allocator;
|
|
|
|
encoder->priv->params = params;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (query)
|
|
|
|
gst_query_unref (query);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
return ret;
|
2012-07-23 07:07:02 +00:00
|
|
|
|
|
|
|
/* Errors */
|
|
|
|
no_decide_allocation:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (encoder, "Subclass failed to decide allocation");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 14:31:25 +00:00
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_negotiate_unlocked (GstVideoEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
if (G_LIKELY (klass->negotiate))
|
|
|
|
ret = klass->negotiate (encoder);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-09 12:39:18 +00:00
|
|
|
/**
|
|
|
|
* gst_video_encoder_negotiate:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
*
|
|
|
|
* Negotiate with downstream elements to currently configured #GstVideoCodecState.
|
2013-12-05 14:31:25 +00:00
|
|
|
* Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
|
|
|
|
* negotiate fails.
|
2012-08-09 12:39:18 +00:00
|
|
|
*
|
2017-10-03 21:31:18 +00:00
|
|
|
* Returns: %TRUE if the negotiation succeeded, else %FALSE.
|
2012-08-09 12:39:18 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_video_encoder_negotiate (GstVideoEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstVideoEncoderClass *klass;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VIDEO_ENCODER (encoder), FALSE);
|
2013-05-24 14:51:17 +00:00
|
|
|
g_return_val_if_fail (encoder->priv->output_state, FALSE);
|
2012-08-09 12:39:18 +00:00
|
|
|
|
|
|
|
klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2013-12-05 14:31:25 +00:00
|
|
|
gst_pad_check_reconfigure (encoder->srcpad);
|
|
|
|
if (klass->negotiate) {
|
2012-08-09 12:39:18 +00:00
|
|
|
ret = klass->negotiate (encoder);
|
2013-12-05 14:31:25 +00:00
|
|
|
if (!ret)
|
|
|
|
gst_pad_mark_reconfigure (encoder->srcpad);
|
|
|
|
}
|
2012-08-09 12:39:18 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-23 07:07:02 +00:00
|
|
|
/**
|
2012-07-23 08:18:41 +00:00
|
|
|
* gst_video_encoder_allocate_output_buffer:
|
2012-07-23 07:07:02 +00:00
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @size: size of the buffer
|
|
|
|
*
|
|
|
|
* Helper function that allocates a buffer to hold an encoded video frame
|
|
|
|
* for @encoder's current #GstVideoCodecState.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): allocated buffer
|
|
|
|
*/
|
|
|
|
GstBuffer *
|
2012-07-23 08:18:41 +00:00
|
|
|
gst_video_encoder_allocate_output_buffer (GstVideoEncoder * encoder, gsize size)
|
2012-07-23 07:07:02 +00:00
|
|
|
{
|
|
|
|
GstBuffer *buffer;
|
2013-12-05 14:31:25 +00:00
|
|
|
gboolean needs_reconfigure = FALSE;
|
2012-07-23 07:07:02 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (size > 0, NULL);
|
|
|
|
|
|
|
|
GST_DEBUG ("alloc src buffer");
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2013-12-05 14:31:25 +00:00
|
|
|
needs_reconfigure = gst_pad_check_reconfigure (encoder->srcpad);
|
2012-07-23 07:07:02 +00:00
|
|
|
if (G_UNLIKELY (encoder->priv->output_state_changed
|
2013-12-05 14:31:25 +00:00
|
|
|
|| (encoder->priv->output_state && needs_reconfigure))) {
|
|
|
|
if (!gst_video_encoder_negotiate_unlocked (encoder)) {
|
2013-05-24 14:51:17 +00:00
|
|
|
GST_DEBUG_OBJECT (encoder, "Failed to negotiate, fallback allocation");
|
2013-09-12 07:35:00 +00:00
|
|
|
gst_pad_mark_reconfigure (encoder->srcpad);
|
2013-05-24 14:51:17 +00:00
|
|
|
goto fallback;
|
|
|
|
}
|
|
|
|
}
|
2012-07-23 07:07:02 +00:00
|
|
|
|
|
|
|
buffer =
|
|
|
|
gst_buffer_new_allocate (encoder->priv->allocator, size,
|
|
|
|
&encoder->priv->params);
|
2013-05-24 14:51:17 +00:00
|
|
|
if (!buffer) {
|
|
|
|
GST_INFO_OBJECT (encoder, "couldn't allocate output buffer");
|
|
|
|
goto fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
fallback:
|
|
|
|
buffer = gst_buffer_new_allocate (NULL, size, NULL);
|
2012-07-23 07:07:02 +00:00
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-23 08:18:41 +00:00
|
|
|
* gst_video_encoder_allocate_output_frame:
|
2012-07-23 07:07:02 +00:00
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @frame: a #GstVideoCodecFrame
|
|
|
|
* @size: size of the buffer
|
|
|
|
*
|
|
|
|
* Helper function that allocates a buffer to hold an encoded video frame for @encoder's
|
|
|
|
* current #GstVideoCodecState. Subclass should already have configured video
|
|
|
|
* state and set src pad caps.
|
|
|
|
*
|
|
|
|
* The buffer allocated here is owned by the frame and you should only
|
|
|
|
* keep references to the frame, not the buffer.
|
|
|
|
*
|
|
|
|
* Returns: %GST_FLOW_OK if an output buffer could be allocated
|
|
|
|
*/
|
|
|
|
GstFlowReturn
|
2012-07-23 08:18:41 +00:00
|
|
|
gst_video_encoder_allocate_output_frame (GstVideoEncoder *
|
2012-07-23 07:07:02 +00:00
|
|
|
encoder, GstVideoCodecFrame * frame, gsize size)
|
|
|
|
{
|
2013-12-05 14:31:25 +00:00
|
|
|
gboolean needs_reconfigure = FALSE;
|
|
|
|
|
2012-07-23 07:07:02 +00:00
|
|
|
g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
|
|
|
|
|
2012-07-23 10:06:16 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2013-12-05 14:31:25 +00:00
|
|
|
needs_reconfigure = gst_pad_check_reconfigure (encoder->srcpad);
|
2012-07-23 07:07:02 +00:00
|
|
|
if (G_UNLIKELY (encoder->priv->output_state_changed
|
2013-12-05 14:31:25 +00:00
|
|
|
|| (encoder->priv->output_state && needs_reconfigure))) {
|
|
|
|
if (!gst_video_encoder_negotiate_unlocked (encoder)) {
|
2013-09-12 07:35:00 +00:00
|
|
|
GST_DEBUG_OBJECT (encoder, "Failed to negotiate, fallback allocation");
|
|
|
|
gst_pad_mark_reconfigure (encoder->srcpad);
|
|
|
|
}
|
|
|
|
}
|
2012-07-23 07:07:02 +00:00
|
|
|
|
2012-08-08 12:59:49 +00:00
|
|
|
GST_LOG_OBJECT (encoder, "alloc buffer size %" G_GSIZE_FORMAT, size);
|
2012-07-23 07:07:02 +00:00
|
|
|
|
|
|
|
frame->output_buffer =
|
|
|
|
gst_buffer_new_allocate (encoder->priv->allocator, size,
|
|
|
|
&encoder->priv->params);
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return frame->output_buffer ? GST_FLOW_OK : GST_FLOW_ERROR;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 14:23:14 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_release_frame (GstVideoEncoder * enc,
|
|
|
|
GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GList *link;
|
|
|
|
|
|
|
|
/* unref once from the list */
|
2020-06-03 11:37:00 +00:00
|
|
|
link = g_queue_find (&enc->priv->frames, frame);
|
2013-04-25 14:23:14 +00:00
|
|
|
if (link) {
|
|
|
|
gst_video_codec_frame_unref (frame);
|
2020-06-03 11:37:00 +00:00
|
|
|
g_queue_delete_link (&enc->priv->frames, link);
|
2013-04-25 14:23:14 +00:00
|
|
|
}
|
|
|
|
/* unref because this function takes ownership */
|
|
|
|
gst_video_codec_frame_unref (frame);
|
|
|
|
}
|
|
|
|
|
2015-06-29 11:59:25 +00:00
|
|
|
static gboolean
|
|
|
|
gst_video_encoder_transform_meta_default (GstVideoEncoder *
|
|
|
|
encoder, GstVideoCodecFrame * frame, GstMeta * meta)
|
|
|
|
{
|
|
|
|
const GstMetaInfo *info = meta->info;
|
|
|
|
const gchar *const *tags;
|
|
|
|
|
|
|
|
tags = gst_meta_api_type_get_tags (info->api);
|
|
|
|
|
|
|
|
if (!tags || (g_strv_length ((gchar **) tags) == 1
|
|
|
|
&& gst_meta_api_type_has_tag (info->api,
|
|
|
|
g_quark_from_string (GST_META_TAG_VIDEO_STR))))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstVideoEncoder *encoder;
|
|
|
|
GstVideoCodecFrame *frame;
|
|
|
|
} CopyMetaData;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
|
|
|
|
{
|
|
|
|
CopyMetaData *data = user_data;
|
|
|
|
GstVideoEncoder *encoder = data->encoder;
|
|
|
|
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
GstVideoCodecFrame *frame = data->frame;
|
|
|
|
const GstMetaInfo *info = (*meta)->info;
|
|
|
|
gboolean do_copy = FALSE;
|
|
|
|
|
2015-07-01 08:58:07 +00:00
|
|
|
if (gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
|
2015-06-29 11:59:25 +00:00
|
|
|
/* never call the transform_meta with memory specific metadata */
|
|
|
|
GST_DEBUG_OBJECT (encoder, "not copying memory specific metadata %s",
|
|
|
|
g_type_name (info->api));
|
|
|
|
do_copy = FALSE;
|
|
|
|
} else if (klass->transform_meta) {
|
|
|
|
do_copy = klass->transform_meta (encoder, frame, *meta);
|
|
|
|
GST_DEBUG_OBJECT (encoder, "transformed metadata %s: copy: %d",
|
|
|
|
g_type_name (info->api), do_copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we only copy metadata when the subclass implemented a transform_meta
|
|
|
|
* function and when it returns %TRUE */
|
2017-05-02 11:31:14 +00:00
|
|
|
if (do_copy && info->transform_func) {
|
2015-06-29 11:59:25 +00:00
|
|
|
GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
|
|
|
|
GST_DEBUG_OBJECT (encoder, "copy metadata %s", g_type_name (info->api));
|
|
|
|
/* simply copy then */
|
|
|
|
info->transform_func (frame->output_buffer, *meta, inbuf,
|
|
|
|
_gst_meta_transform_copy, ©_data);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-09-21 13:18:10 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_drop_frame (GstVideoEncoder * enc, GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = enc->priv;
|
|
|
|
GstClockTime stream_time, jitter, earliest_time, qostime, timestamp;
|
|
|
|
GstSegment *segment;
|
|
|
|
GstMessage *qos_msg;
|
|
|
|
gdouble proportion;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (enc, "dropping frame %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (frame->pts));
|
|
|
|
|
|
|
|
priv->dropped++;
|
|
|
|
|
|
|
|
/* post QoS message */
|
|
|
|
GST_OBJECT_LOCK (enc);
|
|
|
|
proportion = priv->proportion;
|
|
|
|
earliest_time = priv->earliest_time;
|
|
|
|
GST_OBJECT_UNLOCK (enc);
|
|
|
|
|
|
|
|
timestamp = frame->pts;
|
|
|
|
segment = &enc->output_segment;
|
|
|
|
if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
|
|
|
|
segment = &enc->input_segment;
|
|
|
|
stream_time =
|
|
|
|
gst_segment_to_stream_time (segment, GST_FORMAT_TIME, timestamp);
|
|
|
|
qostime = gst_segment_to_running_time (segment, GST_FORMAT_TIME, timestamp);
|
|
|
|
jitter = GST_CLOCK_DIFF (qostime, earliest_time);
|
|
|
|
qos_msg =
|
|
|
|
gst_message_new_qos (GST_OBJECT_CAST (enc), FALSE, qostime, stream_time,
|
|
|
|
timestamp, GST_CLOCK_TIME_NONE);
|
|
|
|
gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
|
|
|
|
gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
|
|
|
|
priv->processed, priv->dropped);
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (enc), qos_msg);
|
|
|
|
}
|
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_video_encoder_can_push_unlocked (GstVideoEncoder * encoder)
|
2012-03-07 09:18:49 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
2018-08-31 08:55:30 +00:00
|
|
|
gboolean needs_reconfigure;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2013-12-05 14:31:25 +00:00
|
|
|
needs_reconfigure = gst_pad_check_reconfigure (encoder->srcpad);
|
2012-07-23 09:50:11 +00:00
|
|
|
if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
|
2013-12-05 14:31:25 +00:00
|
|
|
&& needs_reconfigure))) {
|
|
|
|
if (!gst_video_encoder_negotiate_unlocked (encoder)) {
|
2013-09-12 07:35:00 +00:00
|
|
|
gst_pad_mark_reconfigure (encoder->srcpad);
|
2013-06-30 16:17:15 +00:00
|
|
|
if (GST_PAD_IS_FLUSHING (encoder->srcpad))
|
2018-08-31 08:55:30 +00:00
|
|
|
return GST_FLOW_FLUSHING;
|
2013-06-30 16:17:15 +00:00
|
|
|
else
|
2018-08-31 08:55:30 +00:00
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
2013-06-30 16:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
if (G_UNLIKELY (priv->output_state == NULL)) {
|
|
|
|
GST_ERROR_OBJECT (encoder, "Output state was not configured");
|
|
|
|
GST_ELEMENT_ERROR (encoder, LIBRARY, FAILED,
|
|
|
|
("Output state was not configured"), (NULL));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_video_encoder_push_pending_unlocked (GstVideoEncoder * encoder,
|
|
|
|
GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
|
|
|
GList *l;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
/* Push all pending events that arrived before this frame */
|
2020-06-03 11:37:00 +00:00
|
|
|
for (l = priv->frames.head; l; l = l->next) {
|
2012-03-07 09:18:49 +00:00
|
|
|
GstVideoCodecFrame *tmp = l->data;
|
|
|
|
|
|
|
|
if (tmp->events) {
|
|
|
|
GList *k;
|
|
|
|
|
|
|
|
for (k = g_list_last (tmp->events); k; k = k->prev)
|
|
|
|
gst_video_encoder_push_event (encoder, k->data);
|
|
|
|
g_list_free (tmp->events);
|
|
|
|
tmp->events = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp == frame)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-28 18:30:35 +00:00
|
|
|
gst_video_encoder_check_and_push_tags (encoder);
|
2018-08-31 08:55:30 +00:00
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_infer_dts_unlocked (GstVideoEncoder * encoder,
|
|
|
|
GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
/* DTS is expected to be monotonously increasing,
|
|
|
|
* so a good guess is the lowest unsent PTS (all being OK) */
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
|
|
|
GList *l;
|
|
|
|
GstClockTime min_ts = GST_CLOCK_TIME_NONE;
|
|
|
|
GstVideoCodecFrame *oframe = NULL;
|
|
|
|
gboolean seen_none = FALSE;
|
|
|
|
|
|
|
|
/* some maintenance regardless */
|
2020-06-03 11:37:00 +00:00
|
|
|
for (l = priv->frames.head; l; l = l->next) {
|
2018-08-31 08:55:30 +00:00
|
|
|
GstVideoCodecFrame *tmp = l->data;
|
|
|
|
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (tmp->abidata.ABI.ts)) {
|
|
|
|
seen_none = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (min_ts) || tmp->abidata.ABI.ts < min_ts) {
|
|
|
|
min_ts = tmp->abidata.ABI.ts;
|
|
|
|
oframe = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* save a ts if needed */
|
|
|
|
if (oframe && oframe != frame) {
|
|
|
|
oframe->abidata.ABI.ts = frame->abidata.ABI.ts;
|
2019-12-19 21:59:10 +00:00
|
|
|
}
|
2017-09-21 13:18:10 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
/* and set if needed */
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (frame->dts) && !seen_none) {
|
|
|
|
frame->dts = min_ts;
|
|
|
|
GST_DEBUG_OBJECT (encoder,
|
|
|
|
"no valid DTS, using oldest PTS %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (frame->pts));
|
|
|
|
}
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_send_header_unlocked (GstVideoEncoder * encoder,
|
2020-06-03 19:38:07 +00:00
|
|
|
gboolean * discont, gboolean key_unit)
|
2018-08-31 08:55:30 +00:00
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
if (G_UNLIKELY (priv->new_headers)) {
|
2020-06-03 12:06:08 +00:00
|
|
|
GList *tmp;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
GST_DEBUG_OBJECT (encoder, "Sending headers");
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
/* First make all buffers metadata-writable */
|
|
|
|
for (tmp = priv->headers; tmp; tmp = tmp->next) {
|
|
|
|
GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
|
2013-08-27 13:03:54 +00:00
|
|
|
|
2020-06-03 12:06:08 +00:00
|
|
|
tmp->data = tmpbuf = gst_buffer_make_writable (tmpbuf);
|
2019-12-19 21:59:10 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
priv->bytes += gst_buffer_get_size (tmpbuf);
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
2020-06-03 19:38:07 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (key_unit)) {
|
|
|
|
key_unit = FALSE;
|
|
|
|
GST_BUFFER_FLAG_UNSET (tmpbuf, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
}
|
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
if (G_UNLIKELY (*discont)) {
|
|
|
|
GST_LOG_OBJECT (encoder, "marking discont");
|
|
|
|
GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
*discont = FALSE;
|
2020-06-03 19:38:07 +00:00
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_UNSET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
|
2019-12-19 22:52:12 +00:00
|
|
|
}
|
2018-08-31 08:55:30 +00:00
|
|
|
|
|
|
|
gst_pad_push (encoder->srcpad, gst_buffer_ref (tmpbuf));
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
2018-08-31 08:55:30 +00:00
|
|
|
priv->new_headers = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_transform_meta_unlocked (GstVideoEncoder * encoder,
|
|
|
|
GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GstVideoEncoderClass *encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
if (encoder_class->transform_meta) {
|
|
|
|
if (G_LIKELY (frame->input_buffer)) {
|
|
|
|
CopyMetaData data;
|
|
|
|
|
|
|
|
data.encoder = encoder;
|
|
|
|
data.frame = frame;
|
|
|
|
gst_buffer_foreach_meta (frame->input_buffer, foreach_metadata, &data);
|
|
|
|
} else {
|
|
|
|
GST_FIXME_OBJECT (encoder,
|
|
|
|
"Can't copy metadata because input frame disappeared");
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
2018-08-31 08:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
static void
|
|
|
|
gst_video_encoder_send_key_unit_unlocked (GstVideoEncoder * encoder,
|
|
|
|
GstVideoCodecFrame * frame, gboolean * send_headers)
|
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
|
|
|
GstClockTime stream_time, running_time;
|
|
|
|
GstEvent *ev;
|
2020-06-03 17:17:06 +00:00
|
|
|
GList *l;
|
|
|
|
GQueue matching_fevt = G_QUEUE_INIT;
|
|
|
|
ForcedKeyUnitEvent *fevt;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
running_time =
|
|
|
|
gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
|
|
|
|
frame->pts);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
GST_OBJECT_LOCK (encoder);
|
2020-06-03 17:17:06 +00:00
|
|
|
for (l = priv->force_key_unit.head; l;) {
|
|
|
|
fevt = l->data;
|
2019-12-19 21:59:10 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
/* Skip non-pending keyunits */
|
2020-06-03 17:17:06 +00:00
|
|
|
if (!fevt->pending) {
|
|
|
|
l = l->next;
|
2018-08-31 08:55:30 +00:00
|
|
|
continue;
|
2020-06-03 17:17:06 +00:00
|
|
|
}
|
2019-12-19 21:59:10 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
/* Exact match using the frame id */
|
2020-06-03 17:17:06 +00:00
|
|
|
if (frame->system_frame_number == fevt->frame_id) {
|
|
|
|
GList *next = l->next;
|
|
|
|
g_queue_push_tail (&matching_fevt, fevt);
|
|
|
|
g_queue_delete_link (&priv->force_key_unit, l);
|
|
|
|
l = next;
|
|
|
|
continue;
|
2018-08-31 08:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Simple case, keyunit ASAP */
|
2020-06-03 17:17:06 +00:00
|
|
|
if (fevt->running_time == GST_CLOCK_TIME_NONE) {
|
|
|
|
GList *next = l->next;
|
|
|
|
g_queue_push_tail (&matching_fevt, fevt);
|
|
|
|
g_queue_delete_link (&priv->force_key_unit, l);
|
|
|
|
l = next;
|
|
|
|
continue;
|
2019-12-19 21:59:10 +00:00
|
|
|
}
|
2018-08-31 08:55:30 +00:00
|
|
|
|
|
|
|
/* Event for before this frame */
|
2020-06-03 17:17:06 +00:00
|
|
|
if (fevt->running_time <= running_time) {
|
|
|
|
GList *next = l->next;
|
|
|
|
g_queue_push_tail (&matching_fevt, fevt);
|
|
|
|
g_queue_delete_link (&priv->force_key_unit, l);
|
|
|
|
l = next;
|
|
|
|
continue;
|
2018-08-31 08:55:30 +00:00
|
|
|
}
|
2020-06-03 17:17:06 +00:00
|
|
|
|
|
|
|
/* Otherwise all following events are in the future */
|
|
|
|
break;
|
2018-08-31 08:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
2020-06-03 17:17:06 +00:00
|
|
|
while ((fevt = g_queue_pop_head (&matching_fevt))) {
|
2018-08-31 08:55:30 +00:00
|
|
|
stream_time =
|
|
|
|
gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
|
|
|
|
frame->pts);
|
|
|
|
|
|
|
|
ev = gst_video_event_new_downstream_force_key_unit
|
|
|
|
(frame->pts, stream_time, running_time, fevt->all_headers, fevt->count);
|
|
|
|
|
|
|
|
gst_video_encoder_push_event (encoder, ev);
|
|
|
|
|
|
|
|
if (fevt->all_headers)
|
|
|
|
*send_headers = TRUE;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder,
|
|
|
|
"Forced key unit: running-time %" GST_TIME_FORMAT
|
|
|
|
", all_headers %d, count %u",
|
|
|
|
GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
|
|
|
|
forced_key_unit_event_free (fevt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_finish_frame:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @frame: (transfer full): an encoded #GstVideoCodecFrame
|
|
|
|
*
|
|
|
|
* @frame must have a valid encoded data buffer, whose metadata fields
|
|
|
|
* are then appropriately set according to frame data or no buffer at
|
|
|
|
* all if the frame should be dropped.
|
|
|
|
* It is subsequently pushed downstream or provided to @pre_push.
|
|
|
|
* In any case, the frame is considered finished and released.
|
|
|
|
*
|
|
|
|
* After calling this function the output buffer of the frame is to be
|
|
|
|
* considered read-only. This function will also change the metadata
|
|
|
|
* of the buffer.
|
|
|
|
*
|
|
|
|
* Returns: a #GstFlowReturn resulting from sending data downstream
|
|
|
|
*/
|
|
|
|
GstFlowReturn
|
|
|
|
gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
|
|
|
|
GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstVideoEncoderClass *encoder_class;
|
|
|
|
gboolean send_headers = FALSE;
|
2020-06-03 19:38:07 +00:00
|
|
|
gboolean key_unit = FALSE;
|
2018-08-31 10:09:57 +00:00
|
|
|
gboolean discont = FALSE;
|
2018-08-31 08:55:30 +00:00
|
|
|
GstBuffer *buffer;
|
|
|
|
|
2018-08-31 10:09:57 +00:00
|
|
|
g_return_val_if_fail (frame, GST_FLOW_ERROR);
|
|
|
|
|
|
|
|
discont = (frame->presentation_frame_number == 0
|
|
|
|
&& frame->abidata.ABI.num_subframes == 0);
|
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (encoder,
|
2018-08-31 10:09:57 +00:00
|
|
|
"finish frame fpn %d sync point: %d", frame->presentation_frame_number,
|
|
|
|
GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame));
|
2018-08-31 08:55:30 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (encoder, "frame PTS %" GST_TIME_FORMAT
|
|
|
|
", DTS %" GST_TIME_FORMAT, GST_TIME_ARGS (frame->pts),
|
|
|
|
GST_TIME_ARGS (frame->dts));
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
|
|
|
|
ret = gst_video_encoder_can_push_unlocked (encoder);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto done;
|
|
|
|
|
2018-08-31 10:09:57 +00:00
|
|
|
if (frame->abidata.ABI.num_subframes == 0)
|
|
|
|
gst_video_encoder_push_pending_unlocked (encoder, frame);
|
2018-08-31 08:55:30 +00:00
|
|
|
|
|
|
|
/* no buffer data means this frame is skipped/dropped */
|
|
|
|
if (!frame->output_buffer) {
|
|
|
|
gst_video_encoder_drop_frame (encoder, frame);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->processed++;
|
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit.head)
|
2018-08-31 08:55:30 +00:00
|
|
|
gst_video_encoder_send_key_unit_unlocked (encoder, frame, &send_headers);
|
|
|
|
|
2018-08-31 10:09:57 +00:00
|
|
|
if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)
|
|
|
|
&& frame->abidata.ABI.num_subframes == 0) {
|
2012-03-07 09:18:49 +00:00
|
|
|
priv->distance_from_sync = 0;
|
2020-06-03 19:38:07 +00:00
|
|
|
key_unit = TRUE;
|
2015-01-02 13:34:41 +00:00
|
|
|
/* For keyframes, DTS = PTS, if encoder doesn't decide otherwise */
|
2012-09-07 15:41:27 +00:00
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (frame->dts)) {
|
|
|
|
frame->dts = frame->pts;
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
gst_video_encoder_infer_dts_unlocked (encoder, frame);
|
2012-09-26 14:31:27 +00:00
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
frame->distance_from_sync = priv->distance_from_sync;
|
|
|
|
priv->distance_from_sync++;
|
|
|
|
|
2012-06-27 14:45:41 +00:00
|
|
|
GST_BUFFER_PTS (frame->output_buffer) = frame->pts;
|
|
|
|
GST_BUFFER_DTS (frame->output_buffer) = frame->dts;
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
|
|
|
|
|
2019-09-25 08:45:28 +00:00
|
|
|
/* At this stage we have a full frame in subframe use case ,
|
|
|
|
* let's mark it to enabled some latency optimization
|
|
|
|
* in some uses cases like RTP. */
|
|
|
|
|
|
|
|
GST_BUFFER_FLAG_SET (frame->output_buffer, GST_VIDEO_BUFFER_FLAG_MARKER);
|
|
|
|
|
2016-07-04 08:52:24 +00:00
|
|
|
GST_OBJECT_LOCK (encoder);
|
2012-03-07 09:18:49 +00:00
|
|
|
/* update rate estimate */
|
2012-04-24 17:35:24 +00:00
|
|
|
priv->bytes += gst_buffer_get_size (frame->output_buffer);
|
2012-03-07 09:18:49 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
|
|
|
|
priv->time += frame->duration;
|
|
|
|
} else {
|
|
|
|
/* better none than nothing valid */
|
|
|
|
priv->time = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
2016-07-04 08:52:24 +00:00
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
if (G_UNLIKELY (send_headers))
|
2019-09-12 10:07:08 +00:00
|
|
|
priv->new_headers = TRUE;
|
|
|
|
|
2020-06-03 19:38:07 +00:00
|
|
|
gst_video_encoder_send_header_unlocked (encoder, &discont, key_unit);
|
|
|
|
|
|
|
|
if (key_unit) {
|
|
|
|
GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (discont)) {
|
|
|
|
GST_LOG_OBJECT (encoder, "marking discont");
|
|
|
|
GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoder_class->pre_push)
|
|
|
|
ret = encoder_class->pre_push (encoder, frame);
|
|
|
|
|
2018-08-31 08:55:30 +00:00
|
|
|
gst_video_encoder_transform_meta_unlocked (encoder, frame);
|
2015-06-29 11:59:25 +00:00
|
|
|
|
2013-04-25 14:23:14 +00:00
|
|
|
/* Get an additional ref to the buffer, which is going to be pushed
|
|
|
|
* downstream, the original ref is owned by the frame */
|
2016-01-26 12:54:46 +00:00
|
|
|
if (ret == GST_FLOW_OK)
|
|
|
|
buffer = gst_buffer_ref (frame->output_buffer);
|
2013-04-25 14:23:14 +00:00
|
|
|
|
|
|
|
/* Release frame so the buffer is writable when we push it downstream
|
|
|
|
* if possible, i.e. if the subclass does not hold additional references
|
|
|
|
* to the frame
|
|
|
|
*/
|
|
|
|
gst_video_encoder_release_frame (encoder, frame);
|
|
|
|
frame = NULL;
|
|
|
|
|
2013-11-25 16:49:50 +00:00
|
|
|
if (ret == GST_FLOW_OK) {
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
2013-04-25 14:23:14 +00:00
|
|
|
ret = gst_pad_push (encoder->srcpad, buffer);
|
2013-11-25 16:49:50 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
}
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
/* handed out */
|
2013-04-25 14:23:14 +00:00
|
|
|
if (frame)
|
|
|
|
gst_video_encoder_release_frame (encoder, frame);
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-31 10:09:57 +00:00
|
|
|
/**
|
|
|
|
* gst_video_encoder_finish_subframe:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @frame: (transfer none): a #GstVideoCodecFrame being encoded
|
|
|
|
*
|
|
|
|
* If multiple subframes are produced for one input frame then use this method
|
|
|
|
* for each subframe, except for the last one. Before calling this function,
|
|
|
|
* you need to fill frame->output_buffer with the encoded buffer to push.
|
|
|
|
|
|
|
|
* You must call #gst_video_encoder_finish_frame() for the last sub-frame
|
|
|
|
* to tell the encoder that the frame has been fully encoded.
|
|
|
|
*
|
|
|
|
* This function will change the metadata of @frame and frame->output_buffer
|
|
|
|
* will be pushed downstream.
|
|
|
|
*
|
|
|
|
* Returns: a #GstFlowReturn resulting from pushing the buffer downstream.
|
|
|
|
*
|
|
|
|
* Since: 1.18
|
|
|
|
*/
|
|
|
|
GstFlowReturn
|
|
|
|
gst_video_encoder_finish_subframe (GstVideoEncoder * encoder,
|
|
|
|
GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
|
|
|
GstVideoEncoderClass *encoder_class;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstBuffer *subframe_buffer = NULL;
|
|
|
|
gboolean discont = FALSE;
|
|
|
|
gboolean send_headers = FALSE;
|
2020-06-03 19:38:07 +00:00
|
|
|
gboolean key_unit = FALSE;
|
2018-08-31 10:09:57 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (frame, GST_FLOW_ERROR);
|
|
|
|
g_return_val_if_fail (frame->output_buffer, GST_FLOW_ERROR);
|
|
|
|
|
|
|
|
subframe_buffer = frame->output_buffer;
|
2020-02-06 08:52:31 +00:00
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2018-08-31 10:09:57 +00:00
|
|
|
discont = (frame->presentation_frame_number == 0
|
|
|
|
&& frame->abidata.ABI.num_subframes == 0);
|
|
|
|
|
|
|
|
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (encoder,
|
|
|
|
"finish subframe %u of frame fpn %u PTS %" GST_TIME_FORMAT ", DTS %"
|
|
|
|
GST_TIME_FORMAT " sync point: %d", frame->abidata.ABI.num_subframes,
|
|
|
|
frame->presentation_frame_number, GST_TIME_ARGS (frame->pts),
|
|
|
|
GST_TIME_ARGS (frame->dts), GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame));
|
|
|
|
|
|
|
|
ret = gst_video_encoder_can_push_unlocked (encoder);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto done;
|
|
|
|
|
2020-06-03 11:37:00 +00:00
|
|
|
if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit.head)
|
2018-08-31 10:09:57 +00:00
|
|
|
gst_video_encoder_send_key_unit_unlocked (encoder, frame, &send_headers);
|
|
|
|
|
|
|
|
/* Push pending events only for the first subframe ie segment event.
|
|
|
|
* Push new incoming events on finish_frame otherwise.
|
|
|
|
*/
|
|
|
|
if (frame->abidata.ABI.num_subframes == 0)
|
|
|
|
gst_video_encoder_push_pending_unlocked (encoder, frame);
|
|
|
|
|
|
|
|
if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)
|
|
|
|
&& frame->abidata.ABI.num_subframes == 0) {
|
2020-06-03 19:38:07 +00:00
|
|
|
priv->distance_from_sync = 0;
|
|
|
|
key_unit = TRUE;
|
2018-08-31 10:09:57 +00:00
|
|
|
/* For keyframes, DTS = PTS, if encoder doesn't decide otherwise */
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (frame->dts)) {
|
|
|
|
frame->dts = frame->pts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_encoder_infer_dts_unlocked (encoder, frame);
|
|
|
|
|
|
|
|
GST_BUFFER_PTS (subframe_buffer) = frame->pts;
|
|
|
|
GST_BUFFER_DTS (subframe_buffer) = frame->dts;
|
|
|
|
GST_BUFFER_DURATION (subframe_buffer) = frame->duration;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
/* update rate estimate */
|
|
|
|
priv->bytes += gst_buffer_get_size (subframe_buffer);
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
|
|
|
if (G_UNLIKELY (send_headers))
|
|
|
|
priv->new_headers = TRUE;
|
|
|
|
|
2020-06-03 19:38:07 +00:00
|
|
|
gst_video_encoder_send_header_unlocked (encoder, &discont, key_unit);
|
|
|
|
|
|
|
|
if (key_unit) {
|
|
|
|
GST_BUFFER_FLAG_UNSET (subframe_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_SET (subframe_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
}
|
2018-08-31 10:09:57 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (discont)) {
|
|
|
|
GST_LOG_OBJECT (encoder, "marking discont buffer: %" GST_PTR_FORMAT,
|
|
|
|
subframe_buffer);
|
|
|
|
GST_BUFFER_FLAG_SET (subframe_buffer, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoder_class->pre_push) {
|
|
|
|
ret = encoder_class->pre_push (encoder, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_encoder_transform_meta_unlocked (encoder, frame);
|
|
|
|
|
|
|
|
if (ret == GST_FLOW_OK) {
|
2020-02-05 10:58:14 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
2018-08-31 10:09:57 +00:00
|
|
|
ret = gst_pad_push (encoder->srcpad, subframe_buffer);
|
|
|
|
subframe_buffer = NULL;
|
2020-02-05 10:58:14 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2018-08-31 10:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
frame->abidata.ABI.num_subframes++;
|
|
|
|
if (subframe_buffer)
|
|
|
|
gst_buffer_unref (subframe_buffer);
|
|
|
|
frame->output_buffer = NULL;
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
/**
|
|
|
|
* gst_video_encoder_get_output_state:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
*
|
|
|
|
* Get the current #GstVideoCodecState
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): #GstVideoCodecState describing format of video data.
|
|
|
|
*/
|
|
|
|
GstVideoCodecState *
|
|
|
|
gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstVideoCodecState *state;
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
state = gst_video_codec_state_ref (encoder->priv->output_state);
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_set_output_state:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @caps: (transfer full): the #GstCaps to use for the output
|
|
|
|
* @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
|
|
|
|
*
|
|
|
|
* Creates a new #GstVideoCodecState with the specified caps as the output state
|
|
|
|
* for the encoder.
|
2012-05-02 17:50:45 +00:00
|
|
|
* Any previously set output state on @encoder will be replaced by the newly
|
2012-03-07 09:18:49 +00:00
|
|
|
* created one.
|
|
|
|
*
|
|
|
|
* The specified @caps should not contain any resolution, pixel-aspect-ratio,
|
|
|
|
* framerate, codec-data, .... Those should be specified instead in the returned
|
|
|
|
* #GstVideoCodecState.
|
|
|
|
*
|
|
|
|
* If the subclass wishes to copy over existing fields (like pixel aspect ratio,
|
|
|
|
* or framerate) from an existing #GstVideoCodecState, it can be provided as a
|
|
|
|
* @reference.
|
|
|
|
*
|
|
|
|
* If the subclass wishes to override some fields from the output state (like
|
|
|
|
* pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
|
|
|
|
*
|
|
|
|
* The new output state will only take effect (set on pads and buffers) starting
|
|
|
|
* from the next call to #gst_video_encoder_finish_frame().
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): the newly configured output state.
|
|
|
|
*/
|
|
|
|
GstVideoCodecState *
|
|
|
|
gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
|
|
|
|
GstVideoCodecState * reference)
|
|
|
|
{
|
|
|
|
GstVideoEncoderPrivate *priv = encoder->priv;
|
|
|
|
GstVideoCodecState *state;
|
|
|
|
|
|
|
|
g_return_val_if_fail (caps != NULL, NULL);
|
|
|
|
|
|
|
|
state = _new_output_state (caps, reference);
|
2016-11-23 18:10:34 +00:00
|
|
|
if (!state)
|
|
|
|
return NULL;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
|
|
|
if (priv->output_state)
|
|
|
|
gst_video_codec_state_unref (priv->output_state);
|
|
|
|
priv->output_state = gst_video_codec_state_ref (state);
|
|
|
|
|
2017-09-21 13:18:10 +00:00
|
|
|
if (priv->output_state != NULL && priv->output_state->info.fps_n > 0) {
|
|
|
|
priv->qos_frame_duration =
|
|
|
|
gst_util_uint64_scale (GST_SECOND, priv->output_state->info.fps_d,
|
|
|
|
priv->output_state->info.fps_n);
|
|
|
|
} else {
|
|
|
|
priv->qos_frame_duration = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:18:49 +00:00
|
|
|
priv->output_state_changed = TRUE;
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_set_latency:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @min_latency: minimum latency
|
|
|
|
* @max_latency: maximum latency
|
|
|
|
*
|
|
|
|
* Informs baseclass of encoding latency.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_encoder_set_latency (GstVideoEncoder * encoder,
|
|
|
|
GstClockTime min_latency, GstClockTime max_latency)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
|
|
|
|
g_return_if_fail (max_latency >= min_latency);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
encoder->priv->min_latency = min_latency;
|
|
|
|
encoder->priv->max_latency = max_latency;
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (encoder),
|
|
|
|
gst_message_new_latency (GST_OBJECT_CAST (encoder)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_get_latency:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
2012-05-16 11:40:07 +00:00
|
|
|
* @min_latency: (out) (allow-none): address of variable in which to store the
|
|
|
|
* configured minimum latency, or %NULL
|
|
|
|
* @max_latency: (out) (allow-none): address of variable in which to store the
|
|
|
|
* configured maximum latency, or %NULL
|
2012-03-07 09:18:49 +00:00
|
|
|
*
|
2012-05-16 11:40:07 +00:00
|
|
|
* Query the configured encoding latency. Results will be returned via
|
|
|
|
* @min_latency and @max_latency.
|
2012-03-07 09:18:49 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_encoder_get_latency (GstVideoEncoder * encoder,
|
|
|
|
GstClockTime * min_latency, GstClockTime * max_latency)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
if (min_latency)
|
|
|
|
*min_latency = encoder->priv->min_latency;
|
|
|
|
if (max_latency)
|
|
|
|
*max_latency = encoder->priv->max_latency;
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_get_oldest_frame:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
*
|
|
|
|
* Get the oldest unfinished pending #GstVideoCodecFrame
|
|
|
|
*
|
2012-05-01 14:12:40 +00:00
|
|
|
* Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
|
2012-03-07 09:18:49 +00:00
|
|
|
*/
|
|
|
|
GstVideoCodecFrame *
|
|
|
|
gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
|
|
|
|
{
|
2012-05-01 14:12:40 +00:00
|
|
|
GstVideoCodecFrame *frame = NULL;
|
2012-03-07 09:18:49 +00:00
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2020-06-03 11:37:00 +00:00
|
|
|
if (encoder->priv->frames.head)
|
|
|
|
frame = gst_video_codec_frame_ref (encoder->priv->frames.head->data);
|
2012-03-07 09:18:49 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
2012-05-01 14:12:40 +00:00
|
|
|
return (GstVideoCodecFrame *) frame;
|
2012-03-07 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_get_frame:
|
2018-04-02 06:34:58 +00:00
|
|
|
* @encoder: a #GstVideoEncoder
|
2012-03-07 09:18:49 +00:00
|
|
|
* @frame_number: system_frame_number of a frame
|
|
|
|
*
|
|
|
|
* Get a pending unfinished #GstVideoCodecFrame
|
2017-01-23 19:36:11 +00:00
|
|
|
*
|
2012-06-19 08:25:00 +00:00
|
|
|
* Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
|
2012-03-07 09:18:49 +00:00
|
|
|
*/
|
|
|
|
GstVideoCodecFrame *
|
|
|
|
gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
|
|
|
|
{
|
|
|
|
GList *g;
|
|
|
|
GstVideoCodecFrame *frame = NULL;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2020-06-03 11:37:00 +00:00
|
|
|
for (g = encoder->priv->frames.head; g; g = g->next) {
|
2012-03-07 09:18:49 +00:00
|
|
|
GstVideoCodecFrame *tmp = g->data;
|
|
|
|
|
|
|
|
if (tmp->system_frame_number == frame_number) {
|
2012-06-19 08:25:00 +00:00
|
|
|
frame = gst_video_codec_frame_ref (tmp);
|
2012-03-07 09:18:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
2012-08-09 14:02:15 +00:00
|
|
|
|
2012-08-16 10:12:06 +00:00
|
|
|
/**
|
|
|
|
* gst_video_encoder_get_frames:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
*
|
|
|
|
* Get all pending unfinished #GstVideoCodecFrame
|
2017-01-23 19:36:11 +00:00
|
|
|
*
|
2012-08-16 10:12:06 +00:00
|
|
|
* Returns: (transfer full) (element-type GstVideoCodecFrame): pending unfinished #GstVideoCodecFrame.
|
|
|
|
*/
|
|
|
|
GList *
|
|
|
|
gst_video_encoder_get_frames (GstVideoEncoder * encoder)
|
|
|
|
{
|
|
|
|
GList *frames;
|
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2020-06-03 11:37:00 +00:00
|
|
|
frames =
|
|
|
|
g_list_copy_deep (encoder->priv->frames.head,
|
|
|
|
(GCopyFunc) gst_video_codec_frame_ref, NULL);
|
2012-08-16 10:12:06 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
2012-08-09 14:02:15 +00:00
|
|
|
/**
|
|
|
|
* gst_video_encoder_merge_tags:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
2015-08-18 11:29:29 +00:00
|
|
|
* @tags: (allow-none): a #GstTagList to merge, or NULL to unset
|
|
|
|
* previously-set tags
|
|
|
|
* @mode: the #GstTagMergeMode to use, usually #GST_TAG_MERGE_REPLACE
|
2012-08-09 14:02:15 +00:00
|
|
|
*
|
2015-08-18 11:29:29 +00:00
|
|
|
* Sets the video encoder tags and how they should be merged with any
|
|
|
|
* upstream stream tags. This will override any tags previously-set
|
|
|
|
* with gst_video_encoder_merge_tags().
|
2012-08-09 14:02:15 +00:00
|
|
|
*
|
|
|
|
* Note that this is provided for convenience, and the subclass is
|
|
|
|
* not required to use this and can still do tag handling on its own.
|
|
|
|
*
|
|
|
|
* MT safe.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_encoder_merge_tags (GstVideoEncoder * encoder,
|
|
|
|
const GstTagList * tags, GstTagMergeMode mode)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
|
|
|
|
g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
|
2015-08-18 11:29:29 +00:00
|
|
|
g_return_if_fail (tags == NULL || mode != GST_TAG_MERGE_UNDEFINED);
|
2012-08-09 14:02:15 +00:00
|
|
|
|
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
|
2015-08-18 11:29:29 +00:00
|
|
|
if (encoder->priv->tags != tags) {
|
|
|
|
if (encoder->priv->tags) {
|
|
|
|
gst_tag_list_unref (encoder->priv->tags);
|
|
|
|
encoder->priv->tags = NULL;
|
|
|
|
encoder->priv->tags_merge_mode = GST_TAG_MERGE_APPEND;
|
|
|
|
}
|
|
|
|
if (tags) {
|
|
|
|
encoder->priv->tags = gst_tag_list_ref ((GstTagList *) tags);
|
|
|
|
encoder->priv->tags_merge_mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (encoder, "setting encoder tags to %" GST_PTR_FORMAT,
|
|
|
|
tags);
|
|
|
|
encoder->priv->tags_changed = TRUE;
|
|
|
|
}
|
2012-08-09 14:02:15 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
|
|
|
|
}
|
2012-08-07 15:19:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_get_allocator:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @allocator: (out) (allow-none) (transfer full): the #GstAllocator
|
|
|
|
* used
|
|
|
|
* @params: (out) (allow-none) (transfer full): the
|
2018-04-02 06:34:58 +00:00
|
|
|
* #GstAllocationParams of @allocator
|
2012-08-07 15:19:05 +00:00
|
|
|
*
|
|
|
|
* Lets #GstVideoEncoder sub-classes to know the memory @allocator
|
|
|
|
* used by the base class and its @params.
|
|
|
|
*
|
|
|
|
* Unref the @allocator after use it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_encoder_get_allocator (GstVideoEncoder * encoder,
|
|
|
|
GstAllocator ** allocator, GstAllocationParams * params)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
|
|
|
|
|
|
|
|
if (allocator)
|
|
|
|
*allocator = encoder->priv->allocator ?
|
|
|
|
gst_object_ref (encoder->priv->allocator) : NULL;
|
|
|
|
|
|
|
|
if (params)
|
|
|
|
*params = encoder->priv->params;
|
|
|
|
}
|
2014-11-26 20:06:57 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-13 12:41:35 +00:00
|
|
|
* gst_video_encoder_set_min_pts:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @min_pts: minimal PTS that will be passed to handle_frame
|
|
|
|
*
|
2014-11-26 20:06:57 +00:00
|
|
|
* Request minimal value for PTS passed to handle_frame.
|
|
|
|
*
|
|
|
|
* For streams with reordered frames this can be used to ensure that there
|
2019-08-29 17:42:39 +00:00
|
|
|
* is enough time to accommodate first DTS, which may be less than first PTS
|
2014-11-26 20:06:57 +00:00
|
|
|
*
|
2019-04-23 12:05:43 +00:00
|
|
|
* Since: 1.6
|
2014-11-26 20:06:57 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_encoder_set_min_pts (GstVideoEncoder * encoder, GstClockTime min_pts)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
|
|
|
|
encoder->priv->min_pts = min_pts;
|
|
|
|
encoder->priv->time_adjustment = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
2017-09-21 13:18:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_get_max_encode_time:
|
|
|
|
* @encoder: a #GstVideoEncoder
|
|
|
|
* @frame: a #GstVideoCodecFrame
|
|
|
|
*
|
|
|
|
* Determines maximum possible encoding time for @frame that will
|
|
|
|
* allow it to encode and arrive in time (as determined by QoS events).
|
|
|
|
* In particular, a negative result means encoding in time is no longer possible
|
|
|
|
* and should therefore occur as soon/skippy as possible.
|
|
|
|
*
|
2017-10-25 09:41:02 +00:00
|
|
|
* If no QoS events have been received from downstream, or if
|
|
|
|
* #GstVideoEncoder:qos is disabled this function returns #G_MAXINT64.
|
|
|
|
*
|
2017-09-21 13:18:10 +00:00
|
|
|
* Returns: max decoding time.
|
|
|
|
* Since: 1.14
|
|
|
|
*/
|
|
|
|
GstClockTimeDiff
|
|
|
|
gst_video_encoder_get_max_encode_time (GstVideoEncoder *
|
|
|
|
encoder, GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GstClockTimeDiff deadline;
|
|
|
|
GstClockTime earliest_time;
|
|
|
|
|
2017-10-25 09:41:02 +00:00
|
|
|
if (!g_atomic_int_get (&encoder->priv->qos_enabled))
|
|
|
|
return G_MAXINT64;
|
|
|
|
|
2017-09-21 13:18:10 +00:00
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
earliest_time = encoder->priv->earliest_time;
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (earliest_time)
|
|
|
|
&& GST_CLOCK_TIME_IS_VALID (frame->deadline))
|
|
|
|
deadline = GST_CLOCK_DIFF (earliest_time, frame->deadline);
|
|
|
|
else
|
|
|
|
deadline = G_MAXINT64;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (encoder, "earliest %" GST_TIME_FORMAT
|
|
|
|
", frame deadline %" GST_TIME_FORMAT ", deadline %" GST_STIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (earliest_time), GST_TIME_ARGS (frame->deadline),
|
|
|
|
GST_STIME_ARGS (deadline));
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return deadline;
|
|
|
|
}
|
2017-10-25 09:41:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_set_qos_enabled:
|
|
|
|
* @encoder: the encoder
|
|
|
|
* @enabled: the new qos value.
|
|
|
|
*
|
|
|
|
* Configures @encoder to handle Quality-of-Service events from downstream.
|
|
|
|
* Since: 1.14
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_encoder_set_qos_enabled (GstVideoEncoder * encoder, gboolean enabled)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
|
|
|
|
|
|
|
|
g_atomic_int_set (&encoder->priv->qos_enabled, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_is_qos_enabled:
|
|
|
|
* @encoder: the encoder
|
|
|
|
*
|
|
|
|
* Checks if @encoder is currently configured to handle Quality-of-Service
|
|
|
|
* events from downstream.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the encoder is configured to perform Quality-of-Service.
|
|
|
|
* Since: 1.14
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_video_encoder_is_qos_enabled (GstVideoEncoder * encoder)
|
|
|
|
{
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VIDEO_ENCODER (encoder), FALSE);
|
|
|
|
|
|
|
|
res = g_atomic_int_get (&encoder->priv->qos_enabled);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2020-06-03 18:46:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_set_min_force_key_unit_interval:
|
|
|
|
* @encoder: the encoder
|
|
|
|
* @interval: minimum interval
|
|
|
|
*
|
|
|
|
* Sets the minimum interval for requesting keyframes based on force-keyunit
|
|
|
|
* events. Setting this to 0 will allow to handle every event, setting this to
|
|
|
|
* %GST_CLOCK_TIME_NONE causes force-keyunit events to be ignored.
|
|
|
|
*
|
|
|
|
* Since: 1.18
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_encoder_set_min_force_key_unit_interval (GstVideoEncoder * encoder,
|
|
|
|
GstClockTime interval)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
encoder->priv->min_force_key_unit_interval = interval;
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_encoder_get_min_force_key_unit_interval:
|
|
|
|
* @encoder: the encoder
|
|
|
|
*
|
|
|
|
* Returns the minimum force-keyunit interval, see gst_video_encoder_set_min_force_key_unit_interval()
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* Returns: the minimum force-keyunit interval
|
|
|
|
*
|
|
|
|
* Since: 1.18
|
|
|
|
*/
|
|
|
|
GstClockTime
|
|
|
|
gst_video_encoder_get_min_force_key_unit_interval (GstVideoEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstClockTime interval;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VIDEO_ENCODER (encoder), GST_CLOCK_TIME_NONE);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (encoder);
|
|
|
|
interval = encoder->priv->min_force_key_unit_interval;
|
|
|
|
GST_OBJECT_UNLOCK (encoder);
|
|
|
|
|
|
|
|
return interval;
|
|
|
|
}
|