summaryrefslogtreecommitdiffstats
path: root/demux/demux_mkv_timeline.c
blob: 0c23e27d2576b0d551fdfbd815ccb1e218a9f1ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
 * This file is part of mpv.
 *
 * mpv is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * mpv 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include <assert.h>
#include <dirent.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libavutil/common.h>

#include "osdep/io.h"

#include "mpv_talloc.h"

#include "common/msg.h"
#include "demux/demux.h"
#include "demux/timeline.h"
#include "demux/matroska.h"
#include "options/m_config.h"
#include "options/options.h"
#include "options/path.h"
#include "misc/bstr.h"
#include "misc/thread_tools.h"
#include "common/common.h"
#include "common/playlist.h"
#include "stream/stream.h"

struct tl_ctx {
    struct mp_log *log;
    struct mpv_global *global;
    struct MPOpts *opts;
    struct timeline *tl;

    struct demuxer *demuxer;

    struct demuxer **sources;
    int num_sources;

    struct timeline_part *timeline;
    int num_parts;

    struct matroska_segment_uid *uids;
    uint64_t start_time; // When the next part should start on the complete timeline.
    uint64_t missing_time; // Total missing time so far.
    uint64_t last_end_time; // When the last part ended on the complete timeline.
    int num_chapters; // Total number of expected chapters.
};

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 bool test_matroska_ext(const char *filename)
{
    static const char *const exts[] = {".mkv", ".mka", ".mks", ".mk3d", NULL};
    for (int n = 0; exts[n]; n++) {
        const char *suffix = exts[n];
        int offset = strlen(filename) - strlen(suffix);
        // name must end with suffix
        if (offset > 0 && strcasecmp(filename + offset, suffix) == 0)
            return true;
    }
    return false;
}

static char **find_files(const char *original_file)
{
    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))) {
        if (!test_matroska_ext(ep->d_name))
            continue;
        // don't list the original name
        if (!strcmp(ep->d_name, basename))
            continue;

        char *name = mp_path_join_bstr(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 bool has_source_request(struct tl_ctx *ctx,
                               struct matroska_segment_uid *new_uid)
{
    for (int i = 0; i < ctx->num_sources; ++i) {
        if (demux_matroska_uid_cmp(&ctx->uids[i], new_uid))
            return true;
    }
    return false;
}

// segment = get Nth segment of a multi-segment file
static bool check_file_seg(struct tl_ctx *ctx, char *filename, int segment)
{
    bool was_valid = false;
    struct demuxer_params params = {
        .force_format = "mkv",
        .matroska_num_wanted_uids = ctx->num_sources,
        .matroska_wanted_uids = ctx->uids,
        .matroska_wanted_segment = segment,
        .matroska_was_valid = &was_valid,
        .disable_timeline = true,
        .stream_flags = ctx->tl->stream_origin,
    };
    struct mp_cancel *cancel = ctx->tl->cancel;
    if (mp_cancel_test(cancel))
        return false;

    struct demuxer *d = demux_open_url(filename, &params, cancel, ctx->global);
    if (!d)
        return false;

    struct matroska_data *m = &d->matroska_data;

    for (int i = 1; i < ctx->num_sources; i++) {
        struct matroska_segment_uid *uid = &ctx->uids[i];
        if (ctx->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_INFO(ctx, "Match for source %d: %s\n", i, d->filename);

            if (!uid->edition) {
                m->uid.edition = 0;
            } else {
                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(ctx, &c->uid))
                        continue;

                    /* Set the requested segment. */
                    MP_TARRAY_GROW(NULL, ctx->uids, ctx->num_sources);
                    ctx->uids[ctx->num_sources] = c->uid;

                    /* Add a new source slot. */
                    MP_TARRAY_APPEND(NULL, ctx->sources, ctx->num_sources, NULL);
                }
            }

            ctx->sources[i] = d;
            return true;
        }
    }

    demux_free(d);
    return was_valid;
}

static void check_file(struct tl_ctx *ctx, char *filename, int first)
{
    for (int segment = first; ; segment++) {
        if (!check_file_seg(ctx, filename, segment))
            break;
    }
}

static bool missing(struct tl_ctx *ctx)
{
    for (int i = 0; i < ctx->num_sources; i++) {
        if (!ctx->sources[i])
            return true;
    }
    return false;
}

static void find_ordered_chapter_sources(struct tl_ctx *ctx)
{
    struct MPOpts *opts = ctx->opts;
    void *tmp = talloc_new(NULL);
    int num_filenames = 0;
    char **filenames = NULL;
    if (ctx->num_sources > 1) {
        char *main_filename = ctx->demuxer->filename;
        MP_INFO(ctx, "This file references data from other sources.\n");
        if (opts->ordered_chapters_files && opts->ordered_chapters_files[0]) {
            MP_INFO(ctx, "Loading references from '%s'.\n",
                    opts->ordered_chapters_files);
            struct playlist *pl =
                playlist_parse_file(opts->ordered_chapters_files,
                                    ctx->tl->cancel, ctx->global);
            talloc_steal(tmp, pl);
            for (int n = 0; n < pl->num_entries; n++) {
                MP_TARRAY_APPEND(tmp, filenames, num_filenames,
                                 pl->entries[n]->filename);
            }
        } else if (!ctx->demuxer->stream->is_local_file) {
            MP_WARN(ctx, "Playback source is not a "
                    "normal disk file. Will not search for related files.\n");
        } else {
            MP_INFO(ctx, "Will scan other files in the "
                    "same directory to find referenced sources.\n");
            filenames = find_files(main_filename);
            num_filenames = MP_TALLOC_AVAIL(filenames);
            talloc_steal(tmp, filenames);
        }
        // Possibly get further segments appended to the first segment
        check_file(ctx, main_filename, 1);
    }

    int old_source_count;
    do {
        old_source_count = ctx->num_sources;
        for (int i = 0; i < num_filenames; i++) {
            if (!missing(ctx))
                break;
            MP_VERBOSE(ctx, "Checking file %s\n", filenames[i]);
            check_file(ctx, filenames[i], 0);
        }
    } while (old_source_count != ctx->num_sources);

    if (missing(ctx)) {
        MP_ERR(ctx, "Failed to find ordered chapter part!\n");
        int j = 1;
        for (int i = 1; i < ctx->num_sources; i++) {
            if (ctx->sources[i]) {
                ctx->sources[j] = ctx->sources[i];
                ctx->uids[j] = ctx->uids[i];
                j++;
            }
        }
        ctx->num_sources = j;
    }

    // Copy attachments from referenced sources so fonts are loaded for sub
    // rendering.
    for (int i = 1; i < ctx->num_sources; i++) {
        for (int j = 0; j < ctx->sources[i]->num_attachments; j++) {
            struct demux_attachment *att = &ctx->sources[i]->attachments[j];
            demuxer_add_attachment(ctx->demuxer, att->name, att->type,
                                   att->data, att->data_size);
        }
    }

    talloc_free(tmp);
}

struct inner_timeline_info {
    uint64_t skip; // Amount of time to skip.
    uint64_t limit; // How much time is expected for the parent chapter.
};

static int64_t add_timeline_part(struct tl_ctx *ctx,
                                 struct demuxer *source,
                                 uint64_t start)
{
    /* Merge directly adjacent parts. 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 - ctx->last_end_time;
    if (ctx->num_parts == 0
        || FFABS(join_diff) > ctx->opts->chapter_merge_threshold * 1e6
        || source != ctx->timeline[ctx->num_parts - 1].source)
    {
        struct timeline_part new = {
            .start = ctx->start_time / 1e9,
            .source_start = start / 1e9,
            .source = source,
        };
        MP_TARRAY_APPEND(NULL, ctx->timeline, ctx->num_parts, new);
    } else if (ctx->num_parts > 0 && join_diff) {
        // Chapter was merged at an inexact boundary; adjust timestamps to match.
        MP_VERBOSE(ctx, "Merging timeline part %d with offset %g ms.\n",
                   ctx->num_parts, join_diff / 1e6);
        ctx->start_time += join_diff;
        return join_diff;
    }

    return 0;
}

static void build_timeline_loop(struct tl_ctx *ctx,
                                struct demux_chapter *chapters,
                                struct inner_timeline_info *info,
                                int current_source)
{
    uint64_t local_starttime = 0;
    struct demuxer *source = ctx->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;

        if (!c->has_segment_uid)
            c->uid = m->uid;

        local_starttime += chapter_length;

        // If we're before the start time for the chapter, skip to the next one.
        if (local_starttime <= info->skip)
            continue;

        /* Look for the source for this chapter. */
        for (int j = 0; j < ctx->num_sources; j++) {
            struct demuxer *linked_source = ctx->sources[j];
            struct matroska_data *linked_m = &linked_source->matroska_data;

            if (!demux_matroska_uid_cmp(&c->uid, &linked_m->uid))
                continue;

            if (!info->limit) {
                if (i >= ctx->num_chapters)
                    break; // malformed files can cause this to happen.

                chapters[i].pts = ctx->start_time / 1e9;
                chapters[i].metadata = talloc_zero(chapters, struct mp_tags);
                mp_tags_set_str(chapters[i].metadata, "title", 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->uid.edition) {
                uint64_t source_full_length = linked_source->duration * 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) {
                    ctx->missing_time += chapter_length;
                    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) {
                    ctx->missing_time += chapter_length - source_length;
                    chapter_length = source_length;
                }

                join_diff = add_timeline_part(ctx, linked_source, c->start);

                /* 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 (info->limit) {
                    info->limit += join_diff;
                    chapter_length += join_diff;
                }
            } else {
                /* 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. */
                struct inner_timeline_info new_info = {
                    .skip = c->start,
                    .limit = c->end
                };
                build_timeline_loop(ctx, chapters, &new_info, j);
                // Already handled by the loop call.
                chapter_length = 0;
            }
            ctx->last_end_time = c->end;
            goto found;
        }

        ctx->missing_time += chapter_length;
        chapter_length = 0;
    found:;
        ctx->start_time += chapter_length;
        /* If we're after the limit on this chapter, stop here. */
        if (info->limit && local_starttime >= info->limit) {
            /* Back up the global start time by the overflow. */
            ctx->start_time -= local_starttime - info->limit;
            break;
        }
    }

    /* If we stopped before the limit, add up the missing time. */
    if (local_starttime < info->limit)
        ctx->missing_time += info->limit - local_starttime;
}

static void check_track_compatibility(struct tl_ctx *tl, struct demuxer *mainsrc)
{
    for (int n = 0; n < tl->num_parts; n++) {
        struct timeline_part *p = &tl->timeline[n];
        if (p->source == mainsrc)
            continue;

        int num_source_streams = demux_get_num_stream(p->source);
        for (int i = 0; i < num_source_streams; i++) {
            struct sh_stream *s = demux_get_stream(p->source, i);
            if (s->attached_picture)
                continue;

            if (!demuxer_stream_by_demuxer_id(mainsrc, s->type, s->demuxer_id)) {
                MP_WARN(tl, "Source %s has %s stream with TID=%d, which "
                            "is not present in the ordered chapters main "
                            "file. This is a broken file. "
                            "The additional stream is ignored.\n",
                            p->source->filename, stream_type_name(s->type),
                            s->demuxer_id);
            }
        }

        int num_main_streams = demux_get_num_stream(mainsrc);
        for (int i = 0; i < num_main_streams; i++) {
            struct sh_stream *m = demux_get_stream(mainsrc, i);
            if (m->attached_picture)
                continue;

            struct sh_stream *s =
                demuxer_stream_by_demuxer_id(p->source, m->type, m->demuxer_id);
            if (s) {
                // There are actually many more things that in theory have to
                // match (though mpv's implementation doesn't care).
                if (strcmp(s->codec->codec, m->codec->codec) != 0)
                    MP_WARN(tl, "Timeline segments have mismatching codec.\n");
                if (s->codec->extradata_size != m->codec->extradata_size ||
                    (s->codec->extradata_size &&
                        memcmp(s->codec->extradata, m->codec->extradata,
                               s->codec->extradata_size) != 0))
                    MP_WARN(tl, "Timeline segments have mismatching codec info.\n");
            } else {
                MP_WARN(tl, "Source %s lacks %s stream with TID=%d, which "
                            "is present in the ordered chapters main "
                            "file. This is a broken file.\n",
                            p->source->filename, stream_type_name(m->type),
                            m->demuxer_id);
            }
        }
    }
}

void build_ordered_chapter_timeline(struct timeline *tl)
{
    struct demuxer *demuxer = tl->demuxer;

    if (!demuxer->matroska_data.ordered_chapters)
        return;

    struct tl_ctx *ctx = talloc_ptrtype(tl, ctx);
    *ctx = (struct tl_ctx){
        .log = tl->log,
        .global = tl->global,
        .tl = tl,
        .demuxer = demuxer,
        .opts = mp_get_config_group(ctx, tl->global, &mp_opt_root),
    };

    if (!ctx->opts->ordered_chapters || !demuxer->access_references) {
        MP_INFO(demuxer, "File uses ordered chapters, but "
                "you have disabled support for them. Ignoring.\n");
        talloc_free(ctx);
        return;
    }

    MP_INFO(ctx, "File uses ordered chapters, will build edit timeline.\n");

    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
    ctx->sources = talloc_zero_array(tl, struct demuxer *,
                                     m->num_ordered_chapters + 1);
    ctx->sources[0] = demuxer;
    ctx->num_sources = 1;

    ctx->uids = talloc_zero_array(NULL, struct matroska_segment_uid,
                                  m->num_ordered_chapters + 1);
    ctx->uids[0] = m->uid;
    ctx->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(ctx, &c->uid))
            continue;

        ctx->uids[ctx->num_sources] = c->uid;
        ctx->sources[ctx->num_sources] = NULL;
        ctx->num_sources++;
    }

    find_ordered_chapter_sources(ctx);

    talloc_free(ctx->uids);
    ctx->uids = NULL;

    struct demux_chapter *chapters =
        talloc_zero_array(tl, struct demux_chapter, m->num_ordered_chapters);

    ctx->timeline = talloc_array_ptrtype(tl, ctx->timeline, 0);
    ctx->num_chapters = m->num_ordered_chapters;

    struct inner_timeline_info info = {
        .skip = 0,
        .limit = 0
    };
    build_timeline_loop(ctx, chapters, &info, 0);

    // Fuck everything: filter out all "unset" chapters.
    for (int n = m->num_ordered_chapters - 1; n >= 0; n--) {
        if (!chapters[n].metadata)
            MP_TARRAY_REMOVE_AT(chapters, m->num_ordered_chapters, n);
    }

    if (!ctx->num_parts) {
        // None of  the parts come from the file itself???
        // Broken file, but we need at least 1 valid timeline part - add a dummy.
        MP_WARN(ctx, "Ordered chapters file with no parts?\n");
        struct timeline_part new = {
            .source = demuxer,
        };
        MP_TARRAY_APPEND(NULL, ctx->timeline, ctx->num_parts, new);
    }

    for (int n = 0; n < ctx->num_parts; n++) {
        ctx->timeline[n].end = n == ctx->num_parts - 1
            ? ctx->start_time / 1e9
            : ctx->timeline[n + 1].start;
    };

    /* 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 (ctx->missing_time >= 1e6) {
        MP_ERR(ctx, "There are %.3f seconds missing from the timeline!\n",
               ctx->missing_time / 1e9);
    }

    // With Matroska, the "master" file usually dictates track layout etc.,
    // except maybe with playlist-like files.
    struct demuxer *track_layout = ctx->timeline[0].source;
    for (int n = 0; n < ctx->num_parts; n++) {
        if (ctx->timeline[n].source == ctx->demuxer) {
            track_layout = ctx->demuxer;
            break;
        }
    }

    check_track_compatibility(ctx, track_layout);

    tl->sources = ctx->sources;
    tl->num_sources = ctx->num_sources;

    struct timeline_par *par = talloc_ptrtype(tl, par);
    *par = (struct timeline_par){
        .parts = ctx->timeline,
        .num_parts = ctx->num_parts,
        .track_layout = track_layout,
    };
    MP_TARRAY_APPEND(tl, tl->pars, tl->num_pars, par);
    tl->chapters = chapters;
    tl->num_chapters = m->num_ordered_chapters;
    tl->meta = track_layout;
    tl->format = "mkv_oc";
}