2 Copyright (C) 2014-2020 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/>.
21 #include "dcp_examiner.h"
22 #include "dcp_content.h"
23 #include "exceptions.h"
28 #include <dcp/decrypted_kdm.h>
31 #include <dcp/reel_picture_asset.h>
32 #include <dcp/reel_sound_asset.h>
33 #include <dcp/mono_picture_asset.h>
34 #include <dcp/mono_picture_asset_reader.h>
35 #include <dcp/mono_picture_frame.h>
36 #include <dcp/stereo_picture_asset.h>
37 #include <dcp/stereo_picture_asset_reader.h>
38 #include <dcp/stereo_picture_frame.h>
39 #include <dcp/sound_asset.h>
40 #include <dcp/sound_asset_reader.h>
41 #include <dcp/subtitle_asset.h>
42 #include <dcp/reel_atmos_asset.h>
43 #include <dcp/reel_subtitle_asset.h>
44 #include <dcp/reel_closed_caption_asset.h>
45 #include <dcp/reel_markers_asset.h>
46 #include <dcp/sound_asset.h>
53 using std::runtime_error;
55 using std::shared_ptr;
57 using std::dynamic_pointer_cast;
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 auto try_to_parse_language = [](optional<string> lang) -> boost::optional<dcp::LanguageTag> {
117 return dcp::LanguageTag (*lang);
123 for (auto i: cpl->reels()) {
125 if (i->main_picture ()) {
126 if (!i->main_picture()->asset_ref().resolved()) {
127 /* We are missing this asset so we can't continue; examination will be repeated later */
128 _needs_assets = true;
132 auto const frac = i->main_picture()->edit_rate ();
133 float const fr = float(frac.numerator) / frac.denominator;
134 if (!_video_frame_rate) {
135 _video_frame_rate = fr;
136 } else if (_video_frame_rate.get() != fr) {
137 throw DCPError (_("Mismatched frame rates in DCP"));
141 auto asset = i->main_picture()->asset();
143 _video_size = asset->size ();
144 } else if (_video_size.get() != asset->size ()) {
145 throw DCPError (_("Mismatched video sizes in DCP"));
148 _video_length += i->main_picture()->actual_duration();
151 if (i->main_sound ()) {
152 if (!i->main_sound()->asset_ref().resolved()) {
153 /* We are missing this asset so we can't continue; examination will be repeated later */
154 _needs_assets = true;
159 auto asset = i->main_sound()->asset();
161 if (!_audio_channels) {
162 _audio_channels = asset->channels ();
163 } else if (_audio_channels.get() != asset->channels ()) {
164 throw DCPError (_("Mismatched audio channel counts in DCP"));
167 if (!_audio_frame_rate) {
168 _audio_frame_rate = asset->sampling_rate ();
169 } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
170 throw DCPError (_("Mismatched audio sample rates in DCP"));
173 _audio_length += i->main_sound()->actual_duration();
174 _audio_language = try_to_parse_language (asset->language());
177 if (i->main_subtitle ()) {
178 if (!i->main_subtitle()->asset_ref().resolved()) {
179 /* We are missing this asset so we can't continue; examination will be repeated later */
180 _needs_assets = true;
184 _text_count[static_cast<int>(TextType::OPEN_SUBTITLE)] = 1;
185 _open_subtitle_language = try_to_parse_language (i->main_subtitle()->language());
188 for (auto j: i->closed_captions()) {
189 if (!j->asset_ref().resolved()) {
190 /* We are missing this asset so we can't continue; examination will be repeated later */
191 _needs_assets = true;
195 _text_count[static_cast<int>(TextType::CLOSED_CAPTION)]++;
196 _dcp_text_tracks.push_back (DCPTextTrack(j->annotation_text(), try_to_parse_language(j->language())));
199 if (i->main_markers ()) {
200 auto rm = i->main_markers()->get();
201 _markers.insert (rm.begin(), rm.end());
206 _atmos_length += i->atmos()->actual_duration();
207 if (_atmos_edit_rate != dcp::Fraction()) {
208 DCPOMATIC_ASSERT (i->atmos()->edit_rate() == _atmos_edit_rate);
210 _atmos_edit_rate = i->atmos()->edit_rate();
213 if (i->main_picture()) {
214 _reel_lengths.push_back (i->main_picture()->actual_duration());
215 } else if (i->main_sound()) {
216 _reel_lengths.push_back (i->main_sound()->actual_duration());
217 } else if (i->main_subtitle()) {
218 _reel_lengths.push_back (i->main_subtitle()->actual_duration());
219 } else if (!i->closed_captions().empty()) {
220 _reel_lengths.push_back (i->closed_captions().front()->actual_duration());
221 } else if (!i->atmos()) {
222 _reel_lengths.push_back (i->atmos()->actual_duration());
226 _encrypted = cpl->any_encrypted ();
229 /* Check first that anything encrypted has a key. We must do this, as if we try to
230 * read encrypted data with asdcplib without even offering a key it will just return
231 * the encrypted data. Secondly, check that we can read the first thing from each
232 * asset in each reel. This checks that when we do have a key it's the right one.
235 for (auto i: cpl->reels()) {
236 auto pic = i->main_picture()->asset();
237 if (pic->encrypted() && !pic->key()) {
240 auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset>(pic);
241 auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset>(pic);
244 auto reader = mono->start_read();
245 reader->set_check_hmac (false);
246 reader->get_frame(0)->xyz_image();
248 auto reader = stereo->start_read();
249 reader->set_check_hmac (false);
250 reader->get_frame(0)->xyz_image(dcp::Eye::LEFT);
253 if (i->main_sound()) {
254 auto sound = i->main_sound()->asset ();
255 if (sound->encrypted() && !sound->key()) {
258 auto reader = i->main_sound()->asset()->start_read();
259 reader->set_check_hmac (false);
260 reader->get_frame(0);
263 if (i->main_subtitle()) {
264 auto sub = i->main_subtitle()->asset();
265 auto mxf_sub = dynamic_pointer_cast<dcp::MXF>(sub);
266 if (mxf_sub && mxf_sub->encrypted() && !mxf_sub->key()) {
273 auto atmos = i->atmos()->asset();
274 if (atmos->encrypted() && !atmos->key()) {
277 auto reader = atmos->start_read();
278 reader->set_check_hmac (false);
279 reader->get_frame(0);
282 } catch (dcp::ReadError& e) {
284 } catch (dcp::MiscError& e) {
288 _standard = cpl->standard();
289 _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
290 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());
291 _ratings = cpl->ratings();
292 for (auto i: cpl->content_versions()) {
293 _content_versions.push_back (i.label_text);