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;
64 bool operator()(shared_ptr<Content> a, shared_ptr<Content> b)
66 if (a->position() != b->position()) {
67 return a->position() < b->position();
70 /* Put video before audio if they start at the same time */
71 if (a->video && !b->video) {
73 } else if (!a->video && b->video) {
78 return a->digest() < b->digest();
83 Playlist::~Playlist ()
85 boost::mutex::scoped_lock lm (_mutex);
92 Playlist::content_change (weak_ptr<const Film> weak_film, ChangeType type, weak_ptr<Content> content, int property, bool frequent)
94 auto film = weak_film.lock ();
95 DCPOMATIC_ASSERT (film);
97 if (type == ChangeType::DONE) {
99 property == ContentProperty::TRIM_START ||
100 property == ContentProperty::TRIM_END ||
101 property == ContentProperty::LENGTH ||
102 property == VideoContentProperty::FRAME_TYPE
104 /* Don't respond to position changes here, as:
105 - sequencing after earlier/later changes is handled by move_earlier/move_later
106 - any other position changes will be timeline drags which should not result in content
109 maybe_sequence (film);
113 property == ContentProperty::POSITION ||
114 property == ContentProperty::LENGTH ||
115 property == ContentProperty::TRIM_START ||
116 property == ContentProperty::TRIM_END) {
118 bool changed = false;
121 boost::mutex::scoped_lock lm (_mutex);
122 ContentList old = _content;
123 sort (_content.begin(), _content.end(), ContentSorter ());
124 changed = _content != old;
131 /* The length might have changed, and that's good enough for this signal */
136 ContentChange (type, content, property, frequent);
141 Playlist::maybe_sequence (shared_ptr<const Film> film)
143 if (!_sequence || _sequencing) {
149 auto cont = content ();
151 /* Keep track of the content that we've set the position of so that we don't
165 if (i->video->frame_type() == VideoFrameType::THREE_D_RIGHT) {
166 i->set_position (film, next_right);
167 next_right = i->end(film);
169 i->set_position (film, next_left);
170 next_left = i->end(film);
173 placed.push_back (i);
180 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
184 i->set_position (film, next);
189 /* This won't change order, so it does not need a sort */
196 Playlist::video_identifier () const
200 for (auto i: content()) {
202 for (auto j: i->text) {
207 if (i->video || burn) {
208 t += i->identifier ();
213 digester.add (t.c_str(), t.length());
214 return digester.get ();
218 /** @param film Film that this Playlist is for.
219 * @param node <Playlist> node.
220 * @param version Metadata version number.
221 * @param notes Output notes about that happened.
224 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
226 boost::mutex::scoped_lock lm (_mutex);
228 for (auto i: node->node_children ("Content")) {
229 auto content = content_factory (i, version, notes);
231 /* See if this content should be nudged to start on a video frame */
232 auto const old_pos = content->position();
233 content->set_position(film, old_pos);
234 if (old_pos != content->position()) {
235 string note = _("Your project contains video content that was not aligned to a frame boundary.");
237 if (old_pos < content->position()) {
238 note += String::compose(
239 _("The file %1 has been moved %2 milliseconds later."),
240 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
243 note += String::compose(
244 _("The file %1 has been moved %2 milliseconds earlier."),
245 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
248 notes.push_back (note);
251 /* ...or have a start trim which is an integer number of frames */
252 auto const old_trim = content->trim_start();
253 content->set_trim_start(film, old_trim);
254 if (old_trim != content->trim_start()) {
255 string note = _("Your project contains video content whose trim was not aligned to a frame boundary.");
257 if (old_trim < content->trim_start()) {
258 note += String::compose(
259 _("The file %1 has been trimmed by %2 milliseconds more."),
260 content->path_summary(), ContentTime(content->trim_start() - old_trim).seconds() * 1000
263 note += String::compose(
264 _("The file %1 has been trimmed by %2 milliseconds less."),
265 content->path_summary(), ContentTime(old_trim - content->trim_start()).seconds() * 1000
268 notes.push_back (note);
271 _content.push_back (content);
274 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
275 sort (_content.begin(), _content.end(), ContentSorter ());
281 /** @param node <Playlist> node.
282 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
285 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
287 for (auto i: content()) {
288 i->as_xml (node->add_child ("Content"), with_content_paths);
294 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
296 Change (ChangeType::PENDING);
299 boost::mutex::scoped_lock lm (_mutex);
300 _content.push_back (c);
301 sort (_content.begin(), _content.end(), ContentSorter ());
305 Change (ChangeType::DONE);
312 Playlist::remove (shared_ptr<Content> c)
314 Change (ChangeType::PENDING);
316 bool cancelled = false;
319 boost::mutex::scoped_lock lm (_mutex);
321 auto i = _content.begin ();
322 while (i != _content.end() && *i != c) {
326 if (i != _content.end()) {
334 Change (ChangeType::CANCELLED);
336 Change (ChangeType::DONE);
339 /* This won't change order, so it does not need a sort */
346 Playlist::remove (ContentList c)
348 Change (ChangeType::PENDING);
351 boost::mutex::scoped_lock lm (_mutex);
354 auto j = _content.begin ();
355 while (j != _content.end() && *j != i) {
359 if (j != _content.end ()) {
365 Change (ChangeType::DONE);
367 /* This won't change order, so it does not need a sort */
373 class FrameRateCandidate
376 FrameRateCandidate (float source_, int dcp_)
386 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
388 Playlist::best_video_frame_rate () const
390 auto const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
392 /* Work out what rates we could manage, including those achieved by using skip / repeat */
393 list<FrameRateCandidate> candidates;
395 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
396 for (auto i: allowed_dcp_frame_rates) {
397 candidates.push_back (FrameRateCandidate(i, i));
400 /* Then the skip/repeat ones */
401 for (auto i: allowed_dcp_frame_rates) {
402 candidates.push_back (FrameRateCandidate (float(i) / 2, i));
403 candidates.push_back (FrameRateCandidate (float(i) * 2, i));
406 /* Pick the best one */
407 float error = std::numeric_limits<float>::max ();
408 optional<FrameRateCandidate> best;
409 auto i = candidates.begin();
410 while (i != candidates.end()) {
412 float this_error = 0;
413 for (auto j: content()) {
414 if (!j->video || !j->video_frame_rate()) {
418 /* Best error for this content; we could use the content as-is or double its rate */
419 float best_error = min (
420 float (fabs (i->source - j->video_frame_rate().get())),
421 float (fabs (i->source - j->video_frame_rate().get() * 2))
424 /* Use the largest difference between DCP and source as the "error" */
425 this_error = max (this_error, best_error);
428 if (this_error < error) {
444 /** @return length of the playlist from time 0 to the last thing on the playlist */
446 Playlist::length (shared_ptr<const Film> film) const
449 for (auto i: content()) {
450 len = max (len, i->end(film));
457 /** @return position of the first thing on the playlist, if it's not empty */
459 Playlist::start () const
461 auto cont = content ();
466 auto start = DCPTime::max ();
468 start = min (start, i->position ());
475 /** Must be called with a lock held on _mutex */
477 Playlist::disconnect ()
479 for (auto& i: _content_connections) {
483 _content_connections.clear ();
487 /** Must be called with a lock held on _mutex */
489 Playlist::reconnect (shared_ptr<const Film> film)
493 for (auto i: _content) {
494 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
500 Playlist::video_end (shared_ptr<const Film> film) const
503 for (auto i: content()) {
505 end = max (end, i->end(film));
514 Playlist::text_end (shared_ptr<const Film> film) const
517 for (auto i: content()) {
518 if (!i->text.empty ()) {
519 end = max (end, i->end(film));
528 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
530 auto cont = content ();
531 for (ContentList::const_reverse_iterator i = cont.rbegin(); i != cont.rend(); ++i) {
536 if ((*i)->position() <= t) {
537 /* This is the first piece of content (going backwards...) that starts before t,
538 so it's the active one.
540 if ((*i)->video_frame_rate ()) {
541 /* This content specified a rate, so use it */
542 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
544 /* No specified rate so just use the DCP one */
545 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
550 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
555 Playlist::set_sequence (bool s)
561 /** @return content in ascending order of position */
563 Playlist::content () const
565 boost::mutex::scoped_lock lm (_mutex);
571 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
573 pair<DCPTime, DCPTime> range (DCPTime::max(), DCPTime());
575 range.first = min (range.first, i->position ());
576 range.second = max (range.second, i->position ());
577 range.first = min (range.first, i->end(film));
578 range.second = max (range.second, i->end(film));
581 Change (ChangeType::PENDING);
584 boost::mutex::scoped_lock lm (_mutex);
586 DCPTime pos = range.second;
587 for (int i = 0; i < n; ++i) {
589 auto copy = j->clone ();
590 copy->set_position (film, pos + copy->position() - range.first);
591 _content.push_back (copy);
593 pos += range.second - range.first;
596 sort (_content.begin(), _content.end(), ContentSorter ());
600 Change (ChangeType::DONE);
605 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
607 auto cont = content ();
608 auto previous = cont.end();
609 auto i = cont.begin();
610 while (i != cont.end() && *i != c) {
615 DCPOMATIC_ASSERT (i != cont.end());
616 if (previous == cont.end()) {
620 auto previous_c = *previous;
622 auto const p = previous_c->position ();
623 previous_c->set_position (film, p + c->length_after_trim(film));
624 c->set_position (film, p);
629 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
631 auto cont = content ();
633 auto iter = std::find(cont.begin(), cont.end(), c);
634 DCPOMATIC_ASSERT(iter != cont.end());
636 ContentList::iterator next = std::next(iter);
637 if (next == cont.end()) {
638 /* This content is already at the end */
642 (*next)->set_position(film, c->position());
643 c->set_position(film, c->position() + (*next)->length_after_trim(film));
648 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
650 int64_t video = uint64_t(j2k_bandwidth / 8) * length(film).seconds();
651 int64_t audio = uint64_t(audio_channels) * audio_frame_rate * 3 * length(film).seconds();
653 for (auto i: content()) {
654 auto d = dynamic_pointer_cast<DCPContent> (i);
656 if (d->reference_video()) {
657 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
659 if (d->reference_audio()) {
660 audio -= uint64_t(audio_channels) * audio_frame_rate * 3 * d->length_after_trim(film).seconds();
665 /* Add on 64k for bits and pieces (metadata, subs etc) */
666 return video + audio + 65536;
671 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
675 for (auto i: content()) {
677 auto const o = i->period(film).overlap(period);
679 score += 100 * o.get().duration().get() / period.duration().get();
686 if (score > best_score) {
687 best_summary = i->path(0).filename().string();
697 Playlist::speed_up_range (int dcp_video_frame_rate) const
699 pair<double, double> range (DBL_MAX, -DBL_MAX);
701 for (auto i: content()) {
705 if (i->video_frame_rate()) {
706 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
707 range.first = min (range.first, frc.speed_up);
708 range.second = max (range.second, frc.speed_up);
710 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
711 range.first = min (range.first, frc.speed_up);
712 range.second = max (range.second, frc.speed_up);