Basics of subtitle split.
[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 "dcp_examiner.h"
23 #include "job.h"
24 #include "film.h"
25 #include "config.h"
26 #include "overlaps.h"
27 #include "compose.hpp"
28 #include "dcp_decoder.h"
29 #include "subtitle_content.h"
30 #include <dcp/dcp.h>
31 #include <dcp/exceptions.h>
32 #include <dcp/reel_picture_asset.h>
33 #include <dcp/reel.h>
34 #include <libxml++/libxml++.h>
35 #include <boost/foreach.hpp>
36 #include <iterator>
37 #include <iostream>
38
39 #include "i18n.h"
40
41 using std::string;
42 using std::cout;
43 using std::distance;
44 using std::pair;
45 using std::list;
46 using boost::shared_ptr;
47 using boost::scoped_ptr;
48 using boost::optional;
49
50 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
51 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
52 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
53 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
54
55 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
56         : Content (film)
57         , SingleStreamAudioContent (film)
58         , _has_subtitles (false)
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         subtitle.reset (new SubtitleContent (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         , SingleStreamAudioContent (film, node, version)
75 {
76         video.reset (new VideoContent (this, film, node, version));
77         subtitle.reset (new SubtitleContent (this, film, node, version));
78
79         _name = node->string_child ("Name");
80         _has_subtitles = node->bool_child ("HasSubtitles");
81         _encrypted = node->bool_child ("Encrypted");
82         if (node->optional_node_child ("KDM")) {
83                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
84         }
85         _kdm_valid = node->bool_child ("KDMValid");
86         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
87         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
88         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
89 }
90
91 void
92 DCPContent::read_directory (boost::filesystem::path p)
93 {
94         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
95                 if (boost::filesystem::is_regular_file (i->path ())) {
96                         _paths.push_back (i->path ());
97                 } else if (boost::filesystem::is_directory (i->path ())) {
98                         read_directory (i->path ());
99                 }
100         }
101 }
102
103 void
104 DCPContent::examine (shared_ptr<Job> job)
105 {
106         bool const could_be_played = can_be_played ();
107
108         job->set_progress_unknown ();
109         Content::examine (job);
110
111         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
112         video->take_from_video_examiner (examiner);
113         set_default_colour_conversion ();
114         take_from_audio_examiner (examiner);
115
116         {
117                 boost::mutex::scoped_lock lm (_mutex);
118                 _name = examiner->name ();
119                 _has_subtitles = examiner->has_subtitles ();
120                 _encrypted = examiner->encrypted ();
121                 _kdm_valid = examiner->kdm_valid ();
122         }
123
124         if (could_be_played != can_be_played ()) {
125                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
126         }
127 }
128
129 string
130 DCPContent::summary () const
131 {
132         boost::mutex::scoped_lock lm (_mutex);
133         return String::compose (_("%1 [DCP]"), _name);
134 }
135
136 string
137 DCPContent::technical_summary () const
138 {
139         return Content::technical_summary() + " - "
140                 + video->technical_summary() + " - "
141                 + AudioContent::technical_summary() + " - ";
142 }
143
144 void
145 DCPContent::as_xml (xmlpp::Node* node) const
146 {
147         node->add_child("Type")->add_child_text ("DCP");
148
149         Content::as_xml (node);
150         video->as_xml (node);
151         SingleStreamAudioContent::as_xml (node);
152         subtitle->as_xml (node);
153
154         boost::mutex::scoped_lock lm (_mutex);
155         node->add_child("Name")->add_child_text (_name);
156         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
157         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
158         if (_kdm) {
159                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
160         }
161         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
162         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
163         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
164         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
165 }
166
167 DCPTime
168 DCPContent::full_length () const
169 {
170         FrameRateChange const frc (video->video_frame_rate (), film()->video_frame_rate ());
171         return DCPTime::from_frames (llrint (video->video_length () * frc.factor ()), film()->video_frame_rate ());
172 }
173
174 string
175 DCPContent::identifier () const
176 {
177         SafeStringStream s;
178         s << Content::identifier() << "_" << video->identifier() << "_" << subtitle->identifier () << " "
179           << (_reference_video ? "1" : "0")
180           << (_reference_subtitle ? "1" : "0");
181         return s.str ();
182 }
183
184 void
185 DCPContent::add_kdm (dcp::EncryptedKDM k)
186 {
187         _kdm = k;
188 }
189
190 bool
191 DCPContent::can_be_played () const
192 {
193         boost::mutex::scoped_lock lm (_mutex);
194         return !_encrypted || _kdm_valid;
195 }
196
197 boost::filesystem::path
198 DCPContent::directory () const
199 {
200         optional<size_t> smallest;
201         boost::filesystem::path dir;
202         for (size_t i = 0; i < number_of_paths(); ++i) {
203                 boost::filesystem::path const p = path (i).parent_path ();
204                 size_t const d = distance (p.begin(), p.end());
205                 if (!smallest || d < smallest.get ()) {
206                         dir = p;
207                 }
208         }
209
210         return dir;
211 }
212
213 void
214 DCPContent::add_properties (list<UserProperty>& p) const
215 {
216         SingleStreamAudioContent::add_properties (p);
217 }
218
219 void
220 DCPContent::set_default_colour_conversion ()
221 {
222         /* Default to no colour conversion for DCPs */
223         video->unset_colour_conversion ();
224 }
225
226 void
227 DCPContent::set_reference_video (bool r)
228 {
229         {
230                 boost::mutex::scoped_lock lm (_mutex);
231                 _reference_video = r;
232         }
233
234         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
235 }
236
237 void
238 DCPContent::set_reference_audio (bool r)
239 {
240         {
241                 boost::mutex::scoped_lock lm (_mutex);
242                 _reference_audio = r;
243         }
244
245         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
246 }
247
248 void
249 DCPContent::set_reference_subtitle (bool r)
250 {
251         {
252                 boost::mutex::scoped_lock lm (_mutex);
253                 _reference_subtitle = r;
254         }
255
256         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
257 }
258
259 list<DCPTimePeriod>
260 DCPContent::reels () const
261 {
262         list<DCPTimePeriod> p;
263         scoped_ptr<DCPDecoder> decoder;
264         try {
265                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
266         } catch (...) {
267                 /* Could not load the DCP; guess reels */
268                 list<DCPTimePeriod> p;
269                 p.push_back (DCPTimePeriod (position(), end()));
270                 return p;
271         }
272
273         DCPTime from = position ();
274         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
275                 DCPTime const to = from + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
276                 p.push_back (DCPTimePeriod (from, to));
277                 from = to;
278         }
279
280         return p;
281 }
282
283 list<DCPTime>
284 DCPContent::reel_split_points () const
285 {
286         list<DCPTime> s;
287         BOOST_FOREACH (DCPTimePeriod i, reels()) {
288                 s.push_back (i.from);
289         }
290         return s;
291 }
292
293 template <class T>
294 bool
295 DCPContent::can_reference (string overlapping, list<string>& why_not) const
296 {
297         list<DCPTimePeriod> const fr = film()->reels ();
298         /* fr must contain reels().  It can also contain other reels, but it must at
299            least contain reels().
300         */
301         BOOST_FOREACH (DCPTimePeriod i, reels()) {
302                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
303                         why_not.push_back (_("Reel lengths in the project differ from those in the DCP; set the reel mode to 'split by video content'."));
304                         return false;
305                 }
306         }
307
308         list<shared_ptr<T> > a = overlaps<T> (film()->content(), position(), end());
309         if (a.size() != 1 || a.front().get() != this) {
310                 why_not.push_back (overlapping);
311                 return false;
312         }
313
314         return true;
315 }
316
317 bool
318 DCPContent::can_reference_video (list<string>& why_not) const
319 {
320         /* XXX: this needs to be fixed */
321         return true;
322 }
323
324 bool
325 DCPContent::can_reference_audio (list<string>& why_not) const
326 {
327         DCPDecoder decoder (shared_from_this(), film()->log(), false);
328         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
329                 if (!i->main_sound()) {
330                         why_not.push_back (_("The DCP does not have sound in all reels."));
331                         return false;
332                 }
333         }
334
335         return can_reference<AudioContent> (_("There is other audio content overlapping this DCP; remove it."), why_not);
336 }
337
338 bool
339 DCPContent::can_reference_subtitle (list<string>& why_not) const
340 {
341         /* XXX: this needs to be fixed */
342         return true;
343 }
344
345 double
346 DCPContent::subtitle_video_frame_rate () const
347 {
348         return video->video_frame_rate ();
349 }