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