Rename some methods.
[dcpomatic.git] / src / lib / dcp_decoder.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "dcp_decoder.h"
21 #include "dcp_content.h"
22 #include "audio_content.h"
23 #include "video_decoder.h"
24 #include "audio_decoder.h"
25 #include "j2k_image_proxy.h"
26 #include "subtitle_decoder.h"
27 #include "image.h"
28 #include "config.h"
29 #include <dcp/dcp.h>
30 #include <dcp/decrypted_kdm.h>
31 #include <dcp/cpl.h>
32 #include <dcp/reel.h>
33 #include <dcp/mono_picture_asset.h>
34 #include <dcp/stereo_picture_asset.h>
35 #include <dcp/reel_picture_asset.h>
36 #include <dcp/reel_sound_asset.h>
37 #include <dcp/reel_subtitle_asset.h>
38 #include <dcp/mono_picture_frame.h>
39 #include <dcp/stereo_picture_frame.h>
40 #include <dcp/sound_frame.h>
41 #include <boost/foreach.hpp>
42 #include <iostream>
43
44 using std::list;
45 using std::cout;
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
48
49 DCPDecoder::DCPDecoder (shared_ptr<const DCPContent> c, shared_ptr<Log> log, bool fast)
50         : _dcp_content (c)
51 {
52         video.reset (new VideoDecoder (this, c, log));
53         audio.reset (new AudioDecoder (this, c->audio, fast, log));
54
55         subtitle.reset (
56                 new SubtitleDecoder (
57                         this,
58                         c->subtitle,
59                         bind (&DCPDecoder::image_subtitles_during, this, _1, _2),
60                         bind (&DCPDecoder::text_subtitles_during, this, _1, _2)
61                         )
62                 );
63
64         dcp::DCP dcp (c->directory ());
65         dcp.read (false, 0, true);
66         if (c->kdm ()) {
67                 dcp.add (dcp::DecryptedKDM (c->kdm().get (), Config::instance()->decryption_chain()->key().get ()));
68         }
69         DCPOMATIC_ASSERT (dcp.cpls().size() == 1);
70         _reels = dcp.cpls().front()->reels ();
71         _reel = _reels.begin ();
72 }
73
74 bool
75 DCPDecoder::pass (PassReason reason, bool)
76 {
77         if (_reel == _reels.end () || !_dcp_content->can_be_played ()) {
78                 return true;
79         }
80
81         /* Offset of the start of the current reel from the start of the content in frames */
82         int offset = 0;
83         list<shared_ptr<dcp::Reel> >::const_iterator i = _reels.begin();
84         while (i != _reel) {
85                 offset += (*i)->main_picture()->duration ();
86                 ++i;
87         }
88
89         double const vfr = _dcp_content->active_video_frame_rate ();
90
91         /* Frame within the (played part of the) reel that is coming up next */
92         int64_t const frame = _next.frames_round (vfr);
93
94         if ((*_reel)->main_picture () && reason != PASS_REASON_SUBTITLE) {
95                 shared_ptr<dcp::PictureAsset> asset = (*_reel)->main_picture()->asset ();
96                 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
97                 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
98                 int64_t const entry_point = (*_reel)->main_picture()->entry_point ();
99                 if (mono) {
100                         video->give (shared_ptr<ImageProxy> (new J2KImageProxy (mono->get_frame (entry_point + frame), asset->size())), offset + frame);
101                 } else {
102                         video->give (
103                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), asset->size(), dcp::EYE_LEFT)),
104                                 offset + frame
105                                 );
106
107                         video->give (
108                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), asset->size(), dcp::EYE_RIGHT)),
109                                 offset + frame
110                                 );
111                 }
112         }
113
114         if ((*_reel)->main_sound () && reason != PASS_REASON_SUBTITLE) {
115                 int64_t const entry_point = (*_reel)->main_sound()->entry_point ();
116                 shared_ptr<const dcp::SoundFrame> sf = (*_reel)->main_sound()->asset()->get_frame (entry_point + frame);
117                 uint8_t const * from = sf->data ();
118
119                 int const channels = _dcp_content->audio->stream()->channels ();
120                 int const frames = sf->size() / (3 * channels);
121                 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, frames));
122                 for (int i = 0; i < frames; ++i) {
123                         for (int j = 0; j < channels; ++j) {
124                                 data->data()[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
125                                 from += 3;
126                         }
127                 }
128
129                 audio->give (_dcp_content->audio->stream(), data, ContentTime::from_frames (offset, vfr) + _next);
130         }
131
132         if ((*_reel)->main_subtitle ()) {
133                 int64_t const entry_point = (*_reel)->main_subtitle()->entry_point ();
134                 list<dcp::SubtitleString> subs = (*_reel)->main_subtitle()->asset()->subtitles_during (
135                         dcp::Time (entry_point + frame, vfr, vfr),
136                         dcp::Time (entry_point + frame + 1, vfr, vfr),
137                         true
138                         );
139
140                 if (!subs.empty ()) {
141                         /* XXX: assuming that all `subs' are at the same time; maybe this is ok */
142                         subtitle->give_text (
143                                 ContentTimePeriod (
144                                         ContentTime::from_frames (offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().in().as_seconds ()),
145                                         ContentTime::from_frames (offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().out().as_seconds ())
146                                         ),
147                                 subs
148                                 );
149                 }
150         }
151
152         _next += ContentTime::from_frames (1, vfr);
153
154         if ((*_reel)->main_picture ()) {
155                 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
156                         ++_reel;
157                         _next = ContentTime ();
158                 }
159         }
160
161         return false;
162 }
163
164 void
165 DCPDecoder::seek (ContentTime t, bool accurate)
166 {
167         video->seek (t, accurate);
168         audio->seek (t, accurate);
169         subtitle->seek (t, accurate);
170
171         _reel = _reels.begin ();
172         while (_reel != _reels.end() && t >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate ())) {
173                 t -= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate ());
174                 ++_reel;
175         }
176
177         _next = t;
178 }
179
180
181 list<ContentTimePeriod>
182 DCPDecoder::image_subtitles_during (ContentTimePeriod, bool) const
183 {
184         return list<ContentTimePeriod> ();
185 }
186
187 list<ContentTimePeriod>
188 DCPDecoder::text_subtitles_during (ContentTimePeriod period, bool starting) const
189 {
190         /* XXX: inefficient */
191
192         list<ContentTimePeriod> ctp;
193         double const vfr = _dcp_content->active_video_frame_rate ();
194
195         BOOST_FOREACH (shared_ptr<dcp::Reel> r, _reels) {
196                 if (!r->main_subtitle ()) {
197                         continue;
198                 }
199
200                 int64_t const entry_point = r->main_subtitle()->entry_point ();
201
202                 list<dcp::SubtitleString> subs = r->main_subtitle()->asset()->subtitles_during (
203                         dcp::Time (period.from.seconds(), 1000) - dcp::Time (entry_point, vfr, vfr),
204                         dcp::Time (period.to.seconds(), 1000) - dcp::Time (entry_point, vfr, vfr),
205                         starting
206                         );
207
208                 BOOST_FOREACH (dcp::SubtitleString const & s, subs) {
209                         ctp.push_back (
210                                 ContentTimePeriod (
211                                         ContentTime::from_seconds (s.in().as_seconds ()),
212                                         ContentTime::from_seconds (s.out().as_seconds ())
213                                         )
214                                 );
215                 }
216         }
217
218         return ctp;
219 }