Remove some unused code.
[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 "audio_content.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 "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         Digester digester;
159         digester.add (t.c_str(), t.length());
160         return digester.get ();
161 }
162
163 /** @param film Film that this Playlist is for.
164  *  @param node &lt;Playlist&gt; node.
165  *  @param version Metadata version number.
166  *  @param notes Output notes about that happened.
167  */
168 void
169 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
170 {
171         BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Content")) {
172                 _content.push_back (content_factory (film, i, version, notes));
173         }
174
175         /* This shouldn't be necessary but better safe than sorry (there could be old files) */
176         sort (_content.begin(), _content.end(), ContentSorter ());
177
178         reconnect ();
179 }
180
181 /** @param node &lt;Playlist&gt; node.
182  *  @param with_content_paths true to include &lt;Path&gt; nodes in &lt;Content&gt; nodes, false to omit them.
183  */
184 void
185 Playlist::as_xml (xmlpp::Node* node, bool with_content_paths)
186 {
187         BOOST_FOREACH (shared_ptr<Content> i, _content) {
188                 i->as_xml (node->add_child ("Content"), with_content_paths);
189         }
190 }
191
192 void
193 Playlist::add (shared_ptr<Content> c)
194 {
195         _content.push_back (c);
196         sort (_content.begin(), _content.end(), ContentSorter ());
197         reconnect ();
198         Changed ();
199 }
200
201 void
202 Playlist::remove (shared_ptr<Content> c)
203 {
204         ContentList::iterator i = _content.begin ();
205         while (i != _content.end() && *i != c) {
206                 ++i;
207         }
208
209         if (i != _content.end ()) {
210                 _content.erase (i);
211                 Changed ();
212         }
213
214         /* This won't change order, so it does not need a sort */
215 }
216
217 void
218 Playlist::remove (ContentList c)
219 {
220         BOOST_FOREACH (shared_ptr<Content> i, c) {
221                 ContentList::iterator j = _content.begin ();
222                 while (j != _content.end() && *j != i) {
223                         ++j;
224                 }
225
226                 if (j != _content.end ()) {
227                         _content.erase (j);
228                 }
229         }
230
231         /* This won't change order, so it does not need a sort */
232
233         Changed ();
234 }
235
236 class FrameRateCandidate
237 {
238 public:
239         FrameRateCandidate (float source_, int dcp_)
240                 : source (source_)
241                 , dcp (dcp_)
242         {}
243
244         float source;
245         int dcp;
246 };
247
248 int
249 Playlist::best_video_frame_rate () const
250 {
251         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
252
253         /* Work out what rates we could manage, including those achieved by using skip / repeat */
254         list<FrameRateCandidate> candidates;
255
256         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
257         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
258                 candidates.push_back (FrameRateCandidate (*i, *i));
259         }
260
261         /* Then the skip/repeat ones */
262         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
263                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
264                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
265         }
266
267         /* Pick the best one */
268         float error = std::numeric_limits<float>::max ();
269         optional<FrameRateCandidate> best;
270         list<FrameRateCandidate>::iterator i = candidates.begin();
271         while (i != candidates.end()) {
272
273                 float this_error = 0;
274                 BOOST_FOREACH (shared_ptr<Content> j, _content) {
275                         if (!j->video || !j->video_frame_rate()) {
276                                 continue;
277                         }
278
279                         /* Best error for this content; we could use the content as-is or double its rate */
280                         float best_error = min (
281                                 float (fabs (i->source - j->video_frame_rate().get())),
282                                 float (fabs (i->source - j->video_frame_rate().get() * 2))
283                                 );
284
285                         /* Use the largest difference between DCP and source as the "error" */
286                         this_error = max (this_error, best_error);
287                 }
288
289                 if (this_error < error) {
290                         error = this_error;
291                         best = *i;
292                 }
293
294                 ++i;
295         }
296
297         if (!best) {
298                 return 24;
299         }
300
301         return best->dcp;
302 }
303
304 /** @return length of the playlist from time 0 to the last thing on the playlist */
305 DCPTime
306 Playlist::length () const
307 {
308         DCPTime len;
309         BOOST_FOREACH (shared_ptr<const Content> i, _content) {
310                 len = max (len, i->end());
311         }
312
313         return len;
314 }
315
316 /** @return position of the first thing on the playlist, if it's not empty */
317 optional<DCPTime>
318 Playlist::start () const
319 {
320         if (_content.empty ()) {
321                 return optional<DCPTime> ();
322         }
323
324         DCPTime start = DCPTime::max ();
325         BOOST_FOREACH (shared_ptr<Content> i, _content) {
326                 start = min (start, i->position ());
327         }
328
329         return start;
330 }
331
332 void
333 Playlist::reconnect ()
334 {
335         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
336                 i->disconnect ();
337         }
338
339         _content_connections.clear ();
340
341         BOOST_FOREACH (shared_ptr<Content> i, _content) {
342                 _content_connections.push_back (i->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
343         }
344 }
345
346 DCPTime
347 Playlist::video_end () const
348 {
349         DCPTime end;
350         BOOST_FOREACH (shared_ptr<Content> i, _content) {
351                 if (i->video) {
352                         end = max (end, i->end ());
353                 }
354         }
355
356         return end;
357 }
358
359 DCPTime
360 Playlist::subtitle_end () const
361 {
362         DCPTime end;
363         BOOST_FOREACH (shared_ptr<Content> i, _content) {
364                 if (i->subtitle) {
365                         end = max (end, i->end ());
366                 }
367         }
368
369         return end;
370 }
371
372 FrameRateChange
373 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
374 {
375         for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
376                 if (!(*i)->video) {
377                         continue;
378                 }
379
380                 if ((*i)->position() <= t) {
381                         /* This is the first piece of content (going backwards...) that starts before t,
382                            so it's the active one.
383                         */
384                         if ((*i)->video_frame_rate ()) {
385                                 /* This content specified a rate, so use it */
386                                 return FrameRateChange ((*i)->video_frame_rate().get(), dcp_video_frame_rate);
387                         } else {
388                                 /* No specified rate so just use the DCP one */
389                                 return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
390                         }
391                 }
392         }
393
394         return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
395 }
396
397 void
398 Playlist::set_sequence (bool s)
399 {
400         _sequence = s;
401 }
402
403 bool
404 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
405 {
406         if (a->position() != b->position()) {
407                 return a->position() < b->position();
408         }
409
410         /* Put video before audio if they start at the same time */
411         if (a->video && !b->video) {
412                 return true;
413         } else if (!a->video && b->video) {
414                 return false;
415         }
416
417         /* Last resort */
418         return a->digest() < b->digest();
419 }
420
421 /** @return content in ascending order of position */
422 ContentList
423 Playlist::content () const
424 {
425         return _content;
426 }
427
428 void
429 Playlist::repeat (ContentList c, int n)
430 {
431         pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
432         BOOST_FOREACH (shared_ptr<Content> i, c) {
433                 range.first = min (range.first, i->position ());
434                 range.second = max (range.second, i->position ());
435                 range.first = min (range.first, i->end ());
436                 range.second = max (range.second, i->end ());
437         }
438
439         DCPTime pos = range.second;
440         for (int i = 0; i < n; ++i) {
441                 BOOST_FOREACH (shared_ptr<Content> j, c) {
442                         shared_ptr<Content> copy = j->clone ();
443                         copy->set_position (pos + copy->position() - range.first);
444                         _content.push_back (copy);
445                 }
446                 pos += range.second - range.first;
447         }
448
449         sort (_content.begin(), _content.end(), ContentSorter ());
450
451         reconnect ();
452         Changed ();
453 }
454
455 void
456 Playlist::move_earlier (shared_ptr<Content> c)
457 {
458         ContentList::iterator previous = _content.end ();
459         ContentList::iterator i = _content.begin();
460         while (i != _content.end() && *i != c) {
461                 previous = i;
462                 ++i;
463         }
464
465         DCPOMATIC_ASSERT (i != _content.end ());
466         if (previous == _content.end ()) {
467                 return;
468         }
469
470         shared_ptr<Content> previous_c = *previous;
471
472         DCPTime const p = previous_c->position ();
473         previous_c->set_position (p + c->length_after_trim ());
474         c->set_position (p);
475 }
476
477 void
478 Playlist::move_later (shared_ptr<Content> c)
479 {
480         ContentList::iterator i = _content.begin();
481         while (i != _content.end() && *i != c) {
482                 ++i;
483         }
484
485         DCPOMATIC_ASSERT (i != _content.end ());
486
487         ContentList::iterator next = i;
488         ++next;
489
490         if (next == _content.end ()) {
491                 return;
492         }
493
494         shared_ptr<Content> next_c = *next;
495
496         next_c->set_position (c->position ());
497         c->set_position (c->position() + next_c->length_after_trim ());
498 }
499
500 int64_t
501 Playlist::required_disk_space (int j2k_bandwidth, int audio_channels, int audio_frame_rate) const
502 {
503         int64_t video = uint64_t (j2k_bandwidth / 8) * length().seconds ();
504         int64_t audio = uint64_t (audio_channels * audio_frame_rate * 3) * length().seconds ();
505
506         BOOST_FOREACH (shared_ptr<Content> i, _content) {
507                 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (i);
508                 if (d) {
509                         if (d->reference_video()) {
510                                 video -= uint64_t (j2k_bandwidth / 8) * d->length_after_trim().seconds();
511                         }
512                         if (d->reference_audio()) {
513                                 audio -= uint64_t (audio_channels * audio_frame_rate * 3) * d->length_after_trim().seconds();
514                         }
515                 }
516         }
517
518         /* Add on 64k for bits and pieces (metadata, subs etc) */
519         return video + audio + 65536;
520 }
521
522 string
523 Playlist::content_summary (DCPTimePeriod period) const
524 {
525         string best_summary;
526         int best_score = -1;
527         BOOST_FOREACH (shared_ptr<Content> i, _content) {
528                 int score = 0;
529                 optional<DCPTimePeriod> const o = DCPTimePeriod(i->position(), i->end()).overlap (period);
530                 if (o) {
531                         score += 100 * o.get().duration().get() / period.duration().get();
532                 }
533
534                 if (i->video) {
535                         score += 100;
536                 }
537
538                 if (score > best_score) {
539                         best_summary = i->path(0).leaf().string();
540                         best_score = score;
541                 }
542         }
543
544         return best_summary;
545 }
546
547 pair<double, double>
548 Playlist::speed_up_range (int dcp_video_frame_rate) const
549 {
550         pair<double, double> range (DBL_MAX, -DBL_MAX);
551
552         BOOST_FOREACH (shared_ptr<Content> i, _content) {
553                 if (!i->video) {
554                         continue;
555                 }
556                 if (i->video_frame_rate()) {
557                         FrameRateChange const frc (i->video_frame_rate().get(), dcp_video_frame_rate);
558                         range.first = min (range.first, frc.speed_up);
559                         range.second = max (range.second, frc.speed_up);
560                 } else {
561                         FrameRateChange const frc (dcp_video_frame_rate, dcp_video_frame_rate);
562                         range.first = min (range.first, frc.speed_up);
563                         range.second = max (range.second, frc.speed_up);
564                 }
565         }
566
567         return range;
568 }