Disallow referencing DCPs of different frame rates to the project.
[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::CAN_BE_PLAYED      = 600;
58 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
59 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
60 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
61
62 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
63         : Content (film)
64         , _encrypted (false)
65         , _needs_assets (false)
66         , _kdm_valid (false)
67         , _reference_video (false)
68         , _reference_audio (false)
69         , _reference_subtitle (false)
70         , _three_d (false)
71 {
72         video.reset (new VideoContent (this));
73         audio.reset (new AudioContent (this));
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         audio->set_stream (
87                 AudioStreamPtr (
88                         new AudioStream (
89                                 node->number_child<int> ("AudioFrameRate"),
90                                 /* AudioLength was not present in some old metadata versions */
91                                 node->optional_number_child<Frame>("AudioLength").get_value_or (
92                                         video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
93                                         ),
94                                 AudioMapping (node->node_child ("AudioMapping"), version)
95                                 )
96                         )
97                 );
98
99         _name = node->string_child ("Name");
100         _encrypted = node->bool_child ("Encrypted");
101         _needs_assets = node->optional_bool_child("NeedsAssets").get_value_or (false);
102         if (node->optional_node_child ("KDM")) {
103                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
104         }
105         _kdm_valid = node->bool_child ("KDMValid");
106         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
107         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
108         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
109         if (node->optional_string_child("Standard")) {
110                 string const s = node->optional_string_child("Standard").get();
111                 if (s == "Interop") {
112                         _standard = dcp::INTEROP;
113                 } else if (s == "SMPTE") {
114                         _standard = dcp::SMPTE;
115                 } else {
116                         DCPOMATIC_ASSERT (false);
117                 }
118         }
119         _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
120         _cpl = node->optional_string_child("CPL");
121 }
122
123 void
124 DCPContent::read_directory (boost::filesystem::path p)
125 {
126         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
127                 if (boost::filesystem::is_regular_file (i->path ())) {
128                         _paths.push_back (i->path ());
129                 } else if (boost::filesystem::is_directory (i->path ())) {
130                         read_directory (i->path ());
131                 }
132         }
133 }
134
135 void
136 DCPContent::examine (shared_ptr<Job> job)
137 {
138         bool const could_be_played = can_be_played ();
139
140         job->set_progress_unknown ();
141         Content::examine (job);
142
143         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
144         video->take_from_examiner (examiner);
145         set_default_colour_conversion ();
146
147         {
148                 boost::mutex::scoped_lock lm (_mutex);
149
150                 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
151                 audio->set_stream (as);
152                 AudioMapping m = as->mapping ();
153                 film()->make_audio_mapping_default (m);
154                 as->set_mapping (m);
155         }
156
157         signal_changed (AudioContentProperty::STREAMS);
158
159         {
160                 boost::mutex::scoped_lock lm (_mutex);
161                 _name = examiner->name ();
162                 if (examiner->has_subtitles ()) {
163                         subtitle.reset (new SubtitleContent (this));
164                 }
165                 _encrypted = examiner->encrypted ();
166                 _needs_assets = examiner->needs_assets ();
167                 _kdm_valid = examiner->kdm_valid ();
168                 _standard = examiner->standard ();
169                 _three_d = examiner->three_d ();
170                 _cpl = examiner->cpl ();
171         }
172
173         if (could_be_played != can_be_played ()) {
174                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
175         }
176
177         video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
178 }
179
180 string
181 DCPContent::summary () const
182 {
183         boost::mutex::scoped_lock lm (_mutex);
184         return String::compose (_("%1 [DCP]"), _name);
185 }
186
187 string
188 DCPContent::technical_summary () const
189 {
190         return Content::technical_summary() + " - "
191                 + video->technical_summary() + " - "
192                 + audio->technical_summary() + " - ";
193 }
194
195 void
196 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
197 {
198         node->add_child("Type")->add_child_text ("DCP");
199
200         Content::as_xml (node, with_paths);
201
202         if (video) {
203                 video->as_xml (node);
204         }
205
206         if (audio) {
207                 audio->as_xml (node);
208                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
209                 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
210                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
211         }
212
213         if (subtitle) {
214                 subtitle->as_xml (node);
215         }
216
217         boost::mutex::scoped_lock lm (_mutex);
218         node->add_child("Name")->add_child_text (_name);
219         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
220         node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
221         if (_kdm) {
222                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
223         }
224         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
225         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
226         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
227         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
228         if (_standard) {
229                 switch (_standard.get ()) {
230                 case dcp::INTEROP:
231                         node->add_child("Standard")->add_child_text ("Interop");
232                         break;
233                 case dcp::SMPTE:
234                         node->add_child("Standard")->add_child_text ("SMPTE");
235                         break;
236                 default:
237                         DCPOMATIC_ASSERT (false);
238                 }
239         }
240         node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
241         if (_cpl) {
242                 node->add_child("CPL")->add_child_text (_cpl.get ());
243         }
244 }
245
246 DCPTime
247 DCPContent::full_length () const
248 {
249         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
250         return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
251 }
252
253 string
254 DCPContent::identifier () const
255 {
256         string s = Content::identifier() + "_" + video->identifier() + "_";
257         if (subtitle) {
258                 s += subtitle->identifier () + " ";
259         }
260
261         s += string (_reference_video ? "1" : "0") + string (_reference_subtitle ? "1" : "0");
262         return s;
263 }
264
265 void
266 DCPContent::add_kdm (dcp::EncryptedKDM k)
267 {
268         _kdm = k;
269 }
270
271 void
272 DCPContent::add_ov (boost::filesystem::path ov)
273 {
274         read_directory (ov);
275 }
276
277 bool
278 DCPContent::can_be_played () const
279 {
280         return !needs_kdm() && !needs_assets();
281 }
282
283 bool
284 DCPContent::needs_kdm () const
285 {
286         boost::mutex::scoped_lock lm (_mutex);
287         return _encrypted && !_kdm_valid;
288 }
289
290 bool
291 DCPContent::needs_assets () const
292 {
293         boost::mutex::scoped_lock lm (_mutex);
294         return _needs_assets;
295 }
296
297 vector<boost::filesystem::path>
298 DCPContent::directories () const
299 {
300         return dcp::DCP::directories_from_files (paths ());
301 }
302
303 void
304 DCPContent::add_properties (list<UserProperty>& p) const
305 {
306         Content::add_properties (p);
307         video->add_properties (p);
308         audio->add_properties (p);
309 }
310
311 void
312 DCPContent::set_default_colour_conversion ()
313 {
314         /* Default to no colour conversion for DCPs */
315         video->unset_colour_conversion ();
316 }
317
318 void
319 DCPContent::set_reference_video (bool r)
320 {
321         {
322                 boost::mutex::scoped_lock lm (_mutex);
323                 _reference_video = r;
324         }
325
326         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
327 }
328
329 void
330 DCPContent::set_reference_audio (bool r)
331 {
332         {
333                 boost::mutex::scoped_lock lm (_mutex);
334                 _reference_audio = r;
335         }
336
337         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
338 }
339
340 void
341 DCPContent::set_reference_subtitle (bool r)
342 {
343         {
344                 boost::mutex::scoped_lock lm (_mutex);
345                 _reference_subtitle = r;
346         }
347
348         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
349 }
350
351 list<DCPTimePeriod>
352 DCPContent::reels () const
353 {
354         list<DCPTimePeriod> p;
355         scoped_ptr<DCPDecoder> decoder;
356         try {
357                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log()));
358         } catch (...) {
359                 /* Could not load the DCP; guess reels */
360                 list<DCPTimePeriod> p;
361                 p.push_back (DCPTimePeriod (position(), end()));
362                 return p;
363         }
364
365         /* This content's frame rate must be the same as the output DCP rate, so we can
366            convert `directly' from ContentTime to DCPTime.
367         */
368
369         /* The starting point of this content on the timeline */
370         DCPTime pos = position() - DCPTime (trim_start().get());
371
372         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels ()) {
373                 /* This reel runs from `pos' to `to' */
374                 DCPTime const to = pos + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
375                 if (to > position()) {
376                         p.push_back (DCPTimePeriod (max(position(), pos), min(end(), to)));
377                         if (to > end()) {
378                                 break;
379                         }
380                 }
381                 pos = to;
382         }
383
384         return p;
385 }
386
387 list<DCPTime>
388 DCPContent::reel_split_points () const
389 {
390         list<DCPTime> s;
391         BOOST_FOREACH (DCPTimePeriod i, reels()) {
392                 s.push_back (i.from);
393         }
394         return s;
395 }
396
397 bool
398 DCPContent::can_reference (function<shared_ptr<ContentPart> (shared_ptr<const Content>)> part, string overlapping, list<string>& why_not) const
399 {
400         /* We must be using the same standard as the film */
401         if (_standard) {
402                 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
403                         why_not.push_back (_("The film is set to SMPTE and this DCP is Interop."));
404                         return false;
405                 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
406                         why_not.push_back (_("The film is set to Interop and this DCP is SMPTE."));
407                         return false;
408                 }
409         }
410
411         /* And the same frame rate */
412         if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film()->video_frame_rate())) {
413                 why_not.push_back (_("The film has a different frame rate to this DCP."));
414                 return false;
415         }
416
417         list<DCPTimePeriod> const fr = film()->reels ();
418
419         /* fr must contain reels().  It can also contain other reels, but it must at
420            least contain reels().
421         */
422         BOOST_FOREACH (DCPTimePeriod i, reels()) {
423                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
424                         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'."));
425                         return false;
426                 }
427         }
428
429         ContentList a = overlaps (film()->content(), part, position(), end());
430         if (a.size() != 1 || a.front().get() != this) {
431                 why_not.push_back (overlapping);
432                 return false;
433         }
434
435         return true;
436 }
437
438 bool
439 DCPContent::can_reference_video (list<string>& why_not) const
440 {
441         return can_reference (bind (&Content::video, _1), _("There is other video content overlapping this DCP; remove it."), why_not);
442 }
443
444 bool
445 DCPContent::can_reference_audio (list<string>& why_not) const
446 {
447         DCPDecoder decoder (shared_from_this(), film()->log());
448         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
449                 if (!i->main_sound()) {
450                         why_not.push_back (_("The DCP does not have sound in all reels."));
451                         return false;
452                 }
453         }
454
455         return can_reference (bind (&Content::audio, _1),   _("There is other audio content overlapping this DCP; remove it."), why_not);
456 }
457
458 bool
459 DCPContent::can_reference_subtitle (list<string>& why_not) const
460 {
461         DCPDecoder decoder (shared_from_this(), film()->log());
462         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
463                 if (!i->main_subtitle()) {
464                         why_not.push_back (_("The DCP does not have subtitles in all reels."));
465                         return false;
466                 }
467         }
468
469         return can_reference (bind (&Content::subtitle, _1), _("There is other subtitle content overlapping this DCP; remove it."), why_not);
470 }
471
472 void
473 DCPContent::use_template (shared_ptr<const Content> c)
474 {
475         shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (c);
476         DCPOMATIC_ASSERT (dc);
477
478         _reference_video = dc->_reference_video;
479         _reference_audio = dc->_reference_audio;
480         _reference_subtitle = dc->_reference_subtitle;
481 }