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