2 Copyright (C) 2013 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"
42 using std::stringstream;
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 if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
66 maybe_sequence_video ();
69 ContentChanged (content, property, frequent);
73 Playlist::maybe_sequence_video ()
75 if (!_sequence_video || _sequencing_video) {
79 _sequencing_video = true;
81 ContentList cl = _content;
84 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
85 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
90 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
91 vc->set_position (next_right);
92 next_right = vc->end() + 1;
94 vc->set_position (next_left);
95 next_left = vc->end() + 1;
99 /* This won't change order, so it does not need a sort */
101 _sequencing_video = false;
105 Playlist::video_identifier () const
109 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
110 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
112 t += vc->identifier ();
116 return md5_digest (t.c_str(), t.length());
119 /** @param node <Playlist> node */
121 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node, int version, list<string>& notes)
123 list<cxml::NodePtr> c = node->node_children ("Content");
124 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
125 _content.push_back (content_factory (film, *i, version, notes));
128 sort (_content.begin(), _content.end(), ContentSorter ());
133 /** @param node <Playlist> node */
135 Playlist::as_xml (xmlpp::Node* node)
137 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
138 (*i)->as_xml (node->add_child ("Content"));
143 Playlist::add (shared_ptr<Content> c)
145 _content.push_back (c);
146 sort (_content.begin(), _content.end(), ContentSorter ());
152 Playlist::remove (shared_ptr<Content> c)
154 ContentList::iterator i = _content.begin ();
155 while (i != _content.end() && *i != c) {
159 if (i != _content.end ()) {
164 /* This won't change order, so it does not need a sort */
168 Playlist::remove (ContentList c)
170 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
171 ContentList::iterator j = _content.begin ();
172 while (j != _content.end() && *j != *i) {
176 if (j != _content.end ()) {
181 /* This won't change order, so it does not need a sort */
187 Playlist::has_subtitles () const
189 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
190 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
191 if (fc && !fc->subtitle_streams().empty()) {
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 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
238 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
243 /* Use the largest difference between DCP and source as the "error" */
244 this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
247 if (this_error < error) {
263 Playlist::length () const
266 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
267 len = max (len, (*i)->end() + 1);
274 Playlist::reconnect ()
276 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
280 _content_connections.clear ();
282 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
283 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
288 Playlist::video_end () const
291 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
292 if (dynamic_pointer_cast<const VideoContent> (*i)) {
293 end = max (end, (*i)->end ());
301 Playlist::set_sequence_video (bool s)
307 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
309 return a->position() < b->position();
312 /** @return content in an undefined order */
314 Playlist::content () const
320 Playlist::repeat (ContentList c, int n)
322 pair<Time, Time> range (TIME_MAX, 0);
323 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
324 range.first = min (range.first, (*i)->position ());
325 range.second = max (range.second, (*i)->position ());
326 range.first = min (range.first, (*i)->end ());
327 range.second = max (range.second, (*i)->end ());
330 Time pos = range.second;
331 for (int i = 0; i < n; ++i) {
332 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
333 shared_ptr<Content> copy = (*i)->clone ();
334 copy->set_position (pos + copy->position() - range.first);
335 _content.push_back (copy);
337 pos += range.second - range.first;
340 sort (_content.begin(), _content.end(), ContentSorter ());
347 Playlist::move_earlier (shared_ptr<Content> c)
349 sort (_content.begin(), _content.end(), ContentSorter ());
351 ContentList::iterator previous = _content.end ();
352 ContentList::iterator i = _content.begin();
353 while (i != _content.end() && *i != c) {
358 assert (i != _content.end ());
359 if (previous == _content.end ()) {
363 Time const p = (*previous)->position ();
364 (*previous)->set_position (p + c->length_after_trim ());
366 sort (_content.begin(), _content.end(), ContentSorter ());
372 Playlist::move_later (shared_ptr<Content> c)
374 sort (_content.begin(), _content.end(), ContentSorter ());
376 ContentList::iterator i = _content.begin();
377 while (i != _content.end() && *i != c) {
381 assert (i != _content.end ());
383 ContentList::iterator next = i;
386 if (next == _content.end ()) {
390 Time const p = (*next)->position ();
391 (*next)->set_position (c->position ());
392 c->set_position (p + c->length_after_trim ());
393 sort (_content.begin(), _content.end(), ContentSorter ());