2 Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
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"
31 #include <dcp/decrypted_kdm.h>
34 #include <dcp/mono_picture_asset.h>
35 #include <dcp/stereo_picture_asset.h>
36 #include <dcp/reel_picture_asset.h>
37 #include <dcp/reel_sound_asset.h>
38 #include <dcp/reel_subtitle_asset.h>
39 #include <dcp/mono_picture_frame.h>
40 #include <dcp/stereo_picture_frame.h>
41 #include <dcp/sound_frame.h>
42 #include <boost/foreach.hpp>
47 using boost::shared_ptr;
48 using boost::dynamic_pointer_cast;
50 DCPDecoder::DCPDecoder (shared_ptr<const DCPContent> c, shared_ptr<Log> log, bool fast)
53 video.reset (new VideoDecoder (this, c, log));
54 audio.reset (new AudioDecoder (this, c->audio, fast, log));
60 bind (&DCPDecoder::image_subtitles_during, this, _1, _2),
61 bind (&DCPDecoder::text_subtitles_during, this, _1, _2)
65 dcp::DCP dcp (c->directory ());
66 dcp.read (false, 0, true);
68 dcp.add (dcp::DecryptedKDM (c->kdm().get (), Config::instance()->decryption_chain()->key().get ()));
70 DCPOMATIC_ASSERT (dcp.cpls().size() == 1);
71 _reels = dcp.cpls().front()->reels ();
72 _reel = _reels.begin ();
76 DCPDecoder::pass (PassReason reason, bool)
78 if (_reel == _reels.end () || !_dcp_content->can_be_played ()) {
82 /* Offset of the start of the current reel from the start of the content in frames */
84 list<shared_ptr<dcp::Reel> >::const_iterator i = _reels.begin();
86 offset += (*i)->main_picture()->duration ();
90 double const vfr = _dcp_content->active_video_frame_rate ();
92 /* Frame within the (played part of the) reel that is coming up next */
93 int64_t const frame = _next.frames_round (vfr);
95 if ((*_reel)->main_picture () && reason != PASS_REASON_SUBTITLE) {
96 shared_ptr<dcp::PictureAsset> asset = (*_reel)->main_picture()->asset ();
97 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
98 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
99 int64_t const entry_point = (*_reel)->main_picture()->entry_point ();
101 video->give (shared_ptr<ImageProxy> (new J2KImageProxy (mono->get_frame (entry_point + frame), asset->size())), offset + frame);
104 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), asset->size(), dcp::EYE_LEFT)),
109 shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame (entry_point + frame), asset->size(), dcp::EYE_RIGHT)),
115 if ((*_reel)->main_sound () && reason != PASS_REASON_SUBTITLE) {
116 int64_t const entry_point = (*_reel)->main_sound()->entry_point ();
117 shared_ptr<const dcp::SoundFrame> sf = (*_reel)->main_sound()->asset()->get_frame (entry_point + frame);
118 uint8_t const * from = sf->data ();
120 int const channels = _dcp_content->audio->stream()->channels ();
121 int const frames = sf->size() / (3 * channels);
122 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, frames));
123 for (int i = 0; i < frames; ++i) {
124 for (int j = 0; j < channels; ++j) {
125 data->data()[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
130 audio->give (_dcp_content->audio->stream(), data, ContentTime::from_frames (offset, vfr) + _next);
133 if ((*_reel)->main_subtitle ()) {
134 int64_t const entry_point = (*_reel)->main_subtitle()->entry_point ();
135 list<dcp::SubtitleString> subs = (*_reel)->main_subtitle()->asset()->subtitles_during (
136 dcp::Time (entry_point + frame, vfr, vfr),
137 dcp::Time (entry_point + frame + 1, vfr, vfr),
141 if (!subs.empty ()) {
142 /* XXX: assuming that all `subs' are at the same time; maybe this is ok */
143 subtitle->give_text (
145 ContentTime::from_frames (offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().in().as_seconds ()),
146 ContentTime::from_frames (offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().out().as_seconds ())
153 _next += ContentTime::from_frames (1, vfr);
155 if ((*_reel)->main_picture ()) {
156 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
158 _next = ContentTime ();
166 DCPDecoder::seek (ContentTime t, bool accurate)
168 video->seek (t, accurate);
169 audio->seek (t, accurate);
170 subtitle->seek (t, accurate);
172 _reel = _reels.begin ();
173 while (_reel != _reels.end() && t >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate ())) {
174 t -= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate ());
182 list<ContentTimePeriod>
183 DCPDecoder::image_subtitles_during (ContentTimePeriod, bool) const
185 return list<ContentTimePeriod> ();
188 list<ContentTimePeriod>
189 DCPDecoder::text_subtitles_during (ContentTimePeriod period, bool starting) const
191 /* XXX: inefficient */
193 list<ContentTimePeriod> ctp;
194 double const vfr = _dcp_content->active_video_frame_rate ();
196 BOOST_FOREACH (shared_ptr<dcp::Reel> r, _reels) {
197 if (!r->main_subtitle ()) {
201 int64_t const entry_point = r->main_subtitle()->entry_point ();
203 list<dcp::SubtitleString> subs = r->main_subtitle()->asset()->subtitles_during (
204 dcp::Time (period.from.seconds(), 1000) - dcp::Time (entry_point, vfr, vfr),
205 dcp::Time (period.to.seconds(), 1000) - dcp::Time (entry_point, vfr, vfr),
209 BOOST_FOREACH (dcp::SubtitleString const & s, subs) {
212 ContentTime::from_seconds (s.in().as_seconds ()),
213 ContentTime::from_seconds (s.out().as_seconds ())