OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // AudioConverter implementation. Uses MultiChannelSincResampler for resampling | 5 // AudioConverter implementation. Uses MultiChannelSincResampler for resampling |
6 // audio, ChannelMixer for channel mixing, and AudioPullFifo for buffering. | 6 // audio, ChannelMixer for channel mixing, and AudioPullFifo for buffering. |
7 // | 7 // |
8 // Delay estimates are provided to InputCallbacks based on the frame delay | 8 // Delay estimates are provided to InputCallbacks based on the frame delay |
9 // information reported via the resampler and FIFO units. | 9 // information reported via the resampler and FIFO units. |
10 | 10 |
11 #include "media/base/audio_converter.h" | 11 #include "media/base/audio_converter.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
17 #include "media/base/audio_bus.h" | 17 #include "media/base/audio_bus.h" |
18 #include "media/base/audio_pull_fifo.h" | 18 #include "media/base/audio_pull_fifo.h" |
19 #include "media/base/channel_mixer.h" | 19 #include "media/base/channel_mixer.h" |
20 #include "media/base/multi_channel_resampler.h" | 20 #include "media/base/multi_channel_resampler.h" |
21 #include "media/base/vector_math.h" | 21 #include "media/base/vector_math.h" |
22 | 22 |
23 namespace media { | 23 namespace media { |
24 | 24 |
25 AudioConverter::AudioConverter(const AudioParameters& input_params, | 25 AudioConverter::AudioConverter(const AudioParameters& input_params, |
26 const AudioParameters& output_params, | 26 const AudioParameters& output_params, |
27 bool disable_fifo) | 27 bool disable_fifo) |
28 : chunk_size_(input_params.frames_per_buffer()), | 28 : chunk_size_(input_params.frames_per_buffer()), |
29 downmix_early_(false), | 29 downmix_early_(false), |
30 resampler_frame_delay_(0), | 30 initial_frames_delayed_(0), |
| 31 resampler_frames_delayed_(0), |
| 32 io_sample_rate_ratio_(input_params.sample_rate() / |
| 33 static_cast<double>(output_params.sample_rate())), |
31 input_channel_count_(input_params.channels()) { | 34 input_channel_count_(input_params.channels()) { |
32 CHECK(input_params.IsValid()); | 35 CHECK(input_params.IsValid()); |
33 CHECK(output_params.IsValid()); | 36 CHECK(output_params.IsValid()); |
34 | 37 |
35 // Handle different input and output channel layouts. | 38 // Handle different input and output channel layouts. |
36 if (input_params.channel_layout() != output_params.channel_layout() || | 39 if (input_params.channel_layout() != output_params.channel_layout() || |
37 input_params.channels() != output_params.channels()) { | 40 input_params.channels() != output_params.channels()) { |
38 DVLOG(1) << "Remixing channel layout from " << input_params.channel_layout() | 41 DVLOG(1) << "Remixing channel layout from " << input_params.channel_layout() |
39 << " to " << output_params.channel_layout() << "; from " | 42 << " to " << output_params.channel_layout() << "; from " |
40 << input_params.channels() << " channels to " | 43 << input_params.channels() << " channels to " |
41 << output_params.channels() << " channels."; | 44 << output_params.channels() << " channels."; |
42 channel_mixer_.reset(new ChannelMixer(input_params, output_params)); | 45 channel_mixer_.reset(new ChannelMixer(input_params, output_params)); |
43 | 46 |
44 // Pare off data as early as we can for efficiency. | 47 // Pare off data as early as we can for efficiency. |
45 downmix_early_ = input_params.channels() > output_params.channels(); | 48 downmix_early_ = input_params.channels() > output_params.channels(); |
46 } | 49 } |
47 | 50 |
48 // Only resample if necessary since it's expensive. | 51 // Only resample if necessary since it's expensive. |
49 if (input_params.sample_rate() != output_params.sample_rate()) { | 52 if (input_params.sample_rate() != output_params.sample_rate()) { |
50 DVLOG(1) << "Resampling from " << input_params.sample_rate() << " to " | 53 DVLOG(1) << "Resampling from " << input_params.sample_rate() << " to " |
51 << output_params.sample_rate(); | 54 << output_params.sample_rate(); |
52 const int request_size = disable_fifo ? SincResampler::kDefaultRequestSize : | 55 const int request_size = disable_fifo ? SincResampler::kDefaultRequestSize : |
53 input_params.frames_per_buffer(); | 56 input_params.frames_per_buffer(); |
54 const double io_sample_rate_ratio = | |
55 input_params.sample_rate() / | |
56 static_cast<double>(output_params.sample_rate()); | |
57 resampler_.reset(new MultiChannelResampler( | 57 resampler_.reset(new MultiChannelResampler( |
58 downmix_early_ ? output_params.channels() : input_params.channels(), | 58 downmix_early_ ? output_params.channels() : input_params.channels(), |
59 io_sample_rate_ratio, | 59 io_sample_rate_ratio_, request_size, |
60 request_size, | |
61 base::Bind(&AudioConverter::ProvideInput, base::Unretained(this)))); | 60 base::Bind(&AudioConverter::ProvideInput, base::Unretained(this)))); |
62 } | 61 } |
63 | 62 |
64 input_frame_duration_ = base::TimeDelta::FromMicroseconds( | |
65 base::Time::kMicrosecondsPerSecond / | |
66 static_cast<double>(input_params.sample_rate())); | |
67 output_frame_duration_ = base::TimeDelta::FromMicroseconds( | |
68 base::Time::kMicrosecondsPerSecond / | |
69 static_cast<double>(output_params.sample_rate())); | |
70 | |
71 // The resampler can be configured to work with a specific request size, so a | 63 // The resampler can be configured to work with a specific request size, so a |
72 // FIFO is not necessary when resampling. | 64 // FIFO is not necessary when resampling. |
73 if (disable_fifo || resampler_) | 65 if (disable_fifo || resampler_) |
74 return; | 66 return; |
75 | 67 |
76 // Since the output device may want a different buffer size than the caller | 68 // Since the output device may want a different buffer size than the caller |
77 // asked for, we need to use a FIFO to ensure that both sides read in chunk | 69 // asked for, we need to use a FIFO to ensure that both sides read in chunk |
78 // sizes they're configured for. | 70 // sizes they're configured for. |
79 if (input_params.frames_per_buffer() != output_params.frames_per_buffer()) { | 71 if (input_params.frames_per_buffer() != output_params.frames_per_buffer()) { |
80 DVLOG(1) << "Rebuffering from " << input_params.frames_per_buffer() | 72 DVLOG(1) << "Rebuffering from " << input_params.frames_per_buffer() |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 return chunk_size_; | 108 return chunk_size_; |
117 return resampler_->ChunkSize(); | 109 return resampler_->ChunkSize(); |
118 } | 110 } |
119 | 111 |
120 void AudioConverter::PrimeWithSilence() { | 112 void AudioConverter::PrimeWithSilence() { |
121 if (resampler_) { | 113 if (resampler_) { |
122 resampler_->PrimeWithSilence(); | 114 resampler_->PrimeWithSilence(); |
123 } | 115 } |
124 } | 116 } |
125 | 117 |
126 void AudioConverter::ConvertWithDelay(const base::TimeDelta& initial_delay, | 118 void AudioConverter::ConvertWithDelay(uint32_t initial_frames_delayed, |
127 AudioBus* dest) { | 119 AudioBus* dest) { |
128 initial_delay_ = initial_delay; | 120 initial_frames_delayed_ = initial_frames_delayed; |
129 | 121 |
130 if (transform_inputs_.empty()) { | 122 if (transform_inputs_.empty()) { |
131 dest->Zero(); | 123 dest->Zero(); |
132 return; | 124 return; |
133 } | 125 } |
134 | 126 |
135 // Determine if channel mixing should be done and if it should be done before | 127 // Determine if channel mixing should be done and if it should be done before |
136 // or after resampling. If it's possible to reduce the channel count prior to | 128 // or after resampling. If it's possible to reduce the channel count prior to |
137 // resampling we can save a lot of processing time. Vice versa, we don't want | 129 // resampling we can save a lot of processing time. Vice versa, we don't want |
138 // to increase the channel count prior to resampling for the same reason. | 130 // to increase the channel count prior to resampling for the same reason. |
(...skipping 18 matching lines...) Expand all Loading... |
157 } | 149 } |
158 | 150 |
159 // Finally upmix the channels if we didn't do so earlier. | 151 // Finally upmix the channels if we didn't do so earlier. |
160 if (needs_mixing) { | 152 if (needs_mixing) { |
161 DCHECK_EQ(temp_dest->frames(), dest->frames()); | 153 DCHECK_EQ(temp_dest->frames(), dest->frames()); |
162 channel_mixer_->Transform(temp_dest, dest); | 154 channel_mixer_->Transform(temp_dest, dest); |
163 } | 155 } |
164 } | 156 } |
165 | 157 |
166 void AudioConverter::Convert(AudioBus* dest) { | 158 void AudioConverter::Convert(AudioBus* dest) { |
167 ConvertWithDelay(base::TimeDelta::FromMilliseconds(0), dest); | 159 ConvertWithDelay(0, dest); |
168 } | 160 } |
169 | 161 |
170 void AudioConverter::SourceCallback(int fifo_frame_delay, AudioBus* dest) { | 162 void AudioConverter::SourceCallback(int fifo_frame_delay, AudioBus* dest) { |
171 const bool needs_downmix = channel_mixer_ && downmix_early_; | 163 const bool needs_downmix = channel_mixer_ && downmix_early_; |
172 | 164 |
173 if (!mixer_input_audio_bus_ || | 165 if (!mixer_input_audio_bus_ || |
174 mixer_input_audio_bus_->frames() != dest->frames()) { | 166 mixer_input_audio_bus_->frames() != dest->frames()) { |
175 mixer_input_audio_bus_ = | 167 mixer_input_audio_bus_ = |
176 AudioBus::Create(input_channel_count_, dest->frames()); | 168 AudioBus::Create(input_channel_count_, dest->frames()); |
177 } | 169 } |
178 | 170 |
179 // If we're downmixing early we need a temporary AudioBus which matches | 171 // If we're downmixing early we need a temporary AudioBus which matches |
180 // the the input channel count and input frame size since we're passing | 172 // the the input channel count and input frame size since we're passing |
181 // |unmixed_audio_| directly to the |source_callback_|. | 173 // |unmixed_audio_| directly to the |source_callback_|. |
182 if (needs_downmix) | 174 if (needs_downmix) |
183 CreateUnmixedAudioIfNecessary(dest->frames()); | 175 CreateUnmixedAudioIfNecessary(dest->frames()); |
184 | 176 |
185 AudioBus* const temp_dest = needs_downmix ? unmixed_audio_.get() : dest; | 177 AudioBus* const temp_dest = needs_downmix ? unmixed_audio_.get() : dest; |
186 | 178 |
187 // Sanity check our inputs. | 179 // Sanity check our inputs. |
188 DCHECK_EQ(temp_dest->frames(), mixer_input_audio_bus_->frames()); | 180 DCHECK_EQ(temp_dest->frames(), mixer_input_audio_bus_->frames()); |
189 DCHECK_EQ(temp_dest->channels(), mixer_input_audio_bus_->channels()); | 181 DCHECK_EQ(temp_dest->channels(), mixer_input_audio_bus_->channels()); |
190 | 182 |
191 // Calculate the buffer delay for this callback. | 183 // |total_frames_delayed| is reported to the *input* source in terms of the |
192 base::TimeDelta buffer_delay = initial_delay_; | 184 // *input* sample rate. |initial_frames_delayed_| is given in terms of the |
| 185 // output sample rate, so we scale by sample rate ratio (in/out). |
| 186 uint32_t total_frames_delayed = |
| 187 std::round(initial_frames_delayed_ * io_sample_rate_ratio_); |
193 if (resampler_) { | 188 if (resampler_) { |
194 buffer_delay += base::TimeDelta::FromMicroseconds( | 189 // |resampler_frames_delayed_| tallies frames queued up inside the resampler |
195 resampler_frame_delay_ * output_frame_duration_.InMicroseconds()); | 190 // that are already converted to the output format. Scale by ratio to get |
| 191 // delay in terms of input sample rate. |
| 192 total_frames_delayed += |
| 193 std::round(resampler_frames_delayed_ * io_sample_rate_ratio_); |
196 } | 194 } |
197 if (audio_fifo_) { | 195 if (audio_fifo_) { |
198 buffer_delay += base::TimeDelta::FromMicroseconds( | 196 total_frames_delayed += fifo_frame_delay; |
199 fifo_frame_delay * input_frame_duration_.InMicroseconds()); | |
200 } | 197 } |
201 | 198 |
202 // If we only have a single input, avoid an extra copy. | 199 // If we only have a single input, avoid an extra copy. |
203 AudioBus* const provide_input_dest = | 200 AudioBus* const provide_input_dest = |
204 transform_inputs_.size() == 1 ? temp_dest : mixer_input_audio_bus_.get(); | 201 transform_inputs_.size() == 1 ? temp_dest : mixer_input_audio_bus_.get(); |
205 | 202 |
206 // Have each mixer render its data into an output buffer then mix the result. | 203 // Have each mixer render its data into an output buffer then mix the result. |
207 for (auto* input : transform_inputs_) { | 204 for (auto* input : transform_inputs_) { |
208 const float volume = input->ProvideInput(provide_input_dest, buffer_delay); | 205 const float volume = |
209 | 206 input->ProvideInput(provide_input_dest, total_frames_delayed); |
210 // Optimize the most common single input, full volume case. | 207 // Optimize the most common single input, full volume case. |
211 if (input == transform_inputs_.front()) { | 208 if (input == transform_inputs_.front()) { |
212 if (volume == 1.0f) { | 209 if (volume == 1.0f) { |
213 if (temp_dest != provide_input_dest) | 210 if (temp_dest != provide_input_dest) |
214 provide_input_dest->CopyTo(temp_dest); | 211 provide_input_dest->CopyTo(temp_dest); |
215 } else if (volume > 0) { | 212 } else if (volume > 0) { |
216 for (int i = 0; i < provide_input_dest->channels(); ++i) { | 213 for (int i = 0; i < provide_input_dest->channels(); ++i) { |
217 vector_math::FMUL( | 214 vector_math::FMUL( |
218 provide_input_dest->channel(i), volume, | 215 provide_input_dest->channel(i), volume, |
219 provide_input_dest->frames(), temp_dest->channel(i)); | 216 provide_input_dest->frames(), temp_dest->channel(i)); |
(...skipping 16 matching lines...) Expand all Loading... |
236 } | 233 } |
237 } | 234 } |
238 | 235 |
239 if (needs_downmix) { | 236 if (needs_downmix) { |
240 DCHECK_EQ(temp_dest->frames(), dest->frames()); | 237 DCHECK_EQ(temp_dest->frames(), dest->frames()); |
241 channel_mixer_->Transform(temp_dest, dest); | 238 channel_mixer_->Transform(temp_dest, dest); |
242 } | 239 } |
243 } | 240 } |
244 | 241 |
245 void AudioConverter::ProvideInput(int resampler_frame_delay, AudioBus* dest) { | 242 void AudioConverter::ProvideInput(int resampler_frame_delay, AudioBus* dest) { |
246 resampler_frame_delay_ = resampler_frame_delay; | 243 resampler_frames_delayed_ = resampler_frame_delay; |
247 if (audio_fifo_) | 244 if (audio_fifo_) |
248 audio_fifo_->Consume(dest, dest->frames()); | 245 audio_fifo_->Consume(dest, dest->frames()); |
249 else | 246 else |
250 SourceCallback(0, dest); | 247 SourceCallback(0, dest); |
251 } | 248 } |
252 | 249 |
253 void AudioConverter::CreateUnmixedAudioIfNecessary(int frames) { | 250 void AudioConverter::CreateUnmixedAudioIfNecessary(int frames) { |
254 if (!unmixed_audio_ || unmixed_audio_->frames() != frames) | 251 if (!unmixed_audio_ || unmixed_audio_->frames() != frames) |
255 unmixed_audio_ = AudioBus::Create(input_channel_count_, frames); | 252 unmixed_audio_ = AudioBus::Create(input_channel_count_, frames); |
256 } | 253 } |
257 | 254 |
258 } // namespace media | 255 } // namespace media |
OLD | NEW |