From patchwork Thu May 18 08:11:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas George X-Patchwork-Id: 3695 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.10.2 with SMTP id 2csp553460vsk; Thu, 18 May 2017 01:11:46 -0700 (PDT) X-Received: by 10.223.166.143 with SMTP id t15mr1591482wrc.183.1495095106050; Thu, 18 May 2017 01:11:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1495095106; cv=none; d=google.com; s=arc-20160816; b=ddClVUsohnR2cqdl+o90Ipg23n9+Iq4gcWlWBoDRnzbCbXMMCBxflcT8uJWleV5Fbh UZbN/2HPbS9QILwuju19p9NBIBbUZ8MdchrsjaiaL5ijx3MHqLA7hKH96mpEwXuoFECa ZQ6mu8cJnkGAgoeIX1H6td+yXqr/GvPu7I6ENAzHizjzcqaxZckEJ2gj81c7ISudXvoF HA1cvtdxbYCTRGV0GarfehVYMfBTQDeFF20pfS/P2s+EoK5IjeDvgdYoVBmg5L13Mv+V xQsCwwG63gEZaBa5NQgLGUuYvC2bWYRagVY5U8uRJrFg2U1FEs2iU8uUQPlj3SLOPSQf dl1g== 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: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:delivered-to:arc-authentication-results; bh=Lz5fX+iZ1oEW/VTtkYaqPwfjv9AUWZd/zmcp/m3ZtoI=; b=Qn5vLf9+IhyQWQSWI8fpotlfpiIUvBPwqJmZy7WgxQMU5mKcibNjD0wAORiM5/tvJl VKSykqcCYz3xv0qTX5LxaO15i6JFVY9GnAdn7BMbhjKeLPaaB0FmvByGYE87fxMoMuG9 LZMLNzkcrfcMc0KL1ThK9by22dIYD3KJ+BlAf+PdAwX7PfW6hJSniUgU6kdBhSg68VrB RLsQdj46/50rqozUcGfimxGllFmimsim8hrVPx56CEplDc6BK+ca5fuhZjGFO0RSLokJ stuGPFyzVFN0GnsnIjD4Pphb08PAskjriUfeoRxi+3vbuETZu3PXavbZnGXfeaxWdK+Z YJzA== ARC-Authentication-Results: i=1; mx.google.com; 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 a26si5084500wra.327.2017.05.18.01.11.45; Thu, 18 May 2017 01:11:46 -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; 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 6B1DB6898A8; Thu, 18 May 2017 11:11:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from nef2.ens.fr (nef2.ens.fr [129.199.96.40]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CDAA7689821 for ; Thu, 18 May 2017 11:11:23 +0300 (EEST) Received: from phare.normalesup.org (archicubes.ens.fr [129.199.129.80]) by nef2.ens.fr (8.13.6/1.01.28121999) with ESMTP id v4I8BKIl016271 for ; Thu, 18 May 2017 10:11:20 +0200 (CEST) Received: by phare.normalesup.org (Postfix, from userid 1001) id 5B9EAE0087; Thu, 18 May 2017 10:11:20 +0200 (CEST) From: Nicolas George To: ffmpeg-devel@ffmpeg.org Date: Thu, 18 May 2017 10:11:14 +0200 Message-Id: <20170518081114.21425-3-george@nsup.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170518081114.21425-1-george@nsup.org> References: <20170518081114.21425-1-george@nsup.org> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (nef2.ens.fr [129.199.96.32]); Thu, 18 May 2017 10:11:20 +0200 (CEST) Subject: [FFmpeg-devel] [PATCH 3/3] lavfi: add a framework to fix alignment problems. 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" A lot of filters require aligned frame data, but do not document it. It can result in crashes with perfectly valid uses of the API. For now, the default alignment is not set. Signed-off-by: Nicolas George --- libavfilter/avfilter.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/avfilter.h | 9 +++++++++ 2 files changed, 60 insertions(+) diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c index 08b86b010d..ff49e80289 100644 --- a/libavfilter/avfilter.c +++ b/libavfilter/avfilter.c @@ -166,6 +166,7 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad, link->type = src->output_pads[srcpad].type; av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1); link->format = -1; + link->alignment = 1; ff_framequeue_init(&link->fifo, &src->graph->internal->frame_queues); return 0; @@ -1515,15 +1516,60 @@ static void consume_update(AVFilterLink *link, const AVFrame *frame) link->frame_count_out++; } +static int frame_realign(AVFilterLink *link, AVFrame *frame) +{ + AVFrame *tmp; + int ret; + + if (!frame || av_frame_check_align(frame, link->alignment)) + return 0; + + switch (link->type) { + case AVMEDIA_TYPE_VIDEO: + tmp = ff_get_video_buffer(link, link->w, link->h); + break; + case AVMEDIA_TYPE_AUDIO: + tmp = ff_get_audio_buffer(link, frame->nb_samples); + break; + default: + av_assert0(!"reached"); + } + if (!tmp) + return AVERROR(ENOMEM); + + if (ret < 0) + goto fail; + ret = av_frame_copy(tmp, frame); + if (ret < 0) + return ret; + ret = av_frame_copy_props(tmp, frame); + if (ret < 0) + return ret; + av_frame_unref(frame); + av_frame_move_ref(frame, tmp); + av_frame_free(&tmp); + return 0; + +fail: + av_frame_free(&tmp); + return ret; +} + int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe) { AVFrame *frame; + int ret; *rframe = NULL; if (!ff_inlink_check_available_frame(link)) return 0; frame = ff_framequeue_take(&link->fifo); consume_update(link, frame); + ret = frame_realign(link, frame); + if (ret < 0) { + av_frame_free(&frame); + return ret; + } *rframe = frame; return 1; } @@ -1544,6 +1590,11 @@ int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, if (ret < 0) return ret; consume_update(link, frame); + ret = frame_realign(link, frame); + if (ret < 0) { + av_frame_free(&frame); + return ret; + } *rframe = frame; return 1; } diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h index 60662c19ac..16543a666e 100644 --- a/libavfilter/avfilter.h +++ b/libavfilter/avfilter.h @@ -569,6 +569,15 @@ struct AVFilterLink { */ AVBufferRef *hw_frames_ctx; + /** + * Minimum alignment of frame data required by the destination filter. + * All frame data pointers must have the alignment lower bits cleared, + * i.e. be a multiple of alignment. + * Frames with insufficient alignment will be realigned by the + * framework. + */ + unsigned alignment; + #ifndef FF_INTERNAL_FIELDS /**