expand.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef MODULES_AUDIO_CODING_NETEQ_EXPAND_H_
  11. #define MODULES_AUDIO_CODING_NETEQ_EXPAND_H_
  12. #include <assert.h>
  13. #include <memory>
  14. #include "modules/audio_coding/neteq/audio_vector.h"
  15. #include "rtc_base/constructor_magic.h"
  16. namespace webrtc {
  17. // Forward declarations.
  18. class AudioMultiVector;
  19. class BackgroundNoise;
  20. class RandomVector;
  21. class StatisticsCalculator;
  22. class SyncBuffer;
  23. // This class handles extrapolation of audio data from the sync_buffer to
  24. // produce packet-loss concealment.
  25. // TODO(hlundin): Refactor this class to divide the long methods into shorter
  26. // ones.
  27. class Expand {
  28. public:
  29. Expand(BackgroundNoise* background_noise,
  30. SyncBuffer* sync_buffer,
  31. RandomVector* random_vector,
  32. StatisticsCalculator* statistics,
  33. int fs,
  34. size_t num_channels);
  35. virtual ~Expand();
  36. // Resets the object.
  37. virtual void Reset();
  38. // The main method to produce concealment data. The data is appended to the
  39. // end of |output|.
  40. virtual int Process(AudioMultiVector* output);
  41. // Prepare the object to do extra expansion during normal operation following
  42. // a period of expands.
  43. virtual void SetParametersForNormalAfterExpand();
  44. // Prepare the object to do extra expansion during merge operation following
  45. // a period of expands.
  46. virtual void SetParametersForMergeAfterExpand();
  47. // Returns the mute factor for |channel|.
  48. int16_t MuteFactor(size_t channel) const {
  49. assert(channel < num_channels_);
  50. return channel_parameters_[channel].mute_factor;
  51. }
  52. // Returns true if expansion has been faded down to zero amplitude (for all
  53. // channels); false otherwise.
  54. bool Muted() const;
  55. // Accessors and mutators.
  56. virtual size_t overlap_length() const;
  57. size_t max_lag() const { return max_lag_; }
  58. protected:
  59. static const int kMaxConsecutiveExpands = 200;
  60. void GenerateRandomVector(int16_t seed_increment,
  61. size_t length,
  62. int16_t* random_vector);
  63. // Initializes member variables at the beginning of an expand period.
  64. void InitializeForAnExpandPeriod();
  65. bool TooManyExpands();
  66. // Analyzes the signal history in |sync_buffer_|, and set up all parameters
  67. // necessary to produce concealment data.
  68. void AnalyzeSignal(int16_t* random_vector);
  69. RandomVector* const random_vector_;
  70. SyncBuffer* const sync_buffer_;
  71. bool first_expand_;
  72. const int fs_hz_;
  73. const size_t num_channels_;
  74. int consecutive_expands_;
  75. private:
  76. static const size_t kUnvoicedLpcOrder = 6;
  77. static const size_t kNumCorrelationCandidates = 3;
  78. static const size_t kDistortionLength = 20;
  79. static const size_t kLpcAnalysisLength = 160;
  80. static const size_t kMaxSampleRate = 48000;
  81. static const int kNumLags = 3;
  82. struct ChannelParameters {
  83. ChannelParameters();
  84. int16_t mute_factor;
  85. int16_t ar_filter[kUnvoicedLpcOrder + 1];
  86. int16_t ar_filter_state[kUnvoicedLpcOrder];
  87. int16_t ar_gain;
  88. int16_t ar_gain_scale;
  89. int16_t voice_mix_factor; /* Q14 */
  90. int16_t current_voice_mix_factor; /* Q14 */
  91. AudioVector expand_vector0;
  92. AudioVector expand_vector1;
  93. bool onset;
  94. int mute_slope; /* Q20 */
  95. };
  96. // Calculate the auto-correlation of |input|, with length |input_length|
  97. // samples. The correlation is calculated from a downsampled version of
  98. // |input|, and is written to |output|.
  99. void Correlation(const int16_t* input,
  100. size_t input_length,
  101. int16_t* output) const;
  102. void UpdateLagIndex();
  103. BackgroundNoise* const background_noise_;
  104. StatisticsCalculator* const statistics_;
  105. const size_t overlap_length_;
  106. size_t max_lag_;
  107. size_t expand_lags_[kNumLags];
  108. int lag_index_direction_;
  109. int current_lag_index_;
  110. bool stop_muting_;
  111. size_t expand_duration_samples_;
  112. std::unique_ptr<ChannelParameters[]> channel_parameters_;
  113. RTC_DISALLOW_COPY_AND_ASSIGN(Expand);
  114. };
  115. struct ExpandFactory {
  116. ExpandFactory() {}
  117. virtual ~ExpandFactory() {}
  118. virtual Expand* Create(BackgroundNoise* background_noise,
  119. SyncBuffer* sync_buffer,
  120. RandomVector* random_vector,
  121. StatisticsCalculator* statistics,
  122. int fs,
  123. size_t num_channels) const;
  124. };
  125. } // namespace webrtc
  126. #endif // MODULES_AUDIO_CODING_NETEQ_EXPAND_H_