xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/block_buffer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 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 #ifndef MODULES_AUDIO_PROCESSING_AEC3_BLOCK_BUFFER_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_BLOCK_BUFFER_H_
13 
14 #include <stddef.h>
15 
16 #include <vector>
17 
18 #include "modules/audio_processing/aec3/block.h"
19 #include "rtc_base/checks.h"
20 
21 namespace webrtc {
22 
23 // Struct for bundling a circular buffer of two dimensional vector objects
24 // together with the read and write indices.
25 struct BlockBuffer {
26   BlockBuffer(size_t size, size_t num_bands, size_t num_channels);
27   ~BlockBuffer();
28 
IncIndexBlockBuffer29   int IncIndex(int index) const {
30     RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
31     return index < size - 1 ? index + 1 : 0;
32   }
33 
DecIndexBlockBuffer34   int DecIndex(int index) const {
35     RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
36     return index > 0 ? index - 1 : size - 1;
37   }
38 
OffsetIndexBlockBuffer39   int OffsetIndex(int index, int offset) const {
40     RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
41     RTC_DCHECK_GE(size, offset);
42     return (size + index + offset) % size;
43   }
44 
UpdateWriteIndexBlockBuffer45   void UpdateWriteIndex(int offset) { write = OffsetIndex(write, offset); }
IncWriteIndexBlockBuffer46   void IncWriteIndex() { write = IncIndex(write); }
DecWriteIndexBlockBuffer47   void DecWriteIndex() { write = DecIndex(write); }
UpdateReadIndexBlockBuffer48   void UpdateReadIndex(int offset) { read = OffsetIndex(read, offset); }
IncReadIndexBlockBuffer49   void IncReadIndex() { read = IncIndex(read); }
DecReadIndexBlockBuffer50   void DecReadIndex() { read = DecIndex(read); }
51 
52   const int size;
53   std::vector<Block> buffer;
54   int write = 0;
55   int read = 0;
56 };
57 
58 }  // namespace webrtc
59 
60 #endif  // MODULES_AUDIO_PROCESSING_AEC3_BLOCK_BUFFER_H_
61