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; }