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