Various bits and pieces.
[dcpomatic.git] / src / lib / playlist.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include <libcxml/cxml.h>
23 #include <boost/shared_ptr.hpp>
24 #include <boost/lexical_cast.hpp>
25 #include "playlist.h"
26 #include "sndfile_content.h"
27 #include "sndfile_decoder.h"
28 #include "video_content.h"
29 #include "ffmpeg_decoder.h"
30 #include "ffmpeg_content.h"
31 #include "imagemagick_decoder.h"
32 #include "imagemagick_content.h"
33 #include "job.h"
34 #include "config.h"
35 #include "util.h"
36
37 #include "i18n.h"
38
39 using std::list;
40 using std::cout;
41 using std::vector;
42 using std::min;
43 using std::max;
44 using std::string;
45 using std::stringstream;
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         : _loop (1)
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         ContentChanged (c, p);
76 }
77
78 string
79 Playlist::audio_digest () const
80 {
81         string t;
82         
83         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
84                 if (!dynamic_pointer_cast<const AudioContent> (*i)) {
85                         continue;
86                 }
87                 
88                 t += (*i)->digest ();
89
90                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
91                 if (fc) {
92                         t += lexical_cast<string> (fc->audio_stream()->id);
93                 }
94         }
95
96         t += lexical_cast<string> (_loop);
97
98         return md5_digest (t.c_str(), t.length());
99 }
100
101 string
102 Playlist::video_digest () const
103 {
104         string t;
105         
106         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
107                 if (!dynamic_pointer_cast<const VideoContent> (*i)) {
108                         continue;
109                 }
110                 
111                 t += (*i)->digest ();
112                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
113                 if (fc && fc->subtitle_stream()) {
114                         t += fc->subtitle_stream()->id;
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 }
147
148 /** @param node <Playlist> node */
149 void
150 Playlist::as_xml (xmlpp::Node* node)
151 {
152         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
153                 (*i)->as_xml (node->add_child ("Content"));
154         }
155
156         node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
157 }
158
159 void
160 Playlist::add (shared_ptr<Content> c)
161 {
162         _content.push_back (c);
163         reconnect ();
164         Changed ();
165 }
166
167 void
168 Playlist::remove (shared_ptr<Content> c)
169 {
170         ContentList::iterator i = _content.begin ();
171         while (i != _content.end() && *i != c) {
172                 ++i;
173         }
174         
175         if (i != _content.end ()) {
176                 _content.erase (i);
177                 Changed ();
178         }
179 }
180
181 void
182 Playlist::set_loop (int l)
183 {
184         _loop = l;
185         Changed ();
186 }
187
188 bool
189 Playlist::has_subtitles () const
190 {
191         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
192                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
193                 if (fc && !fc->subtitle_streams().empty()) {
194                         return true;
195                 }
196         }
197
198         return false;
199 }
200
201 class FrameRateCandidate
202 {
203 public:
204         FrameRateCandidate (float source_, int dcp_)
205                 : source (source_)
206                 , dcp (dcp_)
207         {}
208
209         float source;
210         int dcp;
211 };
212
213 int
214 Playlist::best_dcp_frame_rate () const
215 {
216         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
217
218         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
219         list<FrameRateCandidate> candidates;
220
221         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
222         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
223                 candidates.push_back (FrameRateCandidate (*i, *i));
224         }
225
226         /* Then the skip/repeat ones */
227         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
228                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
229                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
230         }
231
232         /* Pick the best one, bailing early if we hit an exact match */
233         float error = std::numeric_limits<float>::max ();
234         optional<FrameRateCandidate> best;
235         list<FrameRateCandidate>::iterator i = candidates.begin();
236         while (i != candidates.end()) {
237
238                 float this_error = std::numeric_limits<float>::max ();
239                 for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
240                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
241                         if (!vc) {
242                                 continue;
243                         }
244
245                         this_error += fabs (i->source - vc->video_frame_rate ());
246                 }
247
248                 if (this_error < error) {
249                         error = this_error;
250                         best = *i;
251                 }
252
253                 ++i;
254         }
255
256         if (!best) {
257                 return 24;
258         }
259         
260         return best->dcp;
261 }
262
263 Time
264 Playlist::length () const
265 {
266         Time len = 0;
267         for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
268                 len = max (len, (*i)->end ());
269         }
270
271         return len;
272 }
273
274 void
275 Playlist::reconnect ()
276 {
277         for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
278                 i->disconnect ();
279         }
280
281         _content_connections.clear ();
282                 
283         for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
284                 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
285         }
286 }
287