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