White space tweaks.
[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 boost::dynamic_pointer_cast;
55 using dcp::raw_convert;
56
57 int const DCPContentProperty::NEEDS_ASSETS       = 600;
58 int const DCPContentProperty::NEEDS_KDM          = 601;
59 int const DCPContentProperty::REFERENCE_VIDEO    = 602;
60 int const DCPContentProperty::REFERENCE_AUDIO    = 603;
61 int const DCPContentProperty::REFERENCE_SUBTITLE = 604;
62 int const DCPContentProperty::NAME               = 605;
63 int const DCPContentProperty::HAS_SUBTITLES      = 606;
64
65 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
66         : Content (film)
67         , _encrypted (false)
68         , _needs_assets (false)
69         , _kdm_valid (false)
70         , _reference_video (false)
71         , _reference_audio (false)
72         , _reference_subtitle (false)
73         , _three_d (false)
74 {
75         video.reset (new VideoContent (this));
76         audio.reset (new AudioContent (this));
77
78         read_directory (p);
79         set_default_colour_conversion ();
80 }
81
82 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
83         : Content (film, node)
84 {
85         video = VideoContent::from_xml (this, node, version);
86         audio = AudioContent::from_xml (this, node, version);
87         subtitle = SubtitleContent::from_xml (this, node, version);
88
89         audio->set_stream (
90                 AudioStreamPtr (
91                         new AudioStream (
92                                 node->number_child<int> ("AudioFrameRate"),
93                                 /* AudioLength was not present in some old metadata versions */
94                                 node->optional_number_child<Frame>("AudioLength").get_value_or (
95                                         video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
96                                         ),
97                                 AudioMapping (node->node_child ("AudioMapping"), version)
98                                 )
99                         )
100                 );
101
102         _name = node->string_child ("Name");
103         _encrypted = node->bool_child ("Encrypted");
104         _needs_assets = node->optional_bool_child("NeedsAssets").get_value_or (false);
105         if (node->optional_node_child ("KDM")) {
106                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
107         }
108         _kdm_valid = node->bool_child ("KDMValid");
109         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
110         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
111         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
112         if (node->optional_string_child("Standard")) {
113                 string const s = node->optional_string_child("Standard").get();
114                 if (s == "Interop") {
115                         _standard = dcp::INTEROP;
116                 } else if (s == "SMPTE") {
117                         _standard = dcp::SMPTE;
118                 } else {
119                         DCPOMATIC_ASSERT (false);
120                 }
121         }
122         _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
123         _cpl = node->optional_string_child("CPL");
124         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("ReelLength")) {
125                 _reel_lengths.push_back (raw_convert<int64_t> (i->content ()));
126         }
127 }
128
129 void
130 DCPContent::read_directory (boost::filesystem::path p)
131 {
132         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
133                 if (boost::filesystem::is_regular_file (i->path())) {
134                         _paths.push_back (i->path());
135                 } else if (boost::filesystem::is_directory (i->path ())) {
136                         read_directory (i->path());
137                 }
138         }
139 }
140
141 void
142 DCPContent::examine (shared_ptr<Job> job)
143 {
144         bool const needed_assets = needs_assets ();
145         bool const needed_kdm = needs_kdm ();
146         string const old_name = name ();
147         bool had_subtitles = static_cast<bool> (subtitle);
148
149         job->set_progress_unknown ();
150         Content::examine (job);
151
152         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
153         video->take_from_examiner (examiner);
154         set_default_colour_conversion ();
155
156         {
157                 boost::mutex::scoped_lock lm (_mutex);
158
159                 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
160                 audio->set_stream (as);
161                 AudioMapping m = as->mapping ();
162                 film()->make_audio_mapping_default (m);
163                 as->set_mapping (m);
164         }
165
166         signal_changed (AudioContentProperty::STREAMS);
167
168         bool has_subtitles = false;
169         {
170                 boost::mutex::scoped_lock lm (_mutex);
171                 _name = examiner->name ();
172                 if (examiner->has_subtitles ()) {
173                         subtitle.reset (new SubtitleContent (this));
174                 } else {
175                         subtitle.reset ();
176                 }
177                 has_subtitles = static_cast<bool> (subtitle);
178                 _encrypted = examiner->encrypted ();
179                 _needs_assets = examiner->needs_assets ();
180                 _kdm_valid = examiner->kdm_valid ();
181                 _standard = examiner->standard ();
182                 _three_d = examiner->three_d ();
183                 _cpl = examiner->cpl ();
184                 _reel_lengths = examiner->reel_lengths ();
185         }
186
187         if (had_subtitles != has_subtitles) {
188                 signal_changed (DCPContentProperty::HAS_SUBTITLES);
189         }
190
191         if (needed_assets != needs_assets ()) {
192                 signal_changed (DCPContentProperty::NEEDS_ASSETS);
193         }
194
195         if (needed_kdm != needs_kdm ()) {
196                 signal_changed (DCPContentProperty::NEEDS_KDM);
197         }
198
199         if (old_name != name ()) {
200                 signal_changed (DCPContentProperty::NAME);
201         }
202
203         video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
204 }
205
206 string
207 DCPContent::summary () const
208 {
209         boost::mutex::scoped_lock lm (_mutex);
210         return String::compose (_("%1 [DCP]"), _name);
211 }
212
213 string
214 DCPContent::technical_summary () const
215 {
216         return Content::technical_summary() + " - "
217                 + video->technical_summary() + " - "
218                 + audio->technical_summary() + " - ";
219 }
220
221 void
222 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
223 {
224         node->add_child("Type")->add_child_text ("DCP");
225
226         Content::as_xml (node, with_paths);
227
228         if (video) {
229                 video->as_xml (node);
230         }
231
232         if (audio) {
233                 audio->as_xml (node);
234                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
235                 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
236                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
237         }
238
239         if (subtitle) {
240                 subtitle->as_xml (node);
241         }
242
243         boost::mutex::scoped_lock lm (_mutex);
244         node->add_child("Name")->add_child_text (_name);
245         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
246         node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
247         if (_kdm) {
248                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
249         }
250         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
251         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
252         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
253         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
254         if (_standard) {
255                 switch (_standard.get ()) {
256                 case dcp::INTEROP:
257                         node->add_child("Standard")->add_child_text ("Interop");
258                         break;
259                 case dcp::SMPTE:
260                         node->add_child("Standard")->add_child_text ("SMPTE");
261                         break;
262                 default:
263                         DCPOMATIC_ASSERT (false);
264                 }
265         }
266         node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
267         if (_cpl) {
268                 node->add_child("CPL")->add_child_text (_cpl.get ());
269         }
270         BOOST_FOREACH (int64_t i, _reel_lengths) {
271                 node->add_child("ReelLength")->add_child_text (raw_convert<string> (i));
272         }
273 }
274
275 DCPTime
276 DCPContent::full_length () const
277 {
278         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
279         return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
280 }
281
282 string
283 DCPContent::identifier () const
284 {
285         string s = Content::identifier() + "_" + video->identifier() + "_";
286         if (subtitle) {
287                 s += subtitle->identifier () + " ";
288         }
289
290         s += string (_reference_video ? "1" : "0") + string (_reference_subtitle ? "1" : "0");
291         return s;
292 }
293
294 void
295 DCPContent::add_kdm (dcp::EncryptedKDM k)
296 {
297         _kdm = k;
298 }
299
300 void
301 DCPContent::add_ov (boost::filesystem::path ov)
302 {
303         read_directory (ov);
304 }
305
306 bool
307 DCPContent::can_be_played () const
308 {
309         return !needs_kdm() && !needs_assets();
310 }
311
312 bool
313 DCPContent::needs_kdm () const
314 {
315         boost::mutex::scoped_lock lm (_mutex);
316         return _encrypted && !_kdm_valid;
317 }
318
319 bool
320 DCPContent::needs_assets () const
321 {
322         boost::mutex::scoped_lock lm (_mutex);
323         return _needs_assets;
324 }
325
326 vector<boost::filesystem::path>
327 DCPContent::directories () const
328 {
329         return dcp::DCP::directories_from_files (paths());
330 }
331
332 void
333 DCPContent::add_properties (list<UserProperty>& p) const
334 {
335         Content::add_properties (p);
336         video->add_properties (p);
337         audio->add_properties (p);
338 }
339
340 void
341 DCPContent::set_default_colour_conversion ()
342 {
343         /* Default to no colour conversion for DCPs */
344         video->unset_colour_conversion ();
345 }
346
347 void
348 DCPContent::set_reference_video (bool r)
349 {
350         {
351                 boost::mutex::scoped_lock lm (_mutex);
352                 _reference_video = r;
353         }
354
355         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
356 }
357
358 void
359 DCPContent::set_reference_audio (bool r)
360 {
361         {
362                 boost::mutex::scoped_lock lm (_mutex);
363                 _reference_audio = r;
364         }
365
366         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
367 }
368
369 void
370 DCPContent::set_reference_subtitle (bool r)
371 {
372         {
373                 boost::mutex::scoped_lock lm (_mutex);
374                 _reference_subtitle = r;
375         }
376
377         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
378 }
379
380 list<DCPTimePeriod>
381 DCPContent::reels () const
382 {
383         list<int64_t> reel_lengths = _reel_lengths;
384         if (reel_lengths.empty ()) {
385                 /* Old metadata with no reel lengths; get them here instead */
386                 try {
387                         scoped_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this()));
388                         reel_lengths = examiner->reel_lengths ();
389                 } catch (...) {
390                         /* Could not examine the DCP; guess reels */
391                         reel_lengths.push_back (length_after_trim().frames_round (film()->video_frame_rate ()));
392                 }
393         }
394
395         list<DCPTimePeriod> p;
396
397         /* This content's frame rate must be the same as the output DCP rate, so we can
398            convert `directly' from ContentTime to DCPTime.
399         */
400
401         /* The starting point of this content on the timeline */
402         DCPTime pos = position() - DCPTime (trim_start().get());
403
404         BOOST_FOREACH (int64_t i, reel_lengths) {
405                 /* This reel runs from `pos' to `to' */
406                 DCPTime const to = pos + DCPTime::from_frames (i, film()->video_frame_rate());
407                 if (to > position()) {
408                         p.push_back (DCPTimePeriod (max(position(), pos), min(end(), to)));
409                         if (to > end()) {
410                                 break;
411                         }
412                 }
413                 pos = to;
414         }
415
416         return p;
417 }
418
419 list<DCPTime>
420 DCPContent::reel_split_points () const
421 {
422         list<DCPTime> s;
423         BOOST_FOREACH (DCPTimePeriod i, reels()) {
424                 s.push_back (i.from);
425         }
426         return s;
427 }
428
429 bool
430 DCPContent::can_reference (function<shared_ptr<ContentPart> (shared_ptr<const Content>)> part, string overlapping, list<string>& why_not) const
431 {
432         /* We must be using the same standard as the film */
433         if (_standard) {
434                 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
435                         why_not.push_back (_("The film is set to SMPTE and this DCP is Interop."));
436                         return false;
437                 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
438                         why_not.push_back (_("The film is set to Interop and this DCP is SMPTE."));
439                         return false;
440                 }
441         }
442
443         /* And the same frame rate */
444         if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film()->video_frame_rate())) {
445                 why_not.push_back (_("The film has a different frame rate to this DCP."));
446                 return false;
447         }
448
449         list<DCPTimePeriod> const fr = film()->reels ();
450
451         list<DCPTimePeriod> reel_list;
452         try {
453                 reel_list = reels ();
454         } catch (dcp::DCPReadError) {
455                 /* We couldn't read the DCP; it's probably missing */
456                 return false;
457         }
458
459         /* fr must contain reels().  It can also contain other reels, but it must at
460            least contain reels().
461         */
462         BOOST_FOREACH (DCPTimePeriod i, reel_list) {
463                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
464                         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'."));
465                         return false;
466                 }
467         }
468
469         ContentList a = overlaps (film()->content(), part, position(), end());
470         if (a.size() != 1 || a.front().get() != this) {
471                 why_not.push_back (overlapping);
472                 return false;
473         }
474
475         return true;
476 }
477
478 bool
479 DCPContent::can_reference_video (list<string>& why_not) const
480 {
481         if (film()->frame_size() != video->size()) {
482                 why_not.push_back (_("The video frame size in the film differs from that in the DCP."));
483                 return false;
484         }
485
486         return can_reference (bind (&Content::video, _1), _("There is other video content overlapping this DCP; remove it."), why_not);
487 }
488
489 bool
490 DCPContent::can_reference_audio (list<string>& why_not) const
491 {
492         shared_ptr<DCPDecoder> decoder;
493         try {
494                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log()));
495         } catch (dcp::DCPReadError) {
496                 /* We couldn't read the DCP, so it's probably missing */
497                 return false;
498         }
499
500         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
501                 if (!i->main_sound()) {
502                         why_not.push_back (_("The DCP does not have sound in all reels."));
503                         return false;
504                 }
505         }
506
507         return can_reference (bind (&Content::audio, _1), _("There is other audio content overlapping this DCP; remove it."), why_not);
508 }
509
510 bool
511 DCPContent::can_reference_subtitle (list<string>& why_not) const
512 {
513         shared_ptr<DCPDecoder> decoder;
514         try {
515                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log()));
516         } catch (dcp::DCPReadError) {
517                 /* We couldn't read the DCP, so it's probably missing */
518                 return false;
519         }
520
521         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
522                 if (!i->main_subtitle()) {
523                         why_not.push_back (_("The DCP does not have subtitles in all reels."));
524                         return false;
525                 }
526         }
527
528         return can_reference (bind (&Content::subtitle, _1), _("There is other subtitle content overlapping this DCP; remove it."), why_not);
529 }
530
531 void
532 DCPContent::use_template (shared_ptr<const Content> c)
533 {
534         shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (c);
535         DCPOMATIC_ASSERT (dc);
536
537         _reference_video = dc->_reference_video;
538         _reference_audio = dc->_reference_audio;
539         _reference_subtitle = dc->_reference_subtitle;
540 }
541
542 void
543 DCPContent::set_cpl (string id)
544 {
545         boost::mutex::scoped_lock lm (_mutex);
546         _cpl = id;
547 }