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