Merge.
[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::audio_digest () const
98 {
99         string t;
100         
101         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
102                 if (!dynamic_pointer_cast<const AudioContent> (*i)) {
103                         continue;
104                 }
105                 
106                 t += (*i)->digest ();
107
108                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
109                 if (fc) {
110                         t += lexical_cast<string> (fc->audio_stream()->id);
111                 }
112         }
113
114         t += lexical_cast<string> (_loop);
115
116         return md5_digest (t.c_str(), t.length());
117 }
118
119 string
120 Playlist::video_digest () const
121 {
122         string t;
123         
124         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
125                 if (!dynamic_pointer_cast<const VideoContent> (*i)) {
126                         continue;
127                 }
128                 
129                 t += (*i)->digest ();
130                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
131                 if (fc && fc->subtitle_stream()) {
132                         t += fc->subtitle_stream()->id;
133                 }
134         }
135
136         t += lexical_cast<string> (_loop);
137
138         return md5_digest (t.c_str(), t.length());
139 }
140
141 /** @param node <Playlist> node */
142 void
143 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
144 {
145         list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
146         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
147                 string const type = (*i)->string_child ("Type");
148
149                 boost::shared_ptr<Content> content;
150
151                 if (type == "FFmpeg") {
152                         content.reset (new FFmpegContent (film, *i));
153                 } else if (type == "ImageMagick") {
154                         content.reset (new ImageMagickContent (film, *i));
155                 } else if (type == "Sndfile") {
156                         content.reset (new SndfileContent (film, *i));
157                 }
158
159                 _content.push_back (content);
160         }
161
162         reconnect ();
163         _loop = node->number_child<int> ("Loop");
164         _sequence_video = node->bool_child ("SequenceVideo");
165 }
166
167 /** @param node <Playlist> node */
168 void
169 Playlist::as_xml (xmlpp::Node* node)
170 {
171         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
172                 (*i)->as_xml (node->add_child ("Content"));
173         }
174
175         node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
176         node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
177 }
178
179 void
180 Playlist::add (shared_ptr<Content> c)
181 {
182         _content.push_back (c);
183         reconnect ();
184         Changed ();
185 }
186
187 void
188 Playlist::remove (shared_ptr<Content> c)
189 {
190         ContentList::iterator i = _content.begin ();
191         while (i != _content.end() && *i != c) {
192                 ++i;
193         }
194         
195         if (i != _content.end ()) {
196                 _content.erase (i);
197                 Changed ();
198         }
199 }
200
201 void
202 Playlist::set_loop (int l)
203 {
204         _loop = l;
205         Changed ();
206 }
207
208 bool
209 Playlist::has_subtitles () const
210 {
211         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
212                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
213                 if (fc && !fc->subtitle_streams().empty()) {
214                         return true;
215                 }
216         }
217
218         return false;
219 }
220
221 class FrameRateCandidate
222 {
223 public:
224         FrameRateCandidate (float source_, int dcp_)
225                 : source (source_)
226                 , dcp (dcp_)
227         {}
228
229         float source;
230         int dcp;
231 };
232
233 int
234 Playlist::best_dcp_frame_rate () const
235 {
236         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
237
238         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
239         list<FrameRateCandidate> candidates;
240
241         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
242         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
243                 candidates.push_back (FrameRateCandidate (*i, *i));
244         }
245
246         /* Then the skip/repeat ones */
247         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
248                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
249                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
250         }
251
252         /* Pick the best one, bailing early if we hit an exact match */
253         float error = std::numeric_limits<float>::max ();
254         optional<FrameRateCandidate> best;
255         list<FrameRateCandidate>::iterator i = candidates.begin();
256         while (i != candidates.end()) {
257
258                 float this_error = std::numeric_limits<float>::max ();
259                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
260                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
261                         if (!vc) {
262                                 continue;
263                         }
264
265                         this_error += fabs (i->source - vc->video_frame_rate ());
266                 }
267
268                 if (this_error < error) {
269                         error = this_error;
270                         best = *i;
271                 }
272
273                 ++i;
274         }
275
276         if (!best) {
277                 return 24;
278         }
279         
280         return best->dcp;
281 }
282
283 Time
284 Playlist::length () const
285 {
286         Time len = 0;
287         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
288                 len = max (len, (*i)->end ());
289         }
290
291         return len;
292 }
293
294 void
295 Playlist::reconnect ()
296 {
297         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
298                 i->disconnect ();
299         }
300
301         _content_connections.clear ();
302                 
303         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
304                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
305         }
306 }
307
308 Time
309 Playlist::video_end () const
310 {
311         Time end = 0;
312         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
313                 if (dynamic_pointer_cast<const VideoContent> (*i)) {
314                         end = max (end, (*i)->end ());
315                 }
316         }
317
318         return end;
319 }
320
321 void
322 Playlist::set_sequence_video (bool s)
323 {
324         _sequence_video = s;
325 }
326
327 bool
328 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
329 {
330         return a->start() < b->start();
331 }