1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
4 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <libcxml/cxml.h>
23 #include <boost/shared_ptr.hpp>
24 #include <boost/lexical_cast.hpp>
26 #include "sndfile_content.h"
27 #include "sndfile_decoder.h"
28 #include "video_content.h"
29 #include "ffmpeg_decoder.h"
30 #include "ffmpeg_content.h"
31 #include "imagemagick_decoder.h"
32 #include "imagemagick_content.h"
45 using std::stringstream;
46 using boost::optional;
47 using boost::shared_ptr;
48 using boost::weak_ptr;
49 using boost::dynamic_pointer_cast;
50 using boost::lexical_cast;
58 Playlist::Playlist (shared_ptr<const Playlist> other)
59 : _loop (other->_loop)
61 for (RegionList::const_iterator i = other->_regions.begin(); i != other->_regions.end(); ++i) {
62 _regions.push_back (Region (i->content->clone(), i->time, this));
67 Playlist::content_changed (weak_ptr<Content> c, int p)
69 ContentChanged (c, p);
73 Playlist::audio_digest () const
77 for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
78 if (!dynamic_pointer_cast<const AudioContent> (i->content)) {
82 t += i->content->digest ();
84 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
86 t += lexical_cast<string> (fc->audio_stream()->id);
90 t += lexical_cast<string> (_loop);
92 return md5_digest (t.c_str(), t.length());
96 Playlist::video_digest () const
100 for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
101 if (!dynamic_pointer_cast<const VideoContent> (i->content)) {
105 t += i->content->digest ();
106 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
107 if (fc && fc->subtitle_stream()) {
108 t += fc->subtitle_stream()->id;
112 t += lexical_cast<string> (_loop);
114 return md5_digest (t.c_str(), t.length());
118 Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
120 list<shared_ptr<cxml::Node> > c = node->node_children ("Region");
121 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
122 _regions.push_back (Region (*i, this));
125 _loop = node->number_child<int> ("Loop");
129 Playlist::as_xml (xmlpp::Node* node)
131 for (RegionList::iterator i = _regions.begin(); i != _regions.end(); ++i) {
132 i->as_xml (node->add_child ("Region"));
135 node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
139 Playlist::add (shared_ptr<Content> c)
141 _regions.push_back (Region (c, 0, this));
146 Playlist::remove (shared_ptr<Content> c)
148 RegionList::iterator i = _regions.begin ();
149 while (i != _regions.end() && i->content != c) {
153 if (i != _regions.end ()) {
160 Playlist::set_loop (int l)
167 Playlist::has_subtitles () const
169 for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
170 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (i->content);
171 if (fc && !fc->subtitle_streams().empty()) {
179 Playlist::Region::Region (shared_ptr<Content> c, Time t, Playlist* p)
183 connection = c->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
186 Playlist::Region::Region (shared_ptr<const cxml::Node> node, Playlist* p)
188 shared_ptr<const cxml::Node> content_node = node->node_child ("Content");
189 string const type = content_node->string_child ("Type");
191 if (type == "FFmpeg") {
192 content.reset (new FFmpegContent (content_node));
193 } else if (type == "ImageMagick") {
194 content.reset (new ImageMagickContent (content_node));
195 } else if (type == "Sndfile") {
196 content.reset (new SndfileContent (content_node));
199 time = node->number_child<Time> ("Time");
200 connection = content->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
204 Playlist::Region::as_xml (xmlpp::Node* node) const
206 xmlpp::Node* sub = node->add_child ("Content");
207 content->as_xml (sub);
208 node->add_child ("Time")->add_child_text (lexical_cast<string> (time));
211 class FrameRateCandidate
214 FrameRateCandidate (float source_, int dcp_)
224 Playlist::best_dcp_frame_rate () const
226 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
228 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
229 list<FrameRateCandidate> candidates;
231 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
232 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
233 candidates.push_back (FrameRateCandidate (*i, *i));
236 /* Then the skip/repeat ones */
237 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
238 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
239 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
242 /* Pick the best one, bailing early if we hit an exact match */
243 float error = std::numeric_limits<float>::max ();
244 optional<FrameRateCandidate> best;
245 list<FrameRateCandidate>::iterator i = candidates.begin();
246 while (i != candidates.end()) {
248 float this_error = std::numeric_limits<float>::max ();
249 for (RegionList::const_iterator j = _regions.begin(); j != _regions.end(); ++j) {
250 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j->content);
255 this_error += fabs (i->source - vc->video_frame_rate ());
258 if (this_error < error) {
274 Playlist::length (shared_ptr<const Film> film) const
277 for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
278 Time const t = i->time + i->content->length (film);