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