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/foreach.hpp>
50 using boost::optional;
51 using std::shared_ptr;
53 using std::dynamic_pointer_cast;
54 using namespace dcpomatic;
55 #if BOOST_VERSION >= 106100
56 using namespace boost::placeholders;
66 Playlist::~Playlist ()
68 boost::mutex::scoped_lock lm (_mutex);
74 Playlist::content_change (weak_ptr<const Film> weak_film, ChangeType type, weak_ptr<Content> content, int property, bool frequent)
76 shared_ptr<const Film> film = weak_film.lock ();
77 DCPOMATIC_ASSERT (film);
79 if (type == CHANGE_TYPE_DONE) {
81 property == ContentProperty::TRIM_START ||
82 property == ContentProperty::TRIM_END ||
83 property == ContentProperty::LENGTH ||
84 property == VideoContentProperty::FRAME_TYPE
86 /* Don't respond to position changes here, as:
87 - sequencing after earlier/later changes is handled by move_earlier/move_later
88 - any other position changes will be timeline drags which should not result in content
91 maybe_sequence (film);
95 property == ContentProperty::POSITION ||
96 property == ContentProperty::LENGTH ||
97 property == ContentProperty::TRIM_START ||
98 property == ContentProperty::TRIM_END) {
100 bool changed = false;
103 boost::mutex::scoped_lock lm (_mutex);
104 ContentList old = _content;
105 sort (_content.begin(), _content.end(), ContentSorter ());
106 changed = _content != old;
113 /* The length might have changed, and that's good enough for this signal */
118 ContentChange (type, content, property, frequent);
122 Playlist::maybe_sequence (shared_ptr<const Film> film)
124 if (!_sequence || _sequencing) {
130 ContentList cont = content ();
132 /* Keep track of the content that we've set the position of so that we don't
141 BOOST_FOREACH (shared_ptr<Content> i, cont) {
146 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
147 i->set_position (film, next_right);
148 next_right = i->end(film);
150 i->set_position (film, next_left);
151 next_left = i->end(film);
154 placed.push_back (i);
160 BOOST_FOREACH (shared_ptr<Content> i, cont) {
161 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
165 i->set_position (film, next);
170 /* This won't change order, so it does not need a sort */
176 Playlist::video_identifier () const
180 BOOST_FOREACH (shared_ptr<const Content> i, content()) {
182 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
187 if (i->video || burn) {
188 t += i->identifier ();
193 digester.add (t.c_str(), t.length());
194 return digester.get ();
197 /** @param film Film that this Playlist is for.
198 * @param node <Playlist> node.
199 * @param version Metadata version number.
200 * @param notes Output notes about that happened.
203 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
205 boost::mutex::scoped_lock lm (_mutex);
207 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
208 shared_ptr<Content> content = content_factory (i, version, notes);
210 /* See if this content should be nudged to start on a video frame */
211 DCPTime const old_pos = content->position();
212 content->set_position(film, old_pos);
213 if (old_pos != content->position()) {
214 string note = _("Your project contains video content that was not aligned to a frame boundary.");
216 if (old_pos < content->position()) {
217 note += String::compose(
218 _("The file %1 has been moved %2 milliseconds later."),
219 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
222 note += String::compose(
223 _("The file %1 has been moved %2 milliseconds earlier."),
224 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
227 notes.push_back (note);
230 /* ...or have a start trim which is an integer number of frames */
231 ContentTime const old_trim = content->trim_start();
232 content->set_trim_start(old_trim);
233 if (old_trim != content->trim_start()) {
234 string note = _("Your project contains video content whose trim was not aligned to a frame boundary.");
236 if (old_trim < content->trim_start()) {
237 note += String::compose(
238 _("The file %1 has been trimmed by %2 milliseconds more."),
239 content->path_summary(), ContentTime(content->trim_start() - old_trim).seconds() * 1000
242 note += String::compose(
243 _("The file %1 has been trimmed by %2 milliseconds less."),
244 content->path_summary(), ContentTime(old_trim - content->trim_start()).seconds() * 1000
247 notes.push_back (note);
250 _content.push_back (content);
253 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
254 sort (_content.begin(), _content.end(), ContentSorter ());
259 /** @param node <Playlist> node.
260 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
263 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
265 BOOST_FOREACH (shared_ptr<Content> i, content()) {
266 i->as_xml (node->add_child ("Content"), with_content_paths);
271 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
273 Change (CHANGE_TYPE_PENDING);
276 boost::mutex::scoped_lock lm (_mutex);
277 _content.push_back (c);
278 sort (_content.begin(), _content.end(), ContentSorter ());
282 Change (CHANGE_TYPE_DONE);
288 Playlist::remove (shared_ptr<Content> c)
290 Change (CHANGE_TYPE_PENDING);
292 bool cancelled = false;
295 boost::mutex::scoped_lock lm (_mutex);
297 ContentList::iterator i = _content.begin ();
298 while (i != _content.end() && *i != c) {
302 if (i != _content.end()) {
310 Change (CHANGE_TYPE_CANCELLED);
312 Change (CHANGE_TYPE_DONE);
315 /* This won't change order, so it does not need a sort */
321 Playlist::remove (ContentList c)
323 Change (CHANGE_TYPE_PENDING);
326 boost::mutex::scoped_lock lm (_mutex);
328 BOOST_FOREACH (shared_ptr<Content> i, c) {
329 ContentList::iterator j = _content.begin ();
330 while (j != _content.end() && *j != i) {
334 if (j != _content.end ()) {
340 Change (CHANGE_TYPE_DONE);
342 /* This won't change order, so it does not need a sort */
347 class FrameRateCandidate
350 FrameRateCandidate (float source_, int dcp_)
359 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
361 Playlist::best_video_frame_rate () const
363 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
365 /* Work out what rates we could manage, including those achieved by using skip / repeat */
366 list<FrameRateCandidate> candidates;
368 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
369 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
370 candidates.push_back (FrameRateCandidate(i, i));
373 /* Then the skip/repeat ones */
374 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
375 candidates.push_back (FrameRateCandidate (float(i) / 2, i));
376 candidates.push_back (FrameRateCandidate (float(i) * 2, i));
379 /* Pick the best one */
380 float error = std::numeric_limits<float>::max ();
381 optional<FrameRateCandidate> best;
382 list<FrameRateCandidate>::iterator i = candidates.begin();
383 while (i != candidates.end()) {
385 float this_error = 0;
386 BOOST_FOREACH (shared_ptr<Content> j, content()) {
387 if (!j->video || !j->video_frame_rate()) {
391 /* Best error for this content; we could use the content as-is or double its rate */
392 float best_error = min (
393 float (fabs (i->source - j->video_frame_rate().get())),
394 float (fabs (i->source - j->video_frame_rate().get() * 2))
397 /* Use the largest difference between DCP and source as the "error" */
398 this_error = max (this_error, best_error);
401 if (this_error < error) {
416 /** @return length of the playlist from time 0 to the last thing on the playlist */
418 Playlist::length (shared_ptr<const Film> film) const
421 BOOST_FOREACH (shared_ptr<const Content> i, content()) {
422 len = max (len, i->end(film));
428 /** @return position of the first thing on the playlist, if it's not empty */
430 Playlist::start () const
432 ContentList cont = content ();
434 return optional<DCPTime> ();
437 DCPTime start = DCPTime::max ();
438 BOOST_FOREACH (shared_ptr<Content> i, cont) {
439 start = min (start, i->position ());
445 /** Must be called with a lock held on _mutex */
447 Playlist::disconnect ()
449 BOOST_FOREACH (boost::signals2::connection& i, _content_connections) {
453 _content_connections.clear ();
456 /** Must be called with a lock held on _mutex */
458 Playlist::reconnect (shared_ptr<const Film> film)
462 BOOST_FOREACH (shared_ptr<Content> i, _content) {
463 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
468 Playlist::video_end (shared_ptr<const Film> film) const
471 BOOST_FOREACH (shared_ptr<Content> i, content()) {
473 end = max (end, i->end(film));
481 Playlist::text_end (shared_ptr<const Film> film) const
484 BOOST_FOREACH (shared_ptr<Content> i, content()) {
485 if (!i->text.empty ()) {
486 end = max (end, i->end(film));
494 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
496 ContentList cont = content ();
497 for (ContentList::const_reverse_iterator i = cont.rbegin(); i != cont.rend(); ++i) {
502 if ((*i)->position() <= t) {
503 /* This is the first piece of content (going backwards...) that starts before t,
504 so it's the active one.
506 if ((*i)->video_frame_rate ()) {
507 /* This content specified a rate, so use it */
508 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
510 /* No specified rate so just use the DCP one */
511 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
516 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
520 Playlist::set_sequence (bool s)
526 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
528 if (a->position() != b->position()) {
529 return a->position() < b->position();
532 /* Put video before audio if they start at the same time */
533 if (a->video && !b->video) {
535 } else if (!a->video && b->video) {
540 return a->digest() < b->digest();
543 /** @return content in ascending order of position */
545 Playlist::content () const
547 boost::mutex::scoped_lock lm (_mutex);
552 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
554 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
555 BOOST_FOREACH (shared_ptr<Content> i, c) {
556 range.first = min (range.first, i->position ());
557 range.second = max (range.second, i->position ());
558 range.first = min (range.first, i->end(film));
559 range.second = max (range.second, i->end(film));
562 Change (CHANGE_TYPE_PENDING);
565 boost::mutex::scoped_lock lm (_mutex);
567 DCPTime pos = range.second;
568 for (int i = 0; i < n; ++i) {
569 BOOST_FOREACH (shared_ptr<Content> j, c) {
570 shared_ptr<Content> copy = j->clone ();
571 copy->set_position (film, pos + copy->position() - range.first);
572 _content.push_back (copy);
574 pos += range.second - range.first;
577 sort (_content.begin(), _content.end(), ContentSorter ());
581 Change (CHANGE_TYPE_DONE);
585 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
587 ContentList cont = content ();
588 ContentList::iterator previous = cont.end();
589 ContentList::iterator i = cont.begin();
590 while (i != cont.end() && *i != c) {
595 DCPOMATIC_ASSERT (i != cont.end());
596 if (previous == cont.end()) {
600 shared_ptr<Content> previous_c = *previous;
602 DCPTime const p = previous_c->position ();
603 previous_c->set_position (film, p + c->length_after_trim(film));
604 c->set_position (film, p);
608 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
610 ContentList cont = content ();
611 ContentList::iterator i = cont.begin();
612 while (i != cont.end() && *i != c) {
616 DCPOMATIC_ASSERT (i != cont.end());
618 ContentList::iterator next = i;
621 if (next == cont.end()) {
625 shared_ptr<Content> next_c = *next;
627 next_c->set_position (film, c->position());
628 c->set_position (film, c->position() + next_c->length_after_trim(film));
632 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
634 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
635 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
637 BOOST_FOREACH (shared_ptr<Content> i, content()) {
638 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
640 if (d->reference_video()) {
641 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
643 if (d->reference_audio()) {
644 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
649 /* Add on 64k for bits and pieces (metadata, subs etc) */
650 return video + audio + 65536;
654 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
658 BOOST_FOREACH (shared_ptr<Content> i, content()) {
660 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
662 score += 100 * o.get().duration().get() / period.duration().get();
669 if (score > best_score) {
670 best_summary = i->path(0).leaf().string();
679 Playlist::speed_up_range (int dcp_video_frame_rate) const
681 pair<double, double> range (DBL_MAX, -DBL_MAX);
683 BOOST_FOREACH (shared_ptr<Content> i, content()) {
687 if (i->video_frame_rate()) {
688 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
689 range.first = min (range.first, frc.speed_up);
690 range.second = max (range.second, frc.speed_up);
692 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
693 range.first = min (range.first, frc.speed_up);
694 range.second = max (range.second, frc.speed_up);