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