2010-10-27 17:30:11 +00:00
|
|
|
/*
|
2013-02-16 01:44:19 +00:00
|
|
|
* Copyright (C) 2010, 2013 Ole André Vadla Ravnås <oleavr@soundrop.com>
|
2013-10-02 02:49:34 +00:00
|
|
|
* Copyright (C) 2013 Intel Corporation
|
2010-10-27 17:30:11 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-10-27 17:30:11 +00:00
|
|
|
*/
|
2014-09-19 11:36:39 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
#include "vtenc.h"
|
|
|
|
|
|
|
|
#include "coremediabuffer.h"
|
2014-09-19 11:36:39 +00:00
|
|
|
#include "corevideobuffer.h"
|
2010-10-27 17:30:11 +00:00
|
|
|
#include "vtutil.h"
|
2014-11-27 12:33:07 +00:00
|
|
|
#include <gst/pbutils/codec-utils.h>
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
#define VTENC_DEFAULT_USAGE 6 /* Profile: Baseline Level: 2.1 */
|
2014-11-21 14:46:49 +00:00
|
|
|
#define VTENC_DEFAULT_BITRATE 0
|
2014-11-06 12:22:17 +00:00
|
|
|
#define VTENC_DEFAULT_FRAME_REORDERING TRUE
|
|
|
|
#define VTENC_DEFAULT_REALTIME FALSE
|
2014-11-27 12:40:56 +00:00
|
|
|
#define VTENC_DEFAULT_QUALITY 0.5
|
2014-12-05 15:33:20 +00:00
|
|
|
#define VTENC_DEFAULT_MAX_KEYFRAME_INTERVAL 0
|
|
|
|
#define VTENC_DEFAULT_MAX_KEYFRAME_INTERVAL_DURATION 0
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY (gst_vtenc_debug);
|
|
|
|
#define GST_CAT_DEFAULT (gst_vtenc_debug)
|
|
|
|
|
|
|
|
#define GST_VTENC_CODEC_DETAILS_QDATA \
|
|
|
|
g_quark_from_static_string ("vtenc-codec-details")
|
|
|
|
|
2014-11-12 08:59:53 +00:00
|
|
|
/* define EnableHardwareAcceleratedVideoEncoder in < 10.9 */
|
|
|
|
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED < 1090
|
|
|
|
const CFStringRef
|
|
|
|
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder =
|
|
|
|
CFSTR ("EnableHardwareAcceleratedVideoEncoder");
|
2016-06-01 11:43:32 +00:00
|
|
|
const CFStringRef
|
|
|
|
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder =
|
|
|
|
CFSTR ("RequireHardwareAcceleratedVideoEncoder");
|
2014-11-20 15:04:34 +00:00
|
|
|
const CFStringRef kVTCompressionPropertyKey_ProfileLevel =
|
|
|
|
CFSTR ("ProfileLevel");
|
|
|
|
const CFStringRef kVTProfileLevel_H264_Baseline_AutoLevel =
|
|
|
|
CFSTR ("H264_Baseline_AutoLevel");
|
2014-11-12 08:59:53 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-27 12:40:56 +00:00
|
|
|
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED < 1080
|
|
|
|
const CFStringRef kVTCompressionPropertyKey_Quality = CFSTR ("Quality");
|
|
|
|
#endif
|
|
|
|
|
2016-04-20 12:07:28 +00:00
|
|
|
#ifdef HAVE_VIDEOTOOLBOX_10_9_6
|
2016-04-21 01:06:40 +00:00
|
|
|
extern OSStatus
|
2016-04-20 12:07:28 +00:00
|
|
|
VTCompressionSessionPrepareToEncodeFrames (VTCompressionSessionRef session)
|
|
|
|
__attribute__ ((weak_import));
|
|
|
|
#endif
|
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_USAGE,
|
2014-11-06 12:22:17 +00:00
|
|
|
PROP_BITRATE,
|
|
|
|
PROP_ALLOW_FRAME_REORDERING,
|
2014-11-27 12:40:56 +00:00
|
|
|
PROP_REALTIME,
|
2014-12-05 15:33:20 +00:00
|
|
|
PROP_QUALITY,
|
|
|
|
PROP_MAX_KEYFRAME_INTERVAL,
|
|
|
|
PROP_MAX_KEYFRAME_INTERVAL_DURATION
|
2010-10-27 17:30:11 +00:00
|
|
|
};
|
|
|
|
|
2013-02-16 01:37:11 +00:00
|
|
|
typedef struct _GstVTEncFrame GstVTEncFrame;
|
|
|
|
|
|
|
|
struct _GstVTEncFrame
|
|
|
|
{
|
|
|
|
GstBuffer *buf;
|
2013-10-02 02:49:34 +00:00
|
|
|
GstVideoFrame videoframe;
|
2013-02-16 01:37:11 +00:00
|
|
|
};
|
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
|
|
|
|
static void gst_vtenc_get_property (GObject * obj, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_vtenc_set_property (GObject * obj, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
2015-04-25 19:55:28 +00:00
|
|
|
static void gst_vtenc_finalize (GObject * obj);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
static gboolean gst_vtenc_start (GstVideoEncoder * enc);
|
|
|
|
static gboolean gst_vtenc_stop (GstVideoEncoder * enc);
|
|
|
|
static gboolean gst_vtenc_set_format (GstVideoEncoder * enc,
|
|
|
|
GstVideoCodecState * input_state);
|
|
|
|
static GstFlowReturn gst_vtenc_handle_frame (GstVideoEncoder * enc,
|
|
|
|
GstVideoCodecFrame * frame);
|
2014-09-17 14:08:57 +00:00
|
|
|
static GstFlowReturn gst_vtenc_finish (GstVideoEncoder * enc);
|
2014-09-16 13:51:28 +00:00
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
static void gst_vtenc_clear_cached_caps_downstream (GstVTEnc * self);
|
|
|
|
|
2010-11-02 22:08:30 +00:00
|
|
|
static VTCompressionSessionRef gst_vtenc_create_session (GstVTEnc * self);
|
2010-10-27 17:30:11 +00:00
|
|
|
static void gst_vtenc_destroy_session (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef * session);
|
2010-10-27 17:30:11 +00:00
|
|
|
static void gst_vtenc_session_dump_properties (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session);
|
2010-10-27 17:30:11 +00:00
|
|
|
static void gst_vtenc_session_configure_expected_framerate (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, gdouble framerate);
|
2010-10-27 17:30:11 +00:00
|
|
|
static void gst_vtenc_session_configure_max_keyframe_interval (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, gint interval);
|
2010-10-27 17:30:11 +00:00
|
|
|
static void gst_vtenc_session_configure_max_keyframe_interval_duration
|
2010-11-02 22:08:30 +00:00
|
|
|
(GstVTEnc * self, VTCompressionSessionRef session, gdouble duration);
|
2010-10-27 17:30:11 +00:00
|
|
|
static void gst_vtenc_session_configure_bitrate (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, guint bitrate);
|
2014-09-16 12:02:46 +00:00
|
|
|
static OSStatus gst_vtenc_session_configure_property_int (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, CFStringRef name, gint value);
|
2014-09-16 12:02:46 +00:00
|
|
|
static OSStatus gst_vtenc_session_configure_property_double (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, CFStringRef name, gdouble value);
|
2014-11-06 12:22:17 +00:00
|
|
|
static void gst_vtenc_session_configure_allow_frame_reordering (GstVTEnc * self,
|
|
|
|
VTCompressionSessionRef session, gboolean allow_frame_reordering);
|
|
|
|
static void gst_vtenc_session_configure_realtime (GstVTEnc * self,
|
|
|
|
VTCompressionSessionRef session, gboolean realtime);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
static GstFlowReturn gst_vtenc_encode_frame (GstVTEnc * self,
|
|
|
|
GstVideoCodecFrame * frame);
|
2014-09-16 12:02:46 +00:00
|
|
|
static void gst_vtenc_enqueue_buffer (void *outputCallbackRefCon,
|
2014-09-16 13:51:28 +00:00
|
|
|
void *sourceFrameRefCon, OSStatus status, VTEncodeInfoFlags infoFlags,
|
|
|
|
CMSampleBufferRef sampleBuffer);
|
2010-10-27 17:30:11 +00:00
|
|
|
static gboolean gst_vtenc_buffer_is_keyframe (GstVTEnc * self,
|
2010-11-02 21:53:33 +00:00
|
|
|
CMSampleBufferRef sbuf);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-11-06 12:22:17 +00:00
|
|
|
|
2014-09-19 11:36:39 +00:00
|
|
|
#ifndef HAVE_IOS
|
2013-10-02 02:49:34 +00:00
|
|
|
static GstVTEncFrame *gst_vtenc_frame_new (GstBuffer * buf,
|
|
|
|
GstVideoInfo * videoinfo);
|
2013-02-16 01:37:11 +00:00
|
|
|
static void gst_vtenc_frame_free (GstVTEncFrame * frame);
|
|
|
|
|
2014-06-16 10:35:13 +00:00
|
|
|
static void gst_pixel_buffer_release_cb (void *releaseRefCon,
|
|
|
|
const void *dataPtr, size_t dataSize, size_t numberOfPlanes,
|
|
|
|
const void *planeAddresses[]);
|
2014-09-19 11:36:39 +00:00
|
|
|
#endif
|
2014-06-16 10:35:13 +00:00
|
|
|
|
2016-01-06 00:40:38 +00:00
|
|
|
#ifdef HAVE_IOS
|
2013-10-02 02:49:43 +00:00
|
|
|
static GstStaticCaps sink_caps =
|
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ NV12, I420 }"));
|
2016-01-06 00:40:38 +00:00
|
|
|
#else
|
|
|
|
static GstStaticCaps sink_caps =
|
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ UYVY, NV12, I420 }"));
|
|
|
|
#endif
|
2013-10-02 02:49:43 +00:00
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
static void
|
|
|
|
gst_vtenc_base_init (GstVTEncClass * klass)
|
|
|
|
{
|
|
|
|
const GstVTEncoderDetails *codec_details =
|
|
|
|
GST_VTENC_CLASS_GET_CODEC_DETAILS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
const int min_width = 1, max_width = G_MAXINT;
|
|
|
|
const int min_height = 1, max_height = G_MAXINT;
|
|
|
|
const int min_fps_n = 0, max_fps_n = G_MAXINT;
|
|
|
|
const int min_fps_d = 1, max_fps_d = 1;
|
|
|
|
GstPadTemplate *sink_template, *src_template;
|
|
|
|
GstCaps *src_caps;
|
2013-02-16 01:37:11 +00:00
|
|
|
gchar *longname, *description;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-02-16 01:37:11 +00:00
|
|
|
longname = g_strdup_printf ("%s encoder", codec_details->name);
|
|
|
|
description = g_strdup_printf ("%s encoder", codec_details->name);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-02-16 01:37:11 +00:00
|
|
|
gst_element_class_set_metadata (element_class, longname,
|
|
|
|
"Codec/Encoder/Video", description,
|
2013-10-02 02:49:34 +00:00
|
|
|
"Ole André Vadla Ravnås <oleavr@soundrop.com>, Dominik Röttsches <dominik.rottsches@intel.com>");
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-02-16 01:37:11 +00:00
|
|
|
g_free (longname);
|
|
|
|
g_free (description);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
sink_template = gst_pad_template_new ("sink",
|
2013-10-02 02:49:43 +00:00
|
|
|
GST_PAD_SINK, GST_PAD_ALWAYS, gst_static_caps_get (&sink_caps));
|
2010-10-27 17:30:11 +00:00
|
|
|
gst_element_class_add_pad_template (element_class, sink_template);
|
|
|
|
|
|
|
|
src_caps = gst_caps_new_simple (codec_details->mimetype,
|
|
|
|
"width", GST_TYPE_INT_RANGE, min_width, max_width,
|
|
|
|
"height", GST_TYPE_INT_RANGE, min_height, max_height,
|
|
|
|
"framerate", GST_TYPE_FRACTION_RANGE,
|
|
|
|
min_fps_n, min_fps_d, max_fps_n, max_fps_d, NULL);
|
2014-09-16 12:02:46 +00:00
|
|
|
if (codec_details->format_id == kCMVideoCodecType_H264) {
|
2010-10-27 17:30:11 +00:00
|
|
|
gst_structure_set (gst_caps_get_structure (src_caps, 0),
|
2015-06-09 21:16:27 +00:00
|
|
|
"stream-format", G_TYPE_STRING, "avc",
|
|
|
|
"alignment", G_TYPE_STRING, "au", NULL);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
src_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
|
|
|
src_caps);
|
|
|
|
gst_element_class_add_pad_template (element_class, src_template);
|
2017-01-03 03:32:36 +00:00
|
|
|
gst_caps_unref (src_caps);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_class_init (GstVTEncClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2014-09-16 13:51:28 +00:00
|
|
|
GstVideoEncoderClass *gstvideoencoder_class;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2014-09-16 13:51:28 +00:00
|
|
|
gstvideoencoder_class = (GstVideoEncoderClass *) klass;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
|
|
|
gobject_class->get_property = gst_vtenc_get_property;
|
|
|
|
gobject_class->set_property = gst_vtenc_set_property;
|
2015-04-25 19:55:28 +00:00
|
|
|
gobject_class->finalize = gst_vtenc_finalize;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
gstvideoencoder_class->start = gst_vtenc_start;
|
|
|
|
gstvideoencoder_class->stop = gst_vtenc_stop;
|
|
|
|
gstvideoencoder_class->set_format = gst_vtenc_set_format;
|
|
|
|
gstvideoencoder_class->handle_frame = gst_vtenc_handle_frame;
|
2014-09-17 14:08:57 +00:00
|
|
|
gstvideoencoder_class->finish = gst_vtenc_finish;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_BITRATE,
|
|
|
|
g_param_spec_uint ("bitrate", "Bitrate",
|
2014-11-21 14:46:49 +00:00
|
|
|
"Target video bitrate in kbps (0 = auto)",
|
|
|
|
0, G_MAXUINT, VTENC_DEFAULT_BITRATE,
|
2010-10-27 17:30:11 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
2014-11-06 12:22:17 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_ALLOW_FRAME_REORDERING,
|
|
|
|
g_param_spec_boolean ("allow-frame-reordering", "Allow frame reordering",
|
|
|
|
"Whether to allow frame reordering or not",
|
|
|
|
VTENC_DEFAULT_FRAME_REORDERING,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_REALTIME,
|
|
|
|
g_param_spec_boolean ("realtime", "Realtime",
|
|
|
|
"Configure the encoder for realtime output",
|
|
|
|
VTENC_DEFAULT_REALTIME,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
2014-11-27 12:40:56 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_QUALITY,
|
|
|
|
g_param_spec_double ("quality", "Quality",
|
|
|
|
"The desired compression quality",
|
|
|
|
0.0, 1.0, VTENC_DEFAULT_QUALITY,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
2014-12-05 15:33:20 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_MAX_KEYFRAME_INTERVAL,
|
|
|
|
g_param_spec_int ("max-keyframe-interval", "Max Keyframe Interval",
|
|
|
|
"Maximum number of frames between keyframes (0 = auto)",
|
|
|
|
0, G_MAXINT, VTENC_DEFAULT_MAX_KEYFRAME_INTERVAL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_MAX_KEYFRAME_INTERVAL_DURATION,
|
|
|
|
g_param_spec_uint64 ("max-keyframe-interval-duration",
|
|
|
|
"Max Keyframe Interval Duration",
|
|
|
|
"Maximum number of nanoseconds between keyframes (0 = no limit)", 0,
|
|
|
|
G_MAXUINT64, VTENC_DEFAULT_MAX_KEYFRAME_INTERVAL_DURATION,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_init (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
GstVTEncClass *klass = (GstVTEncClass *) G_OBJECT_GET_CLASS (self);
|
2015-04-25 19:55:28 +00:00
|
|
|
CFStringRef keyframe_props_keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
|
|
|
|
CFBooleanRef keyframe_props_values[] = { kCFBooleanTrue };
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
self->details = GST_VTENC_CLASS_GET_CODEC_DETAILS (klass);
|
|
|
|
|
|
|
|
/* These could be controlled by properties later */
|
|
|
|
self->dump_properties = FALSE;
|
|
|
|
self->dump_attributes = FALSE;
|
2014-12-10 08:44:01 +00:00
|
|
|
self->latency_frames = -1;
|
2013-10-02 02:49:34 +00:00
|
|
|
self->session = NULL;
|
2014-12-10 11:36:53 +00:00
|
|
|
self->profile_level = NULL;
|
2015-04-25 19:55:28 +00:00
|
|
|
|
|
|
|
self->keyframe_props =
|
|
|
|
CFDictionaryCreate (NULL, (const void **) keyframe_props_keys,
|
|
|
|
(const void **) keyframe_props_values, G_N_ELEMENTS (keyframe_props_keys),
|
|
|
|
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_finalize (GObject * obj)
|
|
|
|
{
|
|
|
|
GstVTEnc *self = GST_VTENC_CAST (obj);
|
|
|
|
|
|
|
|
CFRelease (self->keyframe_props);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (obj);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gst_vtenc_get_bitrate (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
guint result;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
result = self->bitrate;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_set_bitrate (GstVTEnc * self, guint bitrate)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
|
|
|
|
self->bitrate = bitrate;
|
|
|
|
|
|
|
|
if (self->session != NULL)
|
|
|
|
gst_vtenc_session_configure_bitrate (self, self->session, bitrate);
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
2014-11-06 12:22:17 +00:00
|
|
|
static gboolean
|
|
|
|
gst_vtenc_get_allow_frame_reordering (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
result = self->allow_frame_reordering;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_set_allow_frame_reordering (GstVTEnc * self,
|
|
|
|
gboolean allow_frame_reordering)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->allow_frame_reordering = allow_frame_reordering;
|
|
|
|
if (self->session != NULL) {
|
|
|
|
gst_vtenc_session_configure_allow_frame_reordering (self,
|
|
|
|
self->session, allow_frame_reordering);
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vtenc_get_realtime (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
result = self->realtime;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_set_realtime (GstVTEnc * self, gboolean realtime)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->realtime = realtime;
|
|
|
|
if (self->session != NULL)
|
|
|
|
gst_vtenc_session_configure_realtime (self, self->session, realtime);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
2014-11-27 12:40:56 +00:00
|
|
|
static gdouble
|
|
|
|
gst_vtenc_get_quality (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
gdouble result;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
result = self->quality;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_set_quality (GstVTEnc * self, gdouble quality)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->quality = quality;
|
2014-12-10 08:44:01 +00:00
|
|
|
GST_INFO_OBJECT (self, "setting quality %f", quality);
|
2014-11-27 12:40:56 +00:00
|
|
|
if (self->session != NULL) {
|
|
|
|
gst_vtenc_session_configure_property_double (self, self->session,
|
|
|
|
kVTCompressionPropertyKey_Quality, quality);
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
2014-12-05 15:33:20 +00:00
|
|
|
static gint
|
|
|
|
gst_vtenc_get_max_keyframe_interval (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
gint result;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
result = self->max_keyframe_interval;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_set_max_keyframe_interval (GstVTEnc * self, gint interval)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->max_keyframe_interval = interval;
|
|
|
|
if (self->session != NULL) {
|
|
|
|
gst_vtenc_session_configure_max_keyframe_interval (self, self->session,
|
|
|
|
interval);
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockTime
|
|
|
|
gst_vtenc_get_max_keyframe_interval_duration (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
GstClockTime result;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
result = self->max_keyframe_interval_duration;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_set_max_keyframe_interval_duration (GstVTEnc * self,
|
|
|
|
GstClockTime interval)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->max_keyframe_interval_duration = interval;
|
|
|
|
if (self->session != NULL) {
|
|
|
|
gst_vtenc_session_configure_max_keyframe_interval_duration (self,
|
|
|
|
self->session, interval / ((gdouble) GST_SECOND));
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
static void
|
|
|
|
gst_vtenc_get_property (GObject * obj, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstVTEnc *self = GST_VTENC_CAST (obj);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_BITRATE:
|
2014-12-05 15:15:23 +00:00
|
|
|
g_value_set_uint (value, gst_vtenc_get_bitrate (self) / 1000);
|
2010-10-27 17:30:11 +00:00
|
|
|
break;
|
2014-11-06 12:22:17 +00:00
|
|
|
case PROP_ALLOW_FRAME_REORDERING:
|
|
|
|
g_value_set_boolean (value, gst_vtenc_get_allow_frame_reordering (self));
|
|
|
|
break;
|
|
|
|
case PROP_REALTIME:
|
|
|
|
g_value_set_boolean (value, gst_vtenc_get_realtime (self));
|
|
|
|
break;
|
2014-11-27 12:40:56 +00:00
|
|
|
case PROP_QUALITY:
|
|
|
|
g_value_set_double (value, gst_vtenc_get_quality (self));
|
|
|
|
break;
|
2014-12-05 15:33:20 +00:00
|
|
|
case PROP_MAX_KEYFRAME_INTERVAL:
|
|
|
|
g_value_set_int (value, gst_vtenc_get_max_keyframe_interval (self));
|
|
|
|
break;
|
|
|
|
case PROP_MAX_KEYFRAME_INTERVAL_DURATION:
|
|
|
|
g_value_set_uint64 (value,
|
|
|
|
gst_vtenc_get_max_keyframe_interval_duration (self));
|
|
|
|
break;
|
2010-10-27 17:30:11 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_set_property (GObject * obj, guint prop_id, const GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstVTEnc *self = GST_VTENC_CAST (obj);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_BITRATE:
|
2014-12-05 15:15:23 +00:00
|
|
|
gst_vtenc_set_bitrate (self, g_value_get_uint (value) * 1000);
|
2010-10-27 17:30:11 +00:00
|
|
|
break;
|
2014-11-06 12:22:17 +00:00
|
|
|
case PROP_ALLOW_FRAME_REORDERING:
|
|
|
|
gst_vtenc_set_allow_frame_reordering (self, g_value_get_boolean (value));
|
|
|
|
break;
|
|
|
|
case PROP_REALTIME:
|
|
|
|
gst_vtenc_set_realtime (self, g_value_get_boolean (value));
|
|
|
|
break;
|
2014-11-27 12:40:56 +00:00
|
|
|
case PROP_QUALITY:
|
|
|
|
gst_vtenc_set_quality (self, g_value_get_double (value));
|
|
|
|
break;
|
2014-12-05 15:33:20 +00:00
|
|
|
case PROP_MAX_KEYFRAME_INTERVAL:
|
|
|
|
gst_vtenc_set_max_keyframe_interval (self, g_value_get_int (value));
|
|
|
|
break;
|
|
|
|
case PROP_MAX_KEYFRAME_INTERVAL_DURATION:
|
|
|
|
gst_vtenc_set_max_keyframe_interval_duration (self,
|
|
|
|
g_value_get_uint64 (value));
|
|
|
|
break;
|
2010-10-27 17:30:11 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
static gboolean
|
|
|
|
gst_vtenc_start (GstVideoEncoder * enc)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2014-09-16 13:51:28 +00:00
|
|
|
GstVTEnc *self = GST_VTENC_CAST (enc);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-17 14:08:57 +00:00
|
|
|
self->cur_outframes = g_async_queue_new ();
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
static gboolean
|
|
|
|
gst_vtenc_stop (GstVideoEncoder * enc)
|
|
|
|
{
|
|
|
|
GstVTEnc *self = GST_VTENC_CAST (enc);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
gst_vtenc_destroy_session (self, &self->session);
|
2014-10-21 13:42:32 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-12-10 11:36:53 +00:00
|
|
|
if (self->profile_level)
|
|
|
|
CFRelease (self->profile_level);
|
2015-11-11 10:40:52 +00:00
|
|
|
self->profile_level = NULL;
|
2014-12-10 11:36:53 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
if (self->input_state)
|
|
|
|
gst_video_codec_state_unref (self->input_state);
|
|
|
|
self->input_state = NULL;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
self->negotiated_width = self->negotiated_height = 0;
|
|
|
|
self->negotiated_fps_n = self->negotiated_fps_d = 0;
|
2013-02-16 01:37:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
gst_vtenc_clear_cached_caps_downstream (self);
|
2013-02-16 01:37:11 +00:00
|
|
|
|
2014-09-17 14:08:57 +00:00
|
|
|
g_async_queue_unref (self->cur_outframes);
|
2014-09-16 13:51:28 +00:00
|
|
|
self->cur_outframes = NULL;
|
|
|
|
|
|
|
|
return TRUE;
|
2013-02-16 01:37:11 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 11:36:53 +00:00
|
|
|
static CFStringRef
|
|
|
|
gst_vtenc_profile_level_key (GstVTEnc * self, const gchar * profile,
|
|
|
|
const gchar * level_arg)
|
|
|
|
{
|
|
|
|
char level[64];
|
|
|
|
gchar *key = NULL;
|
|
|
|
CFStringRef ret = NULL;
|
|
|
|
|
|
|
|
if (profile == NULL)
|
|
|
|
profile = "main";
|
|
|
|
if (level_arg == NULL)
|
|
|
|
level_arg = "AutoLevel";
|
|
|
|
strncpy (level, level_arg, sizeof (level));
|
|
|
|
|
|
|
|
if (!strcmp (profile, "constrained-baseline") ||
|
|
|
|
!strcmp (profile, "baseline")) {
|
|
|
|
profile = "Baseline";
|
|
|
|
} else if (g_str_has_prefix (profile, "high")) {
|
|
|
|
profile = "High";
|
|
|
|
} else if (!strcmp (profile, "main")) {
|
|
|
|
profile = "Main";
|
|
|
|
} else {
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen (level) == 1) {
|
|
|
|
level[1] = '_';
|
|
|
|
level[2] = '0';
|
|
|
|
} else if (strlen (level) == 3) {
|
|
|
|
level[1] = '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
key = g_strdup_printf ("H264_%s_%s", profile, level);
|
|
|
|
ret = CFStringCreateWithBytes (NULL, (const guint8 *) key, strlen (key),
|
|
|
|
kCFStringEncodingASCII, 0);
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (self, "negotiated profile and level %s", key);
|
|
|
|
|
|
|
|
g_free (key);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vtenc_negotiate_profile_and_level (GstVideoEncoder * enc)
|
|
|
|
{
|
|
|
|
GstVTEnc *self = GST_VTENC_CAST (enc);
|
|
|
|
GstCaps *allowed_caps = NULL;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
const gchar *profile = NULL;
|
|
|
|
const gchar *level = NULL;
|
|
|
|
|
|
|
|
allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (enc));
|
|
|
|
if (allowed_caps) {
|
|
|
|
GstStructure *s;
|
|
|
|
|
|
|
|
if (gst_caps_is_empty (allowed_caps)) {
|
|
|
|
GST_ERROR_OBJECT (self, "no allowed downstream caps");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
allowed_caps = gst_caps_make_writable (allowed_caps);
|
|
|
|
allowed_caps = gst_caps_fixate (allowed_caps);
|
|
|
|
s = gst_caps_get_structure (allowed_caps, 0);
|
|
|
|
|
|
|
|
profile = gst_structure_get_string (s, "profile");
|
|
|
|
level = gst_structure_get_string (s, "level");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->profile_level)
|
|
|
|
CFRelease (self->profile_level);
|
|
|
|
self->profile_level = gst_vtenc_profile_level_key (self, profile, level);
|
|
|
|
if (self->profile_level == NULL) {
|
|
|
|
GST_ERROR_OBJECT (enc, "invalid profile and level");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (allowed_caps)
|
|
|
|
gst_caps_unref (allowed_caps);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
ret = FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-02-16 01:37:11 +00:00
|
|
|
static gboolean
|
2014-09-16 13:51:28 +00:00
|
|
|
gst_vtenc_set_format (GstVideoEncoder * enc, GstVideoCodecState * state)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2014-09-16 13:51:28 +00:00
|
|
|
GstVTEnc *self = GST_VTENC_CAST (enc);
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
if (self->input_state)
|
|
|
|
gst_video_codec_state_unref (self->input_state);
|
|
|
|
self->input_state = gst_video_codec_state_ref (state);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
self->negotiated_width = state->info.width;
|
|
|
|
self->negotiated_height = state->info.height;
|
|
|
|
self->negotiated_fps_n = state->info.fps_n;
|
|
|
|
self->negotiated_fps_d = state->info.fps_d;
|
|
|
|
self->video_info = state->info;
|
2013-10-02 02:49:34 +00:00
|
|
|
|
2014-10-21 13:42:32 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
2010-10-27 17:30:11 +00:00
|
|
|
gst_vtenc_destroy_session (self, &self->session);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
2014-10-21 13:42:32 +00:00
|
|
|
|
2014-12-10 11:36:53 +00:00
|
|
|
gst_vtenc_negotiate_profile_and_level (enc);
|
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
session = gst_vtenc_create_session (self);
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->session = session;
|
2014-10-21 13:42:32 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2016-06-01 11:43:32 +00:00
|
|
|
return session != NULL;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vtenc_is_negotiated (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
return self->negotiated_width != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-11-02 21:53:33 +00:00
|
|
|
gst_vtenc_negotiate_downstream (GstVTEnc * self, CMSampleBufferRef sbuf)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
gboolean result;
|
|
|
|
GstCaps *caps;
|
|
|
|
GstStructure *s;
|
2014-09-16 13:51:28 +00:00
|
|
|
GstVideoCodecState *state;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
if (self->caps_width == self->negotiated_width &&
|
|
|
|
self->caps_height == self->negotiated_height &&
|
|
|
|
self->caps_fps_n == self->negotiated_fps_n &&
|
|
|
|
self->caps_fps_d == self->negotiated_fps_d) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
caps = gst_pad_get_pad_template_caps (GST_VIDEO_ENCODER_SRC_PAD (self));
|
2014-08-09 11:20:41 +00:00
|
|
|
caps = gst_caps_make_writable (caps);
|
2010-10-27 17:30:11 +00:00
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
gst_structure_set (s,
|
|
|
|
"width", G_TYPE_INT, self->negotiated_width,
|
|
|
|
"height", G_TYPE_INT, self->negotiated_height,
|
|
|
|
"framerate", GST_TYPE_FRACTION,
|
|
|
|
self->negotiated_fps_n, self->negotiated_fps_d, NULL);
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
if (self->details->format_id == kCMVideoCodecType_H264) {
|
2010-11-02 21:53:33 +00:00
|
|
|
CMFormatDescriptionRef fmt;
|
2010-10-27 17:30:11 +00:00
|
|
|
CFDictionaryRef atoms;
|
|
|
|
CFStringRef avccKey;
|
|
|
|
CFDataRef avcc;
|
2014-11-27 12:33:07 +00:00
|
|
|
guint8 *codec_data;
|
2013-02-16 01:37:11 +00:00
|
|
|
gsize codec_data_size;
|
|
|
|
GstBuffer *codec_data_buf;
|
2014-11-27 12:33:07 +00:00
|
|
|
guint8 sps[3];
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
fmt = CMSampleBufferGetFormatDescription (sbuf);
|
|
|
|
atoms = CMFormatDescriptionGetExtension (fmt,
|
|
|
|
kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms);
|
2010-10-27 17:30:11 +00:00
|
|
|
avccKey = CFStringCreateWithCString (NULL, "avcC", kCFStringEncodingUTF8);
|
|
|
|
avcc = CFDictionaryGetValue (atoms, avccKey);
|
|
|
|
CFRelease (avccKey);
|
2013-02-16 01:37:11 +00:00
|
|
|
codec_data_size = CFDataGetLength (avcc);
|
|
|
|
codec_data = g_malloc (codec_data_size);
|
|
|
|
CFDataGetBytes (avcc, CFRangeMake (0, codec_data_size), codec_data);
|
|
|
|
codec_data_buf = gst_buffer_new_wrapped (codec_data, codec_data_size);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-02-16 01:37:11 +00:00
|
|
|
gst_structure_set (s, "codec_data", GST_TYPE_BUFFER, codec_data_buf, NULL);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-11-27 12:33:07 +00:00
|
|
|
sps[0] = codec_data[1];
|
2014-12-10 11:36:53 +00:00
|
|
|
sps[1] = codec_data[2] & ~0xDF;
|
2014-11-27 12:33:07 +00:00
|
|
|
sps[2] = codec_data[3];
|
2014-12-10 11:36:53 +00:00
|
|
|
|
2014-11-27 12:33:07 +00:00
|
|
|
gst_codec_utils_h264_caps_set_level_and_profile (caps, sps, 3);
|
|
|
|
|
2013-02-16 01:37:11 +00:00
|
|
|
gst_buffer_unref (codec_data_buf);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
state =
|
|
|
|
gst_video_encoder_set_output_state (GST_VIDEO_ENCODER_CAST (self), caps,
|
|
|
|
self->input_state);
|
|
|
|
gst_video_codec_state_unref (state);
|
|
|
|
result = gst_video_encoder_negotiate (GST_VIDEO_ENCODER_CAST (self));
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
self->caps_width = self->negotiated_width;
|
|
|
|
self->caps_height = self->negotiated_height;
|
|
|
|
self->caps_fps_n = self->negotiated_fps_n;
|
|
|
|
self->caps_fps_d = self->negotiated_fps_d;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_clear_cached_caps_downstream (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
self->caps_width = self->caps_height = 0;
|
|
|
|
self->caps_fps_n = self->caps_fps_d = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2014-09-16 13:51:28 +00:00
|
|
|
gst_vtenc_handle_frame (GstVideoEncoder * enc, GstVideoCodecFrame * frame)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2014-09-16 13:51:28 +00:00
|
|
|
GstVTEnc *self = GST_VTENC_CAST (enc);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
if (!gst_vtenc_is_negotiated (self))
|
|
|
|
goto not_negotiated;
|
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
return gst_vtenc_encode_frame (self, frame);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
not_negotiated:
|
2014-09-16 13:51:28 +00:00
|
|
|
gst_video_codec_frame_unref (frame);
|
2010-10-27 17:30:11 +00:00
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
|
|
|
|
2014-09-17 14:08:57 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_vtenc_finish (GstVideoEncoder * enc)
|
|
|
|
{
|
|
|
|
GstVTEnc *self = GST_VTENC_CAST (enc);
|
2015-05-11 14:44:33 +00:00
|
|
|
GstVideoCodecFrame *outframe;
|
2014-09-17 14:08:57 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
OSStatus vt_status;
|
|
|
|
|
2015-01-08 13:47:16 +00:00
|
|
|
/* We need to unlock the stream lock here because
|
|
|
|
* it can wait for gst_vtenc_enqueue_buffer() to
|
|
|
|
* handle a buffer... which will take the stream
|
|
|
|
* lock from another thread and then deadlock */
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
|
2014-09-17 14:08:57 +00:00
|
|
|
vt_status =
|
|
|
|
VTCompressionSessionCompleteFrames (self->session,
|
|
|
|
kCMTimePositiveInfinity);
|
2015-01-08 13:47:16 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (self);
|
2014-09-17 14:08:57 +00:00
|
|
|
if (vt_status != noErr) {
|
|
|
|
GST_WARNING_OBJECT (self, "VTCompressionSessionCompleteFrames returned %d",
|
|
|
|
(int) vt_status);
|
|
|
|
}
|
|
|
|
|
2015-05-11 14:44:33 +00:00
|
|
|
while ((outframe = g_async_queue_try_pop (self->cur_outframes))) {
|
2014-09-17 14:08:57 +00:00
|
|
|
ret =
|
|
|
|
gst_video_encoder_finish_frame (GST_VIDEO_ENCODER_CAST (self),
|
|
|
|
outframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-11-02 22:08:30 +00:00
|
|
|
static VTCompressionSessionRef
|
2010-10-27 17:30:11 +00:00
|
|
|
gst_vtenc_create_session (GstVTEnc * self)
|
|
|
|
{
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session = NULL;
|
2014-11-12 15:45:12 +00:00
|
|
|
CFMutableDictionaryRef encoder_spec = NULL, pb_attrs;
|
2014-09-16 12:02:46 +00:00
|
|
|
OSStatus status;
|
2017-01-27 16:11:15 +00:00
|
|
|
|
|
|
|
#if !HAVE_IOS
|
2016-06-01 11:43:32 +00:00
|
|
|
const GstVTEncoderDetails *codec_details =
|
|
|
|
GST_VTENC_CLASS_GET_CODEC_DETAILS (G_OBJECT_GET_CLASS (self));
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-11-11 17:01:54 +00:00
|
|
|
encoder_spec =
|
|
|
|
CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
gst_vtutil_dict_set_boolean (encoder_spec,
|
|
|
|
kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, true);
|
2016-06-01 11:43:32 +00:00
|
|
|
if (codec_details->require_hardware)
|
|
|
|
gst_vtutil_dict_set_boolean (encoder_spec,
|
|
|
|
kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
|
|
|
|
TRUE);
|
2014-11-12 15:45:12 +00:00
|
|
|
#endif
|
2014-11-11 17:01:54 +00:00
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
pb_attrs = CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
2013-04-24 20:15:01 +00:00
|
|
|
gst_vtutil_dict_set_i32 (pb_attrs, kCVPixelBufferWidthKey,
|
2010-10-27 17:30:11 +00:00
|
|
|
self->negotiated_width);
|
2013-04-24 20:15:01 +00:00
|
|
|
gst_vtutil_dict_set_i32 (pb_attrs, kCVPixelBufferHeightKey,
|
2010-10-27 17:30:11 +00:00
|
|
|
self->negotiated_height);
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
status = VTCompressionSessionCreate (NULL,
|
2010-10-27 17:30:11 +00:00
|
|
|
self->negotiated_width, self->negotiated_height,
|
2014-11-11 17:01:54 +00:00
|
|
|
self->details->format_id, encoder_spec, pb_attrs, NULL,
|
|
|
|
gst_vtenc_enqueue_buffer, self, &session);
|
2010-10-27 17:30:11 +00:00
|
|
|
GST_INFO_OBJECT (self, "VTCompressionSessionCreate for %d x %d => %d",
|
2014-09-16 12:02:46 +00:00
|
|
|
self->negotiated_width, self->negotiated_height, (int) status);
|
2014-09-17 14:10:58 +00:00
|
|
|
if (status != noErr) {
|
|
|
|
GST_ERROR_OBJECT (self, "VTCompressionSessionCreate() returned: %d",
|
|
|
|
(int) status);
|
2010-10-27 17:30:11 +00:00
|
|
|
goto beach;
|
2014-09-17 14:10:58 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
gst_vtenc_session_configure_expected_framerate (self, session,
|
|
|
|
(gdouble) self->negotiated_fps_n / (gdouble) self->negotiated_fps_d);
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
status = VTSessionSetProperty (session,
|
2014-12-10 11:36:53 +00:00
|
|
|
kVTCompressionPropertyKey_ProfileLevel, self->profile_level);
|
2010-10-27 17:30:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "kVTCompressionPropertyKey_ProfileLevel => %d",
|
2014-09-16 12:02:46 +00:00
|
|
|
(int) status);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
status = VTSessionSetProperty (session,
|
|
|
|
kVTCompressionPropertyKey_AllowTemporalCompression, kCFBooleanTrue);
|
2010-10-27 17:30:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self,
|
2014-09-16 12:02:46 +00:00
|
|
|
"kVTCompressionPropertyKey_AllowTemporalCompression => %d", (int) status);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-12-05 15:33:20 +00:00
|
|
|
gst_vtenc_session_configure_max_keyframe_interval (self, session,
|
|
|
|
self->max_keyframe_interval);
|
|
|
|
gst_vtenc_session_configure_max_keyframe_interval_duration (self, session,
|
|
|
|
self->max_keyframe_interval_duration / ((gdouble) GST_SECOND));
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
gst_vtenc_session_configure_bitrate (self, session,
|
|
|
|
gst_vtenc_get_bitrate (self));
|
2014-11-06 12:22:17 +00:00
|
|
|
gst_vtenc_session_configure_realtime (self, session,
|
|
|
|
gst_vtenc_get_realtime (self));
|
|
|
|
gst_vtenc_session_configure_allow_frame_reordering (self, session,
|
|
|
|
gst_vtenc_get_allow_frame_reordering (self));
|
2014-11-27 12:40:56 +00:00
|
|
|
gst_vtenc_set_quality (self, self->quality);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-11-27 12:40:56 +00:00
|
|
|
if (self->dump_properties) {
|
|
|
|
gst_vtenc_session_dump_properties (self, session);
|
|
|
|
self->dump_properties = FALSE;
|
|
|
|
}
|
2014-10-19 12:51:40 +00:00
|
|
|
#ifdef HAVE_VIDEOTOOLBOX_10_9_6
|
|
|
|
if (VTCompressionSessionPrepareToEncodeFrames) {
|
|
|
|
status = VTCompressionSessionPrepareToEncodeFrames (session);
|
|
|
|
if (status != noErr) {
|
|
|
|
GST_ERROR_OBJECT (self,
|
|
|
|
"VTCompressionSessionPrepareToEncodeFrames() returned: %d",
|
|
|
|
(int) status);
|
|
|
|
}
|
2014-09-17 14:10:58 +00:00
|
|
|
}
|
2014-10-19 12:51:40 +00:00
|
|
|
#endif
|
2014-09-17 14:10:58 +00:00
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
beach:
|
2014-11-12 15:45:12 +00:00
|
|
|
if (encoder_spec)
|
|
|
|
CFRelease (encoder_spec);
|
2010-10-27 17:30:11 +00:00
|
|
|
CFRelease (pb_attrs);
|
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-02 22:08:30 +00:00
|
|
|
gst_vtenc_destroy_session (GstVTEnc * self, VTCompressionSessionRef * session)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2014-09-16 12:02:46 +00:00
|
|
|
VTCompressionSessionInvalidate (*session);
|
2013-02-16 01:26:51 +00:00
|
|
|
if (*session != NULL) {
|
|
|
|
CFRelease (*session);
|
|
|
|
*session = NULL;
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstVTEnc *self;
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session;
|
2010-10-27 17:30:11 +00:00
|
|
|
} GstVTDumpPropCtx;
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_session_dump_property (CFStringRef prop_name,
|
|
|
|
CFDictionaryRef prop_attrs, GstVTDumpPropCtx * dpc)
|
|
|
|
{
|
|
|
|
gchar *name_str;
|
|
|
|
CFTypeRef prop_value;
|
2014-09-16 12:02:46 +00:00
|
|
|
OSStatus status;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
name_str = gst_vtutil_string_to_utf8 (prop_name);
|
|
|
|
if (dpc->self->dump_attributes) {
|
|
|
|
gchar *attrs_str;
|
|
|
|
|
|
|
|
attrs_str = gst_vtutil_object_to_string (prop_attrs);
|
|
|
|
GST_DEBUG_OBJECT (dpc->self, "%s = %s", name_str, attrs_str);
|
|
|
|
g_free (attrs_str);
|
|
|
|
}
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
status = VTSessionCopyProperty (dpc->session, prop_name, NULL, &prop_value);
|
|
|
|
if (status == noErr) {
|
2010-10-27 17:30:11 +00:00
|
|
|
gchar *value_str;
|
|
|
|
|
|
|
|
value_str = gst_vtutil_object_to_string (prop_value);
|
|
|
|
GST_DEBUG_OBJECT (dpc->self, "%s = %s", name_str, value_str);
|
|
|
|
g_free (value_str);
|
|
|
|
|
|
|
|
if (prop_value != NULL)
|
|
|
|
CFRelease (prop_value);
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (dpc->self, "%s = <failed to query: %d>",
|
2014-09-16 12:02:46 +00:00
|
|
|
name_str, (int) status);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (name_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_session_dump_properties (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2014-09-16 12:02:46 +00:00
|
|
|
GstVTDumpPropCtx dpc = { self, session };
|
2010-10-27 17:30:11 +00:00
|
|
|
CFDictionaryRef dict;
|
2014-09-16 12:02:46 +00:00
|
|
|
OSStatus status;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
status = VTSessionCopySupportedPropertyDictionary (session, &dict);
|
|
|
|
if (status != noErr)
|
2010-10-27 17:30:11 +00:00
|
|
|
goto error;
|
|
|
|
CFDictionaryApplyFunction (dict,
|
|
|
|
(CFDictionaryApplierFunction) gst_vtenc_session_dump_property, &dpc);
|
|
|
|
CFRelease (dict);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
GST_WARNING_OBJECT (self, "failed to dump properties");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_session_configure_expected_framerate (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, gdouble framerate)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
gst_vtenc_session_configure_property_double (self, session,
|
2014-09-16 12:02:46 +00:00
|
|
|
kVTCompressionPropertyKey_ExpectedFrameRate, framerate);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_session_configure_max_keyframe_interval (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, gint interval)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
gst_vtenc_session_configure_property_int (self, session,
|
2014-09-16 12:02:46 +00:00
|
|
|
kVTCompressionPropertyKey_MaxKeyFrameInterval, interval);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_session_configure_max_keyframe_interval_duration (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, gdouble duration)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
gst_vtenc_session_configure_property_double (self, session,
|
2014-09-16 12:02:46 +00:00
|
|
|
kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, duration);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_session_configure_bitrate (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, guint bitrate)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
gst_vtenc_session_configure_property_int (self, session,
|
2014-09-16 12:02:46 +00:00
|
|
|
kVTCompressionPropertyKey_AverageBitRate, bitrate);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 12:22:17 +00:00
|
|
|
static void
|
|
|
|
gst_vtenc_session_configure_allow_frame_reordering (GstVTEnc * self,
|
|
|
|
VTCompressionSessionRef session, gboolean allow_frame_reordering)
|
|
|
|
{
|
|
|
|
VTSessionSetProperty (session, kVTCompressionPropertyKey_AllowFrameReordering,
|
|
|
|
allow_frame_reordering ? kCFBooleanTrue : kCFBooleanFalse);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_session_configure_realtime (GstVTEnc * self,
|
|
|
|
VTCompressionSessionRef session, gboolean realtime)
|
|
|
|
{
|
|
|
|
VTSessionSetProperty (session, kVTCompressionPropertyKey_RealTime,
|
|
|
|
realtime ? kCFBooleanTrue : kCFBooleanFalse);
|
|
|
|
}
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
static OSStatus
|
2010-10-27 17:30:11 +00:00
|
|
|
gst_vtenc_session_configure_property_int (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, CFStringRef name, gint value)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
CFNumberRef num;
|
2014-09-16 12:02:46 +00:00
|
|
|
OSStatus status;
|
2010-10-27 17:30:11 +00:00
|
|
|
gchar name_str[128];
|
|
|
|
|
|
|
|
num = CFNumberCreate (NULL, kCFNumberIntType, &value);
|
2014-09-16 12:02:46 +00:00
|
|
|
status = VTSessionSetProperty (session, name, num);
|
2010-10-27 17:30:11 +00:00
|
|
|
CFRelease (num);
|
|
|
|
|
|
|
|
CFStringGetCString (name, name_str, sizeof (name_str), kCFStringEncodingUTF8);
|
2014-09-16 12:02:46 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "%s(%d) => %d", name_str, value, (int) status);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
static OSStatus
|
2010-10-27 17:30:11 +00:00
|
|
|
gst_vtenc_session_configure_property_double (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, CFStringRef name, gdouble value)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
CFNumberRef num;
|
2014-09-16 12:02:46 +00:00
|
|
|
OSStatus status;
|
2010-10-27 17:30:11 +00:00
|
|
|
gchar name_str[128];
|
|
|
|
|
|
|
|
num = CFNumberCreate (NULL, kCFNumberDoubleType, &value);
|
2014-09-16 12:02:46 +00:00
|
|
|
status = VTSessionSetProperty (session, name, num);
|
2010-10-27 17:30:11 +00:00
|
|
|
CFRelease (num);
|
|
|
|
|
|
|
|
CFStringGetCString (name, name_str, sizeof (name_str), kCFStringEncodingUTF8);
|
2014-09-16 12:02:46 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "%s(%f) => %d", name_str, value, (int) status);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-12-10 08:44:01 +00:00
|
|
|
static void
|
|
|
|
gst_vtenc_update_latency (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
CFNumberRef value;
|
|
|
|
int frames = 0;
|
|
|
|
GstClockTime frame_duration;
|
|
|
|
GstClockTime latency;
|
|
|
|
|
|
|
|
if (self->video_info.fps_d == 0) {
|
|
|
|
GST_INFO_OBJECT (self, "framerate not known, can't set latency");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = VTSessionCopyProperty (self->session,
|
|
|
|
kVTCompressionPropertyKey_NumberOfPendingFrames, NULL, &value);
|
2015-02-16 10:35:09 +00:00
|
|
|
if (status != noErr || !value) {
|
|
|
|
GST_INFO_OBJECT (self, "failed to get NumberOfPendingFrames: %d", status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-10 08:44:01 +00:00
|
|
|
CFNumberGetValue (value, kCFNumberSInt32Type, &frames);
|
|
|
|
if (self->latency_frames == -1 || self->latency_frames != frames) {
|
|
|
|
self->latency_frames = frames;
|
2017-08-09 09:26:37 +00:00
|
|
|
if (self->video_info.fps_d == 0 || self->video_info.fps_n == 0) {
|
|
|
|
/* FIXME: Assume 25fps. This is better than reporting no latency at
|
|
|
|
* all and then later failing in live pipelines
|
|
|
|
*/
|
|
|
|
frame_duration = gst_util_uint64_scale (GST_SECOND, 1, 25);
|
|
|
|
} else {
|
|
|
|
frame_duration = gst_util_uint64_scale (GST_SECOND,
|
|
|
|
self->video_info.fps_d, self->video_info.fps_n);
|
|
|
|
}
|
2014-12-10 08:44:01 +00:00
|
|
|
latency = frame_duration * frames;
|
|
|
|
GST_INFO_OBJECT (self,
|
|
|
|
"latency status %d frames %d fps %d/%d time %" GST_TIME_FORMAT, status,
|
|
|
|
frames, self->video_info.fps_n, self->video_info.fps_d,
|
|
|
|
GST_TIME_ARGS (latency));
|
|
|
|
gst_video_encoder_set_latency (GST_VIDEO_ENCODER (self), latency, latency);
|
|
|
|
}
|
|
|
|
CFRelease (value);
|
|
|
|
}
|
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
static GstFlowReturn
|
2014-09-16 13:51:28 +00:00
|
|
|
gst_vtenc_encode_frame (GstVTEnc * self, GstVideoCodecFrame * frame)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2010-11-02 21:53:33 +00:00
|
|
|
CMTime ts, duration;
|
2013-02-16 01:37:11 +00:00
|
|
|
GstCoreMediaMeta *meta;
|
2010-10-27 17:30:11 +00:00
|
|
|
CVPixelBufferRef pbuf = NULL;
|
2015-05-11 14:44:33 +00:00
|
|
|
GstVideoCodecFrame *outframe;
|
2014-09-16 12:02:46 +00:00
|
|
|
OSStatus vt_status;
|
2010-11-10 22:16:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2015-05-11 14:47:25 +00:00
|
|
|
gboolean renegotiated;
|
2015-04-25 19:55:28 +00:00
|
|
|
CFDictionaryRef frame_props = NULL;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
if (GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (frame)) {
|
2015-04-25 19:55:28 +00:00
|
|
|
GST_INFO_OBJECT (self, "received force-keyframe-event, will force intra");
|
|
|
|
frame_props = self->keyframe_props;
|
2014-09-16 13:51:28 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-17 11:55:24 +00:00
|
|
|
ts = CMTimeMake (frame->pts, GST_SECOND);
|
|
|
|
if (frame->duration != GST_CLOCK_TIME_NONE)
|
|
|
|
duration = CMTimeMake (frame->duration, GST_SECOND);
|
|
|
|
else
|
|
|
|
duration = kCMTimeInvalid;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
meta = gst_buffer_get_core_media_meta (frame->input_buffer);
|
2013-02-16 01:37:11 +00:00
|
|
|
if (meta != NULL) {
|
2014-09-16 13:51:28 +00:00
|
|
|
pbuf = gst_core_media_buffer_get_pixel_buffer (frame->input_buffer);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
2014-09-19 11:36:39 +00:00
|
|
|
#ifdef HAVE_IOS
|
|
|
|
if (pbuf == NULL) {
|
|
|
|
GstVideoFrame inframe, outframe;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
OSType pixel_format_type;
|
|
|
|
CVReturn cv_ret;
|
|
|
|
|
|
|
|
/* FIXME: iOS has special stride requirements that we don't know yet.
|
|
|
|
* Copy into a newly allocated pixelbuffer for now. Probably makes
|
|
|
|
* sense to create a buffer pool around these at some point.
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (GST_VIDEO_INFO_FORMAT (&self->video_info)) {
|
|
|
|
case GST_VIDEO_FORMAT_I420:
|
|
|
|
pixel_format_type = kCVPixelFormatType_420YpCbCr8Planar;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
pixel_format_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto cv_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_video_frame_map (&inframe, &self->video_info, frame->input_buffer,
|
|
|
|
GST_MAP_READ))
|
|
|
|
goto cv_error;
|
|
|
|
|
|
|
|
cv_ret =
|
|
|
|
CVPixelBufferCreate (NULL, self->negotiated_width,
|
|
|
|
self->negotiated_height, pixel_format_type, NULL, &pbuf);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-19 11:36:39 +00:00
|
|
|
if (cv_ret != kCVReturnSuccess) {
|
|
|
|
gst_video_frame_unmap (&inframe);
|
|
|
|
goto cv_error;
|
|
|
|
}
|
|
|
|
|
2016-08-26 07:37:54 +00:00
|
|
|
outbuf =
|
|
|
|
gst_core_video_buffer_new ((CVBufferRef) pbuf, &self->video_info, NULL);
|
2014-09-19 11:36:39 +00:00
|
|
|
if (!gst_video_frame_map (&outframe, &self->video_info, outbuf,
|
|
|
|
GST_MAP_WRITE)) {
|
|
|
|
gst_video_frame_unmap (&inframe);
|
|
|
|
gst_buffer_unref (outbuf);
|
|
|
|
CVPixelBufferRelease (pbuf);
|
|
|
|
goto cv_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_video_frame_copy (&outframe, &inframe)) {
|
|
|
|
gst_video_frame_unmap (&inframe);
|
|
|
|
gst_buffer_unref (outbuf);
|
|
|
|
CVPixelBufferRelease (pbuf);
|
|
|
|
goto cv_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_unref (outbuf);
|
|
|
|
gst_video_frame_unmap (&inframe);
|
|
|
|
gst_video_frame_unmap (&outframe);
|
|
|
|
}
|
|
|
|
#else
|
2010-10-27 17:30:11 +00:00
|
|
|
if (pbuf == NULL) {
|
2014-09-16 13:51:28 +00:00
|
|
|
GstVTEncFrame *vframe;
|
2010-10-27 17:30:11 +00:00
|
|
|
CVReturn cv_ret;
|
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
vframe = gst_vtenc_frame_new (frame->input_buffer, &self->video_info);
|
|
|
|
if (!vframe)
|
2010-10-27 17:30:11 +00:00
|
|
|
goto cv_error;
|
2013-10-02 02:49:34 +00:00
|
|
|
|
|
|
|
{
|
2014-09-16 13:51:28 +00:00
|
|
|
const size_t num_planes = GST_VIDEO_FRAME_N_PLANES (&vframe->videoframe);
|
2014-04-18 18:12:37 +00:00
|
|
|
void *plane_base_addresses[GST_VIDEO_MAX_PLANES];
|
|
|
|
size_t plane_widths[GST_VIDEO_MAX_PLANES];
|
|
|
|
size_t plane_heights[GST_VIDEO_MAX_PLANES];
|
|
|
|
size_t plane_bytes_per_row[GST_VIDEO_MAX_PLANES];
|
2013-10-02 02:49:43 +00:00
|
|
|
OSType pixel_format_type;
|
2013-10-02 02:49:34 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_planes; i++) {
|
|
|
|
plane_base_addresses[i] =
|
2014-09-16 13:51:28 +00:00
|
|
|
GST_VIDEO_FRAME_PLANE_DATA (&vframe->videoframe, i);
|
|
|
|
plane_widths[i] = GST_VIDEO_FRAME_COMP_WIDTH (&vframe->videoframe, i);
|
|
|
|
plane_heights[i] = GST_VIDEO_FRAME_COMP_HEIGHT (&vframe->videoframe, i);
|
2013-10-02 02:49:34 +00:00
|
|
|
plane_bytes_per_row[i] =
|
2014-09-16 13:51:28 +00:00
|
|
|
GST_VIDEO_FRAME_COMP_STRIDE (&vframe->videoframe, i);
|
2013-10-02 02:49:34 +00:00
|
|
|
plane_bytes_per_row[i] =
|
2014-09-16 13:51:28 +00:00
|
|
|
GST_VIDEO_FRAME_COMP_STRIDE (&vframe->videoframe, i);
|
2013-10-02 02:49:34 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 02:49:43 +00:00
|
|
|
switch (GST_VIDEO_INFO_FORMAT (&self->video_info)) {
|
|
|
|
case GST_VIDEO_FORMAT_I420:
|
|
|
|
pixel_format_type = kCVPixelFormatType_420YpCbCr8Planar;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
pixel_format_type = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
|
|
|
|
break;
|
2016-01-06 00:40:38 +00:00
|
|
|
case GST_VIDEO_FORMAT_UYVY:
|
|
|
|
pixel_format_type = kCVPixelFormatType_422YpCbCr8;
|
|
|
|
break;
|
2013-10-02 02:49:43 +00:00
|
|
|
default:
|
2015-05-11 14:39:19 +00:00
|
|
|
gst_vtenc_frame_free (vframe);
|
2013-10-02 02:49:43 +00:00
|
|
|
goto cv_error;
|
|
|
|
}
|
|
|
|
|
2013-10-02 02:49:34 +00:00
|
|
|
cv_ret = CVPixelBufferCreateWithPlanarBytes (NULL,
|
|
|
|
self->negotiated_width, self->negotiated_height,
|
2013-10-02 02:49:43 +00:00
|
|
|
pixel_format_type,
|
2014-06-16 10:35:13 +00:00
|
|
|
frame,
|
2014-09-16 13:51:28 +00:00
|
|
|
GST_VIDEO_FRAME_SIZE (&vframe->videoframe),
|
2013-10-02 02:49:34 +00:00
|
|
|
num_planes,
|
|
|
|
plane_base_addresses,
|
|
|
|
plane_widths,
|
|
|
|
plane_heights,
|
2014-09-16 13:51:28 +00:00
|
|
|
plane_bytes_per_row, gst_pixel_buffer_release_cb, vframe, NULL,
|
|
|
|
&pbuf);
|
2013-10-02 02:49:34 +00:00
|
|
|
if (cv_ret != kCVReturnSuccess) {
|
2014-09-16 13:51:28 +00:00
|
|
|
gst_vtenc_frame_free (vframe);
|
2013-10-02 02:49:34 +00:00
|
|
|
goto cv_error;
|
|
|
|
}
|
2013-02-16 01:37:11 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
2014-09-19 11:36:39 +00:00
|
|
|
#endif
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-10-21 13:42:32 +00:00
|
|
|
/* We need to unlock the stream lock here because
|
|
|
|
* it can wait for gst_vtenc_enqueue_buffer() to
|
|
|
|
* handle a buffer... which will take the stream
|
|
|
|
* lock from another thread and then deadlock */
|
|
|
|
GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
|
2014-09-16 12:02:46 +00:00
|
|
|
vt_status = VTCompressionSessionEncodeFrame (self->session,
|
2015-04-25 19:55:28 +00:00
|
|
|
pbuf, ts, duration, frame_props,
|
2014-09-16 13:51:28 +00:00
|
|
|
GINT_TO_POINTER (frame->system_frame_number), NULL);
|
2014-10-21 13:42:32 +00:00
|
|
|
GST_VIDEO_ENCODER_STREAM_LOCK (self);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-17 14:08:57 +00:00
|
|
|
if (vt_status != noErr) {
|
2010-10-27 17:30:11 +00:00
|
|
|
GST_WARNING_OBJECT (self, "VTCompressionSessionEncodeFrame returned %d",
|
2014-09-16 12:02:46 +00:00
|
|
|
(int) vt_status);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
gst_video_codec_frame_unref (frame);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
CVPixelBufferRelease (pbuf);
|
2010-11-10 22:16:51 +00:00
|
|
|
|
2015-05-11 14:47:25 +00:00
|
|
|
renegotiated = FALSE;
|
2015-05-11 14:44:33 +00:00
|
|
|
while ((outframe = g_async_queue_try_pop (self->cur_outframes))) {
|
2015-05-11 14:47:25 +00:00
|
|
|
if (outframe->output_buffer) {
|
|
|
|
if (!renegotiated) {
|
|
|
|
meta = gst_buffer_get_core_media_meta (outframe->output_buffer);
|
|
|
|
/* Try to renegotiate once */
|
|
|
|
if (meta) {
|
|
|
|
if (gst_vtenc_negotiate_downstream (self, meta->sample_buf)) {
|
|
|
|
renegotiated = TRUE;
|
|
|
|
} else {
|
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
gst_video_codec_frame_unref (outframe);
|
|
|
|
/* the rest of the frames will be pop'd and unref'd later */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-09-17 14:08:57 +00:00
|
|
|
}
|
2014-12-10 08:44:01 +00:00
|
|
|
|
|
|
|
gst_vtenc_update_latency (self);
|
2014-09-17 14:08:57 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-05-11 14:47:25 +00:00
|
|
|
/* releases frame, even if it has no output buffer (i.e. failed to encode) */
|
2014-09-16 13:51:28 +00:00
|
|
|
ret =
|
|
|
|
gst_video_encoder_finish_frame (GST_VIDEO_ENCODER_CAST (self),
|
|
|
|
outframe);
|
2010-11-10 22:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
cv_error:
|
|
|
|
{
|
2014-09-16 13:51:28 +00:00
|
|
|
gst_video_codec_frame_unref (frame);
|
2010-10-27 17:30:11 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
static void
|
|
|
|
gst_vtenc_enqueue_buffer (void *outputCallbackRefCon,
|
|
|
|
void *sourceFrameRefCon,
|
|
|
|
OSStatus status,
|
|
|
|
VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2014-09-16 12:02:46 +00:00
|
|
|
GstVTEnc *self = outputCallbackRefCon;
|
2010-10-27 17:30:11 +00:00
|
|
|
gboolean is_keyframe;
|
2014-09-16 13:51:28 +00:00
|
|
|
GstVideoCodecFrame *frame;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-05-11 14:47:25 +00:00
|
|
|
frame =
|
|
|
|
gst_video_encoder_get_frame (GST_VIDEO_ENCODER_CAST (self),
|
|
|
|
GPOINTER_TO_INT (sourceFrameRefCon));
|
|
|
|
|
2014-09-17 12:39:26 +00:00
|
|
|
if (status != noErr) {
|
2015-05-11 14:47:25 +00:00
|
|
|
if (frame) {
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, ENCODE, (NULL),
|
|
|
|
("Failed to encode frame %d: %d", frame->system_frame_number,
|
|
|
|
(int) status));
|
|
|
|
} else {
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, ENCODE, (NULL),
|
|
|
|
("Failed to encode (frame unknown): %d", (int) status));
|
|
|
|
}
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frame) {
|
|
|
|
GST_WARNING_OBJECT (self, "No corresponding frame found!");
|
2014-09-17 12:39:26 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
/* This may happen if we don't have enough bitrate */
|
2014-09-16 12:02:46 +00:00
|
|
|
if (sampleBuffer == NULL)
|
2010-10-27 17:30:11 +00:00
|
|
|
goto beach;
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
is_keyframe = gst_vtenc_buffer_is_keyframe (self, sampleBuffer);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-18 10:11:05 +00:00
|
|
|
if (is_keyframe) {
|
|
|
|
GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
|
|
|
|
gst_vtenc_clear_cached_caps_downstream (self);
|
|
|
|
}
|
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
/* We are dealing with block buffers here, so we don't need
|
|
|
|
* to enable the use of the video meta API on the core media buffer */
|
2016-08-26 07:37:54 +00:00
|
|
|
frame->output_buffer = gst_core_media_buffer_new (sampleBuffer, FALSE, NULL);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
beach:
|
2015-05-11 14:47:25 +00:00
|
|
|
/* needed anyway so the frame will be released */
|
|
|
|
if (frame)
|
|
|
|
g_async_queue_push (self->cur_outframes, frame);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-11-02 21:53:33 +00:00
|
|
|
gst_vtenc_buffer_is_keyframe (GstVTEnc * self, CMSampleBufferRef sbuf)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
gboolean result = FALSE;
|
|
|
|
CFArrayRef attachments_for_sample;
|
|
|
|
|
2013-10-02 02:49:34 +00:00
|
|
|
attachments_for_sample = CMSampleBufferGetSampleAttachmentsArray (sbuf, 0);
|
2010-10-27 17:30:11 +00:00
|
|
|
if (attachments_for_sample != NULL) {
|
|
|
|
CFDictionaryRef attachments;
|
|
|
|
CFBooleanRef depends_on_others;
|
|
|
|
|
|
|
|
attachments = CFArrayGetValueAtIndex (attachments_for_sample, 0);
|
|
|
|
depends_on_others = CFDictionaryGetValue (attachments,
|
2013-04-24 20:15:01 +00:00
|
|
|
kCMSampleAttachmentKey_DependsOnOthers);
|
2010-10-27 17:30:11 +00:00
|
|
|
result = (depends_on_others == kCFBooleanFalse);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-19 11:36:39 +00:00
|
|
|
#ifndef HAVE_IOS
|
2013-02-16 01:37:11 +00:00
|
|
|
static GstVTEncFrame *
|
2013-10-02 02:49:34 +00:00
|
|
|
gst_vtenc_frame_new (GstBuffer * buf, GstVideoInfo * video_info)
|
2013-02-16 01:37:11 +00:00
|
|
|
{
|
|
|
|
GstVTEncFrame *frame;
|
|
|
|
|
|
|
|
frame = g_slice_new (GstVTEncFrame);
|
|
|
|
frame->buf = gst_buffer_ref (buf);
|
2013-10-02 02:49:34 +00:00
|
|
|
if (!gst_video_frame_map (&frame->videoframe, video_info, buf, GST_MAP_READ)) {
|
|
|
|
gst_buffer_unref (frame->buf);
|
|
|
|
g_slice_free (GstVTEncFrame, frame);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-02-16 01:37:11 +00:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_frame_free (GstVTEncFrame * frame)
|
|
|
|
{
|
2013-10-02 02:49:34 +00:00
|
|
|
gst_video_frame_unmap (&frame->videoframe);
|
2013-02-16 01:37:11 +00:00
|
|
|
gst_buffer_unref (frame->buf);
|
|
|
|
g_slice_free (GstVTEncFrame, frame);
|
|
|
|
}
|
|
|
|
|
2014-06-16 10:35:13 +00:00
|
|
|
static void
|
|
|
|
gst_pixel_buffer_release_cb (void *releaseRefCon, const void *dataPtr,
|
|
|
|
size_t dataSize, size_t numberOfPlanes, const void *planeAddresses[])
|
|
|
|
{
|
|
|
|
GstVTEncFrame *frame = (GstVTEncFrame *) releaseRefCon;
|
|
|
|
gst_vtenc_frame_free (frame);
|
|
|
|
}
|
2014-09-19 11:36:39 +00:00
|
|
|
#endif
|
2014-06-16 10:35:13 +00:00
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
static void
|
|
|
|
gst_vtenc_register (GstPlugin * plugin,
|
|
|
|
const GstVTEncoderDetails * codec_details)
|
|
|
|
{
|
|
|
|
GTypeInfo type_info = {
|
|
|
|
sizeof (GstVTEncClass),
|
|
|
|
(GBaseInitFunc) gst_vtenc_base_init,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_vtenc_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2014-09-16 13:13:16 +00:00
|
|
|
sizeof (GstVTEnc),
|
2010-10-27 17:30:11 +00:00
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_vtenc_init,
|
|
|
|
};
|
|
|
|
gchar *type_name;
|
|
|
|
GType type;
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
type_name = g_strdup_printf ("vtenc_%s", codec_details->element_name);
|
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
type =
|
|
|
|
g_type_register_static (GST_TYPE_VIDEO_ENCODER, type_name, &type_info, 0);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
g_type_set_qdata (type, GST_VTENC_CODEC_DETAILS_QDATA,
|
|
|
|
(gpointer) codec_details);
|
|
|
|
|
2014-12-10 13:02:26 +00:00
|
|
|
result = gst_element_register (plugin, type_name, GST_RANK_PRIMARY, type);
|
2010-10-27 17:30:11 +00:00
|
|
|
if (!result) {
|
|
|
|
GST_ERROR_OBJECT (plugin, "failed to register element %s", type_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (type_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GstVTEncoderDetails gst_vtenc_codecs[] = {
|
2016-06-01 11:43:32 +00:00
|
|
|
{"H.264", "h264", "video/x-h264", kCMVideoCodecType_H264, FALSE},
|
|
|
|
#ifndef HAVE_IOS
|
|
|
|
{"H.264 (HW only)", "h264_hw", "video/x-h264", kCMVideoCodecType_H264, TRUE},
|
|
|
|
#endif
|
2010-10-27 17:30:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_vtenc_register_elements (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_vtenc_debug, "vtenc",
|
|
|
|
0, "Apple VideoToolbox Encoder Wrapper");
|
|
|
|
|
|
|
|
for (i = 0; i != G_N_ELEMENTS (gst_vtenc_codecs); i++)
|
|
|
|
gst_vtenc_register (plugin, &gst_vtenc_codecs[i]);
|
|
|
|
}
|