blob: ca122e5ebbdee8a79aab77bca0de599166586b16 [file] [log] [blame]
peahd0263542017-01-03 04:20:34 -08001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
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/aec3/frame_blocker.h"
peahd0263542017-01-03 04:20:34 -080012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include "modules/audio_processing/aec3/aec3_common.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
peahd0263542017-01-03 04:20:34 -080015
16namespace webrtc {
17
18FrameBlocker::FrameBlocker(size_t num_bands)
19 : num_bands_(num_bands), buffer_(num_bands_) {
20 for (auto& b : buffer_) {
21 b.reserve(kBlockSize);
22 RTC_DCHECK(b.empty());
23 }
24}
25
26FrameBlocker::~FrameBlocker() = default;
27
28void FrameBlocker::InsertSubFrameAndExtractBlock(
29 const std::vector<rtc::ArrayView<float>>& sub_frame,
30 std::vector<std::vector<float>>* block) {
31 RTC_DCHECK(block);
32 RTC_DCHECK_EQ(num_bands_, block->size());
33 RTC_DCHECK_EQ(num_bands_, sub_frame.size());
34 for (size_t i = 0; i < num_bands_; ++i) {
35 RTC_DCHECK_GE(kBlockSize - 16, buffer_[i].size());
36 RTC_DCHECK_EQ(kBlockSize, (*block)[i].size());
37 RTC_DCHECK_EQ(kSubFrameLength, sub_frame[i].size());
38 const int samples_to_block = kBlockSize - buffer_[i].size();
39 (*block)[i].clear();
40 (*block)[i].insert((*block)[i].begin(), buffer_[i].begin(),
41 buffer_[i].end());
42 (*block)[i].insert((*block)[i].begin() + buffer_[i].size(),
43 sub_frame[i].begin(),
44 sub_frame[i].begin() + samples_to_block);
45 buffer_[i].clear();
46 buffer_[i].insert(buffer_[i].begin(),
47 sub_frame[i].begin() + samples_to_block,
48 sub_frame[i].end());
49 }
50}
51
52bool FrameBlocker::IsBlockAvailable() const {
53 return kBlockSize == buffer_[0].size();
54}
55
56void FrameBlocker::ExtractBlock(std::vector<std::vector<float>>* block) {
57 RTC_DCHECK(block);
58 RTC_DCHECK_EQ(num_bands_, block->size());
59 RTC_DCHECK(IsBlockAvailable());
60 for (size_t i = 0; i < num_bands_; ++i) {
61 RTC_DCHECK_EQ(kBlockSize, buffer_[i].size());
62 RTC_DCHECK_EQ(kBlockSize, (*block)[i].size());
63 (*block)[i].clear();
64 (*block)[i].insert((*block)[i].begin(), buffer_[i].begin(),
65 buffer_[i].end());
66 buffer_[i].clear();
67 }
68}
69
70} // namespace webrtc