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