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>
|
2016-02-09 03:04:09 +00:00
|
|
|
* Copyright (C) 2012-2016 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.
|
|
|
|
*/
|
|
|
|
/**
|
2017-03-08 18:01:13 +00:00
|
|
|
* SECTION:element-vtdec
|
2022-01-28 17:45:28 +00:00
|
|
|
* @title: vtdec
|
2013-12-07 22:55:13 +00:00
|
|
|
*
|
2022-01-28 17:45:28 +00:00
|
|
|
* Apple VideoToolbox based decoder which might use a HW or a SW
|
|
|
|
* implementation depending on the device.
|
2013-12-07 22:55:13 +00:00
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* ## Example launch line
|
2013-12-07 22:55:13 +00:00
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 -v filesrc location=file.mov ! qtdemux ! queue ! h264parse ! vtdec ! videoconvert ! autovideosink
|
2013-12-07 22:55:13 +00:00
|
|
|
* ]|
|
|
|
|
* Decode h264 video from a mov file.
|
2017-03-08 18:01:13 +00:00
|
|
|
*
|
2010-10-27 17:30:11 +00:00
|
|
|
*/
|
|
|
|
|
2022-01-28 17:45:28 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-vtdec_hw
|
|
|
|
* @title: vtdec_hw
|
|
|
|
*
|
|
|
|
* Apple VideoToolbox based HW-only decoder.
|
|
|
|
*
|
|
|
|
* ## Example launch line
|
|
|
|
* |[
|
|
|
|
* gst-launch-1.0 -v filesrc location=file.mov ! qtdemux ! queue ! h264parse ! vtdec_hw ! videoconvert ! autovideosink
|
|
|
|
* ]|
|
|
|
|
* Decode h264 video from a mov file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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"
|
2023-03-09 03:44:47 +00:00
|
|
|
#include "helpers.h"
|
2013-12-07 22:55:13 +00:00
|
|
|
#include "corevideobuffer.h"
|
2015-01-15 03:09:43 +00:00
|
|
|
#include "coremediabuffer.h"
|
2019-08-28 08:59:35 +00:00
|
|
|
#include "videotexturecache-gl.h"
|
2019-09-03 03:56:22 +00:00
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
#include "videotexturecache-vulkan.h"
|
|
|
|
#endif
|
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
|
|
|
|
2015-11-17 00:19:57 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* leave some headroom for new GstVideoCodecFrameFlags flags */
|
|
|
|
VTDEC_FRAME_FLAG_SKIP = (1 << 10),
|
|
|
|
VTDEC_FRAME_FLAG_DROP = (1 << 11),
|
2021-10-17 13:24:10 +00:00
|
|
|
VTDEC_FRAME_FLAG_ERROR = (1 << 12),
|
2015-11-17 00:19:57 +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);
|
2024-03-18 17:46:17 +00:00
|
|
|
static void gst_vtdec_output_loop (GstVtdec * self);
|
2015-11-17 00:21:27 +00:00
|
|
|
static gboolean gst_vtdec_negotiate (GstVideoDecoder * decoder);
|
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);
|
2023-08-08 22:20:46 +00:00
|
|
|
static gboolean gst_vtdec_sink_event (GstVideoDecoder * decoder,
|
|
|
|
GstEvent * event);
|
2024-04-30 16:15:56 +00:00
|
|
|
static GstStateChangeReturn gst_vtdec_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
2013-12-07 22:55:13 +00:00
|
|
|
static GstFlowReturn gst_vtdec_handle_frame (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecFrame * frame);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2016-06-02 06:30:02 +00:00
|
|
|
static OSStatus gst_vtdec_create_session (GstVtdec * vtdec,
|
|
|
|
GstVideoFormat format, gboolean enable_hardware);
|
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);
|
2023-08-08 22:20:46 +00:00
|
|
|
static GstFlowReturn gst_vtdec_drain_decoder (GstVideoDecoder * decoder,
|
|
|
|
gboolean flush);
|
2013-12-07 22:55:13 +00:00
|
|
|
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);
|
2023-08-08 22:20:46 +00:00
|
|
|
static gboolean compute_h264_decode_picture_buffer_size (GstVtdec * vtdec,
|
2013-12-11 06:58:23 +00:00
|
|
|
GstBuffer * codec_data, int *length);
|
2023-08-08 22:20:46 +00:00
|
|
|
static gboolean compute_hevc_decode_picture_buffer_size (GstVtdec * vtdec,
|
2022-07-13 08:30:49 +00:00
|
|
|
GstBuffer * codec_data, int *length);
|
2023-08-08 22:20:46 +00:00
|
|
|
static gboolean gst_vtdec_compute_dpb_size (GstVtdec * vtdec,
|
2013-12-11 07:10:19 +00:00
|
|
|
CMVideoCodecType cm_format, GstBuffer * codec_data);
|
|
|
|
static void gst_vtdec_set_latency (GstVtdec * vtdec);
|
2016-02-09 03:07:54 +00:00
|
|
|
static void gst_vtdec_set_context (GstElement * element, GstContext * context);
|
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,
|
2015-05-20 19:04:40 +00:00
|
|
|
GST_STATIC_CAPS ("video/x-h264, stream-format=avc, alignment=au,"
|
2022-07-13 08:30:49 +00:00
|
|
|
" width=(int)[1, MAX], height=(int)[1, MAX];"
|
|
|
|
"video/x-h265, stream-format=(string){ hev1, hvc1 }, alignment=au,"
|
2015-05-20 19:04:40 +00:00
|
|
|
" width=(int)[1, MAX], height=(int)[1, MAX];"
|
2016-08-17 17:59:17 +00:00
|
|
|
"video/mpeg, mpegversion=2, systemstream=false, parsed=true;"
|
2021-10-17 13:24:10 +00:00
|
|
|
"image/jpeg;"
|
|
|
|
"video/x-prores, variant = { (string)standard, (string)hq, (string)lt,"
|
|
|
|
" (string)proxy, (string)4444, (string)4444xq };")
|
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");
|
2015-03-07 09:28:35 +00:00
|
|
|
const CFStringRef
|
|
|
|
kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder =
|
|
|
|
CFSTR ("RequireHardwareAcceleratedVideoDecoder");
|
2013-12-17 09:01:42 +00:00
|
|
|
#endif
|
|
|
|
|
2022-01-31 21:30:41 +00:00
|
|
|
#define VIDEO_SRC_CAPS_FORMATS "{ NV12, AYUV64, ARGB64_BE }"
|
2021-10-17 13:24:10 +00:00
|
|
|
|
|
|
|
#define VIDEO_SRC_CAPS_NATIVE \
|
|
|
|
GST_VIDEO_CAPS_MAKE(VIDEO_SRC_CAPS_FORMATS) ";" \
|
2019-09-03 03:56:22 +00:00
|
|
|
GST_VIDEO_CAPS_MAKE_WITH_FEATURES(GST_CAPS_FEATURE_MEMORY_GL_MEMORY,\
|
2021-10-17 13:24:10 +00:00
|
|
|
VIDEO_SRC_CAPS_FORMATS) ", " \
|
2019-09-03 03:56:22 +00:00
|
|
|
"texture-target = (string) rectangle "
|
2021-10-17 13:24:10 +00:00
|
|
|
|
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
#define VIDEO_SRC_CAPS VIDEO_SRC_CAPS_NATIVE "; " \
|
|
|
|
GST_VIDEO_CAPS_MAKE_WITH_FEATURES(GST_CAPS_FEATURE_MEMORY_VULKAN_IMAGE, \
|
|
|
|
VIDEO_SRC_CAPS_FORMATS)
|
|
|
|
#else
|
|
|
|
#define VIDEO_SRC_CAPS VIDEO_SRC_CAPS_NATIVE
|
2019-09-03 03:56:22 +00:00
|
|
|
#endif
|
2013-12-07 22:55:13 +00:00
|
|
|
|
2015-03-07 09:28:35 +00:00
|
|
|
G_DEFINE_TYPE (GstVtdec, gst_vtdec, GST_TYPE_VIDEO_DECODER);
|
2013-12-07 22:55:13 +00:00
|
|
|
|
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);
|
2016-02-09 03:07:54 +00:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
2013-12-07 22:55:13 +00:00
|
|
|
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. */
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_vtdec_sink_template);
|
2022-01-31 21:30:41 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
GstCaps *caps = gst_caps_from_string (VIDEO_SRC_CAPS);
|
|
|
|
/* RGBA64_LE is kCVPixelFormatType_64RGBALE, only available on macOS 11.3+ */
|
2023-03-09 03:44:47 +00:00
|
|
|
if (GST_APPLEMEDIA_HAVE_64RGBALE)
|
2022-01-31 21:30:41 +00:00
|
|
|
caps = gst_vtutil_caps_append_video_format (caps, "RGBA64_LE");
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps));
|
|
|
|
}
|
2013-12-07 22:55:13 +00:00
|
|
|
|
2016-02-09 03:07:54 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
2013-12-07 22:55:13 +00:00
|
|
|
"Apple VideoToolbox decoder",
|
2019-02-14 09:49:03 +00:00
|
|
|
"Codec/Decoder/Video/Hardware",
|
2013-12-07 22:55:13 +00:00
|
|
|
"Apple VideoToolbox Decoder",
|
|
|
|
"Ole André Vadla Ravnås <oleavr@soundrop.com>; "
|
|
|
|
"Alessandro Decina <alessandro.d@gmail.com>");
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_vtdec_finalize;
|
2016-02-09 03:07:54 +00:00
|
|
|
element_class->set_context = gst_vtdec_set_context;
|
2024-04-30 16:15:56 +00:00
|
|
|
element_class->change_state = gst_vtdec_change_state;
|
2013-12-07 22:55:13 +00:00
|
|
|
video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_vtdec_start);
|
|
|
|
video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_vtdec_stop);
|
2015-11-17 00:21:27 +00:00
|
|
|
video_decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_vtdec_negotiate);
|
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);
|
2023-08-08 22:20:46 +00:00
|
|
|
video_decoder_class->sink_event = GST_DEBUG_FUNCPTR (gst_vtdec_sink_event);
|
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
|
|
|
{
|
2023-08-08 22:20:46 +00:00
|
|
|
g_mutex_init (&vtdec->queue_mutex);
|
|
|
|
g_cond_init (&vtdec->queue_cond);
|
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
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
g_mutex_clear (&vtdec->queue_mutex);
|
|
|
|
g_cond_clear (&vtdec->queue_cond);
|
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
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
vtdec->is_flushing = FALSE;
|
|
|
|
vtdec->is_draining = FALSE;
|
|
|
|
vtdec->downstream_ret = GST_FLOW_OK;
|
2024-04-18 14:07:25 +00:00
|
|
|
vtdec->reorder_queue = gst_vec_deque_new (0);
|
2023-08-08 22:20:46 +00:00
|
|
|
|
|
|
|
/* Create the output task, but pause it immediately */
|
|
|
|
vtdec->pause_task = TRUE;
|
|
|
|
if (!gst_pad_start_task (GST_VIDEO_DECODER_SRC_PAD (decoder),
|
2024-03-18 17:46:17 +00:00
|
|
|
(GstTaskFunction) gst_vtdec_output_loop, vtdec, NULL)) {
|
2023-08-08 22:20:46 +00:00
|
|
|
GST_ERROR_OBJECT (vtdec, "failed to start output thread");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* This blocks until the loop actually pauses */
|
|
|
|
gst_pad_pause_task (GST_VIDEO_DECODER_SRC_PAD (decoder));
|
|
|
|
vtdec->pause_task = FALSE;
|
|
|
|
|
2017-09-22 04:27:51 +00:00
|
|
|
if (!vtdec->ctxh)
|
|
|
|
vtdec->ctxh = gst_gl_context_helper_new (GST_ELEMENT (decoder));
|
|
|
|
|
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
|
|
|
{
|
2023-08-08 22:20:46 +00:00
|
|
|
GstVideoCodecFrame *frame;
|
2013-12-07 22:55:13 +00:00
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2024-04-30 16:15:56 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec, "stop");
|
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
gst_vtdec_drain_decoder (GST_VIDEO_DECODER_CAST (vtdec), TRUE);
|
|
|
|
vtdec->downstream_ret = GST_FLOW_FLUSHING;
|
|
|
|
|
2024-04-18 14:07:25 +00:00
|
|
|
while ((frame = gst_vec_deque_pop_head (vtdec->reorder_queue))) {
|
2023-08-08 22:20:46 +00:00
|
|
|
gst_video_decoder_release_frame (decoder, frame);
|
|
|
|
}
|
2024-04-18 14:07:25 +00:00
|
|
|
gst_vec_deque_free (vtdec->reorder_queue);
|
2023-08-08 22:20:46 +00:00
|
|
|
vtdec->reorder_queue = NULL;
|
|
|
|
|
|
|
|
gst_pad_stop_task (GST_VIDEO_DECODER_SRC_PAD (decoder));
|
2017-10-23 08:42:51 +00:00
|
|
|
|
2015-11-17 00:21:27 +00:00
|
|
|
if (vtdec->input_state)
|
|
|
|
gst_video_codec_state_unref (vtdec->input_state);
|
|
|
|
vtdec->input_state = NULL;
|
|
|
|
|
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)
|
2019-08-26 06:51:03 +00:00
|
|
|
g_object_unref (vtdec->texture_cache);
|
2015-01-15 03:09:43 +00:00
|
|
|
vtdec->texture_cache = NULL;
|
|
|
|
|
2017-09-22 04:27:51 +00:00
|
|
|
if (vtdec->ctxh)
|
|
|
|
gst_gl_context_helper_free (vtdec->ctxh);
|
|
|
|
vtdec->ctxh = NULL;
|
|
|
|
|
2019-10-30 07:56:41 +00:00
|
|
|
if (vtdec->format_description)
|
|
|
|
CFRelease (vtdec->format_description);
|
|
|
|
vtdec->format_description = NULL;
|
|
|
|
|
2019-09-03 03:56:22 +00:00
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
gst_clear_object (&vtdec->device);
|
|
|
|
gst_clear_object (&vtdec->instance);
|
|
|
|
#endif
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
return TRUE;
|
2012-03-03 18:03:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
static void
|
2024-03-18 17:46:17 +00:00
|
|
|
gst_vtdec_output_loop (GstVtdec * vtdec)
|
2023-08-08 22:20:46 +00:00
|
|
|
{
|
|
|
|
GstVideoCodecFrame *frame;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstVideoDecoder *decoder = GST_VIDEO_DECODER (vtdec);
|
|
|
|
gboolean is_flushing;
|
|
|
|
|
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
2024-04-18 14:07:25 +00:00
|
|
|
while (gst_vec_deque_is_empty (vtdec->reorder_queue)
|
2023-08-08 22:20:46 +00:00
|
|
|
&& !vtdec->pause_task && !vtdec->is_flushing && !vtdec->is_draining) {
|
|
|
|
g_cond_wait (&vtdec->queue_cond, &vtdec->queue_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vtdec->pause_task) {
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
|
|
|
gst_pad_pause_task (GST_VIDEO_DECODER_SRC_PAD (decoder));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* push a buffer if there are enough frames to guarantee
|
|
|
|
* that we push in PTS order, or if we're draining/flushing */
|
2024-04-18 14:07:25 +00:00
|
|
|
while ((gst_vec_deque_get_length (vtdec->reorder_queue) >=
|
2023-08-08 22:20:46 +00:00
|
|
|
vtdec->dbp_size) || vtdec->is_flushing || vtdec->is_draining) {
|
2024-04-18 14:07:25 +00:00
|
|
|
frame = gst_vec_deque_pop_head (vtdec->reorder_queue);
|
2023-08-08 22:20:46 +00:00
|
|
|
is_flushing = vtdec->is_flushing;
|
|
|
|
g_cond_signal (&vtdec->queue_cond);
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
|
|
|
|
|
|
|
/* we need to check this in case dpb_size=0 (jpeg for
|
|
|
|
* example) or we're draining/flushing */
|
|
|
|
if (frame) {
|
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (vtdec);
|
|
|
|
|
|
|
|
if (frame->flags & VTDEC_FRAME_FLAG_ERROR) {
|
|
|
|
GST_LOG_OBJECT (vtdec, "ignoring frame %d because of error flag",
|
|
|
|
frame->system_frame_number);
|
|
|
|
gst_video_decoder_release_frame (decoder, frame);
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
} else if (is_flushing || (frame->flags & VTDEC_FRAME_FLAG_SKIP)) {
|
|
|
|
GST_LOG_OBJECT (vtdec, "flushing frame %d", frame->system_frame_number);
|
|
|
|
gst_video_decoder_release_frame (decoder, frame);
|
|
|
|
} else if (frame->flags & VTDEC_FRAME_FLAG_DROP) {
|
|
|
|
GST_LOG_OBJECT (vtdec, "dropping frame %d", frame->system_frame_number);
|
|
|
|
gst_video_decoder_drop_frame (decoder, frame);
|
|
|
|
} else {
|
2024-03-18 17:46:17 +00:00
|
|
|
GST_TRACE_OBJECT (vtdec, "pushing frame %d",
|
|
|
|
frame->system_frame_number);
|
2023-08-08 22:20:46 +00:00
|
|
|
ret = gst_video_decoder_finish_frame (decoder, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (vtdec);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
|
|
|
if (!frame || ret != GST_FLOW_OK)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (vtdec);
|
|
|
|
vtdec->downstream_ret = ret;
|
|
|
|
|
|
|
|
/* We need to empty the queue immediately so that session_output_callback()
|
|
|
|
* can push out the current buffer, otherwise it can deadlock */
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
|
|
|
|
2024-04-18 14:07:25 +00:00
|
|
|
while ((frame = gst_vec_deque_pop_head (vtdec->reorder_queue))) {
|
2023-08-08 22:20:46 +00:00
|
|
|
GST_LOG_OBJECT (vtdec, "flushing frame %d", frame->system_frame_number);
|
|
|
|
gst_video_decoder_release_frame (decoder, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_cond_signal (&vtdec->queue_cond);
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (vtdec);
|
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "pausing output task: %s",
|
|
|
|
gst_flow_get_name (ret));
|
|
|
|
gst_pad_pause_task (GST_VIDEO_DECODER_SRC_PAD (decoder));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vtdec_ensure_output_loop (GstVtdec * vtdec)
|
|
|
|
{
|
|
|
|
GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (vtdec);
|
|
|
|
GstTask *task = GST_PAD_TASK (pad);
|
|
|
|
|
|
|
|
return gst_task_resume (task);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtdec_pause_output_loop (GstVtdec * vtdec)
|
|
|
|
{
|
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
|
|
|
vtdec->pause_task = TRUE;
|
|
|
|
g_cond_signal (&vtdec->queue_cond);
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
|
|
|
|
|
|
|
gst_pad_pause_task (GST_VIDEO_DECODER_SRC_PAD (vtdec));
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "paused output thread");
|
|
|
|
|
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
|
|
|
vtdec->pause_task = FALSE;
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
|
|
|
}
|
|
|
|
|
2015-11-17 00:21:27 +00:00
|
|
|
static void
|
2021-10-17 13:24:10 +00:00
|
|
|
setup_texture_cache (GstVtdec * vtdec, GstVideoFormat format)
|
2015-11-17 00:21:27 +00:00
|
|
|
{
|
2015-11-17 05:14:11 +00:00
|
|
|
GstVideoCodecState *output_state;
|
2015-11-17 00:21:27 +00:00
|
|
|
|
2021-10-17 13:24:10 +00:00
|
|
|
GST_INFO_OBJECT (vtdec, "setting up texture cache");
|
2015-11-17 05:14:11 +00:00
|
|
|
output_state = gst_video_decoder_get_output_state (GST_VIDEO_DECODER (vtdec));
|
2021-10-17 13:24:10 +00:00
|
|
|
gst_video_texture_cache_set_format (vtdec->texture_cache, format,
|
|
|
|
output_state->caps);
|
2015-11-17 00:21:27 +00:00
|
|
|
gst_video_codec_state_unref (output_state);
|
|
|
|
}
|
2015-01-15 03:09:43 +00:00
|
|
|
|
2021-10-17 13:24:10 +00:00
|
|
|
/*
|
|
|
|
* Unconditionally output a high bit-depth + alpha format when decoding Apple
|
|
|
|
* ProRes video if downstream supports it.
|
|
|
|
* TODO: read src_pix_fmt to get the preferred output format
|
|
|
|
* https://wiki.multimedia.cx/index.php/Apple_ProRes#Frame_header
|
|
|
|
*/
|
|
|
|
static GstVideoFormat
|
|
|
|
get_preferred_video_format (GstStructure * s, gboolean prores)
|
|
|
|
{
|
|
|
|
const GValue *list = gst_structure_get_value (s, "format");
|
|
|
|
guint i, size = gst_value_list_get_size (list);
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
const GValue *value = gst_value_list_get_value (list, i);
|
|
|
|
const char *fmt = g_value_get_string (value);
|
|
|
|
GstVideoFormat vfmt = gst_video_format_from_string (fmt);
|
|
|
|
switch (vfmt) {
|
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
if (!prores)
|
|
|
|
return vfmt;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_AYUV64:
|
2021-10-31 08:13:40 +00:00
|
|
|
case GST_VIDEO_FORMAT_ARGB64_BE:
|
2021-10-17 13:24:10 +00:00
|
|
|
if (prores)
|
|
|
|
return vfmt;
|
|
|
|
break;
|
2022-01-31 21:30:41 +00:00
|
|
|
case GST_VIDEO_FORMAT_RGBA64_LE:
|
2023-03-09 03:44:47 +00:00
|
|
|
if (GST_APPLEMEDIA_HAVE_64RGBALE) {
|
2022-01-31 21:30:41 +00:00
|
|
|
if (prores)
|
|
|
|
return vfmt;
|
|
|
|
} else {
|
|
|
|
/* Codepath will never be hit on macOS older than Big Sur (11.3) */
|
|
|
|
g_warn_if_reached ();
|
|
|
|
}
|
|
|
|
break;
|
2021-10-17 13:24:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GST_VIDEO_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2015-09-08 07:17:36 +00:00
|
|
|
static gboolean
|
2015-11-17 00:21:27 +00:00
|
|
|
gst_vtdec_negotiate (GstVideoDecoder * decoder)
|
2015-01-15 03:09:43 +00:00
|
|
|
{
|
2015-09-08 07:17:36 +00:00
|
|
|
GstVideoCodecState *output_state = NULL;
|
2016-06-07 06:00:01 +00:00
|
|
|
GstCaps *peercaps = NULL, *caps = NULL, *templcaps = NULL, *prevcaps = NULL;
|
2021-10-17 13:24:10 +00:00
|
|
|
GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
|
2015-11-17 00:21:27 +00:00
|
|
|
GstVtdec *vtdec;
|
2016-06-02 06:30:02 +00:00
|
|
|
OSStatus err = noErr;
|
2015-12-14 03:23:20 +00:00
|
|
|
GstCapsFeatures *features = NULL;
|
2019-09-03 03:56:22 +00:00
|
|
|
gboolean output_textures = FALSE;
|
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
gboolean output_vulkan = FALSE;
|
|
|
|
#endif
|
2015-01-15 03:09:43 +00:00
|
|
|
|
2015-11-17 00:21:27 +00:00
|
|
|
vtdec = GST_VTDEC (decoder);
|
2016-06-07 07:22:01 +00:00
|
|
|
if (vtdec->session)
|
2023-08-08 22:20:46 +00:00
|
|
|
gst_vtdec_drain_decoder (GST_VIDEO_DECODER_CAST (vtdec), FALSE);
|
2016-06-16 04:18:18 +00:00
|
|
|
|
|
|
|
output_state = gst_video_decoder_get_output_state (GST_VIDEO_DECODER (vtdec));
|
|
|
|
if (output_state) {
|
|
|
|
prevcaps = gst_caps_ref (output_state->caps);
|
|
|
|
gst_video_codec_state_unref (output_state);
|
|
|
|
}
|
|
|
|
|
2024-03-22 15:14:24 +00:00
|
|
|
templcaps =
|
|
|
|
gst_pad_get_pad_template_caps (GST_VIDEO_DECODER_SRC_PAD (decoder));
|
|
|
|
peercaps =
|
|
|
|
gst_pad_peer_query_caps (GST_VIDEO_DECODER_SRC_PAD (vtdec), templcaps);
|
|
|
|
gst_caps_unref (templcaps);
|
|
|
|
|
|
|
|
if (gst_caps_is_empty (peercaps)) {
|
|
|
|
GST_INFO_OBJECT (vtdec, "empty peer caps, can't negotiate");
|
|
|
|
|
|
|
|
gst_caps_unref (peercaps);
|
|
|
|
if (prevcaps)
|
|
|
|
gst_caps_unref (prevcaps);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-06-16 04:18:18 +00:00
|
|
|
if (prevcaps && gst_caps_can_intersect (prevcaps, peercaps)) {
|
|
|
|
/* The hardware decoder can become (temporarily) unavailable across
|
|
|
|
* VTDecompressionSessionCreate/Destroy calls. So if the currently configured
|
|
|
|
* caps are still accepted by downstream we keep them so we don't have to
|
|
|
|
* destroy and recreate the session.
|
|
|
|
*/
|
|
|
|
GST_INFO_OBJECT (vtdec,
|
|
|
|
"current and peer caps are compatible, keeping current caps");
|
|
|
|
caps = gst_caps_ref (prevcaps);
|
2024-03-22 15:14:24 +00:00
|
|
|
gst_caps_unref (peercaps);
|
2016-06-16 04:18:18 +00:00
|
|
|
} else {
|
2024-03-22 15:14:24 +00:00
|
|
|
caps = peercaps;
|
2016-06-16 04:18:18 +00:00
|
|
|
}
|
2015-09-08 07:17:36 +00:00
|
|
|
|
2016-06-07 06:00:01 +00:00
|
|
|
caps = gst_caps_truncate (gst_caps_make_writable (caps));
|
2021-10-17 13:24:10 +00:00
|
|
|
|
|
|
|
/* Try to use whatever video format downstream prefers */
|
|
|
|
{
|
|
|
|
GstStructure *s = gst_caps_get_structure (caps, 0);
|
|
|
|
|
|
|
|
if (gst_structure_has_field_typed (s, "format", GST_TYPE_LIST)) {
|
|
|
|
GstStructure *is = gst_caps_get_structure (vtdec->input_state->caps, 0);
|
|
|
|
const char *name = gst_structure_get_name (is);
|
|
|
|
format = get_preferred_video_format (s,
|
|
|
|
g_strcmp0 (name, "video/x-prores") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format == GST_VIDEO_FORMAT_UNKNOWN) {
|
|
|
|
const char *fmt;
|
|
|
|
gst_structure_fixate_field (s, "format");
|
|
|
|
fmt = gst_structure_get_string (s, "format");
|
|
|
|
if (fmt)
|
|
|
|
format = gst_video_format_from_string (fmt);
|
|
|
|
else
|
|
|
|
/* If all fails, just use NV12 */
|
|
|
|
format = GST_VIDEO_FORMAT_NV12;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 03:23:20 +00:00
|
|
|
features = gst_caps_get_features (caps, 0);
|
|
|
|
if (features)
|
|
|
|
features = gst_caps_features_copy (features);
|
2015-01-15 03:09:43 +00:00
|
|
|
|
2015-11-17 00:21:27 +00:00
|
|
|
output_state = gst_video_decoder_set_output_state (GST_VIDEO_DECODER (vtdec),
|
|
|
|
format, vtdec->video_info.width, vtdec->video_info.height,
|
|
|
|
vtdec->input_state);
|
|
|
|
output_state->caps = gst_video_info_to_caps (&output_state->info);
|
2015-12-14 03:23:20 +00:00
|
|
|
if (features) {
|
|
|
|
gst_caps_set_features (output_state->caps, 0, features);
|
|
|
|
output_textures =
|
|
|
|
gst_caps_features_contains (features,
|
|
|
|
GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
|
2015-12-14 05:10:01 +00:00
|
|
|
if (output_textures)
|
2015-12-16 06:02:27 +00:00
|
|
|
gst_caps_set_simple (output_state->caps, "texture-target", G_TYPE_STRING,
|
|
|
|
#if !HAVE_IOS
|
|
|
|
GST_GL_TEXTURE_TARGET_RECTANGLE_STR,
|
|
|
|
#else
|
|
|
|
GST_GL_TEXTURE_TARGET_2D_STR,
|
|
|
|
#endif
|
2015-12-14 05:10:01 +00:00
|
|
|
NULL);
|
2019-09-03 03:56:22 +00:00
|
|
|
|
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
output_vulkan =
|
|
|
|
gst_caps_features_contains (features,
|
|
|
|
GST_CAPS_FEATURE_MEMORY_VULKAN_IMAGE);
|
|
|
|
#endif
|
2015-09-08 07:17:36 +00:00
|
|
|
}
|
2015-12-14 03:23:20 +00:00
|
|
|
gst_caps_unref (caps);
|
2015-09-08 07:17:36 +00:00
|
|
|
|
2015-11-17 00:21:27 +00:00
|
|
|
if (!prevcaps || !gst_caps_is_equal (prevcaps, output_state->caps)) {
|
2016-06-02 06:30:02 +00:00
|
|
|
gboolean renegotiating = vtdec->session != NULL;
|
|
|
|
|
2016-06-02 01:36:57 +00:00
|
|
|
GST_INFO_OBJECT (vtdec,
|
|
|
|
"negotiated output format %" GST_PTR_FORMAT " previous %"
|
|
|
|
GST_PTR_FORMAT, output_state->caps, prevcaps);
|
2015-11-17 05:14:11 +00:00
|
|
|
|
2016-06-07 07:22:01 +00:00
|
|
|
if (vtdec->session)
|
2015-11-17 00:21:27 +00:00
|
|
|
gst_vtdec_invalidate_session (vtdec);
|
2015-11-17 05:14:11 +00:00
|
|
|
|
2016-06-02 06:30:02 +00:00
|
|
|
err = gst_vtdec_create_session (vtdec, format, TRUE);
|
|
|
|
if (err == noErr) {
|
|
|
|
GST_INFO_OBJECT (vtdec, "using hardware decoder");
|
|
|
|
} else if (err == kVTVideoDecoderNotAvailableNowErr && renegotiating) {
|
|
|
|
GST_WARNING_OBJECT (vtdec, "hw decoder not available anymore");
|
|
|
|
err = gst_vtdec_create_session (vtdec, format, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err != noErr) {
|
|
|
|
GST_ELEMENT_ERROR (vtdec, RESOURCE, FAILED, (NULL),
|
|
|
|
("VTDecompressionSessionCreate returned %d", (int) err));
|
|
|
|
}
|
2016-01-29 04:07:59 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 03:56:22 +00:00
|
|
|
if (vtdec->texture_cache != NULL
|
|
|
|
&& ((GST_IS_VIDEO_TEXTURE_CACHE_GL (vtdec->texture_cache)
|
|
|
|
&& !output_textures)
|
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
|| (GST_IS_VIDEO_TEXTURE_CACHE_VULKAN (vtdec->texture_cache)
|
|
|
|
&& !output_vulkan)
|
|
|
|
#endif
|
|
|
|
)) {
|
2019-08-26 06:51:03 +00:00
|
|
|
g_object_unref (vtdec->texture_cache);
|
2016-06-02 03:10:51 +00:00
|
|
|
vtdec->texture_cache = NULL;
|
|
|
|
}
|
|
|
|
|
2019-09-03 03:56:22 +00:00
|
|
|
if (err == noErr) {
|
|
|
|
if (output_textures) {
|
|
|
|
GstVideoTextureCacheGL *cache_gl = NULL;
|
|
|
|
|
|
|
|
if (vtdec->texture_cache)
|
|
|
|
cache_gl = GST_VIDEO_TEXTURE_CACHE_GL (vtdec->texture_cache);
|
|
|
|
|
|
|
|
/* call this regardless of whether caps have changed or not since a new
|
|
|
|
* local context could have become available
|
|
|
|
*/
|
|
|
|
if (!vtdec->ctxh)
|
|
|
|
vtdec->ctxh = gst_gl_context_helper_new (GST_ELEMENT (vtdec));
|
|
|
|
gst_gl_context_helper_ensure_context (vtdec->ctxh);
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (vtdec, "pushing GL textures, context %p old context %p",
|
|
|
|
vtdec->ctxh->context, cache_gl ? cache_gl->ctx : NULL);
|
|
|
|
|
|
|
|
if (cache_gl && cache_gl->ctx != vtdec->ctxh->context) {
|
|
|
|
g_object_unref (vtdec->texture_cache);
|
|
|
|
vtdec->texture_cache = NULL;
|
|
|
|
}
|
|
|
|
if (!vtdec->texture_cache) {
|
|
|
|
vtdec->texture_cache =
|
|
|
|
gst_video_texture_cache_gl_new (vtdec->ctxh->context);
|
2021-10-17 13:24:10 +00:00
|
|
|
setup_texture_cache (vtdec, format);
|
2019-09-03 03:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
if (output_vulkan) {
|
|
|
|
GstVideoTextureCacheVulkan *cache_vulkan = NULL;
|
|
|
|
|
|
|
|
if (vtdec->texture_cache)
|
|
|
|
cache_vulkan = GST_VIDEO_TEXTURE_CACHE_VULKAN (vtdec->texture_cache);
|
|
|
|
|
|
|
|
gst_vulkan_ensure_element_data (GST_ELEMENT (vtdec), NULL,
|
|
|
|
&vtdec->instance);
|
|
|
|
|
|
|
|
if (!gst_vulkan_device_run_context_query (GST_ELEMENT (vtdec),
|
|
|
|
&vtdec->device)) {
|
|
|
|
GError *error = NULL;
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "No device retrieved from peer elements");
|
|
|
|
if (!(vtdec->device =
|
|
|
|
gst_vulkan_instance_create_device (vtdec->instance, &error))) {
|
|
|
|
GST_ELEMENT_ERROR (vtdec, RESOURCE, NOT_FOUND,
|
|
|
|
("Failed to create vulkan device"), ("%s", error->message));
|
|
|
|
g_clear_error (&error);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (vtdec, "pushing vulkan images, device %" GST_PTR_FORMAT
|
|
|
|
" old device %" GST_PTR_FORMAT, vtdec->device,
|
|
|
|
cache_vulkan ? cache_vulkan->device : NULL);
|
|
|
|
|
|
|
|
if (cache_vulkan && cache_vulkan->device != vtdec->device) {
|
|
|
|
g_object_unref (vtdec->texture_cache);
|
|
|
|
vtdec->texture_cache = NULL;
|
|
|
|
}
|
|
|
|
if (!vtdec->texture_cache) {
|
|
|
|
vtdec->texture_cache =
|
|
|
|
gst_video_texture_cache_vulkan_new (vtdec->device);
|
2021-10-17 13:24:10 +00:00
|
|
|
setup_texture_cache (vtdec, format);
|
2019-09-03 03:56:22 +00:00
|
|
|
}
|
2015-11-17 05:14:11 +00:00
|
|
|
}
|
2019-09-03 03:56:22 +00:00
|
|
|
#endif
|
2015-11-17 00:21:27 +00:00
|
|
|
}
|
2015-11-17 05:14:11 +00:00
|
|
|
|
2015-11-17 00:21:27 +00:00
|
|
|
if (prevcaps)
|
|
|
|
gst_caps_unref (prevcaps);
|
2015-09-08 07:17:36 +00:00
|
|
|
|
2016-06-02 06:30:02 +00:00
|
|
|
if (err != noErr)
|
|
|
|
return FALSE;
|
2015-11-17 00:21:27 +00:00
|
|
|
|
|
|
|
return GST_VIDEO_DECODER_CLASS (gst_vtdec_parent_class)->negotiate (decoder);
|
2015-01-15 03:09:43 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
2022-07-13 08:30:49 +00:00
|
|
|
} else if (!strcmp (caps_name, "video/x-h265")) {
|
|
|
|
cm_format = kCMVideoCodecType_HEVC;
|
2013-12-07 22:55:13 +00:00
|
|
|
} 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;
|
2021-10-17 13:24:10 +00:00
|
|
|
} else if (!strcmp (caps_name, "video/x-prores")) {
|
|
|
|
const char *variant = gst_structure_get_string (structure, "variant");
|
|
|
|
|
|
|
|
if (variant)
|
|
|
|
cm_format = gst_vtutil_codec_type_from_prores_variant (variant);
|
|
|
|
|
|
|
|
if (cm_format == GST_kCMVideoCodecType_Some_AppleProRes) {
|
|
|
|
GST_ERROR_OBJECT (vtdec, "Invalid ProRes variant %s", variant);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 08:30:49 +00:00
|
|
|
if ((cm_format == kCMVideoCodecType_H264
|
|
|
|
|| cm_format == kCMVideoCodecType_HEVC)
|
|
|
|
&& state->codec_data == NULL) {
|
2013-12-11 06:58:23 +00:00
|
|
|
GST_INFO_OBJECT (vtdec, "no codec data, wait for one");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
if (!gst_vtdec_compute_dpb_size (vtdec, cm_format, state->codec_data))
|
2013-12-11 07:10:19 +00:00
|
|
|
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-11-17 00:21:27 +00:00
|
|
|
if (vtdec->input_state)
|
|
|
|
gst_video_codec_state_unref (vtdec->input_state);
|
|
|
|
vtdec->input_state = gst_video_codec_state_ref (state);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2024-05-07 08:35:26 +00:00
|
|
|
return gst_vtdec_negotiate (decoder);
|
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
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
return gst_vtdec_drain_decoder (GST_VIDEO_DECODER_CAST (vtdec),
|
|
|
|
TRUE) == GST_FLOW_OK;
|
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
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
return gst_vtdec_drain_decoder (GST_VIDEO_DECODER_CAST (vtdec), FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vtdec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
|
|
|
GstEventType type = GST_EVENT_TYPE (event);
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GST_EVENT_FLUSH_START:
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "flush start received, setting flushing flag");
|
|
|
|
|
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
|
|
|
vtdec->is_flushing = TRUE;
|
|
|
|
g_cond_signal (&vtdec->queue_cond);
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret =
|
|
|
|
GST_VIDEO_DECODER_CLASS (gst_vtdec_parent_class)->sink_event (decoder,
|
|
|
|
event);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
/* The base class handles this event and calls _flush().
|
|
|
|
* We can then safely reset the flushing flag. */
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "flush stop received, removing flushing flag");
|
|
|
|
|
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
|
|
|
vtdec->is_flushing = FALSE;
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-12-07 22:55:13 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2024-04-30 16:15:56 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_vtdec_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstVtdec *self = GST_VTDEC (element);
|
|
|
|
|
|
|
|
if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
|
|
|
|
GST_DEBUG_OBJECT (self, "pausing output loop on PAUSED->READY");
|
|
|
|
gst_vtdec_pause_output_loop (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_ELEMENT_CLASS (gst_vtdec_parent_class)->change_state (element,
|
|
|
|
transition);
|
|
|
|
}
|
|
|
|
|
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;
|
2022-03-28 09:37:54 +00:00
|
|
|
VTDecodeFrameFlags input_flags;
|
2013-12-07 22:55:13 +00:00
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
int decode_frame_number = frame->decode_frame_number;
|
2023-08-08 22:20:46 +00:00
|
|
|
GstTaskState task_state;
|
|
|
|
gboolean is_flushing;
|
2013-12-07 22:55:13 +00:00
|
|
|
|
2013-12-08 14:24:58 +00:00
|
|
|
if (vtdec->format_description == NULL) {
|
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
2023-08-08 22:20:46 +00:00
|
|
|
goto drop;
|
2013-12-08 14:24:58 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
/* Negotiate now so that we know whether we need to use the GL upload meta or not.
|
|
|
|
* gst_vtenc_negotiate() will drain before attempting to negotiate. */
|
|
|
|
if (gst_pad_check_reconfigure (decoder->srcpad)) {
|
2023-10-20 11:50:16 +00:00
|
|
|
if (!gst_vtdec_negotiate (decoder)) {
|
2023-08-08 22:20:46 +00:00
|
|
|
gst_pad_mark_reconfigure (decoder->srcpad);
|
|
|
|
if (GST_PAD_IS_FLUSHING (decoder->srcpad))
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
else
|
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 22:55:13 +00:00
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (vtdec));
|
|
|
|
if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
|
|
|
|
/* Abort if our loop failed to push frames downstream... */
|
|
|
|
if (vtdec->downstream_ret != GST_FLOW_OK) {
|
2024-03-18 17:46:17 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec,
|
|
|
|
"Output loop stopped because of %s, ignoring frame",
|
|
|
|
gst_flow_get_name (vtdec->downstream_ret));
|
2023-08-08 22:20:46 +00:00
|
|
|
|
|
|
|
ret = vtdec->downstream_ret;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ...or if it stopped because of the flushing flag while the queue
|
|
|
|
* was empty, in which case we didn't get GST_FLOW_FLUSHING... */
|
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
|
|
|
is_flushing = vtdec->is_flushing;
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
|
|
|
if (is_flushing) {
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "Flushing flag set, ignoring frame");
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* .. or if it refuses to resume - e.g. it was stopped instead of paused */
|
|
|
|
if (!gst_vtdec_ensure_output_loop (vtdec)) {
|
|
|
|
GST_ERROR_OBJECT (vtdec, "Output loop failed to resume");
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (vtdec, "got input frame %d", decode_frame_number);
|
2013-12-07 22:55:13 +00:00
|
|
|
|
|
|
|
/* don't bother enabling kVTDecodeFrame_EnableTemporalProcessing at all since
|
|
|
|
* it's not mandatory for the underlying VT codec to respect it. KISS and do
|
2023-08-08 22:20:46 +00:00
|
|
|
* reordering ourselves. */
|
2013-12-07 22:55:13 +00:00
|
|
|
input_flags = kVTDecodeFrame_EnableAsynchronousDecompression;
|
|
|
|
|
|
|
|
cm_sample_buffer =
|
|
|
|
cm_sample_buffer_from_gst_buffer (vtdec, frame->input_buffer);
|
2023-08-08 22:20:46 +00:00
|
|
|
|
|
|
|
/* We need to unlock the stream lock here because
|
|
|
|
* the decode call can wait until gst_vtdec_session_output_callback()
|
|
|
|
* is finished, which in turn can wait until there's space in the
|
|
|
|
* output queue, which is being handled by the output loop,
|
|
|
|
* which also uses the stream lock... */
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (vtdec);
|
|
|
|
status = VTDecompressionSessionDecodeFrame (vtdec->session, cm_sample_buffer,
|
2013-12-07 22:55:13 +00:00
|
|
|
input_flags, frame, NULL);
|
2023-08-08 22:20:46 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (vtdec);
|
|
|
|
|
|
|
|
if (status != noErr) {
|
|
|
|
GST_VIDEO_DECODER_ERROR (vtdec, 1, STREAM, DECODE,
|
|
|
|
("Failed to decode frame"),
|
|
|
|
("VTDecompressionSessionDecodeFrame returned %d", (int) status), ret);
|
|
|
|
goto out;
|
|
|
|
}
|
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);
|
2023-08-08 22:20:46 +00:00
|
|
|
frame = NULL;
|
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;
|
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
drop:
|
|
|
|
gst_video_decoder_release_frame (decoder, frame);
|
2013-12-07 22:55:13 +00:00
|
|
|
goto out;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 00:21:27 +00:00
|
|
|
static void
|
|
|
|
gst_vtdec_invalidate_session (GstVtdec * vtdec)
|
|
|
|
{
|
|
|
|
g_return_if_fail (vtdec->session);
|
|
|
|
|
|
|
|
VTDecompressionSessionInvalidate (vtdec->session);
|
|
|
|
CFRelease (vtdec->session);
|
|
|
|
vtdec->session = NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-02 06:30:02 +00:00
|
|
|
static OSStatus
|
|
|
|
gst_vtdec_create_session (GstVtdec * vtdec, GstVideoFormat format,
|
|
|
|
gboolean enable_hardware)
|
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;
|
2023-03-09 03:44:47 +00:00
|
|
|
guint32 cv_format = gst_video_format_to_cvpixelformat (format);
|
2015-01-15 03:09:43 +00:00
|
|
|
|
2015-11-17 00:21:27 +00:00
|
|
|
g_return_val_if_fail (vtdec->session == NULL, FALSE);
|
|
|
|
|
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,
|
2016-06-02 06:30:02 +00:00
|
|
|
kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder,
|
|
|
|
enable_hardware);
|
|
|
|
if (enable_hardware && vtdec->require_hardware)
|
2015-03-07 09:28:35 +00:00
|
|
|
gst_vtutil_dict_set_boolean (videoDecoderSpecification,
|
|
|
|
kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
|
|
|
|
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
|
|
|
|
2019-10-31 08:14:21 +00:00
|
|
|
if (videoDecoderSpecification)
|
|
|
|
CFRelease (videoDecoderSpecification);
|
|
|
|
|
2013-12-07 22:55:13 +00:00
|
|
|
CFRelease (output_image_buffer_attrs);
|
|
|
|
|
2016-06-02 06:30:02 +00:00
|
|
|
return status;
|
2013-12-07 22:55:13 +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);
|
2022-07-13 08:30:49 +00:00
|
|
|
|
|
|
|
if (cm_format == kCMVideoCodecType_HEVC)
|
|
|
|
gst_vtutil_dict_set_data (atoms, CFSTR ("hvcC"), map.data, map.size);
|
|
|
|
else
|
|
|
|
gst_vtutil_dict_set_data (atoms, CFSTR ("avcC"), map.data, map.size);
|
|
|
|
|
2013-04-25 09:17:16 +00:00
|
|
|
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
|
|
|
|
2019-10-31 08:14:21 +00:00
|
|
|
if (extensions)
|
|
|
|
CFRelease (extensions);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-28 23:42:06 +00:00
|
|
|
/* Custom FreeBlock function for CMBlockBuffer */
|
|
|
|
static void
|
|
|
|
cm_block_buffer_freeblock (void *refCon, void *doomedMemoryBlock,
|
|
|
|
size_t sizeInBytes)
|
|
|
|
{
|
|
|
|
GstMapInfo *info = (GstMapInfo *) refCon;
|
|
|
|
|
|
|
|
gst_memory_unmap (info->memory, info);
|
|
|
|
gst_memory_unref (info->memory);
|
|
|
|
g_slice_free (GstMapInfo, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static CMBlockBufferRef
|
|
|
|
cm_block_buffer_from_gst_buffer (GstBuffer * buf, GstMapFlags flags)
|
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
CMBlockBufferRef bbuf;
|
|
|
|
CMBlockBufferCustomBlockSource blockSource;
|
|
|
|
guint memcount, i;
|
|
|
|
|
|
|
|
/* Initialize custom block source structure */
|
|
|
|
blockSource.version = kCMBlockBufferCustomBlockSourceVersion;
|
|
|
|
blockSource.AllocateBlock = NULL;
|
|
|
|
blockSource.FreeBlock = cm_block_buffer_freeblock;
|
|
|
|
|
|
|
|
/* Determine number of memory blocks */
|
|
|
|
memcount = gst_buffer_n_memory (buf);
|
|
|
|
status = CMBlockBufferCreateEmpty (NULL, memcount, 0, &bbuf);
|
|
|
|
if (status != kCMBlockBufferNoErr) {
|
|
|
|
GST_ERROR ("CMBlockBufferCreateEmpty returned %d", (int) status);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go over all GstMemory objects and add them to the CMBlockBuffer */
|
|
|
|
for (i = 0; i < memcount; ++i) {
|
|
|
|
GstMemory *mem;
|
|
|
|
GstMapInfo *info;
|
|
|
|
|
|
|
|
mem = gst_buffer_get_memory (buf, i);
|
|
|
|
|
|
|
|
info = g_slice_new (GstMapInfo);
|
|
|
|
if (!gst_memory_map (mem, info, flags)) {
|
|
|
|
GST_ERROR ("failed mapping memory");
|
|
|
|
g_slice_free (GstMapInfo, info);
|
|
|
|
gst_memory_unref (mem);
|
|
|
|
CFRelease (bbuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
blockSource.refCon = info;
|
|
|
|
status =
|
|
|
|
CMBlockBufferAppendMemoryBlock (bbuf, info->data, info->size, NULL,
|
|
|
|
&blockSource, 0, info->size, 0);
|
|
|
|
if (status != kCMBlockBufferNoErr) {
|
|
|
|
GST_ERROR ("CMBlockBufferAppendMemoryBlock returned %d", (int) status);
|
|
|
|
gst_memory_unmap (mem, info);
|
|
|
|
g_slice_free (GstMapInfo, info);
|
|
|
|
gst_memory_unref (mem);
|
|
|
|
CFRelease (bbuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bbuf;
|
|
|
|
}
|
|
|
|
|
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;
|
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
|
|
|
|
2015-06-28 23:42:06 +00:00
|
|
|
/* create a block buffer */
|
|
|
|
bbuf = cm_block_buffer_from_gst_buffer (buf, GST_MAP_READ);
|
|
|
|
if (bbuf == NULL) {
|
|
|
|
GST_ELEMENT_ERROR (vtdec, RESOURCE, FAILED, (NULL),
|
|
|
|
("failed creating CMBlockBuffer"));
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-06-28 23:42:06 +00:00
|
|
|
/* create a sample buffer */
|
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);
|
2015-06-28 23:42:06 +00:00
|
|
|
if (status != noErr) {
|
|
|
|
GST_ELEMENT_ERROR (vtdec, RESOURCE, FAILED, (NULL),
|
|
|
|
("CMSampleBufferCreate returned %d", (int) status));
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
return sbuf;
|
2013-12-07 22:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-11-17 00:19:57 +00:00
|
|
|
pts1 = pts2 = GST_CLOCK_TIME_NONE;
|
|
|
|
if (frame1->output_buffer)
|
|
|
|
pts1 = GST_BUFFER_PTS (frame1->output_buffer);
|
|
|
|
if (frame2->output_buffer)
|
|
|
|
pts2 = GST_BUFFER_PTS (frame2->output_buffer);
|
2013-12-07 22:55:13 +00:00
|
|
|
|
|
|
|
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;
|
2013-12-10 10:10:54 +00:00
|
|
|
GstVideoCodecState *state;
|
2024-03-18 17:38:41 +00:00
|
|
|
gboolean push_anyway = FALSE;
|
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
|
|
|
|
2015-11-17 00:19:57 +00:00
|
|
|
frame->output_buffer = NULL;
|
|
|
|
|
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);
|
2023-08-08 22:20:46 +00:00
|
|
|
frame->flags |= VTDEC_FRAME_FLAG_ERROR;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 00:19:57 +00:00
|
|
|
if (image_buffer) {
|
|
|
|
GstBuffer *buf = NULL;
|
|
|
|
|
|
|
|
/* FIXME: use gst_video_decoder_allocate_output_buffer */
|
|
|
|
state = gst_video_decoder_get_output_state (GST_VIDEO_DECODER (vtdec));
|
|
|
|
if (state == NULL) {
|
|
|
|
GST_WARNING_OBJECT (vtdec, "Output state not configured, release buffer");
|
|
|
|
frame->flags &= VTDEC_FRAME_FLAG_SKIP;
|
|
|
|
} else {
|
2016-08-26 07:37:54 +00:00
|
|
|
buf =
|
|
|
|
gst_core_video_buffer_new (image_buffer, &state->info,
|
|
|
|
vtdec->texture_cache);
|
2015-11-17 00:19:57 +00:00
|
|
|
gst_video_codec_state_unref (state);
|
|
|
|
GST_BUFFER_PTS (buf) = pts.value;
|
|
|
|
GST_BUFFER_DURATION (buf) = duration.value;
|
|
|
|
frame->output_buffer = buf;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (info_flags & kVTDecodeInfo_FrameDropped) {
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "Frame dropped by video toolbox %p %d",
|
|
|
|
frame, frame->decode_frame_number);
|
2015-11-17 04:18:28 +00:00
|
|
|
frame->flags |= VTDEC_FRAME_FLAG_DROP;
|
2015-11-17 00:19:57 +00:00
|
|
|
} else {
|
2014-07-02 03:27:12 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec, "Decoded frame is NULL");
|
2015-11-17 04:18:28 +00:00
|
|
|
frame->flags |= VTDEC_FRAME_FLAG_SKIP;
|
2015-11-17 00:19:57 +00:00
|
|
|
}
|
2014-07-02 03:27:12 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
/* Limit the amount of frames in our output queue
|
|
|
|
* to avoid processing too many frames ahead.
|
|
|
|
* The DPB * 2 size limit is completely arbitrary. */
|
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
2024-03-18 17:38:41 +00:00
|
|
|
/* If negotiate() gets called from the output loop (via finish_frame()),
|
|
|
|
* it can attempt to drain and call VTDecompressionSessionWaitForAsynchronousFrames,
|
|
|
|
* which will lock up if we decide to wait in this callback, creating a deadlock. */
|
|
|
|
push_anyway = vtdec->is_flushing || vtdec->is_draining;
|
|
|
|
while (!push_anyway
|
2024-04-18 14:07:25 +00:00
|
|
|
&& gst_vec_deque_get_length (vtdec->reorder_queue) >
|
2024-03-18 17:46:17 +00:00
|
|
|
vtdec->dbp_size * 2 + 1) {
|
2023-08-08 22:20:46 +00:00
|
|
|
g_cond_wait (&vtdec->queue_cond, &vtdec->queue_mutex);
|
2024-03-18 17:38:41 +00:00
|
|
|
push_anyway = vtdec->is_flushing || vtdec->is_draining;
|
2023-08-08 22:20:46 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 14:07:25 +00:00
|
|
|
gst_vec_deque_push_sorted (vtdec->reorder_queue, frame, sort_frames_by_pts,
|
2023-08-08 22:20:46 +00:00
|
|
|
NULL);
|
2024-04-18 14:07:25 +00:00
|
|
|
GST_LOG ("pushed frame %d, queue length %" G_GSIZE_FORMAT,
|
|
|
|
frame->decode_frame_number,
|
|
|
|
gst_vec_deque_get_length (vtdec->reorder_queue));
|
2023-08-08 22:20:46 +00:00
|
|
|
g_cond_signal (&vtdec->queue_cond);
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
2013-12-07 22:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2023-08-08 22:20:46 +00:00
|
|
|
gst_vtdec_drain_decoder (GstVideoDecoder * decoder, gboolean flush)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2023-08-08 22:20:46 +00:00
|
|
|
GstVtdec *vtdec = GST_VTDEC (decoder);
|
|
|
|
OSStatus vt_status;
|
2013-12-07 22:55:13 +00:00
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec, "drain_decoder, flushing: %d", flush);
|
2015-01-15 03:09:43 +00:00
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
/* In case of EOS before the first buffer/caps */
|
|
|
|
if (vtdec->session == NULL)
|
|
|
|
return GST_FLOW_OK;
|
2013-12-07 22:55:13 +00:00
|
|
|
|
2024-03-18 17:46:17 +00:00
|
|
|
/* Only early-return here if we're draining (as that needs to output frames).
|
|
|
|
* Flushing doesn't care about errors from downstream. */
|
|
|
|
if (!flush && vtdec->downstream_ret != GST_FLOW_OK
|
2023-08-08 22:20:46 +00:00
|
|
|
&& vtdec->downstream_ret != GST_FLOW_FLUSHING) {
|
|
|
|
GST_WARNING_OBJECT (vtdec, "Output loop stopped with error (%s), leaving",
|
|
|
|
gst_flow_get_name (vtdec->downstream_ret));
|
|
|
|
return vtdec->downstream_ret;
|
|
|
|
}
|
2015-01-15 03:09:43 +00:00
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
g_mutex_lock (&vtdec->queue_mutex);
|
|
|
|
if (flush)
|
|
|
|
vtdec->is_flushing = TRUE;
|
|
|
|
else
|
|
|
|
vtdec->is_draining = TRUE;
|
|
|
|
g_cond_signal (&vtdec->queue_cond);
|
|
|
|
g_mutex_unlock (&vtdec->queue_mutex);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
if (!gst_vtdec_ensure_output_loop (vtdec)) {
|
|
|
|
GST_ERROR_OBJECT (vtdec, "Output loop failed to resume");
|
|
|
|
return GST_FLOW_ERROR;
|
2013-12-07 22:55:13 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (vtdec);
|
|
|
|
vt_status = VTDecompressionSessionWaitForAsynchronousFrames (vtdec->session);
|
|
|
|
if (vt_status != noErr) {
|
|
|
|
GST_WARNING_OBJECT (vtdec,
|
|
|
|
"VTDecompressionSessionWaitForAsynchronousFrames returned %d",
|
|
|
|
(int) vt_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_vtdec_pause_output_loop (vtdec);
|
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (vtdec);
|
|
|
|
|
|
|
|
/* Only reset the draining flag here,
|
|
|
|
* is_flushing will be reset in sink_event() */
|
|
|
|
if (vtdec->is_draining)
|
|
|
|
vtdec->is_draining = FALSE;
|
|
|
|
|
|
|
|
if (vtdec->downstream_ret == GST_FLOW_OK)
|
|
|
|
GST_DEBUG_OBJECT (vtdec, "buffer queue cleaned");
|
|
|
|
else
|
|
|
|
GST_DEBUG_OBJECT (vtdec,
|
|
|
|
"buffer queue not cleaned, output thread returned %s",
|
|
|
|
gst_flow_get_name (vtdec->downstream_ret));
|
|
|
|
|
|
|
|
return vtdec->downstream_ret;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
2013-12-11 06:58:23 +00:00
|
|
|
|
|
|
|
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
|
2023-08-08 22:20:46 +00:00
|
|
|
gst_vtdec_compute_dpb_size (GstVtdec * vtdec,
|
2013-12-11 07:10:19 +00:00
|
|
|
CMVideoCodecType cm_format, GstBuffer * codec_data)
|
|
|
|
{
|
|
|
|
if (cm_format == kCMVideoCodecType_H264) {
|
2023-08-08 22:20:46 +00:00
|
|
|
if (!compute_h264_decode_picture_buffer_size (vtdec, codec_data,
|
|
|
|
&vtdec->dbp_size)) {
|
2013-12-11 07:10:19 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2022-07-13 08:30:49 +00:00
|
|
|
} else if (cm_format == kCMVideoCodecType_HEVC) {
|
2023-08-08 22:20:46 +00:00
|
|
|
if (!compute_hevc_decode_picture_buffer_size (vtdec, codec_data,
|
|
|
|
&vtdec->dbp_size)) {
|
2022-07-13 08:30:49 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2013-12-11 07:10:19 +00:00
|
|
|
} else {
|
2023-08-08 22:20:46 +00:00
|
|
|
vtdec->dbp_size = 0;
|
2013-12-11 07:10:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 22:20:46 +00:00
|
|
|
GST_DEBUG_OBJECT (vtdec, "Calculated DPB size: %d", vtdec->dbp_size);
|
2023-02-23 13:14:57 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
parse_h264_decoder_config_record (GstVtdec * vtdec, GstBuffer * codec_data,
|
|
|
|
GstH264DecoderConfigRecord ** config)
|
|
|
|
{
|
|
|
|
GstH264NalParser *parser = gst_h264_nal_parser_new ();
|
|
|
|
GstMapInfo map;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
gst_buffer_map (codec_data, &map, GST_MAP_READ);
|
|
|
|
|
|
|
|
if (gst_h264_parser_parse_decoder_config_record (parser, map.data, map.size,
|
|
|
|
config) != GST_H264_PARSER_OK) {
|
|
|
|
GST_WARNING_OBJECT (vtdec, "Failed to parse codec-data");
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_h264_nal_parser_free (parser);
|
|
|
|
gst_buffer_unmap (codec_data, &map);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
get_h264_dpb_size_from_sps (GstVtdec * vtdec, GstH264NalUnit * nalu,
|
|
|
|
gint * dpb_size)
|
|
|
|
{
|
|
|
|
GstH264ParserResult result;
|
|
|
|
GstH264SPS sps;
|
|
|
|
gint width_mb, height_mb;
|
|
|
|
gint max_dpb_frames, max_dpb_size, max_dpb_mbs;
|
|
|
|
|
|
|
|
result = gst_h264_parse_sps (nalu, &sps);
|
|
|
|
if (result != GST_H264_PARSER_OK) {
|
|
|
|
GST_WARNING_OBJECT (vtdec, "Failed to parse SPS, result %d", result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_dpb_mbs = get_dpb_max_mb_s_from_level (vtdec, sps.level_idc);
|
|
|
|
if (max_dpb_mbs == -1) {
|
|
|
|
GST_ELEMENT_ERROR (vtdec, STREAM, DECODE, (NULL),
|
|
|
|
("invalid level found in SPS, could not compute max_dpb_mbs"));
|
|
|
|
gst_h264_sps_clear (&sps);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This formula is specified in sections A.3.1.h and A.3.2.f of the 2009
|
|
|
|
* edition of the standard */
|
|
|
|
width_mb = sps.width / 16;
|
|
|
|
height_mb = sps.height / 16;
|
|
|
|
max_dpb_frames = MIN (max_dpb_mbs / (width_mb * height_mb),
|
|
|
|
GST_VTDEC_DPB_MAX_SIZE);
|
|
|
|
|
|
|
|
if (sps.vui_parameters_present_flag
|
|
|
|
&& sps.vui_parameters.bitstream_restriction_flag)
|
|
|
|
max_dpb_frames = MAX (1, sps.vui_parameters.max_dec_frame_buffering);
|
|
|
|
|
|
|
|
/* Some non-conforming H264 streams may request a number of frames
|
|
|
|
* larger than the calculated limit.
|
|
|
|
* See https://chromium-review.googlesource.com/c/chromium/src/+/760276/
|
|
|
|
*/
|
|
|
|
max_dpb_size = MAX (max_dpb_frames, sps.num_ref_frames);
|
|
|
|
if (max_dpb_size > GST_VTDEC_DPB_MAX_SIZE) {
|
|
|
|
GST_WARNING_OBJECT (vtdec, "Too large calculated DPB size %d",
|
|
|
|
max_dpb_size);
|
|
|
|
max_dpb_size = GST_VTDEC_DPB_MAX_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dpb_size = max_dpb_size;
|
|
|
|
|
|
|
|
gst_h264_sps_clear (&sps);
|
2013-12-11 07:10:19 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-12-11 06:58:23 +00:00
|
|
|
static gboolean
|
2023-08-08 22:20:46 +00:00
|
|
|
compute_h264_decode_picture_buffer_size (GstVtdec * vtdec,
|
2023-02-23 13:14:57 +00:00
|
|
|
GstBuffer * codec_data, gint * length)
|
2013-12-11 06:58:23 +00:00
|
|
|
{
|
2023-02-23 13:14:57 +00:00
|
|
|
GstH264DecoderConfigRecord *config = NULL;
|
|
|
|
GstH264NalUnit *nalu;
|
|
|
|
guint8 profile, level;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
gint new_length;
|
|
|
|
guint i;
|
2013-12-11 06:58:23 +00:00
|
|
|
|
2014-10-14 07:19:59 +00:00
|
|
|
*length = 0;
|
|
|
|
|
2023-02-23 13:14:57 +00:00
|
|
|
if (vtdec->video_info.width == 0 || vtdec->video_info.height == 0)
|
2013-12-11 06:58:23 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2023-02-23 13:14:57 +00:00
|
|
|
if (!parse_h264_decoder_config_record (vtdec, codec_data, &config))
|
2014-11-20 08:41:43 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2023-02-23 13:14:57 +00:00
|
|
|
profile = config->profile_indication;
|
|
|
|
level = config->level_indication;
|
2014-11-20 14:50:18 +00:00
|
|
|
GST_INFO_OBJECT (vtdec, "parsed profile %d, level %d", profile, level);
|
2023-02-23 13:14:57 +00:00
|
|
|
|
2014-11-20 14:50:18 +00:00
|
|
|
if (profile == 66) {
|
|
|
|
/* baseline or constrained-baseline, we don't need to reorder */
|
2023-02-23 13:14:57 +00:00
|
|
|
goto out;
|
2014-11-20 14:50:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:14:57 +00:00
|
|
|
for (i = 0; i < config->sps->len; i++) {
|
|
|
|
nalu = &g_array_index (config->sps, GstH264NalUnit, i);
|
|
|
|
|
|
|
|
if (nalu->type != GST_H264_NAL_SPS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!get_h264_dpb_size_from_sps (vtdec, nalu, &new_length))
|
|
|
|
GST_WARNING_OBJECT (vtdec, "Failed to get DPB size from SPS");
|
|
|
|
else
|
|
|
|
*length = MAX (*length, new_length);
|
2013-12-11 06:58:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:14:57 +00:00
|
|
|
out:
|
|
|
|
gst_h264_decoder_config_record_free (config);
|
|
|
|
return ret;
|
2013-12-11 06:58:23 +00:00
|
|
|
}
|
2013-12-11 07:10:19 +00:00
|
|
|
|
2022-07-13 08:30:49 +00:00
|
|
|
static gboolean
|
2023-08-08 22:20:46 +00:00
|
|
|
compute_hevc_decode_picture_buffer_size (GstVtdec * vtdec,
|
2022-07-13 08:30:49 +00:00
|
|
|
GstBuffer * codec_data, int *length)
|
|
|
|
{
|
|
|
|
/* This value should be level dependent (table A.8)
|
|
|
|
* but let's assume the maximum possible one for simplicity. */
|
|
|
|
const gint max_luma_ps = 35651584;
|
|
|
|
const gint max_dpb_pic_buf = 6;
|
|
|
|
gint max_dbp_size, pic_size_samples_y;
|
|
|
|
|
|
|
|
if (vtdec->video_info.width == 0 || vtdec->video_info.height == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* A.4.2 */
|
|
|
|
pic_size_samples_y = vtdec->video_info.width * vtdec->video_info.height;
|
|
|
|
if (pic_size_samples_y <= (max_luma_ps >> 2))
|
|
|
|
max_dbp_size = max_dpb_pic_buf * 4;
|
|
|
|
else if (pic_size_samples_y <= (max_luma_ps >> 1))
|
|
|
|
max_dbp_size = max_dpb_pic_buf * 2;
|
|
|
|
else if (pic_size_samples_y <= ((3 * max_luma_ps) >> 2))
|
|
|
|
max_dbp_size = (max_dpb_pic_buf * 4) / 3;
|
|
|
|
else
|
|
|
|
max_dbp_size = max_dpb_pic_buf;
|
|
|
|
|
|
|
|
*length = MIN (max_dbp_size, 16);
|
|
|
|
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);
|
2023-08-08 22:20:46 +00:00
|
|
|
latency = frame_duration * vtdec->dbp_size;
|
2013-12-11 07:10:19 +00:00
|
|
|
|
|
|
|
GST_INFO_OBJECT (vtdec, "setting latency frames:%d time:%" GST_TIME_FORMAT,
|
2023-08-08 22:20:46 +00:00
|
|
|
vtdec->dbp_size, GST_TIME_ARGS (latency));
|
2013-12-11 07:10:19 +00:00
|
|
|
gst_video_decoder_set_latency (GST_VIDEO_DECODER (vtdec), latency, latency);
|
|
|
|
}
|
2015-03-07 09:28:35 +00:00
|
|
|
|
2016-02-09 03:07:54 +00:00
|
|
|
static void
|
|
|
|
gst_vtdec_set_context (GstElement * element, GstContext * context)
|
|
|
|
{
|
|
|
|
GstVtdec *vtdec = GST_VTDEC (element);
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (element, "setting context %s",
|
|
|
|
gst_context_get_context_type (context));
|
2017-09-22 04:27:51 +00:00
|
|
|
if (!vtdec->ctxh)
|
|
|
|
vtdec->ctxh = gst_gl_context_helper_new (element);
|
2016-02-09 03:07:54 +00:00
|
|
|
gst_gl_handle_set_context (element, context,
|
|
|
|
&vtdec->ctxh->display, &vtdec->ctxh->other_context);
|
2019-09-03 03:56:22 +00:00
|
|
|
|
|
|
|
#if defined (APPLEMEDIA_MOLTENVK)
|
|
|
|
gst_vulkan_handle_set_context (element, context, NULL, &vtdec->instance);
|
|
|
|
#endif
|
|
|
|
|
2016-02-09 03:07:54 +00:00
|
|
|
GST_ELEMENT_CLASS (gst_vtdec_parent_class)->set_context (element, context);
|
|
|
|
}
|
|
|
|
|
2015-03-07 09:28:35 +00:00
|
|
|
#ifndef HAVE_IOS
|
|
|
|
#define GST_TYPE_VTDEC_HW (gst_vtdec_hw_get_type())
|
|
|
|
#define GST_VTDEC_HW(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VTDEC_HW,GstVtdecHw))
|
|
|
|
#define GST_VTDEC_HW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VTDEC_HW,GstVtdecHwClass))
|
|
|
|
#define GST_IS_VTDEC_HW(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VTDEC_HW))
|
|
|
|
#define GST_IS_VTDEC_HW_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VTDEC_HW))
|
|
|
|
|
|
|
|
typedef GstVtdec GstVtdecHw;
|
|
|
|
typedef GstVtdecClass GstVtdecHwClass;
|
|
|
|
|
|
|
|
GType gst_vtdec_hw_get_type (void);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstVtdecHw, gst_vtdec_hw, GST_TYPE_VTDEC);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtdec_hw_class_init (GstVtdecHwClass * klass)
|
|
|
|
{
|
|
|
|
gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
|
|
|
|
"Apple VideoToolbox decoder (hardware only)",
|
2019-02-14 09:49:03 +00:00
|
|
|
"Codec/Decoder/Video/Hardware",
|
2015-03-07 09:28:35 +00:00
|
|
|
"Apple VideoToolbox Decoder",
|
|
|
|
"Ole André Vadla Ravnås <oleavr@soundrop.com>; "
|
|
|
|
"Alessandro Decina <alessandro.d@gmail.com>");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vtdec_hw_init (GstVtdecHw * vtdec)
|
|
|
|
{
|
|
|
|
GST_VTDEC (vtdec)->require_hardware = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_vtdec_register_elements (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_vtdec_debug_category, "vtdec", 0,
|
|
|
|
"debug category for vtdec element");
|
|
|
|
|
|
|
|
#ifdef HAVE_IOS
|
|
|
|
gst_element_register (plugin, "vtdec", GST_RANK_PRIMARY, GST_TYPE_VTDEC);
|
|
|
|
#else
|
|
|
|
gst_element_register (plugin, "vtdec_hw", GST_RANK_PRIMARY + 1,
|
|
|
|
GST_TYPE_VTDEC_HW);
|
|
|
|
gst_element_register (plugin, "vtdec", GST_RANK_SECONDARY, GST_TYPE_VTDEC);
|
|
|
|
#endif
|
|
|
|
}
|