From patchwork Mon Apr 11 09:05:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35251 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:671c:b0:7c:62c8:b2d1 with SMTP id q28csp938080pzh; Mon, 11 Apr 2022 02:05:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzRgJ5gua1IcvW/seIgkhstzb+fTJ+gHe8e+jxs3WECoijk0wXjsP1Qtxly0JK7xToCNilX X-Received: by 2002:a05:6402:1e8a:b0:41a:4242:7de9 with SMTP id f10-20020a0564021e8a00b0041a42427de9mr32127014edf.284.1649667946637; Mon, 11 Apr 2022 02:05:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649667946; cv=none; d=google.com; s=arc-20160816; b=HfZ1alpDfVX+auXR5rsZQMqmdJLPE5le6T7VhRyeLRDhLr70IwKyF2OPjEIc1o/jIe ZKegRIiS9Qhjq5DBWTyY2l75/uHk+9cn8we4uPfAuNijdIV6munBA4tasroCATU7RAN/ 9HmXgnjTPEldSetPxs9BLyDzgH4p3J6j/if4i1kM5HwgtUmjRWrn1Bdp325IwpEDwhKI vWqruUFR4RzSSFBRWWa05qO+1uxnK7Ic8DR/0+3x0xJHdyHFq0Gpw49iA7d3EALssjs5 fb09M8QaV5s0C4Ku7cSRVkzCX/s3V+rndcfdG6ueOOpqGkcLjqwW0/4uPJl0fGHYW0BK 6Ohg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=Bn3CZwHlhfl5jR2WL0dcrKtlVcrKsw5OzN7rt1AEZHY=; b=IhFD4mKiYM8pzkvtur9iIfCoYgDhNm2pVKv7Xfs0xoqXRrAphPqrSmb6fDVmeg0G0I fSRfpN6Mx1aGP7yywslT9WU81vTd2AH/ewNzsKlt4bnZfy9bEqctxBBIYoPe7GlRRX1f JCB0X7qGVePJ1T+zq5XN0Xm4gZUkkuY7kUHYvnGlZt3ix7rUxS7UALu4uGtP3Tby9+da x3BdLrAC9IyhtceGyE1LFqFc+0eWwKvW4nuEOpl4SDaNHsW/BOFp+szSZRlrnavdea0A 1Z2D7/2324EJl+v6JLnIEGqMWF7vLadOEFBm8WH3nYZUHBXSh7Y6OlL4iqF/ZPfKyMKa YGNQ== 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 ka18-20020a170907991200b006e6ec6e7598si6380476ejc.425.2022.04.11.02.05.46; Mon, 11 Apr 2022 02:05: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 C8BE868B3A1; Mon, 11 Apr 2022 12:05:43 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B7DD668B0DB for ; Mon, 11 Apr 2022 12:05:36 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 750C2240179 for ; Mon, 11 Apr 2022 11:05:36 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 1c89_51rS5va for ; Mon, 11 Apr 2022 11:05:35 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id A81F82400F5 for ; Mon, 11 Apr 2022 11:05:35 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 7EB703A0406; Mon, 11 Apr 2022 11:05:35 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 11 Apr 2022 11:05:12 +0200 Message-Id: <20220411090512.10466-1-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] lavc/encode: add an encoder-specific get_buffer() variant X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: F3md3Cn+CPjw Several encoders (roqvideo, svq1, snow, and the mpegvideo family) currently call ff_get_buffer(). However this function is written assuming it is called by a decoder. Though nothing has been obviously broken by this until now, this may change in the future. To avoid potential future issues, introduce a simple encode-specific wrapper around avcodec_default_get_buffer2() and enforce its use in encoders. --- libavcodec/decode.c | 2 ++ libavcodec/encode.c | 34 ++++++++++++++++++++++++++++++++++ libavcodec/encode.h | 5 +++++ libavcodec/mpegpicture.c | 16 +++++++++------- libavcodec/roqvideoenc.c | 4 ++-- libavcodec/snow.c | 8 ++++++-- libavcodec/svq1enc.c | 4 ++-- 7 files changed, 60 insertions(+), 13 deletions(-) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index fffad5f700..3733e6d4b8 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -1405,6 +1405,8 @@ int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags) int override_dimensions = 1; int ret; + av_assert0(av_codec_is_decoder(avctx->codec)); + if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN || (ret = av_image_check_size2(FFALIGN(avctx->width, STRIDE_ALIGN), avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) { diff --git a/libavcodec/encode.c b/libavcodec/encode.c index 8b0d4443cd..adeb28f0d3 100644 --- a/libavcodec/encode.c +++ b/libavcodec/encode.c @@ -571,3 +571,37 @@ FF_ENABLE_DEPRECATION_WARNINGS return 0; } + +int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame) +{ + int ret; + + switch (avctx->codec->type) { + case AVMEDIA_TYPE_VIDEO: + frame->format = avctx->pix_fmt; + if (frame->width <= 0 || frame->height <= 0) { + frame->width = FFMAX(avctx->width, avctx->coded_width); + frame->height = FFMAX(avctx->height, avctx->coded_height); + } + + break; + case AVMEDIA_TYPE_AUDIO: + frame->sample_rate = avctx->sample_rate; + frame->format = avctx->sample_fmt; + if (!frame->ch_layout.nb_channels) { + ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout); + if (ret < 0) + return ret; + } + break; + } + + ret = avcodec_default_get_buffer2(avctx, frame, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + av_frame_unref(frame); + return ret; + } + + return 0; +} diff --git a/libavcodec/encode.h b/libavcodec/encode.h index 97b3acf9df..b2536bf0f3 100644 --- a/libavcodec/encode.h +++ b/libavcodec/encode.h @@ -44,6 +44,11 @@ int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame); */ int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags); +/** + * Allocate buffers for a frame. Encoder equivalent to ff_get_buffer(). + */ +int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame); + /** * Check AVPacket size and allocate data. * diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c index 681ccc2b82..aaa1df0bd8 100644 --- a/libavcodec/mpegpicture.c +++ b/libavcodec/mpegpicture.c @@ -26,6 +26,7 @@ #include "libavutil/imgutils.h" #include "avcodec.h" +#include "encode.h" #include "motion_est.h" #include "mpegpicture.h" #include "mpegutils.h" @@ -123,14 +124,15 @@ static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic, int r, ret; pic->tf.f = pic->f; - if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE && - avctx->codec_id != AV_CODEC_ID_VC1IMAGE && - avctx->codec_id != AV_CODEC_ID_MSS2) { - if (edges_needed) { - pic->f->width = avctx->width + 2 * EDGE_WIDTH; - pic->f->height = avctx->height + 2 * EDGE_WIDTH; - } + if (edges_needed) { + pic->f->width = avctx->width + 2 * EDGE_WIDTH; + pic->f->height = avctx->height + 2 * EDGE_WIDTH; + + r = ff_encode_alloc_frame(avctx, pic->f); + } else if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE && + avctx->codec_id != AV_CODEC_ID_VC1IMAGE && + avctx->codec_id != AV_CODEC_ID_MSS2) { r = ff_thread_get_ext_buffer(avctx, &pic->tf, pic->reference ? AV_GET_BUFFER_FLAG_REF : 0); } else { diff --git a/libavcodec/roqvideoenc.c b/libavcodec/roqvideoenc.c index ef7861088d..95db514140 100644 --- a/libavcodec/roqvideoenc.c +++ b/libavcodec/roqvideoenc.c @@ -1081,8 +1081,8 @@ static int roq_encode_frame(AVCodecContext *avctx, AVPacket *pkt, if (enc->first_frame) { /* Alloc memory for the reconstruction data (we must know the stride for that) */ - if ((ret = ff_get_buffer(avctx, roq->current_frame, 0)) < 0 || - (ret = ff_get_buffer(avctx, roq->last_frame, 0)) < 0) + if ((ret = ff_encode_alloc_frame(avctx, roq->current_frame)) < 0 || + (ret = ff_encode_alloc_frame(avctx, roq->last_frame )) < 0) return ret; /* Before the first video frame, write a "video info" chunk */ diff --git a/libavcodec/snow.c b/libavcodec/snow.c index 97b0448dbf..293a0eb7d9 100644 --- a/libavcodec/snow.c +++ b/libavcodec/snow.c @@ -23,6 +23,7 @@ #include "libavutil/opt.h" #include "libavutil/thread.h" #include "avcodec.h" +#include "encode.h" #include "me_cmp.h" #include "snow_dwt.h" #include "internal.h" @@ -76,8 +77,11 @@ int ff_snow_get_buffer(SnowContext *s, AVFrame *frame) if (edges_needed) { frame->width += 2 * EDGE_WIDTH; frame->height += 2 * EDGE_WIDTH; - } - if ((ret = ff_get_buffer(s->avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) + + ret = ff_encode_alloc_frame(s->avctx, frame); + } else + ret = ff_get_buffer(s->avctx, frame, AV_GET_BUFFER_FLAG_REF); + if (ret < 0) return ret; if (edges_needed) { for (i = 0; frame->data[i]; i++) { diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c index d934ef4646..999f974bcf 100644 --- a/libavcodec/svq1enc.c +++ b/libavcodec/svq1enc.c @@ -595,12 +595,12 @@ static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt, } if (!s->current_picture->data[0]) { - if ((ret = ff_get_buffer(avctx, s->current_picture, 0)) < 0) { + if ((ret = ff_encode_alloc_frame(avctx, s->current_picture)) < 0) { return ret; } } if (!s->last_picture->data[0]) { - ret = ff_get_buffer(avctx, s->last_picture, 0); + ret = ff_encode_alloc_frame(avctx, s->last_picture); if (ret < 0) return ret; }