d473476a00cf7c91b7cbd040b1a314d7587729bc
[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_j2k_picture_asset.h>
40 #include <dcp/mono_j2k_picture_asset_reader.h>
41 #include <dcp/mono_j2k_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_j2k_picture_asset.h>
52 #include <dcp/stereo_j2k_picture_asset_reader.h>
53 #include <dcp/stereo_j2k_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                 auto cpl_list = dcp::find_and_resolve_cpls(content->directories(), tolerant);
109
110                 if (cpl_list.empty()) {
111                         throw DCPError (_("No CPLs found in DCP."));
112                 }
113
114                 shared_ptr<dcp::CPL> cpl;
115                 for (auto i: cpl_list) {
116                         if (_dcp_content->cpl() && i->id() == _dcp_content->cpl().get()) {
117                                 cpl = i;
118                         }
119                 }
120
121                 if (!cpl) {
122                         /* No CPL found; probably an old file that doesn't specify it;
123                            just use the first one.
124                         */
125                         cpl = cpl_list.front();
126                 }
127
128                 if (content->kdm()) {
129                         cpl->add (decrypt_kdm_with_helpful_error(content->kdm().get()));
130                 }
131
132                 _reels = cpl->reels ();
133         }
134
135         set_decode_referenced (false);
136
137         _reel = _reels.begin ();
138         get_readers ();
139
140         _font_id_allocator.add_fonts_from_reels(_reels);
141         _font_id_allocator.allocate();
142 }
143
144
145 bool
146 DCPDecoder::pass ()
147 {
148         if (!_dcp_content->can_be_played()) {
149                 return true;
150         }
151
152         if (_reel == _reels.end()) {
153                 if (audio) {
154                         audio->flush ();
155                 }
156                 return true;
157         }
158
159         auto const vfr = _dcp_content->active_video_frame_rate (film());
160
161         /* Frame within the (played part of the) reel that is coming up next */
162         auto const frame = _next.frames_round (vfr);
163
164         auto picture_asset = (*_reel)->main_picture()->asset();
165         DCPOMATIC_ASSERT (picture_asset);
166
167         /* We must emit texts first as when we emit the video for this frame
168            it will expect already to have the texts.
169         */
170         pass_texts (_next, picture_asset->size());
171
172         if ((_j2k_mono_reader || _j2k_stereo_reader) && (_decode_referenced || !_dcp_content->reference_video())) {
173                 auto const entry_point = (*_reel)->main_picture()->entry_point().get_value_or(0);
174                 if (_j2k_mono_reader) {
175                         video->emit (
176                                 film(),
177                                 std::make_shared<J2KImageProxy>(
178                                         _j2k_mono_reader->get_frame(entry_point + frame),
179                                         picture_asset->size(),
180                                         AV_PIX_FMT_XYZ12LE,
181                                         _forced_reduction
182                                         ),
183                                 ContentTime::from_frames(_offset + frame, vfr)
184                                 );
185                 } else if (_j2k_stereo_reader) {
186                         video->emit (
187                                 film(),
188                                 std::make_shared<J2KImageProxy>(
189                                         _j2k_stereo_reader->get_frame (entry_point + frame),
190                                         picture_asset->size(),
191                                         dcp::Eye::LEFT,
192                                         AV_PIX_FMT_XYZ12LE,
193                                         _forced_reduction
194                                         ),
195                                 ContentTime::from_frames(_offset + frame, vfr)
196                                 );
197
198                         video->emit (
199                                 film(),
200                                 std::make_shared<J2KImageProxy>(
201                                         _j2k_stereo_reader->get_frame (entry_point + frame),
202                                         picture_asset->size(),
203                                         dcp::Eye::RIGHT,
204                                         AV_PIX_FMT_XYZ12LE,
205                                         _forced_reduction
206                                         ),
207                                 ContentTime::from_frames(_offset + frame, vfr)
208                                 );
209                 }
210         }
211
212         if (_sound_reader && (_decode_referenced || !_dcp_content->reference_audio())) {
213                 auto const entry_point = (*_reel)->main_sound()->entry_point().get_value_or(0);
214                 auto sf = _sound_reader->get_frame (entry_point + frame);
215                 auto from = sf->data ();
216
217                 int const channels = _dcp_content->audio->stream()->channels ();
218                 int const frames = sf->size() / (3 * channels);
219                 auto data = make_shared<AudioBuffers>(channels, frames);
220                 auto data_data = data->data();
221                 for (int i = 0; i < frames; ++i) {
222                         for (int j = 0; j < channels; ++j) {
223                                 data_data[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
224                                 from += 3;
225                         }
226                 }
227
228                 audio->emit (film(), _dcp_content->audio->stream(), data, ContentTime::from_frames (_offset, vfr) + _next);
229         }
230
231         if (_atmos_reader) {
232                 DCPOMATIC_ASSERT (_atmos_metadata);
233                 auto const entry_point = (*_reel)->atmos()->entry_point().get_value_or(0);
234                 atmos->emit (film(), _atmos_reader->get_frame(entry_point + frame), _offset + frame, *_atmos_metadata);
235         }
236
237         _next += ContentTime::from_frames (1, vfr);
238
239         if ((*_reel)->main_picture ()) {
240                 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
241                         next_reel ();
242                         _next = ContentTime ();
243                 }
244         }
245
246         return false;
247 }
248
249
250 void
251 DCPDecoder::pass_texts (ContentTime next, dcp::Size size)
252 {
253         auto decoder = text.begin ();
254         if (decoder == text.end()) {
255                 /* It's possible that there is now a main subtitle but no TextDecoders, for example if
256                    the CPL has just changed but the TextContent's texts have not been recreated yet.
257                 */
258                 return;
259         }
260
261         if ((*_reel)->main_subtitle()) {
262                 pass_texts (
263                         next,
264                         (*_reel)->main_subtitle()->asset(),
265                         _dcp_content->reference_text(TextType::OPEN_SUBTITLE),
266                         (*_reel)->main_subtitle()->entry_point().get_value_or(0),
267                         *decoder,
268                         size
269                         );
270                 ++decoder;
271         }
272
273         for (auto i: (*_reel)->closed_captions()) {
274                 pass_texts (
275                         next, i->asset(), _dcp_content->reference_text(TextType::CLOSED_CAPTION), i->entry_point().get_value_or(0), *decoder, size
276                         );
277                 ++decoder;
278         }
279 }
280
281 void
282 DCPDecoder::pass_texts (
283         ContentTime next, shared_ptr<dcp::SubtitleAsset> asset, bool reference, int64_t entry_point, shared_ptr<TextDecoder> decoder, dcp::Size size
284         )
285 {
286         auto const vfr = _dcp_content->active_video_frame_rate (film());
287         /* Frame within the (played part of the) reel that is coming up next */
288         auto const frame = next.frames_round (vfr);
289
290         if (_decode_referenced || !reference) {
291                 auto subs = asset->subtitles_during (
292                         dcp::Time (entry_point + frame, vfr, vfr),
293                         dcp::Time (entry_point + frame + 1, vfr, vfr),
294                         true
295                         );
296
297                 vector<dcp::SubtitleString> strings;
298
299                 for (auto i: subs) {
300                         auto is = dynamic_pointer_cast<const dcp::SubtitleString>(i);
301                         if (is) {
302                                 if (!strings.empty() && (strings.back().in() != is->in() || strings.back().out() != is->out())) {
303                                         auto b = strings.back();
304                                         decoder->emit_plain (
305                                                 ContentTimePeriod (
306                                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
307                                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
308                                                         ),
309                                                 strings,
310                                                 asset->subtitle_standard()
311                                                 );
312                                         strings.clear ();
313                                 }
314
315                                 dcp::SubtitleString is_copy = *is;
316                                 if (is_copy.font()) {
317                                         is_copy.set_font(_font_id_allocator.font_id(_reel - _reels.begin(), asset->id(), is_copy.font().get()));
318                                 } else {
319                                         is_copy.set_font(_font_id_allocator.default_font_id());
320                                 }
321                                 strings.push_back(is_copy);
322                         }
323
324                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
325                            this would need to be done both here and in DCPSubtitleDecoder.
326                         */
327
328                         auto ii = dynamic_pointer_cast<const dcp::SubtitleImage>(i);
329                         if (ii) {
330                                 emit_subtitle_image (
331                                         ContentTimePeriod (
332                                                 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->in().as_seconds ()),
333                                                 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->out().as_seconds ())
334                                                 ),
335                                         *ii,
336                                         size,
337                                         decoder
338                                         );
339                         }
340                 }
341
342                 if (!strings.empty()) {
343                         auto b = strings.back();
344                         decoder->emit_plain (
345                                 ContentTimePeriod (
346                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
347                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
348                                         ),
349                                 strings,
350                                 asset->subtitle_standard()
351                                 );
352                         strings.clear ();
353                 }
354         }
355 }
356
357
358 void
359 DCPDecoder::next_reel ()
360 {
361         _offset += (*_reel)->main_picture()->actual_duration();
362         ++_reel;
363         get_readers ();
364 }
365
366
367 void
368 DCPDecoder::get_readers ()
369 {
370         _j2k_mono_reader.reset();
371         _j2k_stereo_reader.reset();
372         _sound_reader.reset();
373         _atmos_reader.reset();
374         _atmos_metadata = boost::none;
375
376         if (_reel == _reels.end() || !_dcp_content->can_be_played ()) {
377                 return;
378         }
379
380         if (video && !video->ignore() && (*_reel)->main_picture()) {
381                 auto asset = (*_reel)->main_picture()->asset ();
382                 auto j2k_mono = dynamic_pointer_cast<dcp::MonoJ2KPictureAsset>(asset);
383                 auto j2k_stereo = dynamic_pointer_cast<dcp::StereoJ2KPictureAsset>(asset);
384                 DCPOMATIC_ASSERT(j2k_mono || j2k_stereo)
385                 if (j2k_mono) {
386                         _j2k_mono_reader = j2k_mono->start_read();
387                         _j2k_mono_reader->set_check_hmac(false);
388                 } else if (j2k_stereo) {
389                         _j2k_stereo_reader = j2k_stereo->start_read();
390                         _j2k_stereo_reader->set_check_hmac(false);
391                 }
392         }
393
394         if (audio && !audio->ignore() && (*_reel)->main_sound()) {
395                 _sound_reader = (*_reel)->main_sound()->asset()->start_read ();
396                 _sound_reader->set_check_hmac (false);
397         }
398
399         if ((*_reel)->atmos()) {
400                 auto asset = (*_reel)->atmos()->asset();
401                 _atmos_reader = asset->start_read();
402                 _atmos_reader->set_check_hmac (false);
403                 _atmos_metadata = AtmosMetadata (asset);
404         }
405 }
406
407
408 void
409 DCPDecoder::seek (ContentTime t, bool accurate)
410 {
411         if (!_dcp_content->can_be_played ()) {
412                 return;
413         }
414
415         Decoder::seek (t, accurate);
416
417         _reel = _reels.begin ();
418         _offset = 0;
419         get_readers ();
420
421         int const pre_roll_seconds = 2;
422
423         /* Pre-roll for subs */
424
425         auto pre = t - ContentTime::from_seconds (pre_roll_seconds);
426         if (pre < ContentTime()) {
427                 pre = ContentTime ();
428         }
429
430         /* Seek to pre-roll position */
431
432         while (
433                 _reel != _reels.end() &&
434                 pre >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
435                 ) {
436
437                 auto rd = ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
438                 pre -= rd;
439                 t -= rd;
440                 next_reel ();
441         }
442
443         /* Pass texts in the pre-roll */
444
445         if (_reel != _reels.end()) {
446                 auto const vfr = _dcp_content->active_video_frame_rate (film());
447                 for (int i = 0; i < pre_roll_seconds * vfr; ++i) {
448                         pass_texts (pre, (*_reel)->main_picture()->asset()->size());
449                         pre += ContentTime::from_frames (1, vfr);
450                 }
451         }
452
453         /* Seek to correct position */
454
455         while (
456                 _reel != _reels.end() &&
457                 t >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
458                 ) {
459
460                 t -= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
461                 next_reel ();
462         }
463
464         _next = t;
465 }
466
467
468 void
469 DCPDecoder::set_decode_referenced (bool r)
470 {
471         _decode_referenced = r;
472
473         if (video) {
474                 video->set_ignore (_dcp_content->reference_video() && !_decode_referenced);
475         }
476         if (audio) {
477                 audio->set_ignore (_dcp_content->reference_audio() && !_decode_referenced);
478         }
479 }
480
481
482 void
483 DCPDecoder::set_forced_reduction (optional<int> reduction)
484 {
485         _forced_reduction = reduction;
486 }
487
488
489 string
490 DCPDecoder::calculate_lazy_digest (shared_ptr<const DCPContent> c) const
491 {
492         Digester d;
493         for (auto i: c->paths()) {
494                 d.add (i.string());
495         }
496         if (_dcp_content->kdm()) {
497                 d.add(_dcp_content->kdm()->id());
498         }
499         d.add (static_cast<bool>(c->cpl()));
500         if (c->cpl()) {
501                 d.add (c->cpl().get());
502         }
503         return d.get ();
504 }
505
506
507 ContentTime
508 DCPDecoder::position () const
509 {
510         return ContentTime::from_frames(_offset, _dcp_content->active_video_frame_rate(film())) + _next;
511 }
512