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