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