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