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