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