stream.c: make some stream messages translatable
[mplayer.git] / timeline / tl_edl.c
blob99b9ddad82dce5b7b12b67d26ccca383ee2dc708
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 <ctype.h>
24 #include "talloc.h"
26 #include "mp_core.h"
27 #include "mp_msg.h"
28 #include "libmpdemux/demuxer.h"
29 #include "path.h"
30 #include "bstr.h"
31 #include "mpcommon.h"
34 struct edl_source {
35 struct bstr id;
36 char *filename;
37 int lineno;
40 struct edl_time {
41 int64_t start;
42 int64_t end;
43 bool implied_start;
44 bool implied_end;
47 struct edl_part {
48 struct edl_time tl;
49 struct edl_time src;
50 int64_t duration;
51 int id;
52 int lineno;
55 static int find_edl_source(struct edl_source *sources, int num_sources,
56 struct bstr name)
58 for (int i = 0; i < num_sources; i++)
59 if (!bstrcmp(sources[i].id, name))
60 return i;
61 return -1;
64 void build_edl_timeline(struct MPContext *mpctx)
66 const struct bstr file_prefix = BSTR("<");
67 void *tmpmem = talloc_new(NULL);
69 struct bstr *lines = bstr_splitlines(tmpmem, mpctx->demuxer->file_contents);
70 int linec = MP_TALLOC_ELEMS(lines);
71 struct bstr header = BSTR("mplayer EDL file, version ");
72 if (!linec || !bstr_startswith(lines[0], header)) {
73 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Bad EDL header!\n");
74 goto out;
76 struct bstr version = bstr_strip(bstr_cut(lines[0], header.len));
77 if (bstrcmp(BSTR("2"), version)) {
78 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Unsupported EDL file version!\n");
79 goto out;
81 int num_sources = 0;
82 int num_parts = 0;
83 for (int i = 1; i < linec; i++) {
84 if (bstr_startswith(lines[i], file_prefix)) {
85 num_sources++;
86 } else {
87 int comment = bstrchr(lines[i], '#');
88 if (comment >= 0)
89 lines[i] = bstr_splice(lines[i], 0, comment);
90 if (bstr_strip(lines[i]).len)
91 num_parts++;
94 if (!num_parts) {
95 mp_msg(MSGT_CPLAYER, MSGL_ERR, "No parts in timeline!\n");
96 goto out;
99 // Parse source filename definitions
101 struct edl_source *edl_ids = talloc_array_ptrtype(tmpmem, edl_ids,
102 num_sources);
103 num_sources = 0;
104 for (int i = 1; i < linec; i++) {
105 struct bstr line = lines[i];
106 if (!bstr_startswith(line, file_prefix))
107 continue;
108 line = bstr_cut(line, file_prefix.len);
109 struct bstr id = bstr_split(line, WHITESPACE, &line);
110 if (find_edl_source(edl_ids, num_sources, id) >= 0) {
111 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Repeated ID on line %d!\n",
112 i+1);
113 goto out;
115 if (!isalpha(*id.start)) {
116 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Invalid ID on line %d!\n",
117 i+1);
118 goto out;
120 char *filename = mp_basename(bstrdup0(tmpmem, bstr_strip(line)));
121 if (!strlen(filename)) {
122 mp_msg(MSGT_CPLAYER, MSGL_ERR,
123 "EDL: Invalid filename on line %d!\n", i+1);
124 goto out;
126 struct bstr dirname = mp_dirname(mpctx->demuxer->filename);
127 char *fullname = mp_path_join(tmpmem, dirname, BSTR(filename));
128 edl_ids[num_sources++] = (struct edl_source){id, fullname, i+1};
131 // Parse timeline part definitions
133 struct edl_part *parts = talloc_array_ptrtype(tmpmem, parts, num_parts);
134 int total_parts = num_parts;
135 num_parts = 0;
136 for (int i = 1; i < linec; i++) {
137 struct bstr line = bstr_strip(lines[i]);
138 if (!line.len || bstr_startswith(line, file_prefix))
139 continue;
140 parts[num_parts] = (struct edl_part){{-1, -1}, {-1, -1}, 0, -1};
141 parts[num_parts].lineno = i + 1;
142 for (int s = 0; s < 2; s++) {
143 struct edl_time *p = !s ? &parts[num_parts].tl :
144 &parts[num_parts].src;
145 while (1) {
146 struct bstr t = bstr_split(line, WHITESPACE, &line);
147 if (!t.len) {
148 if (!s && num_parts < total_parts - 1) {
149 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: missing source "
150 "identifier on line %d (not last)!\n", i+1);
151 goto out;
153 break;
155 if (isalpha(*t.start)) {
156 if (s)
157 goto bad;
158 parts[num_parts].id = find_edl_source(edl_ids, num_sources,
160 if (parts[num_parts].id < 0) {
161 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Undefined source "
162 "identifier on line %d!\n", i+1);
163 goto out;
165 break;
167 while (t.len) {
168 struct bstr next;
169 struct bstr arg = bstr_split(t, "+-", &next);
170 if (!arg.len) {
171 next = bstr_split(line, WHITESPACE, &line);
172 arg = bstr_split(next, "+-", &next);
174 if (!arg.len)
175 goto bad;
176 int64_t val;
177 if (!bstrcmp(arg, BSTR("*")))
178 val = -1;
179 else if (isdigit(*arg.start)) {
180 val = bstrtoll(arg, &arg, 10) * 1000000000;
181 if (arg.len && *arg.start == '.') {
182 int len = arg.len - 1;
183 arg = bstr_splice(arg, 1, 10);
184 int64_t val2 = bstrtoll(arg, &arg, 10);
185 if (arg.len)
186 goto bad;
187 for (; len < 9; len++)
188 val2 *= 10;
189 val += val2;
191 } else
192 goto bad;
193 int c = *t.start;
194 if (isdigit(c) || c == '*') {
195 if (val < 0)
196 p->implied_start = true;
197 else
198 p->start = val;
199 } else if (c == '-') {
200 if (val < 0)
201 p->implied_end = true;
202 else
203 p->end = val;
204 } else if (c == '+') {
205 if (val < 0)
206 goto bad;
207 if (val == 0) {
208 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: zero duration "
209 "on line %d!\n", i+1);
210 goto out;
212 parts[num_parts].duration = val;
213 } else
214 goto bad;
215 t = next;
219 num_parts++;
220 continue;
221 bad:
222 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Malformed line %d!\n", i+1);
223 goto out;
226 // Fill in implied start/stop/duration values
228 int64_t *times = talloc_zero_array(tmpmem, int64_t, num_sources);
229 while (1) {
230 int64_t time = 0;
231 for (int i = 0; i < num_parts; i++) {
232 for (int s = 0; s < 2; s++) {
233 struct edl_time *p = s ? &parts[i].tl : &parts[i].src;
234 if (!s && parts[i].id == -1)
235 continue;
236 int64_t *t = s ? &time : times + parts[i].id;
237 p->implied_start |= s && *t >= 0;
238 if (p->implied_start && p->start >= 0 && *t >= 0
239 && p->start != *t) {
240 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line "
241 "%d!\n", parts[i].lineno);
242 goto out;
244 if (p->start >= 0)
245 *t = p->start;
246 if (p->implied_start)
247 p->start = *t;
248 if (*t >= 0 && parts[i].duration)
249 *t += parts[i].duration;
250 else
251 *t = -1;
252 if (p->end >= 0)
253 *t = p->end;
256 for (int i = 0; i < num_sources; i++)
257 times[i] = -1;
258 time = -1;
259 for (int i = num_parts - 1; i >= 0; i--) {
260 for (int s = 0; s < 2; s++) {
261 struct edl_time *p = s ? &parts[i].tl : &parts[i].src;
262 if (!s && parts[i].id == -1)
263 continue;
264 int64_t *t = s ? &time : times + parts[i].id;
265 p->implied_end |= s && *t >= 0;
266 if (p->implied_end && p->end >= 0 && *t >=0 && p->end != *t) {
267 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line "
268 "%d!\n", parts[i].lineno);
269 goto out;
271 if (p->end >= 0)
272 *t = p->end;
273 if (p->implied_end)
274 p->end = *t;
275 if (*t >= 0 && parts[i].duration) {
276 *t -= parts[i].duration;
277 if (t < 0) {
278 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Negative time "
279 "on line %d!\n", parts[i].lineno);
280 goto out;
282 } else
283 *t = -1;
284 if (p->start >= 0)
285 *t = p->start;
288 int missing_duration = -1;
289 int missing_srcstart = -1;
290 bool anything_done = false;
291 for (int i = 0; i < num_parts; i++) {
292 int64_t duration = parts[i].duration;
293 if (parts[i].tl.start >= 0 && parts[i].tl.end >= 0) {
294 int64_t duration2 = parts[i].tl.end - parts[i].tl.start;
295 if (duration && duration != duration2)
296 goto incons;
297 duration = duration2;
298 if (duration <= 0)
299 goto neg;
301 if (parts[i].src.start >= 0 && parts[i].src.end >= 0) {
302 int64_t duration2 = parts[i].src.end - parts[i].src.start;
303 if (duration && duration != duration2) {
304 incons:
305 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Inconsistent line "
306 "%d!\n", i+1);
307 goto out;
309 duration = duration2;
310 if (duration <= 0) {
311 neg:
312 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: duration <= 0 on "
313 "line %d!\n", parts[i].lineno);
314 goto out;
317 if (parts[i].id == -1)
318 continue;
319 if (!duration)
320 missing_duration = i;
321 else if (!parts[i].duration)
322 anything_done = true;
323 parts[i].duration = duration;
324 if (duration && parts[i].src.start < 0)
325 if (parts[i].src.end < 0)
326 missing_srcstart = i;
327 else
328 parts[i].src.start = parts[i].src.end - duration;
330 if (!anything_done) {
331 if (missing_duration >= 0) {
332 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Could not determine "
333 "duration for line %d!\n",
334 parts[missing_duration].lineno);
335 goto out;
337 if (missing_srcstart >= 0) {
338 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: no source start time for "
339 "line %d!\n", parts[missing_srcstart].lineno);
340 goto out;
342 break;
346 // Open source files
348 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
349 num_sources + 1);
350 mpctx->sources = sources;
351 sources[0].stream = mpctx->stream;
352 sources[0].demuxer = mpctx->demuxer;
353 mpctx->num_sources = 1;
355 for (int i = 0; i < num_sources; i++) {
356 int format = 0;
357 struct stream *s = open_stream(edl_ids[i].filename, &mpctx->opts,
358 &format);
359 if (!s)
360 goto openfail;
361 struct demuxer *d = demux_open(&mpctx->opts, s, format,
362 mpctx->opts.audio_id,
363 mpctx->opts.video_id,
364 mpctx->opts.sub_id,
365 edl_ids[i].filename);
366 if (!d) {
367 free_stream(s);
368 openfail:
369 mp_msg(MSGT_CPLAYER, MSGL_ERR, "EDL: Could not open source "
370 "file on line %d!\n", edl_ids[i].lineno);
371 goto out;
373 sources[mpctx->num_sources].stream = s;
374 sources[mpctx->num_sources].demuxer = d;
375 mpctx->num_sources++;
378 // Write final timeline structure
380 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
381 num_parts + 1);
382 int64_t starttime = 0;
383 for (int i = 0; i < num_parts; i++) {
384 timeline[i].start = starttime / 1e9;
385 starttime += parts[i].duration;
386 timeline[i].source_start = parts[i].src.start / 1e9;
387 timeline[i].source = sources + parts[i].id + 1;
389 if (parts[num_parts - 1].id != -1) {
390 timeline[num_parts].start = starttime / 1e9;
391 num_parts++;
393 mpctx->timeline = timeline;
394 mpctx->num_timeline_parts = num_parts - 1;
396 out:
397 talloc_free(tmpmem);