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 shared_ptr<const Film> film = weak_film.lock ();
74 DCPOMATIC_ASSERT (film);
76 if (type == CHANGE_TYPE_DONE) {
78 property == ContentProperty::TRIM_START ||
79 property == ContentProperty::TRIM_END ||
80 property == ContentProperty::LENGTH ||
81 property == VideoContentProperty::FRAME_TYPE
83 /* Don't respond to position changes here, as:
84 - sequencing after earlier/later changes is handled by move_earlier/move_later
85 - any other position changes will be timeline drags which should not result in content
88 maybe_sequence (film);
92 property == ContentProperty::POSITION ||
93 property == ContentProperty::LENGTH ||
94 property == ContentProperty::TRIM_START ||
95 property == ContentProperty::TRIM_END) {
100 boost::mutex::scoped_lock lm (_mutex);
101 ContentList old = _content;
102 sort (_content.begin(), _content.end(), ContentSorter ());
103 changed = _content != old;
110 /* The length might have changed, and that's good enough for this signal */
115 ContentChange (type, content, property, frequent);
119 Playlist::maybe_sequence (shared_ptr<const Film> film)
121 if (!_sequence || _sequencing) {
127 ContentList cont = content ();
129 /* Keep track of the content that we've set the position of so that we don't
138 BOOST_FOREACH (shared_ptr<Content> i, cont) {
143 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
144 i->set_position (film, next_right);
145 next_right = i->end(film);
147 i->set_position (film, next_left);
148 next_left = i->end(film);
151 placed.push_back (i);
157 BOOST_FOREACH (shared_ptr<Content> i, cont) {
158 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
162 i->set_position (film, next);
167 /* This won't change order, so it does not need a sort */
173 Playlist::video_identifier () const
177 BOOST_FOREACH (shared_ptr<const Content> i, content()) {
179 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
184 if (i->video || burn) {
185 t += i->identifier ();
190 digester.add (t.c_str(), t.length());
191 return digester.get ();
194 /** @param film Film that this Playlist is for.
195 * @param node <Playlist> node.
196 * @param version Metadata version number.
197 * @param notes Output notes about that happened.
200 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
202 boost::mutex::scoped_lock lm (_mutex);
204 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
205 shared_ptr<Content> content = content_factory (i, version, notes);
207 /* See if this content should be nudged to start on a video frame */
208 DCPTime const old_pos = content->position();
209 content->set_position(film, old_pos);
210 if (old_pos != content->position()) {
211 string note = _("Your project contains video content that was not aligned to a frame boundary.");
213 if (old_pos < content->position()) {
214 note += String::compose(
215 _("The file %1 has been moved %2 milliseconds later."),
216 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
219 note += String::compose(
220 _("The file %1 has been moved %2 milliseconds earlier."),
221 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
224 notes.push_back (note);
227 /* ...or have a start trim which is an integer number of frames */
228 ContentTime const old_trim = content->trim_start();
229 content->set_trim_start(old_trim);
230 if (old_trim != content->trim_start()) {
231 string note = _("Your project contains video content whose trim was not aligned to a frame boundary.");
233 if (old_trim < content->trim_start()) {
234 note += String::compose(
235 _("The file %1 has been trimmed by %2 milliseconds more."),
236 content->path_summary(), ContentTime(content->trim_start() - old_trim).seconds() * 1000
239 note += String::compose(
240 _("The file %1 has been trimmed by %2 milliseconds less."),
241 content->path_summary(), ContentTime(old_trim - content->trim_start()).seconds() * 1000
244 notes.push_back (note);
247 _content.push_back (content);
250 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
251 sort (_content.begin(), _content.end(), ContentSorter ());
256 /** @param node <Playlist> node.
257 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
260 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
262 BOOST_FOREACH (shared_ptr<Content> i, content()) {
263 i->as_xml (node->add_child ("Content"), with_content_paths);
268 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
270 Change (CHANGE_TYPE_PENDING);
273 boost::mutex::scoped_lock lm (_mutex);
274 _content.push_back (c);
275 sort (_content.begin(), _content.end(), ContentSorter ());
279 Change (CHANGE_TYPE_DONE);
285 Playlist::remove (shared_ptr<Content> c)
287 Change (CHANGE_TYPE_PENDING);
289 bool cancelled = false;
292 boost::mutex::scoped_lock lm (_mutex);
294 ContentList::iterator i = _content.begin ();
295 while (i != _content.end() && *i != c) {
299 if (i != _content.end()) {
307 Change (CHANGE_TYPE_CANCELLED);
309 Change (CHANGE_TYPE_DONE);
312 /* 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 Change (CHANGE_TYPE_DONE);
339 /* This won't change order, so it does not need a sort */
344 class FrameRateCandidate
347 FrameRateCandidate (float source_, int dcp_)
356 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
358 Playlist::best_video_frame_rate () const
360 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
362 /* Work out what rates we could manage, including those achieved by using skip / repeat */
363 list<FrameRateCandidate> candidates;
365 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
366 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
367 candidates.push_back (FrameRateCandidate(i, i));
370 /* Then the skip/repeat ones */
371 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
372 candidates.push_back (FrameRateCandidate (float(i) / 2, i));
373 candidates.push_back (FrameRateCandidate (float(i) * 2, i));
376 /* Pick the best one */
377 float error = std::numeric_limits<float>::max ();
378 optional<FrameRateCandidate> best;
379 list<FrameRateCandidate>::iterator i = candidates.begin();
380 while (i != candidates.end()) {
382 float this_error = 0;
383 BOOST_FOREACH (shared_ptr<Content> j, content()) {
384 if (!j->video || !j->video_frame_rate()) {
388 /* Best error for this content; we could use the content as-is or double its rate */
389 float best_error = min (
390 float (fabs (i->source - j->video_frame_rate().get())),
391 float (fabs (i->source - j->video_frame_rate().get() * 2))
394 /* Use the largest difference between DCP and source as the "error" */
395 this_error = max (this_error, best_error);
398 if (this_error < error) {
413 /** @return length of the playlist from time 0 to the last thing on the playlist */
415 Playlist::length (shared_ptr<const Film> film) const
418 BOOST_FOREACH (shared_ptr<const Content> i, content()) {
419 len = max (len, i->end(film));
425 /** @return position of the first thing on the playlist, if it's not empty */
427 Playlist::start () const
429 ContentList cont = content ();
431 return optional<DCPTime> ();
434 DCPTime start = DCPTime::max ();
435 BOOST_FOREACH (shared_ptr<Content> i, cont) {
436 start = min (start, i->position ());
442 /** Must be called with a lock held on _mutex */
444 Playlist::disconnect ()
446 BOOST_FOREACH (boost::signals2::connection& i, _content_connections) {
450 _content_connections.clear ();
453 /** Must be called with a lock held on _mutex */
455 Playlist::reconnect (shared_ptr<const Film> film)
459 BOOST_FOREACH (shared_ptr<Content> i, _content) {
460 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
465 Playlist::video_end (shared_ptr<const Film> film) const
468 BOOST_FOREACH (shared_ptr<Content> i, content()) {
470 end = max (end, i->end(film));
478 Playlist::text_end (shared_ptr<const Film> film) const
481 BOOST_FOREACH (shared_ptr<Content> i, content()) {
482 if (!i->text.empty ()) {
483 end = max (end, i->end(film));
491 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
493 ContentList cont = content ();
494 for (ContentList::const_reverse_iterator i = cont.rbegin(); i != cont.rend(); ++i) {
499 if ((*i)->position() <= t) {
500 /* This is the first piece of content (going backwards...) that starts before t,
501 so it's the active one.
503 if ((*i)->video_frame_rate ()) {
504 /* This content specified a rate, so use it */
505 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
507 /* No specified rate so just use the DCP one */
508 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
513 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
517 Playlist::set_sequence (bool s)
523 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
525 if (a->position() != b->position()) {
526 return a->position() < b->position();
529 /* Put video before audio if they start at the same time */
530 if (a->video && !b->video) {
532 } else if (!a->video && b->video) {
537 return a->digest() < b->digest();
540 /** @return content in ascending order of position */
542 Playlist::content () const
544 boost::mutex::scoped_lock lm (_mutex);
549 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
551 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
552 BOOST_FOREACH (shared_ptr<Content> i, c) {
553 range.first = min (range.first, i->position ());
554 range.second = max (range.second, i->position ());
555 range.first = min (range.first, i->end(film));
556 range.second = max (range.second, i->end(film));
559 Change (CHANGE_TYPE_PENDING);
562 boost::mutex::scoped_lock lm (_mutex);
564 DCPTime pos = range.second;
565 for (int i = 0; i < n; ++i) {
566 BOOST_FOREACH (shared_ptr<Content> j, c) {
567 shared_ptr<Content> copy = j->clone ();
568 copy->set_position (film, pos + copy->position() - range.first);
569 _content.push_back (copy);
571 pos += range.second - range.first;
574 sort (_content.begin(), _content.end(), ContentSorter ());
578 Change (CHANGE_TYPE_DONE);
582 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
584 ContentList cont = content ();
585 ContentList::iterator previous = cont.end();
586 ContentList::iterator i = cont.begin();
587 while (i != cont.end() && *i != c) {
592 DCPOMATIC_ASSERT (i != cont.end());
593 if (previous == cont.end()) {
597 shared_ptr<Content> previous_c = *previous;
599 DCPTime const p = previous_c->position ();
600 previous_c->set_position (film, p + c->length_after_trim(film));
601 c->set_position (film, p);
605 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
607 ContentList cont = content ();
608 ContentList::iterator i = cont.begin();
609 while (i != cont.end() && *i != c) {
613 DCPOMATIC_ASSERT (i != cont.end());
615 ContentList::iterator next = i;
618 if (next == cont.end()) {
622 shared_ptr<Content> next_c = *next;
624 next_c->set_position (film, c->position());
625 c->set_position (film, c->position() + next_c->length_after_trim(film));
629 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
631 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
632 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
634 BOOST_FOREACH (shared_ptr<Content> i, content()) {
635 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
637 if (d->reference_video()) {
638 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
640 if (d->reference_audio()) {
641 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
646 /* Add on 64k for bits and pieces (metadata, subs etc) */
647 return video + audio + 65536;
651 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
655 BOOST_FOREACH (shared_ptr<Content> i, content()) {
657 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
659 score += 100 * o.get().duration().get() / period.duration().get();
666 if (score > best_score) {
667 best_summary = i->path(0).leaf().string();
676 Playlist::speed_up_range (int dcp_video_frame_rate) const
678 pair<double, double> range (DBL_MAX, -DBL_MAX);
680 BOOST_FOREACH (shared_ptr<Content> i, content()) {
684 if (i->video_frame_rate()) {
685 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
686 range.first = min (range.first, frc.speed_up);
687 range.second = max (range.second, frc.speed_up);
689 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
690 range.first = min (range.first, frc.speed_up);
691 range.second = max (range.second, frc.speed_up);