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