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