Support for keeping video in sequence when changing lengths; tie selection in timelin...
[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 <boost/lexical_cast.hpp>
23 #include "playlist.h"
24 #include "sndfile_content.h"
25 #include "sndfile_decoder.h"
26 #include "video_content.h"
27 #include "ffmpeg_decoder.h"
28 #include "ffmpeg_content.h"
29 #include "imagemagick_decoder.h"
30 #include "imagemagick_content.h"
31 #include "job.h"
32 #include "config.h"
33 #include "util.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 boost::optional;
45 using boost::shared_ptr;
46 using boost::weak_ptr;
47 using boost::dynamic_pointer_cast;
48 using boost::lexical_cast;
49
50 Playlist::Playlist ()
51         : _loop (1)
52         , _sequence_video (true)
53         , _sequencing_video (false)
54 {
55
56 }
57
58 Playlist::Playlist (shared_ptr<const Playlist> other)
59         : _loop (other->_loop)
60 {
61         for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
62                 _content.push_back ((*i)->clone ());
63         }
64 }
65
66 Playlist::~Playlist ()
67 {
68         _content.clear ();
69         reconnect ();
70 }
71
72 void
73 Playlist::content_changed (weak_ptr<Content> c, int p)
74 {
75         if (p == ContentProperty::LENGTH && _sequence_video && !_sequencing_video) {
76                 cout << "sequencing.\n";
77                 _sequencing_video = true;
78
79                 ContentList cl = _content;
80                 sort (cl.begin(), cl.end(), ContentSorter ());
81                 Time last = 0;
82                 for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
83                         if (!dynamic_pointer_cast<VideoContent> (*i)) {
84                                 continue;
85                         }
86
87                         (*i)->set_start (last);
88                         cout << (*i)->file() << " -> " << last << "\n";
89                         last = (*i)->end ();
90                 }
91
92                 _sequencing_video = false;
93         }
94         
95         ContentChanged (c, p);
96 }
97
98 string
99 Playlist::audio_digest () const
100 {
101         string t;
102         
103         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
104                 if (!dynamic_pointer_cast<const AudioContent> (*i)) {
105                         continue;
106                 }
107                 
108                 t += (*i)->digest ();
109
110                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
111                 if (fc) {
112                         t += lexical_cast<string> (fc->audio_stream()->id);
113                 }
114         }
115
116         t += lexical_cast<string> (_loop);
117
118         return md5_digest (t.c_str(), t.length());
119 }
120
121 string
122 Playlist::video_digest () const
123 {
124         string t;
125         
126         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
127                 if (!dynamic_pointer_cast<const VideoContent> (*i)) {
128                         continue;
129                 }
130                 
131                 t += (*i)->digest ();
132                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
133                 if (fc && fc->subtitle_stream()) {
134                         t += fc->subtitle_stream()->id;
135                 }
136         }
137
138         t += lexical_cast<string> (_loop);
139
140         return md5_digest (t.c_str(), t.length());
141 }
142
143 /** @param node <Playlist> node */
144 void
145 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
146 {
147         list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
148         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
149                 string const type = (*i)->string_child ("Type");
150
151                 boost::shared_ptr<Content> content;
152
153                 if (type == "FFmpeg") {
154                         content.reset (new FFmpegContent (film, *i));
155                 } else if (type == "ImageMagick") {
156                         content.reset (new ImageMagickContent (film, *i));
157                 } else if (type == "Sndfile") {
158                         content.reset (new SndfileContent (film, *i));
159                 }
160
161                 _content.push_back (content);
162         }
163
164         reconnect ();
165         _loop = node->number_child<int> ("Loop");
166         _sequence_video = node->bool_child ("SequenceVideo");
167 }
168
169 /** @param node <Playlist> node */
170 void
171 Playlist::as_xml (xmlpp::Node* node)
172 {
173         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
174                 (*i)->as_xml (node->add_child ("Content"));
175         }
176
177         node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
178         node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
179 }
180
181 void
182 Playlist::add (shared_ptr<Content> c)
183 {
184         _content.push_back (c);
185         reconnect ();
186         Changed ();
187 }
188
189 void
190 Playlist::remove (shared_ptr<Content> c)
191 {
192         ContentList::iterator i = _content.begin ();
193         while (i != _content.end() && *i != c) {
194                 ++i;
195         }
196         
197         if (i != _content.end ()) {
198                 _content.erase (i);
199                 Changed ();
200         }
201 }
202
203 void
204 Playlist::set_loop (int l)
205 {
206         _loop = l;
207         Changed ();
208 }
209
210 bool
211 Playlist::has_subtitles () const
212 {
213         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
214                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
215                 if (fc && !fc->subtitle_streams().empty()) {
216                         return true;
217                 }
218         }
219
220         return false;
221 }
222
223 class FrameRateCandidate
224 {
225 public:
226         FrameRateCandidate (float source_, int dcp_)
227                 : source (source_)
228                 , dcp (dcp_)
229         {}
230
231         float source;
232         int dcp;
233 };
234
235 int
236 Playlist::best_dcp_frame_rate () const
237 {
238         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
239
240         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
241         list<FrameRateCandidate> candidates;
242
243         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
244         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
245                 candidates.push_back (FrameRateCandidate (*i, *i));
246         }
247
248         /* Then the skip/repeat ones */
249         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
250                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
251                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
252         }
253
254         /* Pick the best one, bailing early if we hit an exact match */
255         float error = std::numeric_limits<float>::max ();
256         optional<FrameRateCandidate> best;
257         list<FrameRateCandidate>::iterator i = candidates.begin();
258         while (i != candidates.end()) {
259
260                 float this_error = std::numeric_limits<float>::max ();
261                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
262                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
263                         if (!vc) {
264                                 continue;
265                         }
266
267                         this_error += fabs (i->source - vc->video_frame_rate ());
268                 }
269
270                 if (this_error < error) {
271                         error = this_error;
272                         best = *i;
273                 }
274
275                 ++i;
276         }
277
278         if (!best) {
279                 return 24;
280         }
281         
282         return best->dcp;
283 }
284
285 Time
286 Playlist::length () const
287 {
288         Time len = 0;
289         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
290                 len = max (len, (*i)->end ());
291         }
292
293         return len;
294 }
295
296 void
297 Playlist::reconnect ()
298 {
299         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
300                 i->disconnect ();
301         }
302
303         _content_connections.clear ();
304                 
305         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
306                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
307         }
308 }
309
310 Time
311 Playlist::video_end () const
312 {
313         Time end = 0;
314         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
315                 if (dynamic_pointer_cast<const VideoContent> (*i)) {
316                         end = max (end, (*i)->end ());
317                 }
318         }
319
320         return end;
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->start() < b->start();
333 }