Compiles; strange hang on adding content to a film.
[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 {
53
54 }
55
56 Playlist::Playlist (shared_ptr<const Playlist> other)
57         : _loop (other->_loop)
58 {
59         for (RegionList::const_iterator i = other->_regions.begin(); i != other->_regions.end(); ++i) {
60                 _regions.push_back (Region (i->content->clone(), i->time, this));
61         }
62 }
63
64 void
65 Playlist::content_changed (weak_ptr<Content> c, int p)
66 {
67         ContentChanged (c, p);
68 }
69
70 string
71 Playlist::audio_digest () const
72 {
73         string t;
74         
75         for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
76                 if (!dynamic_pointer_cast<const AudioContent> (i->content)) {
77                         continue;
78                 }
79                 
80                 t += i->content->digest ();
81
82                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
83                 if (fc) {
84                         t += lexical_cast<string> (fc->audio_stream()->id);
85                 }
86         }
87
88         t += lexical_cast<string> (_loop);
89
90         return md5_digest (t.c_str(), t.length());
91 }
92
93 string
94 Playlist::video_digest () const
95 {
96         string t;
97         
98         for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
99                 if (!dynamic_pointer_cast<const VideoContent> (i->content)) {
100                         continue;
101                 }
102                 
103                 t += i->content->digest ();
104                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
105                 if (fc && fc->subtitle_stream()) {
106                         t += fc->subtitle_stream()->id;
107                 }
108         }
109
110         t += lexical_cast<string> (_loop);
111
112         return md5_digest (t.c_str(), t.length());
113 }
114
115 void
116 Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
117 {
118         list<shared_ptr<cxml::Node> > c = node->node_children ("Region");
119         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
120                 _regions.push_back (Region (*i, this));
121         }
122
123         _loop = node->number_child<int> ("Loop");
124 }
125
126 void
127 Playlist::as_xml (xmlpp::Node* node)
128 {
129         for (RegionList::iterator i = _regions.begin(); i != _regions.end(); ++i) {
130                 i->as_xml (node->add_child ("Region"));
131         }
132
133         node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
134 }
135
136 void
137 Playlist::add (shared_ptr<Content> c)
138 {
139         _regions.push_back (Region (c, 0, this));
140         Changed ();
141 }
142
143 void
144 Playlist::remove (shared_ptr<Content> c)
145 {
146         RegionList::iterator i = _regions.begin ();
147         while (i != _regions.end() && i->content != c) {
148                 ++i;
149         }
150         
151         if (i != _regions.end ()) {
152                 _regions.erase (i);
153                 Changed ();
154         }
155 }
156
157 void
158 Playlist::set_loop (int l)
159 {
160         _loop = l;
161         Changed ();
162 }
163
164 bool
165 Playlist::has_subtitles () const
166 {
167         for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
168                 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (i->content);
169                 if (fc && !fc->subtitle_streams().empty()) {
170                         return true;
171                 }
172         }
173
174         return false;
175 }
176
177 Playlist::Region::Region (shared_ptr<Content> c, Time t, Playlist* p)
178         : content (c)
179         , time (t)
180 {
181         connection = c->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
182 }
183
184 Playlist::Region::Region (shared_ptr<const cxml::Node> node, Playlist* p)
185 {
186         shared_ptr<const cxml::Node> content_node = node->node_child ("Content");
187         string const type = content_node->string_child ("Type");
188
189         if (type == "FFmpeg") {
190                 content.reset (new FFmpegContent (content_node));
191         } else if (type == "ImageMagick") {
192                 content.reset (new ImageMagickContent (content_node));
193         } else if (type == "Sndfile") {
194                 content.reset (new SndfileContent (content_node));
195         }
196
197         time = node->number_child<Time> ("Time");
198         connection = content->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
199 }
200
201 void
202 Playlist::Region::as_xml (xmlpp::Node* node) const
203 {
204         xmlpp::Node* sub = node->add_child ("Content");
205         content->as_xml (sub);
206         sub->add_child ("Time")->add_child_text (lexical_cast<string> (time));
207 }
208
209 class FrameRateCandidate
210 {
211 public:
212         FrameRateCandidate (float source_, int dcp_)
213                 : source (source_)
214                 , dcp (dcp_)
215         {}
216
217         float source;
218         int dcp;
219 };
220
221 int
222 Playlist::best_dcp_frame_rate () const
223 {
224         list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
225
226         /* Work out what rates we could manage, including those achieved by using skip / repeat. */
227         list<FrameRateCandidate> candidates;
228
229         /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
230         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
231                 candidates.push_back (FrameRateCandidate (*i, *i));
232         }
233
234         /* Then the skip/repeat ones */
235         for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
236                 candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
237                 candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
238         }
239
240         /* Pick the best one, bailing early if we hit an exact match */
241         float error = std::numeric_limits<float>::max ();
242         optional<FrameRateCandidate> best;
243         list<FrameRateCandidate>::iterator i = candidates.begin();
244         while (i != candidates.end()) {
245
246                 float this_error = std::numeric_limits<float>::max ();
247                 for (RegionList::const_iterator j = _regions.begin(); j != _regions.end(); ++j) {
248                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j->content);
249                         if (!vc) {
250                                 continue;
251                         }
252
253                         this_error += fabs (i->source - vc->video_frame_rate ());
254                 }
255
256                 if (this_error < error) {
257                         error = this_error;
258                         best = *i;
259                 }
260
261                 ++i;
262         }
263
264         if (!best) {
265                 return 24;
266         }
267         
268         return best->dcp;
269 }
270
271 Time
272 Playlist::length (shared_ptr<const Film> film) const
273 {
274         Time len = 0;
275         for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
276                 Time const t = i->time + i->content->length (film);
277                 len = max (len, t);
278         }
279
280         return len;
281 }