Files
Marcin Krycki bed45b6f2f 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.
2026-10-14 21:08:47 +00:00

177 lines
5.3 KiB
C

/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_AMFENC_H
#define AVCODEC_AMFENC_H
#include <AMF/core/Factory.h>
#include <AMF/components/ColorSpace.h>
#include <AMF/components/VideoEncoderVCE.h>
#include <AMF/components/VideoEncoderHEVC.h>
#include <AMF/components/VideoEncoderAV1.h>
#include "libavutil/fifo.h"
#include "avcodec.h"
#include "hwconfig.h"
#define MAX_LOOKAHEAD_DEPTH 41
/**
* AMF encoder context
*/
typedef struct AMFEncoderContext {
AVClass *avclass;
// access to AMF runtime
AVBufferRef *device_ctx_ref;
//encoder
AMFComponent *encoder; ///< AMF encoder object
amf_bool eof; ///< flag indicating EOF happened
AMF_SURFACE_FORMAT format; ///< AMF surface format
wchar_t *pts_property_name;
wchar_t *av_frame_property_name;
int hwsurfaces_in_queue;
int hwsurfaces_in_queue_max;
int query_timeout_supported;
// helpers to handle async calls
int delayed_drain;
// shift dts back by max_b_frames in timing
AVFifo *timestamp_list;
int64_t dts_delay;
int64_t submitted_frame;
int64_t encoded_frame;
AVFifo *output_list;
// common encoder options
// Static options, have to be set before Init() call
int usage;
int profile;
int level;
int latency;
int preencode;
int quality;
int b_frame_delta_qp;
int ref_b_frame_delta_qp;
int bit_depth;
int smart_access_video;
// Dynamic options, can be set after Init() call
int rate_control_mode;
int enforce_hrd;
int filler_data;
int enable_vbaq;
int skip_frame;
int qp_i;
int qp_p;
int qp_b;
int max_au_size;
int header_spacing;
int b_frame_ref;
int intra_refresh_mb;
int coding_mode;
int me_half_pel;
int me_quarter_pel;
int aud;
int max_consecutive_b_frames;
int max_b_frames;
int qvbr_quality_level;
int hw_high_motion_quality_boost;
int forced_idr;
int qp_stats;
// HEVC - specific options
int gops_per_idr;
int header_insertion_mode;
int min_qp_i;
int max_qp_i;
int min_qp_p;
int max_qp_p;
int min_qp_b;
int max_qp_b;
int tier;
// AV1 - specific options
/* enum AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_ENUM, use int for AVOption */
int align;
/* enum AMF_VIDEO_ENCODER_AV1_AQ_MODE_ENUM */
int aq_mode;
// Preanalysis - specific options
int preanalysis;
int pa_activity_type;
int pa_scene_change_detection;
int pa_scene_change_detection_sensitivity;
int pa_static_scene_detection;
int pa_static_scene_detection_sensitivity;
int pa_initial_qp;
int pa_max_qp;
int pa_caq_strength;
int pa_frame_sad;
int pa_ltr;
int pa_lookahead_buffer_depth;
int pa_paq_mode;
int pa_taq_mode;
int pa_high_motion_quality_boost_mode;
int pa_adaptive_mini_gop;
} AMFEncoderContext;
extern const AVCodecHWConfigInternal *const ff_amfenc_hw_configs[];
/**
* Common encoder initization function
*/
int ff_amf_encode_init(AVCodecContext *avctx);
/**
* Common encoder termination function
*/
int ff_amf_encode_close(AVCodecContext *avctx);
/**
* Ecoding one frame - common function for all AMF encoders
*/
int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);
/**
* Supported formats
*/
extern const enum AVPixelFormat ff_amf_pix_fmts[];
/**
* Error handling helper
*/
#define AMF_RETURN_IF_FALSE(avctx, exp, ret_value, /*message,*/ ...) \
if (!(exp)) { \
av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
return ret_value; \
}
#endif //AVCODEC_AMFENC_H