From patchwork Tue May 9 13:19:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas George X-Patchwork-Id: 3627 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.3.129 with SMTP id 123csp1331591vsd; Tue, 9 May 2017 06:20:18 -0700 (PDT) X-Received: by 10.223.154.100 with SMTP id z91mr21626wrb.76.1494336018866; Tue, 09 May 2017 06:20:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1494336018; cv=none; d=google.com; s=arc-20160816; b=LfKpv8p98dZlzVfwTMfNr4Uuu2+ptqRXu6H7BW53MrgeUwgm+j/lTUXF3ohcZnyu4u LeGiN2ekdQXcYe5k/UlvC3YhV+JxEhOpTY37RloTiRg2a1VarAk3K/cPw7ssLgsI0qcr HPUrfCKHEYWjvNfu4aM3ozNIlb9sD0ccr7NAJYSalakw6S3VyP32T908Zjt04oNZTDIf H3xO/VuXsfgPfpUah9kxBV9IUu7xTe7oD0PlmmiszGDoL0YE3kikgxIzO4KDazZM+9SW QMfg1IQRWWhpidyck9+ZMoFqnD9wPMBGEpFsyHZdD2PwcpeljyZNTBCSA9eHEQy4IyM7 2c7w== 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=Z/I37qOE/dsiosZeXeo/Ip9on8C7mKSc3WDoHhsYNso=; b=t82LFdT/le645/rtycPQiuC20wyhkpyhF6Rye9jAlNrymv+G9vnuwW0dIbFy8xZEPW LwgykXZfY7hr+TCl4TSVy/h+7fbA0H/A0wm20gsNMsTnwa9q+W12DsAVoW4wujdd2V8T BTlYX5oSWJZ9PzuvcIjLRQZ+vkff5EZU025f+lsEnxnBWqhIUbd4Ms2hD4W80hcW43Mv vFBET8Q/Kspu2t4XEMfVLWHIlzDIRbcj1sCXyfGBqFgEHsAZ5od2L4ZPE9KgEIeGtz4C iKagywMzdVIiFF5f2o//ugjR597DbUbQa4DWHa3oH9Yw4kFBR5jTtU6tJeIyzgf/PWLp qgfg== 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 a31si17604403wrc.277.2017.05.09.06.20.18; Tue, 09 May 2017 06:20:18 -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 41C97688399; Tue, 9 May 2017 16:20:00 +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 5DCE5688340 for ; Tue, 9 May 2017 16:19:54 +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 v49DK0YT066775 for ; Tue, 9 May 2017 15:20:00 +0200 (CEST) Received: by phare.normalesup.org (Postfix, from userid 1001) id 58BE4E00F9; Tue, 9 May 2017 15:20:00 +0200 (CEST) From: Nicolas George To: ffmpeg-devel@ffmpeg.org Date: Tue, 9 May 2017 15:19:43 +0200 Message-Id: <20170509131944.27166-2-george@nsup.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170509131944.27166-1-george@nsup.org> References: <20170509131944.27166-1-george@nsup.org> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (nef2.ens.fr [129.199.96.32]); Tue, 09 May 2017 15:20:00 +0200 (CEST) Subject: [FFmpeg-devel] [PATCH 2/3] lavc: 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 codecs require aligned frame data, but do not document it. It can result in crashes with perfectly valid uses of the API. Design rationale: - requiring frame data to be always aligned would be wasteful; - alignment requirements will evolve (the 16->32 bump is still recent); - requiring applications to worry about alignment is not convenient. For now, the default alignment is fixed at 5. Fix: trac ticket #6349 Signed-off-by: Nicolas George --- libavcodec/avcodec.h | 10 +++++++++ libavcodec/encode.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++--- libavcodec/utils.c | 2 ++ 3 files changed, 66 insertions(+), 3 deletions(-) A little less simple than I hoped due to the const issue, but still reasonable. For backporting to branch, I suggest: copy-paste av_frame_check_align() and make it static, replace avctx->alignment by hardcoded 5. diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index df6d2bc748..7dbed3c82c 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -3671,6 +3671,16 @@ typedef struct AVCodecContext { * (with the display dimensions being determined by the crop_* fields). */ int apply_cropping; + + /** + * Minimum alignment of frame data required by the codec. + * All frame data pointers must have the alignment lower bits cleared, + * i.e. be a multiple of 1<alignment)) + return 0; + + tmp = av_frame_alloc(); + if (!tmp) + return AVERROR(ENOMEM); + tmp->format = (*frame)->format; + tmp->width = (*frame)->width; + tmp->height = (*frame)->height; + tmp->channels = (*frame)->channels; + tmp->channel_layout = (*frame)->channel_layout; + tmp->nb_samples = (*frame)->nb_samples; + ret = av_frame_get_buffer(tmp, 1 << avctx->alignment); + 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; + *frame = *realigned = tmp; + return 0; + +fail: + av_frame_free(&tmp); + return ret; +} + int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, @@ -122,6 +156,7 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, { AVFrame *extended_frame = NULL; AVFrame *padded_frame = NULL; + AVFrame *realigned_frame = NULL; int ret; AVPacket user_pkt = *avpkt; int needs_realloc = !user_pkt.data; @@ -195,6 +230,9 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, av_assert0(avctx->codec->encode2); + ret = frame_realign(avctx, &frame, &realigned_frame); + if (ret < 0) + goto end; ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr); if (!ret) { if (*got_packet_ptr) { @@ -252,6 +290,7 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, end: av_frame_free(&padded_frame); + av_frame_free(&realigned_frame); av_free(extended_frame); #if FF_API_AUDIOENC_DELAY @@ -269,6 +308,7 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, int ret; AVPacket user_pkt = *avpkt; int needs_realloc = !user_pkt.data; + AVFrame *realigned_frame = NULL; *got_packet_ptr = 0; @@ -301,7 +341,9 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, av_assert0(avctx->codec->encode2); - ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr); + ret = frame_realign(avctx, &frame, &realigned_frame); + if (!ret) + ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr); av_assert0(ret <= 0); emms_c(); @@ -340,6 +382,7 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, avctx->frame_number++; } + av_frame_free(&realigned_frame); if (ret < 0 || !*got_packet_ptr) av_packet_unref(avpkt); @@ -406,8 +449,16 @@ int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame return 0; } - if (avctx->codec->send_frame) - return avctx->codec->send_frame(avctx, frame); + if (avctx->codec->send_frame) { + AVFrame *realigned_frame = NULL; + int ret; + ret = frame_realign(avctx, &frame, &realigned_frame); + if (ret < 0) + return ret; + ret = avctx->codec->send_frame(avctx, frame); + av_frame_free(&realigned_frame); + return ret; + } // Emulation via old API. Do it here instead of avcodec_receive_packet, because: // 1. if the AVFrame is not refcounted, the copying will be much more diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 1336e921c9..3681d71487 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -1003,6 +1003,8 @@ FF_ENABLE_DEPRECATION_WARNINGS } avctx->sw_pix_fmt = frames_ctx->sw_format; } + + avctx->alignment = 5; } avctx->pts_correction_num_faulty_pts =