blob: d778e50f2406ed1f2e01c916f49f60b85d1790bc [file] [log] [blame]
peahd0263542017-01-03 04:20:34 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AEC3_AEC3_COMMON_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_AEC3_COMMON_H_
peahd0263542017-01-03 04:20:34 -080013
14#include <stddef.h>
15
16namespace webrtc {
17
peah522d71b2017-02-23 05:16:26 -080018#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
peah5d153c72017-05-03 06:45:44 -070026enum class Aec3Optimization { kNone, kSse2, kNeon };
peah522d71b2017-02-23 05:16:26 -080027
peah86afe9d2017-04-06 15:45:32 -070028constexpr int kNumBlocksPerSecond = 250;
29
30constexpr int kMetricsReportingIntervalBlocks = 10 * kNumBlocksPerSecond;
Gustaf Ullberg2723fb12017-11-23 14:46:48 +010031constexpr int kMetricsComputationBlocks = 11;
peahe985b3f2017-02-28 22:08:53 -080032constexpr int kMetricsCollectionBlocks =
33 kMetricsReportingIntervalBlocks - kMetricsComputationBlocks;
34
peahd0263542017-01-03 04:20:34 -080035constexpr size_t kFftLengthBy2 = 64;
36constexpr size_t kFftLengthBy2Plus1 = kFftLengthBy2 + 1;
peah522d71b2017-02-23 05:16:26 -080037constexpr size_t kFftLengthBy2Minus1 = kFftLengthBy2 - 1;
peahd0263542017-01-03 04:20:34 -080038constexpr size_t kFftLength = 2 * kFftLengthBy2;
Per Åhgrenef5d5af2018-07-31 00:03:46 +020039constexpr size_t kFftLengthBy2Log2 = 6;
peahd0263542017-01-03 04:20:34 -080040
Per Åhgren09a718a2017-12-11 22:28:45 +010041constexpr int kMaxAdaptiveFilterLength = 50;
Per Åhgren8ba58612017-12-01 23:01:44 +010042constexpr int kRenderTransferQueueSizeFrames = 100;
peah29103572017-07-11 02:54:02 -070043
peahd0263542017-01-03 04:20:34 -080044constexpr size_t kMaxNumBands = 3;
Per Åhgrenc20a19c2019-11-13 11:12:29 +010045constexpr size_t kFrameSize = 160;
46constexpr size_t kSubFrameLength = kFrameSize / 2;
peahd0263542017-01-03 04:20:34 -080047
48constexpr size_t kBlockSize = kFftLengthBy2;
Per Åhgrenef5d5af2018-07-31 00:03:46 +020049constexpr size_t kBlockSizeLog2 = kFftLengthBy2Log2;
Per Åhgrenc59a5762017-12-11 21:34:19 +010050
peahd0263542017-01-03 04:20:34 -080051constexpr size_t kExtendedBlockSize = 2 * kFftLengthBy2;
peahcf02cf12017-04-05 14:18:07 -070052constexpr size_t kMatchedFilterWindowSizeSubBlocks = 32;
53constexpr size_t kMatchedFilterAlignmentShiftSizeSubBlocks =
54 kMatchedFilterWindowSizeSubBlocks * 3 / 4;
peahcf02cf12017-04-05 14:18:07 -070055
peah2ce640f2017-04-07 03:57:48 -070056// TODO(peah): Integrate this with how it is done inside audio_processing_impl.
peahd0263542017-01-03 04:20:34 -080057constexpr size_t NumBandsForRate(int sample_rate_hz) {
Per Åhgrence202a02019-09-02 17:01:19 +020058 return static_cast<size_t>(sample_rate_hz / 16000);
peahd0263542017-01-03 04:20:34 -080059}
60
peah21920892017-02-08 05:08:56 -080061constexpr bool ValidFullBandRate(int sample_rate_hz) {
Per Åhgrence202a02019-09-02 17:01:19 +020062 return sample_rate_hz == 16000 || sample_rate_hz == 32000 ||
63 sample_rate_hz == 48000;
peah21920892017-02-08 05:08:56 -080064}
65
Per Åhgren09a718a2017-12-11 22:28:45 +010066constexpr int GetTimeDomainLength(int filter_length_blocks) {
67 return filter_length_blocks * kFftLengthBy2;
68}
69
Per Åhgren38e2d952017-11-17 14:54:28 +010070constexpr size_t GetDownSampledBufferSize(size_t down_sampling_factor,
71 size_t num_matched_filters) {
72 return kBlockSize / down_sampling_factor *
73 (kMatchedFilterAlignmentShiftSizeSubBlocks * num_matched_filters +
74 kMatchedFilterWindowSizeSubBlocks + 1);
75}
76
77constexpr size_t GetRenderDelayBufferSize(size_t down_sampling_factor,
Per Åhgren09a718a2017-12-11 22:28:45 +010078 size_t num_matched_filters,
79 size_t filter_length_blocks) {
Per Åhgren8ba58612017-12-01 23:01:44 +010080 return GetDownSampledBufferSize(down_sampling_factor, num_matched_filters) /
Per Åhgrenc59a5762017-12-11 21:34:19 +010081 (kBlockSize / down_sampling_factor) +
Per Åhgren09a718a2017-12-11 22:28:45 +010082 filter_length_blocks + 1;
Per Åhgren38e2d952017-11-17 14:54:28 +010083}
84
peah522d71b2017-02-23 05:16:26 -080085// Detects what kind of optimizations to use for the code.
86Aec3Optimization DetectOptimization();
87
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020088// Computes the log2 of the input in a fast an approximate manner.
89float FastApproxLog2f(const float in);
90
91// Returns dB from a power quantity expressed in log2.
92float Log2TodB(const float in_log2);
93
Per Åhgrenc59a5762017-12-11 21:34:19 +010094static_assert(1 << kBlockSizeLog2 == kBlockSize,
95 "Proper number of shifts for blocksize");
96
Per Åhgrenef5d5af2018-07-31 00:03:46 +020097static_assert(1 << kFftLengthBy2Log2 == kFftLengthBy2,
98 "Proper number of shifts for the fft length");
99
peahd0263542017-01-03 04:20:34 -0800100static_assert(1 == NumBandsForRate(16000), "Number of bands for 16 kHz");
101static_assert(2 == NumBandsForRate(32000), "Number of bands for 32 kHz");
102static_assert(3 == NumBandsForRate(48000), "Number of bands for 48 kHz");
103
peah21920892017-02-08 05:08:56 -0800104static_assert(ValidFullBandRate(16000),
105 "Test that 16 kHz is a valid sample rate");
106static_assert(ValidFullBandRate(32000),
107 "Test that 32 kHz is a valid sample rate");
108static_assert(ValidFullBandRate(48000),
109 "Test that 48 kHz is a valid sample rate");
110static_assert(!ValidFullBandRate(8001),
111 "Test that 8001 Hz is not a valid sample rate");
112
peahd0263542017-01-03 04:20:34 -0800113} // namespace webrtc
114
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200115#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC3_COMMON_H_