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