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