b96b0fbe0edf48a809ba93f89c28e87de56a5b08
[dcpomatic.git] / src / lib / playlist.cc
1 /*
2     Copyright (C) 2013-2018 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 "compose.hpp"
35 #include <libcxml/cxml.h>
36 #include <libxml++/libxml++.h>
37 #include <boost/shared_ptr.hpp>
38 #include <boost/foreach.hpp>
39 #include <iostream>
40
41 #include "i18n.h"
42
43 using std::list;
44 using std::cout;
45 using std::vector;
46 using std::min;
47 using std::max;
48 using std::string;
49 using std::pair;
50 using boost::optional;
51 using boost::shared_ptr;
52 using boost::weak_ptr;
53 using boost::dynamic_pointer_cast;
54 using namespace dcpomatic;
55
56 Playlist::Playlist ()
57         : _sequence (true)
58         , _sequencing (false)
59 {
60
61 }
62
63 Playlist::~Playlist ()
64 {
65         boost::mutex::scoped_lock lm (_mutex);
66         _content.clear ();
67         disconnect ();
68 }
69
70 void
71 Playlist::content_change (weak_ptr<const Film> weak_film, ChangeType type, weak_ptr<Content> content, int property, bool frequent)
72 {
73         shared_ptr<const Film> film = weak_film.lock ();
74         DCPOMATIC_ASSERT (film);
75
76         if (type == CHANGE_TYPE_DONE) {
77                 if (
78                         property == ContentProperty::TRIM_START ||
79                         property == ContentProperty::TRIM_END ||
80                         property == ContentProperty::LENGTH ||
81                         property == VideoContentProperty::FRAME_TYPE
82                         ) {
83                         /* Don't respond to position changes here, as:
84                            - sequencing after earlier/later changes is handled by move_earlier/move_later
85                            - any other position changes will be timeline drags which should not result in content
86                            being sequenced.
87                         */
88                         maybe_sequence (film);
89                 }
90
91                 if (
92                         property == ContentProperty::POSITION ||
93                         property == ContentProperty::LENGTH ||
94                         property == ContentProperty::TRIM_START ||
95                         property == ContentProperty::TRIM_END) {
96
97                         bool changed = false;
98
99                         {
100                                 boost::mutex::scoped_lock lm (_mutex);
101                                 ContentList old = _content;
102                                 sort (_content.begin(), _content.end(), ContentSorter ());
103                                 changed = _content != old;
104                         }
105
106                         if (changed) {
107                                 OrderChange ();
108                         }
109
110                         /* The length might have changed, and that's good enough for this signal */
111                         LengthChange ();
112                 }
113         }
114
115         ContentChange (type, content, property, frequent);
116 }
117
118 void
119 Playlist::maybe_sequence (shared_ptr<const Film> film)
120 {
121         if (!_sequence || _sequencing) {
122                 return;
123         }
124
125         _sequencing = true;
126
127         ContentList cont = content ();
128
129         /* Keep track of the content that we've set the position of so that we don't
130            do it twice.
131         */
132         ContentList placed;
133
134         /* Video */
135
136         DCPTime next_left;
137         DCPTime next_right;
138         BOOST_FOREACH (shared_ptr<Content> i, cont) {
139                 if (!i->video) {
140                         continue;
141                 }
142
143                 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
144                         i->set_position (film, next_right);
145                         next_right = i->end(film);
146                 } else {
147                         i->set_position (film, next_left);
148                         next_left = i->end(film);
149                 }
150
151                 placed.push_back (i);
152         }
153
154         /* Captions */
155
156         DCPTime next;
157         BOOST_FOREACH (shared_ptr<Content> i, cont) {
158                 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
159                         continue;
160                 }
161
162                 i->set_position (film, next);
163                 next = i->end(film);
164         }
165
166
167         /* This won't change order, so it does not need a sort */
168
169         _sequencing = false;
170 }
171
172 string
173 Playlist::video_identifier () const
174 {
175         string t;
176
177         BOOST_FOREACH (shared_ptr<const Content> i, content()) {
178                 bool burn = false;
179                 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
180                         if (j->burn()) {
181                                 burn = true;
182                         }
183                 }
184                 if (i->video || burn) {
185                         t += i->identifier ();
186                 }
187         }
188
189         Digester digester;
190         digester.add (t.c_str(), t.length());
191         return digester.get ();
192 }
193
194 /** @param film Film that this Playlist is for.
195  *  @param node &lt;Playlist&gt; node.
196  *  @param version Metadata version number.
197  *  @param notes Output notes about that happened.
198  */
199 void
200 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
201 {
202         boost::mutex::scoped_lock lm (_mutex);
203
204         BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
205                 shared_ptr<Content> content = content_factory (i, version, notes);
206
207                 /* See if this content should be nudged to start on a video frame */
208                 DCPTime const old_pos = content->position();
209                 content->set_position(film, old_pos);
210                 if (old_pos != content->position()) {
211                         string note = _("Your project contains video content that was not aligned to a frame boundary.");
212                         note += "  ";
213                         if (old_pos < content->position()) {
214                                 note += String::compose(
215                                         _("The file %1 has been moved %2 milliseconds later."),
216                                         content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
217                                         );
218                         } else {
219                                 note += String::compose(
220                                         _("The file %1 has been moved %2 milliseconds earlier."),
221                                         content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
222                                         );
223                         }
224                         notes.push_back (note);
225                 }
226
227                 /* ...or have a start trim which is an integer number of frames */
228                 ContentTime const old_trim = content->trim_start();
229                 content->set_trim_start(old_trim);
230                 if (old_trim != content->trim_start()) {
231                         string note = _("Your project contains video content whose trim was not aligned to a frame boundary.");
232                         note += "  ";
233                         if (old_trim < content->trim_start()) {
234                                 note += String::compose(
235                                         _("The file %1 has been trimmed by %2 milliseconds more."),
236                                         content->path_summary(), ContentTime(content->trim_start() - old_trim).seconds() * 1000
237                                         );
238                         } else {
239                                 note += String::compose(
240                                         _("The file %1 has been trimmed by %2 milliseconds less."),
241                                         content->path_summary(), ContentTime(old_trim - content->trim_start()).seconds() * 1000
242                                         );
243                         }
244                         notes.push_back (note);
245                 }
246
247                 _content.push_back (content);
248         }
249
250         /* This shouldn't be necessary but better safe than sorry (there could be old files) */
251         sort (_content.begin(), _content.end(), ContentSorter ());
252
253         reconnect (film);
254 }
255
256 /** @param node &lt;Playlist&gt; node.
257  *  @param with_content_paths true to include &lt;Path&gt; nodes in &lt;Content&gt; nodes, false to omit them.
258  */
259 void
260 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
261 {
262         BOOST_FOREACH (shared_ptr<Content> i, content()) {
263                 i->as_xml (node->add_child ("Content"), with_content_paths);
264         }
265 }
266
267 void
268 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
269 {
270         Change (CHANGE_TYPE_PENDING);
271
272         {
273                 boost::mutex::scoped_lock lm (_mutex);
274                 _content.push_back (c);
275                 sort (_content.begin(), _content.end(), ContentSorter ());
276                 reconnect (film);
277         }
278
279         Change (CHANGE_TYPE_DONE);
280
281         LengthChange ();
282 }
283
284 void
285 Playlist::remove (shared_ptr<Content> c)
286 {
287         Change (CHANGE_TYPE_PENDING);
288
289         bool cancelled = false;
290
291         {
292                 boost::mutex::scoped_lock lm (_mutex);
293
294                 ContentList::iterator i = _content.begin ();
295                 while (i != _content.end() && *i != c) {
296                         ++i;
297                 }
298
299                 if (i != _content.end()) {
300                         _content.erase (i);
301                 } else {
302                         cancelled = true;
303                 }
304         }
305
306         if (cancelled) {
307                 Change (CHANGE_TYPE_CANCELLED);
308         } else {
309                 Change (CHANGE_TYPE_DONE);
310         }
311
312         /* This won't change order, so it does not need a sort */
313
314         LengthChange ();
315 }
316
317 void
318 Playlist::remove (ContentList c)
319 {
320         Change (CHANGE_TYPE_PENDING);
321
322         {
323                 boost::mutex::scoped_lock lm (_mutex);
324
325                 BOOST_FOREACH (shared_ptr<Content> i, c) {
326                         ContentList::iterator j = _content.begin ();
327                         while (j != _content.end() && *j != i) {
328                                 ++j;
329                         }
330
331                         if (j != _content.end ()) {
332                                 _content.erase (j);
333                         }
334                 }
335         }
336
337         Change (CHANGE_TYPE_DONE);
338
339         /* This won't change order, so it does not need a sort */
340
341         LengthChange ();
342 }
343
344 class FrameRateCandidate
345 {
346 public:
347         FrameRateCandidate (float source_, int dcp_)
348                 : source (source_)
349                 , dcp (dcp_)
350         {}
351
352         float source;
353         int dcp;
354 };
355
356 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
357 int
358 Playlist::best_video_frame_rate () const
359 {
360         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
361
362         /* Work out what rates we could manage, including those achieved by using skip / repeat */
363         list<FrameRateCandidate> candidates;
364
365         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
366         BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
367                 candidates.push_back (FrameRateCandidate(i, i));
368         }
369
370         /* Then the skip/repeat ones */
371         BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
372                 candidates.push_back (FrameRateCandidate (float(i) / 2, i));
373                 candidates.push_back (FrameRateCandidate (float(i) * 2, i));
374         }
375
376         /* Pick the best one */
377         float error = std::numeric_limits<float>::max ();
378         optional<FrameRateCandidate> best;
379         list<FrameRateCandidate>::iterator i = candidates.begin();
380         while (i != candidates.end()) {
381
382                 float this_error = 0;
383                 BOOST_FOREACH (shared_ptr<Content> j, content()) {
384                         if (!j->video || !j->video_frame_rate()) {
385                                 continue;
386                         }
387
388                         /* Best error for this content; we could use the content as-is or double its rate */
389                         float best_error = min (
390                                 float (fabs (i->source - j->video_frame_rate().get())),
391                                 float (fabs (i->source - j->video_frame_rate().get() * 2))
392                                 );
393
394                         /* Use the largest difference between DCP and source as the "error" */
395                         this_error = max (this_error, best_error);
396                 }
397
398                 if (this_error < error) {
399                         error = this_error;
400                         best = *i;
401                 }
402
403                 ++i;
404         }
405
406         if (!best) {
407                 return 24;
408         }
409
410         return best->dcp;
411 }
412
413 /** @return length of the playlist from time 0 to the last thing on the playlist */
414 DCPTime
415 Playlist::length (shared_ptr<const Film> film) const
416 {
417         DCPTime len;
418         BOOST_FOREACH (shared_ptr<const Content> i, content()) {
419                 len = max (len, i->end(film));
420         }
421
422         return len;
423 }
424
425 /** @return position of the first thing on the playlist, if it's not empty */
426 optional<DCPTime>
427 Playlist::start () const
428 {
429         ContentList cont = content ();
430         if (cont.empty()) {
431                 return optional<DCPTime> ();
432         }
433
434         DCPTime start = DCPTime::max ();
435         BOOST_FOREACH (shared_ptr<Content> i, cont) {
436                 start = min (start, i->position ());
437         }
438
439         return start;
440 }
441
442 /** Must be called with a lock held on _mutex */
443 void
444 Playlist::disconnect ()
445 {
446         BOOST_FOREACH (boost::signals2::connection& i, _content_connections) {
447                 i.disconnect ();
448         }
449
450         _content_connections.clear ();
451 }
452
453 /** Must be called with a lock held on _mutex */
454 void
455 Playlist::reconnect (shared_ptr<const Film> film)
456 {
457         disconnect ();
458
459         BOOST_FOREACH (shared_ptr<Content> i, _content) {
460                 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
461         }
462 }
463
464 DCPTime
465 Playlist::video_end (shared_ptr<const Film> film) const
466 {
467         DCPTime end;
468         BOOST_FOREACH (shared_ptr<Content> i, content()) {
469                 if (i->video) {
470                         end = max (end, i->end(film));
471                 }
472         }
473
474         return end;
475 }
476
477 DCPTime
478 Playlist::text_end (shared_ptr<const Film> film) const
479 {
480         DCPTime end;
481         BOOST_FOREACH (shared_ptr<Content> i, content()) {
482                 if (!i->text.empty ()) {
483                         end = max (end, i->end(film));
484                 }
485         }
486
487         return end;
488 }
489
490 FrameRateChange
491 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
492 {
493         ContentList cont = content ();
494         for (ContentList::const_reverse_iterator i = cont.rbegin(); i != cont.rend(); ++i) {
495                 if (!(*i)->video) {
496                         continue;
497                 }
498
499                 if ((*i)->position() <= t) {
500                         /* This is the first piece of content (going backwards...) that starts before t,
501                            so it's the active one.
502                         */
503                         if ((*i)->video_frame_rate ()) {
504                                 /* This content specified a rate, so use it */
505                                 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
506                         } else {
507                                 /* No specified rate so just use the DCP one */
508                                 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
509                         }
510                 }
511         }
512
513         return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
514 }
515
516 void
517 Playlist::set_sequence (bool s)
518 {
519         _sequence = s;
520 }
521
522 bool
523 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
524 {
525         if (a->position() != b->position()) {
526                 return a->position() < b->position();
527         }
528
529         /* Put video before audio if they start at the same time */
530         if (a->video && !b->video) {
531                 return true;
532         } else if (!a->video && b->video) {
533                 return false;
534         }
535
536         /* Last resort */
537         return a->digest() < b->digest();
538 }
539
540 /** @return content in ascending order of position */
541 ContentList
542 Playlist::content () const
543 {
544         boost::mutex::scoped_lock lm (_mutex);
545         return _content;
546 }
547
548 void
549 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
550 {
551         pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
552         BOOST_FOREACH (shared_ptr<Content> i, c) {
553                 range.first = min (range.first, i->position ());
554                 range.second = max (range.second, i->position ());
555                 range.first = min (range.first, i->end(film));
556                 range.second = max (range.second, i->end(film));
557         }
558
559         Change (CHANGE_TYPE_PENDING);
560
561         {
562                 boost::mutex::scoped_lock lm (_mutex);
563
564                 DCPTime pos = range.second;
565                 for (int i = 0; i < n; ++i) {
566                         BOOST_FOREACH (shared_ptr<Content> j, c) {
567                                 shared_ptr<Content> copy = j->clone ();
568                                 copy->set_position (film, pos + copy->position() - range.first);
569                                 _content.push_back (copy);
570                         }
571                         pos += range.second - range.first;
572                 }
573
574                 sort (_content.begin(), _content.end(), ContentSorter ());
575                 reconnect (film);
576         }
577
578         Change (CHANGE_TYPE_DONE);
579 }
580
581 void
582 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
583 {
584         ContentList cont = content ();
585         ContentList::iterator previous = cont.end();
586         ContentList::iterator i = cont.begin();
587         while (i != cont.end() && *i != c) {
588                 previous = i;
589                 ++i;
590         }
591
592         DCPOMATIC_ASSERT (i != cont.end());
593         if (previous == cont.end()) {
594                 return;
595         }
596
597         shared_ptr<Content> previous_c = *previous;
598
599         DCPTime const p = previous_c->position ();
600         previous_c->set_position (film, p + c->length_after_trim(film));
601         c->set_position (film, p);
602 }
603
604 void
605 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
606 {
607         ContentList cont = content ();
608         ContentList::iterator i = cont.begin();
609         while (i != cont.end() && *i != c) {
610                 ++i;
611         }
612
613         DCPOMATIC_ASSERT (i != cont.end());
614
615         ContentList::iterator next = i;
616         ++next;
617
618         if (next == cont.end()) {
619                 return;
620         }
621
622         shared_ptr<Content> next_c = *next;
623
624         next_c->set_position (film, c->position());
625         c->set_position (film, c->position() + next_c->length_after_trim(film));
626 }
627
628 int64_t
629 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
630 {
631         int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
632         int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
633
634         BOOST_FOREACH (shared_ptr<Content> i, content()) {
635                 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
636                 if (d) {
637                         if (d->reference_video()) {
638                                 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
639                         }
640                         if (d->reference_audio()) {
641                                 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
642                         }
643                 }
644         }
645
646         /* Add on 64k for bits and pieces (metadata, subs etc) */
647         return video + audio + 65536;
648 }
649
650 string
651 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
652 {
653         string best_summary;
654         int best_score = -1;
655         BOOST_FOREACH (shared_ptr<Content> i, content()) {
656                 int score = 0;
657                 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
658                 if (o) {
659                         score += 100 * o.get().duration().get() / period.duration().get();
660                 }
661
662                 if (i->video) {
663                         score += 100;
664                 }
665
666                 if (score > best_score) {
667                         best_summary = i->path(0).leaf().string();
668                         best_score = score;
669                 }
670         }
671
672         return best_summary;
673 }
674
675 pair<double, double>
676 Playlist::speed_up_range (int dcp_video_frame_rate) const
677 {
678         pair<double, double> range (DBL_MAX, -DBL_MAX);
679
680         BOOST_FOREACH (shared_ptr<Content> i, content()) {
681                 if (!i->video) {
682                         continue;
683                 }
684                 if (i->video_frame_rate()) {
685                         FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
686                         range.first = min (range.first, frc.speed_up);
687                         range.second = max (range.second, frc.speed_up);
688                 } else {
689                         FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
690                         range.first = min (range.first, frc.speed_up);
691                         range.second = max (range.second, frc.speed_up);
692                 }
693         }
694
695         return range;
696 }