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