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