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