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,std::vector<std::vector<std::vector<float>>> * block)34 void FrameBlocker::InsertSubFrameAndExtractBlock(
35     const std::vector<std::vector<rtc::ArrayView<float>>>& sub_frame,
36     std::vector<std::vector<std::vector<float>>>* block) {
37   RTC_DCHECK(block);
38   RTC_DCHECK_EQ(num_bands_, block->size());
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)[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     }
62   }
63 }
64 
IsBlockAvailable() const65 bool FrameBlocker::IsBlockAvailable() const {
66   return kBlockSize == buffer_[0][0].size();
67 }
68 
ExtractBlock(std::vector<std::vector<std::vector<float>>> * block)69 void FrameBlocker::ExtractBlock(
70     std::vector<std::vector<std::vector<float>>>* block) {
71   RTC_DCHECK(block);
72   RTC_DCHECK_EQ(num_bands_, block->size());
73   RTC_DCHECK(IsBlockAvailable());
74   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     }
85   }
86 }
87 
88 }  // namespace webrtc
89