delay_estimator.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // Performs delay estimation on binary converted spectra.
  11. // The return value is 0 - OK and -1 - Error, unless otherwise stated.
  12. #ifndef MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
  13. #define MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
  14. #include <stdint.h>
  15. namespace webrtc {
  16. static const int32_t kMaxBitCountsQ9 = (32 << 9); // 32 matching bits in Q9.
  17. typedef struct {
  18. // Pointer to bit counts.
  19. int* far_bit_counts;
  20. // Binary history variables.
  21. uint32_t* binary_far_history;
  22. int history_size;
  23. } BinaryDelayEstimatorFarend;
  24. typedef struct {
  25. // Pointer to bit counts.
  26. int32_t* mean_bit_counts;
  27. // Array only used locally in ProcessBinarySpectrum() but whose size is
  28. // determined at run-time.
  29. int32_t* bit_counts;
  30. // Binary history variables.
  31. uint32_t* binary_near_history;
  32. int near_history_size;
  33. int history_size;
  34. // Delay estimation variables.
  35. int32_t minimum_probability;
  36. int last_delay_probability;
  37. // Delay memory.
  38. int last_delay;
  39. // Robust validation
  40. int robust_validation_enabled;
  41. int allowed_offset;
  42. int last_candidate_delay;
  43. int compare_delay;
  44. int candidate_hits;
  45. float* histogram;
  46. float last_delay_histogram;
  47. // For dynamically changing the lookahead when using SoftReset...().
  48. int lookahead;
  49. // Far-end binary spectrum history buffer etc.
  50. BinaryDelayEstimatorFarend* farend;
  51. } BinaryDelayEstimator;
  52. // Releases the memory allocated by
  53. // WebRtc_CreateBinaryDelayEstimatorFarend(...).
  54. // Input:
  55. // - self : Pointer to the binary delay estimation far-end
  56. // instance which is the return value of
  57. // WebRtc_CreateBinaryDelayEstimatorFarend().
  58. //
  59. void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self);
  60. // Allocates the memory needed by the far-end part of the binary delay
  61. // estimation. The memory needs to be initialized separately through
  62. // WebRtc_InitBinaryDelayEstimatorFarend(...).
  63. //
  64. // Inputs:
  65. // - history_size : Size of the far-end binary spectrum history.
  66. //
  67. // Return value:
  68. // - BinaryDelayEstimatorFarend*
  69. // : Created |handle|. If the memory can't be allocated
  70. // or if any of the input parameters are invalid NULL
  71. // is returned.
  72. //
  73. BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend(
  74. int history_size);
  75. // Re-allocates the buffers.
  76. //
  77. // Inputs:
  78. // - self : Pointer to the binary estimation far-end instance
  79. // which is the return value of
  80. // WebRtc_CreateBinaryDelayEstimatorFarend().
  81. // - history_size : Size of the far-end binary spectrum history.
  82. //
  83. // Return value:
  84. // - history_size : The history size allocated.
  85. int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend* self,
  86. int history_size);
  87. // Initializes the delay estimation far-end instance created with
  88. // WebRtc_CreateBinaryDelayEstimatorFarend(...).
  89. //
  90. // Input:
  91. // - self : Pointer to the delay estimation far-end instance.
  92. //
  93. // Output:
  94. // - self : Initialized far-end instance.
  95. //
  96. void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self);
  97. // Soft resets the delay estimation far-end instance created with
  98. // WebRtc_CreateBinaryDelayEstimatorFarend(...).
  99. //
  100. // Input:
  101. // - delay_shift : The amount of blocks to shift history buffers.
  102. //
  103. void WebRtc_SoftResetBinaryDelayEstimatorFarend(
  104. BinaryDelayEstimatorFarend* self,
  105. int delay_shift);
  106. // Adds the binary far-end spectrum to the internal far-end history buffer. This
  107. // spectrum is used as reference when calculating the delay using
  108. // WebRtc_ProcessBinarySpectrum().
  109. //
  110. // Inputs:
  111. // - self : Pointer to the delay estimation far-end
  112. // instance.
  113. // - binary_far_spectrum : Far-end binary spectrum.
  114. //
  115. // Output:
  116. // - self : Updated far-end instance.
  117. //
  118. void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* self,
  119. uint32_t binary_far_spectrum);
  120. // Releases the memory allocated by WebRtc_CreateBinaryDelayEstimator(...).
  121. //
  122. // Note that BinaryDelayEstimator utilizes BinaryDelayEstimatorFarend, but does
  123. // not take ownership of it, hence the BinaryDelayEstimator has to be torn down
  124. // before the far-end.
  125. //
  126. // Input:
  127. // - self : Pointer to the binary delay estimation instance
  128. // which is the return value of
  129. // WebRtc_CreateBinaryDelayEstimator().
  130. //
  131. void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* self);
  132. // Allocates the memory needed by the binary delay estimation. The memory needs
  133. // to be initialized separately through WebRtc_InitBinaryDelayEstimator(...).
  134. //
  135. // See WebRtc_CreateDelayEstimator(..) in delay_estimator_wrapper.c for detailed
  136. // description.
  137. BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
  138. BinaryDelayEstimatorFarend* farend,
  139. int max_lookahead);
  140. // Re-allocates |history_size| dependent buffers. The far-end buffers will be
  141. // updated at the same time if needed.
  142. //
  143. // Input:
  144. // - self : Pointer to the binary estimation instance which is
  145. // the return value of
  146. // WebRtc_CreateBinaryDelayEstimator().
  147. // - history_size : Size of the history buffers.
  148. //
  149. // Return value:
  150. // - history_size : The history size allocated.
  151. int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator* self,
  152. int history_size);
  153. // Initializes the delay estimation instance created with
  154. // WebRtc_CreateBinaryDelayEstimator(...).
  155. //
  156. // Input:
  157. // - self : Pointer to the delay estimation instance.
  158. //
  159. // Output:
  160. // - self : Initialized instance.
  161. //
  162. void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* self);
  163. // Soft resets the delay estimation instance created with
  164. // WebRtc_CreateBinaryDelayEstimator(...).
  165. //
  166. // Input:
  167. // - delay_shift : The amount of blocks to shift history buffers.
  168. //
  169. // Return value:
  170. // - actual_shifts : The actual number of shifts performed.
  171. //
  172. int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator* self,
  173. int delay_shift);
  174. // Estimates and returns the delay between the binary far-end and binary near-
  175. // end spectra. It is assumed the binary far-end spectrum has been added using
  176. // WebRtc_AddBinaryFarSpectrum() prior to this call. The value will be offset by
  177. // the lookahead (i.e. the lookahead should be subtracted from the returned
  178. // value).
  179. //
  180. // Inputs:
  181. // - self : Pointer to the delay estimation instance.
  182. // - binary_near_spectrum : Near-end binary spectrum of the current block.
  183. //
  184. // Output:
  185. // - self : Updated instance.
  186. //
  187. // Return value:
  188. // - delay : >= 0 - Calculated delay value.
  189. // -2 - Insufficient data for estimation.
  190. //
  191. int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* self,
  192. uint32_t binary_near_spectrum);
  193. // Returns the last calculated delay updated by the function
  194. // WebRtc_ProcessBinarySpectrum(...).
  195. //
  196. // Input:
  197. // - self : Pointer to the delay estimation instance.
  198. //
  199. // Return value:
  200. // - delay : >= 0 - Last calculated delay value
  201. // -2 - Insufficient data for estimation.
  202. //
  203. int WebRtc_binary_last_delay(BinaryDelayEstimator* self);
  204. // Returns the estimation quality of the last calculated delay updated by the
  205. // function WebRtc_ProcessBinarySpectrum(...). The estimation quality is a value
  206. // in the interval [0, 1]. The higher the value, the better the quality.
  207. //
  208. // Return value:
  209. // - delay_quality : >= 0 - Estimation quality of last calculated
  210. // delay value.
  211. float WebRtc_binary_last_delay_quality(BinaryDelayEstimator* self);
  212. // Updates the |mean_value| recursively with a step size of 2^-|factor|. This
  213. // function is used internally in the Binary Delay Estimator as well as the
  214. // Fixed point wrapper.
  215. //
  216. // Inputs:
  217. // - new_value : The new value the mean should be updated with.
  218. // - factor : The step size, in number of right shifts.
  219. //
  220. // Input/Output:
  221. // - mean_value : Pointer to the mean value.
  222. //
  223. void WebRtc_MeanEstimatorFix(int32_t new_value,
  224. int factor,
  225. int32_t* mean_value);
  226. } // namespace webrtc
  227. #endif // MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_