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