8fa41a8a59d536dfe899d9cf0bd54a75a8dfebaa
[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::make_shared;
58 using std::shared_ptr;
59 using std::string;
60 using std::vector;
61 using boost::optional;
62
63
64 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
65 {
66         shared_ptr<dcp::CPL> cpl;
67
68         for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
69                 _text_count[i] = 0;
70         }
71
72         auto cpls = dcp::find_and_resolve_cpls (content->directories(), tolerant);
73
74         if (content->cpl ()) {
75                 /* Use the CPL that the content was using before */
76                 for (auto i: cpls) {
77                         if (i->id() == content->cpl().get()) {
78                                 cpl = i;
79                         }
80                 }
81         } else {
82                 /* Choose the CPL with the fewest unsatisfied references */
83
84                 int least_unsatisfied = INT_MAX;
85
86                 for (auto i: cpls) {
87                         int unsatisfied = 0;
88                         for (auto j: i->reels()) {
89                                 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
90                                         ++unsatisfied;
91                                 }
92                                 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
93                                         ++unsatisfied;
94                                 }
95                                 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
96                                         ++unsatisfied;
97                                 }
98                                 if (j->atmos() && !j->atmos()->asset_ref().resolved()) {
99                                         ++unsatisfied;
100                                 }
101                         }
102
103                         if (unsatisfied < least_unsatisfied) {
104                                 least_unsatisfied = unsatisfied;
105                                 cpl = i;
106                         }
107                 }
108         }
109
110         if (!cpl) {
111                 throw DCPError ("No CPLs found in DCP");
112         }
113
114         if (content->kdm()) {
115                 cpl->add (decrypt_kdm_with_helpful_error(content->kdm().get()));
116         }
117
118         _cpl = cpl->id ();
119         _name = cpl->content_title_text ();
120         _content_kind = cpl->content_kind ();
121
122         LOG_GENERAL ("Selected CPL %1", _cpl);
123
124         auto try_to_parse_language = [](optional<string> lang) -> boost::optional<dcp::LanguageTag> {
125                 try {
126                         if (lang) {
127                                 return dcp::LanguageTag (*lang);
128                         }
129                 } catch (...) {}
130                 return boost::none;
131         };
132
133         LOG_GENERAL ("Looking at %1 reels", cpl->reels().size());
134
135         for (auto i: cpl->reels()) {
136                 LOG_GENERAL ("Reel %1", i->id());
137                 vector<shared_ptr<dcpomatic::Font>> reel_fonts;
138
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());
144                                 return;
145                         }
146
147                         LOG_GENERAL ("Main picture %1 of reel %2 found", i->main_picture()->id(), i->id());
148
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"));
155                         }
156
157                         _has_video = true;
158                         auto asset = i->main_picture()->asset();
159                         if (!_video_size) {
160                                 _video_size = asset->size ();
161                         } else if (_video_size.get() != asset->size ()) {
162                                 throw DCPError (_("Mismatched video sizes in DCP"));
163                         }
164
165                         _video_length += i->main_picture()->actual_duration();
166                 }
167
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());
173                                 return;
174                         }
175
176                         LOG_GENERAL ("Main sound %1 of reel %2 found", i->main_sound()->id(), i->id());
177
178                         _has_audio = true;
179                         auto asset = i->main_sound()->asset();
180
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"));
185                         }
186
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"));
191                         }
192
193                         _audio_length += i->main_sound()->actual_duration();
194                         _audio_language = try_to_parse_language (asset->language());
195                 }
196
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());
202                                 return;
203                         }
204
205                         LOG_GENERAL ("Main subtitle %1 of reel %2 found", i->main_subtitle()->id(), i->id());
206
207                         _text_count[static_cast<int>(TextType::OPEN_SUBTITLE)] = 1;
208                         _open_subtitle_language = try_to_parse_language (i->main_subtitle()->language());
209
210                         for (auto const& font: i->main_subtitle()->asset()->font_data()) {
211                                 reel_fonts.push_back(make_shared<dcpomatic::Font>(font.first, font.second));
212                         }
213                 }
214
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());
220                                 return;
221                         }
222
223                         LOG_GENERAL ("Closed caption %1 of reel %2 found", j->id(), i->id());
224
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())));
227                 }
228
229                 if (i->main_markers ()) {
230                         auto rm = i->main_markers()->get();
231                         _markers.insert (rm.begin(), rm.end());
232                 }
233
234                 if (i->atmos()) {
235                         _has_atmos = true;
236                         _atmos_length += i->atmos()->actual_duration();
237                         if (_atmos_edit_rate != dcp::Fraction()) {
238                                 DCPOMATIC_ASSERT (i->atmos()->edit_rate() == _atmos_edit_rate);
239                         }
240                         _atmos_edit_rate = i->atmos()->edit_rate();
241                 }
242
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());
253                 }
254
255                 _fonts.push_back(reel_fonts);
256         }
257
258         _encrypted = cpl->any_encrypted ();
259         _kdm_valid = true;
260
261         LOG_GENERAL_NC ("Check that everything encrypted has a key");
262
263         /* Check first that anything encrypted has a key.  We must do this, as if we try to
264          * read encrypted data with asdcplib without even offering a key it will just return
265          * the encrypted data.  Secondly, check that we can read the first thing from each
266          * asset in each reel.  This checks that when we do have a key it's the right one.
267          */
268         try {
269                 for (auto i: cpl->reels()) {
270                         LOG_GENERAL ("Reel %1", i->id());
271                         auto pic = i->main_picture()->asset();
272                         if (pic->encrypted() && !pic->key()) {
273                                 _kdm_valid = false;
274                                 LOG_GENERAL_NC ("Picture has no key");
275                                 break;
276                         }
277                         auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset>(pic);
278                         auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset>(pic);
279
280                         if (mono) {
281                                 auto reader = mono->start_read();
282                                 reader->set_check_hmac (false);
283                                 reader->get_frame(0)->xyz_image();
284                         } else {
285                                 auto reader = stereo->start_read();
286                                 reader->set_check_hmac (false);
287                                 reader->get_frame(0)->xyz_image(dcp::Eye::LEFT);
288                         }
289
290                         if (i->main_sound()) {
291                                 auto sound = i->main_sound()->asset ();
292                                 if (sound->encrypted() && !sound->key()) {
293                                         _kdm_valid = false;
294                                         LOG_GENERAL_NC ("Sound has no key");
295                                         break;
296                                 }
297                                 auto reader = i->main_sound()->asset()->start_read();
298                                 reader->set_check_hmac (false);
299                                 reader->get_frame(0);
300                         }
301
302                         if (i->main_subtitle()) {
303                                 auto sub = i->main_subtitle()->asset();
304                                 auto mxf_sub = dynamic_pointer_cast<dcp::MXF>(sub);
305                                 if (mxf_sub && mxf_sub->encrypted() && !mxf_sub->key()) {
306                                         _kdm_valid = false;
307                                         LOG_GENERAL_NC ("Subtitle has no key");
308                                         break;
309                                 }
310                                 sub->subtitles ();
311                         }
312
313                         if (i->atmos()) {
314                                 auto atmos = i->atmos()->asset();
315                                 if (atmos->encrypted() && !atmos->key()) {
316                                         _kdm_valid = false;
317                                         LOG_GENERAL_NC ("ATMOS sound has no key");
318                                         break;
319                                 }
320                                 auto reader = atmos->start_read();
321                                 reader->set_check_hmac (false);
322                                 reader->get_frame(0);
323                         }
324                 }
325         } catch (dcp::ReadError& e) {
326                 _kdm_valid = false;
327                 LOG_GENERAL ("KDM is invalid: %1", e.what());
328         } catch (dcp::MiscError& e) {
329                 _kdm_valid = false;
330                 LOG_GENERAL ("KDM is invalid: %1", e.what());
331         }
332
333         _standard = cpl->standard();
334         _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
335                 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());
336         _ratings = cpl->ratings();
337         for (auto i: cpl->content_versions()) {
338                 _content_versions.push_back (i.label_text);
339         }
340
341         _cpl = cpl->id ();
342 }