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