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 && _sequence_video && !_sequencing_video) {
76 cout << "sequencing.\n";
77 _sequencing_video = true;
79 ContentList cl = _content;
80 sort (cl.begin(), cl.end(), ContentSorter ());
82 for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
83 if (!dynamic_pointer_cast<VideoContent> (*i)) {
87 (*i)->set_start (last);
88 cout << (*i)->file() << " -> " << last << "\n";
92 _sequencing_video = false;
95 ContentChanged (c, p);
99 Playlist::audio_digest () const
103 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
104 if (!dynamic_pointer_cast<const AudioContent> (*i)) {
108 t += (*i)->digest ();
110 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
112 t += lexical_cast<string> (fc->audio_stream()->id);
116 t += lexical_cast<string> (_loop);
118 return md5_digest (t.c_str(), t.length());
122 Playlist::video_digest () const
126 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
127 if (!dynamic_pointer_cast<const VideoContent> (*i)) {
131 t += (*i)->digest ();
132 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
133 if (fc && fc->subtitle_stream()) {
134 t += fc->subtitle_stream()->id;
138 t += lexical_cast<string> (_loop);
140 return md5_digest (t.c_str(), t.length());
143 /** @param node <Playlist> node */
145 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
147 list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
148 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
149 string const type = (*i)->string_child ("Type");
151 boost::shared_ptr<Content> content;
153 if (type == "FFmpeg") {
154 content.reset (new FFmpegContent (film, *i));
155 } else if (type == "ImageMagick") {
156 content.reset (new ImageMagickContent (film, *i));
157 } else if (type == "Sndfile") {
158 content.reset (new SndfileContent (film, *i));
161 _content.push_back (content);
165 _loop = node->number_child<int> ("Loop");
166 _sequence_video = node->bool_child ("SequenceVideo");
169 /** @param node <Playlist> node */
171 Playlist::as_xml (xmlpp::Node* node)
173 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
174 (*i)->as_xml (node->add_child ("Content"));
177 node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
178 node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
182 Playlist::add (shared_ptr<Content> c)
184 _content.push_back (c);
190 Playlist::remove (shared_ptr<Content> c)
192 ContentList::iterator i = _content.begin ();
193 while (i != _content.end() && *i != c) {
197 if (i != _content.end ()) {
204 Playlist::set_loop (int l)
211 Playlist::has_subtitles () const
213 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
214 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
215 if (fc && !fc->subtitle_streams().empty()) {
223 class FrameRateCandidate
226 FrameRateCandidate (float source_, int dcp_)
236 Playlist::best_dcp_frame_rate () const
238 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
240 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
241 list<FrameRateCandidate> candidates;
243 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
244 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
245 candidates.push_back (FrameRateCandidate (*i, *i));
248 /* Then the skip/repeat ones */
249 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
250 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
251 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
254 /* Pick the best one, bailing early if we hit an exact match */
255 float error = std::numeric_limits<float>::max ();
256 optional<FrameRateCandidate> best;
257 list<FrameRateCandidate>::iterator i = candidates.begin();
258 while (i != candidates.end()) {
260 float this_error = std::numeric_limits<float>::max ();
261 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
262 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
267 this_error += fabs (i->source - vc->video_frame_rate ());
270 if (this_error < error) {
286 Playlist::length () const
289 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
290 len = max (len, (*i)->end ());
297 Playlist::reconnect ()
299 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
303 _content_connections.clear ();
305 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
306 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
311 Playlist::video_end () const
314 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
315 if (dynamic_pointer_cast<const VideoContent> (*i)) {
316 end = max (end, (*i)->end ());
324 Playlist::set_sequence_video (bool s)
330 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
332 return a->start() < b->start();