Commit 0944e81506 for ffmpeg
commit 0944e81506d406a232568f9ab5942d1fa6265c41
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Wed Dec 2 23:41:46 2026 -0600
avformat/matroskadec: take the codec delay off cue timestamps
A cue holds the timestamp of its block before the codec delay, while
the index built from the blocks and the skip after a seek use it after
the delay, so a seek could skip the block a cue names and resume at a
later one.
Index the cues after the delay on every track, so that the index, its
search and the skip compare presentation timestamps, and let the WebM
DASH manifest add the delay back to read the cue times it publishes.
A forward seek to a target within the codec delay of the first block
lands on that block, whose output starts after the delay.
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 9cedfb38e4..248ee21055 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -1702,6 +1702,17 @@ static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
return NULL;
}
+static MatroskaTrack *matroska_find_track_by_stream(MatroskaDemuxContext *matroska,
+ const AVStream *st)
+{
+ MatroskaTrack *tracks = matroska->tracks.elem;
+
+ for (int i = 0; i < matroska->tracks.nb_elem; i++)
+ if (tracks[i].stream == st)
+ return &tracks[i];
+ return NULL;
+}
+
static int matroska_decode_buffer(uint8_t **buf, int *buf_size,
MatroskaTrack *track)
{
@@ -2042,11 +2053,13 @@ static void matroska_add_index_entries(MatroskaDemuxContext *matroska)
pos[j].track);
if (track && track->stream) {
int64_t ts = index[i].time / index_scale;
- // A cue holds a rounded block timestamp, before the codec
- // delay; the skip after a seek absorbs the rounding.
+ // A cue holds the timestamp of its block before the codec
+ // delay, rounded on a best-fit track; the skip after a seek
+ // absorbs the rounding.
if (track->exact_audio)
ts = av_rescale_q(ts, track->tick_tb,
track->stream->time_base);
+ ts = av_sat_sub64(ts, track->codec_delay_in_track_tb);
av_add_index_entry(track->stream,
pos[j].pos + matroska->segment_start,
ts, 0, 0, AVINDEX_KEYFRAME);
@@ -4507,13 +4520,8 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf
timecode < track->end_timecode)
is_keyframe = 0; /* overlapping subtitles are not key frame */
if (is_keyframe) {
- // The index of a best-fit track holds block timestamps, as cues
- // do.
- int64_t index_ts = timecode;
- if (track->exact_audio)
- index_ts += track->codec_delay_in_track_tb;
ff_reduce_index(matroska->ctx, st->index);
- av_add_index_entry(st, cluster_pos, index_ts, 0, 0,
+ av_add_index_entry(st, cluster_pos, timecode, 0, 0,
AVINDEX_KEYFRAME);
}
}
@@ -4524,10 +4532,10 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf
// offset. We don't support timestamps greater than int64_t anyway - see
// AVPacket's pts.
int64_t block_ts = timecode;
- // A best-fit index holds block timestamps, and the seek target can
- // be a rounded cue up to ts_tolerance after the block it names.
- if (track->exact_audio && timecode != AV_NOPTS_VALUE)
- block_ts += track->codec_delay_in_track_tb + track->ts_tolerance;
+ // On a best-fit track, the seek target can be a rounded cue up to
+ // ts_tolerance after the block it names.
+ if (timecode != AV_NOPTS_VALUE)
+ block_ts = av_sat_add64(block_ts, track->ts_tolerance);
if (timecode == AV_NOPTS_VALUE ||
av_compare_ts(block_ts, st->time_base,
matroska->skip_to_timecode, matroska->skip_to_tb) < 0)
@@ -4734,7 +4742,7 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index,
int64_t timestamp, int flags)
{
MatroskaDemuxContext *matroska = s->priv_data;
- MatroskaTrack *tracks = NULL;
+ MatroskaTrack *tracks = matroska->tracks.elem, *track;
AVStream *st = s->streams[stream_index];
FFStream *const sti = ffstream(st);
int i, index;
@@ -4749,6 +4757,20 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index,
goto err;
timestamp = FFMAX(timestamp, sti->index_entries[0].timestamp);
+ // The output of the first block starts the codec delay after its
+ // timestamp, so a forward seek to a target within that delay lands on it
+ // when no other index entry comes before the end of the delay.
+ track = matroska_find_track_by_stream(matroska, st);
+ if (track && !(flags & AVSEEK_FLAG_BACKWARD)) {
+ int64_t first_output = av_sat_add64(sti->index_entries[0].timestamp,
+ track->codec_delay_in_track_tb);
+
+ index = av_index_search_timestamp(st, timestamp, flags);
+ if (timestamp <= first_output &&
+ (index < 0 || sti->index_entries[index].timestamp > first_output))
+ timestamp = sti->index_entries[0].timestamp;
+ }
+
if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0 ||
index == sti->nb_index_entries - 1) {
matroska_reset_status(matroska, 0, sti->index_entries[sti->nb_index_entries - 1].pos);
@@ -4765,7 +4787,6 @@ static int matroska_read_seek(AVFormatContext *s, int stream_index,
index == sti->nb_index_entries - 1))
goto err;
- tracks = matroska->tracks.elem;
for (i = 0; i < matroska->tracks.nb_elem; i++) {
tracks[i].audio.pkt_cnt = 0;
tracks[i].audio.sub_packet_cnt = 0;
@@ -4826,6 +4847,20 @@ typedef struct {
int64_t end_offset;
} CueDesc;
+/**
+ * The time of an index entry of the first stream on the segment clock, as
+ * cues hold it: the index holds the time with the codec delay taken off.
+ */
+static int64_t dash_index_time(AVFormatContext *s, int i)
+{
+ MatroskaDemuxContext *matroska = s->priv_data;
+ const MatroskaTrack *track = matroska_find_track_by_stream(matroska,
+ s->streams[0]);
+ int64_t ts = ffstream(s->streams[0])->index_entries[i].timestamp;
+
+ return track ? av_sat_add64(ts, track->codec_delay_in_track_tb) : ts;
+}
+
/* This function searches all the Cues and returns the CueDesc corresponding to
* the timestamp ts. Returned CueDesc will be such that start_time_ns <= ts <
* end_time_ns. All 4 fields will be set to -1 if ts >= file's duration or
@@ -4843,21 +4878,21 @@ static CueDesc get_cue_desc(AVFormatContext *s, int64_t ts, int64_t cues_start)
if (ts >= (int64_t)(matroska->duration * matroska->time_scale))
return (CueDesc) {-1, -1, -1, -1};
for (i = 1; i < nb_index_entries; i++) {
- if (index_entries[i - 1].timestamp * matroska->time_scale <= ts &&
- index_entries[i].timestamp * matroska->time_scale > ts) {
+ if (dash_index_time(s, i - 1) * matroska->time_scale <= ts &&
+ dash_index_time(s, i) * matroska->time_scale > ts) {
break;
}
}
--i;
- if (index_entries[i].timestamp > matroska->duration)
+ if (dash_index_time(s, i) > matroska->duration)
return (CueDesc) {-1, -1, -1, -1};
end_pos = i != nb_index_entries - 1 ? index_entries[i + 1].pos : cues_start;
if (index_entries[i].pos < matroska->segment_start || end_pos < matroska->segment_start)
return (CueDesc) {-1, -1, -1, -1};
- cue_desc.start_time_ns = index_entries[i].timestamp * matroska->time_scale;
+ cue_desc.start_time_ns = dash_index_time(s, i) * matroska->time_scale;
cue_desc.start_offset = index_entries[i].pos - matroska->segment_start;
if (i != nb_index_entries - 1) {
- cue_desc.end_time_ns = index_entries[i + 1].timestamp * matroska->time_scale;
+ cue_desc.end_time_ns = dash_index_time(s, i + 1) * matroska->time_scale;
cue_desc.end_offset = index_entries[i + 1].pos - matroska->segment_start;
} else {
cue_desc.end_time_ns = matroska->duration * matroska->time_scale;
@@ -4881,16 +4916,13 @@ static int webm_clusters_start_with_keyframe(AVFormatContext *s)
FFStream *const sti = ffstream(st);
uint32_t id = matroska->current_id;
int64_t cluster_pos, before_pos;
- int index, rv = 1;
+ int rv = 1;
if (sti->nb_index_entries <= 0)
return 0;
// seek to the first cluster using cues.
- index = av_index_search_timestamp(st, 0, 0);
- if (index < 0)
- return 0;
- cluster_pos = sti->index_entries[index].pos;
+ cluster_pos = sti->index_entries[0].pos;
before_pos = avio_tell(s->pb);
while (1) {
uint64_t cluster_id, cluster_length;
@@ -5016,7 +5048,7 @@ static int64_t webm_dash_manifest_compute_bandwidth(AVFormatContext *s, int64_t
for (int i = 0; i < sti->nb_index_entries; i++) {
int64_t prebuffer_ns = 1000000000;
- int64_t time_ns = sti->index_entries[i].timestamp * matroska->time_scale;
+ int64_t time_ns = dash_index_time(s, i) * matroska->time_scale;
double nano_seconds_per_second = 1000000000.0;
int64_t prebuffered_ns;
double prebuffer_bytes = 0.0;
@@ -5175,7 +5207,7 @@ static int webm_dash_manifest_cues(AVFormatContext *s, int64_t init_range)
// for checking subsegment alignment in the muxer.
av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
for (int j = 0; j < sti->nb_index_entries; j++)
- av_bprintf(&bprint, "%" PRId64",", sti->index_entries[j].timestamp);
+ av_bprintf(&bprint, "%" PRId64",", dash_index_time(s, j));
if (!av_bprint_is_complete(&bprint)) {
av_bprint_finalize(&bprint, NULL);
return AVERROR(ENOMEM);
diff --git a/tests/ref/fate/matroska-opus-cue-dash b/tests/ref/fate/matroska-opus-cue-dash
index 0b5007681a..9c5f370331 100644
--- a/tests/ref/fate/matroska-opus-cue-dash
+++ b/tests/ref/fate/matroska-opus-cue-dash
@@ -110,5 +110,5 @@
[STREAM]
TAG:webm_dash_manifest_bandwidth=60812
TAG:webm_dash_manifest_cluster_keyframe=1
-TAG:webm_dash_manifest_cue_timestamps=-7,0,233,240,474,481,713,720,953,960,1193,1200,1433,1440,1673,1680,1913,1920
+TAG:webm_dash_manifest_cue_timestamps=0,240,481,720,960,1200,1440,1680,1920
[/STREAM]
diff --git a/tests/ref/fate/matroska-opus-cue-seek b/tests/ref/fate/matroska-opus-cue-seek
index b476227dd8..639154d3fd 100644
--- a/tests/ref/fate/matroska-opus-cue-seek
+++ b/tests/ref/fate/matroska-opus-cue-seek
@@ -1,3 +1,3 @@
-packet|pts=6770|pos=8480
-packet|pts=27939|pos=15639
-packet|pts=47345|pos=21765
+packet|pts=-287|pos=5426
+packet|pts=20883|pos=13197
+packet|pts=40288|pos=20239