Add a PlaylistOrderChanged signal and emit it when the playlist
[dcpomatic.git] / src / lib / playlist.cc
1 /*
2     Copyright (C) 2013-2016 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 "playlist.h"
21 #include "sndfile_content.h"
22 #include "sndfile_decoder.h"
23 #include "video_content.h"
24 #include "ffmpeg_decoder.h"
25 #include "ffmpeg_content.h"
26 #include "image_decoder.h"
27 #include "content_factory.h"
28 #include "dcp_content.h"
29 #include "job.h"
30 #include "config.h"
31 #include "util.h"
32 #include "md5_digester.h"
33 #include <libcxml/cxml.h>
34 #include <libxml++/libxml++.h>
35 #include <boost/shared_ptr.hpp>
36 #include <boost/foreach.hpp>
37 #include <iostream>
38
39 #include "i18n.h"
40
41 using std::list;
42 using std::cout;
43 using std::vector;
44 using std::min;
45 using std::max;
46 using std::string;
47 using std::pair;
48 using boost::optional;
49 using boost::shared_ptr;
50 using boost::weak_ptr;
51 using boost::dynamic_pointer_cast;
52
53 Playlist::Playlist ()
54         : _sequence_video (true)
55         , _sequencing_video (false)
56 {
57
58 }
59
60 Playlist::~Playlist ()
61 {
62         _content.clear ();
63         reconnect ();
64 }
65
66 void
67 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
68 {
69         if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
70                 /* Don't respond to position changes here, as:
71                    - sequencing after earlier/later changes is handled by move_earlier/move_later
72                    - any other position changes will be timeline drags which should not result in content
73                    being sequenced.
74                 */
75                 maybe_sequence_video ();
76         }
77
78         if (
79                 property == ContentProperty::POSITION ||
80                 property == ContentProperty::LENGTH ||
81                 property == ContentProperty::TRIM_START ||
82                 property == ContentProperty::TRIM_END) {
83
84                 ContentList old = _content;
85                 sort (_content.begin(), _content.end(), ContentSorter ());
86                 if (_content != old) {
87                         OrderChanged ();
88                 }
89         }
90
91         ContentChanged (content, property, frequent);
92 }
93
94 void
95 Playlist::maybe_sequence_video ()
96 {
97         if (!_sequence_video || _sequencing_video) {
98                 return;
99         }
100
101         _sequencing_video = true;
102
103         DCPTime next_left;
104         DCPTime next_right;
105         BOOST_FOREACH (shared_ptr<Content> i, _content) {
106                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
107                 if (!vc) {
108                         continue;
109                 }
110
111                 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
112                         vc->set_position (next_right);
113                         next_right = vc->end();
114                 } else {
115                         vc->set_position (next_left);
116                         next_left = vc->end();
117                 }
118         }
119
120         /* This won't change order, so it does not need a sort */
121
122         _sequencing_video = false;
123 }
124
125 string
126 Playlist::video_identifier () const
127 {
128         string t;
129
130         BOOST_FOREACH (shared_ptr<const Content> i, _content) {
131                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (i);
132                 shared_ptr<const SubtitleContent> sc = dynamic_pointer_cast<const SubtitleContent> (i);
133                 if (vc) {
134                         t += vc->identifier ();
135                 } else if (sc && sc->burn_subtitles ()) {
136                         t += sc->identifier ();
137                 }
138         }
139
140         MD5Digester digester;
141         digester.add (t.c_str(), t.length());
142         return digester.get ();
143 }
144
145 /** @param node <Playlist> node */
146 void
147 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
148 {
149         BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
150                 _content.push_back (content_factory (film, i, version, notes));
151         }
152
153         /* This shouldn't be necessary but better safe than sorry (there could be old files) */
154         sort (_content.begin(), _content.end(), ContentSorter ());
155
156         reconnect ();
157 }
158
159 /** @param node <Playlist> node */
160 void
161 Playlist::as_xml (xmlpp::Node* node)
162 {
163         BOOST_FOREACH (shared_ptr<Content> i, _content) {
164                 i->as_xml (node->add_child ("Content"));
165         }
166 }
167
168 void
169 Playlist::add (shared_ptr<Content> c)
170 {
171         _content.push_back (c);
172         sort (_content.begin(), _content.end(), ContentSorter ());
173         reconnect ();
174         Changed ();
175 }
176
177 void
178 Playlist::remove (shared_ptr<Content> c)
179 {
180         ContentList::iterator i = _content.begin ();
181         while (i != _content.end() && *i != c) {
182                 ++i;
183         }
184
185         if (i != _content.end ()) {
186                 _content.erase (i);
187                 Changed ();
188         }
189
190         /* This won't change order, so it does not need a sort */
191 }
192
193 void
194 Playlist::remove (ContentList c)
195 {
196         BOOST_FOREACH (shared_ptr<Content> i, c) {
197                 ContentList::iterator j = _content.begin ();
198                 while (j != _content.end() && *j != i) {
199                         ++j;
200                 }
201
202                 if (j != _content.end ()) {
203                         _content.erase (j);
204                 }
205         }
206
207         /* This won't change order, so it does not need a sort */
208
209         Changed ();
210 }
211
212 class FrameRateCandidate
213 {
214 public:
215         FrameRateCandidate (float source_, int dcp_)
216                 : source (source_)
217                 , dcp (dcp_)
218         {}
219
220         float source;
221         int dcp;
222 };
223
224 int
225 Playlist::best_dcp_frame_rate () const
226 {
227         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
228
229         /* Work out what rates we could manage, including those achieved by using skip / repeat */
230         list<FrameRateCandidate> candidates;
231
232         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
233         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
234                 candidates.push_back (FrameRateCandidate (*i, *i));
235         }
236
237         /* Then the skip/repeat ones */
238         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
239                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
240                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
241         }
242
243         /* Pick the best one */
244         float error = std::numeric_limits<float>::max ();
245         optional<FrameRateCandidate> best;
246         list<FrameRateCandidate>::iterator i = candidates.begin();
247         while (i != candidates.end()) {
248
249                 float this_error = 0;
250                 BOOST_FOREACH (shared_ptr<Content> j, _content) {
251                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j);
252                         if (!vc || !vc->has_own_video_frame_rate()) {
253                                 continue;
254                         }
255
256                         /* Best error for this content; we could use the content as-is or double its rate */
257                         float best_error = min (
258                                 float (fabs (i->source - vc->video_frame_rate ())),
259                                 float (fabs (i->source - vc->video_frame_rate () * 2))
260                                 );
261
262                         /* Use the largest difference between DCP and source as the "error" */
263                         this_error = max (this_error, best_error);
264                 }
265
266                 if (this_error < error) {
267                         error = this_error;
268                         best = *i;
269                 }
270
271                 ++i;
272         }
273
274         if (!best) {
275                 return 24;
276         }
277
278         return best->dcp;
279 }
280
281 /** @return length of the playlist from time 0 to the last thing on the playlist */
282 DCPTime
283 Playlist::length () const
284 {
285         DCPTime len;
286         BOOST_FOREACH (shared_ptr<const Content> i, _content) {
287                 len = max (len, i->end());
288         }
289
290         return len;
291 }
292
293 /** @return position of the first thing on the playlist, if it's not empty */
294 optional<DCPTime>
295 Playlist::start () const
296 {
297         if (_content.empty ()) {
298                 return optional<DCPTime> ();
299         }
300
301         DCPTime start = DCPTime::max ();
302         BOOST_FOREACH (shared_ptr<Content> i, _content) {
303                 start = min (start, i->position ());
304         }
305
306         return start;
307 }
308
309 void
310 Playlist::reconnect ()
311 {
312         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
313                 i->disconnect ();
314         }
315
316         _content_connections.clear ();
317
318         BOOST_FOREACH (shared_ptr<Content> i, _content) {
319                 _content_connections.push_back (i->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
320         }
321 }
322
323 DCPTime
324 Playlist::video_end () const
325 {
326         DCPTime end;
327         BOOST_FOREACH (shared_ptr<Content> i, _content) {
328                 if (dynamic_pointer_cast<const VideoContent> (i)) {
329                         end = max (end, i->end ());
330                 }
331         }
332
333         return end;
334 }
335
336 FrameRateChange
337 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
338 {
339         for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
340                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
341                 if (!vc) {
342                         continue;
343                 }
344
345                 if (vc->position() <= t) {
346                         /* This is the first piece of content (going backwards...) that starts before t,
347                            so it's the active one.
348                         */
349                         return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
350                 }
351         }
352
353         return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
354 }
355
356 void
357 Playlist::set_sequence_video (bool s)
358 {
359         _sequence_video = s;
360 }
361
362 bool
363 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
364 {
365         if (a->position() != b->position()) {
366                 return a->position() < b->position();
367         }
368
369         /* Put video before audio if they start at the same time */
370         if (dynamic_pointer_cast<VideoContent>(a) && !dynamic_pointer_cast<VideoContent>(b)) {
371                 return true;
372         } else if (!dynamic_pointer_cast<VideoContent>(a) && dynamic_pointer_cast<VideoContent>(b)) {
373                 return false;
374         }
375
376         /* Last resort */
377         return a->digest() < b->digest();
378 }
379
380 /** @return content in ascending order of position */
381 ContentList
382 Playlist::content () const
383 {
384         return _content;
385 }
386
387 void
388 Playlist::repeat (ContentList c, int n)
389 {
390         pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
391         BOOST_FOREACH (shared_ptr<Content> i, c) {
392                 range.first = min (range.first, i->position ());
393                 range.second = max (range.second, i->position ());
394                 range.first = min (range.first, i->end ());
395                 range.second = max (range.second, i->end ());
396         }
397
398         DCPTime pos = range.second;
399         for (int i = 0; i < n; ++i) {
400                 BOOST_FOREACH (shared_ptr<Content> j, c) {
401                         shared_ptr<Content> copy = j->clone ();
402                         copy->set_position (pos + copy->position() - range.first);
403                         _content.push_back (copy);
404                 }
405                 pos += range.second - range.first;
406         }
407
408         sort (_content.begin(), _content.end(), ContentSorter ());
409
410         reconnect ();
411         Changed ();
412 }
413
414 void
415 Playlist::move_earlier (shared_ptr<Content> c)
416 {
417         ContentList::iterator previous = _content.end ();
418         ContentList::iterator i = _content.begin();
419         while (i != _content.end() && *i != c) {
420                 previous = i;
421                 ++i;
422         }
423
424         DCPOMATIC_ASSERT (i != _content.end ());
425         if (previous == _content.end ()) {
426                 return;
427         }
428
429
430         DCPTime const p = (*previous)->position ();
431         (*previous)->set_position (p + c->length_after_trim ());
432         c->set_position (p);
433 }
434
435 void
436 Playlist::move_later (shared_ptr<Content> c)
437 {
438         ContentList::iterator i = _content.begin();
439         while (i != _content.end() && *i != c) {
440                 ++i;
441         }
442
443         DCPOMATIC_ASSERT (i != _content.end ());
444
445         ContentList::iterator next = i;
446         ++next;
447
448         if (next == _content.end ()) {
449                 return;
450         }
451
452         (*next)->set_position (c->position ());
453         c->set_position (c->position() + (*next)->length_after_trim ());
454 }
455
456 int64_t
457 Playlist::required_disk_space (int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
458 {
459         int64_t video = uint64_t (j2k_bandwidth / 8) * length().seconds ();
460         int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length().seconds ();
461
462         BOOST_FOREACH (shared_ptr<Content> i, _content) {
463                 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
464                 if (d) {
465                         if (d->reference_video()) {
466                                 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim().seconds();
467                         }
468                         if (d->reference_audio()) {
469                                 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim().seconds();
470                         }
471                 }
472         }
473
474         /* Add on 64k for bits and pieces (metadata, subs etc) */
475         return video + audio + 65536;
476 }