FreeCalypso > hg > gsm-codec-lib
comparison libgsmefr/dec_12k2.c @ 53:49dd1ac8e75b
libgsmefr: import most *.c files from ETSI source
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 25 Nov 2022 16:18:21 +0000 |
parents | |
children | 1cc2968f883f |
comparison
equal
deleted
inserted
replaced
52:988fd7ff514f | 53:49dd1ac8e75b |
---|---|
1 /*************************************************************************** | |
2 * | |
3 * FILE NAME: dec_12k2.c | |
4 * | |
5 * FUNCTIONS DEFINED IN THIS FILE: | |
6 * Init_Decoder_12k2 and Decoder_12k2 | |
7 * | |
8 * | |
9 * Init_Decoder_12k2(): | |
10 * Initialization of variables for the decoder section. | |
11 * | |
12 * Decoder_12k2(): | |
13 * Speech decoder routine operating on a frame basis. | |
14 * | |
15 ***************************************************************************/ | |
16 | |
17 #include "typedef.h" | |
18 #include "basic_op.h" | |
19 #include "sig_proc.h" | |
20 #include "count.h" | |
21 #include "codec.h" | |
22 #include "cnst.h" | |
23 | |
24 #include "dtx.h" | |
25 | |
26 extern Word16 dtx_mode; | |
27 | |
28 /*---------------------------------------------------------------* | |
29 * Decoder constant parameters (defined in "cnst.h") * | |
30 *---------------------------------------------------------------* | |
31 * L_FRAME : Frame size. * | |
32 * L_FRAME_BY2 : Half the frame size. * | |
33 * L_SUBFR : Sub-frame size. * | |
34 * M : LPC order. * | |
35 * MP1 : LPC order+1 * | |
36 * PIT_MIN : Minimum pitch lag. * | |
37 * PIT_MAX : Maximum pitch lag. * | |
38 * L_INTERPOL : Length of filter for interpolation * | |
39 * PRM_SIZE : size of vector containing analysis parameters * | |
40 *---------------------------------------------------------------*/ | |
41 | |
42 /*--------------------------------------------------------* | |
43 * Static memory allocation. * | |
44 *--------------------------------------------------------*/ | |
45 | |
46 /* Excitation vector */ | |
47 | |
48 static Word16 old_exc[L_FRAME + PIT_MAX + L_INTERPOL]; | |
49 static Word16 *exc; | |
50 | |
51 /* Lsp (Line spectral pairs) */ | |
52 | |
53 static Word16 lsp_old[M]; | |
54 | |
55 /* Filter's memory */ | |
56 | |
57 static Word16 mem_syn[M]; | |
58 | |
59 /* Memories for bad frame handling */ | |
60 | |
61 static Word16 prev_bf; | |
62 static Word16 state; | |
63 | |
64 /*************************************************************************** | |
65 * | |
66 * FUNCTION: Init_Decoder_12k2 | |
67 * | |
68 * PURPOSE: Initialization of variables for the decoder section. | |
69 * | |
70 ***************************************************************************/ | |
71 | |
72 void Init_Decoder_12k2 (void) | |
73 { | |
74 /* Initialize static pointer */ | |
75 | |
76 exc = old_exc + PIT_MAX + L_INTERPOL; | |
77 | |
78 /* Static vectors to zero */ | |
79 | |
80 Set_zero (old_exc, PIT_MAX + L_INTERPOL); | |
81 Set_zero (mem_syn, M); | |
82 | |
83 /* Initialize lsp_old [] */ | |
84 | |
85 lsp_old[0] = 30000; | |
86 lsp_old[1] = 26000; | |
87 lsp_old[2] = 21000; | |
88 lsp_old[3] = 15000; | |
89 lsp_old[4] = 8000; | |
90 lsp_old[5] = 0; | |
91 lsp_old[6] = -8000; | |
92 lsp_old[7] = -15000; | |
93 lsp_old[8] = -21000; | |
94 lsp_old[9] = -26000; | |
95 | |
96 /* Initialize memories of bad frame handling */ | |
97 | |
98 prev_bf = 0; | |
99 state = 0; | |
100 | |
101 return; | |
102 } | |
103 | |
104 /*************************************************************************** | |
105 * | |
106 * FUNCTION: Decoder_12k2 | |
107 * | |
108 * PURPOSE: Speech decoder routine. | |
109 * | |
110 ***************************************************************************/ | |
111 | |
112 void Decoder_12k2 ( | |
113 Word16 parm[], /* input : vector of synthesis parameters | |
114 parm[0] = bad frame indicator (bfi) */ | |
115 Word16 synth[],/* output: synthesis speech */ | |
116 Word16 A_t[], /* output: decoded LP filter in 4 subframes */ | |
117 Word16 TAF, | |
118 Word16 SID_flag | |
119 ) | |
120 { | |
121 | |
122 /* LPC coefficients */ | |
123 | |
124 Word16 *Az; /* Pointer on A_t */ | |
125 | |
126 /* LSPs */ | |
127 | |
128 Word16 lsp_new[M]; | |
129 Word16 lsp_mid[M]; | |
130 | |
131 /* Algebraic codevector */ | |
132 | |
133 Word16 code[L_SUBFR]; | |
134 | |
135 /* excitation */ | |
136 | |
137 Word16 excp[L_SUBFR]; | |
138 | |
139 /* Scalars */ | |
140 | |
141 Word16 i, i_subfr; | |
142 Word16 T0, T0_frac, index; | |
143 Word16 gain_pit, gain_code, bfi, pit_sharp; | |
144 Word16 temp; | |
145 Word32 L_temp; | |
146 | |
147 extern Word16 rxdtx_ctrl, rx_dtx_state; | |
148 extern Word32 L_pn_seed_rx; | |
149 | |
150 /* Test bad frame indicator (bfi) */ | |
151 | |
152 bfi = *parm++; move16 (); | |
153 | |
154 /* Set state machine */ | |
155 | |
156 test (); test (); | |
157 if (bfi != 0) | |
158 { | |
159 state = add (state, 1); | |
160 } | |
161 else if (sub (state, 6) == 0) | |
162 { | |
163 state = 5; | |
164 } | |
165 else | |
166 { | |
167 state = 0; | |
168 } | |
169 | |
170 test (); | |
171 if (sub (state, 6) > 0) | |
172 { | |
173 state = 6; | |
174 } | |
175 rx_dtx (&rxdtx_ctrl, TAF, bfi, SID_flag); | |
176 | |
177 /* If this frame is the first speech frame after CNI period, */ | |
178 /* set the BFH state machine to an appropriate state depending */ | |
179 /* on whether there was DTX muting before start of speech or not */ | |
180 /* If there was DTX muting, the first speech frame is muted. */ | |
181 /* If there was no DTX muting, the first speech frame is not */ | |
182 /* muted. The BFH state machine starts from state 5, however, to */ | |
183 /* keep the audible noise resulting from a SID frame which is */ | |
184 /* erroneously interpreted as a good speech frame as small as */ | |
185 /* possible (the decoder output in this case is quickly muted) */ | |
186 test (); logic16 (); | |
187 if ((rxdtx_ctrl & RX_FIRST_SP_FLAG) != 0) | |
188 { | |
189 test (); logic16 (); | |
190 if ((rxdtx_ctrl & RX_PREV_DTX_MUTING) != 0) | |
191 { | |
192 state = 5; move16 (); | |
193 prev_bf = 1; move16 (); | |
194 } | |
195 else | |
196 { | |
197 state = 5; move16 (); | |
198 prev_bf = 0; move16 (); | |
199 } | |
200 } | |
201 | |
202 #if (WMOPS) | |
203 fwc (); /* function worst case */ | |
204 | |
205 /* Note! The following test is performed only for determining | |
206 whether or not DTX mode is active, in order to switch off | |
207 worst worst case complexity printout when DTX mode is active | |
208 */ | |
209 if ((rxdtx_ctrl & RX_SP_FLAG) == 0) | |
210 { | |
211 dtx_mode = 1; | |
212 } | |
213 #endif | |
214 | |
215 D_plsf_5 (parm, lsp_mid, lsp_new, bfi, rxdtx_ctrl, rx_dtx_state); | |
216 | |
217 #if (WMOPS) | |
218 fwc (); /* function worst case */ | |
219 #endif | |
220 /* Advance synthesis parameters pointer */ | |
221 parm += 5; move16 (); | |
222 | |
223 test (); logic16 (); | |
224 if ((rxdtx_ctrl & RX_SP_FLAG) != 0) | |
225 { | |
226 /* Interpolation of LPC for the 4 subframes */ | |
227 | |
228 Int_lpc (lsp_old, lsp_mid, lsp_new, A_t); | |
229 } | |
230 else | |
231 { | |
232 /* Comfort noise: use the same parameters in each subframe */ | |
233 Lsp_Az (lsp_new, A_t); | |
234 | |
235 for (i = 0; i < MP1; i++) | |
236 { | |
237 A_t[i + MP1] = A_t[i]; move16 (); | |
238 A_t[i + 2 * MP1] = A_t[i]; move16 (); | |
239 A_t[i + 3 * MP1] = A_t[i]; move16 (); | |
240 } | |
241 } | |
242 | |
243 /* update the LSPs for the next frame */ | |
244 for (i = 0; i < M; i++) | |
245 { | |
246 lsp_old[i] = lsp_new[i]; move16 (); | |
247 } | |
248 #if (WMOPS) | |
249 fwc (); /* function worst case */ | |
250 #endif | |
251 | |
252 /*---------------------------------------------------------------------* | |
253 * Loop for every subframe in the analysis frame * | |
254 *---------------------------------------------------------------------* | |
255 * The subframe size is L_SUBFR and the loop is repeated * | |
256 * L_FRAME/L_SUBFR times * | |
257 * - decode the pitch delay * | |
258 * - decode algebraic code * | |
259 * - decode pitch and codebook gains * | |
260 * - find the excitation and compute synthesis speech * | |
261 *---------------------------------------------------------------------*/ | |
262 | |
263 /* pointer to interpolated LPC parameters */ | |
264 Az = A_t; move16 (); | |
265 | |
266 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) | |
267 { | |
268 | |
269 index = *parm++; move16 (); /* pitch index */ | |
270 | |
271 test (); logic16 (); | |
272 if ((rxdtx_ctrl & RX_SP_FLAG) != 0) | |
273 { | |
274 T0 = Dec_lag6 (index, PIT_MIN, PIT_MAX, i_subfr, L_FRAME_BY2, | |
275 &T0_frac, bfi); | |
276 #if (WMOPS) | |
277 fwc (); /* function worst case */ | |
278 #endif | |
279 | |
280 /*-------------------------------------------------* | |
281 * - Find the adaptive codebook vector. * | |
282 *-------------------------------------------------*/ | |
283 | |
284 Pred_lt_6 (&exc[i_subfr], T0, T0_frac, L_SUBFR); | |
285 #if (WMOPS) | |
286 fwc (); /* function worst case */ | |
287 #endif | |
288 } | |
289 else | |
290 { | |
291 T0 = L_SUBFR; move16 (); | |
292 } | |
293 | |
294 /*-------------------------------------------------------* | |
295 * - Decode pitch gain. * | |
296 *-------------------------------------------------------*/ | |
297 | |
298 index = *parm++; move16 (); | |
299 | |
300 gain_pit = d_gain_pitch (index, bfi, state, prev_bf, rxdtx_ctrl); | |
301 move16 (); | |
302 #if (WMOPS) | |
303 fwc (); /* function worst case */ | |
304 #endif | |
305 | |
306 /*-------------------------------------------------------* | |
307 * - Decode innovative codebook. * | |
308 *-------------------------------------------------------*/ | |
309 | |
310 test (); logic16 (); | |
311 if ((rxdtx_ctrl & RX_SP_FLAG) != 0) | |
312 { | |
313 dec_10i40_35bits (parm, code); | |
314 } | |
315 else | |
316 { /* Use pseudo noise for excitation when SP_flag == 0 */ | |
317 build_CN_code (code, &L_pn_seed_rx); | |
318 } | |
319 | |
320 parm += 10; move16 (); | |
321 #if (WMOPS) | |
322 fwc (); /* function worst case */ | |
323 #endif | |
324 | |
325 /*-------------------------------------------------------* | |
326 * - Add the pitch contribution to code[]. * | |
327 *-------------------------------------------------------*/ | |
328 | |
329 /* pit_sharp = gain_pit; */ | |
330 /* if (pit_sharp > 1.0) pit_sharp = 1.0; */ | |
331 | |
332 pit_sharp = shl (gain_pit, 3); | |
333 | |
334 /* This loop is not entered when SP_FLAG is 0 */ | |
335 for (i = T0; i < L_SUBFR; i++) | |
336 { | |
337 temp = mult (code[i - T0], pit_sharp); | |
338 code[i] = add (code[i], temp); | |
339 move16 (); | |
340 } | |
341 #if (WMOPS) | |
342 fwc (); /* function worst case */ | |
343 #endif | |
344 /* post processing of excitation elements */ | |
345 | |
346 test (); /* This test is not passed when SP_FLAG is 0 */ | |
347 if (sub (pit_sharp, 16384) > 0) | |
348 { | |
349 for (i = 0; i < L_SUBFR; i++) | |
350 { | |
351 temp = mult (exc[i + i_subfr], pit_sharp); | |
352 L_temp = L_mult (temp, gain_pit); | |
353 L_temp = L_shl (L_temp, 1); | |
354 excp[i] = round (L_temp); | |
355 move16 (); | |
356 } | |
357 } | |
358 /*-------------------------------------------------* | |
359 * - Decode codebook gain. * | |
360 *-------------------------------------------------*/ | |
361 | |
362 index = *parm++; move16 (); /* index of energy VQ */ | |
363 | |
364 d_gain_code (index, code, L_SUBFR, &gain_code, bfi, state, prev_bf, | |
365 rxdtx_ctrl, i_subfr, rx_dtx_state); | |
366 #if (WMOPS) | |
367 fwc (); /* function worst case */ | |
368 #endif | |
369 | |
370 /*-------------------------------------------------------* | |
371 * - Find the total excitation. * | |
372 * - Find synthesis speech corresponding to exc[]. * | |
373 *-------------------------------------------------------*/ | |
374 for (i = 0; i < L_SUBFR; i++) | |
375 { | |
376 /* exc[i] = gain_pit*exc[i] + gain_code*code[i]; */ | |
377 | |
378 L_temp = L_mult (exc[i + i_subfr], gain_pit); | |
379 L_temp = L_mac (L_temp, code[i], gain_code); | |
380 L_temp = L_shl (L_temp, 3); | |
381 | |
382 exc[i + i_subfr] = round (L_temp); | |
383 move16 (); | |
384 } | |
385 #if (WMOPS) | |
386 fwc (); /* function worst case */ | |
387 #endif | |
388 | |
389 test (); | |
390 if (sub (pit_sharp, 16384) > 0) | |
391 { | |
392 for (i = 0; i < L_SUBFR; i++) | |
393 { | |
394 excp[i] = add (excp[i], exc[i + i_subfr]); | |
395 move16 (); | |
396 } | |
397 agc2 (&exc[i_subfr], excp, L_SUBFR); | |
398 Syn_filt (Az, excp, &synth[i_subfr], L_SUBFR, mem_syn, 1); | |
399 } | |
400 else | |
401 { | |
402 Syn_filt (Az, &exc[i_subfr], &synth[i_subfr], L_SUBFR, mem_syn, 1); | |
403 } | |
404 | |
405 #if (WMOPS) | |
406 fwc (); /* function worst case */ | |
407 #endif | |
408 /* interpolated LPC parameters for next subframe */ | |
409 Az += MP1; move16 (); | |
410 } | |
411 | |
412 /*--------------------------------------------------* | |
413 * Update signal for next frame. * | |
414 * -> shift to the left by L_FRAME exc[] * | |
415 *--------------------------------------------------*/ | |
416 | |
417 Copy (&old_exc[L_FRAME], &old_exc[0], PIT_MAX + L_INTERPOL); | |
418 #if (WMOPS) | |
419 fwc (); /* function worst case */ | |
420 #endif | |
421 prev_bf = bfi; move16 (); | |
422 | |
423 return; | |
424 } |