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