From patchwork Tue Sep 6 09:22:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Chen, Wenbin" X-Patchwork-Id: 37694 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:139a:b0:8f:1db5:eae2 with SMTP id w26csp3135861pzh; Tue, 6 Sep 2022 02:26:01 -0700 (PDT) X-Google-Smtp-Source: AA6agR6/mWnSG28HbgkUsY43tuoavQ73gemEIsqlstutiL2wg8oC4ttswoMtC20Kejmm5i4b923i X-Received: by 2002:a05:6402:e01:b0:442:dd7e:f49d with SMTP id h1-20020a0564020e0100b00442dd7ef49dmr46121358edh.355.1662456361571; Tue, 06 Sep 2022 02:26:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662456361; cv=none; d=google.com; s=arc-20160816; b=ef2t35LgP5uTB7iwM3+PhLWnL6uh3PtIcVKosi5Y7M3Ic3+iV2FK2q7yKIDJDALSfV FmR3uPlbuKjGxb6lc0RYUREdj3BxtP9KXFBdVbUSbvM5tN6RdVVSDm8B02qlT8AMBIK7 TK45imJeCmcNFXPf276eQ08Qe97lweLgXvwoyxT+g534ub8p0L6tufMvCuyKPyIKUVBk i1tFL9rAdzXGYbhS2E4gUYrKl6qKuX3LD0DkS5vCeYj13WotQ1CNMuAh+PF/5HI7pwy8 XgDUmMVUUntNPSaWdcVYFfUUPskPw8UVx+zn583lb3M7W5T57reGv3Fm1IcsNgTYRLVr fp4g== 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:dkim-signature:delivered-to; bh=bmYIRhDgvc7C9hSLPzg4w/i4ExF3yXFfzaM5RnjXbvU=; b=P3By0hvJO4hDmuHkdH1AuCVSaibczz5zMirrWW54e/R4SXQM7A3W7VYOs7QiMRV9cb 4J5OHMOytqKUCGF2hLOnfu4S6dGQcXzb5LWtUw1+CO7tXlFTPDmFemyiLwVp/TYpFBCP Ddz6QLgB2FU8DSwfyUqoQZziMxdgxrrzllsNkEs+Uf3mLoUlLOmbB23zlrCgYeLkfLea WBzvT2VAcaErkMpVGnKAccVHyq1rY+1tMxkXZWyUosJUR6EUSVhMdjGPaT4xAJ02xlm8 XGKF4UnCIfQDgwP3mOeerd6VaiLoICwVUQdLjkldRUlSeUTbGbq0D64DCTShgn1WHH5J sxbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=oI1a1dCp; 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 k7-20020aa7d2c7000000b0044e6700024asi4617130edr.116.2022.09.06.02.26.01; Tue, 06 Sep 2022 02:26:01 -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; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=oI1a1dCp; 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 50BB668BADF; Tue, 6 Sep 2022 12:25:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6EF4668BAD6 for ; Tue, 6 Sep 2022 12:25:08 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662456313; x=1693992313; h=from:to:subject:date:message-id:in-reply-to:references: mime-version:content-transfer-encoding; bh=d+owIHjPMmWOhdv85sibDyZSbAcPrXO5mYfgmp1qKLE=; b=oI1a1dCpiPTp/CXsSQCIQqG3dGsLUW3MVXssEISOKXEGIZZ8ev+rOXbW q5j2WSPqcuxbt1GFh+EjDU5D7kIf65nr1So1Uq6k920zEKizB7RzLyJxQ wkdIQgIQzYYxzvsQOYFIQtOPGErdSokPEJ8NUHzPpGEYoYSaYZfYZDTFj PO7Us/MZ4DUqKK0CY1AD89Avg7b+deQIXFcXZUd7p3P9aR4kJlSfb8hCa ZqclNFncFDx3s5itxT1ohnAbRtz6mrTk38xC+0CvoUHWTcyh7LzQ4cKou jORu9C0GtgwtC6xAhRRBTsBmZ3U8zR0OZFb/4q47k0m19EoHxrksNNUxL A==; X-IronPort-AV: E=McAfee;i="6500,9779,10461"; a="322720562" X-IronPort-AV: E=Sophos;i="5.93,293,1654585200"; d="scan'208";a="322720562" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Sep 2022 02:25:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,293,1654585200"; d="scan'208";a="565028251" Received: from wenbin-z390-aorus-ultra.sh.intel.com ([10.239.35.4]) by orsmga003.jf.intel.com with ESMTP; 06 Sep 2022 02:25:00 -0700 From: Wenbin Chen To: ffmpeg-devel@ffmpeg.org Date: Tue, 6 Sep 2022 17:22:57 +0800 Message-Id: <20220906092258.994742-6-wenbin.chen@intel.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220906092258.994742-1-wenbin.chen@intel.com> References: <20220906092258.994742-1-wenbin.chen@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 6/7] libavcodec/qsvenc: Add max/min qp reset support in qsvenc 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: oKhZUhXdbWRI Signed-off-by: Wenbin Chen --- doc/encoders.texi | 11 +++++++ libavcodec/qsvenc.c | 78 +++++++++++++++++++++++++++++++++++++++++++++ libavcodec/qsvenc.h | 9 ++++++ 3 files changed, 98 insertions(+) diff --git a/doc/encoders.texi b/doc/encoders.texi index da56159858..850e3c261c 100644 --- a/doc/encoders.texi +++ b/doc/encoders.texi @@ -3358,6 +3358,17 @@ Change this value to reset qsv codec's gop configuration. @item @var{int_ref_cycle_dist} Supported in h264_qsv and hevc_qsv. Change these value to reset qsv codec's Intra Refresh configuration. + +@item @var{qmax} +@item @var{qmin} +@item @var{max_qp_i} +@item @var{min_qp_i} +@item @var{max_qp_p} +@item @var{min_qp_p} +@item @var{max_qp_b} +@item @var{min_qp_b} +Supported in h264_qsv. +Change these value to reset qsv codec's max/min qp configuration. @end table @subsection H264 options diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index cd0549b249..215e5930ef 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -858,22 +858,30 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) q->extco2.MinQPI = avctx->qmin > 51 ? 51 : avctx->qmin; q->extco2.MinQPP = q->extco2.MinQPB = q->extco2.MinQPI; } + q->old_qmin = avctx->qmin; if (avctx->qmax >= 0) { q->extco2.MaxQPI = avctx->qmax > 51 ? 51 : avctx->qmax; q->extco2.MaxQPP = q->extco2.MaxQPB = q->extco2.MaxQPI; } + q->old_qmax = avctx->qmax; if (q->min_qp_i >= 0) q->extco2.MinQPI = q->min_qp_i > 51 ? 51 : q->min_qp_i; + q->old_min_qp_i = q->min_qp_i; if (q->max_qp_i >= 0) q->extco2.MaxQPI = q->max_qp_i > 51 ? 51 : q->max_qp_i; + q->old_max_qp_i = q->max_qp_i; if (q->min_qp_p >= 0) q->extco2.MinQPP = q->min_qp_p > 51 ? 51 : q->min_qp_p; + q->old_min_qp_p = q->min_qp_p; if (q->max_qp_p >= 0) q->extco2.MaxQPP = q->max_qp_p > 51 ? 51 : q->max_qp_p; + q->old_max_qp_p = q->max_qp_p; if (q->min_qp_b >= 0) q->extco2.MinQPB = q->min_qp_b > 51 ? 51 : q->min_qp_b; + q->old_min_qp_b = q->min_qp_b; if (q->max_qp_b >= 0) q->extco2.MaxQPB = q->max_qp_b > 51 ? 51 : q->max_qp_b; + q->old_max_qp_b = q->max_qp_b; if (q->mbbrc >= 0) q->extco2.MBBRC = q->mbbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF; @@ -1741,6 +1749,71 @@ static int update_rir(AVCodecContext *avctx, QSVEncContext *q) return updated; } +static int update_min_max_qp(AVCodecContext *avctx, QSVEncContext *q) +{ + int updated = 0; + + if (avctx->codec_id != AV_CODEC_ID_H264) + return 0; + + UPDATE_PARAM(q->old_qmax, avctx->qmin); + UPDATE_PARAM(q->old_qmax, avctx->qmin); + UPDATE_PARAM(q->old_min_qp_i, q->min_qp_i); + UPDATE_PARAM(q->old_max_qp_i, q->max_qp_i); + UPDATE_PARAM(q->old_min_qp_p, q->min_qp_p); + UPDATE_PARAM(q->old_max_qp_p, q->max_qp_p); + UPDATE_PARAM(q->old_min_qp_b, q->min_qp_b); + UPDATE_PARAM(q->old_max_qp_b, q->max_qp_b); + if (!updated) + return 0; + + if ((avctx->qmin >= 0 && avctx->qmax >= 0 && avctx->qmin > avctx->qmax) || + (q->max_qp_i >= 0 && q->min_qp_i >= 0 && q->min_qp_i > q->max_qp_i) || + (q->max_qp_p >= 0 && q->min_qp_p >= 0 && q->min_qp_p > q->max_qp_p) || + (q->max_qp_b >= 0 && q->min_qp_b >= 0 && q->min_qp_b > q->max_qp_b)) { + av_log(avctx, AV_LOG_ERROR, + "qmin and or qmax are set but invalid," + " please make sure min <= max\n"); + return AVERROR(EINVAL); + } + + q->extco2.MinQPI = 0; + q->extco2.MaxQPI = 0; + q->extco2.MinQPP = 0; + q->extco2.MaxQPP = 0; + q->extco2.MinQPB = 0; + q->extco2.MaxQPB = 0; + if (avctx->qmin >= 0) { + q->extco2.MinQPI = avctx->qmin > 51 ? 51 : avctx->qmin; + q->extco2.MinQPB = q->extco2.MinQPP = q->extco2.MinQPI; + } + if (avctx->qmax >= 0) { + q->extco2.MaxQPI = avctx->qmax > 51 ? 51 : avctx->qmax; + q->extco2.MaxQPB = q->extco2.MaxQPP = q->extco2.MaxQPI; + } + if (q->min_qp_i >= 0) + q->extco2.MinQPI = q->min_qp_i > 51 ? 51 : q->min_qp_i; + if (q->max_qp_i >= 0) + q->extco2.MaxQPI = q->max_qp_i > 51 ? 51 : q->max_qp_i; + if (q->min_qp_p >= 0) + q->extco2.MinQPP = q->min_qp_p > 51 ? 51 : q->min_qp_p; + if (q->max_qp_p >= 0) + q->extco2.MaxQPP = q->max_qp_p > 51 ? 51 : q->max_qp_p; + if (q->min_qp_b >= 0) + q->extco2.MinQPB = q->min_qp_b > 51 ? 51 : q->min_qp_b; + if (q->max_qp_b >= 0) + q->extco2.MaxQPB = q->max_qp_b > 51 ? 51 : q->max_qp_b; + + av_log(avctx, AV_LOG_VERBOSE, "Reset MinQPI: %d; MaxQPI: %d; " + "MinQPP: %d; MaxQPP: %d; " + "MinQPB: %d; MaxQPB: %d\n", + q->extco2.MinQPI, q->extco2.MaxQPI, + q->extco2.MinQPP, q->extco2.MaxQPP, + q->extco2.MinQPB, q->extco2.MaxQPB); + + return updated; +} + static int update_parameters(AVCodecContext *avctx, QSVEncContext *q, const AVFrame *frame) { @@ -1753,6 +1826,11 @@ static int update_parameters(AVCodecContext *avctx, QSVEncContext *q, needReset |= update_max_frame_size(avctx, q); needReset |= update_gop_size(avctx, q); needReset |= update_rir(avctx, q); + + ret = update_min_max_qp(avctx, q); + if (ret < 0) + return ret; + needReset |= ret; if (!needReset) return 0; diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h index 2eada6ab26..3ed20f757b 100644 --- a/libavcodec/qsvenc.h +++ b/libavcodec/qsvenc.h @@ -258,6 +258,15 @@ typedef struct QSVEncContext { int old_int_ref_cycle_size; int old_int_ref_qp_delta; int old_int_ref_cycle_dist; + // These are used for max/min qp reset; + int old_qmax; + int old_qmin; + int old_max_qp_i; + int old_min_qp_i; + int old_max_qp_p; + int old_min_qp_p; + int old_max_qp_b; + int old_min_qp_b; } QSVEncContext; int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q);