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