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