FreeCalypso > hg > gsm-codec-lib
comparison libtwamr/cl_ltp.c @ 406:85e9768d497f
libtwamr: integrate cl_ltp.c
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Mon, 06 May 2024 23:32:59 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 405:8fff74ca83e8 | 406:85e9768d497f |
|---|---|
| 1 /* | |
| 2 ***************************************************************************** | |
| 3 * | |
| 4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001 | |
| 5 * R99 Version 3.3.0 | |
| 6 * REL-4 Version 4.1.0 | |
| 7 * | |
| 8 ***************************************************************************** | |
| 9 * | |
| 10 * File : cl_ltp.c | |
| 11 * | |
| 12 ***************************************************************************** | |
| 13 */ | |
| 14 | |
| 15 /* | |
| 16 ***************************************************************************** | |
| 17 * MODULE INCLUDE FILE AND VERSION ID | |
| 18 ***************************************************************************** | |
| 19 */ | |
| 20 #include "namespace.h" | |
| 21 #include "cl_ltp.h" | |
| 22 | |
| 23 /* | |
| 24 ***************************************************************************** | |
| 25 * INCLUDE FILES | |
| 26 ***************************************************************************** | |
| 27 */ | |
| 28 #include <stddef.h> | |
| 29 #include "typedef.h" | |
| 30 #include "basic_op.h" | |
| 31 #include "no_count.h" | |
| 32 #include "oper_32b.h" | |
| 33 #include "cnst.h" | |
| 34 #include "convolve.h" | |
| 35 #include "g_pitch.h" | |
| 36 #include "pred_lt.h" | |
| 37 #include "pitch_fr.h" | |
| 38 #include "enc_lag3.h" | |
| 39 #include "enc_lag6.h" | |
| 40 #include "q_gain_p.h" | |
| 41 #include "ton_stab.h" | |
| 42 | |
| 43 /* | |
| 44 ***************************************************************************** | |
| 45 * LOCAL VARIABLES AND TABLES | |
| 46 ***************************************************************************** | |
| 47 */ | |
| 48 | |
| 49 /* | |
| 50 ***************************************************************************** | |
| 51 * PUBLIC PROGRAM CODE | |
| 52 ***************************************************************************** | |
| 53 */ | |
| 54 | |
| 55 /************************************************************************* | |
| 56 * | |
| 57 * Function: cl_ltp_reset | |
| 58 * Purpose: Initializes state memory to zero | |
| 59 * | |
| 60 ************************************************************************** | |
| 61 */ | |
| 62 void cl_ltp_reset (clLtpState *state) | |
| 63 { | |
| 64 /* Reset pitch search states */ | |
| 65 Pitch_fr_reset (&state->pitchSt); | |
| 66 } | |
| 67 | |
| 68 /************************************************************************* | |
| 69 * | |
| 70 * Function: cl_ltp | |
| 71 * Purpose: closed-loop fractional pitch search | |
| 72 * | |
| 73 ************************************************************************** | |
| 74 */ | |
| 75 int cl_ltp ( | |
| 76 clLtpState *clSt, /* i/o : State struct */ | |
| 77 tonStabState *tonSt, /* i/o : State struct */ | |
| 78 enum Mode mode, /* i : coder mode */ | |
| 79 Word16 frameOffset, /* i : Offset to subframe */ | |
| 80 Word16 T_op[], /* i : Open loop pitch lags */ | |
| 81 Word16 *h1, /* i : Impulse response vector Q12 */ | |
| 82 Word16 *exc, /* i/o : Excitation vector Q0 */ | |
| 83 Word16 res2[], /* i/o : Long term prediction residual Q0 */ | |
| 84 Word16 xn[], /* i : Target vector for pitch search Q0 */ | |
| 85 Word16 lsp_flag, /* i : LSP resonance flag */ | |
| 86 Word16 xn2[], /* o : Target vector for codebook search Q0 */ | |
| 87 Word16 y1[], /* o : Filtered adaptive excitation Q0 */ | |
| 88 Word16 *T0, /* o : Pitch delay (integer part) */ | |
| 89 Word16 *T0_frac, /* o : Pitch delay (fractional part) */ | |
| 90 Word16 *gain_pit, /* o : Pitch gain Q14 */ | |
| 91 Word16 g_coeff[], /* o : Correlations between xn, y1, & y2 */ | |
| 92 Word16 **anap, /* o : Analysis parameters */ | |
| 93 Word16 *gp_limit /* o : pitch gain limit */ | |
| 94 ) | |
| 95 { | |
| 96 Word16 i; | |
| 97 Word16 index; | |
| 98 Word32 L_temp; /* temporarily variable */ | |
| 99 Word16 resu3; /* flag for upsample resolution */ | |
| 100 Word16 gpc_flag; | |
| 101 | |
| 102 /*----------------------------------------------------------------------* | |
| 103 * Closed-loop fractional pitch search * | |
| 104 *----------------------------------------------------------------------*/ | |
| 105 *T0 = Pitch_fr(&clSt->pitchSt, | |
| 106 mode, T_op, exc, xn, h1, | |
| 107 L_SUBFR, frameOffset, | |
| 108 T0_frac, &resu3, &index); move16 (); | |
| 109 | |
| 110 *(*anap)++ = index; move16 (); | |
| 111 | |
| 112 /*-----------------------------------------------------------------* | |
| 113 * - find unity gain pitch excitation (adapitve codebook entry) * | |
| 114 * with fractional interpolation. * | |
| 115 * - find filtered pitch exc. y1[]=exc[] convolve with h1[]) * | |
| 116 * - compute pitch gain and limit between 0 and 1.2 * | |
| 117 * - update target vector for codebook search * | |
| 118 * - find LTP residual. * | |
| 119 *-----------------------------------------------------------------*/ | |
| 120 | |
| 121 Pred_lt_3or6(exc, *T0, *T0_frac, L_SUBFR, resu3); | |
| 122 | |
| 123 Convolve(exc, h1, y1, L_SUBFR); | |
| 124 | |
| 125 /* gain_pit is Q14 for all modes */ | |
| 126 *gain_pit = G_pitch(mode, xn, y1, g_coeff, L_SUBFR); move16 (); | |
| 127 | |
| 128 | |
| 129 /* check if the pitch gain should be limit due to resonance in LPC filter */ | |
| 130 gpc_flag = 0; move16 (); | |
| 131 *gp_limit = MAX_16; move16 (); | |
| 132 test (); test (); | |
| 133 if ((lsp_flag != 0) && | |
| 134 (sub(*gain_pit, GP_CLIP) > 0)) | |
| 135 { | |
| 136 gpc_flag = check_gp_clipping(tonSt, *gain_pit); move16 (); | |
| 137 } | |
| 138 | |
| 139 /* special for the MR475, MR515 mode; limit the gain to 0.85 to */ | |
| 140 /* cope with bit errors in the decoder in a better way. */ | |
| 141 test (); test (); | |
| 142 if ((sub (mode, MR475) == 0) || (sub (mode, MR515) == 0)) { | |
| 143 test (); | |
| 144 if ( sub (*gain_pit, 13926) > 0) { | |
| 145 *gain_pit = 13926; /* 0.85 in Q14 */ move16 (); | |
| 146 } | |
| 147 | |
| 148 test (); | |
| 149 if (gpc_flag != 0) { | |
| 150 *gp_limit = GP_CLIP; move16 (); | |
| 151 } | |
| 152 } | |
| 153 else | |
| 154 { | |
| 155 test (); | |
| 156 if (gpc_flag != 0) | |
| 157 { | |
| 158 *gp_limit = GP_CLIP; move16 (); | |
| 159 *gain_pit = GP_CLIP; move16 (); | |
| 160 } | |
| 161 /* For MR122, gain_pit is quantized here and not in gainQuant */ | |
| 162 if (test (), sub(mode, MR122)==0) | |
| 163 { | |
| 164 *(*anap)++ = q_gain_pitch(MR122, *gp_limit, gain_pit, | |
| 165 NULL, NULL); | |
| 166 move16 (); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 /* update target vector und evaluate LTP residual */ | |
| 171 for (i = 0; i < L_SUBFR; i++) { | |
| 172 L_temp = L_mult(y1[i], *gain_pit); | |
| 173 L_temp = L_shl(L_temp, 1); | |
| 174 xn2[i] = sub(xn[i], extract_h(L_temp)); move16 (); | |
| 175 | |
| 176 L_temp = L_mult(exc[i], *gain_pit); | |
| 177 L_temp = L_shl(L_temp, 1); | |
| 178 res2[i] = sub(res2[i], extract_h(L_temp)); move16 (); | |
| 179 } | |
| 180 | |
| 181 return 0; | |
| 182 } |
