Express trims using ContentTime so that they do not change
[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 <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                 shared_ptr<const SubtitleContent> sc = dynamic_pointer_cast<const SubtitleContent> (*i);
117                 if (vc) {
118                         t += vc->identifier ();
119                 } else if (sc && sc->burn_subtitles ()) {
120                         t += sc->identifier ();
121                 }
122         }
123
124         MD5Digester digester;
125         digester.add (t.c_str(), t.length());
126         return digester.get ();
127 }
128
129 /** @param node <Playlist> node */
130 void
131 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
132 {
133         list<cxml::NodePtr> c = node->node_children ("Content");
134         for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
135                 _content.push_back (content_factory (film, *i, version, notes));
136         }
137
138         sort (_content.begin(), _content.end(), ContentSorter ());
139
140         reconnect ();
141 }
142
143 /** @param node <Playlist> node */
144 void
145 Playlist::as_xml (xmlpp::Node* node)
146 {
147         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
148                 (*i)->as_xml (node->add_child ("Content"));
149         }
150 }
151
152 void
153 Playlist::add (shared_ptr<Content> c)
154 {
155         _content.push_back (c);
156         sort (_content.begin(), _content.end(), ContentSorter ());
157         reconnect ();
158         Changed ();
159 }
160
161 void
162 Playlist::remove (shared_ptr<Content> c)
163 {
164         ContentList::iterator i = _content.begin ();
165         while (i != _content.end() && *i != c) {
166                 ++i;
167         }
168
169         if (i != _content.end ()) {
170                 _content.erase (i);
171                 Changed ();
172         }
173
174         /* This won't change order, so it does not need a sort */
175 }
176
177 void
178 Playlist::remove (ContentList c)
179 {
180         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
181                 ContentList::iterator j = _content.begin ();
182                 while (j != _content.end() && *j != *i) {
183                         ++j;
184                 }
185
186                 if (j != _content.end ()) {
187                         _content.erase (j);
188                 }
189         }
190
191         /* This won't change order, so it does not need a sort */
192
193         Changed ();
194 }
195
196 class FrameRateCandidate
197 {
198 public:
199         FrameRateCandidate (float source_, int dcp_)
200                 : source (source_)
201                 , dcp (dcp_)
202         {}
203
204         float source;
205         int dcp;
206 };
207
208 int
209 Playlist::best_dcp_frame_rate () const
210 {
211         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
212
213         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
214         list<FrameRateCandidate> candidates;
215
216         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
217         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
218                 candidates.push_back (FrameRateCandidate (*i, *i));
219         }
220
221         /* Then the skip/repeat ones */
222         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
223                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
224                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
225         }
226
227         /* Pick the best one */
228         float error = std::numeric_limits<float>::max ();
229         optional<FrameRateCandidate> best;
230         list<FrameRateCandidate>::iterator i = candidates.begin();
231         while (i != candidates.end()) {
232
233                 float this_error = 0;
234                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
235                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
236                         if (!vc) {
237                                 continue;
238                         }
239
240                         /* Best error for this content; we could use the content as-is or double its rate */
241                         float best_error = min (
242                                 float (fabs (i->source - vc->video_frame_rate ())),
243                                 float (fabs (i->source - vc->video_frame_rate () * 2))
244                                 );
245
246                         /* Use the largest difference between DCP and source as the "error" */
247                         this_error = max (this_error, best_error);
248                 }
249
250                 if (this_error < error) {
251                         error = this_error;
252                         best = *i;
253                 }
254
255                 ++i;
256         }
257
258         if (!best) {
259                 return 24;
260         }
261
262         return best->dcp;
263 }
264
265 DCPTime
266 Playlist::length () const
267 {
268         DCPTime len;
269         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
270                 len = max (len, (*i)->end());
271         }
272
273         return len;
274 }
275
276 void
277 Playlist::reconnect ()
278 {
279         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
280                 i->disconnect ();
281         }
282
283         _content_connections.clear ();
284
285         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
286                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
287         }
288 }
289
290 DCPTime
291 Playlist::video_end () const
292 {
293         DCPTime end;
294         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
295                 if (dynamic_pointer_cast<const VideoContent> (*i)) {
296                         end = max (end, (*i)->end ());
297                 }
298         }
299
300         return end;
301 }
302
303 FrameRateChange
304 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
305 {
306         for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) {
307                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
308                 if (!vc) {
309                         continue;
310                 }
311
312                 if (vc->position() <= t) {
313                         /* This is the first piece of content (going backwards...) that starts before t,
314                            so it's the active one.
315                         */
316                         return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
317                 }
318         }
319
320         return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
321 }
322
323 void
324 Playlist::set_sequence_video (bool s)
325 {
326         _sequence_video = s;
327 }
328
329 bool
330 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
331 {
332         return a->position() < b->position();
333 }
334
335 /** @return content in an undefined order */
336 ContentList
337 Playlist::content () const
338 {
339         return _content;
340 }
341
342 void
343 Playlist::repeat (ContentList c, int n)
344 {
345         pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
346         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
347                 range.first = min (range.first, (*i)->position ());
348                 range.second = max (range.second, (*i)->position ());
349                 range.first = min (range.first, (*i)->end ());
350                 range.second = max (range.second, (*i)->end ());
351         }
352
353         DCPTime pos = range.second;
354         for (int i = 0; i < n; ++i) {
355                 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
356                         shared_ptr<Content> copy = (*i)->clone ();
357                         copy->set_position (pos + copy->position() - range.first);
358                         _content.push_back (copy);
359                 }
360                 pos += range.second - range.first;
361         }
362
363         sort (_content.begin(), _content.end(), ContentSorter ());
364
365         reconnect ();
366         Changed ();
367 }
368
369 void
370 Playlist::move_earlier (shared_ptr<Content> c)
371 {
372         sort (_content.begin(), _content.end(), ContentSorter ());
373
374         ContentList::iterator previous = _content.end ();
375         ContentList::iterator i = _content.begin();
376         while (i != _content.end() && *i != c) {
377                 previous = i;
378                 ++i;
379         }
380
381         DCPOMATIC_ASSERT (i != _content.end ());
382         if (previous == _content.end ()) {
383                 return;
384         }
385
386
387         DCPTime const p = (*previous)->position ();
388         (*previous)->set_position (p + c->length_after_trim ());
389         c->set_position (p);
390         sort (_content.begin(), _content.end(), ContentSorter ());
391 }
392
393 void
394 Playlist::move_later (shared_ptr<Content> c)
395 {
396         sort (_content.begin(), _content.end(), ContentSorter ());
397
398         ContentList::iterator i = _content.begin();
399         while (i != _content.end() && *i != c) {
400                 ++i;
401         }
402
403         DCPOMATIC_ASSERT (i != _content.end ());
404
405         ContentList::iterator next = i;
406         ++next;
407
408         if (next == _content.end ()) {
409                 return;
410         }
411
412         (*next)->set_position (c->position ());
413         c->set_position (c->position() + (*next)->length_after_trim ());
414         sort (_content.begin(), _content.end(), ContentSorter ());
415 }