OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 distortion_scale = std::max(16 - WebRtcSpl_NormW32(best_distortion_w32[i]), | 397 distortion_scale = std::max(16 - WebRtcSpl_NormW32(best_distortion_w32[i]), |
398 distortion_scale); | 398 distortion_scale); |
399 } | 399 } |
400 // Shift the distortion values to fit in 16 bits. | 400 // Shift the distortion values to fit in 16 bits. |
401 WebRtcSpl_VectorBitShiftW32ToW16(best_distortion, kNumCorrelationCandidates, | 401 WebRtcSpl_VectorBitShiftW32ToW16(best_distortion, kNumCorrelationCandidates, |
402 best_distortion_w32, distortion_scale); | 402 best_distortion_w32, distortion_scale); |
403 | 403 |
404 // Find the maximizing index |i| of the cost function | 404 // Find the maximizing index |i| of the cost function |
405 // f[i] = best_correlation[i] / best_distortion[i]. | 405 // f[i] = best_correlation[i] / best_distortion[i]. |
406 int32_t best_ratio = std::numeric_limits<int32_t>::min(); | 406 int32_t best_ratio = std::numeric_limits<int32_t>::min(); |
407 int best_index = -1; | 407 int best_index = std::numeric_limits<int>::max(); |
408 for (int i = 0; i < kNumCorrelationCandidates; ++i) { | 408 for (int i = 0; i < kNumCorrelationCandidates; ++i) { |
409 int32_t ratio; | 409 int32_t ratio; |
410 if (best_distortion[i] > 0) { | 410 if (best_distortion[i] > 0) { |
411 ratio = (best_correlation[i] << 16) / best_distortion[i]; | 411 ratio = (best_correlation[i] << 16) / best_distortion[i]; |
412 } else if (best_correlation[i] == 0) { | 412 } else if (best_correlation[i] == 0) { |
413 ratio = 0; // No correlation set result to zero. | 413 ratio = 0; // No correlation set result to zero. |
414 } else { | 414 } else { |
415 ratio = std::numeric_limits<int32_t>::max(); // Denominator is zero. | 415 ratio = std::numeric_limits<int32_t>::max(); // Denominator is zero. |
416 } | 416 } |
417 if (ratio > best_ratio) { | 417 if (ratio > best_ratio) { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 parameters.expand_vector0.CopyTo(¶meters.expand_vector1); | 542 parameters.expand_vector0.CopyTo(¶meters.expand_vector1); |
543 // Set the energy_ratio since it is used by muting slope. | 543 // Set the energy_ratio since it is used by muting slope. |
544 if ((energy1 / 4 < energy2) || (energy2 == 0)) { | 544 if ((energy1 / 4 < energy2) || (energy2 == 0)) { |
545 amplitude_ratio = 4096; // 0.5 in Q13. | 545 amplitude_ratio = 4096; // 0.5 in Q13. |
546 } else { | 546 } else { |
547 amplitude_ratio = 16384; // 2.0 in Q13. | 547 amplitude_ratio = 16384; // 2.0 in Q13. |
548 } | 548 } |
549 } | 549 } |
550 | 550 |
551 // Set the 3 lag values. | 551 // Set the 3 lag values. |
552 int lag_difference = distortion_lag - correlation_lag; | 552 if (distortion_lag == correlation_lag) { |
553 if (lag_difference == 0) { | |
554 // |distortion_lag| and |correlation_lag| are equal. | |
555 expand_lags_[0] = distortion_lag; | 553 expand_lags_[0] = distortion_lag; |
556 expand_lags_[1] = distortion_lag; | 554 expand_lags_[1] = distortion_lag; |
557 expand_lags_[2] = distortion_lag; | 555 expand_lags_[2] = distortion_lag; |
558 } else { | 556 } else { |
559 // |distortion_lag| and |correlation_lag| are not equal; use different | 557 // |distortion_lag| and |correlation_lag| are not equal; use different |
560 // combinations of the two. | 558 // combinations of the two. |
561 // First lag is |distortion_lag| only. | 559 // First lag is |distortion_lag| only. |
562 expand_lags_[0] = distortion_lag; | 560 expand_lags_[0] = distortion_lag; |
563 // Second lag is the average of the two. | 561 // Second lag is the average of the two. |
564 expand_lags_[1] = (distortion_lag + correlation_lag) / 2; | 562 expand_lags_[1] = (distortion_lag + correlation_lag) / 2; |
565 // Third lag is the average again, but rounding towards |correlation_lag|. | 563 // Third lag is the average again, but rounding towards |correlation_lag|. |
566 if (lag_difference > 0) { | 564 if (distortion_lag > correlation_lag) { |
567 expand_lags_[2] = (distortion_lag + correlation_lag - 1) / 2; | 565 expand_lags_[2] = (distortion_lag + correlation_lag - 1) / 2; |
568 } else { | 566 } else { |
569 expand_lags_[2] = (distortion_lag + correlation_lag + 1) / 2; | 567 expand_lags_[2] = (distortion_lag + correlation_lag + 1) / 2; |
570 } | 568 } |
571 } | 569 } |
572 | 570 |
573 // Calculate the LPC and the gain of the filters. | 571 // Calculate the LPC and the gain of the filters. |
574 // Calculate scale value needed for auto-correlation. | 572 // Calculate scale value needed for auto-correlation. |
575 correlation_scale = WebRtcSpl_MaxAbsValueW16( | 573 correlation_scale = WebRtcSpl_MaxAbsValueW16( |
576 &(audio_history[signal_length - fs_mult_lpc_analysis_len]), | 574 &(audio_history[signal_length - fs_mult_lpc_analysis_len]), |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 int16_t x1, x2, x3; | 682 int16_t x1, x2, x3; |
685 // |corr_coefficient| is in Q14. | 683 // |corr_coefficient| is in Q14. |
686 x1 = static_cast<int16_t>(corr_coefficient); | 684 x1 = static_cast<int16_t>(corr_coefficient); |
687 x2 = (x1 * x1) >> 14; // Shift 14 to keep result in Q14. | 685 x2 = (x1 * x1) >> 14; // Shift 14 to keep result in Q14. |
688 x3 = (x1 * x2) >> 14; | 686 x3 = (x1 * x2) >> 14; |
689 static const int kCoefficients[4] = { -5179, 19931, -16422, 5776 }; | 687 static const int kCoefficients[4] = { -5179, 19931, -16422, 5776 }; |
690 int32_t temp_sum = kCoefficients[0] << 14; | 688 int32_t temp_sum = kCoefficients[0] << 14; |
691 temp_sum += kCoefficients[1] * x1; | 689 temp_sum += kCoefficients[1] * x1; |
692 temp_sum += kCoefficients[2] * x2; | 690 temp_sum += kCoefficients[2] * x2; |
693 temp_sum += kCoefficients[3] * x3; | 691 temp_sum += kCoefficients[3] * x3; |
694 parameters.voice_mix_factor = temp_sum / 4096; | 692 parameters.voice_mix_factor = |
695 parameters.voice_mix_factor = std::min(parameters.voice_mix_factor, | 693 static_cast<int16_t>(std::min(temp_sum / 4096, 16384)); |
696 static_cast<int16_t>(16384)); | |
697 parameters.voice_mix_factor = std::max(parameters.voice_mix_factor, | 694 parameters.voice_mix_factor = std::max(parameters.voice_mix_factor, |
698 static_cast<int16_t>(0)); | 695 static_cast<int16_t>(0)); |
699 } else { | 696 } else { |
700 parameters.voice_mix_factor = 0; | 697 parameters.voice_mix_factor = 0; |
701 } | 698 } |
702 | 699 |
703 // Calculate muting slope. Reuse value from earlier scaling of | 700 // Calculate muting slope. Reuse value from earlier scaling of |
704 // |expand_vector0| and |expand_vector1|. | 701 // |expand_vector0| and |expand_vector1|. |
705 int16_t slope = amplitude_ratio; | 702 int16_t slope = amplitude_ratio; |
706 if (slope > 12288) { | 703 if (slope > 12288) { |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
938 const size_t kMaxRandSamples = RandomVector::kRandomTableSize; | 935 const size_t kMaxRandSamples = RandomVector::kRandomTableSize; |
939 while (samples_generated < length) { | 936 while (samples_generated < length) { |
940 size_t rand_length = std::min(length - samples_generated, kMaxRandSamples); | 937 size_t rand_length = std::min(length - samples_generated, kMaxRandSamples); |
941 random_vector_->IncreaseSeedIncrement(seed_increment); | 938 random_vector_->IncreaseSeedIncrement(seed_increment); |
942 random_vector_->Generate(rand_length, &random_vector[samples_generated]); | 939 random_vector_->Generate(rand_length, &random_vector[samples_generated]); |
943 samples_generated += rand_length; | 940 samples_generated += rand_length; |
944 } | 941 } |
945 } | 942 } |
946 | 943 |
947 } // namespace webrtc | 944 } // namespace webrtc |
OLD | NEW |