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