#ifndef kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder
# define kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder CFSTR("RequireHardwareAcceleratedVideoDecoder")
#endif
+#ifndef kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder
+# define kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder CFSTR("EnableHardwareAcceleratedVideoDecoder")
+#endif
#if !HAVE_KCMVIDEOCODECTYPE_HEVC
enum { kCMVideoCodecType_HEVC = 'hvc1' };
#define VIDEOTOOLBOX_ESDS_EXTRADATA_PADDING 12
+typedef struct VTHWFrame {
+ CVPixelBufferRef pixbuf;
+ AVBufferRef *hw_frames_ctx;
+} VTHWFrame;
+
static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
{
- CVPixelBufferRef cv_buffer = *(CVPixelBufferRef *)data;
- CVPixelBufferRelease(cv_buffer);
+ VTHWFrame *ref = (VTHWFrame *)data;
+ av_buffer_unref(&ref->hw_frames_ctx);
+ CVPixelBufferRelease(ref->pixbuf);
av_free(data);
}
static int videotoolbox_postproc_frame(void *avctx, AVFrame *frame)
{
- CVPixelBufferRef ref = *(CVPixelBufferRef *)frame->buf[0]->data;
+ VTHWFrame *ref = (VTHWFrame *)frame->buf[0]->data;
- if (!ref) {
+ if (!ref->pixbuf) {
av_log(avctx, AV_LOG_ERROR, "No frame decoded?\n");
av_frame_unref(frame);
return AVERROR_EXTERNAL;
}
- frame->data[3] = (uint8_t*)ref;
+ frame->data[3] = (uint8_t*)ref->pixbuf;
+
+ if (ref->hw_frames_ctx) {
+ av_buffer_unref(&frame->hw_frames_ctx);
+ frame->hw_frames_ctx = av_buffer_ref(ref->hw_frames_ctx);
+ if (!frame->hw_frames_ctx)
+ return AVERROR(ENOMEM);
+ }
return 0;
}
int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
{
- size_t size = sizeof(CVPixelBufferRef);
+ size_t size = sizeof(VTHWFrame);
uint8_t *data = NULL;
AVBufferRef *buf = NULL;
int ret = ff_attach_decode_data(frame);
return data;
}
-static int videotoolbox_set_frame(AVCodecContext *avctx, AVFrame *frame)
-{
- VTContext *vtctx = avctx->internal->hwaccel_priv_data;
- if (!frame->buf[0] || frame->data[3]) {
- av_log(avctx, AV_LOG_ERROR, "videotoolbox: invalid state\n");
- av_frame_unref(frame);
- return AVERROR_EXTERNAL;
- }
-
- CVPixelBufferRef *ref = (CVPixelBufferRef *)frame->buf[0]->data;
-
- if (*ref) {
- av_log(avctx, AV_LOG_ERROR, "videotoolbox: frame already set?\n");
- av_frame_unref(frame);
- return AVERROR_EXTERNAL;
- }
-
- *ref = vtctx->frame;
- vtctx->frame = NULL;
-
- return 0;
-}
-
int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx,
const uint8_t *buffer,
uint32_t size)
{
VTContext *vtctx = avctx->internal->hwaccel_priv_data;
- H264Context *h = avctx->priv_data;
+ H264Context *h = avctx->priv_data;
if (h->is_avc == 1) {
return videotoolbox_buffer_copy(vtctx, buffer, size);
return ff_videotoolbox_h264_decode_slice(avctx, buffer, size);
}
-int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
- const uint8_t *buffer,
- uint32_t size)
+static int videotoolbox_common_decode_slice(AVCodecContext *avctx,
+ const uint8_t *buffer,
+ uint32_t size)
{
VTContext *vtctx = avctx->internal->hwaccel_priv_data;
- H264Context *h = avctx->priv_data;
void *tmp;
- if (h->is_avc == 1)
- return 0;
-
tmp = av_fast_realloc(vtctx->bitstream,
&vtctx->allocated_size,
vtctx->bitstream_size+size+4);
return 0;
}
+int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx,
+ const uint8_t *buffer,
+ uint32_t size)
+{
+ H264Context *h = avctx->priv_data;
+
+ if (h->is_avc == 1)
+ return 0;
+
+ return videotoolbox_common_decode_slice(avctx, buffer, size);
+}
+
int ff_videotoolbox_uninit(AVCodecContext *avctx)
{
VTContext *vtctx = avctx->internal->hwaccel_priv_data;
int width = CVPixelBufferGetWidth(pixbuf);
int height = CVPixelBufferGetHeight(pixbuf);
AVHWFramesContext *cached_frames;
+ VTHWFrame *ref;
int ret;
- ret = videotoolbox_set_frame(avctx, frame);
- if (ret < 0)
- return ret;
+ if (!frame->buf[0] || frame->data[3]) {
+ av_log(avctx, AV_LOG_ERROR, "videotoolbox: invalid state\n");
+ av_frame_unref(frame);
+ return AVERROR_EXTERNAL;
+ }
+
+ ref = (VTHWFrame *)frame->buf[0]->data;
+
+ if (ref->pixbuf)
+ CVPixelBufferRelease(ref->pixbuf);
+ ref->pixbuf = vtctx->frame;
+ vtctx->frame = NULL;
// Old API code path.
if (!vtctx->cached_hw_frames_ctx)
vtctx->cached_hw_frames_ctx = hw_frames_ctx;
}
- av_buffer_unref(&frame->hw_frames_ctx);
- frame->hw_frames_ctx = av_buffer_ref(vtctx->cached_hw_frames_ctx);
- if (!frame->hw_frames_ctx)
+ av_buffer_unref(&ref->hw_frames_ctx);
+ ref->hw_frames_ctx = av_buffer_ref(vtctx->cached_hw_frames_ctx);
+ if (!ref->hw_frames_ctx)
return AVERROR(ENOMEM);
return 0;
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(config_info,
- kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
+ codec_type == kCMVideoCodecType_HEVC ?
+ kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder :
+ kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
kCFBooleanTrue);
CFMutableDictionaryRef avc_info;
case kVTVideoDecoderUnsupportedDataFormatErr:
av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox does not support this format.\n");
return AVERROR(ENOSYS);
+ case kVTCouldNotFindVideoDecoderErr:
+ av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox decoder for this format not found.\n");
+ return AVERROR(ENOSYS);
case kVTVideoDecoderMalfunctionErr:
av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox malfunction.\n");
return AVERROR(EINVAL);
case 0:
return 0;
default:
- av_log(avctx, AV_LOG_VERBOSE, "Unknown VideoToolbox session creation error %u\n", (unsigned)status);
+ av_log(avctx, AV_LOG_VERBOSE, "Unknown VideoToolbox session creation error %d\n", (int)status);
return AVERROR_UNKNOWN;
}
}
AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
VTContext *vtctx = avctx->internal->hwaccel_priv_data;
+ frame->crop_right = 0;
+ frame->crop_left = 0;
+ frame->crop_top = 0;
+ frame->crop_bottom = 0;
+
if (vtctx->reconfig_needed == true) {
vtctx->reconfig_needed = false;
av_log(avctx, AV_LOG_VERBOSE, "VideoToolbox decoder needs reconfig, restarting..\n");
return ret;
}
+static int videotoolbox_hevc_start_frame(AVCodecContext *avctx,
+ const uint8_t *buffer,
+ uint32_t size)
+{
+ return 0;
+}
+
+static int videotoolbox_hevc_decode_slice(AVCodecContext *avctx,
+ const uint8_t *buffer,
+ uint32_t size)
+{
+ return videotoolbox_common_decode_slice(avctx, buffer, size);
+}
+
+
static int videotoolbox_hevc_decode_params(AVCodecContext *avctx,
int type,
const uint8_t *buffer,
uint32_t size)
{
- return ff_videotoolbox_h264_decode_slice(avctx, buffer, size);
+ return videotoolbox_common_decode_slice(avctx, buffer, size);
}
static int videotoolbox_hevc_end_frame(AVCodecContext *avctx)
HEVCContext *h = avctx->priv_data;
AVFrame *frame = h->ref->frame;
VTContext *vtctx = avctx->internal->hwaccel_priv_data;
- int ret;
- ret = videotoolbox_common_end_frame(avctx, frame);
+ h->output_frame->crop_right = 0;
+ h->output_frame->crop_left = 0;
+ h->output_frame->crop_top = 0;
+ h->output_frame->crop_bottom = 0;
+
+ int ret = videotoolbox_common_end_frame(avctx, frame);
vtctx->bitstream_size = 0;
return ret;
}
.id = AV_CODEC_ID_HEVC,
.pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
.alloc_frame = ff_videotoolbox_alloc_frame,
- .start_frame = ff_videotoolbox_h264_start_frame,
- .decode_slice = ff_videotoolbox_h264_decode_slice,
+ .start_frame = videotoolbox_hevc_start_frame,
+ .decode_slice = videotoolbox_hevc_decode_slice,
.decode_params = videotoolbox_hevc_decode_params,
.end_frame = videotoolbox_hevc_end_frame,
.frame_params = videotoolbox_frame_params,