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