Put times in subtitle view.
[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::stringstream;
44 using std::pair;
45 using boost::optional;
46 using boost::shared_ptr;
47 using boost::weak_ptr;
48 using boost::dynamic_pointer_cast;
49
50 Playlist::Playlist ()
51         : _sequence_video (true)
52         , _sequencing_video (false)
53 {
54
55 }
56
57 Playlist::~Playlist ()
58 {
59         _content.clear ();
60         reconnect ();
61 }
62
63 void
64 Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
65 {
66         if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
67                 maybe_sequence_video ();
68         }
69         
70         ContentChanged (content, property, frequent);
71 }
72
73 void
74 Playlist::maybe_sequence_video ()
75 {
76         if (!_sequence_video || _sequencing_video) {
77                 return;
78         }
79         
80         _sequencing_video = true;
81         
82         ContentList cl = _content;
83         DCPTime next_left;
84         DCPTime next_right;
85         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
86                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
87                 if (!vc) {
88                         continue;
89                 }
90                 
91                 if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
92                         vc->set_position (next_right);
93                         next_right = vc->end() + DCPTime::delta ();
94                 } else {
95                         vc->set_position (next_left);
96                         next_left = vc->end() + DCPTime::delta ();
97                 }
98         }
99
100         /* This won't change order, so it does not need a sort */
101         
102         _sequencing_video = false;
103 }
104
105 string
106 Playlist::video_identifier () const
107 {
108         string t;
109         
110         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
111                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
112                 if (vc) {
113                         t += vc->identifier ();
114                 }
115         }
116
117         MD5Digester digester;
118         digester.add (t.c_str(), t.length());
119         return digester.get ();
120 }
121
122 /** @param node <Playlist> node */
123 void
124 Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
125 {
126         list<cxml::NodePtr> c = node->node_children ("Content");
127         for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
128                 _content.push_back (content_factory (film, *i, version, notes));
129         }
130
131         sort (_content.begin(), _content.end(), ContentSorter ());
132
133         reconnect ();
134 }
135
136 /** @param node <Playlist> node */
137 void
138 Playlist::as_xml (xmlpp::Node* node)
139 {
140         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
141                 (*i)->as_xml (node->add_child ("Content"));
142         }
143 }
144
145 void
146 Playlist::add (shared_ptr<Content> c)
147 {
148         _content.push_back (c);
149         sort (_content.begin(), _content.end(), ContentSorter ());
150         reconnect ();
151         Changed ();
152 }
153
154 void
155 Playlist::remove (shared_ptr<Content> c)
156 {
157         ContentList::iterator i = _content.begin ();
158         while (i != _content.end() && *i != c) {
159                 ++i;
160         }
161         
162         if (i != _content.end ()) {
163                 _content.erase (i);
164                 Changed ();
165         }
166
167         /* This won't change order, so it does not need a sort */
168 }
169
170 void
171 Playlist::remove (ContentList c)
172 {
173         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
174                 ContentList::iterator j = _content.begin ();
175                 while (j != _content.end() && *j != *i) {
176                         ++j;
177                 }
178         
179                 if (j != _content.end ()) {
180                         _content.erase (j);
181                 }
182         }
183
184         /* This won't change order, so it does not need a sort */
185         
186         Changed ();
187 }
188
189 bool
190 Playlist::has_subtitles () const
191 {
192         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
193                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
194                 if (fc && !fc->subtitle_streams().empty()) {
195                         return true;
196                 }
197
198                 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (*i);
199                 if (sc) {
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 DCPTime
271 Playlist::length () const
272 {
273         DCPTime len;
274         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
275                 len = max (len, (*i)->end() + DCPTime::delta ());
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 DCPTime
296 Playlist::video_end () const
297 {
298         DCPTime end;
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 FrameRateChange
309 Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
310 {
311         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
312                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
313                 if (!vc) {
314                         continue;
315                 }
316
317                 if (vc->position() >= t && t < vc->end()) {
318                         return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
319                 }
320         }
321
322         return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
323 }
324
325 void
326 Playlist::set_sequence_video (bool s)
327 {
328         _sequence_video = s;
329 }
330
331 bool
332 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
333 {
334         return a->position() < b->position();
335 }
336
337 /** @return content in an undefined order */
338 ContentList
339 Playlist::content () const
340 {
341         return _content;
342 }
343
344 void
345 Playlist::repeat (ContentList c, int n)
346 {
347         pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
348         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
349                 range.first = min (range.first, (*i)->position ());
350                 range.second = max (range.second, (*i)->position ());
351                 range.first = min (range.first, (*i)->end ());
352                 range.second = max (range.second, (*i)->end ());
353         }
354
355         DCPTime pos = range.second;
356         for (int i = 0; i < n; ++i) {
357                 for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
358                         shared_ptr<Content> copy = (*i)->clone ();
359                         copy->set_position (pos + copy->position() - range.first);
360                         _content.push_back (copy);
361                 }
362                 pos += range.second - range.first;
363         }
364
365         sort (_content.begin(), _content.end(), ContentSorter ());
366         
367         reconnect ();
368         Changed ();
369 }
370
371 void
372 Playlist::move_earlier (shared_ptr<Content> c)
373 {
374         sort (_content.begin(), _content.end(), ContentSorter ());
375         
376         ContentList::iterator previous = _content.end ();
377         ContentList::iterator i = _content.begin();
378         while (i != _content.end() && *i != c) {
379                 previous = i;
380                 ++i;
381         }
382
383         assert (i != _content.end ());
384         if (previous == _content.end ()) {
385                 return;
386         }
387         
388         DCPTime const p = (*previous)->position ();
389         (*previous)->set_position (p + c->length_after_trim ());
390         c->set_position (p);
391         sort (_content.begin(), _content.end(), ContentSorter ());
392         
393         Changed ();
394 }
395
396 void
397 Playlist::move_later (shared_ptr<Content> c)
398 {
399         sort (_content.begin(), _content.end(), ContentSorter ());
400         
401         ContentList::iterator i = _content.begin();
402         while (i != _content.end() && *i != c) {
403                 ++i;
404         }
405
406         assert (i != _content.end ());
407
408         ContentList::iterator next = i;
409         ++next;
410
411         if (next == _content.end ()) {
412                 return;
413         }
414
415         DCPTime const p = (*next)->position ();
416         (*next)->set_position (c->position ());
417         c->set_position (p + c->length_after_trim ());
418         sort (_content.begin(), _content.end(), ContentSorter ());
419         
420         Changed ();
421 }