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>
22 #include <boost/lexical_cast.hpp>
24 #include "sndfile_content.h"
25 #include "sndfile_decoder.h"
26 #include "video_content.h"
27 #include "ffmpeg_decoder.h"
28 #include "ffmpeg_content.h"
29 #include "imagemagick_decoder.h"
30 #include "imagemagick_content.h"
43 using std::stringstream;
44 using boost::optional;
45 using boost::shared_ptr;
46 using boost::weak_ptr;
47 using boost::dynamic_pointer_cast;
48 using boost::lexical_cast;
52 , _sequence_video (true)
53 , _sequencing_video (false)
58 Playlist::Playlist (shared_ptr<const Playlist> other)
59 : _loop (other->_loop)
61 for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
62 _content.push_back ((*i)->clone ());
66 Playlist::~Playlist ()
73 Playlist::content_changed (weak_ptr<Content> c, int p)
75 if (p == ContentProperty::LENGTH) {
76 maybe_sequence_video ();
80 ContentChanged (c, p);
85 ContentChanged (c, p);
88 >>>>>>> 1ea6a456bc2b4a695f6db4720353c35167597b30
90 Playlist::maybe_sequence_video ()
92 if (!_sequence_video || _sequencing_video) {
97 _sequencing_video = true;
99 ContentList cl = _content;
100 sort (cl.begin(), cl.end(), ContentSorter ());
102 for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
103 if (!dynamic_pointer_cast<VideoContent> (*i)) {
107 (*i)->set_start (last);
114 _sequencing_video = true;
116 ContentList cl = _content;
117 sort (cl.begin(), cl.end(), ContentSorter ());
119 for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
120 if (!dynamic_pointer_cast<VideoContent> (*i)) {
124 (*i)->set_start (last);
128 >>>>>>> 1ea6a456bc2b4a695f6db4720353c35167597b30
129 _sequencing_video = false;
133 Playlist::video_identifier () const
137 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
138 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
140 t += vc->identifier ();
144 t += lexical_cast<string> (_loop);
146 return md5_digest (t.c_str(), t.length());
149 /** @param node <Playlist> node */
151 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
153 list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
154 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
155 string const type = (*i)->string_child ("Type");
157 boost::shared_ptr<Content> content;
159 if (type == "FFmpeg") {
160 content.reset (new FFmpegContent (film, *i));
161 } else if (type == "ImageMagick") {
162 content.reset (new ImageMagickContent (film, *i));
163 } else if (type == "Sndfile") {
164 content.reset (new SndfileContent (film, *i));
167 _content.push_back (content);
171 _loop = node->number_child<int> ("Loop");
172 _sequence_video = node->bool_child ("SequenceVideo");
175 /** @param node <Playlist> node */
177 Playlist::as_xml (xmlpp::Node* node)
179 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
180 (*i)->as_xml (node->add_child ("Content"));
183 node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
184 node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
188 Playlist::add (shared_ptr<Content> c)
190 _content.push_back (c);
196 Playlist::remove (shared_ptr<Content> c)
198 ContentList::iterator i = _content.begin ();
199 while (i != _content.end() && *i != c) {
203 if (i != _content.end ()) {
210 Playlist::set_loop (int l)
217 Playlist::has_subtitles () const
219 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
220 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
221 if (fc && !fc->subtitle_streams().empty()) {
229 class FrameRateCandidate
232 FrameRateCandidate (float source_, int dcp_)
242 Playlist::best_dcp_frame_rate () const
244 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
246 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
247 list<FrameRateCandidate> candidates;
249 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
250 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
251 candidates.push_back (FrameRateCandidate (*i, *i));
254 /* Then the skip/repeat ones */
255 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
256 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
257 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
260 /* Pick the best one */
261 float error = std::numeric_limits<float>::max ();
262 optional<FrameRateCandidate> best;
263 list<FrameRateCandidate>::iterator i = candidates.begin();
264 while (i != candidates.end()) {
266 float this_error = 0;
267 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
268 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
273 /* Use the largest difference between DCP and source as the "error" */
274 this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
277 if (this_error < error) {
293 Playlist::length () const
296 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
297 len = max (len, (*i)->end ());
304 Playlist::reconnect ()
306 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
310 _content_connections.clear ();
312 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
313 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
318 Playlist::video_end () const
321 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
322 if (dynamic_pointer_cast<const VideoContent> (*i)) {
323 end = max (end, (*i)->end ());
331 Playlist::set_sequence_video (bool s)
337 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
339 return a->start() < b->start();