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"
31 #include "content_factory.h"
44 using std::stringstream;
45 using boost::optional;
46 using boost::shared_ptr;
47 using boost::weak_ptr;
48 using boost::dynamic_pointer_cast;
49 using boost::lexical_cast;
52 : _sequence_video (true)
53 , _sequencing_video (false)
58 Playlist::~Playlist ()
65 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
67 if (property == ContentProperty::LENGTH) {
68 maybe_sequence_video ();
71 ContentChanged (content, property, frequent);
76 Playlist::maybe_sequence_video ()
78 if (!_sequence_video || _sequencing_video) {
82 _sequencing_video = true;
84 ContentList cl = _content;
85 sort (cl.begin(), cl.end(), ContentSorter ());
87 for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
88 if (!dynamic_pointer_cast<VideoContent> (*i)) {
92 (*i)->set_start (last);
96 _sequencing_video = false;
100 Playlist::video_identifier () const
104 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
105 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
107 t += vc->identifier ();
111 return md5_digest (t.c_str(), t.length());
114 /** @param node <Playlist> node */
116 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
118 list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
119 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
120 _content.push_back (content_factory (film, *i));
124 _sequence_video = node->bool_child ("SequenceVideo");
127 /** @param node <Playlist> node */
129 Playlist::as_xml (xmlpp::Node* node)
131 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
132 (*i)->as_xml (node->add_child ("Content"));
135 node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
139 Playlist::add (shared_ptr<Content> c)
141 _content.push_back (c);
147 Playlist::remove (shared_ptr<Content> c)
149 ContentList::iterator i = _content.begin ();
150 while (i != _content.end() && *i != c) {
154 if (i != _content.end ()) {
161 Playlist::has_subtitles () const
163 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
164 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
165 if (fc && !fc->subtitle_streams().empty()) {
173 class FrameRateCandidate
176 FrameRateCandidate (float source_, int dcp_)
186 Playlist::best_dcp_frame_rate () const
188 list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
190 /* Work out what rates we could manage, including those achieved by using skip / repeat. */
191 list<FrameRateCandidate> candidates;
193 /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
194 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
195 candidates.push_back (FrameRateCandidate (*i, *i));
198 /* Then the skip/repeat ones */
199 for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
200 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
201 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
204 /* Pick the best one */
205 float error = std::numeric_limits<float>::max ();
206 optional<FrameRateCandidate> best;
207 list<FrameRateCandidate>::iterator i = candidates.begin();
208 while (i != candidates.end()) {
210 float this_error = 0;
211 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
212 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
217 /* Use the largest difference between DCP and source as the "error" */
218 this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
221 if (this_error < error) {
237 Playlist::length () const
240 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
241 len = max (len, (*i)->end ());
248 Playlist::reconnect ()
250 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
254 _content_connections.clear ();
256 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
257 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
262 Playlist::video_end () const
265 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
266 if (dynamic_pointer_cast<const VideoContent> (*i)) {
267 end = max (end, (*i)->end ());
275 Playlist::set_sequence_video (bool s)
281 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
283 return a->start() < b->start();
286 /** @return content in an undefined order */
287 Playlist::ContentList
288 Playlist::content () const
294 Playlist::repeat (shared_ptr<Content> c, int n)
296 Time pos = c->end ();
297 for (int i = 0; i < n; ++i) {
298 shared_ptr<Content> copy = c->clone ();
299 copy->set_start (pos);
300 _content.push_back (copy);