2 Copyright (C) 2014-2022 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "atmos_content.h"
23 #include "audio_content.h"
24 #include "compose.hpp"
26 #include "dcp_content.h"
27 #include "dcp_decoder.h"
28 #include "dcp_examiner.h"
29 #include "dcpomatic_log.h"
34 #include "text_content.h"
35 #include "video_content.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>
43 #include <libxml++/libxml++.h>
51 using std::dynamic_pointer_cast;
54 using std::make_shared;
55 using std::shared_ptr;
58 using boost::optional;
59 using boost::scoped_ptr;
60 #if BOOST_VERSION >= 106100
61 using namespace boost::placeholders;
63 using dcp::raw_convert;
64 using namespace dcpomatic;
67 int const DCPContentProperty::NEEDS_ASSETS = 600;
68 int const DCPContentProperty::NEEDS_KDM = 601;
69 int const DCPContentProperty::REFERENCE_VIDEO = 602;
70 int const DCPContentProperty::REFERENCE_AUDIO = 603;
71 int const DCPContentProperty::REFERENCE_TEXT = 604;
72 int const DCPContentProperty::NAME = 605;
73 int const DCPContentProperty::TEXTS = 606;
74 int const DCPContentProperty::CPL = 607;
77 DCPContent::DCPContent (boost::filesystem::path p)
79 , _needs_assets (false)
81 , _reference_video (false)
82 , _reference_audio (false)
85 LOG_GENERAL ("Creating DCP content from %1", p.string());
88 set_default_colour_conversion ();
91 DCPContent::DCPContent (cxml::ConstNodePtr node, int version)
94 video = VideoContent::from_xml (this, node, version, VideoRange::FULL);
95 audio = AudioContent::from_xml (this, node, version);
97 text = TextContent::from_xml (this, node, version, notes);
98 atmos = AtmosContent::from_xml (this, node);
100 if (video && audio) {
102 make_shared<AudioStream> (
103 node->number_child<int> ("AudioFrameRate"),
104 /* AudioLength was not present in some old metadata versions */
105 node->optional_number_child<Frame>("AudioLength").get_value_or (
106 video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
108 AudioMapping(node->node_child("AudioMapping"), version),
114 _name = node->string_child ("Name");
115 _encrypted = node->bool_child ("Encrypted");
116 _needs_assets = node->optional_bool_child("NeedsAssets").get_value_or (false);
117 if (node->optional_node_child ("KDM")) {
118 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
120 _kdm_valid = node->bool_child ("KDMValid");
121 _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
122 _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
124 _reference_text[TextType::OPEN_SUBTITLE] = node->optional_bool_child("ReferenceOpenSubtitle").get_value_or(false);
125 _reference_text[TextType::CLOSED_CAPTION] = node->optional_bool_child("ReferenceClosedCaption").get_value_or(false);
127 _reference_text[TextType::OPEN_SUBTITLE] = node->optional_bool_child("ReferenceSubtitle").get_value_or(false);
128 _reference_text[TextType::CLOSED_CAPTION] = false;
130 if (node->optional_string_child("Standard")) {
131 auto const s = node->optional_string_child("Standard").get();
132 if (s == "Interop") {
133 _standard = dcp::Standard::INTEROP;
134 } else if (s == "SMPTE") {
135 _standard = dcp::Standard::SMPTE;
137 DCPOMATIC_ASSERT (false);
140 _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
142 auto ck = node->optional_string_child("ContentKind");
144 _content_kind = dcp::ContentKind::from_name(*ck);
146 _cpl = node->optional_string_child("CPL");
147 for (auto i: node->node_children("ReelLength")) {
148 _reel_lengths.push_back (raw_convert<int64_t> (i->content ()));
151 for (auto i: node->node_children("Marker")) {
152 _markers[dcp::marker_from_string(i->string_attribute("type"))] = ContentTime(raw_convert<int64_t>(i->content()));
155 for (auto i: node->node_children("Rating")) {
156 _ratings.push_back (dcp::Rating(i));
159 for (auto i: node->node_children("ContentVersion")) {
160 _content_versions.push_back (i->content());
165 DCPContent::read_directory (boost::filesystem::path p)
167 using namespace boost::filesystem;
169 bool have_assetmap = false;
170 bool have_metadata = false;
172 for (auto i: directory_iterator(p)) {
173 if (i.path().filename() == "ASSETMAP" || i.path().filename() == "ASSETMAP.xml") {
174 have_assetmap = true;
175 } else if (i.path().filename() == "metadata.xml") {
176 have_metadata = true;
180 if (!have_assetmap) {
181 if (!have_metadata) {
182 throw DCPError ("No ASSETMAP or ASSETMAP.xml file found: is this a DCP?");
184 throw ProjectFolderError ();
188 read_sub_directory (p);
192 DCPContent::read_sub_directory (boost::filesystem::path p)
194 LOG_GENERAL ("DCPContent::read_sub_directory reads %1", p.string());
195 for (auto i: boost::filesystem::directory_iterator(p)) {
196 if (boost::filesystem::is_regular_file(i.path())) {
197 LOG_GENERAL ("Inside there's regular file %1", i.path().string());
199 } else if (boost::filesystem::is_directory(i.path()) && i.path().filename() != ".AppleDouble") {
200 LOG_GENERAL ("Inside there's directory %1", i.path().string());
201 read_sub_directory (i.path());
203 LOG_GENERAL("Ignoring %1 from inside: status is %2", i.path().string(), static_cast<int>(boost::filesystem::status(i.path()).type()));
208 /** @param film Film, or 0 */
210 DCPContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
212 bool const needed_assets = needs_assets ();
213 bool const needed_kdm = needs_kdm ();
214 string const old_name = name ();
216 ContentChangeSignaller cc_texts (this, DCPContentProperty::TEXTS);
217 ContentChangeSignaller cc_assets (this, DCPContentProperty::NEEDS_ASSETS);
218 ContentChangeSignaller cc_kdm (this, DCPContentProperty::NEEDS_KDM);
219 ContentChangeSignaller cc_name (this, DCPContentProperty::NAME);
222 job->set_progress_unknown ();
224 Content::examine (film, job);
226 auto examiner = make_shared<DCPExaminer>(shared_from_this(), film ? film->tolerant() : true);
228 if (examiner->has_video()) {
230 boost::mutex::scoped_lock lm (_mutex);
231 video = make_shared<VideoContent>(this);
233 video->take_from_examiner(film, examiner);
234 set_default_colour_conversion ();
237 if (examiner->has_audio()) {
239 boost::mutex::scoped_lock lm (_mutex);
240 audio = make_shared<AudioContent>(this);
242 auto as = make_shared<AudioStream>(examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels(), 24);
243 audio->set_stream (as);
244 auto m = as->mapping ();
245 m.make_default (film ? film->audio_processor() : 0);
249 if (examiner->has_atmos()) {
251 boost::mutex::scoped_lock lm (_mutex);
252 atmos = make_shared<AtmosContent>(this);
254 /* Setting length will cause calculations to be made based on edit rate, so that must
255 * be set up first otherwise hard-to-spot exceptions will be thrown.
257 atmos->set_edit_rate (examiner->atmos_edit_rate());
258 atmos->set_length (examiner->atmos_length());
261 list<shared_ptr<TextContent>> new_text;
263 for (int i = 0; i < examiner->text_count(TextType::OPEN_SUBTITLE); ++i) {
264 auto c = make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::OPEN_SUBTITLE);
265 c->set_language (examiner->open_subtitle_language());
266 add_fonts_from_examiner(c, examiner->fonts());
267 new_text.push_back (c);
270 for (int i = 0; i < examiner->text_count(TextType::CLOSED_CAPTION); ++i) {
271 auto c = make_shared<TextContent>(this, TextType::CLOSED_CAPTION, TextType::CLOSED_CAPTION);
272 c->set_dcp_track (examiner->dcp_text_track(i));
273 new_text.push_back (c);
277 boost::mutex::scoped_lock lm (_mutex);
279 _name = examiner->name ();
280 _encrypted = examiner->encrypted ();
281 _needs_assets = examiner->needs_assets ();
282 _kdm_valid = examiner->kdm_valid ();
283 _standard = examiner->standard ();
284 _three_d = examiner->three_d ();
285 _content_kind = examiner->content_kind ();
286 _cpl = examiner->cpl ();
287 _reel_lengths = examiner->reel_lengths ();
288 for (auto const& i: examiner->markers()) {
289 _markers[i.first] = ContentTime(i.second.as_editable_units_ceil(DCPTime::HZ));
291 _ratings = examiner->ratings ();
292 _content_versions = examiner->content_versions ();
295 if (needed_assets == needs_assets()) {
299 if (needed_kdm == needs_kdm()) {
303 if (old_name == name()) {
308 video->set_frame_type (_three_d ? VideoFrameType::THREE_D : VideoFrameType::TWO_D);
313 DCPContent::summary () const
315 boost::mutex::scoped_lock lm (_mutex);
316 return String::compose (_("%1 [DCP]"), _name);
320 DCPContent::technical_summary () const
322 string s = Content::technical_summary() + " - ";
324 s += video->technical_summary() + " - ";
327 s += audio->technical_summary() + " - ";
333 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
335 node->add_child("Type")->add_child_text ("DCP");
337 Content::as_xml (node, with_paths);
340 video->as_xml (node);
344 audio->as_xml (node);
345 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
346 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
347 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
355 atmos->as_xml (node);
358 boost::mutex::scoped_lock lm (_mutex);
359 node->add_child("Name")->add_child_text (_name);
360 node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
361 node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
363 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
365 node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
366 node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
367 node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
368 node->add_child("ReferenceOpenSubtitle")->add_child_text(_reference_text[TextType::OPEN_SUBTITLE] ? "1" : "0");
369 node->add_child("ReferenceClosedCaption")->add_child_text(_reference_text[TextType::CLOSED_CAPTION] ? "1" : "0");
371 switch (_standard.get ()) {
372 case dcp::Standard::INTEROP:
373 node->add_child("Standard")->add_child_text ("Interop");
375 case dcp::Standard::SMPTE:
376 node->add_child("Standard")->add_child_text ("SMPTE");
379 DCPOMATIC_ASSERT (false);
382 node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
384 node->add_child("ContentKind")->add_child_text(_content_kind->name());
387 node->add_child("CPL")->add_child_text (_cpl.get ());
389 for (auto i: _reel_lengths) {
390 node->add_child("ReelLength")->add_child_text (raw_convert<string> (i));
393 for (auto const& i: _markers) {
394 auto marker = node->add_child("Marker");
395 marker->set_attribute("type", dcp::marker_to_string(i.first));
396 marker->add_child_text(raw_convert<string>(i.second.get()));
399 for (auto i: _ratings) {
400 auto rating = node->add_child("Rating");
404 for (auto i: _content_versions) {
405 node->add_child("ContentVersion")->add_child_text(i);
410 DCPContent::full_length (shared_ptr<const Film> film) const
415 FrameRateChange const frc (film, shared_from_this());
416 return DCPTime::from_frames (llrint(video->length() * frc.factor()), film->video_frame_rate());
420 DCPContent::approximate_length () const
425 return DCPTime::from_frames (video->length(), 24);
429 DCPContent::identifier () const
431 string s = Content::identifier() + "_";
434 s += video->identifier() + "_";
438 s += i->identifier () + " ";
441 s += string (_reference_video ? "1" : "0");
442 for (auto text: _reference_text) {
443 s += string(text ? "1" : "0");
449 DCPContent::add_kdm (dcp::EncryptedKDM k)
455 DCPContent::add_ov (boost::filesystem::path ov)
461 DCPContent::can_be_played () const
463 return !needs_kdm() && !needs_assets();
467 DCPContent::needs_kdm () const
469 boost::mutex::scoped_lock lm (_mutex);
470 return _encrypted && !_kdm_valid;
474 DCPContent::needs_assets () const
476 boost::mutex::scoped_lock lm (_mutex);
477 return _needs_assets;
480 vector<boost::filesystem::path>
481 DCPContent::directories () const
483 return dcp::DCP::directories_from_files (paths());
487 DCPContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
489 Content::add_properties (film, p);
491 video->add_properties (p);
494 audio->add_properties (film, p);
499 DCPContent::set_default_colour_conversion ()
501 /* Default to no colour conversion for DCPs */
503 video->unset_colour_conversion ();
508 DCPContent::set_reference_video (bool r)
510 ContentChangeSignaller cc (this, DCPContentProperty::REFERENCE_VIDEO);
513 boost::mutex::scoped_lock lm (_mutex);
514 _reference_video = r;
519 DCPContent::set_reference_audio (bool r)
521 ContentChangeSignaller cc (this, DCPContentProperty::REFERENCE_AUDIO);
524 boost::mutex::scoped_lock lm (_mutex);
525 _reference_audio = r;
530 DCPContent::set_reference_text (TextType type, bool r)
532 ContentChangeSignaller cc (this, DCPContentProperty::REFERENCE_TEXT);
535 boost::mutex::scoped_lock lm (_mutex);
536 _reference_text[type] = r;
541 DCPContent::reels (shared_ptr<const Film> film) const
543 auto reel_lengths = _reel_lengths;
544 if (reel_lengths.empty()) {
545 /* Old metadata with no reel lengths; get them here instead */
547 scoped_ptr<DCPExaminer> examiner (new DCPExaminer(shared_from_this(), film->tolerant()));
548 reel_lengths = examiner->reel_lengths ();
550 /* Could not examine the DCP; guess reels */
551 reel_lengths.push_back (length_after_trim(film).frames_round(film->video_frame_rate()));
555 list<DCPTimePeriod> p;
557 /* This content's frame rate must be the same as the output DCP rate, so we can
558 convert `directly' from ContentTime to DCPTime.
561 /* The starting point of this content on the timeline */
562 auto pos = position() - DCPTime (trim_start().get());
564 for (auto i: reel_lengths) {
565 /* This reel runs from `pos' to `to' */
566 DCPTime const to = pos + DCPTime::from_frames (i, film->video_frame_rate());
567 if (to > position()) {
568 p.push_back (DCPTimePeriod(max(position(), pos), min(end(film), to)));
569 if (to > end(film)) {
580 DCPContent::reel_split_points (shared_ptr<const Film> film) const
583 for (auto i: reels(film)) {
584 s.push_back (i.from);
590 DCPContent::can_reference (shared_ptr<const Film> film, function<bool (shared_ptr<const Content>)> part, string overlapping, string& why_not) const
592 /* We must be using the same standard as the film */
594 if (_standard.get() == dcp::Standard::INTEROP && !film->interop()) {
595 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
596 why_not = _("it is Interop and the film is set to SMPTE.");
598 } else if (_standard.get() == dcp::Standard::SMPTE && film->interop()) {
599 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
600 why_not = _("it is SMPTE and the film is set to Interop.");
605 /* And the same frame rate */
606 if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film->video_frame_rate())) {
607 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
608 why_not = _("it has a different frame rate to the film.");
612 auto const fr = film->reels ();
614 list<DCPTimePeriod> reel_list;
616 reel_list = reels (film);
617 } catch (dcp::ReadError &) {
618 /* We couldn't read the DCP; it's probably missing */
620 } catch (dcp::KDMDecryptionError &) {
621 /* We have an incorrect KDM */
625 /* fr must contain reels(). It can also contain other reels, but it must at
626 least contain reels().
628 for (auto i: reel_list) {
629 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
630 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
631 why_not = _("its reel lengths differ from those in the film; set the reel mode to 'split by video content'.");
636 auto a = overlaps (film, film->content(), part, position(), end(film));
637 if (a.size() != 1 || a.front().get() != this) {
638 why_not = overlapping;
646 bool check_video (shared_ptr<const Content> c)
648 return static_cast<bool>(c->video) && c->video->use();
652 DCPContent::can_reference_video (shared_ptr<const Film> film, string& why_not) const
655 why_not = _("There is no video in this DCP");
659 if (film->resolution() != resolution()) {
660 if (resolution() == Resolution::FOUR_K) {
661 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
662 why_not = _("it is 4K and the film is 2K.");
664 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
665 why_not = _("it is 2K and the film is 4K.");
668 } else if (film->frame_size() != video->size()) {
669 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
670 why_not = _("its video frame size differs from the film's.");
674 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
675 return can_reference (film, bind (&check_video, _1), _("it overlaps other video content; remove the other content."), why_not);
679 bool check_audio (shared_ptr<const Content> c)
681 return static_cast<bool>(c->audio) && !c->audio->mapping().mapped_output_channels().empty();
685 DCPContent::can_reference_audio (shared_ptr<const Film> film, string& why_not) const
687 shared_ptr<DCPDecoder> decoder;
689 decoder = make_shared<DCPDecoder>(film, shared_from_this(), false, film->tolerant(), shared_ptr<DCPDecoder>());
690 } catch (dcp::ReadError &) {
691 /* We couldn't read the DCP, so it's probably missing */
693 } catch (DCPError &) {
694 /* We couldn't read the DCP, so it's probably missing */
696 } catch (dcp::KDMDecryptionError &) {
697 /* We have an incorrect KDM */
701 for (auto i: decoder->reels()) {
702 if (!i->main_sound()) {
703 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
704 why_not = _("it does not have sound in all its reels.");
709 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
710 return can_reference (film, bind (&check_audio, _1), _("it overlaps other audio content; remove the other content."), why_not);
714 bool check_text (shared_ptr<const Content> c)
716 return !c->text.empty();
720 DCPContent::can_reference_text (shared_ptr<const Film> film, TextType type, string& why_not) const
722 shared_ptr<DCPDecoder> decoder;
724 decoder = make_shared<DCPDecoder>(film, shared_from_this(), false, film->tolerant(), shared_ptr<DCPDecoder>());
725 } catch (dcp::ReadError &) {
726 /* We couldn't read the DCP, so it's probably missing */
728 } catch (dcp::KDMDecryptionError &) {
729 /* We have an incorrect KDM */
733 for (auto i: decoder->reels()) {
734 if (type == TextType::OPEN_SUBTITLE) {
735 if (!i->main_subtitle()) {
736 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
737 why_not = _("it does not have open subtitles in all its reels.");
739 } else if (i->main_subtitle()->entry_point().get_value_or(0) != 0) {
740 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
741 why_not = _("one of its subtitle reels has a non-zero entry point so it must be re-written.");
745 if (type == TextType::CLOSED_CAPTION) {
746 if (i->closed_captions().empty()) {
747 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
748 why_not = _("it does not have closed captions in all its reels.");
751 for (auto j: i->closed_captions()) {
752 if (j->entry_point().get_value_or(0) != 0) {
753 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
754 why_not = _("one of its closed caption has a non-zero entry point so it must be re-written.");
761 if (trim_start() != dcpomatic::ContentTime()) {
762 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
763 why_not = _("it has a start trim so its subtitles or closed captions must be re-written.");
767 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
768 return can_reference (film, bind (&check_text, _1), _("it overlaps other text content; remove the other content."), why_not);
772 DCPContent::take_settings_from (shared_ptr<const Content> c)
774 auto dc = dynamic_pointer_cast<const DCPContent>(c);
779 _reference_video = dc->_reference_video;
780 _reference_audio = dc->_reference_audio;
781 _reference_text = dc->_reference_text;
785 DCPContent::set_cpl (string id)
787 ContentChangeSignaller cc (this, DCPContentProperty::CPL);
790 boost::mutex::scoped_lock lm (_mutex);
796 DCPContent::kdm_timing_window_valid () const
803 return _kdm->not_valid_before() < now && now < _kdm->not_valid_after();
808 DCPContent::resolution () const
810 if (video->size().width > 2048 || video->size().height > 1080) {
811 return Resolution::FOUR_K;
814 return Resolution::TWO_K;
819 add_fonts_from_examiner(shared_ptr<TextContent> text, vector<vector<shared_ptr<Font>>> const & all_fonts)
822 for (auto reel_fonts: all_fonts) {
823 for (auto font: reel_fonts) {
824 /* Each reel could have its own font with the same ID, so we disambiguate them here
825 * by prepending the reel number. We do the same disambiguation when emitting the
826 * subtitles in the DCP decoder.
828 font->set_id(id_for_font_in_reel(font->id(), reel_number));
829 text->add_font(font);
838 id_for_font_in_reel(string id, int reel)
840 return String::compose("%1_%2", reel, id);
845 DCPContent::check_font_ids()
851 DCPExaminer examiner(shared_from_this(), true);
852 add_fonts_from_examiner(text.front(), examiner.fonts());