aec3_common.h 4.1 KB

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