X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fvideotoolbox.c;h=ac45e23c16feb8299788673cfa1e2b75b54a8947;hb=d54ae9b782c85e626a1e49a8ee204728746227d1;hp=afec1edf3f6a7be574bdbcfac823add9ba31ed2c;hpb=b656fa710a34ec7c3b192c38344a2c470ff2eaf5;p=ffmpeg diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c index afec1edf3f6..ac45e23c16f 100644 --- a/libavcodec/videotoolbox.c +++ b/libavcodec/videotoolbox.c @@ -36,6 +36,9 @@ #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' }; @@ -43,10 +46,18 @@ 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, @@ -69,19 +80,54 @@ 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; } @@ -285,31 +331,12 @@ CFDataRef ff_videotoolbox_hvcc_extradata_create(AVCodecContext *avctx) 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); @@ -341,17 +368,13 @@ static int videotoolbox_h264_decode_params(AVCodecContext *avctx, 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); @@ -368,6 +391,18 @@ int ff_videotoolbox_h264_decode_slice(AVCodecContext *avctx, 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; @@ -404,11 +439,21 @@ static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame) 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) @@ -440,9 +485,9 @@ static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame) 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; @@ -678,7 +723,9 @@ static CFDictionaryRef videotoolbox_decoder_config_create(CMVideoCodecType codec &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(config_info, - kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder, + codec_type == kCMVideoCodecType_HEVC ? + kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder : + kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder, kCFBooleanTrue); CFMutableDictionaryRef avc_info; @@ -802,6 +849,9 @@ static int videotoolbox_start(AVCodecContext *avctx) 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); @@ -811,7 +861,7 @@ static int videotoolbox_start(AVCodecContext *avctx) 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; } } @@ -891,12 +941,27 @@ static int videotoolbox_h264_end_frame(AVCodecContext *avctx) 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) @@ -904,9 +969,7 @@ 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; } @@ -1055,8 +1118,8 @@ const AVHWAccel ff_hevc_videotoolbox_hwaccel = { .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,