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"
33 #include "md5_digester.h"
43 using std::stringstream;
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 if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
67 maybe_sequence_video ();
70 ContentChanged (content, property, frequent);
74 Playlist::maybe_sequence_video ()
76 if (!_sequence_video || _sequencing_video) {
80 _sequencing_video = true;
82 ContentList cl = _content;
85 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
86 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
91 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
92 vc->set_position (next_right);
93 next_right = vc->end() + 1;
95 vc->set_position (next_left);
96 next_left = vc->end() + 1;
100 /* This won't change order, so it does not need a sort */
102 _sequencing_video = false;
106 Playlist::video_identifier () const
110 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
111 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
113 t += vc->identifier ();
117 MD5Digester digester;
118 digester.add (t.c_str(), t.length());
119 return digester.get ();
122 /** @param node <Playlist> node */
124 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node, int version, list<string>& notes)
126 list<cxml::NodePtr> c = node->node_children ("Content");
127 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
128 _content.push_back (content_factory (film, *i, version, notes));
131 sort (_content.begin(), _content.end(), ContentSorter ());
136 /** @param node <Playlist> node */
138 Playlist::as_xml (xmlpp::Node* node)
140 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
141 (*i)->as_xml (node->add_child ("Content"));
146 Playlist::add (shared_ptr<Content> c)
148 _content.push_back (c);
149 sort (_content.begin(), _content.end(), ContentSorter ());
155 Playlist::remove (shared_ptr<Content> c)
157 ContentList::iterator i = _content.begin ();
158 while (i != _content.end() && *i != c) {
162 if (i != _content.end ()) {
167 /* This won't change order, so it does not need a sort */
171 Playlist::remove (ContentList c)
173 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
174 ContentList::iterator j = _content.begin ();
175 while (j != _content.end() && *j != *i) {
179 if (j != _content.end ()) {
184 /* This won't change order, so it does not need a sort */
190 Playlist::has_subtitles () const
192 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
193 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
194 if (fc && !fc->subtitle_streams().empty()) {
202 class FrameRateCandidate
205 FrameRateCandidate (float source_, int dcp_)
215 Playlist::best_dcp_frame_rate () const
217 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
219 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
220 list<FrameRateCandidate> candidates;
222 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated 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 (*i, *i));
227 /* Then the skip/repeat ones */
228 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
229 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
230 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
233 /* Pick the best one */
234 float error = std::numeric_limits<float>::max ();
235 optional<FrameRateCandidate> best;
236 list<FrameRateCandidate>::iterator i = candidates.begin();
237 while (i != candidates.end()) {
239 float this_error = 0;
240 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
241 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
246 /* Use the largest difference between DCP and source as the "error" */
247 this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
250 if (this_error < error) {
266 Playlist::length () const
269 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
270 len = max (len, (*i)->end() + 1);
277 Playlist::reconnect ()
279 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
283 _content_connections.clear ();
285 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
286 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
291 Playlist::video_end () const
294 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
295 if (dynamic_pointer_cast<const VideoContent> (*i)) {
296 end = max (end, (*i)->end ());
304 Playlist::set_sequence_video (bool s)
310 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
312 return a->position() < b->position();
315 /** @return content in an undefined order */
317 Playlist::content () const
323 Playlist::repeat (ContentList c, int n)
325 pair<Time, Time> range (TIME_MAX, 0);
326 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
327 range.first = min (range.first, (*i)->position ());
328 range.second = max (range.second, (*i)->position ());
329 range.first = min (range.first, (*i)->end ());
330 range.second = max (range.second, (*i)->end ());
333 Time pos = range.second;
334 for (int i = 0; i < n; ++i) {
335 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
336 shared_ptr<Content> copy = (*i)->clone ();
337 copy->set_position (pos + copy->position() - range.first);
338 _content.push_back (copy);
340 pos += range.second - range.first;
343 sort (_content.begin(), _content.end(), ContentSorter ());
350 Playlist::move_earlier (shared_ptr<Content> c)
352 sort (_content.begin(), _content.end(), ContentSorter ());
354 ContentList::iterator previous = _content.end ();
355 ContentList::iterator i = _content.begin();
356 while (i != _content.end() && *i != c) {
361 assert (i != _content.end ());
362 if (previous == _content.end ()) {
366 Time const p = (*previous)->position ();
367 (*previous)->set_position (p + c->length_after_trim ());
369 sort (_content.begin(), _content.end(), ContentSorter ());
375 Playlist::move_later (shared_ptr<Content> c)
377 sort (_content.begin(), _content.end(), ContentSorter ());
379 ContentList::iterator i = _content.begin();
380 while (i != _content.end() && *i != c) {
384 assert (i != _content.end ());
386 ContentList::iterator next = i;
389 if (next == _content.end ()) {
393 Time const p = (*next)->position ();
394 (*next)->set_position (c->position ());
395 c->set_position (p + c->length_after_trim ());
396 sort (_content.begin(), _content.end(), ContentSorter ());
402 Playlist::active_frame_rate_change (Time t, int dcp_video_frame_rate) const
404 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
405 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
410 if (vc->position() >= t && t < vc->end()) {
411 return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
415 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);