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