2 Copyright (C) 2014-2018 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 "text_decoder.h"
28 #include "ffmpeg_image_proxy.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/reel_closed_caption_asset.h>
42 #include <dcp/mono_picture_frame.h>
43 #include <dcp/stereo_picture_frame.h>
44 #include <dcp/sound_frame.h>
45 #include <dcp/sound_asset_reader.h>
46 #include <dcp/subtitle_image.h>
47 #include <boost/foreach.hpp>
54 using boost::shared_ptr;
55 using boost::dynamic_pointer_cast;
56 using boost::optional;
58 DCPDecoder::DCPDecoder (shared_ptr<const Film> film, shared_ptr<const DCPContent> c, bool fast)
61 , _decode_referenced (false)
64 video.reset (new VideoDecoder (this, c));
67 audio.reset (new AudioDecoder (this, c->audio, fast));
69 BOOST_FOREACH (shared_ptr<TextContent> i, c->text) {
70 /* XXX: this time here should be the time of the first subtitle, not 0 */
71 text.push_back (shared_ptr<TextDecoder> (new TextDecoder (this, i, ContentTime())));
74 list<shared_ptr<dcp::CPL> > cpl_list = cpls ();
76 if (cpl_list.empty()) {
77 throw DCPError (_("No CPLs found in DCP."));
80 shared_ptr<dcp::CPL> cpl;
81 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpl_list) {
82 if (_dcp_content->cpl() && i->id() == _dcp_content->cpl().get()) {
88 /* No CPL found; probably an old file that doesn't specify it;
89 just use the first one.
91 cpl = cpls().front ();
94 set_decode_referenced (false);
96 _reels = cpl->reels ();
98 _reel = _reels.begin ();
107 if (_reel == _reels.end () || !_dcp_content->can_be_played ()) {
111 double const vfr = _dcp_content->active_video_frame_rate (film());
113 /* Frame within the (played part of the) reel that is coming up next */
114 int64_t const frame = _next.frames_round (vfr);
116 shared_ptr<dcp::PictureAsset> picture_asset = (*_reel)->main_picture()->asset();
117 DCPOMATIC_ASSERT (picture_asset);
119 /* We must emit texts first as when we emit the video for this frame
120 it will expect already to have the texts.
122 pass_texts (_next, picture_asset->size());
124 if ((_mono_reader || _stereo_reader) && (_decode_referenced || !_dcp_content->reference_video())) {
125 int64_t const entry_point = (*_reel)->main_picture()->entry_point ();
129 shared_ptr<ImageProxy> (
131 _mono_reader->get_frame (entry_point + frame),
132 picture_asset->size(),
142 shared_ptr<ImageProxy> (
144 _stereo_reader->get_frame (entry_point + frame),
145 picture_asset->size(),
156 shared_ptr<ImageProxy> (
158 _stereo_reader->get_frame (entry_point + frame),
159 picture_asset->size(),
170 if (_sound_reader && (_decode_referenced || !_dcp_content->reference_audio())) {
171 int64_t const entry_point = (*_reel)->main_sound()->entry_point ();
172 shared_ptr<const dcp::SoundFrame> sf = _sound_reader->get_frame (entry_point + frame);
173 uint8_t const * from = sf->data ();
175 int const channels = _dcp_content->audio->stream()->channels ();
176 int const frames = sf->size() / (3 * channels);
177 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, frames));
178 float** data_data = data->data();
179 for (int i = 0; i < frames; ++i) {
180 for (int j = 0; j < channels; ++j) {
181 data_data[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
186 audio->emit (film(), _dcp_content->audio->stream(), data, ContentTime::from_frames (_offset, vfr) + _next);
189 _next += ContentTime::from_frames (1, vfr);
191 if ((*_reel)->main_picture ()) {
192 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
194 _next = ContentTime ();
202 DCPDecoder::pass_texts (ContentTime next, dcp::Size size)
204 list<shared_ptr<TextDecoder> >::const_iterator decoder = text.begin ();
205 if ((*_reel)->main_subtitle()) {
206 DCPOMATIC_ASSERT (decoder != text.end ());
209 (*_reel)->main_subtitle()->asset(),
210 _dcp_content->reference_text(TEXT_OPEN_SUBTITLE),
211 (*_reel)->main_subtitle()->entry_point(),
217 BOOST_FOREACH (shared_ptr<dcp::ReelClosedCaptionAsset> i, (*_reel)->closed_captions()) {
218 DCPOMATIC_ASSERT (decoder != text.end ());
220 next, i->asset(), _dcp_content->reference_text(TEXT_CLOSED_CAPTION), i->entry_point(), *decoder, size
227 DCPDecoder::pass_texts (
228 ContentTime next, shared_ptr<dcp::SubtitleAsset> asset, bool reference, int64_t entry_point, shared_ptr<TextDecoder> decoder, dcp::Size size
231 double const vfr = _dcp_content->active_video_frame_rate (film());
232 /* Frame within the (played part of the) reel that is coming up next */
233 int64_t const frame = next.frames_round (vfr);
235 if (_decode_referenced || !reference) {
236 list<shared_ptr<dcp::Subtitle> > subs = asset->subtitles_during (
237 dcp::Time (entry_point + frame, vfr, vfr),
238 dcp::Time (entry_point + frame + 1, vfr, vfr),
242 list<dcp::SubtitleString> strings;
244 BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, subs) {
245 shared_ptr<dcp::SubtitleString> is = dynamic_pointer_cast<dcp::SubtitleString> (i);
247 if (!strings.empty() && (strings.back().in() != is->in() || strings.back().out() != is->out())) {
248 dcp::SubtitleString b = strings.back();
249 decoder->emit_plain (
251 ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
252 ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
259 strings.push_back (*is);
262 /* XXX: perhaps these image subs should also be collected together like the string ones are;
263 this would need to be done both here and in DCPSubtitleDecoder.
266 shared_ptr<dcp::SubtitleImage> ii = dynamic_pointer_cast<dcp::SubtitleImage> (i);
268 emit_subtitle_image (
270 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->in().as_seconds ()),
271 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->out().as_seconds ())
280 if (!strings.empty()) {
281 dcp::SubtitleString b = strings.back();
282 decoder->emit_plain (
284 ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
285 ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
295 DCPDecoder::next_reel ()
297 _offset += (*_reel)->main_picture()->duration();
303 DCPDecoder::get_readers ()
305 if (_reel == _reels.end() || !_dcp_content->can_be_played ()) {
306 _mono_reader.reset ();
307 _stereo_reader.reset ();
308 _sound_reader.reset ();
312 if ((*_reel)->main_picture()) {
313 shared_ptr<dcp::PictureAsset> asset = (*_reel)->main_picture()->asset ();
314 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
315 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
316 DCPOMATIC_ASSERT (mono || stereo);
318 _mono_reader = mono->start_read ();
319 _stereo_reader.reset ();
321 _stereo_reader = stereo->start_read ();
322 _mono_reader.reset ();
325 _mono_reader.reset ();
326 _stereo_reader.reset ();
329 if ((*_reel)->main_sound()) {
330 _sound_reader = (*_reel)->main_sound()->asset()->start_read ();
332 _sound_reader.reset ();
337 DCPDecoder::seek (ContentTime t, bool accurate)
339 if (!_dcp_content->can_be_played ()) {
343 Decoder::seek (t, accurate);
345 _reel = _reels.begin ();
349 int const pre_roll_seconds = 2;
351 /* Pre-roll for subs */
353 ContentTime pre = t - ContentTime::from_seconds (pre_roll_seconds);
354 if (pre < ContentTime()) {
355 pre = ContentTime ();
358 /* Seek to pre-roll position */
360 while (_reel != _reels.end() && pre >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate(film()))) {
361 ContentTime rd = ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate(film()));
367 /* Pass texts in the pre-roll */
369 double const vfr = _dcp_content->active_video_frame_rate (film());
370 for (int i = 0; i < pre_roll_seconds * vfr; ++i) {
371 pass_texts (pre, (*_reel)->main_picture()->asset()->size());
372 pre += ContentTime::from_frames (1, vfr);
375 /* Seek to correct position */
377 while (_reel != _reels.end() && t >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate(film()))) {
378 t -= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate(film()));
386 DCPDecoder::set_decode_referenced (bool r)
388 _decode_referenced = r;
391 video->set_ignore (_dcp_content->reference_video() && !_decode_referenced);
394 audio->set_ignore (_dcp_content->reference_audio() && !_decode_referenced);
399 DCPDecoder::set_forced_reduction (optional<int> reduction)
401 _forced_reduction = reduction;