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) {
103 /* Keep track of the content that we've set the position of so that we don't
112 BOOST_FOREACH (shared_ptr<Content> i, _content) {
113 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
118 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
119 vc->set_position (next_right);
120 next_right = vc->end();
122 vc->set_position (next_left);
123 next_left = vc->end();
126 placed.push_back (vc);
132 BOOST_FOREACH (shared_ptr<Content> i, _content) {
133 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (i);
134 if (!sc || !sc->has_subtitles() || find (placed.begin(), placed.end(), i) != placed.end()) {
138 sc->set_position (next);
143 /* This won't change order, so it does not need a sort */
149 Playlist::video_identifier () const
153 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
154 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (i);
155 shared_ptr<const SubtitleContent> sc = dynamic_pointer_cast<const SubtitleContent> (i);
157 t += vc->identifier ();
158 } else if (sc && sc->burn_subtitles ()) {
159 t += sc->identifier ();
163 MD5Digester digester;
164 digester.add (t.c_str(), t.length());
165 return digester.get ();
168 /** @param node <Playlist> node */
170 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
172 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
173 _content.push_back (content_factory (film, i, version, notes));
176 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
177 sort (_content.begin(), _content.end(), ContentSorter ());
182 /** @param node <Playlist> node */
184 Playlist::as_xml (xmlpp::Node* node)
186 BOOST_FOREACH (shared_ptr<Content> i, _content) {
187 i->as_xml (node->add_child ("Content"));
192 Playlist::add (shared_ptr<Content> c)
194 _content.push_back (c);
195 sort (_content.begin(), _content.end(), ContentSorter ());
201 Playlist::remove (shared_ptr<Content> c)
203 ContentList::iterator i = _content.begin ();
204 while (i != _content.end() && *i != c) {
208 if (i != _content.end ()) {
213 /* This won't change order, so it does not need a sort */
217 Playlist::remove (ContentList c)
219 BOOST_FOREACH (shared_ptr<Content> i, c) {
220 ContentList::iterator j = _content.begin ();
221 while (j != _content.end() && *j != i) {
225 if (j != _content.end ()) {
230 /* This won't change order, so it does not need a sort */
235 class FrameRateCandidate
238 FrameRateCandidate (float source_, int dcp_)
248 Playlist::best_dcp_frame_rate () const
250 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
252 /* Work out what rates we could manage, including those achieved by using skip / repeat */
253 list<FrameRateCandidate> candidates;
255 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
256 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
257 candidates.push_back (FrameRateCandidate (*i, *i));
260 /* Then the skip/repeat ones */
261 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
262 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
263 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
266 /* Pick the best one */
267 float error = std::numeric_limits<float>::max ();
268 optional<FrameRateCandidate> best;
269 list<FrameRateCandidate>::iterator i = candidates.begin();
270 while (i != candidates.end()) {
272 float this_error = 0;
273 BOOST_FOREACH (shared_ptr<Content> j, _content) {
274 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j);
275 if (!vc || !vc->has_own_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 - vc->video_frame_rate ())),
282 float (fabs (i->source - vc->video_frame_rate () * 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) {
351 if (dynamic_pointer_cast<const VideoContent> (i)) {
352 end = max (end, i->end ());
360 Playlist::subtitle_end () const
363 BOOST_FOREACH (shared_ptr<Content> i, _content) {
364 if (dynamic_pointer_cast<const SubtitleContent> (i)) {
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) {
376 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
381 if (vc->position() <= t) {
382 /* This is the first piece of content (going backwards...) that starts before t,
383 so it's the active one.
385 return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
389 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
393 Playlist::set_sequence (bool s)
399 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
401 if (a->position() != b->position()) {
402 return a->position() < b->position();
405 /* Put video before audio if they start at the same time */
406 if (dynamic_pointer_cast<VideoContent>(a) && !dynamic_pointer_cast<VideoContent>(b)) {
408 } else if (!dynamic_pointer_cast<VideoContent>(a) && dynamic_pointer_cast<VideoContent>(b)) {
413 return a->digest() < b->digest();
416 /** @return content in ascending order of position */
418 Playlist::content () const
424 Playlist::repeat (ContentList c, int n)
426 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
427 BOOST_FOREACH (shared_ptr<Content> i, c) {
428 range.first = min (range.first, i->position ());
429 range.second = max (range.second, i->position ());
430 range.first = min (range.first, i->end ());
431 range.second = max (range.second, i->end ());
434 DCPTime pos = range.second;
435 for (int i = 0; i < n; ++i) {
436 BOOST_FOREACH (shared_ptr<Content> j, c) {
437 shared_ptr<Content> copy = j->clone ();
438 copy->set_position (pos + copy->position() - range.first);
439 _content.push_back (copy);
441 pos += range.second - range.first;
444 sort (_content.begin(), _content.end(), ContentSorter ());
451 Playlist::move_earlier (shared_ptr<Content> c)
453 ContentList::iterator previous = _content.end ();
454 ContentList::iterator i = _content.begin();
455 while (i != _content.end() && *i != c) {
460 DCPOMATIC_ASSERT (i != _content.end ());
461 if (previous == _content.end ()) {
465 shared_ptr<Content> previous_c = *previous;
467 DCPTime const p = previous_c->position ();
468 previous_c->set_position (p + c->length_after_trim ());
473 Playlist::move_later (shared_ptr<Content> c)
475 ContentList::iterator i = _content.begin();
476 while (i != _content.end() && *i != c) {
480 DCPOMATIC_ASSERT (i != _content.end ());
482 ContentList::iterator next = i;
485 if (next == _content.end ()) {
489 shared_ptr<Content> next_c = *next;
491 next_c->set_position (c->position ());
492 c->set_position (c->position() + next_c->length_after_trim ());
496 Playlist::required_disk_space (int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
498 int64_t video = uint64_t (j2k_bandwidth / 8) * length().seconds ();
499 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length().seconds ();
501 BOOST_FOREACH (shared_ptr<Content> i, _content) {
502 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
504 if (d->reference_video()) {
505 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim().seconds();
507 if (d->reference_audio()) {
508 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim().seconds();
513 /* Add on 64k for bits and pieces (metadata, subs etc) */
514 return video + audio + 65536;