#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 = (CVImageBufferRef)data;
- CVPixelBufferRelease(cv_buffer);
+ VTHWFrame *ref = (VTHWFrame *)data;
+ av_buffer_unref(&ref->hw_frames_ctx);
+ CVPixelBufferRelease(ref->pixbuf);
+
+ av_free(data);
}
static int videotoolbox_buffer_copy(VTContext *vtctx,
return 0;
}
+static int videotoolbox_postproc_frame(void *avctx, AVFrame *frame)
+{
+ VTHWFrame *ref = (VTHWFrame *)frame->buf[0]->data;
+
+ 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->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(VTHWFrame);
+ uint8_t *data = NULL;
+ AVBufferRef *buf = NULL;
int ret = ff_attach_decode_data(frame);
+ FrameDecodeData *fdd;
if (ret < 0)
return ret;
+ data = av_mallocz(size);
+ if (!data)
+ return AVERROR(ENOMEM);
+ buf = av_buffer_create(data, size, videotoolbox_buffer_release, NULL, 0);
+ if (!buf) {
+ av_freep(&data);
+ return AVERROR(ENOMEM);
+ }
+ frame->buf[0] = buf;
+
+ fdd = (FrameDecodeData*)frame->private_ref->data;
+ fdd->post_process = videotoolbox_postproc_frame;
+
frame->width = avctx->width;
frame->height = avctx->height;
frame->format = avctx->pix_fmt;
- frame->buf[0] = av_buffer_alloc(1);
-
- if (!frame->buf[0])
- return AVERROR(ENOMEM);
return 0;
}
return data;
}
-int ff_videotoolbox_buffer_create(VTContext *vtctx, AVFrame *frame)
-{
- av_buffer_unref(&frame->buf[0]);
-
- frame->buf[0] = av_buffer_create((uint8_t*)vtctx->frame,
- sizeof(vtctx->frame),
- videotoolbox_buffer_release,
- NULL,
- AV_BUFFER_FLAG_READONLY);
- if (!frame->buf[0]) {
- return AVERROR(ENOMEM);
- }
-
- frame->data[3] = (uint8_t*)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 = ff_videotoolbox_buffer_create(vtctx, 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;
}
}
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);
+ 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,