Commit c8df5bb00a for ffmpeg
commit c8df5bb00a2b307cfb594126ddbe8c6f3a1f6d52
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Mon Nov 23 15:50:26 2026 -0800
avcodec/libx264: set picture timing from interlaced field order
x264 chooses automatic pic_struct when queuing a picture, before
applying the pending b_tff change. Set it explicitly once frame setup
succeeds. Preserve the frame-derived order with codec interlace flags.
Otherwise use x264's configured order for a progressive lead-in. Keep
the established order and warn once on a later switch.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Assisted-by: Fairy
diff --git a/configure b/configure
index 9337636ece..927c8d29ae 100755
--- a/configure
+++ b/configure
@@ -2766,6 +2766,7 @@ CONFIG_EXTRA="
lgplv3
libx262
libx264_hdr10
+ libx264_interlaced_420_8bit
llauddsp
llviddsp
llvidencdsp
@@ -7483,6 +7484,9 @@ enabled libx264 && require_pkg_config libx264 x264 "stdint.h x264.h" x
require_cpp_condition libx264 x264.h "X264_BUILD >= 158"; } &&
check_cpp_condition libx264_hdr10 x264.h "X264_BUILD >= 163" &&
check_cpp_condition libx262 x264.h "X264_MPEG2"
+enabled libx264 && check_cpp_condition libx264_interlaced_420_8bit x264.h \
+ "X264_INTERLACED && (X264_BIT_DEPTH == 0 || X264_BIT_DEPTH == 8) && \
+ (X264_CHROMA_FORMAT == 0 || X264_CHROMA_FORMAT == X264_CSP_I420)"
enabled libx265 && require_pkg_config libx265 x265 x265.h x265_api_get &&
require_cpp_condition libx265 x265.h "X265_BUILD >= 89"
enabled libxavs && require libxavs "stdint.h xavs.h" xavs_encoder_encode "-lxavs $pthreads_extralibs $libm_extralibs"
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index f53aa3d65f..6681773a0e 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -120,6 +120,8 @@ typedef struct X264Context {
int nb_reordered_opaque, next_reordered_opaque;
X264Opaque *reordered_opaque;
+ int pic_struct;
+ int field_order_warned;
/**
* If the encoder does not support ROI then warn the first time we
@@ -245,7 +247,8 @@ static void reconfig_encoder(AVCodecContext *ctx)
}
}
-static void reconfig_encoder_from_frame(AVCodecContext *ctx, const AVFrame *frame)
+static void reconfig_encoder_from_frame(AVCodecContext *ctx, const AVFrame *frame,
+ int top_field_first)
{
X264Context *x4 = ctx->priv_data;
AVFrameSideData *side_data;
@@ -253,9 +256,9 @@ static void reconfig_encoder_from_frame(AVCodecContext *ctx, const AVFrame *fram
reconfig_encoder(ctx);
if (x4->avcintra_class < 0) {
- if (x4->params.b_interlaced && x4->params.b_tff != !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)) {
+ if (x4->params.b_interlaced && x4->params.b_tff != top_field_first) {
- x4->params.b_tff = !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
+ x4->params.b_tff = top_field_first;
x264_encoder_reconfig(x4->enc, &x4->params);
}
}
@@ -533,6 +536,8 @@ static int setup_frame(AVCodecContext *ctx, const AVFrame *frame,
x264_sei_t *sei = &pic->extra_sei;
unsigned int sei_data_size = 0;
unsigned frame_index = 0;
+ int pic_struct = PIC_STRUCT_AUTO;
+ int top_field_first;
int64_t wallclock = 0;
int ret;
AVFrameSideData *sd;
@@ -594,7 +599,6 @@ static int setup_frame(AVCodecContext *ctx, const AVFrame *frame,
pic->i_type = X264_TYPE_AUTO;
break;
}
- reconfig_encoder_from_frame(ctx, frame);
if (x4->a53_cc) {
void *sei_data;
@@ -671,6 +675,29 @@ static int setup_frame(AVCodecContext *ctx, const AVFrame *frame,
}
}
+ /* A picture dropped during setup must not establish the field order. */
+ top_field_first = !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
+ if (ctx->codec_id == AV_CODEC_ID_H264 && x4->params.b_interlaced &&
+ x4->avcintra_class < 0 && !x4->params.i_avcintra_class &&
+ !x4->params.rc.psz_zones && !x4->params.rc.i_zones) {
+ /* Preserve the frame-derived order used for container metadata when
+ * codec flags force interlacing. */
+ if (!(frame->flags & AV_FRAME_FLAG_INTERLACED) &&
+ !(ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME)))
+ top_field_first = x4->params.b_tff;
+ pic_struct = top_field_first ? PIC_STRUCT_TOP_BOTTOM : PIC_STRUCT_BOTTOM_TOP;
+ if (x4->pic_struct && x4->pic_struct != pic_struct) {
+ if (frame->flags & AV_FRAME_FLAG_INTERLACED)
+ av_log_once(ctx, AV_LOG_WARNING, AV_LOG_DEBUG, &x4->field_order_warned,
+ "Ignoring interlaced field order change.\n");
+ pic_struct = x4->pic_struct;
+ top_field_first = pic_struct == PIC_STRUCT_TOP_BOTTOM;
+ }
+ }
+ reconfig_encoder_from_frame(ctx, frame, top_field_first);
+
+ /* x264 chooses automatic picture timing at queueing, before applying b_tff. */
+ pic->i_pic_struct = x4->pic_struct = pic_struct;
*ppic = pic;
return 0;
@@ -1490,6 +1517,11 @@ static av_cold int X264_init(AVCodecContext *avctx)
if (!x4->enc)
return AVERROR_EXTERNAL;
+ /* Native x264 options can enable interlacing for progressive input. */
+ if (avctx->codec_id == AV_CODEC_ID_H264 && x4->params.b_interlaced &&
+ avctx->field_order == AV_FIELD_PROGRESSIVE)
+ avctx->field_order = AV_FIELD_UNKNOWN;
+
if (x4->qpfile_path && *x4->qpfile_path) {
x264_param_t effective;
x264_encoder_parameters(x4->enc, &effective);
diff --git a/tests/fate/enc_external.mak b/tests/fate/enc_external.mak
index f5b8100d06..2cfc34126c 100644
--- a/tests/fate/enc_external.mak
+++ b/tests/fate/enc_external.mak
@@ -39,3 +39,34 @@ fate-libx265-encinfo: CMD = $(foreach p,$(FATE_LIBX265_ENCINFO),echo $(p): && \
FATE-yes += $(FATE_ENC_EXTERNAL_ENCINFO-yes)
fate-enc-external: $(FATE_ENC_EXTERNAL_ENCINFO-yes)
+
+# Check startup order for forced and native interlacing, and retain it across a switch.
+LIBX264_FIELD_DEPS = FFMPEG LIBX264_ENCODER LIBX264_INTERLACED_420_8BIT \
+ CBS_H264 TRACE_HEADERS_BSF LAVFI_INDEV \
+ WRAPPED_AVFRAME_DECODER TESTSRC2_FILTER
+FATE_LIBX264-$(call ALLYES, $(LIBX264_FIELD_DEPS) NULL_MUXER) += fate-libx264-progressive-first-bff
+FATE_LIBX264-$(call ALLYES, $(LIBX264_FIELD_DEPS) NULL_MUXER SETFIELD_FILTER CONCAT_FILTER) += fate-libx264-bff-pic-struct
+LIBX264_FIELD_SRC = testsrc2=size=352x288:rate=25
+fate-libx264-bff-pic-struct: LIBX264_FIELD_INPUT = $(LIBX264_FIELD_SRC):duration=0.08,setfield=bff[a];$(LIBX264_FIELD_SRC):duration=0.04,setfield=tff[b];[a][b]concat=n=2:v=1:a=0
+fate-libx264-progressive-first-bff: LIBX264_FIELD_INPUT = $(LIBX264_FIELD_SRC):duration=0.04
+LIBX264_FIELD_OPTS = -flags +ildct+ilme
+fate-libx264-progressive-first-bff: LIBX264_FIELD_OPTS = -x264-params bff=1
+LIBX264_FIELD_ENC = -c:v libx264 -threads:v 1 -bf 0 $(LIBX264_FIELD_OPTS) -bsf:v trace_headers
+LIBX264_FIELD_TRACE = awk "\$$5 == \"pic_struct\" || \$$5 == \"delta_pic_order_cnt_bottom\" { print \$$5 \"=\" \$$8 }" "$$errfile"
+$(FATE_LIBX264-yes): CMD = ffmpeg -loglevel info -f lavfi \
+ -i "$(LIBX264_FIELD_INPUT)" $(LIBX264_FIELD_ENC) -f null - && $(LIBX264_FIELD_TRACE)
+
+FATE_FFMPEG += $(FATE_LIBX264-yes)
+fate-enc-external: $(FATE_LIBX264-yes)
+
+FATE_LIBX264_MKV-$(call ALLYES, $(LIBX264_FIELD_DEPS) FFPROBE \
+ MATROSKA_MUXER MATROSKA_DEMUXER FILE_PROTOCOL) += fate-libx264-native-field-order \
+ fate-libx264-progressive-first-field-order
+fate-libx264-native-field-order: LIBX264_FIELD_OPTS = -x264-params tff=1
+$(FATE_LIBX264_MKV-yes): CMD = run_with_temp \
+ "$(FFMPEG) -nostdin -nostats -loglevel info -f lavfi -i $(LIBX264_FIELD_SRC) -frames:v 1 $(LIBX264_FIELD_ENC) -y" \
+ "ffprobe$(PROGSSUF)$(EXESUF) -v error -nofind_stream_info -show_entries stream=field_order -of csv=p=0" mkv && \
+ $(LIBX264_FIELD_TRACE)
+
+FATE_FFMPEG_FFPROBE += $(FATE_LIBX264_MKV-yes)
+fate-enc-external: $(FATE_LIBX264_MKV-yes)
diff --git a/tests/ref/fate/libx264-bff-pic-struct b/tests/ref/fate/libx264-bff-pic-struct
new file mode 100644
index 0000000000..0b0ad6c9c2
--- /dev/null
+++ b/tests/ref/fate/libx264-bff-pic-struct
@@ -0,0 +1,6 @@
+pic_struct=4
+delta_pic_order_cnt_bottom=-1
+pic_struct=4
+delta_pic_order_cnt_bottom=-1
+pic_struct=4
+delta_pic_order_cnt_bottom=-1
diff --git a/tests/ref/fate/libx264-native-field-order b/tests/ref/fate/libx264-native-field-order
new file mode 100644
index 0000000000..931d95d92f
--- /dev/null
+++ b/tests/ref/fate/libx264-native-field-order
@@ -0,0 +1,3 @@
+unknown
+pic_struct=3
+delta_pic_order_cnt_bottom=1
diff --git a/tests/ref/fate/libx264-progressive-first-bff b/tests/ref/fate/libx264-progressive-first-bff
new file mode 100644
index 0000000000..413c6f7f67
--- /dev/null
+++ b/tests/ref/fate/libx264-progressive-first-bff
@@ -0,0 +1,2 @@
+pic_struct=4
+delta_pic_order_cnt_bottom=-1
diff --git a/tests/ref/fate/libx264-progressive-first-field-order b/tests/ref/fate/libx264-progressive-first-field-order
new file mode 100644
index 0000000000..fe5b4fbe3a
--- /dev/null
+++ b/tests/ref/fate/libx264-progressive-first-field-order
@@ -0,0 +1,3 @@
+bt
+pic_struct=4
+delta_pic_order_cnt_bottom=-1