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