blob: d8d56c692131f7786f00f438b30e5011d7711bcd [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,
33 WebRtc_UWord16 seqNumber, WebRtc_UWord32 timeStamp,
34 WebRtc_Word32 fsHz, int mdCodec, int streamingMode)
35{
36 WebRtc_UWord32 timeIat; /* inter-arrival time */
37 int i;
38 WebRtc_Word32 tempsum = 0; /* temp summation */
39 WebRtc_Word32 tempvar; /* temporary variable */
40 int retval = 0; /* return value */
41 WebRtc_Word16 packetLenSamp; /* packet speech length in samples */
42
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 */
66 packetLenSamp = (WebRtc_Word16) WebRtcSpl_DivU32U16(timeStamp - inst->lastTimeStamp,
67 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 */
84 WebRtc_Word16 timeIatQ8 = (WebRtc_Word16) WebRtcSpl_DivW32W16(
85 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 */
108 if (inst->maxCSumUpdateTimer > (WebRtc_UWord32) WEBRTC_SPL_MUL_32_16(fsHz,
109 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,
123 (WebRtc_UWord32) (seqNumber - inst->lastSeqNo - 1));
124 }
125 else if (seqNumber < inst->lastSeqNo)
126 {
127 /* compensate for re-ordering */
128 timeIat += (WebRtc_UWord32) (inst->lastSeqNo + 1 - seqNumber);
129 }
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 {
137 WebRtc_Word32 tempHi, tempLo; /* Temporary variables */
138
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
146 * 16 steps right to get the high 16 bits in a WebRtc_Word16 prior to
147 * 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,
151 (WebRtc_Word16) WEBRTC_SPL_RSHIFT_W32(inst->iatProb[i], 16));
152 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,
160 (WebRtc_UWord16) tempLo);
161 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 */
215 tempvar = (WebRtc_Word32) WebRtcNetEQ_CalcOptimalBufLvl(inst, fsHz, mdCodec, timeIat,
216 streamingMode);
217 if (tempvar > 0)
218 {
niklas.enbom@webrtc.orgcd2f1352013-01-24 22:05:30 +0000219 inst->optBufLevel = (WebRtc_UWord16) tempvar;
niklase@google.com470e71d2011-07-07 08:21:25 +0000220
221 if (streamingMode != 0)
222 {
223 inst->optBufLevel = WEBRTC_SPL_MAX(inst->optBufLevel,
224 inst->maxCSumIatQ8);
225 }
226
227 /*********/
228 /* Limit */
229 /*********/
230
231 /* Subtract extra delay from maxBufLen */
232 if (inst->extraDelayMs > 0 && inst->packetSpeechLenSamp > 0)
233 {
234 maxBufLen -= inst->extraDelayMs / inst->packetSpeechLenSamp * fsHz / 1000;
235 maxBufLen = WEBRTC_SPL_MAX(maxBufLen, 1); // sanity: at least one packet
236 }
237
238 maxBufLen = WEBRTC_SPL_LSHIFT_W32(maxBufLen, 8); /* shift to Q8 */
239
240 /* Enforce upper limit; 75% of maxBufLen */
241 inst->optBufLevel = (WebRtc_UWord16) WEBRTC_SPL_MIN( inst->optBufLevel,
242 (maxBufLen >> 1) + (maxBufLen >> 2) ); /* 1/2 + 1/4 = 75% */
243 }
244 else
245 {
246 retval = (int) tempvar;
247 }
248
249 } /* end if */
250
251 /*******************************/
252 /* Update post-call statistics */
253 /*******************************/
254
255 /* Calculate inter-arrival time in ms = packetIatCountSamp / (fsHz / 1000) */
256 timeIat = WEBRTC_SPL_UDIV(
257 WEBRTC_SPL_UMUL_32_16(inst->packetIatCountSamp, (WebRtc_Word16) 1000),
258 (WebRtc_UWord32) fsHz);
259
260 /* Increase counter corresponding to current inter-arrival time */
261 if (timeIat > 2000)
262 {
263 inst->countIAT2000ms++;
264 }
265 else if (timeIat > 1000)
266 {
267 inst->countIAT1000ms++;
268 }
269 else if (timeIat > 500)
270 {
271 inst->countIAT500ms++;
272 }
273
274 if (timeIat > inst->longestIATms)
275 {
276 /* update maximum value */
277 inst->longestIATms = timeIat;
278 }
279
280 /***********************************/
281 /* Prepare for next packet arrival */
282 /***********************************/
283
284 inst->packetIatCountSamp = 0; /* reset inter-arrival time counter */
285
286 inst->lastSeqNo = seqNumber; /* remember current sequence number */
287
288 inst->lastTimeStamp = timeStamp; /* remember current timestamp */
289
290 return retval;
291}
292
293
294WebRtc_Word16 WebRtcNetEQ_CalcOptimalBufLvl(AutomodeInst_t *inst, WebRtc_Word32 fsHz,
295 int mdCodec, WebRtc_UWord32 timeIatPkts,
296 int streamingMode)
297{
298
299 WebRtc_Word32 sum1 = 1 << 30; /* assign to 1 in Q30 */
300 WebRtc_Word16 B;
301 WebRtc_UWord16 Bopt;
302 int i;
303 WebRtc_Word32 betaInv; /* optimization parameter */
304
305#ifdef NETEQ_DELAY_LOGGING
306 /* special code for offline delay logging */
307 int temp_var;
308#endif
309
310 /****************/
311 /* Sanity check */
312 /****************/
313
314 if (fsHz <= 0)
315 {
316 /* fsHz must be strictly positive */
317 return -1;
318 }
319
320 /***********************************************/
321 /* Get betaInv parameter based on playout mode */
322 /***********************************************/
323
324 if (streamingMode)
325 {
326 /* streaming (listen-only) mode */
327 betaInv = AUTOMODE_STREAMING_BETA_INV_Q30;
328 }
329 else
330 {
331 /* normal mode */
332 betaInv = AUTOMODE_BETA_INV_Q30;
333 }
334
335 /*******************************************************************/
336 /* Calculate optimal buffer level without considering jitter peaks */
337 /*******************************************************************/
338
339 /*
340 * Find the B for which the probability of observing an inter-arrival time larger
341 * than or equal to B is less than or equal to betaInv.
342 */
343 B = 0; /* start from the beginning of iatProb */
344 sum1 -= inst->iatProb[B]; /* ensure that optimal level is not less than 1 */
345
346 do
347 {
348 /*
349 * Subtract the probabilities one by one until the sum is no longer greater
350 * than betaInv.
351 */
352 sum1 -= inst->iatProb[++B];
353 }
354 while ((sum1 > betaInv) && (B < MAX_IAT));
355
356 Bopt = B; /* This is our primary value for the optimal buffer level Bopt */
357
358 if (mdCodec)
359 {
360 /*
361 * Use alternative cost function when multiple description codec is in use.
362 * Do not have to re-calculate all points, just back off a few steps from
363 * previous value of B.
364 */
365 WebRtc_Word32 sum2 = sum1; /* copy sum1 */
366
367 while ((sum2 <= betaInv + inst->iatProb[Bopt]) && (Bopt > 0))
368 {
369 /* Go backwards in the sum until the modified cost function solution is found */
370 sum2 += inst->iatProb[Bopt--];
371 }
372
373 Bopt++; /* This is the optimal level when using an MD codec */
374
375 /* Now, Bopt and B can have different values. */
376 }
377
378#ifdef NETEQ_DELAY_LOGGING
379 /* special code for offline delay logging */
380 temp_var = NETEQ_DELAY_LOGGING_SIGNAL_OPTBUF;
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000381 if (fwrite( &temp_var, sizeof(int), 1, delay_fid2 ) != 1) {
382 return -1;
383 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000384 temp_var = (int) (Bopt * inst->packetSpeechLenSamp);
385#endif
386
387 /******************************************************************/
388 /* Make levelFiltFact adaptive: Larger B <=> larger levelFiltFact */
389 /******************************************************************/
390
391 switch (B)
392 {
393 case 0:
394 case 1:
395 {
396 inst->levelFiltFact = 251;
397 break;
398 }
399 case 2:
400 case 3:
401 {
402 inst->levelFiltFact = 252;
403 break;
404 }
405 case 4:
406 case 5:
407 case 6:
408 case 7:
409 {
410 inst->levelFiltFact = 253;
411 break;
412 }
413 default: /* B > 7 */
414 {
415 inst->levelFiltFact = 254;
416 break;
417 }
418 }
419
420 /************************/
421 /* Peak mode operations */
422 /************************/
423
424 /* Compare current IAT with peak threshold
425 *
426 * If IAT > optimal level + threshold (+1 for MD codecs)
427 * or if IAT > 2 * optimal level (note: optimal level is in Q8):
428 */
429 if (timeIatPkts > (WebRtc_UWord32) (Bopt + inst->peakThresholdPkt + (mdCodec != 0))
430 || timeIatPkts > (WebRtc_UWord32) WEBRTC_SPL_LSHIFT_U16(Bopt, 1))
431 {
432 /* A peak is observed */
433
434 if (inst->peakIndex == -1)
435 {
436 /* this is the first peak; prepare for next peak */
437 inst->peakIndex = 0;
438 /* set the mode-disable counter */
439 inst->peakModeDisabled = WEBRTC_SPL_LSHIFT_W16(1, NUM_PEAKS_REQUIRED-2);
440 }
441 else if (inst->peakIatCountSamp
442 <=
443 (WebRtc_UWord32) WEBRTC_SPL_MUL_32_16(fsHz, MAX_PEAK_PERIOD))
444 {
445 /* This is not the first peak and the period time is valid */
446
447 /* store time elapsed since last peak */
448 inst->peakPeriodSamp[inst->peakIndex] = inst->peakIatCountSamp;
449
450 /* saturate height to 16 bits */
451 inst->peakHeightPkt[inst->peakIndex]
452 =
453 (WebRtc_Word16) WEBRTC_SPL_MIN(timeIatPkts, WEBRTC_SPL_WORD16_MAX);
454
455 /* increment peakIndex and wrap/modulo */
andrew@webrtc.org4f390002011-08-24 20:35:35 +0000456 inst->peakIndex = (inst->peakIndex + 1) & PEAK_INDEX_MASK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
458 /* process peak vectors */
459 inst->curPeakHeight = 0;
460 inst->curPeakPeriod = 0;
461
462 for (i = 0; i < NUM_PEAKS; i++)
463 {
464 /* Find maximum of peak heights and peak periods */
465 inst->curPeakHeight
466 = WEBRTC_SPL_MAX(inst->curPeakHeight, inst->peakHeightPkt[i]);
467 inst->curPeakPeriod
468 = WEBRTC_SPL_MAX(inst->curPeakPeriod, inst->peakPeriodSamp[i]);
469
470 }
471
472 inst->peakModeDisabled >>= 1; /* decrease mode-disable "counter" */
473
474 }
475 else if (inst->peakIatCountSamp > (WebRtc_UWord32) WEBRTC_SPL_MUL_32_16(fsHz,
476 WEBRTC_SPL_LSHIFT_W16(MAX_PEAK_PERIOD, 1)))
477 {
478 /*
479 * More than 2 * MAX_PEAK_PERIOD has elapsed since last peak;
480 * too long time => reset peak statistics
481 */
482 inst->curPeakHeight = 0;
483 inst->curPeakPeriod = 0;
484 for (i = 0; i < NUM_PEAKS; i++)
485 {
486 inst->peakHeightPkt[i] = 0;
487 inst->peakPeriodSamp[i] = 0;
488 }
489
490 inst->peakIndex = -1; /* Next peak is first peak */
491 inst->peakIatCountSamp = 0;
492 }
493
494 inst->peakIatCountSamp = 0; /* Reset peak interval timer */
495 } /* end if peak is observed */
496
497 /* Evaluate peak mode conditions */
498
499 /*
500 * If not disabled (enough peaks have been observed) and
501 * time since last peak is less than two peak periods.
502 */
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000503 inst->peakFound = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000504 if ((!inst->peakModeDisabled) && (inst->peakIatCountSamp
505 <= WEBRTC_SPL_LSHIFT_W32(inst->curPeakPeriod , 1)))
506 {
507 /* Engage peak mode */
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000508 inst->peakFound = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000509 /* Set optimal buffer level to curPeakHeight (if it's not already larger) */
510 Bopt = WEBRTC_SPL_MAX(Bopt, inst->curPeakHeight);
511
512#ifdef NETEQ_DELAY_LOGGING
513 /* special code for offline delay logging */
514 temp_var = (int) -(Bopt * inst->packetSpeechLenSamp);
515#endif
516 }
517
518 /* Scale Bopt to Q8 */
519 Bopt = WEBRTC_SPL_LSHIFT_U16(Bopt,8);
520
521#ifdef NETEQ_DELAY_LOGGING
522 /* special code for offline delay logging */
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000523 if (fwrite( &temp_var, sizeof(int), 1, delay_fid2 ) != 1) {
524 return -1;
525 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000526#endif
527
528 /* Sanity check: Bopt must be strictly positive */
529 if (Bopt <= 0)
530 {
531 Bopt = WEBRTC_SPL_LSHIFT_W16(1, 8); /* 1 in Q8 */
532 }
533
534 return Bopt; /* return value in Q8 */
535}
536
537
538int WebRtcNetEQ_BufferLevelFilter(WebRtc_Word32 curSizeMs8, AutomodeInst_t *inst,
539 int sampPerCall, WebRtc_Word16 fsMult)
540{
541
542 WebRtc_Word16 curSizeFrames;
543
544 /****************/
545 /* Sanity check */
546 /****************/
547
548 if (sampPerCall <= 0 || fsMult <= 0)
549 {
550 /* sampPerCall and fsMult must both be strictly positive */
551 return -1;
552 }
553
554 /* Check if packet size has been detected */
555 if (inst->packetSpeechLenSamp > 0)
556 {
557 /*
558 * Current buffer level in packet lengths
559 * = (curSizeMs8 * fsMult) / packetSpeechLenSamp
560 */
561 curSizeFrames = (WebRtc_Word16) WebRtcSpl_DivW32W16(
562 WEBRTC_SPL_MUL_32_16(curSizeMs8, fsMult), inst->packetSpeechLenSamp);
563 }
564 else
565 {
566 curSizeFrames = 0;
567 }
568
569 /* Filter buffer level */
570 if (inst->levelFiltFact > 0) /* check that filter factor is set */
571 {
572 /* Filter:
573 * buffLevelFilt = levelFiltFact * buffLevelFilt
574 * + (1-levelFiltFact) * curSizeFrames
575 *
576 * levelFiltFact is in Q8
577 */
niklas.enbom@webrtc.orgcd2f1352013-01-24 22:05:30 +0000578 inst->buffLevelFilt = (WebRtc_UWord16) (WEBRTC_SPL_RSHIFT_W32(
niklase@google.com470e71d2011-07-07 08:21:25 +0000579 WEBRTC_SPL_MUL_16_U16(inst->levelFiltFact, inst->buffLevelFilt), 8)
580 + WEBRTC_SPL_MUL_16_16(256 - inst->levelFiltFact, curSizeFrames));
581 }
582
583 /* Account for time-scale operations (accelerate and pre-emptive expand) */
584 if (inst->prevTimeScale)
585 {
586 /*
587 * Time-scaling has been performed since last filter update.
588 * Subtract the sampleMemory from buffLevelFilt after converting sampleMemory
589 * from samples to packets in Q8. Make sure that the filtered value is
590 * non-negative.
591 */
niklas.enbom@webrtc.orgcd2f1352013-01-24 22:05:30 +0000592 inst->buffLevelFilt = (WebRtc_UWord16) WEBRTC_SPL_MAX( inst->buffLevelFilt -
niklase@google.com470e71d2011-07-07 08:21:25 +0000593 WebRtcSpl_DivW32W16(
594 WEBRTC_SPL_LSHIFT_W32(inst->sampleMemory, 8), /* sampleMemory in Q8 */
595 inst->packetSpeechLenSamp ), /* divide by packetSpeechLenSamp */
596 0);
597
598 /*
599 * Reset flag and set timescaleHoldOff timer to prevent further time-scaling
600 * for some time.
601 */
602 inst->prevTimeScale = 0;
603 inst->timescaleHoldOff = AUTOMODE_TIMESCALE_LIMIT;
604 }
605
606 /* Update time counters and HoldOff timer */
607 inst->packetIatCountSamp += sampPerCall; /* packet inter-arrival time */
608 inst->peakIatCountSamp += sampPerCall; /* peak inter-arrival time */
609 inst->timescaleHoldOff >>= 1; /* time-scaling limiter */
610 inst->maxCSumUpdateTimer += sampPerCall; /* cumulative-sum timer */
611
612 return 0;
613
614}
615
616
617int WebRtcNetEQ_SetPacketSpeechLen(AutomodeInst_t *inst, WebRtc_Word16 newLenSamp,
618 WebRtc_Word32 fsHz)
619{
620
621 /* Sanity check for newLenSamp and fsHz */
622 if (newLenSamp <= 0 || fsHz <= 0)
623 {
624 return -1;
625 }
626
627 inst->packetSpeechLenSamp = newLenSamp; /* Store packet size in instance */
628
629 /* Make NetEQ wait for first regular packet before starting the timer */
630 inst->lastPackCNGorDTMF = 1;
631
632 inst->packetIatCountSamp = 0; /* Reset packet time counter */
633
634 /*
635 * Calculate peak threshold from packet size. The threshold is defined as
636 * the (fractional) number of packets that corresponds to PEAK_HEIGHT
637 * (in Q8 seconds). That is, threshold = PEAK_HEIGHT/256 * fsHz / packLen.
638 */
639 inst->peakThresholdPkt = (WebRtc_UWord16) WebRtcSpl_DivW32W16ResW16(
640 WEBRTC_SPL_MUL_16_16_RSFT(PEAK_HEIGHT,
641 (WebRtc_Word16) WEBRTC_SPL_RSHIFT_W32(fsHz, 6), 2), inst->packetSpeechLenSamp);
642
643 return 0;
644}
645
646
647int WebRtcNetEQ_ResetAutomode(AutomodeInst_t *inst, int maxBufLenPackets)
648{
649
650 int i;
651 WebRtc_UWord16 tempprob = 0x4002; /* 16384 + 2 = 100000000000010 binary; */
652
653 /* Sanity check for maxBufLenPackets */
654 if (maxBufLenPackets <= 1)
655 {
656 /* Invalid value; set to 10 instead (arbitary small number) */
657 maxBufLenPackets = 10;
658 }
659
660 /* Reset filtered buffer level */
661 inst->buffLevelFilt = 0;
662
663 /* Reset packet size to unknown */
664 inst->packetSpeechLenSamp = 0;
665
666 /*
667 * Flag that last packet was special payload, so that automode will treat the next speech
668 * payload as the first payload received.
669 */
670 inst->lastPackCNGorDTMF = 1;
671
672 /* Reset peak detection parameters */
673 inst->peakModeDisabled = 1; /* disable peak mode */
674 inst->peakIatCountSamp = 0;
675 inst->peakIndex = -1; /* indicates that no peak is registered */
676 inst->curPeakHeight = 0;
677 inst->curPeakPeriod = 0;
678 for (i = 0; i < NUM_PEAKS; i++)
679 {
680 inst->peakHeightPkt[i] = 0;
681 inst->peakPeriodSamp[i] = 0;
682 }
683
684 /*
685 * Set the iatProb PDF vector to an exponentially decaying distribution
686 * iatProb[i] = 0.5^(i+1), i = 0, 1, 2, ...
687 * iatProb is in Q30.
688 */
689 for (i = 0; i <= MAX_IAT; i++)
690 {
691 /* iatProb[i] = 0.5^(i+1) = iatProb[i-1] / 2 */
692 tempprob = WEBRTC_SPL_RSHIFT_U16(tempprob, 1);
693 /* store in PDF vector */
694 inst->iatProb[i] = WEBRTC_SPL_LSHIFT_W32((WebRtc_Word32) tempprob, 16);
695 }
696
697 /*
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000698 * Calculate the optimal buffer level corresponding to the initial PDF.
niklase@google.com470e71d2011-07-07 08:21:25 +0000699 * No need to call WebRtcNetEQ_CalcOptimalBufLvl() since we have just hard-coded
700 * all the variables that the buffer level depends on => we know the result
701 */
702 inst->optBufLevel = WEBRTC_SPL_MIN(4,
703 (maxBufLenPackets >> 1) + (maxBufLenPackets >> 1)); /* 75% of maxBufLenPackets */
704 inst->levelFiltFact = 253;
705
706 /*
707 * Reset the iat update forgetting factor to 0 to make the impact of the first
708 * incoming packets greater.
709 */
710 inst->iatProbFact = 0;
711
712 /* Reset packet inter-arrival time counter */
713 inst->packetIatCountSamp = 0;
714
715 /* Clear time-scaling related variables */
716 inst->prevTimeScale = 0;
717 inst->timescaleHoldOff = AUTOMODE_TIMESCALE_LIMIT; /* don't allow time-scaling immediately */
718
719 inst->cSumIatQ8 = 0;
720 inst->maxCSumIatQ8 = 0;
721
722 return 0;
723}
724
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000725int32_t WebRtcNetEQ_AverageIAT(const AutomodeInst_t *inst) {
726 int i;
727 int32_t sum_q24 = 0;
728 assert(inst);
729 for (i = 0; i <= MAX_IAT; ++i) {
730 /* Shift 6 to fit worst case: 2^30 * 64. */
731 sum_q24 += (inst->iatProb[i] >> 6) * i;
732 }
733 /* Subtract the nominal inter-arrival time 1 = 2^24 in Q24. */
734 sum_q24 -= (1 << 24);
735 /*
henrik.lundin@webrtc.orgd4e8c0b2012-01-10 13:46:06 +0000736 * Multiply with 1000000 / 2^24 = 15625 / 2^18 to get in parts-per-million.
henrik.lundin@webrtc.orgd4398702012-01-04 13:09:55 +0000737 * Shift 7 to Q17 first, then multiply with 15625 and shift another 11.
738 */
739 return ((sum_q24 >> 7) * 15625) >> 11;
740}