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 #ifndef MODULES_AUDIO_PROCESSING_AEC3_AEC3_COMMON_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_AEC3_COMMON_H_
13 
14 #include <stddef.h>
15 #include "typedefs.h"  // NOLINT(build/include)
16 
17 namespace webrtc {
18 
19 #ifdef _MSC_VER /* visual c++ */
20 #define ALIGN16_BEG __declspec(align(16))
21 #define ALIGN16_END
22 #else /* gcc or icc */
23 #define ALIGN16_BEG
24 #define ALIGN16_END __attribute__((aligned(16)))
25 #endif
26 
27 enum class Aec3Optimization { kNone, kSse2, kNeon };
28 
29 constexpr int kNumBlocksPerSecond = 250;
30 
31 constexpr int kMetricsReportingIntervalBlocks = 10 * kNumBlocksPerSecond;
32 constexpr int kMetricsComputationBlocks = 9;
33 constexpr int kMetricsCollectionBlocks =
34     kMetricsReportingIntervalBlocks - kMetricsComputationBlocks;
35 
36 constexpr size_t kFftLengthBy2 = 64;
37 constexpr size_t kFftLengthBy2Plus1 = kFftLengthBy2 + 1;
38 constexpr size_t kFftLengthBy2Minus1 = kFftLengthBy2 - 1;
39 constexpr size_t kFftLength = 2 * kFftLengthBy2;
40 
41 constexpr int kAdaptiveFilterLength = 12;
42 constexpr int kUnknownDelayRenderWindowSize = 12;
43 constexpr int kAdaptiveFilterTimeDomainLength =
44     kAdaptiveFilterLength * kFftLengthBy2;
45 
46 constexpr size_t kMaxNumBands = 3;
47 constexpr size_t kSubFrameLength = 80;
48 
49 constexpr size_t kBlockSize = kFftLengthBy2;
50 constexpr size_t kExtendedBlockSize = 2 * kFftLengthBy2;
51 constexpr size_t kMatchedFilterWindowSizeSubBlocks = 32;
52 constexpr size_t kMatchedFilterAlignmentShiftSizeSubBlocks =
53     kMatchedFilterWindowSizeSubBlocks * 3 / 4;
54 
55 constexpr size_t kMinEchoPathDelayBlocks = 5;
56 constexpr size_t kMaxApiCallsJitterBlocks = 26;
57 constexpr size_t kRenderTransferQueueSize = kMaxApiCallsJitterBlocks / 2;
58 static_assert(2 * kRenderTransferQueueSize >= kMaxApiCallsJitterBlocks,
59               "Requirement to ensure buffer overflow detection");
60 
61 constexpr size_t kEchoPathChangeConvergenceBlocks = 2 * kNumBlocksPerSecond;
62 
63 // TODO(peah): Integrate this with how it is done inside audio_processing_impl.
NumBandsForRate(int sample_rate_hz)64 constexpr size_t NumBandsForRate(int sample_rate_hz) {
65   return static_cast<size_t>(sample_rate_hz == 8000 ? 1
66                                                     : sample_rate_hz / 16000);
67 }
LowestBandRate(int sample_rate_hz)68 constexpr int LowestBandRate(int sample_rate_hz) {
69   return sample_rate_hz == 8000 ? sample_rate_hz : 16000;
70 }
71 
ValidFullBandRate(int sample_rate_hz)72 constexpr bool ValidFullBandRate(int sample_rate_hz) {
73   return sample_rate_hz == 8000 || sample_rate_hz == 16000 ||
74          sample_rate_hz == 32000 || sample_rate_hz == 48000;
75 }
76 
GetDownSampledBufferSize(size_t down_sampling_factor,size_t num_matched_filters)77 constexpr size_t GetDownSampledBufferSize(size_t down_sampling_factor,
78                                           size_t num_matched_filters) {
79   return kBlockSize / down_sampling_factor *
80          (kMatchedFilterAlignmentShiftSizeSubBlocks * num_matched_filters +
81           kMatchedFilterWindowSizeSubBlocks + 1);
82 }
83 
GetRenderDelayBufferSize(size_t down_sampling_factor,size_t num_matched_filters)84 constexpr size_t GetRenderDelayBufferSize(size_t down_sampling_factor,
85                                           size_t num_matched_filters) {
86   return (3 *
87           GetDownSampledBufferSize(down_sampling_factor, num_matched_filters)) /
88          (4 * kBlockSize / down_sampling_factor);
89 }
90 
91 // Detects what kind of optimizations to use for the code.
92 Aec3Optimization DetectOptimization();
93 
94 static_assert(1 == NumBandsForRate(8000), "Number of bands for 8 kHz");
95 static_assert(1 == NumBandsForRate(16000), "Number of bands for 16 kHz");
96 static_assert(2 == NumBandsForRate(32000), "Number of bands for 32 kHz");
97 static_assert(3 == NumBandsForRate(48000), "Number of bands for 48 kHz");
98 
99 static_assert(8000 == LowestBandRate(8000), "Sample rate of band 0 for 8 kHz");
100 static_assert(16000 == LowestBandRate(16000),
101               "Sample rate of band 0 for 16 kHz");
102 static_assert(16000 == LowestBandRate(32000),
103               "Sample rate of band 0 for 32 kHz");
104 static_assert(16000 == LowestBandRate(48000),
105               "Sample rate of band 0 for 48 kHz");
106 
107 static_assert(ValidFullBandRate(8000),
108               "Test that 8 kHz is a valid sample rate");
109 static_assert(ValidFullBandRate(16000),
110               "Test that 16 kHz is a valid sample rate");
111 static_assert(ValidFullBandRate(32000),
112               "Test that 32 kHz is a valid sample rate");
113 static_assert(ValidFullBandRate(48000),
114               "Test that 48 kHz is a valid sample rate");
115 static_assert(!ValidFullBandRate(8001),
116               "Test that 8001 Hz is not a valid sample rate");
117 
118 }  // namespace webrtc
119 
120 #endif  // MODULES_AUDIO_PROCESSING_AEC3_AEC3_COMMON_H_
121