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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vtenc.h"
|
|
|
|
|
|
|
|
#include "coremediabuffer.h"
|
|
|
|
#include "vtutil.h"
|
|
|
|
|
|
|
|
#define VTENC_DEFAULT_USAGE 6 /* Profile: Baseline Level: 2.1 */
|
|
|
|
#define VTENC_DEFAULT_BITRATE 768
|
|
|
|
|
|
|
|
#define VTENC_MIN_RESET_INTERVAL (GST_SECOND / 2)
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_USAGE,
|
|
|
|
PROP_BITRATE
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
|
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_expected_duration (GstVTEnc * self,
|
2010-11-02 22:08:30 +00:00
|
|
|
VTCompressionSessionRef session, gdouble duration);
|
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);
|
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
|
|
|
|
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[]);
|
|
|
|
|
2013-10-02 02:49:43 +00:00
|
|
|
static GstStaticCaps sink_caps =
|
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ NV12, I420 }"));
|
|
|
|
|
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),
|
2010-11-04 13:24:36 +00:00
|
|
|
"stream-format", G_TYPE_STRING, "avc", 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_BITRATE,
|
|
|
|
g_param_spec_uint ("bitrate", "Bitrate",
|
|
|
|
"Target video bitrate in kbps",
|
|
|
|
1, G_MAXUINT, VTENC_DEFAULT_BITRATE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtenc_init (GstVTEnc * self)
|
|
|
|
{
|
|
|
|
GstVTEncClass *klass = (GstVTEncClass *) G_OBJECT_GET_CLASS (self);
|
|
|
|
|
|
|
|
self->details = GST_VTENC_CLASS_GET_CODEC_DETAILS (klass);
|
|
|
|
|
|
|
|
/* These could be controlled by properties later */
|
|
|
|
self->dump_properties = FALSE;
|
|
|
|
self->dump_attributes = FALSE;
|
2013-10-02 02:49:34 +00:00
|
|
|
|
|
|
|
self->session = NULL;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
g_value_set_uint (value, gst_vtenc_get_bitrate (self) * 8 / 1000);
|
|
|
|
break;
|
|
|
|
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:
|
|
|
|
gst_vtenc_set_bitrate (self, g_value_get_uint (value) * 1000 / 8);
|
|
|
|
break;
|
|
|
|
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-16 13:51:28 +00:00
|
|
|
self->cur_outframes = g_ptr_array_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);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
gst_vtenc_destroy_session (self, &self->session);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
if (self->options != NULL) {
|
|
|
|
CFRelease (self->options);
|
|
|
|
self->options = NULL;
|
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 = 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-16 13:51:28 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
2013-02-16 01:37:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
g_ptr_array_free (self->cur_outframes, TRUE);
|
|
|
|
self->cur_outframes = NULL;
|
|
|
|
|
|
|
|
return TRUE;
|
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
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
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
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
gst_vtenc_destroy_session (self, &self->session);
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
session = gst_vtenc_create_session (self);
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
|
|
|
|
self->session = session;
|
|
|
|
|
|
|
|
if (self->options != NULL)
|
|
|
|
CFRelease (self->options);
|
|
|
|
self->options = CFDictionaryCreateMutable (NULL, 0,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-02-16 01:37:11 +00:00
|
|
|
gpointer codec_data;
|
|
|
|
gsize codec_data_size;
|
|
|
|
GstBuffer *codec_data_buf;
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
2010-10-27 17:30:11 +00:00
|
|
|
CFMutableDictionaryRef pb_attrs;
|
2014-09-16 12:02:46 +00:00
|
|
|
OSStatus status;
|
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-09-16 12:02:46 +00:00
|
|
|
self->details->format_id, NULL, 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);
|
|
|
|
if (status != noErr)
|
2010-10-27 17:30:11 +00:00
|
|
|
goto beach;
|
|
|
|
|
|
|
|
if (self->dump_properties) {
|
|
|
|
gst_vtenc_session_dump_properties (self, session);
|
|
|
|
|
|
|
|
self->dump_properties = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_vtenc_session_configure_expected_framerate (self, session,
|
|
|
|
(gdouble) self->negotiated_fps_n / (gdouble) self->negotiated_fps_d);
|
|
|
|
gst_vtenc_session_configure_expected_duration (self, session,
|
|
|
|
(gdouble) self->negotiated_fps_d / (gdouble) self->negotiated_fps_n);
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
status = VTSessionSetProperty (session,
|
|
|
|
kVTCompressionPropertyKey_ProfileLevel,
|
|
|
|
kVTProfileLevel_H264_Baseline_3_0);
|
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
|
|
|
|
|
|
|
gst_vtenc_session_configure_max_keyframe_interval (self, session, 0);
|
|
|
|
gst_vtenc_session_configure_max_keyframe_interval_duration (self, session,
|
|
|
|
G_MAXDOUBLE);
|
|
|
|
|
|
|
|
gst_vtenc_session_configure_bitrate (self, session,
|
|
|
|
gst_vtenc_get_bitrate (self));
|
|
|
|
|
|
|
|
beach:
|
|
|
|
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_expected_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_ExpectedDuration, duration);
|
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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2014-09-16 12:02:46 +00:00
|
|
|
OSStatus vt_status;
|
2010-11-10 22:16:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
guint i;
|
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)) {
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
if (self->options != NULL) {
|
|
|
|
GST_INFO_OBJECT (self, "received PLI, will force intra");
|
|
|
|
CFDictionaryAddValue (self->options,
|
|
|
|
kVTEncodeFrameOptionKey_ForceKeyFrame, kCFBooleanTrue);
|
|
|
|
} else {
|
|
|
|
GST_INFO_OBJECT (self,
|
|
|
|
"received PLI but encode not yet started, ignoring");
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
ts = CMTimeMake (GST_TIME_AS_MSECONDS (frame->pts), 1000);
|
|
|
|
duration = CMTimeMake (GST_TIME_AS_MSECONDS (frame->duration), 1000);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
|
|
|
|
self->expect_keyframe = CFDictionaryContainsKey (self->options,
|
2014-09-16 12:02:46 +00:00
|
|
|
kVTEncodeFrameOptionKey_ForceKeyFrame);
|
2010-11-08 20:22:13 +00:00
|
|
|
if (self->expect_keyframe)
|
2010-10-27 17:30:11 +00:00
|
|
|
gst_vtenc_clear_cached_caps_downstream (self);
|
|
|
|
|
2014-09-16 12:02:46 +00:00
|
|
|
vt_status = VTCompressionSessionEncodeFrame (self->session,
|
2014-09-16 13:51:28 +00:00
|
|
|
pbuf, ts, duration, self->options,
|
|
|
|
GINT_TO_POINTER (frame->system_frame_number), NULL);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
if (vt_status != 0) {
|
|
|
|
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 12:02:46 +00:00
|
|
|
VTCompressionSessionCompleteFrames (self->session, kCMTimeInvalid);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
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
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
if (self->cur_outframes->len > 0) {
|
|
|
|
GstVideoCodecFrame *outframe = g_ptr_array_index (self->cur_outframes,
|
|
|
|
0);
|
|
|
|
meta = gst_buffer_get_core_media_meta (outframe->output_buffer);
|
2013-02-16 01:37:11 +00:00
|
|
|
if (!gst_vtenc_negotiate_downstream (self, meta->sample_buf))
|
2010-12-09 23:58:58 +00:00
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
for (i = 0; i != self->cur_outframes->len; i++) {
|
|
|
|
GstVideoCodecFrame *outframe = g_ptr_array_index (self->cur_outframes, i);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
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
|
|
|
}
|
2014-09-16 13:51:28 +00:00
|
|
|
g_ptr_array_set_size (self->cur_outframes, 0);
|
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
|
|
|
|
|
|
|
/* 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-11-08 20:22:13 +00:00
|
|
|
if (self->expect_keyframe) {
|
|
|
|
if (!is_keyframe)
|
|
|
|
goto beach;
|
|
|
|
CFDictionaryRemoveValue (self->options,
|
2014-09-16 12:02:46 +00:00
|
|
|
kVTEncodeFrameOptionKey_ForceKeyFrame);
|
2010-11-08 20:22:13 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
self->expect_keyframe = FALSE;
|
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
frame =
|
|
|
|
gst_video_encoder_get_frame (GST_VIDEO_ENCODER_CAST (self),
|
|
|
|
GPOINTER_TO_INT (sourceFrameRefCon));
|
|
|
|
|
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 */
|
2014-09-16 13:51:28 +00:00
|
|
|
frame->output_buffer = gst_core_media_buffer_new (sampleBuffer, FALSE);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-09-16 13:51:28 +00:00
|
|
|
g_ptr_array_add (self->cur_outframes, frame);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
beach:
|
2014-09-16 12:02:46 +00:00
|
|
|
return;
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
result = gst_element_register (plugin, type_name, GST_RANK_NONE, type);
|
|
|
|
if (!result) {
|
|
|
|
GST_ERROR_OBJECT (plugin, "failed to register element %s", type_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (type_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GstVTEncoderDetails gst_vtenc_codecs[] = {
|
2014-09-16 12:02:46 +00:00
|
|
|
{"H.264", "h264", "video/x-h264", kCMVideoCodecType_H264},
|
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]);
|
|
|
|
}
|