2 Copyright (C) 2013-2016 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 "subtitle_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_changed (weak_ptr<Content> content, int property, bool frequent)
70 if (property == ContentProperty::LENGTH || property == VideoContentProperty::FRAME_TYPE) {
71 /* Don't respond to position changes here, as:
72 - sequencing after earlier/later changes is handled by move_earlier/move_later
73 - any other position changes will be timeline drags which should not result in content
80 property == ContentProperty::POSITION ||
81 property == ContentProperty::LENGTH ||
82 property == ContentProperty::TRIM_START ||
83 property == ContentProperty::TRIM_END) {
85 ContentList old = _content;
86 sort (_content.begin(), _content.end(), ContentSorter ());
87 if (_content != old) {
92 ContentChanged (content, property, frequent);
96 Playlist::maybe_sequence ()
98 if (!_sequence || _sequencing) {
104 /* Keep track of the content that we've set the position of so that we don't
113 BOOST_FOREACH (shared_ptr<Content> i, _content) {
118 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
119 i->set_position (next_right);
120 next_right = i->end();
122 i->set_position (next_left);
123 next_left = i->end();
126 placed.push_back (i);
132 BOOST_FOREACH (shared_ptr<Content> i, _content) {
133 if (!i->subtitle || find (placed.begin(), placed.end(), i) != placed.end()) {
137 i->set_position (next);
142 /* This won't change order, so it does not need a sort */
148 Playlist::video_identifier () const
152 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
153 if (i->video || (i->subtitle && i->subtitle->burn())) {
154 t += i->identifier ();
159 digester.add (t.c_str(), t.length());
160 return digester.get ();
163 /** @param film Film that this Playlist is for.
164 * @param node <Playlist> node.
165 * @param version Metadata version number.
166 * @param notes Output notes about that happened.
169 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
171 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
172 _content.push_back (content_factory (film, i, version, notes));
175 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
176 sort (_content.begin(), _content.end(), ContentSorter ());
181 /** @param node <Playlist> node.
182 * @param with_content_paths true to include <Path> nodes in <Content> nodes, false to omit them.
185 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
187 BOOST_FOREACH (shared_ptr<Content> i, _content) {
188 i->as_xml (node->add_child ("Content"), with_content_paths);
193 Playlist::add (shared_ptr<Content> c)
195 _content.push_back (c);
196 sort (_content.begin(), _content.end(), ContentSorter ());
202 Playlist::remove (shared_ptr<Content> c)
204 ContentList::iterator i = _content.begin ();
205 while (i != _content.end() && *i != c) {
209 if (i != _content.end ()) {
214 /* This won't change order, so it does not need a sort */
218 Playlist::remove (ContentList c)
220 BOOST_FOREACH (shared_ptr<Content> i, c) {
221 ContentList::iterator j = _content.begin ();
222 while (j != _content.end() && *j != i) {
226 if (j != _content.end ()) {
231 /* This won't change order, so it does not need a sort */
236 class FrameRateCandidate
239 FrameRateCandidate (float source_, int dcp_)
249 Playlist::best_video_frame_rate () const
251 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
253 /* Work out what rates we could manage, including those achieved by using skip / repeat */
254 list<FrameRateCandidate> candidates;
256 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
257 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
258 candidates.push_back (FrameRateCandidate (*i, *i));
261 /* Then the skip/repeat ones */
262 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
263 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
264 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
267 /* Pick the best one */
268 float error = std::numeric_limits<float>::max ();
269 optional<FrameRateCandidate> best;
270 list<FrameRateCandidate>::iterator i = candidates.begin();
271 while (i != candidates.end()) {
273 float this_error = 0;
274 BOOST_FOREACH (shared_ptr<Content> j, _content) {
275 if (!j->video || !j->video_frame_rate()) {
279 /* Best error for this content; we could use the content as-is or double its rate */
280 float best_error = min (
281 float (fabs (i->source - j->video_frame_rate().get())),
282 float (fabs (i->source - j->video_frame_rate().get() * 2))
285 /* Use the largest difference between DCP and source as the "error" */
286 this_error = max (this_error, best_error);
289 if (this_error < error) {
304 /** @return length of the playlist from time 0 to the last thing on the playlist */
306 Playlist::length () const
309 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
310 len = max (len, i->end());
316 /** @return position of the first thing on the playlist, if it's not empty */
318 Playlist::start () const
320 if (_content.empty ()) {
321 return optional<DCPTime> ();
324 DCPTime start = DCPTime::max ();
325 BOOST_FOREACH (shared_ptr<Content> i, _content) {
326 start = min (start, i->position ());
333 Playlist::reconnect ()
335 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
339 _content_connections.clear ();
341 BOOST_FOREACH (shared_ptr<Content> i, _content) {
342 _content_connections.push_back (i->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
347 Playlist::video_end () const
350 BOOST_FOREACH (shared_ptr<Content> i, _content) {
352 end = max (end, i->end ());
360 Playlist::subtitle_end () const
363 BOOST_FOREACH (shared_ptr<Content> i, _content) {
365 end = max (end, i->end ());
373 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
375 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
380 if ((*i)->position() <= t) {
381 /* This is the first piece of content (going backwards...) that starts before t,
382 so it's the active one.
384 if ((*i)->video_frame_rate ()) {
385 /* This content specified a rate, so use it */
386 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
388 /* No specified rate so just use the DCP one */
389 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
394 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
398 Playlist::set_sequence (bool s)
404 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
406 if (a->position() != b->position()) {
407 return a->position() < b->position();
410 /* Put video before audio if they start at the same time */
411 if (a->video && !b->video) {
413 } else if (!a->video && b->video) {
418 return a->digest() < b->digest();
421 /** @return content in ascending order of position */
423 Playlist::content () const
429 Playlist::repeat (ContentList c, int n)
431 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
432 BOOST_FOREACH (shared_ptr<Content> i, c) {
433 range.first = min (range.first, i->position ());
434 range.second = max (range.second, i->position ());
435 range.first = min (range.first, i->end ());
436 range.second = max (range.second, i->end ());
439 DCPTime pos = range.second;
440 for (int i = 0; i < n; ++i) {
441 BOOST_FOREACH (shared_ptr<Content> j, c) {
442 shared_ptr<Content> copy = j->clone ();
443 copy->set_position (pos + copy->position() - range.first);
444 _content.push_back (copy);
446 pos += range.second - range.first;
449 sort (_content.begin(), _content.end(), ContentSorter ());
456 Playlist::move_earlier (shared_ptr<Content> c)
458 ContentList::iterator previous = _content.end ();
459 ContentList::iterator i = _content.begin();
460 while (i != _content.end() && *i != c) {
465 DCPOMATIC_ASSERT (i != _content.end ());
466 if (previous == _content.end ()) {
470 shared_ptr<Content> previous_c = *previous;
472 DCPTime const p = previous_c->position ();
473 previous_c->set_position (p + c->length_after_trim ());
478 Playlist::move_later (shared_ptr<Content> c)
480 ContentList::iterator i = _content.begin();
481 while (i != _content.end() && *i != c) {
485 DCPOMATIC_ASSERT (i != _content.end ());
487 ContentList::iterator next = i;
490 if (next == _content.end ()) {
494 shared_ptr<Content> next_c = *next;
496 next_c->set_position (c->position ());
497 c->set_position (c->position() + next_c->length_after_trim ());
501 Playlist::required_disk_space (int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
503 int64_t video = uint64_t (j2k_bandwidth / 8) * length().seconds ();
504 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length().seconds ();
506 BOOST_FOREACH (shared_ptr<Content> i, _content) {
507 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
509 if (d->reference_video()) {
510 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim().seconds();
512 if (d->reference_audio()) {
513 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim().seconds();
518 /* Add on 64k for bits and pieces (metadata, subs etc) */
519 return video + audio + 65536;
523 Playlist::content_summary (DCPTimePeriod period) const
527 BOOST_FOREACH (shared_ptr<Content> i, _content) {
529 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end()).overlap (period);
531 score += 100 * o.get().duration().get() / period.duration().get();
538 if (score > best_score) {
539 best_summary = i->path(0).leaf().string();
548 Playlist::video_content_at (DCPTime time) const
550 BOOST_FOREACH (shared_ptr<Content> i, _content) {
551 if (i->video && i->position() <= time && time < i->end()) {
560 Playlist::audio_content_at (DCPTime time) const
562 BOOST_FOREACH (shared_ptr<Content> i, _content) {
566 if (i->position() <= time && time < i->end()) {
575 Playlist::next_audio_content (DCPTime time) const
577 shared_ptr<Content> next;
578 DCPTime next_position;
579 BOOST_FOREACH (shared_ptr<Content> i, _content) {
583 if (i->position() >= time && (!next || i->position() < next_position)) {
585 next_position = i->position();
593 Playlist::speed_up_range (int dcp_video_frame_rate) const
595 pair<double, double> range (DBL_MAX, -DBL_MAX);
597 BOOST_FOREACH (shared_ptr<Content> i, _content) {
601 if (i->video_frame_rate()) {
602 FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
603 range.first = min (range.first, frc.speed_up);
604 range.second = max (range.second, frc.speed_up);
606 FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
607 range.first = min (range.first, frc.speed_up);
608 range.second = max (range.second, frc.speed_up);