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