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;
54 : _sequence_video (true)
55 , _sequencing_video (false)
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
75 maybe_sequence_video ();
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_video ()
97 if (!_sequence_video || _sequencing_video) {
101 _sequencing_video = true;
105 BOOST_FOREACH (shared_ptr<Content> i, _content) {
106 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
111 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
112 vc->set_position (next_right);
113 next_right = vc->end();
115 vc->set_position (next_left);
116 next_left = vc->end();
120 /* This won't change order, so it does not need a sort */
122 _sequencing_video = false;
126 Playlist::video_identifier () const
130 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
131 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (i);
132 shared_ptr<const SubtitleContent> sc = dynamic_pointer_cast<const SubtitleContent> (i);
134 t += vc->identifier ();
135 } else if (sc && sc->burn_subtitles ()) {
136 t += sc->identifier ();
140 MD5Digester digester;
141 digester.add (t.c_str(), t.length());
142 return digester.get ();
145 /** @param node <Playlist> node */
147 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
149 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
150 _content.push_back (content_factory (film, i, version, notes));
153 /* This shouldn't be necessary but better safe than sorry (there could be old files) */
154 sort (_content.begin(), _content.end(), ContentSorter ());
159 /** @param node <Playlist> node */
161 Playlist::as_xml (xmlpp::Node* node)
163 BOOST_FOREACH (shared_ptr<Content> i, _content) {
164 i->as_xml (node->add_child ("Content"));
169 Playlist::add (shared_ptr<Content> c)
171 _content.push_back (c);
172 sort (_content.begin(), _content.end(), ContentSorter ());
178 Playlist::remove (shared_ptr<Content> c)
180 ContentList::iterator i = _content.begin ();
181 while (i != _content.end() && *i != c) {
185 if (i != _content.end ()) {
190 /* This won't change order, so it does not need a sort */
194 Playlist::remove (ContentList c)
196 BOOST_FOREACH (shared_ptr<Content> i, c) {
197 ContentList::iterator j = _content.begin ();
198 while (j != _content.end() && *j != i) {
202 if (j != _content.end ()) {
207 /* This won't change order, so it does not need a sort */
212 class FrameRateCandidate
215 FrameRateCandidate (float source_, int dcp_)
225 Playlist::best_dcp_frame_rate () const
227 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
229 /* Work out what rates we could manage, including those achieved by using skip / repeat */
230 list<FrameRateCandidate> candidates;
232 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
233 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
234 candidates.push_back (FrameRateCandidate (*i, *i));
237 /* Then the skip/repeat ones */
238 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
239 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
240 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
243 /* Pick the best one */
244 float error = std::numeric_limits<float>::max ();
245 optional<FrameRateCandidate> best;
246 list<FrameRateCandidate>::iterator i = candidates.begin();
247 while (i != candidates.end()) {
249 float this_error = 0;
250 BOOST_FOREACH (shared_ptr<Content> j, _content) {
251 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j);
252 if (!vc || !vc->has_own_video_frame_rate()) {
256 /* Best error for this content; we could use the content as-is or double its rate */
257 float best_error = min (
258 float (fabs (i->source - vc->video_frame_rate ())),
259 float (fabs (i->source - vc->video_frame_rate () * 2))
262 /* Use the largest difference between DCP and source as the "error" */
263 this_error = max (this_error, best_error);
266 if (this_error < error) {
281 /** @return length of the playlist from time 0 to the last thing on the playlist */
283 Playlist::length () const
286 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
287 len = max (len, i->end());
293 /** @return position of the first thing on the playlist, if it's not empty */
295 Playlist::start () const
297 if (_content.empty ()) {
298 return optional<DCPTime> ();
301 DCPTime start = DCPTime::max ();
302 BOOST_FOREACH (shared_ptr<Content> i, _content) {
303 start = min (start, i->position ());
310 Playlist::reconnect ()
312 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
316 _content_connections.clear ();
318 BOOST_FOREACH (shared_ptr<Content> i, _content) {
319 _content_connections.push_back (i->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
324 Playlist::video_end () const
327 BOOST_FOREACH (shared_ptr<Content> i, _content) {
328 if (dynamic_pointer_cast<const VideoContent> (i)) {
329 end = max (end, i->end ());
337 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
339 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
340 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
345 if (vc->position() <= t) {
346 /* This is the first piece of content (going backwards...) that starts before t,
347 so it's the active one.
349 return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
353 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
357 Playlist::set_sequence_video (bool s)
363 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
365 if (a->position() != b->position()) {
366 return a->position() < b->position();
369 /* Put video before audio if they start at the same time */
370 if (dynamic_pointer_cast<VideoContent>(a) && !dynamic_pointer_cast<VideoContent>(b)) {
372 } else if (!dynamic_pointer_cast<VideoContent>(a) && dynamic_pointer_cast<VideoContent>(b)) {
377 return a->digest() < b->digest();
380 /** @return content in ascending order of position */
382 Playlist::content () const
388 Playlist::repeat (ContentList c, int n)
390 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
391 BOOST_FOREACH (shared_ptr<Content> i, c) {
392 range.first = min (range.first, i->position ());
393 range.second = max (range.second, i->position ());
394 range.first = min (range.first, i->end ());
395 range.second = max (range.second, i->end ());
398 DCPTime pos = range.second;
399 for (int i = 0; i < n; ++i) {
400 BOOST_FOREACH (shared_ptr<Content> j, c) {
401 shared_ptr<Content> copy = j->clone ();
402 copy->set_position (pos + copy->position() - range.first);
403 _content.push_back (copy);
405 pos += range.second - range.first;
408 sort (_content.begin(), _content.end(), ContentSorter ());
415 Playlist::move_earlier (shared_ptr<Content> c)
417 ContentList::iterator previous = _content.end ();
418 ContentList::iterator i = _content.begin();
419 while (i != _content.end() && *i != c) {
424 DCPOMATIC_ASSERT (i != _content.end ());
425 if (previous == _content.end ()) {
430 DCPTime const p = (*previous)->position ();
431 (*previous)->set_position (p + c->length_after_trim ());
436 Playlist::move_later (shared_ptr<Content> c)
438 ContentList::iterator i = _content.begin();
439 while (i != _content.end() && *i != c) {
443 DCPOMATIC_ASSERT (i != _content.end ());
445 ContentList::iterator next = i;
448 if (next == _content.end ()) {
452 (*next)->set_position (c->position ());
453 c->set_position (c->position() + (*next)->length_after_trim ());
457 Playlist::required_disk_space (int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
459 int64_t video = uint64_t (j2k_bandwidth / 8) * length().seconds ();
460 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length().seconds ();
462 BOOST_FOREACH (shared_ptr<Content> i, _content) {
463 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
465 if (d->reference_video()) {
466 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim().seconds();
468 if (d->reference_audio()) {
469 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim().seconds();
474 /* Add on 64k for bits and pieces (metadata, subs etc) */
475 return video + audio + 65536;