Various Doxygen fixes.
[dcpomatic.git] / src / lib / playlist.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "playlist.h"
22 #include "video_content.h"
23 #include "subtitle_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 "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 (true)
55         , _sequencing (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::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 ();
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 ()
96 {
97         if (!_sequence || _sequencing) {
98                 return;
99         }
100
101         _sequencing = true;
102
103         /* Keep track of the content that we've set the position of so that we don't
104            do it twice.
105         */
106         ContentList placed;
107
108         /* Video */
109
110         DCPTime next_left;
111         DCPTime next_right;
112         BOOST_FOREACH (shared_ptr<Content> i, _content) {
113                 if (!i->video) {
114                         continue;
115                 }
116
117                 if (i->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
118                         i->set_position (next_right);
119                         next_right = i->end();
120                 } else {
121                         i->set_position (next_left);
122                         next_left = i->end();
123                 }
124
125                 placed.push_back (i);
126         }
127
128         /* Subtitles */
129
130         DCPTime next;
131         BOOST_FOREACH (shared_ptr<Content> i, _content) {
132                 if (!i->subtitle || find (placed.begin(), placed.end(), i) != placed.end()) {
133                         continue;
134                 }
135
136                 i->set_position (next);
137                 next = i->end();
138         }
139
140
141         /* This won't change order, so it does not need a sort */
142
143         _sequencing = false;
144 }
145
146 string
147 Playlist::video_identifier () const
148 {
149         string t;
150
151         BOOST_FOREACH (shared_ptr<const Content> i, _content) {
152                 if (i->video || (i->subtitle && i->subtitle->burn())) {
153                         t += i->identifier ();
154                 }
155         }
156
157         Digester digester;
158         digester.add (t.c_str(), t.length());
159         return digester.get ();
160 }
161
162 /** @param film Film that this Playlist is for.
163  *  @param node &lt;Playlist&gt; node.
164  *  @param version Metadata version number.
165  *  @param notes Output notes about that happened.
166  */
167 void
168 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
169 {
170         BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
171                 _content.push_back (content_factory (film, i, version, notes));
172         }
173
174         /* This shouldn't be necessary but better safe than sorry (there could be old files) */
175         sort (_content.begin(), _content.end(), ContentSorter ());
176
177         reconnect ();
178 }
179
180 /** @param node &lt;Playlist&gt; node.
181  *  @param with_content_paths true to include &lt;Path&gt; nodes in &lt;Content&gt; nodes, false to omit them.
182  */
183 void
184 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
185 {
186         BOOST_FOREACH (shared_ptr<Content> i, _content) {
187                 i->as_xml (node->add_child ("Content"), with_content_paths);
188         }
189 }
190
191 void
192 Playlist::add (shared_ptr<Content> c)
193 {
194         _content.push_back (c);
195         sort (_content.begin(), _content.end(), ContentSorter ());
196         reconnect ();
197         Changed ();
198 }
199
200 void
201 Playlist::remove (shared_ptr<Content> c)
202 {
203         ContentList::iterator i = _content.begin ();
204         while (i != _content.end() && *i != c) {
205                 ++i;
206         }
207
208         if (i != _content.end ()) {
209                 _content.erase (i);
210                 Changed ();
211         }
212
213         /* This won't change order, so it does not need a sort */
214 }
215
216 void
217 Playlist::remove (ContentList c)
218 {
219         BOOST_FOREACH (shared_ptr<Content> i, c) {
220                 ContentList::iterator j = _content.begin ();
221                 while (j != _content.end() && *j != i) {
222                         ++j;
223                 }
224
225                 if (j != _content.end ()) {
226                         _content.erase (j);
227                 }
228         }
229
230         /* This won't change order, so it does not need a sort */
231
232         Changed ();
233 }
234
235 class FrameRateCandidate
236 {
237 public:
238         FrameRateCandidate (float source_, int dcp_)
239                 : source (source_)
240                 , dcp (dcp_)
241         {}
242
243         float source;
244         int dcp;
245 };
246
247 int
248 Playlist::best_video_frame_rate () const
249 {
250         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
251
252         /* Work out what rates we could manage, including those achieved by using skip / repeat */
253         list<FrameRateCandidate> candidates;
254
255         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated 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 (*i, *i));
258         }
259
260         /* Then the skip/repeat ones */
261         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
262                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
263                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
264         }
265
266         /* Pick the best one */
267         float error = std::numeric_limits<float>::max ();
268         optional<FrameRateCandidate> best;
269         list<FrameRateCandidate>::iterator i = candidates.begin();
270         while (i != candidates.end()) {
271
272                 float this_error = 0;
273                 BOOST_FOREACH (shared_ptr<Content> j, _content) {
274                         if (!j->video || !j->video_frame_rate()) {
275                                 continue;
276                         }
277
278                         /* Best error for this content; we could use the content as-is or double its rate */
279                         float best_error = min (
280                                 float (fabs (i->source - j->video_frame_rate().get())),
281                                 float (fabs (i->source - j->video_frame_rate().get() * 2))
282                                 );
283
284                         /* Use the largest difference between DCP and source as the "error" */
285                         this_error = max (this_error, best_error);
286                 }
287
288                 if (this_error < error) {
289                         error = this_error;
290                         best = *i;
291                 }
292
293                 ++i;
294         }
295
296         if (!best) {
297                 return 24;
298         }
299
300         return best->dcp;
301 }
302
303 /** @return length of the playlist from time 0 to the last thing on the playlist */
304 DCPTime
305 Playlist::length () const
306 {
307         DCPTime len;
308         BOOST_FOREACH (shared_ptr<const Content> i, _content) {
309                 len = max (len, i->end());
310         }
311
312         return len;
313 }
314
315 /** @return position of the first thing on the playlist, if it's not empty */
316 optional<DCPTime>
317 Playlist::start () const
318 {
319         if (_content.empty ()) {
320                 return optional<DCPTime> ();
321         }
322
323         DCPTime start = DCPTime::max ();
324         BOOST_FOREACH (shared_ptr<Content> i, _content) {
325                 start = min (start, i->position ());
326         }
327
328         return start;
329 }
330
331 void
332 Playlist::reconnect ()
333 {
334         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
335                 i->disconnect ();
336         }
337
338         _content_connections.clear ();
339
340         BOOST_FOREACH (shared_ptr<Content> i, _content) {
341                 _content_connections.push_back (i->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
342         }
343 }
344
345 DCPTime
346 Playlist::video_end () const
347 {
348         DCPTime end;
349         BOOST_FOREACH (shared_ptr<Content> i, _content) {
350                 if (i->video) {
351                         end = max (end, i->end ());
352                 }
353         }
354
355         return end;
356 }
357
358 DCPTime
359 Playlist::subtitle_end () const
360 {
361         DCPTime end;
362         BOOST_FOREACH (shared_ptr<Content> i, _content) {
363                 if (i->subtitle) {
364                         end = max (end, i->end ());
365                 }
366         }
367
368         return end;
369 }
370
371 FrameRateChange
372 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
373 {
374         for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
375                 if (!(*i)->video) {
376                         continue;
377                 }
378
379                 if ((*i)->position() <= t) {
380                         /* This is the first piece of content (going backwards...) that starts before t,
381                            so it's the active one.
382                         */
383                         if ((*i)->video_frame_rate ()) {
384                                 /* This content specified a rate, so use it */
385                                 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
386                         } else {
387                                 /* No specified rate so just use the DCP one */
388                                 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
389                         }
390                 }
391         }
392
393         return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
394 }
395
396 void
397 Playlist::set_sequence (bool s)
398 {
399         _sequence = s;
400 }
401
402 bool
403 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
404 {
405         if (a->position() != b->position()) {
406                 return a->position() < b->position();
407         }
408
409         /* Put video before audio if they start at the same time */
410         if (a->video && !b->video) {
411                 return true;
412         } else if (!a->video && b->video) {
413                 return false;
414         }
415
416         /* Last resort */
417         return a->digest() < b->digest();
418 }
419
420 /** @return content in ascending order of position */
421 ContentList
422 Playlist::content () const
423 {
424         return _content;
425 }
426
427 void
428 Playlist::repeat (ContentList c, int n)
429 {
430         pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
431         BOOST_FOREACH (shared_ptr<Content> i, c) {
432                 range.first = min (range.first, i->position ());
433                 range.second = max (range.second, i->position ());
434                 range.first = min (range.first, i->end ());
435                 range.second = max (range.second, i->end ());
436         }
437
438         DCPTime pos = range.second;
439         for (int i = 0; i < n; ++i) {
440                 BOOST_FOREACH (shared_ptr<Content> j, c) {
441                         shared_ptr<Content> copy = j->clone ();
442                         copy->set_position (pos + copy->position() - range.first);
443                         _content.push_back (copy);
444                 }
445                 pos += range.second - range.first;
446         }
447
448         sort (_content.begin(), _content.end(), ContentSorter ());
449
450         reconnect ();
451         Changed ();
452 }
453
454 void
455 Playlist::move_earlier (shared_ptr<Content> c)
456 {
457         ContentList::iterator previous = _content.end ();
458         ContentList::iterator i = _content.begin();
459         while (i != _content.end() && *i != c) {
460                 previous = i;
461                 ++i;
462         }
463
464         DCPOMATIC_ASSERT (i != _content.end ());
465         if (previous == _content.end ()) {
466                 return;
467         }
468
469         shared_ptr<Content> previous_c = *previous;
470
471         DCPTime const p = previous_c->position ();
472         previous_c->set_position (p + c->length_after_trim ());
473         c->set_position (p);
474 }
475
476 void
477 Playlist::move_later (shared_ptr<Content> c)
478 {
479         ContentList::iterator i = _content.begin();
480         while (i != _content.end() && *i != c) {
481                 ++i;
482         }
483
484         DCPOMATIC_ASSERT (i != _content.end ());
485
486         ContentList::iterator next = i;
487         ++next;
488
489         if (next == _content.end ()) {
490                 return;
491         }
492
493         shared_ptr<Content> next_c = *next;
494
495         next_c->set_position (c->position ());
496         c->set_position (c->position() + next_c->length_after_trim ());
497 }
498
499 int64_t
500 Playlist::required_disk_space (int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
501 {
502         int64_t video = uint64_t (j2k_bandwidth / 8) * length().seconds ();
503         int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length().seconds ();
504
505         BOOST_FOREACH (shared_ptr<Content> i, _content) {
506                 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
507                 if (d) {
508                         if (d->reference_video()) {
509                                 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim().seconds();
510                         }
511                         if (d->reference_audio()) {
512                                 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim().seconds();
513                         }
514                 }
515         }
516
517         /* Add on 64k for bits and pieces (metadata, subs etc) */
518         return video + audio + 65536;
519 }
520
521 string
522 Playlist::content_summary (DCPTimePeriod period) const
523 {
524         string best_summary;
525         int best_score = -1;
526         BOOST_FOREACH (shared_ptr<Content> i, _content) {
527                 int score = 0;
528                 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end()).overlap (period);
529                 if (o) {
530                         score += 100 * o.get().duration().get() / period.duration().get();
531                 }
532
533                 if (i->video) {
534                         score += 100;
535                 }
536
537                 if (score > best_score) {
538                         best_summary = i->path(0).leaf().string();
539                         best_score = score;
540                 }
541         }
542
543         return best_summary;
544 }
545
546 bool
547 Playlist::video_content_at (DCPTime time) const
548 {
549         BOOST_FOREACH (shared_ptr<Content> i, _content) {
550                 if (i->video && i->position() <= time && time < i->end()) {
551                         return true;
552                 }
553         }
554
555         return false;
556 }