blob: b6fc36a75937055a1b904bf4b7170e7dcd1f00ed [file] [log] [blame]
bjornv@google.comb47d4b22011-09-15 12:27:36 +00001/*
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
bjornv@google.comb47d4b22011-09-15 12:27:36 +00003 *
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.org6a9835d2011-11-18 08:30:34 +000011// Performs delay estimation on binary converted spectra.
bjornv@google.comb47d4b22011-09-15 12:27:36 +000012// The return value is 0 - OK and -1 - Error, unless otherwise stated.
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#ifndef MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
15#define MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
bjornv@google.comb47d4b22011-09-15 12:27:36 +000016
Niels Möllera12c42a2018-07-25 16:05:48 +020017#include <stdint.h>
bjornv@google.comb47d4b22011-09-15 12:27:36 +000018
Per Åhgrene7175c92020-03-20 16:43:34 +010019namespace webrtc {
20
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +000021static const int32_t kMaxBitCountsQ9 = (32 << 9); // 32 matching bits in Q9.
22
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000023typedef struct {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000024 // Pointer to bit counts.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000025 int* far_bit_counts;
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000026 // Binary history variables.
27 uint32_t* binary_far_history;
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000028 int history_size;
29} BinaryDelayEstimatorFarend;
bjornv@google.comb47d4b22011-09-15 12:27:36 +000030
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000031typedef struct {
32 // Pointer to bit counts.
33 int32_t* mean_bit_counts;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000034 // Array only used locally in ProcessBinarySpectrum() but whose size is
35 // determined at run-time.
36 int32_t* bit_counts;
37
andrew@webrtc.org828af1b2011-11-22 22:40:27 +000038 // Binary history variables.
andrew@webrtc.org828af1b2011-11-22 22:40:27 +000039 uint32_t* binary_near_history;
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +000040 int near_history_size;
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +000041 int history_size;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000042
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000043 // Delay estimation variables.
44 int32_t minimum_probability;
45 int last_delay_probability;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000046
andrew@webrtc.org828af1b2011-11-22 22:40:27 +000047 // Delay memory.
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000048 int last_delay;
49
bjornv@webrtc.orgbd41a842013-11-28 14:58:35 +000050 // Robust validation
51 int robust_validation_enabled;
bjornv@webrtc.orgbccd53d2014-01-08 08:18:15 +000052 int allowed_offset;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +000053 int last_candidate_delay;
54 int compare_delay;
55 int candidate_hits;
56 float* histogram;
57 float last_delay_histogram;
bjornv@webrtc.orgbd41a842013-11-28 14:58:35 +000058
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +000059 // For dynamically changing the lookahead when using SoftReset...().
60 int lookahead;
61
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000062 // Far-end binary spectrum history buffer etc.
63 BinaryDelayEstimatorFarend* farend;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000064} BinaryDelayEstimator;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000065
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000066// Releases the memory allocated by
67// WebRtc_CreateBinaryDelayEstimatorFarend(...).
68// Input:
69// - self : Pointer to the binary delay estimation far-end
70// instance which is the return value of
71// WebRtc_CreateBinaryDelayEstimatorFarend().
72//
73void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self);
74
75// Allocates the memory needed by the far-end part of the binary delay
76// estimation. The memory needs to be initialized separately through
77// WebRtc_InitBinaryDelayEstimatorFarend(...).
78//
79// Inputs:
80// - history_size : Size of the far-end binary spectrum history.
81//
82// Return value:
83// - BinaryDelayEstimatorFarend*
Artem Titov0b489302021-07-28 20:50:03 +020084// : Created `handle`. If the memory can't be allocated
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000085// or if any of the input parameters are invalid NULL
86// is returned.
87//
88BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend(
89 int history_size);
90
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +000091// Re-allocates the buffers.
92//
93// Inputs:
94// - self : Pointer to the binary estimation far-end instance
95// which is the return value of
96// WebRtc_CreateBinaryDelayEstimatorFarend().
97// - history_size : Size of the far-end binary spectrum history.
98//
99// Return value:
100// - history_size : The history size allocated.
101int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend* self,
102 int history_size);
103
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000104// Initializes the delay estimation far-end instance created with
105// WebRtc_CreateBinaryDelayEstimatorFarend(...).
106//
107// Input:
108// - self : Pointer to the delay estimation far-end instance.
109//
110// Output:
111// - self : Initialized far-end instance.
112//
113void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self);
114
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000115// Soft resets the delay estimation far-end instance created with
116// WebRtc_CreateBinaryDelayEstimatorFarend(...).
117//
118// Input:
119// - delay_shift : The amount of blocks to shift history buffers.
120//
121void WebRtc_SoftResetBinaryDelayEstimatorFarend(
Yves Gerey665174f2018-06-19 15:03:05 +0200122 BinaryDelayEstimatorFarend* self,
123 int delay_shift);
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000124
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000125// Adds the binary far-end spectrum to the internal far-end history buffer. This
126// spectrum is used as reference when calculating the delay using
127// WebRtc_ProcessBinarySpectrum().
128//
129// Inputs:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000130// - self : Pointer to the delay estimation far-end
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000131// instance.
132// - binary_far_spectrum : Far-end binary spectrum.
133//
134// Output:
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +0000135// - self : Updated far-end instance.
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000136//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000137void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* self,
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000138 uint32_t binary_far_spectrum);
139
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000140// Releases the memory allocated by WebRtc_CreateBinaryDelayEstimator(...).
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000141//
142// Note that BinaryDelayEstimator utilizes BinaryDelayEstimatorFarend, but does
143// not take ownership of it, hence the BinaryDelayEstimator has to be torn down
144// before the far-end.
145//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000146// Input:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000147// - self : Pointer to the binary delay estimation instance
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000148// which is the return value of
149// WebRtc_CreateBinaryDelayEstimator().
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000150//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000151void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* self);
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000152
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000153// Allocates the memory needed by the binary delay estimation. The memory needs
154// to be initialized separately through WebRtc_InitBinaryDelayEstimator(...).
155//
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000156// See WebRtc_CreateDelayEstimator(..) in delay_estimator_wrapper.c for detailed
157// description.
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000158BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
Yves Gerey665174f2018-06-19 15:03:05 +0200159 BinaryDelayEstimatorFarend* farend,
160 int max_lookahead);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000161
Artem Titov0b489302021-07-28 20:50:03 +0200162// Re-allocates `history_size` dependent buffers. The far-end buffers will be
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000163// updated at the same time if needed.
164//
165// Input:
166// - self : Pointer to the binary estimation instance which is
167// the return value of
168// WebRtc_CreateBinaryDelayEstimator().
169// - history_size : Size of the history buffers.
170//
171// Return value:
172// - history_size : The history size allocated.
173int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator* self,
174 int history_size);
175
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000176// Initializes the delay estimation instance created with
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000177// WebRtc_CreateBinaryDelayEstimator(...).
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000178//
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000179// Input:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000180// - self : Pointer to the delay estimation instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000181//
182// Output:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000183// - self : Initialized instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000184//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000185void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* self);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000186
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000187// Soft resets the delay estimation instance created with
188// WebRtc_CreateBinaryDelayEstimator(...).
189//
190// Input:
191// - delay_shift : The amount of blocks to shift history buffers.
192//
193// Return value:
194// - actual_shifts : The actual number of shifts performed.
195//
196int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator* self,
197 int delay_shift);
198
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000199// Estimates and returns the delay between the binary far-end and binary near-
200// end spectra. It is assumed the binary far-end spectrum has been added using
201// WebRtc_AddBinaryFarSpectrum() prior to this call. The value will be offset by
202// the lookahead (i.e. the lookahead should be subtracted from the returned
203// value).
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000204//
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000205// Inputs:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000206// - self : Pointer to the delay estimation instance.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000207// - binary_near_spectrum : Near-end binary spectrum of the current block.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000208//
209// Output:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000210// - self : Updated instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000211//
212// Return value:
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000213// - delay : >= 0 - Calculated delay value.
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000214// -2 - Insufficient data for estimation.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000215//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000216int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* self,
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000217 uint32_t binary_near_spectrum);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000218
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000219// Returns the last calculated delay updated by the function
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000220// WebRtc_ProcessBinarySpectrum(...).
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000221//
222// Input:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000223// - self : Pointer to the delay estimation instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000224//
225// Return value:
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000226// - delay : >= 0 - Last calculated delay value
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000227// -2 - Insufficient data for estimation.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000228//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000229int WebRtc_binary_last_delay(BinaryDelayEstimator* self);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000230
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000231// Returns the estimation quality of the last calculated delay updated by the
232// function WebRtc_ProcessBinarySpectrum(...). The estimation quality is a value
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000233// in the interval [0, 1]. The higher the value, the better the quality.
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000234//
235// Return value:
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000236// - delay_quality : >= 0 - Estimation quality of last calculated
237// delay value.
238float WebRtc_binary_last_delay_quality(BinaryDelayEstimator* self);
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000239
Artem Titov0b489302021-07-28 20:50:03 +0200240// Updates the `mean_value` recursively with a step size of 2^-`factor`. This
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000241// function is used internally in the Binary Delay Estimator as well as the
242// Fixed point wrapper.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000243//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000244// Inputs:
245// - new_value : The new value the mean should be updated with.
246// - factor : The step size, in number of right shifts.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000247//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000248// Input/Output:
249// - mean_value : Pointer to the mean value.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000250//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000251void WebRtc_MeanEstimatorFix(int32_t new_value,
252 int factor,
253 int32_t* mean_value);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000254
Per Åhgrene7175c92020-03-20 16:43:34 +0100255} // namespace webrtc
256
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200257#endif // MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_