2 Copyright (C) 2012-2014 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.h"
21 #include "audio_buffers.h"
22 #include "audio_processor.h"
23 #include "resampler.h"
33 using boost::optional;
34 using boost::shared_ptr;
36 AudioDecoder::AudioDecoder (shared_ptr<const AudioContent> content)
37 : _audio_content (content)
39 if (content->resampled_audio_frame_rate() != content->audio_frame_rate() && content->audio_channels ()) {
40 _resampler.reset (new Resampler (content->audio_frame_rate(), content->resampled_audio_frame_rate(), content->audio_channels ()));
43 if (content->audio_processor ()) {
44 _processor = content->audio_processor()->clone (content->resampled_audio_frame_rate ());
47 reset_decoded_audio ();
51 AudioDecoder::reset_decoded_audio ()
53 _decoded_audio = ContentAudio (shared_ptr<AudioBuffers> (new AudioBuffers (_audio_content->processed_audio_channels(), 0)), 0);
56 shared_ptr<ContentAudio>
57 AudioDecoder::get_audio (AudioFrame frame, AudioFrame length, bool accurate)
59 shared_ptr<ContentAudio> dec;
61 AudioFrame const end = frame + length - 1;
63 if (frame < _decoded_audio.frame || end > (_decoded_audio.frame + length * 4)) {
64 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
65 seek (ContentTime::from_frames (frame, _audio_content->audio_frame_rate()), accurate);
68 /* Offset of the data that we want from the start of _decoded_audio.audio
69 (to be set up shortly)
71 AudioFrame decoded_offset = 0;
73 /* Now enough pass() calls will either:
74 * (a) give us what we want, or
75 * (b) hit the end of the decoder.
77 * If we are being accurate, we want the right frames,
78 * otherwise any frames will do.
81 /* Keep stuffing data into _decoded_audio until we have enough data, or the subclass does not want to give us any more */
82 while (_decoded_audio.frame > frame || (_decoded_audio.frame + _decoded_audio.audio->frames()) < end && !pass ()) {}
83 decoded_offset = frame - _decoded_audio.frame;
85 while (_decoded_audio.audio->frames() < length && !pass ()) {}
86 /* Use decoded_offset of 0, as we don't really care what frames we return */
89 /* The amount of data available in _decoded_audio.audio starting from `frame'. This could be -ve
90 if pass() returned true before we got enough data.
92 AudioFrame const available = _decoded_audio.audio->frames() - decoded_offset;
94 /* We will return either that, or the requested amount, whichever is smaller */
95 AudioFrame const to_return = max ((AudioFrame) 0, min (available, length));
97 /* Copy our data to the output */
98 shared_ptr<AudioBuffers> out (new AudioBuffers (_decoded_audio.audio->channels(), to_return));
99 out->copy_from (_decoded_audio.audio.get(), to_return, decoded_offset, 0);
101 AudioFrame const remaining = max ((AudioFrame) 0, available - to_return);
103 /* Clean up decoded; first, move the data after what we just returned to the start of the buffer */
104 _decoded_audio.audio->move (decoded_offset + to_return, 0, remaining);
105 /* And set up the number of frames we have left */
106 _decoded_audio.audio->set_frames (remaining);
107 /* Also bump where those frames are in terms of the content */
108 _decoded_audio.frame += decoded_offset + to_return;
110 return shared_ptr<ContentAudio> (new ContentAudio (out, frame));
113 /** Called by subclasses when audio data is ready.
115 * Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
116 * We have to assume that we are feeding continuous data into the resampler, and so we get continuous
117 * data out. Hence we do the timestamping here, post-resampler, just by counting samples.
119 * The time is passed in here so that after a seek we can set up our _audio_position. The
120 * time is ignored once this has been done.
123 AudioDecoder::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
126 data = _resampler->run (data);
130 data = _processor->run (data);
133 AudioFrame const frame_rate = _audio_content->resampled_audio_frame_rate ();
135 if (_seek_reference) {
136 /* We've had an accurate seek and now we're seeing some data */
137 ContentTime const delta = time - _seek_reference.get ();
138 AudioFrame const delta_frames = delta.frames (frame_rate);
139 if (delta_frames > 0) {
140 /* This data comes after the seek time. Pad the data with some silence. */
141 shared_ptr<AudioBuffers> padded (new AudioBuffers (data->channels(), data->frames() + delta_frames));
142 padded->make_silent ();
143 padded->copy_from (data.get(), data->frames(), 0, delta_frames);
146 } else if (delta_frames < 0) {
147 /* This data comes before the seek time. Throw some data away */
148 AudioFrame const to_discard = min (-delta_frames, static_cast<AudioFrame> (data->frames()));
149 AudioFrame const to_keep = data->frames() - to_discard;
151 /* We have to throw all this data away, so keep _seek_reference and
152 try again next time some data arrives.
156 shared_ptr<AudioBuffers> trimmed (new AudioBuffers (data->channels(), to_keep));
157 trimmed->copy_from (data.get(), to_keep, to_discard, 0);
159 time += ContentTime::from_frames (to_discard, frame_rate);
161 _seek_reference = optional<ContentTime> ();
164 if (!_audio_position) {
165 _audio_position = time.frames (frame_rate);
168 assert (_audio_position.get() >= (_decoded_audio.frame + _decoded_audio.audio->frames()));
174 AudioDecoder::add (shared_ptr<const AudioBuffers> data)
176 /* Resize _decoded_audio to fit the new data */
178 if (_decoded_audio.audio->frames() == 0) {
179 /* There's nothing in there, so just store the new data */
180 new_size = data->frames ();
181 _decoded_audio.frame = _audio_position.get ();
183 /* Otherwise we need to extend _decoded_audio to include the new stuff */
184 new_size = _audio_position.get() + data->frames() - _decoded_audio.frame;
187 _decoded_audio.audio->ensure_size (new_size);
188 _decoded_audio.audio->set_frames (new_size);
190 /* Copy new data in */
191 _decoded_audio.audio->copy_from (data.get(), data->frames(), 0, _audio_position.get() - _decoded_audio.frame);
192 _audio_position = _audio_position.get() + data->frames ();
194 /* Limit the amount of data we keep in case nobody is asking for it */
195 int const max_frames = _audio_content->resampled_audio_frame_rate () * 10;
196 if (_decoded_audio.audio->frames() > max_frames) {
197 int const to_remove = _decoded_audio.audio->frames() - max_frames;
198 _decoded_audio.frame += to_remove;
199 _decoded_audio.audio->move (to_remove, 0, max_frames);
200 _decoded_audio.audio->set_frames (max_frames);
205 AudioDecoder::flush ()
211 shared_ptr<const AudioBuffers> b = _resampler->flush ();
218 AudioDecoder::seek (ContentTime t, bool accurate)
220 _audio_position.reset ();
221 reset_decoded_audio ();
226 _processor->flush ();