Minor grammatical tweak.
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "dcp_content.h"
22 #include "video_content.h"
23 #include "audio_content.h"
24 #include "dcp_examiner.h"
25 #include "job.h"
26 #include "film.h"
27 #include "config.h"
28 #include "overlaps.h"
29 #include "compose.hpp"
30 #include "dcp_decoder.h"
31 #include "subtitle_content.h"
32 #include <dcp/dcp.h>
33 #include <dcp/raw_convert.h>
34 #include <dcp/exceptions.h>
35 #include <dcp/reel_picture_asset.h>
36 #include <dcp/reel.h>
37 #include <libxml++/libxml++.h>
38 #include <boost/foreach.hpp>
39 #include <iterator>
40 #include <iostream>
41
42 #include "i18n.h"
43
44 using std::string;
45 using std::cout;
46 using std::distance;
47 using std::pair;
48 using std::vector;
49 using std::list;
50 using boost::shared_ptr;
51 using boost::scoped_ptr;
52 using boost::optional;
53 using boost::function;
54 using dcp::raw_convert;
55
56 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
57 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
58 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
59 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
60
61 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
62         : Content (film)
63         , _encrypted (false)
64         , _kdm_valid (false)
65         , _reference_video (false)
66         , _reference_audio (false)
67         , _reference_subtitle (false)
68         , _three_d (false)
69 {
70         video.reset (new VideoContent (this));
71         audio.reset (new AudioContent (this));
72
73         read_directory (p);
74         set_default_colour_conversion ();
75 }
76
77 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
78         : Content (film, node)
79 {
80         video = VideoContent::from_xml (this, node, version);
81         audio = AudioContent::from_xml (this, node, version);
82         subtitle = SubtitleContent::from_xml (this, node, version);
83
84         audio->set_stream (
85                 AudioStreamPtr (
86                         new AudioStream (
87                                 node->number_child<int> ("AudioFrameRate"),
88                                 /* AudioLength was not present in some old metadata versions */
89                                 node->optional_number_child<Frame>("AudioLength").get_value_or (
90                                         video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
91                                         ),
92                                 AudioMapping (node->node_child ("AudioMapping"), version)
93                                 )
94                         )
95                 );
96
97         _name = node->string_child ("Name");
98
99         _encrypted = node->bool_child ("Encrypted");
100         if (node->optional_node_child ("KDM")) {
101                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
102         }
103         _kdm_valid = node->bool_child ("KDMValid");
104         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
105         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
106         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
107         if (node->optional_string_child("Standard")) {
108                 string const s = node->optional_string_child("Standard").get();
109                 if (s == "Interop") {
110                         _standard = dcp::INTEROP;
111                 } else if (s == "SMPTE") {
112                         _standard = dcp::SMPTE;
113                 } else {
114                         DCPOMATIC_ASSERT (false);
115                 }
116         }
117         _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
118 }
119
120 void
121 DCPContent::read_directory (boost::filesystem::path p)
122 {
123         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
124                 if (boost::filesystem::is_regular_file (i->path ())) {
125                         _paths.push_back (i->path ());
126                 } else if (boost::filesystem::is_directory (i->path ())) {
127                         read_directory (i->path ());
128                 }
129         }
130 }
131
132 void
133 DCPContent::examine (shared_ptr<Job> job)
134 {
135         bool const could_be_played = can_be_played ();
136
137         job->set_progress_unknown ();
138         Content::examine (job);
139
140         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
141         video->take_from_examiner (examiner);
142         set_default_colour_conversion ();
143
144         {
145                 boost::mutex::scoped_lock lm (_mutex);
146
147                 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
148                 audio->set_stream (as);
149                 AudioMapping m = as->mapping ();
150                 film()->make_audio_mapping_default (m);
151                 as->set_mapping (m);
152         }
153
154         signal_changed (AudioContentProperty::STREAMS);
155
156         {
157                 boost::mutex::scoped_lock lm (_mutex);
158                 _name = examiner->name ();
159                 if (examiner->has_subtitles ()) {
160                         subtitle.reset (new SubtitleContent (this));
161                 }
162                 _encrypted = examiner->encrypted ();
163                 _kdm_valid = examiner->kdm_valid ();
164                 _standard = examiner->standard ();
165                 _three_d = examiner->three_d ();
166         }
167
168         if (could_be_played != can_be_played ()) {
169                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
170         }
171
172         video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
173 }
174
175 string
176 DCPContent::summary () const
177 {
178         boost::mutex::scoped_lock lm (_mutex);
179         return String::compose (_("%1 [DCP]"), _name);
180 }
181
182 string
183 DCPContent::technical_summary () const
184 {
185         return Content::technical_summary() + " - "
186                 + video->technical_summary() + " - "
187                 + audio->technical_summary() + " - ";
188 }
189
190 void
191 DCPContent::as_xml (xmlpp::Node* node) const
192 {
193         node->add_child("Type")->add_child_text ("DCP");
194
195         Content::as_xml (node);
196
197         if (video) {
198                 video->as_xml (node);
199         }
200
201         if (audio) {
202                 audio->as_xml (node);
203                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
204                 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
205                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
206         }
207
208         if (subtitle) {
209                 subtitle->as_xml (node);
210         }
211
212         boost::mutex::scoped_lock lm (_mutex);
213         node->add_child("Name")->add_child_text (_name);
214         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
215         if (_kdm) {
216                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
217         }
218         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
219         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
220         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
221         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
222         if (_standard) {
223                 switch (_standard.get ()) {
224                 case dcp::INTEROP:
225                         node->add_child("Standard")->add_child_text ("Interop");
226                         break;
227                 case dcp::SMPTE:
228                         node->add_child("Standard")->add_child_text ("SMPTE");
229                         break;
230                 default:
231                         DCPOMATIC_ASSERT (false);
232                 }
233         }
234         node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
235 }
236
237 DCPTime
238 DCPContent::full_length () const
239 {
240         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
241         return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
242 }
243
244 string
245 DCPContent::identifier () const
246 {
247         string s = Content::identifier() + "_" + video->identifier() + "_";
248         if (subtitle) {
249                 s += subtitle->identifier () + " ";
250         }
251
252         s += string (_reference_video ? "1" : "0") + string (_reference_subtitle ? "1" : "0");
253         return s;
254 }
255
256 void
257 DCPContent::add_kdm (dcp::EncryptedKDM k)
258 {
259         _kdm = k;
260 }
261
262 bool
263 DCPContent::can_be_played () const
264 {
265         boost::mutex::scoped_lock lm (_mutex);
266         return !_encrypted || _kdm_valid;
267 }
268
269 boost::filesystem::path
270 DCPContent::directory () const
271 {
272         optional<size_t> smallest;
273         boost::filesystem::path dir;
274         for (size_t i = 0; i < number_of_paths(); ++i) {
275                 boost::filesystem::path const p = path (i).parent_path ();
276                 size_t const d = distance (p.begin(), p.end());
277                 if (!smallest || d < smallest.get ()) {
278                         dir = p;
279                 }
280         }
281
282         return dir;
283 }
284
285 void
286 DCPContent::add_properties (list<UserProperty>& p) const
287 {
288         Content::add_properties (p);
289         video->add_properties (p);
290         audio->add_properties (p);
291 }
292
293 void
294 DCPContent::set_default_colour_conversion ()
295 {
296         /* Default to no colour conversion for DCPs */
297         video->unset_colour_conversion ();
298 }
299
300 void
301 DCPContent::set_reference_video (bool r)
302 {
303         {
304                 boost::mutex::scoped_lock lm (_mutex);
305                 _reference_video = r;
306         }
307
308         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
309 }
310
311 void
312 DCPContent::set_reference_audio (bool r)
313 {
314         {
315                 boost::mutex::scoped_lock lm (_mutex);
316                 _reference_audio = r;
317         }
318
319         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
320 }
321
322 void
323 DCPContent::set_reference_subtitle (bool r)
324 {
325         {
326                 boost::mutex::scoped_lock lm (_mutex);
327                 _reference_subtitle = r;
328         }
329
330         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
331 }
332
333 list<DCPTimePeriod>
334 DCPContent::reels () const
335 {
336         list<DCPTimePeriod> p;
337         scoped_ptr<DCPDecoder> decoder;
338         try {
339                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log()));
340         } catch (...) {
341                 /* Could not load the DCP; guess reels */
342                 list<DCPTimePeriod> p;
343                 p.push_back (DCPTimePeriod (position(), end()));
344                 return p;
345         }
346
347         DCPTime from = position ();
348         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
349                 DCPTime const to = from + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
350                 p.push_back (DCPTimePeriod (from, to));
351                 from = to;
352         }
353
354         return p;
355 }
356
357 list<DCPTime>
358 DCPContent::reel_split_points () const
359 {
360         list<DCPTime> s;
361         BOOST_FOREACH (DCPTimePeriod i, reels()) {
362                 s.push_back (i.from);
363         }
364         return s;
365 }
366
367 bool
368 DCPContent::can_reference (function<shared_ptr<ContentPart> (shared_ptr<const Content>)> part, string overlapping, list<string>& why_not) const
369 {
370         /* We must be using the same standard as the film */
371         if (_standard) {
372                 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
373                         why_not.push_back (_("The film is set to SMPTE and this DCP is Interop."));
374                         return false;
375                 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
376                         why_not.push_back (_("The film is set to Interop and this DCP is SMPTE."));
377                         return false;
378                 }
379         }
380
381         list<DCPTimePeriod> const fr = film()->reels ();
382         /* fr must contain reels().  It can also contain other reels, but it must at
383            least contain reels().
384         */
385         BOOST_FOREACH (DCPTimePeriod i, reels()) {
386                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
387                         why_not.push_back (_("The reel lengths in the film differ from those in the DCP; set the reel mode to 'split by video content'."));
388                         return false;
389                 }
390         }
391
392         ContentList a = overlaps (film()->content(), part, position(), end());
393         if (a.size() != 1 || a.front().get() != this) {
394                 why_not.push_back (overlapping);
395                 return false;
396         }
397
398         return true;
399 }
400
401 bool
402 DCPContent::can_reference_video (list<string>& why_not) const
403 {
404         return can_reference (bind (&Content::video, _1), _("There is other video content overlapping this DCP; remove it."), why_not);
405 }
406
407 bool
408 DCPContent::can_reference_audio (list<string>& why_not) const
409 {
410         DCPDecoder decoder (shared_from_this(), film()->log());
411         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
412                 if (!i->main_sound()) {
413                         why_not.push_back (_("The DCP does not have sound in all reels."));
414                         return false;
415                 }
416         }
417
418         return can_reference (bind (&Content::audio, _1),   _("There is other audio content overlapping this DCP; remove it."), why_not);
419 }
420
421 bool
422 DCPContent::can_reference_subtitle (list<string>& why_not) const
423 {
424         DCPDecoder decoder (shared_from_this(), film()->log());
425         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
426                 if (!i->main_subtitle()) {
427                         why_not.push_back (_("The DCP does not have subtitles in all reels."));
428                         return false;
429                 }
430         }
431
432         return can_reference (bind (&Content::subtitle, _1), _("There is other subtitle content overlapping this DCP; remove it."), why_not);
433 }