2013-12-07 22:55:13 +00:00
|
|
|
/* GStreamer
|
2013-02-16 01:44:19 +00:00
|
|
|
* Copyright (C) 2010, 2013 Ole André Vadla Ravnås <oleavr@soundrop.com>
|
2013-12-07 22:55:13 +00:00
|
|
|
* Copyright (C) 2012, 2013 Alessandro Decina <alessandro.d@gmail.com>
|
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
|
2013-12-07 22:55:13 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
|
|
|
|
* Boston, MA 02110-1335, USA.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* SECTION:element-gstvtdec
|
|
|
|
*
|
|
|
|
* Apple VideoToolbox based decoder.
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example launch line</title>
|
|
|
|
* |[
|
|
|
|
* gst-launch -v filesrc location=file.mov ! qtdemux ! queue ! h264parse ! vtdec ! videoconvert ! autovideosink
|
|
|
|
* ]|
|
|
|
|
* Decode h264 video from a mov file.
|
|
|
|
* </refsect2>
|
2010-10-27 17:30:11 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
#include <string.h>
|
2013-12-07 22:55:13 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <gst/video/video.h>
|
|
|
|
#include <gst/video/gstvideodecoder.h>
|
2015-01-15 03:09:43 +00:00
|
|
|
#include <gst/gl/gstglcontext.h>
|
2013-12-07 22:55:13 +00:00
|
|
|
#include "vtdec.h"
|
2010-10-27 17:30:11 +00:00
|
|
|
#include "vtutil.h"
|
2013-12-07 22:55:13 +00:00
|
|
|
#include "corevideobuffer.h"
|
2015-01-15 03:09:43 +00:00
|
|
|
#include "coremediabuffer.h"
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_vtdec_debug_category);
|
|
|
|
#define GST_CAT_DEFAULT gst_vtdec_debug_category
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
static void gst_vtdec_finalize (GObject * object);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
static gboolean gst_vtdec_start (GstVideoDecoder * decoder);
|
|
|
|
static gboolean gst_vtdec_stop (GstVideoDecoder * decoder);
|
2015-01-15 03:09:43 +00:00
|
|
|
static gboolean gst_vtdec_decide_allocation (GstVideoDecoder * decoder,
|
|
|
|
GstQuery * query);
|
2013-12-07 22:55:13 +00:00
|
|
|
static gboolean gst_vtdec_set_format (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecState * state);
|
|
|
|
static gboolean gst_vtdec_flush (GstVideoDecoder * decoder);
|
|
|
|
static GstFlowReturn gst_vtdec_finish (GstVideoDecoder * decoder);
|
|
|
|
static GstFlowReturn gst_vtdec_handle_frame (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecFrame * frame);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
static gboolean gst_vtdec_create_session (GstVtdec * vtdec,
|
|
|
|
GstVideoFormat format);
|
2013-12-07 22:55:13 +00:00
|
|
|
static void gst_vtdec_invalidate_session (GstVtdec * vtdec);
|
|
|
|
static CMSampleBufferRef cm_sample_buffer_from_gst_buffer (GstVtdec * vtdec,
|
2012-03-03 18:03:11 +00:00
|
|
|
GstBuffer * buf);
|
2013-12-07 22:55:13 +00:00
|
|
|
static GstFlowReturn gst_vtdec_push_frames_if_needed (GstVtdec * vtdec,
|
|
|
|
gboolean drain, gboolean flush);
|
|
|
|
static CMFormatDescriptionRef create_format_description (GstVtdec * vtdec,
|
|
|
|
CMVideoCodecType cm_format);
|
2010-11-02 21:53:33 +00:00
|
|
|
static CMFormatDescriptionRef
|
2013-12-07 22:55:13 +00:00
|
|
|
create_format_description_from_codec_data (GstVtdec * vtdec,
|
|
|
|
CMVideoCodecType cm_format, GstBuffer * codec_data);
|
|
|
|
static void gst_vtdec_session_output_callback (void
|
|
|
|
*decompression_output_ref_con, void *source_frame_ref_con, OSStatus status,
|
|
|
|
VTDecodeInfoFlags info_flags, CVImageBufferRef image_buffer, CMTime pts,
|
|
|
|
CMTime duration);
|
2013-12-11 06:58:23 +00:00
|
|
|
static gboolean compute_h264_decode_picture_buffer_length (GstVtdec * vtdec,
|
|
|
|
GstBuffer * codec_data, int *length);
|
2013-12-11 07:11:50 +00:00
|
|
|
static gboolean gst_vtdec_compute_reorder_queue_length (GstVtdec * vtdec,
|
2013-12-11 07:10:19 +00:00
|
|
|
CMVideoCodecType cm_format, GstBuffer * codec_data);
|
|
|
|
static void gst_vtdec_set_latency (GstVtdec * vtdec);
|
2013-12-07 22:55:13 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_vtdec_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/x-h264, stream-format=avc, alignment=au;"
|
2013-12-08 14:31:09 +00:00
|
|
|
"video/mpeg, mpegversion=2;" "image/jpeg")
|
2013-12-07 22:55:13 +00:00
|
|
|
);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-17 09:01:42 +00:00
|
|
|
/* define EnableHardwareAcceleratedVideoDecoder in < 10.9 */
|
2014-09-15 12:12:31 +00:00
|
|
|
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED < 1090
|
2013-12-17 09:01:42 +00:00
|
|
|
const CFStringRef
|
|
|
|
kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder =
|
|
|
|
CFSTR ("EnableHardwareAcceleratedVideoDecoder");
|
|
|
|
#endif
|
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
#ifdef HAVE_IOS
|
|
|
|
#define GST_VTDEC_VIDEO_FORMAT_STR "NV12"
|
|
|
|
#else
|
|
|
|
#define GST_VTDEC_VIDEO_FORMAT_STR "UYVY"
|
|
|
|
#endif
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
#define VIDEO_SRC_CAPS \
|
2015-01-15 03:09:43 +00:00
|
|
|
GST_VIDEO_CAPS_MAKE(GST_VTDEC_VIDEO_FORMAT_STR) ";" \
|
|
|
|
GST_VIDEO_CAPS_MAKE_WITH_FEATURES \
|
2015-01-29 13:28:18 +00:00
|
|
|
(GST_CAPS_FEATURE_MEMORY_GL_MEMORY, \
|
2015-01-15 03:09:43 +00:00
|
|
|
"RGBA") ";"
|
2013-12-07 22:55:13 +00:00
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstVtdec, gst_vtdec, GST_TYPE_VIDEO_DECODER,
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_vtdec_debug_category, "vtdec", 0,
|
|
|
|
"debug category for vtdec element"));
|
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
static void
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_vtdec_class_init (GstVtdecClass * klass)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstVideoDecoderClass *video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
|
|
|
|
|
|
|
|
/* Setting up pads and setting metadata should be moved to
|
|
|
|
base_class_init if you intend to subclass this class. */
|
|
|
|
gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
|
|
|
|
gst_static_pad_template_get (&gst_vtdec_sink_template));
|
|
|
|
gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
|
|
|
|
gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
|
|
|
gst_caps_from_string (VIDEO_SRC_CAPS)));
|
|
|
|
|
|
|
|
gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
|
|
|
|
"Apple VideoToolbox decoder",
|
|
|
|
"Codec/Decoder/Video",
|
|
|
|
"Apple VideoToolbox Decoder",
|
|
|
|
"Ole André Vadla Ravnås <oleavr@soundrop.com>; "
|
|
|
|
"Alessandro Decina <alessandro.d@gmail.com>");
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_vtdec_finalize;
|
|
|
|
video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_vtdec_start);
|
|
|
|
video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_vtdec_stop);
|
2015-01-15 03:09:43 +00:00
|
|
|
video_decoder_class->decide_allocation =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_vtdec_decide_allocation);
|
2013-12-07 22:55:13 +00:00
|
|
|
video_decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_vtdec_set_format);
|
|
|
|
video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_vtdec_flush);
|
|
|
|
video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_vtdec_finish);
|
|
|
|
video_decoder_class->handle_frame =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_vtdec_handle_frame);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_vtdec_init (GstVtdec * vtdec)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
vtdec->reorder_queue = g_async_queue_new ();
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
void
|
|
|
|
gst_vtdec_finalize (GObject * object)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
GstVtdec *vtdec = GST_VTDEC (object);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec, "finalize");
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
g_async_queue_unref (vtdec->reorder_queue);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
G_OBJECT_CLASS (gst_vtdec_parent_class)->finalize (object);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
static gboolean
|
|
|
|
gst_vtdec_start (GstVideoDecoder * decoder)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec, "start");
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
return TRUE;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_vtdec_stop (GstVideoDecoder * decoder)
|
2012-03-03 18:03:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
if (vtdec->session)
|
|
|
|
gst_vtdec_invalidate_session (vtdec);
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
if (vtdec->texture_cache)
|
|
|
|
gst_core_video_texture_cache_free (vtdec->texture_cache);
|
|
|
|
vtdec->texture_cache = NULL;
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec, "stop");
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
return TRUE;
|
2012-03-03 18:03:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
static gboolean
|
|
|
|
gst_vtdec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
|
|
|
|
|
|
|
ret =
|
|
|
|
GST_VIDEO_DECODER_CLASS (gst_vtdec_parent_class)->decide_allocation
|
|
|
|
(decoder, query);
|
|
|
|
if (ret) {
|
|
|
|
guint idx;
|
|
|
|
|
|
|
|
if (gst_query_find_allocation_meta (query,
|
|
|
|
GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
|
|
|
|
GstGLContext *context;
|
|
|
|
const GstStructure *upload_meta_params;
|
|
|
|
|
|
|
|
gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
|
|
|
|
if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
|
|
|
|
GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
|
|
|
|
vtdec->texture_cache = gst_core_video_texture_cache_new (context);
|
|
|
|
gst_object_unref (context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstVideoFormat
|
|
|
|
gst_vtdec_negotiate_output_format (GstVtdec * vtdec)
|
|
|
|
{
|
|
|
|
GstCaps *caps = NULL;
|
|
|
|
GstVideoFormat format;
|
|
|
|
GstStructure *structure;
|
|
|
|
const gchar *s;
|
|
|
|
|
|
|
|
caps = gst_pad_get_allowed_caps (GST_VIDEO_DECODER_SRC_PAD (vtdec));
|
|
|
|
if (!caps)
|
|
|
|
caps = gst_pad_query_caps (GST_VIDEO_DECODER_SRC_PAD (vtdec), NULL);
|
|
|
|
caps = gst_caps_truncate (caps);
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
s = gst_structure_get_string (structure, "format");
|
|
|
|
format = gst_video_format_from_string (s);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2012-03-03 18:03:11 +00:00
|
|
|
static gboolean
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_vtdec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
GstStructure *structure;
|
2013-12-11 17:52:53 +00:00
|
|
|
CMVideoCodecType cm_format = 0;
|
2013-12-07 22:55:13 +00:00
|
|
|
CMFormatDescriptionRef format_description = NULL;
|
|
|
|
const char *caps_name;
|
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
2015-01-15 03:09:43 +00:00
|
|
|
GstVideoFormat output_format;
|
|
|
|
GstVideoCodecState *output_state;
|
2013-12-07 22:55:13 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "set_format");
|
|
|
|
|
|
|
|
structure = gst_caps_get_structure (state->caps, 0);
|
|
|
|
caps_name = gst_structure_get_name (structure);
|
|
|
|
if (!strcmp (caps_name, "video/x-h264")) {
|
|
|
|
cm_format = kCMVideoCodecType_H264;
|
|
|
|
} else if (!strcmp (caps_name, "video/mpeg")) {
|
2013-12-08 14:31:09 +00:00
|
|
|
cm_format = kCMVideoCodecType_MPEG2Video;
|
2013-12-07 22:55:13 +00:00
|
|
|
} else if (!strcmp (caps_name, "image/jpeg")) {
|
|
|
|
cm_format = kCMVideoCodecType_JPEG;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 06:58:23 +00:00
|
|
|
if (cm_format == kCMVideoCodecType_H264 && state->codec_data == NULL) {
|
|
|
|
GST_INFO_OBJECT (vtdec, "no codec data, wait for one");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vtdec->session)
|
|
|
|
gst_vtdec_invalidate_session (vtdec);
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_video_info_from_caps (&vtdec->video_info, state->caps);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-11 07:11:50 +00:00
|
|
|
if (!gst_vtdec_compute_reorder_queue_length (vtdec, cm_format,
|
2013-12-11 07:10:19 +00:00
|
|
|
state->codec_data))
|
|
|
|
return FALSE;
|
|
|
|
gst_vtdec_set_latency (vtdec);
|
2013-12-11 06:58:23 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
if (state->codec_data) {
|
|
|
|
format_description = create_format_description_from_codec_data (vtdec,
|
|
|
|
cm_format, state->codec_data);
|
|
|
|
} else {
|
|
|
|
format_description = create_format_description (vtdec, cm_format);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
if (vtdec->format_description)
|
|
|
|
CFRelease (vtdec->format_description);
|
|
|
|
vtdec->format_description = format_description;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
output_format = gst_vtdec_negotiate_output_format (vtdec);
|
|
|
|
|
|
|
|
if (!gst_vtdec_create_session (vtdec, output_format))
|
2010-10-27 17:30:11 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
output_state = gst_video_decoder_set_output_state (decoder,
|
|
|
|
output_format, vtdec->video_info.width, vtdec->video_info.height, state);
|
|
|
|
output_state->caps = gst_video_info_to_caps (&output_state->info);
|
|
|
|
if (output_state->info.finfo->format == GST_VIDEO_FORMAT_RGBA) {
|
|
|
|
gst_caps_set_features (output_state->caps, 0,
|
2015-01-29 13:28:18 +00:00
|
|
|
gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, NULL));
|
2015-01-15 03:09:43 +00:00
|
|
|
}
|
2013-12-07 22:55:13 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_vtdec_flush (GstVideoDecoder * decoder)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec, "flush");
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_vtdec_push_frames_if_needed (vtdec, FALSE, TRUE);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
return TRUE;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_vtdec_finish (GstVideoDecoder * decoder)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec, "finish");
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
return gst_vtdec_push_frames_if_needed (vtdec, TRUE, FALSE);
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_vtdec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
CMSampleBufferRef cm_sample_buffer = NULL;
|
|
|
|
VTDecodeFrameFlags input_flags, output_flags;
|
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
int decode_frame_number = frame->decode_frame_number;
|
|
|
|
|
2013-12-08 14:24:58 +00:00
|
|
|
if (vtdec->format_description == NULL) {
|
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
GST_LOG_OBJECT (vtdec, "got input frame %d", decode_frame_number);
|
|
|
|
|
|
|
|
ret = gst_vtdec_push_frames_if_needed (vtdec, FALSE, FALSE);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* don't bother enabling kVTDecodeFrame_EnableTemporalProcessing at all since
|
|
|
|
* it's not mandatory for the underlying VT codec to respect it. KISS and do
|
|
|
|
* reordering ourselves.
|
|
|
|
*/
|
|
|
|
input_flags = kVTDecodeFrame_EnableAsynchronousDecompression;
|
|
|
|
output_flags = 0;
|
|
|
|
|
|
|
|
cm_sample_buffer =
|
|
|
|
cm_sample_buffer_from_gst_buffer (vtdec, frame->input_buffer);
|
|
|
|
status =
|
|
|
|
VTDecompressionSessionDecodeFrame (vtdec->session, cm_sample_buffer,
|
|
|
|
input_flags, frame, NULL);
|
|
|
|
if (status != noErr && FALSE)
|
|
|
|
goto error;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
GST_LOG_OBJECT (vtdec, "submitted input frame %d", decode_frame_number);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-08 14:24:58 +00:00
|
|
|
out:
|
|
|
|
if (cm_sample_buffer)
|
|
|
|
CFRelease (cm_sample_buffer);
|
2013-12-07 22:55:13 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
error:
|
|
|
|
GST_ELEMENT_ERROR (vtdec, STREAM, DECODE, (NULL),
|
2014-09-16 12:48:11 +00:00
|
|
|
("VTDecompressionSessionDecodeFrame returned %d", (int) status));
|
2013-12-07 22:55:13 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto out;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
static gboolean
|
2015-01-15 03:09:43 +00:00
|
|
|
gst_vtdec_create_session (GstVtdec * vtdec, GstVideoFormat format)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
CFMutableDictionaryRef output_image_buffer_attrs;
|
|
|
|
VTDecompressionOutputCallbackRecord callback;
|
2013-12-17 09:01:42 +00:00
|
|
|
CFMutableDictionaryRef videoDecoderSpecification;
|
2010-11-02 21:53:33 +00:00
|
|
|
OSStatus status;
|
2015-01-15 03:09:43 +00:00
|
|
|
guint32 cv_format;
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
cv_format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_UYVY:
|
|
|
|
cv_format = kCVPixelFormatType_422YpCbCr8;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_RGBA:
|
2015-01-29 13:28:18 +00:00
|
|
|
#ifdef HAVE_IOS
|
|
|
|
cv_format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
|
|
|
|
#else
|
|
|
|
cv_format = kCVPixelFormatType_422YpCbCr8;
|
|
|
|
#endif
|
2015-01-15 03:09:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warn_if_reached ();
|
|
|
|
break;
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-17 09:01:42 +00:00
|
|
|
videoDecoderSpecification =
|
|
|
|
CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
2014-09-15 12:42:41 +00:00
|
|
|
|
|
|
|
/* This is the default on iOS and the key does not exist there */
|
|
|
|
#ifndef HAVE_IOS
|
2013-12-17 09:01:42 +00:00
|
|
|
gst_vtutil_dict_set_boolean (videoDecoderSpecification,
|
|
|
|
kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder, TRUE);
|
2014-09-15 12:42:41 +00:00
|
|
|
#endif
|
2013-12-17 09:01:42 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
output_image_buffer_attrs =
|
|
|
|
CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
gst_vtutil_dict_set_i32 (output_image_buffer_attrs,
|
2015-01-15 03:09:43 +00:00
|
|
|
kCVPixelBufferPixelFormatTypeKey, cv_format);
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_vtutil_dict_set_i32 (output_image_buffer_attrs, kCVPixelBufferWidthKey,
|
|
|
|
vtdec->video_info.width);
|
|
|
|
gst_vtutil_dict_set_i32 (output_image_buffer_attrs, kCVPixelBufferHeightKey,
|
|
|
|
vtdec->video_info.height);
|
|
|
|
|
|
|
|
callback.decompressionOutputCallback = gst_vtdec_session_output_callback;
|
|
|
|
callback.decompressionOutputRefCon = vtdec;
|
|
|
|
|
|
|
|
status = VTDecompressionSessionCreate (NULL, vtdec->format_description,
|
2013-12-17 09:01:42 +00:00
|
|
|
videoDecoderSpecification, output_image_buffer_attrs, &callback,
|
|
|
|
&vtdec->session);
|
2013-12-07 22:55:13 +00:00
|
|
|
|
|
|
|
CFRelease (output_image_buffer_attrs);
|
|
|
|
|
|
|
|
if (status != noErr) {
|
|
|
|
GST_ELEMENT_ERROR (vtdec, RESOURCE, FAILED, (NULL),
|
2014-09-16 12:48:11 +00:00
|
|
|
("VTDecompressionSessionCreate returned %d", (int) status));
|
2013-12-07 22:55:13 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtdec_invalidate_session (GstVtdec * vtdec)
|
|
|
|
{
|
|
|
|
g_return_if_fail (vtdec->session);
|
|
|
|
|
|
|
|
VTDecompressionSessionInvalidate (vtdec->session);
|
|
|
|
CFRelease (vtdec->session);
|
|
|
|
vtdec->session = NULL;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2010-11-02 21:53:33 +00:00
|
|
|
static CMFormatDescriptionRef
|
2013-12-07 22:55:13 +00:00
|
|
|
create_format_description (GstVtdec * vtdec, CMVideoCodecType cm_format)
|
|
|
|
{
|
2013-12-08 14:25:27 +00:00
|
|
|
OSStatus status;
|
|
|
|
CMFormatDescriptionRef format_description;
|
|
|
|
|
|
|
|
status = CMVideoFormatDescriptionCreate (NULL,
|
|
|
|
cm_format, vtdec->video_info.width, vtdec->video_info.height,
|
|
|
|
NULL, &format_description);
|
|
|
|
if (status != noErr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return format_description;
|
2013-12-07 22:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static CMFormatDescriptionRef
|
|
|
|
create_format_description_from_codec_data (GstVtdec * vtdec,
|
|
|
|
CMVideoCodecType cm_format, GstBuffer * codec_data)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2010-11-02 21:53:33 +00:00
|
|
|
CMFormatDescriptionRef fmt_desc;
|
2013-04-25 09:17:16 +00:00
|
|
|
CFMutableDictionaryRef extensions, par, atoms;
|
2012-03-03 18:03:11 +00:00
|
|
|
GstMapInfo map;
|
2013-04-25 09:17:16 +00:00
|
|
|
OSStatus status;
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
/* Extensions dict */
|
|
|
|
extensions =
|
|
|
|
CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
gst_vtutil_dict_set_string (extensions,
|
|
|
|
CFSTR ("CVImageBufferChromaLocationBottomField"), "left");
|
|
|
|
gst_vtutil_dict_set_string (extensions,
|
|
|
|
CFSTR ("CVImageBufferChromaLocationTopField"), "left");
|
|
|
|
gst_vtutil_dict_set_boolean (extensions, CFSTR ("FullRangeVideo"), FALSE);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-04-25 09:17:16 +00:00
|
|
|
/* CVPixelAspectRatio dict */
|
|
|
|
par = CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
gst_vtutil_dict_set_i32 (par, CFSTR ("HorizontalSpacing"),
|
2013-12-07 22:55:13 +00:00
|
|
|
vtdec->video_info.par_n);
|
2013-04-25 09:17:16 +00:00
|
|
|
gst_vtutil_dict_set_i32 (par, CFSTR ("VerticalSpacing"),
|
2013-12-07 22:55:13 +00:00
|
|
|
vtdec->video_info.par_d);
|
|
|
|
gst_vtutil_dict_set_object (extensions, CFSTR ("CVPixelAspectRatio"),
|
|
|
|
(CFTypeRef *) par);
|
2013-04-25 09:17:16 +00:00
|
|
|
|
|
|
|
/* SampleDescriptionExtensionAtoms dict */
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_buffer_map (codec_data, &map, GST_MAP_READ);
|
2013-04-25 09:17:16 +00:00
|
|
|
atoms = CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
gst_vtutil_dict_set_data (atoms, CFSTR ("avcC"), map.data, map.size);
|
|
|
|
gst_vtutil_dict_set_object (extensions,
|
|
|
|
CFSTR ("SampleDescriptionExtensionAtoms"), (CFTypeRef *) atoms);
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_buffer_unmap (codec_data, &map);
|
2013-04-25 09:17:16 +00:00
|
|
|
|
|
|
|
status = CMVideoFormatDescriptionCreate (NULL,
|
2013-12-07 22:55:13 +00:00
|
|
|
cm_format, vtdec->video_info.width, vtdec->video_info.height,
|
2013-04-25 09:17:16 +00:00
|
|
|
extensions, &fmt_desc);
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2010-11-02 21:53:33 +00:00
|
|
|
if (status == noErr)
|
2010-10-27 17:30:11 +00:00
|
|
|
return fmt_desc;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-02 21:53:33 +00:00
|
|
|
static CMSampleBufferRef
|
2013-12-07 22:55:13 +00:00
|
|
|
cm_sample_buffer_from_gst_buffer (GstVtdec * vtdec, GstBuffer * buf)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2010-11-02 21:53:33 +00:00
|
|
|
OSStatus status;
|
|
|
|
CMBlockBufferRef bbuf = NULL;
|
|
|
|
CMSampleBufferRef sbuf = NULL;
|
2012-03-03 18:03:11 +00:00
|
|
|
GstMapInfo map;
|
2013-04-26 15:40:00 +00:00
|
|
|
CMSampleTimingInfo sample_timing;
|
|
|
|
CMSampleTimingInfo time_array[1];
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
g_return_val_if_fail (vtdec->format_description, NULL);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2012-03-03 18:03:11 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
/* create a block buffer, the CoreMedia equivalent of GstMemory */
|
2013-04-24 20:15:01 +00:00
|
|
|
status = CMBlockBufferCreateWithMemoryBlock (NULL,
|
2012-03-03 18:03:11 +00:00
|
|
|
map.data, (gint64) map.size, kCFAllocatorNull, NULL, 0, (gint64) map.size,
|
|
|
|
FALSE, &bbuf);
|
|
|
|
|
|
|
|
gst_buffer_unmap (buf, &map);
|
|
|
|
|
2010-11-02 21:53:33 +00:00
|
|
|
if (status != noErr)
|
2013-12-07 22:55:13 +00:00
|
|
|
goto block_error;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
/* create a sample buffer, the CoreMedia equivalent of GstBuffer */
|
2014-09-17 11:54:39 +00:00
|
|
|
if (GST_BUFFER_DURATION_IS_VALID (buf))
|
|
|
|
sample_timing.duration = CMTimeMake (GST_BUFFER_DURATION (buf), GST_SECOND);
|
|
|
|
else
|
|
|
|
sample_timing.duration = kCMTimeInvalid;
|
|
|
|
|
|
|
|
if (GST_BUFFER_PTS_IS_VALID (buf))
|
|
|
|
sample_timing.presentationTimeStamp =
|
|
|
|
CMTimeMake (GST_BUFFER_PTS (buf), GST_SECOND);
|
|
|
|
else
|
|
|
|
sample_timing.presentationTimeStamp = kCMTimeInvalid;
|
|
|
|
|
|
|
|
if (GST_BUFFER_DTS_IS_VALID (buf))
|
|
|
|
sample_timing.decodeTimeStamp =
|
|
|
|
CMTimeMake (GST_BUFFER_DTS (buf), GST_SECOND);
|
|
|
|
else
|
|
|
|
sample_timing.decodeTimeStamp = kCMTimeInvalid;
|
|
|
|
|
2013-04-26 15:40:00 +00:00
|
|
|
time_array[0] = sample_timing;
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
status =
|
|
|
|
CMSampleBufferCreate (NULL, bbuf, TRUE, 0, 0, vtdec->format_description,
|
2013-04-26 15:40:00 +00:00
|
|
|
1, 1, time_array, 0, NULL, &sbuf);
|
2015-01-27 13:53:59 +00:00
|
|
|
CFRelease (bbuf);
|
2010-11-02 21:53:33 +00:00
|
|
|
if (status != noErr)
|
2013-12-07 22:55:13 +00:00
|
|
|
goto sample_error;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
out:
|
2010-10-27 17:30:11 +00:00
|
|
|
return sbuf;
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
block_error:
|
|
|
|
GST_ELEMENT_ERROR (vtdec, RESOURCE, FAILED, (NULL),
|
2014-09-16 12:48:11 +00:00
|
|
|
("CMBlockBufferCreateWithMemoryBlock returned %d", (int) status));
|
2013-12-07 22:55:13 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
sample_error:
|
|
|
|
GST_ELEMENT_ERROR (vtdec, RESOURCE, FAILED, (NULL),
|
2014-09-16 12:48:11 +00:00
|
|
|
("CMSampleBufferCreate returned %d", (int) status));
|
2013-12-07 22:55:13 +00:00
|
|
|
|
|
|
|
if (bbuf)
|
|
|
|
CFRelease (bbuf);
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
sort_frames_by_pts (gconstpointer f1, gconstpointer f2, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstVideoCodecFrame *frame1, *frame2;
|
|
|
|
GstClockTime pts1, pts2;
|
|
|
|
|
|
|
|
frame1 = (GstVideoCodecFrame *) f1;
|
|
|
|
frame2 = (GstVideoCodecFrame *) f2;
|
|
|
|
pts1 = GST_BUFFER_PTS (frame1->output_buffer);
|
|
|
|
pts2 = GST_BUFFER_PTS (frame2->output_buffer);
|
|
|
|
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (pts1) || !GST_CLOCK_TIME_IS_VALID (pts2))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pts1 < pts2)
|
|
|
|
return -1;
|
|
|
|
else if (pts1 == pts2)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_vtdec_session_output_callback (void *decompression_output_ref_con,
|
|
|
|
void *source_frame_ref_con, OSStatus status, VTDecodeInfoFlags info_flags,
|
|
|
|
CVImageBufferRef image_buffer, CMTime pts, CMTime duration)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
GstVtdec *vtdec = (GstVtdec *) decompression_output_ref_con;
|
|
|
|
GstVideoCodecFrame *frame = (GstVideoCodecFrame *) source_frame_ref_con;
|
|
|
|
GstBuffer *buf;
|
2013-12-10 10:10:54 +00:00
|
|
|
GstVideoCodecState *state;
|
2013-12-07 22:55:13 +00:00
|
|
|
|
2014-07-02 03:27:12 +00:00
|
|
|
GST_LOG_OBJECT (vtdec, "got output frame %p %d and VT buffer %p", frame,
|
|
|
|
frame->decode_frame_number, image_buffer);
|
2013-12-07 22:55:13 +00:00
|
|
|
|
|
|
|
if (status != noErr) {
|
2014-09-16 12:48:11 +00:00
|
|
|
GST_ERROR_OBJECT (vtdec, "Error decoding frame %d", (int) status);
|
2013-12-07 22:55:13 +00:00
|
|
|
goto drop;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-02 03:27:12 +00:00
|
|
|
if (image_buffer == NULL) {
|
|
|
|
if (info_flags & kVTDecodeInfo_FrameDropped)
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "Frame dropped by video toolbox");
|
|
|
|
else
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "Decoded frame is NULL");
|
2013-12-07 22:55:13 +00:00
|
|
|
goto drop;
|
2014-07-02 03:27:12 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
/* FIXME: use gst_video_decoder_allocate_output_buffer */
|
2013-12-10 10:10:54 +00:00
|
|
|
state = gst_video_decoder_get_output_state (GST_VIDEO_DECODER (vtdec));
|
2014-07-02 07:54:29 +00:00
|
|
|
if (state == NULL) {
|
|
|
|
GST_WARNING_OBJECT (vtdec, "Output state not configured, release buffer");
|
|
|
|
/* release as this usually means that the baseclass isn't ready to do
|
|
|
|
* the QoS that _drop requires and will lead to an assertion with the
|
|
|
|
* segment.format being undefined */
|
|
|
|
goto release;
|
|
|
|
}
|
2015-01-22 02:17:11 +00:00
|
|
|
buf =
|
|
|
|
gst_core_video_buffer_new (image_buffer, &state->info,
|
|
|
|
vtdec->texture_cache == NULL);
|
2013-12-10 10:10:54 +00:00
|
|
|
gst_video_codec_state_unref (state);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
gst_buffer_copy_into (buf, frame->input_buffer,
|
|
|
|
GST_BUFFER_COPY_METADATA | GST_BUFFER_COPY_FLAGS, 0, -1);
|
|
|
|
GST_BUFFER_PTS (buf) = pts.value;
|
|
|
|
GST_BUFFER_DURATION (buf) = duration.value;
|
2015-01-15 03:09:43 +00:00
|
|
|
frame->output_buffer = buf;
|
2013-12-07 22:55:13 +00:00
|
|
|
g_async_queue_push_sorted (vtdec->reorder_queue, frame,
|
|
|
|
sort_frames_by_pts, NULL);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
drop:
|
|
|
|
GST_WARNING_OBJECT (vtdec, "Frame dropped %p %d", frame,
|
|
|
|
frame->decode_frame_number);
|
|
|
|
gst_video_decoder_drop_frame (GST_VIDEO_DECODER (vtdec), frame);
|
2014-07-02 07:54:29 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
release:
|
|
|
|
GST_WARNING_OBJECT (vtdec, "Frame released %p %d", frame,
|
|
|
|
frame->decode_frame_number);
|
|
|
|
gst_video_decoder_release_frame (GST_VIDEO_DECODER (vtdec), frame);
|
|
|
|
return;
|
2013-12-07 22:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_vtdec_push_frames_if_needed (GstVtdec * vtdec, gboolean drain,
|
|
|
|
gboolean flush)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-07 22:55:13 +00:00
|
|
|
GstVideoCodecFrame *frame;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstVideoDecoder *decoder = GST_VIDEO_DECODER (vtdec);
|
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
/* negotiate now so that we know whether we need to use the GL upload meta or
|
|
|
|
* not */
|
2015-01-29 13:28:18 +00:00
|
|
|
if (gst_pad_check_reconfigure (decoder->srcpad)) {
|
2015-01-15 03:09:43 +00:00
|
|
|
gst_video_decoder_negotiate (decoder);
|
2015-01-29 13:28:18 +00:00
|
|
|
if (vtdec->texture_cache) {
|
|
|
|
GstVideoCodecState *output_state =
|
|
|
|
gst_video_decoder_get_output_state (decoder);
|
|
|
|
gst_core_video_texture_cache_set_format (vtdec->texture_cache,
|
|
|
|
GST_VTDEC_VIDEO_FORMAT_STR, output_state->caps);
|
|
|
|
gst_video_codec_state_unref (output_state);
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 03:09:43 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
if (drain)
|
|
|
|
VTDecompressionSessionWaitForAsynchronousFrames (vtdec->session);
|
|
|
|
|
|
|
|
/* push a buffer if there are enough frames to guarantee that we push in PTS
|
|
|
|
* order
|
|
|
|
*/
|
|
|
|
while ((g_async_queue_length (vtdec->reorder_queue) >=
|
2013-12-11 07:11:50 +00:00
|
|
|
vtdec->reorder_queue_length) || drain || flush) {
|
2013-12-07 22:55:13 +00:00
|
|
|
frame = (GstVideoCodecFrame *) g_async_queue_try_pop (vtdec->reorder_queue);
|
2015-02-09 10:18:24 +00:00
|
|
|
if (frame && vtdec->texture_cache != NULL) {
|
2015-01-29 13:28:18 +00:00
|
|
|
frame->output_buffer =
|
|
|
|
gst_core_video_texture_cache_get_gl_buffer (vtdec->texture_cache,
|
|
|
|
frame->output_buffer);
|
2015-02-09 10:18:24 +00:00
|
|
|
if (!frame->output_buffer)
|
|
|
|
GST_ERROR_OBJECT (vtdec, "couldn't get textures from buffer");
|
|
|
|
}
|
2015-01-15 03:09:43 +00:00
|
|
|
|
2013-12-11 07:11:50 +00:00
|
|
|
/* we need to check this in case reorder_queue_length=0 (jpeg for
|
2013-12-07 22:55:13 +00:00
|
|
|
* example) or we're draining/flushing
|
|
|
|
*/
|
|
|
|
if (frame) {
|
|
|
|
if (flush)
|
|
|
|
gst_video_decoder_drop_frame (decoder, frame);
|
|
|
|
else
|
|
|
|
ret = gst_video_decoder_finish_frame (decoder, frame);
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
if (!frame || ret != GST_FLOW_OK)
|
|
|
|
break;
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
return ret;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
2013-12-11 06:58:23 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
parse_h264_profile_and_level_from_codec_data (GstVtdec * vtdec,
|
|
|
|
GstBuffer * codec_data, int *profile, int *level)
|
|
|
|
{
|
|
|
|
GstMapInfo map;
|
|
|
|
guint8 *data;
|
|
|
|
gint size;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
gst_buffer_map (codec_data, &map, GST_MAP_READ);
|
|
|
|
data = map.data;
|
|
|
|
size = map.size;
|
|
|
|
|
|
|
|
/* parse the avcC data */
|
|
|
|
if (size < 7)
|
|
|
|
goto avcc_too_small;
|
|
|
|
|
|
|
|
/* parse the version, this must be 1 */
|
|
|
|
if (data[0] != 1)
|
|
|
|
goto wrong_version;
|
|
|
|
|
|
|
|
/* AVCProfileIndication */
|
|
|
|
/* profile_compat */
|
|
|
|
/* AVCLevelIndication */
|
|
|
|
if (profile)
|
|
|
|
*profile = data[1];
|
|
|
|
|
|
|
|
if (level)
|
|
|
|
*level = data[3];
|
|
|
|
|
|
|
|
out:
|
|
|
|
gst_buffer_unmap (codec_data, &map);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
avcc_too_small:
|
|
|
|
GST_ELEMENT_ERROR (vtdec, STREAM, DECODE, (NULL),
|
|
|
|
("invalid codec_data buffer length"));
|
|
|
|
ret = FALSE;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
wrong_version:
|
|
|
|
GST_ELEMENT_ERROR (vtdec, STREAM, DECODE, (NULL),
|
|
|
|
("wrong avcC version in codec_data"));
|
|
|
|
ret = FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-12-14 14:55:42 +00:00
|
|
|
get_dpb_max_mb_s_from_level (GstVtdec * vtdec, int level)
|
2013-12-11 06:58:23 +00:00
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case 10:
|
|
|
|
/* 1b?? */
|
|
|
|
return 396;
|
|
|
|
case 11:
|
|
|
|
return 900;
|
|
|
|
case 12:
|
|
|
|
case 13:
|
|
|
|
case 20:
|
|
|
|
return 2376;
|
|
|
|
case 21:
|
|
|
|
return 4752;
|
|
|
|
case 22:
|
2013-12-14 14:55:42 +00:00
|
|
|
case 30:
|
2013-12-11 06:58:23 +00:00
|
|
|
return 8100;
|
|
|
|
case 31:
|
|
|
|
return 18000;
|
|
|
|
case 32:
|
|
|
|
return 20480;
|
|
|
|
case 40:
|
|
|
|
case 41:
|
|
|
|
return 32768;
|
|
|
|
case 42:
|
|
|
|
return 34816;
|
|
|
|
case 50:
|
|
|
|
return 110400;
|
|
|
|
case 51:
|
|
|
|
case 52:
|
|
|
|
return 184320;
|
|
|
|
default:
|
2013-12-14 14:55:42 +00:00
|
|
|
GST_ERROR_OBJECT (vtdec, "unknown level %d", level);
|
2013-12-11 06:58:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 07:10:19 +00:00
|
|
|
static gboolean
|
2013-12-11 07:11:50 +00:00
|
|
|
gst_vtdec_compute_reorder_queue_length (GstVtdec * vtdec,
|
2013-12-11 07:10:19 +00:00
|
|
|
CMVideoCodecType cm_format, GstBuffer * codec_data)
|
|
|
|
{
|
|
|
|
if (cm_format == kCMVideoCodecType_H264) {
|
|
|
|
if (!compute_h264_decode_picture_buffer_length (vtdec, codec_data,
|
2013-12-11 07:11:50 +00:00
|
|
|
&vtdec->reorder_queue_length)) {
|
2013-12-11 07:10:19 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
2013-12-11 07:11:50 +00:00
|
|
|
vtdec->reorder_queue_length = 0;
|
2013-12-11 07:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-12-11 06:58:23 +00:00
|
|
|
static gboolean
|
|
|
|
compute_h264_decode_picture_buffer_length (GstVtdec * vtdec,
|
|
|
|
GstBuffer * codec_data, int *length)
|
|
|
|
{
|
|
|
|
int profile, level;
|
|
|
|
int dpb_mb_size = 16;
|
|
|
|
int max_dpb_size_frames = 16;
|
|
|
|
int max_dpb_mb_s = -1;
|
2014-11-20 08:41:43 +00:00
|
|
|
int width_in_mb_s = GST_ROUND_UP_16 (vtdec->video_info.width) / dpb_mb_size;
|
|
|
|
int height_in_mb_s = GST_ROUND_UP_16 (vtdec->video_info.height) / dpb_mb_size;
|
2013-12-11 06:58:23 +00:00
|
|
|
|
2014-10-14 07:19:59 +00:00
|
|
|
*length = 0;
|
|
|
|
|
2013-12-11 06:58:23 +00:00
|
|
|
if (!parse_h264_profile_and_level_from_codec_data (vtdec, codec_data,
|
|
|
|
&profile, &level))
|
|
|
|
return FALSE;
|
|
|
|
|
2014-11-20 08:41:43 +00:00
|
|
|
if (vtdec->video_info.width == 0 || vtdec->video_info.height == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-11-20 14:50:18 +00:00
|
|
|
GST_INFO_OBJECT (vtdec, "parsed profile %d, level %d", profile, level);
|
|
|
|
if (profile == 66) {
|
|
|
|
/* baseline or constrained-baseline, we don't need to reorder */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-12-14 14:55:42 +00:00
|
|
|
max_dpb_mb_s = get_dpb_max_mb_s_from_level (vtdec, level);
|
2013-12-11 06:58:23 +00:00
|
|
|
if (max_dpb_mb_s == -1) {
|
|
|
|
GST_ELEMENT_ERROR (vtdec, STREAM, DECODE, (NULL),
|
|
|
|
("invalid level in codec_data, could not compute max_dpb_mb_s"));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this formula is specified in sections A.3.1.h and A.3.2.f of the 2009
|
|
|
|
* edition of the standard */
|
|
|
|
*length = MIN (floor (max_dpb_mb_s / (width_in_mb_s * height_in_mb_s)),
|
|
|
|
max_dpb_size_frames);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-12-11 07:10:19 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtdec_set_latency (GstVtdec * vtdec)
|
|
|
|
{
|
2014-09-18 06:47:06 +00:00
|
|
|
GstClockTime frame_duration;
|
|
|
|
GstClockTime latency;
|
|
|
|
|
|
|
|
if (vtdec->video_info.fps_n == 0) {
|
|
|
|
GST_INFO_OBJECT (vtdec, "Framerate not known, can't set latency");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-18 07:30:04 +00:00
|
|
|
frame_duration = gst_util_uint64_scale (GST_SECOND,
|
2013-12-11 07:10:19 +00:00
|
|
|
vtdec->video_info.fps_d, vtdec->video_info.fps_n);
|
2014-09-18 06:47:06 +00:00
|
|
|
latency = frame_duration * vtdec->reorder_queue_length;
|
2013-12-11 07:10:19 +00:00
|
|
|
|
|
|
|
GST_INFO_OBJECT (vtdec, "setting latency frames:%d time:%" GST_TIME_FORMAT,
|
2013-12-11 07:11:50 +00:00
|
|
|
vtdec->reorder_queue_length, GST_TIME_ARGS (latency));
|
2013-12-11 07:10:19 +00:00
|
|
|
gst_video_decoder_set_latency (GST_VIDEO_DECODER (vtdec), latency, latency);
|
|
|
|
}
|