Remove DCP class and replace its functionality with a plain method in libdcp.
[dcpomatic.git] / src / lib / dcp_decoder.cc
1 /*
2     Copyright (C) 2014-2022 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "atmos_decoder.h"
23 #include "audio_content.h"
24 #include "audio_decoder.h"
25 #include "config.h"
26 #include "dcp_content.h"
27 #include "dcp_decoder.h"
28 #include "digester.h"
29 #include "ffmpeg_image_proxy.h"
30 #include "frame_interval_checker.h"
31 #include "image.h"
32 #include "j2k_image_proxy.h"
33 #include "text_decoder.h"
34 #include "video_decoder.h"
35 #include <dcp/cpl.h>
36 #include <dcp/dcp.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>
41 #include <dcp/reel.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>
54 #include <iostream>
55
56 #include "i18n.h"
57
58
59 using std::cout;
60 using std::dynamic_pointer_cast;
61 using std::list;
62 using std::make_shared;
63 using std::map;
64 using std::shared_ptr;
65 using std::string;
66 using std::vector;
67 using boost::optional;
68 using namespace dcpomatic;
69
70
71 DCPDecoder::DCPDecoder (shared_ptr<const Film> film, shared_ptr<const DCPContent> content, bool fast, bool tolerant, shared_ptr<DCPDecoder> old)
72         : Decoder (film)
73         , _dcp_content (content)
74 {
75         if (content->can_be_played()) {
76                 if (content->video) {
77                         video = make_shared<VideoDecoder>(this, content);
78                 }
79                 if (content->audio) {
80                         audio = make_shared<AudioDecoder>(this, content->audio, fast);
81                 }
82                 for (auto i: content->text) {
83                         /* XXX: this time here should be the time of the first subtitle, not 0 */
84                         text.push_back (make_shared<TextDecoder>(this, i, ContentTime()));
85                 }
86                 if (content->atmos) {
87                         atmos = make_shared<AtmosDecoder>(this, content);
88                 }
89         }
90
91         /* We try to avoid re-scanning the DCP's files every time we make a new DCPDecoder; we do this
92            by re-using the _reels list.  Before we do this we need to check that nothing too serious
93            has changed in the DCPContent.
94
95            We do this by storing a digest of the important bits of the DCPContent and then checking that's
96            the same before we re-use _reels.
97         */
98
99         _lazy_digest = calculate_lazy_digest (content);
100
101         if (old && old->lazy_digest() == _lazy_digest) {
102                 _reels = old->_reels;
103         } else {
104
105                 auto cpl_list = dcp::find_and_resolve_cpls(content->directories(), tolerant);
106
107                 if (cpl_list.empty()) {
108                         throw DCPError (_("No CPLs found in DCP."));
109                 }
110
111                 shared_ptr<dcp::CPL> cpl;
112                 for (auto i: cpl_list) {
113                         if (_dcp_content->cpl() && i->id() == _dcp_content->cpl().get()) {
114                                 cpl = i;
115                         }
116                 }
117
118                 if (!cpl) {
119                         /* No CPL found; probably an old file that doesn't specify it;
120                            just use the first one.
121                         */
122                         cpl = cpl_list.front();
123                 }
124
125                 if (content->kdm()) {
126                         cpl->add (decrypt_kdm_with_helpful_error(content->kdm().get()));
127                 }
128
129                 _reels = cpl->reels ();
130         }
131
132         set_decode_referenced (false);
133
134         _reel = _reels.begin ();
135         get_readers ();
136 }
137
138
139 bool
140 DCPDecoder::pass ()
141 {
142         if (!_dcp_content->can_be_played()) {
143                 return true;
144         }
145
146         if (_reel == _reels.end()) {
147                 if (audio) {
148                         audio->flush ();
149                 }
150                 return true;
151         }
152
153         auto const vfr = _dcp_content->active_video_frame_rate (film());
154
155         /* Frame within the (played part of the) reel that is coming up next */
156         auto const frame = _next.frames_round (vfr);
157
158         auto picture_asset = (*_reel)->main_picture()->asset();
159         DCPOMATIC_ASSERT (picture_asset);
160
161         /* We must emit texts first as when we emit the video for this frame
162            it will expect already to have the texts.
163         */
164         pass_texts (_next, picture_asset->size());
165
166         if ((_mono_reader || _stereo_reader) && (_decode_referenced || !_dcp_content->reference_video())) {
167                 auto const entry_point = (*_reel)->main_picture()->entry_point().get_value_or(0);
168                 if (_mono_reader) {
169                         video->emit (
170                                 film(),
171                                 std::make_shared<J2KImageProxy>(
172                                         _mono_reader->get_frame (entry_point + frame),
173                                         picture_asset->size(),
174                                         AV_PIX_FMT_XYZ12LE,
175                                         _forced_reduction
176                                         ),
177                                 _offset + frame
178                                 );
179                 } else {
180                         video->emit (
181                                 film(),
182                                 std::make_shared<J2KImageProxy>(
183                                         _stereo_reader->get_frame (entry_point + frame),
184                                         picture_asset->size(),
185                                         dcp::Eye::LEFT,
186                                         AV_PIX_FMT_XYZ12LE,
187                                         _forced_reduction
188                                         ),
189                                 _offset + frame
190                                 );
191
192                         video->emit (
193                                 film(),
194                                 std::make_shared<J2KImageProxy>(
195                                         _stereo_reader->get_frame (entry_point + frame),
196                                         picture_asset->size(),
197                                         dcp::Eye::RIGHT,
198                                         AV_PIX_FMT_XYZ12LE,
199                                         _forced_reduction
200                                         ),
201                                 _offset + frame
202                                 );
203                 }
204         }
205
206         if (_sound_reader && (_decode_referenced || !_dcp_content->reference_audio())) {
207                 auto const entry_point = (*_reel)->main_sound()->entry_point().get_value_or(0);
208                 auto sf = _sound_reader->get_frame (entry_point + frame);
209                 auto from = sf->data ();
210
211                 int const channels = _dcp_content->audio->stream()->channels ();
212                 int const frames = sf->size() / (3 * channels);
213                 auto data = make_shared<AudioBuffers>(channels, frames);
214                 auto data_data = data->data();
215                 for (int i = 0; i < frames; ++i) {
216                         for (int j = 0; j < channels; ++j) {
217                                 data_data[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
218                                 from += 3;
219                         }
220                 }
221
222                 audio->emit (film(), _dcp_content->audio->stream(), data, ContentTime::from_frames (_offset, vfr) + _next);
223         }
224
225         if (_atmos_reader) {
226                 DCPOMATIC_ASSERT (_atmos_metadata);
227                 auto const entry_point = (*_reel)->atmos()->entry_point().get_value_or(0);
228                 atmos->emit (film(), _atmos_reader->get_frame(entry_point + frame), _offset + frame, *_atmos_metadata);
229         }
230
231         _next += ContentTime::from_frames (1, vfr);
232
233         if ((*_reel)->main_picture ()) {
234                 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
235                         next_reel ();
236                         _next = ContentTime ();
237                 }
238         }
239
240         return false;
241 }
242
243
244 void
245 DCPDecoder::pass_texts (ContentTime next, dcp::Size size)
246 {
247         auto decoder = text.begin ();
248         if (decoder == text.end()) {
249                 /* It's possible that there is now a main subtitle but no TextDecoders, for example if
250                    the CPL has just changed but the TextContent's texts have not been recreated yet.
251                 */
252                 return;
253         }
254
255         if ((*_reel)->main_subtitle()) {
256                 pass_texts (
257                         next,
258                         (*_reel)->main_subtitle()->asset(),
259                         _dcp_content->reference_text(TextType::OPEN_SUBTITLE),
260                         (*_reel)->main_subtitle()->entry_point().get_value_or(0),
261                         *decoder,
262                         size
263                         );
264                 ++decoder;
265         }
266
267         for (auto i: (*_reel)->closed_captions()) {
268                 pass_texts (
269                         next, i->asset(), _dcp_content->reference_text(TextType::CLOSED_CAPTION), i->entry_point().get_value_or(0), *decoder, size
270                         );
271                 ++decoder;
272         }
273 }
274
275 void
276 DCPDecoder::pass_texts (
277         ContentTime next, shared_ptr<dcp::SubtitleAsset> asset, bool reference, int64_t entry_point, shared_ptr<TextDecoder> decoder, dcp::Size size
278         )
279 {
280         auto const vfr = _dcp_content->active_video_frame_rate (film());
281         /* Frame within the (played part of the) reel that is coming up next */
282         auto const frame = next.frames_round (vfr);
283
284         if (_decode_referenced || !reference) {
285                 auto subs = asset->subtitles_during (
286                         dcp::Time (entry_point + frame, vfr, vfr),
287                         dcp::Time (entry_point + frame + 1, vfr, vfr),
288                         true
289                         );
290
291                 list<dcp::SubtitleString> strings;
292
293                 for (auto i: subs) {
294                         auto is = dynamic_pointer_cast<const dcp::SubtitleString>(i);
295                         if (is) {
296                                 if (!strings.empty() && (strings.back().in() != is->in() || strings.back().out() != is->out())) {
297                                         auto b = strings.back();
298                                         decoder->emit_plain (
299                                                 ContentTimePeriod (
300                                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
301                                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
302                                                         ),
303                                                 strings
304                                                 );
305                                         strings.clear ();
306                                 }
307
308                                 strings.push_back (*is);
309                         }
310
311                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
312                            this would need to be done both here and in DCPSubtitleDecoder.
313                         */
314
315                         auto ii = dynamic_pointer_cast<const dcp::SubtitleImage>(i);
316                         if (ii) {
317                                 emit_subtitle_image (
318                                         ContentTimePeriod (
319                                                 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->in().as_seconds ()),
320                                                 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->out().as_seconds ())
321                                                 ),
322                                         *ii,
323                                         size,
324                                         decoder
325                                         );
326                         }
327                 }
328
329                 if (!strings.empty()) {
330                         auto b = strings.back();
331                         decoder->emit_plain (
332                                 ContentTimePeriod (
333                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
334                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
335                                         ),
336                                 strings
337                                 );
338                         strings.clear ();
339                 }
340         }
341 }
342
343
344 void
345 DCPDecoder::next_reel ()
346 {
347         _offset += (*_reel)->main_picture()->actual_duration();
348         ++_reel;
349         get_readers ();
350 }
351
352
353 void
354 DCPDecoder::get_readers ()
355 {
356         if (_reel == _reels.end() || !_dcp_content->can_be_played ()) {
357                 _mono_reader.reset ();
358                 _stereo_reader.reset ();
359                 _sound_reader.reset ();
360                 _atmos_reader.reset ();
361                 return;
362         }
363
364         if ((*_reel)->main_picture()) {
365                 auto asset = (*_reel)->main_picture()->asset ();
366                 auto mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
367                 auto stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
368                 DCPOMATIC_ASSERT (mono || stereo);
369                 if (mono) {
370                         _mono_reader = mono->start_read ();
371                         _mono_reader->set_check_hmac (false);
372                         _stereo_reader.reset ();
373                 } else {
374                         _stereo_reader = stereo->start_read ();
375                         _stereo_reader->set_check_hmac (false);
376                         _mono_reader.reset ();
377                 }
378         } else {
379                 _mono_reader.reset ();
380                 _stereo_reader.reset ();
381         }
382
383         if ((*_reel)->main_sound()) {
384                 _sound_reader = (*_reel)->main_sound()->asset()->start_read ();
385                 _sound_reader->set_check_hmac (false);
386         } else {
387                 _sound_reader.reset ();
388         }
389
390         if ((*_reel)->atmos()) {
391                 auto asset = (*_reel)->atmos()->asset();
392                 _atmos_reader = asset->start_read();
393                 _atmos_reader->set_check_hmac (false);
394                 _atmos_metadata = AtmosMetadata (asset);
395         } else {
396                 _atmos_reader.reset ();
397                 _atmos_metadata = boost::none;
398         }
399 }
400
401
402 void
403 DCPDecoder::seek (ContentTime t, bool accurate)
404 {
405         if (!_dcp_content->can_be_played ()) {
406                 return;
407         }
408
409         Decoder::seek (t, accurate);
410
411         _reel = _reels.begin ();
412         _offset = 0;
413         get_readers ();
414
415         int const pre_roll_seconds = 2;
416
417         /* Pre-roll for subs */
418
419         auto pre = t - ContentTime::from_seconds (pre_roll_seconds);
420         if (pre < ContentTime()) {
421                 pre = ContentTime ();
422         }
423
424         /* Seek to pre-roll position */
425
426         while (
427                 _reel != _reels.end() &&
428                 pre >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
429                 ) {
430
431                 auto rd = ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
432                 pre -= rd;
433                 t -= rd;
434                 next_reel ();
435         }
436
437         /* Pass texts in the pre-roll */
438
439         auto const vfr = _dcp_content->active_video_frame_rate (film());
440         for (int i = 0; i < pre_roll_seconds * vfr; ++i) {
441                 pass_texts (pre, (*_reel)->main_picture()->asset()->size());
442                 pre += ContentTime::from_frames (1, vfr);
443         }
444
445         /* Seek to correct position */
446
447         while (
448                 _reel != _reels.end() &&
449                 t >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
450                 ) {
451
452                 t -= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
453                 next_reel ();
454         }
455
456         _next = t;
457 }
458
459
460 void
461 DCPDecoder::set_decode_referenced (bool r)
462 {
463         _decode_referenced = r;
464
465         if (video) {
466                 video->set_ignore (_dcp_content->reference_video() && !_decode_referenced);
467         }
468         if (audio) {
469                 audio->set_ignore (_dcp_content->reference_audio() && !_decode_referenced);
470         }
471 }
472
473
474 void
475 DCPDecoder::set_forced_reduction (optional<int> reduction)
476 {
477         _forced_reduction = reduction;
478 }
479
480
481 string
482 DCPDecoder::calculate_lazy_digest (shared_ptr<const DCPContent> c) const
483 {
484         Digester d;
485         for (auto i: c->paths()) {
486                 d.add (i.string());
487         }
488         if (_dcp_content->kdm()) {
489                 d.add(_dcp_content->kdm()->id());
490         }
491         d.add (static_cast<bool>(c->cpl()));
492         if (c->cpl()) {
493                 d.add (c->cpl().get());
494         }
495         return d.get ();
496 }
497
498
499 ContentTime
500 DCPDecoder::position () const
501 {
502         return ContentTime::from_frames(_offset, _dcp_content->active_video_frame_rate(film())) + _next;
503 }
504
505
506 vector<FontData>
507 DCPDecoder::fonts () const
508 {
509         vector<FontData> data;
510         for (auto i: _reels) {
511                 if (i->main_subtitle() && i->main_subtitle()->asset()) {
512                         for (auto const& j: i->main_subtitle()->asset()->font_data()) {
513                                 data.push_back (FontData(j.first, j.second));
514                         }
515                 }
516         }
517         return data;
518 }
519