a82ebc4cf4b04e8b4d48dec0c20875994078d06c
[dcpomatic.git] / src / lib / audio_decoder_stream.cc
1 /*
2     Copyright (C) 2012-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 "audio_decoder_stream.h"
22 #include "audio_buffers.h"
23 #include "audio_processor.h"
24 #include "audio_decoder.h"
25 #include "resampler.h"
26 #include "util.h"
27 #include "film.h"
28 #include "log.h"
29 #include "audio_content.h"
30 #include "compose.hpp"
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::list;
36 using std::pair;
37 using std::cout;
38 using std::min;
39 using std::max;
40 using boost::optional;
41 using boost::shared_ptr;
42
43 AudioDecoderStream::AudioDecoderStream (
44         shared_ptr<const AudioContent> content, AudioStreamPtr stream, Decoder* decoder, AudioDecoder* audio_decoder, shared_ptr<Log> log
45         )
46         : _content (content)
47         , _stream (stream)
48         , _decoder (decoder)
49         , _audio_decoder (audio_decoder)
50         , _log (log)
51           /* We effectively start having done a seek to zero; this allows silence-padding of the first
52              data that comes out of our decoder.
53           */
54         , _seek_reference (ContentTime ())
55 {
56         if (content->resampled_frame_rate() != _stream->frame_rate() && _stream->channels() > 0) {
57                 _resampler.reset (new Resampler (_stream->frame_rate(), content->resampled_frame_rate(), _stream->channels ()));
58         }
59
60         reset_decoded ();
61 }
62
63 void
64 AudioDecoderStream::reset_decoded ()
65 {
66         _decoded = ContentAudio (shared_ptr<AudioBuffers> (new AudioBuffers (_stream->channels(), 0)), 0);
67 }
68
69 ContentAudio
70 AudioDecoderStream::get (Frame frame, Frame length, bool accurate)
71 {
72         shared_ptr<ContentAudio> dec;
73
74         _log->log (
75                 String::compose (
76                         "ADS has request for %1 %2; has %3 %4",
77                         frame, length, _decoded.frame, _decoded.audio->frames()
78                         ), LogEntry::TYPE_DEBUG_DECODE
79                 );
80
81         Frame const from = frame;
82         Frame const to = from + length;
83         Frame const have_from = _decoded.frame;
84         Frame const have_to = _decoded.frame + _decoded.audio->frames();
85
86         optional<Frame> missing;
87         if (have_from > from || have_to < to) {
88                 /* We need something */
89                 if (have_from <= from && from < have_to) {
90                         missing = have_to;
91                 } else {
92                         missing = from;
93                 }
94         }
95
96         if (missing) {
97                 optional<ContentTime> pos = _audio_decoder->position ();
98                 _log->log (
99                         String::compose ("ADS suggests seek to %1 (now at %2)", *missing, pos ? to_string(pos.get()) : "none"),
100                         LogEntry::TYPE_DEBUG_DECODE
101                         );
102                 _audio_decoder->maybe_seek (ContentTime::from_frames (*missing, _content->resampled_frame_rate()), accurate);
103         }
104
105         /* Offset of the data that we want from the start of _decoded.audio
106            (to be set up shortly)
107         */
108         Frame decoded_offset = 0;
109
110         /* Now enough pass() calls will either:
111          *  (a) give us what we want, or
112          *  (b) hit the end of the decoder.
113          *
114          * If we are being accurate, we want the right frames,
115          * otherwise any frames will do.
116          */
117         if (accurate) {
118                 /* Keep stuffing data into _decoded until we have enough data, or the subclass does not want to give us any more */
119                 while (
120                         (_decoded.frame > frame || (_decoded.frame + _decoded.audio->frames()) <= to) &&
121                         !_decoder->pass (Decoder::PASS_REASON_AUDIO, accurate)
122                         )
123                 {}
124
125                 decoded_offset = frame - _decoded.frame;
126
127                 _log->log (
128                         String::compose ("Accurate ADS::get has offset %1 from request %2 and available %3", decoded_offset, frame, have_from),
129                         LogEntry::TYPE_DEBUG_DECODE
130                         );
131         } else {
132                 while (
133                         _decoded.audio->frames() < length &&
134                         !_decoder->pass (Decoder::PASS_REASON_AUDIO, accurate)
135                         )
136                 {}
137
138                 /* Use decoded_offset of 0, as we don't really care what frames we return */
139         }
140
141         /* The amount of data available in _decoded.audio starting from `frame'.  This could be -ve
142            if pass() returned true before we got enough data.
143         */
144         Frame const available = _decoded.audio->frames() - decoded_offset;
145
146         /* We will return either that, or the requested amount, whichever is smaller */
147         Frame const to_return = max ((Frame) 0, min (available, length));
148
149         /* Copy our data to the output */
150         shared_ptr<AudioBuffers> out (new AudioBuffers (_decoded.audio->channels(), to_return));
151         out->copy_from (_decoded.audio.get(), to_return, decoded_offset, 0);
152
153         Frame const remaining = max ((Frame) 0, available - to_return);
154
155         /* Clean up decoded; first, move the data after what we just returned to the start of the buffer */
156         _decoded.audio->move (decoded_offset + to_return, 0, remaining);
157         /* And set up the number of frames we have left */
158         _decoded.audio->set_frames (remaining);
159         /* Also bump where those frames are in terms of the content */
160         _decoded.frame += decoded_offset + to_return;
161
162         return ContentAudio (out, frame);
163 }
164
165 /** Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
166  *  We have to assume that we are feeding continuous data into the resampler, and so we get continuous
167  *  data out.  Hence we do the timestamping here, post-resampler, just by counting samples.
168  *
169  *  The time is passed in here so that after a seek we can set up our _position.  The
170  *  time is ignored once this has been done.
171  */
172 void
173 AudioDecoderStream::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
174 {
175         _log->log (String::compose ("ADS receives %1 %2", to_string(time), data->frames ()), LogEntry::TYPE_DEBUG_DECODE);
176
177         if (_resampler) {
178                 data = _resampler->run (data);
179         }
180
181         Frame const frame_rate = _content->resampled_frame_rate ();
182
183         if (_seek_reference) {
184                 /* We've had an accurate seek and now we're seeing some data */
185                 ContentTime const delta = time - _seek_reference.get ();
186                 Frame const delta_frames = delta.frames_round (frame_rate);
187                 if (delta_frames > 0) {
188                         /* This data comes after the seek time.  Pad the data with some silence. */
189                         shared_ptr<AudioBuffers> padded (new AudioBuffers (data->channels(), data->frames() + delta_frames));
190                         padded->make_silent ();
191                         padded->copy_from (data.get(), data->frames(), 0, delta_frames);
192                         data = padded;
193                         time -= delta;
194                 }
195                 _seek_reference = optional<ContentTime> ();
196         }
197
198         if (!_position) {
199                 _position = time.frames_round (frame_rate);
200         }
201
202         DCPOMATIC_ASSERT (_position.get() >= (_decoded.frame + _decoded.audio->frames()));
203
204         add (data);
205 }
206
207 void
208 AudioDecoderStream::add (shared_ptr<const AudioBuffers> data)
209 {
210         if (!_position) {
211                 /* This should only happen when there is a seek followed by a flush, but
212                    we need to cope with it.
213                 */
214                 return;
215         }
216
217         /* Resize _decoded to fit the new data */
218         int new_size = 0;
219         if (_decoded.audio->frames() == 0) {
220                 /* There's nothing in there, so just store the new data */
221                 new_size = data->frames ();
222                 _decoded.frame = _position.get ();
223         } else {
224                 /* Otherwise we need to extend _decoded to include the new stuff */
225                 new_size = _position.get() + data->frames() - _decoded.frame;
226         }
227
228         _decoded.audio->ensure_size (new_size);
229         _decoded.audio->set_frames (new_size);
230
231         /* Copy new data in */
232         _decoded.audio->copy_from (data.get(), data->frames(), 0, _position.get() - _decoded.frame);
233         _position = _position.get() + data->frames ();
234
235         /* Limit the amount of data we keep in case nobody is asking for it */
236         int const max_frames = _content->resampled_frame_rate () * 10;
237         if (_decoded.audio->frames() > max_frames) {
238                 int const to_remove = _decoded.audio->frames() - max_frames;
239                 _decoded.frame += to_remove;
240                 _decoded.audio->move (to_remove, 0, max_frames);
241                 _decoded.audio->set_frames (max_frames);
242         }
243 }
244
245 void
246 AudioDecoderStream::flush ()
247 {
248         if (!_resampler) {
249                 return;
250         }
251
252         shared_ptr<const AudioBuffers> b = _resampler->flush ();
253         if (b) {
254                 add (b);
255         }
256 }
257
258 void
259 AudioDecoderStream::seek (ContentTime t, bool accurate)
260 {
261         _position.reset ();
262         reset_decoded ();
263         if (accurate) {
264                 _seek_reference = t;
265         }
266 }
267
268 void
269 AudioDecoderStream::set_fast ()
270 {
271         if (_resampler) {
272                 _resampler->set_fast ();
273         }
274 }
275
276 optional<ContentTime>
277 AudioDecoderStream::position () const
278 {
279         if (!_position) {
280                 return optional<ContentTime> ();
281         }
282
283         return ContentTime::from_frames (_position.get(), _content->resampled_frame_rate());
284 }