2 Copyright (C) 2013-2021 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 "audio_content.h"
23 #include "compose.hpp"
25 #include "content_factory.h"
26 #include "dcp_content.h"
28 #include "ffmpeg_content.h"
29 #include "ffmpeg_decoder.h"
30 #include "image_decoder.h"
33 #include "text_content.h"
35 #include "video_content.h"
36 #include <libcxml/cxml.h>
37 #include <libxml++/libxml++.h>
38 #include <boost/bind/placeholders.hpp>
45 using std::dynamic_pointer_cast;
50 using std::shared_ptr;
54 using boost::optional;
55 using namespace dcpomatic;
56 #if BOOST_VERSION >= 106100
57 using namespace boost::placeholders;
67 Playlist::~Playlist ()
69 boost::mutex::scoped_lock lm (_mutex);
76 Playlist::content_change (weak_ptr<const Film> weak_film, ChangeType type, weak_ptr<Content> content, int property, bool frequent)
78 auto film = weak_film.lock ();
79 DCPOMATIC_ASSERT (film);
81 if (type == ChangeType::DONE) {
83 property == ContentProperty::TRIM_START ||
84 property == ContentProperty::TRIM_END ||
85 property == ContentProperty::LENGTH ||
86 property == VideoContentProperty::FRAME_TYPE
88 /* Don't respond to position changes here, as:
89 - sequencing after earlier/later changes is handled by move_earlier/move_later
90 - any other position changes will be timeline drags which should not result in content
93 maybe_sequence (film);
97 property == ContentProperty::POSITION ||
98 property == ContentProperty::LENGTH ||
99 property == ContentProperty::TRIM_START ||
100 property == ContentProperty::TRIM_END) {
102 bool changed = false;
105 boost::mutex::scoped_lock lm (_mutex);
106 ContentList old = _content;
107 sort (_content.begin(), _content.end(), ContentSorter ());
108 changed = _content != old;
115 /* The length might have changed, and that's good enough for this signal */
120 ContentChange (type, content, property, frequent);
125 Playlist::maybe_sequence (shared_ptr<const Film> film)
127 if (!_sequence || _sequencing) {
133 auto cont = content ();
135 /* Keep track of the content that we've set the position of so that we don't
149 if (i->video->frame_type() == VideoFrameType::THREE_D_RIGHT) {
150 i->set_position (film, next_right);
151 next_right = i->end(film);
153 i->set_position (film, next_left);
154 next_left = i->end(film);
157 placed.push_back (i);
164 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
168 i->set_position (film, next);
173 /* This won't change order, so it does not need a sort */
180 Playlist::video_identifier () const
184 for (auto i: content()) {
186 for (auto j: i->text) {
191 if (i->video || burn) {
192 t += i->identifier ();
197 digester.add (t.c_str(), t.length());
198 return digester.get ();
202 /** @param film Film that this Playlist is for.
203 * @param node <Playlist> node.
204 * @param version Metadata version number.
205 * @param notes Output notes about that happened.
208 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
210 boost::mutex::scoped_lock lm (_mutex);
212 for (auto i: node->node_children ("Content")) {
213 auto content = content_factory (i, version, notes);
215 /* See if this content should be nudged to start on a video frame */
216 auto const old_pos = content->position();
217 content->set_position(film, old_pos);
218 if (old_pos != content->position()) {
219 string note = _("Your project contains video content that was not aligned to a frame boundary.");
221 if (old_pos < content->position()) {
222 note += String::compose(
223 _("The file %1 has been moved %2 milliseconds later."),
224 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
227 note += String::compose(
228 _("The file %1 has been moved %2 milliseconds earlier."),
229 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
232 notes.push_back (note);
235 /* ...or have a start trim which is an integer number of frames */
236 auto const old_trim = content->trim_start();
237 content->set_trim_start(film, old_trim);
238 if (old_trim != content->trim_start()) {
239 string note = _("Your project contains video content whose trim was not aligned to a frame boundary.");
241 if (old_trim < content->trim_start()) {
242 note += String::compose(
243 _("The file %1 has been trimmed by %2 milliseconds more."),
244 content->path_summary(), ContentTime(content->trim_start() - old_trim).seconds() * 1000
247 note += String::compose(
248 _("The file %1 has been trimmed by %2 milliseconds less."),
249 content->path_summary(), ContentTime(old_trim - content->trim_start()).seconds() * 1000
252 notes.push_back (note);
255 _content.push_back (content);
258 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
259 sort (_content.begin(), _content.end(), ContentSorter ());
265 /** @param node <Playlist> node.
266 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
269 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
271 for (auto i: content()) {
272 i->as_xml (node->add_child ("Content"), with_content_paths);
278 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
280 Change (ChangeType::PENDING);
283 boost::mutex::scoped_lock lm (_mutex);
284 _content.push_back (c);
285 sort (_content.begin(), _content.end(), ContentSorter ());
289 Change (ChangeType::DONE);
296 Playlist::remove (shared_ptr<Content> c)
298 Change (ChangeType::PENDING);
300 bool cancelled = false;
303 boost::mutex::scoped_lock lm (_mutex);
305 auto i = _content.begin ();
306 while (i != _content.end() && *i != c) {
310 if (i != _content.end()) {
318 Change (ChangeType::CANCELLED);
320 Change (ChangeType::DONE);
323 /* This won't change order, so it does not need a sort */
330 Playlist::remove (ContentList c)
332 Change (ChangeType::PENDING);
335 boost::mutex::scoped_lock lm (_mutex);
338 auto j = _content.begin ();
339 while (j != _content.end() && *j != i) {
343 if (j != _content.end ()) {
349 Change (ChangeType::DONE);
351 /* This won't change order, so it does not need a sort */
357 class FrameRateCandidate
360 FrameRateCandidate (float source_, int dcp_)
370 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
372 Playlist::best_video_frame_rate () const
374 auto const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
376 /* Work out what rates we could manage, including those achieved by using skip / repeat */
377 list<FrameRateCandidate> candidates;
379 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
380 for (auto i: allowed_dcp_frame_rates) {
381 candidates.push_back (FrameRateCandidate(i, i));
384 /* Then the skip/repeat ones */
385 for (auto i: allowed_dcp_frame_rates) {
386 candidates.push_back (FrameRateCandidate (float(i) / 2, i));
387 candidates.push_back (FrameRateCandidate (float(i) * 2, i));
390 /* Pick the best one */
391 float error = std::numeric_limits<float>::max ();
392 optional<FrameRateCandidate> best;
393 auto i = candidates.begin();
394 while (i != candidates.end()) {
396 float this_error = 0;
397 for (auto j: content()) {
398 if (!j->video || !j->video_frame_rate()) {
402 /* Best error for this content; we could use the content as-is or double its rate */
403 float best_error = min (
404 float (fabs (i->source - j->video_frame_rate().get())),
405 float (fabs (i->source - j->video_frame_rate().get() * 2))
408 /* Use the largest difference between DCP and source as the "error" */
409 this_error = max (this_error, best_error);
412 if (this_error < error) {
428 /** @return length of the playlist from time 0 to the last thing on the playlist */
430 Playlist::length (shared_ptr<const Film> film) const
433 for (auto i: content()) {
434 len = max (len, i->end(film));
441 /** @return position of the first thing on the playlist, if it's not empty */
443 Playlist::start () const
445 auto cont = content ();
450 auto start = DCPTime::max ();
452 start = min (start, i->position ());
459 /** Must be called with a lock held on _mutex */
461 Playlist::disconnect ()
463 for (auto& i: _content_connections) {
467 _content_connections.clear ();
471 /** Must be called with a lock held on _mutex */
473 Playlist::reconnect (shared_ptr<const Film> film)
477 for (auto i: _content) {
478 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
484 Playlist::video_end (shared_ptr<const Film> film) const
487 for (auto i: content()) {
489 end = max (end, i->end(film));
498 Playlist::text_end (shared_ptr<const Film> film) const
501 for (auto i: content()) {
502 if (!i->text.empty ()) {
503 end = max (end, i->end(film));
512 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
514 auto cont = content ();
515 for (ContentList::const_reverse_iterator i = cont.rbegin(); i != cont.rend(); ++i) {
520 if ((*i)->position() <= t) {
521 /* This is the first piece of content (going backwards...) that starts before t,
522 so it's the active one.
524 if ((*i)->video_frame_rate ()) {
525 /* This content specified a rate, so use it */
526 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
528 /* No specified rate so just use the DCP one */
529 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
534 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
539 Playlist::set_sequence (bool s)
546 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
548 if (a->position() != b->position()) {
549 return a->position() < b->position();
552 /* Put video before audio if they start at the same time */
553 if (a->video && !b->video) {
555 } else if (!a->video && b->video) {
560 return a->digest() < b->digest();
564 /** @return content in ascending order of position */
566 Playlist::content () const
568 boost::mutex::scoped_lock lm (_mutex);
574 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
576 pair<DCPTime, DCPTime> range (DCPTime::max(), DCPTime());
578 range.first = min (range.first, i->position ());
579 range.second = max (range.second, i->position ());
580 range.first = min (range.first, i->end(film));
581 range.second = max (range.second, i->end(film));
584 Change (ChangeType::PENDING);
587 boost::mutex::scoped_lock lm (_mutex);
589 DCPTime pos = range.second;
590 for (int i = 0; i < n; ++i) {
592 auto copy = j->clone ();
593 copy->set_position (film, pos + copy->position() - range.first);
594 _content.push_back (copy);
596 pos += range.second - range.first;
599 sort (_content.begin(), _content.end(), ContentSorter ());
603 Change (ChangeType::DONE);
608 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
610 auto cont = content ();
611 auto previous = cont.end();
612 auto i = cont.begin();
613 while (i != cont.end() && *i != c) {
618 DCPOMATIC_ASSERT (i != cont.end());
619 if (previous == cont.end()) {
623 auto previous_c = *previous;
625 auto const p = previous_c->position ();
626 previous_c->set_position (film, p + c->length_after_trim(film));
627 c->set_position (film, p);
632 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
634 auto cont = content ();
635 auto i = cont.begin();
636 while (i != cont.end() && *i != c) {
640 DCPOMATIC_ASSERT (i != cont.end());
642 ContentList::iterator next = i;
645 if (next == cont.end()) {
651 next_c->set_position (film, c->position());
652 c->set_position (film, c->position() + next_c->length_after_trim(film));
657 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
659 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
660 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
662 for (auto i: content()) {
663 auto d = dynamic_pointer_cast<DCPContent> (i);
665 if (d->reference_video()) {
666 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
668 if (d->reference_audio()) {
669 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
674 /* Add on 64k for bits and pieces (metadata, subs etc) */
675 return video + audio + 65536;
680 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
684 for (auto i: content()) {
686 auto const o = i->period(film).overlap(period);
688 score += 100 * o.get().duration().get() / period.duration().get();
695 if (score > best_score) {
696 best_summary = i->path(0).leaf().string();
706 Playlist::speed_up_range (int dcp_video_frame_rate) const
708 pair<double, double> range (DBL_MAX, -DBL_MAX);
710 for (auto i: content()) {
714 if (i->video_frame_rate()) {
715 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
716 range.first = min (range.first, frc.speed_up);
717 range.second = max (range.second, frc.speed_up);
719 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
720 range.first = min (range.first, frc.speed_up);
721 range.second = max (range.second, frc.speed_up);