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.
20 #include <libcxml/cxml.h>
21 #include <boost/shared_ptr.hpp>
23 #include "sndfile_content.h"
24 #include "sndfile_decoder.h"
25 #include "video_content.h"
26 #include "ffmpeg_decoder.h"
27 #include "ffmpeg_content.h"
28 #include "image_decoder.h"
29 #include "content_factory.h"
33 #include "md5_digester.h"
44 using boost::optional;
45 using boost::shared_ptr;
46 using boost::weak_ptr;
47 using boost::dynamic_pointer_cast;
50 : _sequence_video (true)
51 , _sequencing_video (false)
56 Playlist::~Playlist ()
63 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
65 /* Don't respond to position changes here, as:
66 - sequencing after earlier/later changes is handled by move_earlier/move_later
67 - any other position changes will be timeline drags which should not result in content
71 if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
72 maybe_sequence_video ();
75 ContentChanged (content, property, frequent);
79 Playlist::maybe_sequence_video ()
81 if (!_sequence_video || _sequencing_video) {
85 _sequencing_video = true;
89 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
90 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
95 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
96 vc->set_position (next_right);
97 next_right = vc->end() + DCPTime::delta ();
99 vc->set_position (next_left);
100 next_left = vc->end() + DCPTime::delta ();
104 /* This won't change order, so it does not need a sort */
106 _sequencing_video = false;
110 Playlist::video_identifier () const
114 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
115 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
116 shared_ptr<const SubtitleContent> sc = dynamic_pointer_cast<const SubtitleContent> (*i);
118 t += vc->identifier ();
119 } else if (sc && sc->burn_subtitles ()) {
120 t += sc->identifier ();
124 MD5Digester digester;
125 digester.add (t.c_str(), t.length());
126 return digester.get ();
129 /** @param node <Playlist> node */
131 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
133 list<cxml::NodePtr> c = node->node_children ("Content");
134 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
135 _content.push_back (content_factory (film, *i, version, notes));
138 sort (_content.begin(), _content.end(), ContentSorter ());
143 /** @param node <Playlist> node */
145 Playlist::as_xml (xmlpp::Node* node)
147 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
148 (*i)->as_xml (node->add_child ("Content"));
153 Playlist::add (shared_ptr<Content> c)
155 _content.push_back (c);
156 sort (_content.begin(), _content.end(), ContentSorter ());
162 Playlist::remove (shared_ptr<Content> c)
164 ContentList::iterator i = _content.begin ();
165 while (i != _content.end() && *i != c) {
169 if (i != _content.end ()) {
174 /* This won't change order, so it does not need a sort */
178 Playlist::remove (ContentList c)
180 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
181 ContentList::iterator j = _content.begin ();
182 while (j != _content.end() && *j != *i) {
186 if (j != _content.end ()) {
191 /* This won't change order, so it does not need a sort */
196 class FrameRateCandidate
199 FrameRateCandidate (float source_, int dcp_)
209 Playlist::best_dcp_frame_rate () const
211 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
213 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
214 list<FrameRateCandidate> candidates;
216 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
217 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
218 candidates.push_back (FrameRateCandidate (*i, *i));
221 /* Then the skip/repeat ones */
222 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
223 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
224 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
227 /* Pick the best one */
228 float error = std::numeric_limits<float>::max ();
229 optional<FrameRateCandidate> best;
230 list<FrameRateCandidate>::iterator i = candidates.begin();
231 while (i != candidates.end()) {
233 float this_error = 0;
234 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
235 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
240 /* Use the largest difference between DCP and source as the "error" */
241 this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
244 if (this_error < error) {
260 Playlist::length () const
263 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
264 len = max (len, (*i)->end());
271 Playlist::reconnect ()
273 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
277 _content_connections.clear ();
279 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
280 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
285 Playlist::video_end () const
288 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
289 if (dynamic_pointer_cast<const VideoContent> (*i)) {
290 end = max (end, (*i)->end ());
298 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
300 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
301 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
306 if (vc->position() >= t && t < vc->end()) {
307 return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
311 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
315 Playlist::set_sequence_video (bool s)
321 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
323 return a->position() < b->position();
326 /** @return content in an undefined order */
328 Playlist::content () const
334 Playlist::repeat (ContentList c, int n)
336 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
337 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
338 range.first = min (range.first, (*i)->position ());
339 range.second = max (range.second, (*i)->position ());
340 range.first = min (range.first, (*i)->end ());
341 range.second = max (range.second, (*i)->end ());
344 DCPTime pos = range.second;
345 for (int i = 0; i < n; ++i) {
346 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
347 shared_ptr<Content> copy = (*i)->clone ();
348 copy->set_position (pos + copy->position() - range.first);
349 _content.push_back (copy);
351 pos += range.second - range.first;
354 sort (_content.begin(), _content.end(), ContentSorter ());
361 Playlist::move_earlier (shared_ptr<Content> c)
363 sort (_content.begin(), _content.end(), ContentSorter ());
365 ContentList::iterator previous = _content.end ();
366 ContentList::iterator i = _content.begin();
367 while (i != _content.end() && *i != c) {
372 DCPOMATIC_ASSERT (i != _content.end ());
373 if (previous == _content.end ()) {
378 DCPTime const p = (*previous)->position ();
379 (*previous)->set_position (p + c->length_after_trim ());
381 sort (_content.begin(), _content.end(), ContentSorter ());
385 Playlist::move_later (shared_ptr<Content> c)
387 sort (_content.begin(), _content.end(), ContentSorter ());
389 ContentList::iterator i = _content.begin();
390 while (i != _content.end() && *i != c) {
394 DCPOMATIC_ASSERT (i != _content.end ());
396 ContentList::iterator next = i;
399 if (next == _content.end ()) {
403 (*next)->set_position (c->position ());
404 c->set_position (c->position() + (*next)->length_after_trim ());
405 sort (_content.begin(), _content.end(), ContentSorter ());