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