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 <libcxml/cxml.h>
35 #include <libxml++/libxml++.h>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/foreach.hpp>
49 using boost::optional;
50 using boost::shared_ptr;
51 using boost::weak_ptr;
52 using boost::dynamic_pointer_cast;
61 Playlist::~Playlist ()
68 Playlist::content_change (weak_ptr<const Film> weak_film, ChangeType type, weak_ptr<Content> content, int property, bool frequent)
70 shared_ptr<const Film> film = weak_film.lock ();
71 DCPOMATIC_ASSERT (film);
73 if (type == CHANGE_TYPE_DONE) {
75 property == ContentProperty::TRIM_START ||
76 property == ContentProperty::TRIM_END ||
77 property == ContentProperty::LENGTH ||
78 property == VideoContentProperty::FRAME_TYPE
80 /* Don't respond to position changes here, as:
81 - sequencing after earlier/later changes is handled by move_earlier/move_later
82 - any other position changes will be timeline drags which should not result in content
85 maybe_sequence (film);
89 property == ContentProperty::POSITION ||
90 property == ContentProperty::LENGTH ||
91 property == ContentProperty::TRIM_START ||
92 property == ContentProperty::TRIM_END) {
94 ContentList old = _content;
95 sort (_content.begin(), _content.end(), ContentSorter ());
96 if (_content != old) {
102 ContentChange (type, content, property, frequent);
106 Playlist::maybe_sequence (shared_ptr<const Film> film)
108 if (!_sequence || _sequencing) {
114 /* Keep track of the content that we've set the position of so that we don't
123 BOOST_FOREACH (shared_ptr<Content> i, _content) {
128 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
129 i->set_position (film, next_right);
130 next_right = i->end(film);
132 i->set_position (film, next_left);
133 next_left = i->end(film);
136 placed.push_back (i);
142 BOOST_FOREACH (shared_ptr<Content> i, _content) {
143 if (i->text.empty() || find (placed.begin(), placed.end(), i) != placed.end()) {
147 i->set_position (film, next);
152 /* This won't change order, so it does not need a sort */
158 Playlist::video_identifier () const
162 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
164 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
169 if (i->video || burn) {
170 t += i->identifier ();
175 digester.add (t.c_str(), t.length());
176 return digester.get ();
179 /** @param film Film that this Playlist is for.
180 * @param node <Playlist> node.
181 * @param version Metadata version number.
182 * @param notes Output notes about that happened.
185 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
187 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
188 _content.push_back (content_factory (film, i, version, notes));
191 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
192 sort (_content.begin(), _content.end(), ContentSorter ());
197 /** @param node <Playlist> node.
198 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
201 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
203 BOOST_FOREACH (shared_ptr<Content> i, _content) {
204 i->as_xml (node->add_child ("Content"), with_content_paths);
209 Playlist::add (shared_ptr<const Film> film, shared_ptr<Content> c)
211 Change (CHANGE_TYPE_PENDING);
212 _content.push_back (c);
213 sort (_content.begin(), _content.end(), ContentSorter ());
215 Change (CHANGE_TYPE_DONE);
219 Playlist::remove (shared_ptr<Content> c)
221 Change (CHANGE_TYPE_PENDING);
223 ContentList::iterator i = _content.begin ();
224 while (i != _content.end() && *i != c) {
228 if (i != _content.end ()) {
230 Change (CHANGE_TYPE_DONE);
232 Change (CHANGE_TYPE_CANCELLED);
235 /* This won't change order, so it does not need a sort */
239 Playlist::remove (ContentList c)
241 Change (CHANGE_TYPE_PENDING);
243 BOOST_FOREACH (shared_ptr<Content> i, c) {
244 ContentList::iterator j = _content.begin ();
245 while (j != _content.end() && *j != i) {
249 if (j != _content.end ()) {
254 /* This won't change order, so it does not need a sort */
256 Change (CHANGE_TYPE_DONE);
259 class FrameRateCandidate
262 FrameRateCandidate (float source_, int dcp_)
272 Playlist::best_video_frame_rate () const
274 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
276 /* Work out what rates we could manage, including those achieved by using skip / repeat */
277 list<FrameRateCandidate> candidates;
279 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
280 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
281 candidates.push_back (FrameRateCandidate (*i, *i));
284 /* Then the skip/repeat ones */
285 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
286 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
287 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
290 /* Pick the best one */
291 float error = std::numeric_limits<float>::max ();
292 optional<FrameRateCandidate> best;
293 list<FrameRateCandidate>::iterator i = candidates.begin();
294 while (i != candidates.end()) {
296 float this_error = 0;
297 BOOST_FOREACH (shared_ptr<Content> j, _content) {
298 if (!j->video || !j->video_frame_rate()) {
302 /* Best error for this content; we could use the content as-is or double its rate */
303 float best_error = min (
304 float (fabs (i->source - j->video_frame_rate().get())),
305 float (fabs (i->source - j->video_frame_rate().get() * 2))
308 /* Use the largest difference between DCP and source as the "error" */
309 this_error = max (this_error, best_error);
312 if (this_error < error) {
327 /** @return length of the playlist from time 0 to the last thing on the playlist */
329 Playlist::length (shared_ptr<const Film> film) const
332 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
333 len = max (len, i->end(film));
339 /** @return position of the first thing on the playlist, if it's not empty */
341 Playlist::start () const
343 if (_content.empty ()) {
344 return optional<DCPTime> ();
347 DCPTime start = DCPTime::max ();
348 BOOST_FOREACH (shared_ptr<Content> i, _content) {
349 start = min (start, i->position ());
356 Playlist::disconnect ()
358 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
362 _content_connections.clear ();
366 Playlist::reconnect (shared_ptr<const Film> film)
370 BOOST_FOREACH (shared_ptr<Content> i, _content) {
371 _content_connections.push_back (i->Change.connect(boost::bind(&Playlist::content_change, this, film, _1, _2, _3, _4)));
376 Playlist::video_end (shared_ptr<const Film> film) const
379 BOOST_FOREACH (shared_ptr<Content> i, _content) {
381 end = max (end, i->end(film));
389 Playlist::text_end (shared_ptr<const Film> film) const
392 BOOST_FOREACH (shared_ptr<Content> i, _content) {
393 if (!i->text.empty ()) {
394 end = max (end, i->end(film));
402 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
404 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
409 if ((*i)->position() <= t) {
410 /* This is the first piece of content (going backwards...) that starts before t,
411 so it's the active one.
413 if ((*i)->video_frame_rate ()) {
414 /* This content specified a rate, so use it */
415 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
417 /* No specified rate so just use the DCP one */
418 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
423 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
427 Playlist::set_sequence (bool s)
433 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
435 if (a->position() != b->position()) {
436 return a->position() < b->position();
439 /* Put video before audio if they start at the same time */
440 if (a->video && !b->video) {
442 } else if (!a->video && b->video) {
447 return a->digest() < b->digest();
450 /** @return content in ascending order of position */
452 Playlist::content () const
458 Playlist::repeat (shared_ptr<const Film> film, ContentList c, int n)
460 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
461 BOOST_FOREACH (shared_ptr<Content> i, c) {
462 range.first = min (range.first, i->position ());
463 range.second = max (range.second, i->position ());
464 range.first = min (range.first, i->end(film));
465 range.second = max (range.second, i->end(film));
468 Change (CHANGE_TYPE_PENDING);
470 DCPTime pos = range.second;
471 for (int i = 0; i < n; ++i) {
472 BOOST_FOREACH (shared_ptr<Content> j, c) {
473 shared_ptr<Content> copy = j->clone (film);
474 copy->set_position (film, pos + copy->position() - range.first);
475 _content.push_back (copy);
477 pos += range.second - range.first;
480 sort (_content.begin(), _content.end(), ContentSorter ());
483 Change (CHANGE_TYPE_DONE);
487 Playlist::move_earlier (shared_ptr<const Film> film, shared_ptr<Content> c)
489 ContentList::iterator previous = _content.end ();
490 ContentList::iterator i = _content.begin();
491 while (i != _content.end() && *i != c) {
496 DCPOMATIC_ASSERT (i != _content.end ());
497 if (previous == _content.end ()) {
501 shared_ptr<Content> previous_c = *previous;
503 DCPTime const p = previous_c->position ();
504 previous_c->set_position (film, p + c->length_after_trim(film));
505 c->set_position (film, p);
509 Playlist::move_later (shared_ptr<const Film> film, shared_ptr<Content> c)
511 ContentList::iterator i = _content.begin();
512 while (i != _content.end() && *i != c) {
516 DCPOMATIC_ASSERT (i != _content.end ());
518 ContentList::iterator next = i;
521 if (next == _content.end ()) {
525 shared_ptr<Content> next_c = *next;
527 next_c->set_position (film, c->position());
528 c->set_position (film, c->position() + next_c->length_after_trim(film));
532 Playlist::required_disk_space (shared_ptr<const Film> film, int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
534 int64_t video = uint64_t (j2k_bandwidth / 8) * length(film).seconds();
535 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length(film).seconds();
537 BOOST_FOREACH (shared_ptr<Content> i, _content) {
538 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
540 if (d->reference_video()) {
541 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim(film).seconds();
543 if (d->reference_audio()) {
544 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim(film).seconds();
549 /* Add on 64k for bits and pieces (metadata, subs etc) */
550 return video + audio + 65536;
554 Playlist::content_summary (shared_ptr<const Film> film, DCPTimePeriod period) const
558 BOOST_FOREACH (shared_ptr<Content> i, _content) {
560 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end(film)).overlap (period);
562 score += 100 * o.get().duration().get() / period.duration().get();
569 if (score > best_score) {
570 best_summary = i->path(0).leaf().string();
579 Playlist::speed_up_range (int dcp_video_frame_rate) const
581 pair<double, double> range (DBL_MAX, -DBL_MAX);
583 BOOST_FOREACH (shared_ptr<Content> i, _content) {
587 if (i->video_frame_rate()) {
588 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
589 range.first = min (range.first, frc.speed_up);
590 range.second = max (range.second, frc.speed_up);
592 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
593 range.first = min (range.first, frc.speed_up);
594 range.second = max (range.second, frc.speed_up);