Skip to content

Commit 1890529

Browse files
author
wm4
committed
demux: get rid of DEMUXER_CTRL_GET_TIME_LENGTH
Similar purpose as f34e1a0. Somehow this is much more natural too, and needs less code. This breaks runtime updates to duration. This could easily be fixed, but no important demuxer does this anyway. Only demux_raw and demux_disc might (the latter for BD/DVD). For the latter it might actually have some importance when changing titles at runtime (I guess?), but guess what, I don't care.
1 parent 5bfbe6d commit 1890529

12 files changed

+52
-155
lines changed

demux/demux.c

+1-21
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ struct demux_internal {
173173

174174
// Cached state.
175175
bool force_cache_update;
176-
double time_length;
177176
struct mp_tags *stream_metadata;
178177
struct stream_cache_info stream_cache_info;
179178
int64_t stream_size;
@@ -1085,6 +1084,7 @@ static void demux_copy(struct demuxer *dst, struct demuxer *src)
10851084
dst->ts_resets_possible = src->ts_resets_possible;
10861085
dst->fully_read = src->fully_read;
10871086
dst->start_time = src->start_time;
1087+
dst->duration = src->duration;
10881088
dst->is_network = src->is_network;
10891089
dst->priv = src->priv;
10901090
}
@@ -1572,36 +1572,21 @@ int demuxer_add_chapter(demuxer_t *demuxer, char *name,
15721572
return demuxer->num_chapters - 1;
15731573
}
15741574

1575-
double demuxer_get_time_length(struct demuxer *demuxer)
1576-
{
1577-
double len;
1578-
if (demux_control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, &len) > 0)
1579-
return len;
1580-
return -1;
1581-
}
1582-
15831575
// must be called not locked
15841576
static void update_cache(struct demux_internal *in)
15851577
{
15861578
struct demuxer *demuxer = in->d_thread;
15871579
struct stream *stream = demuxer->stream;
15881580

15891581
// Don't lock while querying the stream.
1590-
double time_length = -1;
15911582
struct mp_tags *stream_metadata = NULL;
15921583
struct stream_cache_info stream_cache_info = {.size = -1};
15931584

1594-
if (demuxer->desc->control) {
1595-
demuxer->desc->control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH,
1596-
&time_length);
1597-
}
1598-
15991585
int64_t stream_size = stream_get_size(stream);
16001586
stream_control(stream, STREAM_CTRL_GET_METADATA, &stream_metadata);
16011587
stream_control(stream, STREAM_CTRL_GET_CACHE_INFO, &stream_cache_info);
16021588

16031589
pthread_mutex_lock(&in->lock);
1604-
in->time_length = time_length;
16051590
in->stream_size = stream_size;
16061591
in->stream_cache_info = stream_cache_info;
16071592
if (stream_metadata) {
@@ -1645,11 +1630,6 @@ static int cached_stream_control(struct demux_internal *in, int cmd, void *arg)
16451630
static int cached_demux_control(struct demux_internal *in, int cmd, void *arg)
16461631
{
16471632
switch (cmd) {
1648-
case DEMUXER_CTRL_GET_TIME_LENGTH:
1649-
if (in->time_length < 0)
1650-
return CONTROL_FALSE;
1651-
*(double *)arg = in->time_length;
1652-
return CONTROL_OK;
16531633
case DEMUXER_CTRL_STREAM_CTRL: {
16541634
struct demux_ctrl_stream_ctrl *c = arg;
16551635
int r = cached_stream_control(in, c->ctrl, c->arg);

demux/demux.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
enum demux_ctrl {
3434
DEMUXER_CTRL_SWITCHED_TRACKS = 1,
35-
DEMUXER_CTRL_GET_TIME_LENGTH,
3635
DEMUXER_CTRL_RESYNC,
3736
DEMUXER_CTRL_IDENTIFY_PROGRAM,
3837
DEMUXER_CTRL_STREAM_CTRL,
@@ -175,6 +174,7 @@ typedef struct demuxer {
175174
bool seekable;
176175
bool partially_seekable; // true if _maybe_ seekable; implies seekable=true
177176
double start_time;
177+
double duration; // -1 if unknown
178178
// File format allows PTS resets (even if the current file is without)
179179
bool ts_resets_possible;
180180
// The file data was fully read, and there is no need to keep the stream
@@ -280,8 +280,6 @@ int demuxer_add_chapter(demuxer_t *demuxer, char *name,
280280
void demux_set_stream_tags(struct demuxer *demuxer, struct sh_stream *sh,
281281
struct mp_tags *tags);
282282

283-
double demuxer_get_time_length(struct demuxer *demuxer);
284-
285283
int demux_stream_control(demuxer_t *demuxer, int ctrl, void *arg);
286284

287285
void demux_changed(demuxer_t *demuxer, int events);

demux/demux_cue.c

+1-15
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,6 @@ static bool open_source(struct timeline *tl, char *filename)
130130
return res;
131131
}
132132

133-
// return length of the source in seconds, or -1 if unknown
134-
static double source_get_length(struct demuxer *demuxer)
135-
{
136-
double get_time_ans;
137-
// <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
138-
if (demuxer && demux_control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH,
139-
(void *) &get_time_ans) > 0)
140-
{
141-
return get_time_ans;
142-
} else {
143-
return -1;
144-
}
145-
}
146-
147133
static void build_timeline(struct timeline *tl)
148134
{
149135
struct priv *p = tl->demuxer->priv;
@@ -210,7 +196,7 @@ static void build_timeline(struct timeline *tl)
210196
if (i + 1 < track_count && tracks[i].source == tracks[i + 1].source) {
211197
duration = tracks[i + 1].start - tracks[i].start;
212198
} else {
213-
duration = source_get_length(source);
199+
duration = source->duration;
214200
// Two cases: 1) last track of a single-file cue, or 2) any track of
215201
// a multi-file cue. We need to do this for 1) only because the
216202
// timeline needs to be terminated with the length of the last

demux/demux_disc.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ static int d_open(demuxer_t *demuxer, enum demux_check check)
338338
add_streams(demuxer);
339339
add_stream_chapters(demuxer);
340340

341+
double len;
342+
if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) >= 1)
343+
demuxer->duration = len;
344+
341345
return 0;
342346
}
343347

@@ -352,13 +356,6 @@ static int d_control(demuxer_t *demuxer, int cmd, void *arg)
352356
struct priv *p = demuxer->priv;
353357

354358
switch (cmd) {
355-
case DEMUXER_CTRL_GET_TIME_LENGTH: {
356-
double len;
357-
if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) < 1)
358-
break;
359-
*(double *)arg = len;
360-
return CONTROL_OK;
361-
}
362359
case DEMUXER_CTRL_RESYNC:
363360
demux_flush(p->slave);
364361
break; // relay to slave demuxer

demux/demux_edl.c

+1-11
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,6 @@ static void copy_chapters(struct demux_chapter **chapters, int *num_chapters,
199199
}
200200
}
201201

202-
// return length of the source in seconds, or -1 if unknown
203-
static double source_get_length(struct demuxer *demuxer)
204-
{
205-
double time;
206-
// <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
207-
if (demux_control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, &time) <= 0)
208-
time = -1;
209-
return time;
210-
}
211-
212202
static void resolve_timestamps(struct tl_part *part, struct demuxer *demuxer)
213203
{
214204
if (part->chapter_ts) {
@@ -279,7 +269,7 @@ static void build_timeline(struct timeline *tl, struct tl_parts *parts)
279269

280270
resolve_timestamps(part, source);
281271

282-
double end_time = source_get_length(source);
272+
double end_time = source->duration;
283273
if (end_time >= 0)
284274
end_time += source->start_time;
285275

demux/demux_lavf.c

+20-24
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,26 @@ static int demux_open_lavf(demuxer_t *demuxer, enum demux_check check)
939939

940940
demuxer->fully_read = priv->format_hack.fully_read;
941941

942+
if (priv->avfc->duration > 0) {
943+
demuxer->duration = (double)priv->avfc->duration / AV_TIME_BASE;
944+
} else {
945+
double total_duration = 0;
946+
double av_duration = 0;
947+
for (int n = 0; n < priv->avfc->nb_streams; n++) {
948+
AVStream *st = priv->avfc->streams[n];
949+
if (st->duration <= 0)
950+
continue;
951+
double f_duration = st->duration * av_q2d(st->time_base);
952+
total_duration = MPMAX(total_duration, f_duration);
953+
if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ||
954+
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
955+
av_duration = MPMAX(av_duration, f_duration);
956+
}
957+
double duration = av_duration > 0 ? av_duration : total_duration;
958+
if (duration > 0)
959+
demuxer->duration = duration;
960+
}
961+
942962
return 0;
943963
}
944964

@@ -1045,30 +1065,6 @@ static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
10451065
lavf_priv_t *priv = demuxer->priv;
10461066

10471067
switch (cmd) {
1048-
case DEMUXER_CTRL_GET_TIME_LENGTH:
1049-
if (priv->avfc->duration <= 0) {
1050-
double total_duration = 0;
1051-
double av_duration = 0;
1052-
for (int n = 0; n < priv->avfc->nb_streams; n++) {
1053-
AVStream *st = priv->avfc->streams[n];
1054-
if (st->duration <= 0)
1055-
continue;
1056-
double f_duration = st->duration * av_q2d(st->time_base);
1057-
total_duration = MPMAX(total_duration, f_duration);
1058-
if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ||
1059-
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
1060-
av_duration = MPMAX(av_duration, f_duration);
1061-
}
1062-
double duration = av_duration > 0 ? av_duration : total_duration;
1063-
if (duration <= 0)
1064-
return CONTROL_FALSE;
1065-
*(double *)arg = duration;
1066-
return CONTROL_OK;
1067-
}
1068-
1069-
*((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
1070-
return CONTROL_OK;
1071-
10721068
case DEMUXER_CTRL_SWITCHED_TRACKS:
10731069
{
10741070
select_tracks(demuxer, 0);

demux/demux_mf.c

+1-15
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ static int demux_open_mf(demuxer_t *demuxer, enum demux_check check)
335335
mf->sh = sh;
336336
demuxer->priv = (void *)mf;
337337
demuxer->seekable = true;
338+
demuxer->duration = mf->nr_of_files / mf->sh->codec->fps;
338339

339340
return 0;
340341

@@ -346,26 +347,11 @@ static void demux_close_mf(demuxer_t *demuxer)
346347
{
347348
}
348349

349-
static int demux_control_mf(demuxer_t *demuxer, int cmd, void *arg)
350-
{
351-
mf_t *mf = demuxer->priv;
352-
353-
switch (cmd) {
354-
case DEMUXER_CTRL_GET_TIME_LENGTH:
355-
*((double *)arg) = (double)mf->nr_of_files / mf->sh->codec->fps;
356-
return CONTROL_OK;
357-
358-
default:
359-
return CONTROL_UNKNOWN;
360-
}
361-
}
362-
363350
const demuxer_desc_t demuxer_desc_mf = {
364351
.name = "mf",
365352
.desc = "image files (mf)",
366353
.fill_buffer = demux_mf_fill_buffer,
367354
.open = demux_open_mf,
368355
.close = demux_close_mf,
369356
.seek = demux_seek_mf,
370-
.control = demux_control_mf,
371357
};

demux/demux_mkv.c

+4-18
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ static int demux_mkv_read_info(demuxer_t *demuxer)
411411
mkv_d->duration = info.duration * mkv_d->tc_scale / 1e9;
412412
MP_VERBOSE(demuxer, "| + duration: %.3fs\n",
413413
mkv_d->duration);
414+
demuxer->duration = mkv_d->duration;
414415
}
415416
if (info.title) {
416417
mp_tags_set_str(demuxer->metadata, "TITLE", info.title);
@@ -3090,8 +3091,10 @@ static void probe_last_timestamp(struct demuxer *demuxer, int64_t start_pos)
30903091
if (!last_ts[STREAM_VIDEO])
30913092
last_ts[STREAM_VIDEO] = mkv_d->cluster_tc;
30923093

3093-
if (last_ts[STREAM_VIDEO])
3094+
if (last_ts[STREAM_VIDEO]) {
30943095
mkv_d->duration = last_ts[STREAM_VIDEO] / 1e9 - demuxer->start_time;
3096+
demuxer->duration = mkv_d->duration;
3097+
}
30953098

30963099
stream_seek(demuxer->stream, start_pos);
30973100
mkv_d->cluster_start = mkv_d->cluster_end = 0;
@@ -3116,22 +3119,6 @@ static void probe_first_timestamp(struct demuxer *demuxer)
31163119
MP_VERBOSE(demuxer, "Start PTS: %f\n", demuxer->start_time);
31173120
}
31183121

3119-
static int demux_mkv_control(demuxer_t *demuxer, int cmd, void *arg)
3120-
{
3121-
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
3122-
3123-
switch (cmd) {
3124-
case DEMUXER_CTRL_GET_TIME_LENGTH:
3125-
if (mkv_d->duration == 0)
3126-
return CONTROL_FALSE;
3127-
3128-
*((double *) arg) = (double) mkv_d->duration;
3129-
return CONTROL_OK;
3130-
default:
3131-
return CONTROL_UNKNOWN;
3132-
}
3133-
}
3134-
31353122
static void mkv_free(struct demuxer *demuxer)
31363123
{
31373124
struct mkv_demuxer *mkv_d = demuxer->priv;
@@ -3149,7 +3136,6 @@ const demuxer_desc_t demuxer_desc_matroska = {
31493136
.fill_buffer = demux_mkv_fill_buffer,
31503137
.close = mkv_free,
31513138
.seek = demux_mkv_seek,
3152-
.control = demux_mkv_control,
31533139
.load_timeline = build_ordered_chapter_timeline,
31543140
};
31553141

demux/demux_mkv_timeline.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ static void build_timeline_loop(struct tl_ctx *ctx,
384384
/* If we're the source or it's a non-ordered edition reference,
385385
* just add a timeline part from the source. */
386386
if (current_source == j || !linked_m->uid.edition) {
387-
uint64_t source_full_length =
388-
demuxer_get_time_length(linked_source) * 1e9;
387+
uint64_t source_full_length = linked_source->duration * 1e9;
389388
uint64_t source_length = source_full_length - c->start;
390389
int64_t join_diff = 0;
391390

0 commit comments

Comments
 (0)