Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 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_MATRIX_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_MATRIX_BUFFER_H_ |
| 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 14 | #include <stddef.h> |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
| 17 | #include "rtc_base/checks.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | // Struct for bundling a circular buffer of two dimensional vector objects |
| 22 | // together with the read and write indices. |
| 23 | struct MatrixBuffer { |
| 24 | MatrixBuffer(size_t size, size_t height, size_t width); |
| 25 | ~MatrixBuffer(); |
| 26 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 27 | int IncIndex(int index) const { |
| 28 | RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size)); |
| 29 | return index < size - 1 ? index + 1 : 0; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 30 | } |
| 31 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 32 | int DecIndex(int index) const { |
| 33 | RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size)); |
| 34 | return index > 0 ? index - 1 : size - 1; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 37 | int OffsetIndex(int index, int offset) const { |
| 38 | RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size)); |
| 39 | RTC_DCHECK_GE(size, offset); |
| 40 | return (size + index + offset) % size; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void UpdateWriteIndex(int offset) { write = OffsetIndex(write, offset); } |
| 44 | void IncWriteIndex() { write = IncIndex(write); } |
| 45 | void DecWriteIndex() { write = DecIndex(write); } |
| 46 | void UpdateReadIndex(int offset) { read = OffsetIndex(read, offset); } |
| 47 | void IncReadIndex() { read = IncIndex(read); } |
| 48 | void DecReadIndex() { read = DecIndex(read); } |
| 49 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 50 | const int size; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 51 | std::vector<std::vector<std::vector<float>>> buffer; |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 52 | int write = 0; |
| 53 | int read = 0; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | } // namespace webrtc |
| 57 | |
| 58 | #endif // MODULES_AUDIO_PROCESSING_AEC3_MATRIX_BUFFER_H_ |