5a72fb2281b30fda0d06d1cefc6b33b779b9792c
[dcpomatic.git] / src / lib / dcp_decoder.cc
1 /*
2     Copyright (C) 2014-2018 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 #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"
29 #include "image.h"
30 #include "config.h"
31 #include <dcp/dcp.h>
32 #include <dcp/cpl.h>
33 #include <dcp/reel.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>
48 #include <iostream>
49
50 #include "i18n.h"
51
52 using std::list;
53 using std::cout;
54 using boost::shared_ptr;
55 using boost::dynamic_pointer_cast;
56 using boost::optional;
57 using namespace dcpomatic;
58
59 DCPDecoder::DCPDecoder (shared_ptr<const Film> film, shared_ptr<const DCPContent> c, bool fast)
60         : DCP (c)
61         , Decoder (film)
62         , _decode_referenced (false)
63 {
64         if (c->can_be_played()) {
65                 if (c->video) {
66                         video.reset (new VideoDecoder (this, c));
67                 }
68                 if (c->audio) {
69                         audio.reset (new AudioDecoder (this, c->audio, fast));
70                 }
71                 BOOST_FOREACH (shared_ptr<TextContent> i, c->text) {
72                         /* XXX: this time here should be the time of the first subtitle, not 0 */
73                         text.push_back (shared_ptr<TextDecoder> (new TextDecoder (this, i, ContentTime())));
74                 }
75         }
76
77         list<shared_ptr<dcp::CPL> > cpl_list = cpls ();
78
79         if (cpl_list.empty()) {
80                 throw DCPError (_("No CPLs found in DCP."));
81         }
82
83         shared_ptr<dcp::CPL> cpl;
84         BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpl_list) {
85                 if (_dcp_content->cpl() && i->id() == _dcp_content->cpl().get()) {
86                         cpl = i;
87                 }
88         }
89
90         if (!cpl) {
91                 /* No CPL found; probably an old file that doesn't specify it;
92                    just use the first one.
93                 */
94                 cpl = cpls().front ();
95         }
96
97         set_decode_referenced (false);
98
99         _reels = cpl->reels ();
100
101         _reel = _reels.begin ();
102         _offset = 0;
103         get_readers ();
104 }
105
106
107 bool
108 DCPDecoder::pass ()
109 {
110         if (_reel == _reels.end () || !_dcp_content->can_be_played ()) {
111                 return true;
112         }
113
114         double const vfr = _dcp_content->active_video_frame_rate (film());
115
116         /* Frame within the (played part of the) reel that is coming up next */
117         int64_t const frame = _next.frames_round (vfr);
118
119         shared_ptr<dcp::PictureAsset> picture_asset = (*_reel)->main_picture()->asset();
120         DCPOMATIC_ASSERT (picture_asset);
121
122         /* We must emit texts first as when we emit the video for this frame
123            it will expect already to have the texts.
124         */
125         pass_texts (_next, picture_asset->size());
126
127         if ((_mono_reader || _stereo_reader) && (_decode_referenced || !_dcp_content->reference_video())) {
128                 int64_t const entry_point = (*_reel)->main_picture()->entry_point().get_value_or(0);
129                 if (_mono_reader) {
130                         video->emit (
131                                 film(),
132                                 shared_ptr<ImageProxy> (
133                                         new J2KImageProxy (
134                                                 _mono_reader->get_frame (entry_point + frame),
135                                                 picture_asset->size(),
136                                                 AV_PIX_FMT_XYZ12LE,
137                                                 _forced_reduction
138                                                 )
139                                         ),
140                                 _offset + frame
141                                 );
142                 } else {
143                         video->emit (
144                                 film(),
145                                 shared_ptr<ImageProxy> (
146                                         new J2KImageProxy (
147                                                 _stereo_reader->get_frame (entry_point + frame),
148                                                 picture_asset->size(),
149                                                 dcp::EYE_LEFT,
150                                                 AV_PIX_FMT_XYZ12LE,
151                                                 _forced_reduction
152                                                 )
153                                         ),
154                                 _offset + frame
155                                 );
156
157                         video->emit (
158                                 film(),
159                                 shared_ptr<ImageProxy> (
160                                         new J2KImageProxy (
161                                                 _stereo_reader->get_frame (entry_point + frame),
162                                                 picture_asset->size(),
163                                                 dcp::EYE_RIGHT,
164                                                 AV_PIX_FMT_XYZ12LE,
165                                                 _forced_reduction
166                                                 )
167                                         ),
168                                 _offset + frame
169                                 );
170                 }
171         }
172
173         if (_sound_reader && (_decode_referenced || !_dcp_content->reference_audio())) {
174                 int64_t const entry_point = (*_reel)->main_sound()->entry_point().get_value_or(0);
175                 shared_ptr<const dcp::SoundFrame> sf = _sound_reader->get_frame (entry_point + frame);
176                 uint8_t const * from = sf->data ();
177
178                 int const channels = _dcp_content->audio->stream()->channels ();
179                 int const frames = sf->size() / (3 * channels);
180                 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, frames));
181                 float** data_data = data->data();
182                 for (int i = 0; i < frames; ++i) {
183                         for (int j = 0; j < channels; ++j) {
184                                 data_data[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
185                                 from += 3;
186                         }
187                 }
188
189                 audio->emit (film(), _dcp_content->audio->stream(), data, ContentTime::from_frames (_offset, vfr) + _next);
190         }
191
192         _next += ContentTime::from_frames (1, vfr);
193
194         if ((*_reel)->main_picture ()) {
195                 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
196                         next_reel ();
197                         _next = ContentTime ();
198                 }
199         }
200
201         return false;
202 }
203
204 void
205 DCPDecoder::pass_texts (ContentTime next, dcp::Size size)
206 {
207         list<shared_ptr<TextDecoder> >::const_iterator decoder = text.begin ();
208         if ((*_reel)->main_subtitle()) {
209                 DCPOMATIC_ASSERT (decoder != text.end ());
210                 pass_texts (
211                         next,
212                         (*_reel)->main_subtitle()->asset(),
213                         _dcp_content->reference_text(TEXT_OPEN_SUBTITLE),
214                         (*_reel)->main_subtitle()->entry_point().get_value_or(0),
215                         *decoder,
216                         size
217                         );
218                 ++decoder;
219         }
220         BOOST_FOREACH (shared_ptr<dcp::ReelClosedCaptionAsset> i, (*_reel)->closed_captions()) {
221                 DCPOMATIC_ASSERT (decoder != text.end ());
222                 pass_texts (
223                         next, i->asset(), _dcp_content->reference_text(TEXT_CLOSED_CAPTION), i->entry_point().get_value_or(0), *decoder, size
224                         );
225                 ++decoder;
226         }
227 }
228
229 void
230 DCPDecoder::pass_texts (
231         ContentTime next, shared_ptr<dcp::SubtitleAsset> asset, bool reference, int64_t entry_point, shared_ptr<TextDecoder> decoder, dcp::Size size
232         )
233 {
234         double const vfr = _dcp_content->active_video_frame_rate (film());
235         /* Frame within the (played part of the) reel that is coming up next */
236         int64_t const frame = next.frames_round (vfr);
237
238         if (_decode_referenced || !reference) {
239                 list<shared_ptr<dcp::Subtitle> > subs = asset->subtitles_during (
240                         dcp::Time (entry_point + frame, vfr, vfr),
241                         dcp::Time (entry_point + frame + 1, vfr, vfr),
242                         true
243                         );
244
245                 list<dcp::SubtitleString> strings;
246
247                 BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, subs) {
248                         shared_ptr<dcp::SubtitleString> is = dynamic_pointer_cast<dcp::SubtitleString> (i);
249                         if (is) {
250                                 if (!strings.empty() && (strings.back().in() != is->in() || strings.back().out() != is->out())) {
251                                         dcp::SubtitleString b = strings.back();
252                                         decoder->emit_plain (
253                                                 ContentTimePeriod (
254                                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
255                                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
256                                                         ),
257                                                 strings
258                                                 );
259                                         strings.clear ();
260                                 }
261
262                                 strings.push_back (*is);
263                         }
264
265                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
266                            this would need to be done both here and in DCPSubtitleDecoder.
267                         */
268
269                         shared_ptr<dcp::SubtitleImage> ii = dynamic_pointer_cast<dcp::SubtitleImage> (i);
270                         if (ii) {
271                                 emit_subtitle_image (
272                                         ContentTimePeriod (
273                                                 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->in().as_seconds ()),
274                                                 ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (i->out().as_seconds ())
275                                                 ),
276                                         *ii,
277                                         size,
278                                         decoder
279                                         );
280                         }
281                 }
282
283                 if (!strings.empty()) {
284                         dcp::SubtitleString b = strings.back();
285                         decoder->emit_plain (
286                                 ContentTimePeriod (
287                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.in().as_seconds()),
288                                         ContentTime::from_frames(_offset - entry_point, vfr) + ContentTime::from_seconds(b.out().as_seconds())
289                                         ),
290                                 strings
291                                 );
292                         strings.clear ();
293                 }
294         }
295 }
296
297 void
298 DCPDecoder::next_reel ()
299 {
300         _offset += (*_reel)->main_picture()->actual_duration();
301         ++_reel;
302         get_readers ();
303 }
304
305 void
306 DCPDecoder::get_readers ()
307 {
308         if (_reel == _reels.end() || !_dcp_content->can_be_played ()) {
309                 _mono_reader.reset ();
310                 _stereo_reader.reset ();
311                 _sound_reader.reset ();
312                 return;
313         }
314
315         if ((*_reel)->main_picture()) {
316                 shared_ptr<dcp::PictureAsset> asset = (*_reel)->main_picture()->asset ();
317                 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
318                 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
319                 DCPOMATIC_ASSERT (mono || stereo);
320                 if (mono) {
321                         _mono_reader = mono->start_read ();
322                         _stereo_reader.reset ();
323                 } else {
324                         _stereo_reader = stereo->start_read ();
325                         _mono_reader.reset ();
326                 }
327         } else {
328                 _mono_reader.reset ();
329                 _stereo_reader.reset ();
330         }
331
332         if ((*_reel)->main_sound()) {
333                 _sound_reader = (*_reel)->main_sound()->asset()->start_read ();
334         } else {
335                 _sound_reader.reset ();
336         }
337 }
338
339 void
340 DCPDecoder::seek (ContentTime t, bool accurate)
341 {
342         if (!_dcp_content->can_be_played ()) {
343                 return;
344         }
345
346         Decoder::seek (t, accurate);
347
348         _reel = _reels.begin ();
349         _offset = 0;
350         get_readers ();
351
352         int const pre_roll_seconds = 2;
353
354         /* Pre-roll for subs */
355
356         ContentTime pre = t - ContentTime::from_seconds (pre_roll_seconds);
357         if (pre < ContentTime()) {
358                 pre = ContentTime ();
359         }
360
361         /* Seek to pre-roll position */
362
363         while (
364                 _reel != _reels.end() &&
365                 pre >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
366                 ) {
367
368                 ContentTime rd = ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
369                 pre -= rd;
370                 t -= rd;
371                 next_reel ();
372         }
373
374         /* Pass texts in the pre-roll */
375
376         double const vfr = _dcp_content->active_video_frame_rate (film());
377         for (int i = 0; i < pre_roll_seconds * vfr; ++i) {
378                 pass_texts (pre, (*_reel)->main_picture()->asset()->size());
379                 pre += ContentTime::from_frames (1, vfr);
380         }
381
382         /* Seek to correct position */
383
384         while (
385                 _reel != _reels.end() &&
386                 t >= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()))
387                 ) {
388
389                 t -= ContentTime::from_frames ((*_reel)->main_picture()->actual_duration(), _dcp_content->active_video_frame_rate(film()));
390                 next_reel ();
391         }
392
393         _next = t;
394 }
395
396 void
397 DCPDecoder::set_decode_referenced (bool r)
398 {
399         _decode_referenced = r;
400
401         if (video) {
402                 video->set_ignore (_dcp_content->reference_video() && !_decode_referenced);
403         }
404         if (audio) {
405                 audio->set_ignore (_dcp_content->reference_audio() && !_decode_referenced);
406         }
407 }
408
409 void
410 DCPDecoder::set_forced_reduction (optional<int> reduction)
411 {
412         _forced_reduction = reduction;
413 }