1 /* 2 * Copyright (c) 2012 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 * isacfix.c 13 * 14 * This C file contains the functions for the ISAC API 15 * 16 */ 17 18 #include "modules/audio_coding/codecs/isac/fix/interface/isacfix.h" 19 20 #include <stdlib.h> 21 22 #include "modules/audio_coding/codecs/isac/fix/source/bandwidth_estimator.h" 23 #include "modules/audio_coding/codecs/isac/fix/source/codec.h" 24 #include "modules/audio_coding/codecs/isac/fix/source/entropy_coding.h" 25 #include "modules/audio_coding/codecs/isac/fix/source/filterbank_internal.h" 26 #include "modules/audio_coding/codecs/isac/fix/source/lpc_masking_model.h" 27 #include "modules/audio_coding/codecs/isac/fix/source/structs.h" 28 #include "system_wrappers/interface/cpu_features_wrapper.h" 29 30 // Declare function pointers. 31 FilterMaLoopFix WebRtcIsacfix_FilterMaLoopFix; 32 Spec2Time WebRtcIsacfix_Spec2Time; 33 Time2Spec WebRtcIsacfix_Time2Spec; 34 MatrixProduct1 WebRtcIsacfix_MatrixProduct1; 35 MatrixProduct2 WebRtcIsacfix_MatrixProduct2; 36 37 /************************************************************************** 38 * WebRtcIsacfix_AssignSize(...) 39 * 40 * Functions used when malloc is not allowed 41 * Returns number of bytes needed to allocate for iSAC struct. 42 * 43 */ 44 45 int16_t WebRtcIsacfix_AssignSize(int *sizeinbytes) { 46 *sizeinbytes=sizeof(ISACFIX_SubStruct)*2/sizeof(int16_t); 47 return(0); 48 } 49 50 /*************************************************************************** 51 * WebRtcIsacfix_Assign(...) 52 * 53 * Functions used when malloc is not allowed 54 * Place struct at given address 55 * 56 * If successful, Return 0, else Return -1 57 */ 58 59 int16_t WebRtcIsacfix_Assign(ISACFIX_MainStruct **inst, void *ISACFIX_inst_Addr) { 60 if (ISACFIX_inst_Addr!=NULL) { 61 *inst = (ISACFIX_MainStruct*)ISACFIX_inst_Addr; 62 (*(ISACFIX_SubStruct**)inst)->errorcode = 0; 63 (*(ISACFIX_SubStruct**)inst)->initflag = 0; 64 (*(ISACFIX_SubStruct**)inst)->ISACenc_obj.SaveEnc_ptr = NULL; 65 return(0); 66 } else { 67 return(-1); 68 } 69 } 70 71 72 #ifndef ISACFIX_NO_DYNAMIC_MEM 73 74 /**************************************************************************** 75 * WebRtcIsacfix_Create(...) 76 * 77 * This function creates a ISAC instance, which will contain the state 78 * information for one coding/decoding channel. 79 * 80 * Input: 81 * - *ISAC_main_inst : a pointer to the coder instance. 82 * 83 * Return value : 0 - Ok 84 * -1 - Error 85 */ 86 87 int16_t WebRtcIsacfix_Create(ISACFIX_MainStruct **ISAC_main_inst) 88 { 89 ISACFIX_SubStruct *tempo; 90 tempo = malloc(1 * sizeof(ISACFIX_SubStruct)); 91 *ISAC_main_inst = (ISACFIX_MainStruct *)tempo; 92 if (*ISAC_main_inst!=NULL) { 93 (*(ISACFIX_SubStruct**)ISAC_main_inst)->errorcode = 0; 94 (*(ISACFIX_SubStruct**)ISAC_main_inst)->initflag = 0; 95 (*(ISACFIX_SubStruct**)ISAC_main_inst)->ISACenc_obj.SaveEnc_ptr = NULL; 96 WebRtcSpl_Init(); 97 return(0); 98 } else { 99 return(-1); 100 } 101 } 102 103 104 /**************************************************************************** 105 * WebRtcIsacfix_CreateInternal(...) 106 * 107 * This function creates the memory that is used to store data in the encoder 108 * 109 * Input: 110 * - *ISAC_main_inst : a pointer to the coder instance. 111 * 112 * Return value : 0 - Ok 113 * -1 - Error 114 */ 115 116 int16_t WebRtcIsacfix_CreateInternal(ISACFIX_MainStruct *ISAC_main_inst) 117 { 118 ISACFIX_SubStruct *ISAC_inst; 119 120 /* typecast pointer to real structure */ 121 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 122 123 /* Allocate memory for storing encoder data */ 124 ISAC_inst->ISACenc_obj.SaveEnc_ptr = malloc(1 * sizeof(ISAC_SaveEncData_t)); 125 126 if (ISAC_inst->ISACenc_obj.SaveEnc_ptr!=NULL) { 127 return(0); 128 } else { 129 return(-1); 130 } 131 } 132 133 134 #endif 135 136 137 138 /**************************************************************************** 139 * WebRtcIsacfix_Free(...) 140 * 141 * This function frees the ISAC instance created at the beginning. 142 * 143 * Input: 144 * - ISAC_main_inst : a ISAC instance. 145 * 146 * Return value : 0 - Ok 147 * -1 - Error 148 */ 149 150 int16_t WebRtcIsacfix_Free(ISACFIX_MainStruct *ISAC_main_inst) 151 { 152 free(ISAC_main_inst); 153 return(0); 154 } 155 156 /**************************************************************************** 157 * WebRtcIsacfix_FreeInternal(...) 158 * 159 * This function frees the internal memory for storing encoder data. 160 * 161 * Input: 162 * - ISAC_main_inst : a ISAC instance. 163 * 164 * Return value : 0 - Ok 165 * -1 - Error 166 */ 167 168 int16_t WebRtcIsacfix_FreeInternal(ISACFIX_MainStruct *ISAC_main_inst) 169 { 170 ISACFIX_SubStruct *ISAC_inst; 171 172 /* typecast pointer to real structure */ 173 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 174 175 /* Release memory */ 176 free(ISAC_inst->ISACenc_obj.SaveEnc_ptr); 177 178 return(0); 179 } 180 181 /**************************************************************************** 182 * WebRtcIsacfix_InitNeon(...) 183 * 184 * This function initializes function pointers for ARM Neon platform. 185 */ 186 187 #if (defined WEBRTC_DETECT_ARM_NEON || defined WEBRTC_ARCH_ARM_NEON) 188 static void WebRtcIsacfix_InitNeon(void) { 189 WebRtcIsacfix_AutocorrFix = WebRtcIsacfix_AutocorrNeon; 190 WebRtcIsacfix_FilterMaLoopFix = WebRtcIsacfix_FilterMaLoopNeon; 191 WebRtcIsacfix_Spec2Time = WebRtcIsacfix_Spec2TimeNeon; 192 WebRtcIsacfix_Time2Spec = WebRtcIsacfix_Time2SpecNeon; 193 WebRtcIsacfix_CalculateResidualEnergy = 194 WebRtcIsacfix_CalculateResidualEnergyNeon; 195 WebRtcIsacfix_AllpassFilter2FixDec16 = 196 WebRtcIsacfix_AllpassFilter2FixDec16Neon; 197 WebRtcIsacfix_MatrixProduct1 = WebRtcIsacfix_MatrixProduct1Neon; 198 WebRtcIsacfix_MatrixProduct2 = WebRtcIsacfix_MatrixProduct2Neon; 199 } 200 #endif 201 202 /**************************************************************************** 203 * WebRtcIsacfix_InitMIPS(...) 204 * 205 * This function initializes function pointers for MIPS platform. 206 */ 207 208 #if defined(MIPS32_LE) 209 static void WebRtcIsacfix_InitMIPS(void) { 210 WebRtcIsacfix_AutocorrFix = WebRtcIsacfix_AutocorrMIPS; 211 WebRtcIsacfix_FilterMaLoopFix = WebRtcIsacfix_FilterMaLoopMIPS; 212 #if defined(MIPS_DSP_R1_LE) 213 WebRtcIsacfix_AllpassFilter2FixDec16 = 214 WebRtcIsacfix_AllpassFilter2FixDec16MIPS; 215 #endif 216 } 217 #endif 218 219 /**************************************************************************** 220 * WebRtcIsacfix_EncoderInit(...) 221 * 222 * This function initializes a ISAC instance prior to the encoder calls. 223 * 224 * Input: 225 * - ISAC_main_inst : ISAC instance. 226 * - CodingMode : 0 -> Bit rate and frame length are automatically 227 * adjusted to available bandwidth on 228 * transmission channel. 229 * 1 -> User sets a frame length and a target bit 230 * rate which is taken as the maximum short-term 231 * average bit rate. 232 * 233 * Return value : 0 - Ok 234 * -1 - Error 235 */ 236 237 int16_t WebRtcIsacfix_EncoderInit(ISACFIX_MainStruct *ISAC_main_inst, 238 int16_t CodingMode) 239 { 240 int k; 241 int16_t statusInit; 242 ISACFIX_SubStruct *ISAC_inst; 243 244 statusInit = 0; 245 /* typecast pointer to rela structure */ 246 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 247 248 /* flag encoder init */ 249 ISAC_inst->initflag |= 2; 250 251 if (CodingMode == 0) 252 /* Adaptive mode */ 253 ISAC_inst->ISACenc_obj.new_framelength = INITIAL_FRAMESAMPLES; 254 else if (CodingMode == 1) 255 /* Instantaneous mode */ 256 ISAC_inst->ISACenc_obj.new_framelength = 480; /* default for I-mode */ 257 else { 258 ISAC_inst->errorcode = ISAC_DISALLOWED_CODING_MODE; 259 statusInit = -1; 260 } 261 262 ISAC_inst->CodingMode = CodingMode; 263 264 WebRtcIsacfix_InitMaskingEnc(&ISAC_inst->ISACenc_obj.maskfiltstr_obj); 265 WebRtcIsacfix_InitPreFilterbank(&ISAC_inst->ISACenc_obj.prefiltbankstr_obj); 266 WebRtcIsacfix_InitPitchFilter(&ISAC_inst->ISACenc_obj.pitchfiltstr_obj); 267 WebRtcIsacfix_InitPitchAnalysis(&ISAC_inst->ISACenc_obj.pitchanalysisstr_obj); 268 269 270 WebRtcIsacfix_InitBandwidthEstimator(&ISAC_inst->bwestimator_obj); 271 WebRtcIsacfix_InitRateModel(&ISAC_inst->ISACenc_obj.rate_data_obj); 272 273 274 ISAC_inst->ISACenc_obj.buffer_index = 0; 275 ISAC_inst->ISACenc_obj.frame_nb = 0; 276 ISAC_inst->ISACenc_obj.BottleNeck = 32000; /* default for I-mode */ 277 ISAC_inst->ISACenc_obj.MaxDelay = 10; /* default for I-mode */ 278 ISAC_inst->ISACenc_obj.current_framesamples = 0; 279 ISAC_inst->ISACenc_obj.s2nr = 0; 280 ISAC_inst->ISACenc_obj.MaxBits = 0; 281 ISAC_inst->ISACenc_obj.bitstr_seed = 4447; 282 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = STREAM_MAXW16_30MS << 1; 283 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = STREAM_MAXW16_60MS << 1; 284 ISAC_inst->ISACenc_obj.maxPayloadBytes = STREAM_MAXW16_60MS << 1; 285 ISAC_inst->ISACenc_obj.maxRateInBytes = STREAM_MAXW16_30MS << 1; 286 ISAC_inst->ISACenc_obj.enforceFrameSize = 0; 287 288 /* Init the bistream data area to zero */ 289 for (k=0; k<STREAM_MAXW16_60MS; k++){ 290 ISAC_inst->ISACenc_obj.bitstr_obj.stream[k] = 0; 291 } 292 293 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED 294 WebRtcIsacfix_InitPostFilterbank(&ISAC_inst->ISACenc_obj.interpolatorstr_obj); 295 #endif 296 297 // Initiaze function pointers. 298 WebRtcIsacfix_AutocorrFix = WebRtcIsacfix_AutocorrC; 299 WebRtcIsacfix_FilterMaLoopFix = WebRtcIsacfix_FilterMaLoopC; 300 WebRtcIsacfix_CalculateResidualEnergy = 301 WebRtcIsacfix_CalculateResidualEnergyC; 302 WebRtcIsacfix_AllpassFilter2FixDec16 = WebRtcIsacfix_AllpassFilter2FixDec16C; 303 WebRtcIsacfix_Time2Spec = WebRtcIsacfix_Time2SpecC; 304 WebRtcIsacfix_Spec2Time = WebRtcIsacfix_Spec2TimeC; 305 WebRtcIsacfix_MatrixProduct1 = WebRtcIsacfix_MatrixProduct1C; 306 WebRtcIsacfix_MatrixProduct2 = WebRtcIsacfix_MatrixProduct2C ; 307 308 #ifdef WEBRTC_DETECT_ARM_NEON 309 if ((WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON) != 0) { 310 WebRtcIsacfix_InitNeon(); 311 } 312 #elif defined(WEBRTC_ARCH_ARM_NEON) 313 WebRtcIsacfix_InitNeon(); 314 #endif 315 316 #if defined(MIPS32_LE) 317 WebRtcIsacfix_InitMIPS(); 318 #endif 319 320 return statusInit; 321 } 322 323 /**************************************************************************** 324 * WebRtcIsacfix_Encode(...) 325 * 326 * This function encodes 10ms frame(s) and inserts it into a package. 327 * Input speech length has to be 160 samples (10ms). The encoder buffers those 328 * 10ms frames until it reaches the chosen Framesize (480 or 960 samples 329 * corresponding to 30 or 60 ms frames), and then proceeds to the encoding. 330 * 331 * Input: 332 * - ISAC_main_inst : ISAC instance. 333 * - speechIn : input speech vector. 334 * 335 * Output: 336 * - encoded : the encoded data vector 337 * 338 * Return value: 339 * : >0 - Length (in bytes) of coded data 340 * : 0 - The buffer didn't reach the chosen framesize 341 * so it keeps buffering speech samples. 342 * : -1 - Error 343 */ 344 345 int16_t WebRtcIsacfix_Encode(ISACFIX_MainStruct *ISAC_main_inst, 346 const int16_t *speechIn, 347 int16_t *encoded) 348 { 349 ISACFIX_SubStruct *ISAC_inst; 350 int16_t stream_len; 351 #ifndef WEBRTC_ARCH_BIG_ENDIAN 352 int k; 353 #endif 354 355 /* typecast pointer to rela structure */ 356 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 357 358 359 /* check if encoder initiated */ 360 if ((ISAC_inst->initflag & 2) != 2) { 361 ISAC_inst->errorcode = ISAC_ENCODER_NOT_INITIATED; 362 return (-1); 363 } 364 365 stream_len = WebRtcIsacfix_EncodeImpl((int16_t*)speechIn, 366 &ISAC_inst->ISACenc_obj, 367 &ISAC_inst->bwestimator_obj, 368 ISAC_inst->CodingMode); 369 if (stream_len<0) { 370 ISAC_inst->errorcode = - stream_len; 371 return -1; 372 } 373 374 375 /* convert from bytes to int16_t */ 376 #ifndef WEBRTC_ARCH_BIG_ENDIAN 377 for (k=0;k<(stream_len+1)>>1;k++) { 378 encoded[k] = (int16_t)( ( (uint16_t)(ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] >> 8 ) 379 | (((ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] & 0x00FF) << 8)); 380 } 381 382 #else 383 WEBRTC_SPL_MEMCPY_W16(encoded, (ISAC_inst->ISACenc_obj.bitstr_obj).stream, (stream_len + 1)>>1); 384 #endif 385 386 387 388 return stream_len; 389 390 } 391 392 393 394 395 /**************************************************************************** 396 * WebRtcIsacfix_EncodeNb(...) 397 * 398 * This function encodes 10ms narrow band (8 kHz sampling) frame(s) and inserts 399 * it into a package. Input speech length has to be 80 samples (10ms). The encoder 400 * interpolates into wide-band (16 kHz sampling) buffers those 401 * 10ms frames until it reaches the chosen Framesize (480 or 960 wide-band samples 402 * corresponding to 30 or 60 ms frames), and then proceeds to the encoding. 403 * 404 * The function is enabled if WEBRTC_ISAC_FIX_NB_CALLS_ENABLED is defined 405 * 406 * Input: 407 * - ISAC_main_inst : ISAC instance. 408 * - speechIn : input speech vector. 409 * 410 * Output: 411 * - encoded : the encoded data vector 412 * 413 * Return value: 414 * : >0 - Length (in bytes) of coded data 415 * : 0 - The buffer didn't reach the chosen framesize 416 * so it keeps buffering speech samples. 417 * : -1 - Error 418 */ 419 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED 420 int16_t WebRtcIsacfix_EncodeNb(ISACFIX_MainStruct *ISAC_main_inst, 421 const int16_t *speechIn, 422 int16_t *encoded) 423 { 424 ISACFIX_SubStruct *ISAC_inst; 425 int16_t stream_len; 426 int16_t speechInWB[FRAMESAMPLES_10ms]; 427 int16_t Vector_Word16_1[FRAMESAMPLES_10ms/2]; 428 int16_t Vector_Word16_2[FRAMESAMPLES_10ms/2]; 429 430 int k; 431 432 433 /* typecast pointer to rela structure */ 434 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 435 436 437 /* check if encoder initiated */ 438 if ((ISAC_inst->initflag & 2) != 2) { 439 ISAC_inst->errorcode = ISAC_ENCODER_NOT_INITIATED; 440 return (-1); 441 } 442 443 444 /* Oversample to WB */ 445 446 /* Form polyphase signals, and compensate for DC offset */ 447 for (k=0;k<FRAMESAMPLES_10ms/2;k++) { 448 Vector_Word16_1[k] = speechIn[k] + 1; 449 Vector_Word16_2[k] = speechIn[k]; 450 } 451 WebRtcIsacfix_FilterAndCombine2(Vector_Word16_1, Vector_Word16_2, speechInWB, &ISAC_inst->ISACenc_obj.interpolatorstr_obj, FRAMESAMPLES_10ms); 452 453 454 /* Encode WB signal */ 455 stream_len = WebRtcIsacfix_EncodeImpl((int16_t*)speechInWB, 456 &ISAC_inst->ISACenc_obj, 457 &ISAC_inst->bwestimator_obj, 458 ISAC_inst->CodingMode); 459 if (stream_len<0) { 460 ISAC_inst->errorcode = - stream_len; 461 return -1; 462 } 463 464 465 /* convert from bytes to int16_t */ 466 #ifndef WEBRTC_ARCH_BIG_ENDIAN 467 for (k=0;k<(stream_len+1)>>1;k++) { 468 encoded[k] = (int16_t)(((uint16_t)(ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] >> 8) 469 | (((ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] & 0x00FF) << 8)); 470 } 471 472 #else 473 WEBRTC_SPL_MEMCPY_W16(encoded, (ISAC_inst->ISACenc_obj.bitstr_obj).stream, (stream_len + 1)>>1); 474 #endif 475 476 477 478 return stream_len; 479 } 480 #endif /* WEBRTC_ISAC_FIX_NB_CALLS_ENABLED */ 481 482 483 /**************************************************************************** 484 * WebRtcIsacfix_GetNewBitStream(...) 485 * 486 * This function returns encoded data, with the recieved bwe-index in the 487 * stream. It should always return a complete packet, i.e. only called once 488 * even for 60 msec frames 489 * 490 * Input: 491 * - ISAC_main_inst : ISAC instance. 492 * - bweIndex : index of bandwidth estimate to put in new bitstream 493 * 494 * Output: 495 * - encoded : the encoded data vector 496 * 497 * Return value: 498 * : >0 - Length (in bytes) of coded data 499 * : -1 - Error 500 */ 501 502 int16_t WebRtcIsacfix_GetNewBitStream(ISACFIX_MainStruct *ISAC_main_inst, 503 int16_t bweIndex, 504 float scale, 505 int16_t *encoded) 506 { 507 ISACFIX_SubStruct *ISAC_inst; 508 int16_t stream_len; 509 #ifndef WEBRTC_ARCH_BIG_ENDIAN 510 int k; 511 #endif 512 513 /* typecast pointer to rela structure */ 514 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 515 516 517 /* check if encoder initiated */ 518 if ((ISAC_inst->initflag & 2) != 2) { 519 ISAC_inst->errorcode = ISAC_ENCODER_NOT_INITIATED; 520 return (-1); 521 } 522 523 stream_len = WebRtcIsacfix_EncodeStoredData(&ISAC_inst->ISACenc_obj, 524 bweIndex, 525 scale); 526 if (stream_len<0) { 527 ISAC_inst->errorcode = - stream_len; 528 return -1; 529 } 530 531 #ifndef WEBRTC_ARCH_BIG_ENDIAN 532 for (k=0;k<(stream_len+1)>>1;k++) { 533 encoded[k] = (int16_t)( ( (uint16_t)(ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] >> 8 ) 534 | (((ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] & 0x00FF) << 8)); 535 } 536 537 #else 538 WEBRTC_SPL_MEMCPY_W16(encoded, (ISAC_inst->ISACenc_obj.bitstr_obj).stream, (stream_len + 1)>>1); 539 #endif 540 541 return stream_len; 542 543 } 544 545 546 547 /**************************************************************************** 548 * WebRtcIsacfix_DecoderInit(...) 549 * 550 * This function initializes a ISAC instance prior to the decoder calls. 551 * 552 * Input: 553 * - ISAC_main_inst : ISAC instance. 554 * 555 * Return value 556 * : 0 - Ok 557 * -1 - Error 558 */ 559 560 int16_t WebRtcIsacfix_DecoderInit(ISACFIX_MainStruct *ISAC_main_inst) 561 { 562 ISACFIX_SubStruct *ISAC_inst; 563 564 /* typecast pointer to real structure */ 565 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 566 567 /* flag decoder init */ 568 ISAC_inst->initflag |= 1; 569 570 WebRtcIsacfix_InitMaskingDec(&ISAC_inst->ISACdec_obj.maskfiltstr_obj); 571 WebRtcIsacfix_InitPostFilterbank(&ISAC_inst->ISACdec_obj.postfiltbankstr_obj); 572 WebRtcIsacfix_InitPitchFilter(&ISAC_inst->ISACdec_obj.pitchfiltstr_obj); 573 574 /* TS */ 575 WebRtcIsacfix_InitPlc( &ISAC_inst->ISACdec_obj.plcstr_obj ); 576 577 578 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED 579 WebRtcIsacfix_InitPreFilterbank(&ISAC_inst->ISACdec_obj.decimatorstr_obj); 580 #endif 581 582 return 0; 583 } 584 585 586 /**************************************************************************** 587 * WebRtcIsacfix_UpdateBwEstimate1(...) 588 * 589 * This function updates the estimate of the bandwidth. 590 * 591 * Input: 592 * - ISAC_main_inst : ISAC instance. 593 * - encoded : encoded ISAC frame(s). 594 * - packet_size : size of the packet. 595 * - rtp_seq_number : the RTP number of the packet. 596 * - arr_ts : the arrival time of the packet (from NetEq) 597 * in samples. 598 * 599 * Return value : 0 - Ok 600 * -1 - Error 601 */ 602 603 int16_t WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_MainStruct *ISAC_main_inst, 604 const uint16_t *encoded, 605 int32_t packet_size, 606 uint16_t rtp_seq_number, 607 uint32_t arr_ts) 608 { 609 ISACFIX_SubStruct *ISAC_inst; 610 Bitstr_dec streamdata; 611 #ifndef WEBRTC_ARCH_BIG_ENDIAN 612 int k; 613 #endif 614 int16_t err; 615 616 /* typecast pointer to real structure */ 617 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 618 619 /* Sanity check of packet length */ 620 if (packet_size <= 0) { 621 /* return error code if the packet length is null or less */ 622 ISAC_inst->errorcode = ISAC_EMPTY_PACKET; 623 return -1; 624 } else if (packet_size > (STREAM_MAXW16<<1)) { 625 /* return error code if length of stream is too long */ 626 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH; 627 return -1; 628 } 629 630 /* check if decoder initiated */ 631 if ((ISAC_inst->initflag & 1) != 1) { 632 ISAC_inst->errorcode = ISAC_DECODER_NOT_INITIATED; 633 return (-1); 634 } 635 636 streamdata.W_upper = 0xFFFFFFFF; 637 streamdata.streamval = 0; 638 streamdata.stream_index = 0; 639 streamdata.full = 1; 640 641 #ifndef WEBRTC_ARCH_BIG_ENDIAN 642 for (k=0; k<5; k++) { 643 streamdata.stream[k] = (uint16_t) (((uint16_t)encoded[k] >> 8)|((encoded[k] & 0xFF)<<8)); 644 } 645 #else 646 memcpy(streamdata.stream, encoded, 5); 647 #endif 648 649 err = WebRtcIsacfix_EstimateBandwidth(&ISAC_inst->bwestimator_obj, 650 &streamdata, 651 packet_size, 652 rtp_seq_number, 653 0, 654 arr_ts); 655 656 657 if (err < 0) 658 { 659 /* return error code if something went wrong */ 660 ISAC_inst->errorcode = -err; 661 return -1; 662 } 663 664 665 return 0; 666 } 667 668 /**************************************************************************** 669 * WebRtcIsacfix_UpdateBwEstimate(...) 670 * 671 * This function updates the estimate of the bandwidth. 672 * 673 * Input: 674 * - ISAC_main_inst : ISAC instance. 675 * - encoded : encoded ISAC frame(s). 676 * - packet_size : size of the packet. 677 * - rtp_seq_number : the RTP number of the packet. 678 * - send_ts : Send Time Stamp from RTP header 679 * - arr_ts : the arrival time of the packet (from NetEq) 680 * in samples. 681 * 682 * Return value : 0 - Ok 683 * -1 - Error 684 */ 685 686 int16_t WebRtcIsacfix_UpdateBwEstimate(ISACFIX_MainStruct *ISAC_main_inst, 687 const uint16_t *encoded, 688 int32_t packet_size, 689 uint16_t rtp_seq_number, 690 uint32_t send_ts, 691 uint32_t arr_ts) 692 { 693 ISACFIX_SubStruct *ISAC_inst; 694 Bitstr_dec streamdata; 695 #ifndef WEBRTC_ARCH_BIG_ENDIAN 696 int k; 697 #endif 698 int16_t err; 699 700 /* typecast pointer to real structure */ 701 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 702 703 /* Sanity check of packet length */ 704 if (packet_size <= 0) { 705 /* return error code if the packet length is null or less */ 706 ISAC_inst->errorcode = ISAC_EMPTY_PACKET; 707 return -1; 708 } else if (packet_size > (STREAM_MAXW16<<1)) { 709 /* return error code if length of stream is too long */ 710 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH; 711 return -1; 712 } 713 714 /* check if decoder initiated */ 715 if ((ISAC_inst->initflag & 1) != 1) { 716 ISAC_inst->errorcode = ISAC_DECODER_NOT_INITIATED; 717 return (-1); 718 } 719 720 streamdata.W_upper = 0xFFFFFFFF; 721 streamdata.streamval = 0; 722 streamdata.stream_index = 0; 723 streamdata.full = 1; 724 725 #ifndef WEBRTC_ARCH_BIG_ENDIAN 726 for (k=0; k<5; k++) { 727 streamdata.stream[k] = (uint16_t) ((encoded[k] >> 8)|((encoded[k] & 0xFF)<<8)); 728 } 729 #else 730 memcpy(streamdata.stream, encoded, 5); 731 #endif 732 733 err = WebRtcIsacfix_EstimateBandwidth(&ISAC_inst->bwestimator_obj, 734 &streamdata, 735 packet_size, 736 rtp_seq_number, 737 send_ts, 738 arr_ts); 739 740 if (err < 0) 741 { 742 /* return error code if something went wrong */ 743 ISAC_inst->errorcode = -err; 744 return -1; 745 } 746 747 748 return 0; 749 } 750 751 /**************************************************************************** 752 * WebRtcIsacfix_Decode(...) 753 * 754 * This function decodes a ISAC frame. Output speech length 755 * will be a multiple of 480 samples: 480 or 960 samples, 756 * depending on the framesize (30 or 60 ms). 757 * 758 * Input: 759 * - ISAC_main_inst : ISAC instance. 760 * - encoded : encoded ISAC frame(s) 761 * - len : bytes in encoded vector 762 * 763 * Output: 764 * - decoded : The decoded vector 765 * 766 * Return value : >0 - number of samples in decoded vector 767 * -1 - Error 768 */ 769 770 771 int16_t WebRtcIsacfix_Decode(ISACFIX_MainStruct *ISAC_main_inst, 772 const uint16_t *encoded, 773 int16_t len, 774 int16_t *decoded, 775 int16_t *speechType) 776 { 777 ISACFIX_SubStruct *ISAC_inst; 778 /* number of samples (480 or 960), output from decoder */ 779 /* that were actually used in the encoder/decoder (determined on the fly) */ 780 int16_t number_of_samples; 781 #ifndef WEBRTC_ARCH_BIG_ENDIAN 782 int k; 783 #endif 784 int16_t declen = 0; 785 786 /* typecast pointer to real structure */ 787 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 788 789 /* check if decoder initiated */ 790 if ((ISAC_inst->initflag & 1) != 1) { 791 ISAC_inst->errorcode = ISAC_DECODER_NOT_INITIATED; 792 return (-1); 793 } 794 795 /* Sanity check of packet length */ 796 if (len <= 0) { 797 /* return error code if the packet length is null or less */ 798 ISAC_inst->errorcode = ISAC_EMPTY_PACKET; 799 return -1; 800 } else if (len > (STREAM_MAXW16<<1)) { 801 /* return error code if length of stream is too long */ 802 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH; 803 return -1; 804 } 805 806 ISAC_inst->ISACdec_obj.bitstr_obj.stream_size = (len + 1) >> 1; 807 808 /* convert bitstream from int16_t to bytes */ 809 #ifndef WEBRTC_ARCH_BIG_ENDIAN 810 for (k=0; k<(len>>1); k++) { 811 (ISAC_inst->ISACdec_obj.bitstr_obj).stream[k] = (uint16_t) ((encoded[k] >> 8)|((encoded[k] & 0xFF)<<8)); 812 } 813 if (len & 0x0001) 814 (ISAC_inst->ISACdec_obj.bitstr_obj).stream[k] = (uint16_t) ((encoded[k] & 0xFF)<<8); 815 #endif 816 817 /* added for NetEq purposes (VAD/DTX related) */ 818 *speechType=1; 819 820 declen = WebRtcIsacfix_DecodeImpl(decoded,&ISAC_inst->ISACdec_obj, &number_of_samples); 821 822 if (declen < 0) { 823 /* Some error inside the decoder */ 824 ISAC_inst->errorcode = -declen; 825 memset(decoded, 0, sizeof(int16_t) * MAX_FRAMESAMPLES); 826 return -1; 827 } 828 829 /* error check */ 830 831 if (declen & 0x0001) { 832 if (len != declen && len != declen + (((ISAC_inst->ISACdec_obj.bitstr_obj).stream[declen>>1]) & 0x00FF) ) { 833 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH; 834 memset(decoded, 0, sizeof(int16_t) * number_of_samples); 835 return -1; 836 } 837 } else { 838 if (len != declen && len != declen + (((ISAC_inst->ISACdec_obj.bitstr_obj).stream[declen>>1]) >> 8) ) { 839 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH; 840 memset(decoded, 0, sizeof(int16_t) * number_of_samples); 841 return -1; 842 } 843 } 844 845 return number_of_samples; 846 } 847 848 849 850 851 852 /**************************************************************************** 853 * WebRtcIsacfix_DecodeNb(...) 854 * 855 * This function decodes a ISAC frame in narrow-band (8 kHz sampling). 856 * Output speech length will be a multiple of 240 samples: 240 or 480 samples, 857 * depending on the framesize (30 or 60 ms). 858 * 859 * The function is enabled if WEBRTC_ISAC_FIX_NB_CALLS_ENABLED is defined 860 * 861 * Input: 862 * - ISAC_main_inst : ISAC instance. 863 * - encoded : encoded ISAC frame(s) 864 * - len : bytes in encoded vector 865 * 866 * Output: 867 * - decoded : The decoded vector 868 * 869 * Return value : >0 - number of samples in decoded vector 870 * -1 - Error 871 */ 872 873 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED 874 int16_t WebRtcIsacfix_DecodeNb(ISACFIX_MainStruct *ISAC_main_inst, 875 const uint16_t *encoded, 876 int16_t len, 877 int16_t *decoded, 878 int16_t *speechType) 879 { 880 ISACFIX_SubStruct *ISAC_inst; 881 /* twice the number of samples (480 or 960), output from decoder */ 882 /* that were actually used in the encoder/decoder (determined on the fly) */ 883 int16_t number_of_samples; 884 #ifndef WEBRTC_ARCH_BIG_ENDIAN 885 int k; 886 #endif 887 int16_t declen = 0; 888 int16_t dummy[FRAMESAMPLES/2]; 889 890 891 /* typecast pointer to real structure */ 892 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 893 894 /* check if decoder initiated */ 895 if ((ISAC_inst->initflag & 1) != 1) { 896 ISAC_inst->errorcode = ISAC_DECODER_NOT_INITIATED; 897 return (-1); 898 } 899 900 if (len == 0) 901 { /* return error code if the packet length is null */ 902 903 ISAC_inst->errorcode = ISAC_EMPTY_PACKET; 904 return -1; 905 } 906 907 ISAC_inst->ISACdec_obj.bitstr_obj.stream_size = (len + 1) >> 1; 908 909 /* convert bitstream from int16_t to bytes */ 910 #ifndef WEBRTC_ARCH_BIG_ENDIAN 911 for (k=0; k<(len>>1); k++) { 912 (ISAC_inst->ISACdec_obj.bitstr_obj).stream[k] = (uint16_t) ((encoded[k] >> 8)|((encoded[k] & 0xFF)<<8)); 913 } 914 if (len & 0x0001) 915 (ISAC_inst->ISACdec_obj.bitstr_obj).stream[k] = (uint16_t) ((encoded[k] & 0xFF)<<8); 916 #endif 917 918 /* added for NetEq purposes (VAD/DTX related) */ 919 *speechType=1; 920 921 declen = WebRtcIsacfix_DecodeImpl(decoded,&ISAC_inst->ISACdec_obj, &number_of_samples); 922 923 if (declen < 0) { 924 /* Some error inside the decoder */ 925 ISAC_inst->errorcode = -declen; 926 memset(decoded, 0, sizeof(int16_t) * FRAMESAMPLES); 927 return -1; 928 } 929 930 /* error check */ 931 932 if (declen & 0x0001) { 933 if (len != declen && len != declen + (((ISAC_inst->ISACdec_obj.bitstr_obj).stream[declen>>1]) & 0x00FF) ) { 934 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH; 935 memset(decoded, 0, sizeof(int16_t) * number_of_samples); 936 return -1; 937 } 938 } else { 939 if (len != declen && len != declen + (((ISAC_inst->ISACdec_obj.bitstr_obj).stream[declen>>1]) >> 8) ) { 940 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH; 941 memset(decoded, 0, sizeof(int16_t) * number_of_samples); 942 return -1; 943 } 944 } 945 946 WebRtcIsacfix_SplitAndFilter2(decoded, decoded, dummy, &ISAC_inst->ISACdec_obj.decimatorstr_obj); 947 948 if (number_of_samples>FRAMESAMPLES) { 949 WebRtcIsacfix_SplitAndFilter2(decoded + FRAMESAMPLES, decoded + FRAMESAMPLES/2, 950 dummy, &ISAC_inst->ISACdec_obj.decimatorstr_obj); 951 } 952 953 return number_of_samples/2; 954 } 955 #endif /* WEBRTC_ISAC_FIX_NB_CALLS_ENABLED */ 956 957 958 /**************************************************************************** 959 * WebRtcIsacfix_DecodePlcNb(...) 960 * 961 * This function conducts PLC for ISAC frame(s) in narrow-band (8kHz sampling). 962 * Output speech length will be "240*noOfLostFrames" samples 963 * that is equevalent of "30*noOfLostFrames" millisecond. 964 * 965 * The function is enabled if WEBRTC_ISAC_FIX_NB_CALLS_ENABLED is defined 966 * 967 * Input: 968 * - ISAC_main_inst : ISAC instance. 969 * - noOfLostFrames : Number of PLC frames (240 sample=30ms) to produce 970 * 971 * Output: 972 * - decoded : The decoded vector 973 * 974 * Return value : >0 - number of samples in decoded PLC vector 975 * -1 - Error 976 */ 977 978 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED 979 int16_t WebRtcIsacfix_DecodePlcNb(ISACFIX_MainStruct *ISAC_main_inst, 980 int16_t *decoded, 981 int16_t noOfLostFrames ) 982 { 983 int16_t no_of_samples, declen, k, ok; 984 int16_t outframeNB[FRAMESAMPLES]; 985 int16_t outframeWB[FRAMESAMPLES]; 986 int16_t dummy[FRAMESAMPLES/2]; 987 988 989 ISACFIX_SubStruct *ISAC_inst; 990 /* typecast pointer to real structure */ 991 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 992 993 /* Limit number of frames to two = 60 msec. Otherwise we exceed data vectors */ 994 if (noOfLostFrames > 2){ 995 noOfLostFrames = 2; 996 } 997 998 k = 0; 999 declen = 0; 1000 while( noOfLostFrames > 0 ) 1001 { 1002 ok = WebRtcIsacfix_DecodePlcImpl( outframeWB, &ISAC_inst->ISACdec_obj, &no_of_samples ); 1003 if(ok) 1004 return -1; 1005 1006 WebRtcIsacfix_SplitAndFilter2(outframeWB, &(outframeNB[k*240]), dummy, &ISAC_inst->ISACdec_obj.decimatorstr_obj); 1007 1008 declen += no_of_samples; 1009 noOfLostFrames--; 1010 k++; 1011 } 1012 1013 declen>>=1; 1014 1015 for (k=0;k<declen;k++) { 1016 decoded[k] = outframeNB[k]; 1017 } 1018 1019 return declen; 1020 } 1021 #endif /* WEBRTC_ISAC_FIX_NB_CALLS_ENABLED */ 1022 1023 1024 1025 1026 /**************************************************************************** 1027 * WebRtcIsacfix_DecodePlc(...) 1028 * 1029 * This function conducts PLC for ISAC frame(s) in wide-band (16kHz sampling). 1030 * Output speech length will be "480*noOfLostFrames" samples 1031 * that is equevalent of "30*noOfLostFrames" millisecond. 1032 * 1033 * Input: 1034 * - ISAC_main_inst : ISAC instance. 1035 * - noOfLostFrames : Number of PLC frames (480sample = 30ms) 1036 * to produce 1037 * 1038 * Output: 1039 * - decoded : The decoded vector 1040 * 1041 * Return value : >0 - number of samples in decoded PLC vector 1042 * -1 - Error 1043 */ 1044 1045 int16_t WebRtcIsacfix_DecodePlc(ISACFIX_MainStruct *ISAC_main_inst, 1046 int16_t *decoded, 1047 int16_t noOfLostFrames) 1048 { 1049 1050 int16_t no_of_samples, declen, k, ok; 1051 int16_t outframe16[MAX_FRAMESAMPLES]; 1052 1053 ISACFIX_SubStruct *ISAC_inst; 1054 /* typecast pointer to real structure */ 1055 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1056 1057 /* Limit number of frames to two = 60 msec. Otherwise we exceed data vectors */ 1058 if (noOfLostFrames > 2) { 1059 noOfLostFrames = 2; 1060 } 1061 k = 0; 1062 declen = 0; 1063 while( noOfLostFrames > 0 ) 1064 { 1065 ok = WebRtcIsacfix_DecodePlcImpl( &(outframe16[k*480]), &ISAC_inst->ISACdec_obj, &no_of_samples ); 1066 if(ok) 1067 return -1; 1068 declen += no_of_samples; 1069 noOfLostFrames--; 1070 k++; 1071 } 1072 1073 for (k=0;k<declen;k++) { 1074 decoded[k] = outframe16[k]; 1075 } 1076 1077 return declen; 1078 } 1079 1080 1081 /**************************************************************************** 1082 * WebRtcIsacfix_Control(...) 1083 * 1084 * This function sets the limit on the short-term average bit rate and the 1085 * frame length. Should be used only in Instantaneous mode. 1086 * 1087 * Input: 1088 * - ISAC_main_inst : ISAC instance. 1089 * - rate : limit on the short-term average bit rate, 1090 * in bits/second (between 10000 and 32000) 1091 * - framesize : number of milliseconds per frame (30 or 60) 1092 * 1093 * Return value : 0 - ok 1094 * -1 - Error 1095 */ 1096 1097 int16_t WebRtcIsacfix_Control(ISACFIX_MainStruct *ISAC_main_inst, 1098 int16_t rate, 1099 int16_t framesize) 1100 { 1101 ISACFIX_SubStruct *ISAC_inst; 1102 /* typecast pointer to real structure */ 1103 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1104 1105 if (ISAC_inst->CodingMode == 0) 1106 { 1107 /* in adaptive mode */ 1108 ISAC_inst->errorcode = ISAC_MODE_MISMATCH; 1109 return -1; 1110 } 1111 1112 1113 if (rate >= 10000 && rate <= 32000) 1114 ISAC_inst->ISACenc_obj.BottleNeck = rate; 1115 else { 1116 ISAC_inst->errorcode = ISAC_DISALLOWED_BOTTLENECK; 1117 return -1; 1118 } 1119 1120 1121 1122 if (framesize == 30 || framesize == 60) 1123 ISAC_inst->ISACenc_obj.new_framelength = (FS/1000) * framesize; 1124 else { 1125 ISAC_inst->errorcode = ISAC_DISALLOWED_FRAME_LENGTH; 1126 return -1; 1127 } 1128 1129 return 0; 1130 } 1131 1132 1133 /**************************************************************************** 1134 * WebRtcIsacfix_ControlBwe(...) 1135 * 1136 * This function sets the initial values of bottleneck and frame-size if 1137 * iSAC is used in channel-adaptive mode. Through this API, users can 1138 * enforce a frame-size for all values of bottleneck. Then iSAC will not 1139 * automatically change the frame-size. 1140 * 1141 * 1142 * Input: 1143 * - ISAC_main_inst : ISAC instance. 1144 * - rateBPS : initial value of bottleneck in bits/second 1145 * 10000 <= rateBPS <= 32000 is accepted 1146 * For default bottleneck set rateBPS = 0 1147 * - frameSizeMs : number of milliseconds per frame (30 or 60) 1148 * - enforceFrameSize : 1 to enforce the given frame-size through out 1149 * the adaptation process, 0 to let iSAC change 1150 * the frame-size if required. 1151 * 1152 * Return value : 0 - ok 1153 * -1 - Error 1154 */ 1155 1156 int16_t WebRtcIsacfix_ControlBwe(ISACFIX_MainStruct *ISAC_main_inst, 1157 int16_t rateBPS, 1158 int16_t frameSizeMs, 1159 int16_t enforceFrameSize) 1160 { 1161 ISACFIX_SubStruct *ISAC_inst; 1162 /* Typecast pointer to real structure */ 1163 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1164 1165 /* check if encoder initiated */ 1166 if ((ISAC_inst->initflag & 2) != 2) { 1167 ISAC_inst->errorcode = ISAC_ENCODER_NOT_INITIATED; 1168 return (-1); 1169 } 1170 1171 /* Check that we are in channel-adaptive mode, otherwise, return -1 */ 1172 if (ISAC_inst->CodingMode != 0) { 1173 ISAC_inst->errorcode = ISAC_MODE_MISMATCH; 1174 return (-1); 1175 } 1176 1177 /* Set struct variable if enforceFrameSize is set. ISAC will then keep the */ 1178 /* chosen frame size. */ 1179 ISAC_inst->ISACenc_obj.enforceFrameSize = (enforceFrameSize != 0)? 1:0; 1180 1181 /* Set initial rate, if value between 10000 and 32000, */ 1182 /* if rateBPS is 0, keep the default initial bottleneck value (15000) */ 1183 if ((rateBPS >= 10000) && (rateBPS <= 32000)) { 1184 ISAC_inst->bwestimator_obj.sendBwAvg = (((uint32_t)rateBPS) << 7); 1185 } else if (rateBPS != 0) { 1186 ISAC_inst->errorcode = ISAC_DISALLOWED_BOTTLENECK; 1187 return -1; 1188 } 1189 1190 /* Set initial framesize. If enforceFrameSize is set the frame size will not change */ 1191 if ((frameSizeMs == 30) || (frameSizeMs == 60)) { 1192 ISAC_inst->ISACenc_obj.new_framelength = (FS/1000) * frameSizeMs; 1193 } else { 1194 ISAC_inst->errorcode = ISAC_DISALLOWED_FRAME_LENGTH; 1195 return -1; 1196 } 1197 1198 return 0; 1199 } 1200 1201 1202 1203 1204 1205 /**************************************************************************** 1206 * WebRtcIsacfix_GetDownLinkBwIndex(...) 1207 * 1208 * This function returns index representing the Bandwidth estimate from 1209 * other side to this side. 1210 * 1211 * Input: 1212 * - ISAC_main_inst: iSAC struct 1213 * 1214 * Output: 1215 * - rateIndex : Bandwidth estimate to transmit to other side. 1216 * 1217 */ 1218 1219 int16_t WebRtcIsacfix_GetDownLinkBwIndex(ISACFIX_MainStruct* ISAC_main_inst, 1220 int16_t* rateIndex) 1221 { 1222 ISACFIX_SubStruct *ISAC_inst; 1223 1224 /* typecast pointer to real structure */ 1225 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1226 1227 /* Call function to get Bandwidth Estimate */ 1228 *rateIndex = WebRtcIsacfix_GetDownlinkBwIndexImpl(&ISAC_inst->bwestimator_obj); 1229 1230 return 0; 1231 } 1232 1233 1234 /**************************************************************************** 1235 * WebRtcIsacfix_UpdateUplinkBw(...) 1236 * 1237 * This function takes an index representing the Bandwidth estimate from 1238 * this side to other side and updates BWE. 1239 * 1240 * Input: 1241 * - ISAC_main_inst: iSAC struct 1242 * - rateIndex : Bandwidth estimate from other side. 1243 * 1244 */ 1245 1246 int16_t WebRtcIsacfix_UpdateUplinkBw(ISACFIX_MainStruct* ISAC_main_inst, 1247 int16_t rateIndex) 1248 { 1249 int16_t err = 0; 1250 ISACFIX_SubStruct *ISAC_inst; 1251 1252 /* typecast pointer to real structure */ 1253 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1254 1255 /* Call function to update BWE with received Bandwidth Estimate */ 1256 err = WebRtcIsacfix_UpdateUplinkBwRec(&ISAC_inst->bwestimator_obj, rateIndex); 1257 if (err < 0) { 1258 ISAC_inst->errorcode = -err; 1259 return (-1); 1260 } 1261 1262 return 0; 1263 } 1264 1265 /**************************************************************************** 1266 * WebRtcIsacfix_ReadFrameLen(...) 1267 * 1268 * This function returns the length of the frame represented in the packet. 1269 * 1270 * Input: 1271 * - encoded : Encoded bitstream 1272 * 1273 * Output: 1274 * - frameLength : Length of frame in packet (in samples) 1275 * 1276 */ 1277 1278 int16_t WebRtcIsacfix_ReadFrameLen(const int16_t* encoded, 1279 int16_t* frameLength) 1280 { 1281 Bitstr_dec streamdata; 1282 #ifndef WEBRTC_ARCH_BIG_ENDIAN 1283 int k; 1284 #endif 1285 int16_t err; 1286 1287 streamdata.W_upper = 0xFFFFFFFF; 1288 streamdata.streamval = 0; 1289 streamdata.stream_index = 0; 1290 streamdata.full = 1; 1291 1292 #ifndef WEBRTC_ARCH_BIG_ENDIAN 1293 for (k=0; k<5; k++) { 1294 streamdata.stream[k] = (uint16_t) (((uint16_t)encoded[k] >> 8)|((encoded[k] & 0xFF)<<8)); 1295 } 1296 #else 1297 memcpy(streamdata.stream, encoded, 5); 1298 #endif 1299 1300 /* decode frame length */ 1301 err = WebRtcIsacfix_DecodeFrameLen(&streamdata, frameLength); 1302 if (err<0) // error check 1303 return err; 1304 1305 return 0; 1306 } 1307 1308 1309 /**************************************************************************** 1310 * WebRtcIsacfix_ReadBwIndex(...) 1311 * 1312 * This function returns the index of the Bandwidth estimate from the bitstream. 1313 * 1314 * Input: 1315 * - encoded : Encoded bitstream 1316 * 1317 * Output: 1318 * - frameLength : Length of frame in packet (in samples) 1319 * - rateIndex : Bandwidth estimate in bitstream 1320 * 1321 */ 1322 1323 int16_t WebRtcIsacfix_ReadBwIndex(const int16_t* encoded, 1324 int16_t* rateIndex) 1325 { 1326 Bitstr_dec streamdata; 1327 #ifndef WEBRTC_ARCH_BIG_ENDIAN 1328 int k; 1329 #endif 1330 int16_t err; 1331 1332 streamdata.W_upper = 0xFFFFFFFF; 1333 streamdata.streamval = 0; 1334 streamdata.stream_index = 0; 1335 streamdata.full = 1; 1336 1337 #ifndef WEBRTC_ARCH_BIG_ENDIAN 1338 for (k=0; k<5; k++) { 1339 streamdata.stream[k] = (uint16_t) (((uint16_t)encoded[k] >> 8)|((encoded[k] & 0xFF)<<8)); 1340 } 1341 #else 1342 memcpy(streamdata.stream, encoded, 5); 1343 #endif 1344 1345 /* decode frame length, needed to get to the rateIndex in the bitstream */ 1346 err = WebRtcIsacfix_DecodeFrameLen(&streamdata, rateIndex); 1347 if (err<0) // error check 1348 return err; 1349 1350 /* decode BW estimation */ 1351 err = WebRtcIsacfix_DecodeSendBandwidth(&streamdata, rateIndex); 1352 if (err<0) // error check 1353 return err; 1354 1355 return 0; 1356 } 1357 1358 1359 1360 1361 /**************************************************************************** 1362 * WebRtcIsacfix_GetErrorCode(...) 1363 * 1364 * This function can be used to check the error code of an iSAC instance. When 1365 * a function returns -1 a error code will be set for that instance. The 1366 * function below extract the code of the last error that occured in the 1367 * specified instance. 1368 * 1369 * Input: 1370 * - ISAC_main_inst : ISAC instance 1371 * 1372 * Return value : Error code 1373 */ 1374 1375 int16_t WebRtcIsacfix_GetErrorCode(ISACFIX_MainStruct *ISAC_main_inst) 1376 { 1377 ISACFIX_SubStruct *ISAC_inst; 1378 /* typecast pointer to real structure */ 1379 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1380 1381 return ISAC_inst->errorcode; 1382 } 1383 1384 1385 1386 /**************************************************************************** 1387 * WebRtcIsacfix_GetUplinkBw(...) 1388 * 1389 * This function returns the inst quantized iSAC send bitrate 1390 * 1391 * Input: 1392 * - ISAC_main_inst : iSAC instance 1393 * 1394 * Return value : bitrate 1395 */ 1396 1397 int32_t WebRtcIsacfix_GetUplinkBw(ISACFIX_MainStruct *ISAC_main_inst) 1398 { 1399 ISACFIX_SubStruct *ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1400 BwEstimatorstr * bw = (BwEstimatorstr*)&(ISAC_inst->bwestimator_obj); 1401 1402 return (int32_t) WebRtcIsacfix_GetUplinkBandwidth(bw); 1403 } 1404 1405 /**************************************************************************** 1406 * WebRtcIsacfix_GetNewFrameLen(...) 1407 * 1408 * This function return the next frame length (in samples) of iSAC. 1409 * 1410 * Input: 1411 * - ISAC_main_inst : iSAC instance 1412 * 1413 * Return value : frame lenght in samples 1414 */ 1415 1416 int16_t WebRtcIsacfix_GetNewFrameLen(ISACFIX_MainStruct *ISAC_main_inst) 1417 { 1418 ISACFIX_SubStruct *ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1419 return ISAC_inst->ISACenc_obj.new_framelength; 1420 } 1421 1422 1423 /**************************************************************************** 1424 * WebRtcIsacfix_SetMaxPayloadSize(...) 1425 * 1426 * This function sets a limit for the maximum payload size of iSAC. The same 1427 * value is used both for 30 and 60 msec packets. 1428 * The absolute max will be valid until next time the function is called. 1429 * NOTE! This function may override the function WebRtcIsacfix_SetMaxRate() 1430 * 1431 * Input: 1432 * - ISAC_main_inst : iSAC instance 1433 * - maxPayloadBytes : maximum size of the payload in bytes 1434 * valid values are between 100 and 400 bytes 1435 * 1436 * 1437 * Return value : 0 if sucessful 1438 * -1 if error happens 1439 */ 1440 1441 int16_t WebRtcIsacfix_SetMaxPayloadSize(ISACFIX_MainStruct *ISAC_main_inst, 1442 int16_t maxPayloadBytes) 1443 { 1444 ISACFIX_SubStruct *ISAC_inst; 1445 1446 /* typecast pointer to real structure */ 1447 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1448 1449 if((maxPayloadBytes < 100) || (maxPayloadBytes > 400)) 1450 { 1451 /* maxPayloadBytes is out of valid range */ 1452 return -1; 1453 } 1454 else 1455 { 1456 /* Set new absolute max, which will not change unless this function 1457 is called again with a new value */ 1458 ISAC_inst->ISACenc_obj.maxPayloadBytes = maxPayloadBytes; 1459 1460 /* Set new maximum values for 30 and 60 msec packets */ 1461 if (maxPayloadBytes < ISAC_inst->ISACenc_obj.maxRateInBytes) { 1462 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = maxPayloadBytes; 1463 } else { 1464 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = ISAC_inst->ISACenc_obj.maxRateInBytes; 1465 } 1466 1467 if ( maxPayloadBytes < (ISAC_inst->ISACenc_obj.maxRateInBytes << 1)) { 1468 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = maxPayloadBytes; 1469 } else { 1470 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = (ISAC_inst->ISACenc_obj.maxRateInBytes << 1); 1471 } 1472 } 1473 return 0; 1474 } 1475 1476 1477 /**************************************************************************** 1478 * WebRtcIsacfix_SetMaxRate(...) 1479 * 1480 * This function sets the maximum rate which the codec may not exceed for a 1481 * singel packet. The maximum rate is set in bits per second. 1482 * The codec has an absolute maximum rate of 53400 bits per second (200 bytes 1483 * per 30 msec). 1484 * It is possible to set a maximum rate between 32000 and 53400 bits per second. 1485 * 1486 * The rate limit is valid until next time the function is called. 1487 * 1488 * NOTE! Packet size will never go above the value set if calling 1489 * WebRtcIsacfix_SetMaxPayloadSize() (default max packet size is 400 bytes). 1490 * 1491 * Input: 1492 * - ISAC_main_inst : iSAC instance 1493 * - maxRateInBytes : maximum rate in bits per second, 1494 * valid values are 32000 to 53400 bits 1495 * 1496 * Return value : 0 if sucessful 1497 * -1 if error happens 1498 */ 1499 1500 int16_t WebRtcIsacfix_SetMaxRate(ISACFIX_MainStruct *ISAC_main_inst, 1501 int32_t maxRate) 1502 { 1503 ISACFIX_SubStruct *ISAC_inst; 1504 int16_t maxRateInBytes; 1505 1506 /* typecast pointer to real structure */ 1507 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst; 1508 1509 if((maxRate < 32000) || (maxRate > 53400)) 1510 { 1511 /* maxRate is out of valid range */ 1512 return -1; 1513 } 1514 else 1515 { 1516 /* Calculate maximum number of bytes per 30 msec packets for the given 1517 maximum rate. Multiply with 30/1000 to get number of bits per 30 msec, 1518 divide by 8 to get number of bytes per 30 msec: 1519 maxRateInBytes = floor((maxRate * 30/1000) / 8); */ 1520 maxRateInBytes = (int16_t)( WebRtcSpl_DivW32W16ResW16(WEBRTC_SPL_MUL(maxRate, 3), 800) ); 1521 1522 /* Store the value for usage in the WebRtcIsacfix_SetMaxPayloadSize-function */ 1523 ISAC_inst->ISACenc_obj.maxRateInBytes = maxRateInBytes; 1524 1525 /* For 30 msec packets: if the new limit is below the maximum 1526 payload size, set a new limit */ 1527 if (maxRateInBytes < ISAC_inst->ISACenc_obj.maxPayloadBytes) { 1528 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = maxRateInBytes; 1529 } else { 1530 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = ISAC_inst->ISACenc_obj.maxPayloadBytes; 1531 } 1532 1533 /* For 60 msec packets: if the new limit (times 2) is below the 1534 maximum payload size, set a new limit */ 1535 if ( (maxRateInBytes << 1) < ISAC_inst->ISACenc_obj.maxPayloadBytes) { 1536 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = (maxRateInBytes << 1); 1537 } else { 1538 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = ISAC_inst->ISACenc_obj.maxPayloadBytes; 1539 } 1540 } 1541 1542 return 0; 1543 } 1544 1545 1546 1547 /**************************************************************************** 1548 * WebRtcIsacfix_version(...) 1549 * 1550 * This function returns the version number. 1551 * 1552 * Output: 1553 * - version : Pointer to character string 1554 * 1555 */ 1556 1557 void WebRtcIsacfix_version(char *version) 1558 { 1559 strcpy(version, "3.6.0"); 1560 } 1561