From patchwork Thu May 17 20:19:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 9001 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:155:0:0:0:0:0 with SMTP id c82-v6csp4511718jad; Thu, 17 May 2018 13:27:00 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqJ++IrdzKxnLKNdnyTmKw3/qXd694w3ULHzo21ObahXHCHeIrHEHQnJQC+w0FxDrUddFW/ X-Received: by 2002:adf:8b08:: with SMTP id n8-v6mr4989662wra.189.1526588820190; Thu, 17 May 2018 13:27:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526588820; cv=none; d=google.com; s=arc-20160816; b=DV+ba/WljdUnU/cU8Z9xr5YIVw0lyVhqO6v8g1eI6ruosHbOuXpvnDyHJu9i8EhHWt 0Z2geCNlR1Gr8Yo9WF4+X/Km1QIso87Kz6P3IDYjikR/KQ6JIBCvnJnhL7h3pXkTeIo/ C0/E5G44uUDrfh+AmsDkhp09AnRqbD9T796GQZxBPOjaO081UW801ba7GcloHbGOetTJ rAAtRnaARPMjt9Mxj5hYyOCXR8h6+DAdPJPMj84ban7HgQCgv3lvqtYKdbcpzTrbfxAU WAU9pUngD8wMfA7Voafi9FtuNusVM3TjOezJHLfejYGq9V5HgTp0Wqt+QlC25zP9iyKq 62Og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=B3w/rjzUwQw8+FDyjihq/CCQkB4VfnUF2hAZsWZb3Jk=; b=STj1kemEmY3INwwm0HLLhGxNLslT8hMHFTNiKxgQ1bpyHXKXPh6pe1a3zx9Qr03CIq a/He+smN9UhOji5CAmRVQ47qq0m1KH9HkkBnHzTJHcmZoHrB9yh94EJmhuY51t/QjbRX hLrKQlN3Ifd7YINzckCqm1aoScHhB2mqlkrX1XYEFtNH1NEvsuJIn6jiLk9PhbP5CXPO eNGGOumNpwpfbv5GjbRKjyl+cyLDetvpqc7PdOk6g+2pUUe02HcbT6K0w7c73tVupf9b OhM0ElhkBI+8K2aoUUl069KrN9DPYS/Yq1mr3kgjazlgkBMznqIomKFx8C9r2V63Nv/L 9TRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@tmm1-net.20150623.gappssmtp.com header.s=20150623 header.b=xjxFTAnz; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u9-v6si4376471wmg.105.2018.05.17.13.26.59; Thu, 17 May 2018 13:27:00 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@tmm1-net.20150623.gappssmtp.com header.s=20150623 header.b=xjxFTAnz; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AF97568A4F2; Thu, 17 May 2018 23:26:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f67.google.com (mail-pg0-f67.google.com [74.125.83.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3FB5A689F18 for ; Thu, 17 May 2018 23:26:12 +0300 (EEST) Received: by mail-pg0-f67.google.com with SMTP id u8-v6so2293946pgp.13 for ; Thu, 17 May 2018 13:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tmm1-net.20150623.gappssmtp.com; s=20150623; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=GiKg3//i2aNPrc3i6TKN0xM3pr366kz4CYtI9b7NCj0=; b=xjxFTAnzaH8+/cYigDnQCnbsf9Pf57btgt0d+kdQ/aKPOU4r5CMwKCQ78yWxvrOO7H PMdNXmJ13EJFNaNb4JjDdnKOmSG8nxPVuNTV5QwZ5wIcqqbSc4WX8yRh3fRtaIR/wFWO YgClwjYajdcTVFyBUSr8q0QS6jwPUwJN6rK9LCEUdmTwbb/hlg15wHRUiol83Z3cqKvA xbXzJK8N0Pqq5fQK79AQh33LNqHGyi1qvlw8+2qfR7Fq3vy0rZslZ1O5CpftPO7OCgIf CjK/OzVCoaYFeGQWIOqwp8boLexNe/RmM6VZWk9cKJG2KKh7Zl65xdRPwexer9Nv6XG4 m1WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=GiKg3//i2aNPrc3i6TKN0xM3pr366kz4CYtI9b7NCj0=; b=GZ89hQZ/hUjgbvaFQ/Y2eqaJGh4VnstIeuLoM9N33OK9zHAvJIuxayXqQsuFk4gn2H bnsrUswaz5jg3TV7TC1Sr9QadYRo9wuv/6Tk1s9alDs0kfpfQA9aitpNw0RxEWd/qDqY hb988ma88wVYUtydyV14mw0nZvdh4VQHzPcBab1YHPEihhf9pJJOBJ8gALVSPoKBoFIV 23CDkm5tJdL7l8UNLR6EKPpbIypFg8SSnJUbdl0hVh1Q1mxq9IsBooqf0thVOQMG13kZ 5m/UarCq2K6bfEfCIwY5ow/StCxPzha0kiEZCSUw0VPcc4yPLddRIAwVVyIOUlQ279Wt 5dMA== X-Gm-Message-State: ALKqPweVn5VHyy7VRG20ahI3m6mHJb+U0d0yc3JYUizQLYeb1LAoqu93 F/Bs1UgLcIvWK+qqQvzepqmx9xvM X-Received: by 2002:a62:f55b:: with SMTP id n88-v6mr6487996pfh.208.1526588387076; Thu, 17 May 2018 13:19:47 -0700 (PDT) Received: from localhost.localdomain (c-69-181-54-242.hsd1.ca.comcast.net. [69.181.54.242]) by smtp.gmail.com with ESMTPSA id g64-v6sm11447372pfd.50.2018.05.17.13.19.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 17 May 2018 13:19:46 -0700 (PDT) From: Aman Gupta To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 May 2018 13:19:40 -0700 Message-Id: <20180517201940.79542-1-ffmpeg@tmm1.net> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20180504225711.69909-4-ffmpeg@tmm1.net> References: <20180504225711.69909-4-ffmpeg@tmm1.net> Subject: [FFmpeg-devel] [PATCH v2] avcodec/videotoolbox: fix decoding of some hevc videos X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: nfxjfg@googlemail.com, Aman Gupta , kernrj@gmail.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Aman Gupta See https://s3.amazonaws.com/tmm1/videotoolbox/germany-hevc-zdf.ts This moves the hw_frames_ctx reference from the AVFrame to a new VTHWFrame which now holds both the CVPixelBufferRef and the AVBuffer. Previously we would set the hw_frames_context on the AVFrame directly, but on some videos the HEVC decoder would copy and move frames around to reorder them, and in the process could accidentally free the hw_frames_ctx, such that it was NULL by the time videotoolbox_postproc_frame() got called. This appears to be a bug specific to the HEVC decoder, but after several days of debugging I was unable to figure out where the memory management issue is occurring. This patch works around the issue by assigning hw_frames_ctx only once the AVFrame is about to returned to the user. Fixes playback of the HEVC sample linked above on both macOS and iOS. Signed-off-by: Aman Gupta --- libavcodec/videotoolbox.c | 67 +++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c index fe5c9004b4..ac45e23c16 100644 --- a/libavcodec/videotoolbox.c +++ b/libavcodec/videotoolbox.c @@ -46,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); } @@ -76,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); @@ -318,26 +331,6 @@ 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) - CVPixelBufferRelease(*ref); - - *ref = vtctx->frame; - vtctx->frame = NULL; - - return 0; -} - int ff_videotoolbox_h264_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) @@ -446,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) @@ -482,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;