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