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