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