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;
62 Playlist::~Playlist ()
69 Playlist::content_change (weak_ptr<const Film> weak_film, ChangeType type, weak_ptr<Content> content, int property, bool frequent)
71 /* Make sure we only hear about atomic changes (e.g. a PENDING always with the DONE/CANCELLED)
72 Ignore any DONE/CANCELLED that arrives without a PENDING.
74 if (_checker.send (type, property)) {
78 shared_ptr<const Film> film = weak_film.lock ();
79 DCPOMATIC_ASSERT (film);
81 if (type == CHANGE_TYPE_DONE) {
83 property == ContentProperty::TRIM_START ||
84 property == ContentProperty::TRIM_END ||
85 property == ContentProperty::LENGTH ||
86 property == VideoContentProperty::FRAME_TYPE
88 /* Don't respond to position changes here, as:
89 - sequencing after earlier/later changes is handled by move_earlier/move_later
90 - any other position changes will be timeline drags which should not result in content
93 maybe_sequence (film);
97 property == ContentProperty::POSITION ||
98 property == ContentProperty::LENGTH ||
99 property == ContentProperty::TRIM_START ||
100 property == ContentProperty::TRIM_END) {
102 ContentList old = _content;
103 sort (_content.begin(), _content.end(), ContentSorter ());
104 if (_content != old) {
110 ContentChange (type, content, property, frequent);
114 Playlist::maybe_sequence (shared_ptr<const Film> film)
116 if (!_sequence || _sequencing) {
122 /* Keep track of the content that we've set the position of so that we don't
131 BOOST_FOREACH (shared_ptr<Content> i, _content) {
136 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
137 i->set_position (film, next_right);
138 next_right = i->end(film);
140 i->set_position (film, next_left);
141 next_left = i->end(film);
144 placed.push_back (i);
150 BOOST_FOREACH (shared_ptr<Content> i, _content) {
151 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
155 i->set_position (film, next);
160 /* This won't change order, so it does not need a sort */
166 Playlist::video_identifier () const
170 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
172 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
177 if (i->video || burn) {
178 t += i->identifier ();
183 digester.add (t.c_str(), t.length());
184 return digester.get ();
187 /** @param film Film that this Playlist is for.
188 * @param node <Playlist> node.
189 * @param version Metadata version number.
190 * @param notes Output notes about that happened.
193 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
195 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
196 shared_ptr<Content> content = content_factory (i, version, notes);
198 /* See if this content should be nudged to start on a video frame */
199 DCPTime const old_pos = content->position();
200 content->set_position(film, old_pos);
201 if (old_pos != content->position()) {
202 string note = _("Your project contains video content that was not aligned to a frame boundary.");
204 if (old_pos < content->position()) {
205 note += String::compose(
206 _("The file %1 has been moved %2 milliseconds later."),
207 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
210 note += String::compose(
211 _("The file %1 has been moved %2 milliseconds earlier."),
212 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
215 notes.push_back (note);
218 /* ...or have a start trim which is an integer number of frames */
219 ContentTime const old_trim = content->trim_start();
220 content->set_trim_start(old_trim);
221 if (old_trim != content->trim_start()) {
222 string note = _("Your project contains video content whose trim was not aligned to a frame boundary.");
224 if (old_trim < content->trim_start()) {
225 note += String::compose(
226 _("The file %1 has been trimmed by %2 milliseconds more."),
227 content->path_summary(), ContentTime(content->trim_start() - old_trim).seconds() * 1000
230 note += String::compose(
231 _("The file %1 has been trimmed by %2 milliseconds less."),
232 content->path_summary(), ContentTime(old_trim - content->trim_start()).seconds() * 1000
235 notes.push_back (note);
238 _content.push_back (content);
241 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
242 sort (_content.begin(), _content.end(), ContentSorter ());
247 /** @param node <Playlist> node.
248 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
251 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
253 BOOST_FOREACH (shared_ptr<Content> i, _content) {
254 i->as_xml (node->add_child ("Content"), with_content_paths);
259 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
261 Change (CHANGE_TYPE_PENDING);
262 _content.push_back (c);
263 sort (_content.begin(), _content.end(), ContentSorter ());
265 Change (CHANGE_TYPE_DONE);
269 Playlist::remove (shared_ptr<Content> c)
271 Change (CHANGE_TYPE_PENDING);
273 ContentList::iterator i = _content.begin ();
274 while (i != _content.end() && *i != c) {
278 if (i != _content.end ()) {
280 Change (CHANGE_TYPE_DONE);
282 Change (CHANGE_TYPE_CANCELLED);
285 /* This won't change order, so it does not need a sort */
289 Playlist::remove (ContentList c)
291 Change (CHANGE_TYPE_PENDING);
293 BOOST_FOREACH (shared_ptr<Content> i, c) {
294 ContentList::iterator j = _content.begin ();
295 while (j != _content.end() && *j != i) {
299 if (j != _content.end ()) {
304 /* This won't change order, so it does not need a sort */
306 Change (CHANGE_TYPE_DONE);
309 class FrameRateCandidate
312 FrameRateCandidate (float source_, int dcp_)
321 /** @return the best frame rate from Config::_allowed_dcp_frame_rates for the content in this list */
323 Playlist::best_video_frame_rate () const
325 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
327 /* Work out what rates we could manage, including those achieved by using skip / repeat */
328 list<FrameRateCandidate> candidates;
330 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
331 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
332 candidates.push_back (FrameRateCandidate(i, i));
335 /* Then the skip/repeat ones */
336 BOOST_FOREACH (int i, allowed_dcp_frame_rates) {
337 candidates.push_back (FrameRateCandidate (float(i) / 2, i));
338 candidates.push_back (FrameRateCandidate (float(i) * 2, i));
341 /* Pick the best one */
342 float error = std::numeric_limits<float>::max ();
343 optional<FrameRateCandidate> best;
344 list<FrameRateCandidate>::iterator i = candidates.begin();
345 while (i != candidates.end()) {
347 float this_error = 0;
348 BOOST_FOREACH (shared_ptr<Content> j, _content) {
349 if (!j->video || !j->video_frame_rate()) {
353 /* Best error for this content; we could use the content as-is or double its rate */
354 float best_error = min (
355 float (fabs (i->source - j->video_frame_rate().get())),
356 float (fabs (i->source - j->video_frame_rate().get() * 2))
359 /* Use the largest difference between DCP and source as the "error" */
360 this_error = max (this_error, best_error);
363 if (this_error < error) {
378 /** @return length of the playlist from time 0 to the last thing on the playlist */
380 Playlist::length (shared_ptr<const Film> film) const
383 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
384 len = max (len, i->end(film));
390 /** @return position of the first thing on the playlist, if it's not empty */
392 Playlist::start () const
394 if (_content.empty ()) {
395 return optional<DCPTime> ();
398 DCPTime start = DCPTime::max ();
399 BOOST_FOREACH (shared_ptr<Content> i, _content) {
400 start = min (start, i->position ());
407 Playlist::disconnect ()
409 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
413 _content_connections.clear ();
417 Playlist::reconnect (shared_ptr<const Film> film)
421 BOOST_FOREACH (shared_ptr<Content> i, _content) {
422 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
427 Playlist::video_end (shared_ptr<const Film> film) const
430 BOOST_FOREACH (shared_ptr<Content> i, _content) {
432 end = max (end, i->end(film));
440 Playlist::text_end (shared_ptr<const Film> film) const
443 BOOST_FOREACH (shared_ptr<Content> i, _content) {
444 if (!i->text.empty ()) {
445 end = max (end, i->end(film));
453 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
455 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
460 if ((*i)->position() <= t) {
461 /* This is the first piece of content (going backwards...) that starts before t,
462 so it's the active one.
464 if ((*i)->video_frame_rate ()) {
465 /* This content specified a rate, so use it */
466 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
468 /* No specified rate so just use the DCP one */
469 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
474 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
478 Playlist::set_sequence (bool s)
484 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
486 if (a->position() != b->position()) {
487 return a->position() < b->position();
490 /* Put video before audio if they start at the same time */
491 if (a->video && !b->video) {
493 } else if (!a->video && b->video) {
498 return a->digest() < b->digest();
501 /** @return content in ascending order of position */
503 Playlist::content () const
509 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
511 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
512 BOOST_FOREACH (shared_ptr<Content> i, c) {
513 range.first = min (range.first, i->position ());
514 range.second = max (range.second, i->position ());
515 range.first = min (range.first, i->end(film));
516 range.second = max (range.second, i->end(film));
519 Change (CHANGE_TYPE_PENDING);
521 DCPTime pos = range.second;
522 for (int i = 0; i < n; ++i) {
523 BOOST_FOREACH (shared_ptr<Content> j, c) {
524 shared_ptr<Content> copy = j->clone ();
525 copy->set_position (film, pos + copy->position() - range.first);
526 _content.push_back (copy);
528 pos += range.second - range.first;
531 sort (_content.begin(), _content.end(), ContentSorter ());
534 Change (CHANGE_TYPE_DONE);
538 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
540 ContentList::iterator previous = _content.end ();
541 ContentList::iterator i = _content.begin();
542 while (i != _content.end() && *i != c) {
547 DCPOMATIC_ASSERT (i != _content.end ());
548 if (previous == _content.end ()) {
552 shared_ptr<Content> previous_c = *previous;
554 DCPTime const p = previous_c->position ();
555 previous_c->set_position (film, p + c->length_after_trim(film));
556 c->set_position (film, p);
560 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
562 ContentList::iterator i = _content.begin();
563 while (i != _content.end() && *i != c) {
567 DCPOMATIC_ASSERT (i != _content.end ());
569 ContentList::iterator next = i;
572 if (next == _content.end ()) {
576 shared_ptr<Content> next_c = *next;
578 next_c->set_position (film, c->position());
579 c->set_position (film, c->position() + next_c->length_after_trim(film));
583 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
585 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
586 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
588 BOOST_FOREACH (shared_ptr<Content> i, _content) {
589 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
591 if (d->reference_video()) {
592 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
594 if (d->reference_audio()) {
595 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
600 /* Add on 64k for bits and pieces (metadata, subs etc) */
601 return video + audio + 65536;
605 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
609 BOOST_FOREACH (shared_ptr<Content> i, _content) {
611 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
613 score += 100 * o.get().duration().get() / period.duration().get();
620 if (score > best_score) {
621 best_summary = i->path(0).leaf().string();
630 Playlist::speed_up_range (int dcp_video_frame_rate) const
632 pair<double, double> range (DBL_MAX, -DBL_MAX);
634 BOOST_FOREACH (shared_ptr<Content> i, _content) {
638 if (i->video_frame_rate()) {
639 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
640 range.first = min (range.first, frc.speed_up);
641 range.second = max (range.second, frc.speed_up);
643 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
644 range.first = min (range.first, frc.speed_up);
645 range.second = max (range.second, frc.speed_up);