From patchwork Thu Mar 17 06:41:49 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: 34804 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:3486:0:0:0:0 with SMTP id ek6csp645213nkb; Wed, 16 Mar 2022 23:47:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwWqjd6eTvP6Oqnfbrpev6qZYsIwz5jTBxtUu0PWA3rq0cQY4uGiDT00a2Fc1j6VT/00Iai X-Received: by 2002:a05:6402:1746:b0:418:d816:14e0 with SMTP id v6-20020a056402174600b00418d81614e0mr2803085edx.340.1647499678414; Wed, 16 Mar 2022 23:47:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647499678; cv=none; d=google.com; s=arc-20160816; b=Jt5IRLEYf2pVy9rwk/0VMQtU+8hPDWEbBfniXclsTGUzX32mEaag3nFBQcMKBgcLgS g53UKzlufx5xbEIRhqdSCuj8/YHll1pD8dG+6cbjYuazvLvp/YdizwsxN5/HN/gZDTd9 qHXpmO6H5gutW+DaY6qXupinVSmvcjL/G7JmtpbZWdPgj6NHARxhCmpBkhFCJcr3/qDk 30LtUl5JhXl2rgeYDfEGq2Am7MrSlPCKRtMVhrZDhWHbUOWq3wZ5Jse7qTJzX+WWskJi hfBuwFJFOtWQh9hwwT0gx4+WHnoYG5dBf8EQpV3rzJG45LilQknxRMo7UTErlbSkUYKL TmWA== 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:message-id:date:to:from :dkim-signature:delivered-to; bh=DwWD768riP0e1gMNHRAOeVdKey2she2FeripKcCIUCA=; b=SfqIfSnzklDKqE2DFYt3iZwx7h/rJvpAGurHWWCOHOxBHqIvxBIog2W9vDwa3UTQyX Z8q//7s7GIb6fzQUfuoEjC1TYDff7ghGEpTrIjRB9W/eTo/yRuiHs+mhx3g1Cc66TEMS DxP8baZGG5e2ittPejh0QPL4MFK06Jwm1HZSAW4+aD6cuigNNEqzWw/saRE5EupF76d4 uwiB0jPZbmRGR3DzS7oEljH7RZvH63U8EZ1H0tOL7CuTwDXa8fl2U8A8+Jp9n/l3zq++ u246IdNfXWMU22SbUe3tLotSgw72LX0s8Qx/vHHx5J4YfTPfp52vr7khUb+GSpkUXS1X wdZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=bwd67ytr; 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 t18-20020a508d52000000b00418c2b5bd5bsi952479edt.61.2022.03.16.23.47.58; Wed, 16 Mar 2022 23:47:58 -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=bwd67ytr; 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 636AD68B03D; Thu, 17 Mar 2022 08:47:55 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4F732689F8F for ; Thu, 17 Mar 2022 08:47:48 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647499673; x=1679035673; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=NkM2JLVweZvn/RvRntVEXCt7Ko+EkYYsi4DOnGmFwRc=; b=bwd67ytrxZBQop/2XglkSpzmxHecZyznp57tkxFcoSBfg4eJbV0TLYZs vIbQXxtaw+cfUb6C7hkwTpW8PayJA2ythxKcSxjPMPO34s/DhaJtqiqqA T9LWC1rJu/vmseOG779fRMGSnweq8mC0DnnnQJdqDPbGOxqE1qVoUNlAH xdv5pQHQL1eE+iHUORuObBXBIHdVYSqr1aGetC0OMW2QKh3x2i2KmvYJA rq7lZtuuxm+/0wACCbz/HjbHF5aVI6HTHTyz3OPNjjFlRwhw4phy0cVk7 o5OUUQG7gJx+v8w8zXg6ZxKhXF8gKPhXETYFTo0TAleBXt9as+4HTuSGi Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10288"; a="238960169" X-IronPort-AV: E=Sophos;i="5.90,188,1643702400"; d="scan'208";a="238960169" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2022 23:47:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,188,1643702400"; d="scan'208";a="714919915" Received: from t.sh.intel.com ([10.239.159.147]) by orsmga005.jf.intel.com with ESMTP; 16 Mar 2022 23:47:45 -0700 From: Fei Wang To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Mar 2022 14:41:49 +0800 Message-Id: <20220317064152.430212-1-fei.w.wang@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 1/4] lavc/vaapi_encode_h265: Add GPB frame support for hevc_vaapi 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: Fei Wang , Linjie Fu Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Qo+iE1gpB4C1 From: Linjie Fu Use GPB frames to replace regular P/B frames if backend driver does not support it. - GPB: Generalized P and B picture. Regular P/B frames replaced by B frames with previous-predict only, L0 == L1. Normal B frames still have 2 different ref_lists and allow bi-prediction Signed-off-by: Linjie Fu Signed-off-by: Fei Wang --- update: 1. fall back logic that implemented in v3. 2. refine debug message. libavcodec/vaapi_encode.c | 67 +++++++++++++++++++++++++++++++--- libavcodec/vaapi_encode.h | 1 + libavcodec/vaapi_encode_h265.c | 15 ++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index 3bf379b1a0..081eb475a3 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -1827,6 +1827,7 @@ static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx) VAStatus vas; VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames }; uint32_t ref_l0, ref_l1; + int prediction_pre_only; vas = vaGetConfigAttributes(ctx->hwctx->display, ctx->va_profile, @@ -1845,6 +1846,51 @@ static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx) ref_l1 = attr.value >> 16 & 0xffff; } + ctx->p_to_gpb = 0; + prediction_pre_only = 0; + +#if VA_CHECK_VERSION(1, 9, 0) + if (!(ctx->codec->flags & FLAG_INTRA_ONLY || + avctx->gop_size <= 1)) { + attr = (VAConfigAttrib) { VAConfigAttribPredictionDirection }; + vas = vaGetConfigAttributes(ctx->hwctx->display, + ctx->va_profile, + ctx->va_entrypoint, + &attr, 1); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_WARNING, "Failed to query prediction direction " + "attribute: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR_EXTERNAL; + } else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) { + av_log(avctx, AV_LOG_VERBOSE, "Driver does not report any additional " + "prediction constraints.\n"); + } else { + if (((ref_l0 > 0 || ref_l1 > 0) && !(attr.value & VA_PREDICTION_DIRECTION_PREVIOUS)) || + ((ref_l1 == 0) && (attr.value & (VA_PREDICTION_DIRECTION_FUTURE | VA_PREDICTION_DIRECTION_BI_NOT_EMPTY)))) { + av_log(avctx, AV_LOG_ERROR, "Driver report incorrect prediction " + "direction attribute.\n"); + return AVERROR_EXTERNAL; + } + + if (!(attr.value & VA_PREDICTION_DIRECTION_FUTURE)) { + if (ref_l0 > 0 && ref_l1 > 0) { + prediction_pre_only = 1; + av_log(avctx, AV_LOG_VERBOSE, "Driver only support same reference " + "lists for B-frames.\n"); + } + } + + if (attr.value & VA_PREDICTION_DIRECTION_BI_NOT_EMPTY) { + if (ref_l0 > 0 && ref_l1 > 0) { + ctx->p_to_gpb = 1; + av_log(avctx, AV_LOG_VERBOSE, "Driver does not support P-frames, " + "replacing them with B-frames.\n"); + } + } + } + } +#endif + if (ctx->codec->flags & FLAG_INTRA_ONLY || avctx->gop_size <= 1) { av_log(avctx, AV_LOG_VERBOSE, "Using intra frames only.\n"); @@ -1854,15 +1900,26 @@ static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx) "reference frames.\n"); return AVERROR(EINVAL); } else if (!(ctx->codec->flags & FLAG_B_PICTURES) || - ref_l1 < 1 || avctx->max_b_frames < 1) { - av_log(avctx, AV_LOG_VERBOSE, "Using intra and P-frames " - "(supported references: %d / %d).\n", ref_l0, ref_l1); + ref_l1 < 1 || avctx->max_b_frames < 1 || + prediction_pre_only) { + if (ctx->p_to_gpb) + av_log(avctx, AV_LOG_VERBOSE, "Using intra and B-frames " + "(supported references: %d / %d).\n", + ref_l0, ref_l1); + else + av_log(avctx, AV_LOG_VERBOSE, "Using intra and P-frames " + "(supported references: %d / %d).\n", ref_l0, ref_l1); ctx->gop_size = avctx->gop_size; ctx->p_per_i = INT_MAX; ctx->b_per_p = 0; } else { - av_log(avctx, AV_LOG_VERBOSE, "Using intra, P- and B-frames " - "(supported references: %d / %d).\n", ref_l0, ref_l1); + if (ctx->p_to_gpb) + av_log(avctx, AV_LOG_VERBOSE, "Using intra and B-frames " + "(supported references: %d / %d).\n", + ref_l0, ref_l1); + else + av_log(avctx, AV_LOG_VERBOSE, "Using intra, P- and B-frames " + "(supported references: %d / %d).\n", ref_l0, ref_l1); ctx->gop_size = avctx->gop_size; ctx->p_per_i = INT_MAX; ctx->b_per_p = avctx->max_b_frames; diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index b41604a883..61c5615eb8 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -328,6 +328,7 @@ typedef struct VAAPIEncodeContext { int idr_counter; int gop_counter; int end_of_stream; + int p_to_gpb; // Whether the driver supports ROI at all. int roi_allowed; diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c index e1dc53dfa9..ea45893508 100644 --- a/libavcodec/vaapi_encode_h265.c +++ b/libavcodec/vaapi_encode_h265.c @@ -886,6 +886,7 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice) { + VAAPIEncodeContext *ctx = avctx->priv_data; VAAPIEncodeH265Context *priv = avctx->priv_data; VAAPIEncodeH265Picture *hpic = pic->priv_data; const H265RawSPS *sps = &priv->raw_sps; @@ -908,6 +909,9 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, sh->slice_type = hpic->slice_type; + if (sh->slice_type == HEVC_SLICE_P && ctx->p_to_gpb) + sh->slice_type = HEVC_SLICE_B; + sh->slice_pic_order_cnt_lsb = hpic->pic_order_cnt & (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1; @@ -1066,6 +1070,9 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, av_assert0(pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B); vslice->ref_pic_list0[0] = vpic->reference_frames[0]; + if (ctx->p_to_gpb && pic->type == PICTURE_TYPE_P) + // Reference for GPB B-frame, L0 == L1 + vslice->ref_pic_list1[0] = vpic->reference_frames[0]; } if (pic->nb_refs >= 2) { // Forward reference for B-frame. @@ -1073,6 +1080,14 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, vslice->ref_pic_list1[0] = vpic->reference_frames[1]; } + if (pic->type == PICTURE_TYPE_P && ctx->p_to_gpb) { + vslice->slice_type = HEVC_SLICE_B; + for (i = 0; i < FF_ARRAY_ELEMS(vslice->ref_pic_list0); i++) { + vslice->ref_pic_list1[i].picture_id = vslice->ref_pic_list0[i].picture_id; + vslice->ref_pic_list1[i].flags = vslice->ref_pic_list0[i].flags; + } + } + return 0; } From patchwork Thu Mar 17 06:41:50 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: 34805 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:3486:0:0:0:0 with SMTP id ek6csp645302nkb; Wed, 16 Mar 2022 23:48:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6w4wNaJGrnhhyELK4bi1OqYkr/MhxNUOefzoF6VYaGwrOhzGr2bUwDTtpdrc9SFxUnWy/ X-Received: by 2002:a17:906:4313:b0:6b8:b3e5:a46 with SMTP id j19-20020a170906431300b006b8b3e50a46mr2918735ejm.417.1647499687574; Wed, 16 Mar 2022 23:48:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647499687; cv=none; d=google.com; s=arc-20160816; b=UF/UPqTxF85KNsUWBXZ1zUQSnGvFyJ28jy59ZEkHEjWks0tR/GtW4CCYWKqSMnPKKF +Idd8aDHvV/dYal/Y1GUIJ1KutHAjGZz8ZSnx3OpaDdy0k3K9Q/TtKWUPMfPMmRep5yr mIWj9DgOsUTZ3kf3ClQ5I/yX9Iuy0b/5u1JD/U+frxEA2Xma0pz9uw4/nBrUwH4AknX8 AblPYxdYsXWzwN7zNlabnCoPJ58UGA4kezneb2AeAS6aUdix6/BB6Niz9I8HBSVIXCxH CTH5nILCUoLP6rgaXEuTtqmLhieKVJ0Ad9KeGKeRzAK3VFkL0woboNW9IycLppyNjKS0 QjFQ== 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=9neWumwDYvAZm2qy5hTwr7qTgpyP0FNWyjGEHeoBW2o=; b=c+97m3GSMJUdy1e+uTIYuRF1bDh3dugic3VFKesMy3gO7SSZOzBszX7f+Zj5o+m8Gu Bp+c4TS++qvMTOwNoR8dZpXmGkrCUfAyi243S84qnGllSimeF4CvI83AQoJO3atkcyP8 pxIB65dTS1BCw4LWgJ3Bao0C8333e42Rns1hNOfWgrZ0VnOEF8THnKuAO1eXaQU+rhp4 kTbdqYVniyY9U2vGSRf5UyJao5QhkQtro/hD8b5P1mCZQLwy4UYgkJEcB8gIbmfjmWOo H9UaV87SVyhFbbbsEVOdh3bP2kz8vREm5I31Tgrd/GBrMxataZOtevsc3gYtcENl5rJw 2uLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=AK+aZWrt; 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 j10-20020a50d00a000000b00418c2b5bf27si867774edf.521.2022.03.16.23.48.07; Wed, 16 Mar 2022 23:48:07 -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=AK+aZWrt; 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 7912D68B17E; Thu, 17 Mar 2022 08:47:58 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9F16268AEC2 for ; Thu, 17 Mar 2022 08:47:51 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647499676; x=1679035676; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rykIsUZUgZ0xJyWCvarHf/fqDcrl8JcISEuT+lu+Y/w=; b=AK+aZWrti3p5uaczCtEfUQeBtqpU2USMuEABfRJ9K14KEXc5eVw25Z7a 6vz307CnlW5YRDJwPiPjpsWA/SsB8wMmcRPbkKmT6lRTwoT4Mwx/dBjym 6zTS5EUXezZ9WR46UKMxcG7/2B+MmHCO9a/twA/5vsBql2qoSNxkcLDGU mHxdCGZ5/0n9ncqkugtOk6qAc4cc7U6kCXUnf7PPgaTm3pFA46rlb1tmA yuVsfWZrldVzKhGGN74uc+81EzfwyOOEM7Hz3NdeH0x/wbNhiclz6joHi jco2M2BjbLeSO3yCG1lK4bWHbkN0DCskbaXSpoVxACv5oMF+naCLK8ZUj Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10288"; a="238960172" X-IronPort-AV: E=Sophos;i="5.90,188,1643702400"; d="scan'208";a="238960172" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2022 23:47:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,188,1643702400"; d="scan'208";a="714919925" Received: from t.sh.intel.com ([10.239.159.147]) by orsmga005.jf.intel.com with ESMTP; 16 Mar 2022 23:47:48 -0700 From: Fei Wang To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Mar 2022 14:41:50 +0800 Message-Id: <20220317064152.430212-2-fei.w.wang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220317064152.430212-1-fei.w.wang@intel.com> References: <20220317064152.430212-1-fei.w.wang@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 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: OpbB7OwkqnjP 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 081eb475a3..3148c8915e 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -2069,6 +2069,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) / @@ -2458,6 +2460,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); } From patchwork Thu Mar 17 06:41:51 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: 34806 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:3486:0:0:0:0 with SMTP id ek6csp645369nkb; Wed, 16 Mar 2022 23:48:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmTFx8qThUWMS/o9tiqEwt6yfQ7R910Olkp+udM+ZqYSIVsaQoXUf8wUOCF4GrSvEc26fY X-Received: by 2002:a17:907:9506:b0:6da:b4cd:515b with SMTP id ew6-20020a170907950600b006dab4cd515bmr3010103ejc.602.1647499696272; Wed, 16 Mar 2022 23:48:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647499696; cv=none; d=google.com; s=arc-20160816; b=VteG+byfdbfPBD3QtgfD/LpjM4RpeKwH3MT40dbUPF7OiLtXedzxlqDdCIBmRxD/xk P8UU+Xs94nyvkfeXmjUZ3LTzGWtF7T2/Jo6Pg029UXxm0hcD3sGZkqXQS+xdseFN4O/Y 7Lxq5lNpZLC0z8LJzh2SVXW0sY3X9Q7dYDTRnj9LaUWDLHP2RW4qrGGhzQNbBv1BjvPH 5WnPTbZSi5NZdDr1uGXxhsn12RoEasoQGdD0ybYs8EwuKVSDUD4dtgolNXanC92qhqcS bsB4U5eh+utIMkg5+ghGrsUcfv1D4pEfI6+MQYysOSXqcZgNcJMHwTqzpRZemBwXu0CZ UO/A== 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=mVR1+yG7yOktCeDV+UxjYS8nyG7gsvaApP3kw6v0vMs=; b=o3efaARxJlsg87Yzr/xyfFOwoOorZZtXd6PiAeUBS50zQTJAnIKLJqmLCmKr/8HRqA TyDnHzPHCR/0xGc0vrcAGiyIKB7ZMt12A1YqgAbUFEfN7hTRZL0wHfU1YQqnQ7VjMtHR c49Tigv5Ip0MFPL3V1gzgKrLWRDVBCYp5LU6/EeOppifiw3mU0kc3IpngNGKcGG+NoM4 2A5/tlEMdOxzZcgJAfWDg44cGIJ2SaewVC6nRYOYm85xDXYfO503FTT4hXKE5lpoRyuG Ds/ITDKAm5iG86e5qvEnuKZNZRPaNbtAs9kQDxQ4TlQvtWhEZs5E0PJUe/Hnn0UVuEmS N/Gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b="O/FqjDh0"; 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 n17-20020a509351000000b00418c2b5be53si816831eda.309.2022.03.16.23.48.15; Wed, 16 Mar 2022 23:48:16 -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="O/FqjDh0"; 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 A406268B165; Thu, 17 Mar 2022 08:48:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C61F968AEA2 for ; Thu, 17 Mar 2022 08:47:52 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647499678; x=1679035678; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sVxPY9uHQIST9BqtrHlhsPu95BZTdwm99pzeO4VLRXM=; b=O/FqjDh0QF3XCLHUQZd8/LqRuuhuQLNsjIkSe3loy8S4ySHzwZ7eWDSK hy2nLjdJcWDs7VtiFq+Hnz3ubBHKtcdGkVC9mnbXuTMRP3GtucdISYKXr r+PusNMA0oC6n8JFNBsry/qP/0wKjmuHm2DU8aV3b5hFmG0o0HQIqc9K0 k0JP/Xe4JO5lJH9iv7IdgCJ7e8+h9iaH2IFPrQvUPsigMFQrhqJB4vWHJ Z77mZSbk6jIqu4yTSEKEwMuryJqd3c6PpBSRXXPkeg64FisISYFdFDZpq 4L0wFy07L2af/LI4soR6Uup9kK/uyMEanm38vQ7j4IBJ8jDOVJzcThq7g A==; X-IronPort-AV: E=McAfee;i="6200,9189,10288"; a="238960174" X-IronPort-AV: E=Sophos;i="5.90,188,1643702400"; d="scan'208";a="238960174" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2022 23:47:51 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,188,1643702400"; d="scan'208";a="714919929" Received: from t.sh.intel.com ([10.239.159.147]) by orsmga005.jf.intel.com with ESMTP; 16 Mar 2022 23:47:50 -0700 From: Fei Wang To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Mar 2022 14:41:51 +0800 Message-Id: <20220317064152.430212-3-fei.w.wang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220317064152.430212-1-fei.w.wang@intel.com> References: <20220317064152.430212-1-fei.w.wang@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 3/4] vaapi_encode_h265: Explicitly set and correct some flags 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: 9WCFKbbETFzE From: Mark Thompson max_14bit_constraint_flag should be set if the bit depth is not greater than 14 (currently always true). one_picture_only_flag should not be set because we don't support the still picture profiles. general_profile_compatibility_flag should be set according to general_profile_idc instead of bit depth. Signed-off-by: Fei Wang --- libavcodec/vaapi_encode_h265.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c index 706ec5d849..cafc860772 100644 --- a/libavcodec/vaapi_encode_h265.c +++ b/libavcodec/vaapi_encode_h265.c @@ -303,17 +303,21 @@ static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx) ptl->general_profile_idc = avctx->profile; ptl->general_tier_flag = priv->tier; - if (chroma_format == 1) { - ptl->general_profile_compatibility_flag[1] = bit_depth == 8; - ptl->general_profile_compatibility_flag[2] = bit_depth <= 10; + ptl->general_profile_compatibility_flag[ptl->general_profile_idc] = 1; + + if (ptl->general_profile_compatibility_flag[1]) + ptl->general_profile_compatibility_flag[2] = 1; + if (ptl->general_profile_compatibility_flag[3]) { + ptl->general_profile_compatibility_flag[1] = 1; + ptl->general_profile_compatibility_flag[2] = 1; } - ptl->general_profile_compatibility_flag[4] = 1; ptl->general_progressive_source_flag = 1; ptl->general_interlaced_source_flag = 0; ptl->general_non_packed_constraint_flag = 1; ptl->general_frame_only_constraint_flag = 1; + ptl->general_max_14bit_constraint_flag = bit_depth <= 14; ptl->general_max_12bit_constraint_flag = bit_depth <= 12; ptl->general_max_10bit_constraint_flag = bit_depth <= 10; ptl->general_max_8bit_constraint_flag = bit_depth == 8; @@ -323,6 +327,7 @@ static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx) ptl->general_max_monochrome_constraint_flag = chroma_format == 0; ptl->general_intra_constraint_flag = ctx->gop_size == 1; + ptl->general_one_picture_only_constraint_flag = 0; ptl->general_lower_bit_rate_constraint_flag = 1; From patchwork Thu Mar 17 06:41:52 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: 34807 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:3486:0:0:0:0 with SMTP id ek6csp645427nkb; Wed, 16 Mar 2022 23:48:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMiGKGy1o6qlwRBWt3AKsr/uK9Zm0cI9+j9YgZ+VyGgcmHLNMPjT0xGVTdWDsf0J0DJNRP X-Received: by 2002:a17:906:d54f:b0:6db:ade0:9829 with SMTP id cr15-20020a170906d54f00b006dbade09829mr2947522ejc.127.1647499706017; Wed, 16 Mar 2022 23:48:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647499706; cv=none; d=google.com; s=arc-20160816; b=iccy7UrhZHRo9fRzFZKsdxnGYIV6govakjbRiVQjp8WAfgdi4TaIJunuQu5b/TH9dW nTVJl9ZUw03vCOqzwZoz7gYfyFozYPkYwqnQLNT67zTlBFFLryrxg6XCaFitsdCMIsR+ dRhbwAqy0jwBkYP+1fAjmy6gRmLyxwJGlpR2w3AkbcwCYngBsaR3COat4HDpBdeORRUL 01Hwlu6B6v3iPI5D8mWPgYjhRLH0kIV9VO03RS4TvppNO3YhHDbErMlfLfNGvJ6iuhDv GyOCYBx3WCc0/PhnXs73sTI/8kjNxjUgKxqvgibNpdv8AOZkH7mFY9/8V+WoXuha28tf AFzA== 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=BPeLz+UVLV1FvEDCDiRI+7Upv1Gnt7eYXgy+Nn88cLw=; b=AxIezDWLSwAZk4HUpq2HhchOREE7ZKXAvb3Un0OiKzd5hG3/3UWnFwrR0WjDPuqa5x /75jM79htmoRvOmQEPPI2ToInwp2pEe/IEaNa0GYEyApG9zH7BPqWjj+baWBkHS8K/CX xzhBTlTG0VIgcUK8/xjCtBRgEVwZ27DFZL69BZl/DlyHUcXtiqj2Zdoz96cXfYeXZ05M E7vH4wbQonRoxqz/u9LL5zoi7aodMNTnYrX4CDVrWQgJhvMLrYtSnCy/K1Me1pkWSiTy Nt6RMmyj8TIGT4S9EkTPEbLBzMb/rLfLm/3s9Kkt/GBEetnC6MVTh9kdeQa9K1qSq759 4N6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=jqXTWpAn; 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 st11-20020a170907c08b00b006df8f44af5asi736837ejc.932.2022.03.16.23.48.25; Wed, 16 Mar 2022 23:48:26 -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=jqXTWpAn; 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 A985A68B19C; Thu, 17 Mar 2022 08:48:01 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 50E9D68B00F for ; Thu, 17 Mar 2022 08:47:54 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647499679; x=1679035679; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=agBmL9/3qtKx+VHVXod0cD325OM4O9IfZiFoDyU/RhU=; b=jqXTWpAn43LYrfkHbTEVNFSMArw01TbMRnFsX81eCpdmNzrJ/Xyigy5Y AyCrXB+yk+Bk9voqwFTmHfCwvgd0ygyo/wOiD75aZXZ726Lj7c2U7P1Zq /C5dBX8sLCDLSBoh83OvOYAKdt0klfpiTQtfRIL6rFYCX9owKN/doaLGi rqaWZSKPjsKzoEPMRc+gxAjR3esmgKFSH8SdxLtmqOvmHm/ukKwMlcsfr 9gCzj1WBNJ1ls2QY5n684f9jl5dgVLEJ3uLssl2yPrI8cxIneLGXm17TB 2MHlB3F60WPgI+/wlioBj6gQTGX+CHp0GLhJ4G9FfuXmyyn0ThUfB3XLl g==; X-IronPort-AV: E=McAfee;i="6200,9189,10288"; a="238960177" X-IronPort-AV: E=Sophos;i="5.90,188,1643702400"; d="scan'208";a="238960177" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2022 23:47:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,188,1643702400"; d="scan'208";a="714919934" Received: from t.sh.intel.com ([10.239.159.147]) by orsmga005.jf.intel.com with ESMTP; 16 Mar 2022 23:47:51 -0700 From: Fei Wang To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Mar 2022 14:41:52 +0800 Message-Id: <20220317064152.430212-4-fei.w.wang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220317064152.430212-1-fei.w.wang@intel.com> References: <20220317064152.430212-1-fei.w.wang@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 4/4] vaapi_encode_h265: Query encoding block sizes and features 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: JMw+KMdNKvVf From: Mark Thompson Signed-off-by: Fei Wang --- libavcodec/vaapi_encode_h265.c | 114 +++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 6 deletions(-) diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c index cafc860772..ad138b6ddb 100644 --- a/libavcodec/vaapi_encode_h265.c +++ b/libavcodec/vaapi_encode_h265.c @@ -56,6 +56,9 @@ typedef struct VAAPIEncodeH265Context { VAAPIEncodeContext common; // Encoder features. + uint32_t va_features; + // Block size info. + uint32_t va_bs; uint32_t ctu_size; uint32_t min_cb_size; @@ -427,9 +430,9 @@ static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx) vps->vps_max_latency_increase_plus1[i]; } - // These have to come from the capabilities of the encoder. We have no - // way to query them, so just hardcode parameters which work on the Intel - // driver. + // These values come from the capabilities of the first encoder + // implementation in the i965 driver on Intel Skylake. They may + // fail badly with other platforms or drivers. // CTB size from 8x8 to 32x32. sps->log2_min_luma_coding_block_size_minus3 = 0; sps->log2_diff_max_min_luma_coding_block_size = 2; @@ -447,6 +450,42 @@ static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx) sps->pcm_enabled_flag = 0; +// update sps setting according to queried result +#if VA_CHECK_VERSION(1, 13, 0) + if (priv->va_features) { + VAConfigAttribValEncHEVCFeatures features = { .value = priv->va_features }; + + // Enable feature if get queried result is VA_FEATURE_SUPPORTED | VA_FEATURE_REQUIRED + sps->amp_enabled_flag = + !!features.bits.amp; + sps->sample_adaptive_offset_enabled_flag = + !!features.bits.sao; + sps->sps_temporal_mvp_enabled_flag = + !!features.bits.temporal_mvp; + sps->pcm_enabled_flag = + !!features.bits.pcm; + } + + if (priv->va_bs) { + VAConfigAttribValEncHEVCBlockSizes bs = { .value = priv->va_bs }; + sps->log2_min_luma_coding_block_size_minus3 = + ff_ctz(priv->min_cb_size) - 3; + sps->log2_diff_max_min_luma_coding_block_size = + ff_ctz(priv->ctu_size) - ff_ctz(priv->min_cb_size); + + sps->log2_min_luma_transform_block_size_minus2 = + bs.bits.log2_min_luma_transform_block_size_minus2; + sps->log2_diff_max_min_luma_transform_block_size = + bs.bits.log2_max_luma_transform_block_size_minus2 - + bs.bits.log2_min_luma_transform_block_size_minus2; + + sps->max_transform_hierarchy_depth_inter = + bs.bits.max_max_transform_hierarchy_depth_inter; + sps->max_transform_hierarchy_depth_intra = + bs.bits.max_max_transform_hierarchy_depth_intra; + } +#endif + // STRPSs should ideally be here rather than defined individually in // each slice, but the structure isn't completely fixed so for now // don't bother. @@ -539,6 +578,23 @@ static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx) pps->cu_qp_delta_enabled_flag = (ctx->va_rc_mode != VA_RC_CQP); pps->diff_cu_qp_delta_depth = 0; +// update pps setting according to queried result +#if VA_CHECK_VERSION(1, 13, 0) + if (priv->va_features) { + VAConfigAttribValEncHEVCFeatures features = { .value = priv->va_features }; + if (ctx->va_rc_mode != VA_RC_CQP) + pps->cu_qp_delta_enabled_flag = + !!features.bits.cu_qp_delta; + + pps->transform_skip_enabled_flag = + !!features.bits.transform_skip; + // set diff_cu_qp_delta_depth as its max value if cu_qp_delta enabled. Otherwise + // 0 will make cu_qp_delta invalid. + if (pps->cu_qp_delta_enabled_flag) + pps->diff_cu_qp_delta_depth = sps->log2_diff_max_min_luma_coding_block_size; + } +#endif + if (ctx->tile_rows && ctx->tile_cols) { int uniform_spacing; @@ -640,8 +696,8 @@ static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx) .coded_buf = VA_INVALID_ID, - .collocated_ref_pic_index = 0xff, - + .collocated_ref_pic_index = sps->sps_temporal_mvp_enabled_flag ? + 0 : 0xff, .last_picture = 0, .pic_init_qp = pps->init_qp_minus26 + 26, @@ -674,6 +730,8 @@ static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx) .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag, .loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag, + .pps_loop_filter_across_slices_enabled_flag = + pps->pps_loop_filter_across_slices_enabled_flag, .scaling_list_data_present_flag = (sps->sps_scaling_list_data_present_flag | pps->pps_scaling_list_data_present_flag), .screen_content_flag = 0, @@ -1001,10 +1059,13 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, sh->num_long_term_sps = 0; sh->num_long_term_pics = 0; + // when this flag is not present, it is inerred to 1. + sh->collocated_from_l0_flag = 1; sh->slice_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag; if (sh->slice_temporal_mvp_enabled_flag) { - sh->collocated_from_l0_flag = sh->slice_type == HEVC_SLICE_B; + if (sh->slice_type == HEVC_SLICE_B) + sh->collocated_from_l0_flag = 1; sh->collocated_ref_idx = 0; } @@ -1105,6 +1166,47 @@ static av_cold int vaapi_encode_h265_get_encoder_caps(AVCodecContext *avctx) VAAPIEncodeContext *ctx = avctx->priv_data; VAAPIEncodeH265Context *priv = avctx->priv_data; +#if VA_CHECK_VERSION(1, 13, 0) + { + VAConfigAttribValEncHEVCBlockSizes block_size; + VAConfigAttrib attr; + VAStatus vas; + + attr.type = VAConfigAttribEncHEVCFeatures; + vas = vaGetConfigAttributes(ctx->hwctx->display, ctx->va_profile, + ctx->va_entrypoint, &attr, 1); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to query encoder " + "features, using guessed defaults.\n"); + return AVERROR_EXTERNAL; + } else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) { + av_log(avctx, AV_LOG_WARNING, "Driver does not advertise " + "encoder features, using guessed defaults.\n"); + } else { + priv->va_features = attr.value; + } + + attr.type = VAConfigAttribEncHEVCBlockSizes; + vas = vaGetConfigAttributes(ctx->hwctx->display, ctx->va_profile, + ctx->va_entrypoint, &attr, 1); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to query encoder " + "block size, using guessed defaults.\n"); + return AVERROR_EXTERNAL; + } else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) { + av_log(avctx, AV_LOG_WARNING, "Driver does not advertise " + "encoder block size, using guessed defaults.\n"); + } else { + priv->va_bs = block_size.value = attr.value; + + priv->ctu_size = + 1 << block_size.bits.log2_max_coding_tree_block_size_minus3 + 3; + priv->min_cb_size = + 1 << block_size.bits.log2_min_luma_coding_block_size_minus3 + 3; + } + } +#endif + if (!priv->ctu_size) { priv->ctu_size = 32; priv->min_cb_size = 16;