From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 5E74F40861 for ; Sun, 30 Oct 2022 13:10:40 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A893B68BD13; Sun, 30 Oct 2022 15:10:37 +0200 (EET) Received: from w4.tutanota.de (w4.tutanota.de [81.3.6.165]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 762FA68B8BF for ; Sun, 30 Oct 2022 15:10:30 +0200 (EET) Received: from tutadb.w10.tutanota.de (unknown [192.168.1.10]) by w4.tutanota.de (Postfix) with ESMTP id D257E1060154 for ; Sun, 30 Oct 2022 13:10:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1667135429; s=s1; d=lynne.ee; h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Content-Transfer-Encoding:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender; bh=nIe8hE+CWOuV4PzH38vQaTSN+2XAO82lwdY2sFZYoSc=; b=coNTwdoMts4ZpSz7OBZrQ1QpgHF4wsKMXpHgzS35xRAqZy0S63dq+WvhkS/TXl71 cjaYqyKRCnOl7i2CpvPnr3Unh68mgg8kw9I0ut5ML2gYQ7aerW3LLK2Gto5HPFnB2+N 3AwaHMzFq3gB0sOviBZR5ucOVyjyQ8QVKWQ8qXx9woAAgi9yxf0Z5lfCx6ChpMPEMpP M3DEBL12BV4gUu3hXcRlhauh07dGqN4hO3mTM9DYxGQfTvtnkc1wrT9iqHqs+ctfEBG rw1nsH7+nS/AX5fO5Bsh8c9NUclyXVer0GJuPHXtUwWN4tKFQPKa0IwyLcPtPtKbEmB VrAnYF5Cyg== Date: Sun, 30 Oct 2022 14:10:29 +0100 (CET) From: Lynne To: FFmpeg development discussions and patches Message-ID: In-Reply-To: <20220927210605.692742-1-gustav.grusell@gmail.com> References: <20220927210605.692742-1-gustav.grusell@gmail.com> MIME-Version: 1.0 Subject: Re: [FFmpeg-devel] [PATCH] avcodec/libsvtav1: Add support for multipass encoding 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Sep 27, 2022, 23:06 by gustav.grusell@gmail.com: > Implements support for 2-pass CRF and 3-pass VBR by implementing > reading and writing of stats file, and passing the pass number on > to the encoder. For 3-pass VBR, the first pass should be run with > '-pass 1', the second with '-pass 3' and the third with '-pass 2'. > > Signed-off-by: Gustav Grusell > --- > libavcodec/libsvtav1.c | 82 ++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 82 insertions(+) > > diff --git a/libavcodec/libsvtav1.c b/libavcodec/libsvtav1.c > index 2f5634cee0..73fece49b7 100644 > --- a/libavcodec/libsvtav1.c > +++ b/libavcodec/libsvtav1.c > @@ -24,6 +24,7 @@ > #include > #include > > +#include "libavutil/base64.h" > #include "libavutil/common.h" > #include "libavutil/frame.h" > #include "libavutil/imgutils.h" > @@ -312,6 +313,22 @@ static int config_enc_params(EbSvtAv1EncConfiguration *param, > cpb_props->avg_bitrate = avctx->bit_rate; > } > > + if (avctx->flags & AV_CODEC_FLAG_PASS2) { > + if (param->rate_control_mode == SVT_AV1_RC_MODE_VBR) { > + if (avctx->flags & AV_CODEC_FLAG_PASS1) { > + param->pass = 2; > + } else { > + param->pass = 3; > + } > + } else { > + param->pass = 2; > + } > + } else if (avctx->flags & AV_CODEC_FLAG_PASS1) { > + param->pass = 1; > + } else { > + param->pass = 0; > + } > + > return 0; > } > > @@ -371,6 +388,34 @@ static av_cold int eb_enc_init(AVCodecContext *avctx) > return ret; > } > > + if (svt_enc->enc_params.pass >= 2) { > + int decode_size; > + > + if (!avctx->stats_in) { > + av_log(avctx, AV_LOG_ERROR, "No stats file for %s pass\n", > + svt_enc->enc_params.pass == 2 ? "second" : "third"); > + return AVERROR_INVALIDDATA; > + } > + > + svt_enc->enc_params.rc_stats_buffer.sz = strlen(avctx->stats_in) * 3 / 4; > + svt_enc->enc_params.rc_stats_buffer.buf = av_malloc(svt_enc->enc_params.rc_stats_buffer.sz); > + if (!svt_enc->enc_params.rc_stats_buffer.buf) { > + av_log(avctx, AV_LOG_ERROR, > + "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", > + svt_enc->enc_params.rc_stats_buffer.sz); > + svt_enc->enc_params.rc_stats_buffer.sz = 0; > + return AVERROR(ENOMEM); > + } > + decode_size = av_base64_decode(svt_enc->enc_params.rc_stats_buffer.buf, avctx->stats_in, > + svt_enc->enc_params.rc_stats_buffer.sz); > + if (decode_size < 0) { > + av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n"); > + return AVERROR_INVALIDDATA; > + } > + > + svt_enc->enc_params.rc_stats_buffer.sz = decode_size; > + } > + > svt_ret = svt_av1_enc_set_parameter(svt_enc->svt_handle, &svt_enc->enc_params); > if (svt_ret != EB_ErrorNone) { > return svt_print_error(avctx, svt_ret, "Error setting encoder parameters"); > @@ -544,6 +589,38 @@ static int eb_receive_packet(AVCodecContext *avctx, AVPacket *pkt) > if (headerPtr->flags & EB_BUFFERFLAG_EOS) > svt_enc->eos_flag = EOS_RECEIVED; > > + if (svt_enc->eos_flag == EOS_RECEIVED) { > + if (svt_enc->enc_params.pass == 1) { > + SvtAv1FixedBuf first_pass_stat; > + EbErrorType ret = svt_av1_enc_get_stream_info( > + svt_enc->svt_handle, > + SVT_AV1_STREAM_INFO_FIRST_PASS_STATS_OUT, > + &first_pass_stat); > + if (ret == EB_ErrorNone) { > + size_t b64_size = AV_BASE64_SIZE(first_pass_stat.sz); > + avctx->stats_out = av_malloc(b64_size); > + if (!avctx->stats_out) { > + av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", > + b64_size); > + return AVERROR(ENOMEM); > + } > + av_base64_encode(avctx->stats_out, b64_size, first_pass_stat.buf, > + first_pass_stat.sz); > + } > + } > + if (svt_enc->enc_params.pass == 2) { > + size_t b64_size = AV_BASE64_SIZE(svt_enc->enc_params.rc_stats_buffer.sz); > + avctx->stats_out = av_malloc(b64_size); > + if (!avctx->stats_out) { > + av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", > + b64_size); > + return AVERROR(ENOMEM); > + } > + av_base64_encode(avctx->stats_out, b64_size, svt_enc->enc_params.rc_stats_buffer.buf, > + svt_enc->enc_params.rc_stats_buffer.sz); > + } > + } > + > ff_side_data_set_encoder_stats(pkt, headerPtr->qp * FF_QP2LAMBDA, NULL, 0, pict_type); > > svt_av1_enc_release_out_buffer(&headerPtr); > @@ -564,6 +641,11 @@ static av_cold int eb_enc_close(AVCodecContext *avctx) > av_freep(&svt_enc->in_buf); > } > > + if (svt_enc->enc_params.rc_stats_buffer.buf) { > + av_freep(&svt_enc->enc_params.rc_stats_buffer.buf); > + svt_enc->enc_params.rc_stats_buffer.sz = 0; > + } > + > av_buffer_pool_uninit(&svt_enc->pool); > av_frame_free(&svt_enc->frame); > 2-pass doesn't seem to work, the encoder complains it's not the final pass. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".