summaryrefslogtreecommitdiffstats
path: root/mpvcore/player/timeline
diff options
context:
space:
mode:
Diffstat (limited to 'mpvcore/player/timeline')
-rw-r--r--mpvcore/player/timeline/tl_cue.c417
-rw-r--r--mpvcore/player/timeline/tl_edl.c393
-rw-r--r--mpvcore/player/timeline/tl_matroska.c575
3 files changed, 1385 insertions, 0 deletions
diff --git a/mpvcore/player/timeline/tl_cue.c b/mpvcore/player/timeline/tl_cue.c
new file mode 100644
index 0000000000..e68b3349a5
--- /dev/null
+++ b/mpvcore/player/timeline/tl_cue.c
@@ -0,0 +1,417 @@
+/*
+ * This file is part of mplayer2.
+ *
+ * mplayer2 is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mplayer2 is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with mplayer2; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <dirent.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <ctype.h>
+
+#include "talloc.h"
+
+#include "mpvcore/player/mp_core.h"
+#include "mpvcore/mp_msg.h"
+#include "demux/demux.h"
+#include "mpvcore/path.h"
+#include "mpvcore/bstr.h"
+#include "mpvcore/mp_common.h"
+#include "stream/stream.h"
+
+// used by demuxer_cue.c
+bool mp_probe_cue(struct bstr data);
+
+#define SECS_PER_CUE_FRAME (1.0/75.0)
+
+enum cue_command {
+ CUE_ERROR = -1, // not a valid CUE command, or an unknown extension
+ CUE_EMPTY, // line with whitespace only
+ CUE_UNUSED, // valid CUE command, but ignored by this code
+ CUE_FILE,
+ CUE_TRACK,
+ CUE_INDEX,
+ CUE_TITLE,
+};
+
+static const struct {
+ enum cue_command command;
+ const char *text;
+} cue_command_strings[] = {
+ { CUE_FILE, "FILE" },
+ { CUE_TRACK, "TRACK" },
+ { CUE_INDEX, "INDEX" },
+ { CUE_TITLE, "TITLE" },
+ { CUE_UNUSED, "CATALOG" },
+ { CUE_UNUSED, "CDTEXTFILE" },
+ { CUE_UNUSED, "FLAGS" },
+ { CUE_UNUSED, "ISRC" },
+ { CUE_UNUSED, "PERFORMER" },
+ { CUE_UNUSED, "POSTGAP" },
+ { CUE_UNUSED, "PREGAP" },
+ { CUE_UNUSED, "REM" },
+ { CUE_UNUSED, "SONGWRITER" },
+ { CUE_UNUSED, "MESSAGE" },
+ { -1 },
+};
+
+struct cue_track {
+ double pregap_start; // corresponds to INDEX 00
+ double start; // corresponds to INDEX 01
+ struct bstr filename;
+ int source;
+ struct bstr title;
+};
+
+static enum cue_command read_cmd(struct bstr *data, struct bstr *out_params)
+{
+ struct bstr line = bstr_strip_linebreaks(bstr_getline(*data, data));
+ line = bstr_lstrip(line);
+ if (line.len == 0)
+ return CUE_EMPTY;
+ for (int n = 0; cue_command_strings[n].command != -1; n++) {
+ struct bstr name = bstr0(cue_command_strings[n].text);
+ if (bstr_startswith(line, name)) {
+ struct bstr rest = bstr_cut(line, name.len);
+ if (rest.len && !strchr(WHITESPACE, rest.start[0]))
+ continue;
+ if (out_params)
+ *out_params = rest;
+ return cue_command_strings[n].command;
+ }
+ }
+ return CUE_ERROR;
+}
+
+static bool eat_char(struct bstr *data, char ch)
+{
+ if (data->len && data->start[0] == ch) {
+ *data = bstr_cut(*data, 1);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+static struct bstr read_quoted(struct bstr *data)
+{
+ *data = bstr_lstrip(*data);
+ if (!eat_char(data, '"'))
+ return (struct bstr) {0};
+ int end = bstrchr(*data, '"');
+ if (end < 0)
+ return (struct bstr) {0};
+ struct bstr res = bstr_splice(*data, 0, end);
+ *data = bstr_cut(*data, end + 1);
+ return res;
+}
+
+// Read a 2 digit unsigned decimal integer.
+// Return -1 on failure.
+static int read_int_2(struct bstr *data)
+{
+ *data = bstr_lstrip(*data);
+ if (data->len && data->start[0] == '-')
+ return -1;
+ struct bstr s = *data;
+ int res = (int)bstrtoll(s, &s, 10);
+ if (data->len == s.len || data->len - s.len > 2)
+ return -1;
+ *data = s;
+ return res;
+}
+
+static double read_time(struct bstr *data)
+{
+ struct bstr s = *data;
+ bool ok = true;
+ double t1 = read_int_2(&s);
+ ok = eat_char(&s, ':') && ok;
+ double t2 = read_int_2(&s);
+ ok = eat_char(&s, ':') && ok;
+ double t3 = read_int_2(&s);
+ ok = ok && t1 >= 0 && t2 >= 0 && t3 >= 0;
+ return ok ? t1 * 60.0 + t2 + t3 * SECS_PER_CUE_FRAME : 0;
+}
+
+static struct bstr skip_utf8_bom(struct bstr data)
+{
+ return bstr_startswith0(data, "\xEF\xBB\xBF") ? bstr_cut(data, 3) : data;
+}
+
+// Check if the text in data is most likely CUE data. This is used by the
+// demuxer code to check the file type.
+// data is the start of the probed file, possibly cut off at a random point.
+bool mp_probe_cue(struct bstr data)
+{
+ bool valid = false;
+ data = skip_utf8_bom(data);
+ for (;;) {
+ enum cue_command cmd = read_cmd(&data, NULL);
+ // End reached. Since the line was most likely cut off, don't use the
+ // result of the last parsing call.
+ if (data.len == 0)
+ break;
+ if (cmd == CUE_ERROR)
+ return false;
+ if (cmd != CUE_EMPTY)
+ valid = true;
+ }
+ return valid;
+}
+
+static void add_source(struct MPContext *mpctx, struct demuxer *d)
+{
+ MP_TARRAY_APPEND(NULL, mpctx->sources, mpctx->num_sources, d);
+}
+
+static bool try_open(struct MPContext *mpctx, char *filename)
+{
+ struct bstr bfilename = bstr0(filename);
+ // Avoid trying to open itself or another .cue file. Best would be
+ // to check the result of demuxer auto-detection, but the demuxer
+ // API doesn't allow this without opening a full demuxer.
+ if (bstr_case_endswith(bfilename, bstr0(".cue"))
+ || bstrcasecmp(bstr0(mpctx->demuxer->filename), bfilename) == 0)
+ return false;
+
+ struct stream *s = stream_open(filename, mpctx->opts);
+ if (!s)
+ return false;
+ struct demuxer *d = demux_open(s, NULL, NULL, mpctx->opts);
+ // Since .bin files are raw PCM data with no headers, we have to explicitly
+ // open them. Also, try to avoid to open files that are most likely not .bin
+ // files, as that would only play noise. Checking the file extension is
+ // fragile, but it's about the only way we have.
+ // TODO: maybe also could check if the .bin file is a multiple of the Audio
+ // CD sector size (2352 bytes)
+ if (!d && bstr_case_endswith(bfilename, bstr0(".bin"))) {
+ mp_msg(MSGT_CPLAYER, MSGL_WARN, "CUE: Opening as BIN file!\n");
+ d = demux_open(s, "rawaudio", NULL, mpctx->opts);
+ }
+ if (d) {
+ add_source(mpctx, d);
+ return true;
+ }
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "Could not open source '%s'!\n", filename);
+ free_stream(s);
+ return false;
+}
+
+static bool open_source(struct MPContext *mpctx, struct bstr filename)
+{
+ void *ctx = talloc_new(NULL);
+ bool res = false;
+
+ struct bstr dirname = mp_dirname(mpctx->demuxer->filename);
+
+ struct bstr base_filename = bstr0(mp_basename(bstrdup0(ctx, filename)));
+ if (!base_filename.len) {
+ mp_msg(MSGT_CPLAYER, MSGL_WARN,
+ "CUE: Invalid audio filename in .cue file!\n");
+ } else {
+ char *fullname = mp_path_join(ctx, dirname, base_filename);
+ if (try_open(mpctx, fullname)) {
+ res = true;
+ goto out;
+ }
+ }
+
+ // Try an audio file with the same name as the .cue file (but different
+ // extension).
+ // Rationale: this situation happens easily if the audio file or both files
+ // are renamed.
+
+ struct bstr cuefile =
+ bstr_strip_ext(bstr0(mp_basename(mpctx->demuxer->filename)));
+
+ DIR *d = opendir(bstrdup0(ctx, dirname));
+ if (!d)
+ goto out;
+ struct dirent *de;
+ while ((de = readdir(d))) {
+ char *dename0 = de->d_name;
+ struct bstr dename = bstr0(dename0);
+ if (bstr_case_startswith(dename, cuefile)) {
+ mp_msg(MSGT_CPLAYER, MSGL_WARN, "CUE: No useful audio filename "
+ "in .cue file found, trying with '%s' instead!\n",
+ dename0);
+ if (try_open(mpctx, mp_path_join(ctx, dirname, dename))) {
+ res = true;
+ break;
+ }
+ }
+ }
+ closedir(d);
+
+out:
+ talloc_free(ctx);
+ if (!res)
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "CUE: Could not open audio file!\n");
+ return res;
+}
+
+// return length of the source in seconds, or -1 if unknown
+static double source_get_length(struct demuxer *demuxer)
+{
+ double get_time_ans;
+ // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
+ if (demuxer && demux_control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH,
+ (void *) &get_time_ans) > 0)
+ {
+ return get_time_ans;
+ } else {
+ return -1;
+ }
+}
+
+void build_cue_timeline(struct MPContext *mpctx)
+{
+ void *ctx = talloc_new(NULL);
+
+ struct bstr data = mpctx->demuxer->file_contents;
+ data = skip_utf8_bom(data);
+
+ struct cue_track *tracks = NULL;
+ size_t track_count = 0;
+
+ struct bstr filename = {0};
+ // Global metadata, and copied into new tracks.
+ struct cue_track proto_track = {0};
+ struct cue_track *cur_track = &proto_track;
+
+ while (data.len) {
+ struct bstr param;
+ switch (read_cmd(&data, &param)) {
+ case CUE_ERROR:
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "CUE: error parsing input file!\n");
+ goto out;
+ case CUE_TRACK: {
+ track_count++;
+ tracks = talloc_realloc(ctx, tracks, struct cue_track, track_count);
+ cur_track = &tracks[track_count - 1];
+ *cur_track = proto_track;
+ break;
+ }
+ case CUE_TITLE:
+ cur_track->title = read_quoted(&param);
+ break;
+ case CUE_INDEX: {
+ int type = read_int_2(&param);
+ double time = read_time(&param);
+ if (type == 1) {
+ cur_track->start = time;
+ cur_track->filename = filename;
+ } else if (type == 0) {
+ cur_track->pregap_start = time;
+ }
+ break;
+ }
+ case CUE_FILE:
+ // NOTE: FILE comes before TRACK, so don't use cur_track->filename
+ filename = read_quoted(&param);
+ break;
+ }
+ }
+
+ if (track_count == 0) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "CUE: no tracks found!\n");
+ goto out;
+ }
+
+ // Remove duplicate file entries. This might be too sophisticated, since
+ // CUE files usually use either separate files for every single track, or
+ // only one file for all tracks.
+
+ struct bstr *files = 0;
+ size_t file_count = 0;
+
+ for (size_t n = 0; n < track_count; n++) {
+ struct cue_track *track = &tracks[n];
+ track->source = -1;
+ for (size_t file = 0; file < file_count; file++) {
+ if (bstrcmp(files[file], track->filename) == 0) {
+ track->source = file;
+ break;
+ }
+ }
+ if (track->source == -1) {
+ file_count++;
+ files = talloc_realloc(ctx, files, struct bstr, file_count);
+ files[file_count - 1] = track->filename;
+ track->source = file_count - 1;
+ }
+ }
+
+ for (size_t i = 0; i < file_count; i++) {
+ if (!open_source(mpctx, files[i]))
+ goto out;
+ }
+
+ struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
+ track_count + 1);
+ struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
+ track_count);
+ double starttime = 0;
+ for (int i = 0; i < track_count; i++) {
+ struct demuxer *source = mpctx->sources[1 + tracks[i].source];
+ double duration;
+ if (i + 1 < track_count && tracks[i].source == tracks[i + 1].source) {
+ duration = tracks[i + 1].start - tracks[i].start;
+ } else {
+ duration = source_get_length(source);
+ // Two cases: 1) last track of a single-file cue, or 2) any track of
+ // a multi-file cue. We need to do this for 1) only because the
+ // timeline needs to be terminated with the length of the last
+ // track.
+ duration -= tracks[i].start;
+ }
+ if (duration < 0) {
+ mp_msg(MSGT_CPLAYER, MSGL_WARN,
+ "CUE: Can't get duration of source file!\n");
+ // xxx: do something more reasonable
+ duration = 0.0;
+ }
+ timeline[i] = (struct timeline_part) {
+ .start = starttime,
+ .source_start = tracks[i].start,
+ .source = source,
+ };
+ chapters[i] = (struct chapter) {
+ .start = timeline[i].start,
+ // might want to include other metadata here
+ .name = bstrdup0(chapters, tracks[i].title),
+ };
+ starttime += duration;
+ }
+
+ // apparently we need this to give the last part a non-zero length
+ timeline[track_count] = (struct timeline_part) {
+ .start = starttime,
+ // perhaps unused by the timeline code
+ .source_start = 0,
+ .source = timeline[0].source,
+ };
+
+ mpctx->timeline = timeline;
+ // the last part is not included it in the count
+ mpctx->num_timeline_parts = track_count + 1 - 1;
+ mpctx->chapters = chapters;
+ mpctx->num_chapters = track_count;
+
+out:
+ talloc_free(ctx);
+}
diff --git a/mpvcore/player/timeline/tl_edl.c b/mpvcore/player/timeline/tl_edl.c
new file mode 100644
index 0000000000..69e2402149
--- /dev/null
+++ b/mpvcore/player/timeline/tl_edl.c
@@ -0,0 +1,393 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <ctype.h>
+
+#include "talloc.h"
+
+#include "mpvcore/player/mp_core.h"
+#include "mpvcore/mp_msg.h"
+#include "demux/demux.h"
+#include "mpvcore/path.h"
+#include "mpvcore/bstr.h"
+#include "mpvcore/mp_common.h"
+#include "stream/stream.h"
+
+
+struct edl_source {
+ struct bstr id;
+ char *filename;
+ int lineno;
+};
+
+struct edl_time {
+ int64_t start;
+ int64_t end;
+ bool implied_start;
+ bool implied_end;
+};
+
+struct edl_part {
+ struct edl_time tl;
+ struct edl_time src;
+ int64_t duration;
+ int id;
+ int lineno;
+};
+
+static int find_edl_source(struct edl_source *sources, int num_sources,
+ struct bstr name)
+{
+ for (int i = 0; i < num_sources; i++)
+ if (!bstrcmp(sources[i].id, name))
+ return i;
+ return -1;
+}
+
+void build_edl_timeline(struct MPContext *mpctx)
+{
+ const struct bstr file_prefix = bstr0("<");
+ void *tmpmem = talloc_new(NULL);
+
+ struct bstr *lines = bstr_splitlines(tmpmem, mpctx->demuxer->file_contents);
+ int linec = MP_TALLOC_ELEMS(lines);
+ struct bstr header = bstr0("mplayer EDL file, version ");
+ if (!linec || !bstr_startswith(lines[0], header)) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Bad EDL header!\n");
+ goto out;
+ }
+ struct bstr version = bstr_strip(bstr_cut(lines[0], header.len));
+ if (bstrcmp(bstr0("2"), version)) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Unsupported EDL file version!\n");
+ goto out;
+ }
+ int num_sources = 0;
+ int num_parts = 0;
+ for (int i = 1; i < linec; i++) {
+ if (bstr_startswith(lines[i], file_prefix)) {
+ num_sources++;
+ } else {
+ int comment = bstrchr(lines[i], '#');
+ if (comment >= 0)
+ lines[i] = bstr_splice(lines[i], 0, comment);
+ if (bstr_strip(lines[i]).len)
+ num_parts++;
+ }
+ }
+ if (!num_parts) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "No parts in timeline!\n");
+ goto out;
+ }
+
+ // Parse source filename definitions
+
+ struct edl_source *edl_ids = talloc_array_ptrtype(tmpmem, edl_ids,
+ num_sources);
+ num_sources = 0;
+ for (int i = 1; i < linec; i++) {
+ struct bstr line = lines[i];
+ if (!bstr_startswith(line, file_prefix))
+ continue;
+ line = bstr_cut(line, file_prefix.len);
+ struct bstr id = bstr_split(line, WHITESPACE, &line);
+ if (find_edl_source(edl_ids, num_sources, id) >= 0) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Repeated ID on line %d!\n",
+ i+1);
+ goto out;
+ }
+ if (!isalpha(*id.start)) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Invalid ID on line %d!\n",
+ i+1);
+ goto out;
+ }
+ char *filename = mp_basename(bstrdup0(tmpmem, bstr_strip(line)));
+ if (!strlen(filename)) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR,
+ "EDL: Invalid filename on line %d!\n", i+1);
+ goto out;
+ }
+ struct bstr dirname = mp_dirname(mpctx->demuxer->filename);
+ char *fullname = mp_path_join(tmpmem, dirname, bstr0(filename));
+ edl_ids[num_sources++] = (struct edl_source){id, fullname, i+1};
+ }
+
+ // Parse timeline part definitions
+
+ struct edl_part *parts = talloc_array_ptrtype(tmpmem, parts, num_parts);
+ int total_parts = num_parts;
+ num_parts = 0;
+ for (int i = 1; i < linec; i++) {
+ struct bstr line = bstr_strip(lines[i]);
+ if (!line.len || bstr_startswith(line, file_prefix))
+ continue;
+ parts[num_parts] = (struct edl_part){{-1, -1}, {-1, -1}, 0, -1};
+ parts[num_parts].lineno = i + 1;
+ for (int s = 0; s < 2; s++) {
+ struct edl_time *p = !s ? &parts[num_parts].tl :
+ &parts[num_parts].src;
+ while (1) {
+ struct bstr t = bstr_split(line, WHITESPACE, &line);
+ if (!t.len) {
+ if (!s && num_parts < total_parts - 1) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: missing source "
+ "identifier on line %d (not last)!\n", i+1);
+ goto out;
+ }
+ break;
+ }
+ if (isalpha(*t.start)) {
+ if (s)
+ goto bad;
+ parts[num_parts].id = find_edl_source(edl_ids, num_sources,
+ t);
+ if (parts[num_parts].id < 0) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Undefined source "
+ "identifier on line %d!\n", i+1);
+ goto out;
+ }
+ break;
+ }
+ while (t.len) {
+ struct bstr next;
+ struct bstr arg = bstr_split(t, "+-", &next);
+ if (!arg.len) {
+ next = bstr_split(line, WHITESPACE, &line);
+ arg = bstr_split(next, "+-", &next);
+ }
+ if (!arg.len)
+ goto bad;
+ int64_t val;
+ if (!bstrcmp(arg, bstr0("*")))
+ val = -1;
+ else if (isdigit(*arg.start)) {
+ val = bstrtoll(arg, &arg, 10) * 1000000000;
+ if (arg.len && *arg.start == '.') {
+ int len = arg.len - 1;
+ arg = bstr_splice(arg, 1, 10);
+ int64_t val2 = bstrtoll(arg, &arg, 10);
+ if (arg.len)
+ goto bad;
+ for (; len < 9; len++)
+ val2 *= 10;
+ val += val2;
+ }
+ } else
+ goto bad;
+ int c = *t.start;
+ if (isdigit(c) || c == '*') {
+ if (val < 0)
+ p->implied_start = true;
+ else
+ p->start = val;
+ } else if (c == '-') {
+ if (val < 0)
+ p->implied_end = true;
+ else
+ p->end = val;
+ } else if (c == '+') {
+ if (val < 0)
+ goto bad;
+ if (val == 0) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: zero duration "
+ "on line %d!\n", i+1);
+ goto out;
+ }
+ parts[num_parts].duration = val;
+ } else
+ goto bad;
+ t = next;
+ }
+ }
+ }
+ num_parts++;
+ continue;
+ bad:
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Malformed line %d!\n", i+1);
+ goto out;
+ }
+
+ // Fill in implied start/stop/duration values
+
+ int64_t *times = talloc_zero_array(tmpmem, int64_t, num_sources);
+ while (1) {
+ int64_t time = 0;
+ for (int i = 0; i < num_parts; i++) {
+ for (int s = 0; s < 2; s++) {
+ struct edl_time *p = s ? &parts[i].tl : &parts[i].src;
+ if (!s && parts[i].id == -1)
+ continue;
+ int64_t *t = s ? &time : times + parts[i].id;
+ p->implied_start |= s && *t >= 0;
+ if (p->implied_start && p->start >= 0 && *t >= 0
+ && p->start != *t) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line "
+ "%d!\n", parts[i].lineno);
+ goto out;
+ }
+ if (p->start >= 0)
+ *t = p->start;
+ if (p->implied_start)
+ p->start = *t;
+ if (*t >= 0 && parts[i].duration)
+ *t += parts[i].duration;
+ else
+ *t = -1;
+ if (p->end >= 0)
+ *t = p->end;
+ }
+ }
+ for (int i = 0; i < num_sources; i++)
+ times[i] = -1;
+ time = -1;
+ for (int i = num_parts - 1; i >= 0; i--) {
+ for (int s = 0; s < 2; s++) {
+ struct edl_time *p = s ? &parts[i].tl : &parts[i].src;
+ if (!s && parts[i].id == -1)
+ continue;
+ int64_t *t = s ? &time : times + parts[i].id;
+ p->implied_end |= s && *t >= 0;
+ if (p->implied_end && p->end >= 0 && *t >=0 && p->end != *t) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line "
+ "%d!\n", parts[i].lineno);
+ goto out;
+ }
+ if (p->end >= 0)
+ *t = p->end;
+ if (p->implied_end)
+ p->end = *t;
+ if (*t >= 0 && parts[i].duration) {
+ *t -= parts[i].duration;
+ if (*t < 0) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Negative time "
+ "on line %d!\n", parts[i].lineno);
+ goto out;
+ }
+ } else
+ *t = -1;
+ if (p->start >= 0)
+ *t = p->start;
+ }
+ }
+ int missing_duration = -1;
+ int missing_srcstart = -1;
+ bool anything_done = false;
+ for (int i = 0; i < num_parts; i++) {
+ int64_t duration = parts[i].duration;
+ if (parts[i].tl.start >= 0 && parts[i].tl.end >= 0) {
+ int64_t duration2 = parts[i].tl.end - parts[i].tl.start;
+ if (duration && duration != duration2)
+ goto incons;
+ duration = duration2;
+ if (duration <= 0)
+ goto neg;
+ }
+ if (parts[i].src.start >= 0 && parts[i].src.end >= 0) {
+ int64_t duration2 = parts[i].src.end - parts[i].src.start;
+ if (duration && duration != duration2) {
+ incons:
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line "
+ "%d!\n", i+1);
+ goto out;
+ }
+ duration = duration2;
+ if (duration <= 0) {
+ neg:
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: duration <= 0 on "
+ "line %d!\n", parts[i].lineno);
+ goto out;
+ }
+ }
+ if (parts[i].id == -1)
+ continue;
+ if (!duration)
+ missing_duration = i;
+ else if (!parts[i].duration)
+ anything_done = true;
+ parts[i].duration = duration;
+ if (duration && parts[i].src.start < 0) {
+ if (parts[i].src.end < 0)
+ missing_srcstart = i;
+ else
+ parts[i].src.start = parts[i].src.end - duration;
+ }
+ }
+ if (!anything_done) {
+ if (missing_duration >= 0) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Could not determine "
+ "duration for line %d!\n",
+ parts[missing_duration].lineno);
+ goto out;
+ }
+ if (missing_srcstart >= 0) {
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: no source start time for "
+ "line %d!\n", parts[missing_srcstart].lineno);
+ goto out;
+ }
+ break;
+ }
+ }
+
+ // Open source files
+
+ struct demuxer **sources = talloc_array_ptrtype(NULL, sources,
+ num_sources + 1);
+ talloc_free(mpctx->sources);
+ mpctx->sources = sources;
+ sources[0] = mpctx->demuxer;
+ mpctx->num_sources = 1;
+
+ for (int i = 0; i < num_sources; i++) {
+ struct stream *s = stream_open(edl_ids[i].filename, mpctx->opts);
+ if (!s)
+ goto openfail;
+ struct demuxer *d = demux_open(s, NULL, NULL, mpctx->opts);
+ if (!d) {
+ free_stream(s);
+ openfail:
+ mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Could not open source "
+ "file on line %d!\n", edl_ids[i].lineno);
+ goto out;
+ }
+ sources[mpctx->num_sources] = d;
+ mpctx->num_sources++;
+ }
+
+ // Write final timeline structure
+
+ struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
+ num_parts + 1);
+ int64_t starttime = 0;
+ for (int i = 0; i < num_parts; i++) {
+ timeline[i].start = starttime / 1e9;
+ starttime += parts[i].duration;
+ timeline[i].source_start = parts[i].src.start / 1e9;
+ timeline[i].source = sources[parts[i].id + 1];
+ }
+ if (parts[num_parts - 1].id != -1) {
+ timeline[num_parts].start = starttime / 1e9;
+ num_parts++;
+ }
+ mpctx->timeline = timeline;
+ mpctx->num_timeline_parts = num_parts - 1;
+
+ out:
+ talloc_free(tmpmem);
+}
diff --git a/mpvcore/player/timeline/tl_matroska.c b/mpvcore/player/timeline/tl_matroska.c
new file mode 100644
index 0000000000..5a96cfe5f1
--- /dev/null
+++ b/mpvcore/player/timeline/tl_matroska.c
@@ -0,0 +1,575 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <assert.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <libavutil/common.h>
+
+#include "osdep/io.h"
+
+#include "talloc.h"
+
+#include "mpvcore/player/mp_core.h"
+#include "mpvcore/mp_msg.h"
+#include "demux/demux.h"
+#include "mpvcore/path.h"
+#include "mpvcore/bstr.h"
+#include "mpvcore/mp_common.h"
+#include "stream/stream.h"
+
+struct find_entry {
+ char *name;
+ int matchlen;
+ off_t size;
+};
+
+static int cmp_entry(const void *pa, const void *pb)
+{
+ const struct find_entry *a = pa, *b = pb;
+ // check "similar" filenames first
+ int matchdiff = b->matchlen - a->matchlen;
+ if (matchdiff)
+ return FFSIGN(matchdiff);
+ // check small files first
+ off_t sizediff = a->size - b->size;
+ if (sizediff)
+ return FFSIGN(sizediff);
+ return 0;
+}
+
+static char **find_files(const char *original_file, const char *suffix)
+{
+ void *tmpmem = talloc_new(NULL);
+ char *basename = mp_basename(original_file);
+ struct bstr directory = mp_dirname(original_file);
+ char **results = talloc_size(NULL, 0);
+ char *dir_zero = bstrdup0(tmpmem, directory);
+ DIR *dp = opendir(dir_zero);
+ if (!dp) {
+ talloc_free(tmpmem);
+ return results;
+ }
+ struct find_entry *entries = NULL;
+ struct dirent *ep;
+ int num_results = 0;
+ while ((ep = readdir(dp))) {
+ int suffix_offset = strlen(ep->d_name) - strlen(suffix);
+ // name must end with suffix
+ if (suffix_offset < 0 || strcmp(ep->d_name + suffix_offset, suffix))
+ continue;
+ // don't list the original name
+ if (!strcmp(ep->d_name, basename))
+ continue;
+
+ char *name = mp_path_join(results, directory, bstr0(ep->d_name));
+ char *s1 = ep->d_name;
+ char *s2 = basename;
+ int matchlen = 0;
+ while (*s1 && *s1++ == *s2++)
+ matchlen++;
+ // be a bit more fuzzy about matching the filename
+ matchlen = (matchlen + 3) / 5;
+
+ struct stat statbuf;
+ if (stat(name, &statbuf) != 0)
+ continue;
+ off_t size = statbuf.st_size;
+
+ entries = talloc_realloc(tmpmem, entries, struct find_entry,
+ num_results + 1);
+ entries[num_results] = (struct find_entry) { name, matchlen, size };
+ num_results++;
+ }
+ closedir(dp);
+ // NOTE: maybe should make it compare pointers instead
+ if (entries)
+ qsort(entries, num_results, sizeof(struct find_entry), cmp_entry);
+ results = talloc_realloc(NULL, results, char *, num_results);
+ for (int i = 0; i < num_results; i++) {
+ results[i] = entries[i].name;
+ }
+ talloc_free(tmpmem);
+ return results;
+}
+
+static int enable_cache(struct MPContext *mpctx, struct stream **stream,
+ struct demuxer **demuxer, struct demuxer_params *params)
+{
+ struct MPOpts *opts = mpctx->opts;
+
+ if (opts->stream_cache_size <= 0)
+ return 0;
+
+ char *filename = talloc_strdup(NULL, (*demuxer)->filename);
+ free_demuxer(*demuxer);
+ free_stream(*stream);
+
+ *stream = stream_open(filename, opts);
+ if (!*stream) {
+ talloc_free(filename);
+ return -1;
+ }
+
+ stream_enable_cache_percent(stream,
+ opts->stream_cache_size,
+ opts->stream_cache_def_size,
+ opts->stream_cache_min_percent,
+ opts->stream_cache_seek_min_percent);
+
+ *demuxer = demux_open(*stream, "mkv", params, opts);
+ if (!*demuxer) {
+ talloc_free(filename);
+ free_stream(*stream);
+ return -1;
+ }
+
+ talloc_free(filename);
+ return 1;
+}
+
+static bool has_source_request(struct matroska_segment_uid *uids,
+ int num_sources,
+ struct matroska_segment_uid *new_uid)
+{
+ for (int i = 0; i < num_sources; ++i) {
+ if (demux_matroska_uid_cmp(uids + i, new_uid))
+ return true;
+ }
+
+ return false;
+}
+
+// segment = get Nth segment of a multi-segment file
+static bool check_file_seg(struct MPContext *mpctx, struct demuxer ***sources,
+ int *num_sources, struct matroska_segment_uid **uids,
+ char *filename, int segment)
+{
+ bool was_valid = false;
+ struct demuxer_params params = {
+ .matroska_num_wanted_uids = *num_sources,
+ .matroska_wanted_uids = *uids,
+ .matroska_wanted_segment = segment,
+ .matroska_was_valid = &was_valid,
+ };
+ struct stream *s = stream_open(filename, mpc