Allow repeat of multiple stuff.
[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 std::pair;
46 using boost::optional;
47 using boost::shared_ptr;
48 using boost::weak_ptr;
49 using boost::dynamic_pointer_cast;
50 using boost::lexical_cast;
51
52 Playlist::Playlist ()
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         return md5_digest (t.c_str(), t.length());
113 }
114
115 /** @param node <Playlist> node */
116 void
117 Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
118 {
119         list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
120         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
121                 _content.push_back (content_factory (film, *i));
122         }
123
124         reconnect ();
125         _sequence_video = node->bool_child ("SequenceVideo");
126 }
127
128 /** @param node <Playlist> node */
129 void
130 Playlist::as_xml (xmlpp::Node* node)
131 {
132         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
133                 (*i)->as_xml (node->add_child ("Content"));
134         }
135
136         node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
137 }
138
139 void
140 Playlist::add (shared_ptr<Content> c)
141 {
142         _content.push_back (c);
143         reconnect ();
144         Changed ();
145 }
146
147 void
148 Playlist::remove (shared_ptr<Content> c)
149 {
150         ContentList::iterator i = _content.begin ();
151         while (i != _content.end() && *i != c) {
152                 ++i;
153         }
154         
155         if (i != _content.end ()) {
156                 _content.erase (i);
157                 Changed ();
158         }
159 }
160
161 bool
162 Playlist::has_subtitles () const
163 {
164         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
165                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
166                 if (fc && !fc->subtitle_streams().empty()) {
167                         return true;
168                 }
169         }
170
171         return false;
172 }
173
174 class FrameRateCandidate
175 {
176 public:
177         FrameRateCandidate (float source_, int dcp_)
178                 : source (source_)
179                 , dcp (dcp_)
180         {}
181
182         float source;
183         int dcp;
184 };
185
186 int
187 Playlist::best_dcp_frame_rate () const
188 {
189         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
190
191         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
192         list<FrameRateCandidate> candidates;
193
194         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
195         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
196                 candidates.push_back (FrameRateCandidate (*i, *i));
197         }
198
199         /* Then the skip/repeat ones */
200         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
201                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
202                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
203         }
204
205         /* Pick the best one */
206         float error = std::numeric_limits<float>::max ();
207         optional<FrameRateCandidate> best;
208         list<FrameRateCandidate>::iterator i = candidates.begin();
209         while (i != candidates.end()) {
210
211                 float this_error = 0;
212                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
213                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
214                         if (!vc) {
215                                 continue;
216                         }
217
218                         /* Use the largest difference between DCP and source as the "error" */
219                         this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
220                 }
221
222                 if (this_error < error) {
223                         error = this_error;
224                         best = *i;
225                 }
226
227                 ++i;
228         }
229
230         if (!best) {
231                 return 24;
232         }
233         
234         return best->dcp;
235 }
236
237 Time
238 Playlist::length () const
239 {
240         Time len = 0;
241         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
242                 len = max (len, (*i)->end ());
243         }
244
245         return len;
246 }
247
248 void
249 Playlist::reconnect ()
250 {
251         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
252                 i->disconnect ();
253         }
254
255         _content_connections.clear ();
256                 
257         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
258                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
259         }
260 }
261
262 Time
263 Playlist::video_end () const
264 {
265         Time end = 0;
266         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
267                 if (dynamic_pointer_cast<const VideoContent> (*i)) {
268                         end = max (end, (*i)->end ());
269                 }
270         }
271
272         return end;
273 }
274
275 void
276 Playlist::set_sequence_video (bool s)
277 {
278         _sequence_video = s;
279 }
280
281 bool
282 ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
283 {
284         return a->start() < b->start();
285 }
286
287 /** @return content in an undefined order */
288 Playlist::ContentList
289 Playlist::content () const
290 {
291         return _content;
292 }
293
294 void
295 Playlist::repeat (list<shared_ptr<Content> > c, int n)
296 {
297         pair<Time, Time> range (TIME_MAX, 0);
298         for (list<shared_ptr<Content> >::iterator i = c.begin(); i != c.end(); ++i) {
299                 range.first = min (range.first, (*i)->start ());
300                 range.second = max (range.second, (*i)->start ());
301                 range.first = min (range.first, (*i)->end ());
302                 range.second = max (range.second, (*i)->end ());
303         }
304
305         Time pos = range.second;
306         for (int i = 0; i < n; ++i) {
307                 for (list<shared_ptr<Content> >::iterator i = c.begin(); i != c.end(); ++i) {
308                         shared_ptr<Content> copy = (*i)->clone ();
309                         copy->set_start (pos + copy->start() - range.first);
310                         _content.push_back (copy);
311                 }
312                 pos += range.second - range.first;
313         }
314
315         reconnect ();
316         Changed ();
317 }