Split audio; builds.
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-2016 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 "dcp_content.h"
21 #include "video_content.h"
22 #include "audio_content.h"
23 #include "dcp_examiner.h"
24 #include "job.h"
25 #include "film.h"
26 #include "config.h"
27 #include "overlaps.h"
28 #include "compose.hpp"
29 #include "dcp_decoder.h"
30 #include "subtitle_content.h"
31 #include <dcp/dcp.h>
32 #include <dcp/exceptions.h>
33 #include <dcp/reel_picture_asset.h>
34 #include <dcp/reel.h>
35 #include <libxml++/libxml++.h>
36 #include <boost/foreach.hpp>
37 #include <iterator>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 using std::string;
43 using std::cout;
44 using std::distance;
45 using std::pair;
46 using std::vector;
47 using std::list;
48 using boost::shared_ptr;
49 using boost::scoped_ptr;
50 using boost::optional;
51
52 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
53 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
54 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
55 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
56
57 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
58         : Content (film)
59         , _has_subtitles (false)
60         , _encrypted (false)
61         , _kdm_valid (false)
62         , _reference_video (false)
63         , _reference_audio (false)
64         , _reference_subtitle (false)
65 {
66         video.reset (new VideoContent (this, film));
67         audio.reset (new AudioContent (this, film));
68         subtitle.reset (new SubtitleContent (this, film));
69
70         read_directory (p);
71         set_default_colour_conversion ();
72 }
73
74 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
75         : Content (film, node)
76 {
77         video.reset (new VideoContent (this, film, node, version));
78         audio.reset (new AudioContent (this, film, node));
79         audio->set_stream (
80                 AudioStreamPtr (
81                         new AudioStream (node->number_child<int> ("AudioFrameRate"), AudioMapping (node->node_child ("AudioMapping"), version))
82                         )
83                 );
84         subtitle.reset (new SubtitleContent (this, film, node, version));
85
86         _name = node->string_child ("Name");
87         _has_subtitles = node->bool_child ("HasSubtitles");
88         _encrypted = node->bool_child ("Encrypted");
89         if (node->optional_node_child ("KDM")) {
90                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
91         }
92         _kdm_valid = node->bool_child ("KDMValid");
93         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
94         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
95         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
96 }
97
98 void
99 DCPContent::read_directory (boost::filesystem::path p)
100 {
101         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
102                 if (boost::filesystem::is_regular_file (i->path ())) {
103                         _paths.push_back (i->path ());
104                 } else if (boost::filesystem::is_directory (i->path ())) {
105                         read_directory (i->path ());
106                 }
107         }
108 }
109
110 void
111 DCPContent::examine (shared_ptr<Job> job)
112 {
113         bool const could_be_played = can_be_played ();
114
115         job->set_progress_unknown ();
116         Content::examine (job);
117
118         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
119         video->take_from_video_examiner (examiner);
120         set_default_colour_conversion ();
121
122         {
123                 boost::mutex::scoped_lock lm (_mutex);
124
125                 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_channels ()));
126                 audio->set_stream (as);
127                 AudioMapping m = as->mapping ();
128                 film()->make_audio_mapping_default (m);
129                 as->set_mapping (m);
130         }
131
132         signal_changed (AudioContentProperty::AUDIO_STREAMS);
133
134         {
135                 boost::mutex::scoped_lock lm (_mutex);
136                 _name = examiner->name ();
137                 _has_subtitles = examiner->has_subtitles ();
138                 _encrypted = examiner->encrypted ();
139                 _kdm_valid = examiner->kdm_valid ();
140         }
141
142         if (could_be_played != can_be_played ()) {
143                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
144         }
145 }
146
147 string
148 DCPContent::summary () const
149 {
150         boost::mutex::scoped_lock lm (_mutex);
151         return String::compose (_("%1 [DCP]"), _name);
152 }
153
154 string
155 DCPContent::technical_summary () const
156 {
157         return Content::technical_summary() + " - "
158                 + video->technical_summary() + " - "
159                 + audio->technical_summary() + " - ";
160 }
161
162 void
163 DCPContent::as_xml (xmlpp::Node* node) const
164 {
165         node->add_child("Type")->add_child_text ("DCP");
166
167         Content::as_xml (node);
168         video->as_xml (node);
169         audio->as_xml (node);
170         node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
171         audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
172         subtitle->as_xml (node);
173
174         boost::mutex::scoped_lock lm (_mutex);
175         node->add_child("Name")->add_child_text (_name);
176         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
177         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
178         if (_kdm) {
179                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
180         }
181         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
182         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
183         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
184         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
185 }
186
187 DCPTime
188 DCPContent::full_length () const
189 {
190         FrameRateChange const frc (video->video_frame_rate (), film()->video_frame_rate ());
191         return DCPTime::from_frames (llrint (video->video_length () * frc.factor ()), film()->video_frame_rate ());
192 }
193
194 string
195 DCPContent::identifier () const
196 {
197         SafeStringStream s;
198         s << Content::identifier() << "_" << video->identifier() << "_" << subtitle->identifier () << " "
199           << (_reference_video ? "1" : "0")
200           << (_reference_subtitle ? "1" : "0");
201         return s.str ();
202 }
203
204 void
205 DCPContent::add_kdm (dcp::EncryptedKDM k)
206 {
207         _kdm = k;
208 }
209
210 bool
211 DCPContent::can_be_played () const
212 {
213         boost::mutex::scoped_lock lm (_mutex);
214         return !_encrypted || _kdm_valid;
215 }
216
217 boost::filesystem::path
218 DCPContent::directory () const
219 {
220         optional<size_t> smallest;
221         boost::filesystem::path dir;
222         for (size_t i = 0; i < number_of_paths(); ++i) {
223                 boost::filesystem::path const p = path (i).parent_path ();
224                 size_t const d = distance (p.begin(), p.end());
225                 if (!smallest || d < smallest.get ()) {
226                         dir = p;
227                 }
228         }
229
230         return dir;
231 }
232
233 void
234 DCPContent::add_properties (list<UserProperty>& p) const
235 {
236         audio->add_properties (p);
237 }
238
239 void
240 DCPContent::set_default_colour_conversion ()
241 {
242         /* Default to no colour conversion for DCPs */
243         video->unset_colour_conversion ();
244 }
245
246 void
247 DCPContent::set_reference_video (bool r)
248 {
249         {
250                 boost::mutex::scoped_lock lm (_mutex);
251                 _reference_video = r;
252         }
253
254         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
255 }
256
257 void
258 DCPContent::set_reference_audio (bool r)
259 {
260         {
261                 boost::mutex::scoped_lock lm (_mutex);
262                 _reference_audio = r;
263         }
264
265         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
266 }
267
268 void
269 DCPContent::set_reference_subtitle (bool r)
270 {
271         {
272                 boost::mutex::scoped_lock lm (_mutex);
273                 _reference_subtitle = r;
274         }
275
276         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
277 }
278
279 list<DCPTimePeriod>
280 DCPContent::reels () const
281 {
282         list<DCPTimePeriod> p;
283         scoped_ptr<DCPDecoder> decoder;
284         try {
285                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
286         } catch (...) {
287                 /* Could not load the DCP; guess reels */
288                 list<DCPTimePeriod> p;
289                 p.push_back (DCPTimePeriod (position(), end()));
290                 return p;
291         }
292
293         DCPTime from = position ();
294         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
295                 DCPTime const to = from + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
296                 p.push_back (DCPTimePeriod (from, to));
297                 from = to;
298         }
299
300         return p;
301 }
302
303 list<DCPTime>
304 DCPContent::reel_split_points () const
305 {
306         list<DCPTime> s;
307         BOOST_FOREACH (DCPTimePeriod i, reels()) {
308                 s.push_back (i.from);
309         }
310         return s;
311 }
312
313 template <class T>
314 bool
315 DCPContent::can_reference (string overlapping, list<string>& why_not) const
316 {
317         list<DCPTimePeriod> const fr = film()->reels ();
318         /* fr must contain reels().  It can also contain other reels, but it must at
319            least contain reels().
320         */
321         BOOST_FOREACH (DCPTimePeriod i, reels()) {
322                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
323                         why_not.push_back (_("Reel lengths in the project differ from those in the DCP; set the reel mode to 'split by video content'."));
324                         return false;
325                 }
326         }
327
328         list<shared_ptr<T> > a = overlaps<T> (film()->content(), position(), end());
329         if (a.size() != 1 || a.front().get() != this) {
330                 why_not.push_back (overlapping);
331                 return false;
332         }
333
334         return true;
335 }
336
337 bool
338 DCPContent::can_reference_video (list<string>& why_not) const
339 {
340         /* XXX: this needs to be fixed */
341         return true;
342 }
343
344 bool
345 DCPContent::can_reference_audio (list<string>& why_not) const
346 {
347         /* XXX: this needs to be fixed */
348         return true;
349 }
350
351 bool
352 DCPContent::can_reference_subtitle (list<string>& why_not) const
353 {
354         /* XXX: this needs to be fixed */
355         return true;
356 }
357
358 void
359 DCPContent::changed (int property)
360 {
361         if (property == VideoContentProperty::VIDEO_FRAME_RATE && subtitle) {
362                 subtitle->set_subtitle_video_frame_rate (video->video_frame_rate ());
363         }
364 }