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