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