Commit bed45b6f2f for ffmpeg
commit bed45b6f2f6d8a33673d0835ef03063d7f14b19d
Author: Marcin Krycki <m.krycki@gmail.com>
Date: Sun Sep 6 14:36:27 2026 +0100
avcodec/amfenc: make encoder statistics feedback opt-in
Commit 87b7e578ec ("avcodec/amfenc: add encoder average QP stats") enables
AMF_VIDEO_ENCODER_*_STATISTICS_FEEDBACK on every submitted surface. The AMF
property defaults to false and asking the encoder to collect per-frame
statistics is not free: the hardware has to gather the average QP over all
CTBs of every picture.
On a Radeon RX 9070 XT (Navi48, VCN5) driven through the open source
Mesa/RADV Vulkan backend the cost is roughly 48% additional wall time per
frame, for a bit identical bitstream. Measured with a minimal build
(--enable-amf, AMF headers v1.5.2), 1080p23.976, 4353 frames, hevc_amf,
-rc cqp -qp_i 24 -qp_p 24, software decode. Median of repeated runs on an
otherwise idle machine:
n8.1.2 -quality speed 9.27 s 469.6 fps
n9.0.1 -quality speed 13.78 s 315.9 fps
n9.0.1 + this patch -quality speed 9.27 s 469.6 fps
n9.0.1 + this patch, -qp_stats 1 13.78 s 315.9 fps
n8.1.2 -quality quality 16.29 s 267.2 fps
n9.0.1 -quality quality 24.05 s 181.0 fps
n9.0.1 + this patch -quality quality 16.29 s 267.2 fps
n9.0.1 + this patch, -qp_stats 1 23.80 s 182.9 fps
The SHA256 of the encoded video stream is identical across all runs, with
and without the feedback enabled. Per-process DRM fdinfo shows the VCN
encode engine going from 69% to 92% busy at the quality preset once the
feedback is disabled - the engine was being starved, not doing more work.
Other encoders that call ff_encode_add_stats_side_data() (libx264, libx265,
libsvtav1, ...) get the quantiser for free from data they already have, so
reporting it unconditionally costs them nothing. That is not the case here,
which is why this is made opt-in rather than always on.
Add a qp_stats AVOption, defaulting to false, restoring pre-9.0 throughput
by default. Users who want the average QP reported in the ffmpeg CLI
progress line, or as AV_PKT_DATA_QUALITY_STATS side data, can enable it
with -qp_stats 1.
diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
index 3ffca750d8..20bc22d98c 100644
--- a/libavcodec/amfenc.c
+++ b/libavcodec/amfenc.c
@@ -250,7 +250,7 @@ static int amf_copy_buffer(AVCodecContext *avctx, AVPacket *pkt, AMFBuffer *buff
break;
}
- if (average_qp >= 0) {
+ if (ctx->qp_stats && average_qp >= 0) {
ff_encode_add_stats_side_data(pkt, average_qp * FF_QP2LAMBDA, NULL, 0, pict_type);
}
@@ -475,7 +475,9 @@ static int amf_submit_frame(AVCodecContext *avctx, AVFrame *frame, AMFSurface
switch (avctx->codec->id) {
case AV_CODEC_ID_H264:
- AMF_ASSIGN_PROPERTY_BOOL(res, surface, AMF_VIDEO_ENCODER_STATISTICS_FEEDBACK, 1);
+ if (ctx->qp_stats) {
+ AMF_ASSIGN_PROPERTY_BOOL(res, surface, AMF_VIDEO_ENCODER_STATISTICS_FEEDBACK, 1);
+ }
AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!ctx->aud);
switch (frame->pict_type) {
case AV_PICTURE_TYPE_I:
@@ -496,7 +498,9 @@ static int amf_submit_frame(AVCodecContext *avctx, AVFrame *frame, AMFSurface
}
break;
case AV_CODEC_ID_HEVC:
- AMF_ASSIGN_PROPERTY_BOOL(res, surface, AMF_VIDEO_ENCODER_HEVC_STATISTICS_FEEDBACK, 1);
+ if (ctx->qp_stats) {
+ AMF_ASSIGN_PROPERTY_BOOL(res, surface, AMF_VIDEO_ENCODER_HEVC_STATISTICS_FEEDBACK, 1);
+ }
AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!ctx->aud);
switch (frame->pict_type) {
case AV_PICTURE_TYPE_I:
@@ -513,7 +517,9 @@ static int amf_submit_frame(AVCodecContext *avctx, AVFrame *frame, AMFSurface
}
break;
case AV_CODEC_ID_AV1:
- AMF_ASSIGN_PROPERTY_BOOL(res, surface, AMF_VIDEO_ENCODER_AV1_STATISTICS_FEEDBACK, 1);
+ if (ctx->qp_stats) {
+ AMF_ASSIGN_PROPERTY_BOOL(res, surface, AMF_VIDEO_ENCODER_AV1_STATISTICS_FEEDBACK, 1);
+ }
if (frame->pict_type == AV_PICTURE_TYPE_I) {
if (ctx->forced_idr) {
AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_INSERT_SEQUENCE_HEADER, 1);
diff --git a/libavcodec/amfenc.h b/libavcodec/amfenc.h
index 1571541b9b..6a380208d1 100644
--- a/libavcodec/amfenc.h
+++ b/libavcodec/amfenc.h
@@ -100,6 +100,7 @@ typedef struct AMFEncoderContext {
int qvbr_quality_level;
int hw_high_motion_quality_boost;
int forced_idr;
+ int qp_stats;
// HEVC - specific options
diff --git a/libavcodec/amfenc_av1.c b/libavcodec/amfenc_av1.c
index af20b5d04d..a572d439a7 100644
--- a/libavcodec/amfenc_av1.c
+++ b/libavcodec/amfenc_av1.c
@@ -130,6 +130,7 @@ static const AVOption options[] = {
{ "caq", "context adaptive quantization", 0, AV_OPT_TYPE_CONST, {.i64 = AMF_VIDEO_ENCODER_AV1_AQ_MODE_CAQ }, 0, 0, VE, .unit = "adaptive_quantisation_mode" },
{ "forced_idr", "Force I frames to be IDR frames", OFFSET(forced_idr), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
+ { "qp_stats", "Report average QP per frame as packet side data (reduces throughput)", OFFSET(qp_stats), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
{ "align", "alignment mode", OFFSET(align), AV_OPT_TYPE_INT, {.i64 = AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_NO_RESTRICTIONS }, AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_64X16_ONLY, AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_NO_RESTRICTIONS, VE, .unit = "align" },
{ "64x16", "", 0, AV_OPT_TYPE_CONST, {.i64 = AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_64X16_ONLY }, 0, 0, VE, .unit = "align" },
diff --git a/libavcodec/amfenc_h264.c b/libavcodec/amfenc_h264.c
index 650a9bc7e9..214fac6909 100644
--- a/libavcodec/amfenc_h264.c
+++ b/libavcodec/amfenc_h264.c
@@ -140,6 +140,7 @@ static const AVOption options[] = {
{ "me_quarter_pel", "Enable ME Quarter Pixel", OFFSET(me_quarter_pel),AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
{ "forced_idr", "Force I frames to be IDR frames", OFFSET(forced_idr) , AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
+ { "qp_stats", "Report average QP per frame as packet side data (reduces throughput)", OFFSET(qp_stats), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
{ "aud", "Inserts AU Delimiter NAL unit", OFFSET(aud) , AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
{ "smart_access_video", "Enable Smart Access Video to enhance performance by utilizing both APU and dGPU memory access", OFFSET(smart_access_video), AV_OPT_TYPE_BOOL, {.i64 = -1 }, -1, 1, VE},
diff --git a/libavcodec/amfenc_hevc.c b/libavcodec/amfenc_hevc.c
index 6fa20d172e..4e280fcf15 100644
--- a/libavcodec/amfenc_hevc.c
+++ b/libavcodec/amfenc_hevc.c
@@ -111,6 +111,7 @@ static const AVOption options[] = {
{ "me_quarter_pel", "Enable ME Quarter Pixel ", OFFSET(me_quarter_pel),AV_OPT_TYPE_BOOL,{ .i64 = -1 }, -1, 1, VE },
{ "forced_idr", "Force I frames to be IDR frames", OFFSET(forced_idr) ,AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, VE },
+ { "qp_stats", "Report average QP per frame as packet side data (reduces throughput)", OFFSET(qp_stats), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
{ "aud", "Inserts AU Delimiter NAL unit", OFFSET(aud) ,AV_OPT_TYPE_BOOL,{ .i64 = -1 }, -1, 1, VE },
{ "smart_access_video", "Enable Smart Access Video to enhance performance by utilizing both APU and dGPU memory access", OFFSET(smart_access_video), AV_OPT_TYPE_BOOL, {.i64 = -1 }, -1, 1, VE},