blob: 63aaf098c50084f81f65d59b8e831f540520323a [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
Per Åhgrence202a02019-09-02 17:01:19 +020018FrameBlocker::FrameBlocker(size_t num_bands, size_t num_channels)
19 : num_bands_(num_bands),
20 num_channels_(num_channels),
21 buffer_(num_bands_, std::vector<std::vector<float>>(num_channels)) {
22 RTC_DCHECK_LT(0, num_bands);
23 RTC_DCHECK_LT(0, num_channels);
24 for (auto& band : buffer_) {
25 for (auto& channel : band) {
26 channel.reserve(kBlockSize);
27 RTC_DCHECK(channel.empty());
28 }
peahd0263542017-01-03 04:20:34 -080029 }
30}
31
32FrameBlocker::~FrameBlocker() = default;
33
34void FrameBlocker::InsertSubFrameAndExtractBlock(
Per Åhgrence202a02019-09-02 17:01:19 +020035 const std::vector<std::vector<rtc::ArrayView<float>>>& sub_frame,
36 std::vector<std::vector<std::vector<float>>>* block) {
peahd0263542017-01-03 04:20:34 -080037 RTC_DCHECK(block);
38 RTC_DCHECK_EQ(num_bands_, block->size());
39 RTC_DCHECK_EQ(num_bands_, sub_frame.size());
Per Åhgrence202a02019-09-02 17:01:19 +020040 for (size_t band = 0; band < num_bands_; ++band) {
41 RTC_DCHECK_EQ(num_channels_, (*block)[band].size());
42 RTC_DCHECK_EQ(num_channels_, sub_frame[band].size());
43 for (size_t channel = 0; channel < num_channels_; ++channel) {
44 RTC_DCHECK_GE(kBlockSize - 16, buffer_[band][channel].size());
45 RTC_DCHECK_EQ(kBlockSize, (*block)[band][channel].size());
46 RTC_DCHECK_EQ(kSubFrameLength, sub_frame[band][channel].size());
47 const int samples_to_block = kBlockSize - buffer_[band][channel].size();
48 (*block)[band][channel].clear();
49 (*block)[band][channel].insert((*block)[band][channel].begin(),
50 buffer_[band][channel].begin(),
51 buffer_[band][channel].end());
52 (*block)[band][channel].insert(
53 (*block)[band][channel].begin() + buffer_[band][channel].size(),
54 sub_frame[band][channel].begin(),
55 sub_frame[band][channel].begin() + samples_to_block);
56 buffer_[band][channel].clear();
57 buffer_[band][channel].insert(
58 buffer_[band][channel].begin(),
59 sub_frame[band][channel].begin() + samples_to_block,
60 sub_frame[band][channel].end());
61 }
peahd0263542017-01-03 04:20:34 -080062 }
63}
64
65bool FrameBlocker::IsBlockAvailable() const {
Per Åhgrence202a02019-09-02 17:01:19 +020066 return kBlockSize == buffer_[0][0].size();
peahd0263542017-01-03 04:20:34 -080067}
68
Per Åhgrence202a02019-09-02 17:01:19 +020069void FrameBlocker::ExtractBlock(
70 std::vector<std::vector<std::vector<float>>>* block) {
peahd0263542017-01-03 04:20:34 -080071 RTC_DCHECK(block);
72 RTC_DCHECK_EQ(num_bands_, block->size());
73 RTC_DCHECK(IsBlockAvailable());
Per Åhgrence202a02019-09-02 17:01:19 +020074 for (size_t band = 0; band < num_bands_; ++band) {
75 RTC_DCHECK_EQ(num_channels_, (*block)[band].size());
76 for (size_t channel = 0; channel < num_channels_; ++channel) {
77 RTC_DCHECK_EQ(kBlockSize, buffer_[band][channel].size());
78 RTC_DCHECK_EQ(kBlockSize, (*block)[band][channel].size());
79 (*block)[band][channel].clear();
80 (*block)[band][channel].insert((*block)[band][channel].begin(),
81 buffer_[band][channel].begin(),
82 buffer_[band][channel].end());
83 buffer_[band][channel].clear();
84 }
peahd0263542017-01-03 04:20:34 -080085 }
86}
87
88} // namespace webrtc