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