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