2 Copyright (C) 2013-2015 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"
31 #include "md5_digester.h"
32 #include <libcxml/cxml.h>
33 #include <libxml++/libxml++.h>
34 #include <boost/shared_ptr.hpp>
35 #include <boost/foreach.hpp>
47 using boost::optional;
48 using boost::shared_ptr;
49 using boost::weak_ptr;
50 using boost::dynamic_pointer_cast;
53 : _sequence_video (true)
54 , _sequencing_video (false)
59 Playlist::~Playlist ()
66 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
68 /* Don't respond to position changes here, as:
69 - sequencing after earlier/later changes is handled by move_earlier/move_later
70 - any other position changes will be timeline drags which should not result in content
74 if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
75 maybe_sequence_video ();
78 ContentChanged (content, property, frequent);
82 Playlist::maybe_sequence_video ()
84 if (!_sequence_video || _sequencing_video) {
88 _sequencing_video = true;
92 BOOST_FOREACH (shared_ptr<Content> i, _content) {
93 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
98 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
99 vc->set_position (next_right);
100 next_right = vc->end() + DCPTime::delta ();
102 vc->set_position (next_left);
103 next_left = vc->end() + DCPTime::delta ();
107 /* This won't change order, so it does not need a sort */
109 _sequencing_video = false;
113 Playlist::video_identifier () const
117 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
118 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (i);
119 shared_ptr<const SubtitleContent> sc = dynamic_pointer_cast<const SubtitleContent> (i);
121 t += vc->identifier ();
122 } else if (sc && sc->burn_subtitles ()) {
123 t += sc->identifier ();
127 MD5Digester digester;
128 digester.add (t.c_str(), t.length());
129 return digester.get ();
132 /** @param node <Playlist> node */
134 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
136 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
137 _content.push_back (content_factory (film, i, version, notes));
140 sort (_content.begin(), _content.end(), ContentSorter ());
145 /** @param node <Playlist> node */
147 Playlist::as_xml (xmlpp::Node* node)
149 BOOST_FOREACH (shared_ptr<Content> i, _content) {
150 i->as_xml (node->add_child ("Content"));
155 Playlist::add (shared_ptr<Content> c)
157 _content.push_back (c);
158 sort (_content.begin(), _content.end(), ContentSorter ());
164 Playlist::remove (shared_ptr<Content> c)
166 ContentList::iterator i = _content.begin ();
167 while (i != _content.end() && *i != c) {
171 if (i != _content.end ()) {
176 /* This won't change order, so it does not need a sort */
180 Playlist::remove (ContentList c)
182 BOOST_FOREACH (shared_ptr<Content> i, c) {
183 ContentList::iterator j = _content.begin ();
184 while (j != _content.end() && *j != i) {
188 if (j != _content.end ()) {
193 /* This won't change order, so it does not need a sort */
198 class FrameRateCandidate
201 FrameRateCandidate (float source_, int dcp_)
211 Playlist::best_dcp_frame_rate () const
213 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
215 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
216 list<FrameRateCandidate> candidates;
218 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
219 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
220 candidates.push_back (FrameRateCandidate (*i, *i));
223 /* Then the skip/repeat ones */
224 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
225 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
226 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
229 /* Pick the best one */
230 float error = std::numeric_limits<float>::max ();
231 optional<FrameRateCandidate> best;
232 list<FrameRateCandidate>::iterator i = candidates.begin();
233 while (i != candidates.end()) {
235 float this_error = 0;
236 BOOST_FOREACH (shared_ptr<Content> j, _content) {
237 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j);
242 /* Best error for this content; we could use the content as-is or double its rate */
243 float best_error = min (
244 float (fabs (i->source - vc->video_frame_rate ())),
245 float (fabs (i->source - vc->video_frame_rate () * 2))
248 /* Use the largest difference between DCP and source as the "error" */
249 this_error = max (this_error, best_error);
252 if (this_error < error) {
267 /** @return length of the playlist from time 0 to the last thing on the playlist */
269 Playlist::length () const
272 BOOST_FOREACH (shared_ptr<const Content> i, _content) {
273 len = max (len, i->end());
279 /** @return position of the first thing on the playlist, if it's not empty */
281 Playlist::start () const
283 if (_content.empty ()) {
284 return optional<DCPTime> ();
287 DCPTime start = DCPTime::max ();
288 BOOST_FOREACH (shared_ptr<Content> i, _content) {
289 start = min (start, i->position ());
296 Playlist::reconnect ()
298 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
302 _content_connections.clear ();
304 BOOST_FOREACH (shared_ptr<Content> i, _content) {
305 _content_connections.push_back (i->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
310 Playlist::video_end () const
313 BOOST_FOREACH (shared_ptr<Content> i, _content) {
314 if (dynamic_pointer_cast<const VideoContent> (i)) {
315 end = max (end, i->end ());
323 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
325 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
326 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
331 if (vc->position() <= t) {
332 /* This is the first piece of content (going backwards...) that starts before t,
333 so it's the active one.
335 return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
339 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
343 Playlist::set_sequence_video (bool s)
349 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
351 return a->position() < b->position();
354 /** @return content in an undefined order */
356 Playlist::content () const
362 Playlist::repeat (ContentList c, int n)
364 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
365 BOOST_FOREACH (shared_ptr<Content> i, c) {
366 range.first = min (range.first, i->position ());
367 range.second = max (range.second, i->position ());
368 range.first = min (range.first, i->end ());
369 range.second = max (range.second, i->end ());
372 DCPTime pos = range.second;
373 for (int i = 0; i < n; ++i) {
374 BOOST_FOREACH (shared_ptr<Content> j, c) {
375 shared_ptr<Content> copy = j->clone ();
376 copy->set_position (pos + copy->position() - range.first);
377 _content.push_back (copy);
379 pos += range.second - range.first;
382 sort (_content.begin(), _content.end(), ContentSorter ());
389 Playlist::move_earlier (shared_ptr<Content> c)
391 sort (_content.begin(), _content.end(), ContentSorter ());
393 ContentList::iterator previous = _content.end ();
394 ContentList::iterator i = _content.begin();
395 while (i != _content.end() && *i != c) {
400 DCPOMATIC_ASSERT (i != _content.end ());
401 if (previous == _content.end ()) {
406 DCPTime const p = (*previous)->position ();
407 (*previous)->set_position (p + c->length_after_trim ());
409 sort (_content.begin(), _content.end(), ContentSorter ());
413 Playlist::move_later (shared_ptr<Content> c)
415 sort (_content.begin(), _content.end(), ContentSorter ());
417 ContentList::iterator i = _content.begin();
418 while (i != _content.end() && *i != c) {
422 DCPOMATIC_ASSERT (i != _content.end ());
424 ContentList::iterator next = i;
427 if (next == _content.end ()) {
431 (*next)->set_position (c->position ());
432 c->set_position (c->position() + (*next)->length_after_trim ());
433 sort (_content.begin(), _content.end(), ContentSorter ());