More of previous.
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "dcp_content.h"
21 #include "video_content.h"
22 #include "audio_content.h"
23 #include "dcp_examiner.h"
24 #include "job.h"
25 #include "film.h"
26 #include "config.h"
27 #include "overlaps.h"
28 #include "compose.hpp"
29 #include "dcp_decoder.h"
30 #include "subtitle_content.h"
31 #include <dcp/dcp.h>
32 #include <dcp/exceptions.h>
33 #include <dcp/reel_picture_asset.h>
34 #include <dcp/reel.h>
35 #include <libxml++/libxml++.h>
36 #include <boost/foreach.hpp>
37 #include <iterator>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 using std::string;
43 using std::cout;
44 using std::distance;
45 using std::pair;
46 using std::vector;
47 using std::list;
48 using boost::shared_ptr;
49 using boost::scoped_ptr;
50 using boost::optional;
51 using boost::function;
52
53 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
54 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
55 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
56 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
57
58 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
59         : Content (film)
60         , _encrypted (false)
61         , _kdm_valid (false)
62         , _reference_video (false)
63         , _reference_audio (false)
64         , _reference_subtitle (false)
65 {
66         video.reset (new VideoContent (this));
67         audio.reset (new AudioContent (this));
68
69         read_directory (p);
70         set_default_colour_conversion ();
71 }
72
73 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
74         : Content (film, node)
75 {
76         video = VideoContent::from_xml (this, node, version);
77         audio = AudioContent::from_xml (this, node);
78         subtitle = SubtitleContent::from_xml (this, node, version);
79
80         audio->set_stream (
81                 AudioStreamPtr (
82                         new AudioStream (
83                                 node->number_child<int> ("AudioFrameRate"),
84                                 node->number_child<Frame> ("AudioLength"),
85                                 AudioMapping (node->node_child ("AudioMapping"), version)
86                                 )
87                         )
88                 );
89
90         _name = node->string_child ("Name");
91
92         _encrypted = node->bool_child ("Encrypted");
93         if (node->optional_node_child ("KDM")) {
94                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
95         }
96         _kdm_valid = node->bool_child ("KDMValid");
97         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
98         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
99         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
100 }
101
102 void
103 DCPContent::read_directory (boost::filesystem::path p)
104 {
105         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
106                 if (boost::filesystem::is_regular_file (i->path ())) {
107                         _paths.push_back (i->path ());
108                 } else if (boost::filesystem::is_directory (i->path ())) {
109                         read_directory (i->path ());
110                 }
111         }
112 }
113
114 void
115 DCPContent::examine (shared_ptr<Job> job)
116 {
117         bool const could_be_played = can_be_played ();
118
119         job->set_progress_unknown ();
120         Content::examine (job);
121
122         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
123         video->take_from_examiner (examiner);
124         set_default_colour_conversion ();
125
126         {
127                 boost::mutex::scoped_lock lm (_mutex);
128
129                 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
130                 audio->set_stream (as);
131                 AudioMapping m = as->mapping ();
132                 film()->make_audio_mapping_default (m);
133                 as->set_mapping (m);
134         }
135
136         signal_changed (AudioContentProperty::STREAMS);
137
138         {
139                 boost::mutex::scoped_lock lm (_mutex);
140                 _name = examiner->name ();
141                 if (examiner->has_subtitles ()) {
142                         subtitle.reset (new SubtitleContent (this));
143                 }
144                 _encrypted = examiner->encrypted ();
145                 _kdm_valid = examiner->kdm_valid ();
146         }
147
148         if (could_be_played != can_be_played ()) {
149                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
150         }
151 }
152
153 string
154 DCPContent::summary () const
155 {
156         boost::mutex::scoped_lock lm (_mutex);
157         return String::compose (_("%1 [DCP]"), _name);
158 }
159
160 string
161 DCPContent::technical_summary () const
162 {
163         return Content::technical_summary() + " - "
164                 + video->technical_summary() + " - "
165                 + audio->technical_summary() + " - ";
166 }
167
168 void
169 DCPContent::as_xml (xmlpp::Node* node) const
170 {
171         node->add_child("Type")->add_child_text ("DCP");
172
173         Content::as_xml (node);
174
175         if (video) {
176                 video->as_xml (node);
177         }
178
179         if (audio) {
180                 audio->as_xml (node);
181                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
182                 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
183                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
184         }
185
186         if (subtitle) {
187                 subtitle->as_xml (node);
188         }
189
190         boost::mutex::scoped_lock lm (_mutex);
191         node->add_child("Name")->add_child_text (_name);
192         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
193         if (_kdm) {
194                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
195         }
196         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
197         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
198         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
199         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
200 }
201
202 DCPTime
203 DCPContent::full_length () const
204 {
205         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
206         return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
207 }
208
209 string
210 DCPContent::identifier () const
211 {
212         SafeStringStream s;
213         s << Content::identifier() << "_" << video->identifier() << "_";
214         if (subtitle) {
215                 s << subtitle->identifier () << " ";
216         }
217         s << (_reference_video ? "1" : "0")
218           << (_reference_subtitle ? "1" : "0");
219         return s.str ();
220 }
221
222 void
223 DCPContent::add_kdm (dcp::EncryptedKDM k)
224 {
225         _kdm = k;
226 }
227
228 bool
229 DCPContent::can_be_played () const
230 {
231         boost::mutex::scoped_lock lm (_mutex);
232         return !_encrypted || _kdm_valid;
233 }
234
235 boost::filesystem::path
236 DCPContent::directory () const
237 {
238         optional<size_t> smallest;
239         boost::filesystem::path dir;
240         for (size_t i = 0; i < number_of_paths(); ++i) {
241                 boost::filesystem::path const p = path (i).parent_path ();
242                 size_t const d = distance (p.begin(), p.end());
243                 if (!smallest || d < smallest.get ()) {
244                         dir = p;
245                 }
246         }
247
248         return dir;
249 }
250
251 void
252 DCPContent::add_properties (list<UserProperty>& p) const
253 {
254         audio->add_properties (p);
255 }
256
257 void
258 DCPContent::set_default_colour_conversion ()
259 {
260         /* Default to no colour conversion for DCPs */
261         video->unset_colour_conversion ();
262 }
263
264 void
265 DCPContent::set_reference_video (bool r)
266 {
267         {
268                 boost::mutex::scoped_lock lm (_mutex);
269                 _reference_video = r;
270         }
271
272         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
273 }
274
275 void
276 DCPContent::set_reference_audio (bool r)
277 {
278         {
279                 boost::mutex::scoped_lock lm (_mutex);
280                 _reference_audio = r;
281         }
282
283         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
284 }
285
286 void
287 DCPContent::set_reference_subtitle (bool r)
288 {
289         {
290                 boost::mutex::scoped_lock lm (_mutex);
291                 _reference_subtitle = r;
292         }
293
294         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
295 }
296
297 list<DCPTimePeriod>
298 DCPContent::reels () const
299 {
300         list<DCPTimePeriod> p;
301         scoped_ptr<DCPDecoder> decoder;
302         try {
303                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
304         } catch (...) {
305                 /* Could not load the DCP; guess reels */
306                 list<DCPTimePeriod> p;
307                 p.push_back (DCPTimePeriod (position(), end()));
308                 return p;
309         }
310
311         DCPTime from = position ();
312         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
313                 DCPTime const to = from + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
314                 p.push_back (DCPTimePeriod (from, to));
315                 from = to;
316         }
317
318         return p;
319 }
320
321 list<DCPTime>
322 DCPContent::reel_split_points () const
323 {
324         list<DCPTime> s;
325         BOOST_FOREACH (DCPTimePeriod i, reels()) {
326                 s.push_back (i.from);
327         }
328         return s;
329 }
330
331 bool
332 DCPContent::can_reference (function<shared_ptr<ContentPart> (shared_ptr<const Content>)> part, string overlapping, list<string>& why_not) const
333 {
334         list<DCPTimePeriod> const fr = film()->reels ();
335         /* fr must contain reels().  It can also contain other reels, but it must at
336            least contain reels().
337         */
338         BOOST_FOREACH (DCPTimePeriod i, reels()) {
339                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
340                         why_not.push_back (_("Reel lengths in the project differ from those in the DCP; set the reel mode to 'split by video content'."));
341                         return false;
342                 }
343         }
344
345         ContentList a = overlaps (film()->content(), part, position(), end());
346         if (a.size() != 1 || a.front().get() != this) {
347                 why_not.push_back (overlapping);
348                 return false;
349         }
350
351         return true;
352 }
353
354 bool
355 DCPContent::can_reference_video (list<string>& why_not) const
356 {
357         return can_reference (bind (&Content::video, _1), _("There is other video content overlapping this DCP; remove it."), why_not);
358 }
359
360 bool
361 DCPContent::can_reference_audio (list<string>& why_not) const
362 {
363         DCPDecoder decoder (shared_from_this(), film()->log(), false);
364         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
365                 if (!i->main_sound()) {
366                         why_not.push_back (_("The DCP does not have sound in all reels."));
367                         return false;
368                 }
369         }
370
371         return can_reference (bind (&Content::audio, _1),   _("There is other audio content overlapping this DCP; remove it."), why_not);
372 }
373
374 bool
375 DCPContent::can_reference_subtitle (list<string>& why_not) const
376 {
377         DCPDecoder decoder (shared_from_this(), film()->log(), false);
378         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
379                 if (!i->main_subtitle()) {
380                         why_not.push_back (_("The DCP does not have subtitles in all reels."));
381                         return false;
382                 }
383         }
384
385         return can_reference (bind (&Content::subtitle, _1), _("There is other subtitle content overlapping this DCP; remove it."), why_not);
386 }