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