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 /* Don't respond to position changes here, as:
70 - sequencing after earlier/later changes is handled by move_earlier/move_later
71 - any other position changes will be timeline drags which should not result in content
75 if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
76 maybe_sequence_video ();
79 ContentChanged (content, property, frequent);
83 Playlist::maybe_sequence_video ()
85 if (!_sequence_video || _sequencing_video) {
89 _sequencing_video = true;
93 BOOST_FOREACH (shared_ptr<Content> i, _content) {
94 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
99 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
100 vc->set_position (next_right);
101 next_right = vc->end();
103 vc->set_position (next_left);
104 next_left = vc->end();
108 /* This won't change order, so it does not need a sort */
110 _sequencing_video = false;
114 Playlist::video_identifier () const
118 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
119 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (i);
120 shared_ptr<const SubtitleContent> sc = dynamic_pointer_cast<const SubtitleContent> (i);
122 t += vc->identifier ();
123 } else if (sc && sc->burn_subtitles ()) {
124 t += sc->identifier ();
128 MD5Digester digester;
129 digester.add (t.c_str(), t.length());
130 return digester.get ();
133 /** @param node <Playlist> node */
135 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
137 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
138 _content.push_back (content_factory (film, i, version, notes));
141 sort (_content.begin(), _content.end(), ContentSorter ());
146 /** @param node <Playlist> node */
148 Playlist::as_xml (xmlpp::Node* node)
150 BOOST_FOREACH (shared_ptr<Content> i, _content) {
151 i->as_xml (node->add_child ("Content"));
156 Playlist::add (shared_ptr<Content> c)
158 _content.push_back (c);
159 sort (_content.begin(), _content.end(), ContentSorter ());
165 Playlist::remove (shared_ptr<Content> c)
167 ContentList::iterator i = _content.begin ();
168 while (i != _content.end() && *i != c) {
172 if (i != _content.end ()) {
177 /* This won't change order, so it does not need a sort */
181 Playlist::remove (ContentList c)
183 BOOST_FOREACH (shared_ptr<Content> i, c) {
184 ContentList::iterator j = _content.begin ();
185 while (j != _content.end() && *j != i) {
189 if (j != _content.end ()) {
194 /* This won't change order, so it does not need a sort */
199 class FrameRateCandidate
202 FrameRateCandidate (float source_, int dcp_)
212 Playlist::best_dcp_frame_rate () const
214 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
216 /* Work out what rates we could manage, including those achieved by using skip / repeat */
217 list<FrameRateCandidate> candidates;
219 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
220 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
221 candidates.push_back (FrameRateCandidate (*i, *i));
224 /* Then the skip/repeat ones */
225 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
226 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
227 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
230 /* Pick the best one */
231 float error = std::numeric_limits<float>::max ();
232 optional<FrameRateCandidate> best;
233 list<FrameRateCandidate>::iterator i = candidates.begin();
234 while (i != candidates.end()) {
236 float this_error = 0;
237 BOOST_FOREACH (shared_ptr<Content> j, _content) {
238 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j);
239 if (!vc || !vc->has_own_video_frame_rate()) {
243 /* Best error for this content; we could use the content as-is or double its rate */
244 float best_error = min (
245 float (fabs (i->source - vc->video_frame_rate ())),
246 float (fabs (i->source - vc->video_frame_rate () * 2))
249 /* Use the largest difference between DCP and source as the "error" */
250 this_error = max (this_error, best_error);
253 if (this_error < error) {
268 /** @return length of the playlist from time 0 to the last thing on the playlist */
270 Playlist::length () const
273 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
274 len = max (len, i->end());
280 /** @return position of the first thing on the playlist, if it's not empty */
282 Playlist::start () const
284 if (_content.empty ()) {
285 return optional<DCPTime> ();
288 DCPTime start = DCPTime::max ();
289 BOOST_FOREACH (shared_ptr<Content> i, _content) {
290 start = min (start, i->position ());
297 Playlist::reconnect ()
299 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
303 _content_connections.clear ();
305 BOOST_FOREACH (shared_ptr<Content> i, _content) {
306 _content_connections.push_back (i->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
311 Playlist::video_end () const
314 BOOST_FOREACH (shared_ptr<Content> i, _content) {
315 if (dynamic_pointer_cast<const VideoContent> (i)) {
316 end = max (end, i->end ());
324 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
326 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
327 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
332 if (vc->position() <= t) {
333 /* This is the first piece of content (going backwards...) that starts before t,
334 so it's the active one.
336 return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
340 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
344 Playlist::set_sequence_video (bool s)
350 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
352 return a->position() < b->position();
355 /** @return content in ascending order of position */
357 Playlist::content () const
363 Playlist::repeat (ContentList c, int n)
365 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
366 BOOST_FOREACH (shared_ptr<Content> i, c) {
367 range.first = min (range.first, i->position ());
368 range.second = max (range.second, i->position ());
369 range.first = min (range.first, i->end ());
370 range.second = max (range.second, i->end ());
373 DCPTime pos = range.second;
374 for (int i = 0; i < n; ++i) {
375 BOOST_FOREACH (shared_ptr<Content> j, c) {
376 shared_ptr<Content> copy = j->clone ();
377 copy->set_position (pos + copy->position() - range.first);
378 _content.push_back (copy);
380 pos += range.second - range.first;
383 sort (_content.begin(), _content.end(), ContentSorter ());
390 Playlist::move_earlier (shared_ptr<Content> c)
392 sort (_content.begin(), _content.end(), ContentSorter ());
394 ContentList::iterator previous = _content.end ();
395 ContentList::iterator i = _content.begin();
396 while (i != _content.end() && *i != c) {
401 DCPOMATIC_ASSERT (i != _content.end ());
402 if (previous == _content.end ()) {
407 DCPTime const p = (*previous)->position ();
408 (*previous)->set_position (p + c->length_after_trim ());
410 sort (_content.begin(), _content.end(), ContentSorter ());
414 Playlist::move_later (shared_ptr<Content> c)
416 sort (_content.begin(), _content.end(), ContentSorter ());
418 ContentList::iterator i = _content.begin();
419 while (i != _content.end() && *i != c) {
423 DCPOMATIC_ASSERT (i != _content.end ());
425 ContentList::iterator next = i;
428 if (next == _content.end ()) {
432 (*next)->set_position (c->position ());
433 c->set_position (c->position() + (*next)->length_after_trim ());
434 sort (_content.begin(), _content.end(), ContentSorter ());
438 Playlist::required_disk_space (int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
440 int64_t video = uint64_t (j2k_bandwidth / 8) * length().seconds ();
441 int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length().seconds ();
443 BOOST_FOREACH (shared_ptr<Content> i, _content) {
444 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
446 if (d->reference_video()) {
447 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim().seconds();
449 if (d->reference_audio()) {
450 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim().seconds();
455 /* Add on 64k for bits and pieces (metadata, subs etc) */
456 return video + audio + 65536;