From patchwork Fri Mar 4 08:37:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Fei W" X-Patchwork-Id: 34599 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:d078:0:0:0:0 with SMTP id x24csp1387310nkx; Fri, 4 Mar 2022 00:43:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJy/8OuaJBiBfuDQ7k9GB2yox7rDcMVzt7tQJTHT75jrQ3tV/P45Khao0BorbhbDI5HEM9eM X-Received: by 2002:a05:6402:3487:b0:40f:fa53:956c with SMTP id v7-20020a056402348700b0040ffa53956cmr37568857edc.22.1646383385562; Fri, 04 Mar 2022 00:43:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646383385; cv=none; d=google.com; s=arc-20160816; b=0xu8iMeypNIionduj0mR5SuAUbl2q4wau20H/PGozsMj0TaenATm7LjKJMpdSeGJwH fD+oRA3P+aMiznLocK3fO/UBir5+rtMeMuR3tQU24q/t1nrByD4TaU1iyjM71SU4BvsP +dRFjFwh41OnC5QSMMb8RjJd3K6e/5w8bD90x9IVU8Xsbk3RxNRUwDc1rms8V4jOXyiS bJUyi6XZgZXVBHd8VQZfFlxq2x9fhLJPZv6ImR7VRAj03zSBQajn9mAI8YQVDsUUhteC LtQ85ERLFRm4jAv+YiY6bqgCD88+EQaTdT5t5I4G0qwP79nEuHkt4nWjgNhyXd/Pr57+ QEqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc: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=zGl5fyVf3mLlHYEZ/f+4cCRrJutuENBDriFqpO79cUE=; b=Kih3xOIwFAGYkounodvwHriyNYI/GQlmnbcD9oF6p5q8157MHizXf6QbgCzuvOeYxL J2Xrg8hRylpT2NbxEFmVUT2/A/1MahnbWguCZm5/0oNrY1mlWnyRAvAqPzjGyNCGPoe6 URVQqYUHEviPNiI0YOoOj82pDa0aIivmz4gwOSZTNzbnmMuo3HQ2mK/+6ORWk5axfyMJ nt+HchI/mFIs4iJduLUTrcBFdm/t0V1yvRPRWWA2rV4KHcPGmWeRHNOPmDAIzQBXO06U AnjXeAV+tTpstASgvR+p+07kSB3kWRDPfVf3iJhDTbCC/+NtW1uJZdCHZWO1gpwNppSC W4FA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=CqOElcCx; 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 a13-20020a05640213cd00b0041600c790d0si481180edx.492.2022.03.04.00.43.05; Fri, 04 Mar 2022 00:43:05 -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=@intel.com header.s=Intel header.b=CqOElcCx; 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 9237C68B107; Fri, 4 Mar 2022 10:42:54 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2B79C68B0E1 for ; Fri, 4 Mar 2022 10:42:47 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646383373; x=1677919373; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+SnqDBXJgW9AftLZTWD9cjX8GIsVy+AkZOXOHiSnKCY=; b=CqOElcCx1TOUQrDWh3nH8JUvAwEpRwPcx/Ey0NG94I9aPCvOHopExt2M 7z4mBgc/fIgMNtM/6zbY5Gja5/+ssNFS0tBW2l2IKvuHL41sbSkVu7hdO hbl7bjBrwQOHMiN2EZsr6uPPWzqLwvHUqOaGcuIoJZPTXkcsp4YawmmVF DFY+yaZFKT21/nliFQchDpHgWN1mOEKHVpUuYJwEyWnxEv8lOaSd7NxXy +LhYaH/kdJJPvEHrREb2vAfOlCZzsvOZFw0AoaKIoowfh2z2P73WWrMS9 RhF4wqC6VYumzWDklPvq0zz0hiSymCjYmRUC5EodhU85mlrVMgf958kqJ g==; X-IronPort-AV: E=McAfee;i="6200,9189,10275"; a="241353040" X-IronPort-AV: E=Sophos;i="5.90,154,1643702400"; d="scan'208";a="241353040" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 00:42:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,154,1643702400"; d="scan'208";a="536202399" Received: from t.sh.intel.com ([10.239.159.147]) by orsmga007.jf.intel.com with ESMTP; 04 Mar 2022 00:42:44 -0800 From: Fei Wang To: ffmpeg-devel@ffmpeg.org Date: Fri, 4 Mar 2022 16:37:09 +0800 Message-Id: <20220304083711.3383019-2-fei.w.wang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220304083711.3383019-1-fei.w.wang@intel.com> References: <20220304083711.3383019-1-fei.w.wang@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 2/4] vaapi_encode: Move block size calculation after entrypoint selection 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 Cc: Mark Thompson , Fei Wang Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Je+/TlIK2Sg9 From: Mark Thompson The block size can be dependent on the profile and entrypoint selected. It defaults to 16x16, with codecs able to override this choice with their own function. Signed-off-by: Fei Wang --- libavcodec/vaapi_encode.c | 16 ++++++++++++++++ libavcodec/vaapi_encode.h | 7 +++++++ libavcodec/vaapi_encode_h265.c | 32 ++++++++++++++++++++++++++------ libavcodec/vaapi_encode_mjpeg.c | 18 +++++++++++++++--- libavcodec/vaapi_encode_mpeg2.c | 3 --- libavcodec/vaapi_encode_vp8.c | 3 --- libavcodec/vaapi_encode_vp9.c | 16 ++++++++++++---- 7 files changed, 76 insertions(+), 19 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index 1818da8731..8d1a08d940 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -2058,6 +2058,8 @@ static av_cold int vaapi_encode_init_slice_structure(AVCodecContext *avctx) return 0; } + av_assert0(ctx->slice_block_height > 0 && ctx->slice_block_width > 0); + ctx->slice_block_rows = (avctx->height + ctx->slice_block_height - 1) / ctx->slice_block_height; ctx->slice_block_cols = (avctx->width + ctx->slice_block_width - 1) / @@ -2447,6 +2449,20 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx) if (err < 0) goto fail; + if (ctx->codec->get_encoder_caps) { + err = ctx->codec->get_encoder_caps(avctx); + if (err < 0) + goto fail; + } else { + // Assume 16x16 blocks. + ctx->surface_width = FFALIGN(avctx->width, 16); + ctx->surface_height = FFALIGN(avctx->height, 16); + if (ctx->codec->flags & FLAG_SLICE_CONTROL) { + ctx->slice_block_width = 16; + ctx->slice_block_height = 16; + } + } + err = vaapi_encode_init_rate_control(avctx); if (err < 0) goto fail; diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index 61c5615eb8..f0021acccf 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -376,6 +376,13 @@ typedef struct VAAPIEncodeType { // factor depending on RC mode. int default_quality; + // Determine encode parameters like block sizes for surface alignment + // and slices. This may need to query the profile and entrypoint, + // which will be available when this function is called. If not set, + // assume that all blocks are 16x16 and that surfaces should be + // aligned to match this. + int (*get_encoder_caps)(AVCodecContext *avctx); + // Perform any extra codec-specific configuration after the // codec context is initialised (set up the private data and // add any necessary global parameters). diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c index ea45893508..706ec5d849 100644 --- a/libavcodec/vaapi_encode_h265.c +++ b/libavcodec/vaapi_encode_h265.c @@ -55,6 +55,10 @@ typedef struct VAAPIEncodeH265Picture { typedef struct VAAPIEncodeH265Context { VAAPIEncodeContext common; + // Encoder features. + uint32_t ctu_size; + uint32_t min_cb_size; + // User options. int qp; int aud; @@ -1091,6 +1095,27 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, return 0; } +static av_cold int vaapi_encode_h265_get_encoder_caps(AVCodecContext *avctx) +{ + VAAPIEncodeContext *ctx = avctx->priv_data; + VAAPIEncodeH265Context *priv = avctx->priv_data; + + if (!priv->ctu_size) { + priv->ctu_size = 32; + priv->min_cb_size = 16; + } + av_log(avctx, AV_LOG_VERBOSE, "Using CTU size %dx%d, " + "min CB size %dx%d.\n", priv->ctu_size, priv->ctu_size, + priv->min_cb_size, priv->min_cb_size); + + ctx->surface_width = FFALIGN(avctx->width, priv->min_cb_size); + ctx->surface_height = FFALIGN(avctx->height, priv->min_cb_size); + + ctx->slice_block_width = ctx->slice_block_height = priv->ctu_size; + + return 0; +} + static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx) { VAAPIEncodeContext *ctx = avctx->priv_data; @@ -1160,6 +1185,7 @@ static const VAAPIEncodeType vaapi_encode_type_h265 = { .default_quality = 25, + .get_encoder_caps = &vaapi_encode_h265_get_encoder_caps, .configure = &vaapi_encode_h265_configure, .picture_priv_data_size = sizeof(VAAPIEncodeH265Picture), @@ -1205,12 +1231,6 @@ static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx) VA_ENC_PACKED_HEADER_SLICE | // Slice headers. VA_ENC_PACKED_HEADER_MISC; // SEI - ctx->surface_width = FFALIGN(avctx->width, 16); - ctx->surface_height = FFALIGN(avctx->height, 16); - - // CTU size is currently hard-coded to 32. - ctx->slice_block_width = ctx->slice_block_height = 32; - if (priv->qp > 0) ctx->explicit_qp = priv->qp; diff --git a/libavcodec/vaapi_encode_mjpeg.c b/libavcodec/vaapi_encode_mjpeg.c index 6206b23e5f..87a03f181b 100644 --- a/libavcodec/vaapi_encode_mjpeg.c +++ b/libavcodec/vaapi_encode_mjpeg.c @@ -434,6 +434,20 @@ static int vaapi_encode_mjpeg_init_slice_params(AVCodecContext *avctx, return 0; } +static av_cold int vaapi_encode_mjpeg_get_encoder_caps(AVCodecContext *avctx) +{ + VAAPIEncodeContext *ctx = avctx->priv_data; + const AVPixFmtDescriptor *desc; + + desc = av_pix_fmt_desc_get(ctx->input_frames->sw_format); + av_assert0(desc); + + ctx->surface_width = FFALIGN(avctx->width, 8 << desc->log2_chroma_w); + ctx->surface_height = FFALIGN(avctx->height, 8 << desc->log2_chroma_h); + + return 0; +} + static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx) { VAAPIEncodeContext *ctx = avctx->priv_data; @@ -483,6 +497,7 @@ static const VAAPIEncodeType vaapi_encode_type_mjpeg = { .flags = FLAG_CONSTANT_QUALITY_ONLY | FLAG_INTRA_ONLY, + .get_encoder_caps = &vaapi_encode_mjpeg_get_encoder_caps, .configure = &vaapi_encode_mjpeg_configure, .default_quality = 80, @@ -509,9 +524,6 @@ static av_cold int vaapi_encode_mjpeg_init(AVCodecContext *avctx) ctx->desired_packed_headers = VA_ENC_PACKED_HEADER_RAW_DATA; - ctx->surface_width = FFALIGN(avctx->width, 8); - ctx->surface_height = FFALIGN(avctx->height, 8); - return ff_vaapi_encode_init(avctx); } diff --git a/libavcodec/vaapi_encode_mpeg2.c b/libavcodec/vaapi_encode_mpeg2.c index d084d8c4b9..f8809b08ab 100644 --- a/libavcodec/vaapi_encode_mpeg2.c +++ b/libavcodec/vaapi_encode_mpeg2.c @@ -623,9 +623,6 @@ static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx) ctx->desired_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_PICTURE; - ctx->surface_width = FFALIGN(avctx->width, 16); - ctx->surface_height = FFALIGN(avctx->height, 16); - return ff_vaapi_encode_init(avctx); } diff --git a/libavcodec/vaapi_encode_vp8.c b/libavcodec/vaapi_encode_vp8.c index 7d4578f674..ab3c84a618 100644 --- a/libavcodec/vaapi_encode_vp8.c +++ b/libavcodec/vaapi_encode_vp8.c @@ -210,9 +210,6 @@ static av_cold int vaapi_encode_vp8_init(AVCodecContext *avctx) // adding them anyway. ctx->desired_packed_headers = 0; - ctx->surface_width = FFALIGN(avctx->width, 16); - ctx->surface_height = FFALIGN(avctx->height, 16); - return ff_vaapi_encode_init(avctx); } diff --git a/libavcodec/vaapi_encode_vp9.c b/libavcodec/vaapi_encode_vp9.c index be706e3bd6..837adaa825 100644 --- a/libavcodec/vaapi_encode_vp9.c +++ b/libavcodec/vaapi_encode_vp9.c @@ -184,6 +184,17 @@ static int vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx, return 0; } +static av_cold int vaapi_encode_vp9_get_encoder_caps(AVCodecContext *avctx) +{ + VAAPIEncodeContext *ctx = avctx->priv_data; + + // Surfaces must be aligned to 64x64 superblock boundaries. + ctx->surface_width = FFALIGN(avctx->width, 64); + ctx->surface_height = FFALIGN(avctx->height, 64); + + return 0; +} + static av_cold int vaapi_encode_vp9_configure(AVCodecContext *avctx) { VAAPIEncodeContext *ctx = avctx->priv_data; @@ -231,6 +242,7 @@ static const VAAPIEncodeType vaapi_encode_type_vp9 = { .picture_priv_data_size = sizeof(VAAPIEncodeVP9Picture), + .get_encoder_caps = &vaapi_encode_vp9_get_encoder_caps, .configure = &vaapi_encode_vp9_configure, .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP9), @@ -251,10 +263,6 @@ static av_cold int vaapi_encode_vp9_init(AVCodecContext *avctx) // can write its own headers and there is no metadata to include. ctx->desired_packed_headers = 0; - // Surfaces must be aligned to superblock boundaries. - ctx->surface_width = FFALIGN(avctx->width, 64); - ctx->surface_height = FFALIGN(avctx->height, 64); - return ff_vaapi_encode_init(avctx); }