Chromium Code Reviews| 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 |
| 162 // TODO - make that a uint32_t | |
|
DaleCurtis
2016/05/24 20:27:02
?
chcunningham
2016/05/25 18:21:15
Woops. Was a note-to-self about fifo_frame_delay,
| |
| 170 void AudioConverter::SourceCallback(int fifo_frame_delay, AudioBus* dest) { | 163 void AudioConverter::SourceCallback(int fifo_frame_delay, AudioBus* dest) { |
| 171 const bool needs_downmix = channel_mixer_ && downmix_early_; | 164 const bool needs_downmix = channel_mixer_ && downmix_early_; |
| 172 | 165 |
| 173 if (!mixer_input_audio_bus_ || | 166 if (!mixer_input_audio_bus_ || |
| 174 mixer_input_audio_bus_->frames() != dest->frames()) { | 167 mixer_input_audio_bus_->frames() != dest->frames()) { |
| 175 mixer_input_audio_bus_ = | 168 mixer_input_audio_bus_ = |
| 176 AudioBus::Create(input_channel_count_, dest->frames()); | 169 AudioBus::Create(input_channel_count_, dest->frames()); |
| 177 } | 170 } |
| 178 | 171 |
| 179 // If we're downmixing early we need a temporary AudioBus which matches | 172 // 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 | 173 // the the input channel count and input frame size since we're passing |
| 181 // |unmixed_audio_| directly to the |source_callback_|. | 174 // |unmixed_audio_| directly to the |source_callback_|. |
| 182 if (needs_downmix) | 175 if (needs_downmix) |
| 183 CreateUnmixedAudioIfNecessary(dest->frames()); | 176 CreateUnmixedAudioIfNecessary(dest->frames()); |
| 184 | 177 |
| 185 AudioBus* const temp_dest = needs_downmix ? unmixed_audio_.get() : dest; | 178 AudioBus* const temp_dest = needs_downmix ? unmixed_audio_.get() : dest; |
| 186 | 179 |
| 187 // Sanity check our inputs. | 180 // Sanity check our inputs. |
| 188 DCHECK_EQ(temp_dest->frames(), mixer_input_audio_bus_->frames()); | 181 DCHECK_EQ(temp_dest->frames(), mixer_input_audio_bus_->frames()); |
| 189 DCHECK_EQ(temp_dest->channels(), mixer_input_audio_bus_->channels()); | 182 DCHECK_EQ(temp_dest->channels(), mixer_input_audio_bus_->channels()); |
| 190 | 183 |
| 191 // Calculate the buffer delay for this callback. | 184 // |total_frames_delayed| is reported to the *input* source in terms of the |
| 192 base::TimeDelta buffer_delay = initial_delay_; | 185 // *input* sample rate. |initial_frames_delayed_| is given in terms of the |
| 186 // output sample rate, so we scale by sample rate ratio (in/out). | |
| 187 uint32_t total_frames_delayed = | |
| 188 std::round(initial_frames_delayed_ * io_sample_rate_ratio_); | |
| 193 if (resampler_) { | 189 if (resampler_) { |
| 194 buffer_delay += base::TimeDelta::FromMicroseconds( | 190 // |resampler_frames_delayed_| tallies frames queued up inside the resampler |
| 195 resampler_frame_delay_ * output_frame_duration_.InMicroseconds()); | 191 // that are already converted to the output format. Scale by ratio to get |
| 192 // delay in terms of input sample rate. | |
| 193 total_frames_delayed += | |
| 194 std::round(resampler_frames_delayed_ * io_sample_rate_ratio_); | |
| 195 LOG(ERROR) << __FUNCTION__ | |
|
DaleCurtis
2016/05/24 20:27:02
Remove?
chcunningham
2016/05/25 18:21:15
Done.
| |
| 196 << " reampler_frames_delayed:" << resampler_frames_delayed_ | |
| 197 << " total frames_delayed:" << total_frames_delayed; | |
| 196 } | 198 } |
| 197 if (audio_fifo_) { | 199 if (audio_fifo_) { |
| 198 buffer_delay += base::TimeDelta::FromMicroseconds( | 200 total_frames_delayed += fifo_frame_delay; |
| 199 fifo_frame_delay * input_frame_duration_.InMicroseconds()); | |
| 200 } | 201 } |
| 201 | 202 |
| 202 // If we only have a single input, avoid an extra copy. | 203 // If we only have a single input, avoid an extra copy. |
| 203 AudioBus* const provide_input_dest = | 204 AudioBus* const provide_input_dest = |
| 204 transform_inputs_.size() == 1 ? temp_dest : mixer_input_audio_bus_.get(); | 205 transform_inputs_.size() == 1 ? temp_dest : mixer_input_audio_bus_.get(); |
| 205 | 206 |
| 206 // Have each mixer render its data into an output buffer then mix the result. | 207 // Have each mixer render its data into an output buffer then mix the result. |
| 207 for (auto* input : transform_inputs_) { | 208 for (auto* input : transform_inputs_) { |
| 208 const float volume = input->ProvideInput(provide_input_dest, buffer_delay); | 209 const float volume = |
| 209 | 210 input->ProvideInput(provide_input_dest, total_frames_delayed); |
| 210 // Optimize the most common single input, full volume case. | 211 // Optimize the most common single input, full volume case. |
| 211 if (input == transform_inputs_.front()) { | 212 if (input == transform_inputs_.front()) { |
| 212 if (volume == 1.0f) { | 213 if (volume == 1.0f) { |
| 213 if (temp_dest != provide_input_dest) | 214 if (temp_dest != provide_input_dest) |
| 214 provide_input_dest->CopyTo(temp_dest); | 215 provide_input_dest->CopyTo(temp_dest); |
| 215 } else if (volume > 0) { | 216 } else if (volume > 0) { |
| 216 for (int i = 0; i < provide_input_dest->channels(); ++i) { | 217 for (int i = 0; i < provide_input_dest->channels(); ++i) { |
| 217 vector_math::FMUL( | 218 vector_math::FMUL( |
| 218 provide_input_dest->channel(i), volume, | 219 provide_input_dest->channel(i), volume, |
| 219 provide_input_dest->frames(), temp_dest->channel(i)); | 220 provide_input_dest->frames(), temp_dest->channel(i)); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 236 } | 237 } |
| 237 } | 238 } |
| 238 | 239 |
| 239 if (needs_downmix) { | 240 if (needs_downmix) { |
| 240 DCHECK_EQ(temp_dest->frames(), dest->frames()); | 241 DCHECK_EQ(temp_dest->frames(), dest->frames()); |
| 241 channel_mixer_->Transform(temp_dest, dest); | 242 channel_mixer_->Transform(temp_dest, dest); |
| 242 } | 243 } |
| 243 } | 244 } |
| 244 | 245 |
| 245 void AudioConverter::ProvideInput(int resampler_frame_delay, AudioBus* dest) { | 246 void AudioConverter::ProvideInput(int resampler_frame_delay, AudioBus* dest) { |
| 246 resampler_frame_delay_ = resampler_frame_delay; | 247 resampler_frames_delayed_ = resampler_frame_delay; |
| 247 if (audio_fifo_) | 248 if (audio_fifo_) |
| 248 audio_fifo_->Consume(dest, dest->frames()); | 249 audio_fifo_->Consume(dest, dest->frames()); |
| 249 else | 250 else |
| 250 SourceCallback(0, dest); | 251 SourceCallback(0, dest); |
| 251 } | 252 } |
| 252 | 253 |
| 253 void AudioConverter::CreateUnmixedAudioIfNecessary(int frames) { | 254 void AudioConverter::CreateUnmixedAudioIfNecessary(int frames) { |
| 254 if (!unmixed_audio_ || unmixed_audio_->frames() != frames) | 255 if (!unmixed_audio_ || unmixed_audio_->frames() != frames) |
| 255 unmixed_audio_ = AudioBus::Create(input_channel_count_, frames); | 256 unmixed_audio_ = AudioBus::Create(input_channel_count_, frames); |
| 256 } | 257 } |
| 257 | 258 |
| 258 } // namespace media | 259 } // namespace media |
| OLD | NEW |