24ff507e687ba43f5918e3bb4986e9fc979dbbf8
[dcpomatic.git] / src / lib / dcp_decoder.cc
1 /*
2     Copyright (C) 2014-2022 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 "atmos_decoder.h"
23 #include "audio_content.h"
24 #include "audio_decoder.h"
25 #include "config.h"
26 #include "constants.h"
27 #include "dcp_content.h"
28 #include "dcp_decoder.h"
29 #include "digester.h"
30 #include "ffmpeg_image_proxy.h"
31 #include "frame_interval_checker.h"
32 #include "image.h"
33 #include "j2k_image_proxy.h"
34 #include "text_decoder.h"
35 #include "video_decoder.h"
36 #include <dcp/cpl.h>
37 #include <dcp/dcp.h>
38 #include <dcp/decrypted_kdm.h>
39 #include <dcp/mono_picture_asset.h>
40 #include <dcp/mono_picture_asset_reader.h>
41 #include <dcp/mono_picture_frame.h>
42 #include <dcp/reel.h>
43 #include <dcp/reel_atmos_asset.h>
44 #include <dcp/reel_closed_caption_asset.h>
45 #include <dcp/reel_picture_asset.h>
46 #include <dcp/reel_sound_asset.h>
47 #include <dcp/reel_subtitle_asset.h>
48 #include <dcp/search.h>
49 #include <dcp/sound_asset_reader.h>
50 #include <dcp/sound_frame.h>
51 #include <dcp/stereo_picture_asset.h>
52 #include <dcp/stereo_picture_asset_reader.h>
53 #include <dcp/stereo_picture_frame.h>
54 #include <dcp/subtitle_image.h>
55 #include <iostream>
56
57 #include "i18n.h"
58
59
60 using std::cout;
61 using std::dynamic_pointer_cast;
62 using std::list;
63 using std::make_shared;
64 using std::map;
65 using std::shared_ptr;
66 using std::string;
67 using std::vector;
68 using boost::optional;
69 using namespace dcpomatic;
70
71
72 DCPDecoder::DCPDecoder (shared_ptr<const Film> film, shared_ptr<const DCPContent> content, bool fast, bool tolerant, shared_ptr<DCPDecoder> old)
73         : Decoder (film)
74         , _dcp_content (content)
75 {
76         if (content->can_be_played()) {
77                 if (content->video) {
78                         video = make_shared<VideoDecoder>(this, content);
79                 }
80                 if (content->audio) {
81                         audio = make_shared<AudioDecoder>(this, content->audio, fast);
82                 }
83                 for (auto i: content->text) {
84                         text.push_back (make_shared<TextDecoder>(this, i));
85                         /* We should really call maybe_set_position() on this TextDecoder to set the time
86                          * of the first subtitle, but it probably doesn't matter since we'll always
87                          * have regularly occurring video (and maybe audio) content.
88                          */
89                 }
90                 if (content->atmos) {
91                         atmos = make_shared<AtmosDecoder>(this, content);
92                 }
93         }
94
95         /* We try to avoid re-scanning the DCP's files every time we make a new DCPDecoder; we do this
96            by re-using the _reels list.  Before we do this we need to check that nothing too serious
97            has changed in the DCPContent.
98
99            We do this by storing a digest of the important bits of the DCPContent and then checking that's
100            the same before we re-use _reels.
101         */
102
103         _lazy_digest = calculate_lazy_digest (content);
104
105         if (old && old->lazy_digest() == _lazy_digest) {
106                 _reels = old->_reels;
107         } else {
108
109                 auto cpl_list = dcp::find_and_resolve_cpls(content->directories(), tolerant);
110
111                 if (cpl_list.empty()) {
112                         throw DCPError (_("No CPLs found in DCP."));
113                 }
114
115                 shared_ptr<dcp::CPL> cpl;
116                 for (auto i: cpl_list) {
117                         if (_dcp_content->cpl() && i->id() == _dcp_content->cpl().get()) {
118                                 cpl = i;
119                         }
120                 }
121
122                 if (!cpl) {
123                         /* No CPL found; probably an old file that doesn't specify it;
124                            just use the first one.
125                         */
126                         cpl = cpl_list.front();
127                 }
128
129                 if (content->kdm()) {
130                         cpl->add (decrypt_kdm_with_helpful_error(content->kdm().get()));
131                 }
132
133                 _reels = cpl->reels ();
134         }
135
136         set_decode_referenced (false);
137
138         _reel = _reels.begin ();
139         get_readers ();
140
141         _font_id_allocator.add_fonts_from_reels(_reels);
142         _font_id_allocator.allocate();
143 }
144
145
146 bool
147 DCPDecoder::pass ()
148 {
149         if (!_dcp_content->can_be_played()) {
150                 return true;
151         }
152
153         if (_reel == _reels.end()) {
154                 if (audio) {
155                         audio->flush ();
156                 }
157                 return true;
158         }
159
160         auto const vfr = _dcp_content->active_video_frame_rate (film());
161
162         /* Frame within the (played part of the) reel that is coming up next */
163         auto const frame = _next.frames_round (vfr);
164
165         auto picture_asset = (*_reel)->main_picture()->asset();
166         DCPOMATIC_ASSERT (picture_asset);
167
168         /* We must emit texts first as when we emit the video for this frame
169            it will expect already to have the texts.
170         */
171         pass_texts (_next, picture_asset->size());
172
173         if ((_mono_reader || _stereo_reader) && (_decode_referenced || !_dcp_content->reference_video())) {
174                 auto const entry_point = (*_reel)->main_picture()->entry_point().get_value_or(0);
175                 if (_mono_reader) {
176                         video->emit (
177                                 film(),
178                                 std::make_shared<J2KImageProxy>(
179                                         _mono_reader->get_frame (entry_point + frame),
180                                         picture_asset->size(),
181                                         AV_PIX_FMT_XYZ12LE,
182                                         _forced_reduction
183                                         ),
184                                 _offset + frame
185                                 );
186                 } else {
187                         video->emit (
188                                 film(),
189                                 std::make_shared<J2KImageProxy>(
190                                         _stereo_reader->get_frame (entry_point + frame),
191                                         picture_asset->size(),
192                                         dcp::Eye::LEFT,
193                                         AV_PIX_FMT_XYZ12LE,
194                                         _forced_reduction
195                                         ),
196                                 _offset + frame
197                                 );
198
199                         video->emit (
200                                 film(),
201                                 std::make_shared<J2KImageProxy>(
202                                         _stereo_reader->get_frame (entry_point + frame),
203                                         picture_asset->size(),
204                                         dcp::Eye::RIGHT,
205                                         AV_PIX_FMT_XYZ12LE,
206                                         _forced_reduction
207                                         ),
208                                 _offset + frame
209                                 );
210                 }
211         }
212
213         if (_sound_reader && (_decode_referenced || !_dcp_content->reference_audio())) {
214                 auto const entry_point = (*_reel)->main_sound()->entry_point().get_value_or(0);
215                 auto sf = _sound_reader->get_frame (entry_point + frame);
216                 auto from = sf->data ();
217
218                 int const channels = _dcp_content->audio->stream()->channels ();
219                 int const frames = sf->size() / (3 * channels);
220                 auto data = make_shared<AudioBuffers>(channels, frames);
221                 auto data_data = data->data();
222                 for (int i = 0; i < frames; ++i) {
223                         for (int j = 0; j < channels; ++j) {
224                                 data_data[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
225                                 from += 3;
226                         }
227                 }
228
229                 audio->emit (film(), _dcp_content->audio->stream(), data, ContentTime::from_frames (_offset, vfr) + _next);
230         }
231
232         if (_atmos_reader) {
233                 DCPOMATIC_ASSERT (_atmos_metadata);
234                 auto const entry_point = (*_reel)->atmos()->entry_point().get_value_or(0);
235                 atmos->emit (film(), _atmos_reader->get_frame(entry_point + frame), _offset + frame, *_atmos_metadata);
236         }
237
238         _next += ContentTime::from_frames (1, vfr);
239
240         if ((*_reel)->main_picture ()) {
241                 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
242                         next_reel ();
243                         _next = ContentTime ();
244                 }
245         }
246
247         return false;
248 }
249
250
251 void
252 DCPDecoder::pass_texts (ContentTime next, dcp::Size size)
253 {
254         auto decoder = text.begin ();
255         if (decoder == text.end()) {
256                 /* It's possible that there is now a main subtitle but no TextDecoders, for example if
257                    the CPL has just changed but the TextContent's texts have not been recreated yet.
258                 */
259                 return;
260         }
261
262         if ((*_reel)->main_subtitle()) {
263                 pass_texts (
264                         next,
265                         (*_reel)->main_subtitle()->asset(),
266                         _dcp_content->reference_text(TextType::OPEN_SUBTITLE),
267                         (*_reel)->main_subtitle()->entry_point().get_value_or(0),
268                         *decoder,
269                         size
270                         );
271                 ++decoder;
272         }
273
274         for (auto i: (*_reel)->closed_captions()) {
275                 pass_texts (
276                         next, i->asset(), _dcp_content->reference_text(TextType::CLOSED_CAPTION), i->entry_point().get_value_or(0), *decoder, size
277                         );
278                 ++decoder;
279         }
280 }
281
282 void
283 DCPDecoder::pass_texts (
284         ContentTime next, shared_ptr<dcp::SubtitleAsset> asset, bool reference, int64_t entry_point, shared_ptr<TextDecoder> decoder, dcp::Size size
285         )
286 {
287         auto const vfr = _dcp_content->active_video_frame_rate (film());
288         /* Frame within the (played part of the) reel that is coming up next */
289         auto const frame = next.frames_round (vfr);
290
291         if (_decode_referenced || !reference) {
292                 auto subs = asset->subtitles_during (
293                         dcp::Time (entry_point + frame, vfr, vfr),
294                         dcp::Time (entry_point + frame + 1, vfr, vfr),
295                         true
296                         );
297
298                 vector<dcp::SubtitleString> strings;
299
300                 for (auto i: subs) {
301                         auto is = dynamic_pointer_cast<const dcp::SubtitleString>(i);
302                         if (is) {
303                                 if (!strings.empty() && (strings.back().in() != is->in() || strings.back().out() != is->out())) {
304                                         auto b = strings.back();
305                                         decoder->emit_plain (
306                                                 ContentTimePeriod (
307                                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
308                                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
309                                                         ),
310                                                 strings,
311                                                 asset->subtitle_standard()
312                                                 );
313                                         strings.clear ();
314                                 }
315
316                                 dcp::SubtitleString is_copy = *is;
317                                 if (is_copy.font()) {
318                                         is_copy.set_font(_font_id_allocator.font_id(_reel - _reels.begin(), asset->id(), is_copy.font().get()));
319                                 } else {
320                                         is_copy.set_font(_font_id_allocator.default_font_id());
321                                 }
322                                 strings.push_back(is_copy);
323                         }
324
325                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
326                            this would need to be done both here and in DCPSubtitleDecoder.
327                         */
328
329                         auto ii = dynamic_pointer_cast<const dcp::SubtitleImage>(i);
330                         if (ii) {
331                                 emit_subtitle_image (
332                                         ContentTimePeriod (
333                                                 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->in().as_seconds ()),
334                                                 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->out().as_seconds ())
335                                                 ),
336                                         *ii,
337                                         size,
338                                         decoder
339                                         );
340                         }
341                 }
342
343                 if (!strings.empty()) {
344                         auto b = strings.back();
345                         decoder->emit_plain (
346                                 ContentTimePeriod (
347                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
348                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
349                                         ),
350                                 strings,
351                                 asset->subtitle_standard()
352                                 );
353                         strings.clear ();
354                 }
355         }
356 }
357
358
359 void
360 DCPDecoder::next_reel ()
361 {
362         _offset += (*_reel)->main_picture()->actual_duration();
363         ++_reel;
364         get_readers ();
365 }
366
367
368 void
369 DCPDecoder::get_readers ()
370 {
371         if (_reel == _reels.end() || !_dcp_content->can_be_played ()) {
372                 _mono_reader.reset ();
373                 _stereo_reader.reset ();
374                 _sound_reader.reset ();
375                 _atmos_reader.reset ();
376                 return;
377         }
378
379         if (video && !video->ignore() && (*_reel)->main_picture()) {
380                 auto asset = (*_reel)->main_picture()->asset ();
381                 auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
382                 auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
383                 DCPOMATIC_ASSERT (mono || stereo);
384                 if (mono) {
385                         _mono_reader = mono->start_read ();
386                         _mono_reader->set_check_hmac (false);
387                         _stereo_reader.reset ();
388                 } else {
389                         _stereo_reader = stereo->start_read ();
390                         _stereo_reader->set_check_hmac (false);
391                         _mono_reader.reset ();
392                 }
393         } else {
394                 _mono_reader.reset ();
395                 _stereo_reader.reset ();
396         }
397
398         if (audio && !audio->ignore() && (*_reel)->main_sound()) {
399                 _sound_reader = (*_reel)->main_sound()->asset()->start_read ();
400                 _sound_reader->set_check_hmac (false);
401         } else {
402                 _sound_reader.reset ();
403         }
404
405         if ((*_reel)->atmos()) {
406                 auto asset = (*_reel)->atmos()->asset();
407                 _atmos_reader = asset->start_read();
408                 _atmos_reader->set_check_hmac (false);
409                 _atmos_metadata = AtmosMetadata (asset);
410         } else {
411                 _atmos_reader.reset ();
412                 _atmos_metadata = boost::none;
413         }
414 }
415
416
417 void
418 DCPDecoder::seek (ContentTime t, bool accurate)
419 {
420         if (!_dcp_content->can_be_played ()) {
421                 return;
422         }
423
424         Decoder::seek (t, accurate);
425
426         _reel = _reels.begin ();
427         _offset = 0;
428         get_readers ();
429
430         int const pre_roll_seconds = 2;
431
432         /* Pre-roll for subs */
433
434         auto pre = t - ContentTime::from_seconds (pre_roll_seconds);
435         if (pre < ContentTime()) {
436                 pre = ContentTime ();
437         }
438
439         /* Seek to pre-roll position */
440
441         while (
442                 _reel != _reels.end() &&
443                 pre >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
444                 ) {
445
446                 auto rd = ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
447                 pre -= rd;
448                 t -= rd;
449                 next_reel ();
450         }
451
452         /* Pass texts in the pre-roll */
453
454         if (_reel != _reels.end()) {
455                 auto const vfr = _dcp_content->active_video_frame_rate (film());
456                 for (int i = 0; i < pre_roll_seconds * vfr; ++i) {
457                         pass_texts (pre, (*_reel)->main_picture()->asset()->size());
458                         pre += ContentTime::from_frames (1, vfr);
459                 }
460         }
461
462         /* Seek to correct position */
463
464         while (
465                 _reel != _reels.end() &&
466                 t >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
467                 ) {
468
469                 t -= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
470                 next_reel ();
471         }
472
473         _next = t;
474 }
475
476
477 void
478 DCPDecoder::set_decode_referenced (bool r)
479 {
480         _decode_referenced = r;
481
482         if (video) {
483                 video->set_ignore (_dcp_content->reference_video() && !_decode_referenced);
484         }
485         if (audio) {
486                 audio->set_ignore (_dcp_content->reference_audio() && !_decode_referenced);
487         }
488 }
489
490
491 void
492 DCPDecoder::set_forced_reduction (optional<int> reduction)
493 {
494         _forced_reduction = reduction;
495 }
496
497
498 string
499 DCPDecoder::calculate_lazy_digest (shared_ptr<const DCPContent> c) const
500 {
501         Digester d;
502         for (auto i: c->paths()) {
503                 d.add (i.string());
504         }
505         if (_dcp_content->kdm()) {
506                 d.add(_dcp_content->kdm()->id());
507         }
508         d.add (static_cast<bool>(c->cpl()));
509         if (c->cpl()) {
510                 d.add (c->cpl().get());
511         }
512         return d.get ();
513 }
514
515
516 ContentTime
517 DCPDecoder::position () const
518 {
519         return ContentTime::from_frames(_offset, _dcp_content->active_video_frame_rate(film())) + _next;
520 }
521