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);
136 _cpl = node->optional_string_child("CPL");
137 BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("ReelLength")) {
138 _reel_lengths.push_back (raw_convert<int64_t> (i->content ()));
143 DCPContent::read_directory (boost::filesystem::path p)
145 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
146 if (boost::filesystem::is_regular_file (i->path())) {
147 add_path (i->path());
148 } else if (boost::filesystem::is_directory (i->path ())) {
149 read_directory (i->path());
155 DCPContent::examine (shared_ptr<Job> job)
157 bool const needed_assets = needs_assets ();
158 bool const needed_kdm = needs_kdm ();
159 string const old_name = name ();
160 int const old_texts = text.size ();
162 ChangeSignaller<Content> cc_texts (this, DCPContentProperty::TEXTS);
163 ChangeSignaller<Content> cc_assets (this, DCPContentProperty::NEEDS_ASSETS);
164 ChangeSignaller<Content> cc_kdm (this, DCPContentProperty::NEEDS_KDM);
165 ChangeSignaller<Content> cc_name (this, DCPContentProperty::NAME);
166 ChangeSignaller<Content> cc_streams (this, AudioContentProperty::STREAMS);
169 job->set_progress_unknown ();
171 Content::examine (job);
173 shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
175 if (examiner->has_video()) {
177 boost::mutex::scoped_lock lm (_mutex);
178 video.reset (new VideoContent (this));
180 video->take_from_examiner (examiner);
181 set_default_colour_conversion ();
184 if (examiner->has_audio()) {
185 ChangeSignaller<Content> cc (this, AudioContentProperty::STREAMS);
187 boost::mutex::scoped_lock lm (_mutex);
188 audio.reset (new AudioContent (this));
190 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
191 audio->set_stream (as);
192 AudioMapping m = as->mapping ();
193 film()->make_audio_mapping_default (m);
199 boost::mutex::scoped_lock lm (_mutex);
200 _name = examiner->name ();
201 for (int i = 0; i < TEXT_COUNT; ++i) {
202 if (examiner->has_text(static_cast<TextType>(i))) {
203 text.push_back (shared_ptr<TextContent>(new TextContent(this, static_cast<TextType>(i), static_cast<TextType>(i))));
206 texts = text.size ();
207 _encrypted = examiner->encrypted ();
208 _needs_assets = examiner->needs_assets ();
209 _kdm_valid = examiner->kdm_valid ();
210 _standard = examiner->standard ();
211 _three_d = examiner->three_d ();
212 _cpl = examiner->cpl ();
213 _reel_lengths = examiner->reel_lengths ();
216 if (old_texts == texts) {
220 if (needed_assets == needs_assets()) {
224 if (needed_kdm == needs_kdm()) {
228 if (old_name == name()) {
233 video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
238 DCPContent::summary () const
240 boost::mutex::scoped_lock lm (_mutex);
241 return String::compose (_("%1 [DCP]"), _name);
245 DCPContent::technical_summary () const
247 string s = Content::technical_summary() + " - ";
249 s += video->technical_summary() + " - ";
252 s += audio->technical_summary() + " - ";
258 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
260 node->add_child("Type")->add_child_text ("DCP");
262 Content::as_xml (node, with_paths);
265 video->as_xml (node);
269 audio->as_xml (node);
270 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
271 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
272 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
275 BOOST_FOREACH (shared_ptr<TextContent> i, text) {
279 boost::mutex::scoped_lock lm (_mutex);
280 node->add_child("Name")->add_child_text (_name);
281 node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
282 node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
284 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
286 node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
287 node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
288 node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
289 node->add_child("ReferenceOpenSubtitle")->add_child_text(_reference_text[TEXT_OPEN_SUBTITLE] ? "1" : "0");
290 node->add_child("ReferenceClosedCaption")->add_child_text(_reference_text[TEXT_CLOSED_CAPTION] ? "1" : "0");
292 switch (_standard.get ()) {
294 node->add_child("Standard")->add_child_text ("Interop");
297 node->add_child("Standard")->add_child_text ("SMPTE");
300 DCPOMATIC_ASSERT (false);
303 node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
305 node->add_child("CPL")->add_child_text (_cpl.get ());
307 BOOST_FOREACH (int64_t i, _reel_lengths) {
308 node->add_child("ReelLength")->add_child_text (raw_convert<string> (i));
313 DCPContent::full_length () const
318 FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
319 return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
323 DCPContent::identifier () const
325 string s = Content::identifier() + "_";
328 s += video->identifier() + "_";
331 BOOST_FOREACH (shared_ptr<TextContent> i, text) {
332 s += i->identifier () + " ";
335 s += string (_reference_video ? "1" : "0");
336 for (int i = 0; i < TEXT_COUNT; ++i) {
337 s += string (_reference_text[i] ? "1" : "0");
343 DCPContent::add_kdm (dcp::EncryptedKDM k)
349 DCPContent::add_ov (boost::filesystem::path ov)
355 DCPContent::can_be_played () const
357 return !needs_kdm() && !needs_assets();
361 DCPContent::needs_kdm () const
363 boost::mutex::scoped_lock lm (_mutex);
364 return _encrypted && !_kdm_valid;
368 DCPContent::needs_assets () const
370 boost::mutex::scoped_lock lm (_mutex);
371 return _needs_assets;
374 vector<boost::filesystem::path>
375 DCPContent::directories () const
377 return dcp::DCP::directories_from_files (paths());
381 DCPContent::add_properties (list<UserProperty>& p) const
383 Content::add_properties (p);
385 video->add_properties (p);
388 audio->add_properties (p);
393 DCPContent::set_default_colour_conversion ()
395 /* Default to no colour conversion for DCPs */
397 video->unset_colour_conversion ();
402 DCPContent::set_reference_video (bool r)
404 ChangeSignaller<Content> cc (this, DCPContentProperty::REFERENCE_VIDEO);
407 boost::mutex::scoped_lock lm (_mutex);
408 _reference_video = r;
413 DCPContent::set_reference_audio (bool r)
415 ChangeSignaller<Content> cc (this, DCPContentProperty::REFERENCE_AUDIO);
418 boost::mutex::scoped_lock lm (_mutex);
419 _reference_audio = r;
424 DCPContent::set_reference_text (TextType type, bool r)
426 ChangeSignaller<Content> cc (this, DCPContentProperty::REFERENCE_TEXT);
429 boost::mutex::scoped_lock lm (_mutex);
430 _reference_text[type] = r;
435 DCPContent::reels () const
437 list<int64_t> reel_lengths = _reel_lengths;
438 if (reel_lengths.empty ()) {
439 /* Old metadata with no reel lengths; get them here instead */
441 scoped_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this()));
442 reel_lengths = examiner->reel_lengths ();
444 /* Could not examine the DCP; guess reels */
445 reel_lengths.push_back (length_after_trim().frames_round (film()->video_frame_rate ()));
449 list<DCPTimePeriod> p;
451 /* This content's frame rate must be the same as the output DCP rate, so we can
452 convert `directly' from ContentTime to DCPTime.
455 /* The starting point of this content on the timeline */
456 DCPTime pos = position() - DCPTime (trim_start().get());
458 BOOST_FOREACH (int64_t i, reel_lengths) {
459 /* This reel runs from `pos' to `to' */
460 DCPTime const to = pos + DCPTime::from_frames (i, film()->video_frame_rate());
461 if (to > position()) {
462 p.push_back (DCPTimePeriod (max(position(), pos), min(end(), to)));
474 DCPContent::reel_split_points () const
477 BOOST_FOREACH (DCPTimePeriod i, reels()) {
478 s.push_back (i.from);
484 DCPContent::can_reference (function<bool (shared_ptr<const Content>)> part, string overlapping, string& why_not) const
486 /* We must be using the same standard as the film */
488 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
489 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
490 why_not = _("it is Interop and the film is set to SMPTE.");
492 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
493 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
494 why_not = _("it is SMPTE and the film is set to Interop.");
499 /* And the same frame rate */
500 if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film()->video_frame_rate())) {
501 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
502 why_not = _("it has a different frame rate to the film.");
506 list<DCPTimePeriod> const fr = film()->reels ();
508 list<DCPTimePeriod> reel_list;
510 reel_list = reels ();
511 } catch (dcp::DCPReadError) {
512 /* We couldn't read the DCP; it's probably missing */
514 } catch (dcp::KDMDecryptionError) {
515 /* We have an incorrect KDM */
519 /* fr must contain reels(). It can also contain other reels, but it must at
520 least contain reels().
522 BOOST_FOREACH (DCPTimePeriod i, reel_list) {
523 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
524 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
525 why_not = _("its reel lengths differ from those in the film; set the reel mode to 'split by video content'.");
530 ContentList a = overlaps (film()->content(), part, position(), end());
531 if (a.size() != 1 || a.front().get() != this) {
532 why_not = overlapping;
540 bool check_video (shared_ptr<const Content> c)
542 return static_cast<bool>(c->video);
546 DCPContent::can_reference_video (string& why_not) const
549 why_not = _("There is no video in this DCP");
553 if (film()->frame_size() != video->size()) {
554 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
555 why_not = _("its video frame size differs from the film's.");
559 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
560 return can_reference (bind (&check_video, _1), _("it overlaps other video content; remove the other content."), why_not);
564 bool check_audio (shared_ptr<const Content> c)
566 return static_cast<bool>(c->audio);
570 DCPContent::can_reference_audio (string& why_not) const
572 shared_ptr<DCPDecoder> decoder;
574 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
575 } catch (dcp::DCPReadError) {
576 /* We couldn't read the DCP, so it's probably missing */
579 /* We couldn't read the DCP, so it's probably missing */
581 } catch (dcp::KDMDecryptionError) {
582 /* We have an incorrect KDM */
586 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
587 if (!i->main_sound()) {
588 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
589 why_not = _("it does not have sound in all its reels.");
594 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
595 return can_reference (bind (&check_audio, _1), _("it overlaps other audio content; remove the other content."), why_not);
599 bool check_text (shared_ptr<const Content> c)
601 return !c->text.empty();
605 DCPContent::can_reference_text (TextType type, string& why_not) const
607 shared_ptr<DCPDecoder> decoder;
609 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
610 } catch (dcp::DCPReadError) {
611 /* We couldn't read the DCP, so it's probably missing */
613 } catch (dcp::KDMDecryptionError) {
614 /* We have an incorrect KDM */
618 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
619 if (type == TEXT_OPEN_SUBTITLE && !i->main_subtitle()) {
620 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
621 why_not = _("it does not have open subtitles in all its reels.");
624 if (type == TEXT_CLOSED_CAPTION && i->closed_captions().empty()) {
625 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
626 why_not = _("it does not have closed captions in all its reels.");
631 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
632 return can_reference (bind (&check_text, _1), _("it overlaps other text content; remove the other content."), why_not);
636 DCPContent::take_settings_from (shared_ptr<const Content> c)
638 shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (c);
643 _reference_video = dc->_reference_video;
644 _reference_audio = dc->_reference_audio;
645 for (int i = 0; i < TEXT_COUNT; ++i) {
646 _reference_text[i] = dc->_reference_text[i];
651 DCPContent::set_cpl (string id)
653 ChangeSignaller<Content> cc (this, DCPContentProperty::CPL);
656 boost::mutex::scoped_lock lm (_mutex);