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
Peter Kastingdce40cf2015-08-24 14:52:23 -070026const size_t kSamplesPer16kHzChannel = 160;
27const size_t kSamplesPer32kHzChannel = 320;
28const size_t kSamplesPer48kHzChannel = 480;
Alejandro Luebs5a92aa82015-04-27 11:34:45 -070029
Peter Kastingdce40cf2015-08-24 14:52:23 -070030size_t NumBandsFromSamplesPerChannel(size_t num_frames) {
31 size_t num_bands = 1;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000032 if (num_frames == kSamplesPer32kHzChannel ||
33 num_frames == kSamplesPer48kHzChannel) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070034 num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000035 }
36 return num_bands;
37}
38
niklase@google.com470e71d2011-07-07 08:21:25 +000039} // namespace
40
Peter Kastingdce40cf2015-08-24 14:52:23 -070041AudioBuffer::AudioBuffer(size_t input_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080042 size_t num_input_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070043 size_t process_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080044 size_t num_process_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070045 size_t output_num_frames)
Yves Gerey665174f2018-06-19 15:03:05 +020046 : 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_)),
Yves Gerey665174f2018-06-19 15:03:05 +020054 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);
57 RTC_DCHECK_GT(proc_num_frames_, 0);
58 RTC_DCHECK_GT(output_num_frames_, 0);
59 RTC_DCHECK_GT(num_input_channels_, 0);
60 RTC_DCHECK_GT(num_proc_channels_, 0);
kwiberg9e2be5f2016-09-14 05:23:22 -070061 RTC_DCHECK_LE(num_proc_channels_, num_input_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000063 if (input_num_frames_ != proc_num_frames_ ||
64 output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000065 // Create an intermediate buffer for resampling.
Yves Gerey665174f2018-06-19 15:03:05 +020066 process_buffer_.reset(
67 new ChannelBuffer<float>(proc_num_frames_, num_proc_channels_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000068
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000069 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -080070 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 10:24:26 -070071 input_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
72 new PushSincResampler(input_num_frames_, proc_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000073 }
74 }
75
76 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -080077 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 10:24:26 -070078 output_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
79 new PushSincResampler(proc_num_frames_, output_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000080 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081 }
82 }
83
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000084 if (num_bands_ > 1) {
Yves Gerey665174f2018-06-19 15:03:05 +020085 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_);
97 RTC_DCHECK_EQ(stream_config.num_channels(), num_input_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000098 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -070099 // Initialized lazily because there's a different condition in
100 // DeinterleaveFrom.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700101 const bool need_to_downmix =
102 num_input_channels_ > 1 && num_proc_channels_ == 1;
103 if (need_to_downmix && !input_buffer_) {
Alejandro Luebs05c76052015-05-20 14:39:39 -0700104 input_buffer_.reset(
105 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
106 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000107
108 // Downmix.
109 const float* const* data_ptr = data;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700110 if (need_to_downmix) {
111 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_,
112 input_buffer_->fbuf()->channels()[0]);
Alejandro Luebs05c76052015-05-20 14:39:39 -0700113 data_ptr = input_buffer_->fbuf_const()->channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000114 }
115
116 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000117 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800118 for (size_t i = 0; i < num_proc_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200119 input_resamplers_[i]->Resample(data_ptr[i], input_num_frames_,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000120 process_buffer_->channels()[i],
121 proc_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122 }
123 data_ptr = process_buffer_->channels();
124 }
125
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000126 // Convert to the S16 range.
Peter Kasting69558702016-01-12 16:26:35 -0800127 for (size_t i = 0; i < num_proc_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200128 FloatToFloatS16(data_ptr[i], proc_num_frames_,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000129 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_);
136 RTC_DCHECK(stream_config.num_channels() == num_channels_ ||
137 num_channels_ == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000138
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000139 // Convert to the float range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000140 float* const* data_ptr = data;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000141 if (output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000142 // Convert to an intermediate buffer for subsequent resampling.
143 data_ptr = process_buffer_->channels();
144 }
Peter Kasting69558702016-01-12 16:26:35 -0800145 for (size_t i = 0; i < num_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200146 FloatS16ToFloat(data_->fbuf()->channels()[i], proc_num_frames_,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000147 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000148 }
149
150 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000151 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800152 for (size_t i = 0; i < num_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200153 output_resamplers_[i]->Resample(data_ptr[i], proc_num_frames_, data[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000154 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
158 // 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
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000164void AudioBuffer::InitForNewData() {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000165 num_channels_ = num_proc_channels_;
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700166 data_->set_num_channels(num_proc_channels_);
167 if (split_data_.get()) {
168 split_data_->set_num_channels(num_proc_channels_);
169 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000170}
171
Per Åhgren928146f2019-08-20 09:19:21 +0200172const float* const* AudioBuffer::split_channels_const_f(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000173 if (split_data_.get()) {
Per Åhgren928146f2019-08-20 09:19:21 +0200174 return split_data_->fbuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000175 } else {
Per Åhgren928146f2019-08-20 09:19:21 +0200176 return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000177 }
178}
179
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000180const float* const* AudioBuffer::channels_const_f() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000181 return data_->fbuf_const()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000182}
183
184float* const* AudioBuffer::channels_f() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000185 return data_->fbuf()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000186}
187
Peter Kasting69558702016-01-12 16:26:35 -0800188const float* const* AudioBuffer::split_bands_const_f(size_t channel) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200189 return split_data_.get() ? split_data_->fbuf_const()->bands(channel)
190 : data_->fbuf_const()->bands(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
Peter Kasting69558702016-01-12 16:26:35 -0800193float* const* AudioBuffer::split_bands_f(size_t channel) {
Yves Gerey665174f2018-06-19 15:03:05 +0200194 return split_data_.get() ? split_data_->fbuf()->bands(channel)
195 : data_->fbuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000196}
197
Peter Kasting69558702016-01-12 16:26:35 -0800198size_t AudioBuffer::num_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000199 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
Peter Kastingdce40cf2015-08-24 14:52:23 -0700210size_t AudioBuffer::num_frames() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000211 return proc_num_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
Peter Kastingdce40cf2015-08-24 14:52:23 -0700214size_t AudioBuffer::num_frames_per_band() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000215 return num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000216}
217
Peter Kastingdce40cf2015-08-24 14:52:23 -0700218size_t AudioBuffer::num_bands() const {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000219 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.
Per Åhgrena1351272019-08-15 12:15:46 +0200223void AudioBuffer::DeinterleaveFrom(const AudioFrame* frame) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700224 RTC_DCHECK_EQ(frame->num_channels_, num_input_channels_);
225 RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000226 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700227 // 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
Alejandro Luebs05c76052015-05-20 14:39:39 -0700233 int16_t* const* deinterleaved;
234 if (input_num_frames_ == proc_num_frames_) {
235 deinterleaved = data_->ibuf()->channels();
236 } else {
237 deinterleaved = input_buffer_->ibuf()->channels();
238 }
yujo36b1a5f2017-06-12 12:45:32 -0700239 // TODO(yujo): handle muted frames more efficiently.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700240 if (num_proc_channels_ == 1) {
241 // Downmix and deinterleave simultaneously.
yujo36b1a5f2017-06-12 12:45:32 -0700242 DownmixInterleavedToMono(frame->data(), input_num_frames_,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700243 num_input_channels_, deinterleaved[0]);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000244 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700245 RTC_DCHECK_EQ(num_proc_channels_, num_input_channels_);
Yves Gerey665174f2018-06-19 15:03:05 +0200246 Deinterleave(frame->data(), input_num_frames_, num_proc_channels_,
Alejandro Luebs05c76052015-05-20 14:39:39 -0700247 deinterleaved);
248 }
249
250 // Resample.
251 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800252 for (size_t i = 0; i < num_proc_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200253 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
Per Åhgrena1351272019-08-15 12:15:46 +0200260void 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
264 // Resample if necessary.
265 IFChannelBuffer* data_ptr = data_.get();
266 if (proc_num_frames_ != output_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800267 for (size_t i = 0; i < num_channels_; ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -0700268 output_resamplers_[i]->Resample(
269 data_->fbuf()->channels()[i], proc_num_frames_,
270 output_buffer_->fbuf()->channels()[i], output_num_frames_);
271 }
272 data_ptr = output_buffer_.get();
273 }
274
yujo36b1a5f2017-06-12 12:45:32 -0700275 // TODO(yujo): handle muted frames more efficiently.
ekmeyerson60d9b332015-08-14 10:35:55 -0700276 if (frame->num_channels_ == num_channels_) {
Alejandro Luebs40cbec52016-04-05 17:29:19 -0700277 Interleave(data_ptr->ibuf()->channels(), output_num_frames_, num_channels_,
yujo36b1a5f2017-06-12 12:45:32 -0700278 frame->mutable_data());
ekmeyerson60d9b332015-08-14 10:35:55 -0700279 } else {
Alejandro Luebs40cbec52016-04-05 17:29:19 -0700280 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], output_num_frames_,
yujo36b1a5f2017-06-12 12:45:32 -0700281 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
Per Åhgren928146f2019-08-20 09:19:21 +0200293void AudioBuffer::CopySplitChannelDataTo(size_t channel,
294 int16_t* const* split_band_data) {
295 for (size_t k = 0; k < num_bands(); ++k) {
296 const float* band_data = split_bands_f(channel)[k];
297 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
305void AudioBuffer::CopySplitChannelDataFrom(
306 size_t channel,
307 const int16_t* const* split_band_data) {
308 for (size_t k = 0; k < num_bands(); ++k) {
309 float* band_data = split_bands_f(channel)[k];
310 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