2 Copyright (C) 2014-2016 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 "subtitle_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_SUBTITLE = 604;
62 int const DCPContentProperty::NAME = 605;
64 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
67 , _needs_assets (false)
69 , _reference_video (false)
70 , _reference_audio (false)
71 , _reference_subtitle (false)
74 video.reset (new VideoContent (this));
75 audio.reset (new AudioContent (this));
78 set_default_colour_conversion ();
81 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
82 : Content (film, node)
84 video = VideoContent::from_xml (this, node, version);
85 audio = AudioContent::from_xml (this, node, version);
86 subtitle = SubtitleContent::from_xml (this, node, version);
91 node->number_child<int> ("AudioFrameRate"),
92 /* AudioLength was not present in some old metadata versions */
93 node->optional_number_child<Frame>("AudioLength").get_value_or (
94 video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
96 AudioMapping (node->node_child ("AudioMapping"), version)
101 _name = node->string_child ("Name");
102 _encrypted = node->bool_child ("Encrypted");
103 _needs_assets = node->optional_bool_child("NeedsAssets").get_value_or (false);
104 if (node->optional_node_child ("KDM")) {
105 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
107 _kdm_valid = node->bool_child ("KDMValid");
108 _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
109 _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
110 _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
111 if (node->optional_string_child("Standard")) {
112 string const s = node->optional_string_child("Standard").get();
113 if (s == "Interop") {
114 _standard = dcp::INTEROP;
115 } else if (s == "SMPTE") {
116 _standard = dcp::SMPTE;
118 DCPOMATIC_ASSERT (false);
121 _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
122 _cpl = node->optional_string_child("CPL");
126 DCPContent::read_directory (boost::filesystem::path p)
128 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
129 if (boost::filesystem::is_regular_file (i->path ())) {
130 _paths.push_back (i->path ());
131 } else if (boost::filesystem::is_directory (i->path ())) {
132 read_directory (i->path ());
138 DCPContent::examine (shared_ptr<Job> job)
140 bool const needed_assets = needs_assets ();
141 bool const needed_kdm = needs_kdm ();
142 string const old_name = name ();
144 job->set_progress_unknown ();
145 Content::examine (job);
147 shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
148 video->take_from_examiner (examiner);
149 set_default_colour_conversion ();
152 boost::mutex::scoped_lock lm (_mutex);
154 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
155 audio->set_stream (as);
156 AudioMapping m = as->mapping ();
157 film()->make_audio_mapping_default (m);
161 signal_changed (AudioContentProperty::STREAMS);
164 boost::mutex::scoped_lock lm (_mutex);
165 _name = examiner->name ();
166 if (examiner->has_subtitles ()) {
167 subtitle.reset (new SubtitleContent (this));
169 _encrypted = examiner->encrypted ();
170 _needs_assets = examiner->needs_assets ();
171 _kdm_valid = examiner->kdm_valid ();
172 _standard = examiner->standard ();
173 _three_d = examiner->three_d ();
174 _cpl = examiner->cpl ();
177 if (needed_assets != needs_assets ()) {
178 signal_changed (DCPContentProperty::NEEDS_ASSETS);
181 if (needed_kdm != needs_kdm ()) {
182 signal_changed (DCPContentProperty::NEEDS_KDM);
185 if (old_name != name ()) {
186 signal_changed (DCPContentProperty::NAME);
189 video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
193 DCPContent::summary () const
195 boost::mutex::scoped_lock lm (_mutex);
196 return String::compose (_("%1 [DCP]"), _name);
200 DCPContent::technical_summary () const
202 return Content::technical_summary() + " - "
203 + video->technical_summary() + " - "
204 + audio->technical_summary() + " - ";
208 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
210 node->add_child("Type")->add_child_text ("DCP");
212 Content::as_xml (node, with_paths);
215 video->as_xml (node);
219 audio->as_xml (node);
220 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
221 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
222 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
226 subtitle->as_xml (node);
229 boost::mutex::scoped_lock lm (_mutex);
230 node->add_child("Name")->add_child_text (_name);
231 node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
232 node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
234 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
236 node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
237 node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
238 node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
239 node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
241 switch (_standard.get ()) {
243 node->add_child("Standard")->add_child_text ("Interop");
246 node->add_child("Standard")->add_child_text ("SMPTE");
249 DCPOMATIC_ASSERT (false);
252 node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
254 node->add_child("CPL")->add_child_text (_cpl.get ());
259 DCPContent::full_length () const
261 FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
262 return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
266 DCPContent::identifier () const
268 string s = Content::identifier() + "_" + video->identifier() + "_";
270 s += subtitle->identifier () + " ";
273 s += string (_reference_video ? "1" : "0") + string (_reference_subtitle ? "1" : "0");
278 DCPContent::add_kdm (dcp::EncryptedKDM k)
284 DCPContent::add_ov (boost::filesystem::path ov)
290 DCPContent::can_be_played () const
292 return !needs_kdm() && !needs_assets();
296 DCPContent::needs_kdm () const
298 boost::mutex::scoped_lock lm (_mutex);
299 return _encrypted && !_kdm_valid;
303 DCPContent::needs_assets () const
305 boost::mutex::scoped_lock lm (_mutex);
306 return _needs_assets;
309 vector<boost::filesystem::path>
310 DCPContent::directories () const
312 return dcp::DCP::directories_from_files (paths ());
316 DCPContent::add_properties (list<UserProperty>& p) const
318 Content::add_properties (p);
319 video->add_properties (p);
320 audio->add_properties (p);
324 DCPContent::set_default_colour_conversion ()
326 /* Default to no colour conversion for DCPs */
327 video->unset_colour_conversion ();
331 DCPContent::set_reference_video (bool r)
334 boost::mutex::scoped_lock lm (_mutex);
335 _reference_video = r;
338 signal_changed (DCPContentProperty::REFERENCE_VIDEO);
342 DCPContent::set_reference_audio (bool r)
345 boost::mutex::scoped_lock lm (_mutex);
346 _reference_audio = r;
349 signal_changed (DCPContentProperty::REFERENCE_AUDIO);
353 DCPContent::set_reference_subtitle (bool r)
356 boost::mutex::scoped_lock lm (_mutex);
357 _reference_subtitle = r;
360 signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
364 DCPContent::reels () const
366 list<DCPTimePeriod> p;
367 scoped_ptr<DCPDecoder> decoder;
369 decoder.reset (new DCPDecoder (shared_from_this(), film()->log()));
371 /* Could not load the DCP; guess reels */
372 list<DCPTimePeriod> p;
373 p.push_back (DCPTimePeriod (position(), end()));
377 /* This content's frame rate must be the same as the output DCP rate, so we can
378 convert `directly' from ContentTime to DCPTime.
381 /* The starting point of this content on the timeline */
382 DCPTime pos = position() - DCPTime (trim_start().get());
384 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels ()) {
385 /* This reel runs from `pos' to `to' */
386 DCPTime const to = pos + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
387 if (to > position()) {
388 p.push_back (DCPTimePeriod (max(position(), pos), min(end(), to)));
400 DCPContent::reel_split_points () const
403 BOOST_FOREACH (DCPTimePeriod i, reels()) {
404 s.push_back (i.from);
410 DCPContent::can_reference (function<shared_ptr<ContentPart> (shared_ptr<const Content>)> part, string overlapping, list<string>& why_not) const
412 /* We must be using the same standard as the film */
414 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
415 why_not.push_back (_("The film is set to SMPTE and this DCP is Interop."));
417 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
418 why_not.push_back (_("The film is set to Interop and this DCP is SMPTE."));
423 /* And the same frame rate */
424 if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film()->video_frame_rate())) {
425 why_not.push_back (_("The film has a different frame rate to this DCP."));
429 list<DCPTimePeriod> const fr = film()->reels ();
431 list<DCPTimePeriod> reel_list;
433 reel_list = reels ();
434 } catch (dcp::DCPReadError) {
435 /* We couldn't read the DCP; it's probably missing */
439 /* fr must contain reels(). It can also contain other reels, but it must at
440 least contain reels().
442 BOOST_FOREACH (DCPTimePeriod i, reel_list) {
443 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
444 why_not.push_back (_("The reel lengths in the film differ from those in the DCP; set the reel mode to 'split by video content'."));
449 ContentList a = overlaps (film()->content(), part, position(), end());
450 if (a.size() != 1 || a.front().get() != this) {
451 why_not.push_back (overlapping);
459 DCPContent::can_reference_video (list<string>& why_not) const
461 if (film()->frame_size() != video->size()) {
462 why_not.push_back (_("The video frame size in the film differs from that in the DCP."));
466 return can_reference (bind (&Content::video, _1), _("There is other video content overlapping this DCP; remove it."), why_not);
470 DCPContent::can_reference_audio (list<string>& why_not) const
472 shared_ptr<DCPDecoder> decoder;
474 decoder.reset (new DCPDecoder (shared_from_this(), film()->log()));
475 } catch (dcp::DCPReadError) {
476 /* We couldn't read the DCP, so it's probably missing */
480 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
481 if (!i->main_sound()) {
482 why_not.push_back (_("The DCP does not have sound in all reels."));
487 return can_reference (bind (&Content::audio, _1), _("There is other audio content overlapping this DCP; remove it."), why_not);
491 DCPContent::can_reference_subtitle (list<string>& why_not) const
493 shared_ptr<DCPDecoder> decoder;
495 decoder.reset (new DCPDecoder (shared_from_this(), film()->log()));
496 } catch (dcp::DCPReadError) {
497 /* We couldn't read the DCP, so it's probably missing */
501 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
502 if (!i->main_subtitle()) {
503 why_not.push_back (_("The DCP does not have subtitles in all reels."));
508 return can_reference (bind (&Content::subtitle, _1), _("There is other subtitle content overlapping this DCP; remove it."), why_not);
512 DCPContent::use_template (shared_ptr<const Content> c)
514 shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (c);
515 DCPOMATIC_ASSERT (dc);
517 _reference_video = dc->_reference_video;
518 _reference_audio = dc->_reference_audio;
519 _reference_subtitle = dc->_reference_subtitle;
523 DCPContent::set_cpl (string id)
525 boost::mutex::scoped_lock lm (_mutex);