Revert "Use make_shared<>."
[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/exceptions.h>
34 #include <dcp/reel_picture_asset.h>
35 #include <dcp/reel.h>
36 #include <libxml++/libxml++.h>
37 #include <boost/foreach.hpp>
38 #include <iterator>
39 #include <iostream>
40
41 #include "i18n.h"
42
43 using std::string;
44 using std::cout;
45 using std::distance;
46 using std::pair;
47 using std::vector;
48 using std::list;
49 using boost::shared_ptr;
50 using boost::scoped_ptr;
51 using boost::optional;
52 using boost::function;
53
54 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
55 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
56 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
57 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
58
59 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
60         : Content (film)
61         , _encrypted (false)
62         , _kdm_valid (false)
63         , _reference_video (false)
64         , _reference_audio (false)
65         , _reference_subtitle (false)
66         , _three_d (false)
67 {
68         video.reset (new VideoContent (this));
69         audio.reset (new AudioContent (this));
70
71         read_directory (p);
72         set_default_colour_conversion ();
73 }
74
75 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
76         : Content (film, node)
77 {
78         video = VideoContent::from_xml (this, node, version);
79         audio = AudioContent::from_xml (this, node, version);
80         subtitle = SubtitleContent::from_xml (this, node, version);
81
82         audio->set_stream (
83                 AudioStreamPtr (
84                         new AudioStream (
85                                 node->number_child<int> ("AudioFrameRate"),
86                                 node->number_child<Frame> ("AudioLength"),
87                                 AudioMapping (node->node_child ("AudioMapping"), version)
88                                 )
89                         )
90                 );
91
92         _name = node->string_child ("Name");
93
94         _encrypted = node->bool_child ("Encrypted");
95         if (node->optional_node_child ("KDM")) {
96                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
97         }
98         _kdm_valid = node->bool_child ("KDMValid");
99         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
100         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
101         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
102         if (node->optional_string_child("Standard")) {
103                 string const s = node->optional_string_child("Standard").get();
104                 if (s == "Interop") {
105                         _standard = dcp::INTEROP;
106                 } else if (s == "SMPTE") {
107                         _standard = dcp::SMPTE;
108                 } else {
109                         DCPOMATIC_ASSERT (false);
110                 }
111         }
112         _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
113 }
114
115 void
116 DCPContent::read_directory (boost::filesystem::path p)
117 {
118         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
119                 if (boost::filesystem::is_regular_file (i->path ())) {
120                         _paths.push_back (i->path ());
121                 } else if (boost::filesystem::is_directory (i->path ())) {
122                         read_directory (i->path ());
123                 }
124         }
125 }
126
127 void
128 DCPContent::examine (shared_ptr<Job> job)
129 {
130         bool const could_be_played = can_be_played ();
131
132         job->set_progress_unknown ();
133         Content::examine (job);
134
135         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
136         video->take_from_examiner (examiner);
137         set_default_colour_conversion ();
138
139         {
140                 boost::mutex::scoped_lock lm (_mutex);
141
142                 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
143                 audio->set_stream (as);
144                 AudioMapping m = as->mapping ();
145                 film()->make_audio_mapping_default (m);
146                 as->set_mapping (m);
147         }
148
149         signal_changed (AudioContentProperty::STREAMS);
150
151         {
152                 boost::mutex::scoped_lock lm (_mutex);
153                 _name = examiner->name ();
154                 if (examiner->has_subtitles ()) {
155                         subtitle.reset (new SubtitleContent (this));
156                 }
157                 _encrypted = examiner->encrypted ();
158                 _kdm_valid = examiner->kdm_valid ();
159                 _standard = examiner->standard ();
160                 _three_d = examiner->three_d ();
161         }
162
163         if (could_be_played != can_be_played ()) {
164                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
165         }
166
167         video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
168 }
169
170 string
171 DCPContent::summary () const
172 {
173         boost::mutex::scoped_lock lm (_mutex);
174         return String::compose (_("%1 [DCP]"), _name);
175 }
176
177 string
178 DCPContent::technical_summary () const
179 {
180         return Content::technical_summary() + " - "
181                 + video->technical_summary() + " - "
182                 + audio->technical_summary() + " - ";
183 }
184
185 void
186 DCPContent::as_xml (xmlpp::Node* node) const
187 {
188         node->add_child("Type")->add_child_text ("DCP");
189
190         Content::as_xml (node);
191
192         if (video) {
193                 video->as_xml (node);
194         }
195
196         if (audio) {
197                 audio->as_xml (node);
198                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
199                 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
200                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
201         }
202
203         if (subtitle) {
204                 subtitle->as_xml (node);
205         }
206
207         boost::mutex::scoped_lock lm (_mutex);
208         node->add_child("Name")->add_child_text (_name);
209         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
210         if (_kdm) {
211                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
212         }
213         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
214         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
215         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
216         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
217         if (_standard) {
218                 switch (_standard.get ()) {
219                 case dcp::INTEROP:
220                         node->add_child("Standard")->add_child_text ("Interop");
221                         break;
222                 case dcp::SMPTE:
223                         node->add_child("Standard")->add_child_text ("SMPTE");
224                         break;
225                 default:
226                         DCPOMATIC_ASSERT (false);
227                 }
228         }
229         node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
230 }
231
232 DCPTime
233 DCPContent::full_length () const
234 {
235         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
236         return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
237 }
238
239 string
240 DCPContent::identifier () const
241 {
242         SafeStringStream s;
243         s << Content::identifier() << "_" << video->identifier() << "_";
244         if (subtitle) {
245                 s << subtitle->identifier () << " ";
246         }
247         s << (_reference_video ? "1" : "0")
248           << (_reference_subtitle ? "1" : "0");
249         return s.str ();
250 }
251
252 void
253 DCPContent::add_kdm (dcp::EncryptedKDM k)
254 {
255         _kdm = k;
256 }
257
258 bool
259 DCPContent::can_be_played () const
260 {
261         boost::mutex::scoped_lock lm (_mutex);
262         return !_encrypted || _kdm_valid;
263 }
264
265 boost::filesystem::path
266 DCPContent::directory () const
267 {
268         optional<size_t> smallest;
269         boost::filesystem::path dir;
270         for (size_t i = 0; i < number_of_paths(); ++i) {
271                 boost::filesystem::path const p = path (i).parent_path ();
272                 size_t const d = distance (p.begin(), p.end());
273                 if (!smallest || d < smallest.get ()) {
274                         dir = p;
275                 }
276         }
277
278         return dir;
279 }
280
281 void
282 DCPContent::add_properties (list<UserProperty>& p) const
283 {
284         Content::add_properties (p);
285         video->add_properties (p);
286         audio->add_properties (p);
287 }
288
289 void
290 DCPContent::set_default_colour_conversion ()
291 {
292         /* Default to no colour conversion for DCPs */
293         video->unset_colour_conversion ();
294 }
295
296 void
297 DCPContent::set_reference_video (bool r)
298 {
299         {
300                 boost::mutex::scoped_lock lm (_mutex);
301                 _reference_video = r;
302         }
303
304         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
305 }
306
307 void
308 DCPContent::set_reference_audio (bool r)
309 {
310         {
311                 boost::mutex::scoped_lock lm (_mutex);
312                 _reference_audio = r;
313         }
314
315         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
316 }
317
318 void
319 DCPContent::set_reference_subtitle (bool r)
320 {
321         {
322                 boost::mutex::scoped_lock lm (_mutex);
323                 _reference_subtitle = r;
324         }
325
326         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
327 }
328
329 list<DCPTimePeriod>
330 DCPContent::reels () const
331 {
332         list<DCPTimePeriod> p;
333         scoped_ptr<DCPDecoder> decoder;
334         try {
335                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
336         } catch (...) {
337                 /* Could not load the DCP; guess reels */
338                 list<DCPTimePeriod> p;
339                 p.push_back (DCPTimePeriod (position(), end()));
340                 return p;
341         }
342
343         DCPTime from = position ();
344         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
345                 DCPTime const to = from + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
346                 p.push_back (DCPTimePeriod (from, to));
347                 from = to;
348         }
349
350         return p;
351 }
352
353 list<DCPTime>
354 DCPContent::reel_split_points () const
355 {
356         list<DCPTime> s;
357         BOOST_FOREACH (DCPTimePeriod i, reels()) {
358                 s.push_back (i.from);
359         }
360         return s;
361 }
362
363 bool
364 DCPContent::can_reference (function<shared_ptr<ContentPart> (shared_ptr<const Content>)> part, string overlapping, list<string>& why_not) const
365 {
366         /* We must be using the same standard as the film */
367         if (_standard) {
368                 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
369                         why_not.push_back (_("The film is set to SMPTE and this DCP is Interop."));
370                         return false;
371                 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
372                         why_not.push_back (_("The film is set to Interop and this DCP is SMPTE."));
373                         return false;
374                 }
375         }
376
377         list<DCPTimePeriod> const fr = film()->reels ();
378         /* fr must contain reels().  It can also contain other reels, but it must at
379            least contain reels().
380         */
381         BOOST_FOREACH (DCPTimePeriod i, reels()) {
382                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
383                         why_not.push_back (_("Reel lengths in the film differ from those in the DCP; set the reel mode to 'split by video content'."));
384                         return false;
385                 }
386         }
387
388         ContentList a = overlaps (film()->content(), part, position(), end());
389         if (a.size() != 1 || a.front().get() != this) {
390                 why_not.push_back (overlapping);
391                 return false;
392         }
393
394         return true;
395 }
396
397 bool
398 DCPContent::can_reference_video (list<string>& why_not) const
399 {
400         return can_reference (bind (&Content::video, _1), _("There is other video content overlapping this DCP; remove it."), why_not);
401 }
402
403 bool
404 DCPContent::can_reference_audio (list<string>& why_not) const
405 {
406         DCPDecoder decoder (shared_from_this(), film()->log(), false);
407         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
408                 if (!i->main_sound()) {
409                         why_not.push_back (_("The DCP does not have sound in all reels."));
410                         return false;
411                 }
412         }
413
414         return can_reference (bind (&Content::audio, _1),   _("There is other audio content overlapping this DCP; remove it."), why_not);
415 }
416
417 bool
418 DCPContent::can_reference_subtitle (list<string>& why_not) const
419 {
420         DCPDecoder decoder (shared_from_this(), film()->log(), false);
421         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
422                 if (!i->main_subtitle()) {
423                         why_not.push_back (_("The DCP does not have subtitles in all reels."));
424                         return false;
425                 }
426         }
427
428         return can_reference (bind (&Content::subtitle, _1), _("There is other subtitle content overlapping this DCP; remove it."), why_not);
429 }