X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fvideotoolbox.c;h=da7236f1002c75ecb0a7773cb2f437743b32d0d6;hb=06476249cd2332e30b66576633b2827adf3478dd;hp=f82c31c5df653c401bb9e48baf0b1b1d986f2c8f;hpb=67e8f476b7d3c21686a2d453d052818ac92688b3;p=ffmpeg diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c index f82c31c5df6..da7236f1002 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,16 @@ 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); } @@ -73,22 +82,29 @@ static int videotoolbox_buffer_copy(VTContext *vtctx, 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); @@ -315,35 +331,12 @@ CFDataRef ff_videotoolbox_hvcc_extradata_create(AVCodecContext *avctx) 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); @@ -375,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); @@ -402,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; @@ -438,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 = 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) @@ -474,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; @@ -712,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; @@ -836,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); @@ -845,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; } } @@ -887,6 +903,11 @@ static int videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame) 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"); @@ -925,12 +946,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) @@ -938,9 +974,13 @@ 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; } @@ -1089,8 +1129,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,