blob: ddbea4ca533266e9d63e44f37ed05152fd357371 [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
11#include "webrtc/modules/audio_coding/neteq4/normal.h"
12
13#include <algorithm> // min
14#include <cstring> // memset, memcpy
15
16#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
17#include "webrtc/modules/audio_coding/codecs/cng/include/webrtc_cng.h"
18#include "webrtc/modules/audio_coding/neteq4/audio_multi_vector.h"
19#include "webrtc/modules/audio_coding/neteq4/background_noise.h"
20#include "webrtc/modules/audio_coding/neteq4/decoder_database.h"
21#include "webrtc/modules/audio_coding/neteq4/expand.h"
22#include "webrtc/modules/audio_coding/neteq4/interface/audio_decoder.h"
23
24namespace webrtc {
25
26int Normal::Process(const int16_t* input,
27 size_t length,
28 Modes last_mode,
29 int16_t* external_mute_factor_array,
30 AudioMultiVector<int16_t>* output) {
31 if (length == 0) {
32 // Nothing to process.
33 output->Clear();
34 return length;
35 }
36
37 assert(output->Empty());
38 // Output should be empty at this point.
39 output->PushBackInterleaved(input, length);
40 int16_t* signal = &(*output)[0][0];
41
42 const unsigned fs_mult = fs_hz_ / 8000;
43 assert(fs_mult > 0);
44 // fs_shift = log2(fs_mult), rounded down.
45 // Note that |fs_shift| is not "exact" for 48 kHz.
46 // TODO(hlundin): Investigate this further.
47 const int fs_shift = 30 - WebRtcSpl_NormW32(fs_mult);
48
49 // Check if last RecOut call resulted in an Expand. If so, we have to take
50 // care of some cross-fading and unmuting.
51 if (last_mode == kModeExpand) {
52 // Generate interpolation data using Expand.
53 // First, set Expand parameters to appropriate values.
54 expand_->SetParametersForNormalAfterExpand();
55
56 // Call Expand.
57 AudioMultiVector<int16_t> expanded(output->Channels());
58 expand_->Process(&expanded);
59 expand_->Reset();
60
61 for (size_t channel_ix = 0; channel_ix < output->Channels(); ++channel_ix) {
62 // Adjust muting factor (main muting factor times expand muting factor).
63 external_mute_factor_array[channel_ix] = static_cast<int16_t>(
64 WEBRTC_SPL_MUL_16_16_RSFT(external_mute_factor_array[channel_ix],
65 expand_->MuteFactor(channel_ix), 14));
66
67 int16_t* signal = &(*output)[channel_ix][0];
68 size_t length_per_channel = length / output->Channels();
69 // Find largest absolute value in new data.
70 int16_t decoded_max = WebRtcSpl_MaxAbsValueW16(signal,
71 length_per_channel);
72 // Adjust muting factor if needed (to BGN level).
73 int energy_length = std::min(static_cast<size_t>(fs_mult * 64),
74 length_per_channel);
75 int scaling = 6 + fs_shift
76 - WebRtcSpl_NormW32(decoded_max * decoded_max);
77 scaling = std::max(scaling, 0); // |scaling| should always be >= 0.
78 int32_t energy = WebRtcSpl_DotProductWithScale(signal, signal,
79 energy_length, scaling);
80 energy = energy / (energy_length >> scaling);
81
82 int mute_factor;
83 if ((energy != 0) &&
84 (energy > background_noise_.Energy(channel_ix))) {
85 // Normalize new frame energy to 15 bits.
86 scaling = WebRtcSpl_NormW32(energy) - 16;
87 // We want background_noise_.energy() / energy in Q14.
88 int32_t bgn_energy =
89 background_noise_.Energy(channel_ix) << (scaling+14);
90 int16_t energy_scaled = energy << scaling;
91 int16_t ratio = WebRtcSpl_DivW32W16(bgn_energy, energy_scaled);
92 mute_factor = WebRtcSpl_SqrtFloor(static_cast<int32_t>(ratio) << 14);
93 } else {
94 mute_factor = 16384; // 1.0 in Q14.
95 }
96 if (mute_factor > external_mute_factor_array[channel_ix]) {
97 external_mute_factor_array[channel_ix] = std::min(mute_factor, 16384);
98 }
99
100 // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14).
101 int16_t increment = 64 / fs_mult;
102 for (size_t i = 0; i < length_per_channel; i++) {
103 // Scale with mute factor.
104 assert(channel_ix < output->Channels());
105 assert(i < output->Size());
106 int32_t scaled_signal = (*output)[channel_ix][i] *
107 external_mute_factor_array[channel_ix];
108 // Shift 14 with proper rounding.
109 (*output)[channel_ix][i] = (scaled_signal + 8192) >> 14;
110 // Increase mute_factor towards 16384.
111 external_mute_factor_array[channel_ix] =
112 std::min(external_mute_factor_array[channel_ix] + increment, 16384);
113 }
114
115 // Interpolate the expanded data into the new vector.
116 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
117 assert(fs_shift < 3); // Will always be 0, 1, or, 2.
118 increment = 4 >> fs_shift;
119 int fraction = increment;
120 for (size_t i = 0; i < 8 * fs_mult; i++) {
121 // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8
122 // now for legacy bit-exactness.
123 assert(channel_ix < output->Channels());
124 assert(i < output->Size());
125 (*output)[channel_ix][i] =
126 (fraction * (*output)[channel_ix][i] +
127 (32 - fraction) * expanded[channel_ix][i] + 8) >> 5;
128 fraction += increment;
129 }
130 }
131 } else if (last_mode == kModeRfc3389Cng) {
132 assert(output->Channels() == 1); // Not adapted for multi-channel yet.
133 static const int kCngLength = 32;
134 int16_t cng_output[kCngLength];
135 // Reset mute factor and start up fresh.
136 external_mute_factor_array[0] = 16384;
137 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
138
139 if (cng_decoder) {
140 CNG_dec_inst* cng_inst = static_cast<CNG_dec_inst*>(cng_decoder->state());
141 // Generate long enough for 32kHz.
142 if (WebRtcCng_Generate(cng_inst, cng_output, kCngLength, 0) < 0) {
143 // Error returned; set return vector to all zeros.
144 memset(cng_output, 0, sizeof(cng_output));
145 }
146 } else {
147 // If no CNG instance is defined, just copy from the decoded data.
148 // (This will result in interpolating the decoded with itself.)
149 memcpy(cng_output, signal, fs_mult * 8 * sizeof(int16_t));
150 }
151 // Interpolate the CNG into the new vector.
152 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
153 assert(fs_shift < 3); // Will always be 0, 1, or, 2.
154 int16_t increment = 4 >> fs_shift;
155 int16_t fraction = increment;
156 for (size_t i = 0; i < 8 * fs_mult; i++) {
157 // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8 now
158 // for legacy bit-exactness.
159 signal[i] =
160 (fraction * signal[i] + (32 - fraction) * cng_output[i] + 8) >> 5;
161 fraction += increment;
162 }
163 } else if (external_mute_factor_array[0] < 16384) {
164 // Previous was neither of Expand, FadeToBGN or RFC3389_CNG, but we are
165 // still ramping up from previous muting.
166 // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14).
167 int16_t increment = 64 / fs_mult;
168 size_t length_per_channel = length / output->Channels();
169 for (size_t i = 0; i < length_per_channel; i++) {
170 for (size_t channel_ix = 0; channel_ix < output->Channels();
171 ++channel_ix) {
172 // Scale with mute factor.
173 assert(channel_ix < output->Channels());
174 assert(i < output->Size());
175 int32_t scaled_signal = (*output)[channel_ix][i] *
176 external_mute_factor_array[channel_ix];
177 // Shift 14 with proper rounding.
178 (*output)[channel_ix][i] = (scaled_signal + 8192) >> 14;
179 // Increase mute_factor towards 16384.
180 external_mute_factor_array[channel_ix] =
181 std::min(16384, external_mute_factor_array[channel_ix] + increment);
182 }
183 }
184 }
185
186 return length;
187}
188
189} // namespace webrtc