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