Rename video/audio/subtitle part 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 "j2k_image_proxy.h"
24 #include "image.h"
25 #include "config.h"
26 #include <dcp/dcp.h>
27 #include <dcp/decrypted_kdm.h>
28 #include <dcp/cpl.h>
29 #include <dcp/reel.h>
30 #include <dcp/mono_picture_asset.h>
31 #include <dcp/stereo_picture_asset.h>
32 #include <dcp/reel_picture_asset.h>
33 #include <dcp/reel_sound_asset.h>
34 #include <dcp/reel_subtitle_asset.h>
35 #include <dcp/mono_picture_frame.h>
36 #include <dcp/stereo_picture_frame.h>
37 #include <dcp/sound_frame.h>
38 #include <boost/foreach.hpp>
39 #include <iostream>
40
41 using std::list;
42 using std::cout;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45
46 DCPDecoder::DCPDecoder (shared_ptr<const DCPContent> c, shared_ptr<Log> log, bool fast)
47         : VideoDecoder (c->video, log)
48         , AudioDecoder (c->audio, fast, log)
49         , SubtitleDecoder (c->subtitle)
50         , _dcp_content (c)
51 {
52         dcp::DCP dcp (c->directory ());
53         dcp.read (false, 0, true);
54         if (c->kdm ()) {
55                 dcp.add (dcp::DecryptedKDM (c->kdm().get (), Config::instance()->decryption_chain()->key().get ()));
56         }
57         DCPOMATIC_ASSERT (dcp.cpls().size() == 1);
58         _reels = dcp.cpls().front()->reels ();
59         _reel = _reels.begin ();
60 }
61
62 bool
63 DCPDecoder::pass (PassReason reason, bool)
64 {
65         if (_reel == _reels.end () || !_dcp_content->can_be_played ()) {
66                 return true;
67         }
68
69         /* Offset of the start of the current reel from the start of the content in frames */
70         int offset = 0;
71         list<shared_ptr<dcp::Reel> >::const_iterator i = _reels.begin();
72         while (i != _reel) {
73                 offset += (*i)->main_picture()->duration ();
74                 ++i;
75         }
76
77         double const vfr = _dcp_content->video->frame_rate ();
78
79         /* Frame within the (played part of the) reel that is coming up next */
80         int64_t const frame = _next.frames_round (vfr);
81
82         if ((*_reel)->main_picture () && reason != PASS_REASON_SUBTITLE) {
83                 shared_ptr<dcp::PictureAsset> asset = (*_reel)->main_picture()->asset ();
84                 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
85                 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
86                 int64_t const entry_point = (*_reel)->main_picture()->entry_point ();
87                 if (mono) {
88                         video (shared_ptr<ImageProxy> (new J2KImageProxy (mono->get_frame (entry_point + frame), asset->size())), offset + frame);
89                 } else {
90                         video (
91                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), asset->size(), dcp::EYE_LEFT)),
92                                 offset + frame
93                                 );
94
95                         video (
96                                 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), asset->size(), dcp::EYE_RIGHT)),
97                                 offset + frame
98                                 );
99                 }
100         }
101
102         if ((*_reel)->main_sound () && reason != PASS_REASON_SUBTITLE) {
103                 int64_t const entry_point = (*_reel)->main_sound()->entry_point ();
104                 shared_ptr<const dcp::SoundFrame> sf = (*_reel)->main_sound()->asset()->get_frame (entry_point + frame);
105                 uint8_t const * from = sf->data ();
106
107                 int const channels = _dcp_content->audio->stream()->channels ();
108                 int const frames = sf->size() / (3 * channels);
109                 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, frames));
110                 for (int i = 0; i < frames; ++i) {
111                         for (int j = 0; j < channels; ++j) {
112                                 data->data()[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
113                                 from += 3;
114                         }
115                 }
116
117                 audio (_dcp_content->audio->stream(), data, ContentTime::from_frames (offset, vfr) + _next);
118         }
119
120         if ((*_reel)->main_subtitle ()) {
121                 int64_t const entry_point = (*_reel)->main_subtitle()->entry_point ();
122                 list<dcp::SubtitleString> subs = (*_reel)->main_subtitle()->asset()->subtitles_during (
123                         dcp::Time (entry_point + frame, vfr, vfr),
124                         dcp::Time (entry_point + frame + 1, vfr, vfr),
125                         true
126                         );
127
128                 if (!subs.empty ()) {
129                         /* XXX: assuming that all `subs' are at the same time; maybe this is ok */
130                         text_subtitle (
131                                 ContentTimePeriod (
132                                         ContentTime::from_frames (offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().in().as_seconds ()),
133                                         ContentTime::from_frames (offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().out().as_seconds ())
134                                         ),
135                                 subs
136                                 );
137                 }
138         }
139
140         _next += ContentTime::from_frames (1, vfr);
141
142         if ((*_reel)->main_picture ()) {
143                 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
144                         ++_reel;
145                         _next = ContentTime ();
146                 }
147         }
148
149         return false;
150 }
151
152 void
153 DCPDecoder::seek (ContentTime t, bool accurate)
154 {
155         VideoDecoder::seek (t, accurate);
156         AudioDecoder::seek (t, accurate);
157         SubtitleDecoder::seek (t, accurate);
158
159         _reel = _reels.begin ();
160         while (_reel != _reels.end() && t >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video->frame_rate ())) {
161                 t -= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->video->frame_rate ());
162                 ++_reel;
163         }
164
165         _next = t;
166 }
167
168
169 list<ContentTimePeriod>
170 DCPDecoder::image_subtitles_during (ContentTimePeriod, bool) const
171 {
172         return list<ContentTimePeriod> ();
173 }
174
175 list<ContentTimePeriod>
176 DCPDecoder::text_subtitles_during (ContentTimePeriod period, bool starting) const
177 {
178         /* XXX: inefficient */
179
180         list<ContentTimePeriod> ctp;
181         double const vfr = _dcp_content->video->frame_rate ();
182
183         BOOST_FOREACH (shared_ptr<dcp::Reel> r, _reels) {
184                 if (!r->main_subtitle ()) {
185                         continue;
186                 }
187
188                 int64_t const entry_point = r->main_subtitle()->entry_point ();
189
190                 list<dcp::SubtitleString> subs = r->main_subtitle()->asset()->subtitles_during (
191                         dcp::Time (period.from.seconds(), 1000) - dcp::Time (entry_point, vfr, vfr),
192                         dcp::Time (period.to.seconds(), 1000) - dcp::Time (entry_point, vfr, vfr),
193                         starting
194                         );
195
196                 BOOST_FOREACH (dcp::SubtitleString const & s, subs) {
197                         ctp.push_back (
198                                 ContentTimePeriod (
199                                         ContentTime::from_seconds (s.in().as_seconds ()),
200                                         ContentTime::from_seconds (s.out().as_seconds ())
201                                         )
202                                 );
203                 }
204         }
205
206         return ctp;
207 }