aboutsummaryrefslogtreecommitdiffhomepage
path: root/mpvcore/player/timeline
diff options
context:
space:
mode:
authorGravatar wm4 <wm4@nowhere>2013-12-17 00:53:22 +0100
committerGravatar wm4 <wm4@nowhere>2013-12-17 00:53:22 +0100
commite44911142914783c9ec717f329bd9b6a8bb9b70e (patch)
tree92bb653f7d56553ffd3bb6e5a22ffc0db91142e8 /mpvcore/player/timeline
parent7dc7b900c622235d595337c988a0c75280084b7c (diff)
Move mpvcore/player/ to player/
Diffstat (limited to 'mpvcore/player/timeline')
-rw-r--r--mpvcore/player/timeline/tl_cue.c417
-rw-r--r--mpvcore/player/timeline/tl_matroska.c591
-rw-r--r--mpvcore/player/timeline/tl_mpv_edl.c274
3 files changed, 0 insertions, 1282 deletions
diff --git a/mpvcore/player/timeline/tl_cue.c b/mpvcore/player/timeline/tl_cue.c
deleted file mode 100644
index e68b3349a5..0000000000
--- a/mpvcore/player/timeline/tl_cue.c
+++ /dev/null
@@ -1,417 +0,0 @@
-/*
- * 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_matroska.c b/mpvcore/player/timeline/tl_matroska.c
deleted file mode 100644
index fa0e315fc5..0000000000
--- a/mpvcore/player/timeline/tl_matroska.c
+++ /dev/null
@@ -1,591 +0,0 @@
-/*
- * 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 "mpvcore/playlist.h"
-#include "mpvcore/playlist_parser.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, mpctx->opts);
- if (!s)
- return false;
- struct demuxer *d = demux_open(s, "mkv", &params, mpctx->opts);
-
- if (!d) {
- free_stream(s);
- return was_valid;
- }
- if (d->type == DEMUXER_TYPE_MATROSKA) {
- struct matroska_data *m = &d->matroska_data;
-
- for (int i = 1; i < *num_sources; i++) {
- struct matroska_segment_uid *uid = *uids + i;
- if ((*sources)[i])
- continue;
- /* Accept the source if the segment uid matches and the edition
- * either matches or isn't specified. */
- if (!memcmp(uid->segment, m->uid.segment, 16) &&
- (!uid->edition || uid->edition == m->uid.edition)) {
- mp_msg(MSGT_CPLAYER, MSGL_INFO, "Match for source %d: %s\n",
- i, d->filename);
-
- for (int j = 0; j < m->num_ordered_chapters; j++) {
- struct matroska_chapter *c = m->ordered_chapters + j;
-
- if (!c->has_segment_uid)
- continue;
-
- if (has_source_request(*uids, *num_sources, &c->uid))
- continue;
-
- /* Set the requested segment. */
- MP_TARRAY_GROW(NULL, *uids, *num_sources);
- memcpy((*uids) + *num_sources, &c->uid, sizeof(c->uid));
-
- /* Add a new source slot. */
- MP_TARRAY_APPEND(NULL, *sources, *num_sources, NULL);
- }
-
- if (enable_cache(mpctx, &s, &d, &params) < 0)
- continue;
-
- (*sources)[i] = d;
- return true;
- }
- }
- }
- free_demuxer(d);
- free_stream(s);
- return was_valid;
-}
-
-static void check_file(struct MPContext *mpctx, struct demuxer ***sources,
- int *num_sources, struct matroska_segment_uid **uids,
- char *filename, int first)
-{
- for (int segment = first; ; segment++) {
- if (!check_file_seg(mpctx, sources, num_sources,
- uids, filename, segment))
- break;
- }
-}
-
-static bool missing(struct demuxer **sources, int num_sources)
-{
- for (int i = 0; i < num_sources; i++) {
- if (!sources[i])
- return true;
- }
- return false;
-}
-
-static int find_ordered_chapter_sources(struct MPContext *mpctx,
- struct demuxer ***sources,
- int *num_sources,
- struct matroska_segment_uid **uids)
-{
- struct MPOpts *opts = mpctx->opts;
- void *tmp = talloc_new(NULL);
- int num_filenames = 0;
- char **filenames = NULL;
- if (*num_sources > 1) {
- char *main_filename = mpctx->demuxer->filename;
- mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
- "other sources.\n");
- if (opts->ordered_chapters_files && opts->ordered_chapters_files[0]) {
- mp_msg(MSGT_CPLAYER, MSGL_INFO, "Loading references from '%s'.\n",
- opts->ordered_chapters_files);
- struct playlist *pl =
- playlist_parse_file(opts->ordered_chapters_files, opts);
- talloc_steal(tmp, pl);
- for (struct playlist_entry *e = pl->first; e; e = e->next)
- MP_TARRAY_APPEND(tmp, filenames, num_filenames, e->filename);
- } else if (mpctx->demuxer->stream->uncached_type != STREAMTYPE_FILE) {
- mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
- "normal disk file. Will not search for related files.\n");
- } else {
- mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
- "same directory to find referenced sources.\n");
- filenames = find_files(main_filename, ".mkv");
- num_filenames = MP_TALLOC_ELEMS(filenames);
- talloc_steal(tmp, filenames);
- }
- // Possibly get further segments appended to the first segment
- check_file(mpctx, sources, num_sources, uids, main_filename, 1);
- }
-
- int old_source_count;
- do {
- old_source_count = *num_sources;
- for (int i = 0; i < num_filenames; i++) {
- if (!missing(*sources, *num_sources))
- break;
- mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n", filenames[i]);
- check_file(mpctx, sources, num_sources, uids, filenames[i], 0);
- }
- /* Loop while we have new sources to look for. */
- } while (old_source_count != *num_sources);
-
- if (missing(*sources, *num_sources)) {
- mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
- "There will be parts MISSING from the video!\n");
- int j = 1;
- for (int i = 1; i < *num_sources; i++)
- if ((*sources)[i]) {
- struct matroska_segment_uid *source_uid = *uids + i;
- struct matroska_segment_uid *target_uid = *uids + j;
- (*sources)[j] = (*sources)[i];
- memmove(target_uid, source_uid, sizeof(*source_uid));
- j++;
- }
- *num_sources = j;
- }
-
- talloc_free(tmp);
- return *num_sources;
-}
-
-static int64_t add_timeline_part(struct MPOpts *opts,
- struct demuxer *source,
- struct timeline_part **timeline,
- int *part_count,
- uint64_t start,
- uint64_t *last_end_time,
- uint64_t *starttime)
-{
- /* Only add a separate part if the time or file actually changes.
- * Matroska files have chapter divisions that are redundant from
- * timeline point of view because the same chapter structure is used
- * both to specify the timeline and for normal chapter information.
- * Removing a missing inserted external chapter can also cause this.
- * We allow for a configurable fudge factor because of files which
- * specify chapter end times that are one frame too early;
- * we don't want to try seeking over a one frame gap. */
- int64_t join_diff = start - *last_end_time;
- if (*part_count == 0
- || FFABS(join_diff) > opts->chapter_merge_threshold * 1e6
- || source != (*timeline)[*part_count - 1].source) {
- struct timeline_part new = {
- .start = *starttime / 1e9,
- .source_start = start / 1e9,
- .source = source,
- };
- MP_TARRAY_APPEND(NULL, *timeline, *part_count, new);
- } else if (*part_count > 0 && join_diff) {
- /* Chapter was merged at an inexact boundary;
- * adjust timestamps to match. */
- mp_msg(MSGT_CPLAYER, MSGL_V, "Merging timeline part %d with "
- "offset %g ms.\n", *part_count, join_diff / 1e6);
- *starttime += join_diff;
- return join_diff;
- }
-
- return 0;
-}
-
-static void account_missing_time(uint64_t *total_time,
- uint64_t new_time,
- const char *message)
-{
- if (!new_time)
- return;
-
- *total_time += new_time;
- mp_msg(MSGT_CPLAYER, MSGL_HINT,
- "missing %"PRIu64" nanoseconds: %s\n",
- new_time, message);
-}
-
-static void build_timeline_loop(struct MPOpts *opts,
- struct demuxer **sources,
- int num_sources,
- int current_source,
- uint64_t *starttime,
- uint64_t *missing_time,
- uint64_t *last_end_time,
- struct timeline_part **timeline,
- struct chapter *chapters,
- int *part_count,
- uint64_t skip,
- uint64_t limit)
-{
- uint64_t local_starttime = 0;
- struct demuxer *source = sources[current_source];
- struct matroska_data *m = &source->matroska_data;
-
- for (int i = 0; i < m->num_ordered_chapters; i++) {
- struct matroska_chapter *c = m->ordered_chapters + i;
- uint64_t chapter_length = c->end - c->start;
-
- /* Fill in the uid with the current one if one isn't requested. */
- if (!c->has_segment_uid)
- memcpy(&c->uid, &m->uid, sizeof(c->uid));
-
- /* "Seek" to the end of the chapter. */
- local_starttime += chapter_length;
-
- /* If we're before the start time for the chapter, skip to the next
- * one. */
- if (local_starttime <= skip)
- continue;
-
- /* Look for the source for this chapter. */
- for (int j = 0; j < num_sources; j++) {
- struct demuxer *linked_source = sources[j];
- struct matroska_data *linked_m = &linked_source->matroska_data;
-
- /* Skip if the segment or edition isn't acceptable. */
- if (!demux_matroska_uid_cmp(&c->uid, &linked_m->uid))
- continue;
-
- /* TODO: Add option to support recursive chapters when loading
- * recursive ordered chapter editions? If so, more code will be
- * needed to add chapters for external non-ordered segment loading
- * as well since that part is not recursive. */
- if (!limit) {
- chapters[i].start = *starttime / 1e9;
- chapters[i].name = talloc_strdup(chapters, c->name);
- }
-
- /* If we're the source or it's a non-ordered edition reference,
- * just add a timeline part from the source. */
- if (current_source == j || !linked_m->num_ordered_chapters) {
- double source_full_length_seconds = demuxer_get_time_length(linked_source);
- /* Some accuracy lost, but not enough to care. (Over one
- * million parts, a nanosecond off here could add up to a
- * millisecond and trigger a false-positive error message, but
- * if that's your biggest problem at that point,
- * congratulations. */
- uint64_t source_full_length = source_full_length_seconds * 1e9;
- uint64_t source_length = source_full_length - c->start;
- int64_t join_diff = 0;
-
- /* If the chapter starts after the end of a source, there's
- * nothing we can get from it. Instead, mark the entire chapter
- * as missing and make the chapter length 0. */
- if (source_full_length <= c->start) {
- account_missing_time(missing_time, chapter_length,
- "referenced segment ends before the requested start time");
- chapter_length = 0;
- goto found;
- }
-
- /* If the source length starting at the chapter start is
- * shorter than the chapter it is supposed to fill, add the gap
- * to missing_time. Also, modify the chapter length to be what
- * we actually have to avoid playing off the end of the file
- * and not switching to the next source. */
- if (source_length < chapter_length) {
- account_missing_time(missing_time, chapter_length - source_length,
- "referenced segment ends before the requested end time");
- chapter_length = source_length;
- }
-
- join_diff = add_timeline_part(opts, linked_source, timeline, part_count,
- c->start, last_end_time, starttime);
-
- /* If we merged two chapters into a single part due to them
- * being off by a few frames, we need to change the limit to
- * avoid chopping the end of the intended chapter (the adding
- * frames case) or showing extra content (the removing frames
- * case). Also update chapter_length to incorporate the extra
- * time. */
- if (limit) {
- limit += join_diff;
- chapter_length += join_diff;
- }
- /* Otherwise, we have an ordered edition as the source. Since this
- * can jump around all over the place, we need to build up the
- * timeline parts for each of its chapters, but not add them as
- * chapters. */
- } else {
- build_timeline_loop(opts, sources, num_sources, j, starttime,
- missing_time, last_end_time, timeline,
- chapters, part_count, c->start, c->end);
- /* The loop call has added time as needed (we can't add it here
- * due to 'join_diff' in the add_timeline_part function. Since
- * the time has already been added as needed, the chapter has
- * an effective 0 length at this point. */
- chapter_length = 0;
- }
- *last_end_time = c->end;
- goto found;
- }
-
- /* We're missing a part of the chapter, so add it to the accounting. */
- account_missing_time(missing_time, chapter_length,
- "the source for a chapter could not be found");
- /* We don't have the source, but don't leave a gap in the timeline for
- * the source. */
- chapter_length = 0;
- found:;
- *starttime += chapter_length;
- /* If we're after the limit on this chapter, stop here. */
- if (limit && local_starttime >= limit) {
- /* Back up the global start time by the overflow. */
- *starttime -= local_starttime - limit;
- break;
- }
- }
-
- /* If we stopped before the limit, add up the missing time. */
- if (local_starttime < limit)
- account_missing_time(missing_time, limit - local_starttime,
- "nested ordered chapter segment is shorter than the requested end time");
-}
-
-void build_ordered_chapter_timeline(struct MPContext *mpctx)
-{
- struct MPOpts *opts = mpctx->opts;
-
- if (!opts->ordered_chapters) {
- mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
- "you have disabled support for them. Ignoring.\n");
- return;
- }
-
- mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
- "edit timeline.\n");
-
- struct demuxer *demuxer = mpctx->demuxer;
- struct matroska_data *m = &demuxer->matroska_data;
-
- // +1 because sources/uid_map[0] is original file even if all chapters
- // actually use other sources and need separate entries
- struct demuxer **sources = talloc_zero_array(NULL, struct demuxer *,
- m->num_ordered_chapters+1);
- sources[0] = mpctx->demuxer;
- struct matroska_segment_uid *uids =
- talloc_zero_array(NULL, struct matroska_segment_uid,
- m->num_ordered_chapters + 1);
- int num_sources = 1;
- memcpy(uids[0].segment, m->uid.segment, 16);
- uids[0].edition = 0;
-
- for (int i = 0; i < m->num_ordered_chapters; i++) {
- struct matroska_chapter *c = m->ordered_chapters + i;
- /* If there isn't a segment uid, we are the source. If the segment uid
- * is our segment uid and the edition matches. We can't accept the
- * "don't care" edition value of 0 since the user may have requested a
- * non-default edition. */
- if (!c->has_segment_uid || demux_matroska_uid_cmp(&c->uid, &m->uid))
- continue;
-
- if (has_source_request(uids, num_sources, &c->uid))
- continue;
-
- memcpy(uids + num_sources, &c->uid, sizeof(c->uid));
- sources[num_sources] = NULL;
- num_sources++;
- }
-
- num_sources = find_ordered_chapter_sources(mpctx, &sources, &num_sources,
- &uids);
- talloc_free(uids);
-
- struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline, 0);
- struct chapter *chapters =
- talloc_zero_array(NULL, struct chapter, m->num_ordered_chapters);
- uint64_t starttime = 0;
- uint64_t missing_time = 0;
- uint64_t last_end_time = 0;
- int part_count = 0;
- build_timeline_loop(opts, sources, num_sources, 0, &starttime,
- &missing_time, &last_end_time, &timeline,
- chapters, &part_count, 0, 0);
-
- if (!part_count) {
- // None of the parts come from the file itself???
- talloc_free(sources);
- talloc_free(timeline);
- talloc_free(chapters);
- return;
- }
-
- struct timeline_part new = {
- .start = starttime / 1e9,
- };
- MP_TARRAY_APPEND(NULL, timeline, part_count, new);
-
- /* Ignore anything less than a millisecond when reporting missing time. If
- * users really notice less than a millisecond missing, maybe this can be
- * revisited. */
- if (missing_time >= 1e6)
- mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
- "from the timeline!\n", missing_time / 1e9);
- talloc_free(mpctx->sources);
- mpctx->sources = sources;
- mpctx->num_sources = num_sources;
- mpctx->timeline = timeline;
- mpctx->num_timeline_parts = part_count - 1;
- mpctx->num_chapters = m->num_ordered_chapters;
- mpctx->chapters = chapters;
-}
diff --git a/mpvcore/player/timeline/tl_mpv_edl.c b/mpvcore/player/timeline/tl_mpv_edl.c
deleted file mode 100644
index fc0b6ebeeb..0000000000
--- a/mpvcore/player/timeline/tl_mpv_edl.c
+++ /dev/null
@@ -1,274 +0,0 @@
-/*
- * 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 <math.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 tl_part {
- char *filename; // what is stream_open()ed
- double offset; // offset into the source file
- double length; // length of the part (-1 if rest of the file)
-};
-
-struct tl_parts {
- struct tl_part *parts;
- int num_parts;
-};
-
-// Parse a time (absolute file time or duration). Currently equivalent to a
-// number. Return false on failure.
-static bool parse_time(bstr str, double *out_time)
-{
- bstr rest;
- double time = bstrtod(str, &rest);
- if (!str.len || rest.len || !isfinite(time))
- return false;
- *out_time = time;
- return true;
-}
-
-/* Returns a list of parts, or NULL on parse error.
- * Syntax (without file header or URI prefix):
- * url ::= <entry> ( (';' | '\n') <entry> )*
- * entry ::= <param> ( <param> ',' )*
- * param ::= [<string> '='] (<string> | '%' <number> '%' <bytes>)
- */
-static struct tl_parts *parse_edl(bstr str)
-{
- struct tl_parts *tl = talloc_zero(NULL, struct tl_parts);
- while (str.len) {
- if (bstr_eatstart0(&str, "#"))
- bstr_split_tok(str, "\n", &(bstr){0}, &str);
- if (bstr_eatstart0(&str, "\n") || bstr_eatstart0(&str, ";"))
- continue;
- struct tl_part p = { .length = -1 };
- int nparam = 0;
- while (1) {
- bstr name, val;
- // Check if it's of the form "name=..."
- int next = bstrcspn(str, "=%,;\n");
- if (next > 0 && next < str.len && str.start[next] == '=') {
- name = bstr_splice(str, 0, next);
- str = bstr_cut(str, next + 1);
- } else {
- const char *names[] = {"file", "start", "length"}; // implied name
- name = bstr0(nparam < 3 ? names[nparam] : "-");
- }
- if (bstr_eatstart0(&str, "%")) {
- int len = bstrtoll(str, &str, 0);
- if (!bstr_startswith0(str, "%") || (len > str.len - 1))
- goto error;
- val = bstr_splice(str, 1, len + 1);
- str = bstr_cut(str, len + 1);
- } else {
- next = bstrcspn(str, ",;\n");
- val = bstr_splice(str, 0, next);
- str = bstr_cut(str, next);
- }
- // Interpret parameters. Explicitly ignore unknown ones.
- if (bstr_equals0(name, "file")) {
- p.filename = bstrto0(tl, val);
- } else if (bstr_equals0(name, "start")) {
- if (!parse_time(val, &p.offset))
- goto error;
- } else if (bstr_equals0(name, "length")) {
- if (!parse_time(val, &p.length))
- goto error;
- }
- nparam++;
- if (!bstr_eatstart0(&str, ","))
- break;
- }
- if (!p.filename)
- goto error;
- MP_TARRAY_APPEND(tl, tl->parts, tl->num_parts, p);
- }
- if (!tl->num_parts)
- goto error;
- return tl;
-error:
- talloc_free(tl);
- return NULL;
-}
-
-static struct demuxer *open_file(char *filename, struct MPContext *mpctx)
-{
- struct MPOpts *opts = mpctx->opts;
- struct demuxer *d = NULL;
- struct stream *s = stream_open(filename, opts);
- if (s) {
- stream_enable_cache_percent(&s,
- opts->stream_cache_size,
- opts->stream_cache_def_size,
- opts->stream_cache_min_percent,
- opts->stream_cache_seek_min_percent);
- d = demux_open(s, NULL, NULL, opts);
- }
- if (!d) {
- mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Could not open source file '%s'.\n",
- filename);
- free_stream(s);
- }
- return d;
-}
-
-static struct demuxer *open_source(struct MPContext *mpctx, char *filename)
-{
- for (int n = 0; n < mpctx->num_sources; n++) {
- struct demuxer *d = mpctx->sources[n];
- if (strcmp(d->stream->url, filename) == 0)
- return d;
- }
- struct demuxer *d = open_file(filename, mpctx);
- if (d)
- MP_TARRAY_APPEND(NULL, mpctx->sources, mpctx->num_sources, d);
- return d;
-}
-
-// Append all chapters from src to the chapters array.
-// Ignore chapters outside of the given time range.
-static void copy_chapters(struct chapter **chapters, int *num_chapters,
- struct demuxer *src, double start, double len,
- double dest_offset)
-{
- int count = demuxer_chapter_count(src);
- for (int n = 0; n < count; n++) {
- double time = demuxer_chapter_time(src, n);
- if (time >= start && time <= start + len) {
- struct chapter ch = {
- .start = dest_offset + time,
- .name = talloc_steal(*chapters, demuxer_chapter_name(src, n)),
- };
- MP_TARRAY_APPEND(NULL, *chapters, *num_chapters, ch);
- }
- }
-}
-
-// return length of the source in seconds, or -1 if unknown
-static double source_get_length(struct demuxer *demuxer)
-{
- double time;
- // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
- if (demux_control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, &time) <= 0)
- time = -1;
- return time;
-}
-
-static void build_timeline(struct MPContext *mpctx, struct tl_parts *parts)
-{
- struct chapter *chapters = talloc_new(NULL);
- int num_chapters = 0;
- struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
- parts->num_parts + 1);
- double starttime = 0;
- for (int n = 0; n < parts->num_parts; n++) {
- struct tl_part *part = &parts->parts[n];
- struct demuxer *source = open_source(mpctx, part->filename);
- if (!source)
- goto error;
-
- double len = source_get_length(source);
- if (len <= 0) {
- mp_msg(MSGT_CPLAYER, MSGL_WARN,
- "EDL: source file '%s' has unknown duration.\n",
- part->filename);
- }
-
- // Unkown length => use rest of the file. If duration is unknown, make
- // something up.
- if (part->length < 0)
- part->length = (len < 0 ? 1 : len) - part->offset;
-
- if (len > 0) {
- double partlen = part->offset + part->length;
- if (partlen > len) {
- mp_msg(MSGT_CPLAYER, MSGL_WARN, "EDL: entry %d uses %f "
- "seconds, but file has only %f seconds.\n",
- n, partlen, len);
- }
- }
-
- // Add a chapter between each file.
- struct chapter ch = {
- .start = starttime,
- .name = talloc_strdup(chapters, part->filename),
- };
- MP_TARRAY_APPEND(NULL, chapters, num_chapters, ch);
-
- // Also copy the source file's chapters for the relevant parts
- copy_chapters(&chapters, &num_chapters, source, part->offset,
- part->length, starttime);
-
- timeline[n] = (struct timeline_part) {
- .start = starttime,
- .source_start = part->offset,
- .source = source,
- };
-
- starttime += part->length;
- }
- timeline[parts->num_parts] = (struct timeline_part) {.start = starttime};
- mpctx->timeline = timeline;
- mpctx->num_timeline_parts = parts->num_parts;
- mpctx->chapters = chapters;
- mpctx->num_chapters = num_chapters;
- return;
-
-error:
- talloc_free(timeline);
- talloc_free(chapters);
-}
-
-// For security, don't allow relative or absolute paths, only plain filenames.
-// Also, make these filenames relative to the edl source file.
-static void fix_filenames(struct tl_parts *parts, char *source_path)
-{
- struct bstr dirname = mp_dirname(source_path);
- for (int n = 0; n < parts->num_parts; n++) {
- struct tl_part *part = &parts->parts[n];
- char *filename = mp_basename(part->filename); // plain filename only
- part->filename = mp_path_join(parts, dirname, bstr0(filename));
- }
-}
-
-void build_mpv_edl_timeline(struct MPContext *mpctx)
-{
- struct tl_parts *parts = parse_edl(mpctx->demuxer->file_contents);
- if (!parts) {
- mp_msg(MSGT_CPLAYER, MSGL_ERR, "Error in EDL.\n");
- return;
- }
- // Source is .edl and not edl:// => don't allow arbitrary paths
- if (mpctx->demuxer->stream->uncached_type != STREAMTYPE_EDL)
- fix_filenames(parts, mpctx->demuxer->filename);
- build_timeline(mpctx, parts);
- talloc_free(parts);
-}