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