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