Basic grunt-work, untested and unfinished, but it compiles.
[dcpomatic.git] / src / lib / dcp_decoder.cc
1 /*
2     Copyright (C) 2014-2016 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 "subtitle_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/mono_picture_frame.h>
41 #include <dcp/stereo_picture_frame.h>
42 #include <dcp/sound_frame.h>
43 #include <dcp/sound_asset_reader.h>
44 #include <boost/foreach.hpp>
45 #include <iostream>
46
47 using std::list;
48 using std::cout;
49 using boost::shared_ptr;
50 using boost::dynamic_pointer_cast;
51
52 DCPDecoder::DCPDecoder (shared_ptr<const DCPContent> c, shared_ptr<Log> log)
53         : DCP (c)
54         , _decode_referenced (false)
55 {
56         video.reset (new VideoDecoder (this, c, log));
57         audio.reset (new AudioDecoder (this, c->audio, log));
58
59         subtitle.reset (new SubtitleDecoder (this, c->subtitle, log));
60
61         shared_ptr<dcp::CPL> cpl;
62         BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls ()) {
63                 if (_dcp_content->cpl() && i->id() == _dcp_content->cpl().get()) {
64                         cpl = i;
65                 }
66         }
67
68         if (!cpl) {
69                 /* No CPL found; probably an old file that doesn't specify it;
70                    just use the first one.
71                 */
72                 cpl = cpls().front ();
73         }
74
75         _reels = cpl->reels ();
76
77         _reel = _reels.begin ();
78         _offset = 0;
79         get_readers ();
80 }
81
82 void
83 DCPDecoder::pass ()
84 {
85         if (_reel == _reels.end () || !_dcp_content->can_be_played ()) {
86                 return;
87         }
88
89         double const vfr = _dcp_content->active_video_frame_rate ();
90
91         /* Frame within the (played part of the) reel that is coming up next */
92         int64_t const frame = _next.frames_round (vfr);
93
94         if ((_mono_reader || _stereo_reader) && (_decode_referenced || !_dcp_content->reference_video())) {
95                 shared_ptr<dcp::PictureAsset> asset = (*_reel)->main_picture()->asset ();
96                 int64_t const entry_point = (*_reel)->main_picture()->entry_point ();
97                 if (_mono_reader) {
98                         video->emit (
99                                 shared_ptr<ImageProxy> (
100                                         new J2KImageProxy (_mono_reader->get_frame (entry_point + frame), asset->size(), AV_PIX_FMT_XYZ12LE)
101                                         ),
102                                 _offset + frame
103                                 );
104                 } else {
105                         video->emit (
106                                 shared_ptr<ImageProxy> (
107                                         new J2KImageProxy (_stereo_reader->get_frame (entry_point + frame), asset->size(), dcp::EYE_LEFT, AV_PIX_FMT_XYZ12LE)),
108                                 _offset + frame
109                                 );
110
111                         video->emit (
112                                 shared_ptr<ImageProxy> (
113                                         new J2KImageProxy (_stereo_reader->get_frame (entry_point + frame), asset->size(), dcp::EYE_RIGHT, AV_PIX_FMT_XYZ12LE)),
114                                 _offset + frame
115                                 );
116                 }
117         }
118
119         if (_sound_reader && (_decode_referenced || !_dcp_content->reference_audio())) {
120                 int64_t const entry_point = (*_reel)->main_sound()->entry_point ();
121                 shared_ptr<const dcp::SoundFrame> sf = _sound_reader->get_frame (entry_point + frame);
122                 uint8_t const * from = sf->data ();
123
124                 int const channels = _dcp_content->audio->stream()->channels ();
125                 int const frames = sf->size() / (3 * channels);
126                 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, frames));
127                 float** data_data = data->data();
128                 for (int i = 0; i < frames; ++i) {
129                         for (int j = 0; j < channels; ++j) {
130                                 data_data[j][i] = static_cast<int> ((from[0] << 8) | (from[1] << 16) | (from[2] << 24)) / static_cast<float> (INT_MAX - 256);
131                                 from += 3;
132                         }
133                 }
134
135                 audio->emit (_dcp_content->audio->stream(), data, ContentTime::from_frames (_offset, vfr) + _next);
136         }
137
138         if ((*_reel)->main_subtitle() && (_decode_referenced || !_dcp_content->reference_subtitle())) {
139                 int64_t const entry_point = (*_reel)->main_subtitle()->entry_point ();
140                 list<dcp::SubtitleString> subs = (*_reel)->main_subtitle()->asset()->subtitles_during (
141                         dcp::Time (entry_point + frame, vfr, vfr),
142                         dcp::Time (entry_point + frame + 1, vfr, vfr),
143                         true
144                         );
145
146                 if (!subs.empty ()) {
147                         /* XXX: assuming that all `subs' are at the same time; maybe this is ok */
148                         subtitle->emit_text (
149                                 ContentTimePeriod (
150                                         ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().in().as_seconds ()),
151                                         ContentTime::from_frames (_offset - entry_point, vfr) + ContentTime::from_seconds (subs.front().out().as_seconds ())
152                                         ),
153                                 subs
154                                 );
155                 }
156         }
157
158         _next += ContentTime::from_frames (1, vfr);
159
160         if ((*_reel)->main_picture ()) {
161                 if (_next.frames_round (vfr) >= (*_reel)->main_picture()->duration()) {
162                         next_reel ();
163                         _next = ContentTime ();
164                 }
165         }
166 }
167
168 void
169 DCPDecoder::next_reel ()
170 {
171         _offset += (*_reel)->main_picture()->duration();
172         ++_reel;
173         get_readers ();
174 }
175
176 void
177 DCPDecoder::get_readers ()
178 {
179         if (_reel == _reels.end() || !_dcp_content->can_be_played ()) {
180                 _mono_reader.reset ();
181                 _stereo_reader.reset ();
182                 _sound_reader.reset ();
183                 return;
184         }
185
186         if ((*_reel)->main_picture()) {
187                 shared_ptr<dcp::PictureAsset> asset = (*_reel)->main_picture()->asset ();
188                 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
189                 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
190                 DCPOMATIC_ASSERT (mono || stereo);
191                 if (mono) {
192                         _mono_reader = mono->start_read ();
193                         _stereo_reader.reset ();
194                 } else {
195                         _stereo_reader = stereo->start_read ();
196                         _mono_reader.reset ();
197                 }
198         } else {
199                 _mono_reader.reset ();
200                 _stereo_reader.reset ();
201         }
202
203         if ((*_reel)->main_sound()) {
204                 _sound_reader = (*_reel)->main_sound()->asset()->start_read ();
205         } else {
206                 _sound_reader.reset ();
207         }
208 }
209
210 void
211 DCPDecoder::seek (ContentTime t, bool)
212 {
213         _reel = _reels.begin ();
214         _offset = 0;
215         get_readers ();
216
217         while (_reel != _reels.end() && t >= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate ())) {
218                 t -= ContentTime::from_frames ((*_reel)->main_picture()->duration(), _dcp_content->active_video_frame_rate ());
219                 next_reel ();
220         }
221
222         _next = t;
223 }
224
225 void
226 DCPDecoder::set_decode_referenced ()
227 {
228         _decode_referenced = true;
229 }