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;
56 Playlist::Playlist (shared_ptr<const Playlist> other)
57 : _loop (other->_loop)
59 for (RegionList::const_iterator i = other->_regions.begin(); i != other->_regions.end(); ++i) {
60 _regions.push_back (Region (i->content->clone(), i->time, this));
65 Playlist::content_changed (weak_ptr<Content> c, int p)
67 ContentChanged (c, p);
71 Playlist::audio_digest () const
75 for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
76 if (!dynamic_pointer_cast<const AudioContent> (i->content)) {
80 t += i->content->digest ();
82 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
84 t += lexical_cast<string> (fc->audio_stream()->id);
88 t += lexical_cast<string> (_loop);
90 return md5_digest (t.c_str(), t.length());
94 Playlist::video_digest () const
98 for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
99 if (!dynamic_pointer_cast<const VideoContent> (i->content)) {
103 t += i->content->digest ();
104 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
105 if (fc && fc->subtitle_stream()) {
106 t += fc->subtitle_stream()->id;
110 t += lexical_cast<string> (_loop);
112 return md5_digest (t.c_str(), t.length());
116 Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
118 list<shared_ptr<cxml::Node> > c = node->node_children ("Region");
119 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
120 _regions.push_back (Region (*i, this));
123 _loop = node->number_child<int> ("Loop");
127 Playlist::as_xml (xmlpp::Node* node)
129 for (RegionList::iterator i = _regions.begin(); i != _regions.end(); ++i) {
130 i->as_xml (node->add_child ("Region"));
133 node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
137 Playlist::add (shared_ptr<Content> c)
139 _regions.push_back (Region (c, 0, this));
144 Playlist::remove (shared_ptr<Content> c)
146 RegionList::iterator i = _regions.begin ();
147 while (i != _regions.end() && i->content != c) {
151 if (i != _regions.end ()) {
158 Playlist::set_loop (int l)
165 Playlist::has_subtitles () const
167 for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
168 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (i->content);
169 if (fc && !fc->subtitle_streams().empty()) {
177 Playlist::Region::Region (shared_ptr<Content> c, Time t, Playlist* p)
181 connection = c->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
184 Playlist::Region::Region (shared_ptr<const cxml::Node> node, Playlist* p)
186 shared_ptr<const cxml::Node> content_node = node->node_child ("Content");
187 string const type = content_node->string_child ("Type");
189 if (type == "FFmpeg") {
190 content.reset (new FFmpegContent (content_node));
191 } else if (type == "ImageMagick") {
192 content.reset (new ImageMagickContent (content_node));
193 } else if (type == "Sndfile") {
194 content.reset (new SndfileContent (content_node));
197 time = node->number_child<Time> ("Time");
198 connection = content->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
202 Playlist::Region::as_xml (xmlpp::Node* node) const
204 xmlpp::Node* sub = node->add_child ("Content");
205 content->as_xml (sub);
206 sub->add_child ("Time")->add_child_text (lexical_cast<string> (time));
209 class FrameRateCandidate
212 FrameRateCandidate (float source_, int dcp_)
222 Playlist::best_dcp_frame_rate () const
224 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
226 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
227 list<FrameRateCandidate> candidates;
229 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
230 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
231 candidates.push_back (FrameRateCandidate (*i, *i));
234 /* Then the skip/repeat ones */
235 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
236 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
237 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
240 /* Pick the best one, bailing early if we hit an exact match */
241 float error = std::numeric_limits<float>::max ();
242 optional<FrameRateCandidate> best;
243 list<FrameRateCandidate>::iterator i = candidates.begin();
244 while (i != candidates.end()) {
246 float this_error = std::numeric_limits<float>::max ();
247 for (RegionList::const_iterator j = _regions.begin(); j != _regions.end(); ++j) {
248 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j->content);
253 this_error += fabs (i->source - vc->video_frame_rate ());
256 if (this_error < error) {
272 Playlist::length (shared_ptr<const Film> film) const
275 for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
276 Time const t = i->time + i->content->length (film);