xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/block_framer.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/block_framer.h"
12 
13 #include <algorithm>
14 
15 #include "modules/audio_processing/aec3/aec3_common.h"
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 
BlockFramer(size_t num_bands,size_t num_channels)20 BlockFramer::BlockFramer(size_t num_bands, size_t num_channels)
21     : num_bands_(num_bands),
22       num_channels_(num_channels),
23       buffer_(num_bands_,
24               std::vector<std::vector<float>>(
25                   num_channels,
26                   std::vector<float>(kBlockSize, 0.f))) {
27   RTC_DCHECK_LT(0, num_bands);
28   RTC_DCHECK_LT(0, num_channels);
29 }
30 
31 BlockFramer::~BlockFramer() = default;
32 
33 // All the constants are chosen so that the buffer is either empty or has enough
34 // samples for InsertBlockAndExtractSubFrame to produce a frame. In order to
35 // achieve this, the InsertBlockAndExtractSubFrame and InsertBlock methods need
36 // to be called in the correct order.
InsertBlock(const Block & block)37 void BlockFramer::InsertBlock(const Block& block) {
38   RTC_DCHECK_EQ(num_bands_, block.NumBands());
39   RTC_DCHECK_EQ(num_channels_, block.NumChannels());
40   for (size_t band = 0; band < num_bands_; ++band) {
41     for (size_t channel = 0; channel < num_channels_; ++channel) {
42       RTC_DCHECK_EQ(0, buffer_[band][channel].size());
43 
44       buffer_[band][channel].insert(buffer_[band][channel].begin(),
45                                     block.begin(band, channel),
46                                     block.end(band, channel));
47     }
48   }
49 }
50 
InsertBlockAndExtractSubFrame(const Block & block,std::vector<std::vector<rtc::ArrayView<float>>> * sub_frame)51 void BlockFramer::InsertBlockAndExtractSubFrame(
52     const Block& block,
53     std::vector<std::vector<rtc::ArrayView<float>>>* sub_frame) {
54   RTC_DCHECK(sub_frame);
55   RTC_DCHECK_EQ(num_bands_, block.NumBands());
56   RTC_DCHECK_EQ(num_channels_, block.NumChannels());
57   RTC_DCHECK_EQ(num_bands_, sub_frame->size());
58   for (size_t band = 0; band < num_bands_; ++band) {
59     RTC_DCHECK_EQ(num_channels_, (*sub_frame)[0].size());
60     for (size_t channel = 0; channel < num_channels_; ++channel) {
61       RTC_DCHECK_LE(kSubFrameLength,
62                     buffer_[band][channel].size() + kBlockSize);
63       RTC_DCHECK_GE(kBlockSize, buffer_[band][channel].size());
64       RTC_DCHECK_EQ(kSubFrameLength, (*sub_frame)[band][channel].size());
65 
66       const int samples_to_frame =
67           kSubFrameLength - buffer_[band][channel].size();
68       std::copy(buffer_[band][channel].begin(), buffer_[band][channel].end(),
69                 (*sub_frame)[band][channel].begin());
70       std::copy(
71           block.begin(band, channel),
72           block.begin(band, channel) + samples_to_frame,
73           (*sub_frame)[band][channel].begin() + buffer_[band][channel].size());
74       buffer_[band][channel].clear();
75       buffer_[band][channel].insert(
76           buffer_[band][channel].begin(),
77           block.begin(band, channel) + samples_to_frame,
78           block.end(band, channel));
79     }
80   }
81 }
82 
83 }  // namespace webrtc
84