2 Copyright (C) 2014-2018 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/>.
21 #include "dcp_content.h"
22 #include "video_content.h"
23 #include "audio_content.h"
24 #include "dcp_examiner.h"
29 #include "compose.hpp"
30 #include "dcp_decoder.h"
31 #include "text_content.h"
33 #include <dcp/raw_convert.h>
34 #include <dcp/exceptions.h>
35 #include <dcp/reel_picture_asset.h>
37 #include <libxml++/libxml++.h>
38 #include <boost/foreach.hpp>
50 using boost::shared_ptr;
51 using boost::scoped_ptr;
52 using boost::optional;
53 using boost::function;
54 using boost::dynamic_pointer_cast;
55 using dcp::raw_convert;
57 int const DCPContentProperty::NEEDS_ASSETS = 600;
58 int const DCPContentProperty::NEEDS_KDM = 601;
59 int const DCPContentProperty::REFERENCE_VIDEO = 602;
60 int const DCPContentProperty::REFERENCE_AUDIO = 603;
61 int const DCPContentProperty::REFERENCE_TEXT = 604;
62 int const DCPContentProperty::NAME = 605;
63 int const DCPContentProperty::TEXTS = 606;
64 int const DCPContentProperty::CPL = 607;
66 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
69 , _needs_assets (false)
71 , _reference_video (false)
72 , _reference_audio (false)
76 set_default_colour_conversion ();
78 for (int i = 0; i < TEXT_COUNT; ++i) {
79 _reference_text[i] = false;
83 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
84 : Content (film, node)
86 video = VideoContent::from_xml (this, node, version);
87 audio = AudioContent::from_xml (this, node, version);
88 text = TextContent::from_xml (this, node, version);
90 for (int i = 0; i < TEXT_COUNT; ++i) {
91 _reference_text[i] = false;
98 node->number_child<int> ("AudioFrameRate"),
99 /* AudioLength was not present in some old metadata versions */
100 node->optional_number_child<Frame>("AudioLength").get_value_or (
101 video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
103 AudioMapping (node->node_child ("AudioMapping"), version)
109 _name = node->string_child ("Name");
110 _encrypted = node->bool_child ("Encrypted");
111 _needs_assets = node->optional_bool_child("NeedsAssets").get_value_or (false);
112 if (node->optional_node_child ("KDM")) {
113 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
115 _kdm_valid = node->bool_child ("KDMValid");
116 _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
117 _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
119 _reference_text[TEXT_OPEN_SUBTITLE] = node->optional_bool_child("ReferenceOpenSubtitle").get_value_or(false);
120 _reference_text[TEXT_CLOSED_CAPTION] = node->optional_bool_child("ReferenceClosedCaption").get_value_or(false);
122 _reference_text[TEXT_OPEN_SUBTITLE] = node->optional_bool_child("ReferenceSubtitle").get_value_or(false);
123 _reference_text[TEXT_CLOSED_CAPTION] = false;
125 if (node->optional_string_child("Standard")) {
126 string const s = node->optional_string_child("Standard").get();
127 if (s == "Interop") {
128 _standard = dcp::INTEROP;
129 } else if (s == "SMPTE") {
130 _standard = dcp::SMPTE;
132 DCPOMATIC_ASSERT (false);
135 _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
137 optional<string> ck = node->optional_string_child("ContentKind");
139 _content_kind = dcp::content_kind_from_string (*ck);
141 _cpl = node->optional_string_child("CPL");
142 BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("ReelLength")) {
143 _reel_lengths.push_back (raw_convert<int64_t> (i->content ()));
148 DCPContent::read_directory (boost::filesystem::path p)
150 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
151 if (boost::filesystem::is_regular_file (i->path())) {
152 add_path (i->path());
153 } else if (boost::filesystem::is_directory (i->path ())) {
154 read_directory (i->path());
160 DCPContent::examine (shared_ptr<Job> job)
162 bool const needed_assets = needs_assets ();
163 bool const needed_kdm = needs_kdm ();
164 string const old_name = name ();
165 int const old_texts = text.size ();
167 ChangeSignaller<Content> cc_texts (this, DCPContentProperty::TEXTS);
168 ChangeSignaller<Content> cc_assets (this, DCPContentProperty::NEEDS_ASSETS);
169 ChangeSignaller<Content> cc_kdm (this, DCPContentProperty::NEEDS_KDM);
170 ChangeSignaller<Content> cc_name (this, DCPContentProperty::NAME);
171 ChangeSignaller<Content> cc_streams (this, AudioContentProperty::STREAMS);
174 job->set_progress_unknown ();
176 Content::examine (job);
178 shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
180 if (examiner->has_video()) {
182 boost::mutex::scoped_lock lm (_mutex);
183 video.reset (new VideoContent (this));
185 video->take_from_examiner (examiner);
186 set_default_colour_conversion ();
189 if (examiner->has_audio()) {
190 ChangeSignaller<Content> cc (this, AudioContentProperty::STREAMS);
192 boost::mutex::scoped_lock lm (_mutex);
193 audio.reset (new AudioContent (this));
195 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
196 audio->set_stream (as);
197 AudioMapping m = as->mapping ();
198 film()->make_audio_mapping_default (m);
204 boost::mutex::scoped_lock lm (_mutex);
205 _name = examiner->name ();
206 for (int i = 0; i < TEXT_COUNT; ++i) {
207 if (examiner->has_text(static_cast<TextType>(i))) {
208 text.push_back (shared_ptr<TextContent>(new TextContent(this, static_cast<TextType>(i), static_cast<TextType>(i))));
211 texts = text.size ();
212 _encrypted = examiner->encrypted ();
213 _needs_assets = examiner->needs_assets ();
214 _kdm_valid = examiner->kdm_valid ();
215 _standard = examiner->standard ();
216 _three_d = examiner->three_d ();
217 _content_kind = examiner->content_kind ();
218 _cpl = examiner->cpl ();
219 _reel_lengths = examiner->reel_lengths ();
222 if (old_texts == texts) {
226 if (needed_assets == needs_assets()) {
230 if (needed_kdm == needs_kdm()) {
234 if (old_name == name()) {
239 video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
244 DCPContent::summary () const
246 boost::mutex::scoped_lock lm (_mutex);
247 return String::compose (_("%1 [DCP]"), _name);
251 DCPContent::technical_summary () const
253 string s = Content::technical_summary() + " - ";
255 s += video->technical_summary() + " - ";
258 s += audio->technical_summary() + " - ";
264 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
266 node->add_child("Type")->add_child_text ("DCP");
268 Content::as_xml (node, with_paths);
271 video->as_xml (node);
275 audio->as_xml (node);
276 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
277 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
278 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
281 BOOST_FOREACH (shared_ptr<TextContent> i, text) {
285 boost::mutex::scoped_lock lm (_mutex);
286 node->add_child("Name")->add_child_text (_name);
287 node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
288 node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
290 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
292 node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
293 node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
294 node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
295 node->add_child("ReferenceOpenSubtitle")->add_child_text(_reference_text[TEXT_OPEN_SUBTITLE] ? "1" : "0");
296 node->add_child("ReferenceClosedCaption")->add_child_text(_reference_text[TEXT_CLOSED_CAPTION] ? "1" : "0");
298 switch (_standard.get ()) {
300 node->add_child("Standard")->add_child_text ("Interop");
303 node->add_child("Standard")->add_child_text ("SMPTE");
306 DCPOMATIC_ASSERT (false);
309 node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
311 node->add_child("ContentKind")->add_child_text(dcp::content_kind_to_string(*_content_kind));
314 node->add_child("CPL")->add_child_text (_cpl.get ());
316 BOOST_FOREACH (int64_t i, _reel_lengths) {
317 node->add_child("ReelLength")->add_child_text (raw_convert<string> (i));
322 DCPContent::full_length () const
327 FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
328 return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
332 DCPContent::identifier () const
334 string s = Content::identifier() + "_";
337 s += video->identifier() + "_";
340 BOOST_FOREACH (shared_ptr<TextContent> i, text) {
341 s += i->identifier () + " ";
344 s += string (_reference_video ? "1" : "0");
345 for (int i = 0; i < TEXT_COUNT; ++i) {
346 s += string (_reference_text[i] ? "1" : "0");
352 DCPContent::add_kdm (dcp::EncryptedKDM k)
358 DCPContent::add_ov (boost::filesystem::path ov)
364 DCPContent::can_be_played () const
366 return !needs_kdm() && !needs_assets();
370 DCPContent::needs_kdm () const
372 boost::mutex::scoped_lock lm (_mutex);
373 return _encrypted && !_kdm_valid;
377 DCPContent::needs_assets () const
379 boost::mutex::scoped_lock lm (_mutex);
380 return _needs_assets;
383 vector<boost::filesystem::path>
384 DCPContent::directories () const
386 return dcp::DCP::directories_from_files (paths());
390 DCPContent::add_properties (list<UserProperty>& p) const
392 Content::add_properties (p);
394 video->add_properties (p);
397 audio->add_properties (p);
402 DCPContent::set_default_colour_conversion ()
404 /* Default to no colour conversion for DCPs */
406 video->unset_colour_conversion ();
411 DCPContent::set_reference_video (bool r)
413 ChangeSignaller<Content> cc (this, DCPContentProperty::REFERENCE_VIDEO);
416 boost::mutex::scoped_lock lm (_mutex);
417 _reference_video = r;
422 DCPContent::set_reference_audio (bool r)
424 ChangeSignaller<Content> cc (this, DCPContentProperty::REFERENCE_AUDIO);
427 boost::mutex::scoped_lock lm (_mutex);
428 _reference_audio = r;
433 DCPContent::set_reference_text (TextType type, bool r)
435 ChangeSignaller<Content> cc (this, DCPContentProperty::REFERENCE_TEXT);
438 boost::mutex::scoped_lock lm (_mutex);
439 _reference_text[type] = r;
444 DCPContent::reels () const
446 list<int64_t> reel_lengths = _reel_lengths;
447 if (reel_lengths.empty ()) {
448 /* Old metadata with no reel lengths; get them here instead */
450 scoped_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this()));
451 reel_lengths = examiner->reel_lengths ();
453 /* Could not examine the DCP; guess reels */
454 reel_lengths.push_back (length_after_trim().frames_round (film()->video_frame_rate ()));
458 list<DCPTimePeriod> p;
460 /* This content's frame rate must be the same as the output DCP rate, so we can
461 convert `directly' from ContentTime to DCPTime.
464 /* The starting point of this content on the timeline */
465 DCPTime pos = position() - DCPTime (trim_start().get());
467 BOOST_FOREACH (int64_t i, reel_lengths) {
468 /* This reel runs from `pos' to `to' */
469 DCPTime const to = pos + DCPTime::from_frames (i, film()->video_frame_rate());
470 if (to > position()) {
471 p.push_back (DCPTimePeriod (max(position(), pos), min(end(), to)));
483 DCPContent::reel_split_points () const
486 BOOST_FOREACH (DCPTimePeriod i, reels()) {
487 s.push_back (i.from);
493 DCPContent::can_reference (function<bool (shared_ptr<const Content>)> part, string overlapping, string& why_not) const
495 /* We must be using the same standard as the film */
497 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
498 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
499 why_not = _("it is Interop and the film is set to SMPTE.");
501 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
502 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
503 why_not = _("it is SMPTE and the film is set to Interop.");
508 /* And the same frame rate */
509 if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film()->video_frame_rate())) {
510 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
511 why_not = _("it has a different frame rate to the film.");
515 list<DCPTimePeriod> const fr = film()->reels ();
517 list<DCPTimePeriod> reel_list;
519 reel_list = reels ();
520 } catch (dcp::DCPReadError) {
521 /* We couldn't read the DCP; it's probably missing */
523 } catch (dcp::KDMDecryptionError) {
524 /* We have an incorrect KDM */
528 /* fr must contain reels(). It can also contain other reels, but it must at
529 least contain reels().
531 BOOST_FOREACH (DCPTimePeriod i, reel_list) {
532 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
533 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
534 why_not = _("its reel lengths differ from those in the film; set the reel mode to 'split by video content'.");
539 ContentList a = overlaps (film()->content(), part, position(), end());
540 if (a.size() != 1 || a.front().get() != this) {
541 why_not = overlapping;
549 bool check_video (shared_ptr<const Content> c)
551 return static_cast<bool>(c->video);
555 DCPContent::can_reference_video (string& why_not) const
558 why_not = _("There is no video in this DCP");
562 Resolution video_res = RESOLUTION_2K;
563 if (video->size().width > 2048 || video->size().height > 1080) {
564 video_res = RESOLUTION_4K;
567 if (film()->resolution() != video_res) {
568 if (video_res == RESOLUTION_4K) {
569 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
570 why_not = _("it is 4K and the film is 2K.");
572 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
573 why_not = _("it is 2K and the film is 4K.");
576 } else if (film()->frame_size() != video->size()) {
577 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
578 why_not = _("its video frame size differs from the film's.");
582 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
583 return can_reference (bind (&check_video, _1), _("it overlaps other video content; remove the other content."), why_not);
587 bool check_audio (shared_ptr<const Content> c)
589 return static_cast<bool>(c->audio);
593 DCPContent::can_reference_audio (string& why_not) const
595 shared_ptr<DCPDecoder> decoder;
597 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
598 } catch (dcp::DCPReadError) {
599 /* We couldn't read the DCP, so it's probably missing */
602 /* We couldn't read the DCP, so it's probably missing */
604 } catch (dcp::KDMDecryptionError) {
605 /* We have an incorrect KDM */
609 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
610 if (!i->main_sound()) {
611 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
612 why_not = _("it does not have sound in all its reels.");
617 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
618 return can_reference (bind (&check_audio, _1), _("it overlaps other audio content; remove the other content."), why_not);
622 bool check_text (shared_ptr<const Content> c)
624 return !c->text.empty();
628 DCPContent::can_reference_text (TextType type, string& why_not) const
630 shared_ptr<DCPDecoder> decoder;
632 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
633 } catch (dcp::DCPReadError) {
634 /* We couldn't read the DCP, so it's probably missing */
636 } catch (dcp::KDMDecryptionError) {
637 /* We have an incorrect KDM */
641 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
642 if (type == TEXT_OPEN_SUBTITLE && !i->main_subtitle()) {
643 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
644 why_not = _("it does not have open subtitles in all its reels.");
647 if (type == TEXT_CLOSED_CAPTION && i->closed_captions().empty()) {
648 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
649 why_not = _("it does not have closed captions in all its reels.");
654 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
655 return can_reference (bind (&check_text, _1), _("it overlaps other text content; remove the other content."), why_not);
659 DCPContent::take_settings_from (shared_ptr<const Content> c)
661 shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (c);
666 _reference_video = dc->_reference_video;
667 _reference_audio = dc->_reference_audio;
668 for (int i = 0; i < TEXT_COUNT; ++i) {
669 _reference_text[i] = dc->_reference_text[i];
674 DCPContent::set_cpl (string id)
676 ChangeSignaller<Content> cc (this, DCPContentProperty::CPL);
679 boost::mutex::scoped_lock lm (_mutex);
685 DCPContent::kdm_timing_window_valid () const
692 return _kdm->not_valid_before() < now && now < _kdm->not_valid_after();