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