Tell user we need a KDM when we have none, and content is encrypted.
[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 "exceptions.h"
24 #include "image.h"
25 #include "config.h"
26 #include "util.h"
27 #include <dcp/dcp.h>
28 #include <dcp/decrypted_kdm.h>
29 #include <dcp/cpl.h>
30 #include <dcp/reel.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>
47 #include <iostream>
48
49 #include "i18n.h"
50
51 using std::list;
52 using std::cout;
53 using std::runtime_error;
54 using std::map;
55 using std::shared_ptr;
56 using std::dynamic_pointer_cast;
57
58 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
59         : DCP (content, tolerant)
60 {
61         shared_ptr<dcp::CPL> cpl;
62
63         for (int i = 0; i < static_cast<int>(TextType::COUNT); ++i) {
64                 _text_count[i] = 0;
65         }
66
67         if (content->cpl ()) {
68                 /* Use the CPL that the content was using before */
69                 for (auto i: cpls()) {
70                         if (i->id() == content->cpl().get()) {
71                                 cpl = i;
72                         }
73                 }
74         } else {
75                 /* Choose the CPL with the fewest unsatisfied references */
76
77                 int least_unsatisfied = INT_MAX;
78
79                 for (auto i: cpls()) {
80                         int unsatisfied = 0;
81                         for (auto j: i->reels()) {
82                                 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
83                                         ++unsatisfied;
84                                 }
85                                 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
86                                         ++unsatisfied;
87                                 }
88                                 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
89                                         ++unsatisfied;
90                                 }
91                                 if (j->atmos() && !j->atmos()->asset_ref().resolved()) {
92                                         ++unsatisfied;
93                                 }
94                         }
95
96                         if (unsatisfied < least_unsatisfied) {
97                                 least_unsatisfied = unsatisfied;
98                                 cpl = i;
99                         }
100                 }
101         }
102
103         if (!cpl) {
104                 throw DCPError ("No CPLs found in DCP");
105         }
106
107         _cpl = cpl->id ();
108         _name = cpl->content_title_text ();
109         _content_kind = cpl->content_kind ();
110
111         for (auto i: cpl->reels()) {
112
113                 if (i->main_picture ()) {
114                         if (!i->main_picture()->asset_ref().resolved()) {
115                                 /* We are missing this asset so we can't continue; examination will be repeated later */
116                                 _needs_assets = true;
117                                 return;
118                         }
119
120                         auto const frac = i->main_picture()->edit_rate ();
121                         float const fr = float(frac.numerator) / frac.denominator;
122                         if (!_video_frame_rate) {
123                                 _video_frame_rate = fr;
124                         } else if (_video_frame_rate.get() != fr) {
125                                 throw DCPError (_("Mismatched frame rates in DCP"));
126                         }
127
128                         _has_video = true;
129                         auto asset = i->main_picture()->asset();
130                         if (!_video_size) {
131                                 _video_size = asset->size ();
132                         } else if (_video_size.get() != asset->size ()) {
133                                 throw DCPError (_("Mismatched video sizes in DCP"));
134                         }
135
136                         _video_length += i->main_picture()->actual_duration();
137                 }
138
139                 if (i->main_sound ()) {
140                         if (!i->main_sound()->asset_ref().resolved()) {
141                                 /* We are missing this asset so we can't continue; examination will be repeated later */
142                                 _needs_assets = true;
143                                 return;
144                         }
145
146                         _has_audio = true;
147                         auto asset = i->main_sound()->asset();
148
149                         if (!_audio_channels) {
150                                 _audio_channels = asset->channels ();
151                         } else if (_audio_channels.get() != asset->channels ()) {
152                                 throw DCPError (_("Mismatched audio channel counts in DCP"));
153                         }
154
155                         if (!_audio_frame_rate) {
156                                 _audio_frame_rate = asset->sampling_rate ();
157                         } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
158                                 throw DCPError (_("Mismatched audio sample rates in DCP"));
159                         }
160
161                         _audio_length += i->main_sound()->actual_duration();
162                 }
163
164                 if (i->main_subtitle ()) {
165                         if (!i->main_subtitle()->asset_ref().resolved()) {
166                                 /* We are missing this asset so we can't continue; examination will be repeated later */
167                                 _needs_assets = true;
168                                 return;
169                         }
170
171                         _text_count[static_cast<int>(TextType::OPEN_SUBTITLE)] = 1;
172                 }
173
174                 for (auto j: i->closed_captions()) {
175                         if (!j->asset_ref().resolved()) {
176                                 /* We are missing this asset so we can't continue; examination will be repeated later */
177                                 _needs_assets = true;
178                                 return;
179                         }
180
181                         _text_count[static_cast<int>(TextType::CLOSED_CAPTION)]++;
182                         _dcp_text_tracks.push_back (DCPTextTrack(j->annotation_text(), j->language().get_value_or(_("Unknown"))));
183                 }
184
185                 if (i->main_markers ()) {
186                         map<dcp::Marker, dcp::Time> rm = i->main_markers()->get();
187                         _markers.insert (rm.begin(), rm.end());
188                 }
189
190                 if (i->atmos()) {
191                         _has_atmos = true;
192                         _atmos_length += i->atmos()->actual_duration();
193                         if (_atmos_edit_rate != dcp::Fraction()) {
194                                 DCPOMATIC_ASSERT (i->atmos()->edit_rate() == _atmos_edit_rate);
195                         }
196                         _atmos_edit_rate = i->atmos()->edit_rate();
197                 }
198
199                 if (i->main_picture()) {
200                         _reel_lengths.push_back (i->main_picture()->actual_duration());
201                 } else if (i->main_sound()) {
202                         _reel_lengths.push_back (i->main_sound()->actual_duration());
203                 } else if (i->main_subtitle()) {
204                         _reel_lengths.push_back (i->main_subtitle()->actual_duration());
205                 } else if (!i->closed_captions().empty()) {
206                         _reel_lengths.push_back (i->closed_captions().front()->actual_duration());
207                 } else if (!i->atmos()) {
208                         _reel_lengths.push_back (i->atmos()->actual_duration());
209                 }
210         }
211
212         _encrypted = cpl->any_encrypted ();
213         _kdm_valid = true;
214
215         /* Check first that anything encrypted has a key.  We must do this, as if we try to
216          * read encrypted data with asdcplib without even offering a key it will just return
217          * the encrypted data.  Secondly, check that we can read the first thing from each
218          * asset in each reel.  This checks that when we do have a key it's the right one.
219          */
220         try {
221                 for (auto i: cpl->reels()) {
222                         auto pic = i->main_picture()->asset();
223                         if (pic->encrypted() && !pic->key()) {
224                                 _kdm_valid = false;
225                         }
226                         auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset>(pic);
227                         auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset>(pic);
228
229                         if (mono) {
230                                 mono->start_read()->get_frame(0)->xyz_image ();
231                         } else {
232                                 stereo->start_read()->get_frame(0)->xyz_image(dcp::Eye::LEFT);
233                         }
234
235                         if (i->main_sound()) {
236                                 auto sound = i->main_sound()->asset ();
237                                 if (sound->encrypted() && !sound->key()) {
238                                         _kdm_valid = false;
239                                 }
240                                 i->main_sound()->asset()->start_read()->get_frame(0);
241                         }
242
243                         if (i->main_subtitle()) {
244                                 auto sub = i->main_subtitle()->asset();
245                                 auto mxf_sub = dynamic_pointer_cast<dcp::MXF>(sub);
246                                 if (mxf_sub && mxf_sub->encrypted() && !mxf_sub->key()) {
247                                         _kdm_valid = false;
248                                 }
249                                 sub->subtitles ();
250                         }
251
252                         if (i->atmos()) {
253                                 auto atmos = i->atmos()->asset();
254                                 if (atmos->encrypted() && !atmos->key()) {
255                                         _kdm_valid = false;
256                                 }
257                                 atmos->start_read()->get_frame(0);
258                         }
259                 }
260         } catch (dcp::ReadError& e) {
261                 _kdm_valid = false;
262         } catch (dcp::MiscError& e) {
263                 _kdm_valid = false;
264         }
265
266         DCPOMATIC_ASSERT (cpl->standard ());
267         _standard = cpl->standard().get();
268         _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
269                 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());
270         _ratings = cpl->ratings();
271         for (auto i: cpl->content_versions()) {
272                 _content_versions.push_back (i.label_text);
273         }
274
275         _cpl = cpl->id ();
276 }