1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 /**************************************************************************************** 19 Portions of this file are derived from the following 3GPP standard: 20 21 3GPP TS 26.073 22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec 23 Available from http://www.3gpp.org 24 25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) 26 Permission to distribute, modify and use this file under the standard license 27 terms listed above has been obtained from the copyright holder. 28 ****************************************************************************************/ 29 /* 30 ------------------------------------------------------------------------------ 31 32 33 34 Pathname: ./audio/gsm-amr/c/src/cod_amr.c 35 Funtions: cod_amr_init 36 cod_amr_reset 37 cod_amr_exit 38 cod_amr_first 39 cod_amr 40 41 Date: 06/09/2000 42 43 ------------------------------------------------------------------------------ 44 REVISION HISTORY 45 46 Description: Made changes based on comments from the review meeting. 47 48 Description: Synchronized file with UMTS version 3.2.0. Updated coding 49 template. Removed unnecessary include files. 50 51 Description: Added initialization of the overflow flag in cod_amr_init() 52 and in cod_amr_reset(). This overflow flag is now part of 53 the cod_amrState structure. 54 55 Description: Cleaned up INCLUDES. removed inclusion of basic_op.h and repeat 56 inclusion of copy.h 57 58 Description: Updated function call to dtx_enc 59 60 Description: For cod_amr_first() and cod_amr() 61 1. Replaced copy() function with memcpy() 62 63 Description: Replaced OSCL mem type functions and eliminated include 64 files that now are chosen by OSCL definitions 65 66 Description: Replaced "int" and/or "char" with OSCL defined types. 67 68 Description: 69 70 ------------------------------------------------------------------------------ 71 MODULE DESCRIPTION 72 73 These functions comprise the main encoder routine operating on a frame basis. 74 75 ------------------------------------------------------------------------------ 76 */ 77 78 79 /*---------------------------------------------------------------------------- 80 ; INCLUDES 81 ----------------------------------------------------------------------------*/ 82 #include <stdlib.h> 83 #include <string.h> 84 85 #include "cod_amr.h" 86 #include "typedef.h" 87 #include "cnst.h" 88 #include "copy.h" 89 #include "qua_gain.h" 90 #include "lpc.h" 91 #include "lsp.h" 92 #include "pre_big.h" 93 #include "ol_ltp.h" 94 #include "p_ol_wgh.h" 95 #include "spreproc.h" 96 #include "cl_ltp.h" 97 #include "pred_lt.h" 98 #include "spstproc.h" 99 #include "cbsearch.h" 100 #include "gain_q.h" 101 #include "convolve.h" 102 #include "ton_stab.h" 103 #include "vad.h" 104 #include "dtx_enc.h" 105 106 /*---------------------------------------------------------------------------- 107 ; MACROS 108 ; Define module specific macros here 109 ----------------------------------------------------------------------------*/ 110 111 112 /*---------------------------------------------------------------------------- 113 ; DEFINES 114 ; Include all pre-processor statements here. Include conditional 115 ; compile variables also. 116 ----------------------------------------------------------------------------*/ 117 118 /*---------------------------------------------------------------------------- 119 ; LOCAL FUNCTION DEFINITIONS 120 ; Function Prototype declaration 121 ----------------------------------------------------------------------------*/ 122 123 /*---------------------------------------------------------------------------- 124 ; LOCAL VARIABLE DEFINITIONS 125 ; Variable declaration - defined here and used outside this module 126 ----------------------------------------------------------------------------*/ 127 128 /* Spectral expansion factors */ 129 130 static const Word16 gamma1[M] = 131 { 132 30802, 28954, 27217, 25584, 24049, 133 22606, 21250, 19975, 18777, 17650 134 }; 135 136 /* gamma1 differs for the 12k2 coder */ 137 static const Word16 gamma1_12k2[M] = 138 { 139 29491, 26542, 23888, 21499, 19349, 140 17414, 15672, 14105, 12694, 11425 141 }; 142 143 static const Word16 gamma2[M] = 144 { 145 19661, 11797, 7078, 4247, 2548, 146 1529, 917, 550, 330, 198 147 }; 148 149 150 /* 151 ------------------------------------------------------------------------------ 152 FUNCTION NAME: cod_amr_init 153 ------------------------------------------------------------------------------ 154 INPUT AND OUTPUT DEFINITIONS 155 156 Inputs: 157 state = pointer to a pointer to a structure of type cod_amrState 158 159 Outputs: 160 Structure pointed to by the pointer pointed to by state is 161 initialized to its reset value 162 state points to the allocated memory 163 164 Returns: 165 Returns 0 if memory was successfully initialized, 166 otherwise returns -1. 167 168 Global Variables Used: 169 None. 170 171 Local Variables Needed: 172 None. 173 174 ------------------------------------------------------------------------------ 175 FUNCTION DESCRIPTION 176 177 This function allocates memory and initializes state variables. 178 179 ------------------------------------------------------------------------------ 180 REQUIREMENTS 181 182 None. 183 184 ------------------------------------------------------------------------------ 185 REFERENCES 186 187 cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 188 189 ------------------------------------------------------------------------------ 190 PSEUDO-CODE 191 192 int cod_amr_init (cod_amrState **state, Flag dtx) 193 { 194 cod_amrState* s; 195 196 if (state == (cod_amrState **) NULL){ 197 fprintf(stderr, "cod_amr_init: invalid parameter\n"); 198 return -1; 199 } 200 *state = NULL; 201 202 // allocate memory 203 if ((s= (cod_amrState *) malloc(sizeof(cod_amrState))) == NULL){ 204 fprintf(stderr, "cod_amr_init: can not malloc state structure\n"); 205 return -1; 206 } 207 208 s->lpcSt = NULL; 209 s->lspSt = NULL; 210 s->clLtpSt = NULL; 211 s->gainQuantSt = NULL; 212 s->pitchOLWghtSt = NULL; 213 s->tonStabSt = NULL; 214 s->vadSt = NULL; 215 s->dtx_encSt = NULL; 216 s->dtx = dtx; 217 218 // Init sub states 219 if (cl_ltp_init(&s->clLtpSt) || 220 lsp_init(&s->lspSt) || 221 gainQuant_init(&s->gainQuantSt) || 222 p_ol_wgh_init(&s->pitchOLWghtSt) || 223 ton_stab_init(&s->tonStabSt) || 224 #ifndef VAD2 225 vad1_init(&s->vadSt) || 226 #else 227 vad2_init(&s->vadSt) || 228 #endif 229 dtx_enc_init(&s->dtx_encSt) || 230 lpc_init(&s->lpcSt)) { 231 cod_amr_exit(&s); 232 return -1; 233 } 234 235 cod_amr_reset(s); 236 237 *state = s; 238 239 return 0; 240 } 241 242 ------------------------------------------------------------------------------ 243 RESOURCES USED [optional] 244 245 When the code is written for a specific target processor the 246 the resources used should be documented below. 247 248 HEAP MEMORY USED: x bytes 249 250 STACK MEMORY USED: x bytes 251 252 CLOCK CYCLES: (cycle count equation for this function) + (variable 253 used to represent cycle count for each subroutine 254 called) 255 where: (cycle count variable) = cycle count for [subroutine 256 name] 257 258 ------------------------------------------------------------------------------ 259 CAUTION [optional] 260 [State any special notes, constraints or cautions for users of this function] 261 262 ------------------------------------------------------------------------------ 263 */ 264 265 Word16 cod_amr_init(cod_amrState **state, Flag dtx) 266 { 267 cod_amrState* s; 268 269 if (state == (cod_amrState **) NULL) 270 { 271 /* fprint(stderr, "cod_amr_init: invalid parameter\n"); */ 272 return(-1); 273 } 274 *state = NULL; 275 276 /* allocate memory */ 277 if ((s = (cod_amrState *) malloc(sizeof(cod_amrState))) == NULL) 278 { 279 /* fprint(stderr, "cod_amr_init: 280 can not malloc state structure\n"); */ 281 return(-1); 282 } 283 284 s->lpcSt = NULL; 285 s->lspSt = NULL; 286 s->clLtpSt = NULL; 287 s->gainQuantSt = NULL; 288 s->pitchOLWghtSt = NULL; 289 s->tonStabSt = NULL; 290 s->vadSt = NULL; 291 s->dtx_encSt = NULL; 292 s->dtx = dtx; 293 294 /* Initialize overflow Flag */ 295 296 s->overflow = 0; 297 298 299 /* Init sub states */ 300 if (cl_ltp_init(&s->clLtpSt) || 301 lsp_init(&s->lspSt) || 302 gainQuant_init(&s->gainQuantSt) || 303 p_ol_wgh_init(&s->pitchOLWghtSt) || 304 ton_stab_init(&s->tonStabSt) || 305 #ifndef VAD2 306 vad1_init(&s->vadSt) || 307 #else 308 vad2_init(&s->vadSt) || 309 #endif 310 dtx_enc_init(&s->dtx_encSt) || 311 lpc_init(&s->lpcSt)) 312 { 313 cod_amr_exit(&s); 314 return(-1); 315 } 316 317 cod_amr_reset(s); 318 319 *state = s; 320 321 return(0); 322 } 323 324 /****************************************************************************/ 325 326 /* 327 ------------------------------------------------------------------------------ 328 FUNCTION NAME: cod_amr_reset 329 ------------------------------------------------------------------------------ 330 INPUT AND OUTPUT DEFINITIONS 331 332 Inputs: 333 state = pointer to a structure of type cod_amrState 334 335 Outputs: 336 Structure pointed to by state is initialized to initial values. 337 338 Returns: 339 Returns 0 if memory was successfully initialized, 340 otherwise returns -1. 341 342 Global Variables Used: 343 None. 344 345 Local Variables Needed: 346 None. 347 348 ------------------------------------------------------------------------------ 349 FUNCTION DESCRIPTION 350 351 This function resets the state memory for cod_amr. 352 353 ------------------------------------------------------------------------------ 354 REQUIREMENTS 355 356 None. 357 358 ------------------------------------------------------------------------------ 359 REFERENCES 360 361 cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 362 363 ------------------------------------------------------------------------------ 364 PSEUDO-CODE 365 366 int cod_amr_reset (cod_amrState *st) 367 { 368 Word16 i; 369 370 if (st == (cod_amrState *) NULL){ 371 fprintf(stderr, "cod_amr_reset: invalid parameter\n"); 372 return -1; 373 } 374 375 *-----------------------------------------------------------------------* 376 * Initialize pointers to speech vector. * 377 *-----------------------------------------------------------------------* 378 379 st->new_speech = st->old_speech + L_TOTAL - L_FRAME; // New speech 380 381 st->speech = st->new_speech - L_NEXT; // Present frame 382 383 st->p_window = st->old_speech + L_TOTAL - L_WINDOW; // For LPC window 384 st->p_window_12k2 = st->p_window - L_NEXT; // EFR LPC window: no lookahead 385 386 // Initialize static pointers 387 388 st->wsp = st->old_wsp + PIT_MAX; 389 st->exc = st->old_exc + PIT_MAX + L_INTERPOL; 390 st->zero = st->ai_zero + MP1; 391 st->error = st->mem_err + M; 392 st->h1 = &st->hvec[L_SUBFR]; 393 394 // Static vectors to zero 395 396 Set_zero(st->old_speech, L_TOTAL); 397 Set_zero(st->old_exc, PIT_MAX + L_INTERPOL); 398 Set_zero(st->old_wsp, PIT_MAX); 399 Set_zero(st->mem_syn, M); 400 Set_zero(st->mem_w, M); 401 Set_zero(st->mem_w0, M); 402 Set_zero(st->mem_err, M); 403 Set_zero(st->zero, L_SUBFR); 404 Set_zero(st->hvec, L_SUBFR); // set to zero "h1[-L_SUBFR..-1]" 405 406 // OL LTP states 407 for (i = 0; i < 5; i++) 408 { 409 st->old_lags[i] = 40; 410 } 411 412 // Reset lpc states 413 lpc_reset(st->lpcSt); 414 415 // Reset lsp states 416 lsp_reset(st->lspSt); 417 418 // Reset clLtp states 419 cl_ltp_reset(st->clLtpSt); 420 421 gainQuant_reset(st->gainQuantSt); 422 423 p_ol_wgh_reset(st->pitchOLWghtSt); 424 425 ton_stab_reset(st->tonStabSt); 426 427 #ifndef VAD2 428 vad1_reset(st->vadSt); 429 #else 430 vad2_reset(st->vadSt); 431 #endif 432 433 dtx_enc_reset(st->dtx_encSt); 434 435 st->sharp = SHARPMIN; 436 437 return 0; 438 } 439 440 ------------------------------------------------------------------------------ 441 RESOURCES USED [optional] 442 443 When the code is written for a specific target processor the 444 the resources used should be documented below. 445 446 HEAP MEMORY USED: x bytes 447 448 STACK MEMORY USED: x bytes 449 450 CLOCK CYCLES: (cycle count equation for this function) + (variable 451 used to represent cycle count for each subroutine 452 called) 453 where: (cycle count variable) = cycle count for [subroutine 454 name] 455 456 ------------------------------------------------------------------------------ 457 CAUTION [optional] 458 [State any special notes, constraints or cautions for users of this function] 459 460 ------------------------------------------------------------------------------ 461 */ 462 463 Word16 cod_amr_reset(cod_amrState *st) 464 { 465 Word16 i; 466 467 if (st == (cod_amrState *) NULL) 468 { 469 /* fprint(stderr, "cod_amr_reset: invalid parameter\n"); */ 470 return(-1); 471 } 472 473 /*-----------------------------------------------------------------------* 474 * Initialize pointers to speech vector. * 475 *-----------------------------------------------------------------------*/ 476 477 st->new_speech = st->old_speech + L_TOTAL - L_FRAME; /* New speech */ 478 479 st->speech = st->new_speech - L_NEXT; /* Present frame */ 480 481 st->p_window = st->old_speech + L_TOTAL - L_WINDOW; /* For LPC window */ 482 st->p_window_12k2 = st->p_window - L_NEXT; /* EFR LPC window: no lookahead */ 483 484 /* Initialize static pointers */ 485 486 st->wsp = st->old_wsp + PIT_MAX; 487 st->exc = st->old_exc + PIT_MAX + L_INTERPOL; 488 st->zero = st->ai_zero + MP1; 489 st->error = st->mem_err + M; 490 st->h1 = &st->hvec[L_SUBFR]; 491 492 /* Initialize overflow Flag */ 493 494 st->overflow = 0; 495 496 /* Static vectors to zero */ 497 memset(st->old_speech, 0, sizeof(Word16)*L_TOTAL); 498 memset(st->old_exc, 0, sizeof(Word16)*(PIT_MAX + L_INTERPOL)); 499 memset(st->old_wsp, 0, sizeof(Word16)*PIT_MAX); 500 memset(st->mem_syn, 0, sizeof(Word16)*M); 501 memset(st->mem_w, 0, sizeof(Word16)*M); 502 memset(st->mem_w0, 0, sizeof(Word16)*M); 503 memset(st->mem_err, 0, sizeof(Word16)*M); 504 memset(st->zero, 0, sizeof(Word16)*L_SUBFR); 505 memset(st->hvec, 0, sizeof(Word16)*L_SUBFR); /* set to zero "h1[-L_SUBFR..-1]" */ 506 507 /* OL LTP states */ 508 for (i = 0; i < 5; i++) 509 { 510 st->old_lags[i] = 40; 511 } 512 513 /* Reset lpc states */ 514 lpc_reset(st->lpcSt); 515 516 /* Reset lsp states */ 517 lsp_reset(st->lspSt); 518 519 /* Reset clLtp states */ 520 cl_ltp_reset(st->clLtpSt); 521 522 gainQuant_reset(st->gainQuantSt); 523 524 p_ol_wgh_reset(st->pitchOLWghtSt); 525 526 ton_stab_reset(st->tonStabSt); 527 528 #ifndef VAD2 529 vad1_reset(st->vadSt); 530 #else 531 vad2_reset(st->vadSt); 532 #endif 533 534 dtx_enc_reset(st->dtx_encSt); 535 536 st->sharp = SHARPMIN; 537 538 return(0); 539 } 540 541 /****************************************************************************/ 542 543 /* 544 ------------------------------------------------------------------------------ 545 FUNCTION NAME: cod_amr_exit 546 ------------------------------------------------------------------------------ 547 INPUT AND OUTPUT DEFINITIONS 548 549 Inputs: 550 state = pointer to a pointer to a structure of type cod_amrState 551 552 Outputs: 553 state points to a NULL address 554 555 Returns: 556 None. 557 558 Global Variables Used: 559 None. 560 561 Local Variables Needed: 562 None. 563 564 ------------------------------------------------------------------------------ 565 FUNCTION DESCRIPTION 566 567 This function frees the memory used for state memory. 568 569 ------------------------------------------------------------------------------ 570 REQUIREMENTS 571 572 None. 573 574 ------------------------------------------------------------------------------ 575 REFERENCES 576 577 cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 578 579 ------------------------------------------------------------------------------ 580 PSEUDO-CODE 581 582 void cod_amr_exit (cod_amrState **state) 583 { 584 if (state == NULL || *state == NULL) 585 return; 586 587 // dealloc members 588 lpc_exit(&(*state)->lpcSt); 589 lsp_exit(&(*state)->lspSt); 590 gainQuant_exit(&(*state)->gainQuantSt); 591 cl_ltp_exit(&(*state)->clLtpSt); 592 p_ol_wgh_exit(&(*state)->pitchOLWghtSt); 593 ton_stab_exit(&(*state)->tonStabSt); 594 #ifndef VAD2 595 vad1_exit(&(*state)->vadSt); 596 #else 597 vad2_exit(&(*state)->vadSt); 598 #endif 599 dtx_enc_exit(&(*state)->dtx_encSt); 600 601 // deallocate memory 602 free(*state); 603 *state = NULL; 604 605 return; 606 } 607 608 ------------------------------------------------------------------------------ 609 RESOURCES USED [optional] 610 611 When the code is written for a specific target processor the 612 the resources used should be documented below. 613 614 HEAP MEMORY USED: x bytes 615 616 STACK MEMORY USED: x bytes 617 618 CLOCK CYCLES: (cycle count equation for this function) + (variable 619 used to represent cycle count for each subroutine 620 called) 621 where: (cycle count variable) = cycle count for [subroutine 622 name] 623 624 ------------------------------------------------------------------------------ 625 CAUTION [optional] 626 [State any special notes, constraints or cautions for users of this function] 627 628 ------------------------------------------------------------------------------ 629 */ 630 631 void cod_amr_exit(cod_amrState **state) 632 { 633 if (state == NULL || *state == NULL) 634 { 635 return; 636 } 637 638 /* dealloc members */ 639 lpc_exit(&(*state)->lpcSt); 640 lsp_exit(&(*state)->lspSt); 641 gainQuant_exit(&(*state)->gainQuantSt); 642 cl_ltp_exit(&(*state)->clLtpSt); 643 p_ol_wgh_exit(&(*state)->pitchOLWghtSt); 644 ton_stab_exit(&(*state)->tonStabSt); 645 #ifndef VAD2 646 vad1_exit(&(*state)->vadSt); 647 #else 648 vad2_exit(&(*state)->vadSt); 649 #endif 650 dtx_enc_exit(&(*state)->dtx_encSt); 651 652 /* deallocate memory */ 653 free(*state); // BX 654 *state = NULL; 655 656 return; 657 } 658 659 /****************************************************************************/ 660 661 /* 662 ------------------------------------------------------------------------------ 663 FUNCTION NAME: cod_amr_first 664 ------------------------------------------------------------------------------ 665 INPUT AND OUTPUT DEFINITIONS 666 667 Inputs: 668 st = pointer to a structure of type cod_amrState 669 new_speech = pointer to buffer of length L_FRAME that contains 670 the speech input (Word16) 671 672 Outputs: 673 The structure of type cod_amrState pointed to by st is updated. 674 675 Returns: 676 return_value = 0 (int) 677 678 Global Variables Used: 679 None. 680 681 Local Variables Needed: 682 None. 683 684 ------------------------------------------------------------------------------ 685 FUNCTION DESCRIPTION 686 687 This function copes with look-ahead and calls cod_amr. 688 No input argument are passed to this function. However, before 689 calling this function, 40 new speech data should be copied to the 690 vector new_speech[]. This is a global pointer which is declared in 691 this file (it points to the end of speech buffer minus 200). 692 693 ------------------------------------------------------------------------------ 694 REQUIREMENTS 695 696 None. 697 698 ------------------------------------------------------------------------------ 699 REFERENCES 700 701 cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 702 703 ------------------------------------------------------------------------------ 704 PSEUDO-CODE 705 706 int cod_amr_first(cod_amrState *st, // i/o : State struct 707 Word16 new_speech[]) // i : speech input (L_FRAME) 708 { 709 Copy(new_speech,&st->new_speech[-L_NEXT], L_NEXT); 710 // Copy(new_speech,st->new_speech,L_FRAME); 711 712 return 0; 713 } 714 715 ------------------------------------------------------------------------------ 716 RESOURCES USED [optional] 717 718 When the code is written for a specific target processor the 719 the resources used should be documented below. 720 721 HEAP MEMORY USED: x bytes 722 723 STACK MEMORY USED: x bytes 724 725 CLOCK CYCLES: (cycle count equation for this function) + (variable 726 used to represent cycle count for each subroutine 727 called) 728 where: (cycle count variable) = cycle count for [subroutine 729 name] 730 731 ------------------------------------------------------------------------------ 732 CAUTION [optional] 733 [State any special notes, constraints or cautions for users of this function] 734 735 ------------------------------------------------------------------------------ 736 */ 737 738 Word16 cod_amr_first(cod_amrState *st, /* i/o : State struct */ 739 Word16 new_speech[]) /* i : speech input (L_FRAME) */ 740 { 741 742 memcpy(&st->new_speech[-L_NEXT], new_speech, L_NEXT*sizeof(Word16)); 743 744 /* Copy(new_speech,st->new_speech,L_FRAME); */ 745 746 return(0); 747 } 748 749 /****************************************************************************/ 750 751 /* 752 ------------------------------------------------------------------------------ 753 FUNCTION NAME: cod_amr 754 ------------------------------------------------------------------------------ 755 INPUT AND OUTPUT DEFINITIONS 756 757 Inputs: 758 st = pointer to a structure of type cod_amrState 759 mode = AMR mode of type enum Mode 760 new_speech = pointer to buffer of length L_FRAME that contains 761 the speech input of type Word16 762 ana = pointer to the analysis parameters of type Word16 763 usedMode = pointer to the used mode of type enum Mode 764 synth = pointer to a buffer containing the local synthesis speech of 765 type Word16 766 767 Outputs: 768 The structure of type cod_amrState pointed to by st is updated. 769 The analysis parameter buffer pointed to by ana is updated. 770 The value pointed to by usedMode is updated. 771 The local synthesis speech buffer pointed to by synth is updated. 772 773 Returns: 774 return_value = 0 (int) 775 776 Global Variables Used: 777 None. 778 779 Local Variables Needed: 780 None. 781 782 ------------------------------------------------------------------------------ 783 FUNCTION DESCRIPTION 784 785 This function is the main encoder routine. It is called every 20 ms speech 786 frame, operating on the newly read 160 speech samples. It performs the 787 principle encoding functions to produce the set of encoded parameters 788 which include the LSP, adaptive codebook, and fixed codebook 789 quantization indices (addresses and gains). 790 791 Before calling this function, 160 new speech data should be copied to the 792 vector new_speech[]. This is a global pointer which is declared in 793 this file (it points to the end of speech buffer minus 160). 794 795 The outputs of the function are: 796 ana[]: vector of analysis parameters. 797 synth[]: Local synthesis speech (for debugging purposes) 798 799 ------------------------------------------------------------------------------ 800 REQUIREMENTS 801 802 None. 803 804 ------------------------------------------------------------------------------ 805 REFERENCES 806 807 cod_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 808 809 ------------------------------------------------------------------------------ 810 PSEUDO-CODE 811 812 int cod_amr( 813 cod_amrState *st, // i/o : State struct 814 enum Mode mode, // i : AMR mode 815 Word16 new_speech[], // i : speech input (L_FRAME) 816 Word16 ana[], // o : Analysis parameters 817 enum Mode *usedMode, // o : used mode 818 Word16 synth[] // o : Local synthesis 819 ) 820 { 821 // LPC coefficients 822 Word16 A_t[(MP1) * 4]; // A(z) unquantized for the 4 subframes 823 Word16 Aq_t[(MP1) * 4]; // A(z) quantized for the 4 subframes 824 Word16 *A, *Aq; // Pointer on A_t and Aq_t 825 Word16 lsp_new[M]; 826 827 // Other vectors 828 Word16 xn[L_SUBFR]; // Target vector for pitch search 829 Word16 xn2[L_SUBFR]; // Target vector for codebook search 830 Word16 code[L_SUBFR]; // Fixed codebook excitation 831 Word16 y1[L_SUBFR]; // Filtered adaptive excitation 832 Word16 y2[L_SUBFR]; // Filtered fixed codebook excitation 833 Word16 gCoeff[6]; // Correlations between xn, y1, & y2: 834 Word16 res[L_SUBFR]; // Short term (LPC) prediction residual 835 Word16 res2[L_SUBFR]; // Long term (LTP) prediction residual 836 837 // Vector and scalars needed for the MR475 838 Word16 xn_sf0[L_SUBFR]; // Target vector for pitch search 839 Word16 y2_sf0[L_SUBFR]; // Filtered codebook innovation 840 Word16 code_sf0[L_SUBFR]; // Fixed codebook excitation 841 Word16 h1_sf0[L_SUBFR]; // The impulse response of sf0 842 Word16 mem_syn_save[M]; // Filter memory 843 Word16 mem_w0_save[M]; // Filter memory 844 Word16 mem_err_save[M]; // Filter memory 845 Word16 sharp_save; // Sharpening 846 Word16 evenSubfr; // Even subframe indicator 847 Word16 T0_sf0 = 0; // Integer pitch lag of sf0 848 Word16 T0_frac_sf0 = 0; // Fractional pitch lag of sf0 849 Word16 i_subfr_sf0 = 0; // Position in exc[] for sf0 850 Word16 gain_pit_sf0; // Quantized pitch gain for sf0 851 Word16 gain_code_sf0; // Quantized codebook gain for sf0 852 853 // Scalars 854 Word16 i_subfr, subfrNr; 855 Word16 T_op[L_FRAME/L_FRAME_BY2]; 856 Word16 T0, T0_frac; 857 Word16 gain_pit, gain_code; 858 859 // Flags 860 Word16 lsp_flag = 0; // indicates resonance in LPC filter 861 Word16 gp_limit; // pitch gain limit value 862 Word16 vad_flag; // VAD decision flag 863 Word16 compute_sid_flag; // SID analysis flag 864 865 Copy(new_speech, st->new_speech, L_FRAME); 866 867 *usedMode = mode; 868 869 // DTX processing 870 if (st->dtx) 871 { // no test() call since this if is only in simulation env 872 // Find VAD decision 873 874 #ifdef VAD2 875 vad_flag = vad2 (st->new_speech, st->vadSt); 876 vad_flag = vad2 (st->new_speech+80, st->vadSt) || vad_flag; 877 #else 878 vad_flag = vad1(st->vadSt, st->new_speech); 879 #endif 880 881 // NB! usedMode may change here 882 compute_sid_flag = tx_dtx_handler(st->dtx_encSt, 883 vad_flag, 884 usedMode); 885 } 886 else 887 { 888 compute_sid_flag = 0; 889 } 890 891 *------------------------------------------------------------------------* 892 * - Perform LPC analysis: * 893 * * autocorrelation + lag windowing * 894 * * Levinson-durbin algorithm to find a[] * 895 * * convert a[] to lsp[] * 896 * * quantize and code the LSPs * 897 * * find the interpolated LSPs and convert to a[] for all * 898 * subframes (both quantized and unquantized) * 899 *------------------------------------------------------------------------* 900 901 // LP analysis 902 lpc(st->lpcSt, mode, st->p_window, st->p_window_12k2, A_t); 903 904 905 // From A(z) to lsp. LSP quantization and interpolation 906 lsp(st->lspSt, mode, *usedMode, A_t, Aq_t, lsp_new, &ana); 907 908 909 // Buffer lsp's and energy 910 dtx_buffer(st->dtx_encSt, 911 lsp_new, 912 st->new_speech); 913 914 // Check if in DTX mode 915 if (sub(*usedMode, MRDTX) == 0) 916 { 917 dtx_enc(st->dtx_encSt, 918 compute_sid_flag, 919 st->lspSt->qSt, 920 st->gainQuantSt->gc_predSt, 921 &ana); 922 923 Set_zero(st->old_exc, PIT_MAX + L_INTERPOL); 924 Set_zero(st->mem_w0, M); 925 Set_zero(st->mem_err, M); 926 Set_zero(st->zero, L_SUBFR); 927 Set_zero(st->hvec, L_SUBFR); // set to zero "h1[-L_SUBFR..-1]" 928 // Reset lsp states 929 lsp_reset(st->lspSt); 930 Copy(lsp_new, st->lspSt->lsp_old, M); 931 Copy(lsp_new, st->lspSt->lsp_old_q, M); 932 933 // Reset clLtp states 934 cl_ltp_reset(st->clLtpSt); 935 st->sharp = SHARPMIN; 936 } 937 else 938 { 939 // check resonance in the filter 940 lsp_flag = check_lsp(st->tonStabSt, st->lspSt->lsp_old); 941 } 942 943 *----------------------------------------------------------------------* 944 * - Find the weighted input speech w_sp[] for the whole speech frame * 945 * - Find the open-loop pitch delay for first 2 subframes * 946 * - Set the range for searching closed-loop pitch in 1st subframe * 947 * - Find the open-loop pitch delay for last 2 subframes * 948 *----------------------------------------------------------------------* 949 950 #ifdef VAD2 951 if (st->dtx) 952 { // no test() call since this if is only in simulation env 953 st->vadSt->L_Rmax = 0; 954 st->vadSt->L_R0 = 0; 955 } 956 #endif 957 for(subfrNr = 0, i_subfr = 0; 958 subfrNr < L_FRAME/L_FRAME_BY2; 959 subfrNr++, i_subfr += L_FRAME_BY2) 960 { 961 // Pre-processing on 80 samples 962 pre_big(mode, gamma1, gamma1_12k2, gamma2, A_t, i_subfr, st->speech, 963 st->mem_w, st->wsp); 964 965 if ((sub(mode, MR475) != 0) && (sub(mode, MR515) != 0)) 966 { 967 // Find open loop pitch lag for two subframes 968 ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[i_subfr], 969 &T_op[subfrNr], st->old_lags, st->ol_gain_flg, subfrNr, 970 st->dtx); 971 } 972 } 973 974 if ((sub(mode, MR475) == 0) || (sub(mode, MR515) == 0)) 975 { 976 // Find open loop pitch lag for ONE FRAME ONLY 977 // search on 160 samples 978 979 ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[0], &T_op[0], 980 st->old_lags, st->ol_gain_flg, 1, st->dtx); 981 T_op[1] = T_op[0]; 982 } 983 984 #ifdef VAD2 985 if (st->dtx) 986 { // no test() call since this if is only in simulation env 987 LTP_flag_update(st->vadSt, mode); 988 } 989 #endif 990 991 #ifndef VAD2 992 // run VAD pitch detection 993 if (st->dtx) 994 { // no test() call since this if is only in simulation env 995 vad_pitch_detection(st->vadSt, T_op); 996 } 997 #endif 998 999 if (sub(*usedMode, MRDTX) == 0) 1000 { 1001 goto the_end; 1002 } 1003 1004 *------------------------------------------------------------------------* 1005 * Loop for every subframe in the analysis frame * 1006 *------------------------------------------------------------------------* 1007 * To find the pitch and innovation parameters. The subframe size is * 1008 * L_SUBFR and the loop is repeated L_FRAME/L_SUBFR times. * 1009 * - find the weighted LPC coefficients * 1010 * - find the LPC residual signal res[] * 1011 * - compute the target signal for pitch search * 1012 * - compute impulse response of weighted synthesis filter (h1[]) * 1013 * - find the closed-loop pitch parameters * 1014 * - encode the pitch dealy * 1015 * - update the impulse response h1[] by including fixed-gain pitch * 1016 * - find target vector for codebook search * 1017 * - codebook search * 1018 * - encode codebook address * 1019 * - VQ of pitch and codebook gains * 1020 * - find synthesis speech * 1021 * - update states of weighting filter * 1022 *------------------------------------------------------------------------* 1023 1024 A = A_t; // pointer to interpolated LPC parameters 1025 Aq = Aq_t; // pointer to interpolated quantized LPC parameters 1026 1027 evenSubfr = 0; 1028 subfrNr = -1; 1029 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) 1030 { 1031 subfrNr = add(subfrNr, 1); 1032 evenSubfr = sub(1, evenSubfr); 1033 1034 // Save states for the MR475 mode 1035 if ((evenSubfr != 0) && (sub(*usedMode, MR475) == 0)) 1036 { 1037 Copy(st->mem_syn, mem_syn_save, M); 1038 Copy(st->mem_w0, mem_w0_save, M); 1039 Copy(st->mem_err, mem_err_save, M); 1040 sharp_save = st->sharp; 1041 } 1042 1043 *-----------------------------------------------------------------* 1044 * - Preprocessing of subframe * 1045 *-----------------------------------------------------------------* 1046 if (sub(*usedMode, MR475) != 0) 1047 { 1048 subframePreProc(*usedMode, gamma1, gamma1_12k2, 1049 gamma2, A, Aq, &st->speech[i_subfr], 1050 st->mem_err, st->mem_w0, st->zero, 1051 st->ai_zero, &st->exc[i_subfr], 1052 st->h1, xn, res, st->error); 1053 } 1054 else 1055 { // MR475 1056 subframePreProc(*usedMode, gamma1, gamma1_12k2, 1057 gamma2, A, Aq, &st->speech[i_subfr], 1058 st->mem_err, mem_w0_save, st->zero, 1059 st->ai_zero, &st->exc[i_subfr], 1060 st->h1, xn, res, st->error); 1061 1062 // save impulse response (modified in cbsearch) 1063 if (evenSubfr != 0) 1064 { 1065 Copy (st->h1, h1_sf0, L_SUBFR); 1066 } 1067 } 1068 1069 // copy the LP residual (res2 is modified in the CL LTP search) 1070 Copy (res, res2, L_SUBFR); 1071 1072 1073 *-----------------------------------------------------------------* 1074 * - Closed-loop LTP search * 1075 *-----------------------------------------------------------------* 1076 cl_ltp(st->clLtpSt, st->tonStabSt, *usedMode, i_subfr, T_op, st->h1, 1077 &st->exc[i_subfr], res2, xn, lsp_flag, xn2, y1, 1078 &T0, &T0_frac, &gain_pit, gCoeff, &ana, 1079 &gp_limit); 1080 1081 // update LTP lag history 1082 if ((subfrNr == 0) && (st->ol_gain_flg[0] > 0)) 1083 { 1084 st->old_lags[1] = T0; 1085 } 1086 1087 if ((sub(subfrNr, 3) == 0) && (st->ol_gain_flg[1] > 0)) 1088 { 1089 st->old_lags[0] = T0; 1090 } 1091 1092 1093 *-----------------------------------------------------------------* 1094 * - Inovative codebook search (find index and gain) * 1095 *-----------------------------------------------------------------* 1096 cbsearch(xn2, st->h1, T0, st->sharp, gain_pit, res2, 1097 code, y2, &ana, *usedMode, subfrNr); 1098 1099 *------------------------------------------------------* 1100 * - Quantization of gains. * 1101 *------------------------------------------------------* 1102 gainQuant(st->gainQuantSt, *usedMode, res, &st->exc[i_subfr], code, 1103 xn, xn2, y1, y2, gCoeff, evenSubfr, gp_limit, 1104 &gain_pit_sf0, &gain_code_sf0, 1105 &gain_pit, &gain_code, &ana); 1106 1107 // update gain history 1108 update_gp_clipping(st->tonStabSt, gain_pit); 1109 1110 if (sub(*usedMode, MR475) != 0) 1111 { 1112 // Subframe Post Porcessing 1113 subframePostProc(st->speech, *usedMode, i_subfr, gain_pit, 1114 gain_code, Aq, synth, xn, code, y1, y2, st->mem_syn, 1115 st->mem_err, st->mem_w0, st->exc, &st->sharp); 1116 } 1117 else 1118 { 1119 if (evenSubfr != 0) 1120 { 1121 i_subfr_sf0 = i_subfr; 1122 Copy(xn, xn_sf0, L_SUBFR); 1123 Copy(y2, y2_sf0, L_SUBFR); 1124 Copy(code, code_sf0, L_SUBFR); 1125 T0_sf0 = T0; 1126 T0_frac_sf0 = T0_frac; 1127 1128 // Subframe Post Porcessing 1129 subframePostProc(st->speech, *usedMode, i_subfr, gain_pit, 1130 gain_code, Aq, synth, xn, code, y1, y2, 1131 mem_syn_save, st->mem_err, mem_w0_save, 1132 st->exc, &st->sharp); 1133 st->sharp = sharp_save; 1134 } 1135 else 1136 { 1137 // update both subframes for the MR475 1138 1139 // Restore states for the MR475 mode 1140 Copy(mem_err_save, st->mem_err, M); 1141 1142 // re-build excitation for sf 0 1143 Pred_lt_3or6(&st->exc[i_subfr_sf0], T0_sf0, T0_frac_sf0, 1144 L_SUBFR, 1); 1145 Convolve(&st->exc[i_subfr_sf0], h1_sf0, y1, L_SUBFR); 1146 1147 Aq -= MP1; 1148 subframePostProc(st->speech, *usedMode, i_subfr_sf0, 1149 gain_pit_sf0, gain_code_sf0, Aq, 1150 synth, xn_sf0, code_sf0, y1, y2_sf0, 1151 st->mem_syn, st->mem_err, st->mem_w0, st->exc, 1152 &sharp_save); // overwrites sharp_save 1153 Aq += MP1; 1154 1155 // re-run pre-processing to get xn right (needed by postproc) 1156 // (this also reconstructs the unsharpened h1 for sf 1) 1157 subframePreProc(*usedMode, gamma1, gamma1_12k2, 1158 gamma2, A, Aq, &st->speech[i_subfr], 1159 st->mem_err, st->mem_w0, st->zero, 1160 st->ai_zero, &st->exc[i_subfr], 1161 st->h1, xn, res, st->error); 1162 1163 // re-build excitation sf 1 (changed if lag < L_SUBFR) 1164 Pred_lt_3or6(&st->exc[i_subfr], T0, T0_frac, L_SUBFR, 1); 1165 Convolve(&st->exc[i_subfr], st->h1, y1, L_SUBFR); 1166 1167 subframePostProc(st->speech, *usedMode, i_subfr, gain_pit, 1168 gain_code, Aq, synth, xn, code, y1, y2, 1169 st->mem_syn, st->mem_err, st->mem_w0, 1170 st->exc, &st->sharp); 1171 } 1172 } 1173 1174 1175 A += MP1; // interpolated LPC parameters for next subframe 1176 Aq += MP1; 1177 } 1178 1179 Copy(&st->old_exc[L_FRAME], &st->old_exc[0], PIT_MAX + L_INTERPOL); 1180 1181 the_end: 1182 1183 *--------------------------------------------------* 1184 * Update signal for next frame. * 1185 *--------------------------------------------------* 1186 Copy(&st->old_wsp[L_FRAME], &st->old_wsp[0], PIT_MAX); 1187 1188 Copy(&st->old_speech[L_FRAME], &st->old_speech[0], L_TOTAL - L_FRAME); 1189 1190 return 0; 1191 } 1192 ------------------------------------------------------------------------------ 1193 RESOURCES USED [optional] 1194 1195 When the code is written for a specific target processor the 1196 the resources used should be documented below. 1197 1198 HEAP MEMORY USED: x bytes 1199 1200 STACK MEMORY USED: x bytes 1201 1202 CLOCK CYCLES: (cycle count equation for this function) + (variable 1203 used to represent cycle count for each subroutine 1204 called) 1205 where: (cycle count variable) = cycle count for [subroutine 1206 name] 1207 1208 ------------------------------------------------------------------------------ 1209 CAUTION [optional] 1210 [State any special notes, constraints or cautions for users of this function] 1211 1212 ------------------------------------------------------------------------------ 1213 */ 1214 1215 Word16 cod_amr( 1216 cod_amrState *st, /* i/o : State struct */ 1217 enum Mode mode, /* i : AMR mode */ 1218 Word16 new_speech[], /* i : speech input (L_FRAME) */ 1219 Word16 ana[], /* o : Analysis parameters */ 1220 enum Mode *usedMode, /* o : used mode */ 1221 Word16 synth[] /* o : Local synthesis */ 1222 ) 1223 { 1224 /* LPC coefficients */ 1225 Word16 A_t[(MP1) * 4]; /* A(z) unquantized for the 4 subframes */ 1226 Word16 Aq_t[(MP1) * 4]; /* A(z) quantized for the 4 subframes */ 1227 Word16 *A, *Aq; /* Pointer on A_t and Aq_t */ 1228 Word16 lsp_new[M]; 1229 1230 /* Other vectors */ 1231 Word16 xn[L_SUBFR]; /* Target vector for pitch search */ 1232 Word16 xn2[L_SUBFR]; /* Target vector for codebook search */ 1233 Word16 code[L_SUBFR]; /* Fixed codebook excitation */ 1234 Word16 y1[L_SUBFR]; /* Filtered adaptive excitation */ 1235 Word16 y2[L_SUBFR]; /* Filtered fixed codebook excitation */ 1236 Word16 gCoeff[6]; /* Correlations between xn, y1, & y2: */ 1237 Word16 res[L_SUBFR]; /* Short term (LPC) prediction residual */ 1238 Word16 res2[L_SUBFR]; /* Long term (LTP) prediction residual */ 1239 1240 /* Vector and scalars needed for the MR475 */ 1241 Word16 xn_sf0[L_SUBFR]; /* Target vector for pitch search */ 1242 Word16 y2_sf0[L_SUBFR]; /* Filtered codebook innovation */ 1243 Word16 code_sf0[L_SUBFR]; /* Fixed codebook excitation */ 1244 Word16 h1_sf0[L_SUBFR]; /* The impulse response of sf0 */ 1245 Word16 mem_syn_save[M]; /* Filter memory */ 1246 Word16 mem_w0_save[M]; /* Filter memory */ 1247 Word16 mem_err_save[M]; /* Filter memory */ 1248 Word16 sharp_save; /* Sharpening */ 1249 Word16 evenSubfr; /* Even subframe indicator */ 1250 Word16 T0_sf0 = 0; /* Integer pitch lag of sf0 */ 1251 Word16 T0_frac_sf0 = 0; /* Fractional pitch lag of sf0 */ 1252 Word16 i_subfr_sf0 = 0; /* Position in exc[] for sf0 */ 1253 Word16 gain_pit_sf0; /* Quantized pitch gain for sf0 */ 1254 Word16 gain_code_sf0; /* Quantized codebook gain for sf0 */ 1255 1256 /* Scalars */ 1257 Word16 i_subfr, subfrNr; 1258 Word16 T_op[L_FRAME/L_FRAME_BY2]; 1259 Word16 T0, T0_frac; 1260 Word16 gain_pit, gain_code; 1261 1262 /* Flags */ 1263 Word16 lsp_flag = 0; /* indicates resonance in LPC filter */ 1264 Word16 gp_limit; /* pitch gain limit value */ 1265 Word16 vad_flag; /* VAD decision flag */ 1266 Word16 compute_sid_flag; /* SID analysis flag */ 1267 Flag *pOverflow = &(st->overflow); /* Overflow flag */ 1268 1269 1270 memcpy(st->new_speech, new_speech, L_FRAME*sizeof(Word16)); 1271 1272 *usedMode = mode; 1273 1274 /* DTX processing */ 1275 if (st->dtx) 1276 { 1277 /* Find VAD decision */ 1278 #ifdef VAD2 1279 vad_flag = vad2(st->new_speech, st->vadSt, pOverflow); 1280 vad_flag = vad2(st->new_speech + 80, st->vadSt, pOverflow) || vad_flag; 1281 #else 1282 vad_flag = vad1(st->vadSt, st->new_speech, pOverflow); 1283 #endif 1284 1285 /* NB! usedMode may change here */ 1286 compute_sid_flag = tx_dtx_handler(st->dtx_encSt, 1287 vad_flag, 1288 usedMode, pOverflow); 1289 } 1290 else 1291 { 1292 compute_sid_flag = 0; 1293 } 1294 1295 /*------------------------------------------------------------------------* 1296 * - Perform LPC analysis: * 1297 * * autocorrelation + lag windowing * 1298 * * Levinson-durbin algorithm to find a[] * 1299 * * convert a[] to lsp[] * 1300 * * quantize and code the LSPs * 1301 * * find the interpolated LSPs and convert to a[] for all * 1302 * subframes (both quantized and unquantized) * 1303 *------------------------------------------------------------------------*/ 1304 1305 /* LP analysis */ 1306 lpc(st->lpcSt, mode, st->p_window, st->p_window_12k2, A_t, pOverflow); 1307 1308 /* From A(z) to lsp. LSP quantization and interpolation */ 1309 lsp(st->lspSt, mode, *usedMode, A_t, Aq_t, lsp_new, &ana, pOverflow); 1310 1311 /* Buffer lsp's and energy */ 1312 dtx_buffer(st->dtx_encSt, 1313 lsp_new, 1314 st->new_speech, pOverflow); 1315 1316 /* Check if in DTX mode */ 1317 1318 if (*usedMode == MRDTX) 1319 { 1320 dtx_enc(st->dtx_encSt, 1321 compute_sid_flag, 1322 st->lspSt->qSt, 1323 &(st->gainQuantSt->gc_predSt), 1324 &ana, pOverflow); 1325 1326 memset(st->old_exc, 0, sizeof(Word16)*(PIT_MAX + L_INTERPOL)); 1327 memset(st->mem_w0, 0, sizeof(Word16)*M); 1328 memset(st->mem_err, 0, sizeof(Word16)*M); 1329 memset(st->zero, 0, sizeof(Word16)*L_SUBFR); 1330 memset(st->hvec, 0, sizeof(Word16)*L_SUBFR); /* set to zero "h1[-L_SUBFR..-1]" */ 1331 /* Reset lsp states */ 1332 lsp_reset(st->lspSt); 1333 1334 memcpy(st->lspSt->lsp_old, lsp_new, M*sizeof(Word16)); 1335 memcpy(st->lspSt->lsp_old_q, lsp_new, M*sizeof(Word16)); 1336 1337 /* Reset clLtp states */ 1338 cl_ltp_reset(st->clLtpSt); 1339 st->sharp = SHARPMIN; 1340 } 1341 else 1342 { 1343 /* check resonance in the filter */ 1344 lsp_flag = check_lsp(st->tonStabSt, st->lspSt->lsp_old, pOverflow); 1345 } 1346 1347 /*----------------------------------------------------------------------* 1348 * - Find the weighted input speech w_sp[] for the whole speech frame * 1349 * - Find the open-loop pitch delay for first 2 subframes * 1350 * - Set the range for searching closed-loop pitch in 1st subframe * 1351 * - Find the open-loop pitch delay for last 2 subframes * 1352 *----------------------------------------------------------------------*/ 1353 1354 #ifdef VAD2 1355 if (st->dtx) 1356 { 1357 st->vadSt->L_Rmax = 0; 1358 st->vadSt->L_R0 = 0; 1359 } 1360 #endif 1361 1362 for (subfrNr = 0, i_subfr = 0; 1363 subfrNr < L_FRAME / L_FRAME_BY2; 1364 subfrNr++, i_subfr += L_FRAME_BY2) 1365 { 1366 /* Pre-processing on 80 samples */ 1367 pre_big(mode, gamma1, gamma1_12k2, gamma2, A_t, i_subfr, st->speech, 1368 st->mem_w, st->wsp, pOverflow); 1369 1370 1371 if ((mode != MR475) && (mode != MR515)) 1372 { 1373 /* Find open loop pitch lag for two subframes */ 1374 ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[i_subfr], 1375 &T_op[subfrNr], st->old_lags, st->ol_gain_flg, subfrNr, 1376 st->dtx, pOverflow); 1377 } 1378 } 1379 1380 if ((mode == MR475) || (mode == MR515)) 1381 { 1382 /* Find open loop pitch lag for ONE FRAME ONLY */ 1383 /* search on 160 samples */ 1384 1385 ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[0], &T_op[0], 1386 st->old_lags, st->ol_gain_flg, 1, st->dtx, pOverflow); 1387 T_op[1] = T_op[0]; 1388 } 1389 1390 #ifdef VAD2 1391 if (st->dtx) 1392 { 1393 LTP_flag_update(st->vadSt, (Word16) mode, pOverflow); 1394 } 1395 #endif 1396 1397 #ifndef VAD2 1398 /* run VAD pitch detection */ 1399 if (st->dtx) 1400 { 1401 vad_pitch_detection(st->vadSt, T_op, pOverflow); 1402 } 1403 #endif 1404 1405 if (*usedMode == MRDTX) 1406 { 1407 goto the_end; 1408 } 1409 1410 /*------------------------------------------------------------------------* 1411 * Loop for every subframe in the analysis frame * 1412 *------------------------------------------------------------------------* 1413 * To find the pitch and innovation parameters. The subframe size is * 1414 * L_SUBFR and the loop is repeated L_FRAME/L_SUBFR times. * 1415 * - find the weighted LPC coefficients * 1416 * - find the LPC residual signal res[] * 1417 * - compute the target signal for pitch search * 1418 * - compute impulse response of weighted synthesis filter (h1[]) * 1419 * - find the closed-loop pitch parameters * 1420 * - encode the pitch dealy * 1421 * - update the impulse response h1[] by including fixed-gain pitch * 1422 * - find target vector for codebook search * 1423 * - codebook search * 1424 * - encode codebook address * 1425 * - VQ of pitch and codebook gains * 1426 * - find synthesis speech * 1427 * - update states of weighting filter * 1428 *------------------------------------------------------------------------*/ 1429 1430 A = A_t; /* pointer to interpolated LPC parameters */ 1431 Aq = Aq_t; /* pointer to interpolated quantized LPC parameters */ 1432 1433 evenSubfr = 0; 1434 subfrNr = -1; 1435 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) 1436 { 1437 subfrNr++; 1438 evenSubfr = 1 - evenSubfr; 1439 1440 /* Save states for the MR475 mode */ 1441 1442 if ((evenSubfr != 0) && (*usedMode == MR475)) 1443 { 1444 memcpy(mem_syn_save, st->mem_syn, M*sizeof(Word16)); 1445 memcpy(mem_w0_save, st->mem_w0, M*sizeof(Word16)); 1446 memcpy(mem_err_save, st->mem_err, M*sizeof(Word16)); 1447 1448 sharp_save = st->sharp; 1449 } 1450 1451 /*-----------------------------------------------------------------* 1452 * - Preprocessing of subframe * 1453 *-----------------------------------------------------------------*/ 1454 1455 if (*usedMode != MR475) 1456 { 1457 subframePreProc(*usedMode, gamma1, gamma1_12k2, 1458 gamma2, A, Aq, &st->speech[i_subfr], 1459 st->mem_err, st->mem_w0, st->zero, 1460 st->ai_zero, &st->exc[i_subfr], 1461 st->h1, xn, res, st->error); 1462 } 1463 else 1464 { /* MR475 */ 1465 subframePreProc(*usedMode, gamma1, gamma1_12k2, 1466 gamma2, A, Aq, &st->speech[i_subfr], 1467 st->mem_err, mem_w0_save, st->zero, 1468 st->ai_zero, &st->exc[i_subfr], 1469 st->h1, xn, res, st->error); 1470 1471 /* save impulse response (modified in cbsearch) */ 1472 1473 if (evenSubfr != 0) 1474 { 1475 memcpy(h1_sf0, st->h1, L_SUBFR*sizeof(Word16)); 1476 1477 } 1478 } 1479 1480 /* copy the LP residual (res2 is modified in the CL LTP search) */ 1481 memcpy(res2, res, L_SUBFR*sizeof(Word16)); 1482 1483 /*-----------------------------------------------------------------* 1484 * - Closed-loop LTP search * 1485 *-----------------------------------------------------------------*/ 1486 cl_ltp(st->clLtpSt, st->tonStabSt, *usedMode, i_subfr, T_op, st->h1, 1487 &st->exc[i_subfr], res2, xn, lsp_flag, xn2, y1, 1488 &T0, &T0_frac, &gain_pit, gCoeff, &ana, 1489 &gp_limit, pOverflow); 1490 1491 /* update LTP lag history */ 1492 1493 if ((subfrNr == 0) && (st->ol_gain_flg[0] > 0)) 1494 { 1495 st->old_lags[1] = T0; 1496 } 1497 1498 1499 if ((subfrNr == 3) && (st->ol_gain_flg[1] > 0)) 1500 { 1501 st->old_lags[0] = T0; 1502 } 1503 1504 /*-----------------------------------------------------------------* 1505 * - Inovative codebook search (find index and gain) * 1506 *-----------------------------------------------------------------*/ 1507 cbsearch(xn2, st->h1, T0, st->sharp, gain_pit, res2, 1508 code, y2, &ana, *usedMode, subfrNr, pOverflow); 1509 1510 /*------------------------------------------------------* 1511 * - Quantization of gains. * 1512 *------------------------------------------------------*/ 1513 gainQuant(st->gainQuantSt, *usedMode, res, &st->exc[i_subfr], code, 1514 xn, xn2, y1, y2, gCoeff, evenSubfr, gp_limit, 1515 &gain_pit_sf0, &gain_code_sf0, 1516 &gain_pit, &gain_code, &ana, pOverflow); 1517 1518 /* update gain history */ 1519 update_gp_clipping(st->tonStabSt, gain_pit, pOverflow); 1520 1521 1522 if (*usedMode != MR475) 1523 { 1524 /* Subframe Post Porcessing */ 1525 subframePostProc(st->speech, *usedMode, i_subfr, gain_pit, 1526 gain_code, Aq, synth, xn, code, y1, y2, st->mem_syn, 1527 st->mem_err, st->mem_w0, st->exc, &st->sharp, pOverflow); 1528 } 1529 else 1530 { 1531 1532 if (evenSubfr != 0) 1533 { 1534 i_subfr_sf0 = i_subfr; 1535 1536 memcpy(xn_sf0, xn, L_SUBFR*sizeof(Word16)); 1537 memcpy(y2_sf0, y2, L_SUBFR*sizeof(Word16)); 1538 memcpy(code_sf0, code, L_SUBFR*sizeof(Word16)); 1539 1540 T0_sf0 = T0; 1541 T0_frac_sf0 = T0_frac; 1542 1543 /* Subframe Post Porcessing */ 1544 subframePostProc(st->speech, *usedMode, i_subfr, gain_pit, 1545 gain_code, Aq, synth, xn, code, y1, y2, 1546 mem_syn_save, st->mem_err, mem_w0_save, 1547 st->exc, &st->sharp, pOverflow); 1548 st->sharp = sharp_save; 1549 } 1550 else 1551 { 1552 /* update both subframes for the MR475 */ 1553 1554 /* Restore states for the MR475 mode */ 1555 memcpy(st->mem_err, mem_err_save, M*sizeof(Word16)); 1556 1557 1558 /* re-build excitation for sf 0 */ 1559 Pred_lt_3or6(&st->exc[i_subfr_sf0], T0_sf0, T0_frac_sf0, 1560 L_SUBFR, 1, pOverflow); 1561 Convolve(&st->exc[i_subfr_sf0], h1_sf0, y1, L_SUBFR); 1562 1563 Aq -= MP1; 1564 subframePostProc(st->speech, *usedMode, i_subfr_sf0, 1565 gain_pit_sf0, gain_code_sf0, Aq, 1566 synth, xn_sf0, code_sf0, y1, y2_sf0, 1567 st->mem_syn, st->mem_err, st->mem_w0, st->exc, 1568 &sharp_save, pOverflow); /* overwrites sharp_save */ 1569 Aq += MP1; 1570 1571 /* re-run pre-processing to get xn right (needed by postproc) */ 1572 /* (this also reconstructs the unsharpened h1 for sf 1) */ 1573 subframePreProc(*usedMode, gamma1, gamma1_12k2, 1574 gamma2, A, Aq, &st->speech[i_subfr], 1575 st->mem_err, st->mem_w0, st->zero, 1576 st->ai_zero, &st->exc[i_subfr], 1577 st->h1, xn, res, st->error); 1578 1579 /* re-build excitation sf 1 (changed if lag < L_SUBFR) */ 1580 Pred_lt_3or6(&st->exc[i_subfr], T0, T0_frac, L_SUBFR, 1, pOverflow); 1581 Convolve(&st->exc[i_subfr], st->h1, y1, L_SUBFR); 1582 1583 subframePostProc(st->speech, *usedMode, i_subfr, gain_pit, 1584 gain_code, Aq, synth, xn, code, y1, y2, 1585 st->mem_syn, st->mem_err, st->mem_w0, 1586 st->exc, &st->sharp, pOverflow); 1587 } 1588 } 1589 1590 A += MP1; /* interpolated LPC parameters for next subframe */ 1591 Aq += MP1; 1592 } 1593 1594 memcpy(&st->old_exc[0], &st->old_exc[L_FRAME], (PIT_MAX + L_INTERPOL)*sizeof(Word16)); 1595 1596 the_end: 1597 1598 /*--------------------------------------------------* 1599 * Update signal for next frame. * 1600 *--------------------------------------------------*/ 1601 1602 memcpy(&st->old_wsp[0], &st->old_wsp[L_FRAME], PIT_MAX*sizeof(Word16)); 1603 memcpy(&st->old_speech[0], &st->old_speech[L_FRAME], (L_TOTAL - L_FRAME)*sizeof(Word16)); 1604 1605 return(0); 1606 } 1607 1608 1609