2011-02-14 07:34:39 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
2011-02-23 15:57:08 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <dirent.h>
|
2011-10-04 00:41:18 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2011-02-14 07:34:39 +00:00
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
2012-02-03 07:05:11 +00:00
|
|
|
#include "osdep/io.h"
|
|
|
|
|
2011-02-14 07:34:39 +00:00
|
|
|
#include "talloc.h"
|
|
|
|
|
2013-12-17 00:08:53 +00:00
|
|
|
#include "player/core.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "demux/demux.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/path.h"
|
2014-08-29 10:09:04 +00:00
|
|
|
#include "misc/bstr.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/playlist.h"
|
2011-08-19 02:36:27 +00:00
|
|
|
#include "stream/stream.h"
|
|
|
|
|
2011-10-04 00:41:18 +00:00
|
|
|
struct find_entry {
|
|
|
|
char *name;
|
|
|
|
int matchlen;
|
2012-02-28 22:01:13 +00:00
|
|
|
off_t size;
|
2011-10-04 00:41:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
2012-02-28 22:01:13 +00:00
|
|
|
off_t sizediff = a->size - b->size;
|
2011-10-04 00:41:18 +00:00
|
|
|
if (sizediff)
|
|
|
|
return FFSIGN(sizediff);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-23 15:57:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-10-04 00:41:18 +00:00
|
|
|
struct find_entry *entries = NULL;
|
2011-02-23 15:57:08 +00:00
|
|
|
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;
|
|
|
|
|
2012-07-28 21:47:42 +00:00
|
|
|
char *name = mp_path_join(results, directory, bstr0(ep->d_name));
|
2011-02-23 15:57:08 +00:00
|
|
|
char *s1 = ep->d_name;
|
|
|
|
char *s2 = basename;
|
|
|
|
int matchlen = 0;
|
|
|
|
while (*s1 && *s1++ == *s2++)
|
|
|
|
matchlen++;
|
2011-10-04 00:41:18 +00:00
|
|
|
// be a bit more fuzzy about matching the filename
|
|
|
|
matchlen = (matchlen + 3) / 5;
|
|
|
|
|
|
|
|
struct stat statbuf;
|
|
|
|
if (stat(name, &statbuf) != 0)
|
|
|
|
continue;
|
2012-02-28 22:01:13 +00:00
|
|
|
off_t size = statbuf.st_size;
|
2011-10-04 00:41:18 +00:00
|
|
|
|
2012-08-19 15:58:58 +00:00
|
|
|
entries = talloc_realloc(tmpmem, entries, struct find_entry,
|
2011-10-04 00:41:18 +00:00
|
|
|
num_results + 1);
|
2012-02-28 22:01:13 +00:00
|
|
|
entries[num_results] = (struct find_entry) { name, matchlen, size };
|
2011-02-23 15:57:08 +00:00
|
|
|
num_results++;
|
|
|
|
}
|
|
|
|
closedir(dp);
|
2011-10-04 00:41:18 +00:00
|
|
|
// NOTE: maybe should make it compare pointers instead
|
2012-10-31 23:24:08 +00:00
|
|
|
if (entries)
|
|
|
|
qsort(entries, num_results, sizeof(struct find_entry), cmp_entry);
|
2011-02-23 15:57:08 +00:00
|
|
|
results = talloc_realloc(NULL, results, char *, num_results);
|
2011-10-04 00:41:18 +00:00
|
|
|
for (int i = 0; i < num_results; i++) {
|
|
|
|
results[i] = entries[i].name;
|
2011-02-23 15:57:08 +00:00
|
|
|
}
|
|
|
|
talloc_free(tmpmem);
|
|
|
|
return results;
|
|
|
|
}
|
2011-02-14 07:34:39 +00:00
|
|
|
|
2012-08-19 13:31:38 +00:00
|
|
|
static int enable_cache(struct MPContext *mpctx, struct stream **stream,
|
2013-04-14 00:49:07 +00:00
|
|
|
struct demuxer **demuxer, struct demuxer_params *params)
|
2012-08-19 13:31:38 +00:00
|
|
|
{
|
2013-07-27 19:24:54 +00:00
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2012-08-19 13:31:38 +00:00
|
|
|
|
2014-05-19 21:27:09 +00:00
|
|
|
if (!stream_wants_cache(*stream, &opts->stream_cache))
|
2012-08-19 13:31:38 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
char *filename = talloc_strdup(NULL, (*demuxer)->filename);
|
|
|
|
free_demuxer(*demuxer);
|
|
|
|
free_stream(*stream);
|
|
|
|
|
2013-12-21 19:36:45 +00:00
|
|
|
*stream = stream_open(filename, mpctx->global);
|
2012-08-19 13:31:38 +00:00
|
|
|
if (!*stream) {
|
|
|
|
talloc_free(filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-19 21:27:09 +00:00
|
|
|
stream_enable_cache(stream, &opts->stream_cache);
|
2012-08-19 13:31:38 +00:00
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
*demuxer = demux_open(*stream, "mkv", params, mpctx->global);
|
2012-08-19 13:31:38 +00:00
|
|
|
if (!*demuxer) {
|
|
|
|
talloc_free(filename);
|
|
|
|
free_stream(*stream);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free(filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-08 04:33:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-14 00:49:07 +00:00
|
|
|
// segment = get Nth segment of a multi-segment file
|
2013-10-05 07:01:49 +00:00
|
|
|
static bool check_file_seg(struct MPContext *mpctx, struct demuxer ***sources,
|
|
|
|
int *num_sources, struct matroska_segment_uid **uids,
|
2013-04-14 00:49:07 +00:00
|
|
|
char *filename, int segment)
|
|
|
|
{
|
|
|
|
bool was_valid = false;
|
|
|
|
struct demuxer_params params = {
|
2013-10-05 07:01:49 +00:00
|
|
|
.matroska_num_wanted_uids = *num_sources,
|
|
|
|
.matroska_wanted_uids = *uids,
|
2013-04-14 00:49:07 +00:00
|
|
|
.matroska_wanted_segment = segment,
|
|
|
|
.matroska_was_valid = &was_valid,
|
|
|
|
};
|
2013-12-21 19:36:45 +00:00
|
|
|
struct stream *s = stream_open(filename, mpctx->global);
|
2013-04-14 00:49:07 +00:00
|
|
|
if (!s)
|
|
|
|
return false;
|
2013-12-21 19:24:20 +00:00
|
|
|
struct demuxer *d = demux_open(s, "mkv", ¶ms, mpctx->global);
|
2013-04-14 00:49:07 +00:00
|
|
|
|
|
|
|
if (!d) {
|
|
|
|
free_stream(s);
|
|
|
|
return was_valid;
|
|
|
|
}
|
2013-07-12 19:58:11 +00:00
|
|
|
if (d->type == DEMUXER_TYPE_MATROSKA) {
|
2013-10-07 00:49:12 +00:00
|
|
|
struct matroska_data *m = &d->matroska_data;
|
2013-10-05 07:01:49 +00:00
|
|
|
|
|
|
|
for (int i = 1; i < *num_sources; i++) {
|
|
|
|
struct matroska_segment_uid *uid = *uids + i;
|
|
|
|
if ((*sources)[i])
|
2013-04-14 00:49:07 +00:00
|
|
|
continue;
|
2013-09-26 06:53:54 +00:00
|
|
|
/* 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)) {
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_INFO(mpctx, "Match for source %d: %s\n",
|
2013-04-14 00:49:07 +00:00
|
|
|
i, d->filename);
|
|
|
|
|
2013-10-05 07:01:49 +00:00
|
|
|
for (int j = 0; j < m->num_ordered_chapters; j++) {
|
|
|
|
struct matroska_chapter *c = m->ordered_chapters + j;
|
|
|
|
|
|
|
|
if (!c->has_segment_uid)
|
|
|
|
continue;
|
|
|
|
|
2013-10-08 04:33:42 +00:00
|
|
|
if (has_source_request(*uids, *num_sources, &c->uid))
|
|
|
|
continue;
|
|
|
|
|
2013-10-05 07:01:49 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2013-10-19 20:58:02 +00:00
|
|
|
if (enable_cache(mpctx, &s, &d, ¶ms) < 0)
|
|
|
|
continue;
|
|
|
|
|
2013-10-05 07:01:49 +00:00
|
|
|
(*sources)[i] = d;
|
2013-04-14 00:49:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_demuxer(d);
|
|
|
|
free_stream(s);
|
|
|
|
return was_valid;
|
|
|
|
}
|
|
|
|
|
2013-10-05 07:01:49 +00:00
|
|
|
static void check_file(struct MPContext *mpctx, struct demuxer ***sources,
|
|
|
|
int *num_sources, struct matroska_segment_uid **uids,
|
2013-04-14 00:49:07 +00:00
|
|
|
char *filename, int first)
|
|
|
|
{
|
|
|
|
for (int segment = first; ; segment++) {
|
2013-10-07 00:49:12 +00:00
|
|
|
if (!check_file_seg(mpctx, sources, num_sources,
|
|
|
|
uids, filename, segment))
|
2013-04-14 00:49:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-02-14 07:34:39 +00:00
|
|
|
static int find_ordered_chapter_sources(struct MPContext *mpctx,
|
2013-10-05 07:01:49 +00:00
|
|
|
struct demuxer ***sources,
|
|
|
|
int *num_sources,
|
|
|
|
struct matroska_segment_uid **uids)
|
2011-02-14 07:34:39 +00:00
|
|
|
{
|
2013-12-14 20:52:37 +00:00
|
|
|
struct MPOpts *opts = mpctx->opts;
|
|
|
|
void *tmp = talloc_new(NULL);
|
2011-02-14 07:34:39 +00:00
|
|
|
int num_filenames = 0;
|
|
|
|
char **filenames = NULL;
|
2013-10-05 07:01:49 +00:00
|
|
|
if (*num_sources > 1) {
|
2013-04-14 00:49:07 +00:00
|
|
|
char *main_filename = mpctx->demuxer->filename;
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_INFO(mpctx, "This file references data from "
|
2011-02-14 07:34:39 +00:00
|
|
|
"other sources.\n");
|
2013-12-14 20:52:37 +00:00
|
|
|
if (opts->ordered_chapters_files && opts->ordered_chapters_files[0]) {
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_INFO(mpctx, "Loading references from '%s'.\n",
|
2013-12-14 20:52:37 +00:00
|
|
|
opts->ordered_chapters_files);
|
|
|
|
struct playlist *pl =
|
2013-12-21 19:24:20 +00:00
|
|
|
playlist_parse_file(opts->ordered_chapters_files, mpctx->global);
|
2013-12-14 20:52:37 +00:00
|
|
|
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) {
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_WARN(mpctx, "Playback source is not a "
|
2011-02-14 07:34:39 +00:00
|
|
|
"normal disk file. Will not search for related files.\n");
|
|
|
|
} else {
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_INFO(mpctx, "Will scan other files in the "
|
2011-02-14 07:34:39 +00:00
|
|
|
"same directory to find referenced sources.\n");
|
2013-04-14 00:49:07 +00:00
|
|
|
filenames = find_files(main_filename, ".mkv");
|
2011-02-23 15:57:08 +00:00
|
|
|
num_filenames = MP_TALLOC_ELEMS(filenames);
|
2013-12-14 20:52:37 +00:00
|
|
|
talloc_steal(tmp, filenames);
|
2011-02-14 07:34:39 +00:00
|
|
|
}
|
2013-04-14 00:49:07 +00:00
|
|
|
// Possibly get further segments appended to the first segment
|
2013-10-07 00:49:12 +00:00
|
|
|
check_file(mpctx, sources, num_sources, uids, main_filename, 1);
|
2011-02-14 07:34:39 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 07:01:49 +00:00
|
|
|
int old_source_count;
|
|
|
|
do {
|
|
|
|
old_source_count = *num_sources;
|
|
|
|
for (int i = 0; i < num_filenames; i++) {
|
|
|
|
if (!missing(*sources, *num_sources))
|
|
|
|
break;
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_INFO(mpctx, "Checking file %s\n", filenames[i]);
|
2013-10-05 07:01:49 +00:00
|
|
|
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);
|
2013-04-14 00:49:07 +00:00
|
|
|
|
2013-10-05 07:01:49 +00:00
|
|
|
if (missing(*sources, *num_sources)) {
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_ERR(mpctx, "Failed to find ordered chapter part!\n"
|
2011-02-14 07:34:39 +00:00
|
|
|
"There will be parts MISSING from the video!\n");
|
2013-04-14 00:49:07 +00:00
|
|
|
int j = 1;
|
2013-10-05 07:01:49 +00:00
|
|
|
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];
|
2013-10-08 04:35:04 +00:00
|
|
|
memmove(target_uid, source_uid, sizeof(*source_uid));
|
2011-02-14 07:34:39 +00:00
|
|
|
j++;
|
|
|
|
}
|
2013-10-05 07:01:49 +00:00
|
|
|
*num_sources = j;
|
2011-02-14 07:34:39 +00:00
|
|
|
}
|
2013-12-14 20:52:37 +00:00
|
|
|
|
|
|
|
talloc_free(tmp);
|
2013-10-05 07:01:49 +00:00
|
|
|
return *num_sources;
|
2011-02-14 07:34:39 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 17:59:32 +00:00
|
|
|
static int64_t add_timeline_part(struct MPContext *mpctx,
|
2013-10-14 22:51:03 +00:00
|
|
|
struct demuxer *source,
|
|
|
|
struct timeline_part **timeline,
|
|
|
|
int *part_count,
|
|
|
|
uint64_t start,
|
|
|
|
uint64_t *last_end_time,
|
|
|
|
uint64_t *starttime)
|
2013-10-07 01:42:30 +00:00
|
|
|
{
|
|
|
|
/* 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
|
2013-12-21 17:59:32 +00:00
|
|
|
|| FFABS(join_diff) > mpctx->opts->chapter_merge_threshold * 1e6
|
2013-10-07 01:42:30 +00:00
|
|
|
|| 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. */
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_VERBOSE(mpctx, "Merging timeline part %d with "
|
2013-10-07 01:42:30 +00:00
|
|
|
"offset %g ms.\n", *part_count, join_diff / 1e6);
|
|
|
|
*starttime += join_diff;
|
2013-10-14 22:51:03 +00:00
|
|
|
return join_diff;
|
2013-10-07 01:42:30 +00:00
|
|
|
}
|
2013-10-14 22:51:03 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-10-07 01:42:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 17:59:32 +00:00
|
|
|
static void account_missing_time(struct MPContext *mpctx,
|
|
|
|
uint64_t *total_time,
|
2013-10-16 01:04:16 +00:00
|
|
|
uint64_t new_time,
|
|
|
|
const char *message)
|
|
|
|
{
|
|
|
|
if (!new_time)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*total_time += new_time;
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_INFO(mpctx, "missing %"PRIu64" nanoseconds: %s\n",
|
2013-10-16 01:04:16 +00:00
|
|
|
new_time, message);
|
|
|
|
}
|
|
|
|
|
2013-12-21 17:59:32 +00:00
|
|
|
static void build_timeline_loop(struct MPContext *mpctx,
|
2013-10-07 01:42:30 +00:00
|
|
|
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,
|
2013-10-07 03:22:19 +00:00
|
|
|
int *part_count,
|
|
|
|
uint64_t skip,
|
|
|
|
uint64_t limit)
|
2013-10-07 01:42:30 +00:00
|
|
|
{
|
2013-10-07 03:22:19 +00:00
|
|
|
uint64_t local_starttime = 0;
|
2013-10-07 01:42:30 +00:00
|
|
|
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;
|
|
|
|
|
2013-10-07 03:22:19 +00:00
|
|
|
/* Fill in the uid with the current one if one isn't requested. */
|
2013-10-07 01:42:30 +00:00
|
|
|
if (!c->has_segment_uid)
|
|
|
|
memcpy(&c->uid, &m->uid, sizeof(c->uid));
|
|
|
|
|
2013-10-07 03:22:19 +00:00
|
|
|
/* "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;
|
|
|
|
|
2013-10-07 01:42:30 +00:00
|
|
|
/* 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;
|
|
|
|
|
2013-10-07 03:22:19 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
2013-10-07 01:42:30 +00:00
|
|
|
|
2013-10-07 03:22:19 +00:00
|
|
|
/* 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) {
|
2013-10-14 23:28:47 +00:00
|
|
|
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) {
|
2013-12-21 17:59:32 +00:00
|
|
|
account_missing_time(mpctx, missing_time, chapter_length,
|
2013-10-16 01:04:16 +00:00
|
|
|
"referenced segment ends before the requested start time");
|
2013-10-14 23:28:47 +00:00
|
|
|
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) {
|
2013-12-21 17:59:32 +00:00
|
|
|
account_missing_time(mpctx, missing_time, chapter_length - source_length,
|
2013-10-16 01:04:16 +00:00
|
|
|
"referenced segment ends before the requested end time");
|
2013-10-14 23:28:47 +00:00
|
|
|
chapter_length = source_length;
|
|
|
|
}
|
|
|
|
|
2013-12-21 17:59:32 +00:00
|
|
|
join_diff = add_timeline_part(mpctx, linked_source, timeline, part_count,
|
2013-10-14 23:28:47 +00:00
|
|
|
c->start, last_end_time, starttime);
|
2013-10-14 22:51:03 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
2013-10-07 03:22:19 +00:00
|
|
|
/* 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 {
|
2013-12-21 17:59:32 +00:00
|
|
|
build_timeline_loop(mpctx, sources, num_sources, j, starttime,
|
2013-10-07 03:22:19 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-10-07 01:42:30 +00:00
|
|
|
*last_end_time = c->end;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're missing a part of the chapter, so add it to the accounting. */
|
2013-12-21 17:59:32 +00:00
|
|
|
account_missing_time(mpctx, missing_time, chapter_length,
|
2013-10-16 01:04:16 +00:00
|
|
|
"the source for a chapter could not be found");
|
2013-10-16 01:16:05 +00:00
|
|
|
/* We don't have the source, but don't leave a gap in the timeline for
|
|
|
|
* the source. */
|
|
|
|
chapter_length = 0;
|
2013-10-07 01:42:30 +00:00
|
|
|
found:;
|
|
|
|
*starttime += chapter_length;
|
2013-10-07 03:22:19 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
2013-10-07 01:42:30 +00:00
|
|
|
}
|
2013-10-07 03:22:19 +00:00
|
|
|
|
|
|
|
/* If we stopped before the limit, add up the missing time. */
|
|
|
|
if (local_starttime < limit)
|
2013-12-21 17:59:32 +00:00
|
|
|
account_missing_time(mpctx, missing_time, limit - local_starttime,
|
2013-10-16 01:04:16 +00:00
|
|
|
"nested ordered chapter segment is shorter than the requested end time");
|
2013-10-07 01:42:30 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 07:34:39 +00:00
|
|
|
void build_ordered_chapter_timeline(struct MPContext *mpctx)
|
|
|
|
{
|
2013-07-27 19:24:54 +00:00
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2011-02-14 07:34:39 +00:00
|
|
|
|
|
|
|
if (!opts->ordered_chapters) {
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_INFO(mpctx, "File uses ordered chapters, but "
|
2011-02-14 07:34:39 +00:00
|
|
|
"you have disabled support for them. Ignoring.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_INFO(mpctx, "File uses ordered chapters, will build "
|
2011-02-14 07:34:39 +00:00
|
|
|
"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
|
2013-11-01 16:20:56 +00:00
|
|
|
struct demuxer **sources = talloc_zero_array(NULL, struct demuxer *,
|
2012-08-19 15:58:58 +00:00
|
|
|
m->num_ordered_chapters+1);
|
|
|
|
sources[0] = mpctx->demuxer;
|
2013-11-01 16:20:56 +00:00
|
|
|
struct matroska_segment_uid *uids =
|
|
|
|
talloc_zero_array(NULL, struct matroska_segment_uid,
|
|
|
|
m->num_ordered_chapters + 1);
|
2011-02-14 07:34:39 +00:00
|
|
|
int num_sources = 1;
|
2013-10-07 00:49:12 +00:00
|
|
|
memcpy(uids[0].segment, m->uid.segment, 16);
|
|
|
|
uids[0].edition = 0;
|
2011-02-14 07:34:39 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < m->num_ordered_chapters; i++) {
|
|
|
|
struct matroska_chapter *c = m->ordered_chapters + i;
|
2013-10-07 01:42:30 +00:00
|
|
|
/* 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;
|
|
|
|
|
2013-10-08 04:33:42 +00:00
|
|
|
if (has_source_request(uids, num_sources, &c->uid))
|
|
|
|
continue;
|
|
|
|
|
2013-10-07 00:49:12 +00:00
|
|
|
memcpy(uids + num_sources, &c->uid, sizeof(c->uid));
|
2012-08-19 15:58:58 +00:00
|
|
|
sources[num_sources] = NULL;
|
2011-02-14 07:34:39 +00:00
|
|
|
num_sources++;
|
|
|
|
}
|
|
|
|
|
2013-10-05 07:01:49 +00:00
|
|
|
num_sources = find_ordered_chapter_sources(mpctx, &sources, &num_sources,
|
|
|
|
&uids);
|
2013-10-07 01:42:30 +00:00
|
|
|
talloc_free(uids);
|
2011-02-14 07:34:39 +00:00
|
|
|
|
2013-10-07 01:42:30 +00:00
|
|
|
struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline, 0);
|
2013-11-01 16:20:56 +00:00
|
|
|
struct chapter *chapters =
|
|
|
|
talloc_zero_array(NULL, struct chapter, m->num_ordered_chapters);
|
2011-02-14 07:34:39 +00:00
|
|
|
uint64_t starttime = 0;
|
|
|
|
uint64_t missing_time = 0;
|
2013-10-07 01:42:30 +00:00
|
|
|
uint64_t last_end_time = 0;
|
2011-02-14 07:34:39 +00:00
|
|
|
int part_count = 0;
|
2013-12-21 17:59:32 +00:00
|
|
|
build_timeline_loop(mpctx, sources, num_sources, 0, &starttime,
|
2013-10-07 01:42:30 +00:00
|
|
|
&missing_time, &last_end_time, &timeline,
|
2013-10-07 03:22:19 +00:00
|
|
|
chapters, &part_count, 0, 0);
|
2011-02-14 07:34:39 +00:00
|
|
|
|
|
|
|
if (!part_count) {
|
|
|
|
// None of the parts come from the file itself???
|
|
|
|
talloc_free(sources);
|
|
|
|
talloc_free(timeline);
|
|
|
|
talloc_free(chapters);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-07 01:42:30 +00:00
|
|
|
struct timeline_part new = {
|
|
|
|
.start = starttime / 1e9,
|
|
|
|
};
|
|
|
|
MP_TARRAY_APPEND(NULL, timeline, part_count, new);
|
|
|
|
|
2013-10-14 23:29:42 +00:00
|
|
|
/* 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)
|
2013-12-21 17:59:32 +00:00
|
|
|
MP_ERR(mpctx, "There are %.3f seconds missing "
|
2011-02-14 07:34:39 +00:00
|
|
|
"from the timeline!\n", missing_time / 1e9);
|
2013-09-08 03:17:05 +00:00
|
|
|
talloc_free(mpctx->sources);
|
2011-02-14 07:34:39 +00:00
|
|
|
mpctx->sources = sources;
|
|
|
|
mpctx->num_sources = num_sources;
|
|
|
|
mpctx->timeline = timeline;
|
2013-10-07 01:42:30 +00:00
|
|
|
mpctx->num_timeline_parts = part_count - 1;
|
|
|
|
mpctx->num_chapters = m->num_ordered_chapters;
|
2011-02-14 07:34:39 +00:00
|
|
|
mpctx->chapters = chapters;
|
|
|
|
}
|