From patchwork Thu Jun 2 05:56:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Chen, Wenbin" X-Patchwork-Id: 36055 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6914:b0:82:6b11:2509 with SMTP id q20csp3847979pzj; Wed, 1 Jun 2022 22:57:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwPnW2so4AoRQH7v6fvSOh9zRzBF18sGH+m38NYRqWUZEZ86wFojpePf1eUfUyqq3dnkmli X-Received: by 2002:a17:906:6bd3:b0:6fe:8c20:f218 with SMTP id t19-20020a1709066bd300b006fe8c20f218mr2697612ejs.32.1654149429049; Wed, 01 Jun 2022 22:57:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654149429; cv=none; d=google.com; s=arc-20160816; b=o12Mb3nYEgvKV+XxwLxxpCZYf7Tet6rUGn3K/L77MYhm+6cPjmDtL1rUqjNIMRxmSo TDyfCmw+jmOaDc4kXLZyXc9SYmPfqe5BbXPzmBVOhXsUKil49l59XGD3SnFDSqSVzQPK m8cTFaEyTP4d2ZV9eD2l/F5p/fpxkFdDzvbV9v7KPb0gS8JZUWh5jow+RIzODxLMCgy9 dy7c+v2ODCvv4yy8N0CuX9Qy7bcLtJH+77TKRLgWdmOfIJuOKcNRnn2OyMkFqh1raBpT G4obXNzn9kT7koAeQpD3/1HPMtGeJ4GyteyUnBAACePRDNEUFBG3tKHFdj1g/fJOMdqU asiA== 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=84KjcvorMf/xRAxgH4rf+mhegdAmrPKUsqScAB7Q6tQ=; b=CzeenWt9VBT1WMtmOk0Am5AxUPaJ/cG1WL5bp70TNKrNP53+W+jAi8dpJVqv2dU6tm TEnOn/w6nxP8znaOD6YNdhdUCh4KdftyI6pEP7tlfYlvChACZspbNBm7myAi4UuDcgOh C+ArrDqPRtVvgeyXKh4q1r2tjJ0rZDFcVTw3uQ1nhYS5jbTvVssWYh8/xrbHaf16ZLnO 4SATlJvTmIcBHzZv6XmohbkgZ3JHqh3lEk8IMnYlE9vpTh89IGDReWJRSLqGlyX/wpNc Y4AHhyHdguGJXe47lFVp406aiiuzQUXWh+lMRggWwUJ9q87PDG4NIsARFk67UQTCVQ7h nzoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=dVyjaY9u; 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 ht16-20020a170907609000b007091e2fbd0fsi2778459ejc.100.2022.06.01.22.57.08; Wed, 01 Jun 2022 22:57:09 -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=dVyjaY9u; 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 BBF5E68B803; Thu, 2 Jun 2022 08:57:04 +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 B1AA468B7ED for ; Thu, 2 Jun 2022 08:56:56 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1654149422; x=1685685422; h=from:to:subject:date:message-id:mime-version: content-transfer-encoding; bh=/UYU8n+wJdugPYYAhnlSe0Z6gXmoUHa8tjJ6QYT7DOc=; b=dVyjaY9uYucDca6o1jtjSOWHK86hLIWMhOcKVHrkiT2njYKrxx+JzR9L OZ5RQbG86f8O82L9it1cPNOIeficyW4Rc7kegP/2qnSp7TFXSqpMbr3+8 OKqQDPc8sJdeQ8gyQFOHcJc2eU4vJDRQ10TSNBjlq0eNXDOOqLnmeIlG3 Gk4b979EE5OkmWPj4iMrneeFu59N0ZD4EEW5rZMdV5/EVclxEbiD3PG6F Xdd7pIpqTMG5GoDXc6kwI75da7VViGuGb4Cw3qrgbIdXWwf4X+o0HVHhx uSI6vWP1sfcalo9aESbcSKMsw93TMh19ufTCpCmCSs8sOnqRRmnzxOa7Q Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10365"; a="336489852" X-IronPort-AV: E=Sophos;i="5.91,270,1647327600"; d="scan'208";a="336489852" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jun 2022 22:56:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,270,1647327600"; d="scan'208";a="606664672" Received: from wenbin-z390-aorus-ultra.sh.intel.com ([10.239.35.4]) by orsmga008.jf.intel.com with ESMTP; 01 Jun 2022 22:56:52 -0700 From: Wenbin Chen To: ffmpeg-devel@ffmpeg.org Date: Thu, 2 Jun 2022 13:56:36 +0800 Message-Id: <20220602055636.3585381-1-wenbin.chen@intel.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] libavcodec/qsvenc: add ROI support to qsv encoder 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: KX5Zs35xvOBb From: Wenbin Chen Use the mfxEncoderCtrl parameter to enable ROI. Get side data "AVRegionOfInterest" and use it to configure "mfxExtEncoderROI" which is the MediaSDK's ROI configuration. Signed-off-by: Wenbin Chen --- libavcodec/qsv_internal.h | 4 ++ libavcodec/qsvenc.c | 86 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/libavcodec/qsv_internal.h b/libavcodec/qsv_internal.h index e2aecdcbd6..8131acdae9 100644 --- a/libavcodec/qsv_internal.h +++ b/libavcodec/qsv_internal.h @@ -51,6 +51,9 @@ #define ASYNC_DEPTH_DEFAULT 4 // internal parallelism #define QSV_MAX_ENC_PAYLOAD 2 // # of mfxEncodeCtrl payloads supported +#define QSV_MAX_ENC_EXTPARAM 2 + +#define QSV_MAX_ROI_NUM 256 #define QSV_MAX_FRAME_EXT_PARAMS 4 @@ -83,6 +86,7 @@ typedef struct QSVFrame { int num_ext_params; mfxPayload *payloads[QSV_MAX_ENC_PAYLOAD]; ///< used for enc_ctrl.Payload + mfxExtBuffer *extparam[QSV_MAX_ENC_EXTPARAM]; ///< used for enc_ctrl.ExtParam int queued; int used; diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 2b3b06767d..0e88a10489 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -1373,15 +1373,29 @@ static void free_encoder_ctrl_payloads(mfxEncodeCtrl* enc_ctrl) } } +static void free_encoder_ctrl_extparam(mfxEncodeCtrl* enc_ctrl) +{ + if (enc_ctrl) { + int i; + for (i = 0; i < enc_ctrl->NumExtParam && i < QSV_MAX_ENC_EXTPARAM; i++) { + if (enc_ctrl->ExtParam[i]) + av_freep(&(enc_ctrl->ExtParam[i])); + } + enc_ctrl->NumExtParam = 0; + } +} + static void clear_unused_frames(QSVEncContext *q) { QSVFrame *cur = q->work_frames; while (cur) { if (cur->used && !cur->surface.Data.Locked) { free_encoder_ctrl_payloads(&cur->enc_ctrl); + free_encoder_ctrl_extparam(&cur->enc_ctrl); //do not reuse enc_ctrl from previous frame memset(&cur->enc_ctrl, 0, sizeof(cur->enc_ctrl)); cur->enc_ctrl.Payload = cur->payloads; + cur->enc_ctrl.ExtParam = cur->extparam; if (cur->frame->format == AV_PIX_FMT_QSV) { av_frame_unref(cur->frame); } @@ -1419,6 +1433,7 @@ static int get_free_frame(QSVEncContext *q, QSVFrame **f) return AVERROR(ENOMEM); } frame->enc_ctrl.Payload = frame->payloads; + frame->enc_ctrl.ExtParam = frame->extparam; *last = frame; *f = frame; @@ -1520,6 +1535,68 @@ static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q) } } +static int set_roi_encode_ctrl(AVCodecContext *avctx, const AVFrame *frame, + mfxEncodeCtrl *enc_ctrl) +{ + AVFrameSideData *sd = NULL; + int mb_size; + + if (avctx->codec_id == AV_CODEC_ID_H264) { + mb_size = 16; + } else if (avctx->codec_id == AV_CODEC_ID_H265) { + mb_size = 32; + } else { + return 0; + } + + if (frame) + sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); + + if (sd) { + mfxExtEncoderROI *enc_roi = NULL; + AVRegionOfInterest *roi; + uint32_t roi_size; + int nb_roi, i; + + roi = (AVRegionOfInterest *)sd->data; + roi_size = roi->self_size; + if (!roi_size || sd->size % roi_size) { + av_log(avctx, AV_LOG_ERROR, "Invalid ROI Data.\n"); + return AVERROR(EINVAL); + } + nb_roi = sd->size / roi_size; + if (nb_roi > QSV_MAX_ROI_NUM) { + av_log(avctx, AV_LOG_WARNING, "More ROIs set than " + "supported by driver (%d > %d).\n", + nb_roi, QSV_MAX_ROI_NUM); + nb_roi = QSV_MAX_ROI_NUM; + } + + enc_roi = av_mallocz(sizeof(*enc_roi)); + if (!enc_roi) + return AVERROR(ENOMEM); + enc_roi->Header.BufferId = MFX_EXTBUFF_ENCODER_ROI; + enc_roi->Header.BufferSz = sizeof(*enc_roi); + enc_roi->NumROI = nb_roi; + enc_roi->ROIMode = MFX_ROI_MODE_QP_DELTA; + for (i = 0; i < nb_roi; i++) { + roi = (AVRegionOfInterest *)(sd->data + roi_size * i); + enc_roi->ROI[i].Top = FFALIGN(roi->top, mb_size); + enc_roi->ROI[i].Bottom = FFALIGN(roi->bottom, mb_size); + enc_roi->ROI[i].Left = FFALIGN(roi->left, mb_size); + enc_roi->ROI[i].Right = FFALIGN(roi->right, mb_size); + enc_roi->ROI[i].DeltaQP = + roi->qoffset.num * 51 / roi->qoffset.den; + av_log(avctx, AV_LOG_DEBUG, "ROI: (%d,%d)-(%d,%d) -> %+d.\n", + roi->top, roi->left, roi->bottom, roi->right, + enc_roi->ROI[i].DeltaQP); + } + enc_ctrl->ExtParam[enc_ctrl->NumExtParam] = (mfxExtBuffer *)enc_roi; + enc_ctrl->NumExtParam++; + } + return 0; +} + static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, const AVFrame *frame) { @@ -1582,6 +1659,14 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, q->set_encode_ctrl_cb(avctx, frame, &qsv_frame->enc_ctrl); } + if ((avctx->codec_id == AV_CODEC_ID_H264 || + avctx->codec_id == AV_CODEC_ID_H265) && + enc_ctrl && QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 8)) { + ret = set_roi_encode_ctrl(avctx, frame, enc_ctrl); + if (ret < 0) + goto free; + } + pkt.sync = av_mallocz(sizeof(*pkt.sync)); if (!pkt.sync) goto nomem; @@ -1704,6 +1789,7 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q) while (cur) { q->work_frames = cur->next; av_frame_free(&cur->frame); + free_encoder_ctrl_extparam(&cur->enc_ctrl); free_encoder_ctrl_payloads(&cur->enc_ctrl); av_freep(&cur); cur = q->work_frames;