Reasonably straightforward stuff; main things are adding
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "dcp_content.h"
21 #include "video_content.h"
22 #include "dcp_examiner.h"
23 #include "job.h"
24 #include "film.h"
25 #include "config.h"
26 #include "overlaps.h"
27 #include "compose.hpp"
28 #include "dcp_decoder.h"
29 #include <dcp/dcp.h>
30 #include <dcp/exceptions.h>
31 #include <dcp/reel_picture_asset.h>
32 #include <dcp/reel.h>
33 #include <libxml++/libxml++.h>
34 #include <boost/foreach.hpp>
35 #include <iterator>
36 #include <iostream>
37
38 #include "i18n.h"
39
40 using std::string;
41 using std::cout;
42 using std::distance;
43 using std::pair;
44 using std::list;
45 using boost::shared_ptr;
46 using boost::scoped_ptr;
47 using boost::optional;
48
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;
53
54 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
55         : Content (film)
56         , SingleStreamAudioContent (film)
57         , SubtitleContent (film)
58         , _has_subtitles (false)
59         , _encrypted (false)
60         , _kdm_valid (false)
61         , _reference_video (false)
62         , _reference_audio (false)
63         , _reference_subtitle (false)
64 {
65         video.reset (new VideoContent (this, film));
66
67         read_directory (p);
68         set_default_colour_conversion ();
69 }
70
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)
75 {
76         video.reset (new VideoContent (this, film, node, version));
77
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"));
83         }
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);
88 }
89
90 void
91 DCPContent::read_directory (boost::filesystem::path p)
92 {
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 ());
98                 }
99         }
100 }
101
102 void
103 DCPContent::examine (shared_ptr<Job> job)
104 {
105         bool const could_be_played = can_be_played ();
106
107         job->set_progress_unknown ();
108         Content::examine (job);
109
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);
114
115         {
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 ();
121         }
122
123         if (could_be_played != can_be_played ()) {
124                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
125         }
126 }
127
128 string
129 DCPContent::summary () const
130 {
131         boost::mutex::scoped_lock lm (_mutex);
132         return String::compose (_("%1 [DCP]"), _name);
133 }
134
135 string
136 DCPContent::technical_summary () const
137 {
138         return Content::technical_summary() + " - "
139                 + video->technical_summary() + " - "
140                 + AudioContent::technical_summary() + " - ";
141 }
142
143 void
144 DCPContent::as_xml (xmlpp::Node* node) const
145 {
146         node->add_child("Type")->add_child_text ("DCP");
147
148         Content::as_xml (node);
149         video->as_xml (node);
150         SingleStreamAudioContent::as_xml (node);
151         SubtitleContent::as_xml (node);
152
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");
157         if (_kdm) {
158                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
159         }
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");
164 }
165
166 DCPTime
167 DCPContent::full_length () const
168 {
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 ());
171 }
172
173 string
174 DCPContent::identifier () const
175 {
176         SafeStringStream s;
177         s << Content::identifier() << "_" << video->identifier() << "_" << SubtitleContent::identifier () << " "
178           << (_reference_video ? "1" : "0")
179           << (_reference_subtitle ? "1" : "0");
180         return s.str ();
181 }
182
183 void
184 DCPContent::add_kdm (dcp::EncryptedKDM k)
185 {
186         _kdm = k;
187 }
188
189 bool
190 DCPContent::can_be_played () const
191 {
192         boost::mutex::scoped_lock lm (_mutex);
193         return !_encrypted || _kdm_valid;
194 }
195
196 boost::filesystem::path
197 DCPContent::directory () const
198 {
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 ()) {
205                         dir = p;
206                 }
207         }
208
209         return dir;
210 }
211
212 void
213 DCPContent::add_properties (list<UserProperty>& p) const
214 {
215         SingleStreamAudioContent::add_properties (p);
216 }
217
218 void
219 DCPContent::set_default_colour_conversion ()
220 {
221         /* Default to no colour conversion for DCPs */
222         video->unset_colour_conversion ();
223 }
224
225 void
226 DCPContent::set_reference_video (bool r)
227 {
228         {
229                 boost::mutex::scoped_lock lm (_mutex);
230                 _reference_video = r;
231         }
232
233         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
234 }
235
236 void
237 DCPContent::set_reference_audio (bool r)
238 {
239         {
240                 boost::mutex::scoped_lock lm (_mutex);
241                 _reference_audio = r;
242         }
243
244         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
245 }
246
247 void
248 DCPContent::set_reference_subtitle (bool r)
249 {
250         {
251                 boost::mutex::scoped_lock lm (_mutex);
252                 _reference_subtitle = r;
253         }
254
255         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
256 }
257
258 list<DCPTimePeriod>
259 DCPContent::reels () const
260 {
261         list<DCPTimePeriod> p;
262         scoped_ptr<DCPDecoder> decoder;
263         try {
264                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
265         } catch (...) {
266                 /* Could not load the DCP; guess reels */
267                 list<DCPTimePeriod> p;
268                 p.push_back (DCPTimePeriod (position(), end()));
269                 return p;
270         }
271
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));
276                 from = to;
277         }
278
279         return p;
280 }
281
282 list<DCPTime>
283 DCPContent::reel_split_points () const
284 {
285         list<DCPTime> s;
286         BOOST_FOREACH (DCPTimePeriod i, reels()) {
287                 s.push_back (i.from);
288         }
289         return s;
290 }
291
292 template <class T>
293 bool
294 DCPContent::can_reference (string overlapping, list<string>& why_not) const
295 {
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().
299         */
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'."));
303                         return false;
304                 }
305         }
306
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);
310                 return false;
311         }
312
313         return true;
314 }
315
316 bool
317 DCPContent::can_reference_video (list<string>& why_not) const
318 {
319         /* XXX: this needs to be fixed */
320         return true;
321 }
322
323 bool
324 DCPContent::can_reference_audio (list<string>& why_not) const
325 {
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."));
330                         return false;
331                 }
332         }
333
334         return can_reference<AudioContent> (_("There is other audio content overlapping this DCP; remove it."), why_not);
335 }
336
337 bool
338 DCPContent::can_reference_subtitle (list<string>& why_not) const
339 {
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."));
344                         return false;
345                 }
346         }
347
348         return can_reference<SubtitleContent> (_("There is other subtitle content overlapping this DCP; remove it."), why_not);
349 }
350
351 double
352 DCPContent::subtitle_video_frame_rate () const
353 {
354         return video->video_frame_rate ();
355 }