2 Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
23 #include "dcp_content.h"
24 #include "dcp_examiner.h"
25 #include "dcpomatic_log.h"
26 #include "exceptions.h"
31 #include <dcp/decrypted_kdm.h>
32 #include <dcp/mono_picture_asset.h>
33 #include <dcp/mono_picture_asset_reader.h>
34 #include <dcp/mono_picture_frame.h>
36 #include <dcp/reel_atmos_asset.h>
37 #include <dcp/reel_closed_caption_asset.h>
38 #include <dcp/reel_markers_asset.h>
39 #include <dcp/reel_picture_asset.h>
40 #include <dcp/reel_sound_asset.h>
41 #include <dcp/reel_subtitle_asset.h>
42 #include <dcp/search.h>
43 #include <dcp/sound_asset.h>
44 #include <dcp/sound_asset.h>
45 #include <dcp/sound_asset_reader.h>
46 #include <dcp/stereo_picture_asset.h>
47 #include <dcp/stereo_picture_asset_reader.h>
48 #include <dcp/stereo_picture_frame.h>
49 #include <dcp/subtitle_asset.h>
56 using std::dynamic_pointer_cast;
57 using std::make_shared;
58 using std::shared_ptr;
61 using boost::optional;
64 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
66 shared_ptr<dcp::CPL> cpl;
68 for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
72 auto cpls = dcp::find_and_resolve_cpls (content->directories(), tolerant);
74 if (content->cpl ()) {
75 /* Use the CPL that was specified, or that the content was using before */
76 auto iter = std::find_if(cpls.begin(), cpls.end(), [content](shared_ptr<dcp::CPL> cpl) { return cpl->id() == content->cpl().get(); });
77 if (iter == cpls.end()) {
78 throw CPLNotFoundError(content->cpl().get());
82 /* Choose the CPL with the fewest unsatisfied references */
84 int least_unsatisfied = INT_MAX;
88 for (auto j: i->reels()) {
89 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
92 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
95 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
98 if (j->atmos() && !j->atmos()->asset_ref().resolved()) {
103 if (unsatisfied < least_unsatisfied) {
104 least_unsatisfied = unsatisfied;
111 throw DCPError ("No CPLs found in DCP");
114 if (content->kdm()) {
115 cpl->add (decrypt_kdm_with_helpful_error(content->kdm().get()));
119 _name = cpl->content_title_text ();
120 _content_kind = cpl->content_kind ();
122 LOG_GENERAL ("Selected CPL %1", _cpl);
124 auto try_to_parse_language = [](optional<string> lang) -> boost::optional<dcp::LanguageTag> {
127 return dcp::LanguageTag (*lang);
133 LOG_GENERAL ("Looking at %1 reels", cpl->reels().size());
135 for (auto i: cpl->reels()) {
136 LOG_GENERAL ("Reel %1", i->id());
137 vector<shared_ptr<dcpomatic::Font>> reel_fonts;
139 if (i->main_picture ()) {
140 if (!i->main_picture()->asset_ref().resolved()) {
141 /* We are missing this asset so we can't continue; examination will be repeated later */
142 _needs_assets = true;
143 LOG_GENERAL ("Main picture %1 of reel %2 is missing", i->main_picture()->id(), i->id());
147 LOG_GENERAL ("Main picture %1 of reel %2 found", i->main_picture()->id(), i->id());
149 auto const frac = i->main_picture()->edit_rate ();
150 float const fr = float(frac.numerator) / frac.denominator;
151 if (!_video_frame_rate) {
152 _video_frame_rate = fr;
153 } else if (_video_frame_rate.get() != fr) {
154 throw DCPError (_("Mismatched frame rates in DCP"));
158 auto asset = i->main_picture()->asset();
160 _video_size = asset->size ();
161 } else if (_video_size.get() != asset->size ()) {
162 throw DCPError (_("Mismatched video sizes in DCP"));
165 _video_length += i->main_picture()->actual_duration();
168 if (i->main_sound ()) {
169 if (!i->main_sound()->asset_ref().resolved()) {
170 /* We are missing this asset so we can't continue; examination will be repeated later */
171 _needs_assets = true;
172 LOG_GENERAL ("Main sound %1 of reel %2 is missing", i->main_sound()->id(), i->id());
176 LOG_GENERAL ("Main sound %1 of reel %2 found", i->main_sound()->id(), i->id());
179 auto asset = i->main_sound()->asset();
181 if (!_audio_channels) {
182 _audio_channels = asset->channels ();
183 } else if (_audio_channels.get() != asset->channels ()) {
184 throw DCPError (_("Mismatched audio channel counts in DCP"));
187 if (!_audio_frame_rate) {
188 _audio_frame_rate = asset->sampling_rate ();
189 } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
190 throw DCPError (_("Mismatched audio sample rates in DCP"));
193 _audio_length += i->main_sound()->actual_duration();
194 _audio_language = try_to_parse_language (asset->language());
197 if (i->main_subtitle ()) {
198 if (!i->main_subtitle()->asset_ref().resolved()) {
199 /* We are missing this asset so we can't continue; examination will be repeated later */
200 _needs_assets = true;
201 LOG_GENERAL ("Main subtitle %1 of reel %2 is missing", i->main_subtitle()->id(), i->id());
205 LOG_GENERAL ("Main subtitle %1 of reel %2 found", i->main_subtitle()->id(), i->id());
207 _text_count[static_cast<int>(TextType::OPEN_SUBTITLE)] = 1;
208 _open_subtitle_language = try_to_parse_language (i->main_subtitle()->language());
210 for (auto const& font: i->main_subtitle()->asset()->font_data()) {
211 reel_fonts.push_back(make_shared<dcpomatic::Font>(font.first, font.second));
215 for (auto j: i->closed_captions()) {
216 if (!j->asset_ref().resolved()) {
217 /* We are missing this asset so we can't continue; examination will be repeated later */
218 _needs_assets = true;
219 LOG_GENERAL ("Closed caption %1 of reel %2 is missing", j->id(), i->id());
223 LOG_GENERAL ("Closed caption %1 of reel %2 found", j->id(), i->id());
225 _text_count[static_cast<int>(TextType::CLOSED_CAPTION)]++;
226 _dcp_text_tracks.push_back (DCPTextTrack(j->annotation_text().get_value_or(""), try_to_parse_language(j->language())));
229 if (i->main_markers ()) {
230 auto rm = i->main_markers()->get();
231 _markers.insert (rm.begin(), rm.end());
236 _atmos_length += i->atmos()->actual_duration();
237 if (_atmos_edit_rate != dcp::Fraction()) {
238 DCPOMATIC_ASSERT (i->atmos()->edit_rate() == _atmos_edit_rate);
240 _atmos_edit_rate = i->atmos()->edit_rate();
243 if (i->main_picture()) {
244 _reel_lengths.push_back (i->main_picture()->actual_duration());
245 } else if (i->main_sound()) {
246 _reel_lengths.push_back (i->main_sound()->actual_duration());
247 } else if (i->main_subtitle()) {
248 _reel_lengths.push_back (i->main_subtitle()->actual_duration());
249 } else if (!i->closed_captions().empty()) {
250 _reel_lengths.push_back (i->closed_captions().front()->actual_duration());
251 } else if (!i->atmos()) {
252 _reel_lengths.push_back (i->atmos()->actual_duration());
255 if (reel_fonts.empty()) {
256 reel_fonts.push_back(make_shared<dcpomatic::Font>(""));
259 _fonts.push_back(reel_fonts);
262 _encrypted = cpl->any_encrypted ();
265 LOG_GENERAL_NC ("Check that everything encrypted has a key");
267 /* Check first that anything encrypted has a key. We must do this, as if we try to
268 * read encrypted data with asdcplib without even offering a key it will just return
269 * the encrypted data. Secondly, check that we can read the first thing from each
270 * asset in each reel. This checks that when we do have a key it's the right one.
273 for (auto i: cpl->reels()) {
274 LOG_GENERAL ("Reel %1", i->id());
275 auto pic = i->main_picture()->asset();
276 if (pic->encrypted() && !pic->key()) {
278 LOG_GENERAL_NC ("Picture has no key");
281 auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset>(pic);
282 auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset>(pic);
285 auto reader = mono->start_read();
286 reader->set_check_hmac (false);
287 reader->get_frame(0)->xyz_image();
289 auto reader = stereo->start_read();
290 reader->set_check_hmac (false);
291 reader->get_frame(0)->xyz_image(dcp::Eye::LEFT);
294 if (i->main_sound()) {
295 auto sound = i->main_sound()->asset ();
296 if (sound->encrypted() && !sound->key()) {
298 LOG_GENERAL_NC ("Sound has no key");
301 auto reader = i->main_sound()->asset()->start_read();
302 reader->set_check_hmac (false);
303 reader->get_frame(0);
306 if (i->main_subtitle()) {
307 auto sub = i->main_subtitle()->asset();
308 auto mxf_sub = dynamic_pointer_cast<dcp::MXF>(sub);
309 if (mxf_sub && mxf_sub->encrypted() && !mxf_sub->key()) {
311 LOG_GENERAL_NC ("Subtitle has no key");
318 auto atmos = i->atmos()->asset();
319 if (atmos->encrypted() && !atmos->key()) {
321 LOG_GENERAL_NC ("ATMOS sound has no key");
324 auto reader = atmos->start_read();
325 reader->set_check_hmac (false);
326 reader->get_frame(0);
329 } catch (dcp::ReadError& e) {
331 LOG_GENERAL ("KDM is invalid: %1", e.what());
332 } catch (dcp::MiscError& e) {
334 LOG_GENERAL ("KDM is invalid: %1", e.what());
337 _standard = cpl->standard();
338 _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
339 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());
340 _ratings = cpl->ratings();
341 for (auto i: cpl->content_versions()) {
342 _content_versions.push_back (i.label_text);