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