Commit e5b51e8b60 for ffmpeg
commit e5b51e8b609e8bc5667f07d4c5a6eaeccedb9f1c
Author: lusoris <lusoris@proton.me>
Date: Sun Nov 8 17:01:42 2026 +0100
avcodec/libx264: add a qpfile option
Add the qpfile option of libaom-av1 to libx264, with the same file
format, and pass the offsets to x264 as per-macroblock quantizer
offsets. A record without offset lines ends the offsets from that
frame on.
On a frame that also carries region-of-interest side data, the
offsets are added to the quantizer offsets built from that side data;
a record without offset lines leaves those unchanged.
The frame number counts every input frame, also one that the encoder
refuses, so the frames after a refused frame keep their own records.
x264 applies the offsets through adaptive quantization. That is off
with constant QP, and with aq-mode 0 or aq-strength 0 unless MB-tree
is on, which keeps it running. The option is therefore checked
against the parameters x264 reports after x264_encoder_open() and
refused when adaptive quantization is off. Interlaced and
fake-interlaced encoding are refused too: x264 rounds the macroblock
row count up to an even number for both, so the per-row offsets no
longer match the grid x264 reads.
libx262 is built from libx264.c and shares its options, so it selects
the qpfile component too.
Counting the refused frames follows a Forgejo Fairy review of this
option.
Signed-off-by: lusoris <lusoris@proton.me>
diff --git a/configure b/configure
index 02680a01e1..eed7a76aa9 100755
--- a/configure
+++ b/configure
@@ -3885,8 +3885,9 @@ libvvenc_encoder_deps="libvvenc"
libwebp_encoder_deps="libwebp"
libwebp_anim_encoder_deps="libwebp"
libx262_encoder_deps="libx262"
+libx262_encoder_select="qpfile"
libx264_encoder_deps="libx264"
-libx264_encoder_select="atsc_a53 golomb"
+libx264_encoder_select="atsc_a53 golomb qpfile"
libx264rgb_encoder_deps="libx264"
libx264rgb_encoder_select="libx264_encoder"
libx265_encoder_deps="libx265"
diff --git a/doc/encoders.texi b/doc/encoders.texi
index 34b8f257dd..ce70150054 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3241,6 +3241,39 @@ Variable bit rate.
Constant bit rate (not allowed in MP4 container).
@end table
+@item qpfile @var{filename}
+Read per-frame macroblock QP offsets from @var{filename} and pass them to
+x264 as quantizer offsets. x264 applies the offsets through adaptive
+quantization; the option is refused when x264 runs without adaptive
+quantization, that is with constant QP, or with @option{aq-mode} 0 or
+@option{aq-strength} 0 while @option{mbtree} is off; it is also refused
+with interlaced or fake-interlaced encoding.
+
+The file is plain text. Each record starts with a header line
+@samp{@var{frame} @var{type} @var{qp}}, where @var{frame} is the 0-based
+input frame number, strictly increasing from record to record.
+@var{type} is @samp{I}, @samp{P} or @samp{B} in either case, and
+@var{qp} is an integer from 0 to 63; both are checked but not used.
+Frame numbers go up to 4294967295, and a file holds at most 1048576 records.
+Zero or more lines of whitespace-separated integer offsets follow, one
+line per row of 16x16 macroblocks and one value per macroblock column; the
+grid must match the video size rounded up to 16 pixels. Offsets are clipped
+to the range -12 to 12. Lines holding only spaces or tabs and lines starting
+with @samp{#} are skipped; a line with a NUL byte is refused. A frame uses
+the last record whose number is not greater than its own; a record without
+offset lines ends the offsets.
+
+On a frame that also carries region-of-interest side data
+(@code{AV_FRAME_DATA_REGIONS_OF_INTEREST}, for example from the
+@code{addroi} filter), the offsets are added to the per-macroblock
+quantizer offsets that the encoder builds from that side data. A record
+without offset lines leaves those region offsets unchanged.
+
+This is not the format of the x264 command-line @option{--qpfile}
+option, which sets frame types and QPs; the type @samp{K} of that format
+is refused here. In two-pass encoding, give both passes the same file:
+x264 leaves the result undefined when the offsets differ between passes.
+
@item x264opts @var{opts}
@item x264-params @var{opts}
Override the x264 configuration using a :-separated list of key=value
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index f83be47e0c..96aef2eefa 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -39,6 +39,8 @@
#include "sei.h"
#include "golomb.h"
+#include "qpfile_parser.h"
+
#include <x264.h>
#include <float.h>
#include <math.h>
@@ -126,6 +128,10 @@ typedef struct X264Context {
int roi_warned;
int mb_info;
+
+ char *qpfile_path;
+ FFQpFile qpf; /* the loaded qpfile (frame_count 0: none) */
+ unsigned qpf_frames; /* input frames seen: the qpfile's frame index */
} X264Context;
static void X264_log(void *p, int level, const char *fmt, va_list args)
@@ -488,6 +494,36 @@ static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic,
return 0;
}
+/* Add the qpfile's per-MB QP offsets of input frame `frame_index` to the
+ * picture's quant_offsets (x264 adds them to adaptive quantization's own, as
+ * for a region of interest). x264 has no qpfile parameter: its qpfile is a
+ * feature of the x264 command line, not of libx264. */
+static int setup_qpfile(AVCodecContext *ctx, x264_picture_t *pic, unsigned frame_index)
+{
+ X264Context *x4 = ctx->priv_data;
+ const FFQpFrame *qf;
+ size_t n_mb;
+ float *qoffsets;
+
+ qf = ff_qpfile_frame_for_index(&x4->qpf, frame_index);
+ if (!qf || !qf->block_offsets)
+ return 0;
+ n_mb = (size_t)qf->block_cols * qf->block_rows;
+
+ qoffsets = pic->prop.quant_offsets;
+ if (!qoffsets) {
+ qoffsets = av_calloc(n_mb, sizeof(*qoffsets));
+ if (!qoffsets)
+ return AVERROR(ENOMEM);
+ pic->prop.quant_offsets = qoffsets;
+ pic->prop.quant_offsets_free = av_free;
+ }
+ for (size_t i = 0; i < n_mb; i++)
+ qoffsets[i] += (float)qf->block_offsets[i];
+
+ return 0;
+}
+
static int setup_frame(AVCodecContext *ctx, const AVFrame *frame,
x264_picture_t **ppic)
{
@@ -496,6 +532,7 @@ static int setup_frame(AVCodecContext *ctx, const AVFrame *frame,
x264_picture_t *pic = &x4->pic;
x264_sei_t *sei = &pic->extra_sei;
unsigned int sei_data_size = 0;
+ unsigned frame_index = 0;
int64_t wallclock = 0;
int ret;
AVFrameSideData *sd;
@@ -505,6 +542,12 @@ static int setup_frame(AVCodecContext *ctx, const AVFrame *frame,
if (!frame)
return 0;
+ /* Count every input frame here, before any step that can fail: the
+ * caller drops a frame whose setup fails, and the following frames
+ * must keep their own qpfile records. */
+ if (x4->qpf.frame_count)
+ frame_index = x4->qpf_frames++;
+
x264_picture_init(pic);
pic->img.i_csp = x4->params.i_csp;
if (x4->params.i_bitdepth > 8)
@@ -585,6 +628,12 @@ static int setup_frame(AVCodecContext *ctx, const AVFrame *frame,
goto fail;
}
+ if (x4->qpf.frame_count) {
+ ret = setup_qpfile(ctx, pic, frame_index);
+ if (ret < 0)
+ goto fail;
+ }
+
mbinfo_sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_HINT);
if (mbinfo_sd) {
int err = setup_mb_info(ctx, pic, frame, (const AVVideoHint *)mbinfo_sd->data);
@@ -800,6 +849,7 @@ static av_cold int X264_close(AVCodecContext *avctx)
X264Context *x4 = avctx->priv_data;
av_freep(&x4->sei);
+ ff_qpfile_free(&x4->qpf);
for (int i = 0; i < x4->nb_reordered_opaque; i++)
opaque_uninit(&x4->reordered_opaque[i]);
@@ -1433,6 +1483,19 @@ static av_cold int X264_init(AVCodecContext *avctx)
x4->params.analyse.b_mb_info = x4->mb_info;
+ if (x4->qpfile_path && *x4->qpfile_path) {
+ ret = ff_qpfile_load(avctx, x4->qpfile_path, &x4->qpf);
+ if (ret < 0) {
+ av_log(avctx, AV_LOG_ERROR, "cannot read qpfile %s: %s\n",
+ x4->qpfile_path, av_err2str(ret));
+ return ret;
+ }
+ ret = ff_qpfile_check_grid(avctx, "libx264", &x4->qpf,
+ avctx->width, avctx->height);
+ if (ret < 0)
+ return ret;
+ }
+
// update AVCodecContext with x264 parameters
avctx->has_b_frames = x4->params.i_bframe ?
x4->params.i_bframe_pyramid ? 2 : 1 : 0;
@@ -1445,6 +1508,26 @@ static av_cold int X264_init(AVCodecContext *avctx)
if (!x4->enc)
return AVERROR_EXTERNAL;
+ if (x4->qpfile_path && *x4->qpfile_path) {
+ x264_param_t effective;
+ x264_encoder_parameters(x4->enc, &effective);
+ if (effective.rc.i_aq_mode == X264_AQ_NONE) {
+ av_log(avctx, AV_LOG_ERROR,
+ "a qpfile needs adaptive quantization, but x264 runs without it (constant QP, or aq-mode 0 or aq-strength 0 without mbtree).\n");
+ return AVERROR(EINVAL);
+ }
+ if (effective.b_interlaced || effective.b_fake_interlaced) {
+ av_log(avctx, AV_LOG_ERROR,
+ "a qpfile does not support interlaced or fake-interlaced encoding.\n");
+ return AVERROR(EINVAL);
+ }
+ const unsigned mbx = (avctx->width + MB_SIZE - 1) / MB_SIZE;
+ const unsigned mby = (avctx->height + MB_SIZE - 1) / MB_SIZE;
+ av_log(avctx, AV_LOG_VERBOSE, "qpfile=%s loaded (%u frames, %ux%u macroblocks); "
+ "offsets applied as quant_offsets.\n", x4->qpfile_path, x4->qpf.frame_count,
+ mbx, mby);
+ }
+
if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
ret = set_extradata(avctx);
if (ret < 0)
@@ -1609,6 +1692,7 @@ static const AVOption options[] = {
{ "udu_sei", "Use user data unregistered SEI if available", OFFSET(udu_sei), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
{ "x264-params", "Override the x264 configuration using a :-separated list of key=value parameters", OFFSET(x264_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
{ "mb_info", "Set mb_info data through AVSideData, only useful when used from the API", OFFSET(mb_info), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
+ { "qpfile", "Read per-frame QP offsets from a file (needs adaptive quantization)", OFFSET(qpfile_path), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
{ NULL },
};