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