ad1f07718d6d114346b59d9419fce1d658487e4b
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-2018 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 boost::dynamic_pointer_cast;
55 using dcp::raw_convert;
56
57 int const DCPContentProperty::NEEDS_ASSETS       = 600;
58 int const DCPContentProperty::NEEDS_KDM          = 601;
59 int const DCPContentProperty::REFERENCE_VIDEO    = 602;
60 int const DCPContentProperty::REFERENCE_AUDIO    = 603;
61 int const DCPContentProperty::REFERENCE_SUBTITLE = 604;
62 int const DCPContentProperty::NAME               = 605;
63 int const DCPContentProperty::HAS_SUBTITLES      = 606;
64
65 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
66         : Content (film)
67         , _encrypted (false)
68         , _needs_assets (false)
69         , _kdm_valid (false)
70         , _reference_video (false)
71         , _reference_audio (false)
72         , _reference_subtitle (false)
73         , _three_d (false)
74 {
75         read_directory (p);
76         set_default_colour_conversion ();
77 }
78
79 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
80         : Content (film, node)
81 {
82         video = VideoContent::from_xml (this, node, version);
83         audio = AudioContent::from_xml (this, node, version);
84         subtitle = SubtitleContent::from_xml (this, node, version);
85
86         if (video && audio) {
87                 audio->set_stream (
88                         AudioStreamPtr (
89                                 new AudioStream (
90                                         node->number_child<int> ("AudioFrameRate"),
91                                         /* AudioLength was not present in some old metadata versions */
92                                         node->optional_number_child<Frame>("AudioLength").get_value_or (
93                                                 video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
94                                                 ),
95                                         AudioMapping (node->node_child ("AudioMapping"), version)
96                                         )
97                                 )
98                         );
99         }
100
101         _name = node->string_child ("Name");
102         _encrypted = node->bool_child ("Encrypted");
103         _needs_assets = node->optional_bool_child("NeedsAssets").get_value_or (false);
104         if (node->optional_node_child ("KDM")) {
105                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
106         }
107         _kdm_valid = node->bool_child ("KDMValid");
108         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
109         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
110         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
111         if (node->optional_string_child("Standard")) {
112                 string const s = node->optional_string_child("Standard").get();
113                 if (s == "Interop") {
114                         _standard = dcp::INTEROP;
115                 } else if (s == "SMPTE") {
116                         _standard = dcp::SMPTE;
117                 } else {
118                         DCPOMATIC_ASSERT (false);
119                 }
120         }
121         _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
122         _cpl = node->optional_string_child("CPL");
123         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("ReelLength")) {
124                 _reel_lengths.push_back (raw_convert<int64_t> (i->content ()));
125         }
126 }
127
128 void
129 DCPContent::read_directory (boost::filesystem::path p)
130 {
131         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
132                 if (boost::filesystem::is_regular_file (i->path())) {
133                         _paths.push_back (i->path());
134                 } else if (boost::filesystem::is_directory (i->path ())) {
135                         read_directory (i->path());
136                 }
137         }
138 }
139
140 void
141 DCPContent::examine (shared_ptr<Job> job)
142 {
143         bool const needed_assets = needs_assets ();
144         bool const needed_kdm = needs_kdm ();
145         string const old_name = name ();
146         bool had_subtitles = static_cast<bool> (subtitle);
147
148         if (job) {
149                 job->set_progress_unknown ();
150         }
151         Content::examine (job);
152
153         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
154
155         if (examiner->has_video()) {
156                 {
157                         boost::mutex::scoped_lock lm (_mutex);
158                         video.reset (new VideoContent (this));
159                 }
160                 video->take_from_examiner (examiner);
161                 set_default_colour_conversion ();
162         }
163
164         if (examiner->has_audio()) {
165                 {
166                         boost::mutex::scoped_lock lm (_mutex);
167                         audio.reset (new AudioContent (this));
168                 }
169                 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
170                 audio->set_stream (as);
171                 AudioMapping m = as->mapping ();
172                 film()->make_audio_mapping_default (m);
173                 as->set_mapping (m);
174                 signal_changed (AudioContentProperty::STREAMS);
175         }
176
177         bool has_subtitles = false;
178         {
179                 boost::mutex::scoped_lock lm (_mutex);
180                 _name = examiner->name ();
181                 if (examiner->has_subtitles ()) {
182                         subtitle.reset (new SubtitleContent (this));
183                 } else {
184                         subtitle.reset ();
185                 }
186                 has_subtitles = static_cast<bool> (subtitle);
187                 _encrypted = examiner->encrypted ();
188                 _needs_assets = examiner->needs_assets ();
189                 _kdm_valid = examiner->kdm_valid ();
190                 _standard = examiner->standard ();
191                 _three_d = examiner->three_d ();
192                 _cpl = examiner->cpl ();
193                 _reel_lengths = examiner->reel_lengths ();
194         }
195
196         if (had_subtitles != has_subtitles) {
197                 signal_changed (DCPContentProperty::HAS_SUBTITLES);
198         }
199
200         if (needed_assets != needs_assets ()) {
201                 signal_changed (DCPContentProperty::NEEDS_ASSETS);
202         }
203
204         if (needed_kdm != needs_kdm ()) {
205                 signal_changed (DCPContentProperty::NEEDS_KDM);
206         }
207
208         if (old_name != name ()) {
209                 signal_changed (DCPContentProperty::NAME);
210         }
211
212         signal_changed (AudioContentProperty::STREAMS);
213
214         if (video) {
215                 video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
216         }
217 }
218
219 string
220 DCPContent::summary () const
221 {
222         boost::mutex::scoped_lock lm (_mutex);
223         return String::compose (_("%1 [DCP]"), _name);
224 }
225
226 string
227 DCPContent::technical_summary () const
228 {
229         string s = Content::technical_summary() + " - ";
230         if (video) {
231                 s += video->technical_summary() + " - ";
232         }
233         if (audio) {
234                 s += audio->technical_summary() + " - ";
235         }
236         return s;
237 }
238
239 void
240 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
241 {
242         node->add_child("Type")->add_child_text ("DCP");
243
244         Content::as_xml (node, with_paths);
245
246         if (video) {
247                 video->as_xml (node);
248         }
249
250         if (audio) {
251                 audio->as_xml (node);
252                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
253                 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
254                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
255         }
256
257         if (subtitle) {
258                 subtitle->as_xml (node);
259         }
260
261         boost::mutex::scoped_lock lm (_mutex);
262         node->add_child("Name")->add_child_text (_name);
263         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
264         node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
265         if (_kdm) {
266                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
267         }
268         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
269         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
270         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
271         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
272         if (_standard) {
273                 switch (_standard.get ()) {
274                 case dcp::INTEROP:
275                         node->add_child("Standard")->add_child_text ("Interop");
276                         break;
277                 case dcp::SMPTE:
278                         node->add_child("Standard")->add_child_text ("SMPTE");
279                         break;
280                 default:
281                         DCPOMATIC_ASSERT (false);
282                 }
283         }
284         node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
285         if (_cpl) {
286                 node->add_child("CPL")->add_child_text (_cpl.get ());
287         }
288         BOOST_FOREACH (int64_t i, _reel_lengths) {
289                 node->add_child("ReelLength")->add_child_text (raw_convert<string> (i));
290         }
291 }
292
293 DCPTime
294 DCPContent::full_length () const
295 {
296         if (!video) {
297                 return DCPTime();
298         }
299         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
300         return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
301 }
302
303 string
304 DCPContent::identifier () const
305 {
306         string s = Content::identifier() + "_";
307
308         if (video) {
309                 s += video->identifier() + "_";
310         }
311
312         if (subtitle) {
313                 s += subtitle->identifier () + " ";
314         }
315
316         s += string (_reference_video ? "1" : "0") + string (_reference_subtitle ? "1" : "0");
317         return s;
318 }
319
320 void
321 DCPContent::add_kdm (dcp::EncryptedKDM k)
322 {
323         _kdm = k;
324 }
325
326 void
327 DCPContent::add_ov (boost::filesystem::path ov)
328 {
329         read_directory (ov);
330 }
331
332 bool
333 DCPContent::can_be_played () const
334 {
335         return !needs_kdm() && !needs_assets();
336 }
337
338 bool
339 DCPContent::needs_kdm () const
340 {
341         boost::mutex::scoped_lock lm (_mutex);
342         return _encrypted && !_kdm_valid;
343 }
344
345 bool
346 DCPContent::needs_assets () const
347 {
348         boost::mutex::scoped_lock lm (_mutex);
349         return _needs_assets;
350 }
351
352 vector<boost::filesystem::path>
353 DCPContent::directories () const
354 {
355         return dcp::DCP::directories_from_files (paths());
356 }
357
358 void
359 DCPContent::add_properties (list<UserProperty>& p) const
360 {
361         Content::add_properties (p);
362         if (video) {
363                 video->add_properties (p);
364         }
365         if (audio) {
366                 audio->add_properties (p);
367         }
368 }
369
370 void
371 DCPContent::set_default_colour_conversion ()
372 {
373         /* Default to no colour conversion for DCPs */
374         if (video) {
375                 video->unset_colour_conversion ();
376         }
377 }
378
379 void
380 DCPContent::set_reference_video (bool r)
381 {
382         {
383                 boost::mutex::scoped_lock lm (_mutex);
384                 _reference_video = r;
385         }
386
387         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
388 }
389
390 void
391 DCPContent::set_reference_audio (bool r)
392 {
393         {
394                 boost::mutex::scoped_lock lm (_mutex);
395                 _reference_audio = r;
396         }
397
398         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
399 }
400
401 void
402 DCPContent::set_reference_subtitle (bool r)
403 {
404         {
405                 boost::mutex::scoped_lock lm (_mutex);
406                 _reference_subtitle = r;
407         }
408
409         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
410 }
411
412 list<DCPTimePeriod>
413 DCPContent::reels () const
414 {
415         list<int64_t> reel_lengths = _reel_lengths;
416         if (reel_lengths.empty ()) {
417                 /* Old metadata with no reel lengths; get them here instead */
418                 try {
419                         scoped_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this()));
420                         reel_lengths = examiner->reel_lengths ();
421                 } catch (...) {
422                         /* Could not examine the DCP; guess reels */
423                         reel_lengths.push_back (length_after_trim().frames_round (film()->video_frame_rate ()));
424                 }
425         }
426
427         list<DCPTimePeriod> p;
428
429         /* This content's frame rate must be the same as the output DCP rate, so we can
430            convert `directly' from ContentTime to DCPTime.
431         */
432
433         /* The starting point of this content on the timeline */
434         DCPTime pos = position() - DCPTime (trim_start().get());
435
436         BOOST_FOREACH (int64_t i, reel_lengths) {
437                 /* This reel runs from `pos' to `to' */
438                 DCPTime const to = pos + DCPTime::from_frames (i, film()->video_frame_rate());
439                 if (to > position()) {
440                         p.push_back (DCPTimePeriod (max(position(), pos), min(end(), to)));
441                         if (to > end()) {
442                                 break;
443                         }
444                 }
445                 pos = to;
446         }
447
448         return p;
449 }
450
451 list<DCPTime>
452 DCPContent::reel_split_points () const
453 {
454         list<DCPTime> s;
455         BOOST_FOREACH (DCPTimePeriod i, reels()) {
456                 s.push_back (i.from);
457         }
458         return s;
459 }
460
461 bool
462 DCPContent::can_reference (function<shared_ptr<ContentPart> (shared_ptr<const Content>)> part, string overlapping, string& why_not) const
463 {
464         /* We must be using the same standard as the film */
465         if (_standard) {
466                 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
467                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
468                         why_not = _("it is Interop and the film is set to SMPTE.");
469                         return false;
470                 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
471                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
472                         why_not = _("it is SMPTE and the film is set to Interop.");
473                         return false;
474                 }
475         }
476
477         /* And the same frame rate */
478         if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film()->video_frame_rate())) {
479                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
480                 why_not = _("it has a different frame rate to the film.");
481                 return false;
482         }
483
484         list<DCPTimePeriod> const fr = film()->reels ();
485
486         list<DCPTimePeriod> reel_list;
487         try {
488                 reel_list = reels ();
489         } catch (dcp::DCPReadError) {
490                 /* We couldn't read the DCP; it's probably missing */
491                 return false;
492         } catch (dcp::KDMDecryptionError) {
493                 /* We have an incorrect KDM */
494                 return false;
495         }
496
497         /* fr must contain reels().  It can also contain other reels, but it must at
498            least contain reels().
499         */
500         BOOST_FOREACH (DCPTimePeriod i, reel_list) {
501                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
502                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
503                         why_not = _("its reel lengths differ from those in the film; set the reel mode to 'split by video content'.");
504                         return false;
505                 }
506         }
507
508         ContentList a = overlaps (film()->content(), part, position(), end());
509         if (a.size() != 1 || a.front().get() != this) {
510                 why_not = overlapping;
511                 return false;
512         }
513
514         return true;
515 }
516
517 bool
518 DCPContent::can_reference_video (string& why_not) const
519 {
520         if (!video) {
521                 why_not = _("There is no video in this DCP");
522                 return false;
523         }
524
525         if (film()->frame_size() != video->size()) {
526                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
527                 why_not = _("its video frame size differs from the film's.");
528                 return false;
529         }
530
531         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
532         return can_reference (bind (&Content::video, _1), _("it overlaps other video content; remove the other content."), why_not);
533 }
534
535 bool
536 DCPContent::can_reference_audio (string& why_not) const
537 {
538         shared_ptr<DCPDecoder> decoder;
539         try {
540                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
541         } catch (dcp::DCPReadError) {
542                 /* We couldn't read the DCP, so it's probably missing */
543                 return false;
544         } catch (DCPError) {
545                 /* We couldn't read the DCP, so it's probably missing */
546                 return false;
547         } catch (dcp::KDMDecryptionError) {
548                 /* We have an incorrect KDM */
549                 return false;
550         }
551
552         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
553                 if (!i->main_sound()) {
554                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
555                         why_not = _("it does not have sound in all its reels.");
556                         return false;
557                 }
558         }
559
560         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
561         return can_reference (bind (&Content::audio, _1), _("it overlaps other audio content; remove the other content."), why_not);
562 }
563
564 bool
565 DCPContent::can_reference_subtitle (string& why_not) const
566 {
567         shared_ptr<DCPDecoder> decoder;
568         try {
569                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
570         } catch (dcp::DCPReadError) {
571                 /* We couldn't read the DCP, so it's probably missing */
572                 return false;
573         } catch (dcp::KDMDecryptionError) {
574                 /* We have an incorrect KDM */
575                 return false;
576         }
577
578         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
579                 if (!i->main_subtitle()) {
580                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
581                         why_not = _("it does not have subtitles in all its reels.");
582                         return false;
583                 }
584         }
585
586         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
587         return can_reference (bind (&Content::subtitle, _1), _("it overlaps other subtitle content; remove the other content."), why_not);
588 }
589
590 void
591 DCPContent::take_settings_from (shared_ptr<const Content> c)
592 {
593         shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (c);
594         if (!dc) {
595                 return;
596         }
597
598         _reference_video = dc->_reference_video;
599         _reference_audio = dc->_reference_audio;
600         _reference_subtitle = dc->_reference_subtitle;
601 }
602
603 void
604 DCPContent::set_cpl (string id)
605 {
606         boost::mutex::scoped_lock lm (_mutex);
607         _cpl = id;
608 }