Commit 6f68427c24 for ffmpeg
commit 6f68427c2496041613ac5a5189359f1a7778cd87
Author: Kacper Michajłow <kasper93@gmail.com>
Date: Thu Dec 3 17:45:54 2026 +0100
avformat/dashdec: resume representations at the playback position
A discarded representation loses its position. When it was enabled
again, it started at the beginning of its file, or at the segment number
of another representation of its type, which is another time or no
segment at all when the numbering differs.
Seek it with dash_seek() to the last seek target, or to the time all
open representations have been read to.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c
index b6a276388f..9d4fc70ad3 100644
--- a/libavformat/dashdec.c
+++ b/libavformat/dashdec.c
@@ -155,6 +155,8 @@ typedef struct DASHContext {
char *adaptionset_lang;
int is_live;
+ int64_t resume_timestamp; /* time in the Period in AV_TIME_BASE units */
+ int resume_flags;
AVIOInterruptCB *interrupt_callback;
char *allowed_extensions;
AVDictionary *avio_opts;
@@ -2037,8 +2039,11 @@ static void close_demux_for_component(struct representation *pls)
/* note: the internal buffer could have changed */
av_freep(&pls->pb.pub.buffer);
memset(&pls->pb, 0x00, sizeof(pls->pb));
- pls->ctx->pb = NULL;
- avformat_close_input(&pls->ctx);
+ if (pls->ctx) {
+ pls->ctx->pb = NULL;
+ avformat_close_input(&pls->ctx);
+ }
+ ff_format_io_close(pls->parent, &pls->input);
pls->eof = 0;
}
@@ -2050,9 +2055,7 @@ static int reopen_demux_for_component(AVFormatContext *s, struct representation
uint8_t *avio_ctx_buffer = NULL;
int ret = 0, i;
- if (pls->ctx) {
- close_demux_for_component(pls);
- }
+ close_demux_for_component(pls);
if (ff_check_interrupt(&s->interrupt_callback)) {
ret = AVERROR_EXIT;
@@ -2067,20 +2070,14 @@ static int reopen_demux_for_component(AVFormatContext *s, struct representation
avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
if (!avio_ctx_buffer ) {
ret = AVERROR(ENOMEM);
- avformat_free_context(pls->ctx);
- pls->ctx = NULL;
goto fail;
}
- av_freep(&pls->pb.pub.buffer);
ffio_init_context(&pls->pb, avio_ctx_buffer, INITIAL_BUFFER_SIZE, 0,
pls, read_data, NULL, c->is_live ? NULL : seek_data);
pls->pb.pub.seekable = 0;
- if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0) {
- avformat_free_context(pls->ctx);
- pls->ctx = NULL;
+ if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
goto fail;
- }
pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
@@ -2089,8 +2086,6 @@ static int reopen_demux_for_component(AVFormatContext *s, struct representation
ret = av_probe_input_buffer(&pls->pb.pub, &in_fmt, "", NULL, 0, 0);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Error when loading first fragment of playlist\n");
- avformat_free_context(pls->ctx);
- pls->ctx = NULL;
goto fail;
}
@@ -2122,6 +2117,11 @@ static int reopen_demux_for_component(AVFormatContext *s, struct representation
}
fail:
+ if (ret < 0) {
+ close_demux_for_component(pls);
+ /* Only a live presentation can have something else to read on the next attempt. */
+ pls->eof = !c->is_live;
+ }
return ret;
}
@@ -2262,6 +2262,8 @@ static int dash_read_header(AVFormatContext *s)
int i, j;
c->interrupt_callback = &s->interrupt_callback;
+ /* Until a packet is read or a seek is done, representations resume at their start. */
+ c->resume_flags = AVSEEK_FLAG_BACKWARD;
if ((ret = ffio_copy_url_options(s->pb, &c->avio_opts)) < 0)
return ret;
@@ -2436,72 +2438,87 @@ static int representation_needed(const struct representation *pls)
return needed;
}
+static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos, int flags);
+
static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
{
- int i, j;
+ DASHContext *c = s->priv_data;
- for (i = 0; i < n; i++) {
+ for (int i = 0; i < n; i++) {
struct representation *pls = p[i];
int needed = representation_needed(pls);
- if (needed && !pls->ctx) {
- pls->cur_seg_offset = 0;
- pls->init_sec_buf_read_offset = 0;
- /* Catch up */
- for (j = 0; j < n; j++) {
- pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
+ if (needed && !pls->ctx && !pls->eof) {
+ if (c->is_live) {
+ pls->cur_seg_offset = 0;
+ pls->init_sec_buf_read_offset = 0;
+ /* Catch up */
+ for (int j = 0; j < n; j++)
+ pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
+ reopen_demux_for_component(s, pls);
+ } else if (dash_seek(s, pls, c->resume_timestamp, c->resume_flags) < 0) {
+ av_log(s, AV_LOG_WARNING, "Failed to receive stream_index %d\n", pls->stream_index);
+ continue;
}
- reopen_demux_for_component(s, pls);
av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
} else if (!needed && pls->ctx) {
close_demux_for_component(pls);
- ff_format_io_close(pls->parent, &pls->input);
av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
}
}
}
+static int64_t rescale_pos(int64_t pos, int64_t b, int64_t c, enum AVRounding rnd)
+{
+ int64_t ret = av_rescale_rnd(pos, b, c, rnd);
+ return ret == INT64_MIN && pos > 0 ? INT64_MAX : ret;
+}
+
+static int64_t presentation_timeoffset_us(const struct representation *pls)
+{
+ if (!pls->fragment_timescale)
+ return 0;
+ return rescale_pos(pls->presentation_timeoffset, AV_TIME_BASE, pls->fragment_timescale,
+ AV_ROUND_DOWN);
+}
+
+static struct representation *get_representation(DASHContext *c, int i)
+{
+ if (i < c->n_videos)
+ return c->videos[i];
+ i -= c->n_videos;
+ if (i < c->n_audios)
+ return c->audios[i];
+ i -= c->n_audios;
+ return i < c->n_subtitles ? c->subtitles[i] : NULL;
+}
+
+static struct representation *get_next_representation(DASHContext *c)
+{
+ struct representation *cur = NULL, *rep;
+
+ for (int i = 0; (rep = get_representation(c, i)); i++)
+ if (rep->ctx && !rep->eof && (!cur || rep->cur_timestamp < cur->cur_timestamp))
+ cur = rep;
+ return cur;
+}
+
static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
{
DASHContext *c = s->priv_data;
- int ret = 0, i;
- int64_t mints = 0;
- struct representation *cur = NULL;
- struct representation *rep = NULL;
+ int ret = 0;
+
+ struct representation *cur = get_next_representation(c);
+ if (cur && cur->cur_timestamp > c->resume_timestamp) {
+ c->resume_timestamp = cur->cur_timestamp;
+ c->resume_flags = AVSEEK_FLAG_BACKWARD;
+ }
recheck_discard_flags(s, c->videos, c->n_videos);
recheck_discard_flags(s, c->audios, c->n_audios);
recheck_discard_flags(s, c->subtitles, c->n_subtitles);
- for (i = 0; i < c->n_videos; i++) {
- rep = c->videos[i];
- if (!rep->ctx || rep->eof)
- continue;
- if (!cur || rep->cur_timestamp < mints) {
- cur = rep;
- mints = rep->cur_timestamp;
- }
- }
- for (i = 0; i < c->n_audios; i++) {
- rep = c->audios[i];
- if (!rep->ctx || rep->eof)
- continue;
- if (!cur || rep->cur_timestamp < mints) {
- cur = rep;
- mints = rep->cur_timestamp;
- }
- }
-
- for (i = 0; i < c->n_subtitles; i++) {
- rep = c->subtitles[i];
- if (!rep->ctx || rep->eof)
- continue;
- if (!cur || rep->cur_timestamp < mints) {
- cur = rep;
- mints = rep->cur_timestamp;
- }
- }
-
+ cur = get_next_representation(c);
if (!cur) {
return AVERROR_EOF;
}
@@ -2509,7 +2526,11 @@ static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
ret = av_read_frame(cur->ctx, pkt);
if (ret >= 0) {
/* If we got a packet, return it */
- cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
+ AVRational tb = cur->ctx->streams[pkt->stream_index]->time_base;
+ int64_t ts = pkt->pts != AV_NOPTS_VALUE ? pkt->pts : pkt->dts;
+ if (ts != AV_NOPTS_VALUE)
+ cur->cur_timestamp = av_sat_sub64(av_rescale_q(ts, tb, AV_TIME_BASE_Q),
+ presentation_timeoffset_us(cur));
pkt->stream_index += cur->stream_index;
return 0;
}
@@ -2540,15 +2561,19 @@ static int dash_close(AVFormatContext *s)
return 0;
}
-static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
+static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos, int flags)
{
+ int dry_run = !representation_needed(pls);
int ret = 0;
int i = 0;
int j = 0;
int64_t duration = 0;
- av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms] %s\n",
- seek_pos_msec, dry_run ? " (dry)" : "");
+ av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"us] %s\n",
+ seek_pos, dry_run ? " (dry)" : "");
+
+ if (dry_run)
+ close_demux_for_component(pls);
// single fragment mode
if (pls->n_fragments == 1) {
@@ -2558,20 +2583,26 @@ static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t see
if (dry_run)
return 0;
if (!pls->ctx) {
+ pls->cur_seq_no = pls->first_seq_no;
pls->init_sec_buf_read_offset = 0;
ret = reopen_demux_for_component(s, pls);
if (ret < 0)
return ret;
}
ff_read_frame_flush(pls->ctx);
- return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
+ ret = av_seek_frame(pls->ctx, -1, seek_pos, flags);
+ /* Nothing can be read at a position the demuxer cannot seek to. */
+ pls->eof = ret < 0;
+ return ret;
}
ff_format_io_close(pls->parent, &pls->input);
// find the nearest fragment
+ int64_t pos = rescale_pos(seek_pos, pls->fragment_timescale, AV_TIME_BASE, AV_ROUND_DOWN);
if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
int64_t num = pls->first_seq_no;
+ pos = av_sat_add64(pos, pls->presentation_timeoffset);
av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
"last_seq_no[%"PRId64"].\n",
(int)pls->n_timelines, (int64_t)pls->last_seq_no);
@@ -2580,13 +2611,13 @@ static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t see
duration = pls->timelines[i]->starttime;
}
duration += pls->timelines[i]->duration;
- if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
+ if (pos < duration) {
goto set_seq_num;
}
for (j = 0; j < pls->timelines[i]->repeat; j++) {
duration += pls->timelines[i]->duration;
num++;
- if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
+ if (pos < duration) {
goto set_seq_num;
}
}
@@ -2598,7 +2629,7 @@ set_seq_num:
av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"].\n",
(int64_t)pls->cur_seq_no);
} else if (pls->fragment_duration > 0) {
- pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
+ pls->cur_seq_no = av_sat_add64(pls->first_seq_no, pos / pls->fragment_duration);
pls->cur_seq_no = FFMAX(pls->cur_seq_no, pls->first_seq_no);
} else {
av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
@@ -2616,24 +2647,34 @@ static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestam
{
int ret = 0, i;
DASHContext *c = s->priv_data;
- int64_t seek_pos_msec = av_rescale_q_rnd(timestamp, s->streams[stream_index]->time_base,
- (AVRational){ 1, 1000 }, flags & AVSEEK_FLAG_BACKWARD ?
- AV_ROUND_DOWN : AV_ROUND_UP);
+ AVRational tb = s->streams[stream_index]->time_base;
+ int64_t seek_pos = rescale_pos(timestamp, tb.num * (int64_t)AV_TIME_BASE, tb.den,
+ flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
return AVERROR(ENOSYS);
- /* Seek in discarded streams with dry_run=1 to avoid reopening them */
+ struct representation *rep;
+ for (i = 0; (rep = get_representation(c, i)); i++)
+ if (stream_index < rep->stream_index + rep->nb_assoc_stream)
+ break;
+ seek_pos = av_sat_sub64(seek_pos, presentation_timeoffset_us(rep));
+
for (i = 0; i < c->n_videos; i++) {
if (!ret)
- ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx && !representation_needed(c->videos[i]));
+ ret = dash_seek(s, c->videos[i], seek_pos, flags);
}
for (i = 0; i < c->n_audios; i++) {
if (!ret)
- ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx && !representation_needed(c->audios[i]));
+ ret = dash_seek(s, c->audios[i], seek_pos, flags);
}
for (i = 0; i < c->n_subtitles; i++) {
if (!ret)
- ret = dash_seek(s, c->subtitles[i], seek_pos_msec, flags, !c->subtitles[i]->ctx && !representation_needed(c->subtitles[i]));
+ ret = dash_seek(s, c->subtitles[i], seek_pos, flags);
+ }
+
+ if (!ret) {
+ c->resume_timestamp = seek_pos;
+ c->resume_flags = flags;
}
return ret;