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