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