summaryrefslogtreecommitdiffstats
path: root/mpvcore/timeline/tl_edl.c
blob: 6fdcd6685c6ff2616756cc18293055a574f6181e (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
/*
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include <ctype.h>

#include "talloc.h"

#include "mpvcore/mp_core.h"
#include "mpvcore/mp_msg.h"
#include "demux/demux.h"
#include "mpvcore/path.h"
#include "mpvcore/bstr.h"
#include "mpvcore/mp_common.h"
#include "stream/stream.h"


struct edl_source {
    struct bstr id;
    char *filename;
    int lineno;
};

struct edl_time {
    int64_t start;
    int64_t end;
    bool implied_start;
    bool implied_end;
};

struct edl_part {
    struct edl_time tl;
    struct edl_time src;
    int64_t duration;
    int id;
    int lineno;
};

static int find_edl_source(struct edl_source *sources, int num_sources,
                           struct bstr name)
{
    for (int i = 0; i < num_sources; i++)
        if (!bstrcmp(sources[i].id, name))
            return i;
    return -1;
}

void build_edl_timeline(struct MPContext *mpctx)
{
    const struct bstr file_prefix = bstr0("<");
    void *tmpmem = talloc_new(NULL);

    struct bstr *lines = bstr_splitlines(tmpmem, mpctx->demuxer->file_contents);
    int linec = MP_TALLOC_ELEMS(lines);
    struct bstr header = bstr0("mplayer EDL file, version ");
    if (!linec || !bstr_startswith(lines[0], header)) {
        mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Bad EDL header!\n");
        goto out;
    }
    struct bstr version = bstr_strip(bstr_cut(lines[0], header.len));
    if (bstrcmp(bstr0("2"), version)) {
        mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Unsupported EDL file version!\n");
        goto out;
    }
    int num_sources = 0;
    int num_parts = 0;
    for (int i = 1; i < linec; i++) {
        if (bstr_startswith(lines[i], file_prefix)) {
            num_sources++;
        } else {
            int comment = bstrchr(lines[i], '#');
            if (comment >= 0)
                lines[i] = bstr_splice(lines[i], 0, comment);
            if (bstr_strip(lines[i]).len)
                num_parts++;
        }
    }
    if (!num_parts) {
        mp_msg(MSGT_CPLAYER, MSGL_ERR, "No parts in timeline!\n");
        goto out;
    }

    // Parse source filename definitions

    struct edl_source *edl_ids = talloc_array_ptrtype(tmpmem, edl_ids,
                                                      num_sources);
    num_sources = 0;
    for (int i = 1; i < linec; i++) {
        struct bstr line = lines[i];
        if (!bstr_startswith(line, file_prefix))
            continue;
        line = bstr_cut(line, file_prefix.len);
        struct bstr id = bstr_split(line, WHITESPACE, &line);
        if (find_edl_source(edl_ids, num_sources, id) >= 0) {
            mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Repeated ID on line %d!\n",
                   i+1);
            goto out;
        }
        if (!isalpha(*id.start)) {
            mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Invalid ID on line %d!\n",
                   i+1);
            goto out;
        }
        char *filename = mp_basename(bstrdup0(tmpmem, bstr_strip(line)));
        if (!strlen(filename)) {
            mp_msg(MSGT_CPLAYER, MSGL_ERR,
                   "EDL: Invalid filename on line %d!\n", i+1);
            goto out;
        }
        struct bstr dirname = mp_dirname(mpctx->demuxer->filename);
        char *fullname = mp_path_join(tmpmem, dirname, bstr0(filename));
        edl_ids[num_sources++] = (struct edl_source){id, fullname, i+1};
    }

    // Parse timeline part definitions

    struct edl_part *parts = talloc_array_ptrtype(tmpmem, parts, num_parts);
    int total_parts = num_parts;
    num_parts = 0;
    for (int i = 1; i < linec; i++) {
        struct bstr line = bstr_strip(lines[i]);
        if (!line.len || bstr_startswith(line, file_prefix))
            continue;
        parts[num_parts] = (struct edl_part){{-1, -1}, {-1, -1}, 0, -1};
        parts[num_parts].lineno = i + 1;
        for (int s = 0; s < 2; s++) {
            struct edl_time *p = !s ? &parts[num_parts].tl :
                &parts[num_parts].src;
            while (1) {
                struct bstr t = bstr_split(line, WHITESPACE, &line);
                if (!t.len) {
                    if (!s && num_parts < total_parts - 1) {
                        mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: missing source "
                               "identifier on line %d (not last)!\n", i+1);
                        goto out;
                    }
                    break;
                }
                if (isalpha(*t.start)) {
                    if (s)
                        goto bad;
                    parts[num_parts].id = find_edl_source(edl_ids, num_sources,
                                                          t);
                    if (parts[num_parts].id < 0) {
                        mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Undefined source "
                               "identifier on line %d!\n", i+1);
                        goto out;
                    }
                    break;
                }
                while (t.len) {
                    struct bstr next;
                    struct bstr arg = bstr_split(t, "+-", &next);
                    if (!arg.len) {
                        next = bstr_split(line, WHITESPACE, &line);
                        arg = bstr_split(next, "+-", &next);
                    }
                    if (!arg.len)
                        goto bad;
                    int64_t val;
                    if (!bstrcmp(arg, bstr0("*")))
                        val = -1;
                    else if (isdigit(*arg.start)) {
                        val = bstrtoll(arg, &arg, 10) * 1000000000;
                        if (arg.len && *arg.start == '.') {
                            int len = arg.len - 1;
                            arg = bstr_splice(arg, 1, 10);
                            int64_t val2 = bstrtoll(arg, &arg, 10);
                            if (arg.len)
                                goto bad;
                            for (; len < 9; len++)
                                val2 *= 10;
                            val += val2;
                        }
                    } else
                        goto bad;
                    int c = *t.start;
                    if (isdigit(c) || c == '*') {
                        if (val < 0)
                            p->implied_start = true;
                        else
                            p->start = val;
                    } else if (c == '-') {
                        if (val < 0)
                            p->implied_end = true;
                        else
                            p->end = val;
                    } else if (c == '+') {
                        if (val < 0)
                            goto bad;
                        if (val == 0) {
                            mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: zero duration "
                                   "on line %d!\n", i+1);
                            goto out;
                        }
                        parts[num_parts].duration = val;
                    } else
                        goto bad;
                    t = next;
                }
            }
        }
        num_parts++;
        continue;
    bad:
        mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Malformed line %d!\n", i+1);
        goto out;
    }

    // Fill in implied start/stop/duration values

    int64_t *times = talloc_zero_array(tmpmem, int64_t, num_sources);
    while (1) {
        int64_t time = 0;
        for (int i = 0; i < num_parts; i++) {
            for (int s = 0; s < 2; s++) {
                struct edl_time *p = s ? &parts[i].tl : &parts[i].src;
                if (!s && parts[i].id == -1)
                    continue;
                int64_t *t = s ? &time : times + parts[i].id;
                p->implied_start |= s && *t >= 0;
                if (p->implied_start && p->start >= 0 && *t >= 0
                    && p->start != *t) {
                    mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line "
                           "%d!\n", parts[i].lineno);
                    goto out;
                }
                if (p->start >= 0)
                    *t = p->start;
                if (p->implied_start)
                    p->start = *t;
                if (*t >= 0 && parts[i].duration)
                    *t += parts[i].duration;
                else
                    *t = -1;
                if (p->end >= 0)
                    *t = p->end;
            }
        }
        for (int i = 0; i < num_sources; i++)
            times[i] = -1;
        time = -1;
        for (int i = num_parts - 1; i >= 0; i--) {
            for (int s = 0; s < 2; s++) {
                struct edl_time *p = s ? &parts[i].tl : &parts[i].src;
                if (!s && parts[i].id == -1)
                    continue;
                int64_t *t = s ? &time : times + parts[i].id;
                p->implied_end |= s && *t >= 0;
                if (p->implied_end && p->end >= 0 && *t >=0 && p->end != *t) {
                    mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line "
                           "%d!\n", parts[i].lineno);
                    goto out;
                }
                if (p->end >= 0)
                    *t = p->end;
                if (p->implied_end)
                    p->end = *t;
                if (*t >= 0 && parts[i].duration) {
                    *t -= parts[i].duration;
                    if (*t < 0) {
                        mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Negative time "
                               "on line %d!\n", parts[i].lineno);
                        goto out;
                    }
                } else
                    *t = -1;
                if (p->start >= 0)
                    *t = p->start;
            }
        }
        int missing_duration = -1;
        int missing_srcstart = -1;
        bool anything_done = false;
        for (int i = 0; i < num_parts; i++) {
            int64_t duration = parts[i].duration;
            if (parts[i].tl.start >= 0 && parts[i].tl.end >= 0) {
                int64_t duration2 = parts[i].tl.end - parts[i].tl.start;
                if (duration && duration != duration2)
                    goto incons;
                duration = duration2;
                if (duration <= 0)
                    goto neg;
            }
            if (parts[i].src.start >= 0 && parts[i].src.end >= 0) {
                int64_t duration2 = parts[i].src.end - parts[i].src.start;
                if (duration && duration != duration2) {
                incons:
                    mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line "
                           "%d!\n", i+1);
                    goto out;
                }
                duration = duration2;
                if (duration <= 0) {
                neg:
                    mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: duration <= 0 on "
                           "line %d!\n", parts[i].lineno);
                    goto out;
                }
            }
            if (parts[i].id == -1)
                continue;
            if (!duration)
                missing_duration = i;
            else if (!parts[i].duration)
                anything_done = true;
            parts[i].duration = duration;
            if (duration && parts[i].src.start < 0) {
                if (parts[i].src.end < 0)
                    missing_srcstart = i;
                else
                    parts[i].src.start = parts[i].src.end - duration;
            }
        }
        if (!anything_done) {
            if (missing_duration >= 0) {
                mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Could not determine "
                        "duration for line %d!\n",
                        parts[missing_duration].lineno);
                goto out;
            }
            if (missing_srcstart >= 0) {
                mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: no source start time for "
                       "line %d!\n", parts[missing_srcstart].lineno);
                    goto out;
            }
            break;
        }
    }

    // Open source files

    struct demuxer **sources = talloc_array_ptrtype(NULL, sources,
                                                    num_sources + 1);
    talloc_free(mpctx->sources);
    mpctx->sources = sources;
    sources[0] = mpctx->demuxer;
    mpctx->num_sources = 1;

    for (int i = 0; i < num_sources; i++) {
        struct stream *s = stream_open(edl_ids[i].filename, mpctx->opts);
        if (!s)
            goto openfail;
        struct demuxer *d = demux_open(s, NULL, NULL, mpctx->opts);
        if (!d) {
            free_stream(s);
        openfail:
            mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Could not open source "
                   "file on line %d!\n", edl_ids[i].lineno);
            goto out;
        }
        sources[mpctx->num_sources] = d;
        mpctx->num_sources++;
    }

    // Write final timeline structure

    struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
                                                          num_parts + 1);
    int64_t starttime = 0;
    for (int i = 0; i < num_parts; i++) {
        timeline[i].start = starttime / 1e9;
        starttime += parts[i].duration;
        timeline[i].source_start = parts[i].src.start / 1e9;
        timeline[i].source = sources[parts[i].id + 1];
    }
    if (parts[num_parts - 1].id != -1) {
        timeline[num_parts].start = starttime / 1e9;
        num_parts++;
    }
    mpctx->timeline = timeline;
    mpctx->num_timeline_parts = num_parts - 1;

 out:
    talloc_free(tmpmem);
}