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