Commit c1b611be41 for ffmpeg
commit c1b611be41427f862707c0181041839c2b15c69f
Author: Kacper Michajłow <kasper93@gmail.com>
Date: Thu Dec 3 23:52:58 2026 +0100
avformat/dashdec: stop at the last segment of the Period
A SegmentTemplate with @duration has ceil(Period duration / segment
duration) segments. Adding that count to startNumber requested one more
file and turned the end of playback into a demuxing error. For an S
element with a negative @r the count itself was the number of the last
segment, it was 0 in a Period without @duration, and a seek did not
repeat the element.
Take the duration of a Period from @duration, the start of the next
Period or the end of the presentation (ISO/IEC 23009-1, 5.3.2.1). Keep
it in microseconds, so a short last segment survives the conversion to
segment numbers, and report it as the duration, since only this Period
is played. In a static presentation, give an S element with a negative
@r the repeat count that reaches the next S element or the end of the
Period.
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c
index 747b6f1693..72cd4f5a8c 100644
--- a/libavformat/dashdec.c
+++ b/libavformat/dashdec.c
@@ -138,7 +138,7 @@ typedef struct DASHContext {
struct representation **subtitles;
/* MediaPresentationDescription Attribute */
- int64_t media_presentation_duration;
+ int64_t media_presentation_duration; /* in AV_TIME_BASE units */
int64_t suggested_presentation_delay;
int64_t availability_start_time;
int64_t availability_end_time;
@@ -148,8 +148,7 @@ typedef struct DASHContext {
int64_t min_buffer_time;
/* Period Attribute */
- uint64_t period_duration;
- uint64_t period_start;
+ int64_t period_duration; /* in AV_TIME_BASE units */
/* AdaptationSet Attribute */
char *adaptionset_lang;
@@ -221,15 +220,12 @@ static int64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
return av_timegm(&timeinfo);
}
-static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
+static int64_t get_duration_us(AVFormatContext *s, const char *duration, int64_t fallback)
{
/* ISO-8601 duration parser */
- uint32_t days = 0;
- uint32_t hours = 0;
- uint32_t mins = 0;
- uint32_t secs = 0;
+ double seconds = 0;
int size = 0;
- float value = 0;
+ double value;
char type = '\0';
const char *ptr = duration;
@@ -239,22 +235,20 @@ static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
continue;
}
- if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
- av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
- return 0; /* parser error */
- }
+ if (av_sscanf(ptr, "%lf%c%n", &value, &type, &size) != 2 || value < 0)
+ goto invalid;
switch (type) {
case 'D':
- days = (uint32_t)value;
+ seconds += value * 86400;
break;
case 'H':
- hours = (uint32_t)value;
+ seconds += value * 3600;
break;
case 'M':
- mins = (uint32_t)value;
+ seconds += value * 60;
break;
case 'S':
- secs = (uint32_t)value;
+ seconds += value;
break;
default:
// handle invalid type
@@ -262,7 +256,11 @@ static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
}
ptr += size;
}
- return ((days * 24 + hours) * 60 + mins) * 60 + secs;
+ if (seconds * AV_TIME_BASE < 0x1p63)
+ return llrint(seconds * AV_TIME_BASE);
+invalid:
+ av_log(s, AV_LOG_WARNING, "Invalid DASH duration '%s'\n", duration);
+ return fallback;
}
static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
@@ -1134,6 +1132,26 @@ static int parse_manifest_representation(AVFormatContext *s, const char *url,
goto free;
}
+ if (!c->is_live) {
+ /* A negative repeat count lasts until the next S element or the end of the Period. */
+ int64_t period_ticks = av_rescale_rnd(c->period_duration, rep->fragment_timescale,
+ AV_TIME_BASE, AV_ROUND_UP);
+ int64_t period_end = av_sat_add64(rep->presentation_timeoffset, period_ticks);
+ int64_t start = 0;
+ for (int i = 0; i < rep->n_timelines; i++) {
+ struct timeline *tml = rep->timelines[i];
+ if (tml->starttime > 0)
+ start = tml->starttime;
+ if (tml->repeat < 0) {
+ int64_t end = i + 1 < rep->n_timelines && rep->timelines[i + 1]->starttime > 0 ?
+ rep->timelines[i + 1]->starttime : period_end;
+ tml->repeat = tml->duration > 0 && end > start ?
+ ((uint64_t)end - start - 1) / tml->duration : 0;
+ }
+ start += tml->duration * (tml->repeat + 1ULL);
+ }
+ }
+
int64_t nb_timeline_segments = 0;
for (int i = 0; i < rep->n_timelines; i++) {
nb_timeline_segments += av_clip64(rep->timelines[i]->repeat, 0, INT_MAX) + 1;
@@ -1304,6 +1322,23 @@ static int parse_programinformation(AVFormatContext *s, xmlNodePtr node)
return 0;
}
+/* A Period without @duration lasts until the next one starts or the presentation ends. */
+static int64_t get_period_end(AVFormatContext *s, xmlNodePtr period)
+{
+ DASHContext *c = s->priv_data;
+ int64_t end = c->media_presentation_duration;
+
+ while ((period = xmlNextElementSibling(period))) {
+ if (!av_strcasecmp(period->name, "Period")) {
+ char *val = xmlGetProp(period, "start");
+ end = val ? get_duration_us(s, val, 0) : 0;
+ xmlFree(val);
+ break;
+ }
+ }
+ return end;
+}
+
static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
{
DASHContext *c = s->priv_data;
@@ -1323,8 +1358,6 @@ static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
xmlNodePtr adaptionset_node = NULL;
xmlAttrPtr attr = NULL;
char *val = NULL;
- uint32_t period_duration_sec = 0;
- uint32_t period_start_sec = 0;
if (!in) {
close_in = 1;
@@ -1402,19 +1435,19 @@ static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
c->publish_time = get_utc_date_time_insec(s, val);
av_log(s, AV_LOG_TRACE, "c->publish_time = [%"PRId64"]\n", c->publish_time);
} else if (!av_strcasecmp(attr->name, "minimumUpdatePeriod")) {
- c->minimum_update_period = get_duration_insec(s, val);
+ c->minimum_update_period = get_duration_us(s, val, 0) / AV_TIME_BASE;
av_log(s, AV_LOG_TRACE, "c->minimum_update_period = [%"PRId64"]\n", c->minimum_update_period);
} else if (!av_strcasecmp(attr->name, "timeShiftBufferDepth")) {
- c->time_shift_buffer_depth = get_duration_insec(s, val);
+ c->time_shift_buffer_depth = get_duration_us(s, val, 0) / AV_TIME_BASE;
av_log(s, AV_LOG_TRACE, "c->time_shift_buffer_depth = [%"PRId64"]\n", c->time_shift_buffer_depth);
} else if (!av_strcasecmp(attr->name, "minBufferTime")) {
- c->min_buffer_time = get_duration_insec(s, val);
+ c->min_buffer_time = get_duration_us(s, val, 0) / AV_TIME_BASE;
av_log(s, AV_LOG_TRACE, "c->min_buffer_time = [%"PRId64"]\n", c->min_buffer_time);
} else if (!av_strcasecmp(attr->name, "suggestedPresentationDelay")) {
- c->suggested_presentation_delay = get_duration_insec(s, val);
+ c->suggested_presentation_delay = get_duration_us(s, val, 0) / AV_TIME_BASE;
av_log(s, AV_LOG_TRACE, "c->suggested_presentation_delay = [%"PRId64"]\n", c->suggested_presentation_delay);
} else if (!av_strcasecmp(attr->name, "mediaPresentationDuration")) {
- c->media_presentation_duration = get_duration_insec(s, val);
+ c->media_presentation_duration = get_duration_us(s, val, 0);
av_log(s, AV_LOG_TRACE, "c->media_presentation_duration = [%"PRId64"]\n", c->media_presentation_duration);
}
attr = attr->next;
@@ -1429,11 +1462,13 @@ static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
}
// at now we can handle only one period, with the longest duration
+ int64_t period_end = 0;
node = xmlFirstElementChild(node);
while (node) {
if (!av_strcasecmp(node->name, "Period")) {
- period_duration_sec = 0;
- period_start_sec = 0;
+ int64_t period_duration = -1;
+ int64_t period_start = period_end;
+
attr = node->properties;
while (attr) {
val = xmlGetProp(node, attr->name);
@@ -1442,19 +1477,20 @@ static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
goto cleanup;
}
if (!av_strcasecmp(attr->name, "duration")) {
- period_duration_sec = get_duration_insec(s, val);
+ period_duration = get_duration_us(s, val, -1);
} else if (!av_strcasecmp(attr->name, "start")) {
- period_start_sec = get_duration_insec(s, val);
+ period_start = get_duration_us(s, val, period_start);
}
attr = attr->next;
xmlFree(val);
}
- if ((period_duration_sec) >= (c->period_duration)) {
+ if (period_duration < 0)
+ period_duration = c->is_live ? 0 :
+ FFMAX(get_period_end(s, node) - period_start, 0);
+ period_end = av_sat_add64(period_start, period_duration);
+ if (period_duration >= c->period_duration) {
period_node = node;
- c->period_duration = period_duration_sec;
- c->period_start = period_start_sec;
- if (c->period_start > 0)
- c->media_presentation_duration = c->period_duration;
+ c->period_duration = period_duration;
}
} else if (!av_strcasecmp(node->name, "ProgramInformation")) {
parse_programinformation(s, node);
@@ -1555,7 +1591,8 @@ static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
num = pls->first_seq_no + pls->n_timelines - 1;
for (i = 0; i < pls->n_timelines; i++) {
if (pls->timelines[i]->repeat == -1) {
- num = pls->timelines[i]->duration ? av_rescale(c->period_duration, pls->fragment_timescale, pls->timelines[i]->duration) : pls->first_seq_no;
+ int64_t end = av_rescale(c->period_duration, pls->fragment_timescale, AV_TIME_BASE);
+ num = pls->timelines[i]->duration ? av_rescale(end, 1, pls->timelines[i]->duration) : pls->first_seq_no;
} else {
num += pls->timelines[i]->repeat;
}
@@ -1563,7 +1600,13 @@ static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
} else if (c->is_live && pls->fragment_duration) {
num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
} else if (pls->fragment_duration) {
- num = pls->first_seq_no + av_rescale_rnd(1, c->media_presentation_duration * pls->fragment_timescale, pls->fragment_duration, AV_ROUND_UP);
+ int64_t end = av_rescale_rnd(c->period_duration, pls->fragment_timescale,
+ AV_TIME_BASE, AV_ROUND_UP);
+
+ /* The last segment contains the tick just before the end of the Period. */
+ num = pls->first_seq_no;
+ if (end > 0)
+ num = av_sat_add64(num, (end - 1) / pls->fragment_duration);
}
return num;
@@ -2314,7 +2357,7 @@ static int dash_read_header(AVFormatContext *s)
/* If this isn't a live stream, fill the total duration of the
* stream. */
if (!c->is_live) {
- s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
+ s->duration = c->period_duration;
} else {
av_dict_set(&c->avio_opts, "seekable", "0", 0);
}