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