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 
16 namespace webrtc {
17 
18 #ifdef _MSC_VER /* visual c++ */
19 #define ALIGN16_BEG __declspec(align(16))
20 #define ALIGN16_END
21 #else /* gcc or icc */
22 #define ALIGN16_BEG
23 #define ALIGN16_END __attribute__((aligned(16)))
24 #endif
25 
26 enum class Aec3Optimization { kNone, kSse2, kNeon };
27 
28 constexpr int kNumBlocksPerSecond = 250;
29 
30 constexpr int kMetricsReportingIntervalBlocks = 10 * kNumBlocksPerSecond;
31 constexpr int kMetricsComputationBlocks = 7;
32 constexpr int kMetricsCollectionBlocks =
33     kMetricsReportingIntervalBlocks - kMetricsComputationBlocks;
34 
35 constexpr size_t kFftLengthBy2 = 64;
36 constexpr size_t kFftLengthBy2Plus1 = kFftLengthBy2 + 1;
37 constexpr size_t kFftLengthBy2Minus1 = kFftLengthBy2 - 1;
38 constexpr size_t kFftLength = 2 * kFftLengthBy2;
39 constexpr size_t kFftLengthBy2Log2 = 6;
40 
41 constexpr int kRenderTransferQueueSizeFrames = 100;
42 
43 constexpr size_t kMaxNumBands = 3;
44 constexpr size_t kFrameSize = 160;
45 constexpr size_t kSubFrameLength = kFrameSize / 2;
46 
47 constexpr size_t kBlockSize = kFftLengthBy2;
48 constexpr size_t kBlockSizeLog2 = kFftLengthBy2Log2;
49 
50 constexpr size_t kExtendedBlockSize = 2 * kFftLengthBy2;
51 constexpr size_t kMatchedFilterWindowSizeSubBlocks = 32;
52 constexpr size_t kMatchedFilterAlignmentShiftSizeSubBlocks =
53     kMatchedFilterWindowSizeSubBlocks * 3 / 4;
54 
55 // TODO(peah): Integrate this with how it is done inside audio_processing_impl.
NumBandsForRate(int sample_rate_hz)56 constexpr size_t NumBandsForRate(int sample_rate_hz) {
57   return static_cast<size_t>(sample_rate_hz / 16000);
58 }
59 
ValidFullBandRate(int sample_rate_hz)60 constexpr bool ValidFullBandRate(int sample_rate_hz) {
61   return sample_rate_hz == 16000 || sample_rate_hz == 32000 ||
62          sample_rate_hz == 48000;
63 }
64 
GetTimeDomainLength(int filter_length_blocks)65 constexpr int GetTimeDomainLength(int filter_length_blocks) {
66   return filter_length_blocks * kFftLengthBy2;
67 }
68 
GetDownSampledBufferSize(size_t down_sampling_factor,size_t num_matched_filters)69 constexpr size_t GetDownSampledBufferSize(size_t down_sampling_factor,
70                                           size_t num_matched_filters) {
71   return kBlockSize / down_sampling_factor *
72          (kMatchedFilterAlignmentShiftSizeSubBlocks * num_matched_filters +
73           kMatchedFilterWindowSizeSubBlocks + 1);
74 }
75 
GetRenderDelayBufferSize(size_t down_sampling_factor,size_t num_matched_filters,size_t filter_length_blocks)76 constexpr size_t GetRenderDelayBufferSize(size_t down_sampling_factor,
77                                           size_t num_matched_filters,
78                                           size_t filter_length_blocks) {
79   return GetDownSampledBufferSize(down_sampling_factor, num_matched_filters) /
80              (kBlockSize / down_sampling_factor) +
81          filter_length_blocks + 1;
82 }
83 
84 // Detects what kind of optimizations to use for the code.
85 Aec3Optimization DetectOptimization();
86 
87 // Computes the log2 of the input in a fast an approximate manner.
88 float FastApproxLog2f(const float in);
89 
90 // Returns dB from a power quantity expressed in log2.
91 float Log2TodB(const float in_log2);
92 
93 static_assert(1 << kBlockSizeLog2 == kBlockSize,
94               "Proper number of shifts for blocksize");
95 
96 static_assert(1 << kFftLengthBy2Log2 == kFftLengthBy2,
97               "Proper number of shifts for the fft length");
98 
99 static_assert(1 == NumBandsForRate(16000), "Number of bands for 16 kHz");
100 static_assert(2 == NumBandsForRate(32000), "Number of bands for 32 kHz");
101 static_assert(3 == NumBandsForRate(48000), "Number of bands for 48 kHz");
102 
103 static_assert(ValidFullBandRate(16000),
104               "Test that 16 kHz is a valid sample rate");
105 static_assert(ValidFullBandRate(32000),
106               "Test that 32 kHz is a valid sample rate");
107 static_assert(ValidFullBandRate(48000),
108               "Test that 48 kHz is a valid sample rate");
109 static_assert(!ValidFullBandRate(8001),
110               "Test that 8001 Hz is not a valid sample rate");
111 
112 }  // namespace webrtc
113 
114 #endif  // MODULES_AUDIO_PROCESSING_AEC3_AEC3_COMMON_H_
115