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