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