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