blob: dbd09cf9a77efa0ca1b5a1beb69d19f6641df77a [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 */
67 WebRtc_UWord16 levelFiltFact; /* filter forgetting factor in Q8 */
niklas.enbom@webrtc.orgcd2f1352013-01-24 22:05:30 +000068 WebRtc_UWord16 buffLevelFilt; /* filtered buffer level in Q8 */
niklase@google.com470e71d2011-07-07 08:21:25 +000069
70 /* Inter-arrival time (iat) statistics */
71 WebRtc_Word32 iatProb[MAX_IAT + 1]; /* iat probabilities in Q30 */
72 WebRtc_Word16 iatProbFact; /* iat forgetting factor in Q15 */
73 WebRtc_UWord32 packetIatCountSamp; /* time (in timestamps) elapsed since last
74 packet arrival, based on RecOut calls */
niklas.enbom@webrtc.orgcd2f1352013-01-24 22:05:30 +000075 WebRtc_UWord16 optBufLevel; /* current optimal buffer level in Q8 */
niklase@google.com470e71d2011-07-07 08:21:25 +000076
77 /* Packet related information */
78 WebRtc_Word16 packetSpeechLenSamp; /* speech samples per incoming packet */
79 WebRtc_Word16 lastPackCNGorDTMF; /* indicates that the last received packet
80 contained special information */
81 WebRtc_UWord16 lastSeqNo; /* sequence number for last packet received */
82 WebRtc_UWord32 lastTimeStamp; /* timestamp for the last packet received */
83 WebRtc_Word32 sampleMemory; /* memory position for keeping track of how many
84 samples we cut during expand */
85 WebRtc_Word16 prevTimeScale; /* indicates that the last mode was an accelerate
86 or pre-emptive expand operation */
87 WebRtc_UWord32 timescaleHoldOff; /* counter that is shifted one step right each
88 RecOut call; time-scaling allowed when it has
89 reached 0 */
90 WebRtc_Word16 extraDelayMs; /* extra delay for sync with video */
91
92 /* Peak-detection */
93 /* vector with the latest peak periods (peak spacing in samples) */
94 WebRtc_UWord32 peakPeriodSamp[NUM_PEAKS];
95 /* vector with the latest peak heights (in packets) */
96 WebRtc_Word16 peakHeightPkt[NUM_PEAKS];
97 WebRtc_Word16 peakIndex; /* index for the vectors peakPeriodSamp and peakHeightPkt;
98 -1 if still waiting for first peak */
99 WebRtc_UWord16 peakThresholdPkt; /* definition of peak (in packets);
100 calculated from PEAK_HEIGHT */
101 WebRtc_UWord32 peakIatCountSamp; /* samples elapsed since last peak was observed */
102 WebRtc_UWord32 curPeakPeriod; /* current maximum of peakPeriodSamp vector */
103 WebRtc_Word16 curPeakHeight; /* derived from peakHeightPkt vector;
104 used as optimal buffer level in peak mode */
105 WebRtc_Word16 peakModeDisabled; /* ==0 if peak mode can be engaged; >0 if not */
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000106 uint16_t peakFound; /* 1 if peaks are detected and extra delay is applied;
107 * 0 otherwise. */
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
109 /* Post-call statistics */
110 WebRtc_UWord32 countIAT500ms; /* number of times we got small network outage */
111 WebRtc_UWord32 countIAT1000ms; /* number of times we got medium network outage */
112 WebRtc_UWord32 countIAT2000ms; /* number of times we got large network outage */
113 WebRtc_UWord32 longestIATms; /* mSec duration of longest network outage */
114
115 WebRtc_Word16 cSumIatQ8; /* cumulative sum of inter-arrival times */
116 WebRtc_Word16 maxCSumIatQ8; /* max cumulative sum IAT */
117 WebRtc_UWord32 maxCSumUpdateTimer;/* time elapsed since maximum was observed */
118
119} AutomodeInst_t;
120
121/*************/
122/* Functions */
123/*************/
124
125/****************************************************************************
126 * WebRtcNetEQ_UpdateIatStatistics(...)
127 *
128 * Update the packet inter-arrival time statistics when a new packet arrives.
129 * This function should be called for every arriving packet, with some
130 * exceptions when using DTX/VAD and DTMF. A new optimal buffer level is
131 * calculated after the update.
132 *
133 * Input:
134 * - inst : Automode instance
135 * - maxBufLen : Maximum number of packets the buffer can hold
136 * - seqNumber : RTP sequence number of incoming packet
137 * - timeStamp : RTP timestamp of incoming packet
138 * - fsHz : Sample rate in Hz
139 * - mdCodec : Non-zero if the current codec is a multiple-
140 * description codec
141 * - streamingMode : A non-zero value will increase jitter robustness (and delay)
142 *
143 * Output:
144 * - inst : Updated automode instance
145 *
146 * Return value : 0 - Ok
147 * <0 - Error
148 */
149
150int WebRtcNetEQ_UpdateIatStatistics(AutomodeInst_t *inst, int maxBufLen,
151 WebRtc_UWord16 seqNumber, WebRtc_UWord32 timeStamp,
152 WebRtc_Word32 fsHz, int mdCodec, int streamingMode);
153
154/****************************************************************************
155 * WebRtcNetEQ_CalcOptimalBufLvl(...)
156 *
157 * Calculate the optimal buffer level based on packet inter-arrival time
158 * statistics.
159 *
160 * Input:
161 * - inst : Automode instance
162 * - fsHz : Sample rate in Hz
163 * - mdCodec : Non-zero if the current codec is a multiple-
164 * description codec
165 * - timeIatPkts : Currently observed inter-arrival time in packets
166 * - streamingMode : A non-zero value will increase jitter robustness (and delay)
167 *
168 * Output:
169 * - inst : Updated automode instance
170 *
171 * Return value : >0 - Optimal buffer level
172 * <0 - Error
173 */
174
175WebRtc_Word16 WebRtcNetEQ_CalcOptimalBufLvl(AutomodeInst_t *inst, WebRtc_Word32 fsHz,
176 int mdCodec, WebRtc_UWord32 timeIatPkts,
177 int streamingMode);
178
179/****************************************************************************
180 * WebRtcNetEQ_BufferLevelFilter(...)
181 *
182 * Update filtered buffer level. The function must be called once for each
183 * RecOut call, since the timing of automode hinges on counters that are
184 * updated by this function.
185 *
186 * Input:
187 * - curSizeMs8 : Total length of unused speech data in packet buffer
188 * and sync buffer, in ms * 8
189 * - inst : Automode instance
190 * - sampPerCall : Number of samples per RecOut call
191 * - fsMult : Sample rate in Hz divided by 8000
192 *
193 * Output:
194 * - inst : Updated automode instance
195 *
196 * Return value : 0 - Ok
197 * : <0 - Error
198 */
199
200int WebRtcNetEQ_BufferLevelFilter(WebRtc_Word32 curSizeMs8, AutomodeInst_t *inst,
201 int sampPerCall, WebRtc_Word16 fsMult);
202
203/****************************************************************************
204 * WebRtcNetEQ_SetPacketSpeechLen(...)
205 *
206 * Provide the number of speech samples extracted from a packet to the
207 * automode instance. Several of the calculations within automode depend
208 * on knowing the packet size.
209 *
210 *
211 * Input:
212 * - inst : Automode instance
213 * - newLenSamp : Number of samples per RecOut call
214 * - fsHz : Sample rate in Hz
215 *
216 * Output:
217 * - inst : Updated automode instance
218 *
219 * Return value : 0 - Ok
220 * <0 - Error
221 */
222
223int WebRtcNetEQ_SetPacketSpeechLen(AutomodeInst_t *inst, WebRtc_Word16 newLenSamp,
224 WebRtc_Word32 fsHz);
225
226/****************************************************************************
227 * WebRtcNetEQ_ResetAutomode(...)
228 *
229 * Reset the automode instance.
230 *
231 *
232 * Input:
233 * - inst : Automode instance
234 * - maxBufLenPackets : Maximum number of packets that the packet
235 * buffer can hold (>1)
236 *
237 * Output:
238 * - inst : Updated automode instance
239 *
240 * Return value : 0 - Ok
241 */
242
243int WebRtcNetEQ_ResetAutomode(AutomodeInst_t *inst, int maxBufLenPackets);
244
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000245/****************************************************************************
246 * WebRtcNetEQ_AverageIAT(...)
247 *
248 * Calculate the average inter-arrival time based on current statistics.
249 * The average is expressed in parts per million relative the nominal. That is,
250 * if the average inter-arrival time is equal to the nominal frame time,
251 * the return value is zero. A positive value corresponds to packet spacing
252 * being too large, while a negative value means that the packets arrive with
253 * less spacing than expected.
254 *
255 *
256 * Input:
257 * - inst : Automode instance.
258 *
259 * Return value : Average relative inter-arrival time in samples.
260 */
261
262int32_t WebRtcNetEQ_AverageIAT(const AutomodeInst_t *inst);
263
niklase@google.com470e71d2011-07-07 08:21:25 +0000264#endif /* AUTOMODE_H */