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