Prepare to convert various types to size_t.
This makes some behaviorally-invariant changes to make certain code that
currently only works correctly with signed types work safely regardless of the
signedness of the types in question. This is preparation for a future change
that will convert a variety of types to size_t.
There are also some formatting changes (e.g. converting "enum hack" usage to real consts) to make it simpler to just change "int" to "size_t" in the future to change the types of those constants.
BUG=none
R=andrew@webrtc.org, juberti@webrtc.org, kwiberg@webrtc.org
TBR=ajm
Review URL: https://codereview.webrtc.org/1174813003
Cr-Commit-Position: refs/heads/master@{#9413}
diff --git a/webrtc/modules/audio_coding/neteq/expand.cc b/webrtc/modules/audio_coding/neteq/expand.cc
index 4bcb7a8..cfd2701 100644
--- a/webrtc/modules/audio_coding/neteq/expand.cc
+++ b/webrtc/modules/audio_coding/neteq/expand.cc
@@ -404,7 +404,7 @@
// Find the maximizing index |i| of the cost function
// f[i] = best_correlation[i] / best_distortion[i].
int32_t best_ratio = std::numeric_limits<int32_t>::min();
- int best_index = -1;
+ int best_index = std::numeric_limits<int>::max();
for (int i = 0; i < kNumCorrelationCandidates; ++i) {
int32_t ratio;
if (best_distortion[i] > 0) {
@@ -549,9 +549,7 @@
}
// Set the 3 lag values.
- int lag_difference = distortion_lag - correlation_lag;
- if (lag_difference == 0) {
- // |distortion_lag| and |correlation_lag| are equal.
+ if (distortion_lag == correlation_lag) {
expand_lags_[0] = distortion_lag;
expand_lags_[1] = distortion_lag;
expand_lags_[2] = distortion_lag;
@@ -563,7 +561,7 @@
// Second lag is the average of the two.
expand_lags_[1] = (distortion_lag + correlation_lag) / 2;
// Third lag is the average again, but rounding towards |correlation_lag|.
- if (lag_difference > 0) {
+ if (distortion_lag > correlation_lag) {
expand_lags_[2] = (distortion_lag + correlation_lag - 1) / 2;
} else {
expand_lags_[2] = (distortion_lag + correlation_lag + 1) / 2;
@@ -691,9 +689,8 @@
temp_sum += kCoefficients[1] * x1;
temp_sum += kCoefficients[2] * x2;
temp_sum += kCoefficients[3] * x3;
- parameters.voice_mix_factor = temp_sum / 4096;
- parameters.voice_mix_factor = std::min(parameters.voice_mix_factor,
- static_cast<int16_t>(16384));
+ parameters.voice_mix_factor =
+ static_cast<int16_t>(std::min(temp_sum / 4096, 16384));
parameters.voice_mix_factor = std::max(parameters.voice_mix_factor,
static_cast<int16_t>(0));
} else {
diff --git a/webrtc/modules/audio_coding/neteq/merge.cc b/webrtc/modules/audio_coding/neteq/merge.cc
index fa033cf..23382ac 100644
--- a/webrtc/modules/audio_coding/neteq/merge.cc
+++ b/webrtc/modules/audio_coding/neteq/merge.cc
@@ -175,7 +175,7 @@
// This is the truncated length.
}
// This assert should always be true thanks to the if statement above.
- assert(210 * kMaxSampleRate / 8000 - *old_length >= 0);
+ assert(210 * kMaxSampleRate / 8000 >= *old_length);
AudioMultiVector expanded_temp(num_channels_);
expand_->Process(&expanded_temp);
@@ -342,7 +342,7 @@
int start_index = timestamps_per_call_ +
static_cast<int>(expand_->overlap_length());
start_index = std::max(start_position, start_index);
- start_index = std::max(start_index - input_length, 0);
+ 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);
diff --git a/webrtc/modules/audio_coding/neteq/neteq_impl.cc b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
index 8cd9aeb..2d4ff27 100644
--- a/webrtc/modules/audio_coding/neteq/neteq_impl.cc
+++ b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
@@ -1520,10 +1520,10 @@
borrowed_samples_per_channel = static_cast<int>(required_samples -
decoded_length_per_channel);
// Calculate how many of these were already played out.
- old_borrowed_samples_per_channel = static_cast<int>(
- borrowed_samples_per_channel - sync_buffer_->FutureLength());
- old_borrowed_samples_per_channel = std::max(
- 0, old_borrowed_samples_per_channel);
+ const int future_length = static_cast<int>(sync_buffer_->FutureLength());
+ old_borrowed_samples_per_channel =
+ (borrowed_samples_per_channel > future_length) ?
+ (borrowed_samples_per_channel - future_length) : 0;
memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels],
decoded_buffer,
sizeof(int16_t) * decoded_length);
diff --git a/webrtc/modules/audio_coding/neteq/normal.cc b/webrtc/modules/audio_coding/neteq/normal.cc
index ce774d7..b172d56 100644
--- a/webrtc/modules/audio_coding/neteq/normal.cc
+++ b/webrtc/modules/audio_coding/neteq/normal.cc
@@ -83,8 +83,10 @@
scaling = std::max(scaling, 0); // |scaling| should always be >= 0.
int32_t energy = WebRtcSpl_DotProductWithScale(signal, signal,
energy_length, scaling);
- if ((energy_length >> scaling) > 0) {
- energy = energy / (energy_length >> scaling);
+ int32_t scaled_energy_length =
+ static_cast<int32_t>(energy_length >> scaling);
+ if (scaled_energy_length > 0) {
+ energy = energy / scaled_energy_length;
} else {
energy = 0;
}
diff --git a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc
index 7d8d60d..1ef9ce5 100644
--- a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc
+++ b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc
@@ -450,7 +450,10 @@
CHECK_NOT_NULL(out_file);
printf("Output file: %s\n\n", argv[2]);
packet_size = atoi(argv[3]);
- CHECK_NOT_NULL(packet_size);
+ if (packet_size <= 0) {
+ printf("Packet size %d must be positive", packet_size);
+ return -1;
+ }
printf("Packet size: %i\n", packet_size);
// check for stereo