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