9d196a684f36d8cdf49e1392af81108ec59d5ebb
[dcpomatic.git] / src / lib / dcp_examiner.cc
1 /*
2     Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "config.h"
23 #include "dcp_content.h"
24 #include "dcp_examiner.h"
25 #include "dcpomatic_log.h"
26 #include "exceptions.h"
27 #include "image.h"
28 #include "util.h"
29 #include <dcp/cpl.h>
30 #include <dcp/dcp.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>
35 #include <dcp/reel.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>
50 #include <iostream>
51
52 #include "i18n.h"
53
54
55 using std::cout;
56 using std::dynamic_pointer_cast;
57 using std::shared_ptr;
58 using std::string;
59 using boost::optional;
60
61
62 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
63 {
64         shared_ptr<dcp::CPL> cpl;
65
66         for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
67                 _text_count[i] = 0;
68         }
69
70         auto cpls = dcp::find_and_resolve_cpls (content->directories(), tolerant);
71
72         if (content->cpl ()) {
73                 /* Use the CPL that the content was using before */
74                 for (auto i: cpls) {
75                         if (i->id() == content->cpl().get()) {
76                                 cpl = i;
77                         }
78                 }
79         } else {
80                 /* Choose the CPL with the fewest unsatisfied references */
81
82                 int least_unsatisfied = INT_MAX;
83
84                 for (auto i: cpls) {
85                         int unsatisfied = 0;
86                         for (auto j: i->reels()) {
87                                 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
88                                         ++unsatisfied;
89                                 }
90                                 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
91                                         ++unsatisfied;
92                                 }
93                                 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
94                                         ++unsatisfied;
95                                 }
96                                 if (j->atmos() && !j->atmos()->asset_ref().resolved()) {
97                                         ++unsatisfied;
98                                 }
99                         }
100
101                         if (unsatisfied < least_unsatisfied) {
102                                 least_unsatisfied = unsatisfied;
103                                 cpl = i;
104                         }
105                 }
106         }
107
108         if (!cpl) {
109                 throw DCPError ("No CPLs found in DCP");
110         }
111
112         if (content->kdm()) {
113                 cpl->add (decrypt_kdm_with_helpful_error(content->kdm().get()));
114         }
115
116         _cpl = cpl->id ();
117         _name = cpl->content_title_text ();
118         _content_kind = cpl->content_kind ();
119
120         LOG_GENERAL ("Selected CPL %1", _cpl);
121
122         auto try_to_parse_language = [](optional<string> lang) -> boost::optional<dcp::LanguageTag> {
123                 try {
124                         if (lang) {
125                                 return dcp::LanguageTag (*lang);
126                         }
127                 } catch (...) {}
128                 return boost::none;
129         };
130
131         LOG_GENERAL ("Looking at %1 reels", cpl->reels().size());
132
133         for (auto i: cpl->reels()) {
134                 LOG_GENERAL ("Reel %1", i->id());
135
136                 if (i->main_picture ()) {
137                         if (!i->main_picture()->asset_ref().resolved()) {
138                                 /* We are missing this asset so we can't continue; examination will be repeated later */
139                                 _needs_assets = true;
140                                 LOG_GENERAL ("Main picture %1 of reel %2 is missing", i->main_picture()->id(), i->id());
141                                 return;
142                         }
143
144                         LOG_GENERAL ("Main picture %1 of reel %2 found", i->main_picture()->id(), i->id());
145
146                         auto const frac = i->main_picture()->edit_rate ();
147                         float const fr = float(frac.numerator) / frac.denominator;
148                         if (!_video_frame_rate) {
149                                 _video_frame_rate = fr;
150                         } else if (_video_frame_rate.get() != fr) {
151                                 throw DCPError (_("Mismatched frame rates in DCP"));
152                         }
153
154                         _has_video = true;
155                         auto asset = i->main_picture()->asset();
156                         if (!_video_size) {
157                                 _video_size = asset->size ();
158                         } else if (_video_size.get() != asset->size ()) {
159                                 throw DCPError (_("Mismatched video sizes in DCP"));
160                         }
161
162                         _video_length += i->main_picture()->actual_duration();
163                 }
164
165                 if (i->main_sound ()) {
166                         if (!i->main_sound()->asset_ref().resolved()) {
167                                 /* We are missing this asset so we can't continue; examination will be repeated later */
168                                 _needs_assets = true;
169                                 LOG_GENERAL ("Main sound %1 of reel %2 is missing", i->main_sound()->id(), i->id());
170                                 return;
171                         }
172
173                         LOG_GENERAL ("Main sound %1 of reel %2 found", i->main_sound()->id(), i->id());
174
175                         _has_audio = true;
176                         auto asset = i->main_sound()->asset();
177
178                         if (!_audio_channels) {
179                                 _audio_channels = asset->channels ();
180                         } else if (_audio_channels.get() != asset->channels ()) {
181                                 throw DCPError (_("Mismatched audio channel counts in DCP"));
182                         }
183
184                         if (!_audio_frame_rate) {
185                                 _audio_frame_rate = asset->sampling_rate ();
186                         } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
187                                 throw DCPError (_("Mismatched audio sample rates in DCP"));
188                         }
189
190                         _audio_length += i->main_sound()->actual_duration();
191                         _audio_language = try_to_parse_language (asset->language());
192                 }
193
194                 if (i->main_subtitle ()) {
195                         if (!i->main_subtitle()->asset_ref().resolved()) {
196                                 /* We are missing this asset so we can't continue; examination will be repeated later */
197                                 _needs_assets = true;
198                                 LOG_GENERAL ("Main subtitle %1 of reel %2 is missing", i->main_subtitle()->id(), i->id());
199                                 return;
200                         }
201
202                         LOG_GENERAL ("Main subtitle %1 of reel %2 found", i->main_subtitle()->id(), i->id());
203
204                         _text_count[static_cast<int>(TextType::OPEN_SUBTITLE)] = 1;
205                         _open_subtitle_language = try_to_parse_language (i->main_subtitle()->language());
206                 }
207
208                 for (auto j: i->closed_captions()) {
209                         if (!j->asset_ref().resolved()) {
210                                 /* We are missing this asset so we can't continue; examination will be repeated later */
211                                 _needs_assets = true;
212                                 LOG_GENERAL ("Closed caption %1 of reel %2 is missing", j->id(), i->id());
213                                 return;
214                         }
215
216                         LOG_GENERAL ("Closed caption %1 of reel %2 found", j->id(), i->id());
217
218                         _text_count[static_cast<int>(TextType::CLOSED_CAPTION)]++;
219                         _dcp_text_tracks.push_back (DCPTextTrack(j->annotation_text(), try_to_parse_language(j->language())));
220                 }
221
222                 if (i->main_markers ()) {
223                         auto rm = i->main_markers()->get();
224                         _markers.insert (rm.begin(), rm.end());
225                 }
226
227                 if (i->atmos()) {
228                         _has_atmos = true;
229                         _atmos_length += i->atmos()->actual_duration();
230                         if (_atmos_edit_rate != dcp::Fraction()) {
231                                 DCPOMATIC_ASSERT (i->atmos()->edit_rate() == _atmos_edit_rate);
232                         }
233                         _atmos_edit_rate = i->atmos()->edit_rate();
234                 }
235
236                 if (i->main_picture()) {
237                         _reel_lengths.push_back (i->main_picture()->actual_duration());
238                 } else if (i->main_sound()) {
239                         _reel_lengths.push_back (i->main_sound()->actual_duration());
240                 } else if (i->main_subtitle()) {
241                         _reel_lengths.push_back (i->main_subtitle()->actual_duration());
242                 } else if (!i->closed_captions().empty()) {
243                         _reel_lengths.push_back (i->closed_captions().front()->actual_duration());
244                 } else if (!i->atmos()) {
245                         _reel_lengths.push_back (i->atmos()->actual_duration());
246                 }
247         }
248
249         _encrypted = cpl->any_encrypted ();
250         _kdm_valid = true;
251
252         LOG_GENERAL_NC ("Check that everything encrypted has a key");
253
254         /* Check first that anything encrypted has a key.  We must do this, as if we try to
255          * read encrypted data with asdcplib without even offering a key it will just return
256          * the encrypted data.  Secondly, check that we can read the first thing from each
257          * asset in each reel.  This checks that when we do have a key it's the right one.
258          */
259         try {
260                 for (auto i: cpl->reels()) {
261                         LOG_GENERAL ("Reel %1", i->id());
262                         auto pic = i->main_picture()->asset();
263                         if (pic->encrypted() && !pic->key()) {
264                                 _kdm_valid = false;
265                         }
266                         auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset>(pic);
267                         auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset>(pic);
268
269                         if (mono) {
270                                 auto reader = mono->start_read();
271                                 reader->set_check_hmac (false);
272                                 reader->get_frame(0)->xyz_image();
273                         } else {
274                                 auto reader = stereo->start_read();
275                                 reader->set_check_hmac (false);
276                                 reader->get_frame(0)->xyz_image(dcp::Eye::LEFT);
277                         }
278
279                         if (i->main_sound()) {
280                                 auto sound = i->main_sound()->asset ();
281                                 if (sound->encrypted() && !sound->key()) {
282                                         _kdm_valid = false;
283                                 }
284                                 auto reader = i->main_sound()->asset()->start_read();
285                                 reader->set_check_hmac (false);
286                                 reader->get_frame(0);
287                         }
288
289                         if (i->main_subtitle()) {
290                                 auto sub = i->main_subtitle()->asset();
291                                 auto mxf_sub = dynamic_pointer_cast<dcp::MXF>(sub);
292                                 if (mxf_sub && mxf_sub->encrypted() && !mxf_sub->key()) {
293                                         _kdm_valid = false;
294                                 }
295                                 sub->subtitles ();
296                         }
297
298                         if (i->atmos()) {
299                                 auto atmos = i->atmos()->asset();
300                                 if (atmos->encrypted() && !atmos->key()) {
301                                         _kdm_valid = false;
302                                 }
303                                 auto reader = atmos->start_read();
304                                 reader->set_check_hmac (false);
305                                 reader->get_frame(0);
306                         }
307                 }
308         } catch (dcp::ReadError& e) {
309                 _kdm_valid = false;
310         } catch (dcp::MiscError& e) {
311                 _kdm_valid = false;
312         }
313
314         _standard = cpl->standard();
315         _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
316                 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());
317         _ratings = cpl->ratings();
318         for (auto i: cpl->content_versions()) {
319                 _content_versions.push_back (i.label_text);
320         }
321
322         _cpl = cpl->id ();
323 }