Merge.
[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) {
76                 maybe_sequence_video ();
77         }
78 <<<<<<< HEAD
79         
80         ContentChanged (c, p);
81 }
82
83 =======
84
85         ContentChanged (c, p);
86 }
87
88 >>>>>>> 1ea6a456bc2b4a695f6db4720353c35167597b30
89 void
90 Playlist::maybe_sequence_video ()
91 {
92         if (!_sequence_video || _sequencing_video) {
93                 return;
94 <<<<<<< HEAD
95         }
96         
97         _sequencing_video = true;
98         
99         ContentList cl = _content;
100         sort (cl.begin(), cl.end(), ContentSorter ());
101         Time last = 0;
102         for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
103                 if (!dynamic_pointer_cast<VideoContent> (*i)) {
104                         continue;
105                 }
106                 
107                 (*i)->set_start (last);
108                 last = (*i)->end ();
109         }
110         
111 =======
112         }
113         
114         _sequencing_video = true;
115         
116         ContentList cl = _content;
117         sort (cl.begin(), cl.end(), ContentSorter ());
118         Time last = 0;
119         for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
120                 if (!dynamic_pointer_cast<VideoContent> (*i)) {
121                         continue;
122                 }
123                 
124                 (*i)->set_start (last);
125                 last = (*i)->end ();
126         }
127         
128 >>>>>>> 1ea6a456bc2b4a695f6db4720353c35167597b30
129         _sequencing_video = false;
130 }
131
132 string
133 Playlist::video_identifier () const
134 {
135         string t;
136         
137         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
138                 shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
139                 if (vc) {
140                         t += vc->identifier ();
141                 }
142         }
143
144         t += lexical_cast<string> (_loop);
145
146         return md5_digest (t.c_str(), t.length());
147 }
148
149 /** @param node <Playlist> node */
150 void
151 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
152 {
153         list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
154         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
155                 string const type = (*i)->string_child ("Type");
156
157                 boost::shared_ptr<Content> content;
158
159                 if (type == "FFmpeg") {
160                         content.reset (new FFmpegContent (film, *i));
161                 } else if (type == "ImageMagick") {
162                         content.reset (new ImageMagickContent (film, *i));
163                 } else if (type == "Sndfile") {
164                         content.reset (new SndfileContent (film, *i));
165                 }
166
167                 _content.push_back (content);
168         }
169
170         reconnect ();
171         _loop = node->number_child<int> ("Loop");
172         _sequence_video = node->bool_child ("SequenceVideo");
173 }
174
175 /** @param node <Playlist> node */
176 void
177 Playlist::as_xml (xmlpp::Node* node)
178 {
179         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
180                 (*i)->as_xml (node->add_child ("Content"));
181         }
182
183         node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
184         node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
185 }
186
187 void
188 Playlist::add (shared_ptr<Content> c)
189 {
190         _content.push_back (c);
191         reconnect ();
192         Changed ();
193 }
194
195 void
196 Playlist::remove (shared_ptr<Content> c)
197 {
198         ContentList::iterator i = _content.begin ();
199         while (i != _content.end() && *i != c) {
200                 ++i;
201         }
202         
203         if (i != _content.end ()) {
204                 _content.erase (i);
205                 Changed ();
206         }
207 }
208
209 void
210 Playlist::set_loop (int l)
211 {
212         _loop = l;
213         Changed ();
214 }
215
216 bool
217 Playlist::has_subtitles () const
218 {
219         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
220                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
221                 if (fc && !fc->subtitle_streams().empty()) {
222                         return true;
223                 }
224         }
225
226         return false;
227 }
228
229 class FrameRateCandidate
230 {
231 public:
232         FrameRateCandidate (float source_, int dcp_)
233                 : source (source_)
234                 , dcp (dcp_)
235         {}
236
237         float source;
238         int dcp;
239 };
240
241 int
242 Playlist::best_dcp_frame_rate () const
243 {
244         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
245
246         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
247         list<FrameRateCandidate> candidates;
248
249         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
250         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
251                 candidates.push_back (FrameRateCandidate (*i, *i));
252         }
253
254         /* Then the skip/repeat ones */
255         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
256                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
257                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
258         }
259
260         /* Pick the best one */
261         float error = std::numeric_limits<float>::max ();
262         optional<FrameRateCandidate> best;
263         list<FrameRateCandidate>::iterator i = candidates.begin();
264         while (i != candidates.end()) {
265
266                 float this_error = 0;
267                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
268                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
269                         if (!vc) {
270                                 continue;
271                         }
272
273                         /* Use the largest difference between DCP and source as the "error" */
274                         this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
275                 }
276
277                 if (this_error < error) {
278                         error = this_error;
279                         best = *i;
280                 }
281
282                 ++i;
283         }
284
285         if (!best) {
286                 return 24;
287         }
288         
289         return best->dcp;
290 }
291
292 Time
293 Playlist::length () const
294 {
295         Time len = 0;
296         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
297                 len = max (len, (*i)->end ());
298         }
299
300         return len;
301 }
302
303 void
304 Playlist::reconnect ()
305 {
306         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
307                 i->disconnect ();
308         }
309
310         _content_connections.clear ();
311                 
312         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
313                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
314         }
315 }
316
317 Time
318 Playlist::video_end () const
319 {
320         Time end = 0;
321         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
322                 if (dynamic_pointer_cast<const VideoContent> (*i)) {
323                         end = max (end, (*i)->end ());
324                 }
325         }
326
327         return end;
328 }
329
330 void
331 Playlist::set_sequence_video (bool s)
332 {
333         _sequence_video = s;
334 }
335
336 bool
337 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
338 {
339         return a->start() < b->start();
340 }