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