2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "sndfile_content.h"
22 #include "sndfile_decoder.h"
23 #include "video_content.h"
24 #include "ffmpeg_decoder.h"
25 #include "ffmpeg_content.h"
26 #include "image_decoder.h"
27 #include "content_factory.h"
28 #include "dcp_content.h"
32 #include "md5_digester.h"
33 #include <libcxml/cxml.h>
34 #include <libxml++/libxml++.h>
35 #include <boost/shared_ptr.hpp>
36 #include <boost/foreach.hpp>
48 using boost::optional;
49 using boost::shared_ptr;
50 using boost::weak_ptr;
51 using boost::dynamic_pointer_cast;
60 Playlist::~Playlist ()
67 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
69 if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
70 /* Don't respond to position changes here, as:
71 - sequencing after earlier/later changes is handled by move_earlier/move_later
72 - any other position changes will be timeline drags which should not result in content
79 property == ContentProperty::POSITION ||
80 property == ContentProperty::LENGTH ||
81 property == ContentProperty::TRIM_START ||
82 property == ContentProperty::TRIM_END) {
84 ContentList old = _content;
85 sort (_content.begin(), _content.end(), ContentSorter ());
86 if (_content != old) {
91 ContentChanged (content, property, frequent);
95 Playlist::maybe_sequence ()
97 if (!_sequence || _sequencing) {
107 BOOST_FOREACH (shared_ptr<Content> i, _content) {
108 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
113 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
114 vc->set_position (next_right);
115 next_right = vc->end();
117 vc->set_position (next_left);
118 next_left = vc->end();
125 BOOST_FOREACH (shared_ptr<Content> i, _content) {
126 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (i);
131 sc->set_position (next);
136 /* This won't change order, so it does not need a sort */
142 Playlist::video_identifier () const
146 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
147 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (i);
148 shared_ptr<const SubtitleContent> sc = dynamic_pointer_cast<const SubtitleContent> (i);
150 t += vc->identifier ();
151 } else if (sc && sc->burn_subtitles ()) {
152 t += sc->identifier ();
156 MD5Digester digester;
157 digester.add (t.c_str(), t.length());
158 return digester.get ();
161 /** @param node <Playlist> node */
163 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
165 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
166 _content.push_back (content_factory (film, i, version, notes));
169 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
170 sort (_content.begin(), _content.end(), ContentSorter ());
175 /** @param node <Playlist> node */
177 Playlist::as_xml (xmlpp::Node* node)
179 BOOST_FOREACH (shared_ptr<Content> i, _content) {
180 i->as_xml (node->add_child ("Content"));
185 Playlist::add (shared_ptr<Content> c)
187 _content.push_back (c);
188 sort (_content.begin(), _content.end(), ContentSorter ());
194 Playlist::remove (shared_ptr<Content> c)
196 ContentList::iterator i = _content.begin ();
197 while (i != _content.end() && *i != c) {
201 if (i != _content.end ()) {
206 /* This won't change order, so it does not need a sort */
210 Playlist::remove (ContentList c)
212 BOOST_FOREACH (shared_ptr<Content> i, c) {
213 ContentList::iterator j = _content.begin ();
214 while (j != _content.end() && *j != i) {
218 if (j != _content.end ()) {
223 /* This won't change order, so it does not need a sort */
228 class FrameRateCandidate
231 FrameRateCandidate (float source_, int dcp_)
241 Playlist::best_dcp_frame_rate () const
243 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
245 /* Work out what rates we could manage, including those achieved by using skip / repeat */
246 list<FrameRateCandidate> candidates;
248 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
249 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
250 candidates.push_back (FrameRateCandidate (*i, *i));
253 /* Then the skip/repeat ones */
254 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
255 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
256 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
259 /* Pick the best one */
260 float error = std::numeric_limits<float>::max ();
261 optional<FrameRateCandidate> best;
262 list<FrameRateCandidate>::iterator i = candidates.begin();
263 while (i != candidates.end()) {
265 float this_error = 0;
266 BOOST_FOREACH (shared_ptr<Content> j, _content) {
267 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j);
268 if (!vc || !vc->has_own_video_frame_rate()) {
272 /* Best error for this content; we could use the content as-is or double its rate */
273 float best_error = min (
274 float (fabs (i->source - vc->video_frame_rate ())),
275 float (fabs (i->source - vc->video_frame_rate () * 2))
278 /* Use the largest difference between DCP and source as the "error" */
279 this_error = max (this_error, best_error);
282 if (this_error < error) {
297 /** @return length of the playlist from time 0 to the last thing on the playlist */
299 Playlist::length () const
302 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
303 len = max (len, i->end());
309 /** @return position of the first thing on the playlist, if it's not empty */
311 Playlist::start () const
313 if (_content.empty ()) {
314 return optional<DCPTime> ();
317 DCPTime start = DCPTime::max ();
318 BOOST_FOREACH (shared_ptr<Content> i, _content) {
319 start = min (start, i->position ());
326 Playlist::reconnect ()
328 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
332 _content_connections.clear ();
334 BOOST_FOREACH (shared_ptr<Content> i, _content) {
335 _content_connections.push_back (i->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
340 Playlist::video_end () const
343 BOOST_FOREACH (shared_ptr<Content> i, _content) {
344 if (dynamic_pointer_cast<const VideoContent> (i)) {
345 end = max (end, i->end ());
353 Playlist::subtitle_end () const
356 BOOST_FOREACH (shared_ptr<Content> i, _content) {
357 if (dynamic_pointer_cast<const SubtitleContent> (i)) {
358 end = max (end, i->end ());
366 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
368 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
369 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
374 if (vc->position() <= t) {
375 /* This is the first piece of content (going backwards...) that starts before t,
376 so it's the active one.
378 return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
382 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
386 Playlist::set_sequence (bool s)
392 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
394 if (a->position() != b->position()) {
395 return a->position() < b->position();
398 /* Put video before audio if they start at the same time */
399 if (dynamic_pointer_cast<VideoContent>(a) && !dynamic_pointer_cast<VideoContent>(b)) {
401 } else if (!dynamic_pointer_cast<VideoContent>(a) && dynamic_pointer_cast<VideoContent>(b)) {
406 return a->digest() < b->digest();
409 /** @return content in ascending order of position */
411 Playlist::content () const
417 Playlist::repeat (ContentList c, int n)
419 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
420 BOOST_FOREACH (shared_ptr<Content> i, c) {
421 range.first = min (range.first, i->position ());
422 range.second = max (range.second, i->position ());
423 range.first = min (range.first, i->end ());
424 range.second = max (range.second, i->end ());
427 DCPTime pos = range.second;
428 for (int i = 0; i < n; ++i) {
429 BOOST_FOREACH (shared_ptr<Content> j, c) {
430 shared_ptr<Content> copy = j->clone ();
431 copy->set_position (pos + copy->position() - range.first);
432 _content.push_back (copy);
434 pos += range.second - range.first;
437 sort (_content.begin(), _content.end(), ContentSorter ());
444 Playlist::move_earlier (shared_ptr<Content> c)
446 ContentList::iterator previous = _content.end ();
447 ContentList::iterator i = _content.begin();
448 while (i != _content.end() && *i != c) {
453 DCPOMATIC_ASSERT (i != _content.end ());
454 if (previous == _content.end ()) {
458 shared_ptr<Content> previous_c = *previous;
460 DCPTime const p = previous_c->position ();
461 previous_c->set_position (p + c->length_after_trim ());
466 Playlist::move_later (shared_ptr<Content> c)
468 ContentList::iterator i = _content.begin();
469 while (i != _content.end() && *i != c) {
473 DCPOMATIC_ASSERT (i != _content.end ());
475 ContentList::iterator next = i;
478 if (next == _content.end ()) {
482 shared_ptr<Content> next_c = *next;
484 next_c->set_position (c->position ());
485 c->set_position (c->position() + next_c->length_after_trim ());
489 Playlist::required_disk_space (int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
491 int64_t video = uint64_t (j2k_bandwidth / 8) * length().seconds ();
492 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length().seconds ();
494 BOOST_FOREACH (shared_ptr<Content> i, _content) {
495 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
497 if (d->reference_video()) {
498 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim().seconds();
500 if (d->reference_audio()) {
501 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim().seconds();
506 /* Add on 64k for bits and pieces (metadata, subs etc) */
507 return video + audio + 65536;