Disallow referring to OV audio unless channel counts match (#2780).
[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 "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 <dcp/scope_guard.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         dcp::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                 examiner->add_fonts(c);
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                 examiner->add_fonts(c);
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
378         node->add_child("Name")->add_child_text (_name);
379         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
380         node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
381         if (_kdm) {
382                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
383         }
384         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
385         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
386         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
387         node->add_child("ReferenceOpenSubtitle")->add_child_text(_reference_text[TextType::OPEN_SUBTITLE] ? "1" : "0");
388         node->add_child("ReferenceClosedCaption")->add_child_text(_reference_text[TextType::CLOSED_CAPTION] ? "1" : "0");
389         if (_standard) {
390                 switch (_standard.get ()) {
391                 case dcp::Standard::INTEROP:
392                         node->add_child("Standard")->add_child_text ("Interop");
393                         break;
394                 case dcp::Standard::SMPTE:
395                         node->add_child("Standard")->add_child_text ("SMPTE");
396                         break;
397                 default:
398                         DCPOMATIC_ASSERT (false);
399                 }
400         }
401         node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
402         if (_content_kind) {
403                 node->add_child("ContentKind")->add_child_text(_content_kind->name());
404         }
405         if (_cpl) {
406                 node->add_child("CPL")->add_child_text (_cpl.get ());
407         }
408         for (auto i: _reel_lengths) {
409                 node->add_child("ReelLength")->add_child_text (raw_convert<string> (i));
410         }
411
412         for (auto const& i: _markers) {
413                 auto marker = node->add_child("Marker");
414                 marker->set_attribute("type", dcp::marker_to_string(i.first));
415                 marker->add_child_text(raw_convert<string>(i.second.get()));
416         }
417
418         for (auto i: _ratings) {
419                 auto rating = node->add_child("Rating");
420                 i.as_xml (rating);
421         }
422
423         for (auto i: _content_versions) {
424                 node->add_child("ContentVersion")->add_child_text(i);
425         }
426
427         if (_active_audio_channels) {
428                 node->add_child("ActiveAudioChannels")->add_child_text(raw_convert<string>(*_active_audio_channels));
429         }
430 }
431
432 DCPTime
433 DCPContent::full_length (shared_ptr<const Film> film) const
434 {
435         if (!video) {
436                 return {};
437         }
438         FrameRateChange const frc (film, shared_from_this());
439         return DCPTime::from_frames (llrint(video->length() * frc.factor()), film->video_frame_rate());
440 }
441
442 DCPTime
443 DCPContent::approximate_length () const
444 {
445         if (!video) {
446                 return {};
447         }
448         return DCPTime::from_frames (video->length(), 24);
449 }
450
451 string
452 DCPContent::identifier () const
453 {
454         string s = Content::identifier() + "_";
455
456         if (video) {
457                 s += video->identifier() + "_";
458         }
459
460         for (auto i: text) {
461                 s += i->identifier () + " ";
462         }
463
464         boost::mutex::scoped_lock lm(_mutex);
465
466         s += string (_reference_video ? "1" : "0");
467         for (auto text: _reference_text) {
468                 s += string(text ? "1" : "0");
469         }
470         return s;
471 }
472
473 void
474 DCPContent::add_kdm (dcp::EncryptedKDM k)
475 {
476         _kdm = k;
477 }
478
479 void
480 DCPContent::add_ov (boost::filesystem::path ov)
481 {
482         read_directory (ov);
483 }
484
485 bool
486 DCPContent::can_be_played () const
487 {
488         return !needs_kdm() && !needs_assets();
489 }
490
491 bool
492 DCPContent::needs_kdm () const
493 {
494         boost::mutex::scoped_lock lm (_mutex);
495         return _encrypted && !_kdm_valid;
496 }
497
498 bool
499 DCPContent::needs_assets () const
500 {
501         boost::mutex::scoped_lock lm (_mutex);
502         return _needs_assets;
503 }
504
505 vector<boost::filesystem::path>
506 DCPContent::directories () const
507 {
508         return dcp::DCP::directories_from_files (paths());
509 }
510
511 void
512 DCPContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
513 {
514         Content::add_properties (film, p);
515         if (video) {
516                 video->add_properties (p);
517         }
518         if (audio) {
519                 audio->add_properties (film, p);
520         }
521 }
522
523 void
524 DCPContent::set_default_colour_conversion ()
525 {
526         /* Default to no colour conversion for DCPs */
527         if (video) {
528                 video->unset_colour_conversion ();
529         }
530 }
531
532 void
533 DCPContent::set_reference_video (bool r)
534 {
535         ContentChangeSignaller cc (this, DCPContentProperty::REFERENCE_VIDEO);
536
537         {
538                 boost::mutex::scoped_lock lm (_mutex);
539                 _reference_video = r;
540         }
541 }
542
543 void
544 DCPContent::set_reference_audio (bool r)
545 {
546         ContentChangeSignaller cc (this, DCPContentProperty::REFERENCE_AUDIO);
547
548         {
549                 boost::mutex::scoped_lock lm (_mutex);
550                 _reference_audio = r;
551         }
552 }
553
554 void
555 DCPContent::set_reference_text (TextType type, bool r)
556 {
557         ContentChangeSignaller cc (this, DCPContentProperty::REFERENCE_TEXT);
558
559         {
560                 boost::mutex::scoped_lock lm (_mutex);
561                 _reference_text[type] = r;
562         }
563 }
564
565 list<DCPTimePeriod>
566 DCPContent::reels (shared_ptr<const Film> film) const
567 {
568         auto reel_lengths = _reel_lengths;
569         if (reel_lengths.empty()) {
570                 /* Old metadata with no reel lengths; get them here instead */
571                 try {
572                         scoped_ptr<DCPExaminer> examiner (new DCPExaminer(shared_from_this(), film->tolerant()));
573                         reel_lengths = examiner->reel_lengths ();
574                 } catch (...) {
575                         /* Could not examine the DCP; guess reels */
576                         reel_lengths.push_back (length_after_trim(film).frames_round(film->video_frame_rate()));
577                 }
578         }
579
580         list<DCPTimePeriod> p;
581
582         /* This content's frame rate must be the same as the output DCP rate, so we can
583            convert `directly' from ContentTime to DCPTime.
584         */
585
586         /* The starting point of this content on the timeline */
587         auto pos = position() - DCPTime (trim_start().get());
588
589         for (auto i: reel_lengths) {
590                 /* This reel runs from `pos' to `to' */
591                 DCPTime const to = pos + DCPTime::from_frames (i, film->video_frame_rate());
592                 if (to > position()) {
593                         p.push_back (DCPTimePeriod(max(position(), pos), min(end(film), to)));
594                         if (to > end(film)) {
595                                 break;
596                         }
597                 }
598                 pos = to;
599         }
600
601         return p;
602 }
603
604 list<DCPTime>
605 DCPContent::reel_split_points (shared_ptr<const Film> film) const
606 {
607         list<DCPTime> s;
608         for (auto i: reels(film)) {
609                 s.push_back (i.from);
610         }
611         return s;
612 }
613
614 bool
615 DCPContent::can_reference (shared_ptr<const Film> film, function<bool (shared_ptr<const Content>)> part, string overlapping, string& why_not) const
616 {
617         /* We must be using the same standard as the film */
618         if (_standard) {
619                 if (_standard.get() == dcp::Standard::INTEROP && !film->interop()) {
620                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
621                         why_not = _("it is Interop and the film is set to SMPTE.");
622                         return false;
623                 } else if (_standard.get() == dcp::Standard::SMPTE && film->interop()) {
624                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
625                         why_not = _("it is SMPTE and the film is set to Interop.");
626                         return false;
627                 }
628         }
629
630         /* And the same frame rate */
631         if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film->video_frame_rate())) {
632                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
633                 why_not = _("it has a different frame rate to the film.");
634                 return false;
635         }
636
637         auto const fr = film->reels ();
638
639         list<DCPTimePeriod> reel_list;
640         try {
641                 reel_list = reels (film);
642         } catch (dcp::ReadError &) {
643                 /* We couldn't read the DCP; it's probably missing */
644                 return false;
645         } catch (dcp::KDMDecryptionError &) {
646                 /* We have an incorrect KDM */
647                 return false;
648         }
649
650         /* fr must contain reels().  It can also contain other reels, but it must at
651            least contain reels().
652         */
653         for (auto i: reel_list) {
654                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
655                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
656                         why_not = _("its reel lengths differ from those in the film; set the reel mode to 'split by video content'.");
657                         return false;
658                 }
659         }
660
661         auto a = overlaps (film, film->content(), part, position(), end(film));
662         if (a.size() != 1 || a.front().get() != this) {
663                 why_not = overlapping;
664                 return false;
665         }
666
667         return true;
668 }
669
670
671 bool
672 DCPContent::can_reference_video (shared_ptr<const Film> film, string& why_not) const
673 {
674         if (!video) {
675                 why_not = _("There is no video in this DCP");
676                 return false;
677         }
678
679         if (film->resolution() != resolution()) {
680                 if (resolution() == Resolution::FOUR_K) {
681                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
682                         why_not = _("it is 4K and the film is 2K.");
683                 } else {
684                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
685                         why_not = _("it is 2K and the film is 4K.");
686                 }
687                 return false;
688         } else if (film->frame_size() != video->size()) {
689                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
690                 why_not = _("its video frame size differs from the film's.");
691                 return false;
692         }
693
694         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
695         return can_reference(
696                 film,
697                 [](shared_ptr<const Content> c) {
698                         return static_cast<bool>(c->video) && c->video->use();
699                 },
700                 _("it overlaps other video content; remove the other content."),
701                 why_not
702         );
703 }
704
705
706 bool
707 DCPContent::can_reference_audio (shared_ptr<const Film> film, string& why_not) const
708 {
709         shared_ptr<DCPDecoder> decoder;
710         try {
711                 decoder = make_shared<DCPDecoder>(film, shared_from_this(), false, film->tolerant(), shared_ptr<DCPDecoder>());
712         } catch (dcp::ReadError &) {
713                 /* We couldn't read the DCP, so it's probably missing */
714                 return false;
715         } catch (DCPError &) {
716                 /* We couldn't read the DCP, so it's probably missing */
717                 return false;
718         } catch (dcp::KDMDecryptionError &) {
719                 /* We have an incorrect KDM */
720                 return false;
721         }
722
723         if (audio && audio->stream()) {
724                 auto const channels = audio->stream()->channels();
725                 if (channels != film->audio_channels()) {
726                         why_not = String::compose(_("it has a different number of audio channels than the project; set the project to have %1 channels."), channels);
727                         return false;
728                 }
729         }
730
731         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
732         return can_reference(
733                 film, [](shared_ptr<const Content> c) {
734                         return static_cast<bool>(c->audio) && !c->audio->mapping().mapped_output_channels().empty();
735                 },
736                 _("it overlaps other audio content; remove the other content."),
737                 why_not
738         );
739 }
740
741
742 bool
743 DCPContent::can_reference_text (shared_ptr<const Film> film, TextType type, string& why_not) const
744 {
745         shared_ptr<DCPDecoder> decoder;
746         try {
747                 decoder = make_shared<DCPDecoder>(film, shared_from_this(), false, film->tolerant(), shared_ptr<DCPDecoder>());
748         } catch (dcp::ReadError &) {
749                 /* We couldn't read the DCP, so it's probably missing */
750                 return false;
751         } catch (DCPError &) {
752                 /* We couldn't read the DCP, so it's probably missing */
753                 return false;
754         } catch (dcp::KDMDecryptionError &) {
755                 /* We have an incorrect KDM */
756                 return false;
757         }
758
759         for (auto i: decoder->reels()) {
760                 if (type == TextType::OPEN_SUBTITLE) {
761                         if (i->main_subtitle() && i->main_subtitle()->entry_point().get_value_or(0) != 0) {
762                                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
763                                 why_not = _("one of its subtitle reels has a non-zero entry point so it must be re-written.");
764                                 return false;
765                         }
766                 }
767                 if (type == TextType::CLOSED_CAPTION) {
768                         for (auto j: i->closed_captions()) {
769                                 if (j->entry_point().get_value_or(0) != 0) {
770                                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
771                                         why_not = _("one of its closed caption has a non-zero entry point so it must be re-written.");
772                                         return false;
773                                 }
774                         }
775                 }
776         }
777
778         if (trim_start() != dcpomatic::ContentTime()) {
779                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
780                 why_not = _("it has a start trim so its subtitles or closed captions must be re-written.");
781                 return false;
782         }
783
784         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
785         return can_reference(
786                 film,
787                 [type](shared_ptr<const Content> c) {
788                         return std::find_if(c->text.begin(), c->text.end(), [type](shared_ptr<const TextContent> t) { return t->type() == type; }) != c->text.end();
789                 },
790                 _("they overlap other text content; remove the other content."),
791                 why_not
792         );
793 }
794
795 void
796 DCPContent::take_settings_from (shared_ptr<const Content> c)
797 {
798         auto dc = dynamic_pointer_cast<const DCPContent>(c);
799         if (!dc) {
800                 return;
801         }
802
803         if (this == dc.get()) {
804                 return;
805         }
806
807         boost::mutex::scoped_lock lm(_mutex);
808         boost::mutex::scoped_lock lm2(dc->_mutex);
809
810         _reference_video = dc->_reference_video;
811         _reference_audio = dc->_reference_audio;
812         _reference_text = dc->_reference_text;
813 }
814
815 void
816 DCPContent::set_cpl (string id)
817 {
818         ContentChangeSignaller cc (this, DCPContentProperty::CPL);
819
820         {
821                 boost::mutex::scoped_lock lm (_mutex);
822                 _cpl = id;
823         }
824 }
825
826 bool
827 DCPContent::kdm_timing_window_valid () const
828 {
829         if (!_kdm) {
830                 return true;
831         }
832
833         dcp::LocalTime now;
834         return _kdm->not_valid_before() < now && now < _kdm->not_valid_after();
835 }
836
837
838 Resolution
839 DCPContent::resolution () const
840 {
841         if (video->size() && (video->size()->width > 2048 || video->size()->height > 1080)) {
842                 return Resolution::FOUR_K;
843         }
844
845         return Resolution::TWO_K;
846 }
847
848
849 void
850 DCPContent::check_font_ids()
851 {
852         if (text.empty()) {
853                 return;
854         }
855
856         DCPExaminer examiner(shared_from_this(), true);
857         examiner.add_fonts(text.front());
858 }
859
860
861 int
862 DCPContent::active_audio_channels() const
863 {
864         return _active_audio_channels.get_value_or(
865                 (audio && audio->stream()) ? audio->stream()->channels() : 0
866                 );
867 }
868