Commit 1dbd0bd561 for ffmpeg
commit 1dbd0bd5610462129d1da4a1c6def82309298a22
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Sun Nov 22 22:06:32 2026 -0800
avcodec/h264_parser: let field POCs decide the field order
H.264 Table D-1 requires pic_struct 3-6 to agree with the field POCs.
When a stream breaks this, use the POCs, as the decoder does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index 87228fb63a..ab7bf007f9 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -626,6 +626,11 @@ static inline int parse_nal_units(AVCodecParserContext *s,
s->field_order = AV_FIELD_PROGRESSIVE;
break;
}
+ /* Field POCs take precedence, as in the decoder. */
+ if (s->field_order != AV_FIELD_PROGRESSIVE &&
+ field_poc[0] != field_poc[1])
+ s->field_order = field_poc[0] < field_poc[1] ?
+ AV_FIELD_TT : AV_FIELD_BB;
} else {
if (field_poc[0] < field_poc[1])
s->field_order = AV_FIELD_TT;
diff --git a/tests/fate/h264.mak b/tests/fate/h264.mak
index 325d1733f5..fcc3006b40 100644
--- a/tests/fate/h264.mak
+++ b/tests/fate/h264.mak
@@ -262,7 +262,9 @@ FATE_H264_FFPROBE-$(call PARSERDEMDEC, H264, H264, H264) += fate-h264-skip-pred
FATE_SAMPLES_AVCONV += $(FATE_H264-yes)
FATE_SAMPLES_FFPROBE += $(FATE_H264_FFPROBE-yes)
-fate-h264: $(FATE_H264-yes) $(FATE_H264_FFPROBE-yes)
+FATE_H264_GENERATED-$(call ALLYES, FFPROBE H264_DEMUXER H264_PARSER H264_DECODER FILE_PROTOCOL) += fate-h264-field-order-poc
+FATE_FFPROBE += $(FATE_H264_GENERATED-yes)
+fate-h264: $(FATE_H264-yes) $(FATE_H264_FFPROBE-yes) $(FATE_H264_GENERATED-yes)
fate-h264-conformance-aud_mw_e: CMD = framecrc -i $(TARGET_SAMPLES)/h264-conformance/AUD_MW_E.264
@@ -531,3 +533,17 @@ FATE_SAMPLES_DUMP_DATA += fate-h264-encparams
fate-h264-twofields-packet-probe: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -bitexact -v 0 -select_streams v \
-show_entries stream=field_order,avg_frame_rate \
$(TARGET_SAMPLES)/h264/twofields_packet.mp4
+
+# SPS, PPS, picture timing SEI (pic_struct 3) and a 16x32 MBAFF IDR
+# with delta_pic_order_cnt_bottom = -1. The parser must report BFF.
+tests/data/h264-pic-struct-mismatch.h264: TAG = GEN
+tests/data/h264-pic-struct-mismatch.h264: | tests/data
+ $(M){ printf '\000\000\000\001\147\144\000\025\254\331\133\140\042\000\000\003\000\002\000\000\003\000\144\076\050\123\054' && \
+ printf '\000\000\000\001\150\373\243\313\042\300' && \
+ printf '\000\000\000\001\006\001\001\062\200' && \
+ printf '\000\000\000\001\145\210\202\013\002\277\366\366\163\174\012\153\155\260\365\037'; } > $@
+
+fate-h264-field-order-poc: tests/data/h264-pic-struct-mismatch.h264
+fate-h264-field-order-poc: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -v error -bitexact \
+ -show_entries stream=field_order:frame=interlaced_frame,top_field_first -of compact \
+ $(TARGET_PATH)/tests/data/h264-pic-struct-mismatch.h264
diff --git a/tests/ref/fate/h264-field-order-poc b/tests/ref/fate/h264-field-order-poc
new file mode 100644
index 0000000000..98b66529e5
--- /dev/null
+++ b/tests/ref/fate/h264-field-order-poc
@@ -0,0 +1,2 @@
+frame|interlaced_frame=1|top_field_first=0
+stream|field_order=bb