From patchwork Tue Jun 21 09:17:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Chen, Wenbin" X-Patchwork-Id: 36372 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp2104512pzb; Tue, 21 Jun 2022 02:22:37 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t+McOt5U2C4PPQ42Sp2PklhIixRCdGemXKX93tR8xaRd6LDNt259sah9bUrTtJVwsCcKGc X-Received: by 2002:aa7:c486:0:b0:435:5d50:ab39 with SMTP id m6-20020aa7c486000000b004355d50ab39mr26567318edq.104.1655803356948; Tue, 21 Jun 2022 02:22:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655803356; cv=none; d=google.com; s=arc-20160816; b=orYn0EGkuFNnEeKnNzpcXb0v6v2NvvunvRvLVRcwU9p5/KLrhl5wBoUI7F9Tybj+zR t/FBByPgOmyuWsz8XjMYwBY97yF4+/iLyTvvA6EXEyq38vVoRF2qVTNuOllOr86Jw4nz BUiCaCK64K8Gb17nSkz2CkTtImDZFJ1BE/s7rmqrnG4wIIvSsh2F+DFKVXYffvSe3ca1 IRlXwh8Z1MhXnTBiglq93Y8J5BsFNJtuzOYRqTct5dW9qI/oBMAsWJBVUlxRKyq5+sEq 1GdYJhaJASn8W/FJBDuLgRdJja7xm1MbAnGmSgETpcxqROfXfGIYR+KO5VWnUXn//xfL GoEw== 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:message-id:date:to:from :dkim-signature:delivered-to; bh=XaY8PCnY/dtnrhj2FeFmxbVW3akbYUwOHTPt1ZsuIW8=; b=h9FjPudeleuM6SoyWeVMIiscIUEFIA71RIS4ZrFY5uBo96E93VnHROygy0ipWOsAO6 oC5kur8Tpld56VXeVIRYSQWv4tqHHndPIMY4pCjKr0j2bi4c4cx7xck5PZrSYG6cFXsq DpLs2+Jrgv+YR181B5eKAn4nPSciKD3eWquAAtW25BNyjN7BHmubGS+697e7qhPMFnGd Ufk1/BW8BDaweLBFmSebi/IqW9pDt091CtRkQmTcyOsHUFdkxzUdfjSOdrwZDxT9swQI 204zfjv+6RkuF4fnDP0va5XWUePd7WnG2azcpezaJdvAWuvwS2EEKiDDWx6vwUCvAaur uMWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=DMDUIhUX; 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 w12-20020a05640234cc00b00435553d6389si16256985edc.271.2022.06.21.02.22.34; Tue, 21 Jun 2022 02:22:36 -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=DMDUIhUX; 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 7899568B63D; Tue, 21 Jun 2022 12:22:31 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B72B168B38C for ; Tue, 21 Jun 2022 12:22:23 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655803349; x=1687339349; h=from:to:subject:date:message-id:mime-version: content-transfer-encoding; bh=dm66qbe4gu8cB6c0MO/guIhDHZdg+yaVtpB/B7xw5lU=; b=DMDUIhUXRbM6UdlXyO0/jJZ++45nAqhDmsXAV6S/AcS2QzzeQ4hssAGt g5Y3xoZj3BIV/LSqSKryka+99cmxbglDA0OPGEbpMXqGLll+f5SkiCXMe Xx4GVo4isQ8zkTxlsE8vik4o1u+G7+wLApaSliIM21YDzWCL5hiCWnYz7 VvhqIkXCNgev/V7fwuIf6O5MyDsEVM6jC0gjvPqj1tbWbdy6c/JmVDrJr 8KIlp3SRaV3UV3+jLCXY8uElBUfOl14VlgtkVOUcBHySDQtBm9h9NQlQK 9UaJKUnlmB/Q1FWjXPEW6gqid6UyLErDgB5kSQ77/i1vVyADk0QxUNZ67 A==; X-IronPort-AV: E=McAfee;i="6400,9594,10384"; a="341758435" X-IronPort-AV: E=Sophos;i="5.92,209,1650956400"; d="scan'208";a="341758435" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Jun 2022 02:22:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,209,1650956400"; d="scan'208";a="676918351" Received: from wenbin-z390-aorus-ultra.sh.intel.com ([10.239.35.4]) by FMSMGA003.fm.intel.com with ESMTP; 21 Jun 2022 02:22:18 -0700 From: Wenbin Chen To: ffmpeg-devel@ffmpeg.org Date: Tue, 21 Jun 2022 17:17:50 +0800 Message-Id: <20220621091750.250120-1-wenbin.chen@intel.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] libavcodec/qsvenc: Enable fixed QP configure in qsv CQP runtime 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: MrBd4+q28ncN From: Yue Heng Enable dynamic QP configuration in runtime on qsv encoder. Through AVFrame->metadata, we can set key "qsv_config_qp" to change QP configuration when we encode video in CQP mode. Signed-off-by: Yue Heng Signed-off-by: Wenbin Chen --- doc/encoders.texi | 10 +++++ libavcodec/qsvenc.c | 89 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/doc/encoders.texi b/doc/encoders.texi index 9796a606fa..7b745734e4 100644 --- a/doc/encoders.texi +++ b/doc/encoders.texi @@ -3333,6 +3333,16 @@ Forcing I frames as IDR frames. For encoders set this flag to ON to reduce power consumption and GPU usage. @end table +@subsection Runtime Options +Following options can be used durning qsv encoding. + +@table @option +@item @var{qsv_config_qp} +Supported in h264_qsv and hevc_qsv. +This option can be set in per-frame metadata. QP parameter can be dynamically +changed when encoding in CQP mode. +@end table + @subsection H264 options These options are used by h264_qsv diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 2b3b06767d..692bdc1090 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -146,6 +146,14 @@ static const struct { { MFX_RATECONTROL_QVBR, "QVBR" }, }; +#define UPDATE_PARAM(a, b) \ +do { \ + if ((a) != (b)) { \ + a = b; \ + updated = 1; \ + } \ +} while (0) \ + static const char *print_ratecontrol(mfxU16 rc_mode) { int i; @@ -1520,6 +1528,83 @@ static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q) } } +static int update_qp(AVCodecContext *avctx, QSVEncContext *q, + const AVFrame *frame) +{ + int updated = 0, qp = 0, new_qp; + char *tail; + AVDictionaryEntry *entry = NULL; + + if (avctx->codec_id != AV_CODEC_ID_H264 && avctx->codec_id != AV_CODEC_ID_HEVC) + return 0; + + entry = av_dict_get(frame->metadata, "qsv_config_qp", NULL, 0); + if (entry && q->param.mfx.RateControlMethod == MFX_RATECONTROL_CQP) { + qp = strtol(entry->value, &tail, 10); + if (*tail) { + av_log(avctx, AV_LOG_WARNING, "Invalid qsv_config_qp string. Ignore this metadata\n"); + return 0; + } + if (qp < 0 || qp > 51) { + av_log(avctx, AV_LOG_WARNING, "Invalid qp, clip to 0 ~ 51\n"); + qp = av_clip(qp, 0, 51); + } + av_log(avctx, AV_LOG_DEBUG, "Configure qp: %d\n",qp); + UPDATE_PARAM(q->param.mfx.QPP, qp); + new_qp = av_clip(qp * fabs(avctx->i_quant_factor) + + avctx->i_quant_offset, 0, 51); + UPDATE_PARAM(q->param.mfx.QPI, new_qp); + new_qp = av_clip(qp * fabs(avctx->b_quant_factor) + + avctx->b_quant_offset, 0, 51); + UPDATE_PARAM(q->param.mfx.QPB, new_qp); + av_log(avctx, AV_LOG_DEBUG, + "using fixed qp = %d/%d/%d for idr/p/b frames\n", + q->param.mfx.QPI, q->param.mfx.QPP, q->param.mfx.QPB); + } + return updated; +} + +static int update_parameters(AVCodecContext *avctx, QSVEncContext *q, + const AVFrame *frame) +{ + int needReset = 0, ret = 0; + + if (!frame) + return 0; + + needReset = update_qp(avctx, q, frame); + if (!needReset) + return 0; + + if (avctx->hwaccel_context) { + AVQSVContext *qsv = avctx->hwaccel_context; + int i, j; + q->param.ExtParam = q->extparam; + for (i = 0; i < qsv->nb_ext_buffers; i++) + q->param.ExtParam[i] = qsv->ext_buffers[i]; + q->param.NumExtParam = qsv->nb_ext_buffers; + + for (i = 0; i < q->nb_extparam_internal; i++) { + for (j = 0; j < qsv->nb_ext_buffers; j++) { + if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId) + break; + } + if (j < qsv->nb_ext_buffers) + continue; + q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i]; + } + } else { + q->param.ExtParam = q->extparam_internal; + q->param.NumExtParam = q->nb_extparam_internal; + } + av_log(avctx, AV_LOG_DEBUG, "Parameter change, call msdk reset.\n"); + ret = MFXVideoENCODE_Reset(q->session, &q->param); + if (ret < 0) + return ff_qsv_print_error(avctx, ret, "Error during resetting"); + + return 0; +} + static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, const AVFrame *frame) { @@ -1630,6 +1715,10 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, { int ret; + ret = update_parameters(avctx, q, frame); + if (ret < 0) + return ret; + ret = encode_frame(avctx, q, frame); if (ret < 0) return ret;