docs: document --quvi-format
[mplayer.git] / timeline / tl_matroska.c
blob22a72e1be1c9962de80c8c28330c87e44de278ee
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdlib.h>
20 #include <stdbool.h>
21 #include <inttypes.h>
22 #include <assert.h>
23 #include <dirent.h>
24 #include <libavutil/common.h>
26 #include "osdep/io.h"
28 #include "talloc.h"
30 #include "mp_core.h"
31 #include "mp_msg.h"
32 #include "libmpdemux/demuxer.h"
33 #include "path.h"
34 #include "bstr.h"
35 #include "mpcommon.h"
36 #include "stream/stream.h"
38 static char **find_files(const char *original_file, const char *suffix)
40 void *tmpmem = talloc_new(NULL);
41 char *basename = mp_basename(original_file);
42 struct bstr directory = mp_dirname(original_file);
43 char **results = talloc_size(NULL, 0);
44 char *dir_zero = bstrdup0(tmpmem, directory);
45 DIR *dp = opendir(dir_zero);
46 if (!dp) {
47 talloc_free(tmpmem);
48 return results;
50 struct dirent *ep;
51 char ***names_by_matchlen = talloc_zero_array(tmpmem, char **,
52 strlen(basename) + 1);
53 int num_results = 0;
54 while ((ep = readdir(dp))) {
55 int suffix_offset = strlen(ep->d_name) - strlen(suffix);
56 // name must end with suffix
57 if (suffix_offset < 0 || strcmp(ep->d_name + suffix_offset, suffix))
58 continue;
59 // don't list the original name
60 if (!strcmp(ep->d_name, basename))
61 continue;
63 char *name = mp_path_join(results, directory, bstr(ep->d_name));
64 char *s1 = ep->d_name;
65 char *s2 = basename;
66 int matchlen = 0;
67 while (*s1 && *s1++ == *s2++)
68 matchlen++;
69 int oldcount = MP_TALLOC_ELEMS(names_by_matchlen[matchlen]);
70 names_by_matchlen[matchlen] = talloc_realloc(names_by_matchlen,
71 names_by_matchlen[matchlen],
72 char *, oldcount + 1);
73 names_by_matchlen[matchlen][oldcount] = name;
74 num_results++;
76 closedir(dp);
77 results = talloc_realloc(NULL, results, char *, num_results);
78 char **resptr = results;
79 for (int i = strlen(basename); i >= 0; i--) {
80 char **p = names_by_matchlen[i];
81 for (int j = 0; j < talloc_get_size(p) / sizeof(char *); j++)
82 *resptr++ = p[j];
84 assert(resptr == results + num_results);
85 talloc_free(tmpmem);
86 return results;
89 static struct demuxer *open_demuxer(struct stream *stream,
90 struct MPContext *mpctx, char *filename, unsigned char uid_map[][16])
92 return demux_open_withparams(&mpctx->opts, stream,
93 DEMUXER_TYPE_MATROSKA, mpctx->opts.audio_id,
94 mpctx->opts.video_id, mpctx->opts.sub_id, filename,
95 &(struct demuxer_params){.matroska_wanted_uids = uid_map});
98 static int enable_cache(struct MPContext *mpctx, struct stream **stream,
99 struct demuxer **demuxer, unsigned char uid_map[][16])
101 struct MPOpts *opts = &mpctx->opts;
103 if (opts->stream_cache_size <= 0)
104 return 0;
106 char *filename = talloc_strdup(NULL, (*demuxer)->filename);
107 free_demuxer(*demuxer);
108 free_stream(*stream);
110 int format = 0;
111 *stream = open_stream(filename, &mpctx->opts, &format);
112 if (!*stream) {
113 talloc_free(filename);
114 return -1;
117 stream_enable_cache_percent(*stream,
118 opts->stream_cache_size,
119 opts->stream_cache_min_percent,
120 opts->stream_cache_seek_min_percent);
122 *demuxer = open_demuxer(*stream, mpctx, filename, uid_map);
123 if (!*demuxer) {
124 talloc_free(filename);
125 free_stream(*stream);
126 return -1;
129 talloc_free(filename);
130 return 1;
133 static int find_ordered_chapter_sources(struct MPContext *mpctx,
134 struct content_source *sources,
135 int num_sources,
136 unsigned char uid_map[][16])
138 int num_filenames = 0;
139 char **filenames = NULL;
140 if (num_sources > 1) {
141 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
142 "other sources.\n");
143 if (mpctx->stream->type != STREAMTYPE_FILE) {
144 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
145 "normal disk file. Will not search for related files.\n");
146 } else {
147 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
148 "same directory to find referenced sources.\n");
149 filenames = find_files(mpctx->demuxer->filename, ".mkv");
150 num_filenames = MP_TALLOC_ELEMS(filenames);
154 int num_left = num_sources - 1;
155 for (int i = 0; i < num_filenames && num_left > 0; i++) {
156 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
157 filename_recode(filenames[i]));
158 int format = 0;
159 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
160 if (!s)
161 continue;
162 struct demuxer *d = open_demuxer(s, mpctx, filenames[i], uid_map);
164 if (!d) {
165 free_stream(s);
166 continue;
168 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
169 for (int i = 1; i < num_sources; i++) {
170 if (sources[i].demuxer)
171 continue;
172 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
173 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
174 i, filename_recode(d->filename));
176 if (enable_cache(mpctx, &s, &d, uid_map) < 0)
177 continue;
179 sources[i].stream = s;
180 sources[i].demuxer = d;
181 num_left--;
182 goto match;
186 free_demuxer(d);
187 free_stream(s);
188 continue;
189 match:
192 talloc_free(filenames);
193 if (num_left) {
194 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
195 "There will be parts MISSING from the video!\n");
196 for (int i = 1, j = 1; i < num_sources; i++)
197 if (sources[i].demuxer) {
198 sources[j] = sources[i];
199 memcpy(uid_map[j], uid_map[i], 16);
200 j++;
203 return num_sources - num_left;
206 void build_ordered_chapter_timeline(struct MPContext *mpctx)
208 struct MPOpts *opts = &mpctx->opts;
210 if (!opts->ordered_chapters) {
211 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
212 "you have disabled support for them. Ignoring.\n");
213 return;
216 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
217 "edit timeline.\n");
219 struct demuxer *demuxer = mpctx->demuxer;
220 struct matroska_data *m = &demuxer->matroska_data;
222 // +1 because sources/uid_map[0] is original file even if all chapters
223 // actually use other sources and need separate entries
224 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
225 m->num_ordered_chapters+1);
226 sources[0].stream = mpctx->stream;
227 sources[0].demuxer = mpctx->demuxer;
228 unsigned char (*uid_map)[16] = talloc_array_ptrtype(NULL, uid_map,
229 m->num_ordered_chapters + 1);
230 int num_sources = 1;
231 memcpy(uid_map[0], m->segment_uid, 16);
233 for (int i = 0; i < m->num_ordered_chapters; i++) {
234 struct matroska_chapter *c = m->ordered_chapters + i;
235 if (!c->has_segment_uid)
236 memcpy(c->segment_uid, m->segment_uid, 16);
238 for (int j = 0; j < num_sources; j++)
239 if (!memcmp(c->segment_uid, uid_map[j], 16))
240 goto found1;
241 memcpy(uid_map[num_sources], c->segment_uid, 16);
242 sources[num_sources] = (struct content_source){};
243 num_sources++;
244 found1:
248 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
249 uid_map);
252 // +1 for terminating chapter with start time marking end of last real one
253 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
254 m->num_ordered_chapters + 1);
255 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
256 m->num_ordered_chapters);
257 uint64_t starttime = 0;
258 uint64_t missing_time = 0;
259 int part_count = 0;
260 int num_chapters = 0;
261 uint64_t prev_part_offset = 0;
262 for (int i = 0; i < m->num_ordered_chapters; i++) {
263 struct matroska_chapter *c = m->ordered_chapters + i;
265 int j;
266 for (j = 0; j < num_sources; j++) {
267 if (!memcmp(c->segment_uid, uid_map[j], 16))
268 goto found2;
270 missing_time += c->end - c->start;
271 continue;
272 found2:;
273 /* Only add a separate part if the time or file actually changes.
274 * Matroska files have chapter divisions that are redundant from
275 * timeline point of view because the same chapter structure is used
276 * both to specify the timeline and for normal chapter information.
277 * Removing a missing inserted external chapter can also cause this.
278 * We allow for a configurable fudge factor because of files which
279 * specify chapter end times that are one frame too early;
280 * we don't want to try seeking over a one frame gap. */
281 int64_t join_diff = c->start - starttime - prev_part_offset;
282 if (part_count == 0
283 || FFABS(join_diff) > opts->chapter_merge_threshold * 1000000
284 || sources + j != timeline[part_count - 1].source) {
285 timeline[part_count].source = sources + j;
286 timeline[part_count].start = starttime / 1e9;
287 timeline[part_count].source_start = c->start / 1e9;
288 prev_part_offset = c->start - starttime;
289 part_count++;
290 } else if (part_count > 0 && join_diff) {
291 /* Chapter was merged at an inexact boundary;
292 * adjust timestamps to match. */
293 mp_msg(MSGT_CPLAYER, MSGL_V, "Merging timeline part %d with "
294 "offset %g ms.\n", i, join_diff / 1e6);
295 starttime += join_diff;
297 chapters[num_chapters].start = starttime / 1e9;
298 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
299 starttime += c->end - c->start;
300 num_chapters++;
302 timeline[part_count].start = starttime / 1e9;
303 talloc_free(uid_map);
305 if (!part_count) {
306 // None of the parts come from the file itself???
307 talloc_free(sources);
308 talloc_free(timeline);
309 talloc_free(chapters);
310 return;
313 if (missing_time)
314 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
315 "from the timeline!\n", missing_time / 1e9);
316 mpctx->sources = sources;
317 mpctx->num_sources = num_sources;
318 mpctx->timeline = timeline;
319 mpctx->num_timeline_parts = part_count;
320 mpctx->num_chapters = num_chapters;
321 mpctx->chapters = chapters;