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