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_)
322 Playlist::best_video_frame_rate () const
324 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
326 /* Work out what rates we could manage, including those achieved by using skip / repeat */
327 list<FrameRateCandidate> candidates;
329 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
330 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
331 candidates.push_back (FrameRateCandidate (*i, *i));
334 /* Then the skip/repeat ones */
335 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
336 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
337 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
340 /* Pick the best one */
341 float error = std::numeric_limits<float>::max ();
342 optional<FrameRateCandidate> best;
343 list<FrameRateCandidate>::iterator i = candidates.begin();
344 while (i != candidates.end()) {
346 float this_error = 0;
347 BOOST_FOREACH (shared_ptr<Content> j, _content) {
348 if (!j->video || !j->video_frame_rate()) {
352 /* Best error for this content; we could use the content as-is or double its rate */
353 float best_error = min (
354 float (fabs (i->source - j->video_frame_rate().get())),
355 float (fabs (i->source - j->video_frame_rate().get() * 2))
358 /* Use the largest difference between DCP and source as the "error" */
359 this_error = max (this_error, best_error);
362 if (this_error < error) {
377 /** @return length of the playlist from time 0 to the last thing on the playlist */
379 Playlist::length (shared_ptr<const Film> film) const
382 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
383 len = max (len, i->end(film));
389 /** @return position of the first thing on the playlist, if it's not empty */
391 Playlist::start () const
393 if (_content.empty ()) {
394 return optional<DCPTime> ();
397 DCPTime start = DCPTime::max ();
398 BOOST_FOREACH (shared_ptr<Content> i, _content) {
399 start = min (start, i->position ());
406 Playlist::disconnect ()
408 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
412 _content_connections.clear ();
416 Playlist::reconnect (shared_ptr<const Film> film)
420 BOOST_FOREACH (shared_ptr<Content> i, _content) {
421 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
426 Playlist::video_end (shared_ptr<const Film> film) const
429 BOOST_FOREACH (shared_ptr<Content> i, _content) {
431 end = max (end, i->end(film));
439 Playlist::text_end (shared_ptr<const Film> film) const
442 BOOST_FOREACH (shared_ptr<Content> i, _content) {
443 if (!i->text.empty ()) {
444 end = max (end, i->end(film));
452 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
454 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
459 if ((*i)->position() <= t) {
460 /* This is the first piece of content (going backwards...) that starts before t,
461 so it's the active one.
463 if ((*i)->video_frame_rate ()) {
464 /* This content specified a rate, so use it */
465 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
467 /* No specified rate so just use the DCP one */
468 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
473 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
477 Playlist::set_sequence (bool s)
483 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
485 if (a->position() != b->position()) {
486 return a->position() < b->position();
489 /* Put video before audio if they start at the same time */
490 if (a->video && !b->video) {
492 } else if (!a->video && b->video) {
497 return a->digest() < b->digest();
500 /** @return content in ascending order of position */
502 Playlist::content () const
508 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
510 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
511 BOOST_FOREACH (shared_ptr<Content> i, c) {
512 range.first = min (range.first, i->position ());
513 range.second = max (range.second, i->position ());
514 range.first = min (range.first, i->end(film));
515 range.second = max (range.second, i->end(film));
518 Change (CHANGE_TYPE_PENDING);
520 DCPTime pos = range.second;
521 for (int i = 0; i < n; ++i) {
522 BOOST_FOREACH (shared_ptr<Content> j, c) {
523 shared_ptr<Content> copy = j->clone ();
524 copy->set_position (film, pos + copy->position() - range.first);
525 _content.push_back (copy);
527 pos += range.second - range.first;
530 sort (_content.begin(), _content.end(), ContentSorter ());
533 Change (CHANGE_TYPE_DONE);
537 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
539 ContentList::iterator previous = _content.end ();
540 ContentList::iterator i = _content.begin();
541 while (i != _content.end() && *i != c) {
546 DCPOMATIC_ASSERT (i != _content.end ());
547 if (previous == _content.end ()) {
551 shared_ptr<Content> previous_c = *previous;
553 DCPTime const p = previous_c->position ();
554 previous_c->set_position (film, p + c->length_after_trim(film));
555 c->set_position (film, p);
559 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
561 ContentList::iterator i = _content.begin();
562 while (i != _content.end() && *i != c) {
566 DCPOMATIC_ASSERT (i != _content.end ());
568 ContentList::iterator next = i;
571 if (next == _content.end ()) {
575 shared_ptr<Content> next_c = *next;
577 next_c->set_position (film, c->position());
578 c->set_position (film, c->position() + next_c->length_after_trim(film));
582 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
584 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
585 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
587 BOOST_FOREACH (shared_ptr<Content> i, _content) {
588 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
590 if (d->reference_video()) {
591 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
593 if (d->reference_audio()) {
594 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
599 /* Add on 64k for bits and pieces (metadata, subs etc) */
600 return video + audio + 65536;
604 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
608 BOOST_FOREACH (shared_ptr<Content> i, _content) {
610 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
612 score += 100 * o.get().duration().get() / period.duration().get();
619 if (score > best_score) {
620 best_summary = i->path(0).leaf().string();
629 Playlist::speed_up_range (int dcp_video_frame_rate) const
631 pair<double, double> range (DBL_MAX, -DBL_MAX);
633 BOOST_FOREACH (shared_ptr<Content> i, _content) {
637 if (i->video_frame_rate()) {
638 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
639 range.first = min (range.first, frc.speed_up);
640 range.second = max (range.second, frc.speed_up);
642 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
643 range.first = min (range.first, frc.speed_up);
644 range.second = max (range.second, frc.speed_up);