Commit ca29ef1245 for ffmpeg
commit ca29ef1245b09723214179022f76e648a430710c
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Thu Dec 3 00:30:32 2026 -0600
avcodec/h264_parser: report full pictures after recovery points
Track, as the decoder does, whether the pictures parsed so far are
whole: from an IDR picture on, or from the first reference picture whose
frame_num is recovery_frame_cnt after that of a recovery point.
A seek with the pre-roll in MP4 then lands on the intra refresh
recovery point before the target when the one before it recovers after
it.
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index ab7bf007f9..06ac42c014 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -67,8 +67,44 @@ typedef struct H264ParseContext {
int parse_last_mb;
int64_t reference_dts;
int last_frame_num, last_picture_structure;
+ int recovered;
+ /* The frame_num of the pending recovery point, -1 when none is
+ * pending. */
+ int recovery_frame;
} H264ParseContext;
+/**
+ * Track recovery as the decoder does: a picture is whole from an IDR picture
+ * on, or from the reference picture whose frame_num is recovery_frame_cnt
+ * after that of the recovery point.
+ */
+static void update_recovery(H264ParseContext *p, const H2645NAL *nal,
+ const SPS *sps)
+{
+ int frame_num = p->poc.frame_num;
+ int cnt = p->sei.recovery_point.recovery_frame_cnt;
+ int bits = sps->log2_max_frame_num;
+
+ if (nal->type == H264_NAL_IDR_SLICE) {
+ p->recovered = 1;
+ p->recovery_frame = -1;
+ return;
+ }
+ if (p->recovered == 1)
+ return;
+ // A nearer recovery point shortens a pending one.
+ if (cnt >= 0 &&
+ (p->recovery_frame < 0 ||
+ av_zero_extend(p->recovery_frame - frame_num, bits) > cnt)) {
+ p->recovery_frame = av_zero_extend(frame_num + cnt, bits);
+ p->recovered = 0;
+ }
+ if (p->recovery_frame == frame_num && nal->ref_idc) {
+ p->recovered = 1;
+ p->recovery_frame = -1;
+ }
+}
+
static int find_start_code(const uint8_t *buf, int buf_size,
int buf_index, int next_avc)
{
@@ -477,6 +513,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
s->key_frame = 1;
p->poc.frame_num = get_bits(&nal.gb, sps->log2_max_frame_num);
+ update_recovery(p, &nal, sps);
s->coded_width = 16 * sps->mb_width;
s->coded_height = 16 * sps->mb_height;
@@ -786,12 +823,21 @@ static av_cold void h264_close(AVCodecParserContext *s)
ff_h264_ps_uninit(&p->ps);
}
+static int h264_full_picture(const AVCodecParserContext *s)
+{
+ const H264ParseContext *p = s->priv_data;
+
+ return p->recovered;
+}
+
static av_cold int init(AVCodecParserContext *s)
{
H264ParseContext *p = s->priv_data;
p->reference_dts = AV_NOPTS_VALUE;
p->last_frame_num = INT_MAX;
+ p->recovered = -1;
+ p->recovery_frame = -1;
ff_h264dsp_init(&p->h264dsp, 8, 1);
return 0;
}
@@ -802,4 +848,5 @@ const FFCodecParser ff_h264_parser = {
.init = init,
.parse = h264_parse,
.close = h264_close,
+ .full_picture = h264_full_picture,
};
diff --git a/tests/ref/fate/mov-mp4-h264-intra-refresh-seek b/tests/ref/fate/mov-mp4-h264-intra-refresh-seek
index 84d3f0f22f..ab0295a1e9 100644
--- a/tests/ref/fate/mov-mp4-h264-intra-refresh-seek
+++ b/tests/ref/fate/mov-mp4-h264-intra-refresh-seek
@@ -5,8 +5,8 @@ ffc8fe11d654e4b09676e7623c9ba175 *tests/data/fate/mov-mp4-h264-intra-refresh-see
#codec_id 0: rawvideo
#dimensions 0: 320x240
#sar 0: 0/1
-0, 33, 33, 1, 115200, 0xee02e02d
-0, 36, 36, 1, 115200, 0xd2b2f951
-0, 37, 37, 1, 115200, 0x26b7fa89
-0, 38, 38, 1, 115200, 0x93fa0031
-0, 39, 39, 1, 115200, 0xde98de0d
+0, 1, 1, 1, 115200, 0x4e3a48f2
+0, 2, 2, 1, 115200, 0x84b01583
+0, 3, 3, 1, 115200, 0xdbe6fc01
+0, 4, 4, 1, 115200, 0x23e10206
+0, 5, 5, 1, 115200, 0x4d7004f0
diff --git a/tests/ref/fate/mov-mp4-h264-intra-refresh-seek-large-sei b/tests/ref/fate/mov-mp4-h264-intra-refresh-seek-large-sei
index 35a43555f3..0267ef2c78 100644
--- a/tests/ref/fate/mov-mp4-h264-intra-refresh-seek-large-sei
+++ b/tests/ref/fate/mov-mp4-h264-intra-refresh-seek-large-sei
@@ -5,5 +5,5 @@
#codec_id 0: rawvideo
#dimensions 0: 320x240
#sar 0: 0/1
-0, 33, 33, 1, 115200, 0xee02e02d
-0, 36, 36, 1, 115200, 0xd2b2f951
+0, 1, 1, 1, 115200, 0x4e3a48f2
+0, 2, 2, 1, 115200, 0x84b01583
diff --git a/tests/ref/fate/mov-mp4-h264-intra-refresh-seek-two-tracks b/tests/ref/fate/mov-mp4-h264-intra-refresh-seek-two-tracks
index ec78f52b90..ad39f64b98 100644
--- a/tests/ref/fate/mov-mp4-h264-intra-refresh-seek-two-tracks
+++ b/tests/ref/fate/mov-mp4-h264-intra-refresh-seek-two-tracks
@@ -5,9 +5,9 @@
#codec_id 0: rawvideo
#dimensions 0: 320x240
#sar 0: 0/1
-0, 33, 33, 1, 115200, 0xee02e02d
-0, 36, 36, 1, 115200, 0xd2b2f951
-packet|stream_index=0|pts=4896000|flags=K__
-packet|stream_index=1|pts=4896000|flags=K__
-packet|stream_index=0|pts=4944000|flags=___
-packet|stream_index=1|pts=4944000|flags=___
+0, 1, 1, 1, 115200, 0x4e3a48f2
+0, 2, 2, 1, 115200, 0x84b01583
+packet|stream_index=0|pts=2352000|flags=K__
+packet|stream_index=1|pts=2352000|flags=K__
+packet|stream_index=0|pts=2400000|flags=___
+packet|stream_index=1|pts=2400000|flags=___