blob: 11483ec6b57ccd7e8e871d76846d11aaa12effc0 [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
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "typedefs.h" // NOLINT(build/include)
bjornv@google.comb47d4b22011-09-15 12:27:36 +000018
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +000019static const int32_t kMaxBitCountsQ9 = (32 << 9); // 32 matching bits in Q9.
20
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000021typedef struct {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000022 // Pointer to bit counts.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000023 int* far_bit_counts;
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000024 // Binary history variables.
25 uint32_t* binary_far_history;
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000026 int history_size;
27} BinaryDelayEstimatorFarend;
bjornv@google.comb47d4b22011-09-15 12:27:36 +000028
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000029typedef struct {
30 // Pointer to bit counts.
31 int32_t* mean_bit_counts;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000032 // Array only used locally in ProcessBinarySpectrum() but whose size is
33 // determined at run-time.
34 int32_t* bit_counts;
35
andrew@webrtc.org828af1b2011-11-22 22:40:27 +000036 // Binary history variables.
andrew@webrtc.org828af1b2011-11-22 22:40:27 +000037 uint32_t* binary_near_history;
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +000038 int near_history_size;
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +000039 int history_size;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000040
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000041 // Delay estimation variables.
42 int32_t minimum_probability;
43 int last_delay_probability;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000044
andrew@webrtc.org828af1b2011-11-22 22:40:27 +000045 // Delay memory.
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000046 int last_delay;
47
bjornv@webrtc.orgbd41a842013-11-28 14:58:35 +000048 // Robust validation
49 int robust_validation_enabled;
bjornv@webrtc.orgbccd53d2014-01-08 08:18:15 +000050 int allowed_offset;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +000051 int last_candidate_delay;
52 int compare_delay;
53 int candidate_hits;
54 float* histogram;
55 float last_delay_histogram;
bjornv@webrtc.orgbd41a842013-11-28 14:58:35 +000056
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +000057 // For dynamically changing the lookahead when using SoftReset...().
58 int lookahead;
59
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000060 // Far-end binary spectrum history buffer etc.
61 BinaryDelayEstimatorFarend* farend;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000062} BinaryDelayEstimator;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +000063
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +000064// 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//
71void 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//
86BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend(
87 int history_size);
88
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +000089// 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.
99int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend* self,
100 int history_size);
101
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000102// 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//
111void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self);
112
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000113// 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//
119void WebRtc_SoftResetBinaryDelayEstimatorFarend(
Yves Gerey665174f2018-06-19 15:03:05 +0200120 BinaryDelayEstimatorFarend* self,
121 int delay_shift);
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000122
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000123// Adds the binary far-end spectrum to the internal far-end history buffer. This
124// spectrum is used as reference when calculating the delay using
125// WebRtc_ProcessBinarySpectrum().
126//
127// Inputs:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000128// - self : Pointer to the delay estimation far-end
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000129// instance.
130// - binary_far_spectrum : Far-end binary spectrum.
131//
132// Output:
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +0000133// - self : Updated far-end instance.
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000134//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000135void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* self,
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000136 uint32_t binary_far_spectrum);
137
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000138// Releases the memory allocated by WebRtc_CreateBinaryDelayEstimator(...).
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000139//
140// Note that BinaryDelayEstimator utilizes BinaryDelayEstimatorFarend, but does
141// not take ownership of it, hence the BinaryDelayEstimator has to be torn down
142// before the far-end.
143//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000144// Input:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000145// - self : Pointer to the binary delay estimation instance
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000146// which is the return value of
147// WebRtc_CreateBinaryDelayEstimator().
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000148//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000149void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* self);
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000150
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000151// Allocates the memory needed by the binary delay estimation. The memory needs
152// to be initialized separately through WebRtc_InitBinaryDelayEstimator(...).
153//
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000154// See WebRtc_CreateDelayEstimator(..) in delay_estimator_wrapper.c for detailed
155// description.
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000156BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
Yves Gerey665174f2018-06-19 15:03:05 +0200157 BinaryDelayEstimatorFarend* farend,
158 int max_lookahead);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000159
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000160// Re-allocates |history_size| dependent buffers. The far-end buffers will be
161// updated at the same time if needed.
162//
163// Input:
164// - self : Pointer to the binary estimation instance which is
165// the return value of
166// WebRtc_CreateBinaryDelayEstimator().
167// - history_size : Size of the history buffers.
168//
169// Return value:
170// - history_size : The history size allocated.
171int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator* self,
172 int history_size);
173
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000174// Initializes the delay estimation instance created with
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000175// WebRtc_CreateBinaryDelayEstimator(...).
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000176//
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000177// Input:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000178// - self : Pointer to the delay estimation instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000179//
180// Output:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000181// - self : Initialized instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000182//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000183void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* self);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000184
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000185// Soft resets the delay estimation instance created with
186// WebRtc_CreateBinaryDelayEstimator(...).
187//
188// Input:
189// - delay_shift : The amount of blocks to shift history buffers.
190//
191// Return value:
192// - actual_shifts : The actual number of shifts performed.
193//
194int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator* self,
195 int delay_shift);
196
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000197// Estimates and returns the delay between the binary far-end and binary near-
198// end spectra. It is assumed the binary far-end spectrum has been added using
199// WebRtc_AddBinaryFarSpectrum() prior to this call. The value will be offset by
200// the lookahead (i.e. the lookahead should be subtracted from the returned
201// value).
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000202//
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000203// Inputs:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000204// - self : Pointer to the delay estimation instance.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000205// - binary_near_spectrum : Near-end binary spectrum of the current block.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000206//
207// Output:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000208// - self : Updated instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000209//
210// Return value:
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000211// - delay : >= 0 - Calculated delay value.
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000212// -2 - Insufficient data for estimation.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000213//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000214int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* self,
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000215 uint32_t binary_near_spectrum);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000216
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000217// Returns the last calculated delay updated by the function
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000218// WebRtc_ProcessBinarySpectrum(...).
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000219//
220// Input:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000221// - self : Pointer to the delay estimation instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000222//
223// Return value:
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000224// - delay : >= 0 - Last calculated delay value
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000225// -2 - Insufficient data for estimation.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000226//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000227int WebRtc_binary_last_delay(BinaryDelayEstimator* self);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000228
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000229// Returns the estimation quality of the last calculated delay updated by the
230// function WebRtc_ProcessBinarySpectrum(...). The estimation quality is a value
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000231// in the interval [0, 1]. The higher the value, the better the quality.
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000232//
233// Return value:
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000234// - delay_quality : >= 0 - Estimation quality of last calculated
235// delay value.
236float WebRtc_binary_last_delay_quality(BinaryDelayEstimator* self);
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000237
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000238// Updates the |mean_value| recursively with a step size of 2^-|factor|. This
239// function is used internally in the Binary Delay Estimator as well as the
240// Fixed point wrapper.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000241//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000242// Inputs:
243// - new_value : The new value the mean should be updated with.
244// - factor : The step size, in number of right shifts.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000245//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000246// Input/Output:
247// - mean_value : Pointer to the mean value.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000248//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000249void WebRtc_MeanEstimatorFix(int32_t new_value,
250 int factor,
251 int32_t* mean_value);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000252
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200253#endif // MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_