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