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