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>
45 using boost::optional;
46 using boost::shared_ptr;
47 using boost::weak_ptr;
48 using boost::dynamic_pointer_cast;
51 : _sequence_video (true)
52 , _sequencing_video (false)
57 Playlist::~Playlist ()
64 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
66 /* Don't respond to position changes here, as:
67 - sequencing after earlier/later changes is handled by move_earlier/move_later
68 - any other position changes will be timeline drags which should not result in content
72 if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
73 maybe_sequence_video ();
76 ContentChanged (content, property, frequent);
80 Playlist::maybe_sequence_video ()
82 if (!_sequence_video || _sequencing_video) {
86 _sequencing_video = true;
90 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
91 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
96 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
97 vc->set_position (next_right);
98 next_right = vc->end() + DCPTime::delta ();
100 vc->set_position (next_left);
101 next_left = vc->end() + DCPTime::delta ();
105 /* This won't change order, so it does not need a sort */
107 _sequencing_video = false;
111 Playlist::video_identifier () const
115 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
116 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
117 shared_ptr<const SubtitleContent> sc = dynamic_pointer_cast<const SubtitleContent> (*i);
119 t += vc->identifier ();
120 } else if (sc && sc->burn_subtitles ()) {
121 t += sc->identifier ();
125 MD5Digester digester;
126 digester.add (t.c_str(), t.length());
127 return digester.get ();
130 /** @param node <Playlist> node */
132 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
134 list<cxml::NodePtr> c = node->node_children ("Content");
135 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
136 _content.push_back (content_factory (film, *i, version, notes));
139 sort (_content.begin(), _content.end(), ContentSorter ());
144 /** @param node <Playlist> node */
146 Playlist::as_xml (xmlpp::Node* node)
148 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
149 (*i)->as_xml (node->add_child ("Content"));
154 Playlist::add (shared_ptr<Content> c)
156 _content.push_back (c);
157 sort (_content.begin(), _content.end(), ContentSorter ());
163 Playlist::remove (shared_ptr<Content> c)
165 ContentList::iterator i = _content.begin ();
166 while (i != _content.end() && *i != c) {
170 if (i != _content.end ()) {
175 /* This won't change order, so it does not need a sort */
179 Playlist::remove (ContentList c)
181 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
182 ContentList::iterator j = _content.begin ();
183 while (j != _content.end() && *j != *i) {
187 if (j != _content.end ()) {
192 /* This won't change order, so it does not need a sort */
197 class FrameRateCandidate
200 FrameRateCandidate (float source_, int dcp_)
210 Playlist::best_dcp_frame_rate () const
212 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
214 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
215 list<FrameRateCandidate> candidates;
217 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
218 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
219 candidates.push_back (FrameRateCandidate (*i, *i));
222 /* Then the skip/repeat ones */
223 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
224 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
225 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
228 /* Pick the best one */
229 float error = std::numeric_limits<float>::max ();
230 optional<FrameRateCandidate> best;
231 list<FrameRateCandidate>::iterator i = candidates.begin();
232 while (i != candidates.end()) {
234 float this_error = 0;
235 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
236 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
241 /* Best error for this content; we could use the content as-is or double its rate */
242 float best_error = min (
243 float (fabs (i->source - vc->video_frame_rate ())),
244 float (fabs (i->source - vc->video_frame_rate () * 2))
247 /* Use the largest difference between DCP and source as the "error" */
248 this_error = max (this_error, best_error);
251 if (this_error < error) {
267 Playlist::length () const
270 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
271 len = max (len, (*i)->end());
278 Playlist::reconnect ()
280 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
284 _content_connections.clear ();
286 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
287 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
292 Playlist::video_end () const
295 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
296 if (dynamic_pointer_cast<const VideoContent> (*i)) {
297 end = max (end, (*i)->end ());
305 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
307 for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
308 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
313 if (vc->position() <= t) {
314 /* This is the first piece of content (going backwards...) that starts before t,
315 so it's the active one.
317 return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
321 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
325 Playlist::set_sequence_video (bool s)
331 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
333 return a->position() < b->position();
336 /** @return content in an undefined order */
338 Playlist::content () const
344 Playlist::repeat (ContentList c, int n)
346 pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
347 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
348 range.first = min (range.first, (*i)->position ());
349 range.second = max (range.second, (*i)->position ());
350 range.first = min (range.first, (*i)->end ());
351 range.second = max (range.second, (*i)->end ());
354 DCPTime pos = range.second;
355 for (int i = 0; i < n; ++i) {
356 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
357 shared_ptr<Content> copy = (*i)->clone ();
358 copy->set_position (pos + copy->position() - range.first);
359 _content.push_back (copy);
361 pos += range.second - range.first;
364 sort (_content.begin(), _content.end(), ContentSorter ());
371 Playlist::move_earlier (shared_ptr<Content> c)
373 sort (_content.begin(), _content.end(), ContentSorter ());
375 ContentList::iterator previous = _content.end ();
376 ContentList::iterator i = _content.begin();
377 while (i != _content.end() && *i != c) {
382 DCPOMATIC_ASSERT (i != _content.end ());
383 if (previous == _content.end ()) {
388 DCPTime const p = (*previous)->position ();
389 (*previous)->set_position (p + c->length_after_trim ());
391 sort (_content.begin(), _content.end(), ContentSorter ());
395 Playlist::move_later (shared_ptr<Content> c)
397 sort (_content.begin(), _content.end(), ContentSorter ());
399 ContentList::iterator i = _content.begin();
400 while (i != _content.end() && *i != c) {
404 DCPOMATIC_ASSERT (i != _content.end ());
406 ContentList::iterator next = i;
409 if (next == _content.end ()) {
413 (*next)->set_position (c->position ());
414 c->set_position (c->position() + (*next)->length_after_trim ());
415 sort (_content.begin(), _content.end(), ContentSorter ());