Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.

This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.

This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002

The change is being landed as TBR to all the folks who reviewed the above.

BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher

Review URL: https://codereview.webrtc.org/1230503003 .

Cr-Commit-Position: refs/heads/master@{#9768}
diff --git a/webrtc/modules/audio_coding/neteq/merge.cc b/webrtc/modules/audio_coding/neteq/merge.cc
index 2c515c1..b6fb2d8 100644
--- a/webrtc/modules/audio_coding/neteq/merge.cc
+++ b/webrtc/modules/audio_coding/neteq/merge.cc
@@ -31,25 +31,25 @@
     : fs_hz_(fs_hz),
       num_channels_(num_channels),
       fs_mult_(fs_hz_ / 8000),
-      timestamps_per_call_(fs_hz_ / 100),
+      timestamps_per_call_(static_cast<size_t>(fs_hz_ / 100)),
       expand_(expand),
       sync_buffer_(sync_buffer),
       expanded_(num_channels_) {
   assert(num_channels_ > 0);
 }
 
-int Merge::Process(int16_t* input, size_t input_length,
-                   int16_t* external_mute_factor_array,
-                   AudioMultiVector* output) {
+size_t Merge::Process(int16_t* input, size_t input_length,
+                      int16_t* external_mute_factor_array,
+                      AudioMultiVector* output) {
   // TODO(hlundin): Change to an enumerator and skip assert.
   assert(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ ==  32000 ||
          fs_hz_ == 48000);
   assert(fs_hz_ <= kMaxSampleRate);  // Should not be possible.
 
-  int old_length;
-  int expand_period;
+  size_t old_length;
+  size_t expand_period;
   // Get expansion data to overlap and mix with.
-  int expanded_length = GetExpandedSignal(&old_length, &expand_period);
+  size_t expanded_length = GetExpandedSignal(&old_length, &expand_period);
 
   // Transfer input signal to an AudioMultiVector.
   AudioMultiVector input_vector(num_channels_);
@@ -57,7 +57,7 @@
   size_t input_length_per_channel = input_vector.Size();
   assert(input_length_per_channel == input_length / num_channels_);
 
-  int16_t best_correlation_index = 0;
+  size_t best_correlation_index = 0;
   size_t output_length = 0;
 
   for (size_t channel = 0; channel < num_channels_; ++channel) {
@@ -65,8 +65,8 @@
     int16_t* expanded_channel = &expanded_[channel][0];
     int16_t expanded_max, input_max;
     int16_t new_mute_factor = SignalScaling(
-        input_channel, static_cast<int>(input_length_per_channel),
-        expanded_channel, &expanded_max, &input_max);
+        input_channel, input_length_per_channel, expanded_channel,
+        &expanded_max, &input_max);
 
     // Adjust muting factor (product of "main" muting factor and expand muting
     // factor).
@@ -84,13 +84,13 @@
       // Downsample, correlate, and find strongest correlation period for the
       // master (i.e., first) channel only.
       // Downsample to 4kHz sample rate.
-      Downsample(input_channel, static_cast<int>(input_length_per_channel),
-                 expanded_channel, expanded_length);
+      Downsample(input_channel, input_length_per_channel, expanded_channel,
+                 expanded_length);
 
       // Calculate the lag of the strongest correlation period.
       best_correlation_index = CorrelateAndPeakSearch(
           expanded_max, input_max, old_length,
-          static_cast<int>(input_length_per_channel), expand_period);
+          input_length_per_channel, expand_period);
     }
 
     static const int kTempDataSize = 3600;
@@ -99,11 +99,11 @@
 
     // Mute the new decoded data if needed (and unmute it linearly).
     // This is the overlapping part of expanded_signal.
-    int interpolation_length = std::min(
+    size_t interpolation_length = std::min(
         kMaxCorrelationLength * fs_mult_,
         expanded_length - best_correlation_index);
     interpolation_length = std::min(interpolation_length,
-                                    static_cast<int>(input_length_per_channel));
+                                    input_length_per_channel);
     if (*external_mute_factor < 16384) {
       // Set a suitable muting slope (Q20). 0.004 for NB, 0.002 for WB,
       // and so on.
@@ -153,14 +153,14 @@
 
   // Return new added length. |old_length| samples were borrowed from
   // |sync_buffer_|.
-  return static_cast<int>(output_length) - old_length;
+  return output_length - old_length;
 }
 
-int Merge::GetExpandedSignal(int* old_length, int* expand_period) {
+size_t Merge::GetExpandedSignal(size_t* old_length, size_t* expand_period) {
   // Check how much data that is left since earlier.
-  *old_length = static_cast<int>(sync_buffer_->FutureLength());
+  *old_length = sync_buffer_->FutureLength();
   // Should never be less than overlap_length.
-  assert(*old_length >= static_cast<int>(expand_->overlap_length()));
+  assert(*old_length >= expand_->overlap_length());
   // Generate data to merge the overlap with using expand.
   expand_->SetParametersForMergeAfterExpand();
 
@@ -171,7 +171,7 @@
     // but shift them towards the end of the buffer. This is ok, since all of
     // the buffer will be expand data anyway, so as long as the beginning is
     // left untouched, we're fine.
-    int16_t length_diff = *old_length - 210 * kMaxSampleRate / 8000;
+    size_t length_diff = *old_length - 210 * kMaxSampleRate / 8000;
     sync_buffer_->InsertZerosAtIndex(length_diff, sync_buffer_->next_index());
     *old_length = 210 * kMaxSampleRate / 8000;
     // This is the truncated length.
@@ -181,34 +181,34 @@
 
   AudioMultiVector expanded_temp(num_channels_);
   expand_->Process(&expanded_temp);
-  *expand_period = static_cast<int>(expanded_temp.Size());  // Samples per
-                                                            // channel.
+  *expand_period = expanded_temp.Size();  // Samples per channel.
 
   expanded_.Clear();
   // Copy what is left since earlier into the expanded vector.
   expanded_.PushBackFromIndex(*sync_buffer_, sync_buffer_->next_index());
-  assert(expanded_.Size() == static_cast<size_t>(*old_length));
+  assert(expanded_.Size() == *old_length);
   assert(expanded_temp.Size() > 0);
   // Do "ugly" copy and paste from the expanded in order to generate more data
   // to correlate (but not interpolate) with.
-  const int required_length = (120 + 80 + 2) * fs_mult_;
-  if (expanded_.Size() < static_cast<size_t>(required_length)) {
-    while (expanded_.Size() < static_cast<size_t>(required_length)) {
+  const size_t required_length = static_cast<size_t>((120 + 80 + 2) * fs_mult_);
+  if (expanded_.Size() < required_length) {
+    while (expanded_.Size() < required_length) {
       // Append one more pitch period each time.
       expanded_.PushBack(expanded_temp);
     }
     // Trim the length to exactly |required_length|.
     expanded_.PopBack(expanded_.Size() - required_length);
   }
-  assert(expanded_.Size() >= static_cast<size_t>(required_length));
+  assert(expanded_.Size() >= required_length);
   return required_length;
 }
 
-int16_t Merge::SignalScaling(const int16_t* input, int input_length,
+int16_t Merge::SignalScaling(const int16_t* input, size_t input_length,
                              const int16_t* expanded_signal,
                              int16_t* expanded_max, int16_t* input_max) const {
   // Adjust muting factor if new vector is more or less of the BGN energy.
-  const int mod_input_length = std::min(64 * fs_mult_, input_length);
+  const size_t mod_input_length =
+      std::min(static_cast<size_t>(64 * fs_mult_), input_length);
   *expanded_max = WebRtcSpl_MaxAbsValueW16(expanded_signal, mod_input_length);
   *input_max = WebRtcSpl_MaxAbsValueW16(input, mod_input_length);
 
@@ -260,13 +260,13 @@
 
 // TODO(hlundin): There are some parameter values in this method that seem
 // strange. Compare with Expand::Correlation.
-void Merge::Downsample(const int16_t* input, int input_length,
-                       const int16_t* expanded_signal, int expanded_length) {
+void Merge::Downsample(const int16_t* input, size_t input_length,
+                       const int16_t* expanded_signal, size_t expanded_length) {
   const int16_t* filter_coefficients;
-  int num_coefficients;
+  size_t num_coefficients;
   int decimation_factor = fs_hz_ / 4000;
-  static const int kCompensateDelay = 0;
-  int length_limit = fs_hz_ / 100;  // 10 ms in samples.
+  static const size_t kCompensateDelay = 0;
+  size_t length_limit = static_cast<size_t>(fs_hz_ / 100);  // 10 ms in samples.
   if (fs_hz_ == 8000) {
     filter_coefficients = DspHelper::kDownsample8kHzTbl;
     num_coefficients = 3;
@@ -280,7 +280,7 @@
     filter_coefficients = DspHelper::kDownsample48kHzTbl;
     num_coefficients = 7;
   }
-  int signal_offset = num_coefficients - 1;
+  size_t signal_offset = num_coefficients - 1;
   WebRtcSpl_DownsampleFast(&expanded_signal[signal_offset],
                            expanded_length - signal_offset,
                            expanded_downsampled_, kExpandDownsampLength,
@@ -288,10 +288,10 @@
                            decimation_factor, kCompensateDelay);
   if (input_length <= length_limit) {
     // Not quite long enough, so we have to cheat a bit.
-    int16_t temp_len = input_length - signal_offset;
+    size_t temp_len = input_length - signal_offset;
     // TODO(hlundin): Should |downsamp_temp_len| be corrected for round-off
     // errors? I.e., (temp_len + decimation_factor - 1) / decimation_factor?
-    int16_t downsamp_temp_len = temp_len / decimation_factor;
+    size_t downsamp_temp_len = temp_len / decimation_factor;
     WebRtcSpl_DownsampleFast(&input[signal_offset], temp_len,
                              input_downsampled_, downsamp_temp_len,
                              filter_coefficients, num_coefficients,
@@ -307,12 +307,12 @@
   }
 }
 
-int16_t Merge::CorrelateAndPeakSearch(int16_t expanded_max, int16_t input_max,
-                                      int start_position, int input_length,
-                                      int expand_period) const {
+size_t Merge::CorrelateAndPeakSearch(int16_t expanded_max, int16_t input_max,
+                                     size_t start_position, size_t input_length,
+                                     size_t expand_period) const {
   // Calculate correlation without any normalization.
-  const int max_corr_length = kMaxCorrelationLength;
-  int stop_position_downsamp =
+  const size_t max_corr_length = kMaxCorrelationLength;
+  size_t stop_position_downsamp =
       std::min(max_corr_length, expand_->max_lag() / (fs_mult_ * 2) + 1);
   int correlation_shift = 0;
   if (expanded_max * input_max > 26843546) {
@@ -325,8 +325,8 @@
                              stop_position_downsamp, correlation_shift, 1);
 
   // Normalize correlation to 14 bits and copy to a 16-bit array.
-  const int pad_length = static_cast<int>(expand_->overlap_length() - 1);
-  const int correlation_buffer_size = 2 * pad_length + kMaxCorrelationLength;
+  const size_t pad_length = expand_->overlap_length() - 1;
+  const size_t correlation_buffer_size = 2 * pad_length + kMaxCorrelationLength;
   rtc::scoped_ptr<int16_t[]> correlation16(
       new int16_t[correlation_buffer_size]);
   memset(correlation16.get(), 0, correlation_buffer_size * sizeof(int16_t));
@@ -342,21 +342,20 @@
   // (1) w16_bestIndex + input_length <
   //     timestamps_per_call_ + expand_->overlap_length();
   // (2) w16_bestIndex + input_length < start_position.
-  int start_index = timestamps_per_call_ +
-      static_cast<int>(expand_->overlap_length());
+  size_t start_index = timestamps_per_call_ + expand_->overlap_length();
   start_index = std::max(start_position, start_index);
   start_index = (input_length > start_index) ? 0 : (start_index - input_length);
   // Downscale starting index to 4kHz domain. (fs_mult_ * 2 = fs_hz_ / 4000.)
-  int start_index_downsamp = start_index / (fs_mult_ * 2);
+  size_t start_index_downsamp = start_index / (fs_mult_ * 2);
 
   // Calculate a modified |stop_position_downsamp| to account for the increased
   // start index |start_index_downsamp| and the effective array length.
-  int modified_stop_pos =
+  size_t modified_stop_pos =
       std::min(stop_position_downsamp,
                kMaxCorrelationLength + pad_length - start_index_downsamp);
-  int best_correlation_index;
+  size_t best_correlation_index;
   int16_t best_correlation;
-  static const int kNumCorrelationCandidates = 1;
+  static const size_t kNumCorrelationCandidates = 1;
   DspHelper::PeakDetection(&correlation_ptr[start_index_downsamp],
                            modified_stop_pos, kNumCorrelationCandidates,
                            fs_mult_, &best_correlation_index,
@@ -368,16 +367,16 @@
   // least 10ms + overlap . (This should never happen thanks to the above
   // modification of peak-finding starting point.)
   while (((best_correlation_index + input_length) <
-      static_cast<int>(timestamps_per_call_ + expand_->overlap_length())) ||
-      ((best_correlation_index + input_length) < start_position)) {
+          (timestamps_per_call_ + expand_->overlap_length())) ||
+         ((best_correlation_index + input_length) < start_position)) {
     assert(false);  // Should never happen.
     best_correlation_index += expand_period;  // Jump one lag ahead.
   }
   return best_correlation_index;
 }
 
-int Merge::RequiredFutureSamples() {
-  return static_cast<int>(fs_hz_ / 100 * num_channels_);  // 10 ms.
+size_t Merge::RequiredFutureSamples() {
+  return fs_hz_ / 100 * num_channels_;  // 10 ms.
 }