d41a59b7161addfa4ff2ffd6e2c3d99b9b5c9445
[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 "audio_content.h"
23 #include "dcp_examiner.h"
24 #include "job.h"
25 #include "film.h"
26 #include "config.h"
27 #include "overlaps.h"
28 #include "compose.hpp"
29 #include "dcp_decoder.h"
30 #include "subtitle_content.h"
31 #include <dcp/dcp.h>
32 #include <dcp/exceptions.h>
33 #include <dcp/reel_picture_asset.h>
34 #include <dcp/reel.h>
35 #include <libxml++/libxml++.h>
36 #include <boost/foreach.hpp>
37 #include <iterator>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 using std::string;
43 using std::cout;
44 using std::distance;
45 using std::pair;
46 using std::vector;
47 using std::list;
48 using boost::shared_ptr;
49 using boost::scoped_ptr;
50 using boost::optional;
51 using boost::function;
52
53 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
54 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
55 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
56 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
57
58 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
59         : Content (film)
60         , _encrypted (false)
61         , _kdm_valid (false)
62         , _reference_video (false)
63         , _reference_audio (false)
64         , _reference_subtitle (false)
65 {
66         video.reset (new VideoContent (this));
67         audio.reset (new AudioContent (this));
68
69         read_directory (p);
70         set_default_colour_conversion ();
71 }
72
73 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
74         : Content (film, node)
75 {
76         video = VideoContent::from_xml (this, node, version);
77         audio = AudioContent::from_xml (this, node);
78         subtitle = SubtitleContent::from_xml (this, node, version);
79
80         audio->set_stream (
81                 AudioStreamPtr (
82                         new AudioStream (
83                                 node->number_child<int> ("AudioFrameRate"),
84                                 node->number_child<Frame> ("AudioLength"),
85                                 AudioMapping (node->node_child ("AudioMapping"), version)
86                                 )
87                         )
88                 );
89
90         _name = node->string_child ("Name");
91
92         _encrypted = node->bool_child ("Encrypted");
93         if (node->optional_node_child ("KDM")) {
94                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
95         }
96         _kdm_valid = node->bool_child ("KDMValid");
97         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
98         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
99         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
100         if (node->optional_string_child("Standard")) {
101                 string const s = node->optional_string_child("Standard").get();
102                 if (s == "Interop") {
103                         _standard = dcp::INTEROP;
104                 } else if (s == "SMPTE") {
105                         _standard = dcp::SMPTE;
106                 } else {
107                         DCPOMATIC_ASSERT (false);
108                 }
109         }
110 }
111
112 void
113 DCPContent::read_directory (boost::filesystem::path p)
114 {
115         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
116                 if (boost::filesystem::is_regular_file (i->path ())) {
117                         _paths.push_back (i->path ());
118                 } else if (boost::filesystem::is_directory (i->path ())) {
119                         read_directory (i->path ());
120                 }
121         }
122 }
123
124 void
125 DCPContent::examine (shared_ptr<Job> job)
126 {
127         bool const could_be_played = can_be_played ();
128
129         job->set_progress_unknown ();
130         Content::examine (job);
131
132         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
133         video->take_from_examiner (examiner);
134         set_default_colour_conversion ();
135
136         {
137                 boost::mutex::scoped_lock lm (_mutex);
138
139                 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
140                 audio->set_stream (as);
141                 AudioMapping m = as->mapping ();
142                 film()->make_audio_mapping_default (m);
143                 as->set_mapping (m);
144         }
145
146         signal_changed (AudioContentProperty::STREAMS);
147
148         {
149                 boost::mutex::scoped_lock lm (_mutex);
150                 _name = examiner->name ();
151                 if (examiner->has_subtitles ()) {
152                         subtitle.reset (new SubtitleContent (this));
153                 }
154                 _encrypted = examiner->encrypted ();
155                 _kdm_valid = examiner->kdm_valid ();
156                 _standard = examiner->standard ();
157         }
158
159         if (could_be_played != can_be_played ()) {
160                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
161         }
162 }
163
164 string
165 DCPContent::summary () const
166 {
167         boost::mutex::scoped_lock lm (_mutex);
168         return String::compose (_("%1 [DCP]"), _name);
169 }
170
171 string
172 DCPContent::technical_summary () const
173 {
174         return Content::technical_summary() + " - "
175                 + video->technical_summary() + " - "
176                 + audio->technical_summary() + " - ";
177 }
178
179 void
180 DCPContent::as_xml (xmlpp::Node* node) const
181 {
182         node->add_child("Type")->add_child_text ("DCP");
183
184         Content::as_xml (node);
185
186         if (video) {
187                 video->as_xml (node);
188         }
189
190         if (audio) {
191                 audio->as_xml (node);
192                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
193                 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
194                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
195         }
196
197         if (subtitle) {
198                 subtitle->as_xml (node);
199         }
200
201         boost::mutex::scoped_lock lm (_mutex);
202         node->add_child("Name")->add_child_text (_name);
203         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
204         if (_kdm) {
205                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
206         }
207         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
208         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
209         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
210         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
211         if (_standard) {
212                 switch (_standard.get ()) {
213                 case dcp::INTEROP:
214                         node->add_child("Standard")->add_child_text ("Interop");
215                         break;
216                 case dcp::SMPTE:
217                         node->add_child("Standard")->add_child_text ("SMPTE");
218                         break;
219                 default:
220                         DCPOMATIC_ASSERT (false);
221                 }
222         }
223 }
224
225 DCPTime
226 DCPContent::full_length () const
227 {
228         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
229         return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
230 }
231
232 string
233 DCPContent::identifier () const
234 {
235         SafeStringStream s;
236         s << Content::identifier() << "_" << video->identifier() << "_";
237         if (subtitle) {
238                 s << subtitle->identifier () << " ";
239         }
240         s << (_reference_video ? "1" : "0")
241           << (_reference_subtitle ? "1" : "0");
242         return s.str ();
243 }
244
245 void
246 DCPContent::add_kdm (dcp::EncryptedKDM k)
247 {
248         _kdm = k;
249 }
250
251 bool
252 DCPContent::can_be_played () const
253 {
254         boost::mutex::scoped_lock lm (_mutex);
255         return !_encrypted || _kdm_valid;
256 }
257
258 boost::filesystem::path
259 DCPContent::directory () const
260 {
261         optional<size_t> smallest;
262         boost::filesystem::path dir;
263         for (size_t i = 0; i < number_of_paths(); ++i) {
264                 boost::filesystem::path const p = path (i).parent_path ();
265                 size_t const d = distance (p.begin(), p.end());
266                 if (!smallest || d < smallest.get ()) {
267                         dir = p;
268                 }
269         }
270
271         return dir;
272 }
273
274 void
275 DCPContent::add_properties (list<UserProperty>& p) const
276 {
277         audio->add_properties (p);
278 }
279
280 void
281 DCPContent::set_default_colour_conversion ()
282 {
283         /* Default to no colour conversion for DCPs */
284         video->unset_colour_conversion ();
285 }
286
287 void
288 DCPContent::set_reference_video (bool r)
289 {
290         {
291                 boost::mutex::scoped_lock lm (_mutex);
292                 _reference_video = r;
293         }
294
295         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
296 }
297
298 void
299 DCPContent::set_reference_audio (bool r)
300 {
301         {
302                 boost::mutex::scoped_lock lm (_mutex);
303                 _reference_audio = r;
304         }
305
306         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
307 }
308
309 void
310 DCPContent::set_reference_subtitle (bool r)
311 {
312         {
313                 boost::mutex::scoped_lock lm (_mutex);
314                 _reference_subtitle = r;
315         }
316
317         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
318 }
319
320 list<DCPTimePeriod>
321 DCPContent::reels () const
322 {
323         list<DCPTimePeriod> p;
324         scoped_ptr<DCPDecoder> decoder;
325         try {
326                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
327         } catch (...) {
328                 /* Could not load the DCP; guess reels */
329                 list<DCPTimePeriod> p;
330                 p.push_back (DCPTimePeriod (position(), end()));
331                 return p;
332         }
333
334         DCPTime from = position ();
335         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
336                 DCPTime const to = from + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
337                 p.push_back (DCPTimePeriod (from, to));
338                 from = to;
339         }
340
341         return p;
342 }
343
344 list<DCPTime>
345 DCPContent::reel_split_points () const
346 {
347         list<DCPTime> s;
348         BOOST_FOREACH (DCPTimePeriod i, reels()) {
349                 s.push_back (i.from);
350         }
351         return s;
352 }
353
354 bool
355 DCPContent::can_reference (function<shared_ptr<ContentPart> (shared_ptr<const Content>)> part, string overlapping, list<string>& why_not) const
356 {
357         /* We must be using the same standard as the film */
358         if (_standard) {
359                 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
360                         why_not.push_back (_("The film is set to SMPTE and this DCP is Interop."));
361                         return false;
362                 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
363                         why_not.push_back (_("The film is set to Interop and this DCP is SMPTE."));
364                         return false;
365                 }
366         }
367
368         list<DCPTimePeriod> const fr = film()->reels ();
369         /* fr must contain reels().  It can also contain other reels, but it must at
370            least contain reels().
371         */
372         BOOST_FOREACH (DCPTimePeriod i, reels()) {
373                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
374                         why_not.push_back (_("Reel lengths in the film differ from those in the DCP; set the reel mode to 'split by video content'."));
375                         return false;
376                 }
377         }
378
379         ContentList a = overlaps (film()->content(), part, position(), end());
380         if (a.size() != 1 || a.front().get() != this) {
381                 why_not.push_back (overlapping);
382                 return false;
383         }
384
385         return true;
386 }
387
388 bool
389 DCPContent::can_reference_video (list<string>& why_not) const
390 {
391         return can_reference (bind (&Content::video, _1), _("There is other video content overlapping this DCP; remove it."), why_not);
392 }
393
394 bool
395 DCPContent::can_reference_audio (list<string>& why_not) const
396 {
397         DCPDecoder decoder (shared_from_this(), film()->log(), false);
398         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
399                 if (!i->main_sound()) {
400                         why_not.push_back (_("The DCP does not have sound in all reels."));
401                         return false;
402                 }
403         }
404
405         return can_reference (bind (&Content::audio, _1),   _("There is other audio content overlapping this DCP; remove it."), why_not);
406 }
407
408 bool
409 DCPContent::can_reference_subtitle (list<string>& why_not) const
410 {
411         DCPDecoder decoder (shared_from_this(), film()->log(), false);
412         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
413                 if (!i->main_subtitle()) {
414                         why_not.push_back (_("The DCP does not have subtitles in all reels."));
415                         return false;
416                 }
417         }
418
419         return can_reference (bind (&Content::subtitle, _1), _("There is other subtitle content overlapping this DCP; remove it."), why_not);
420 }