blob: cce6113a539c43c7989220879f5723ba39149b8c [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(
120 BinaryDelayEstimatorFarend* self, int delay_shift);
121
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000122// 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.org57f3a112013-01-25 22:02:15 +0000127// - self : Pointer to the delay estimation far-end
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000128// instance.
129// - binary_far_spectrum : Far-end binary spectrum.
130//
131// Output:
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +0000132// - self : Updated far-end instance.
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000133//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000134void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* self,
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000135 uint32_t binary_far_spectrum);
136
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000137// Releases the memory allocated by WebRtc_CreateBinaryDelayEstimator(...).
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000138//
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.org6a9835d2011-11-18 08:30:34 +0000143// Input:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000144// - self : Pointer to the binary delay estimation instance
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000145// which is the return value of
146// WebRtc_CreateBinaryDelayEstimator().
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000147//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000148void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* self);
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000149
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000150// Allocates the memory needed by the binary delay estimation. The memory needs
151// to be initialized separately through WebRtc_InitBinaryDelayEstimator(...).
152//
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000153// See WebRtc_CreateDelayEstimator(..) in delay_estimator_wrapper.c for detailed
154// description.
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000155BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000156 BinaryDelayEstimatorFarend* farend, int max_lookahead);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000157
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000158// 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.
169int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator* self,
170 int history_size);
171
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000172// Initializes the delay estimation instance created with
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000173// WebRtc_CreateBinaryDelayEstimator(...).
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000174//
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000175// Input:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000176// - self : Pointer to the delay estimation instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000177//
178// Output:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000179// - self : Initialized instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000180//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000181void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* self);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000182
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000183// 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//
192int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator* self,
193 int delay_shift);
194
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000195// 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.org94c213a2013-01-25 15:53:41 +0000200//
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000201// Inputs:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000202// - self : Pointer to the delay estimation instance.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000203// - binary_near_spectrum : Near-end binary spectrum of the current block.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000204//
205// Output:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000206// - self : Updated instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000207//
208// Return value:
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000209// - delay : >= 0 - Calculated delay value.
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000210// -2 - Insufficient data for estimation.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000211//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000212int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* self,
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000213 uint32_t binary_near_spectrum);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000214
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000215// Returns the last calculated delay updated by the function
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000216// WebRtc_ProcessBinarySpectrum(...).
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000217//
218// Input:
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000219// - self : Pointer to the delay estimation instance.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000220//
221// Return value:
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000222// - delay : >= 0 - Last calculated delay value
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000223// -2 - Insufficient data for estimation.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000224//
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000225int WebRtc_binary_last_delay(BinaryDelayEstimator* self);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000226
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000227// Returns the estimation quality of the last calculated delay updated by the
228// function WebRtc_ProcessBinarySpectrum(...). The estimation quality is a value
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000229// in the interval [0, 1]. The higher the value, the better the quality.
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000230//
231// Return value:
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000232// - delay_quality : >= 0 - Estimation quality of last calculated
233// delay value.
234float WebRtc_binary_last_delay_quality(BinaryDelayEstimator* self);
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000235
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000236// 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.comb47d4b22011-09-15 12:27:36 +0000239//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000240// 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.comb47d4b22011-09-15 12:27:36 +0000243//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000244// Input/Output:
245// - mean_value : Pointer to the mean value.
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000246//
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000247void WebRtc_MeanEstimatorFix(int32_t new_value,
248 int factor,
249 int32_t* mean_value);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000250
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200251#endif // MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_