niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
tina.legrand@webrtc.org | 0de1ee3 | 2012-05-28 11:37:50 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 implementation of automatic buffer level optimization. |
| 13 | */ |
| 14 | |
| 15 | #include "automode.h" |
| 16 | |
henrik.lundin@webrtc.org | d439870 | 2012-01-04 13:09:55 +0000 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | #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 | |
| 28 | extern FILE *delay_fid2; /* file pointer to delay log file */ |
| 29 | #endif /* NETEQ_DELAY_LOGGING */ |
| 30 | |
| 31 | |
| 32 | int WebRtcNetEQ_UpdateIatStatistics(AutomodeInst_t *inst, int maxBufLen, |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 33 | uint16_t seqNumber, uint32_t timeStamp, |
| 34 | int32_t fsHz, int mdCodec, int streamingMode) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 36 | uint32_t timeIat; /* inter-arrival time */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | int i; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 38 | int32_t tempsum = 0; /* temp summation */ |
| 39 | int32_t tempvar; /* temporary variable */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | int retval = 0; /* return value */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 41 | int16_t packetLenSamp; /* packet speech length in samples */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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.org | 0de1ee3 | 2012-05-28 11:37:50 +0000 | [diff] [blame] | 63 | else |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | { |
| 65 | /* calculate timestamps per packet */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 66 | packetLenSamp = (int16_t) WebRtcSpl_DivU32U16(timeStamp - inst->lastTimeStamp, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 84 | int16_t timeIatQ8 = (int16_t) WebRtcSpl_DivW32W16( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 108 | if (inst->maxCSumUpdateTimer > (uint32_t) WEBRTC_SPL_MUL_32_16(fsHz, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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, |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 123 | (uint32_t) (seqNumber - inst->lastSeqNo - 1)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | } |
| 125 | else if (seqNumber < inst->lastSeqNo) |
| 126 | { |
| 127 | /* compensate for re-ordering */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 128 | timeIat += (uint32_t) (inst->lastSeqNo + 1 - seqNumber); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 137 | int32_t tempHi, tempLo; /* Temporary variables */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 146 | * 16 steps right to get the high 16 bits in a int16_t prior to |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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, |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 151 | (int16_t) WEBRTC_SPL_RSHIFT_W32(inst->iatProb[i], 16)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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, |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 160 | (uint16_t) tempLo); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 215 | tempvar = (int32_t) WebRtcNetEQ_CalcOptimalBufLvl(inst, fsHz, mdCodec, timeIat, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | streamingMode); |
| 217 | if (tempvar > 0) |
| 218 | { |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 219 | inst->optBufLevel = tempvar; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | |
| 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 */ |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 241 | inst->optBufLevel = WEBRTC_SPL_MIN( inst->optBufLevel, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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( |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 257 | WEBRTC_SPL_UMUL_32_16(inst->packetIatCountSamp, (int16_t) 1000), |
| 258 | (uint32_t) fsHz); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 294 | int16_t WebRtcNetEQ_CalcOptimalBufLvl(AutomodeInst_t *inst, int32_t fsHz, |
| 295 | int mdCodec, uint32_t timeIatPkts, |
| 296 | int streamingMode) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | { |
| 298 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 299 | int32_t sum1 = 1 << 30; /* assign to 1 in Q30 */ |
| 300 | int16_t B; |
| 301 | uint16_t Bopt; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 302 | int i; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 303 | int32_t betaInv; /* optimization parameter */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 365 | int32_t sum2 = sum1; /* copy sum1 */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 381 | if (fwrite( &temp_var, sizeof(int), 1, delay_fid2 ) != 1) { |
| 382 | return -1; |
| 383 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 384 | 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 | */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 429 | if (timeIatPkts > (uint32_t) (Bopt + inst->peakThresholdPkt + (mdCodec != 0)) |
| 430 | || timeIatPkts > (uint32_t) WEBRTC_SPL_LSHIFT_U16(Bopt, 1)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | <= |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 443 | (uint32_t) WEBRTC_SPL_MUL_32_16(fsHz, MAX_PEAK_PERIOD)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | = |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 453 | (int16_t) WEBRTC_SPL_MIN(timeIatPkts, WEBRTC_SPL_WORD16_MAX); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 454 | |
| 455 | /* increment peakIndex and wrap/modulo */ |
andrew@webrtc.org | 4f39000 | 2011-08-24 20:35:35 +0000 | [diff] [blame] | 456 | inst->peakIndex = (inst->peakIndex + 1) & PEAK_INDEX_MASK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 457 | |
| 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 | } |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 475 | else if (inst->peakIatCountSamp > (uint32_t) WEBRTC_SPL_MUL_32_16(fsHz, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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.org | d439870 | 2012-01-04 13:09:55 +0000 | [diff] [blame] | 503 | inst->peakFound = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 504 | if ((!inst->peakModeDisabled) && (inst->peakIatCountSamp |
| 505 | <= WEBRTC_SPL_LSHIFT_W32(inst->curPeakPeriod , 1))) |
| 506 | { |
| 507 | /* Engage peak mode */ |
henrik.lundin@webrtc.org | d439870 | 2012-01-04 13:09:55 +0000 | [diff] [blame] | 508 | inst->peakFound = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 509 | /* 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.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 523 | if (fwrite( &temp_var, sizeof(int), 1, delay_fid2 ) != 1) { |
| 524 | return -1; |
| 525 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 526 | #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 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 538 | int WebRtcNetEQ_BufferLevelFilter(int32_t curSizeMs8, AutomodeInst_t *inst, |
| 539 | int sampPerCall, int16_t fsMult) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 540 | { |
| 541 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 542 | int16_t curSizeFrames; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 561 | curSizeFrames = (int16_t) WebRtcSpl_DivW32W16( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | */ |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 578 | inst->buffLevelFilt = ((inst->levelFiltFact * inst->buffLevelFilt) >> 8) + |
| 579 | (256 - inst->levelFiltFact) * curSizeFrames; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* Account for time-scale operations (accelerate and pre-emptive expand) */ |
| 583 | if (inst->prevTimeScale) |
| 584 | { |
| 585 | /* |
| 586 | * Time-scaling has been performed since last filter update. |
| 587 | * Subtract the sampleMemory from buffLevelFilt after converting sampleMemory |
| 588 | * from samples to packets in Q8. Make sure that the filtered value is |
| 589 | * non-negative. |
| 590 | */ |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 591 | inst->buffLevelFilt = WEBRTC_SPL_MAX( inst->buffLevelFilt - |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 592 | WebRtcSpl_DivW32W16( |
| 593 | WEBRTC_SPL_LSHIFT_W32(inst->sampleMemory, 8), /* sampleMemory in Q8 */ |
| 594 | inst->packetSpeechLenSamp ), /* divide by packetSpeechLenSamp */ |
| 595 | 0); |
| 596 | |
| 597 | /* |
| 598 | * Reset flag and set timescaleHoldOff timer to prevent further time-scaling |
| 599 | * for some time. |
| 600 | */ |
| 601 | inst->prevTimeScale = 0; |
| 602 | inst->timescaleHoldOff = AUTOMODE_TIMESCALE_LIMIT; |
| 603 | } |
| 604 | |
| 605 | /* Update time counters and HoldOff timer */ |
| 606 | inst->packetIatCountSamp += sampPerCall; /* packet inter-arrival time */ |
| 607 | inst->peakIatCountSamp += sampPerCall; /* peak inter-arrival time */ |
| 608 | inst->timescaleHoldOff >>= 1; /* time-scaling limiter */ |
| 609 | inst->maxCSumUpdateTimer += sampPerCall; /* cumulative-sum timer */ |
| 610 | |
| 611 | return 0; |
| 612 | |
| 613 | } |
| 614 | |
| 615 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 616 | int WebRtcNetEQ_SetPacketSpeechLen(AutomodeInst_t *inst, int16_t newLenSamp, |
| 617 | int32_t fsHz) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 618 | { |
| 619 | |
| 620 | /* Sanity check for newLenSamp and fsHz */ |
| 621 | if (newLenSamp <= 0 || fsHz <= 0) |
| 622 | { |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | inst->packetSpeechLenSamp = newLenSamp; /* Store packet size in instance */ |
| 627 | |
| 628 | /* Make NetEQ wait for first regular packet before starting the timer */ |
| 629 | inst->lastPackCNGorDTMF = 1; |
| 630 | |
| 631 | inst->packetIatCountSamp = 0; /* Reset packet time counter */ |
| 632 | |
| 633 | /* |
| 634 | * Calculate peak threshold from packet size. The threshold is defined as |
| 635 | * the (fractional) number of packets that corresponds to PEAK_HEIGHT |
| 636 | * (in Q8 seconds). That is, threshold = PEAK_HEIGHT/256 * fsHz / packLen. |
| 637 | */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 638 | inst->peakThresholdPkt = (uint16_t) WebRtcSpl_DivW32W16ResW16( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 639 | WEBRTC_SPL_MUL_16_16_RSFT(PEAK_HEIGHT, |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 640 | (int16_t) WEBRTC_SPL_RSHIFT_W32(fsHz, 6), 2), inst->packetSpeechLenSamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | int WebRtcNetEQ_ResetAutomode(AutomodeInst_t *inst, int maxBufLenPackets) |
| 647 | { |
| 648 | |
| 649 | int i; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 650 | uint16_t tempprob = 0x4002; /* 16384 + 2 = 100000000000010 binary; */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 651 | |
| 652 | /* Sanity check for maxBufLenPackets */ |
| 653 | if (maxBufLenPackets <= 1) |
| 654 | { |
| 655 | /* Invalid value; set to 10 instead (arbitary small number) */ |
| 656 | maxBufLenPackets = 10; |
| 657 | } |
| 658 | |
| 659 | /* Reset filtered buffer level */ |
| 660 | inst->buffLevelFilt = 0; |
| 661 | |
| 662 | /* Reset packet size to unknown */ |
| 663 | inst->packetSpeechLenSamp = 0; |
| 664 | |
| 665 | /* |
| 666 | * Flag that last packet was special payload, so that automode will treat the next speech |
| 667 | * payload as the first payload received. |
| 668 | */ |
| 669 | inst->lastPackCNGorDTMF = 1; |
| 670 | |
| 671 | /* Reset peak detection parameters */ |
| 672 | inst->peakModeDisabled = 1; /* disable peak mode */ |
| 673 | inst->peakIatCountSamp = 0; |
| 674 | inst->peakIndex = -1; /* indicates that no peak is registered */ |
| 675 | inst->curPeakHeight = 0; |
| 676 | inst->curPeakPeriod = 0; |
| 677 | for (i = 0; i < NUM_PEAKS; i++) |
| 678 | { |
| 679 | inst->peakHeightPkt[i] = 0; |
| 680 | inst->peakPeriodSamp[i] = 0; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Set the iatProb PDF vector to an exponentially decaying distribution |
| 685 | * iatProb[i] = 0.5^(i+1), i = 0, 1, 2, ... |
| 686 | * iatProb is in Q30. |
| 687 | */ |
| 688 | for (i = 0; i <= MAX_IAT; i++) |
| 689 | { |
| 690 | /* iatProb[i] = 0.5^(i+1) = iatProb[i-1] / 2 */ |
| 691 | tempprob = WEBRTC_SPL_RSHIFT_U16(tempprob, 1); |
| 692 | /* store in PDF vector */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 693 | inst->iatProb[i] = WEBRTC_SPL_LSHIFT_W32((int32_t) tempprob, 16); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* |
henrik.lundin@webrtc.org | d439870 | 2012-01-04 13:09:55 +0000 | [diff] [blame] | 697 | * Calculate the optimal buffer level corresponding to the initial PDF. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 698 | * No need to call WebRtcNetEQ_CalcOptimalBufLvl() since we have just hard-coded |
| 699 | * all the variables that the buffer level depends on => we know the result |
| 700 | */ |
| 701 | inst->optBufLevel = WEBRTC_SPL_MIN(4, |
| 702 | (maxBufLenPackets >> 1) + (maxBufLenPackets >> 1)); /* 75% of maxBufLenPackets */ |
| 703 | inst->levelFiltFact = 253; |
| 704 | |
| 705 | /* |
| 706 | * Reset the iat update forgetting factor to 0 to make the impact of the first |
| 707 | * incoming packets greater. |
| 708 | */ |
| 709 | inst->iatProbFact = 0; |
| 710 | |
| 711 | /* Reset packet inter-arrival time counter */ |
| 712 | inst->packetIatCountSamp = 0; |
| 713 | |
| 714 | /* Clear time-scaling related variables */ |
| 715 | inst->prevTimeScale = 0; |
| 716 | inst->timescaleHoldOff = AUTOMODE_TIMESCALE_LIMIT; /* don't allow time-scaling immediately */ |
| 717 | |
| 718 | inst->cSumIatQ8 = 0; |
| 719 | inst->maxCSumIatQ8 = 0; |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
henrik.lundin@webrtc.org | d439870 | 2012-01-04 13:09:55 +0000 | [diff] [blame] | 724 | int32_t WebRtcNetEQ_AverageIAT(const AutomodeInst_t *inst) { |
| 725 | int i; |
| 726 | int32_t sum_q24 = 0; |
| 727 | assert(inst); |
| 728 | for (i = 0; i <= MAX_IAT; ++i) { |
| 729 | /* Shift 6 to fit worst case: 2^30 * 64. */ |
| 730 | sum_q24 += (inst->iatProb[i] >> 6) * i; |
| 731 | } |
| 732 | /* Subtract the nominal inter-arrival time 1 = 2^24 in Q24. */ |
| 733 | sum_q24 -= (1 << 24); |
| 734 | /* |
henrik.lundin@webrtc.org | d4e8c0b | 2012-01-10 13:46:06 +0000 | [diff] [blame] | 735 | * Multiply with 1000000 / 2^24 = 15625 / 2^18 to get in parts-per-million. |
henrik.lundin@webrtc.org | d439870 | 2012-01-04 13:09:55 +0000 | [diff] [blame] | 736 | * Shift 7 to Q17 first, then multiply with 15625 and shift another 11. |
| 737 | */ |
| 738 | return ((sum_q24 >> 7) * 15625) >> 11; |
| 739 | } |