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 "caption_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_CAPTION = 604;
62 int const DCPContentProperty::NAME = 605;
63 int const DCPContentProperty::CAPTIONS = 606;
65 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
68 , _needs_assets (false)
70 , _reference_video (false)
71 , _reference_audio (false)
75 set_default_colour_conversion ();
77 for (int i = 0; i < CAPTION_COUNT; ++i) {
78 _reference_caption[i] = false;
82 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
83 : Content (film, node)
85 video = VideoContent::from_xml (this, node, version);
86 audio = AudioContent::from_xml (this, node, version);
87 caption = CaptionContent::from_xml (this, node, version);
89 for (int i = 0; i < CAPTION_COUNT; ++i) {
90 _reference_caption[i] = false;
97 node->number_child<int> ("AudioFrameRate"),
98 /* AudioLength was not present in some old metadata versions */
99 node->optional_number_child<Frame>("AudioLength").get_value_or (
100 video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
102 AudioMapping (node->node_child ("AudioMapping"), version)
108 _name = node->string_child ("Name");
109 _encrypted = node->bool_child ("Encrypted");
110 _needs_assets = node->optional_bool_child("NeedsAssets").get_value_or (false);
111 if (node->optional_node_child ("KDM")) {
112 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
114 _kdm_valid = node->bool_child ("KDMValid");
115 _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
116 _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
118 _reference_caption[CAPTION_OPEN] = node->optional_bool_child("ReferenceOpenCaption").get_value_or(false);
119 _reference_caption[CAPTION_CLOSED] = node->optional_bool_child("ReferenceClosedCaption").get_value_or(false);
121 _reference_caption[CAPTION_OPEN] = node->optional_bool_child("ReferenceSubtitle").get_value_or(false);
122 _reference_caption[CAPTION_CLOSED] = false;
124 if (node->optional_string_child("Standard")) {
125 string const s = node->optional_string_child("Standard").get();
126 if (s == "Interop") {
127 _standard = dcp::INTEROP;
128 } else if (s == "SMPTE") {
129 _standard = dcp::SMPTE;
131 DCPOMATIC_ASSERT (false);
134 _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
135 _cpl = node->optional_string_child("CPL");
136 BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("ReelLength")) {
137 _reel_lengths.push_back (raw_convert<int64_t> (i->content ()));
142 DCPContent::read_directory (boost::filesystem::path p)
144 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
145 if (boost::filesystem::is_regular_file (i->path())) {
146 _paths.push_back (i->path());
147 } else if (boost::filesystem::is_directory (i->path ())) {
148 read_directory (i->path());
154 DCPContent::examine (shared_ptr<Job> job)
156 bool const needed_assets = needs_assets ();
157 bool const needed_kdm = needs_kdm ();
158 string const old_name = name ();
159 int const old_captions = caption.size ();
162 job->set_progress_unknown ();
164 Content::examine (job);
166 shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
168 if (examiner->has_video()) {
170 boost::mutex::scoped_lock lm (_mutex);
171 video.reset (new VideoContent (this));
173 video->take_from_examiner (examiner);
174 set_default_colour_conversion ();
177 if (examiner->has_audio()) {
179 boost::mutex::scoped_lock lm (_mutex);
180 audio.reset (new AudioContent (this));
182 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
183 audio->set_stream (as);
184 AudioMapping m = as->mapping ();
185 film()->make_audio_mapping_default (m);
187 signal_changed (AudioContentProperty::STREAMS);
192 boost::mutex::scoped_lock lm (_mutex);
193 _name = examiner->name ();
194 for (int i = 0; i < examiner->captions(); ++i) {
195 caption.push_back (shared_ptr<CaptionContent> (new CaptionContent (this)));
197 captions = caption.size ();
198 _encrypted = examiner->encrypted ();
199 _needs_assets = examiner->needs_assets ();
200 _kdm_valid = examiner->kdm_valid ();
201 _standard = examiner->standard ();
202 _three_d = examiner->three_d ();
203 _cpl = examiner->cpl ();
204 _reel_lengths = examiner->reel_lengths ();
207 if (old_captions != captions) {
208 signal_changed (DCPContentProperty::CAPTIONS);
211 if (needed_assets != needs_assets ()) {
212 signal_changed (DCPContentProperty::NEEDS_ASSETS);
215 if (needed_kdm != needs_kdm ()) {
216 signal_changed (DCPContentProperty::NEEDS_KDM);
219 if (old_name != name ()) {
220 signal_changed (DCPContentProperty::NAME);
223 signal_changed (AudioContentProperty::STREAMS);
226 video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
231 DCPContent::summary () const
233 boost::mutex::scoped_lock lm (_mutex);
234 return String::compose (_("%1 [DCP]"), _name);
238 DCPContent::technical_summary () const
240 string s = Content::technical_summary() + " - ";
242 s += video->technical_summary() + " - ";
245 s += audio->technical_summary() + " - ";
251 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
253 node->add_child("Type")->add_child_text ("DCP");
255 Content::as_xml (node, with_paths);
258 video->as_xml (node);
262 audio->as_xml (node);
263 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
264 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
265 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
268 BOOST_FOREACH (shared_ptr<CaptionContent> i, caption) {
272 boost::mutex::scoped_lock lm (_mutex);
273 node->add_child("Name")->add_child_text (_name);
274 node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
275 node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
277 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
279 node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
280 node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
281 node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
282 node->add_child("ReferenceOpenCaption")->add_child_text(_reference_caption[CAPTION_OPEN] ? "1" : "0");
283 node->add_child("ReferenceClosedCaption")->add_child_text(_reference_caption[CAPTION_CLOSED] ? "1" : "0");
285 switch (_standard.get ()) {
287 node->add_child("Standard")->add_child_text ("Interop");
290 node->add_child("Standard")->add_child_text ("SMPTE");
293 DCPOMATIC_ASSERT (false);
296 node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
298 node->add_child("CPL")->add_child_text (_cpl.get ());
300 BOOST_FOREACH (int64_t i, _reel_lengths) {
301 node->add_child("ReelLength")->add_child_text (raw_convert<string> (i));
306 DCPContent::full_length () const
311 FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
312 return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
316 DCPContent::identifier () const
318 string s = Content::identifier() + "_";
321 s += video->identifier() + "_";
324 BOOST_FOREACH (shared_ptr<CaptionContent> i, caption) {
325 s += i->identifier () + " ";
328 s += string (_reference_video ? "1" : "0");
329 for (int i = 0; i < CAPTION_COUNT; ++i) {
330 s += string (_reference_caption[i] ? "1" : "0");
336 DCPContent::add_kdm (dcp::EncryptedKDM k)
342 DCPContent::add_ov (boost::filesystem::path ov)
348 DCPContent::can_be_played () const
350 return !needs_kdm() && !needs_assets();
354 DCPContent::needs_kdm () const
356 boost::mutex::scoped_lock lm (_mutex);
357 return _encrypted && !_kdm_valid;
361 DCPContent::needs_assets () const
363 boost::mutex::scoped_lock lm (_mutex);
364 return _needs_assets;
367 vector<boost::filesystem::path>
368 DCPContent::directories () const
370 return dcp::DCP::directories_from_files (paths());
374 DCPContent::add_properties (list<UserProperty>& p) const
376 Content::add_properties (p);
378 video->add_properties (p);
381 audio->add_properties (p);
386 DCPContent::set_default_colour_conversion ()
388 /* Default to no colour conversion for DCPs */
390 video->unset_colour_conversion ();
395 DCPContent::set_reference_video (bool r)
398 boost::mutex::scoped_lock lm (_mutex);
399 _reference_video = r;
402 signal_changed (DCPContentProperty::REFERENCE_VIDEO);
406 DCPContent::set_reference_audio (bool r)
409 boost::mutex::scoped_lock lm (_mutex);
410 _reference_audio = r;
413 signal_changed (DCPContentProperty::REFERENCE_AUDIO);
417 DCPContent::set_reference_caption (CaptionType type, bool r)
420 boost::mutex::scoped_lock lm (_mutex);
421 _reference_caption[type] = r;
424 signal_changed (DCPContentProperty::REFERENCE_CAPTION);
428 DCPContent::reels () const
430 list<int64_t> reel_lengths = _reel_lengths;
431 if (reel_lengths.empty ()) {
432 /* Old metadata with no reel lengths; get them here instead */
434 scoped_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this()));
435 reel_lengths = examiner->reel_lengths ();
437 /* Could not examine the DCP; guess reels */
438 reel_lengths.push_back (length_after_trim().frames_round (film()->video_frame_rate ()));
442 list<DCPTimePeriod> p;
444 /* This content's frame rate must be the same as the output DCP rate, so we can
445 convert `directly' from ContentTime to DCPTime.
448 /* The starting point of this content on the timeline */
449 DCPTime pos = position() - DCPTime (trim_start().get());
451 BOOST_FOREACH (int64_t i, reel_lengths) {
452 /* This reel runs from `pos' to `to' */
453 DCPTime const to = pos + DCPTime::from_frames (i, film()->video_frame_rate());
454 if (to > position()) {
455 p.push_back (DCPTimePeriod (max(position(), pos), min(end(), to)));
467 DCPContent::reel_split_points () const
470 BOOST_FOREACH (DCPTimePeriod i, reels()) {
471 s.push_back (i.from);
477 DCPContent::can_reference (function<bool (shared_ptr<const Content>)> part, string overlapping, string& why_not) const
479 /* We must be using the same standard as the film */
481 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
482 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
483 why_not = _("it is Interop and the film is set to SMPTE.");
485 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
486 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
487 why_not = _("it is SMPTE and the film is set to Interop.");
492 /* And the same frame rate */
493 if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film()->video_frame_rate())) {
494 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
495 why_not = _("it has a different frame rate to the film.");
499 list<DCPTimePeriod> const fr = film()->reels ();
501 list<DCPTimePeriod> reel_list;
503 reel_list = reels ();
504 } catch (dcp::DCPReadError) {
505 /* We couldn't read the DCP; it's probably missing */
507 } catch (dcp::KDMDecryptionError) {
508 /* We have an incorrect KDM */
512 /* fr must contain reels(). It can also contain other reels, but it must at
513 least contain reels().
515 BOOST_FOREACH (DCPTimePeriod i, reel_list) {
516 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
517 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
518 why_not = _("its reel lengths differ from those in the film; set the reel mode to 'split by video content'.");
523 ContentList a = overlaps (film()->content(), part, position(), end());
524 if (a.size() != 1 || a.front().get() != this) {
525 why_not = overlapping;
533 bool check_video (shared_ptr<const Content> c)
535 return static_cast<bool>(c->video);
539 DCPContent::can_reference_video (string& why_not) const
542 why_not = _("There is no video in this DCP");
546 if (film()->frame_size() != video->size()) {
547 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
548 why_not = _("its video frame size differs from the film's.");
552 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
553 return can_reference (bind (&check_video, _1), _("it overlaps other video content; remove the other content."), why_not);
557 bool check_audio (shared_ptr<const Content> c)
559 return static_cast<bool>(c->audio);
563 DCPContent::can_reference_audio (string& why_not) const
565 shared_ptr<DCPDecoder> decoder;
567 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
568 } catch (dcp::DCPReadError) {
569 /* We couldn't read the DCP, so it's probably missing */
572 /* We couldn't read the DCP, so it's probably missing */
574 } catch (dcp::KDMDecryptionError) {
575 /* We have an incorrect KDM */
579 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
580 if (!i->main_sound()) {
581 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
582 why_not = _("it does not have sound in all its reels.");
587 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
588 return can_reference (bind (&check_audio, _1), _("it overlaps other audio content; remove the other content."), why_not);
592 bool check_caption (shared_ptr<const Content> c)
594 return !c->caption.empty();
597 DCPContent::can_reference_caption (CaptionType type, string& why_not) const
599 shared_ptr<DCPDecoder> decoder;
601 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
602 } catch (dcp::DCPReadError) {
603 /* We couldn't read the DCP, so it's probably missing */
605 } catch (dcp::KDMDecryptionError) {
606 /* We have an incorrect KDM */
610 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
611 if (type == CAPTION_OPEN && !i->main_subtitle()) {
612 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
613 why_not = _("it does not have subtitles in all its reels.");
616 if (type == CAPTION_CLOSED && !i->closed_caption()) {
617 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
618 why_not = _("it does not have closed captions in all its reels.");
623 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
624 return can_reference (bind (&check_caption, _1), _("it overlaps other caption content; remove the other content."), why_not);
628 DCPContent::take_settings_from (shared_ptr<const Content> c)
630 shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (c);
635 _reference_video = dc->_reference_video;
636 _reference_audio = dc->_reference_audio;
637 for (int i = 0; i < CAPTION_COUNT; ++i) {
638 _reference_caption[i] = dc->_reference_caption[i];
643 DCPContent::set_cpl (string id)
645 boost::mutex::scoped_lock lm (_mutex);