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 shared_ptr<const Film> film = weak_film.lock ();
72 DCPOMATIC_ASSERT (film);
74 if (type == CHANGE_TYPE_DONE) {
76 property == ContentProperty::TRIM_START ||
77 property == ContentProperty::TRIM_END ||
78 property == ContentProperty::LENGTH ||
79 property == VideoContentProperty::FRAME_TYPE
81 /* Don't respond to position changes here, as:
82 - sequencing after earlier/later changes is handled by move_earlier/move_later
83 - any other position changes will be timeline drags which should not result in content
86 maybe_sequence (film);
90 property == ContentProperty::POSITION ||
91 property == ContentProperty::LENGTH ||
92 property == ContentProperty::TRIM_START ||
93 property == ContentProperty::TRIM_END) {
95 ContentList old = _content;
96 sort (_content.begin(), _content.end(), ContentSorter ());
97 if (_content != old) {
103 ContentChange (type, content, property, frequent);
107 Playlist::maybe_sequence (shared_ptr<const Film> film)
109 if (!_sequence || _sequencing) {
115 /* Keep track of the content that we've set the position of so that we don't
124 BOOST_FOREACH (shared_ptr<Content> i, _content) {
129 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
130 i->set_position (film, next_right);
131 next_right = i->end(film);
133 i->set_position (film, next_left);
134 next_left = i->end(film);
137 placed.push_back (i);
143 BOOST_FOREACH (shared_ptr<Content> i, _content) {
144 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
148 i->set_position (film, next);
153 /* This won't change order, so it does not need a sort */
159 Playlist::video_identifier () const
163 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
165 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
170 if (i->video || burn) {
171 t += i->identifier ();
176 digester.add (t.c_str(), t.length());
177 return digester.get ();
180 /** @param film Film that this Playlist is for.
181 * @param node <Playlist> node.
182 * @param version Metadata version number.
183 * @param notes Output notes about that happened.
186 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
188 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
189 shared_ptr<Content> content = content_factory (i, version, notes);
191 /* See if this content should be nudged to start on a video frame */
192 DCPTime const old_pos = content->position();
193 content->set_position(film, old_pos);
194 if (old_pos != content->position()) {
195 string note = _("Your project contains video content that was not aligned to a frame boundary.");
197 if (old_pos < content->position()) {
198 note += String::compose(
199 _("The file %1 has been moved %2 milliseconds later."),
200 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
203 note += String::compose(
204 _("The file %1 has been moved %2 milliseconds earlier."),
205 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
208 notes.push_back (note);
211 /* ...or have a start trim which is an integer number of frames */
212 ContentTime const old_trim = content->trim_start();
213 content->set_trim_start(old_trim);
214 if (old_trim != content->trim_start()) {
215 string note = _("Your project contains video content whose trim was not aligned to a frame boundary.");
217 if (old_trim < content->trim_start()) {
218 note += String::compose(
219 _("The file %1 has been trimmed by %2 milliseconds more."),
220 content->path_summary(), ContentTime(content->trim_start() - old_trim).seconds() * 1000
223 note += String::compose(
224 _("The file %1 has been trimmed by %2 milliseconds less."),
225 content->path_summary(), ContentTime(old_trim - content->trim_start()).seconds() * 1000
228 notes.push_back (note);
231 _content.push_back (content);
234 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
235 sort (_content.begin(), _content.end(), ContentSorter ());
240 /** @param node <Playlist> node.
241 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
244 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
246 BOOST_FOREACH (shared_ptr<Content> i, _content) {
247 i->as_xml (node->add_child ("Content"), with_content_paths);
252 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
254 Change (CHANGE_TYPE_PENDING);
255 _content.push_back (c);
256 sort (_content.begin(), _content.end(), ContentSorter ());
258 Change (CHANGE_TYPE_DONE);
262 Playlist::remove (shared_ptr<Content> c)
264 Change (CHANGE_TYPE_PENDING);
266 ContentList::iterator i = _content.begin ();
267 while (i != _content.end() && *i != c) {
271 if (i != _content.end ()) {
273 Change (CHANGE_TYPE_DONE);
275 Change (CHANGE_TYPE_CANCELLED);
278 /* This won't change order, so it does not need a sort */
282 Playlist::remove (ContentList c)
284 Change (CHANGE_TYPE_PENDING);
286 BOOST_FOREACH (shared_ptr<Content> i, c) {
287 ContentList::iterator j = _content.begin ();
288 while (j != _content.end() && *j != i) {
292 if (j != _content.end ()) {
297 /* This won't change order, so it does not need a sort */
299 Change (CHANGE_TYPE_DONE);
302 class FrameRateCandidate
305 FrameRateCandidate (float source_, int dcp_)
315 Playlist::best_video_frame_rate () const
317 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
319 /* Work out what rates we could manage, including those achieved by using skip / repeat */
320 list<FrameRateCandidate> candidates;
322 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
323 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
324 candidates.push_back (FrameRateCandidate (*i, *i));
327 /* Then the skip/repeat ones */
328 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
329 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
330 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
333 /* Pick the best one */
334 float error = std::numeric_limits<float>::max ();
335 optional<FrameRateCandidate> best;
336 list<FrameRateCandidate>::iterator i = candidates.begin();
337 while (i != candidates.end()) {
339 float this_error = 0;
340 BOOST_FOREACH (shared_ptr<Content> j, _content) {
341 if (!j->video || !j->video_frame_rate()) {
345 /* Best error for this content; we could use the content as-is or double its rate */
346 float best_error = min (
347 float (fabs (i->source - j->video_frame_rate().get())),
348 float (fabs (i->source - j->video_frame_rate().get() * 2))
351 /* Use the largest difference between DCP and source as the "error" */
352 this_error = max (this_error, best_error);
355 if (this_error < error) {
370 /** @return length of the playlist from time 0 to the last thing on the playlist */
372 Playlist::length (shared_ptr<const Film> film) const
375 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
376 len = max (len, i->end(film));
382 /** @return position of the first thing on the playlist, if it's not empty */
384 Playlist::start () const
386 if (_content.empty ()) {
387 return optional<DCPTime> ();
390 DCPTime start = DCPTime::max ();
391 BOOST_FOREACH (shared_ptr<Content> i, _content) {
392 start = min (start, i->position ());
399 Playlist::disconnect ()
401 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
405 _content_connections.clear ();
409 Playlist::reconnect (shared_ptr<const Film> film)
413 BOOST_FOREACH (shared_ptr<Content> i, _content) {
414 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
419 Playlist::video_end (shared_ptr<const Film> film) const
422 BOOST_FOREACH (shared_ptr<Content> i, _content) {
424 end = max (end, i->end(film));
432 Playlist::text_end (shared_ptr<const Film> film) const
435 BOOST_FOREACH (shared_ptr<Content> i, _content) {
436 if (!i->text.empty ()) {
437 end = max (end, i->end(film));
445 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
447 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
452 if ((*i)->position() <= t) {
453 /* This is the first piece of content (going backwards...) that starts before t,
454 so it's the active one.
456 if ((*i)->video_frame_rate ()) {
457 /* This content specified a rate, so use it */
458 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
460 /* No specified rate so just use the DCP one */
461 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
466 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
470 Playlist::set_sequence (bool s)
476 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
478 if (a->position() != b->position()) {
479 return a->position() < b->position();
482 /* Put video before audio if they start at the same time */
483 if (a->video && !b->video) {
485 } else if (!a->video && b->video) {
490 return a->digest() < b->digest();
493 /** @return content in ascending order of position */
495 Playlist::content () const
501 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
503 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
504 BOOST_FOREACH (shared_ptr<Content> i, c) {
505 range.first = min (range.first, i->position ());
506 range.second = max (range.second, i->position ());
507 range.first = min (range.first, i->end(film));
508 range.second = max (range.second, i->end(film));
511 Change (CHANGE_TYPE_PENDING);
513 DCPTime pos = range.second;
514 for (int i = 0; i < n; ++i) {
515 BOOST_FOREACH (shared_ptr<Content> j, c) {
516 shared_ptr<Content> copy = j->clone ();
517 copy->set_position (film, pos + copy->position() - range.first);
518 _content.push_back (copy);
520 pos += range.second - range.first;
523 sort (_content.begin(), _content.end(), ContentSorter ());
526 Change (CHANGE_TYPE_DONE);
530 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
532 ContentList::iterator previous = _content.end ();
533 ContentList::iterator i = _content.begin();
534 while (i != _content.end() && *i != c) {
539 DCPOMATIC_ASSERT (i != _content.end ());
540 if (previous == _content.end ()) {
544 shared_ptr<Content> previous_c = *previous;
546 DCPTime const p = previous_c->position ();
547 previous_c->set_position (film, p + c->length_after_trim(film));
548 c->set_position (film, p);
552 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
554 ContentList::iterator i = _content.begin();
555 while (i != _content.end() && *i != c) {
559 DCPOMATIC_ASSERT (i != _content.end ());
561 ContentList::iterator next = i;
564 if (next == _content.end ()) {
568 shared_ptr<Content> next_c = *next;
570 next_c->set_position (film, c->position());
571 c->set_position (film, c->position() + next_c->length_after_trim(film));
575 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
577 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
578 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
580 BOOST_FOREACH (shared_ptr<Content> i, _content) {
581 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
583 if (d->reference_video()) {
584 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
586 if (d->reference_audio()) {
587 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
592 /* Add on 64k for bits and pieces (metadata, subs etc) */
593 return video + audio + 65536;
597 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
601 BOOST_FOREACH (shared_ptr<Content> i, _content) {
603 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
605 score += 100 * o.get().duration().get() / period.duration().get();
612 if (score > best_score) {
613 best_summary = i->path(0).leaf().string();
622 Playlist::speed_up_range (int dcp_video_frame_rate) const
624 pair<double, double> range (DBL_MAX, -DBL_MAX);
626 BOOST_FOREACH (shared_ptr<Content> i, _content) {
630 if (i->video_frame_rate()) {
631 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
632 range.first = min (range.first, frc.speed_up);
633 range.second = max (range.second, frc.speed_up);
635 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
636 range.first = min (range.first, frc.speed_up);
637 range.second = max (range.second, frc.speed_up);