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 "exceptions.h"
24 #include "resampler.h"
30 using std::stringstream;
35 using boost::optional;
36 using boost::shared_ptr;
38 AudioDecoder::AudioDecoder (shared_ptr<const AudioContent> content)
39 : _audio_content (content)
40 , _decoded_audio (shared_ptr<AudioBuffers> (new AudioBuffers (content->audio_channels(), 0)), 0)
42 if (content->output_audio_frame_rate() != content->content_audio_frame_rate() && content->audio_channels ()) {
43 _resampler.reset (new Resampler (content->content_audio_frame_rate(), content->output_audio_frame_rate(), content->audio_channels ()));
47 shared_ptr<ContentAudio>
48 AudioDecoder::get_audio (AudioFrame frame, AudioFrame length, bool accurate)
50 shared_ptr<ContentAudio> dec;
52 AudioFrame const end = frame + length - 1;
54 if (frame < _decoded_audio.frame || end > (_decoded_audio.frame + length * 4)) {
55 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
56 seek (ContentTime::from_frames (frame, _audio_content->content_audio_frame_rate()), accurate);
59 AudioFrame decoded_offset = 0;
61 /* Now enough pass() calls will either:
62 * (a) give us what we want, or
63 * (b) hit the end of the decoder.
65 * If we are being accurate, we want the right frames,
66 * otherwise any frames will do.
69 while (!pass() && _decoded_audio.audio->frames() < length) {}
70 /* Use decoded_offset of 0, as we don't really care what frames we return */
72 while (!pass() && (_decoded_audio.frame > frame || (_decoded_audio.frame + _decoded_audio.audio->frames()) < end)) {}
73 decoded_offset = frame - _decoded_audio.frame;
76 AudioFrame const amount_left = _decoded_audio.audio->frames() - decoded_offset;
78 AudioFrame const to_return = min (amount_left, length);
79 shared_ptr<AudioBuffers> out (new AudioBuffers (_decoded_audio.audio->channels(), to_return));
80 out->copy_from (_decoded_audio.audio.get(), to_return, decoded_offset, 0);
82 /* Clean up decoded */
83 _decoded_audio.audio->move (decoded_offset + to_return, 0, amount_left - to_return);
84 _decoded_audio.audio->set_frames (amount_left - to_return);
86 return shared_ptr<ContentAudio> (new ContentAudio (out, frame));
89 /** Called by subclasses when audio data is ready.
91 * Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
92 * We have to assume that we are feeding continuous data into the resampler, and so we get continuous
93 * data out. Hence we do the timestamping here, post-resampler, just by counting samples.
95 * The time is passed in here so that after a seek we can set up our _audio_position. The
96 * time is ignored once this has been done.
99 AudioDecoder::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
102 data = _resampler->run (data);
105 if (!_audio_position) {
106 _audio_position = time.frames (_audio_content->output_audio_frame_rate ());
109 assert (_audio_position >= (_decoded_audio.frame + _decoded_audio.audio->frames()));
111 /* Resize _decoded_audio to fit the new data */
112 int const new_size = _audio_position.get() + data->frames() - _decoded_audio.frame;
113 _decoded_audio.audio->ensure_size (new_size);
114 _decoded_audio.audio->set_frames (new_size);
116 /* Copy new data in */
117 _decoded_audio.audio->copy_from (data.get(), data->frames(), 0, _audio_position.get() - _decoded_audio.frame);
118 _audio_position = _audio_position.get() + data->frames ();
123 AudioDecoder::flush ()
130 shared_ptr<const AudioBuffers> b = _resampler->flush ();
132 _pending.push_back (shared_ptr<DecodedAudio> (new DecodedAudio (b, _audio_position.get ())));
133 _audio_position = _audio_position.get() + b->frames ();
139 AudioDecoder::seek (ContentTime, bool)
141 _audio_position.reset ();