peah cf02cf13a7 Major AEC3 render pipeline changes
This CL adds major render pipeline changes to the AEC3 code. The reason
for these are that
1) It allows the echo removal unit to receive information about the content
in bands beyond band 0, thereby allowing removal of high-frequency
echoes
2) It allows more controlled handling of the render buffers, allowing proper
buffer behaviour during capture glitches and clock-drift.

Unfortunately, the render pipeline caused a lot of related changes in much
of the rest of the AEC3 files. Most of these are, however, caused by
a change of class name.

Another unfortunate effect of this CL, is that a number of unittest cease to
compile. I chose to temporarily solve that by removing them from the
build using #if/#endif. The reason for that is that those will anyway again
need to be changed in the next review, and doing like this avoids them
having to be reviewed twice.

BUG=webrtc:6018

Review-Url: https://codereview.webrtc.org/2784023002
Cr-Commit-Position: refs/heads/master@{#17547}
2017-04-05 21:18:07 +00:00

207 lines
6.6 KiB
C++

/*
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/audio_processing/aec3/render_delay_buffer.h"
#include <string.h>
#include <algorithm>
#include "webrtc/base/checks.h"
#include "webrtc/base/constructormagic.h"
#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
#include "webrtc/modules/audio_processing/aec3/block_processor.h"
#include "webrtc/modules/audio_processing/aec3/decimator_by_4.h"
#include "webrtc/modules/audio_processing/aec3/fft_data.h"
#include "webrtc/system_wrappers/include/logging.h"
namespace webrtc {
namespace {
class ApiCallJitterBuffer {
public:
explicit ApiCallJitterBuffer(size_t num_bands) {
buffer_.fill(std::vector<std::vector<float>>(
num_bands, std::vector<float>(kBlockSize, 0.f)));
}
~ApiCallJitterBuffer() = default;
void Reset() {
size_ = 0;
last_insert_index_ = 0;
}
void Insert(const std::vector<std::vector<float>>& block) {
RTC_DCHECK_LT(size_, buffer_.size());
last_insert_index_ = (last_insert_index_ + 1) % buffer_.size();
RTC_DCHECK_EQ(buffer_[last_insert_index_].size(), block.size());
RTC_DCHECK_EQ(buffer_[last_insert_index_][0].size(), block[0].size());
for (size_t k = 0; k < block.size(); ++k) {
std::copy(block[k].begin(), block[k].end(),
buffer_[last_insert_index_][k].begin());
}
++size_;
}
void Remove(std::vector<std::vector<float>>* block) {
RTC_DCHECK_LT(0, size_);
--size_;
const size_t extract_index =
(last_insert_index_ - size_ + buffer_.size()) % buffer_.size();
for (size_t k = 0; k < block->size(); ++k) {
std::copy(buffer_[extract_index][k].begin(),
buffer_[extract_index][k].end(), (*block)[k].begin());
}
}
size_t Size() const { return size_; }
bool Full() const { return size_ >= (buffer_.size()); }
bool Empty() const { return size_ == 0; }
private:
std::array<std::vector<std::vector<float>>, kMaxApiCallsJitterBlocks> buffer_;
size_t size_ = 0;
int last_insert_index_ = 0;
};
class RenderDelayBufferImpl final : public RenderDelayBuffer {
public:
explicit RenderDelayBufferImpl(size_t num_bands);
~RenderDelayBufferImpl() override;
void Reset() override;
bool Insert(const std::vector<std::vector<float>>& block) override;
bool UpdateBuffers() override;
void SetDelay(size_t delay) override;
size_t Delay() const override { return delay_; }
const RenderBuffer& GetRenderBuffer() const override { return fft_buffer_; }
const DownsampledRenderBuffer& GetDownsampledRenderBuffer() const override {
return downsampled_render_buffer_;
}
private:
const Aec3Optimization optimization_;
std::array<std::vector<std::vector<float>>, kRenderDelayBufferSize> buffer_;
size_t delay_ = 0;
size_t last_insert_index_ = 0;
RenderBuffer fft_buffer_;
DownsampledRenderBuffer downsampled_render_buffer_;
DecimatorBy4 render_decimator_;
ApiCallJitterBuffer api_call_jitter_buffer_;
RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayBufferImpl);
};
RenderDelayBufferImpl::RenderDelayBufferImpl(size_t num_bands)
: optimization_(DetectOptimization()),
fft_buffer_(optimization_,
num_bands,
std::max(30, kAdaptiveFilterLength),
std::vector<size_t>(1, kAdaptiveFilterLength)),
api_call_jitter_buffer_(num_bands) {
buffer_.fill(std::vector<std::vector<float>>(
num_bands, std::vector<float>(kBlockSize, 0.f)));
RTC_DCHECK_LT(buffer_.size(), downsampled_render_buffer_.buffer.size());
}
RenderDelayBufferImpl::~RenderDelayBufferImpl() = default;
void RenderDelayBufferImpl::Reset() {
// Empty all data in the buffers.
delay_ = 0;
last_insert_index_ = 0;
downsampled_render_buffer_.position = 0;
downsampled_render_buffer_.buffer.fill(0.f);
fft_buffer_.Clear();
api_call_jitter_buffer_.Reset();
}
bool RenderDelayBufferImpl::Insert(
const std::vector<std::vector<float>>& block) {
RTC_DCHECK_EQ(block.size(), buffer_[0].size());
RTC_DCHECK_EQ(block[0].size(), buffer_[0][0].size());
if (api_call_jitter_buffer_.Full()) {
// Report buffer overrun and let the caller handle the overrun.
return false;
}
api_call_jitter_buffer_.Insert(block);
return true;
}
bool RenderDelayBufferImpl::UpdateBuffers() {
bool underrun = true;
// Update the buffers with a new block if such is available, otherwise repeat
// the previous block.
if (api_call_jitter_buffer_.Size() > 0) {
last_insert_index_ = (last_insert_index_ + 1) % buffer_.size();
api_call_jitter_buffer_.Remove(&buffer_[last_insert_index_]);
underrun = false;
}
downsampled_render_buffer_.position =
(downsampled_render_buffer_.position - kSubBlockSize +
downsampled_render_buffer_.buffer.size()) %
downsampled_render_buffer_.buffer.size();
std::array<float, kSubBlockSize> render_downsampled;
render_decimator_.Decimate(buffer_[last_insert_index_][0],
render_downsampled);
std::copy(render_downsampled.rbegin(), render_downsampled.rend(),
downsampled_render_buffer_.buffer.begin() +
downsampled_render_buffer_.position);
fft_buffer_.Insert(
buffer_[(last_insert_index_ - delay_ + buffer_.size()) % buffer_.size()]);
return !underrun;
}
void RenderDelayBufferImpl::SetDelay(size_t delay) {
if (delay_ == delay) {
return;
}
// If there is a new delay set, clear the fft buffer.
fft_buffer_.Clear();
const size_t max_delay = buffer_.size() - 1;
if (max_delay < delay) {
// If the desired delay is larger than the delay buffer, shorten the delay
// buffer size to achieve the desired alignment with the available buffer
// size.
const size_t delay_decrease = delay - max_delay;
RTC_DCHECK_LT(delay_decrease, buffer_.size());
downsampled_render_buffer_.position =
(downsampled_render_buffer_.position + kSubBlockSize * delay_decrease) %
downsampled_render_buffer_.buffer.size();
last_insert_index_ =
(last_insert_index_ + buffer_.size() - delay_decrease) % buffer_.size();
RTC_DCHECK_EQ(max_delay, delay_ - delay_decrease);
delay_ = max_delay;
} else {
delay_ = delay;
}
}
} // namespace
RenderDelayBuffer* RenderDelayBuffer::Create(size_t num_bands) {
return new RenderDelayBufferImpl(num_bands);
}
} // namespace webrtc