Rename Subtitle -> Text
[dcpomatic.git] / src / lib / playlist.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "playlist.h"
22 #include "video_content.h"
23 #include "text_content.h"
24 #include "ffmpeg_decoder.h"
25 #include "ffmpeg_content.h"
26 #include "image_decoder.h"
27 #include "audio_content.h"
28 #include "content_factory.h"
29 #include "dcp_content.h"
30 #include "job.h"
31 #include "config.h"
32 #include "util.h"
33 #include "digester.h"
34 #include <libcxml/cxml.h>
35 #include <libxml++/libxml++.h>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/foreach.hpp>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 using std::list;
43 using std::cout;
44 using std::vector;
45 using std::min;
46 using std::max;
47 using std::string;
48 using std::pair;
49 using boost::optional;
50 using boost::shared_ptr;
51 using boost::weak_ptr;
52 using boost::dynamic_pointer_cast;
53
54 Playlist::Playlist ()
55         : _sequence (true)
56         , _sequencing (false)
57 {
58
59 }
60
61 Playlist::~Playlist ()
62 {
63         _content.clear ();
64         reconnect ();
65 }
66
67 void
68 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
69 {
70         if (
71                 property == ContentProperty::TRIM_START ||
72                 property == ContentProperty::TRIM_END ||
73                 property == ContentProperty::LENGTH ||
74                 property == VideoContentProperty::FRAME_TYPE
75                 ) {
76                 /* Don't respond to position changes here, as:
77                    - sequencing after earlier/later changes is handled by move_earlier/move_later
78                    - any other position changes will be timeline drags which should not result in content
79                    being sequenced.
80                 */
81                 maybe_sequence ();
82         }
83
84         if (
85                 property == ContentProperty::POSITION ||
86                 property == ContentProperty::LENGTH ||
87                 property == ContentProperty::TRIM_START ||
88                 property == ContentProperty::TRIM_END) {
89
90                 ContentList old = _content;
91                 sort (_content.begin(), _content.end(), ContentSorter ());
92                 if (_content != old) {
93                         OrderChanged ();
94                 }
95         }
96
97         ContentChanged (content, property, frequent);
98 }
99
100 void
101 Playlist::maybe_sequence ()
102 {
103         if (!_sequence || _sequencing) {
104                 return;
105         }
106
107         _sequencing = true;
108
109         /* Keep track of the content that we've set the position of so that we don't
110            do it twice.
111         */
112         ContentList placed;
113
114         /* Video */
115
116         DCPTime next_left;
117         DCPTime next_right;
118         BOOST_FOREACH (shared_ptr<Content> i, _content) {
119                 if (!i->video) {
120                         continue;
121                 }
122
123                 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
124                         i->set_position (next_right);
125                         next_right = i->end();
126                 } else {
127                         i->set_position (next_left);
128                         next_left = i->end();
129                 }
130
131                 placed.push_back (i);
132         }
133
134         /* Subtitles */
135
136         DCPTime next;
137         BOOST_FOREACH (shared_ptr<Content> i, _content) {
138                 if (!i->subtitle || find (placed.begin(), placed.end(), i) != placed.end()) {
139                         continue;
140                 }
141
142                 i->set_position (next);
143                 next = i->end();
144         }
145
146
147         /* This won't change order, so it does not need a sort */
148
149         _sequencing = false;
150 }
151
152 string
153 Playlist::video_identifier () const
154 {
155         string t;
156
157         BOOST_FOREACH (shared_ptr<const Content> i, _content) {
158                 if (i->video || (i->subtitle && i->subtitle->burn())) {
159                         t += i->identifier ();
160                 }
161         }
162
163         Digester digester;
164         digester.add (t.c_str(), t.length());
165         return digester.get ();
166 }
167
168 /** @param film Film that this Playlist is for.
169  *  @param node &lt;Playlist&gt; node.
170  *  @param version Metadata version number.
171  *  @param notes Output notes about that happened.
172  */
173 void
174 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
175 {
176         BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
177                 _content.push_back (content_factory (film, i, version, notes));
178         }
179
180         /* This shouldn't be necessary but better safe than sorry (there could be old files) */
181         sort (_content.begin(), _content.end(), ContentSorter ());
182
183         reconnect ();
184 }
185
186 /** @param node &lt;Playlist&gt; node.
187  *  @param with_content_paths true to include &lt;Path&gt; nodes in &lt;Content&gt; nodes, false to omit them.
188  */
189 void
190 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
191 {
192         BOOST_FOREACH (shared_ptr<Content> i, _content) {
193                 i->as_xml (node->add_child ("Content"), with_content_paths);
194         }
195 }
196
197 void
198 Playlist::add (shared_ptr<Content> c)
199 {
200         _content.push_back (c);
201         sort (_content.begin(), _content.end(), ContentSorter ());
202         reconnect ();
203         Changed ();
204 }
205
206 void
207 Playlist::remove (shared_ptr<Content> c)
208 {
209         ContentList::iterator i = _content.begin ();
210         while (i != _content.end() && *i != c) {
211                 ++i;
212         }
213
214         if (i != _content.end ()) {
215                 _content.erase (i);
216                 Changed ();
217         }
218
219         /* This won't change order, so it does not need a sort */
220 }
221
222 void
223 Playlist::remove (ContentList c)
224 {
225         BOOST_FOREACH (shared_ptr<Content> i, c) {
226                 ContentList::iterator j = _content.begin ();
227                 while (j != _content.end() && *j != i) {
228                         ++j;
229                 }
230
231                 if (j != _content.end ()) {
232                         _content.erase (j);
233                 }
234         }
235
236         /* This won't change order, so it does not need a sort */
237
238         Changed ();
239 }
240
241 class FrameRateCandidate
242 {
243 public:
244         FrameRateCandidate (float source_, int dcp_)
245                 : source (source_)
246                 , dcp (dcp_)
247         {}
248
249         float source;
250         int dcp;
251 };
252
253 int
254 Playlist::best_video_frame_rate () const
255 {
256         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
257
258         /* Work out what rates we could manage, including those achieved by using skip / repeat */
259         list<FrameRateCandidate> candidates;
260
261         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
262         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
263                 candidates.push_back (FrameRateCandidate (*i, *i));
264         }
265
266         /* Then the skip/repeat ones */
267         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
268                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
269                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
270         }
271
272         /* Pick the best one */
273         float error = std::numeric_limits<float>::max ();
274         optional<FrameRateCandidate> best;
275         list<FrameRateCandidate>::iterator i = candidates.begin();
276         while (i != candidates.end()) {
277
278                 float this_error = 0;
279                 BOOST_FOREACH (shared_ptr<Content> j, _content) {
280                         if (!j->video || !j->video_frame_rate()) {
281                                 continue;
282                         }
283
284                         /* Best error for this content; we could use the content as-is or double its rate */
285                         float best_error = min (
286                                 float (fabs (i->source - j->video_frame_rate().get())),
287                                 float (fabs (i->source - j->video_frame_rate().get() * 2))
288                                 );
289
290                         /* Use the largest difference between DCP and source as the "error" */
291                         this_error = max (this_error, best_error);
292                 }
293
294                 if (this_error < error) {
295                         error = this_error;
296                         best = *i;
297                 }
298
299                 ++i;
300         }
301
302         if (!best) {
303                 return 24;
304         }
305
306         return best->dcp;
307 }
308
309 /** @return length of the playlist from time 0 to the last thing on the playlist */
310 DCPTime
311 Playlist::length () const
312 {
313         DCPTime len;
314         BOOST_FOREACH (shared_ptr<const Content> i, _content) {
315                 len = max (len, i->end());
316         }
317
318         return len;
319 }
320
321 /** @return position of the first thing on the playlist, if it's not empty */
322 optional<DCPTime>
323 Playlist::start () const
324 {
325         if (_content.empty ()) {
326                 return optional<DCPTime> ();
327         }
328
329         DCPTime start = DCPTime::max ();
330         BOOST_FOREACH (shared_ptr<Content> i, _content) {
331                 start = min (start, i->position ());
332         }
333
334         return start;
335 }
336
337 void
338 Playlist::reconnect ()
339 {
340         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
341                 i->disconnect ();
342         }
343
344         _content_connections.clear ();
345
346         BOOST_FOREACH (shared_ptr<Content> i, _content) {
347                 _content_connections.push_back (i->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
348         }
349 }
350
351 DCPTime
352 Playlist::video_end () const
353 {
354         DCPTime end;
355         BOOST_FOREACH (shared_ptr<Content> i, _content) {
356                 if (i->video) {
357                         end = max (end, i->end ());
358                 }
359         }
360
361         return end;
362 }
363
364 DCPTime
365 Playlist::subtitle_end () const
366 {
367         DCPTime end;
368         BOOST_FOREACH (shared_ptr<Content> i, _content) {
369                 if (i->subtitle) {
370                         end = max (end, i->end ());
371                 }
372         }
373
374         return end;
375 }
376
377 FrameRateChange
378 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
379 {
380         for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
381                 if (!(*i)->video) {
382                         continue;
383                 }
384
385                 if ((*i)->position() <= t) {
386                         /* This is the first piece of content (going backwards...) that starts before t,
387                            so it's the active one.
388                         */
389                         if ((*i)->video_frame_rate ()) {
390                                 /* This content specified a rate, so use it */
391                                 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
392                         } else {
393                                 /* No specified rate so just use the DCP one */
394                                 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
395                         }
396                 }
397         }
398
399         return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
400 }
401
402 void
403 Playlist::set_sequence (bool s)
404 {
405         _sequence = s;
406 }
407
408 bool
409 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
410 {
411         if (a->position() != b->position()) {
412                 return a->position() < b->position();
413         }
414
415         /* Put video before audio if they start at the same time */
416         if (a->video && !b->video) {
417                 return true;
418         } else if (!a->video && b->video) {
419                 return false;
420         }
421
422         /* Last resort */
423         return a->digest() < b->digest();
424 }
425
426 /** @return content in ascending order of position */
427 ContentList
428 Playlist::content () const
429 {
430         return _content;
431 }
432
433 void
434 Playlist::repeat (ContentList c, int n)
435 {
436         pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
437         BOOST_FOREACH (shared_ptr<Content> i, c) {
438                 range.first = min (range.first, i->position ());
439                 range.second = max (range.second, i->position ());
440                 range.first = min (range.first, i->end ());
441                 range.second = max (range.second, i->end ());
442         }
443
444         DCPTime pos = range.second;
445         for (int i = 0; i < n; ++i) {
446                 BOOST_FOREACH (shared_ptr<Content> j, c) {
447                         shared_ptr<Content> copy = j->clone ();
448                         copy->set_position (pos + copy->position() - range.first);
449                         _content.push_back (copy);
450                 }
451                 pos += range.second - range.first;
452         }
453
454         sort (_content.begin(), _content.end(), ContentSorter ());
455
456         reconnect ();
457         Changed ();
458 }
459
460 void
461 Playlist::move_earlier (shared_ptr<Content> c)
462 {
463         ContentList::iterator previous = _content.end ();
464         ContentList::iterator i = _content.begin();
465         while (i != _content.end() && *i != c) {
466                 previous = i;
467                 ++i;
468         }
469
470         DCPOMATIC_ASSERT (i != _content.end ());
471         if (previous == _content.end ()) {
472                 return;
473         }
474
475         shared_ptr<Content> previous_c = *previous;
476
477         DCPTime const p = previous_c->position ();
478         previous_c->set_position (p + c->length_after_trim ());
479         c->set_position (p);
480 }
481
482 void
483 Playlist::move_later (shared_ptr<Content> c)
484 {
485         ContentList::iterator i = _content.begin();
486         while (i != _content.end() && *i != c) {
487                 ++i;
488         }
489
490         DCPOMATIC_ASSERT (i != _content.end ());
491
492         ContentList::iterator next = i;
493         ++next;
494
495         if (next == _content.end ()) {
496                 return;
497         }
498
499         shared_ptr<Content> next_c = *next;
500
501         next_c->set_position (c->position ());
502         c->set_position (c->position() + next_c->length_after_trim ());
503 }
504
505 int64_t
506 Playlist::required_disk_space (int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
507 {
508         int64_t video = uint64_t (j2k_bandwidth / 8) * length().seconds ();
509         int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length().seconds ();
510
511         BOOST_FOREACH (shared_ptr<Content> i, _content) {
512                 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
513                 if (d) {
514                         if (d->reference_video()) {
515                                 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim().seconds();
516                         }
517                         if (d->reference_audio()) {
518                                 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim().seconds();
519                         }
520                 }
521         }
522
523         /* Add on 64k for bits and pieces (metadata, subs etc) */
524         return video + audio + 65536;
525 }
526
527 string
528 Playlist::content_summary (DCPTimePeriod period) const
529 {
530         string best_summary;
531         int best_score = -1;
532         BOOST_FOREACH (shared_ptr<Content> i, _content) {
533                 int score = 0;
534                 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end()).overlap (period);
535                 if (o) {
536                         score += 100 * o.get().duration().get() / period.duration().get();
537                 }
538
539                 if (i->video) {
540                         score += 100;
541                 }
542
543                 if (score > best_score) {
544                         best_summary = i->path(0).leaf().string();
545                         best_score = score;
546                 }
547         }
548
549         return best_summary;
550 }
551
552 pair<double, double>
553 Playlist::speed_up_range (int dcp_video_frame_rate) const
554 {
555         pair<double, double> range (DBL_MAX, -DBL_MAX);
556
557         BOOST_FOREACH (shared_ptr<Content> i, _content) {
558                 if (!i->video) {
559                         continue;
560                 }
561                 if (i->video_frame_rate()) {
562                         FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
563                         range.first = min (range.first, frc.speed_up);
564                         range.second = max (range.second, frc.speed_up);
565                 } else {
566                         FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
567                         range.first = min (range.first, frc.speed_up);
568                         range.second = max (range.second, frc.speed_up);
569                 }
570         }
571
572         return range;
573 }