ffdcad1c144fd0030a70eefc6215497fecc783f2
[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::string;
57 using std::dynamic_pointer_cast;
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         auto try_to_parse_language = [](optional<string> lang) -> boost::optional<dcp::LanguageTag> {
115                 try {
116                         if (lang) {
117                                 return dcp::LanguageTag (*lang);
118                         }
119                 } catch (...) {}
120                 return boost::none;
121         };
122
123         for (auto i: cpl->reels()) {
124
125                 if (i->main_picture ()) {
126                         if (!i->main_picture()->asset_ref().resolved()) {
127                                 /* We are missing this asset so we can't continue; examination will be repeated later */
128                                 _needs_assets = true;
129                                 return;
130                         }
131
132                         auto const frac = i->main_picture()->edit_rate ();
133                         float const fr = float(frac.numerator) / frac.denominator;
134                         if (!_video_frame_rate) {
135                                 _video_frame_rate = fr;
136                         } else if (_video_frame_rate.get() != fr) {
137                                 throw DCPError (_("Mismatched frame rates in DCP"));
138                         }
139
140                         _has_video = true;
141                         auto asset = i->main_picture()->asset();
142                         if (!_video_size) {
143                                 _video_size = asset->size ();
144                         } else if (_video_size.get() != asset->size ()) {
145                                 throw DCPError (_("Mismatched video sizes in DCP"));
146                         }
147
148                         _video_length += i->main_picture()->actual_duration();
149                 }
150
151                 if (i->main_sound ()) {
152                         if (!i->main_sound()->asset_ref().resolved()) {
153                                 /* We are missing this asset so we can't continue; examination will be repeated later */
154                                 _needs_assets = true;
155                                 return;
156                         }
157
158                         _has_audio = true;
159                         auto asset = i->main_sound()->asset();
160
161                         if (!_audio_channels) {
162                                 _audio_channels = asset->channels ();
163                         } else if (_audio_channels.get() != asset->channels ()) {
164                                 throw DCPError (_("Mismatched audio channel counts in DCP"));
165                         }
166
167                         if (!_audio_frame_rate) {
168                                 _audio_frame_rate = asset->sampling_rate ();
169                         } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
170                                 throw DCPError (_("Mismatched audio sample rates in DCP"));
171                         }
172
173                         _audio_length += i->main_sound()->actual_duration();
174                         _audio_language = try_to_parse_language (asset->language());
175                 }
176
177                 if (i->main_subtitle ()) {
178                         if (!i->main_subtitle()->asset_ref().resolved()) {
179                                 /* We are missing this asset so we can't continue; examination will be repeated later */
180                                 _needs_assets = true;
181                                 return;
182                         }
183
184                         _text_count[static_cast<int>(TextType::OPEN_SUBTITLE)] = 1;
185                         _open_subtitle_language = try_to_parse_language (i->main_subtitle()->language());
186                 }
187
188                 for (auto j: i->closed_captions()) {
189                         if (!j->asset_ref().resolved()) {
190                                 /* We are missing this asset so we can't continue; examination will be repeated later */
191                                 _needs_assets = true;
192                                 return;
193                         }
194
195                         _text_count[static_cast<int>(TextType::CLOSED_CAPTION)]++;
196                         _dcp_text_tracks.push_back (DCPTextTrack(j->annotation_text(), try_to_parse_language(j->language())));
197                 }
198
199                 if (i->main_markers ()) {
200                         auto rm = i->main_markers()->get();
201                         _markers.insert (rm.begin(), rm.end());
202                 }
203
204                 if (i->atmos()) {
205                         _has_atmos = true;
206                         _atmos_length += i->atmos()->actual_duration();
207                         if (_atmos_edit_rate != dcp::Fraction()) {
208                                 DCPOMATIC_ASSERT (i->atmos()->edit_rate() == _atmos_edit_rate);
209                         }
210                         _atmos_edit_rate = i->atmos()->edit_rate();
211                 }
212
213                 if (i->main_picture()) {
214                         _reel_lengths.push_back (i->main_picture()->actual_duration());
215                 } else if (i->main_sound()) {
216                         _reel_lengths.push_back (i->main_sound()->actual_duration());
217                 } else if (i->main_subtitle()) {
218                         _reel_lengths.push_back (i->main_subtitle()->actual_duration());
219                 } else if (!i->closed_captions().empty()) {
220                         _reel_lengths.push_back (i->closed_captions().front()->actual_duration());
221                 } else if (!i->atmos()) {
222                         _reel_lengths.push_back (i->atmos()->actual_duration());
223                 }
224         }
225
226         _encrypted = cpl->any_encrypted ();
227         _kdm_valid = true;
228
229         /* Check first that anything encrypted has a key.  We must do this, as if we try to
230          * read encrypted data with asdcplib without even offering a key it will just return
231          * the encrypted data.  Secondly, check that we can read the first thing from each
232          * asset in each reel.  This checks that when we do have a key it's the right one.
233          */
234         try {
235                 for (auto i: cpl->reels()) {
236                         auto pic = i->main_picture()->asset();
237                         if (pic->encrypted() && !pic->key()) {
238                                 _kdm_valid = false;
239                         }
240                         auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset>(pic);
241                         auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset>(pic);
242
243                         if (mono) {
244                                 auto reader = mono->start_read();
245                                 reader->set_check_hmac (false);
246                                 reader->get_frame(0)->xyz_image();
247                         } else {
248                                 auto reader = stereo->start_read();
249                                 reader->set_check_hmac (false);
250                                 reader->get_frame(0)->xyz_image(dcp::Eye::LEFT);
251                         }
252
253                         if (i->main_sound()) {
254                                 auto sound = i->main_sound()->asset ();
255                                 if (sound->encrypted() && !sound->key()) {
256                                         _kdm_valid = false;
257                                 }
258                                 auto reader = i->main_sound()->asset()->start_read();
259                                 reader->set_check_hmac (false);
260                                 reader->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                                         _kdm_valid = false;
268                                 }
269                                 sub->subtitles ();
270                         }
271
272                         if (i->atmos()) {
273                                 auto atmos = i->atmos()->asset();
274                                 if (atmos->encrypted() && !atmos->key()) {
275                                         _kdm_valid = false;
276                                 }
277                                 auto reader = atmos->start_read();
278                                 reader->set_check_hmac (false);
279                                 reader->get_frame(0);
280                         }
281                 }
282         } catch (dcp::ReadError& e) {
283                 _kdm_valid = false;
284         } catch (dcp::MiscError& e) {
285                 _kdm_valid = false;
286         }
287
288         _standard = cpl->standard();
289         _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
290                 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());
291         _ratings = cpl->ratings();
292         for (auto i: cpl->content_versions()) {
293                 _content_versions.push_back (i.label_text);
294         }
295
296         _cpl = cpl->id ();
297 }