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