From patchwork Fri Aug 7 08:18:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: leozhang X-Patchwork-Id: 21521 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 0AF78448F6A for ; Fri, 7 Aug 2020 11:19:01 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D014268B82C; Fri, 7 Aug 2020 11:19:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from ssqr-bj.qiyi.com (mx1.qiyi.com [202.108.14.131]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B874F68B570 for ; Fri, 7 Aug 2020 11:18:53 +0300 (EEST) Received: from mail.iqiyi.com ([10.16.130.3]) by ssqr-bj.qiyi.com with ESMTP id 0778IZl7089410 for ; Fri, 7 Aug 2020 16:18:35 +0800 (GMT-8) (envelope-from leozhang@qiyi.com) From: leozhang To: Date: Fri, 7 Aug 2020 16:18:28 +0800 Message-ID: <1596788308-57068-1-git-send-email-leozhang@qiyi.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.16.84.72] X-ClientProxiedBy: BJ-CAS31.iqiyi.pps (10.11.50.86) To EXCH28B.iqiyi.pps (10.16.148.55) X-DNSRBL: X-MAIL: ssqr-bj.qiyi.com 0778IZl7089410 Subject: [FFmpeg-devel] [PATCH] avcodec/nvenc: support dynamic resolution change 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Allow dynamic resolution change, this is useful for real time video communication application. Use below commands to test it, ffmpeg -i reinit-large_420_8-to-small_420_8.h264 -noautoscale -c:v hevc_nvenc out.265 -loglevel verbose -y ffmpeg -i reinit-large_420_8-to-small_420_8.h264 -noautoscale -c:v h264_nvenc out.264 -loglevel verbose -y Signed-off-by: leozhang --- libavcodec/nvenc.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c index 8f5036b..68e2a35 100644 --- a/libavcodec/nvenc.c +++ b/libavcodec/nvenc.c @@ -2051,7 +2051,7 @@ static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame) NV_ENC_RECONFIGURE_PARAMS params = { 0 }; int needs_reconfig = 0; int needs_encode_config = 0; - int reconfig_bitrate = 0, reconfig_dar = 0; + int reconfig_bitrate = 0, reconfig_dar = 0, reconfig_res = 0; int dw, dh; params.version = NV_ENC_RECONFIGURE_PARAMS_VER; @@ -2071,6 +2071,21 @@ static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame) reconfig_dar = 1; } + if (frame && (frame->width != ctx->init_encode_params.encodeWidth || + frame->height != ctx->init_encode_params.encodeHeight)) { + av_log(avctx, AV_LOG_VERBOSE, + "input frame changed from %dx%d -> %dx%d\n", + ctx->init_encode_params.encodeWidth, + ctx->init_encode_params.encodeHeight, + frame->width, frame->height); + + params.reInitEncodeParams.encodeWidth = frame->width; + params.reInitEncodeParams.encodeHeight = frame->height; + + needs_reconfig = 1; + reconfig_res = 1; + } + if (ctx->rc != NV_ENC_PARAMS_RC_CONSTQP && ctx->support_dyn_bitrate) { if (avctx->bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->bit_rate) { av_log(avctx, AV_LOG_VERBOSE, @@ -2124,6 +2139,11 @@ static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame) ctx->init_encode_params.darWidth = dw; } + if (reconfig_res) { + avctx->width = ctx->init_encode_params.encodeWidth = params.reInitEncodeParams.encodeWidth; + avctx->height = ctx->init_encode_params.encodeHeight = params.reInitEncodeParams.encodeHeight; + } + if (reconfig_bitrate) { ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate; ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;