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;
119 ContentChange (type, content, property, frequent);
123 Playlist::maybe_sequence (shared_ptr<const Film> film)
125 if (!_sequence || _sequencing) {
131 ContentList cont = content ();
133 /* Keep track of the content that we've set the position of so that we don't
142 BOOST_FOREACH (shared_ptr<Content> i, cont) {
147 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
148 i->set_position (film, next_right);
149 next_right = i->end(film);
151 i->set_position (film, next_left);
152 next_left = i->end(film);
155 placed.push_back (i);
161 BOOST_FOREACH (shared_ptr<Content> i, cont) {
162 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
166 i->set_position (film, next);
171 /* This won't change order, so it does not need a sort */
177 Playlist::video_identifier () const
181 BOOST_FOREACH (shared_ptr<const Content> i, content()) {
183 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
188 if (i->video || burn) {
189 t += i->identifier ();
194 digester.add (t.c_str(), t.length());
195 return digester.get ();
198 /** @param film Film that this Playlist is for.
199 * @param node <Playlist> node.
200 * @param version Metadata version number.
201 * @param notes Output notes about that happened.
204 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
206 boost::mutex::scoped_lock lm (_mutex);
208 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
209 shared_ptr<Content> content = content_factory (i, version, notes);
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.");
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
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
228 notes.push_back (note);
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.");
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
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
248 notes.push_back (note);
251 _content.push_back (content);
254 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
255 sort (_content.begin(), _content.end(), ContentSorter ());
260 /** @param node <Playlist> node.
261 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
264 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
266 BOOST_FOREACH (shared_ptr<Content> i, content()) {
267 i->as_xml (node->add_child ("Content"), with_content_paths);
272 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
274 Change (CHANGE_TYPE_PENDING);
277 boost::mutex::scoped_lock lm (_mutex);
278 _content.push_back (c);
279 sort (_content.begin(), _content.end(), ContentSorter ());
283 Change (CHANGE_TYPE_DONE);
287 Playlist::remove (shared_ptr<Content> c)
289 Change (CHANGE_TYPE_PENDING);
291 bool cancelled = false;
294 boost::mutex::scoped_lock lm (_mutex);
296 ContentList::iterator i = _content.begin ();
297 while (i != _content.end() && *i != c) {
301 if (i != _content.end()) {
309 Change (CHANGE_TYPE_CANCELLED);
311 Change (CHANGE_TYPE_DONE);
314 /* This won't change order, so it does not need a sort */
318 Playlist::remove (ContentList c)
320 Change (CHANGE_TYPE_PENDING);
323 boost::mutex::scoped_lock lm (_mutex);
325 BOOST_FOREACH (shared_ptr<Content> i, c) {
326 ContentList::iterator j = _content.begin ();
327 while (j != _content.end() && *j != i) {
331 if (j != _content.end ()) {
337 /* This won't change order, so it does not need a sort */
339 Change (CHANGE_TYPE_DONE);
342 class FrameRateCandidate
345 FrameRateCandidate (float source_, int dcp_)
354 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
356 Playlist::best_video_frame_rate () const
358 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
360 /* Work out what rates we could manage, including those achieved by using skip / repeat */
361 list<FrameRateCandidate> candidates;
363 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
364 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
365 candidates.push_back (FrameRateCandidate(i, i));
368 /* Then the skip/repeat ones */
369 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
370 candidates.push_back (FrameRateCandidate (float(i) / 2, i));
371 candidates.push_back (FrameRateCandidate (float(i) * 2, i));
374 /* Pick the best one */
375 float error = std::numeric_limits<float>::max ();
376 optional<FrameRateCandidate> best;
377 list<FrameRateCandidate>::iterator i = candidates.begin();
378 while (i != candidates.end()) {
380 float this_error = 0;
381 BOOST_FOREACH (shared_ptr<Content> j, content()) {
382 if (!j->video || !j->video_frame_rate()) {
386 /* Best error for this content; we could use the content as-is or double its rate */
387 float best_error = min (
388 float (fabs (i->source - j->video_frame_rate().get())),
389 float (fabs (i->source - j->video_frame_rate().get() * 2))
392 /* Use the largest difference between DCP and source as the "error" */
393 this_error = max (this_error, best_error);
396 if (this_error < error) {
411 /** @return length of the playlist from time 0 to the last thing on the playlist */
413 Playlist::length (shared_ptr<const Film> film) const
416 BOOST_FOREACH (shared_ptr<const Content> i, content()) {
417 len = max (len, i->end(film));
423 /** @return position of the first thing on the playlist, if it's not empty */
425 Playlist::start () const
427 ContentList cont = content ();
429 return optional<DCPTime> ();
432 DCPTime start = DCPTime::max ();
433 BOOST_FOREACH (shared_ptr<Content> i, cont) {
434 start = min (start, i->position ());
440 /** Must be called with a lock held on _mutex */
442 Playlist::disconnect ()
444 BOOST_FOREACH (boost::signals2::connection& i, _content_connections) {
448 _content_connections.clear ();
451 /** Must be called with a lock held on _mutex */
453 Playlist::reconnect (shared_ptr<const Film> film)
457 BOOST_FOREACH (shared_ptr<Content> i, _content) {
458 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
463 Playlist::video_end (shared_ptr<const Film> film) const
466 BOOST_FOREACH (shared_ptr<Content> i, content()) {
468 end = max (end, i->end(film));
476 Playlist::text_end (shared_ptr<const Film> film) const
479 BOOST_FOREACH (shared_ptr<Content> i, content()) {
480 if (!i->text.empty ()) {
481 end = max (end, i->end(film));
489 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
491 ContentList cont = content ();
492 for (ContentList::const_reverse_iterator i = cont.rbegin(); i != cont.rend(); ++i) {
497 if ((*i)->position() <= t) {
498 /* This is the first piece of content (going backwards...) that starts before t,
499 so it's the active one.
501 if ((*i)->video_frame_rate ()) {
502 /* This content specified a rate, so use it */
503 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
505 /* No specified rate so just use the DCP one */
506 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
511 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
515 Playlist::set_sequence (bool s)
521 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
523 if (a->position() != b->position()) {
524 return a->position() < b->position();
527 /* Put video before audio if they start at the same time */
528 if (a->video && !b->video) {
530 } else if (!a->video && b->video) {
535 return a->digest() < b->digest();
538 /** @return content in ascending order of position */
540 Playlist::content () const
542 boost::mutex::scoped_lock lm (_mutex);
547 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
549 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
550 BOOST_FOREACH (shared_ptr<Content> i, c) {
551 range.first = min (range.first, i->position ());
552 range.second = max (range.second, i->position ());
553 range.first = min (range.first, i->end(film));
554 range.second = max (range.second, i->end(film));
557 Change (CHANGE_TYPE_PENDING);
560 boost::mutex::scoped_lock lm (_mutex);
562 DCPTime pos = range.second;
563 for (int i = 0; i < n; ++i) {
564 BOOST_FOREACH (shared_ptr<Content> j, c) {
565 shared_ptr<Content> copy = j->clone ();
566 copy->set_position (film, pos + copy->position() - range.first);
567 _content.push_back (copy);
569 pos += range.second - range.first;
572 sort (_content.begin(), _content.end(), ContentSorter ());
576 Change (CHANGE_TYPE_DONE);
580 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
582 ContentList cont = content ();
583 ContentList::iterator previous = cont.end();
584 ContentList::iterator i = cont.begin();
585 while (i != cont.end() && *i != c) {
590 DCPOMATIC_ASSERT (i != cont.end());
591 if (previous == cont.end()) {
595 shared_ptr<Content> previous_c = *previous;
597 DCPTime const p = previous_c->position ();
598 previous_c->set_position (film, p + c->length_after_trim(film));
599 c->set_position (film, p);
603 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
605 ContentList cont = content ();
606 ContentList::iterator i = cont.begin();
607 while (i != cont.end() && *i != c) {
611 DCPOMATIC_ASSERT (i != cont.end());
613 ContentList::iterator next = i;
616 if (next == cont.end()) {
620 shared_ptr<Content> next_c = *next;
622 next_c->set_position (film, c->position());
623 c->set_position (film, c->position() + next_c->length_after_trim(film));
627 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
629 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
630 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
632 BOOST_FOREACH (shared_ptr<Content> i, content()) {
633 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
635 if (d->reference_video()) {
636 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
638 if (d->reference_audio()) {
639 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
644 /* Add on 64k for bits and pieces (metadata, subs etc) */
645 return video + audio + 65536;
649 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
653 BOOST_FOREACH (shared_ptr<Content> i, content()) {
655 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
657 score += 100 * o.get().duration().get() / period.duration().get();
664 if (score > best_score) {
665 best_summary = i->path(0).leaf().string();
674 Playlist::speed_up_range (int dcp_video_frame_rate) const
676 pair<double, double> range (DBL_MAX, -DBL_MAX);
678 BOOST_FOREACH (shared_ptr<Content> i, content()) {
682 if (i->video_frame_rate()) {
683 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
684 range.first = min (range.first, frc.speed_up);
685 range.second = max (range.second, frc.speed_up);
687 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
688 range.first = min (range.first, frc.speed_up);
689 range.second = max (range.second, frc.speed_up);