Tidy up video state identifer code slightly.
[dcpomatic.git] / src / lib / playlist.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <libcxml/cxml.h>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/lexical_cast.hpp>
23 #include "playlist.h"
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 "job.h"
32 #include "config.h"
33 #include "util.h"
34
35 #include "i18n.h"
36
37 using std::list;
38 using std::cout;
39 using std::vector;
40 using std::min;
41 using std::max;
42 using std::string;
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;
49
50 Playlist::Playlist ()
51         : _loop (1)
52         , _sequence_video (true)
53         , _sequencing_video (false)
54 {
55
56 }
57
58 Playlist::Playlist (shared_ptr<const Playlist> other)
59         : _loop (other->_loop)
60 {
61         for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
62                 _content.push_back ((*i)->clone ());
63         }
64 }
65
66 Playlist::~Playlist ()
67 {
68         _content.clear ();
69         reconnect ();
70 }
71
72 void
73 Playlist::content_changed (weak_ptr<Content> c, int p)
74 {
75         if (p == ContentProperty::LENGTH && _sequence_video && !_sequencing_video) {
76                 _sequencing_video = true;
77
78                 ContentList cl = _content;
79                 sort (cl.begin(), cl.end(), ContentSorter ());
80                 Time last = 0;
81                 for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
82                         if (!dynamic_pointer_cast<VideoContent> (*i)) {
83                                 continue;
84                         }
85
86                         (*i)->set_start (last);
87                         last = (*i)->end ();
88                 }
89
90                 _sequencing_video = false;
91         }
92         
93         ContentChanged (c, p);
94 }
95
96 string
97 Playlist::video_identifier () const
98 {
99         string t;
100         
101         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
102                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
103                 if (vc) {
104                         t += vc->identifier ();
105                 }
106         }
107
108         t += lexical_cast<string> (_loop);
109
110         return md5_digest (t.c_str(), t.length());
111 }
112
113 /** @param node <Playlist> node */
114 void
115 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
116 {
117         list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
118         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
119                 string const type = (*i)->string_child ("Type");
120
121                 boost::shared_ptr<Content> content;
122
123                 if (type == "FFmpeg") {
124                         content.reset (new FFmpegContent (film, *i));
125                 } else if (type == "ImageMagick") {
126                         content.reset (new ImageMagickContent (film, *i));
127                 } else if (type == "Sndfile") {
128                         content.reset (new SndfileContent (film, *i));
129                 }
130
131                 _content.push_back (content);
132         }
133
134         reconnect ();
135         _loop = node->number_child<int> ("Loop");
136         _sequence_video = node->bool_child ("SequenceVideo");
137 }
138
139 /** @param node <Playlist> node */
140 void
141 Playlist::as_xml (xmlpp::Node* node)
142 {
143         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
144                 (*i)->as_xml (node->add_child ("Content"));
145         }
146
147         node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
148         node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
149 }
150
151 void
152 Playlist::add (shared_ptr<Content> c)
153 {
154         _content.push_back (c);
155         reconnect ();
156         Changed ();
157 }
158
159 void
160 Playlist::remove (shared_ptr<Content> c)
161 {
162         ContentList::iterator i = _content.begin ();
163         while (i != _content.end() && *i != c) {
164                 ++i;
165         }
166         
167         if (i != _content.end ()) {
168                 _content.erase (i);
169                 Changed ();
170         }
171 }
172
173 void
174 Playlist::set_loop (int l)
175 {
176         _loop = l;
177         Changed ();
178 }
179
180 bool
181 Playlist::has_subtitles () const
182 {
183         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
184                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
185                 if (fc && !fc->subtitle_streams().empty()) {
186                         return true;
187                 }
188         }
189
190         return false;
191 }
192
193 class FrameRateCandidate
194 {
195 public:
196         FrameRateCandidate (float source_, int dcp_)
197                 : source (source_)
198                 , dcp (dcp_)
199         {}
200
201         float source;
202         int dcp;
203 };
204
205 int
206 Playlist::best_dcp_frame_rate () const
207 {
208         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
209
210         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
211         list<FrameRateCandidate> candidates;
212
213         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
214         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
215                 candidates.push_back (FrameRateCandidate (*i, *i));
216         }
217
218         /* Then the skip/repeat ones */
219         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
220                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
221                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
222         }
223
224         /* Pick the best one, bailing early if we hit an exact match */
225         float error = std::numeric_limits<float>::max ();
226         optional<FrameRateCandidate> best;
227         list<FrameRateCandidate>::iterator i = candidates.begin();
228         while (i != candidates.end()) {
229
230                 float this_error = 0;
231                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
232                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
233                         if (!vc) {
234                                 continue;
235                         }
236
237                         this_error += fabs (i->source - vc->video_frame_rate ());
238                 }
239
240                 if (this_error < error) {
241                         error = this_error;
242                         best = *i;
243                 }
244
245                 ++i;
246         }
247
248         if (!best) {
249                 return 24;
250         }
251         
252         return best->dcp;
253 }
254
255 Time
256 Playlist::length () const
257 {
258         Time len = 0;
259         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
260                 len = max (len, (*i)->end ());
261         }
262
263         return len;
264 }
265
266 void
267 Playlist::reconnect ()
268 {
269         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
270                 i->disconnect ();
271         }
272
273         _content_connections.clear ();
274                 
275         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
276                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
277         }
278 }
279
280 Time
281 Playlist::video_end () const
282 {
283         Time end = 0;
284         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
285                 if (dynamic_pointer_cast<const VideoContent> (*i)) {
286                         end = max (end, (*i)->end ());
287                 }
288         }
289
290         return end;
291 }
292
293 void
294 Playlist::set_sequence_video (bool s)
295 {
296         _sequence_video = s;
297 }
298
299 bool
300 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
301 {
302         return a->start() < b->start();
303 }