merge.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_MERGE_H_
  11. #define MODULES_AUDIO_CODING_NETEQ_MERGE_H_
  12. #include "modules/audio_coding/neteq/audio_multi_vector.h"
  13. #include "rtc_base/constructor_magic.h"
  14. namespace webrtc {
  15. // Forward declarations.
  16. class Expand;
  17. class SyncBuffer;
  18. // This class handles the transition from expansion to normal operation.
  19. // When a packet is not available for decoding when needed, the expand operation
  20. // is called to generate extrapolation data. If the missing packet arrives,
  21. // i.e., it was just delayed, it can be decoded and appended directly to the
  22. // end of the expanded data (thanks to how the Expand class operates). However,
  23. // if a later packet arrives instead, the loss is a fact, and the new data must
  24. // be stitched together with the end of the expanded data. This stitching is
  25. // what the Merge class does.
  26. class Merge {
  27. public:
  28. Merge(int fs_hz,
  29. size_t num_channels,
  30. Expand* expand,
  31. SyncBuffer* sync_buffer);
  32. virtual ~Merge();
  33. // The main method to produce the audio data. The decoded data is supplied in
  34. // |input|, having |input_length| samples in total for all channels
  35. // (interleaved). The result is written to |output|. The number of channels
  36. // allocated in |output| defines the number of channels that will be used when
  37. // de-interleaving |input|.
  38. virtual size_t Process(int16_t* input,
  39. size_t input_length,
  40. AudioMultiVector* output);
  41. virtual size_t RequiredFutureSamples();
  42. protected:
  43. const int fs_hz_;
  44. const size_t num_channels_;
  45. private:
  46. static const int kMaxSampleRate = 48000;
  47. static const size_t kExpandDownsampLength = 100;
  48. static const size_t kInputDownsampLength = 40;
  49. static const size_t kMaxCorrelationLength = 60;
  50. // Calls |expand_| to get more expansion data to merge with. The data is
  51. // written to |expanded_signal_|. Returns the length of the expanded data,
  52. // while |expand_period| will be the number of samples in one expansion period
  53. // (typically one pitch period). The value of |old_length| will be the number
  54. // of samples that were taken from the |sync_buffer_|.
  55. size_t GetExpandedSignal(size_t* old_length, size_t* expand_period);
  56. // Analyzes |input| and |expanded_signal| and returns muting factor (Q14) to
  57. // be used on the new data.
  58. int16_t SignalScaling(const int16_t* input,
  59. size_t input_length,
  60. const int16_t* expanded_signal) const;
  61. // Downsamples |input| (|input_length| samples) and |expanded_signal| to
  62. // 4 kHz sample rate. The downsampled signals are written to
  63. // |input_downsampled_| and |expanded_downsampled_|, respectively.
  64. void Downsample(const int16_t* input,
  65. size_t input_length,
  66. const int16_t* expanded_signal,
  67. size_t expanded_length);
  68. // Calculates cross-correlation between |input_downsampled_| and
  69. // |expanded_downsampled_|, and finds the correlation maximum. The maximizing
  70. // lag is returned.
  71. size_t CorrelateAndPeakSearch(size_t start_position,
  72. size_t input_length,
  73. size_t expand_period) const;
  74. const int fs_mult_; // fs_hz_ / 8000.
  75. const size_t timestamps_per_call_;
  76. Expand* expand_;
  77. SyncBuffer* sync_buffer_;
  78. int16_t expanded_downsampled_[kExpandDownsampLength];
  79. int16_t input_downsampled_[kInputDownsampLength];
  80. AudioMultiVector expanded_;
  81. std::vector<int16_t> temp_data_;
  82. RTC_DISALLOW_COPY_AND_ASSIGN(Merge);
  83. };
  84. } // namespace webrtc
  85. #endif // MODULES_AUDIO_CODING_NETEQ_MERGE_H_