blob: 32668fa0792ffd50246122683dc00cfbc10e11f6 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/audio_buffer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_audio/channel_buffer.h"
18#include "common_audio/include/audio_util.h"
19#include "common_audio/resampler/push_sinc_resampler.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "modules/audio_processing/splitting_filter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023namespace webrtc {
24namespace {
25
Steve Antonf254e9e2019-08-21 17:52:28 +000026const size_t kSamplesPer16kHzChannel = 160;
27const size_t kSamplesPer32kHzChannel = 320;
28const size_t kSamplesPer48kHzChannel = 480;
Alejandro Luebs5a92aa82015-04-27 11:34:45 -070029
Steve Antonf254e9e2019-08-21 17:52:28 +000030size_t NumBandsFromSamplesPerChannel(size_t num_frames) {
31 size_t num_bands = 1;
32 if (num_frames == kSamplesPer32kHzChannel ||
33 num_frames == kSamplesPer48kHzChannel) {
34 num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000035 }
Steve Antonf254e9e2019-08-21 17:52:28 +000036 return num_bands;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000037}
38
niklase@google.com470e71d2011-07-07 08:21:25 +000039} // namespace
40
Steve Antonf254e9e2019-08-21 17:52:28 +000041AudioBuffer::AudioBuffer(size_t input_num_frames,
42 size_t num_input_channels,
43 size_t process_num_frames,
44 size_t num_process_channels,
45 size_t output_num_frames)
46 : input_num_frames_(input_num_frames),
47 num_input_channels_(num_input_channels),
48 proc_num_frames_(process_num_frames),
49 num_proc_channels_(num_process_channels),
50 output_num_frames_(output_num_frames),
51 num_channels_(num_process_channels),
52 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)),
53 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)),
54 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)),
55 output_buffer_(new IFChannelBuffer(output_num_frames_, num_channels_)) {
kwibergaf476c72016-11-28 15:21:39 -080056 RTC_DCHECK_GT(input_num_frames_, 0);
Steve Antonf254e9e2019-08-21 17:52:28 +000057 RTC_DCHECK_GT(proc_num_frames_, 0);
kwibergaf476c72016-11-28 15:21:39 -080058 RTC_DCHECK_GT(output_num_frames_, 0);
Steve Antonf254e9e2019-08-21 17:52:28 +000059 RTC_DCHECK_GT(num_input_channels_, 0);
60 RTC_DCHECK_GT(num_proc_channels_, 0);
61 RTC_DCHECK_LE(num_proc_channels_, num_input_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
Steve Antonf254e9e2019-08-21 17:52:28 +000063 if (input_num_frames_ != proc_num_frames_ ||
64 output_num_frames_ != proc_num_frames_) {
65 // Create an intermediate buffer for resampling.
66 process_buffer_.reset(
67 new ChannelBuffer<float>(proc_num_frames_, num_proc_channels_));
68
69 if (input_num_frames_ != proc_num_frames_) {
70 for (size_t i = 0; i < num_proc_channels_; ++i) {
71 input_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
72 new PushSincResampler(input_num_frames_, proc_num_frames_)));
73 }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000074 }
75
Steve Antonf254e9e2019-08-21 17:52:28 +000076 if (output_num_frames_ != proc_num_frames_) {
77 for (size_t i = 0; i < num_proc_channels_; ++i) {
78 output_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
79 new PushSincResampler(proc_num_frames_, output_num_frames_)));
80 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081 }
82 }
83
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000084 if (num_bands_ > 1) {
Steve Antonf254e9e2019-08-21 17:52:28 +000085 split_data_.reset(
86 new IFChannelBuffer(proc_num_frames_, num_proc_channels_, num_bands_));
87 splitting_filter_.reset(
88 new SplittingFilter(num_proc_channels_, num_bands_, proc_num_frames_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000089 }
90}
91
andrew@webrtc.org103657b2014-04-24 18:28:56 +000092AudioBuffer::~AudioBuffer() {}
93
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094void AudioBuffer::CopyFrom(const float* const* data,
Michael Graczyk86c6d332015-07-23 11:41:39 -070095 const StreamConfig& stream_config) {
kwiberg9e2be5f2016-09-14 05:23:22 -070096 RTC_DCHECK_EQ(stream_config.num_frames(), input_num_frames_);
Steve Antonf254e9e2019-08-21 17:52:28 +000097 RTC_DCHECK_EQ(stream_config.num_channels(), num_input_channels_);
98 InitForNewData();
99 // Initialized lazily because there's a different condition in
100 // DeinterleaveFrom.
101 const bool need_to_downmix =
102 num_input_channels_ > 1 && num_proc_channels_ == 1;
103 if (need_to_downmix && !input_buffer_) {
104 input_buffer_.reset(
105 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
106 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000107
Steve Antonf254e9e2019-08-21 17:52:28 +0000108 // Downmix.
109 const float* const* data_ptr = data;
110 if (need_to_downmix) {
111 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_,
112 input_buffer_->fbuf()->channels()[0]);
113 data_ptr = input_buffer_->fbuf_const()->channels();
114 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115
Steve Antonf254e9e2019-08-21 17:52:28 +0000116 // Resample.
117 if (input_num_frames_ != proc_num_frames_) {
118 for (size_t i = 0; i < num_proc_channels_; ++i) {
119 input_resamplers_[i]->Resample(data_ptr[i], input_num_frames_,
120 process_buffer_->channels()[i],
121 proc_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122 }
Steve Antonf254e9e2019-08-21 17:52:28 +0000123 data_ptr = process_buffer_->channels();
124 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000125
Steve Antonf254e9e2019-08-21 17:52:28 +0000126 // Convert to the S16 range.
127 for (size_t i = 0; i < num_proc_channels_; ++i) {
128 FloatToFloatS16(data_ptr[i], proc_num_frames_,
129 data_->fbuf()->channels()[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000130 }
131}
132
Michael Graczyk86c6d332015-07-23 11:41:39 -0700133void AudioBuffer::CopyTo(const StreamConfig& stream_config,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000134 float* const* data) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700135 RTC_DCHECK_EQ(stream_config.num_frames(), output_num_frames_);
Steve Antonf254e9e2019-08-21 17:52:28 +0000136 RTC_DCHECK(stream_config.num_channels() == num_channels_ ||
137 num_channels_ == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000138
Steve Antonf254e9e2019-08-21 17:52:28 +0000139 // Convert to the float range.
140 float* const* data_ptr = data;
141 if (output_num_frames_ != proc_num_frames_) {
142 // Convert to an intermediate buffer for subsequent resampling.
143 data_ptr = process_buffer_->channels();
144 }
145 for (size_t i = 0; i < num_channels_; ++i) {
146 FloatS16ToFloat(data_->fbuf()->channels()[i], proc_num_frames_,
147 data_ptr[i]);
148 }
149
150 // Resample.
151 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800152 for (size_t i = 0; i < num_channels_; ++i) {
Steve Antonf254e9e2019-08-21 17:52:28 +0000153 output_resamplers_[i]->Resample(data_ptr[i], proc_num_frames_, data[i],
154 output_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000155 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 }
aluebsb2328d12016-01-11 20:32:29 -0800157
Steve Antonf254e9e2019-08-21 17:52:28 +0000158 // Upmix.
Peter Kasting69558702016-01-12 16:26:35 -0800159 for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) {
aluebsb2328d12016-01-11 20:32:29 -0800160 memcpy(data[i], data[0], output_num_frames_ * sizeof(**data));
161 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000162}
163
Steve Antonf254e9e2019-08-21 17:52:28 +0000164void AudioBuffer::InitForNewData() {
165 num_channels_ = num_proc_channels_;
166 data_->set_num_channels(num_proc_channels_);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700167 if (split_data_.get()) {
Steve Antonf254e9e2019-08-21 17:52:28 +0000168 split_data_->set_num_channels(num_proc_channels_);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700169 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000170}
171
Steve Antonf254e9e2019-08-21 17:52:28 +0000172const float* const* AudioBuffer::split_channels_const_f(Band band) const {
173 if (split_data_.get()) {
174 return split_data_->fbuf_const()->channels(band);
175 } else {
176 return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr;
177 }
178}
179
180const float* const* AudioBuffer::channels_const_f() const {
181 return data_->fbuf_const()->channels();
182}
183
184float* const* AudioBuffer::channels_f() {
185 return data_->fbuf()->channels();
186}
187
188const float* const* AudioBuffer::split_bands_const_f(size_t channel) const {
189 return split_data_.get() ? split_data_->fbuf_const()->bands(channel)
190 : data_->fbuf_const()->bands(channel);
191}
192
193float* const* AudioBuffer::split_bands_f(size_t channel) {
194 return split_data_.get() ? split_data_->fbuf()->bands(channel)
195 : data_->fbuf()->bands(channel);
196}
197
198size_t AudioBuffer::num_channels() const {
199 return num_channels_;
200}
201
Peter Kasting69558702016-01-12 16:26:35 -0800202void AudioBuffer::set_num_channels(size_t num_channels) {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000203 num_channels_ = num_channels;
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700204 data_->set_num_channels(num_channels);
205 if (split_data_.get()) {
206 split_data_->set_num_channels(num_channels);
207 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
Steve Antonf254e9e2019-08-21 17:52:28 +0000210size_t AudioBuffer::num_frames() const {
211 return proc_num_frames_;
212}
213
214size_t AudioBuffer::num_frames_per_band() const {
215 return num_split_frames_;
216}
217
218size_t AudioBuffer::num_bands() const {
219 return num_bands_;
220}
221
Alejandro Luebs05c76052015-05-20 14:39:39 -0700222// The resampler is only for supporting 48kHz to 16kHz in the reverse stream.
Steve Antonf254e9e2019-08-21 17:52:28 +0000223void AudioBuffer::DeinterleaveFrom(const AudioFrame* frame) {
224 RTC_DCHECK_EQ(frame->num_channels_, num_input_channels_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700225 RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_);
Steve Antonf254e9e2019-08-21 17:52:28 +0000226 InitForNewData();
227 // Initialized lazily because there's a different condition in CopyFrom.
228 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) {
229 input_buffer_.reset(
230 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
231 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
Steve Antonf254e9e2019-08-21 17:52:28 +0000233 int16_t* const* deinterleaved;
234 if (input_num_frames_ == proc_num_frames_) {
235 deinterleaved = data_->ibuf()->channels();
Per Åhgren81c0cf22019-08-21 15:02:37 +0200236 } else {
Steve Antonf254e9e2019-08-21 17:52:28 +0000237 deinterleaved = input_buffer_->ibuf()->channels();
238 }
239 // TODO(yujo): handle muted frames more efficiently.
240 if (num_proc_channels_ == 1) {
241 // Downmix and deinterleave simultaneously.
242 DownmixInterleavedToMono(frame->data(), input_num_frames_,
243 num_input_channels_, deinterleaved[0]);
244 } else {
245 RTC_DCHECK_EQ(num_proc_channels_, num_input_channels_);
246 Deinterleave(frame->data(), input_num_frames_, num_proc_channels_,
247 deinterleaved);
248 }
Per Åhgren81c0cf22019-08-21 15:02:37 +0200249
Steve Antonf254e9e2019-08-21 17:52:28 +0000250 // Resample.
251 if (input_num_frames_ != proc_num_frames_) {
252 for (size_t i = 0; i < num_proc_channels_; ++i) {
253 input_resamplers_[i]->Resample(
254 input_buffer_->fbuf_const()->channels()[i], input_num_frames_,
255 data_->fbuf()->channels()[i], proc_num_frames_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000256 }
257 }
258}
259
Steve Antonf254e9e2019-08-21 17:52:28 +0000260void AudioBuffer::InterleaveTo(AudioFrame* frame) const {
kwiberg9e2be5f2016-09-14 05:23:22 -0700261 RTC_DCHECK(frame->num_channels_ == num_channels_ || num_channels_ == 1);
262 RTC_DCHECK_EQ(frame->samples_per_channel_, output_num_frames_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700263
Steve Antonf254e9e2019-08-21 17:52:28 +0000264 // Resample if necessary.
265 IFChannelBuffer* data_ptr = data_.get();
266 if (proc_num_frames_ != output_num_frames_) {
267 for (size_t i = 0; i < num_channels_; ++i) {
268 output_resamplers_[i]->Resample(
269 data_->fbuf()->channels()[i], proc_num_frames_,
270 output_buffer_->fbuf()->channels()[i], output_num_frames_);
Per Åhgren81c0cf22019-08-21 15:02:37 +0200271 }
Steve Antonf254e9e2019-08-21 17:52:28 +0000272 data_ptr = output_buffer_.get();
273 }
Per Åhgren81c0cf22019-08-21 15:02:37 +0200274
Steve Antonf254e9e2019-08-21 17:52:28 +0000275 // TODO(yujo): handle muted frames more efficiently.
276 if (frame->num_channels_ == num_channels_) {
277 Interleave(data_ptr->ibuf()->channels(), output_num_frames_, num_channels_,
278 frame->mutable_data());
ekmeyerson60d9b332015-08-14 10:35:55 -0700279 } else {
Steve Antonf254e9e2019-08-21 17:52:28 +0000280 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], output_num_frames_,
281 frame->num_channels_, frame->mutable_data());
ekmeyerson60d9b332015-08-14 10:35:55 -0700282 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000283}
284
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000285void AudioBuffer::SplitIntoFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000286 splitting_filter_->Analysis(data_.get(), split_data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000287}
288
289void AudioBuffer::MergeFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000290 splitting_filter_->Synthesis(split_data_.get(), data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000291}
292
Steve Antonf254e9e2019-08-21 17:52:28 +0000293void AudioBuffer::CopySplitChannelDataTo(size_t channel,
Per Åhgren928146f2019-08-20 09:19:21 +0200294 int16_t* const* split_band_data) {
295 for (size_t k = 0; k < num_bands(); ++k) {
Steve Antonf254e9e2019-08-21 17:52:28 +0000296 const float* band_data = split_bands_f(channel)[k];
Per Åhgren928146f2019-08-20 09:19:21 +0200297 RTC_DCHECK(split_band_data[k]);
298 RTC_DCHECK(band_data);
299 for (size_t i = 0; i < num_frames_per_band(); ++i) {
300 split_band_data[k][i] = FloatS16ToS16(band_data[i]);
301 }
302 }
303}
304
Steve Antonf254e9e2019-08-21 17:52:28 +0000305void AudioBuffer::CopySplitChannelDataFrom(
Per Åhgren928146f2019-08-20 09:19:21 +0200306 size_t channel,
307 const int16_t* const* split_band_data) {
308 for (size_t k = 0; k < num_bands(); ++k) {
Steve Antonf254e9e2019-08-21 17:52:28 +0000309 float* band_data = split_bands_f(channel)[k];
Per Åhgren928146f2019-08-20 09:19:21 +0200310 RTC_DCHECK(split_band_data[k]);
311 RTC_DCHECK(band_data);
312 for (size_t i = 0; i < num_frames_per_band(); ++i) {
313 band_data[i] = split_band_data[k][i];
314 }
315 }
316}
317
niklase@google.com470e71d2011-07-07 08:21:25 +0000318} // namespace webrtc