blob: 16d72e8d563e75b10477caaf465e9e28adaab1f1 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 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
11/*
12 * This file contains the functionality for automatic buffer level optimization.
13 */
14
15#ifndef AUTOMODE_H
16#define AUTOMODE_H
17
18#include "typedefs.h"
19
20/*************/
21/* Constants */
22/*************/
23
24/* The beta parameter defines the trade-off between delay and underrun probability. */
25/* It is defined through its inverse in Q30 */
26#define AUTOMODE_BETA_INV_Q30 53687091 /* 1/20 in Q30 */
27#define AUTOMODE_STREAMING_BETA_INV_Q30 536871 /* 1/2000 in Q30 */
28
29/* Forgetting factor for the inter-arrival time statistics */
30#define IAT_PROB_FACT 32745 /* 0.9993 in Q15 */
31
32/* Maximum inter-arrival time to register (in "packet-times") */
33#define MAX_IAT 64
34#define PEAK_HEIGHT 20 /* 0.08s in Q8 */
35
36/* The value (1<<5) sets maximum accelerate "speed" to about 100 ms/s */
37#define AUTOMODE_TIMESCALE_LIMIT (1<<5)
38
39/* Peak mode related parameters */
40/* Number of peaks in peak vector; must be a power of 2 */
41#define NUM_PEAKS 8
42
43/* Must be NUM_PEAKS-1 */
44#define PEAK_INDEX_MASK 0x0007
45
46/* Longest accepted peak distance */
47#define MAX_PEAK_PERIOD 10
48#define MAX_STREAMING_PEAK_PERIOD 600 /* 10 minutes */
49
50/* Number of peaks required before peak mode can be engaged */
51#define NUM_PEAKS_REQUIRED 3
52
53/* Drift term for cumulative sum */
54#define CSUM_IAT_DRIFT 2
55
56/*******************/
57/* Automode struct */
58/*******************/
59
60/* The automode struct is a sub-struct of the
61 bufstats-struct (BufstatsInst_t). */
62
63typedef struct
64{
65
66 /* Filtered current buffer level */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000067 uint16_t levelFiltFact; /* filter forgetting factor in Q8 */
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000068 int buffLevelFilt; /* filtered buffer level in Q8 */
niklase@google.com470e71d2011-07-07 08:21:25 +000069
70 /* Inter-arrival time (iat) statistics */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000071 int32_t iatProb[MAX_IAT + 1]; /* iat probabilities in Q30 */
72 int16_t iatProbFact; /* iat forgetting factor in Q15 */
73 uint32_t packetIatCountSamp; /* time (in timestamps) elapsed since last
niklase@google.com470e71d2011-07-07 08:21:25 +000074 packet arrival, based on RecOut calls */
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000075 int optBufLevel; /* current optimal buffer level in Q8 */
niklase@google.com470e71d2011-07-07 08:21:25 +000076
77 /* Packet related information */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000078 int16_t packetSpeechLenSamp; /* speech samples per incoming packet */
79 int16_t lastPackCNGorDTMF; /* indicates that the last received packet
niklase@google.com470e71d2011-07-07 08:21:25 +000080 contained special information */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000081 uint16_t lastSeqNo; /* sequence number for last packet received */
82 uint32_t lastTimeStamp; /* timestamp for the last packet received */
83 int32_t sampleMemory; /* memory position for keeping track of how many
niklase@google.com470e71d2011-07-07 08:21:25 +000084 samples we cut during expand */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000085 int16_t prevTimeScale; /* indicates that the last mode was an accelerate
niklase@google.com470e71d2011-07-07 08:21:25 +000086 or pre-emptive expand operation */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000087 uint32_t timescaleHoldOff; /* counter that is shifted one step right each
niklase@google.com470e71d2011-07-07 08:21:25 +000088 RecOut call; time-scaling allowed when it has
89 reached 0 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000090 int16_t extraDelayMs; /* extra delay for sync with video */
niklase@google.com470e71d2011-07-07 08:21:25 +000091
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000092 int minimum_delay_ms; /* Desired delay, NetEq maintains this amount of
93 delay unless jitter statistics suggests a higher value. */
pwestin@webrtc.org401ef362013-08-06 21:01:36 +000094 int maximum_delay_ms; /* Max desired delay, NetEq will not go above this
95 amount of delay even if jitter statistics suggests a higher value. */
96
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000097 int required_delay_q8; /* Smallest delay required. This is computed
98 according to inter-arrival time and playout mode. It has the same unit
99 as |optBufLevel|. */
100
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 /* Peak-detection */
102 /* vector with the latest peak periods (peak spacing in samples) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000103 uint32_t peakPeriodSamp[NUM_PEAKS];
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 /* vector with the latest peak heights (in packets) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000105 int16_t peakHeightPkt[NUM_PEAKS];
106 int16_t peakIndex; /* index for the vectors peakPeriodSamp and peakHeightPkt;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 -1 if still waiting for first peak */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000108 uint16_t peakThresholdPkt; /* definition of peak (in packets);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 calculated from PEAK_HEIGHT */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000110 uint32_t peakIatCountSamp; /* samples elapsed since last peak was observed */
111 uint32_t curPeakPeriod; /* current maximum of peakPeriodSamp vector */
112 int16_t curPeakHeight; /* derived from peakHeightPkt vector;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 used as optimal buffer level in peak mode */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000114 int16_t peakModeDisabled; /* ==0 if peak mode can be engaged; >0 if not */
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000115 uint16_t peakFound; /* 1 if peaks are detected and extra delay is applied;
116 * 0 otherwise. */
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
118 /* Post-call statistics */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000119 uint32_t countIAT500ms; /* number of times we got small network outage */
120 uint32_t countIAT1000ms; /* number of times we got medium network outage */
121 uint32_t countIAT2000ms; /* number of times we got large network outage */
122 uint32_t longestIATms; /* mSec duration of longest network outage */
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000124 int16_t cSumIatQ8; /* cumulative sum of inter-arrival times */
125 int16_t maxCSumIatQ8; /* max cumulative sum IAT */
126 uint32_t maxCSumUpdateTimer;/* time elapsed since maximum was observed */
niklase@google.com470e71d2011-07-07 08:21:25 +0000127} AutomodeInst_t;
128
129/*************/
130/* Functions */
131/*************/
132
133/****************************************************************************
134 * WebRtcNetEQ_UpdateIatStatistics(...)
135 *
136 * Update the packet inter-arrival time statistics when a new packet arrives.
137 * This function should be called for every arriving packet, with some
138 * exceptions when using DTX/VAD and DTMF. A new optimal buffer level is
139 * calculated after the update.
140 *
141 * Input:
142 * - inst : Automode instance
143 * - maxBufLen : Maximum number of packets the buffer can hold
144 * - seqNumber : RTP sequence number of incoming packet
145 * - timeStamp : RTP timestamp of incoming packet
146 * - fsHz : Sample rate in Hz
147 * - mdCodec : Non-zero if the current codec is a multiple-
148 * description codec
149 * - streamingMode : A non-zero value will increase jitter robustness (and delay)
150 *
151 * Output:
152 * - inst : Updated automode instance
153 *
154 * Return value : 0 - Ok
155 * <0 - Error
156 */
157
158int WebRtcNetEQ_UpdateIatStatistics(AutomodeInst_t *inst, int maxBufLen,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000159 uint16_t seqNumber, uint32_t timeStamp,
160 int32_t fsHz, int mdCodec, int streamingMode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
162/****************************************************************************
163 * WebRtcNetEQ_CalcOptimalBufLvl(...)
164 *
165 * Calculate the optimal buffer level based on packet inter-arrival time
166 * statistics.
167 *
168 * Input:
169 * - inst : Automode instance
170 * - fsHz : Sample rate in Hz
171 * - mdCodec : Non-zero if the current codec is a multiple-
172 * description codec
173 * - timeIatPkts : Currently observed inter-arrival time in packets
174 * - streamingMode : A non-zero value will increase jitter robustness (and delay)
175 *
176 * Output:
177 * - inst : Updated automode instance
178 *
179 * Return value : >0 - Optimal buffer level
180 * <0 - Error
181 */
182
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000183int16_t WebRtcNetEQ_CalcOptimalBufLvl(AutomodeInst_t *inst, int32_t fsHz,
184 int mdCodec, uint32_t timeIatPkts,
185 int streamingMode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
187/****************************************************************************
188 * WebRtcNetEQ_BufferLevelFilter(...)
189 *
190 * Update filtered buffer level. The function must be called once for each
191 * RecOut call, since the timing of automode hinges on counters that are
192 * updated by this function.
193 *
194 * Input:
195 * - curSizeMs8 : Total length of unused speech data in packet buffer
196 * and sync buffer, in ms * 8
197 * - inst : Automode instance
198 * - sampPerCall : Number of samples per RecOut call
199 * - fsMult : Sample rate in Hz divided by 8000
200 *
201 * Output:
202 * - inst : Updated automode instance
203 *
204 * Return value : 0 - Ok
205 * : <0 - Error
206 */
207
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000208int WebRtcNetEQ_BufferLevelFilter(int32_t curSizeMs8, AutomodeInst_t *inst,
209 int sampPerCall, int16_t fsMult);
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
211/****************************************************************************
212 * WebRtcNetEQ_SetPacketSpeechLen(...)
213 *
214 * Provide the number of speech samples extracted from a packet to the
215 * automode instance. Several of the calculations within automode depend
216 * on knowing the packet size.
217 *
218 *
219 * Input:
220 * - inst : Automode instance
221 * - newLenSamp : Number of samples per RecOut call
222 * - fsHz : Sample rate in Hz
223 *
224 * Output:
225 * - inst : Updated automode instance
226 *
227 * Return value : 0 - Ok
228 * <0 - Error
229 */
230
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000231int WebRtcNetEQ_SetPacketSpeechLen(AutomodeInst_t *inst, int16_t newLenSamp,
232 int32_t fsHz);
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
234/****************************************************************************
235 * WebRtcNetEQ_ResetAutomode(...)
236 *
237 * Reset the automode instance.
238 *
239 *
240 * Input:
241 * - inst : Automode instance
242 * - maxBufLenPackets : Maximum number of packets that the packet
243 * buffer can hold (>1)
244 *
245 * Output:
246 * - inst : Updated automode instance
247 *
248 * Return value : 0 - Ok
249 */
250
251int WebRtcNetEQ_ResetAutomode(AutomodeInst_t *inst, int maxBufLenPackets);
252
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000253/****************************************************************************
254 * WebRtcNetEQ_AverageIAT(...)
255 *
256 * Calculate the average inter-arrival time based on current statistics.
257 * The average is expressed in parts per million relative the nominal. That is,
258 * if the average inter-arrival time is equal to the nominal frame time,
259 * the return value is zero. A positive value corresponds to packet spacing
260 * being too large, while a negative value means that the packets arrive with
261 * less spacing than expected.
262 *
263 *
264 * Input:
265 * - inst : Automode instance.
266 *
267 * Return value : Average relative inter-arrival time in samples.
268 */
269
270int32_t WebRtcNetEQ_AverageIAT(const AutomodeInst_t *inst);
271
niklase@google.com470e71d2011-07-07 08:21:25 +0000272#endif /* AUTOMODE_H */