1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
41 ------------------------------------------------------------------------------
44 This file contains the function that performs adaptive phase dispersion of
45 the excitation signal. The phase dispersion initialization, reset, and
46 exit functions are included in this file, as well as, the phase dispersion
47 lock and release functions.
49 ------------------------------------------------------------------------------
52 /*----------------------------------------------------------------------------
54 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
62 ; Define module specific macros here
63 ----------------------------------------------------------------------------*/
65 /*----------------------------------------------------------------------------
67 ; Include all pre-processor statements here. Include conditional
68 ; compile variables also.
69 ----------------------------------------------------------------------------*/
71 /*----------------------------------------------------------------------------
72 ; LOCAL FUNCTION DEFINITIONS
73 ; Function Prototype declaration
74 ----------------------------------------------------------------------------*/
76 /*----------------------------------------------------------------------------
77 ; LOCAL VARIABLE DEFINITIONS
78 ; Variable declaration - defined here and used outside this module
79 ----------------------------------------------------------------------------*/
83 ------------------------------------------------------------------------------
84 FUNCTION NAME: ph_disp_reset
85 ------------------------------------------------------------------------------
86 INPUT AND OUTPUT DEFINITIONS
89 state = pointer to a structure of type ph_dispState
92 Structure pointed to by state is initialized to zeros
95 return_value = 0, if reset was successful; -1, otherwise (int)
97 Global Variables Used:
100 Local Variables Needed:
103 ------------------------------------------------------------------------------
106 This function resets the variables used by the phase dispersion function.
108 ------------------------------------------------------------------------------
113 ------------------------------------------------------------------------------
116 ph_disp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
118 ------------------------------------------------------------------------------
121 int ph_disp_reset (ph_dispState *state)
125 if (state == (ph_dispState *) NULL){
126 fprint(stderr, "ph_disp_reset: invalid parameter\n");
129 for (i=0; i<PHDGAINMEMSIZE; i++)
131 state->gainMem[i] = 0;
133 state->prevState = 0;
134 state->prevCbGain = 0;
136 state->onset = 0; // assume no onset in start
141 ------------------------------------------------------------------------------
143 [State any special notes, constraints or cautions for users of this function]
145 ------------------------------------------------------------------------------
148 Word16 ph_disp_reset(ph_dispState *state)
152 if (state == (ph_dispState *) NULL)
154 /* fprint(stderr, "ph_disp_reset: invalid parameter\n"); */
157 for (i = 0; i < PHDGAINMEMSIZE; i++)
159 state->gainMem[i] = 0;
161 state->prevState = 0;
162 state->prevCbGain = 0;
164 state->onset = 0; /* assume no onset in start */
169 /****************************************************************************/
172 ------------------------------------------------------------------------------
173 FUNCTION NAME: ph_disp_lock
174 ------------------------------------------------------------------------------
175 INPUT AND OUTPUT DEFINITIONS
178 state = pointer to a structure of type ph_dispState
181 lockFull field of the structure pointed to by state is set to 1
186 Global Variables Used:
189 Local Variables Needed:
192 ------------------------------------------------------------------------------
195 This function sets the lockFull flag to indicate a lock condition.
197 ------------------------------------------------------------------------------
202 ------------------------------------------------------------------------------
205 ph_disp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
207 ------------------------------------------------------------------------------
210 void ph_disp_lock (ph_dispState *state)
216 ------------------------------------------------------------------------------
218 [State any special notes, constraints or cautions for users of this function]
220 ------------------------------------------------------------------------------
223 void ph_disp_lock(ph_dispState *state)
230 /****************************************************************************/
233 ------------------------------------------------------------------------------
234 FUNCTION NAME: ph_disp_release
235 ------------------------------------------------------------------------------
236 INPUT AND OUTPUT DEFINITIONS
239 state = pointer to a structure of type ph_dispState
242 lockFull field of the structure pointed to by state is set to 0
247 Global Variables Used:
250 Local Variables Needed:
253 ------------------------------------------------------------------------------
256 This function clears the lockFull flag to indicate an unlocked state.
258 ------------------------------------------------------------------------------
263 ------------------------------------------------------------------------------
266 ph_disp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
268 ------------------------------------------------------------------------------
271 void ph_disp_release (ph_dispState *state)
277 ------------------------------------------------------------------------------
279 [State any special notes, constraints or cautions for users of this function]
281 ------------------------------------------------------------------------------
284 void ph_disp_release(ph_dispState *state)
291 /****************************************************************************/
294 ------------------------------------------------------------------------------
295 FUNCTION NAME: ph_disp
296 ------------------------------------------------------------------------------
297 INPUT AND OUTPUT DEFINITIONS
300 state = pointer to a structure of type ph_dispState
301 mode = codec mode (enum Mode)
302 x = LTP excitation signal buffer (Word16)
303 cbGain = codebook gain (Word16)
304 ltpGain = LTP gain (Word16)
305 inno = innovation buffer (Word16)
306 pitch_fac = pitch factor used to scale the LTP excitation (Word16)
307 tmp_shift = shift factor applied to sum of scaled LTP excitation and
308 innovation before rounding (Word16)
309 pOverflow = pointer to overflow indicator (Flag)
312 structure pointed to by state contains the updated gainMem array,
313 prevState, prevCbGain, and onset fields
314 x buffer contains the new excitation signal
315 inno buffer contains the new innovation signal
316 pOverflow -> 1 if there is overflow
321 Global Variables Used:
324 Local Variables Needed:
327 ------------------------------------------------------------------------------
330 This function performs adaptive phase dispersion, i.e., forming of total
331 excitation for the synthesis part of the decoder.
333 ------------------------------------------------------------------------------
338 ------------------------------------------------------------------------------
341 ph_disp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
343 ------------------------------------------------------------------------------
347 ph_dispState *state, // i/o : State struct
348 enum Mode mode, // i : codec mode
349 Word16 x[], // i/o Q0 : in: LTP excitation signal
350 // out: total excitation signal
351 Word16 cbGain, // i Q1 : Codebook gain
352 Word16 ltpGain, // i Q14 : LTP gain
353 Word16 inno[], // i/o Q13 : Innovation vector (Q12 for 12.2)
354 Word16 pitch_fac, // i Q14 : pitch factor used to scale the
355 LTP excitation (Q13 for 12.2)
356 Word16 tmp_shift // i Q0 : shift factor applied to sum of
357 scaled LTP ex & innov. before
364 Word16 impNr; // indicator for amount of disp./filter used
366 Word16 inno_sav[L_SUBFR];
367 Word16 ps_poss[L_SUBFR];
368 Word16 j, nze, nPulse, ppos;
369 const Word16 *ph_imp; // Pointer to phase dispersion filter
371 // Update LTP gain memory
372 for (i = PHDGAINMEMSIZE-1; i > 0; i--)
374 state->gainMem[i] = state->gainMem[i-1];
376 state->gainMem[0] = ltpGain;
378 // basic adaption of phase dispersion
379 if (sub(ltpGain, PHDTHR2LTP) < 0) { // if (ltpGain < 0.9)
380 if (sub(ltpGain, PHDTHR1LTP) > 0)
381 { // if (ltpGain > 0.6
382 impNr = 1; // medium dispersion
386 impNr = 0; // maximum dispersion
391 impNr = 2; // no dispersion
395 // onset = (cbGain > onFact * cbGainMem[0])
396 tmp1 = pv_round(L_shl(L_mult(state->prevCbGain, ONFACTPLUS1), 2));
397 if (sub(cbGain, tmp1) > 0)
399 state->onset = ONLENGTH;
403 if (state->onset > 0)
405 state->onset = sub (state->onset, 1);
409 // if not onset, check ltpGain buffer and use max phase dispersion if
410 half or more of the ltpGain-parameters say so
411 if (state->onset == 0)
413 // Check LTP gain memory and set filter accordingly
415 for (i = 0; i < PHDGAINMEMSIZE; i++)
417 if (sub(state->gainMem[i], PHDTHR1LTP) < 0)
428 // Restrict decrease in phase dispersion to one step if not onset
429 if ((sub(impNr, add(state->prevState, 1)) > 0) && (state->onset == 0))
431 impNr = sub (impNr, 1);
433 // if onset, use one step less phase dispersion
434 if((sub(impNr, 2) < 0) && (state->onset > 0))
436 impNr = add (impNr, 1);
439 // disable for very low levels
440 if(sub(cbGain, 10) < 0)
445 if(sub(state->lockFull, 1) == 0)
450 // update static memory
451 state->prevState = impNr;
452 state->prevCbGain = cbGain;
454 // do phase dispersion for all modes but 12.2 and 7.4;
455 // don't modify the innovation if impNr >=2 (= no phase disp)
456 if (sub(mode, MR122) != 0 &&
457 sub(mode, MR102) != 0 &&
458 sub(mode, MR74) != 0 &&
461 // track pulse positions, save innovation,
462 and initialize new innovation
464 for (i = 0; i < L_SUBFR; i++)
471 inno_sav[i] = inno[i];
474 // Choose filter corresponding to codec mode and dispersion criterium
475 if (sub (mode, MR795) == 0)
479 ph_imp = ph_imp_low_MR795;
483 ph_imp = ph_imp_mid_MR795;
498 // Do phase dispersion of innovation
499 for (nPulse = 0; nPulse < nze; nPulse++)
501 ppos = ps_poss[nPulse];
503 // circular convolution with impulse response
505 for (i = ppos; i < L_SUBFR; i++)
507 // inno[i1] += inno_sav[ppos] * ph_imp[i1-ppos]
508 tmp1 = mult(inno_sav[ppos], ph_imp[j++]);
509 inno[i] = add(inno[i], tmp1);
512 for (i = 0; i < ppos; i++)
514 // inno[i] += inno_sav[ppos] * ph_imp[L_SUBFR-ppos+i]
515 tmp1 = mult(inno_sav[ppos], ph_imp[j++]);
516 inno[i] = add(inno[i], tmp1);
521 // compute total excitation for synthesis part of decoder
522 // (using modified innovation if phase dispersion is active)
523 for (i = 0; i < L_SUBFR; i++)
525 // x[i] = gain_pit*x[i] + cbGain*code[i];
526 L_temp = L_mult ( x[i], pitch_fac);
529 L_temp = L_mac (L_temp, inno[i], cbGain);
532 L_temp = L_shl (L_temp, tmp_shift); // Q16
533 x[i] = pv_round (L_temp);
539 ------------------------------------------------------------------------------
541 [State any special notes, constraints or cautions for users of this function]
543 ------------------------------------------------------------------------------
547 ph_dispState *state, /* i/o : State struct */
548 enum Mode mode, /* i : codec mode */
549 Word16 x[], /* i/o Q0 : in: LTP excitation signal */
550 /* out: total excitation signal */
551 Word16 cbGain, /* i Q1 : Codebook gain */
552 Word16 ltpGain, /* i Q14 : LTP gain */
553 Word16 inno[], /* i/o Q13 : Innovation vector (Q12 for 12.2) */
554 Word16 pitch_fac, /* i Q14 : pitch factor used to scale the
555 LTP excitation (Q13 for 12.2) */
556 Word16 tmp_shift, /* i Q0 : shift factor applied to sum of
557 scaled LTP ex & innov. before
559 CommonAmrTbls* common_amr_tbls, /* i : ptr to struct of table ptrs */
560 Flag *pOverflow /* i/o : oveflow indicator */
563 register Word16 i, i1;
564 register Word16 tmp1;
567 Word16 impNr; /* indicator for amount of disp./filter used */
569 Word16 inno_sav[L_SUBFR];
570 Word16 ps_poss[L_SUBFR];
571 register Word16 nze, nPulse;
573 const Word16 *ph_imp; /* Pointer to phase dispersion filter */
578 const Word16 *p_ph_imp;
581 const Word16* ph_imp_low_MR795_ptr = common_amr_tbls->ph_imp_low_MR795_ptr;
582 const Word16* ph_imp_mid_MR795_ptr = common_amr_tbls->ph_imp_mid_MR795_ptr;
583 const Word16* ph_imp_low_ptr = common_amr_tbls->ph_imp_low_ptr;
584 const Word16* ph_imp_mid_ptr = common_amr_tbls->ph_imp_mid_ptr;
586 /* Update LTP gain memory */
587 /* Unrolled FOR loop below since PHDGAINMEMSIZE is assumed to stay */
589 /* for (i = PHDGAINMEMSIZE-1; i > 0; i--) */
591 /* state->gainMem[i] = state->gainMem[i-1]; */
593 state->gainMem[4] = state->gainMem[3];
594 state->gainMem[3] = state->gainMem[2];
595 state->gainMem[2] = state->gainMem[1];
596 state->gainMem[1] = state->gainMem[0];
597 state->gainMem[0] = ltpGain;
599 /* basic adaption of phase dispersion */
601 if (ltpGain < PHDTHR2LTP) /* if (ltpGain < 0.9) */
603 if (ltpGain > PHDTHR1LTP)
604 { /* if (ltpGain > 0.6 */
605 impNr = 1; /* medium dispersion */
609 impNr = 0; /* maximum dispersion */
614 impNr = 2; /* no dispersion */
617 /* onset indicator */
618 /* onset = (cbGain > onFact * cbGainMem[0]) */
620 L_temp = ((Word32) state->prevCbGain * ONFACTPLUS1) << 1;
622 /* (L_temp << 2) calculation with saturation check */
623 if (L_temp > (Word32) 0X1fffffffL)
628 else if (L_temp < (Word32) 0xe0000000L)
638 tmp1 = pv_round(L_temp, pOverflow);
642 state->onset = ONLENGTH;
647 if (state->onset > 0)
653 /* if not onset, check ltpGain buffer and use max phase dispersion if
654 half or more of the ltpGain-parameters say so */
655 if (state->onset == 0)
657 /* Check LTP gain memory and set filter accordingly */
659 for (i = 0; i < PHDGAINMEMSIZE; i++)
661 if (state->gainMem[i] < PHDTHR1LTP)
672 /* Restrict decrease in phase dispersion to one step if not onset */
673 if ((impNr > ((state->prevState) + 1)) && (state->onset == 0))
678 /* if onset, use one step less phase dispersion */
679 if ((impNr < 2) && (state->onset > 0))
684 /* disable for very low levels */
690 if (state->lockFull == 1)
695 /* update static memory */
696 state->prevState = impNr;
697 state->prevCbGain = cbGain;
699 /* do phase dispersion for all modes but 12.2 and 7.4;
700 don't modify the innovation if impNr >=2 (= no phase disp) */
701 if ((mode != MR122) && (mode != MR102) && (mode != MR74) && (impNr < 2))
703 /* track pulse positions, save innovation,
704 and initialize new innovation */
707 p_inno_sav = &inno_sav[0];
709 for (i = 0; i < L_SUBFR; i++)
716 *(p_inno_sav++) = *(p_inno);
720 /* Choose filter corresponding to codec mode and dispersion criterium */
725 ph_imp = ph_imp_low_MR795_ptr;
729 ph_imp = ph_imp_mid_MR795_ptr;
736 ph_imp = ph_imp_low_ptr;
740 ph_imp = ph_imp_mid_ptr;
744 /* Do phase dispersion of innovation */
745 for (nPulse = 0; nPulse < nze; nPulse++)
747 ppos = ps_poss[nPulse];
749 /* circular convolution with impulse response */
750 c_inno_sav = inno_sav[ppos];
751 p_inno = &inno[ppos];
754 for (i = ppos; i < L_SUBFR; i++)
756 /* inno[i1] += inno_sav[ppos] * ph_imp[i1-ppos] */
757 L_temp = ((Word32) c_inno_sav * *(p_ph_imp++)) >> 15;
758 tmp1 = (Word16) L_temp;
759 *(p_inno) = add_16(*(p_inno), tmp1, pOverflow);
765 for (i = 0; i < ppos; i++)
767 /* inno[i] += inno_sav[ppos] * ph_imp[L_SUBFR-ppos+i] */
768 L_temp = ((Word32) c_inno_sav * *(p_ph_imp++)) >> 15;
769 tmp1 = (Word16) L_temp;
770 *(p_inno) = add_16(*(p_inno), tmp1, pOverflow);
776 /* compute total excitation for synthesis part of decoder
777 (using modified innovation if phase dispersion is active) */
781 for (i = 0; i < L_SUBFR; i++)
783 /* x[i] = gain_pit*x[i] + cbGain*code[i]; */
784 L_temp = L_mult(x[i], pitch_fac, pOverflow);
787 L_temp2 = ((Word32) * (p_inno++) * cbGain) << 1;
788 L_temp = L_add(L_temp, L_temp2, pOverflow);
791 L_temp = L_shl(L_temp, tmp_shift, pOverflow); /* Q16 */
792 *(p_x++) = pv_round(L_temp, pOverflow);