2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
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"
29 #include "audio_content.h"
30 #include "compose.hpp"
40 using boost::optional;
41 using boost::shared_ptr;
43 AudioDecoderStream::AudioDecoderStream (shared_ptr<const AudioContent> content, AudioStreamPtr stream, Decoder* decoder, shared_ptr<Log> log)
48 /* We effectively start having done a seek to zero; this allows silence-padding of the first
49 data that comes out of our decoder.
51 , _seek_reference (ContentTime ())
53 if (content->resampled_frame_rate() != _stream->frame_rate() && _stream->channels() > 0) {
54 _resampler.reset (new Resampler (_stream->frame_rate(), content->resampled_frame_rate(), _stream->channels ()));
61 AudioDecoderStream::reset_decoded ()
63 _decoded = ContentAudio (shared_ptr<AudioBuffers> (new AudioBuffers (_stream->channels(), 0)), 0);
67 AudioDecoderStream::get (Frame frame, Frame length, bool accurate)
69 shared_ptr<ContentAudio> dec;
71 _log->log (String::compose ("-> ADS has request for %1 %2", frame, length), LogEntry::TYPE_DEBUG_DECODE);
73 Frame const end = frame + length - 1;
75 /* If we are less than (about) 5 seconds behind the data that we want we'll
76 run through it rather than seeking.
78 Frame const slack = 5 * 48000;
80 if (frame < _decoded.frame || end > (_decoded.frame + _decoded.audio->frames() + slack)) {
81 /* Either we have no decoded data, all our data is after the time that we
82 want, or what we do have is a long way from what we want: seek */
83 _decoder->seek (ContentTime::from_frames (frame, _content->resampled_frame_rate()), accurate);
86 /* Offset of the data that we want from the start of _decoded.audio
87 (to be set up shortly)
89 Frame decoded_offset = 0;
91 /* Now enough pass() calls will either:
92 * (a) give us what we want, or
93 * (b) hit the end of the decoder.
95 * If we are being accurate, we want the right frames,
96 * otherwise any frames will do.
99 /* Keep stuffing data into _decoded until we have enough data, or the subclass does not want to give us any more */
101 (_decoded.frame > frame || (_decoded.frame + _decoded.audio->frames()) < end) &&
102 !_decoder->pass (Decoder::PASS_REASON_AUDIO, accurate)
106 decoded_offset = frame - _decoded.frame;
109 String::compose ("Accurate ADS::get has offset %1 from request %2 and available %3", decoded_offset, frame, _decoded.frame),
110 LogEntry::TYPE_DEBUG_DECODE
114 _decoded.audio->frames() < length &&
115 !_decoder->pass (Decoder::PASS_REASON_AUDIO, accurate)
119 /* Use decoded_offset of 0, as we don't really care what frames we return */
122 /* The amount of data available in _decoded.audio starting from `frame'. This could be -ve
123 if pass() returned true before we got enough data.
125 Frame const available = _decoded.audio->frames() - decoded_offset;
127 /* We will return either that, or the requested amount, whichever is smaller */
128 Frame const to_return = max ((Frame) 0, min (available, length));
130 /* Copy our data to the output */
131 shared_ptr<AudioBuffers> out (new AudioBuffers (_decoded.audio->channels(), to_return));
132 out->copy_from (_decoded.audio.get(), to_return, decoded_offset, 0);
134 Frame const remaining = max ((Frame) 0, available - to_return);
136 /* Clean up decoded; first, move the data after what we just returned to the start of the buffer */
137 _decoded.audio->move (decoded_offset + to_return, 0, remaining);
138 /* And set up the number of frames we have left */
139 _decoded.audio->set_frames (remaining);
140 /* Also bump where those frames are in terms of the content */
141 _decoded.frame += decoded_offset + to_return;
143 return ContentAudio (out, frame);
146 /** Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
147 * We have to assume that we are feeding continuous data into the resampler, and so we get continuous
148 * data out. Hence we do the timestamping here, post-resampler, just by counting samples.
150 * The time is passed in here so that after a seek we can set up our _position. The
151 * time is ignored once this has been done.
154 AudioDecoderStream::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
156 _log->log (String::compose ("ADS receives %1 %2", to_string(time), data->frames ()), LogEntry::TYPE_DEBUG_DECODE);
159 data = _resampler->run (data);
162 Frame const frame_rate = _content->resampled_frame_rate ();
164 if (_seek_reference) {
165 /* We've had an accurate seek and now we're seeing some data */
166 ContentTime const delta = time - _seek_reference.get ();
167 Frame const delta_frames = delta.frames_round (frame_rate);
168 if (delta_frames > 0) {
169 /* This data comes after the seek time. Pad the data with some silence. */
170 shared_ptr<AudioBuffers> padded (new AudioBuffers (data->channels(), data->frames() + delta_frames));
171 padded->make_silent ();
172 padded->copy_from (data.get(), data->frames(), 0, delta_frames);
176 _seek_reference = optional<ContentTime> ();
180 _position = time.frames_round (frame_rate);
183 DCPOMATIC_ASSERT (_position.get() >= (_decoded.frame + _decoded.audio->frames()));
189 AudioDecoderStream::add (shared_ptr<const AudioBuffers> data)
192 /* This should only happen when there is a seek followed by a flush, but
193 we need to cope with it.
198 /* Resize _decoded to fit the new data */
200 if (_decoded.audio->frames() == 0) {
201 /* There's nothing in there, so just store the new data */
202 new_size = data->frames ();
203 _decoded.frame = _position.get ();
205 /* Otherwise we need to extend _decoded to include the new stuff */
206 new_size = _position.get() + data->frames() - _decoded.frame;
209 _decoded.audio->ensure_size (new_size);
210 _decoded.audio->set_frames (new_size);
212 /* Copy new data in */
213 _decoded.audio->copy_from (data.get(), data->frames(), 0, _position.get() - _decoded.frame);
214 _position = _position.get() + data->frames ();
216 /* Limit the amount of data we keep in case nobody is asking for it */
217 int const max_frames = _content->resampled_frame_rate () * 10;
218 if (_decoded.audio->frames() > max_frames) {
219 int const to_remove = _decoded.audio->frames() - max_frames;
220 _decoded.frame += to_remove;
221 _decoded.audio->move (to_remove, 0, max_frames);
222 _decoded.audio->set_frames (max_frames);
227 AudioDecoderStream::flush ()
233 shared_ptr<const AudioBuffers> b = _resampler->flush ();
240 AudioDecoderStream::seek (ContentTime t, bool accurate)
250 AudioDecoderStream::set_fast ()
253 _resampler->set_fast ();