tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

normal.cc (8118B)


      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 "modules/audio_coding/neteq/normal.h"
     12 
     13 #include <algorithm>  // min
     14 #include <cstdint>
     15 #include <cstring>  // memset, memcpy
     16 #include <memory>
     17 
     18 #include "api/array_view.h"
     19 #include "api/neteq/neteq.h"
     20 #include "common_audio/signal_processing/dot_product_with_scale.h"
     21 #include "common_audio/signal_processing/include/signal_processing_library.h"
     22 #include "common_audio/signal_processing/include/spl_inl.h"
     23 #include "modules/audio_coding/codecs/cng/webrtc_cng.h"
     24 #include "modules/audio_coding/neteq/audio_multi_vector.h"
     25 #include "modules/audio_coding/neteq/background_noise.h"
     26 #include "modules/audio_coding/neteq/decoder_database.h"
     27 #include "modules/audio_coding/neteq/expand.h"
     28 #include "rtc_base/checks.h"
     29 
     30 namespace webrtc {
     31 
     32 int Normal::Process(const int16_t* input,
     33                    size_t length,
     34                    NetEq::Mode last_mode,
     35                    AudioMultiVector* output) {
     36  if (length == 0) {
     37    // Nothing to process.
     38    output->Clear();
     39    return static_cast<int>(length);
     40  }
     41 
     42  RTC_DCHECK(output->Empty());
     43  // Output should be empty at this point.
     44  if (length % output->Channels() != 0) {
     45    // The length does not match the number of channels.
     46    output->Clear();
     47    return 0;
     48  }
     49  output->PushBackInterleaved(ArrayView<const int16_t>(input, length));
     50 
     51  const int fs_mult = fs_hz_ / 8000;
     52  RTC_DCHECK_GT(fs_mult, 0);
     53  // fs_shift = log2(fs_mult), rounded down.
     54  // Note that `fs_shift` is not "exact" for 48 kHz.
     55  // TODO(hlundin): Investigate this further.
     56  const int fs_shift = 30 - WebRtcSpl_NormW32(fs_mult);
     57 
     58  // If last call resulted in a CodedPlc we don't need to do cross-fading but we
     59  // need to report the end of the interruption once we are back to normal
     60  // operation.
     61  if (last_mode == NetEq::Mode::kCodecPlc) {
     62    statistics_->EndExpandEvent(fs_hz_);
     63  }
     64 
     65  // Check if last RecOut call resulted in an Expand. If so, we have to take
     66  // care of some cross-fading and unmuting.
     67  if (last_mode == NetEq::Mode::kExpand) {
     68    // Generate interpolation data using Expand.
     69    // First, set Expand parameters to appropriate values.
     70    expand_->SetParametersForNormalAfterExpand();
     71 
     72    // Call Expand.
     73    AudioMultiVector expanded(output->Channels());
     74    expand_->Process(&expanded);
     75    expand_->Reset();
     76 
     77    size_t length_per_channel = length / output->Channels();
     78    std::unique_ptr<int16_t[]> signal(new int16_t[length_per_channel]);
     79    for (size_t channel_ix = 0; channel_ix < output->Channels(); ++channel_ix) {
     80      // Set muting factor to the same as expand muting factor.
     81      int16_t mute_factor = expand_->MuteFactor(channel_ix);
     82 
     83      (*output)[channel_ix].CopyTo(length_per_channel, 0, signal.get());
     84 
     85      // Find largest absolute value in new data.
     86      int16_t decoded_max =
     87          WebRtcSpl_MaxAbsValueW16(signal.get(), length_per_channel);
     88      // Adjust muting factor if needed (to BGN level).
     89      size_t energy_length =
     90          std::min(static_cast<size_t>(fs_mult * 64), length_per_channel);
     91      int scaling = 6 + fs_shift - WebRtcSpl_NormW32(decoded_max * decoded_max);
     92      scaling = std::max(scaling, 0);  // `scaling` should always be >= 0.
     93      int32_t energy = WebRtcSpl_DotProductWithScale(signal.get(), signal.get(),
     94                                                     energy_length, scaling);
     95      int32_t scaled_energy_length =
     96          static_cast<int32_t>(energy_length >> scaling);
     97      if (scaled_energy_length > 0) {
     98        energy = energy / scaled_energy_length;
     99      } else {
    100        energy = 0;
    101      }
    102 
    103      int local_mute_factor = 16384;  // 1.0 in Q14.
    104      if ((energy != 0) && (energy > background_noise_.Energy(channel_ix))) {
    105        // Normalize new frame energy to 15 bits.
    106        scaling = WebRtcSpl_NormW32(energy) - 16;
    107        // We want background_noise_.energy() / energy in Q14.
    108        int32_t bgn_energy = WEBRTC_SPL_SHIFT_W32(
    109            background_noise_.Energy(channel_ix), scaling + 14);
    110        int16_t energy_scaled =
    111            static_cast<int16_t>(WEBRTC_SPL_SHIFT_W32(energy, scaling));
    112        int32_t ratio = WebRtcSpl_DivW32W16(bgn_energy, energy_scaled);
    113        local_mute_factor =
    114            std::min(local_mute_factor, WebRtcSpl_SqrtFloor(ratio << 14));
    115      }
    116      mute_factor = std::max<int16_t>(mute_factor, local_mute_factor);
    117      RTC_DCHECK_LE(mute_factor, 16384);
    118      RTC_DCHECK_GE(mute_factor, 0);
    119 
    120      // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14),
    121      // or as fast as it takes to come back to full gain within the frame
    122      // length.
    123      const int back_to_fullscale_inc =
    124          static_cast<int>((16384 - mute_factor) / length_per_channel);
    125      const int increment = std::max(64 / fs_mult, back_to_fullscale_inc);
    126      for (size_t i = 0; i < length_per_channel; i++) {
    127        // Scale with mute factor.
    128        RTC_DCHECK_LT(channel_ix, output->Channels());
    129        RTC_DCHECK_LT(i, output->Size());
    130        int32_t scaled_signal = (*output)[channel_ix][i] * mute_factor;
    131        // Shift 14 with proper rounding.
    132        (*output)[channel_ix][i] =
    133            static_cast<int16_t>((scaled_signal + 8192) >> 14);
    134        // Increase mute_factor towards 16384.
    135        mute_factor =
    136            static_cast<int16_t>(std::min(mute_factor + increment, 16384));
    137      }
    138 
    139      // Interpolate the expanded data into the new vector.
    140      // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
    141      size_t win_length = samples_per_ms_;
    142      int16_t win_slope_Q14 = default_win_slope_Q14_;
    143      RTC_DCHECK_LT(channel_ix, output->Channels());
    144      if (win_length > output->Size()) {
    145        win_length = output->Size();
    146        win_slope_Q14 = (1 << 14) / static_cast<int16_t>(win_length);
    147      }
    148      int16_t win_up_Q14 = 0;
    149      for (size_t i = 0; i < win_length; i++) {
    150        win_up_Q14 += win_slope_Q14;
    151        (*output)[channel_ix][i] =
    152            (win_up_Q14 * (*output)[channel_ix][i] +
    153             ((1 << 14) - win_up_Q14) * expanded[channel_ix][i] + (1 << 13)) >>
    154            14;
    155      }
    156      RTC_DCHECK_GT(win_up_Q14,
    157                    (1 << 14) - 32);  // Worst case rouding is a length of 34
    158    }
    159  } else if (last_mode == NetEq::Mode::kRfc3389Cng) {
    160    RTC_DCHECK_EQ(output->Channels(), 1);  // Not adapted for multi-channel yet.
    161    static const size_t kCngLength = 48;
    162    RTC_DCHECK_LE(8 * fs_mult, kCngLength);
    163    int16_t cng_output[kCngLength];
    164    ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
    165 
    166    if (cng_decoder) {
    167      // Generate long enough for 48kHz.
    168      if (!cng_decoder->Generate(cng_output, false)) {
    169        // Error returned; set return vector to all zeros.
    170        memset(cng_output, 0, sizeof(cng_output));
    171      }
    172    } else {
    173      // If no CNG instance is defined, just copy from the decoded data.
    174      // (This will result in interpolating the decoded with itself.)
    175      (*output)[0].CopyTo(fs_mult * 8, 0, cng_output);
    176    }
    177    // Interpolate the CNG into the new vector.
    178    // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
    179    size_t win_length = samples_per_ms_;
    180    int16_t win_slope_Q14 = default_win_slope_Q14_;
    181    if (win_length > kCngLength) {
    182      win_length = kCngLength;
    183      win_slope_Q14 = (1 << 14) / static_cast<int16_t>(win_length);
    184    }
    185    int16_t win_up_Q14 = 0;
    186    for (size_t i = 0; i < win_length; i++) {
    187      win_up_Q14 += win_slope_Q14;
    188      (*output)[0][i] =
    189          (win_up_Q14 * (*output)[0][i] +
    190           ((1 << 14) - win_up_Q14) * cng_output[i] + (1 << 13)) >>
    191          14;
    192    }
    193    RTC_DCHECK_GT(win_up_Q14,
    194                  (1 << 14) - 32);  // Worst case rouding is a length of 34
    195  }
    196 
    197  return static_cast<int>(length);
    198 }
    199 
    200 }  // namespace webrtc