Merge master.
[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 "image_decoder.h"
30 #include "content_factory.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 std::pair;
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;
50
51 Playlist::Playlist ()
52         : _sequence_video (true)
53         , _sequencing_video (false)
54 {
55
56 }
57
58 Playlist::~Playlist ()
59 {
60         _content.clear ();
61         reconnect ();
62 }
63
64 void
65 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
66 {
67         if (property == ContentProperty::LENGTH) {
68                 maybe_sequence_video ();
69         }
70         
71         ContentChanged (content, property, frequent);
72 }
73
74 void
75 Playlist::maybe_sequence_video ()
76 {
77         if (!_sequence_video || _sequencing_video) {
78                 return;
79         }
80         
81         _sequencing_video = true;
82         
83         ContentList cl = _content;
84         DCPTime next;
85         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
86                 if (!dynamic_pointer_cast<VideoContent> (*i)) {
87                         continue;
88                 }
89                 
90                 (*i)->set_position (next);
91                 next = (*i)->end() + DCPTime::delta ();
92         }
93
94         /* This won't change order, so it does not need a sort */
95         
96         _sequencing_video = false;
97 }
98
99 string
100 Playlist::video_identifier () const
101 {
102         string t;
103         
104         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
105                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
106                 if (vc) {
107                         t += vc->identifier ();
108                 }
109         }
110
111         return md5_digest (t.c_str(), t.length());
112 }
113
114 /** @param node <Playlist> node */
115 void
116 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node, int version, list<string>& notes)
117 {
118         list<cxml::NodePtr> c = node->node_children ("Content");
119         for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
120                 _content.push_back (content_factory (film, *i, version, notes));
121         }
122
123         sort (_content.begin(), _content.end(), ContentSorter ());
124
125         reconnect ();
126 }
127
128 /** @param node <Playlist> node */
129 void
130 Playlist::as_xml (xmlpp::Node* node)
131 {
132         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
133                 (*i)->as_xml (node->add_child ("Content"));
134         }
135 }
136
137 void
138 Playlist::add (shared_ptr<Content> c)
139 {
140         _content.push_back (c);
141         sort (_content.begin(), _content.end(), ContentSorter ());
142         reconnect ();
143         Changed ();
144 }
145
146 void
147 Playlist::remove (shared_ptr<Content> c)
148 {
149         ContentList::iterator i = _content.begin ();
150         while (i != _content.end() && *i != c) {
151                 ++i;
152         }
153         
154         if (i != _content.end ()) {
155                 _content.erase (i);
156                 Changed ();
157         }
158
159         /* This won't change order, so it does not need a sort */
160 }
161
162 void
163 Playlist::remove (ContentList c)
164 {
165         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
166                 ContentList::iterator j = _content.begin ();
167                 while (j != _content.end() && *j != *i) {
168                         ++j;
169                 }
170         
171                 if (j != _content.end ()) {
172                         _content.erase (j);
173                 }
174         }
175
176         /* This won't change order, so it does not need a sort */
177         
178         Changed ();
179 }
180
181 bool
182 Playlist::has_subtitles () const
183 {
184         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
185                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
186                 if (fc && !fc->subtitle_streams().empty()) {
187                         return true;
188                 }
189         }
190
191         return false;
192 }
193
194 class FrameRateCandidate
195 {
196 public:
197         FrameRateCandidate (float source_, int dcp_)
198                 : source (source_)
199                 , dcp (dcp_)
200         {}
201
202         float source;
203         int dcp;
204 };
205
206 int
207 Playlist::best_dcp_frame_rate () const
208 {
209         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
210
211         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
212         list<FrameRateCandidate> candidates;
213
214         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
215         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
216                 candidates.push_back (FrameRateCandidate (*i, *i));
217         }
218
219         /* Then the skip/repeat ones */
220         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
221                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
222                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
223         }
224
225         /* Pick the best one */
226         float error = std::numeric_limits<float>::max ();
227         optional<FrameRateCandidate> best;
228         list<FrameRateCandidate>::iterator i = candidates.begin();
229         while (i != candidates.end()) {
230
231                 float this_error = 0;
232                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
233                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
234                         if (!vc) {
235                                 continue;
236                         }
237
238                         /* Use the largest difference between DCP and source as the "error" */
239                         this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
240                 }
241
242                 if (this_error < error) {
243                         error = this_error;
244                         best = *i;
245                 }
246
247                 ++i;
248         }
249
250         if (!best) {
251                 return 24;
252         }
253         
254         return best->dcp;
255 }
256
257 DCPTime
258 Playlist::length () const
259 {
260         DCPTime len;
261         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
262                 len = max (len, (*i)->end() + DCPTime::delta ());
263         }
264
265         return len;
266 }
267
268 void
269 Playlist::reconnect ()
270 {
271         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
272                 i->disconnect ();
273         }
274
275         _content_connections.clear ();
276                 
277         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
278                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
279         }
280 }
281
282 DCPTime
283 Playlist::video_end () const
284 {
285         DCPTime end;
286         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
287                 if (dynamic_pointer_cast<const VideoContent> (*i)) {
288                         end = max (end, (*i)->end ());
289                 }
290         }
291
292         return end;
293 }
294
295 FrameRateChange
296 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
297 {
298         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
299                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
300                 if (!vc) {
301                         break;
302                 }
303
304                 if (vc->position() >= t && t < vc->end()) {
305                         return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
306                 }
307         }
308
309         return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
310 }
311
312 void
313 Playlist::set_sequence_video (bool s)
314 {
315         _sequence_video = s;
316 }
317
318 bool
319 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
320 {
321         return a->position() < b->position();
322 }
323
324 /** @return content in an undefined order */
325 ContentList
326 Playlist::content () const
327 {
328         return _content;
329 }
330
331 void
332 Playlist::repeat (ContentList c, int n)
333 {
334         pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
335         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
336                 range.first = min (range.first, (*i)->position ());
337                 range.second = max (range.second, (*i)->position ());
338                 range.first = min (range.first, (*i)->end ());
339                 range.second = max (range.second, (*i)->end ());
340         }
341
342         DCPTime pos = range.second;
343         for (int i = 0; i < n; ++i) {
344                 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
345                         shared_ptr<Content> copy = (*i)->clone ();
346                         copy->set_position (pos + copy->position() - range.first);
347                         _content.push_back (copy);
348                 }
349                 pos += range.second - range.first;
350         }
351
352         sort (_content.begin(), _content.end(), ContentSorter ());
353         
354         reconnect ();
355         Changed ();
356 }
357
358 void
359 Playlist::move_earlier (shared_ptr<Content> c)
360 {
361         sort (_content.begin(), _content.end(), ContentSorter ());
362         
363         ContentList::iterator previous = _content.end ();
364         ContentList::iterator i = _content.begin();
365         while (i != _content.end() && *i != c) {
366                 previous = i;
367                 ++i;
368         }
369
370         assert (i != _content.end ());
371         if (previous == _content.end ()) {
372                 return;
373         }
374         
375         DCPTime const p = (*previous)->position ();
376         (*previous)->set_position (p + c->length_after_trim ());
377         c->set_position (p);
378         sort (_content.begin(), _content.end(), ContentSorter ());
379         
380         Changed ();
381 }
382
383 void
384 Playlist::move_later (shared_ptr<Content> c)
385 {
386         sort (_content.begin(), _content.end(), ContentSorter ());
387         
388         ContentList::iterator i = _content.begin();
389         while (i != _content.end() && *i != c) {
390                 ++i;
391         }
392
393         assert (i != _content.end ());
394
395         ContentList::iterator next = i;
396         ++next;
397
398         if (next == _content.end ()) {
399                 return;
400         }
401
402         DCPTime const p = (*next)->position ();
403         (*next)->set_position (c->position ());
404         c->set_position (p + c->length_after_trim ());
405         sort (_content.begin(), _content.end(), ContentSorter ());
406         
407         Changed ();
408 }