2 Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "dcp_content.h"
21 #include "video_content.h"
22 #include "dcp_examiner.h"
27 #include "compose.hpp"
28 #include "dcp_decoder.h"
30 #include <dcp/exceptions.h>
31 #include <dcp/reel_picture_asset.h>
33 #include <libxml++/libxml++.h>
34 #include <boost/foreach.hpp>
45 using boost::shared_ptr;
46 using boost::scoped_ptr;
47 using boost::optional;
49 int const DCPContentProperty::CAN_BE_PLAYED = 600;
50 int const DCPContentProperty::REFERENCE_VIDEO = 601;
51 int const DCPContentProperty::REFERENCE_AUDIO = 602;
52 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
54 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
56 , SingleStreamAudioContent (film)
57 , SubtitleContent (film)
58 , _has_subtitles (false)
61 , _reference_video (false)
62 , _reference_audio (false)
63 , _reference_subtitle (false)
65 video.reset (new VideoContent (this, film));
68 set_default_colour_conversion ();
71 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
72 : Content (film, node)
73 , SingleStreamAudioContent (film, node, version)
74 , SubtitleContent (film, node, version)
76 video.reset (new VideoContent (this, film, node, version));
78 _name = node->string_child ("Name");
79 _has_subtitles = node->bool_child ("HasSubtitles");
80 _encrypted = node->bool_child ("Encrypted");
81 if (node->optional_node_child ("KDM")) {
82 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
84 _kdm_valid = node->bool_child ("KDMValid");
85 _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
86 _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
87 _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
91 DCPContent::read_directory (boost::filesystem::path p)
93 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
94 if (boost::filesystem::is_regular_file (i->path ())) {
95 _paths.push_back (i->path ());
96 } else if (boost::filesystem::is_directory (i->path ())) {
97 read_directory (i->path ());
103 DCPContent::examine (shared_ptr<Job> job)
105 bool const could_be_played = can_be_played ();
107 job->set_progress_unknown ();
108 Content::examine (job);
110 shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
111 video->take_from_video_examiner (examiner);
112 set_default_colour_conversion ();
113 take_from_audio_examiner (examiner);
116 boost::mutex::scoped_lock lm (_mutex);
117 _name = examiner->name ();
118 _has_subtitles = examiner->has_subtitles ();
119 _encrypted = examiner->encrypted ();
120 _kdm_valid = examiner->kdm_valid ();
123 if (could_be_played != can_be_played ()) {
124 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
129 DCPContent::summary () const
131 boost::mutex::scoped_lock lm (_mutex);
132 return String::compose (_("%1 [DCP]"), _name);
136 DCPContent::technical_summary () const
138 return Content::technical_summary() + " - "
139 + video->technical_summary() + " - "
140 + AudioContent::technical_summary() + " - ";
144 DCPContent::as_xml (xmlpp::Node* node) const
146 node->add_child("Type")->add_child_text ("DCP");
148 Content::as_xml (node);
149 video->as_xml (node);
150 SingleStreamAudioContent::as_xml (node);
151 SubtitleContent::as_xml (node);
153 boost::mutex::scoped_lock lm (_mutex);
154 node->add_child("Name")->add_child_text (_name);
155 node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
156 node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
158 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
160 node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
161 node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
162 node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
163 node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
167 DCPContent::full_length () const
169 FrameRateChange const frc (video->video_frame_rate (), film()->video_frame_rate ());
170 return DCPTime::from_frames (llrint (video->video_length () * frc.factor ()), film()->video_frame_rate ());
174 DCPContent::identifier () const
177 s << Content::identifier() << "_" << video->identifier() << "_" << SubtitleContent::identifier () << " "
178 << (_reference_video ? "1" : "0")
179 << (_reference_subtitle ? "1" : "0");
184 DCPContent::add_kdm (dcp::EncryptedKDM k)
190 DCPContent::can_be_played () const
192 boost::mutex::scoped_lock lm (_mutex);
193 return !_encrypted || _kdm_valid;
196 boost::filesystem::path
197 DCPContent::directory () const
199 optional<size_t> smallest;
200 boost::filesystem::path dir;
201 for (size_t i = 0; i < number_of_paths(); ++i) {
202 boost::filesystem::path const p = path (i).parent_path ();
203 size_t const d = distance (p.begin(), p.end());
204 if (!smallest || d < smallest.get ()) {
213 DCPContent::add_properties (list<UserProperty>& p) const
215 SingleStreamAudioContent::add_properties (p);
219 DCPContent::set_default_colour_conversion ()
221 /* Default to no colour conversion for DCPs */
222 video->unset_colour_conversion ();
226 DCPContent::set_reference_video (bool r)
229 boost::mutex::scoped_lock lm (_mutex);
230 _reference_video = r;
233 signal_changed (DCPContentProperty::REFERENCE_VIDEO);
237 DCPContent::set_reference_audio (bool r)
240 boost::mutex::scoped_lock lm (_mutex);
241 _reference_audio = r;
244 signal_changed (DCPContentProperty::REFERENCE_AUDIO);
248 DCPContent::set_reference_subtitle (bool r)
251 boost::mutex::scoped_lock lm (_mutex);
252 _reference_subtitle = r;
255 signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
259 DCPContent::reels () const
261 list<DCPTimePeriod> p;
262 scoped_ptr<DCPDecoder> decoder;
264 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
266 /* Could not load the DCP; guess reels */
267 list<DCPTimePeriod> p;
268 p.push_back (DCPTimePeriod (position(), end()));
272 DCPTime from = position ();
273 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
274 DCPTime const to = from + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
275 p.push_back (DCPTimePeriod (from, to));
283 DCPContent::reel_split_points () const
286 BOOST_FOREACH (DCPTimePeriod i, reels()) {
287 s.push_back (i.from);
294 DCPContent::can_reference (string overlapping, list<string>& why_not) const
296 list<DCPTimePeriod> const fr = film()->reels ();
297 /* fr must contain reels(). It can also contain other reels, but it must at
298 least contain reels().
300 BOOST_FOREACH (DCPTimePeriod i, reels()) {
301 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
302 why_not.push_back (_("Reel lengths in the project differ from those in the DCP; set the reel mode to 'split by video content'."));
307 list<shared_ptr<T> > a = overlaps<T> (film()->content(), position(), end());
308 if (a.size() != 1 || a.front().get() != this) {
309 why_not.push_back (overlapping);
317 DCPContent::can_reference_video (list<string>& why_not) const
319 /* XXX: this needs to be fixed */
324 DCPContent::can_reference_audio (list<string>& why_not) const
326 DCPDecoder decoder (shared_from_this(), film()->log(), false);
327 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
328 if (!i->main_sound()) {
329 why_not.push_back (_("The DCP does not have sound in all reels."));
334 return can_reference<AudioContent> (_("There is other audio content overlapping this DCP; remove it."), why_not);
338 DCPContent::can_reference_subtitle (list<string>& why_not) const
340 DCPDecoder decoder (shared_from_this(), film()->log(), false);
341 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
342 if (!i->main_subtitle()) {
343 why_not.push_back (_("The DCP does not have subtitles in all reels."));
348 return can_reference<SubtitleContent> (_("There is other subtitle content overlapping this DCP; remove it."), why_not);
352 DCPContent::subtitle_video_frame_rate () const
354 return video->video_frame_rate ();