From: Fei Wang <fei.w.wang-at-intel.com@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Fei Wang <fei.w.wang@intel.com>, Linjie Fu <linjie.fu@intel.com>
Subject: [FFmpeg-devel] [PATCH v4 1/4] lavc/vaapi_encode_h265: Add GPB frame support for hevc_vaapi
Date: Fri, 11 Mar 2022 17:00:43 +0800
Message-ID: <20220311090046.262777-1-fei.w.wang@intel.com> (raw)
From: Linjie Fu <linjie.fu@intel.com>
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 <linjie.fu@intel.com>
Signed-off-by: Fei Wang <fei.w.wang@intel.com>
---
update:
1. Add b to gpb.
2. Optimise debug message.
libavcodec/vaapi_encode.c | 74 +++++++++++++++++++++++++++++++---
libavcodec/vaapi_encode.h | 2 +
libavcodec/vaapi_encode_h265.c | 24 ++++++++++-
3 files changed, 93 insertions(+), 7 deletions(-)
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 3bf379b1a0..bdba9726b2 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -848,9 +848,13 @@ static void vaapi_encode_set_b_pictures(AVCodecContext *avctx,
pic->b_depth = current_depth;
vaapi_encode_add_ref(avctx, pic, start, 1, 1, 0);
- vaapi_encode_add_ref(avctx, pic, end, 1, 1, 0);
vaapi_encode_add_ref(avctx, pic, prev, 0, 0, 1);
+ if (!ctx->b_to_gpb)
+ vaapi_encode_add_ref(avctx, pic, end, 1, 1, 0);
+ else
+ vaapi_encode_add_ref(avctx, pic, end, 0, 1, 0);
+
for (ref = end->refs[1]; ref; ref = ref->refs[1])
vaapi_encode_add_ref(avctx, pic, ref, 0, 1, 0);
}
@@ -871,8 +875,11 @@ static void vaapi_encode_set_b_pictures(AVCodecContext *avctx,
vaapi_encode_add_ref(avctx, pic, pic, 0, 1, 0);
vaapi_encode_add_ref(avctx, pic, start, 1, 1, 0);
- vaapi_encode_add_ref(avctx, pic, end, 1, 1, 0);
vaapi_encode_add_ref(avctx, pic, prev, 0, 0, 1);
+ if (!ctx->b_to_gpb)
+ vaapi_encode_add_ref(avctx, pic, end, 1, 1, 0);
+ else
+ vaapi_encode_add_ref(avctx, pic, end, 0, 1, 0);
for (ref = end->refs[1]; ref; ref = ref->refs[1])
vaapi_encode_add_ref(avctx, pic, ref, 0, 1, 0);
@@ -1845,6 +1852,51 @@ static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx)
ref_l1 = attr.value >> 16 & 0xffff;
}
+ ctx->p_to_gpb = 0;
+ ctx->b_to_gpb = 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) {
+ ctx->b_to_gpb = 1;
+ av_log(avctx, AV_LOG_VERBOSE, "Driver support previous prediction "
+ "only 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 previous prediction only 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");
@@ -1855,14 +1907,24 @@ static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx)
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);
+ 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..4ce1df0c6f 100644
--- a/libavcodec/vaapi_encode.h
+++ b/libavcodec/vaapi_encode.h
@@ -328,6 +328,8 @@ typedef struct VAAPIEncodeContext {
int idr_counter;
int gop_counter;
int end_of_stream;
+ int p_to_gpb;
+ int b_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..25b9a707d2 100644
--- a/libavcodec/vaapi_encode_h265.c
+++ b/libavcodec/vaapi_encode_h265.c
@@ -717,7 +717,10 @@ static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
hpic->pic_type = 1;
} else {
VAAPIEncodePicture *irap_ref;
- av_assert0(pic->refs[0] && pic->refs[1]);
+ if (!ctx->b_to_gpb)
+ av_assert0(pic->refs[0] && pic->refs[1]);
+ else
+ av_assert0(pic->refs[0]);
for (irap_ref = pic; irap_ref; irap_ref = irap_ref->refs[1]) {
if (irap_ref->type == PICTURE_TYPE_I)
break;
@@ -886,6 +889,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 +912,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,11 +1073,26 @@ 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.
av_assert0(pic->type == PICTURE_TYPE_B);
vslice->ref_pic_list1[0] = vpic->reference_frames[1];
+ if (ctx->b_to_gpb && pic->type == PICTURE_TYPE_B)
+ // Reference for GPB B-frame, L0 == L1
+ vslice->ref_pic_list1[0] = vpic->reference_frames[0];
+ }
+
+ if ((pic->type == PICTURE_TYPE_P && ctx->p_to_gpb) ||
+ (pic->type == PICTURE_TYPE_B && ctx->b_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;
--
2.25.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next reply other threads:[~2022-03-11 9:08 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-11 9:00 Fei Wang [this message]
2022-03-11 9:00 ` [FFmpeg-devel] [PATCH v4 2/4] vaapi_encode: Move block size calculation after entrypoint selection Fei Wang
2022-03-11 9:00 ` [FFmpeg-devel] [PATCH v4 3/4] vaapi_encode_h265: Explicitly set and correct some flags Fei Wang
2022-03-11 9:00 ` [FFmpeg-devel] [PATCH v4 4/4] vaapi_encode_h265: Query encoding block sizes and features Fei Wang
2022-03-13 20:45 ` [FFmpeg-devel] [PATCH v4 1/4] lavc/vaapi_encode_h265: Add GPB frame support for hevc_vaapi Mark Thompson
2022-03-14 2:15 ` Xiang, Haihao
2022-03-14 11:07 ` Wang, Fei W
2022-03-15 0:24 ` Mark Thompson
2022-03-16 15:04 ` Xiang, Haihao
2022-03-14 23:38 ` Mark Thompson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220311090046.262777-1-fei.w.wang@intel.com \
--to=fei.w.wang-at-intel.com@ffmpeg.org \
--cc=fei.w.wang@intel.com \
--cc=ffmpeg-devel@ffmpeg.org \
--cc=linjie.fu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git