2 Copyright (C) 2014-2022 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/>.
22 #include "atmos_decoder.h"
23 #include "audio_content.h"
24 #include "audio_decoder.h"
26 #include "dcp_content.h"
27 #include "dcp_decoder.h"
29 #include "ffmpeg_image_proxy.h"
30 #include "frame_interval_checker.h"
32 #include "j2k_image_proxy.h"
33 #include "text_decoder.h"
34 #include "video_decoder.h"
37 #include <dcp/decrypted_kdm.h>
38 #include <dcp/mono_picture_asset.h>
39 #include <dcp/mono_picture_asset_reader.h>
40 #include <dcp/mono_picture_frame.h>
42 #include <dcp/reel_atmos_asset.h>
43 #include <dcp/reel_closed_caption_asset.h>
44 #include <dcp/reel_picture_asset.h>
45 #include <dcp/reel_sound_asset.h>
46 #include <dcp/reel_subtitle_asset.h>
47 #include <dcp/search.h>
48 #include <dcp/sound_asset_reader.h>
49 #include <dcp/sound_frame.h>
50 #include <dcp/stereo_picture_asset.h>
51 #include <dcp/stereo_picture_asset_reader.h>
52 #include <dcp/stereo_picture_frame.h>
53 #include <dcp/subtitle_image.h>
59 using std::dynamic_pointer_cast;
61 using std::make_shared;
63 using std::shared_ptr;
66 using boost::optional;
67 using namespace dcpomatic;
70 DCPDecoder::DCPDecoder (shared_ptr<const Film> film, shared_ptr<const DCPContent> content, bool fast, bool tolerant, shared_ptr<DCPDecoder> old)
72 , _dcp_content (content)
74 if (content->can_be_played()) {
76 video = make_shared<VideoDecoder>(this, content);
79 audio = make_shared<AudioDecoder>(this, content->audio, fast);
81 for (auto i: content->text) {
82 /* XXX: this time here should be the time of the first subtitle, not 0 */
83 text.push_back (make_shared<TextDecoder>(this, i, ContentTime()));
86 atmos = make_shared<AtmosDecoder>(this, content);
90 /* We try to avoid re-scanning the DCP's files every time we make a new DCPDecoder; we do this
91 by re-using the _reels list. Before we do this we need to check that nothing too serious
92 has changed in the DCPContent.
94 We do this by storing a digest of the important bits of the DCPContent and then checking that's
95 the same before we re-use _reels.
98 _lazy_digest = calculate_lazy_digest (content);
100 if (old && old->lazy_digest() == _lazy_digest) {
101 _reels = old->_reels;
104 auto cpl_list = dcp::find_and_resolve_cpls (content->directories(), tolerant);
106 if (cpl_list.empty()) {
107 throw DCPError (_("No CPLs found in DCP."));
110 shared_ptr<dcp::CPL> cpl;
111 for (auto i: cpl_list) {
112 if (_dcp_content->cpl() && i->id() == _dcp_content->cpl().get()) {
118 /* No CPL found; probably an old file that doesn't specify it;
119 just use the first one.
121 cpl = cpl_list.front();
124 if (content->kdm()) {
125 cpl->add (decrypt_kdm_with_helpful_error(content->kdm().get()));
127 _reels = cpl->reels ();
130 set_decode_referenced (false);
132 _reel = _reels.begin ();
140 if (!_dcp_content->can_be_played()) {
144 if (_reel == _reels.end()) {
151 auto const vfr = _dcp_content->active_video_frame_rate (film());
153 /* Frame within the (played part of the) reel that is coming up next */
154 auto const frame = _next.frames_round (vfr);
156 auto picture_asset = (*_reel)->main_picture()->asset();
157 DCPOMATIC_ASSERT (picture_asset);
159 /* We must emit texts first as when we emit the video for this frame
160 it will expect already to have the texts.
162 pass_texts (_next, picture_asset->size());
164 if ((_mono_reader || _stereo_reader) && (_decode_referenced || !_dcp_content->reference_video())) {
165 auto const entry_point = (*_reel)->main_picture()->entry_point().get_value_or(0);
169 std::make_shared<J2KImageProxy>(
170 _mono_reader->get_frame (entry_point + frame),
171 picture_asset->size(),
180 std::make_shared<J2KImageProxy>(
181 _stereo_reader->get_frame (entry_point + frame),
182 picture_asset->size(),
192 std::make_shared<J2KImageProxy>(
193 _stereo_reader->get_frame (entry_point + frame),
194 picture_asset->size(),
204 if (_sound_reader && (_decode_referenced || !_dcp_content->reference_audio())) {
205 auto const entry_point = (*_reel)->main_sound()->entry_point().get_value_or(0);
206 auto sf = _sound_reader->get_frame (entry_point + frame);
207 auto from = sf->data ();
209 int const channels = _dcp_content->audio->stream()->channels ();
210 int const frames = sf->size() / (3 * channels);
211 auto data = make_shared<AudioBuffers>(channels, frames);
212 auto data_data = data->data();
213 for (int i = 0; i < frames; ++i) {
214 for (int j = 0; j < channels; ++j) {
215 data_data[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
220 audio->emit (film(), _dcp_content->audio->stream(), data, ContentTime::from_frames (_offset, vfr) + _next);
224 DCPOMATIC_ASSERT (_atmos_metadata);
225 auto const entry_point = (*_reel)->atmos()->entry_point().get_value_or(0);
226 atmos->emit (film(), _atmos_reader->get_frame(entry_point + frame), _offset + frame, *_atmos_metadata);
229 _next += ContentTime::from_frames (1, vfr);
231 if ((*_reel)->main_picture ()) {
232 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
234 _next = ContentTime ();
243 DCPDecoder::pass_texts (ContentTime next, dcp::Size size)
245 auto decoder = text.begin ();
246 if (decoder == text.end()) {
247 /* It's possible that there is now a main subtitle but no TextDecoders, for example if
248 the CPL has just changed but the TextContent's texts have not been recreated yet.
253 if ((*_reel)->main_subtitle()) {
256 (*_reel)->main_subtitle()->asset(),
257 _dcp_content->reference_text(TextType::OPEN_SUBTITLE),
258 (*_reel)->main_subtitle()->entry_point().get_value_or(0),
265 for (auto i: (*_reel)->closed_captions()) {
267 next, i->asset(), _dcp_content->reference_text(TextType::CLOSED_CAPTION), i->entry_point().get_value_or(0), *decoder, size
275 DCPDecoder::pass_texts (
276 ContentTime next, shared_ptr<dcp::SubtitleAsset> asset, bool reference, int64_t entry_point, shared_ptr<TextDecoder> decoder, dcp::Size size
279 auto const vfr = _dcp_content->active_video_frame_rate (film());
280 /* Frame within the (played part of the) reel that is coming up next */
281 auto const frame = next.frames_round (vfr);
283 if (_decode_referenced || !reference) {
284 auto subs = asset->subtitles_during (
285 dcp::Time (entry_point + frame, vfr, vfr),
286 dcp::Time (entry_point + frame + 1, vfr, vfr),
290 list<dcp::SubtitleString> strings;
293 auto is = dynamic_pointer_cast<const dcp::SubtitleString>(i);
295 if (!strings.empty() && (strings.back().in() != is->in() || strings.back().out() != is->out())) {
296 auto b = strings.back();
297 decoder->emit_plain (
299 ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
300 ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
307 strings.push_back (*is);
310 /* XXX: perhaps these image subs should also be collected together like the string ones are;
311 this would need to be done both here and in DCPSubtitleDecoder.
314 auto ii = dynamic_pointer_cast<const dcp::SubtitleImage>(i);
316 emit_subtitle_image (
318 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->in().as_seconds ()),
319 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->out().as_seconds ())
328 if (!strings.empty()) {
329 auto b = strings.back();
330 decoder->emit_plain (
332 ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
333 ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
344 DCPDecoder::next_reel ()
346 _offset += (*_reel)->main_picture()->actual_duration();
353 DCPDecoder::get_readers ()
355 if (_reel == _reels.end() || !_dcp_content->can_be_played ()) {
356 _mono_reader.reset ();
357 _stereo_reader.reset ();
358 _sound_reader.reset ();
359 _atmos_reader.reset ();
363 if ((*_reel)->main_picture()) {
364 auto asset = (*_reel)->main_picture()->asset();
365 auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset>(asset);
366 auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset>(asset);
367 DCPOMATIC_ASSERT (mono || stereo);
369 _mono_reader = mono->start_read ();
370 _mono_reader->set_check_hmac (false);
371 _stereo_reader.reset ();
373 _stereo_reader = stereo->start_read ();
374 _stereo_reader->set_check_hmac (false);
375 _mono_reader.reset ();
378 _mono_reader.reset ();
379 _stereo_reader.reset ();
382 if ((*_reel)->main_sound()) {
383 _sound_reader = (*_reel)->main_sound()->asset()->start_read ();
384 _sound_reader->set_check_hmac (false);
386 _sound_reader.reset ();
389 if ((*_reel)->atmos()) {
390 auto asset = (*_reel)->atmos()->asset();
391 _atmos_reader = asset->start_read();
392 _atmos_reader->set_check_hmac (false);
393 _atmos_metadata = AtmosMetadata (asset);
395 _atmos_reader.reset ();
396 _atmos_metadata = boost::none;
402 DCPDecoder::seek (ContentTime t, bool accurate)
404 if (!_dcp_content->can_be_played ()) {
408 Decoder::seek (t, accurate);
410 _reel = _reels.begin ();
414 int const pre_roll_seconds = 2;
416 /* Pre-roll for subs */
418 auto pre = t - ContentTime::from_seconds (pre_roll_seconds);
419 if (pre < ContentTime()) {
420 pre = ContentTime ();
423 /* Seek to pre-roll position */
426 _reel != _reels.end() &&
427 pre >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
430 auto rd = ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
436 /* Pass texts in the pre-roll */
438 auto const vfr = _dcp_content->active_video_frame_rate (film());
439 for (int i = 0; i < pre_roll_seconds * vfr; ++i) {
440 pass_texts (pre, (*_reel)->main_picture()->asset()->size());
441 pre += ContentTime::from_frames (1, vfr);
444 /* Seek to correct position */
447 _reel != _reels.end() &&
448 t >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
451 t -= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
460 DCPDecoder::set_decode_referenced (bool r)
462 _decode_referenced = r;
465 video->set_ignore (_dcp_content->reference_video() && !_decode_referenced);
468 audio->set_ignore (_dcp_content->reference_audio() && !_decode_referenced);
474 DCPDecoder::set_forced_reduction (optional<int> reduction)
476 _forced_reduction = reduction;
481 DCPDecoder::calculate_lazy_digest (shared_ptr<const DCPContent> c) const
484 for (auto i: c->paths()) {
487 if (_dcp_content->kdm()) {
488 d.add(_dcp_content->kdm()->id());
490 d.add (static_cast<bool>(c->cpl()));
492 d.add (c->cpl().get());
499 DCPDecoder::position () const
501 return ContentTime::from_frames(_offset, _dcp_content->active_video_frame_rate(film())) + _next;
506 DCPDecoder::fonts () const
508 vector<FontData> data;
509 for (auto i: _reels) {
510 if (i->main_subtitle() && i->main_subtitle()->asset()) {
511 for (auto const& j: i->main_subtitle()->asset()->font_data()) {
512 data.push_back (FontData(j.first, j.second));