blob: efa2f9ce06447a8de2f2e34879506f580a68abcb [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_DSP_HELPER_H_
12#define MODULES_AUDIO_CODING_NETEQ_DSP_HELPER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <string.h> // Access to size_t.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_coding/neteq/audio_multi_vector.h"
17#include "rtc_base/constructormagic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018
19namespace webrtc {
20
21// This class contains various signal processing functions, all implemented as
22// static methods.
23class DspHelper {
24 public:
25 // Filter coefficients used when downsampling from the indicated sample rates
26 // (8, 16, 32, 48 kHz) to 4 kHz. Coefficients are in Q12.
27 static const int16_t kDownsample8kHzTbl[3];
28 static const int16_t kDownsample16kHzTbl[5];
29 static const int16_t kDownsample32kHzTbl[7];
30 static const int16_t kDownsample48kHzTbl[7];
31
32 // Constants used to mute and unmute over 5 samples. The coefficients are
33 // in Q15.
34 static const int kMuteFactorStart8kHz = 27307;
35 static const int kMuteFactorIncrement8kHz = -5461;
36 static const int kUnmuteFactorStart8kHz = 5461;
37 static const int kUnmuteFactorIncrement8kHz = 5461;
38 static const int kMuteFactorStart16kHz = 29789;
39 static const int kMuteFactorIncrement16kHz = -2979;
40 static const int kUnmuteFactorStart16kHz = 2979;
41 static const int kUnmuteFactorIncrement16kHz = 2979;
42 static const int kMuteFactorStart32kHz = 31208;
43 static const int kMuteFactorIncrement32kHz = -1560;
44 static const int kUnmuteFactorStart32kHz = 1560;
45 static const int kUnmuteFactorIncrement32kHz = 1560;
46 static const int kMuteFactorStart48kHz = 31711;
47 static const int kMuteFactorIncrement48kHz = -1057;
48 static const int kUnmuteFactorStart48kHz = 1057;
49 static const int kUnmuteFactorIncrement48kHz = 1057;
50
51 // Multiplies the signal with a gradually changing factor.
52 // The first sample is multiplied with |factor| (in Q14). For each sample,
53 // |factor| is increased (additive) by the |increment| (in Q20), which can
54 // be negative. Returns the scale factor after the last increment.
55 static int RampSignal(const int16_t* input,
56 size_t length,
57 int factor,
58 int increment,
59 int16_t* output);
60
61 // Same as above, but with the samples of |signal| being modified in-place.
62 static int RampSignal(int16_t* signal,
63 size_t length,
64 int factor,
65 int increment);
66
67 // Same as above, but processes |length| samples from |signal|, starting at
68 // |start_index|.
minyue-webrtc79553cb2016-05-10 19:55:56 +020069 static int RampSignal(AudioVector* signal,
70 size_t start_index,
71 size_t length,
72 int factor,
73 int increment);
74
75 // Same as above, but for an AudioMultiVector.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000076 static int RampSignal(AudioMultiVector* signal,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077 size_t start_index,
78 size_t length,
79 int factor,
80 int increment);
81
82 // Peak detection with parabolic fit. Looks for |num_peaks| maxima in |data|,
83 // having length |data_length| and sample rate multiplier |fs_mult|. The peak
84 // locations and values are written to the arrays |peak_index| and
85 // |peak_value|, respectively. Both arrays must hold at least |num_peaks|
86 // elements.
Yves Gerey665174f2018-06-19 15:03:05 +020087 static void PeakDetection(int16_t* data,
88 size_t data_length,
89 size_t num_peaks,
90 int fs_mult,
91 size_t* peak_index,
92 int16_t* peak_value);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093
94 // Estimates the height and location of a maximum. The three values in the
95 // array |signal_points| are used as basis for a parabolic fit, which is then
96 // used to find the maximum in an interpolated signal. The |signal_points| are
97 // assumed to be from a 4 kHz signal, while the maximum, written to
98 // |peak_index| and |peak_value| is given in the full sample rate, as
99 // indicated by the sample rate multiplier |fs_mult|.
Yves Gerey665174f2018-06-19 15:03:05 +0200100 static void ParabolicFit(int16_t* signal_points,
101 int fs_mult,
102 size_t* peak_index,
103 int16_t* peak_value);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104
105 // Calculates the sum-abs-diff for |signal| when compared to a displaced
106 // version of itself. Returns the displacement lag that results in the minimum
107 // distortion. The resulting distortion is written to |distortion_value|.
108 // The values of |min_lag| and |max_lag| are boundaries for the search.
Yves Gerey665174f2018-06-19 15:03:05 +0200109 static size_t MinDistortion(const int16_t* signal,
110 size_t min_lag,
111 size_t max_lag,
112 size_t length,
113 int32_t* distortion_value);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000114
115 // Mixes |length| samples from |input1| and |input2| together and writes the
116 // result to |output|. The gain for |input1| starts at |mix_factor| (Q14) and
117 // is decreased by |factor_decrement| (Q14) for each sample. The gain for
118 // |input2| is the complement 16384 - mix_factor.
Yves Gerey665174f2018-06-19 15:03:05 +0200119 static void CrossFade(const int16_t* input1,
120 const int16_t* input2,
121 size_t length,
122 int16_t* mix_factor,
123 int16_t factor_decrement,
124 int16_t* output);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000125
126 // Scales |input| with an increasing gain. Applies |factor| (Q14) to the first
127 // sample and increases the gain by |increment| (Q20) for each sample. The
128 // result is written to |output|. |length| samples are processed.
Yves Gerey665174f2018-06-19 15:03:05 +0200129 static void UnmuteSignal(const int16_t* input,
130 size_t length,
131 int16_t* factor,
132 int increment,
133 int16_t* output);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000134
135 // Starts at unity gain and gradually fades out |signal|. For each sample,
136 // the gain is reduced by |mute_slope| (Q14). |length| samples are processed.
Peter Kasting36b7cc32015-06-11 19:57:18 -0700137 static void MuteSignal(int16_t* signal, int mute_slope, size_t length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000138
139 // Downsamples |input| from |sample_rate_hz| to 4 kHz sample rate. The input
140 // has |input_length| samples, and the method will write |output_length|
141 // samples to |output|. Compensates for the phase delay of the downsampling
142 // filters if |compensate_delay| is true. Returns -1 if the input is too short
143 // to produce |output_length| samples, otherwise 0.
Yves Gerey665174f2018-06-19 15:03:05 +0200144 static int DownsampleTo4kHz(const int16_t* input,
145 size_t input_length,
146 size_t output_length,
147 int input_rate_hz,
148 bool compensate_delay,
149 int16_t* output);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000150
151 private:
152 // Table of constants used in method DspHelper::ParabolicFit().
153 static const int16_t kParabolaCoefficients[17][3];
154
henrikg3c089d72015-09-16 05:37:44 -0700155 RTC_DISALLOW_COPY_AND_ASSIGN(DspHelper);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156};
157
158} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200159#endif // MODULES_AUDIO_CODING_NETEQ_DSP_HELPER_H_