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