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"
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 MD5Digester digester;
117 digester.add (t.c_str(), t.length());
118 return digester.get ();
121 /** @param node <Playlist> node */
123 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node, int version, list<string>& notes)
125 list<cxml::NodePtr> c = node->node_children ("Content");
126 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
127 _content.push_back (content_factory (film, *i, version, notes));
130 sort (_content.begin(), _content.end(), ContentSorter ());
135 /** @param node <Playlist> node */
137 Playlist::as_xml (xmlpp::Node* node)
139 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
140 (*i)->as_xml (node->add_child ("Content"));
145 Playlist::add (shared_ptr<Content> c)
147 _content.push_back (c);
148 sort (_content.begin(), _content.end(), ContentSorter ());
154 Playlist::remove (shared_ptr<Content> c)
156 ContentList::iterator i = _content.begin ();
157 while (i != _content.end() && *i != c) {
161 if (i != _content.end ()) {
166 /* This won't change order, so it does not need a sort */
170 Playlist::remove (ContentList c)
172 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
173 ContentList::iterator j = _content.begin ();
174 while (j != _content.end() && *j != *i) {
178 if (j != _content.end ()) {
183 /* This won't change order, so it does not need a sort */
189 Playlist::has_subtitles () const
191 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
192 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
193 if (fc && !fc->subtitle_streams().empty()) {
201 class FrameRateCandidate
204 FrameRateCandidate (float source_, int dcp_)
214 Playlist::best_dcp_frame_rate () const
216 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
218 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
219 list<FrameRateCandidate> candidates;
221 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated 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 (*i, *i));
226 /* Then the skip/repeat ones */
227 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
228 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
229 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
232 /* Pick the best one */
233 float error = std::numeric_limits<float>::max ();
234 optional<FrameRateCandidate> best;
235 list<FrameRateCandidate>::iterator i = candidates.begin();
236 while (i != candidates.end()) {
238 float this_error = 0;
239 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
240 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
245 /* Use the largest difference between DCP and source as the "error" */
246 this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
249 if (this_error < error) {
265 Playlist::length () const
268 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
269 len = max (len, (*i)->end() + 1);
276 Playlist::reconnect ()
278 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
282 _content_connections.clear ();
284 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
285 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
290 Playlist::video_end () const
293 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
294 if (dynamic_pointer_cast<const VideoContent> (*i)) {
295 end = max (end, (*i)->end ());
303 Playlist::set_sequence_video (bool s)
309 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
311 return a->position() < b->position();
314 /** @return content in an undefined order */
316 Playlist::content () const
322 Playlist::repeat (ContentList c, int n)
324 pair<Time, Time> range (TIME_MAX, 0);
325 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
326 range.first = min (range.first, (*i)->position ());
327 range.second = max (range.second, (*i)->position ());
328 range.first = min (range.first, (*i)->end ());
329 range.second = max (range.second, (*i)->end ());
332 Time pos = range.second;
333 for (int i = 0; i < n; ++i) {
334 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
335 shared_ptr<Content> copy = (*i)->clone ();
336 copy->set_position (pos + copy->position() - range.first);
337 _content.push_back (copy);
339 pos += range.second - range.first;
342 sort (_content.begin(), _content.end(), ContentSorter ());
349 Playlist::move_earlier (shared_ptr<Content> c)
351 sort (_content.begin(), _content.end(), ContentSorter ());
353 ContentList::iterator previous = _content.end ();
354 ContentList::iterator i = _content.begin();
355 while (i != _content.end() && *i != c) {
360 assert (i != _content.end ());
361 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 ());
373 Playlist::move_later (shared_ptr<Content> c)
375 sort (_content.begin(), _content.end(), ContentSorter ());
377 ContentList::iterator i = _content.begin();
378 while (i != _content.end() && *i != c) {
382 assert (i != _content.end ());
384 ContentList::iterator next = i;
387 if (next == _content.end ()) {
391 (*next)->set_position (c->position ());
392 c->set_position (c->position() + c->length_after_trim ());
393 sort (_content.begin(), _content.end(), ContentSorter ());
397 Playlist::active_frame_rate_change (Time t, int dcp_video_frame_rate) const
399 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
400 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
405 if (vc->position() >= t && t < vc->end()) {
406 return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
410 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);