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/>.
22 #include "dcp_examiner.h"
23 #include "dcp_content.h"
24 #include "dcpomatic_log.h"
25 #include "exceptions.h"
30 #include <dcp/decrypted_kdm.h>
33 #include <dcp/reel_picture_asset.h>
34 #include <dcp/reel_sound_asset.h>
35 #include <dcp/mono_picture_asset.h>
36 #include <dcp/mono_picture_asset_reader.h>
37 #include <dcp/mono_picture_frame.h>
38 #include <dcp/stereo_picture_asset.h>
39 #include <dcp/stereo_picture_asset_reader.h>
40 #include <dcp/stereo_picture_frame.h>
41 #include <dcp/sound_asset.h>
42 #include <dcp/sound_asset_reader.h>
43 #include <dcp/subtitle_asset.h>
44 #include <dcp/reel_atmos_asset.h>
45 #include <dcp/reel_subtitle_asset.h>
46 #include <dcp/reel_closed_caption_asset.h>
47 #include <dcp/reel_markers_asset.h>
48 #include <dcp/sound_asset.h>
55 using std::dynamic_pointer_cast;
56 using std::shared_ptr;
58 using boost::optional;
61 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
62 : DCP (content, tolerant)
64 shared_ptr<dcp::CPL> cpl;
66 for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
70 if (content->cpl ()) {
71 /* Use the CPL that the content was using before */
72 for (auto i: cpls()) {
73 if (i->id() == content->cpl().get()) {
78 /* Choose the CPL with the fewest unsatisfied references */
80 int least_unsatisfied = INT_MAX;
82 for (auto i: cpls()) {
84 for (auto j: i->reels()) {
85 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
88 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
91 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
94 if (j->atmos() && !j->atmos()->asset_ref().resolved()) {
99 if (unsatisfied < least_unsatisfied) {
100 least_unsatisfied = unsatisfied;
107 throw DCPError ("No CPLs found in DCP");
111 _name = cpl->content_title_text ();
112 _content_kind = cpl->content_kind ();
114 LOG_GENERAL ("Selected CPL %1", _cpl);
116 auto try_to_parse_language = [](optional<string> lang) -> boost::optional<dcp::LanguageTag> {
119 return dcp::LanguageTag (*lang);
125 LOG_GENERAL ("Looking at %1 reels", cpl->reels().size());
127 for (auto i: cpl->reels()) {
128 LOG_GENERAL ("Reel %1", i->id());
130 if (i->main_picture ()) {
131 if (!i->main_picture()->asset_ref().resolved()) {
132 /* We are missing this asset so we can't continue; examination will be repeated later */
133 _needs_assets = true;
134 LOG_GENERAL ("Main picture %1 of reel %2 is missing", i->main_picture()->id(), i->id());
138 LOG_GENERAL ("Main picture %1 of reel %2 found", i->main_picture()->id(), i->id());
140 auto const frac = i->main_picture()->edit_rate ();
141 float const fr = float(frac.numerator) / frac.denominator;
142 if (!_video_frame_rate) {
143 _video_frame_rate = fr;
144 } else if (_video_frame_rate.get() != fr) {
145 throw DCPError (_("Mismatched frame rates in DCP"));
149 auto asset = i->main_picture()->asset();
151 _video_size = asset->size ();
152 } else if (_video_size.get() != asset->size ()) {
153 throw DCPError (_("Mismatched video sizes in DCP"));
156 _video_length += i->main_picture()->actual_duration();
159 if (i->main_sound ()) {
160 if (!i->main_sound()->asset_ref().resolved()) {
161 /* We are missing this asset so we can't continue; examination will be repeated later */
162 _needs_assets = true;
163 LOG_GENERAL ("Main sound %1 of reel %2 is missing", i->main_sound()->id(), i->id());
167 LOG_GENERAL ("Main sound %1 of reel %2 found", i->main_sound()->id(), i->id());
170 auto asset = i->main_sound()->asset();
172 if (!_audio_channels) {
173 _audio_channels = asset->channels ();
174 } else if (_audio_channels.get() != asset->channels ()) {
175 throw DCPError (_("Mismatched audio channel counts in DCP"));
178 if (!_audio_frame_rate) {
179 _audio_frame_rate = asset->sampling_rate ();
180 } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
181 throw DCPError (_("Mismatched audio sample rates in DCP"));
184 _audio_length += i->main_sound()->actual_duration();
185 _audio_language = try_to_parse_language (asset->language());
188 if (i->main_subtitle ()) {
189 if (!i->main_subtitle()->asset_ref().resolved()) {
190 /* We are missing this asset so we can't continue; examination will be repeated later */
191 _needs_assets = true;
192 LOG_GENERAL ("Main subtitle %1 of reel %2 is missing", i->main_subtitle()->id(), i->id());
196 LOG_GENERAL ("Main subtitle %1 of reel %2 found", i->main_subtitle()->id(), i->id());
198 _text_count[static_cast<int>(TextType::OPEN_SUBTITLE)] = 1;
199 _open_subtitle_language = try_to_parse_language (i->main_subtitle()->language());
202 for (auto j: i->closed_captions()) {
203 if (!j->asset_ref().resolved()) {
204 /* We are missing this asset so we can't continue; examination will be repeated later */
205 _needs_assets = true;
206 LOG_GENERAL ("Closed caption %1 of reel %2 is missing", j->id(), i->id());
210 LOG_GENERAL ("Closed caption %1 of reel %2 found", j->id(), i->id());
212 _text_count[static_cast<int>(TextType::CLOSED_CAPTION)]++;
213 _dcp_text_tracks.push_back (DCPTextTrack(j->annotation_text(), try_to_parse_language(j->language())));
216 if (i->main_markers ()) {
217 auto rm = i->main_markers()->get();
218 _markers.insert (rm.begin(), rm.end());
223 _atmos_length += i->atmos()->actual_duration();
224 if (_atmos_edit_rate != dcp::Fraction()) {
225 DCPOMATIC_ASSERT (i->atmos()->edit_rate() == _atmos_edit_rate);
227 _atmos_edit_rate = i->atmos()->edit_rate();
230 if (i->main_picture()) {
231 _reel_lengths.push_back (i->main_picture()->actual_duration());
232 } else if (i->main_sound()) {
233 _reel_lengths.push_back (i->main_sound()->actual_duration());
234 } else if (i->main_subtitle()) {
235 _reel_lengths.push_back (i->main_subtitle()->actual_duration());
236 } else if (!i->closed_captions().empty()) {
237 _reel_lengths.push_back (i->closed_captions().front()->actual_duration());
238 } else if (!i->atmos()) {
239 _reel_lengths.push_back (i->atmos()->actual_duration());
243 _encrypted = cpl->any_encrypted ();
246 LOG_GENERAL_NC ("Check that everything encrypted has a key");
248 /* Check first that anything encrypted has a key. We must do this, as if we try to
249 * read encrypted data with asdcplib without even offering a key it will just return
250 * the encrypted data. Secondly, check that we can read the first thing from each
251 * asset in each reel. This checks that when we do have a key it's the right one.
254 for (auto i: cpl->reels()) {
255 LOG_GENERAL ("Reel %1", i->id());
256 auto pic = i->main_picture()->asset();
257 if (pic->encrypted() && !pic->key()) {
260 auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset>(pic);
261 auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset>(pic);
264 auto reader = mono->start_read();
265 reader->set_check_hmac (false);
266 reader->get_frame(0)->xyz_image();
268 auto reader = stereo->start_read();
269 reader->set_check_hmac (false);
270 reader->get_frame(0)->xyz_image(dcp::Eye::LEFT);
273 if (i->main_sound()) {
274 auto sound = i->main_sound()->asset ();
275 if (sound->encrypted() && !sound->key()) {
278 auto reader = i->main_sound()->asset()->start_read();
279 reader->set_check_hmac (false);
280 reader->get_frame(0);
283 if (i->main_subtitle()) {
284 auto sub = i->main_subtitle()->asset();
285 auto mxf_sub = dynamic_pointer_cast<dcp::MXF>(sub);
286 if (mxf_sub && mxf_sub->encrypted() && !mxf_sub->key()) {
293 auto atmos = i->atmos()->asset();
294 if (atmos->encrypted() && !atmos->key()) {
297 auto reader = atmos->start_read();
298 reader->set_check_hmac (false);
299 reader->get_frame(0);
302 } catch (dcp::ReadError& e) {
304 } catch (dcp::MiscError& e) {
308 _standard = cpl->standard();
309 _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
310 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());
311 _ratings = cpl->ratings();
312 for (auto i: cpl->content_versions()) {
313 _content_versions.push_back (i.label_text);