Merge branch '2389-vpos'
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-2022 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
22 #include "atmos_content.h"
23 #include "audio_content.h"
24 #include "compose.hpp"
25 #include "config.h"
26 #include "dcp_content.h"
27 #include "dcp_decoder.h"
28 #include "dcp_examiner.h"
29 #include "dcpomatic_log.h"
30 #include "film.h"
31 #include "job.h"
32 #include "log.h"
33 #include "overlaps.h"
34 #include "text_content.h"
35 #include "video_content.h"
36 #include <dcp/dcp.h>
37 #include <dcp/raw_convert.h>
38 #include <dcp/exceptions.h>
39 #include <dcp/reel_closed_caption_asset.h>
40 #include <dcp/reel_picture_asset.h>
41 #include <dcp/reel_subtitle_asset.h>
42 #include <dcp/reel.h>
43 #include <libxml++/libxml++.h>
44 #include <iterator>
45 #include <iostream>
46
47 #include "i18n.h"
48
49
50 using std::cout;
51 using std::dynamic_pointer_cast;
52 using std::function;
53 using std::list;
54 using std::make_shared;
55 using std::shared_ptr;
56 using std::string;
57 using std::vector;
58 using boost::optional;
59 using boost::scoped_ptr;
60 #if BOOST_VERSION >= 106100
61 using namespace boost::placeholders;
62 #endif
63 using dcp::raw_convert;
64 using namespace dcpomatic;
65
66
67 int const DCPContentProperty::NEEDS_ASSETS       = 600;
68 int const DCPContentProperty::NEEDS_KDM          = 601;
69 int const DCPContentProperty::REFERENCE_VIDEO    = 602;
70 int const DCPContentProperty::REFERENCE_AUDIO    = 603;
71 int const DCPContentProperty::REFERENCE_TEXT     = 604;
72 int const DCPContentProperty::NAME               = 605;
73 int const DCPContentProperty::TEXTS              = 606;
74 int const DCPContentProperty::CPL                = 607;
75
76
77 DCPContent::DCPContent (boost::filesystem::path p)
78         : _encrypted (false)
79         , _needs_assets (false)
80         , _kdm_valid (false)
81         , _reference_video (false)
82         , _reference_audio (false)
83         , _three_d (false)
84 {
85         LOG_GENERAL ("Creating DCP content from %1", p.string());
86
87         read_directory (p);
88         set_default_colour_conversion ();
89 }
90
91 DCPContent::DCPContent (cxml::ConstNodePtr node, int version)
92         : Content (node)
93 {
94         video = VideoContent::from_xml (this, node, version, VideoRange::FULL);
95         audio = AudioContent::from_xml (this, node, version);
96         list<string> notes;
97         text = TextContent::from_xml (this, node, version, notes);
98         atmos = AtmosContent::from_xml (this, node);
99
100         if (video && audio) {
101                 audio->set_stream (
102                         make_shared<AudioStream> (
103                                 node->number_child<int> ("AudioFrameRate"),
104                                 /* AudioLength was not present in some old metadata versions */
105                                 node->optional_number_child<Frame>("AudioLength").get_value_or (
106                                         video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
107                                         ),
108                                 AudioMapping(node->node_child("AudioMapping"), version),
109                                 24
110                                 )
111                         );
112         }
113
114         _name = node->string_child ("Name");
115         _encrypted = node->bool_child ("Encrypted");
116         _needs_assets = node->optional_bool_child("NeedsAssets").get_value_or (false);
117         if (node->optional_node_child ("KDM")) {
118                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
119         }
120         _kdm_valid = node->bool_child ("KDMValid");
121         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
122         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
123         if (version >= 37) {
124                 _reference_text[TextType::OPEN_SUBTITLE] = node->optional_bool_child("ReferenceOpenSubtitle").get_value_or(false);
125                 _reference_text[TextType::CLOSED_CAPTION] = node->optional_bool_child("ReferenceClosedCaption").get_value_or(false);
126         } else {
127                 _reference_text[TextType::OPEN_SUBTITLE] = node->optional_bool_child("ReferenceSubtitle").get_value_or(false);
128                 _reference_text[TextType::CLOSED_CAPTION] = false;
129         }
130         if (node->optional_string_child("Standard")) {
131                 auto const s = node->optional_string_child("Standard").get();
132                 if (s == "Interop") {
133                         _standard = dcp::Standard::INTEROP;
134                 } else if (s == "SMPTE") {
135                         _standard = dcp::Standard::SMPTE;
136                 } else {
137                         DCPOMATIC_ASSERT (false);
138                 }
139         }
140         _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
141
142         auto ck = node->optional_string_child("ContentKind");
143         if (ck) {
144                 _content_kind = dcp::ContentKind::from_name(*ck);
145         }
146         _cpl = node->optional_string_child("CPL");
147         for (auto i: node->node_children("ReelLength")) {
148                 _reel_lengths.push_back (raw_convert<int64_t> (i->content ()));
149         }
150
151         for (auto i: node->node_children("Marker")) {
152                 _markers[dcp::marker_from_string(i->string_attribute("type"))] = ContentTime(raw_convert<int64_t>(i->content()));
153         }
154
155         for (auto i: node->node_children("Rating")) {
156                 _ratings.push_back (dcp::Rating(i));
157         }
158
159         for (auto i: node->node_children("ContentVersion")) {
160                 _content_versions.push_back (i->content());
161         }
162 }
163
164 void
165 DCPContent::read_directory (boost::filesystem::path p)
166 {
167         using namespace boost::filesystem;
168
169         bool have_assetmap = false;
170         bool have_metadata = false;
171
172         for (auto i: directory_iterator(p)) {
173                 if (i.path().filename() == "ASSETMAP" || i.path().filename() == "ASSETMAP.xml") {
174                         have_assetmap = true;
175                 } else if (i.path().filename() == "metadata.xml") {
176                         have_metadata = true;
177                 }
178         }
179
180         if (!have_assetmap) {
181                 if (!have_metadata) {
182                         throw DCPError ("No ASSETMAP or ASSETMAP.xml file found: is this a DCP?");
183                 } else {
184                         throw ProjectFolderError ();
185                 }
186         }
187
188         read_sub_directory (p);
189 }
190
191 void
192 DCPContent::read_sub_directory (boost::filesystem::path p)
193 {
194         LOG_GENERAL ("DCPContent::read_sub_directory reads %1", p.string());
195         for (auto i: boost::filesystem::directory_iterator(p)) {
196                 if (boost::filesystem::is_regular_file(i.path())) {
197                         LOG_GENERAL ("Inside there's regular file %1", i.path().string());
198                         add_path (i.path());
199                 } else if (boost::filesystem::is_directory(i.path()) && i.path().filename() != ".AppleDouble") {
200                         LOG_GENERAL ("Inside there's directory %1", i.path().string());
201                         read_sub_directory (i.path());
202                 } else {
203                         LOG_GENERAL("Ignoring %1 from inside: status is %2", i.path().string(), static_cast<int>(boost::filesystem::status(i.path()).type()));
204                 }
205         }
206 }
207
208 /** @param film Film, or 0 */
209 void
210 DCPContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
211 {
212         bool const needed_assets = needs_assets ();
213         bool const needed_kdm = needs_kdm ();
214         string const old_name = name ();
215
216         ContentChangeSignaller cc_texts (this, DCPContentProperty::TEXTS);
217         ContentChangeSignaller cc_assets (this, DCPContentProperty::NEEDS_ASSETS);
218         ContentChangeSignaller cc_kdm (this, DCPContentProperty::NEEDS_KDM);
219         ContentChangeSignaller cc_name (this, DCPContentProperty::NAME);
220
221         if (job) {
222                 job->set_progress_unknown ();
223         }
224         Content::examine (film, job);
225
226         auto examiner = make_shared<DCPExaminer>(shared_from_this(), film ? film->tolerant() : true);
227
228         if (examiner->has_video()) {
229                 {
230                         boost::mutex::scoped_lock lm (_mutex);
231                         video = make_shared<VideoContent>(this);
232                 }
233                 video->take_from_examiner(film, examiner);
234                 set_default_colour_conversion ();
235         }
236
237         if (examiner->has_audio()) {
238                 {
239                         boost::mutex::scoped_lock lm (_mutex);
240                         audio = make_shared<AudioContent>(this);
241                 }
242                 auto as = make_shared<AudioStream>(examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels(), 24);
243                 audio->set_stream (as);
244                 auto m = as->mapping ();
245                 m.make_default (film ? film->audio_processor() : 0);
246                 as->set_mapping (m);
247         }
248
249         if (examiner->has_atmos()) {
250                 {
251                         boost::mutex::scoped_lock lm (_mutex);
252                         atmos = make_shared<AtmosContent>(this);
253                 }
254                 /* Setting length will cause calculations to be made based on edit rate, so that must
255                  * be set up first otherwise hard-to-spot exceptions will be thrown.
256                  */
257                 atmos->set_edit_rate (examiner->atmos_edit_rate());
258                 atmos->set_length (examiner->atmos_length());
259         }
260
261         vector<shared_ptr<TextContent>> new_text;
262
263         for (int i = 0; i < examiner->text_count(TextType::OPEN_SUBTITLE); ++i) {
264                 auto c = make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::OPEN_SUBTITLE);
265                 c->set_language (examiner->open_subtitle_language());
266                 add_fonts_from_examiner(c, examiner->fonts());
267                 new_text.push_back (c);
268         }
269
270         for (int i = 0; i < examiner->text_count(TextType::CLOSED_CAPTION); ++i) {
271                 auto c = make_shared<TextContent>(this, TextType::CLOSED_CAPTION, TextType::CLOSED_CAPTION);
272                 c->set_dcp_track (examiner->dcp_text_track(i));
273                 new_text.push_back (c);
274         }
275
276         {
277                 boost::mutex::scoped_lock lm (_mutex);
278                 text = new_text;
279                 _name = examiner->name ();
280                 _encrypted = examiner->encrypted ();
281                 _needs_assets = examiner->needs_assets ();
282                 _kdm_valid = examiner->kdm_valid ();
283                 _standard = examiner->standard ();
284                 _three_d = examiner->three_d ();
285                 _content_kind = examiner->content_kind ();
286                 _cpl = examiner->cpl ();
287                 _reel_lengths = examiner->reel_lengths ();
288                 for (auto const& i: examiner->markers()) {
289                         _markers[i.first] = ContentTime(i.second.as_editable_units_ceil(DCPTime::HZ));
290                 }
291                 _ratings = examiner->ratings ();
292                 _content_versions = examiner->content_versions ();
293         }
294
295         if (needed_assets == needs_assets()) {
296                 cc_assets.abort ();
297         }
298
299         if (needed_kdm == needs_kdm()) {
300                 cc_kdm.abort ();
301         }
302
303         if (old_name == name()) {
304                 cc_name.abort ();
305         }
306
307         if (video) {
308                 video->set_frame_type (_three_d ? VideoFrameType::THREE_D : VideoFrameType::TWO_D);
309         }
310 }
311
312 string
313 DCPContent::summary () const
314 {
315         boost::mutex::scoped_lock lm (_mutex);
316         return String::compose (_("%1 [DCP]"), _name);
317 }
318
319 string
320 DCPContent::technical_summary () const
321 {
322         string s = Content::technical_summary() + " - ";
323         if (video) {
324                 s += video->technical_summary() + " - ";
325         }
326         if (audio) {
327                 s += audio->technical_summary() + " - ";
328         }
329         return s;
330 }
331
332 void
333 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
334 {
335         node->add_child("Type")->add_child_text ("DCP");
336
337         Content::as_xml (node, with_paths);
338
339         if (video) {
340                 video->as_xml (node);
341         }
342
343         if (audio) {
344                 audio->as_xml (node);
345                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
346                 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
347                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
348         }
349
350         for (auto i: text) {
351                 i->as_xml (node);
352         }
353
354         if (atmos) {
355                 atmos->as_xml (node);
356         }
357
358         boost::mutex::scoped_lock lm (_mutex);
359         node->add_child("Name")->add_child_text (_name);
360         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
361         node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
362         if (_kdm) {
363                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
364         }
365         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
366         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
367         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
368         node->add_child("ReferenceOpenSubtitle")->add_child_text(_reference_text[TextType::OPEN_SUBTITLE] ? "1" : "0");
369         node->add_child("ReferenceClosedCaption")->add_child_text(_reference_text[TextType::CLOSED_CAPTION] ? "1" : "0");
370         if (_standard) {
371                 switch (_standard.get ()) {
372                 case dcp::Standard::INTEROP:
373                         node->add_child("Standard")->add_child_text ("Interop");
374                         break;
375                 case dcp::Standard::SMPTE:
376                         node->add_child("Standard")->add_child_text ("SMPTE");
377                         break;
378                 default:
379                         DCPOMATIC_ASSERT (false);
380                 }
381         }
382         node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
383         if (_content_kind) {
384                 node->add_child("ContentKind")->add_child_text(_content_kind->name());
385         }
386         if (_cpl) {
387                 node->add_child("CPL")->add_child_text (_cpl.get ());
388         }
389         for (auto i: _reel_lengths) {
390                 node->add_child("ReelLength")->add_child_text (raw_convert<string> (i));
391         }
392
393         for (auto const& i: _markers) {
394                 auto marker = node->add_child("Marker");
395                 marker->set_attribute("type", dcp::marker_to_string(i.first));
396                 marker->add_child_text(raw_convert<string>(i.second.get()));
397         }
398
399         for (auto i: _ratings) {
400                 auto rating = node->add_child("Rating");
401                 i.as_xml (rating);
402         }
403
404         for (auto i: _content_versions) {
405                 node->add_child("ContentVersion")->add_child_text(i);
406         }
407 }
408
409 DCPTime
410 DCPContent::full_length (shared_ptr<const Film> film) const
411 {
412         if (!video) {
413                 return {};
414         }
415         FrameRateChange const frc (film, shared_from_this());
416         return DCPTime::from_frames (llrint(video->length() * frc.factor()), film->video_frame_rate());
417 }
418
419 DCPTime
420 DCPContent::approximate_length () const
421 {
422         if (!video) {
423                 return {};
424         }
425         return DCPTime::from_frames (video->length(), 24);
426 }
427
428 string
429 DCPContent::identifier () const
430 {
431         string s = Content::identifier() + "_";
432
433         if (video) {
434                 s += video->identifier() + "_";
435         }
436
437         for (auto i: text) {
438                 s += i->identifier () + " ";
439         }
440
441         s += string (_reference_video ? "1" : "0");
442         for (auto text: _reference_text) {
443                 s += string(text ? "1" : "0");
444         }
445         return s;
446 }
447
448 void
449 DCPContent::add_kdm (dcp::EncryptedKDM k)
450 {
451         _kdm = k;
452 }
453
454 void
455 DCPContent::add_ov (boost::filesystem::path ov)
456 {
457         read_directory (ov);
458 }
459
460 bool
461 DCPContent::can_be_played () const
462 {
463         return !needs_kdm() && !needs_assets();
464 }
465
466 bool
467 DCPContent::needs_kdm () const
468 {
469         boost::mutex::scoped_lock lm (_mutex);
470         return _encrypted && !_kdm_valid;
471 }
472
473 bool
474 DCPContent::needs_assets () const
475 {
476         boost::mutex::scoped_lock lm (_mutex);
477         return _needs_assets;
478 }
479
480 vector<boost::filesystem::path>
481 DCPContent::directories () const
482 {
483         return dcp::DCP::directories_from_files (paths());
484 }
485
486 void
487 DCPContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
488 {
489         Content::add_properties (film, p);
490         if (video) {
491                 video->add_properties (p);
492         }
493         if (audio) {
494                 audio->add_properties (film, p);
495         }
496 }
497
498 void
499 DCPContent::set_default_colour_conversion ()
500 {
501         /* Default to no colour conversion for DCPs */
502         if (video) {
503                 video->unset_colour_conversion ();
504         }
505 }
506
507 void
508 DCPContent::set_reference_video (bool r)
509 {
510         ContentChangeSignaller cc (this, DCPContentProperty::REFERENCE_VIDEO);
511
512         {
513                 boost::mutex::scoped_lock lm (_mutex);
514                 _reference_video = r;
515         }
516 }
517
518 void
519 DCPContent::set_reference_audio (bool r)
520 {
521         ContentChangeSignaller cc (this, DCPContentProperty::REFERENCE_AUDIO);
522
523         {
524                 boost::mutex::scoped_lock lm (_mutex);
525                 _reference_audio = r;
526         }
527 }
528
529 void
530 DCPContent::set_reference_text (TextType type, bool r)
531 {
532         ContentChangeSignaller cc (this, DCPContentProperty::REFERENCE_TEXT);
533
534         {
535                 boost::mutex::scoped_lock lm (_mutex);
536                 _reference_text[type] = r;
537         }
538 }
539
540 list<DCPTimePeriod>
541 DCPContent::reels (shared_ptr<const Film> film) const
542 {
543         auto reel_lengths = _reel_lengths;
544         if (reel_lengths.empty()) {
545                 /* Old metadata with no reel lengths; get them here instead */
546                 try {
547                         scoped_ptr<DCPExaminer> examiner (new DCPExaminer(shared_from_this(), film->tolerant()));
548                         reel_lengths = examiner->reel_lengths ();
549                 } catch (...) {
550                         /* Could not examine the DCP; guess reels */
551                         reel_lengths.push_back (length_after_trim(film).frames_round(film->video_frame_rate()));
552                 }
553         }
554
555         list<DCPTimePeriod> p;
556
557         /* This content's frame rate must be the same as the output DCP rate, so we can
558            convert `directly' from ContentTime to DCPTime.
559         */
560
561         /* The starting point of this content on the timeline */
562         auto pos = position() - DCPTime (trim_start().get());
563
564         for (auto i: reel_lengths) {
565                 /* This reel runs from `pos' to `to' */
566                 DCPTime const to = pos + DCPTime::from_frames (i, film->video_frame_rate());
567                 if (to > position()) {
568                         p.push_back (DCPTimePeriod(max(position(), pos), min(end(film), to)));
569                         if (to > end(film)) {
570                                 break;
571                         }
572                 }
573                 pos = to;
574         }
575
576         return p;
577 }
578
579 list<DCPTime>
580 DCPContent::reel_split_points (shared_ptr<const Film> film) const
581 {
582         list<DCPTime> s;
583         for (auto i: reels(film)) {
584                 s.push_back (i.from);
585         }
586         return s;
587 }
588
589 bool
590 DCPContent::can_reference (shared_ptr<const Film> film, function<bool (shared_ptr<const Content>)> part, string overlapping, string& why_not) const
591 {
592         /* We must be using the same standard as the film */
593         if (_standard) {
594                 if (_standard.get() == dcp::Standard::INTEROP && !film->interop()) {
595                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
596                         why_not = _("it is Interop and the film is set to SMPTE.");
597                         return false;
598                 } else if (_standard.get() == dcp::Standard::SMPTE && film->interop()) {
599                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
600                         why_not = _("it is SMPTE and the film is set to Interop.");
601                         return false;
602                 }
603         }
604
605         /* And the same frame rate */
606         if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film->video_frame_rate())) {
607                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
608                 why_not = _("it has a different frame rate to the film.");
609                 return false;
610         }
611
612         auto const fr = film->reels ();
613
614         list<DCPTimePeriod> reel_list;
615         try {
616                 reel_list = reels (film);
617         } catch (dcp::ReadError &) {
618                 /* We couldn't read the DCP; it's probably missing */
619                 return false;
620         } catch (dcp::KDMDecryptionError &) {
621                 /* We have an incorrect KDM */
622                 return false;
623         }
624
625         /* fr must contain reels().  It can also contain other reels, but it must at
626            least contain reels().
627         */
628         for (auto i: reel_list) {
629                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
630                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
631                         why_not = _("its reel lengths differ from those in the film; set the reel mode to 'split by video content'.");
632                         return false;
633                 }
634         }
635
636         auto a = overlaps (film, film->content(), part, position(), end(film));
637         if (a.size() != 1 || a.front().get() != this) {
638                 why_not = overlapping;
639                 return false;
640         }
641
642         return true;
643 }
644
645 static
646 bool check_video (shared_ptr<const Content> c)
647 {
648         return static_cast<bool>(c->video) && c->video->use();
649 }
650
651 bool
652 DCPContent::can_reference_video (shared_ptr<const Film> film, string& why_not) const
653 {
654         if (!video) {
655                 why_not = _("There is no video in this DCP");
656                 return false;
657         }
658
659         if (film->resolution() != resolution()) {
660                 if (resolution() == Resolution::FOUR_K) {
661                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
662                         why_not = _("it is 4K and the film is 2K.");
663                 } else {
664                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
665                         why_not = _("it is 2K and the film is 4K.");
666                 }
667                 return false;
668         } else if (film->frame_size() != video->size()) {
669                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
670                 why_not = _("its video frame size differs from the film's.");
671                 return false;
672         }
673
674         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
675         return can_reference (film, bind (&check_video, _1), _("it overlaps other video content; remove the other content."), why_not);
676 }
677
678 static
679 bool check_audio (shared_ptr<const Content> c)
680 {
681         return static_cast<bool>(c->audio) && !c->audio->mapping().mapped_output_channels().empty();
682 }
683
684 bool
685 DCPContent::can_reference_audio (shared_ptr<const Film> film, string& why_not) const
686 {
687         shared_ptr<DCPDecoder> decoder;
688         try {
689                 decoder = make_shared<DCPDecoder>(film, shared_from_this(), false, film->tolerant(), shared_ptr<DCPDecoder>());
690         } catch (dcp::ReadError &) {
691                 /* We couldn't read the DCP, so it's probably missing */
692                 return false;
693         } catch (DCPError &) {
694                 /* We couldn't read the DCP, so it's probably missing */
695                 return false;
696         } catch (dcp::KDMDecryptionError &) {
697                 /* We have an incorrect KDM */
698                 return false;
699         }
700
701         for (auto i: decoder->reels()) {
702                 if (!i->main_sound()) {
703                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
704                         why_not = _("it does not have sound in all its reels.");
705                         return false;
706                 }
707         }
708
709         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
710         return can_reference (film, bind (&check_audio, _1), _("it overlaps other audio content; remove the other content."), why_not);
711 }
712
713 static
714 bool check_text (shared_ptr<const Content> c)
715 {
716         return !c->text.empty();
717 }
718
719 bool
720 DCPContent::can_reference_text (shared_ptr<const Film> film, TextType type, string& why_not) const
721 {
722         shared_ptr<DCPDecoder> decoder;
723         try {
724                 decoder = make_shared<DCPDecoder>(film, shared_from_this(), false, film->tolerant(), shared_ptr<DCPDecoder>());
725         } catch (dcp::ReadError &) {
726                 /* We couldn't read the DCP, so it's probably missing */
727                 return false;
728         } catch (dcp::KDMDecryptionError &) {
729                 /* We have an incorrect KDM */
730                 return false;
731         }
732
733         for (auto i: decoder->reels()) {
734                 if (type == TextType::OPEN_SUBTITLE) {
735                         if (!i->main_subtitle()) {
736                                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
737                                 why_not = _("it does not have open subtitles in all its reels.");
738                                 return false;
739                         } else if (i->main_subtitle()->entry_point().get_value_or(0) != 0) {
740                                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
741                                 why_not = _("one of its subtitle reels has a non-zero entry point so it must be re-written.");
742                                 return false;
743                         }
744                 }
745                 if (type == TextType::CLOSED_CAPTION) {
746                         if (i->closed_captions().empty()) {
747                                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
748                                 why_not = _("it does not have closed captions in all its reels.");
749                                 return false;
750                         }
751                         for (auto j: i->closed_captions()) {
752                                 if (j->entry_point().get_value_or(0) != 0) {
753                                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
754                                         why_not = _("one of its closed caption has a non-zero entry point so it must be re-written.");
755                                         return false;
756                                 }
757                         }
758                 }
759         }
760
761         if (trim_start() != dcpomatic::ContentTime()) {
762                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
763                 why_not = _("it has a start trim so its subtitles or closed captions must be re-written.");
764                 return false;
765         }
766
767         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
768         return can_reference (film, bind (&check_text, _1), _("it overlaps other text content; remove the other content."), why_not);
769 }
770
771 void
772 DCPContent::take_settings_from (shared_ptr<const Content> c)
773 {
774         auto dc = dynamic_pointer_cast<const DCPContent>(c);
775         if (!dc) {
776                 return;
777         }
778
779         _reference_video = dc->_reference_video;
780         _reference_audio = dc->_reference_audio;
781         _reference_text = dc->_reference_text;
782 }
783
784 void
785 DCPContent::set_cpl (string id)
786 {
787         ContentChangeSignaller cc (this, DCPContentProperty::CPL);
788
789         {
790                 boost::mutex::scoped_lock lm (_mutex);
791                 _cpl = id;
792         }
793 }
794
795 bool
796 DCPContent::kdm_timing_window_valid () const
797 {
798         if (!_kdm) {
799                 return true;
800         }
801
802         dcp::LocalTime now;
803         return _kdm->not_valid_before() < now && now < _kdm->not_valid_after();
804 }
805
806
807 Resolution
808 DCPContent::resolution () const
809 {
810         if (video->size().width > 2048 || video->size().height > 1080) {
811                 return Resolution::FOUR_K;
812         }
813
814         return Resolution::TWO_K;
815 }
816
817
818 void
819 add_fonts_from_examiner(shared_ptr<TextContent> text, vector<vector<shared_ptr<Font>>> const & all_fonts)
820 {
821         int reel_number = 0;
822         for (auto reel_fonts: all_fonts) {
823                 for (auto font: reel_fonts) {
824                         /* Each reel could have its own font with the same ID, so we disambiguate them here
825                          * by prepending the reel number.  We do the same disambiguation when emitting the
826                          * subtitles in the DCP decoder.
827                          */
828                         font->set_id(id_for_font_in_reel(font->id(), reel_number));
829                         text->add_font(font);
830                 }
831                 ++reel_number;
832         }
833
834 }
835
836
837 string
838 id_for_font_in_reel(string id, int reel)
839 {
840         return String::compose("%1_%2", reel, id);
841 }
842
843
844 void
845 DCPContent::check_font_ids()
846 {
847         if (text.empty()) {
848                 return;
849         }
850
851         DCPExaminer examiner(shared_from_this(), true);
852         add_fonts_from_examiner(text.front(), examiner.fonts());
853 }
854