henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_DSP_HELPER_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_DSP_HELPER_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | #include <string.h> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_coding/neteq/audio_multi_vector.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include "modules/audio_coding/neteq/audio_vector.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // This class contains various signal processing functions, all implemented as |
| 23 | // static methods. |
| 24 | class DspHelper { |
| 25 | public: |
| 26 | // Filter coefficients used when downsampling from the indicated sample rates |
| 27 | // (8, 16, 32, 48 kHz) to 4 kHz. Coefficients are in Q12. |
| 28 | static const int16_t kDownsample8kHzTbl[3]; |
| 29 | static const int16_t kDownsample16kHzTbl[5]; |
| 30 | static const int16_t kDownsample32kHzTbl[7]; |
| 31 | static const int16_t kDownsample48kHzTbl[7]; |
| 32 | |
| 33 | // Constants used to mute and unmute over 5 samples. The coefficients are |
| 34 | // in Q15. |
| 35 | static const int kMuteFactorStart8kHz = 27307; |
| 36 | static const int kMuteFactorIncrement8kHz = -5461; |
| 37 | static const int kUnmuteFactorStart8kHz = 5461; |
| 38 | static const int kUnmuteFactorIncrement8kHz = 5461; |
| 39 | static const int kMuteFactorStart16kHz = 29789; |
| 40 | static const int kMuteFactorIncrement16kHz = -2979; |
| 41 | static const int kUnmuteFactorStart16kHz = 2979; |
| 42 | static const int kUnmuteFactorIncrement16kHz = 2979; |
| 43 | static const int kMuteFactorStart32kHz = 31208; |
| 44 | static const int kMuteFactorIncrement32kHz = -1560; |
| 45 | static const int kUnmuteFactorStart32kHz = 1560; |
| 46 | static const int kUnmuteFactorIncrement32kHz = 1560; |
| 47 | static const int kMuteFactorStart48kHz = 31711; |
| 48 | static const int kMuteFactorIncrement48kHz = -1057; |
| 49 | static const int kUnmuteFactorStart48kHz = 1057; |
| 50 | static const int kUnmuteFactorIncrement48kHz = 1057; |
| 51 | |
| 52 | // Multiplies the signal with a gradually changing factor. |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 53 | // The first sample is multiplied with `factor` (in Q14). For each sample, |
| 54 | // `factor` is increased (additive) by the `increment` (in Q20), which can |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 55 | // be negative. Returns the scale factor after the last increment. |
| 56 | static int RampSignal(const int16_t* input, |
| 57 | size_t length, |
| 58 | int factor, |
| 59 | int increment, |
| 60 | int16_t* output); |
| 61 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 62 | // Same as above, but with the samples of `signal` being modified in-place. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 63 | static int RampSignal(int16_t* signal, |
| 64 | size_t length, |
| 65 | int factor, |
| 66 | int increment); |
| 67 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 68 | // Same as above, but processes `length` samples from `signal`, starting at |
| 69 | // `start_index`. |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 70 | static int RampSignal(AudioVector* signal, |
| 71 | size_t start_index, |
| 72 | size_t length, |
| 73 | int factor, |
| 74 | int increment); |
| 75 | |
| 76 | // Same as above, but for an AudioMultiVector. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 77 | static int RampSignal(AudioMultiVector* signal, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 78 | size_t start_index, |
| 79 | size_t length, |
| 80 | int factor, |
| 81 | int increment); |
| 82 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 83 | // Peak detection with parabolic fit. Looks for `num_peaks` maxima in `data`, |
| 84 | // having length `data_length` and sample rate multiplier `fs_mult`. The peak |
| 85 | // locations and values are written to the arrays `peak_index` and |
| 86 | // `peak_value`, respectively. Both arrays must hold at least `num_peaks` |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 87 | // elements. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 88 | static void PeakDetection(int16_t* data, |
| 89 | size_t data_length, |
| 90 | size_t num_peaks, |
| 91 | int fs_mult, |
| 92 | size_t* peak_index, |
| 93 | int16_t* peak_value); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 94 | |
| 95 | // Estimates the height and location of a maximum. The three values in the |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 96 | // array `signal_points` are used as basis for a parabolic fit, which is then |
| 97 | // used to find the maximum in an interpolated signal. The `signal_points` are |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 98 | // assumed to be from a 4 kHz signal, while the maximum, written to |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 99 | // `peak_index` and `peak_value` is given in the full sample rate, as |
| 100 | // indicated by the sample rate multiplier `fs_mult`. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 101 | static void ParabolicFit(int16_t* signal_points, |
| 102 | int fs_mult, |
| 103 | size_t* peak_index, |
| 104 | int16_t* peak_value); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 105 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 106 | // Calculates the sum-abs-diff for `signal` when compared to a displaced |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 107 | // version of itself. Returns the displacement lag that results in the minimum |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 108 | // distortion. The resulting distortion is written to `distortion_value`. |
| 109 | // The values of `min_lag` and `max_lag` are boundaries for the search. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 110 | static size_t MinDistortion(const int16_t* signal, |
| 111 | size_t min_lag, |
| 112 | size_t max_lag, |
| 113 | size_t length, |
| 114 | int32_t* distortion_value); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 115 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 116 | // Mixes `length` samples from `input1` and `input2` together and writes the |
| 117 | // result to `output`. The gain for `input1` starts at `mix_factor` (Q14) and |
| 118 | // is decreased by `factor_decrement` (Q14) for each sample. The gain for |
| 119 | // `input2` is the complement 16384 - mix_factor. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | static void CrossFade(const int16_t* input1, |
| 121 | const int16_t* input2, |
| 122 | size_t length, |
| 123 | int16_t* mix_factor, |
| 124 | int16_t factor_decrement, |
| 125 | int16_t* output); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 126 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 127 | // Scales `input` with an increasing gain. Applies `factor` (Q14) to the first |
| 128 | // sample and increases the gain by `increment` (Q20) for each sample. The |
| 129 | // result is written to `output`. `length` samples are processed. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 130 | static void UnmuteSignal(const int16_t* input, |
| 131 | size_t length, |
| 132 | int16_t* factor, |
| 133 | int increment, |
| 134 | int16_t* output); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 135 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 136 | // Starts at unity gain and gradually fades out `signal`. For each sample, |
| 137 | // the gain is reduced by `mute_slope` (Q14). `length` samples are processed. |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 138 | static void MuteSignal(int16_t* signal, int mute_slope, size_t length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 139 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 140 | // Downsamples `input` from `sample_rate_hz` to 4 kHz sample rate. The input |
| 141 | // has `input_length` samples, and the method will write `output_length` |
| 142 | // samples to `output`. Compensates for the phase delay of the downsampling |
| 143 | // filters if `compensate_delay` is true. Returns -1 if the input is too short |
| 144 | // to produce `output_length` samples, otherwise 0. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 145 | static int DownsampleTo4kHz(const int16_t* input, |
| 146 | size_t input_length, |
| 147 | size_t output_length, |
| 148 | int input_rate_hz, |
| 149 | bool compensate_delay, |
| 150 | int16_t* output); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 151 | |
Byoungchan Lee | 604fd2f | 2022-01-21 09:49:39 +0900 | [diff] [blame] | 152 | DspHelper(const DspHelper&) = delete; |
| 153 | DspHelper& operator=(const DspHelper&) = delete; |
| 154 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 155 | private: |
| 156 | // Table of constants used in method DspHelper::ParabolicFit(). |
| 157 | static const int16_t kParabolaCoefficients[17][3]; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 161 | #endif // MODULES_AUDIO_CODING_NETEQ_DSP_HELPER_H_ |