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 || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
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_left;
85         DCPTime next_right;
86         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
87                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
88                 if (!vc) {
89                         continue;
90                 }
91                 
92                 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
93                         vc->set_position (next_right);
94                         next_right = vc->end() + DCPTime::delta ();
95                 } else {
96                         vc->set_position (next_left);
97                         next_left = vc->end() + DCPTime::delta ();
98                 }
99         }
100
101         /* This won't change order, so it does not need a sort */
102         
103         _sequencing_video = false;
104 }
105
106 string
107 Playlist::video_identifier () const
108 {
109         string t;
110         
111         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
112                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
113                 if (vc) {
114                         t += vc->identifier ();
115                 }
116         }
117
118         return md5_digest (t.c_str(), t.length());
119 }
120
121 /** @param node <Playlist> node */
122 void
123 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> 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 bool
189 Playlist::has_subtitles () const
190 {
191         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
192                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
193                 if (fc && !fc->subtitle_streams().empty()) {
194                         return true;
195                 }
196         }
197
198         return false;
199 }
200
201 class FrameRateCandidate
202 {
203 public:
204         FrameRateCandidate (float source_, int dcp_)
205                 : source (source_)
206                 , dcp (dcp_)
207         {}
208
209         float source;
210         int dcp;
211 };
212
213 int
214 Playlist::best_dcp_frame_rate () const
215 {
216         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
217
218         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
219         list<FrameRateCandidate> candidates;
220
221         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
222         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
223                 candidates.push_back (FrameRateCandidate (*i, *i));
224         }
225
226         /* Then the skip/repeat ones */
227         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
228                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
229                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
230         }
231
232         /* Pick the best one */
233         float error = std::numeric_limits<float>::max ();
234         optional<FrameRateCandidate> best;
235         list<FrameRateCandidate>::iterator i = candidates.begin();
236         while (i != candidates.end()) {
237
238                 float this_error = 0;
239                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
240                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
241                         if (!vc) {
242                                 continue;
243                         }
244
245                         /* Use the largest difference between DCP and source as the "error" */
246                         this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
247                 }
248
249                 if (this_error < error) {
250                         error = this_error;
251                         best = *i;
252                 }
253
254                 ++i;
255         }
256
257         if (!best) {
258                 return 24;
259         }
260         
261         return best->dcp;
262 }
263
264 DCPTime
265 Playlist::length () const
266 {
267         DCPTime len;
268         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
269                 len = max (len, (*i)->end() + DCPTime::delta ());
270         }
271
272         return len;
273 }
274
275 void
276 Playlist::reconnect ()
277 {
278         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
279                 i->disconnect ();
280         }
281
282         _content_connections.clear ();
283                 
284         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
285                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
286         }
287 }
288
289 DCPTime
290 Playlist::video_end () const
291 {
292         DCPTime end;
293         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
294                 if (dynamic_pointer_cast<const VideoContent> (*i)) {
295                         end = max (end, (*i)->end ());
296                 }
297         }
298
299         return end;
300 }
301
302 FrameRateChange
303 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
304 {
305         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
306                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
307                 if (!vc) {
308                         break;
309                 }
310
311                 if (vc->position() >= t && t < vc->end()) {
312                         return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
313                 }
314         }
315
316         return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
317 }
318
319 void
320 Playlist::set_sequence_video (bool s)
321 {
322         _sequence_video = s;
323 }
324
325 bool
326 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
327 {
328         return a->position() < b->position();
329 }
330
331 /** @return content in an undefined order */
332 ContentList
333 Playlist::content () const
334 {
335         return _content;
336 }
337
338 void
339 Playlist::repeat (ContentList c, int n)
340 {
341         pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
342         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
343                 range.first = min (range.first, (*i)->position ());
344                 range.second = max (range.second, (*i)->position ());
345                 range.first = min (range.first, (*i)->end ());
346                 range.second = max (range.second, (*i)->end ());
347         }
348
349         DCPTime pos = range.second;
350         for (int i = 0; i < n; ++i) {
351                 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
352                         shared_ptr<Content> copy = (*i)->clone ();
353                         copy->set_position (pos + copy->position() - range.first);
354                         _content.push_back (copy);
355                 }
356                 pos += range.second - range.first;
357         }
358
359         sort (_content.begin(), _content.end(), ContentSorter ());
360         
361         reconnect ();
362         Changed ();
363 }
364
365 void
366 Playlist::move_earlier (shared_ptr<Content> c)
367 {
368         sort (_content.begin(), _content.end(), ContentSorter ());
369         
370         ContentList::iterator previous = _content.end ();
371         ContentList::iterator i = _content.begin();
372         while (i != _content.end() && *i != c) {
373                 previous = i;
374                 ++i;
375         }
376
377         assert (i != _content.end ());
378         if (previous == _content.end ()) {
379                 return;
380         }
381         
382         DCPTime const p = (*previous)->position ();
383         (*previous)->set_position (p + c->length_after_trim ());
384         c->set_position (p);
385         sort (_content.begin(), _content.end(), ContentSorter ());
386         
387         Changed ();
388 }
389
390 void
391 Playlist::move_later (shared_ptr<Content> c)
392 {
393         sort (_content.begin(), _content.end(), ContentSorter ());
394         
395         ContentList::iterator i = _content.begin();
396         while (i != _content.end() && *i != c) {
397                 ++i;
398         }
399
400         assert (i != _content.end ());
401
402         ContentList::iterator next = i;
403         ++next;
404
405         if (next == _content.end ()) {
406                 return;
407         }
408
409         DCPTime const p = (*next)->position ();
410         (*next)->set_position (c->position ());
411         c->set_position (p + c->length_after_trim ());
412         sort (_content.begin(), _content.end(), ContentSorter ());
413         
414         Changed ();
415 }