blob: a922448591b3edb29b1126fa0f379b010d59b371 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tina.legrand@webrtc.org0de1ee32012-05-28 11:37:50 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +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
11/*
12 * This file contains the implementation of automatic buffer level optimization.
13 */
14
15#include "automode.h"
16
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +000017#include <assert.h>
18
niklase@google.com470e71d2011-07-07 08:21:25 +000019#include "signal_processing_library.h"
20
21#include "neteq_defines.h"
22
23#ifdef NETEQ_DELAY_LOGGING
24/* special code for offline delay logging */
25#include <stdio.h>
26#include "delay_logging.h"
27
28extern FILE *delay_fid2; /* file pointer to delay log file */
29#endif /* NETEQ_DELAY_LOGGING */
30
31
32int WebRtcNetEQ_UpdateIatStatistics(AutomodeInst_t *inst, int maxBufLen,
pbos@webrtc.org0946a562013-04-09 00:28:06 +000033 uint16_t seqNumber, uint32_t timeStamp,
34 int32_t fsHz, int mdCodec, int streamingMode)
niklase@google.com470e71d2011-07-07 08:21:25 +000035{
pbos@webrtc.org0946a562013-04-09 00:28:06 +000036 uint32_t timeIat; /* inter-arrival time */
niklase@google.com470e71d2011-07-07 08:21:25 +000037 int i;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000038 int32_t tempsum = 0; /* temp summation */
39 int32_t tempvar; /* temporary variable */
niklase@google.com470e71d2011-07-07 08:21:25 +000040 int retval = 0; /* return value */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000041 int16_t packetLenSamp; /* packet speech length in samples */
niklase@google.com470e71d2011-07-07 08:21:25 +000042
43 /****************/
44 /* Sanity check */
45 /****************/
46
47 if (maxBufLen <= 1 || fsHz <= 0)
48 {
49 /* maxBufLen must be at least 2 and fsHz must both be strictly positive */
50 return -1;
51 }
52
53 /****************************/
54 /* Update packet statistics */
55 /****************************/
56
57 /* Try calculating packet length from current and previous timestamps */
58 if ((timeStamp <= inst->lastTimeStamp) || (seqNumber <= inst->lastSeqNo))
59 {
60 /* Wrong timestamp or sequence order; revert to backup plan */
61 packetLenSamp = inst->packetSpeechLenSamp; /* use stored value */
62 }
tina.legrand@webrtc.org0de1ee32012-05-28 11:37:50 +000063 else
niklase@google.com470e71d2011-07-07 08:21:25 +000064 {
65 /* calculate timestamps per packet */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000066 packetLenSamp = (int16_t) WebRtcSpl_DivU32U16(timeStamp - inst->lastTimeStamp,
niklase@google.com470e71d2011-07-07 08:21:25 +000067 seqNumber - inst->lastSeqNo);
68 }
69
70 /* Check that the packet size is positive; if not, the statistics cannot be updated. */
71 if (packetLenSamp > 0)
72 { /* packet size ok */
73
74 /* calculate inter-arrival time in integer packets (rounding down) */
75 timeIat = WebRtcSpl_DivW32W16(inst->packetIatCountSamp, packetLenSamp);
76
77 /* Special operations for streaming mode */
78 if (streamingMode != 0)
79 {
80 /*
81 * Calculate IAT in Q8, including fractions of a packet (i.e., more accurate
82 * than timeIat).
83 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000084 int16_t timeIatQ8 = (int16_t) WebRtcSpl_DivW32W16(
niklase@google.com470e71d2011-07-07 08:21:25 +000085 WEBRTC_SPL_LSHIFT_W32(inst->packetIatCountSamp, 8), packetLenSamp);
86
87 /*
88 * Calculate cumulative sum iat with sequence number compensation (ideal arrival
89 * times makes this sum zero).
90 */
91 inst->cSumIatQ8 += (timeIatQ8
92 - WEBRTC_SPL_LSHIFT_W32(seqNumber - inst->lastSeqNo, 8));
93
94 /* subtract drift term */
95 inst->cSumIatQ8 -= CSUM_IAT_DRIFT;
96
97 /* ensure not negative */
98 inst->cSumIatQ8 = WEBRTC_SPL_MAX(inst->cSumIatQ8, 0);
99
100 /* remember max */
101 if (inst->cSumIatQ8 > inst->maxCSumIatQ8)
102 {
103 inst->maxCSumIatQ8 = inst->cSumIatQ8;
104 inst->maxCSumUpdateTimer = 0;
105 }
106
107 /* too long since the last maximum was observed; decrease max value */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000108 if (inst->maxCSumUpdateTimer > (uint32_t) WEBRTC_SPL_MUL_32_16(fsHz,
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 MAX_STREAMING_PEAK_PERIOD))
110 {
111 inst->maxCSumIatQ8 -= 4; /* remove 1000*4/256 = 15.6 ms/s */
112 }
113 } /* end of streaming mode */
114
115 /* check for discontinuous packet sequence and re-ordering */
116 if (seqNumber > inst->lastSeqNo + 1)
117 {
118 /* Compensate for gap in the sequence numbers.
119 * Reduce IAT with expected extra time due to lost packets, but ensure that
120 * the IAT is not negative.
121 */
122 timeIat -= WEBRTC_SPL_MIN(timeIat,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000123 (uint32_t) (seqNumber - inst->lastSeqNo - 1));
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 }
125 else if (seqNumber < inst->lastSeqNo)
126 {
127 /* compensate for re-ordering */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000128 timeIat += (uint32_t) (inst->lastSeqNo + 1 - seqNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 }
130
131 /* saturate IAT at maximum value */
132 timeIat = WEBRTC_SPL_MIN( timeIat, MAX_IAT );
133
134 /* update iatProb = forgetting_factor * iatProb for all elements */
135 for (i = 0; i <= MAX_IAT; i++)
136 {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000137 int32_t tempHi, tempLo; /* Temporary variables */
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
139 /*
140 * Multiply iatProbFact (Q15) with iatProb (Q30) and right-shift 15 steps
141 * to come back to Q30. The operation is done in two steps:
142 */
143
144 /*
145 * 1) Multiply the high 16 bits (15 bits + sign) of iatProb. Shift iatProb
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000146 * 16 steps right to get the high 16 bits in a int16_t prior to
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 * multiplication, and left-shift with 1 afterwards to come back to
148 * Q30 = (Q15 * (Q30>>16)) << 1.
149 */
150 tempHi = WEBRTC_SPL_MUL_16_16(inst->iatProbFact,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000151 (int16_t) WEBRTC_SPL_RSHIFT_W32(inst->iatProb[i], 16));
niklase@google.com470e71d2011-07-07 08:21:25 +0000152 tempHi = WEBRTC_SPL_LSHIFT_W32(tempHi, 1); /* left-shift 1 step */
153
154 /*
155 * 2) Isolate and multiply the low 16 bits of iatProb. Right-shift 15 steps
156 * afterwards to come back to Q30 = (Q15 * Q30) >> 15.
157 */
158 tempLo = inst->iatProb[i] & 0x0000FFFF; /* sift out the 16 low bits */
159 tempLo = WEBRTC_SPL_MUL_16_U16(inst->iatProbFact,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000160 (uint16_t) tempLo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 tempLo = WEBRTC_SPL_RSHIFT_W32(tempLo, 15);
162
163 /* Finally, add the high and low parts */
164 inst->iatProb[i] = tempHi + tempLo;
165
166 /* Sum all vector elements while we are at it... */
167 tempsum += inst->iatProb[i];
168 }
169
170 /*
171 * Increase the probability for the currently observed inter-arrival time
172 * with 1 - iatProbFact. The factor is in Q15, iatProb in Q30;
173 * hence, left-shift 15 steps to obtain result in Q30.
174 */
175 inst->iatProb[timeIat] += (32768 - inst->iatProbFact) << 15;
176
177 tempsum += (32768 - inst->iatProbFact) << 15; /* add to vector sum */
178
179 /*
180 * Update iatProbFact (changes only during the first seconds after reset)
181 * The factor converges to IAT_PROB_FACT.
182 */
183 inst->iatProbFact += (IAT_PROB_FACT - inst->iatProbFact + 3) >> 2;
184
185 /* iatProb should sum up to 1 (in Q30). */
186 tempsum -= 1 << 30; /* should be zero */
187
188 /* Check if it does, correct if it doesn't. */
189 if (tempsum > 0)
190 {
191 /* tempsum too large => decrease a few values in the beginning */
192 i = 0;
193 while (i <= MAX_IAT && tempsum > 0)
194 {
195 /* Remove iatProb[i] / 16 from iatProb, but not more than tempsum */
196 tempvar = WEBRTC_SPL_MIN(tempsum, inst->iatProb[i] >> 4);
197 inst->iatProb[i++] -= tempvar;
198 tempsum -= tempvar;
199 }
200 }
201 else if (tempsum < 0)
202 {
203 /* tempsum too small => increase a few values in the beginning */
204 i = 0;
205 while (i <= MAX_IAT && tempsum < 0)
206 {
207 /* Add iatProb[i] / 16 to iatProb, but not more than tempsum */
208 tempvar = WEBRTC_SPL_MIN(-tempsum, inst->iatProb[i] >> 4);
209 inst->iatProb[i++] += tempvar;
210 tempsum += tempvar;
211 }
212 }
213
214 /* Calculate optimal buffer level based on updated statistics */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000215 tempvar = (int32_t) WebRtcNetEQ_CalcOptimalBufLvl(inst, fsHz, mdCodec, timeIat,
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 streamingMode);
217 if (tempvar > 0)
218 {
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000219 int high_lim_delay;
220 /* Convert the minimum delay from milliseconds to packets in Q8.
turaj@webrtc.orgfee739c2013-06-12 20:10:06 +0000221 * |fsHz| is sampling rate in Hertz, and |packetLenSamp|
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000222 * is the number of samples per packet (according to the last
223 * decoding).
224 */
225 int32_t minimum_delay_q8 = ((inst->minimum_delay_ms *
turaj@webrtc.orgfee739c2013-06-12 20:10:06 +0000226 (fsHz / 1000)) << 8) / packetLenSamp;
pwestin@webrtc.org401ef362013-08-06 21:01:36 +0000227
228 int32_t maximum_delay_q8 = ((inst->maximum_delay_ms *
229 (fsHz / 1000)) << 8) / packetLenSamp;
230
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000231 inst->optBufLevel = tempvar;
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
233 if (streamingMode != 0)
234 {
235 inst->optBufLevel = WEBRTC_SPL_MAX(inst->optBufLevel,
236 inst->maxCSumIatQ8);
237 }
238
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000239 /* The required delay. */
240 inst->required_delay_q8 = inst->optBufLevel;
241
242 // Maintain the target delay.
243 inst->optBufLevel = WEBRTC_SPL_MAX(inst->optBufLevel,
244 minimum_delay_q8);
245
pwestin@webrtc.org401ef362013-08-06 21:01:36 +0000246 if (maximum_delay_q8 > 0) {
247 // Make sure that max is at least one packet length.
248 maximum_delay_q8 = WEBRTC_SPL_MAX(maximum_delay_q8, (1 << 8));
249 inst->optBufLevel = WEBRTC_SPL_MIN(inst->optBufLevel,
250 maximum_delay_q8);
251 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 /*********/
253 /* Limit */
254 /*********/
255
256 /* Subtract extra delay from maxBufLen */
257 if (inst->extraDelayMs > 0 && inst->packetSpeechLenSamp > 0)
258 {
259 maxBufLen -= inst->extraDelayMs / inst->packetSpeechLenSamp * fsHz / 1000;
260 maxBufLen = WEBRTC_SPL_MAX(maxBufLen, 1); // sanity: at least one packet
261 }
262
263 maxBufLen = WEBRTC_SPL_LSHIFT_W32(maxBufLen, 8); /* shift to Q8 */
264
265 /* Enforce upper limit; 75% of maxBufLen */
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000266 /* 1/2 + 1/4 = 75% */
267 high_lim_delay = (maxBufLen >> 1) + (maxBufLen >> 2);
268 inst->optBufLevel = WEBRTC_SPL_MIN(inst->optBufLevel,
269 high_lim_delay);
270 inst->required_delay_q8 = WEBRTC_SPL_MIN(inst->required_delay_q8,
271 high_lim_delay);
niklase@google.com470e71d2011-07-07 08:21:25 +0000272 }
273 else
274 {
275 retval = (int) tempvar;
276 }
277
278 } /* end if */
279
280 /*******************************/
281 /* Update post-call statistics */
282 /*******************************/
283
284 /* Calculate inter-arrival time in ms = packetIatCountSamp / (fsHz / 1000) */
285 timeIat = WEBRTC_SPL_UDIV(
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000286 WEBRTC_SPL_UMUL_32_16(inst->packetIatCountSamp, (int16_t) 1000),
287 (uint32_t) fsHz);
niklase@google.com470e71d2011-07-07 08:21:25 +0000288
289 /* Increase counter corresponding to current inter-arrival time */
290 if (timeIat > 2000)
291 {
292 inst->countIAT2000ms++;
293 }
294 else if (timeIat > 1000)
295 {
296 inst->countIAT1000ms++;
297 }
298 else if (timeIat > 500)
299 {
300 inst->countIAT500ms++;
301 }
302
303 if (timeIat > inst->longestIATms)
304 {
305 /* update maximum value */
306 inst->longestIATms = timeIat;
307 }
308
309 /***********************************/
310 /* Prepare for next packet arrival */
311 /***********************************/
312
313 inst->packetIatCountSamp = 0; /* reset inter-arrival time counter */
314
315 inst->lastSeqNo = seqNumber; /* remember current sequence number */
316
317 inst->lastTimeStamp = timeStamp; /* remember current timestamp */
318
319 return retval;
320}
321
322
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000323int16_t WebRtcNetEQ_CalcOptimalBufLvl(AutomodeInst_t *inst, int32_t fsHz,
324 int mdCodec, uint32_t timeIatPkts,
325 int streamingMode)
niklase@google.com470e71d2011-07-07 08:21:25 +0000326{
327
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000328 int32_t sum1 = 1 << 30; /* assign to 1 in Q30 */
329 int16_t B;
330 uint16_t Bopt;
niklase@google.com470e71d2011-07-07 08:21:25 +0000331 int i;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000332 int32_t betaInv; /* optimization parameter */
niklase@google.com470e71d2011-07-07 08:21:25 +0000333
334#ifdef NETEQ_DELAY_LOGGING
335 /* special code for offline delay logging */
336 int temp_var;
337#endif
338
339 /****************/
340 /* Sanity check */
341 /****************/
342
343 if (fsHz <= 0)
344 {
345 /* fsHz must be strictly positive */
346 return -1;
347 }
348
349 /***********************************************/
350 /* Get betaInv parameter based on playout mode */
351 /***********************************************/
352
353 if (streamingMode)
354 {
355 /* streaming (listen-only) mode */
356 betaInv = AUTOMODE_STREAMING_BETA_INV_Q30;
357 }
358 else
359 {
360 /* normal mode */
361 betaInv = AUTOMODE_BETA_INV_Q30;
362 }
363
364 /*******************************************************************/
365 /* Calculate optimal buffer level without considering jitter peaks */
366 /*******************************************************************/
367
368 /*
369 * Find the B for which the probability of observing an inter-arrival time larger
370 * than or equal to B is less than or equal to betaInv.
371 */
372 B = 0; /* start from the beginning of iatProb */
373 sum1 -= inst->iatProb[B]; /* ensure that optimal level is not less than 1 */
374
375 do
376 {
377 /*
378 * Subtract the probabilities one by one until the sum is no longer greater
379 * than betaInv.
380 */
381 sum1 -= inst->iatProb[++B];
382 }
383 while ((sum1 > betaInv) && (B < MAX_IAT));
384
385 Bopt = B; /* This is our primary value for the optimal buffer level Bopt */
386
387 if (mdCodec)
388 {
389 /*
390 * Use alternative cost function when multiple description codec is in use.
391 * Do not have to re-calculate all points, just back off a few steps from
392 * previous value of B.
393 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000394 int32_t sum2 = sum1; /* copy sum1 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
396 while ((sum2 <= betaInv + inst->iatProb[Bopt]) && (Bopt > 0))
397 {
398 /* Go backwards in the sum until the modified cost function solution is found */
399 sum2 += inst->iatProb[Bopt--];
400 }
401
402 Bopt++; /* This is the optimal level when using an MD codec */
403
404 /* Now, Bopt and B can have different values. */
405 }
406
407#ifdef NETEQ_DELAY_LOGGING
408 /* special code for offline delay logging */
409 temp_var = NETEQ_DELAY_LOGGING_SIGNAL_OPTBUF;
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000410 if (fwrite( &temp_var, sizeof(int), 1, delay_fid2 ) != 1) {
411 return -1;
412 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000413 temp_var = (int) (Bopt * inst->packetSpeechLenSamp);
414#endif
415
416 /******************************************************************/
417 /* Make levelFiltFact adaptive: Larger B <=> larger levelFiltFact */
418 /******************************************************************/
419
420 switch (B)
421 {
422 case 0:
423 case 1:
424 {
425 inst->levelFiltFact = 251;
426 break;
427 }
428 case 2:
429 case 3:
430 {
431 inst->levelFiltFact = 252;
432 break;
433 }
434 case 4:
435 case 5:
436 case 6:
437 case 7:
438 {
439 inst->levelFiltFact = 253;
440 break;
441 }
442 default: /* B > 7 */
443 {
444 inst->levelFiltFact = 254;
445 break;
446 }
447 }
448
449 /************************/
450 /* Peak mode operations */
451 /************************/
452
453 /* Compare current IAT with peak threshold
454 *
455 * If IAT > optimal level + threshold (+1 for MD codecs)
456 * or if IAT > 2 * optimal level (note: optimal level is in Q8):
457 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000458 if (timeIatPkts > (uint32_t) (Bopt + inst->peakThresholdPkt + (mdCodec != 0))
459 || timeIatPkts > (uint32_t) WEBRTC_SPL_LSHIFT_U16(Bopt, 1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000460 {
461 /* A peak is observed */
462
463 if (inst->peakIndex == -1)
464 {
465 /* this is the first peak; prepare for next peak */
466 inst->peakIndex = 0;
467 /* set the mode-disable counter */
468 inst->peakModeDisabled = WEBRTC_SPL_LSHIFT_W16(1, NUM_PEAKS_REQUIRED-2);
469 }
470 else if (inst->peakIatCountSamp
471 <=
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000472 (uint32_t) WEBRTC_SPL_MUL_32_16(fsHz, MAX_PEAK_PERIOD))
niklase@google.com470e71d2011-07-07 08:21:25 +0000473 {
474 /* This is not the first peak and the period time is valid */
475
476 /* store time elapsed since last peak */
477 inst->peakPeriodSamp[inst->peakIndex] = inst->peakIatCountSamp;
478
479 /* saturate height to 16 bits */
480 inst->peakHeightPkt[inst->peakIndex]
481 =
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000482 (int16_t) WEBRTC_SPL_MIN(timeIatPkts, WEBRTC_SPL_WORD16_MAX);
niklase@google.com470e71d2011-07-07 08:21:25 +0000483
484 /* increment peakIndex and wrap/modulo */
andrew@webrtc.org4f390002011-08-24 20:35:35 +0000485 inst->peakIndex = (inst->peakIndex + 1) & PEAK_INDEX_MASK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000486
487 /* process peak vectors */
488 inst->curPeakHeight = 0;
489 inst->curPeakPeriod = 0;
490
491 for (i = 0; i < NUM_PEAKS; i++)
492 {
493 /* Find maximum of peak heights and peak periods */
494 inst->curPeakHeight
495 = WEBRTC_SPL_MAX(inst->curPeakHeight, inst->peakHeightPkt[i]);
496 inst->curPeakPeriod
497 = WEBRTC_SPL_MAX(inst->curPeakPeriod, inst->peakPeriodSamp[i]);
498
499 }
500
501 inst->peakModeDisabled >>= 1; /* decrease mode-disable "counter" */
502
503 }
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000504 else if (inst->peakIatCountSamp > (uint32_t) WEBRTC_SPL_MUL_32_16(fsHz,
niklase@google.com470e71d2011-07-07 08:21:25 +0000505 WEBRTC_SPL_LSHIFT_W16(MAX_PEAK_PERIOD, 1)))
506 {
507 /*
508 * More than 2 * MAX_PEAK_PERIOD has elapsed since last peak;
509 * too long time => reset peak statistics
510 */
511 inst->curPeakHeight = 0;
512 inst->curPeakPeriod = 0;
513 for (i = 0; i < NUM_PEAKS; i++)
514 {
515 inst->peakHeightPkt[i] = 0;
516 inst->peakPeriodSamp[i] = 0;
517 }
518
519 inst->peakIndex = -1; /* Next peak is first peak */
520 inst->peakIatCountSamp = 0;
521 }
522
523 inst->peakIatCountSamp = 0; /* Reset peak interval timer */
524 } /* end if peak is observed */
525
526 /* Evaluate peak mode conditions */
527
528 /*
529 * If not disabled (enough peaks have been observed) and
530 * time since last peak is less than two peak periods.
531 */
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000532 inst->peakFound = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000533 if ((!inst->peakModeDisabled) && (inst->peakIatCountSamp
534 <= WEBRTC_SPL_LSHIFT_W32(inst->curPeakPeriod , 1)))
535 {
536 /* Engage peak mode */
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000537 inst->peakFound = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000538 /* Set optimal buffer level to curPeakHeight (if it's not already larger) */
539 Bopt = WEBRTC_SPL_MAX(Bopt, inst->curPeakHeight);
540
541#ifdef NETEQ_DELAY_LOGGING
542 /* special code for offline delay logging */
543 temp_var = (int) -(Bopt * inst->packetSpeechLenSamp);
544#endif
545 }
546
547 /* Scale Bopt to Q8 */
548 Bopt = WEBRTC_SPL_LSHIFT_U16(Bopt,8);
549
550#ifdef NETEQ_DELAY_LOGGING
551 /* special code for offline delay logging */
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000552 if (fwrite( &temp_var, sizeof(int), 1, delay_fid2 ) != 1) {
553 return -1;
554 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000555#endif
556
557 /* Sanity check: Bopt must be strictly positive */
558 if (Bopt <= 0)
559 {
560 Bopt = WEBRTC_SPL_LSHIFT_W16(1, 8); /* 1 in Q8 */
561 }
562
563 return Bopt; /* return value in Q8 */
564}
565
566
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000567int WebRtcNetEQ_BufferLevelFilter(int32_t curSizeMs8, AutomodeInst_t *inst,
568 int sampPerCall, int16_t fsMult)
niklase@google.com470e71d2011-07-07 08:21:25 +0000569{
570
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000571 int16_t curSizeFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000572
573 /****************/
574 /* Sanity check */
575 /****************/
576
577 if (sampPerCall <= 0 || fsMult <= 0)
578 {
579 /* sampPerCall and fsMult must both be strictly positive */
580 return -1;
581 }
582
583 /* Check if packet size has been detected */
584 if (inst->packetSpeechLenSamp > 0)
585 {
586 /*
587 * Current buffer level in packet lengths
588 * = (curSizeMs8 * fsMult) / packetSpeechLenSamp
589 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000590 curSizeFrames = (int16_t) WebRtcSpl_DivW32W16(
niklase@google.com470e71d2011-07-07 08:21:25 +0000591 WEBRTC_SPL_MUL_32_16(curSizeMs8, fsMult), inst->packetSpeechLenSamp);
592 }
593 else
594 {
595 curSizeFrames = 0;
596 }
597
598 /* Filter buffer level */
599 if (inst->levelFiltFact > 0) /* check that filter factor is set */
600 {
601 /* Filter:
602 * buffLevelFilt = levelFiltFact * buffLevelFilt
603 * + (1-levelFiltFact) * curSizeFrames
604 *
605 * levelFiltFact is in Q8
606 */
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000607 inst->buffLevelFilt = ((inst->levelFiltFact * inst->buffLevelFilt) >> 8) +
608 (256 - inst->levelFiltFact) * curSizeFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000609 }
610
611 /* Account for time-scale operations (accelerate and pre-emptive expand) */
612 if (inst->prevTimeScale)
613 {
614 /*
615 * Time-scaling has been performed since last filter update.
616 * Subtract the sampleMemory from buffLevelFilt after converting sampleMemory
617 * from samples to packets in Q8. Make sure that the filtered value is
618 * non-negative.
619 */
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000620 inst->buffLevelFilt = WEBRTC_SPL_MAX( inst->buffLevelFilt -
niklase@google.com470e71d2011-07-07 08:21:25 +0000621 WebRtcSpl_DivW32W16(
622 WEBRTC_SPL_LSHIFT_W32(inst->sampleMemory, 8), /* sampleMemory in Q8 */
623 inst->packetSpeechLenSamp ), /* divide by packetSpeechLenSamp */
624 0);
625
626 /*
627 * Reset flag and set timescaleHoldOff timer to prevent further time-scaling
628 * for some time.
629 */
630 inst->prevTimeScale = 0;
631 inst->timescaleHoldOff = AUTOMODE_TIMESCALE_LIMIT;
632 }
633
634 /* Update time counters and HoldOff timer */
635 inst->packetIatCountSamp += sampPerCall; /* packet inter-arrival time */
636 inst->peakIatCountSamp += sampPerCall; /* peak inter-arrival time */
637 inst->timescaleHoldOff >>= 1; /* time-scaling limiter */
638 inst->maxCSumUpdateTimer += sampPerCall; /* cumulative-sum timer */
639
640 return 0;
641
642}
643
644
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000645int WebRtcNetEQ_SetPacketSpeechLen(AutomodeInst_t *inst, int16_t newLenSamp,
646 int32_t fsHz)
niklase@google.com470e71d2011-07-07 08:21:25 +0000647{
648
649 /* Sanity check for newLenSamp and fsHz */
650 if (newLenSamp <= 0 || fsHz <= 0)
651 {
652 return -1;
653 }
654
655 inst->packetSpeechLenSamp = newLenSamp; /* Store packet size in instance */
656
657 /* Make NetEQ wait for first regular packet before starting the timer */
658 inst->lastPackCNGorDTMF = 1;
659
660 inst->packetIatCountSamp = 0; /* Reset packet time counter */
661
662 /*
663 * Calculate peak threshold from packet size. The threshold is defined as
664 * the (fractional) number of packets that corresponds to PEAK_HEIGHT
665 * (in Q8 seconds). That is, threshold = PEAK_HEIGHT/256 * fsHz / packLen.
666 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000667 inst->peakThresholdPkt = (uint16_t) WebRtcSpl_DivW32W16ResW16(
niklase@google.com470e71d2011-07-07 08:21:25 +0000668 WEBRTC_SPL_MUL_16_16_RSFT(PEAK_HEIGHT,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000669 (int16_t) WEBRTC_SPL_RSHIFT_W32(fsHz, 6), 2), inst->packetSpeechLenSamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000670
671 return 0;
672}
673
674
675int WebRtcNetEQ_ResetAutomode(AutomodeInst_t *inst, int maxBufLenPackets)
676{
677
678 int i;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000679 uint16_t tempprob = 0x4002; /* 16384 + 2 = 100000000000010 binary; */
niklase@google.com470e71d2011-07-07 08:21:25 +0000680
681 /* Sanity check for maxBufLenPackets */
682 if (maxBufLenPackets <= 1)
683 {
684 /* Invalid value; set to 10 instead (arbitary small number) */
685 maxBufLenPackets = 10;
686 }
687
688 /* Reset filtered buffer level */
689 inst->buffLevelFilt = 0;
690
691 /* Reset packet size to unknown */
692 inst->packetSpeechLenSamp = 0;
693
694 /*
695 * Flag that last packet was special payload, so that automode will treat the next speech
696 * payload as the first payload received.
697 */
698 inst->lastPackCNGorDTMF = 1;
699
700 /* Reset peak detection parameters */
701 inst->peakModeDisabled = 1; /* disable peak mode */
702 inst->peakIatCountSamp = 0;
703 inst->peakIndex = -1; /* indicates that no peak is registered */
704 inst->curPeakHeight = 0;
705 inst->curPeakPeriod = 0;
706 for (i = 0; i < NUM_PEAKS; i++)
707 {
708 inst->peakHeightPkt[i] = 0;
709 inst->peakPeriodSamp[i] = 0;
710 }
711
712 /*
713 * Set the iatProb PDF vector to an exponentially decaying distribution
714 * iatProb[i] = 0.5^(i+1), i = 0, 1, 2, ...
715 * iatProb is in Q30.
716 */
717 for (i = 0; i <= MAX_IAT; i++)
718 {
719 /* iatProb[i] = 0.5^(i+1) = iatProb[i-1] / 2 */
720 tempprob = WEBRTC_SPL_RSHIFT_U16(tempprob, 1);
721 /* store in PDF vector */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000722 inst->iatProb[i] = WEBRTC_SPL_LSHIFT_W32((int32_t) tempprob, 16);
niklase@google.com470e71d2011-07-07 08:21:25 +0000723 }
724
725 /*
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000726 * Calculate the optimal buffer level corresponding to the initial PDF.
niklase@google.com470e71d2011-07-07 08:21:25 +0000727 * No need to call WebRtcNetEQ_CalcOptimalBufLvl() since we have just hard-coded
728 * all the variables that the buffer level depends on => we know the result
729 */
730 inst->optBufLevel = WEBRTC_SPL_MIN(4,
731 (maxBufLenPackets >> 1) + (maxBufLenPackets >> 1)); /* 75% of maxBufLenPackets */
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000732 inst->required_delay_q8 = inst->optBufLevel;
niklase@google.com470e71d2011-07-07 08:21:25 +0000733 inst->levelFiltFact = 253;
734
735 /*
736 * Reset the iat update forgetting factor to 0 to make the impact of the first
737 * incoming packets greater.
738 */
739 inst->iatProbFact = 0;
740
741 /* Reset packet inter-arrival time counter */
742 inst->packetIatCountSamp = 0;
743
744 /* Clear time-scaling related variables */
745 inst->prevTimeScale = 0;
746 inst->timescaleHoldOff = AUTOMODE_TIMESCALE_LIMIT; /* don't allow time-scaling immediately */
747
748 inst->cSumIatQ8 = 0;
749 inst->maxCSumIatQ8 = 0;
750
751 return 0;
752}
753
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000754int32_t WebRtcNetEQ_AverageIAT(const AutomodeInst_t *inst) {
755 int i;
756 int32_t sum_q24 = 0;
757 assert(inst);
758 for (i = 0; i <= MAX_IAT; ++i) {
759 /* Shift 6 to fit worst case: 2^30 * 64. */
760 sum_q24 += (inst->iatProb[i] >> 6) * i;
761 }
762 /* Subtract the nominal inter-arrival time 1 = 2^24 in Q24. */
763 sum_q24 -= (1 << 24);
764 /*
henrik.lundin@webrtc.orgd4e8c0b2012-01-10 13:46:06 +0000765 * Multiply with 1000000 / 2^24 = 15625 / 2^18 to get in parts-per-million.
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000766 * Shift 7 to Q17 first, then multiply with 15625 and shift another 11.
767 */
768 return ((sum_q24 >> 7) * 15625) >> 11;
769}