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>
49 using boost::optional;
50 using std::shared_ptr;
52 using std::dynamic_pointer_cast;
53 using namespace dcpomatic;
54 #if BOOST_VERSION >= 106100
55 using namespace boost::placeholders;
65 Playlist::~Playlist ()
67 boost::mutex::scoped_lock lm (_mutex);
73 Playlist::content_change (weak_ptr<const Film> weak_film, ChangeType type, weak_ptr<Content> content, int property, bool frequent)
75 auto film = weak_film.lock ();
76 DCPOMATIC_ASSERT (film);
78 if (type == ChangeType::DONE) {
80 property == ContentProperty::TRIM_START ||
81 property == ContentProperty::TRIM_END ||
82 property == ContentProperty::LENGTH ||
83 property == VideoContentProperty::FRAME_TYPE
85 /* Don't respond to position changes here, as:
86 - sequencing after earlier/later changes is handled by move_earlier/move_later
87 - any other position changes will be timeline drags which should not result in content
90 maybe_sequence (film);
94 property == ContentProperty::POSITION ||
95 property == ContentProperty::LENGTH ||
96 property == ContentProperty::TRIM_START ||
97 property == ContentProperty::TRIM_END) {
102 boost::mutex::scoped_lock lm (_mutex);
103 ContentList old = _content;
104 sort (_content.begin(), _content.end(), ContentSorter ());
105 changed = _content != old;
112 /* The length might have changed, and that's good enough for this signal */
117 ContentChange (type, content, property, frequent);
121 Playlist::maybe_sequence (shared_ptr<const Film> film)
123 if (!_sequence || _sequencing) {
129 ContentList cont = content ();
131 /* Keep track of the content that we've set the position of so that we don't
145 if (i->video->frame_type() == VideoFrameType::THREE_D_RIGHT) {
146 i->set_position (film, next_right);
147 next_right = i->end(film);
149 i->set_position (film, next_left);
150 next_left = i->end(film);
153 placed.push_back (i);
160 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
164 i->set_position (film, next);
169 /* This won't change order, so it does not need a sort */
175 Playlist::video_identifier () const
179 for (auto i: content()) {
181 for (auto j: i->text) {
186 if (i->video || burn) {
187 t += i->identifier ();
192 digester.add (t.c_str(), t.length());
193 return digester.get ();
196 /** @param film Film that this Playlist is for.
197 * @param node <Playlist> node.
198 * @param version Metadata version number.
199 * @param notes Output notes about that happened.
202 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
204 boost::mutex::scoped_lock lm (_mutex);
206 for (auto i: node->node_children ("Content")) {
207 auto content = content_factory (i, version, notes);
209 /* See if this content should be nudged to start on a video frame */
210 auto const old_pos = content->position();
211 content->set_position(film, old_pos);
212 if (old_pos != content->position()) {
213 string note = _("Your project contains video content that was not aligned to a frame boundary.");
215 if (old_pos < content->position()) {
216 note += String::compose(
217 _("The file %1 has been moved %2 milliseconds later."),
218 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
221 note += String::compose(
222 _("The file %1 has been moved %2 milliseconds earlier."),
223 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
226 notes.push_back (note);
229 /* ...or have a start trim which is an integer number of frames */
230 ContentTime const old_trim = content->trim_start();
231 content->set_trim_start(old_trim);
232 if (old_trim != content->trim_start()) {
233 string note = _("Your project contains video content whose trim was not aligned to a frame boundary.");
235 if (old_trim < content->trim_start()) {
236 note += String::compose(
237 _("The file %1 has been trimmed by %2 milliseconds more."),
238 content->path_summary(), ContentTime(content->trim_start() - old_trim).seconds() * 1000
241 note += String::compose(
242 _("The file %1 has been trimmed by %2 milliseconds less."),
243 content->path_summary(), ContentTime(old_trim - content->trim_start()).seconds() * 1000
246 notes.push_back (note);
249 _content.push_back (content);
252 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
253 sort (_content.begin(), _content.end(), ContentSorter ());
258 /** @param node <Playlist> node.
259 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
262 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
264 for (auto i: content()) {
265 i->as_xml (node->add_child ("Content"), with_content_paths);
270 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
272 Change (ChangeType::PENDING);
275 boost::mutex::scoped_lock lm (_mutex);
276 _content.push_back (c);
277 sort (_content.begin(), _content.end(), ContentSorter ());
281 Change (ChangeType::DONE);
287 Playlist::remove (shared_ptr<Content> c)
289 Change (ChangeType::PENDING);
291 bool cancelled = false;
294 boost::mutex::scoped_lock lm (_mutex);
296 auto i = _content.begin ();
297 while (i != _content.end() && *i != c) {
301 if (i != _content.end()) {
309 Change (ChangeType::CANCELLED);
311 Change (ChangeType::DONE);
314 /* This won't change order, so it does not need a sort */
320 Playlist::remove (ContentList c)
322 Change (ChangeType::PENDING);
325 boost::mutex::scoped_lock lm (_mutex);
328 ContentList::iterator j = _content.begin ();
329 while (j != _content.end() && *j != i) {
333 if (j != _content.end ()) {
339 Change (ChangeType::DONE);
341 /* This won't change order, so it does not need a sort */
346 class FrameRateCandidate
349 FrameRateCandidate (float source_, int dcp_)
358 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
360 Playlist::best_video_frame_rate () const
362 auto const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
364 /* Work out what rates we could manage, including those achieved by using skip / repeat */
365 list<FrameRateCandidate> candidates;
367 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
368 for (auto i: allowed_dcp_frame_rates) {
369 candidates.push_back (FrameRateCandidate(i, i));
372 /* Then the skip/repeat ones */
373 for (auto i: allowed_dcp_frame_rates) {
374 candidates.push_back (FrameRateCandidate (float(i) / 2, i));
375 candidates.push_back (FrameRateCandidate (float(i) * 2, i));
378 /* Pick the best one */
379 float error = std::numeric_limits<float>::max ();
380 optional<FrameRateCandidate> best;
381 auto i = candidates.begin();
382 while (i != candidates.end()) {
384 float this_error = 0;
385 for (auto j: content()) {
386 if (!j->video || !j->video_frame_rate()) {
390 /* Best error for this content; we could use the content as-is or double its rate */
391 float best_error = min (
392 float (fabs (i->source - j->video_frame_rate().get())),
393 float (fabs (i->source - j->video_frame_rate().get() * 2))
396 /* Use the largest difference between DCP and source as the "error" */
397 this_error = max (this_error, best_error);
400 if (this_error < error) {
415 /** @return length of the playlist from time 0 to the last thing on the playlist */
417 Playlist::length (shared_ptr<const Film> film) const
420 for (auto i: content()) {
421 len = max (len, i->end(film));
427 /** @return position of the first thing on the playlist, if it's not empty */
429 Playlist::start () const
431 auto cont = content ();
436 auto start = DCPTime::max ();
438 start = min (start, i->position ());
444 /** Must be called with a lock held on _mutex */
446 Playlist::disconnect ()
448 for (auto& i: _content_connections) {
452 _content_connections.clear ();
455 /** Must be called with a lock held on _mutex */
457 Playlist::reconnect (shared_ptr<const Film> film)
461 for (auto i: _content) {
462 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
467 Playlist::video_end (shared_ptr<const Film> film) const
470 for (auto i: content()) {
472 end = max (end, i->end(film));
480 Playlist::text_end (shared_ptr<const Film> film) const
483 for (auto i: content()) {
484 if (!i->text.empty ()) {
485 end = max (end, i->end(film));
493 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
495 ContentList cont = content ();
496 for (ContentList::const_reverse_iterator i = cont.rbegin(); i != cont.rend(); ++i) {
501 if ((*i)->position() <= t) {
502 /* This is the first piece of content (going backwards...) that starts before t,
503 so it's the active one.
505 if ((*i)->video_frame_rate ()) {
506 /* This content specified a rate, so use it */
507 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
509 /* No specified rate so just use the DCP one */
510 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
515 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
519 Playlist::set_sequence (bool s)
525 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
527 if (a->position() != b->position()) {
528 return a->position() < b->position();
531 /* Put video before audio if they start at the same time */
532 if (a->video && !b->video) {
534 } else if (!a->video && b->video) {
539 return a->digest() < b->digest();
542 /** @return content in ascending order of position */
544 Playlist::content () const
546 boost::mutex::scoped_lock lm (_mutex);
551 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
553 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
555 range.first = min (range.first, i->position ());
556 range.second = max (range.second, i->position ());
557 range.first = min (range.first, i->end(film));
558 range.second = max (range.second, i->end(film));
561 Change (ChangeType::PENDING);
564 boost::mutex::scoped_lock lm (_mutex);
566 DCPTime pos = range.second;
567 for (int i = 0; i < n; ++i) {
569 auto copy = j->clone ();
570 copy->set_position (film, pos + copy->position() - range.first);
571 _content.push_back (copy);
573 pos += range.second - range.first;
576 sort (_content.begin(), _content.end(), ContentSorter ());
580 Change (ChangeType::DONE);
584 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
586 auto cont = content ();
587 auto previous = cont.end();
588 auto i = cont.begin();
589 while (i != cont.end() && *i != c) {
594 DCPOMATIC_ASSERT (i != cont.end());
595 if (previous == cont.end()) {
599 auto previous_c = *previous;
601 auto const p = previous_c->position ();
602 previous_c->set_position (film, p + c->length_after_trim(film));
603 c->set_position (film, p);
607 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
609 auto cont = content ();
610 auto i = cont.begin();
611 while (i != cont.end() && *i != c) {
615 DCPOMATIC_ASSERT (i != cont.end());
617 ContentList::iterator next = i;
620 if (next == cont.end()) {
626 next_c->set_position (film, c->position());
627 c->set_position (film, c->position() + next_c->length_after_trim(film));
631 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
633 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
634 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
636 for (auto i: content()) {
637 auto d = dynamic_pointer_cast<DCPContent> (i);
639 if (d->reference_video()) {
640 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
642 if (d->reference_audio()) {
643 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
648 /* Add on 64k for bits and pieces (metadata, subs etc) */
649 return video + audio + 65536;
653 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
657 for (auto i: content()) {
659 auto const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
661 score += 100 * o.get().duration().get() / period.duration().get();
668 if (score > best_score) {
669 best_summary = i->path(0).leaf().string();
678 Playlist::speed_up_range (int dcp_video_frame_rate) const
680 pair<double, double> range (DBL_MAX, -DBL_MAX);
682 for (auto i: content()) {
686 if (i->video_frame_rate()) {
687 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
688 range.first = min (range.first, frc.speed_up);
689 range.second = max (range.second, frc.speed_up);
691 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
692 range.first = min (range.first, frc.speed_up);
693 range.second = max (range.second, frc.speed_up);