1 /* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 /* 12 * bandwidth_estimator.c 13 * 14 * This file contains the code for the Bandwidth Estimator designed 15 * for iSAC. 16 * 17 * NOTE! Castings needed for C55, do not remove! 18 * 19 */ 20 21 #include "bandwidth_estimator.h" 22 #include "settings.h" 23 24 25 /* array of quantization levels for bottle neck info; Matlab code: */ 26 /* sprintf('%4.1ff, ', logspace(log10(5000), log10(40000), 12)) */ 27 static const int16_t kQRateTable[12] = { 28 10000, 11115, 12355, 13733, 15265, 16967, 29 18860, 20963, 23301, 25900, 28789, 32000 30 }; 31 32 /* 0.1 times the values in the table kQRateTable */ 33 /* values are in Q16 */ 34 static const int32_t KQRate01[12] = { 35 65536000, 72843264, 80969728, 90000589, 100040704, 111194931, 36 123600896, 137383117, 152705434, 169738240, 188671590, 209715200 37 }; 38 39 /* Bits per Bytes Seconds 40 * 8 bits/byte * 1000 msec/sec * 1/framelength (in msec)->bits/byte*sec 41 * frame length will either be 30 or 60 msec. 8738 is 1/60 in Q19 and 1/30 in Q18 42 * The following number is either in Q15 or Q14 depending on the current frame length */ 43 static const int32_t kBitsByteSec = 4369000; 44 45 /* Received header rate. First value is for 30 ms packets and second for 60 ms */ 46 static const int16_t kRecHeaderRate[2] = { 47 9333, 4666 48 }; 49 50 /* Inverted minimum and maximum bandwidth in Q30. 51 minBwInv 30 ms, maxBwInv 30 ms, 52 minBwInv 60 ms, maxBwInv 69 ms 53 */ 54 static const int32_t kInvBandwidth[4] = { 55 55539, 25978, 56 73213, 29284 57 }; 58 59 /* Number of samples in 25 msec */ 60 static const int32_t kSamplesIn25msec = 400; 61 62 63 /**************************************************************************** 64 * WebRtcIsacfix_InitBandwidthEstimator(...) 65 * 66 * This function initializes the struct for the bandwidth estimator 67 * 68 * Input/Output: 69 * - bweStr : Struct containing bandwidth information. 70 * 71 * Return value : 0 72 */ 73 int32_t WebRtcIsacfix_InitBandwidthEstimator(BwEstimatorstr *bweStr) 74 { 75 bweStr->prevFrameSizeMs = INIT_FRAME_LEN; 76 bweStr->prevRtpNumber = 0; 77 bweStr->prevSendTime = 0; 78 bweStr->prevArrivalTime = 0; 79 bweStr->prevRtpRate = 1; 80 bweStr->lastUpdate = 0; 81 bweStr->lastReduction = 0; 82 bweStr->countUpdates = -9; 83 84 /* INIT_BN_EST = 20000 85 * INIT_BN_EST_Q7 = 2560000 86 * INIT_HDR_RATE = 4666 87 * INIT_REC_BN_EST_Q5 = 789312 88 * 89 * recBwInv = 1/(INIT_BN_EST + INIT_HDR_RATE) in Q30 90 * recBwAvg = INIT_BN_EST + INIT_HDR_RATE in Q5 91 */ 92 bweStr->recBwInv = 43531; 93 bweStr->recBw = INIT_BN_EST; 94 bweStr->recBwAvgQ = INIT_BN_EST_Q7; 95 bweStr->recBwAvg = INIT_REC_BN_EST_Q5; 96 bweStr->recJitter = (int32_t) 327680; /* 10 in Q15 */ 97 bweStr->recJitterShortTerm = 0; 98 bweStr->recJitterShortTermAbs = (int32_t) 40960; /* 5 in Q13 */ 99 bweStr->recMaxDelay = (int32_t) 10; 100 bweStr->recMaxDelayAvgQ = (int32_t) 5120; /* 10 in Q9 */ 101 bweStr->recHeaderRate = INIT_HDR_RATE; 102 bweStr->countRecPkts = 0; 103 bweStr->sendBwAvg = INIT_BN_EST_Q7; 104 bweStr->sendMaxDelayAvg = (int32_t) 5120; /* 10 in Q9 */ 105 106 bweStr->countHighSpeedRec = 0; 107 bweStr->highSpeedRec = 0; 108 bweStr->countHighSpeedSent = 0; 109 bweStr->highSpeedSend = 0; 110 bweStr->inWaitPeriod = 0; 111 112 /* Find the inverse of the max bw and min bw in Q30 113 * (1 / (MAX_ISAC_BW + INIT_HDR_RATE) in Q30 114 * (1 / (MIN_ISAC_BW + INIT_HDR_RATE) in Q30 115 */ 116 bweStr->maxBwInv = kInvBandwidth[3]; 117 bweStr->minBwInv = kInvBandwidth[2]; 118 119 return 0; 120 } 121 122 /**************************************************************************** 123 * WebRtcIsacfix_UpdateUplinkBwImpl(...) 124 * 125 * This function updates bottle neck rate received from other side in payload 126 * and calculates a new bottle neck to send to the other side. 127 * 128 * Input/Output: 129 * - bweStr : struct containing bandwidth information. 130 * - rtpNumber : value from RTP packet, from NetEq 131 * - frameSize : length of signal frame in ms, from iSAC decoder 132 * - sendTime : value in RTP header giving send time in samples 133 * - arrivalTime : value given by timeGetTime() time of arrival in 134 * samples of packet from NetEq 135 * - pksize : size of packet in bytes, from NetEq 136 * - Index : integer (range 0...23) indicating bottle neck & 137 * jitter as estimated by other side 138 * 139 * Return value : 0 if everything went fine, 140 * -1 otherwise 141 */ 142 int32_t WebRtcIsacfix_UpdateUplinkBwImpl(BwEstimatorstr *bweStr, 143 const uint16_t rtpNumber, 144 const int16_t frameSize, 145 const uint32_t sendTime, 146 const uint32_t arrivalTime, 147 const int16_t pksize, 148 const uint16_t Index) 149 { 150 uint16_t weight = 0; 151 uint32_t currBwInv = 0; 152 uint16_t recRtpRate; 153 uint32_t arrTimeProj; 154 int32_t arrTimeDiff; 155 int32_t arrTimeNoise; 156 int32_t arrTimeNoiseAbs; 157 int32_t sendTimeDiff; 158 159 int32_t delayCorrFactor = DELAY_CORRECTION_MED; 160 int32_t lateDiff = 0; 161 int16_t immediateSet = 0; 162 int32_t frameSizeSampl; 163 164 int32_t temp; 165 int32_t msec; 166 uint32_t exponent; 167 uint32_t reductionFactor; 168 uint32_t numBytesInv; 169 int32_t sign; 170 171 uint32_t byteSecondsPerBit; 172 uint32_t tempLower; 173 uint32_t tempUpper; 174 int32_t recBwAvgInv; 175 int32_t numPktsExpected; 176 177 int16_t errCode; 178 179 /* UPDATE ESTIMATES FROM OTHER SIDE */ 180 181 /* The function also checks if Index has a valid value */ 182 errCode = WebRtcIsacfix_UpdateUplinkBwRec(bweStr, Index); 183 if (errCode <0) { 184 return(errCode); 185 } 186 187 188 /* UPDATE ESTIMATES ON THIS SIDE */ 189 190 /* Bits per second per byte * 1/30 or 1/60 */ 191 if (frameSize == 60) { 192 /* If frameSize changed since last call, from 30 to 60, recalculate some values */ 193 if ( (frameSize != bweStr->prevFrameSizeMs) && (bweStr->countUpdates > 0)) { 194 bweStr->countUpdates = 10; 195 bweStr->recHeaderRate = kRecHeaderRate[1]; 196 197 bweStr->maxBwInv = kInvBandwidth[3]; 198 bweStr->minBwInv = kInvBandwidth[2]; 199 bweStr->recBwInv = 1073741824 / (bweStr->recBw + bweStr->recHeaderRate); 200 } 201 202 /* kBitsByteSec is in Q15 */ 203 recRtpRate = (int16_t)WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL(kBitsByteSec, 204 (int32_t)pksize), 15) + bweStr->recHeaderRate; 205 206 } else { 207 /* If frameSize changed since last call, from 60 to 30, recalculate some values */ 208 if ( (frameSize != bweStr->prevFrameSizeMs) && (bweStr->countUpdates > 0)) { 209 bweStr->countUpdates = 10; 210 bweStr->recHeaderRate = kRecHeaderRate[0]; 211 212 bweStr->maxBwInv = kInvBandwidth[1]; 213 bweStr->minBwInv = kInvBandwidth[0]; 214 bweStr->recBwInv = 1073741824 / (bweStr->recBw + bweStr->recHeaderRate); 215 } 216 217 /* kBitsByteSec is in Q14 */ 218 recRtpRate = (uint16_t)WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL(kBitsByteSec, 219 (int32_t)pksize), 14) + bweStr->recHeaderRate; 220 } 221 222 223 /* Check for timer wrap-around */ 224 if (arrivalTime < bweStr->prevArrivalTime) { 225 bweStr->prevArrivalTime = arrivalTime; 226 bweStr->lastUpdate = arrivalTime; 227 bweStr->lastReduction = arrivalTime + FS3; 228 229 bweStr->countRecPkts = 0; 230 231 /* store frame size */ 232 bweStr->prevFrameSizeMs = frameSize; 233 234 /* store far-side transmission rate */ 235 bweStr->prevRtpRate = recRtpRate; 236 237 /* store far-side RTP time stamp */ 238 bweStr->prevRtpNumber = rtpNumber; 239 240 return 0; 241 } 242 243 bweStr->countRecPkts++; 244 245 /* Calculate framesize in msec */ 246 frameSizeSampl = WEBRTC_SPL_MUL_16_16((int16_t)SAMPLES_PER_MSEC, frameSize); 247 248 /* Check that it's not one of the first 9 packets */ 249 if ( bweStr->countUpdates > 0 ) { 250 251 /* Stay in Wait Period for 1.5 seconds (no updates in wait period) */ 252 if(bweStr->inWaitPeriod) { 253 if ((arrivalTime - bweStr->startWaitPeriod)> FS_1_HALF) { 254 bweStr->inWaitPeriod = 0; 255 } 256 } 257 258 /* If not been updated for a long time, reduce the BN estimate */ 259 260 /* Check send time difference between this packet and previous received */ 261 sendTimeDiff = sendTime - bweStr->prevSendTime; 262 if (sendTimeDiff <= WEBRTC_SPL_LSHIFT_W32(frameSizeSampl, 1)) { 263 264 /* Only update if 3 seconds has past since last update */ 265 if ((arrivalTime - bweStr->lastUpdate) > FS3) { 266 267 /* Calculate expected number of received packets since last update */ 268 numPktsExpected = (arrivalTime - bweStr->lastUpdate) / frameSizeSampl; 269 270 /* If received number of packets is more than 90% of expected (922 = 0.9 in Q10): */ 271 /* do the update, else not */ 272 if(WEBRTC_SPL_LSHIFT_W32(bweStr->countRecPkts, 10) > WEBRTC_SPL_MUL_16_16(922, numPktsExpected)) { 273 /* Q4 chosen to approx dividing by 16 */ 274 msec = (arrivalTime - bweStr->lastReduction); 275 276 /* the number below represents 13 seconds, highly unlikely 277 but to insure no overflow when reduction factor is multiplied by recBw inverse */ 278 if (msec > 208000) { 279 msec = 208000; 280 } 281 282 /* Q20 2^(negative number: - 76/1048576) = .99995 283 product is Q24 */ 284 exponent = WEBRTC_SPL_UMUL(0x0000004C, msec); 285 286 /* do the approx with positive exponent so that value is actually rf^-1 287 and multiply by bw inverse */ 288 reductionFactor = WEBRTC_SPL_RSHIFT_U32(0x01000000 | (exponent & 0x00FFFFFF), 289 WEBRTC_SPL_RSHIFT_U32(exponent, 24)); 290 291 /* reductionFactor in Q13 */ 292 reductionFactor = WEBRTC_SPL_RSHIFT_U32(reductionFactor, 11); 293 294 if ( reductionFactor != 0 ) { 295 bweStr->recBwInv = WEBRTC_SPL_MUL((int32_t)bweStr->recBwInv, (int32_t)reductionFactor); 296 bweStr->recBwInv = WEBRTC_SPL_RSHIFT_W32((int32_t)bweStr->recBwInv, 13); 297 298 } else { 299 static const uint32_t kInitRate = INIT_BN_EST + INIT_HDR_RATE; 300 /* recBwInv = 1 / kInitRate in Q26 (Q30??)*/ 301 bweStr->recBwInv = (1073741824 + kInitRate / 2) / kInitRate; 302 } 303 304 /* reset time-since-update counter */ 305 bweStr->lastReduction = arrivalTime; 306 } else { 307 /* Delay last reduction with 3 seconds */ 308 bweStr->lastReduction = arrivalTime + FS3; 309 bweStr->lastUpdate = arrivalTime; 310 bweStr->countRecPkts = 0; 311 } 312 } 313 } else { 314 bweStr->lastReduction = arrivalTime + FS3; 315 bweStr->lastUpdate = arrivalTime; 316 bweStr->countRecPkts = 0; 317 } 318 319 320 /* update only if previous packet was not lost */ 321 if ( rtpNumber == bweStr->prevRtpNumber + 1 ) { 322 arrTimeDiff = arrivalTime - bweStr->prevArrivalTime; 323 324 if (!(bweStr->highSpeedSend && bweStr->highSpeedRec)) { 325 if (arrTimeDiff > frameSizeSampl) { 326 if (sendTimeDiff > 0) { 327 lateDiff = arrTimeDiff - sendTimeDiff - 328 WEBRTC_SPL_LSHIFT_W32(frameSizeSampl, 1); 329 } else { 330 lateDiff = arrTimeDiff - frameSizeSampl; 331 } 332 333 /* 8000 is 1/2 second (in samples at FS) */ 334 if (lateDiff > 8000) { 335 delayCorrFactor = (int32_t) DELAY_CORRECTION_MAX; 336 bweStr->inWaitPeriod = 1; 337 bweStr->startWaitPeriod = arrivalTime; 338 immediateSet = 1; 339 } else if (lateDiff > 5120) { 340 delayCorrFactor = (int32_t) DELAY_CORRECTION_MED; 341 immediateSet = 1; 342 bweStr->inWaitPeriod = 1; 343 bweStr->startWaitPeriod = arrivalTime; 344 } 345 } 346 } 347 348 if ((bweStr->prevRtpRate > WEBRTC_SPL_RSHIFT_W32((int32_t) bweStr->recBwAvg, 5)) && 349 (recRtpRate > WEBRTC_SPL_RSHIFT_W32((int32_t)bweStr->recBwAvg, 5)) && 350 !bweStr->inWaitPeriod) { 351 352 /* test if still in initiation period and increment counter */ 353 if (bweStr->countUpdates++ > 99) { 354 /* constant weight after initiation part, 0.01 in Q13 */ 355 weight = (uint16_t) 82; 356 } else { 357 /* weight decreases with number of updates, 1/countUpdates in Q13 */ 358 weight = (uint16_t) WebRtcSpl_DivW32W16( 359 (int32_t)(8192 + WEBRTC_SPL_RSHIFT_W32((int32_t) bweStr->countUpdates, 1)), 360 (int16_t)bweStr->countUpdates); 361 } 362 363 /* Bottle Neck Estimation */ 364 365 /* limit outliers, if more than 25 ms too much */ 366 if (arrTimeDiff > frameSizeSampl + kSamplesIn25msec) { 367 arrTimeDiff = frameSizeSampl + kSamplesIn25msec; 368 } 369 370 /* don't allow it to be less than frame rate - 10 ms */ 371 if (arrTimeDiff < frameSizeSampl - FRAMESAMPLES_10ms) { 372 arrTimeDiff = frameSizeSampl - FRAMESAMPLES_10ms; 373 } 374 375 /* compute inverse receiving rate for last packet, in Q19 */ 376 numBytesInv = (uint16_t) WebRtcSpl_DivW32W16( 377 (int32_t)(524288 + WEBRTC_SPL_RSHIFT_W32(((int32_t)pksize + HEADER_SIZE), 1)), 378 (int16_t)(pksize + HEADER_SIZE)); 379 380 /* 8389 is ~ 1/128000 in Q30 */ 381 byteSecondsPerBit = WEBRTC_SPL_MUL_16_16(arrTimeDiff, 8389); 382 383 /* get upper N bits */ 384 tempUpper = WEBRTC_SPL_RSHIFT_U32(byteSecondsPerBit, 15); 385 386 /* get lower 15 bits */ 387 tempLower = byteSecondsPerBit & 0x00007FFF; 388 389 tempUpper = WEBRTC_SPL_MUL(tempUpper, numBytesInv); 390 tempLower = WEBRTC_SPL_MUL(tempLower, numBytesInv); 391 tempLower = WEBRTC_SPL_RSHIFT_U32(tempLower, 15); 392 393 currBwInv = tempUpper + tempLower; 394 currBwInv = WEBRTC_SPL_RSHIFT_U32(currBwInv, 4); 395 396 /* Limit inv rate. Note that minBwInv > maxBwInv! */ 397 if(currBwInv < bweStr->maxBwInv) { 398 currBwInv = bweStr->maxBwInv; 399 } else if(currBwInv > bweStr->minBwInv) { 400 currBwInv = bweStr->minBwInv; 401 } 402 403 /* update bottle neck rate estimate */ 404 bweStr->recBwInv = WEBRTC_SPL_UMUL(weight, currBwInv) + 405 WEBRTC_SPL_UMUL((uint32_t) 8192 - weight, bweStr->recBwInv); 406 407 /* Shift back to Q30 from Q40 (actual used bits shouldn't be more than 27 based on minBwInv) 408 up to 30 bits used with Q13 weight */ 409 bweStr->recBwInv = WEBRTC_SPL_RSHIFT_U32(bweStr->recBwInv, 13); 410 411 /* reset time-since-update counter */ 412 bweStr->lastUpdate = arrivalTime; 413 bweStr->lastReduction = arrivalTime + FS3; 414 bweStr->countRecPkts = 0; 415 416 /* to save resolution compute the inverse of recBwAvg in Q26 by left shifting numerator to 2^31 417 and NOT right shifting recBwAvg 5 bits to an integer 418 At max 13 bits are used 419 shift to Q5 */ 420 recBwAvgInv = (0x80000000 + bweStr->recBwAvg / 2) / bweStr->recBwAvg; 421 422 /* Calculate Projected arrival time difference */ 423 424 /* The numerator of the quotient can be 22 bits so right shift inv by 4 to avoid overflow 425 result in Q22 */ 426 arrTimeProj = WEBRTC_SPL_MUL((int32_t)8000, recBwAvgInv); 427 /* shift to Q22 */ 428 arrTimeProj = WEBRTC_SPL_RSHIFT_U32(arrTimeProj, 4); 429 /* complete calulation */ 430 arrTimeProj = WEBRTC_SPL_MUL(((int32_t)pksize + HEADER_SIZE), arrTimeProj); 431 /* shift to Q10 */ 432 arrTimeProj = WEBRTC_SPL_RSHIFT_U32(arrTimeProj, 12); 433 434 /* difference between projected and actual arrival time differences */ 435 /* Q9 (only shift arrTimeDiff by 5 to simulate divide by 16 (need to revisit if change sampling rate) DH */ 436 if (WEBRTC_SPL_LSHIFT_W32(arrTimeDiff, 6) > (int32_t)arrTimeProj) { 437 arrTimeNoise = WEBRTC_SPL_LSHIFT_W32(arrTimeDiff, 6) - arrTimeProj; 438 sign = 1; 439 } else { 440 arrTimeNoise = arrTimeProj - WEBRTC_SPL_LSHIFT_W32(arrTimeDiff, 6); 441 sign = -1; 442 } 443 444 /* Q9 */ 445 arrTimeNoiseAbs = arrTimeNoise; 446 447 /* long term averaged absolute jitter, Q15 */ 448 weight = WEBRTC_SPL_RSHIFT_W32(weight, 3); 449 bweStr->recJitter = WEBRTC_SPL_MUL(weight, WEBRTC_SPL_LSHIFT_W32(arrTimeNoiseAbs, 5)) 450 + WEBRTC_SPL_MUL(1024 - weight, bweStr->recJitter); 451 452 /* remove the fractional portion */ 453 bweStr->recJitter = WEBRTC_SPL_RSHIFT_W32(bweStr->recJitter, 10); 454 455 /* Maximum jitter is 10 msec in Q15 */ 456 if (bweStr->recJitter > (int32_t)327680) { 457 bweStr->recJitter = (int32_t)327680; 458 } 459 460 /* short term averaged absolute jitter */ 461 /* Calculation in Q13 products in Q23 */ 462 bweStr->recJitterShortTermAbs = WEBRTC_SPL_MUL(51, WEBRTC_SPL_LSHIFT_W32(arrTimeNoiseAbs, 3)) + 463 WEBRTC_SPL_MUL(973, bweStr->recJitterShortTermAbs); 464 bweStr->recJitterShortTermAbs = WEBRTC_SPL_RSHIFT_W32(bweStr->recJitterShortTermAbs , 10); 465 466 /* short term averaged jitter */ 467 /* Calculation in Q13 products in Q23 */ 468 bweStr->recJitterShortTerm = WEBRTC_SPL_MUL(205, WEBRTC_SPL_LSHIFT_W32(arrTimeNoise, 3)) * sign + 469 WEBRTC_SPL_MUL(3891, bweStr->recJitterShortTerm); 470 471 if (bweStr->recJitterShortTerm < 0) { 472 temp = -bweStr->recJitterShortTerm; 473 temp = WEBRTC_SPL_RSHIFT_W32(temp, 12); 474 bweStr->recJitterShortTerm = -temp; 475 } else { 476 bweStr->recJitterShortTerm = WEBRTC_SPL_RSHIFT_W32(bweStr->recJitterShortTerm, 12); 477 } 478 } 479 } 480 } else { 481 /* reset time-since-update counter when receiving the first 9 packets */ 482 bweStr->lastUpdate = arrivalTime; 483 bweStr->lastReduction = arrivalTime + FS3; 484 bweStr->countRecPkts = 0; 485 bweStr->countUpdates++; 486 } 487 488 /* Limit to minimum or maximum bottle neck rate (in Q30) */ 489 if (bweStr->recBwInv > bweStr->minBwInv) { 490 bweStr->recBwInv = bweStr->minBwInv; 491 } else if (bweStr->recBwInv < bweStr->maxBwInv) { 492 bweStr->recBwInv = bweStr->maxBwInv; 493 } 494 495 496 /* store frame length */ 497 bweStr->prevFrameSizeMs = frameSize; 498 499 /* store far-side transmission rate */ 500 bweStr->prevRtpRate = recRtpRate; 501 502 /* store far-side RTP time stamp */ 503 bweStr->prevRtpNumber = rtpNumber; 504 505 /* Replace bweStr->recMaxDelay by the new value (atomic operation) */ 506 if (bweStr->prevArrivalTime != 0xffffffff) { 507 bweStr->recMaxDelay = WEBRTC_SPL_MUL(3, bweStr->recJitter); 508 } 509 510 /* store arrival time stamp */ 511 bweStr->prevArrivalTime = arrivalTime; 512 bweStr->prevSendTime = sendTime; 513 514 /* Replace bweStr->recBw by the new value */ 515 bweStr->recBw = 1073741824 / bweStr->recBwInv - bweStr->recHeaderRate; 516 517 if (immediateSet) { 518 /* delay correction factor is in Q10 */ 519 bweStr->recBw = WEBRTC_SPL_UMUL(delayCorrFactor, bweStr->recBw); 520 bweStr->recBw = WEBRTC_SPL_RSHIFT_U32(bweStr->recBw, 10); 521 522 if (bweStr->recBw < (int32_t) MIN_ISAC_BW) { 523 bweStr->recBw = (int32_t) MIN_ISAC_BW; 524 } 525 526 bweStr->recBwAvg = WEBRTC_SPL_LSHIFT_U32(bweStr->recBw + bweStr->recHeaderRate, 5); 527 528 bweStr->recBwAvgQ = WEBRTC_SPL_LSHIFT_U32(bweStr->recBw, 7); 529 530 bweStr->recJitterShortTerm = 0; 531 532 bweStr->recBwInv = 1073741824 / (bweStr->recBw + bweStr->recHeaderRate); 533 534 immediateSet = 0; 535 } 536 537 538 return 0; 539 } 540 541 /* This function updates the send bottle neck rate */ 542 /* Index - integer (range 0...23) indicating bottle neck & jitter as estimated by other side */ 543 /* returns 0 if everything went fine, -1 otherwise */ 544 int16_t WebRtcIsacfix_UpdateUplinkBwRec(BwEstimatorstr *bweStr, 545 const int16_t Index) 546 { 547 uint16_t RateInd; 548 549 if ( (Index < 0) || (Index > 23) ) { 550 return -ISAC_RANGE_ERROR_BW_ESTIMATOR; 551 } 552 553 /* UPDATE ESTIMATES FROM OTHER SIDE */ 554 555 if ( Index > 11 ) { 556 RateInd = Index - 12; 557 /* compute the jitter estimate as decoded on the other side in Q9 */ 558 /* sendMaxDelayAvg = 0.9 * sendMaxDelayAvg + 0.1 * MAX_ISAC_MD */ 559 bweStr->sendMaxDelayAvg = WEBRTC_SPL_MUL(461, bweStr->sendMaxDelayAvg) + 560 WEBRTC_SPL_MUL(51, WEBRTC_SPL_LSHIFT_W32((int32_t)MAX_ISAC_MD, 9)); 561 bweStr->sendMaxDelayAvg = WEBRTC_SPL_RSHIFT_W32(bweStr->sendMaxDelayAvg, 9); 562 563 } else { 564 RateInd = Index; 565 /* compute the jitter estimate as decoded on the other side in Q9 */ 566 /* sendMaxDelayAvg = 0.9 * sendMaxDelayAvg + 0.1 * MIN_ISAC_MD */ 567 bweStr->sendMaxDelayAvg = WEBRTC_SPL_MUL(461, bweStr->sendMaxDelayAvg) + 568 WEBRTC_SPL_MUL(51, WEBRTC_SPL_LSHIFT_W32((int32_t)MIN_ISAC_MD,9)); 569 bweStr->sendMaxDelayAvg = WEBRTC_SPL_RSHIFT_W32(bweStr->sendMaxDelayAvg, 9); 570 571 } 572 573 574 /* compute the BN estimate as decoded on the other side */ 575 /* sendBwAvg = 0.9 * sendBwAvg + 0.1 * kQRateTable[RateInd]; */ 576 bweStr->sendBwAvg = WEBRTC_SPL_UMUL(461, bweStr->sendBwAvg) + 577 WEBRTC_SPL_UMUL(51, WEBRTC_SPL_LSHIFT_U32(kQRateTable[RateInd], 7)); 578 bweStr->sendBwAvg = WEBRTC_SPL_RSHIFT_U32(bweStr->sendBwAvg, 9); 579 580 581 if (WEBRTC_SPL_RSHIFT_U32(bweStr->sendBwAvg, 7) > 28000 && !bweStr->highSpeedSend) { 582 bweStr->countHighSpeedSent++; 583 584 /* approx 2 seconds with 30ms frames */ 585 if (bweStr->countHighSpeedSent >= 66) { 586 bweStr->highSpeedSend = 1; 587 } 588 } else if (!bweStr->highSpeedSend) { 589 bweStr->countHighSpeedSent = 0; 590 } 591 592 return 0; 593 } 594 595 /**************************************************************************** 596 * WebRtcIsacfix_GetDownlinkBwIndexImpl(...) 597 * 598 * This function calculates and returns the bandwidth/jitter estimation code 599 * (integer 0...23) to put in the sending iSAC payload. 600 * 601 * Input: 602 * - bweStr : BWE struct 603 * 604 * Return: 605 * bandwith and jitter index (0..23) 606 */ 607 uint16_t WebRtcIsacfix_GetDownlinkBwIndexImpl(BwEstimatorstr *bweStr) 608 { 609 int32_t rate; 610 int32_t maxDelay; 611 uint16_t rateInd; 612 uint16_t maxDelayBit; 613 int32_t tempTerm1; 614 int32_t tempTerm2; 615 int32_t tempTermX; 616 int32_t tempTermY; 617 int32_t tempMin; 618 int32_t tempMax; 619 620 /* Get Rate Index */ 621 622 /* Get unquantized rate. Always returns 10000 <= rate <= 32000 */ 623 rate = WebRtcIsacfix_GetDownlinkBandwidth(bweStr); 624 625 /* Compute the averaged BN estimate on this side */ 626 627 /* recBwAvg = 0.9 * recBwAvg + 0.1 * (rate + bweStr->recHeaderRate), 0.9 and 0.1 in Q9 */ 628 bweStr->recBwAvg = WEBRTC_SPL_UMUL(922, bweStr->recBwAvg) + 629 WEBRTC_SPL_UMUL(102, WEBRTC_SPL_LSHIFT_U32((uint32_t)rate + bweStr->recHeaderRate, 5)); 630 bweStr->recBwAvg = WEBRTC_SPL_RSHIFT_U32(bweStr->recBwAvg, 10); 631 632 /* Find quantization index that gives the closest rate after averaging. 633 * Note that we don't need to check the last value, rate <= kQRateTable[11], 634 * because we will use rateInd = 11 even if rate > kQRateTable[11]. */ 635 for (rateInd = 1; rateInd < 11; rateInd++) { 636 if (rate <= kQRateTable[rateInd]){ 637 break; 638 } 639 } 640 641 /* find closest quantization index, and update quantized average by taking: */ 642 /* 0.9*recBwAvgQ + 0.1*kQRateTable[rateInd] */ 643 644 /* 0.9 times recBwAvgQ in Q16 */ 645 /* 461/512 - 25/65536 =0.900009 */ 646 tempTerm1 = WEBRTC_SPL_MUL(bweStr->recBwAvgQ, 25); 647 tempTerm1 = WEBRTC_SPL_RSHIFT_W32(tempTerm1, 7); 648 tempTermX = WEBRTC_SPL_UMUL(461, bweStr->recBwAvgQ) - tempTerm1; 649 650 /* rate in Q16 */ 651 tempTermY = WEBRTC_SPL_LSHIFT_W32((int32_t)rate, 16); 652 653 /* 0.1 * kQRateTable[rateInd] = KQRate01[rateInd] */ 654 tempTerm1 = tempTermX + KQRate01[rateInd] - tempTermY; 655 tempTerm2 = tempTermY - tempTermX - KQRate01[rateInd-1]; 656 657 /* Compare (0.9 * recBwAvgQ + 0.1 * kQRateTable[rateInd] - rate) > 658 (rate - 0.9 * recBwAvgQ - 0.1 * kQRateTable[rateInd-1]) */ 659 if (tempTerm1 > tempTerm2) { 660 rateInd--; 661 } 662 663 /* Update quantized average by taking: */ 664 /* 0.9*recBwAvgQ + 0.1*kQRateTable[rateInd] */ 665 666 /* Add 0.1 times kQRateTable[rateInd], in Q16 */ 667 tempTermX += KQRate01[rateInd]; 668 669 /* Shift back to Q7 */ 670 bweStr->recBwAvgQ = WEBRTC_SPL_RSHIFT_W32(tempTermX, 9); 671 672 /* Count consecutive received bandwidth above 28000 kbps (28000 in Q7 = 3584000) */ 673 /* If 66 high estimates in a row, set highSpeedRec to one */ 674 /* 66 corresponds to ~2 seconds in 30 msec mode */ 675 if ((bweStr->recBwAvgQ > 3584000) && !bweStr->highSpeedRec) { 676 bweStr->countHighSpeedRec++; 677 if (bweStr->countHighSpeedRec >= 66) { 678 bweStr->highSpeedRec = 1; 679 } 680 } else if (!bweStr->highSpeedRec) { 681 bweStr->countHighSpeedRec = 0; 682 } 683 684 /* Get Max Delay Bit */ 685 686 /* get unquantized max delay */ 687 maxDelay = WebRtcIsacfix_GetDownlinkMaxDelay(bweStr); 688 689 /* Update quantized max delay average */ 690 tempMax = 652800; /* MAX_ISAC_MD * 0.1 in Q18 */ 691 tempMin = 130560; /* MIN_ISAC_MD * 0.1 in Q18 */ 692 tempTermX = WEBRTC_SPL_MUL((int32_t)bweStr->recMaxDelayAvgQ, (int32_t)461); 693 tempTermY = WEBRTC_SPL_LSHIFT_W32((int32_t)maxDelay, 18); 694 695 tempTerm1 = tempTermX + tempMax - tempTermY; 696 tempTerm2 = tempTermY - tempTermX - tempMin; 697 698 if ( tempTerm1 > tempTerm2) { 699 maxDelayBit = 0; 700 tempTerm1 = tempTermX + tempMin; 701 702 /* update quantized average, shift back to Q9 */ 703 bweStr->recMaxDelayAvgQ = WEBRTC_SPL_RSHIFT_W32(tempTerm1, 9); 704 } else { 705 maxDelayBit = 12; 706 tempTerm1 = tempTermX + tempMax; 707 708 /* update quantized average, shift back to Q9 */ 709 bweStr->recMaxDelayAvgQ = WEBRTC_SPL_RSHIFT_W32(tempTerm1, 9); 710 } 711 712 /* Return bandwitdh and jitter index (0..23) */ 713 return (uint16_t)(rateInd + maxDelayBit); 714 } 715 716 /* get the bottle neck rate from far side to here, as estimated on this side */ 717 uint16_t WebRtcIsacfix_GetDownlinkBandwidth(const BwEstimatorstr *bweStr) 718 { 719 uint32_t recBw; 720 int32_t jitter_sign; /* Q8 */ 721 int32_t bw_adjust; /* Q16 */ 722 int32_t rec_jitter_short_term_abs_inv; /* Q18 */ 723 int32_t temp; 724 725 /* Q18 rec jitter short term abs is in Q13, multiply it by 2^13 to save precision 726 2^18 then needs to be shifted 13 bits to 2^31 */ 727 rec_jitter_short_term_abs_inv = 0x80000000u / bweStr->recJitterShortTermAbs; 728 729 /* Q27 = 9 + 18 */ 730 jitter_sign = WEBRTC_SPL_MUL(WEBRTC_SPL_RSHIFT_W32(bweStr->recJitterShortTerm, 4), (int32_t)rec_jitter_short_term_abs_inv); 731 732 if (jitter_sign < 0) { 733 temp = -jitter_sign; 734 temp = WEBRTC_SPL_RSHIFT_W32(temp, 19); 735 jitter_sign = -temp; 736 } else { 737 jitter_sign = WEBRTC_SPL_RSHIFT_W32(jitter_sign, 19); 738 } 739 740 /* adjust bw proportionally to negative average jitter sign */ 741 //bw_adjust = 1.0f - jitter_sign * (0.15f + 0.15f * jitter_sign * jitter_sign); 742 //Q8 -> Q16 .15 +.15 * jitter^2 first term is .15 in Q16 latter term is Q8*Q8*Q8 743 //38 in Q8 ~.15 9830 in Q16 ~.15 744 temp = 9830 + WEBRTC_SPL_RSHIFT_W32((WEBRTC_SPL_MUL(38, WEBRTC_SPL_MUL(jitter_sign, jitter_sign))), 8); 745 746 if (jitter_sign < 0) { 747 temp = WEBRTC_SPL_MUL(jitter_sign, temp); 748 temp = -temp; 749 temp = WEBRTC_SPL_RSHIFT_W32(temp, 8); 750 bw_adjust = (uint32_t)65536 + temp; /* (1 << 16) + temp; */ 751 } else { 752 bw_adjust = (uint32_t)65536 - WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL(jitter_sign, temp), 8);/* (1 << 16) - ((jitter_sign * temp) >> 8); */ 753 } 754 755 //make sure following multiplication won't overflow 756 //bw adjust now Q14 757 bw_adjust = WEBRTC_SPL_RSHIFT_W32(bw_adjust, 2);//see if good resolution is maintained 758 759 /* adjust Rate if jitter sign is mostly constant */ 760 recBw = WEBRTC_SPL_UMUL(bweStr->recBw, bw_adjust); 761 762 recBw = WEBRTC_SPL_RSHIFT_W32(recBw, 14); 763 764 /* limit range of bottle neck rate */ 765 if (recBw < MIN_ISAC_BW) { 766 recBw = MIN_ISAC_BW; 767 } else if (recBw > MAX_ISAC_BW) { 768 recBw = MAX_ISAC_BW; 769 } 770 771 return (uint16_t) recBw; 772 } 773 774 /* Returns the mmax delay (in ms) */ 775 int16_t WebRtcIsacfix_GetDownlinkMaxDelay(const BwEstimatorstr *bweStr) 776 { 777 int16_t recMaxDelay; 778 779 recMaxDelay = (int16_t) WEBRTC_SPL_RSHIFT_W32(bweStr->recMaxDelay, 15); 780 781 /* limit range of jitter estimate */ 782 if (recMaxDelay < MIN_ISAC_MD) { 783 recMaxDelay = MIN_ISAC_MD; 784 } else if (recMaxDelay > MAX_ISAC_MD) { 785 recMaxDelay = MAX_ISAC_MD; 786 } 787 788 return recMaxDelay; 789 } 790 791 /* get the bottle neck rate from here to far side, as estimated by far side */ 792 int16_t WebRtcIsacfix_GetUplinkBandwidth(const BwEstimatorstr *bweStr) 793 { 794 int16_t send_bw; 795 796 send_bw = (int16_t) WEBRTC_SPL_RSHIFT_U32(bweStr->sendBwAvg, 7); 797 798 /* limit range of bottle neck rate */ 799 if (send_bw < MIN_ISAC_BW) { 800 send_bw = MIN_ISAC_BW; 801 } else if (send_bw > MAX_ISAC_BW) { 802 send_bw = MAX_ISAC_BW; 803 } 804 805 return send_bw; 806 } 807 808 809 810 /* Returns the max delay value from the other side in ms */ 811 int16_t WebRtcIsacfix_GetUplinkMaxDelay(const BwEstimatorstr *bweStr) 812 { 813 int16_t send_max_delay; 814 815 send_max_delay = (int16_t) WEBRTC_SPL_RSHIFT_W32(bweStr->sendMaxDelayAvg, 9); 816 817 /* limit range of jitter estimate */ 818 if (send_max_delay < MIN_ISAC_MD) { 819 send_max_delay = MIN_ISAC_MD; 820 } else if (send_max_delay > MAX_ISAC_MD) { 821 send_max_delay = MAX_ISAC_MD; 822 } 823 824 return send_max_delay; 825 } 826 827 828 829 830 /* 831 * update long-term average bitrate and amount of data in buffer 832 * returns minimum payload size (bytes) 833 */ 834 uint16_t WebRtcIsacfix_GetMinBytes(RateModel *State, 835 int16_t StreamSize, /* bytes in bitstream */ 836 const int16_t FrameSamples, /* samples per frame */ 837 const int16_t BottleNeck, /* bottle neck rate; excl headers (bps) */ 838 const int16_t DelayBuildUp) /* max delay from bottle neck buffering (ms) */ 839 { 840 int32_t MinRate = 0; 841 uint16_t MinBytes; 842 int16_t TransmissionTime; 843 int32_t inv_Q12; 844 int32_t den; 845 846 847 /* first 10 packets @ low rate, then INIT_BURST_LEN packets @ fixed rate of INIT_RATE bps */ 848 if (State->InitCounter > 0) { 849 if (State->InitCounter-- <= INIT_BURST_LEN) { 850 MinRate = INIT_RATE; 851 } else { 852 MinRate = 0; 853 } 854 } else { 855 /* handle burst */ 856 if (State->BurstCounter) { 857 if (State->StillBuffered < 858 (((512 - 512 / BURST_LEN) * DelayBuildUp) >> 9)) { 859 /* max bps derived from BottleNeck and DelayBuildUp values */ 860 inv_Q12 = 4096 / (BURST_LEN * FrameSamples); 861 MinRate = WEBRTC_SPL_MUL(512 + WEBRTC_SPL_MUL(SAMPLES_PER_MSEC, WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL(DelayBuildUp, inv_Q12), 3)), BottleNeck); 862 } else { 863 /* max bps derived from StillBuffered and DelayBuildUp values */ 864 inv_Q12 = 4096 / FrameSamples; 865 if (DelayBuildUp > State->StillBuffered) { 866 MinRate = WEBRTC_SPL_MUL(512 + WEBRTC_SPL_MUL(SAMPLES_PER_MSEC, WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL(DelayBuildUp - State->StillBuffered, inv_Q12), 3)), BottleNeck); 867 } else if ((den = WEBRTC_SPL_MUL(SAMPLES_PER_MSEC, (State->StillBuffered - DelayBuildUp))) >= FrameSamples) { 868 /* MinRate will be negative here */ 869 MinRate = 0; 870 } else { 871 MinRate = WEBRTC_SPL_MUL((512 - WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL(den, inv_Q12), 3)), BottleNeck); 872 } 873 //if (MinRate < 1.04 * BottleNeck) 874 // MinRate = 1.04 * BottleNeck; 875 //Q9 876 if (MinRate < WEBRTC_SPL_MUL(532, BottleNeck)) { 877 MinRate += WEBRTC_SPL_MUL(22, BottleNeck); 878 } 879 } 880 881 State->BurstCounter--; 882 } 883 } 884 885 886 /* convert rate from bits/second to bytes/packet */ 887 //round and shift before conversion 888 MinRate += 256; 889 MinRate = WEBRTC_SPL_RSHIFT_W32(MinRate, 9); 890 MinBytes = MinRate * FrameSamples / FS8; 891 892 /* StreamSize will be adjusted if less than MinBytes */ 893 if (StreamSize < MinBytes) { 894 StreamSize = MinBytes; 895 } 896 897 /* keep track of when bottle neck was last exceeded by at least 1% */ 898 //517/512 ~ 1.01 899 if ((StreamSize * (int32_t)FS8) / FrameSamples > (517 * BottleNeck) >> 9) { 900 if (State->PrevExceed) { 901 /* bottle_neck exceded twice in a row, decrease ExceedAgo */ 902 State->ExceedAgo -= BURST_INTERVAL / (BURST_LEN - 1); 903 if (State->ExceedAgo < 0) { 904 State->ExceedAgo = 0; 905 } 906 } else { 907 State->ExceedAgo += (int16_t)WEBRTC_SPL_RSHIFT_W16(FrameSamples, 4); /* ms */ 908 State->PrevExceed = 1; 909 } 910 } else { 911 State->PrevExceed = 0; 912 State->ExceedAgo += (int16_t)WEBRTC_SPL_RSHIFT_W16(FrameSamples, 4); /* ms */ 913 } 914 915 /* set burst flag if bottle neck not exceeded for long time */ 916 if ((State->ExceedAgo > BURST_INTERVAL) && (State->BurstCounter == 0)) { 917 if (State->PrevExceed) { 918 State->BurstCounter = BURST_LEN - 1; 919 } else { 920 State->BurstCounter = BURST_LEN; 921 } 922 } 923 924 925 /* Update buffer delay */ 926 TransmissionTime = (StreamSize * 8000) / BottleNeck; /* ms */ 927 State->StillBuffered += TransmissionTime; 928 State->StillBuffered -= (int16_t)WEBRTC_SPL_RSHIFT_W16(FrameSamples, 4); //>>4 = SAMPLES_PER_MSEC /* ms */ 929 if (State->StillBuffered < 0) { 930 State->StillBuffered = 0; 931 } 932 933 if (State->StillBuffered > 2000) { 934 State->StillBuffered = 2000; 935 } 936 937 return MinBytes; 938 } 939 940 941 /* 942 * update long-term average bitrate and amount of data in buffer 943 */ 944 void WebRtcIsacfix_UpdateRateModel(RateModel *State, 945 int16_t StreamSize, /* bytes in bitstream */ 946 const int16_t FrameSamples, /* samples per frame */ 947 const int16_t BottleNeck) /* bottle neck rate; excl headers (bps) */ 948 { 949 const int16_t TransmissionTime = (StreamSize * 8000) / BottleNeck; /* ms */ 950 951 /* avoid the initial "high-rate" burst */ 952 State->InitCounter = 0; 953 954 /* Update buffer delay */ 955 State->StillBuffered += TransmissionTime; 956 State->StillBuffered -= (int16_t)WEBRTC_SPL_RSHIFT_W16(FrameSamples, 4); /* ms */ 957 if (State->StillBuffered < 0) { 958 State->StillBuffered = 0; 959 } 960 961 } 962 963 964 void WebRtcIsacfix_InitRateModel(RateModel *State) 965 { 966 State->PrevExceed = 0; /* boolean */ 967 State->ExceedAgo = 0; /* ms */ 968 State->BurstCounter = 0; /* packets */ 969 State->InitCounter = INIT_BURST_LEN + 10; /* packets */ 970 State->StillBuffered = 1; /* ms */ 971 } 972 973 974 975 976 977 int16_t WebRtcIsacfix_GetNewFrameLength(int16_t bottle_neck, int16_t current_framesamples) 978 { 979 int16_t new_framesamples; 980 981 new_framesamples = current_framesamples; 982 983 /* find new framelength */ 984 switch(current_framesamples) { 985 case 480: 986 if (bottle_neck < Thld_30_60) { 987 new_framesamples = 960; 988 } 989 break; 990 case 960: 991 if (bottle_neck >= Thld_60_30) { 992 new_framesamples = 480; 993 } 994 break; 995 default: 996 new_framesamples = -1; /* Error */ 997 } 998 999 return new_framesamples; 1000 } 1001 1002 int16_t WebRtcIsacfix_GetSnr(int16_t bottle_neck, int16_t framesamples) 1003 { 1004 int16_t s2nr = 0; 1005 1006 /* find new SNR value */ 1007 //consider BottleNeck to be in Q10 ( * 1 in Q10) 1008 switch(framesamples) { 1009 case 480: 1010 /*s2nr = -1*(a_30 << 10) + ((b_30 * bottle_neck) >> 10);*/ 1011 s2nr = -22500 + (int16_t)WEBRTC_SPL_MUL_16_16_RSFT(500, bottle_neck, 10); //* 0.001; //+ c_30 * bottle_neck * bottle_neck * 0.000001; 1012 break; 1013 case 960: 1014 /*s2nr = -1*(a_60 << 10) + ((b_60 * bottle_neck) >> 10);*/ 1015 s2nr = -22500 + (int16_t)WEBRTC_SPL_MUL_16_16_RSFT(500, bottle_neck, 10); //* 0.001; //+ c_30 * bottle_neck * bottle_neck * 0.000001; 1016 break; 1017 default: 1018 s2nr = -1; /* Error */ 1019 } 1020 1021 return s2nr; //return in Q10 1022 1023 } 1024