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/bind/placeholders.hpp>
38 #include <boost/shared_ptr.hpp>
39 #include <boost/foreach.hpp>
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;
67 Playlist::~Playlist ()
69 boost::mutex::scoped_lock lm (_mutex);
75 Playlist::content_change (weak_ptr<const Film> weak_film, ChangeType type, weak_ptr<Content> content, int property, bool frequent)
77 shared_ptr<const Film> film = weak_film.lock ();
78 DCPOMATIC_ASSERT (film);
80 if (type == CHANGE_TYPE_DONE) {
82 property == ContentProperty::TRIM_START ||
83 property == ContentProperty::TRIM_END ||
84 property == ContentProperty::LENGTH ||
85 property == VideoContentProperty::FRAME_TYPE
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
92 maybe_sequence (film);
96 property == ContentProperty::POSITION ||
97 property == ContentProperty::LENGTH ||
98 property == ContentProperty::TRIM_START ||
99 property == ContentProperty::TRIM_END) {
101 bool changed = false;
104 boost::mutex::scoped_lock lm (_mutex);
105 ContentList old = _content;
106 sort (_content.begin(), _content.end(), ContentSorter ());
107 changed = _content != old;
114 /* The length might have changed, and that's good enough for this signal */
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);
289 Playlist::remove (shared_ptr<Content> c)
291 Change (CHANGE_TYPE_PENDING);
293 bool cancelled = false;
296 boost::mutex::scoped_lock lm (_mutex);
298 ContentList::iterator i = _content.begin ();
299 while (i != _content.end() && *i != c) {
303 if (i != _content.end()) {
311 Change (CHANGE_TYPE_CANCELLED);
313 Change (CHANGE_TYPE_DONE);
316 /* This won't change order, so it does not need a sort */
322 Playlist::remove (ContentList c)
324 Change (CHANGE_TYPE_PENDING);
327 boost::mutex::scoped_lock lm (_mutex);
329 BOOST_FOREACH (shared_ptr<Content> i, c) {
330 ContentList::iterator j = _content.begin ();
331 while (j != _content.end() && *j != i) {
335 if (j != _content.end ()) {
341 Change (CHANGE_TYPE_DONE);
343 /* This won't change order, so it does not need a sort */
348 class FrameRateCandidate
351 FrameRateCandidate (float source_, int dcp_)
360 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
362 Playlist::best_video_frame_rate () const
364 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
366 /* Work out what rates we could manage, including those achieved by using skip / repeat */
367 list<FrameRateCandidate> candidates;
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));
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));
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()) {
386 float this_error = 0;
387 BOOST_FOREACH (shared_ptr<Content> j, content()) {
388 if (!j->video || !j->video_frame_rate()) {
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))
398 /* Use the largest difference between DCP and source as the "error" */
399 this_error = max (this_error, best_error);
402 if (this_error < error) {
417 /** @return length of the playlist from time 0 to the last thing on the playlist */
419 Playlist::length (shared_ptr<const Film> film) const
422 BOOST_FOREACH (shared_ptr<const Content> i, content()) {
423 len = max (len, i->end(film));
429 /** @return position of the first thing on the playlist, if it's not empty */
431 Playlist::start () const
433 ContentList cont = content ();
435 return optional<DCPTime> ();
438 DCPTime start = DCPTime::max ();
439 BOOST_FOREACH (shared_ptr<Content> i, cont) {
440 start = min (start, i->position ());
446 /** Must be called with a lock held on _mutex */
448 Playlist::disconnect ()
450 BOOST_FOREACH (boost::signals2::connection& i, _content_connections) {
454 _content_connections.clear ();
457 /** Must be called with a lock held on _mutex */
459 Playlist::reconnect (shared_ptr<const Film> film)
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)));
469 Playlist::video_end (shared_ptr<const Film> film) const
472 BOOST_FOREACH (shared_ptr<Content> i, content()) {
474 end = max (end, i->end(film));
482 Playlist::text_end (shared_ptr<const Film> film) const
485 BOOST_FOREACH (shared_ptr<Content> i, content()) {
486 if (!i->text.empty ()) {
487 end = max (end, i->end(film));
495 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
497 ContentList cont = content ();
498 for (ContentList::const_reverse_iterator i = cont.rbegin(); i != cont.rend(); ++i) {
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.
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);
511 /* No specified rate so just use the DCP one */
512 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
517 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
521 Playlist::set_sequence (bool s)
527 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
529 if (a->position() != b->position()) {
530 return a->position() < b->position();
533 /* Put video before audio if they start at the same time */
534 if (a->video && !b->video) {
536 } else if (!a->video && b->video) {
541 return a->digest() < b->digest();
544 /** @return content in ascending order of position */
546 Playlist::content () const
548 boost::mutex::scoped_lock lm (_mutex);
553 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
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));
563 Change (CHANGE_TYPE_PENDING);
566 boost::mutex::scoped_lock lm (_mutex);
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);
575 pos += range.second - range.first;
578 sort (_content.begin(), _content.end(), ContentSorter ());
582 Change (CHANGE_TYPE_DONE);
586 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
588 ContentList cont = content ();
589 ContentList::iterator previous = cont.end();
590 ContentList::iterator i = cont.begin();
591 while (i != cont.end() && *i != c) {
596 DCPOMATIC_ASSERT (i != cont.end());
597 if (previous == cont.end()) {
601 shared_ptr<Content> previous_c = *previous;
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);
609 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
611 ContentList cont = content ();
612 ContentList::iterator i = cont.begin();
613 while (i != cont.end() && *i != c) {
617 DCPOMATIC_ASSERT (i != cont.end());
619 ContentList::iterator next = i;
622 if (next == cont.end()) {
626 shared_ptr<Content> next_c = *next;
628 next_c->set_position (film, c->position());
629 c->set_position (film, c->position() + next_c->length_after_trim(film));
633 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
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();
638 BOOST_FOREACH (shared_ptr<Content> i, content()) {
639 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
641 if (d->reference_video()) {
642 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
644 if (d->reference_audio()) {
645 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
650 /* Add on 64k for bits and pieces (metadata, subs etc) */
651 return video + audio + 65536;
655 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
659 BOOST_FOREACH (shared_ptr<Content> i, content()) {
661 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
663 score += 100 * o.get().duration().get() / period.duration().get();
670 if (score > best_score) {
671 best_summary = i->path(0).leaf().string();
680 Playlist::speed_up_range (int dcp_video_frame_rate) const
682 pair<double, double> range (DBL_MAX, -DBL_MAX);
684 BOOST_FOREACH (shared_ptr<Content> i, content()) {
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);
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);