2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_decoder_stream.h"
21 #include "audio_buffers.h"
22 #include "audio_processor.h"
23 #include "audio_decoder.h"
24 #include "resampler.h"
28 #include "compose.hpp"
38 using boost::optional;
39 using boost::shared_ptr;
41 AudioDecoderStream::AudioDecoderStream (shared_ptr<const AudioContent> content, AudioStreamPtr stream, AudioDecoder* decoder)
46 if (content->resampled_audio_frame_rate() != _stream->frame_rate()) {
47 _resampler.reset (new Resampler (_stream->frame_rate(), content->resampled_audio_frame_rate(), _stream->channels ()));
54 AudioDecoderStream::reset_decoded ()
56 _decoded = ContentAudio (shared_ptr<AudioBuffers> (new AudioBuffers (_stream->channels(), 0)), 0);
60 AudioDecoderStream::get (Frame frame, Frame length, bool accurate)
62 shared_ptr<ContentAudio> dec;
64 _content->film()->log()->log (String::compose ("ADS has request for %1 %2", frame, length), Log::TYPE_DEBUG_DECODE);
66 Frame const end = frame + length - 1;
68 if (frame < _decoded.frame || end > (_decoded.frame + length * 4)) {
69 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
70 seek (ContentTime::from_frames (frame, _content->resampled_audio_frame_rate()), accurate);
73 /* Offset of the data that we want from the start of _decoded.audio
74 (to be set up shortly)
76 Frame decoded_offset = 0;
78 /* Now enough pass() calls will either:
79 * (a) give us what we want, or
80 * (b) hit the end of the decoder.
82 * If we are being accurate, we want the right frames,
83 * otherwise any frames will do.
86 /* Keep stuffing data into _decoded until we have enough data, or the subclass does not want to give us any more */
88 (_decoded.frame > frame || (_decoded.frame + _decoded.audio->frames()) < end) &&
93 decoded_offset = frame - _decoded.frame;
96 _decoded.audio->frames() < length &&
101 /* Use decoded_offset of 0, as we don't really care what frames we return */
104 /* The amount of data available in _decoded.audio starting from `frame'. This could be -ve
105 if pass() returned true before we got enough data.
107 Frame const available = _decoded.audio->frames() - decoded_offset;
109 /* We will return either that, or the requested amount, whichever is smaller */
110 Frame const to_return = max ((Frame) 0, min (available, length));
112 /* Copy our data to the output */
113 shared_ptr<AudioBuffers> out (new AudioBuffers (_decoded.audio->channels(), to_return));
114 out->copy_from (_decoded.audio.get(), to_return, decoded_offset, 0);
116 Frame const remaining = max ((Frame) 0, available - to_return);
118 /* Clean up decoded; first, move the data after what we just returned to the start of the buffer */
119 _decoded.audio->move (decoded_offset + to_return, 0, remaining);
120 /* And set up the number of frames we have left */
121 _decoded.audio->set_frames (remaining);
122 /* Also bump where those frames are in terms of the content */
123 _decoded.frame += decoded_offset + to_return;
125 return ContentAudio (out, frame);
128 /** Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
129 * We have to assume that we are feeding continuous data into the resampler, and so we get continuous
130 * data out. Hence we do the timestamping here, post-resampler, just by counting samples.
132 * The time is passed in here so that after a seek we can set up our _position. The
133 * time is ignored once this has been done.
136 AudioDecoderStream::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
138 _content->film()->log()->log (String::compose ("ADS receives %1 %2", time, data->frames ()), Log::TYPE_DEBUG_DECODE);
141 data = _resampler->run (data);
144 Frame const frame_rate = _content->resampled_audio_frame_rate ();
146 if (_seek_reference) {
147 /* We've had an accurate seek and now we're seeing some data */
148 ContentTime const delta = time - _seek_reference.get ();
149 Frame const delta_frames = delta.frames_round (frame_rate);
150 if (delta_frames > 0) {
151 /* This data comes after the seek time. Pad the data with some silence. */
152 shared_ptr<AudioBuffers> padded (new AudioBuffers (data->channels(), data->frames() + delta_frames));
153 padded->make_silent ();
154 padded->copy_from (data.get(), data->frames(), 0, delta_frames);
157 } else if (delta_frames < 0) {
158 /* This data comes before the seek time. Throw some data away */
159 Frame const to_discard = min (-delta_frames, static_cast<Frame> (data->frames()));
160 Frame const to_keep = data->frames() - to_discard;
162 /* We have to throw all this data away, so keep _seek_reference and
163 try again next time some data arrives.
167 shared_ptr<AudioBuffers> trimmed (new AudioBuffers (data->channels(), to_keep));
168 trimmed->copy_from (data.get(), to_keep, to_discard, 0);
170 time += ContentTime::from_frames (to_discard, frame_rate);
172 _seek_reference = optional<ContentTime> ();
176 _position = time.frames_round (frame_rate);
179 DCPOMATIC_ASSERT (_position.get() >= (_decoded.frame + _decoded.audio->frames()));
185 AudioDecoderStream::add (shared_ptr<const AudioBuffers> data)
188 /* This should only happen when there is a seek followed by a flush, but
189 we need to cope with it.
194 /* Resize _decoded to fit the new data */
196 if (_decoded.audio->frames() == 0) {
197 /* There's nothing in there, so just store the new data */
198 new_size = data->frames ();
199 _decoded.frame = _position.get ();
201 /* Otherwise we need to extend _decoded to include the new stuff */
202 new_size = _position.get() + data->frames() - _decoded.frame;
205 _decoded.audio->ensure_size (new_size);
206 _decoded.audio->set_frames (new_size);
208 /* Copy new data in */
209 _decoded.audio->copy_from (data.get(), data->frames(), 0, _position.get() - _decoded.frame);
210 _position = _position.get() + data->frames ();
212 /* Limit the amount of data we keep in case nobody is asking for it */
213 int const max_frames = _content->resampled_audio_frame_rate () * 10;
214 if (_decoded.audio->frames() > max_frames) {
215 int const to_remove = _decoded.audio->frames() - max_frames;
216 _decoded.frame += to_remove;
217 _decoded.audio->move (to_remove, 0, max_frames);
218 _decoded.audio->set_frames (max_frames);
223 AudioDecoderStream::flush ()
229 shared_ptr<const AudioBuffers> b = _resampler->flush ();
236 AudioDecoderStream::seek (ContentTime t, bool accurate)