19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*
29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *
49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org */
109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
11e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/merge.h"
129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <assert.h>
143f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org#include <string.h>  // memmove, memcpy, memset, size_t
159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <algorithm>  // min, max
179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
19e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
20e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/dsp_helper.h"
21e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/expand.h"
22e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
23c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org#include "webrtc/system_wrappers/interface/scoped_ptr.h"
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace webrtc {
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
27045e45efeca8776975254550137ec65268aadb54turaj@webrtc.orgint Merge::Process(int16_t* input, size_t input_length,
289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                   int16_t* external_mute_factor_array,
290e9c399746f45ceaf46f12b11ba93c09cca0c2bbhenrik.lundin@webrtc.org                   AudioMultiVector* output) {
309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // TODO(hlundin): Change to an enumerator and skip assert.
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  assert(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ ==  32000 ||
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org         fs_hz_ == 48000);
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  assert(fs_hz_ <= kMaxSampleRate);  // Should not be possible.
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int old_length;
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int expand_period;
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Get expansion data to overlap and mix with.
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int expanded_length = GetExpandedSignal(&old_length, &expand_period);
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Transfer input signal to an AudioMultiVector.
410e9c399746f45ceaf46f12b11ba93c09cca0c2bbhenrik.lundin@webrtc.org  AudioMultiVector input_vector(num_channels_);
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  input_vector.PushBackInterleaved(input, input_length);
439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t input_length_per_channel = input_vector.Size();
449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  assert(input_length_per_channel == input_length / num_channels_);
459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int16_t best_correlation_index = 0;
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t output_length = 0;
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (size_t channel = 0; channel < num_channels_; ++channel) {
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t* input_channel = &input_vector[channel][0];
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t* expanded_channel = &expanded_[channel][0];
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t expanded_max, input_max;
53045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org    int16_t new_mute_factor = SignalScaling(
54045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org        input_channel, static_cast<int>(input_length_per_channel),
55045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org        expanded_channel, &expanded_max, &input_max);
569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Adjust muting factor (product of "main" muting factor and expand muting
589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // factor).
599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t* external_mute_factor = &external_mute_factor_array[channel];
609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    *external_mute_factor =
619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        (*external_mute_factor * expand_->MuteFactor(channel)) >> 14;
629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Update |external_mute_factor| if it is lower than |new_mute_factor|.
649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (new_mute_factor > *external_mute_factor) {
659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      *external_mute_factor = std::min(new_mute_factor,
669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                       static_cast<int16_t>(16384));
679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (channel == 0) {
709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // Downsample, correlate, and find strongest correlation period for the
719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // master (i.e., first) channel only.
729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // Downsample to 4kHz sample rate.
73045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org      Downsample(input_channel, static_cast<int>(input_length_per_channel),
74045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org                 expanded_channel, expanded_length);
759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // Calculate the lag of the strongest correlation period.
77045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org      best_correlation_index = CorrelateAndPeakSearch(
78045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org          expanded_max, input_max, old_length,
79045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org          static_cast<int>(input_length_per_channel), expand_period);
809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    static const int kTempDataSize = 3600;
839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t temp_data[kTempDataSize];  // TODO(hlundin) Remove this.
849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t* decoded_output = temp_data + best_correlation_index;
859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Mute the new decoded data if needed (and unmute it linearly).
879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // This is the overlapping part of expanded_signal.
889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int interpolation_length = std::min(
899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        kMaxCorrelationLength * fs_mult_,
909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        expanded_length - best_correlation_index);
919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    interpolation_length = std::min(interpolation_length,
929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                    static_cast<int>(input_length_per_channel));
939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (*external_mute_factor < 16384) {
949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // Set a suitable muting slope (Q20). 0.004 for NB, 0.002 for WB,
959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // and so on.
969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      int increment = 4194 / fs_mult_;
979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      *external_mute_factor = DspHelper::RampSignal(input_channel,
989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                                    interpolation_length,
999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                                    *external_mute_factor,
1009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                                    increment);
1019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      DspHelper::UnmuteSignal(&input_channel[interpolation_length],
1029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                              input_length_per_channel - interpolation_length,
1039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                              external_mute_factor, increment,
1049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                              &decoded_output[interpolation_length]);
1059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    } else {
1069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // No muting needed.
1079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      memmove(
1089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org          &decoded_output[interpolation_length],
1099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org          &input_channel[interpolation_length],
1109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org          sizeof(int16_t) * (input_length_per_channel - interpolation_length));
1119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
1129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Do overlap and mix linearly.
1149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int increment = 16384 / (interpolation_length + 1);  // In Q14.
1159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t mute_factor = 16384 - increment;
1169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    memmove(temp_data, expanded_channel,
1179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            sizeof(int16_t) * best_correlation_index);
1189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    DspHelper::CrossFade(&expanded_channel[best_correlation_index],
1199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                         input_channel, interpolation_length,
1209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                         &mute_factor, increment, decoded_output);
1219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    output_length = best_correlation_index + input_length_per_channel;
1239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (channel == 0) {
1249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      assert(output->Empty());  // Output should be empty at this point.
1259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      output->AssertSize(output_length);
1269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    } else {
1279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      assert(output->Size() == output_length);
1289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
1299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    memcpy(&(*output)[channel][0], temp_data,
1309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org           sizeof(temp_data[0]) * output_length);
1319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
1329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Copy back the first part of the data to |sync_buffer_| and remove it from
1349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // |output|.
1359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  sync_buffer_->ReplaceAtIndex(*output, old_length, sync_buffer_->next_index());
1369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  output->PopFront(old_length);
1379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Return new added length. |old_length| samples were borrowed from
1399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // |sync_buffer_|.
140045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org  return static_cast<int>(output_length) - old_length;
1419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
1429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgint Merge::GetExpandedSignal(int* old_length, int* expand_period) {
1449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check how much data that is left since earlier.
145045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org  *old_length = static_cast<int>(sync_buffer_->FutureLength());
1469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Should never be less than overlap_length.
1479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  assert(*old_length >= static_cast<int>(expand_->overlap_length()));
1489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Generate data to merge the overlap with using expand.
1499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  expand_->SetParametersForMergeAfterExpand();
1509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (*old_length >= 210 * kMaxSampleRate / 8000) {
1529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // TODO(hlundin): Write test case for this.
1539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // The number of samples available in the sync buffer is more than what fits
1549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // in expanded_signal. Keep the first 210 * kMaxSampleRate / 8000 samples,
1559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // but shift them towards the end of the buffer. This is ok, since all of
1569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // the buffer will be expand data anyway, so as long as the beginning is
1579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // left untouched, we're fine.
1589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t length_diff = *old_length - 210 * kMaxSampleRate / 8000;
1599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    sync_buffer_->InsertZerosAtIndex(length_diff, sync_buffer_->next_index());
1609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    *old_length = 210 * kMaxSampleRate / 8000;
1619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // This is the truncated length.
1629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
1639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // This assert should always be true thanks to the if statement above.
1649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  assert(210 * kMaxSampleRate / 8000 - *old_length >= 0);
1659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1660e9c399746f45ceaf46f12b11ba93c09cca0c2bbhenrik.lundin@webrtc.org  AudioMultiVector expanded_temp(num_channels_);
1679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  expand_->Process(&expanded_temp);
168045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org  *expand_period = static_cast<int>(expanded_temp.Size());  // Samples per
169045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org                                                            // channel.
1709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  expanded_.Clear();
1729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Copy what is left since earlier into the expanded vector.
1739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  expanded_.PushBackFromIndex(*sync_buffer_, sync_buffer_->next_index());
1749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  assert(expanded_.Size() == static_cast<size_t>(*old_length));
1759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  assert(expanded_temp.Size() > 0);
1769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Do "ugly" copy and paste from the expanded in order to generate more data
1779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // to correlate (but not interpolate) with.
1789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int required_length = (120 + 80 + 2) * fs_mult_;
1799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (expanded_.Size() < static_cast<size_t>(required_length)) {
1809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    while (expanded_.Size() < static_cast<size_t>(required_length)) {
1819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // Append one more pitch period each time.
1829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      expanded_.PushBack(expanded_temp);
1839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
1849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Trim the length to exactly |required_length|.
1859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    expanded_.PopBack(expanded_.Size() - required_length);
1869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
1879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  assert(expanded_.Size() >= static_cast<size_t>(required_length));
1889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  return required_length;
1899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
1909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgint16_t Merge::SignalScaling(const int16_t* input, int input_length,
1929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             const int16_t* expanded_signal,
1939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             int16_t* expanded_max, int16_t* input_max) const {
1949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Adjust muting factor if new vector is more or less of the BGN energy.
1959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int mod_input_length = std::min(64 * fs_mult_, input_length);
1969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  *expanded_max = WebRtcSpl_MaxAbsValueW16(expanded_signal, mod_input_length);
1979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  *input_max = WebRtcSpl_MaxAbsValueW16(input, mod_input_length);
1989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Calculate energy of expanded signal.
2009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // |log_fs_mult| is log2(fs_mult_), but is not exact for 48000 Hz.
2019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int log_fs_mult = 30 - WebRtcSpl_NormW32(fs_mult_);
2029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int expanded_shift = 6 + log_fs_mult
2039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      - WebRtcSpl_NormW32(*expanded_max * *expanded_max);
2049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  expanded_shift = std::max(expanded_shift, 0);
2059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int32_t energy_expanded = WebRtcSpl_DotProductWithScale(expanded_signal,
2069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                                          expanded_signal,
2079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                                          mod_input_length,
2089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                                          expanded_shift);
2099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Calculate energy of input signal.
2119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int input_shift = 6 + log_fs_mult -
2129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      WebRtcSpl_NormW32(*input_max * *input_max);
2139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  input_shift = std::max(input_shift, 0);
2149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int32_t energy_input = WebRtcSpl_DotProductWithScale(input, input,
2159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                                       mod_input_length,
2169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                                       input_shift);
2179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Align to the same Q-domain.
2199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (input_shift > expanded_shift) {
2209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    energy_expanded = energy_expanded >> (input_shift - expanded_shift);
2219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  } else {
2229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    energy_input = energy_input >> (expanded_shift - input_shift);
2239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
2249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Calculate muting factor to use for new frame.
2269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int16_t mute_factor;
2279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (energy_input > energy_expanded) {
2289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Normalize |energy_input| to 14 bits.
2299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t temp_shift = WebRtcSpl_NormW32(energy_input) - 17;
2309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    energy_input = WEBRTC_SPL_SHIFT_W32(energy_input, temp_shift);
2319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Put |energy_expanded| in a domain 14 higher, so that
2329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // energy_expanded / energy_input is in Q14.
2339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    energy_expanded = WEBRTC_SPL_SHIFT_W32(energy_expanded, temp_shift + 14);
2349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Calculate sqrt(energy_expanded / energy_input) in Q14.
2359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    mute_factor = WebRtcSpl_SqrtFloor((energy_expanded / energy_input) << 14);
2369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  } else {
2379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Set to 1 (in Q14) when |expanded| has higher energy than |input|.
2389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    mute_factor = 16384;
2399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
2409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  return mute_factor;
2429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
2439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// TODO(hlundin): There are some parameter values in this method that seem
2459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// strange. Compare with Expand::Correlation.
2469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgvoid Merge::Downsample(const int16_t* input, int input_length,
2479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                       const int16_t* expanded_signal, int expanded_length) {
2489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int16_t* filter_coefficients;
2499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int num_coefficients;
2509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int decimation_factor = fs_hz_ / 4000;
2519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static const int kCompensateDelay = 0;
25256bf2cdd198ef6bf1ccfc8fe3090066367046de5henrik.lundin@webrtc.org  int length_limit = fs_hz_ / 100;  // 10 ms in samples.
2539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (fs_hz_ == 8000) {
2549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    filter_coefficients = DspHelper::kDownsample8kHzTbl;
2559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    num_coefficients = 3;
2569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  } else if (fs_hz_ == 16000) {
2579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    filter_coefficients = DspHelper::kDownsample16kHzTbl;
2589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    num_coefficients = 5;
2599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  } else if (fs_hz_ == 32000) {
2609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    filter_coefficients = DspHelper::kDownsample32kHzTbl;
2619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    num_coefficients = 7;
2629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  } else {  // fs_hz_ == 48000
2639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    filter_coefficients = DspHelper::kDownsample48kHzTbl;
2649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    num_coefficients = 7;
2659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
2669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int signal_offset = num_coefficients - 1;
2679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  WebRtcSpl_DownsampleFast(&expanded_signal[signal_offset],
2689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                           expanded_length - signal_offset,
2699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                           expanded_downsampled_, kExpandDownsampLength,
2709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                           filter_coefficients, num_coefficients,
2719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                           decimation_factor, kCompensateDelay);
2729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (input_length <= length_limit) {
2739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Not quite long enough, so we have to cheat a bit.
2749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t temp_len = input_length - signal_offset;
2759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // TODO(hlundin): Should |downsamp_temp_len| be corrected for round-off
2769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // errors? I.e., (temp_len + decimation_factor - 1) / decimation_factor?
2779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int16_t downsamp_temp_len = temp_len / decimation_factor;
2789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    WebRtcSpl_DownsampleFast(&input[signal_offset], temp_len,
2799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             input_downsampled_, downsamp_temp_len,
2809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             filter_coefficients, num_coefficients,
2819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             decimation_factor, kCompensateDelay);
2829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    memset(&input_downsampled_[downsamp_temp_len], 0,
2839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org           sizeof(int16_t) * (kInputDownsampLength - downsamp_temp_len));
2849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  } else {
2859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    WebRtcSpl_DownsampleFast(&input[signal_offset],
2869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             input_length - signal_offset, input_downsampled_,
2879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             kInputDownsampLength, filter_coefficients,
2889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             num_coefficients, decimation_factor,
2899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             kCompensateDelay);
2909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
2919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
2929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgint16_t Merge::CorrelateAndPeakSearch(int16_t expanded_max, int16_t input_max,
2949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                      int start_position, int input_length,
2959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                      int expand_period) const {
2969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Calculate correlation without any normalization.
2979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int max_corr_length = kMaxCorrelationLength;
2989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int stop_position_downsamp = std::min(
2999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      max_corr_length, expand_->max_lag() / (fs_mult_ * 2) + 1);
3009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int16_t correlation_shift = 0;
3019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (expanded_max * input_max > 26843546) {
3029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    correlation_shift = 3;
3039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
3049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int32_t correlation[kMaxCorrelationLength];
3069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  WebRtcSpl_CrossCorrelation(correlation, input_downsampled_,
3079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             expanded_downsampled_, kInputDownsampLength,
3089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                             stop_position_downsamp, correlation_shift, 1);
3099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Normalize correlation to 14 bits and copy to a 16-bit array.
311c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org  const int pad_length = static_cast<int>(expand_->overlap_length() - 1);
312c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org  const int correlation_buffer_size = 2 * pad_length + kMaxCorrelationLength;
313c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org  scoped_ptr<int16_t[]> correlation16(new int16_t[correlation_buffer_size]);
314c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org  memset(correlation16.get(), 0, correlation_buffer_size * sizeof(int16_t));
315c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org  int16_t* correlation_ptr = &correlation16[pad_length];
3169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int32_t max_correlation = WebRtcSpl_MaxAbsValueW32(correlation,
3179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                                     stop_position_downsamp);
3189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int16_t norm_shift = std::max(0, 17 - WebRtcSpl_NormW32(max_correlation));
3199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  WebRtcSpl_VectorBitShiftW32ToW16(correlation_ptr, stop_position_downsamp,
3209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                   correlation, norm_shift);
3219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Calculate allowed starting point for peak finding.
3239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // The peak location bestIndex must fulfill two criteria:
3249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // (1) w16_bestIndex + input_length <
3259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  //     timestamps_per_call_ + expand_->overlap_length();
3269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // (2) w16_bestIndex + input_length < start_position.
327045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org  int start_index = timestamps_per_call_ +
328045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org      static_cast<int>(expand_->overlap_length());
3299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  start_index = std::max(start_position, start_index);
3309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  start_index = std::max(start_index - input_length, 0);
3319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Downscale starting index to 4kHz domain. (fs_mult_ * 2 = fs_hz_ / 4000.)
3329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int start_index_downsamp = start_index / (fs_mult_ * 2);
3339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Calculate a modified |stop_position_downsamp| to account for the increased
3359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // start index |start_index_downsamp| and the effective array length.
336045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org  int modified_stop_pos =
3379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      std::min(stop_position_downsamp,
338c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org               kMaxCorrelationLength + pad_length - start_index_downsamp);
3399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int best_correlation_index;
3409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int16_t best_correlation;
3419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static const int kNumCorrelationCandidates = 1;
3429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DspHelper::PeakDetection(&correlation_ptr[start_index_downsamp],
3439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                           modified_stop_pos, kNumCorrelationCandidates,
3449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                           fs_mult_, &best_correlation_index,
3459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                           &best_correlation);
3469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Compensate for modified start index.
3479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  best_correlation_index += start_index;
3489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Ensure that underrun does not occur for 10ms case => we have to get at
3509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // least 10ms + overlap . (This should never happen thanks to the above
3519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // modification of peak-finding starting point.)
3529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while ((best_correlation_index + input_length) <
3539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      static_cast<int>(timestamps_per_call_ + expand_->overlap_length()) ||
3549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      best_correlation_index + input_length < start_position) {
3559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    assert(false);  // Should never happen.
3569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    best_correlation_index += expand_period;  // Jump one lag ahead.
3579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
3589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  return best_correlation_index;
3599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
3609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
361c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.orgint Merge::RequiredFutureSamples() {
362c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org  return static_cast<int>(fs_hz_ / 100 * num_channels_);  // 10 ms.
363c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org}
364c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org
365c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org
3669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}  // namespace webrtc
367