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