xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/frame_blocker.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "modules/audio_processing/aec3/frame_blocker.h"
12 
13 #include "modules/audio_processing/aec3/aec3_common.h"
14 #include "rtc_base/checks.h"
15 
16 namespace webrtc {
17 
FrameBlocker(size_t num_bands,size_t num_channels)18 FrameBlocker::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     }
29   }
30 }
31 
32 FrameBlocker::~FrameBlocker() = default;
33 
InsertSubFrameAndExtractBlock(const std::vector<std::vector<rtc::ArrayView<float>>> & sub_frame,Block * block)34 void FrameBlocker::InsertSubFrameAndExtractBlock(
35     const std::vector<std::vector<rtc::ArrayView<float>>>& sub_frame,
36     Block* block) {
37   RTC_DCHECK(block);
38   RTC_DCHECK_EQ(num_bands_, block->NumBands());
39   RTC_DCHECK_EQ(num_bands_, sub_frame.size());
40   for (size_t band = 0; band < num_bands_; ++band) {
41     RTC_DCHECK_EQ(num_channels_, block->NumChannels());
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(kSubFrameLength, sub_frame[band][channel].size());
46       const int samples_to_block = kBlockSize - buffer_[band][channel].size();
47       std::copy(buffer_[band][channel].begin(), buffer_[band][channel].end(),
48                 block->begin(band, channel));
49       std::copy(sub_frame[band][channel].begin(),
50                 sub_frame[band][channel].begin() + samples_to_block,
51                 block->begin(band, channel) + kBlockSize - samples_to_block);
52       buffer_[band][channel].clear();
53       buffer_[band][channel].insert(
54           buffer_[band][channel].begin(),
55           sub_frame[band][channel].begin() + samples_to_block,
56           sub_frame[band][channel].end());
57     }
58   }
59 }
60 
IsBlockAvailable() const61 bool FrameBlocker::IsBlockAvailable() const {
62   return kBlockSize == buffer_[0][0].size();
63 }
64 
ExtractBlock(Block * block)65 void FrameBlocker::ExtractBlock(Block* block) {
66   RTC_DCHECK(block);
67   RTC_DCHECK_EQ(num_bands_, block->NumBands());
68   RTC_DCHECK_EQ(num_channels_, block->NumChannels());
69   RTC_DCHECK(IsBlockAvailable());
70   for (size_t band = 0; band < num_bands_; ++band) {
71     for (size_t channel = 0; channel < num_channels_; ++channel) {
72       RTC_DCHECK_EQ(kBlockSize, buffer_[band][channel].size());
73       std::copy(buffer_[band][channel].begin(), buffer_[band][channel].end(),
74                 block->begin(band, channel));
75       buffer_[band][channel].clear();
76     }
77   }
78 }
79 
80 }  // namespace webrtc
81