2 Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
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"
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>
50 using boost::optional;
51 using boost::shared_ptr;
52 using boost::weak_ptr;
53 using boost::dynamic_pointer_cast;
54 using namespace dcpomatic;
63 Playlist::~Playlist ()
65 boost::mutex::scoped_lock lm (_mutex);
71 Playlist::content_change (weak_ptr<const Film> weak_film, ChangeType type, weak_ptr<Content> content, int property, bool frequent)
73 /* Make sure we only hear about atomic changes (e.g. a PENDING always with the DONE/CANCELLED)
74 Ignore any DONE/CANCELLED that arrives without a PENDING.
76 if (_checker.send (type, property)) {
80 shared_ptr<const Film> film = weak_film.lock ();
81 DCPOMATIC_ASSERT (film);
83 if (type == CHANGE_TYPE_DONE) {
85 property == ContentProperty::TRIM_START ||
86 property == ContentProperty::TRIM_END ||
87 property == ContentProperty::LENGTH ||
88 property == VideoContentProperty::FRAME_TYPE
90 /* Don't respond to position changes here, as:
91 - sequencing after earlier/later changes is handled by move_earlier/move_later
92 - any other position changes will be timeline drags which should not result in content
95 maybe_sequence (film);
99 property == ContentProperty::POSITION ||
100 property == ContentProperty::LENGTH ||
101 property == ContentProperty::TRIM_START ||
102 property == ContentProperty::TRIM_END) {
104 bool changed = false;
107 boost::mutex::scoped_lock lm (_mutex);
108 ContentList old = _content;
109 sort (_content.begin(), _content.end(), ContentSorter ());
110 changed = _content != old;
117 /* The length might have changed, and that's good enough for this signal */
122 ContentChange (type, content, property, frequent);
126 Playlist::maybe_sequence (shared_ptr<const Film> film)
128 if (!_sequence || _sequencing) {
134 ContentList cont = content ();
136 /* Keep track of the content that we've set the position of so that we don't
145 BOOST_FOREACH (shared_ptr<Content> i, cont) {
150 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
151 i->set_position (film, next_right);
152 next_right = i->end(film);
154 i->set_position (film, next_left);
155 next_left = i->end(film);
158 placed.push_back (i);
164 BOOST_FOREACH (shared_ptr<Content> i, cont) {
165 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
169 i->set_position (film, next);
174 /* This won't change order, so it does not need a sort */
180 Playlist::video_identifier () const
184 BOOST_FOREACH (shared_ptr<const Content> i, content()) {
186 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
191 if (i->video || burn) {
192 t += i->identifier ();
197 digester.add (t.c_str(), t.length());
198 return digester.get ();
201 /** @param film Film that this Playlist is for.
202 * @param node <Playlist> node.
203 * @param version Metadata version number.
204 * @param notes Output notes about that happened.
207 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
209 boost::mutex::scoped_lock lm (_mutex);
211 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
212 shared_ptr<Content> content = content_factory (i, version, notes);
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.");
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
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
231 notes.push_back (note);
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.");
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
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
251 notes.push_back (note);
254 _content.push_back (content);
257 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
258 sort (_content.begin(), _content.end(), ContentSorter ());
263 /** @param node <Playlist> node.
264 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
267 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
269 BOOST_FOREACH (shared_ptr<Content> i, content()) {
270 i->as_xml (node->add_child ("Content"), with_content_paths);
275 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
277 Change (CHANGE_TYPE_PENDING);
280 boost::mutex::scoped_lock lm (_mutex);
281 _content.push_back (c);
282 sort (_content.begin(), _content.end(), ContentSorter ());
286 Change (CHANGE_TYPE_DONE);
292 Playlist::remove (shared_ptr<Content> c)
294 Change (CHANGE_TYPE_PENDING);
296 bool cancelled = false;
299 boost::mutex::scoped_lock lm (_mutex);
301 ContentList::iterator i = _content.begin ();
302 while (i != _content.end() && *i != c) {
306 if (i != _content.end()) {
314 Change (CHANGE_TYPE_CANCELLED);
316 Change (CHANGE_TYPE_DONE);
319 /* This won't change order, so it does not need a sort */
325 Playlist::remove (ContentList c)
327 Change (CHANGE_TYPE_PENDING);
330 boost::mutex::scoped_lock lm (_mutex);
332 BOOST_FOREACH (shared_ptr<Content> i, c) {
333 ContentList::iterator j = _content.begin ();
334 while (j != _content.end() && *j != i) {
338 if (j != _content.end ()) {
344 Change (CHANGE_TYPE_DONE);
346 /* This won't change order, so it does not need a sort */
351 class FrameRateCandidate
354 FrameRateCandidate (float source_, int dcp_)
363 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
365 Playlist::best_video_frame_rate () const
367 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
369 /* Work out what rates we could manage, including those achieved by using skip / repeat */
370 list<FrameRateCandidate> candidates;
372 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
373 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
374 candidates.push_back (FrameRateCandidate(i, i));
377 /* Then the skip/repeat ones */
378 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
379 candidates.push_back (FrameRateCandidate (float(i) / 2, i));
380 candidates.push_back (FrameRateCandidate (float(i) * 2, i));
383 /* Pick the best one */
384 float error = std::numeric_limits<float>::max ();
385 optional<FrameRateCandidate> best;
386 list<FrameRateCandidate>::iterator i = candidates.begin();
387 while (i != candidates.end()) {
389 float this_error = 0;
390 BOOST_FOREACH (shared_ptr<Content> j, content()) {
391 if (!j->video || !j->video_frame_rate()) {
395 /* Best error for this content; we could use the content as-is or double its rate */
396 float best_error = min (
397 float (fabs (i->source - j->video_frame_rate().get())),
398 float (fabs (i->source - j->video_frame_rate().get() * 2))
401 /* Use the largest difference between DCP and source as the "error" */
402 this_error = max (this_error, best_error);
405 if (this_error < error) {
420 /** @return length of the playlist from time 0 to the last thing on the playlist */
422 Playlist::length (shared_ptr<const Film> film) const
425 BOOST_FOREACH (shared_ptr<const Content> i, content()) {
426 len = max (len, i->end(film));
432 /** @return position of the first thing on the playlist, if it's not empty */
434 Playlist::start () const
436 ContentList cont = content ();
438 return optional<DCPTime> ();
441 DCPTime start = DCPTime::max ();
442 BOOST_FOREACH (shared_ptr<Content> i, cont) {
443 start = min (start, i->position ());
449 /** Must be called with a lock held on _mutex */
451 Playlist::disconnect ()
453 BOOST_FOREACH (boost::signals2::connection& i, _content_connections) {
457 _content_connections.clear ();
460 /** Must be called with a lock held on _mutex */
462 Playlist::reconnect (shared_ptr<const Film> film)
466 BOOST_FOREACH (shared_ptr<Content> i, _content) {
467 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
472 Playlist::video_end (shared_ptr<const Film> film) const
475 BOOST_FOREACH (shared_ptr<Content> i, content()) {
477 end = max (end, i->end(film));
485 Playlist::text_end (shared_ptr<const Film> film) const
488 BOOST_FOREACH (shared_ptr<Content> i, content()) {
489 if (!i->text.empty ()) {
490 end = max (end, i->end(film));
498 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
500 ContentList cont = content ();
501 for (ContentList::const_reverse_iterator i = cont.rbegin(); i != cont.rend(); ++i) {
506 if ((*i)->position() <= t) {
507 /* This is the first piece of content (going backwards...) that starts before t,
508 so it's the active one.
510 if ((*i)->video_frame_rate ()) {
511 /* This content specified a rate, so use it */
512 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
514 /* No specified rate so just use the DCP one */
515 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
520 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
524 Playlist::set_sequence (bool s)
530 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
532 if (a->position() != b->position()) {
533 return a->position() < b->position();
536 /* Put video before audio if they start at the same time */
537 if (a->video && !b->video) {
539 } else if (!a->video && b->video) {
544 return a->digest() < b->digest();
547 /** @return content in ascending order of position */
549 Playlist::content () const
551 boost::mutex::scoped_lock lm (_mutex);
556 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
558 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
559 BOOST_FOREACH (shared_ptr<Content> i, c) {
560 range.first = min (range.first, i->position ());
561 range.second = max (range.second, i->position ());
562 range.first = min (range.first, i->end(film));
563 range.second = max (range.second, i->end(film));
566 Change (CHANGE_TYPE_PENDING);
569 boost::mutex::scoped_lock lm (_mutex);
571 DCPTime pos = range.second;
572 for (int i = 0; i < n; ++i) {
573 BOOST_FOREACH (shared_ptr<Content> j, c) {
574 shared_ptr<Content> copy = j->clone ();
575 copy->set_position (film, pos + copy->position() - range.first);
576 _content.push_back (copy);
578 pos += range.second - range.first;
581 sort (_content.begin(), _content.end(), ContentSorter ());
585 Change (CHANGE_TYPE_DONE);
589 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
591 ContentList cont = content ();
592 ContentList::iterator previous = cont.end();
593 ContentList::iterator i = cont.begin();
594 while (i != cont.end() && *i != c) {
599 DCPOMATIC_ASSERT (i != cont.end());
600 if (previous == cont.end()) {
604 shared_ptr<Content> previous_c = *previous;
606 DCPTime const p = previous_c->position ();
607 previous_c->set_position (film, p + c->length_after_trim(film));
608 c->set_position (film, p);
612 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
614 ContentList cont = content ();
615 ContentList::iterator i = cont.begin();
616 while (i != cont.end() && *i != c) {
620 DCPOMATIC_ASSERT (i != cont.end());
622 ContentList::iterator next = i;
625 if (next == cont.end()) {
629 shared_ptr<Content> next_c = *next;
631 next_c->set_position (film, c->position());
632 c->set_position (film, c->position() + next_c->length_after_trim(film));
636 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
638 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
639 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
641 BOOST_FOREACH (shared_ptr<Content> i, content()) {
642 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
644 if (d->reference_video()) {
645 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
647 if (d->reference_audio()) {
648 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
653 /* Add on 64k for bits and pieces (metadata, subs etc) */
654 return video + audio + 65536;
658 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
662 BOOST_FOREACH (shared_ptr<Content> i, content()) {
664 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
666 score += 100 * o.get().duration().get() / period.duration().get();
673 if (score > best_score) {
674 best_summary = i->path(0).leaf().string();
683 Playlist::speed_up_range (int dcp_video_frame_rate) const
685 pair<double, double> range (DBL_MAX, -DBL_MAX);
687 BOOST_FOREACH (shared_ptr<Content> i, content()) {
691 if (i->video_frame_rate()) {
692 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
693 range.first = min (range.first, frc.speed_up);
694 range.second = max (range.second, frc.speed_up);
696 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
697 range.first = min (range.first, frc.speed_up);
698 range.second = max (range.second, frc.speed_up);