From patchwork Sun Feb 12 18:53:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Miroslav_Sluge=C5=88?= X-Patchwork-Id: 2523 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp581383vsb; Sun, 12 Feb 2017 10:54:12 -0800 (PST) X-Received: by 10.223.139.137 with SMTP id o9mr17977352wra.88.1486925652597; Sun, 12 Feb 2017 10:54:12 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id f38si10743700wrf.325.2017.02.12.10.54.12; Sun, 12 Feb 2017 10:54:12 -0800 (PST) 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; dkim=neutral (body hash did not verify) header.i=@email.cz; dkim=neutral (body hash did not verify) header.i=@email.cz; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=email.cz Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 85243689B1E; Sun, 12 Feb 2017 20:54:04 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mxf2.seznam.cz (mxf2.seznam.cz [77.75.76.123]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7EA9B689A37 for ; Sun, 12 Feb 2017 20:53:57 +0200 (EET) Received: from email.seznam.cz by email-smtpc14a.ng.seznam.cz (email-smtpc14a.ng.seznam.cz [10.23.11.165]) id 384a672f0c9d7f3338d84e22; Sun, 12 Feb 2017 19:54:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=email.cz; s=beta; t=1486925641; bh=Jrv/ykdkNBW4r3YmsJgIMYZdgBiuyHCqrdb1SN0FvCM=; h=DKIM-Signature:Received:From:Subject:To:Message-ID:Date: User-Agent:MIME-Version:Content-Type; b=DI5DO+krI1OuRF4cW2DgDig0AEXJdoZdIm1iecs+kWrCip0SmYGL1c5UPsQg1bQ1j T40FKVGF6yDun9OVOnPCH/uYbZghceUP+imhfIzCKHPFPDfuuNnHw57joocQHxk2eC D32MBYn/BU1bgm61HRABgfu0kqqc84x5ZzWO1DH8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=email.cz; s=beta; t=1486925641; bh=Jrv/ykdkNBW4r3YmsJgIMYZdgBiuyHCqrdb1SN0FvCM=; h=Received:From:Subject:To:Message-ID:Date:User-Agent:MIME-Version: Content-Type; b=QSOoqDrj68JNd2k7npimmPJBg/sohroXPVDp2fJXpGy65aUKWhetTvQnBiSXPXa0/ B5UAdKfxI7aEiNJRD7kOAimxsT5+b031FhkIH6qIKgMTWYOyeCI9q1QdgskgFFvr2Y /v61Uk55Y/T8F/E9GWaCb4T05DPnLVUW/dE4Cbto= Received: from [192.168.0.6] (ip-94-113-140-7.net.upcbroadband.cz [94.113.140.7]) by email-relay29.ng.seznam.cz (Seznam SMTPD 1.3.56) with ESMTP; Sun, 12 Feb 2017 19:54:00 +0100 (CET) From: =?UTF-8?Q?Miroslav_Sluge=c5=88?= To: ffmpeg-devel@ffmpeg.org Message-ID: <58A0AF46.5080107@email.cz> Date: Sun, 12 Feb 2017 19:53:58 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.0.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] nvenc: support dynamic aspect ratio 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" If there is input like DVB-T streams it can change aspect ratio on-the-fly, so nvenc should respect this change and change aspect ratio in encoder. From d836780d56fb7d7a63d91a2af87a0f84743d95d6 Mon Sep 17 00:00:00 2001 From: Miroslav Slugen Date: Sun, 12 Feb 2017 19:51:57 +0100 Subject: [PATCH 1/1] nvenc: support dynamic aspect ratio change --- libavcodec/nvenc.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c index 7005465..dea9a1c 100644 --- a/libavcodec/nvenc.c +++ b/libavcodec/nvenc.c @@ -913,6 +913,20 @@ static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx) return 0; } +static void compute_dar(AVCodecContext *avctx, int *dw, int *dh) { + int sw, sh; + + sw = avctx->width; + sh = avctx->height; + + if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) { + sw*= avctx->sample_aspect_ratio.num; + sh*= avctx->sample_aspect_ratio.den; + } + + av_reduce(dw, dh, sw, sh, 1024 * 1024); +} + static av_cold int nvenc_setup_encoder(AVCodecContext *avctx) { NvencContext *ctx = avctx->priv_data; @@ -949,13 +963,7 @@ static av_cold int nvenc_setup_encoder(AVCodecContext *avctx) ctx->encode_config.version = NV_ENC_CONFIG_VER; - dw = avctx->width; - dh = avctx->height; - if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) { - dw*= avctx->sample_aspect_ratio.num; - dh*= avctx->sample_aspect_ratio.den; - } - av_reduce(&dw, &dh, dw, dh, 1024 * 1024); + compute_dar(avctx, &dw, &dh); ctx->init_encode_params.darHeight = dh; ctx->init_encode_params.darWidth = dw; @@ -1644,6 +1652,39 @@ static int output_ready(AVCodecContext *avctx, int flush) return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth); } +static int reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame) +{ + NV_ENC_RECONFIGURE_PARAMS params = { 0 }; + NvencContext *ctx = avctx->priv_data; + NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs; + NVENCSTATUS ret; + int dw, dh; + + compute_dar(avctx, &dw, &dh); + if (dw != ctx->init_encode_params.darWidth || + dh != ctx->init_encode_params.darHeight) { + av_log(avctx, AV_LOG_VERBOSE, + "Aspect ratio change (DAR): %d:%d -> %d:%d\n", + ctx->init_encode_params.darWidth, + ctx->init_encode_params.darHeight, dw, dh); + + params.version = NV_ENC_RECONFIGURE_PARAMS_VER; + params.reInitEncodeParams = ctx->init_encode_params; + params.reInitEncodeParams.darHeight = dh; + params.reInitEncodeParams.darWidth = dw; + + ret = p_nvenc->nvEncReconfigureEncoder(ctx->nvencoder, ¶ms); + if (ret != NV_ENC_SUCCESS) { + nvenc_print_error(avctx, ret, "Failed to reconfigure nvenc on aspect ratio change"); + } else { + ctx->init_encode_params.darHeight = dh; + ctx->init_encode_params.darWidth = dw; + } + } + + return 0; +} + int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { @@ -1659,6 +1700,8 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, pic_params.version = NV_ENC_PIC_PARAMS_VER; if (frame) { + reconfig_encoder(avctx, frame); + inSurf = get_free_frame(ctx); if (!inSurf) { av_log(avctx, AV_LOG_ERROR, "No free surfaces\n"); -- 2.1.4