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