2 Copyright (C) 2014-2015 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 "dcp_examiner.h"
26 #include "compose.hpp"
27 #include "dcp_decoder.h"
29 #include <dcp/exceptions.h>
30 #include <dcp/reel_picture_asset.h>
32 #include <libxml++/libxml++.h>
33 #include <boost/foreach.hpp>
44 using boost::shared_ptr;
45 using boost::scoped_ptr;
46 using boost::optional;
48 int const DCPContentProperty::CAN_BE_PLAYED = 600;
49 int const DCPContentProperty::REFERENCE_VIDEO = 601;
50 int const DCPContentProperty::REFERENCE_AUDIO = 602;
51 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
53 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)
68 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
69 : Content (film, node)
70 , VideoContent (film, node, version)
71 , SingleStreamAudioContent (film, node, version)
72 , SubtitleContent (film, node, version)
74 _name = node->string_child ("Name");
75 _has_subtitles = node->bool_child ("HasSubtitles");
76 _encrypted = node->bool_child ("Encrypted");
77 if (node->optional_node_child ("KDM")) {
78 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
80 _kdm_valid = node->bool_child ("KDMValid");
81 _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
82 _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
83 _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
87 DCPContent::read_directory (boost::filesystem::path p)
89 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
90 if (boost::filesystem::is_regular_file (i->path ())) {
91 _paths.push_back (i->path ());
92 } else if (boost::filesystem::is_directory (i->path ())) {
93 read_directory (i->path ());
99 DCPContent::examine (shared_ptr<Job> job)
101 bool const could_be_played = can_be_played ();
103 job->set_progress_unknown ();
104 Content::examine (job);
106 shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
107 take_from_video_examiner (examiner);
108 take_from_audio_examiner (examiner);
111 boost::mutex::scoped_lock lm (_mutex);
112 _name = examiner->name ();
113 _has_subtitles = examiner->has_subtitles ();
114 _encrypted = examiner->encrypted ();
115 _kdm_valid = examiner->kdm_valid ();
118 if (could_be_played != can_be_played ()) {
119 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
124 DCPContent::summary () const
126 boost::mutex::scoped_lock lm (_mutex);
127 return String::compose (_("%1 [DCP]"), _name);
131 DCPContent::technical_summary () const
133 return Content::technical_summary() + " - "
134 + VideoContent::technical_summary() + " - "
135 + AudioContent::technical_summary() + " - ";
139 DCPContent::as_xml (xmlpp::Node* node) const
141 node->add_child("Type")->add_child_text ("DCP");
143 Content::as_xml (node);
144 VideoContent::as_xml (node);
145 SingleStreamAudioContent::as_xml (node);
146 SubtitleContent::as_xml (node);
148 boost::mutex::scoped_lock lm (_mutex);
149 node->add_child("Name")->add_child_text (_name);
150 node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
151 node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
153 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
155 node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
156 node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
157 node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
158 node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
162 DCPContent::full_length () const
164 shared_ptr<const Film> film = _film.lock ();
165 DCPOMATIC_ASSERT (film);
166 FrameRateChange const frc (video_frame_rate (), film->video_frame_rate ());
167 return DCPTime::from_frames (llrint (video_length () * frc.factor ()), film->video_frame_rate ());
171 DCPContent::identifier () const
174 s << VideoContent::identifier() << "_" << SubtitleContent::identifier () << " "
175 << (_reference_video ? "1" : "0")
176 << (_reference_subtitle ? "1" : "0");
181 DCPContent::add_kdm (dcp::EncryptedKDM k)
187 DCPContent::can_be_played () const
189 boost::mutex::scoped_lock lm (_mutex);
190 return !_encrypted || _kdm_valid;
193 boost::filesystem::path
194 DCPContent::directory () const
196 optional<size_t> smallest;
197 boost::filesystem::path dir;
198 for (size_t i = 0; i < number_of_paths(); ++i) {
199 boost::filesystem::path const p = path (i).parent_path ();
200 size_t const d = distance (p.begin(), p.end());
201 if (!smallest || d < smallest.get ()) {
210 DCPContent::add_properties (list<pair<string, string> >& p) const
212 SingleStreamAudioContent::add_properties (p);
216 DCPContent::set_default_colour_conversion ()
218 /* Default to no colour conversion for DCPs */
219 unset_colour_conversion ();
223 DCPContent::set_reference_video (bool r)
226 boost::mutex::scoped_lock lm (_mutex);
227 _reference_video = r;
230 signal_changed (DCPContentProperty::REFERENCE_VIDEO);
234 DCPContent::set_reference_audio (bool r)
237 boost::mutex::scoped_lock lm (_mutex);
238 _reference_audio = r;
241 signal_changed (DCPContentProperty::REFERENCE_AUDIO);
245 DCPContent::set_reference_subtitle (bool r)
248 boost::mutex::scoped_lock lm (_mutex);
249 _reference_subtitle = r;
252 signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
256 DCPContent::reels () const
258 list<DCPTimePeriod> p;
259 scoped_ptr<DCPDecoder> decoder;
261 decoder.reset (new DCPDecoder (shared_from_this(), false));
263 /* Could not load the DCP; guess reels */
264 list<DCPTimePeriod> p;
265 p.push_back (DCPTimePeriod (position(), end()));
269 shared_ptr<const Film> film = _film.lock ();
270 DCPOMATIC_ASSERT (film);
271 DCPTime from = position ();
272 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
273 DCPTime const to = from + DCPTime::from_frames (i->main_picture()->duration(), film->video_frame_rate());
274 p.push_back (DCPTimePeriod (from, to));
282 DCPContent::reel_split_points () const
285 BOOST_FOREACH (DCPTimePeriod i, reels()) {
286 s.push_back (i.from);
293 DCPContent::can_reference (string overlapping, list<string>& why_not) const
295 shared_ptr<const Film> film = _film.lock ();
296 DCPOMATIC_ASSERT (film);
298 list<DCPTimePeriod> const fr = film->reels ();
299 /* fr must contain reels(). It can also contain other reels, but it must at
300 least contain reels().
302 BOOST_FOREACH (DCPTimePeriod i, reels()) {
303 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
304 why_not.push_back (_("Reel lengths in the project differ from those in the DCP; set the reel mode to `split by video content'."));
309 list<shared_ptr<T> > a = overlaps<T> (film->content(), position(), end());
310 if (a.size() != 1 || a.front().get() != this) {
311 why_not.push_back (overlapping);
319 DCPContent::can_reference_video (list<string>& why_not) const
321 return can_reference<VideoContent> (_("There is other video content overlapping this DCP; remove it."), why_not);
325 DCPContent::can_reference_audio (list<string>& why_not) const
327 DCPDecoder decoder (shared_from_this(), false);
328 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
329 if (!i->main_sound()) {
330 why_not.push_back (_("The DCP does not have sound in all reels."));
335 return can_reference<AudioContent> (_("There is other audio content overlapping this DCP; remove it."), why_not);
339 DCPContent::can_reference_subtitle (list<string>& why_not) const
341 DCPDecoder decoder (shared_from_this(), false);
342 BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
343 if (!i->main_subtitle()) {
344 why_not.push_back (_("The DCP does not have subtitles in all reels."));
349 return can_reference<SubtitleContent> (_("There is other subtitle content overlapping this DCP; remove it."), why_not);