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