summaryrefslogtreecommitdiff
path: root/audio_codec/libamr/sp_dec.c (plain)
blob: 8ccca93d70660daafc444de344e15f0a28d085dc
1/*
2 * ===================================================================
3 * TS 26.104
4 * R99 V3.5.0 2003-03
5 * REL-4 V4.4.0 2003-03
6 * REL-5 V5.1.0 2003-03
7 * 3GPP AMR Floating-point Speech Codec
8 * ===================================================================
9 *
10 */
11
12/*
13 * sp_dec.c
14 *
15 *
16 * Project:
17 * AMR Floating-Point Codec
18 *
19 * Contains:
20 * This module contains all the functions needed decoding AMR
21 * encoder parameters to 16-bit speech samples
22 *
23 */
24/*
25 * include files
26 */
27#include <stdio.h>
28#include <stdlib.h>
29#include <memory.h>
30#include <math.h>
31#include "sp_dec.h"
32#include "rom_dec.h"
33
34
35/*
36 * Declare structure types
37 */
38enum DTXStateType {
39 SPEECH = 0, DTX, DTX_MUTE
40};
41
42/*
43 * Decoder memory structure
44 */
45typedef struct {
46 /* history vector of past synthesis speech energy */
47 Word32 frameEnergyHist[L_ENERGYHIST];
48
49
50 /* state flags */
51 Word16 bgHangover; /* counter; number of frames after last speech frame */
52
53
54} Bgn_scdState;
55typedef struct {
56 Word32 hangCount; /* counter; */
57 /* history vector of past synthesis speech energy */
58 Word32 cbGainHistory[L_CBGAINHIST];
59 Word16 hangVar; /* counter; */
60
61} Cb_gain_averageState;
62typedef struct {
63 Word32 lsp_meanSave[M]; /* Averaged LSPs saved for efficiency */
64
65
66} lsp_avgState;
67typedef struct {
68 Word32 past_r_q[M]; /* Past quantized prediction error, Q15 */
69 Word32 past_lsf_q[M]; /* Past dequantized lsfs, Q15 */
70
71
72} D_plsfState;
73typedef struct {
74 Word32 pbuf[5];
75 Word32 past_gain_pit;
76 Word32 prev_gp;
77
78
79} ec_gain_pitchState;
80typedef struct {
81 Word32 gbuf[5];
82 Word32 past_gain_code;
83 Word32 prev_gc;
84
85
86} ec_gain_codeState;
87typedef struct {
88 /*
89 * normal MA predictor memory, Q10
90 * (contains 20*log10(quaErr))
91 */
92 Word32 past_qua_en[4];
93
94
95 /*
96 * MA predictor memory for MR122 mode, Q10
97 * (contains log2(quaErr))
98 */
99 Word32 past_qua_en_MR122[4];
100
101
102} gc_predState;
103typedef struct {
104 Word32 gainMem[PHDGAINMEMSIZE];
105 Word32 prevCbGain;
106 Word32 prevState;
107 Word16 lockFull;
108 Word16 onset;
109
110
111} ph_dispState;
112typedef struct {
113 enum DTXStateType dtxGlobalState; /* contains previous state */
114
115 Word32 log_en;
116 Word32 old_log_en;
117 Word32 pn_seed_rx;
118 Word32 lsp[M];
119 Word32 lsp_old[M];
120 Word32 lsf_hist[M * DTX_HIST_SIZE];
121 Word32 lsf_hist_mean[M * DTX_HIST_SIZE];
122 Word32 log_en_hist[DTX_HIST_SIZE];
123 Word32 true_sid_period_inv;
124 Word16 since_last_sid;
125 Word16 lsf_hist_ptr;
126 Word16 log_pg_mean;
127 Word16 log_en_hist_ptr;
128 Word16 log_en_adjust;
129 Word16 dtxHangoverCount;
130 Word16 decAnaElapsedCount;
131 Word16 sid_frame;
132 Word16 valid_data;
133 Word16 dtxHangoverAdded;
134
135
136 /* updated in main decoder */
137 Word16 data_updated; /* marker to know if CNI data is ever renewed */
138
139
140} dtx_decState;
141typedef struct {
142 Word32 past_gain;
143
144
145} agcState;
146typedef struct {
147 /* Excitation vector */
148 Word32 old_exc[L_SUBFR + PIT_MAX + L_INTERPOL];
149 Word32 *exc;
150 Word32 lsp_old[M];
151
152
153 /* Filter's memory */
154 Word32 mem_syn[M];
155
156
157 /* pitch sharpening */
158 Word32 sharp;
159 Word32 old_T0;
160
161
162 /* Variable holding received ltpLag, used in background noise and BFI */
163 Word32 T0_lagBuff;
164
165
166 /* Variables for the source characteristic detector (SCD) */
167 Word32 inBackgroundNoise;
168 Word32 voicedHangover;
169 Word32 ltpGainHistory[9];
170
171
172 /* Memories for bad frame handling */
173 Word32 excEnergyHist[9];
174 Word16 prev_bf;
175 Word16 prev_pdf;
176 Word16 state;
177 Word16 nodataSeed;
178
179
180 Bgn_scdState * background_state;
181 Cb_gain_averageState * Cb_gain_averState;
182 lsp_avgState * lsp_avg_st;
183 D_plsfState * lsfState;
184 ec_gain_pitchState * ec_gain_p_st;
185 ec_gain_codeState * ec_gain_c_st;
186 gc_predState * pred_state;
187 ph_dispState * ph_disp_st;
188 dtx_decState * dtxDecoderState;
189} Decoder_amrState;
190typedef struct {
191 Word32 res2[L_SUBFR];
192 Word32 mem_syn_pst[M];
193 Word32 synth_buf[M + L_FRAME];
194 Word32 preemph_state_mem_pre;
195 agcState * agc_state;
196} Post_FilterState;
197typedef struct {
198 Word32 y2_hi;
199 Word32 y2_lo;
200 Word32 y1_hi;
201 Word32 y1_lo;
202 Word32 x0;
203 Word32 x1;
204
205
206} Post_ProcessState;
207typedef struct {
208 Decoder_amrState * decoder_amrState;
209 Post_FilterState * post_state;
210 Post_ProcessState * postHP_state;
211} Speech_Decode_FrameState;
212
213
214/*
215 * CodAmrReset
216 *
217 *
218 * Parameters:
219 * state B: state structure
220 * mode I: AMR mode
221 *
222 * Function:
223 * Resets state memory
224 *
225 * Returns:
226 * void
227 */
228static void Decoder_amr_reset(Decoder_amrState *state, enum Mode mode)
229{
230 Word32 i;
231
232 /* Cb_gain_average_reset */
233 memset(state->Cb_gain_averState->cbGainHistory, 0, L_CBGAINHIST << 2);
234 state->Cb_gain_averState->hangVar = 0;
235 state->Cb_gain_averState->hangCount = 0;
236
237 /* Initialize static pointer */
238 state->exc = state->old_exc + PIT_MAX + L_INTERPOL;
239
240 /* Static vectors to zero */
241 memset(state->old_exc, 0, (PIT_MAX + L_INTERPOL) << 2);
242
243 if (mode != MRDTX) {
244 memset(state->mem_syn, 0, M << 2);
245 }
246
247 /* initialize pitch sharpening */
248 state->sharp = SHARPMIN;
249 state->old_T0 = 40;
250
251 /* Initialize state->lsp_old [] */
252 if (mode != MRDTX) {
253 state->lsp_old[0] = 30000;
254 state->lsp_old[1] = 26000;
255 state->lsp_old[2] = 21000;
256 state->lsp_old[3] = 15000;
257 state->lsp_old[4] = 8000;
258 state->lsp_old[5] = 0;
259 state->lsp_old[6] = -8000;
260 state->lsp_old[7] = -15000;
261 state->lsp_old[8] = -21000;
262 state->lsp_old[9] = -26000;
263 }
264
265 /* Initialize memories of bad frame handling */
266 state->prev_bf = 0;
267 state->prev_pdf = 0;
268 state->state = 0;
269 state->T0_lagBuff = 40;
270 state->inBackgroundNoise = 0;
271 state->voicedHangover = 0;
272
273 if (mode != MRDTX) {
274 memset(state->excEnergyHist, 0, 9 << 2);
275 }
276 memset(state->ltpGainHistory, 0, 9 << 2);
277
278 if (mode != MRDTX) {
279 state->lsp_avg_st->lsp_meanSave[0] = 1384;
280 state->lsp_avg_st->lsp_meanSave[1] = 2077;
281 state->lsp_avg_st->lsp_meanSave[2] = 3420;
282 state->lsp_avg_st->lsp_meanSave[3] = 5108;
283 state->lsp_avg_st->lsp_meanSave[4] = 6742;
284 state->lsp_avg_st->lsp_meanSave[5] = 8122;
285 state->lsp_avg_st->lsp_meanSave[6] = 9863;
286 state->lsp_avg_st->lsp_meanSave[7] = 11092;
287 state->lsp_avg_st->lsp_meanSave[8] = 12714;
288 state->lsp_avg_st->lsp_meanSave[9] = 13701;
289 }
290 memset(state->lsfState->past_r_q, 0, M << 2);
291
292 /* Past dequantized lsfs */
293 state->lsfState->past_lsf_q[0] = 1384;
294 state->lsfState->past_lsf_q[1] = 2077;
295 state->lsfState->past_lsf_q[2] = 3420;
296 state->lsfState->past_lsf_q[3] = 5108;
297 state->lsfState->past_lsf_q[4] = 6742;
298 state->lsfState->past_lsf_q[5] = 8122;
299 state->lsfState->past_lsf_q[6] = 9863;
300 state->lsfState->past_lsf_q[7] = 11092;
301 state->lsfState->past_lsf_q[8] = 12714;
302 state->lsfState->past_lsf_q[9] = 13701;
303
304 for (i = 0; i < 5; i++) {
305 state->ec_gain_p_st->pbuf[i] = 1640;
306 }
307 state->ec_gain_p_st->past_gain_pit = 0;
308 state->ec_gain_p_st->prev_gp = 16384;
309
310 for (i = 0; i < 5; i++) {
311 state->ec_gain_c_st->gbuf[i] = 1;
312 }
313 state->ec_gain_c_st->past_gain_code = 0;
314 state->ec_gain_c_st->prev_gc = 1;
315
316 if (mode != MRDTX) {
317 for (i = 0; i < NPRED; i++) {
318 state->pred_state->past_qua_en[i] = MIN_ENERGY;
319 state->pred_state->past_qua_en_MR122[i] = MIN_ENERGY_MR122;
320 }
321 }
322 state->nodataSeed = 21845;
323
324 /* Static vectors to zero */
325 memset(state->background_state->frameEnergyHist, 0, L_ENERGYHIST << 2);
326
327 /* Initialize hangover handling */
328 state->background_state->bgHangover = 0;
329
330 /* phDispReset */
331 memset(state->ph_disp_st->gainMem, 0, PHDGAINMEMSIZE << 2);
332 state->ph_disp_st->prevState = 0;
333 state->ph_disp_st->prevCbGain = 0;
334 state->ph_disp_st->lockFull = 0;
335 state->ph_disp_st->onset = 0; /* assume no onset in start */
336
337 if (mode != MRDTX) {
338 state->dtxDecoderState->since_last_sid = 0;
339 state->dtxDecoderState->true_sid_period_inv = 8192;
340 state->dtxDecoderState->log_en = 3500;
341 state->dtxDecoderState->old_log_en = 3500;
342
343 /* low level noise for better performance in DTX handover cases*/
344 state->dtxDecoderState->pn_seed_rx = PN_INITIAL_SEED;
345
346 /* Initialize state->lsp [] */
347 state->dtxDecoderState->lsp[0] = 30000;
348 state->dtxDecoderState->lsp[1] = 26000;
349 state->dtxDecoderState->lsp[2] = 21000;
350 state->dtxDecoderState->lsp[3] = 15000;
351 state->dtxDecoderState->lsp[4] = 8000;
352 state->dtxDecoderState->lsp[5] = 0;
353 state->dtxDecoderState->lsp[6] = -8000;
354 state->dtxDecoderState->lsp[7] = -15000;
355 state->dtxDecoderState->lsp[8] = -21000;
356 state->dtxDecoderState->lsp[9] = -26000;
357
358 /* Initialize state->lsp_old [] */
359 state->dtxDecoderState->lsp_old[0] = 30000;
360 state->dtxDecoderState->lsp_old[1] = 26000;
361 state->dtxDecoderState->lsp_old[2] = 21000;
362 state->dtxDecoderState->lsp_old[3] = 15000;
363 state->dtxDecoderState->lsp_old[4] = 8000;
364 state->dtxDecoderState->lsp_old[5] = 0;
365 state->dtxDecoderState->lsp_old[6] = -8000;
366 state->dtxDecoderState->lsp_old[7] = -15000;
367 state->dtxDecoderState->lsp_old[8] = -21000;
368 state->dtxDecoderState->lsp_old[9] = -26000;
369 state->dtxDecoderState->lsf_hist_ptr = 0;
370 state->dtxDecoderState->log_pg_mean = 0;
371 state->dtxDecoderState->log_en_hist_ptr = 0;
372
373 /* initialize decoder lsf history */
374 state->dtxDecoderState->lsf_hist[0] = 1384;
375 state->dtxDecoderState->lsf_hist[1] = 2077;
376 state->dtxDecoderState->lsf_hist[2] = 3420;
377 state->dtxDecoderState->lsf_hist[3] = 5108;
378 state->dtxDecoderState->lsf_hist[4] = 6742;
379 state->dtxDecoderState->lsf_hist[5] = 8122;
380 state->dtxDecoderState->lsf_hist[6] = 9863;
381 state->dtxDecoderState->lsf_hist[7] = 11092;
382 state->dtxDecoderState->lsf_hist[8] = 12714;
383 state->dtxDecoderState->lsf_hist[9] = 13701;
384
385 for (i = 1; i < DTX_HIST_SIZE; i++) {
386 memcpy(&state->dtxDecoderState->lsf_hist[M * i], &state->
387 dtxDecoderState->lsf_hist[0], M << 2);
388 }
389 memset(state->dtxDecoderState->lsf_hist_mean, 0, M * DTX_HIST_SIZE << 2);
390
391 /* initialize decoder log frame energy */
392 for (i = 0; i < DTX_HIST_SIZE; i++) {
393 state->dtxDecoderState->log_en_hist[i] = state->dtxDecoderState->log_en
394 ;
395 }
396 state->dtxDecoderState->log_en_adjust = 0;
397 state->dtxDecoderState->dtxHangoverCount = DTX_HANG_CONST;
398 state->dtxDecoderState->decAnaElapsedCount = 31;
399 state->dtxDecoderState->sid_frame = 0;
400 state->dtxDecoderState->valid_data = 0;
401 state->dtxDecoderState->dtxHangoverAdded = 0;
402 state->dtxDecoderState->dtxGlobalState = DTX;
403 state->dtxDecoderState->data_updated = 0;
404 }
405 return;
406}
407
408
409/*
410 * rx_dtx_handler
411 *
412 *
413 * Parameters:
414 * st->dtxGlobalState I: DTX state
415 * st->since_last_sid B: Frames after last SID frame
416 * st->data_updated I: SID update flag
417 * st->decAnaElapsedCount B: state machine that synch with the GSMEFR txDtx machine
418 * st->dtxHangoverAdded B: DTX hangover
419 * st->sid_frame O: SID frame indicator
420 * st->valid_data O: Vaild data indicator
421 * frame_type O: Frame type
422 *
423 * Function:
424 * Find the new DTX state
425 *
426 * Returns:
427 * DTXStateType DTX, DTX_MUTE or SPEECH
428 */
429static enum DTXStateType rx_dtx_handler(dtx_decState *st, enum RXFrameType frame_type)
430{
431 enum DTXStateType newState;
432 enum DTXStateType encState;
433
434 /* DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH) */
435 if (table_SID[frame_type] | ((st->dtxGlobalState != SPEECH) &
436 table_speech_bad[frame_type])) {
437 newState = DTX;
438
439 /* stay in mute for these input types */
440 if ((st->dtxGlobalState == DTX_MUTE) & table_mute[frame_type]) {
441 newState = DTX_MUTE;
442 }
443
444 /*
445 * evaluate if noise parameters are too old
446 * since_last_sid is reset when CN parameters have been updated
447 */
448 st->since_last_sid += 1;
449
450 /* no update of sid parameters in DTX for a long while */
451 if ((frame_type != RX_SID_UPDATE) & (st->since_last_sid > DTX_MAX_EMPTY_THRESH)) {
452 newState = DTX_MUTE;
453 }
454 } else {
455 newState = SPEECH;
456 st->since_last_sid = 0;
457 }
458
459 /*
460 * reset the decAnaElapsed Counter when receiving CNI data the first
461 * time, to robustify counter missmatch after handover
462 * this might delay the bwd CNI analysis in the new decoder slightly.
463 */
464 if ((st->data_updated == 0) & (frame_type == RX_SID_UPDATE)) {
465 st->decAnaElapsedCount = 0;
466 }
467
468 /*
469 * update the SPE-SPD DTX hangover synchronization
470 * to know when SPE has added dtx hangover
471 */
472 st->decAnaElapsedCount += 1;
473 st->dtxHangoverAdded = 0;
474 encState = SPEECH;
475
476 if (table_DTX[frame_type]) {
477 encState = DTX;
478 if ((frame_type == RX_NO_DATA) & (newState == SPEECH)) {
479 encState = SPEECH;
480 }
481 }
482
483 if (encState == SPEECH) {
484 st->dtxHangoverCount = DTX_HANG_CONST;
485 } else {
486 if (st->decAnaElapsedCount > DTX_ELAPSED_FRAMES_THRESH) {
487 st->dtxHangoverAdded = 1;
488 st->decAnaElapsedCount = 0;
489 st->dtxHangoverCount = 0;
490 } else if (st->dtxHangoverCount == 0) {
491 st->decAnaElapsedCount = 0;
492 } else {
493 st->dtxHangoverCount -= 1;
494 }
495 }
496
497 if (newState != SPEECH) {
498 /*
499 * DTX or DTX_MUTE
500 * CN data is not in a first SID, first SIDs are marked as SID_BAD
501 * but will do backwards analysis if a hangover period has been added
502 * according to the state machine above
503 */
504 st->sid_frame = 0;
505 st->valid_data = 0;
506
507 if (frame_type == RX_SID_FIRST) {
508 st->sid_frame = 1;
509 } else if (frame_type == RX_SID_UPDATE) {
510 st->sid_frame = 1;
511 st->valid_data = 1;
512 } else if (frame_type == RX_SID_BAD) {
513 st->sid_frame = 1;
514
515 /* use old data */
516 st->dtxHangoverAdded = 0;
517 }
518 }
519
520 /* newState is used by both SPEECH AND DTX synthesis routines */
521 return newState;
522}
523
524
525/*
526 * Lsf_lsp
527 *
528 *
529 * Parameters:
530 * lsf I: vector of LSFs
531 * lsp O: vector of LSPs
532 *
533 * Function:
534 * Transformation lsf to lsp, order M
535 *
536 * Returns:
537 * void
538 */
539static void Lsf_lsp(Word32 lsf[], Word32 lsp[])
540{
541 Word32 i, ind, offset, tmp;
542
543
544 for (i = 0; i < M; i++) {
545 /* ind = b8-b15 of lsf[i] */
546 ind = lsf[i] >> 8;
547
548 /* offset = b0-b7 of lsf[i] */
549 offset = lsf[i] & 0x00ff;
550
551 /* lsp[i] = table[ind]+ ((table[ind+1]-table[ind])*offset) / 256 */
552 tmp = ((cos_table[ind + 1] - cos_table[ind]) * offset) << 1;
553 lsp[i] = cos_table[ind] + (tmp >> 9);
554 }
555 return;
556}
557
558
559/*
560 * D_plsf_3
561 *
562 *
563 * Parameters:
564 * st->past_lsf_q I: Past dequantized LFSs
565 * st->past_r_q B: past quantized residual
566 * mode I: AMR mode
567 * bfi B: bad frame indicator
568 * indice I: quantization indices of 3 submatrices, Q0
569 * lsp1_q O: quantized 1st LSP vector
570 *
571 * Function:
572 * Decodes the LSP parameters using the received quantization indices.
573 * 1st order MA prediction and split by 3 vector quantization (split-VQ)
574 *
575 * Returns:
576 * void
577 */
578static void D_plsf_3(D_plsfState *st, enum Mode mode, Word16 bfi, Word16 *
579 indice, Word32 *lsp1_q)
580{
581 Word32 lsf1_r[M], lsf1_q[M];
582 Word32 i, index, temp;
583 const Word32 *p_cb1, *p_cb2, *p_cb3, *p_dico;
584
585
586 /* if bad frame */
587 if (bfi != 0) {
588 /* use the past LSFs slightly shifted towards their mean */
589 for (i = 0; i < M; i++) {
590 /* lsfi_q[i] = ALPHA*past_lsf_q[i] + ONE_ALPHA*meanLsf[i]; */
591 lsf1_q[i] = ((st->past_lsf_q[i] * ALPHA) >> 15) + ((mean_lsf_3[i]
592 * ONE_ALPHA) >> 15);
593 }
594
595 /* estimate past quantized residual to be used in next frame */
596 if (mode != MRDTX) {
597 for (i = 0; i < M; i++) {
598 /* temp = meanLsf[i] + pastR2_q[i] * pred_fac; */
599 temp = mean_lsf_3[i] + ((st->past_r_q[i] * pred_fac[i]) >> 15);
600 st->past_r_q[i] = lsf1_q[i] - temp;
601 }
602 } else {
603 for (i = 0; i < M; i++) {
604 /* temp = meanLsf[i] + pastR2_q[i]; */
605 temp = mean_lsf_3[i] + st->past_r_q[i];
606 st->past_r_q[i] = lsf1_q[i] - temp;
607 }
608 }
609 }
610
611 /* if good LSFs received */
612 else {
613 if ((mode == MR475) | (mode == MR515)) {
614 /* MR475, MR515 */
615 p_cb1 = dico1_lsf_3;
616 p_cb2 = dico2_lsf_3;
617 p_cb3 = mr515_3_lsf;
618 } else if (mode == MR795) {
619 /* MR795 */
620 p_cb1 = mr795_1_lsf;
621 p_cb2 = dico2_lsf_3;
622 p_cb3 = dico3_lsf_3;
623 } else {
624 /* MR59, MR67, MR74, MR102, MRDTX */
625 p_cb1 = dico1_lsf_3;
626 p_cb2 = dico2_lsf_3;
627 p_cb3 = dico3_lsf_3;
628 }
629
630 /* decode prediction residuals from 3 received indices */
631 index = *indice++;
632 p_dico = &p_cb1[index + index + index];
633 index = *indice++;
634 lsf1_r[0] = *p_dico++;
635 lsf1_r[1] = *p_dico++;
636 lsf1_r[2] = *p_dico++;
637
638 if ((mode == MR475) | (mode == MR515)) {
639 /* MR475, MR515 only using every second entry */
640 index = index << 1;
641 }
642 p_dico = &p_cb2[index + index + index];
643 index = *indice++;
644 lsf1_r[3] = *p_dico++;
645 lsf1_r[4] = *p_dico++;
646 lsf1_r[5] = *p_dico++;
647 p_dico = &p_cb3[index << 2];
648 lsf1_r[6] = *p_dico++;
649 lsf1_r[7] = *p_dico++;
650 lsf1_r[8] = *p_dico++;
651 lsf1_r[9] = *p_dico++;
652
653 /* Compute quantized LSFs and update the past quantized residual */
654 if (mode != MRDTX) {
655 for (i = 0; i < M; i++) {
656 lsf1_q[i] = lsf1_r[i] + (mean_lsf_3[i] + ((st->past_r_q[i] *
657 pred_fac[i]) >> 15));
658 }
659 memcpy(st->past_r_q, lsf1_r, M << 2);
660 } else {
661 for (i = 0; i < M; i++) {
662 lsf1_q[i] = lsf1_r[i] + (mean_lsf_3[i] + st->past_r_q[i]);
663 }
664 memcpy(st->past_r_q, lsf1_r, M << 2);
665 }
666 }
667
668 /* verification that LSFs has minimum distance of LSF_GAP Hz */
669 temp = LSF_GAP;
670
671 for (i = 0; i < M; i++) {
672 if (lsf1_q[i] < temp) {
673 lsf1_q[i] = temp;
674 }
675 temp = lsf1_q[i] + LSF_GAP;
676 }
677 memcpy(st->past_lsf_q, lsf1_q, M << 2);
678
679 /* convert LSFs to the cosine domain */
680 Lsf_lsp(lsf1_q, lsp1_q);
681 return;
682}
683
684
685/*
686 * pseudonoise
687 *
688 *
689 * Parameters:
690 * shift_reg B: Old CN generator shift register state
691 * no_bits I: Number of bits
692 *
693 * Function:
694 * pseudonoise
695 *
696 * Returns:
697 * noise_bits
698 */
699static Word32 pseudonoise(Word32 *shift_reg, Word32 no_bits)
700{
701 Word32 noise_bits, Sn, i;
702 Word32 s_reg;
703
704
705 s_reg = *shift_reg;
706 noise_bits = 0;
707
708 for (i = 0; i < no_bits; i++) {
709 /* State n == 31 */
710 Sn = s_reg & 0x00000001L;
711
712 /* State n == 3 */
713 if (s_reg & 0x10000000L) {
714 Sn = Sn ^ 0x1L;
715 } else {
716 Sn = Sn ^ 0x0L;
717 }
718 noise_bits = (noise_bits << 1) | (s_reg & 1);
719 s_reg = s_reg >> 1;
720
721 if (Sn & 1) {
722 s_reg = s_reg | 0x40000000L;
723 }
724 }
725 *shift_reg = s_reg;
726 return noise_bits;
727}
728
729
730/*
731 * Lsp_lsf
732 *
733 *
734 * Parameters:
735 * lsp I: LSP vector (range: -1<=val<1)
736 * lsf O: LSF vector Old CN generator shift register state
737 *
738 * Function:
739 * Transformation lsp to lsf, LPC order M
740 * lsf[i] = arccos(lsp[i])/(2*pi)
741 *
742 * Returns:
743 * void
744 */
745static void Lsp_lsf(Word32 lsp[], Word32 lsf[])
746{
747 Word32 i, ind = 63; /* begin at end of table -1 */
748
749
750 for (i = M - 1; i >= 0; i--) {
751 /* find value in table that is just greater than lsp[i] */
752 while (cos_table[ind] < lsp[i]) {
753 ind--;
754 }
755 lsf[i] = ((((lsp[i] - cos_table[ind]) * acos_slope[ind]) + 0x800)
756 >> 12) + (ind << 8);
757 }
758 return;
759}
760
761
762/*
763 * Reorder_lsf
764 *
765 *
766 * Parameters:
767 * lsf B: vector of LSFs (range: 0<=val<=0.5)
768 * min_dist I: minimum required distance
769 *
770 * Function:
771 * Make sure that the LSFs are properly ordered and to keep a certain minimum
772 * distance between adjacent LSFs. LPC order = M.
773 *
774 * Returns:
775 * void
776 */
777static void Reorder_lsf(Word32 *lsf, Word32 min_dist)
778{
779 Word32 lsf_min, i;
780
781
782 lsf_min = min_dist;
783
784 for (i = 0; i < M; i++) {
785 if (lsf[i] < lsf_min) {
786 lsf[i] = lsf_min;
787 }
788 lsf_min = lsf[i] + min_dist;
789 }
790}
791
792/* VC5.0 Global optimization does not work with this function */
793#if _MSC_VER == 1100
794#pragma optimize( "g", off )
795#endif
796/*
797 * Get_lsp_pol
798 *
799 *
800 * Parameters:
801 * lsp I: line spectral frequencies
802 * f O: polynomial F1(z) or F2(z)
803 *
804 * Function:
805 * Find the polynomial F1(z) or F2(z) from the LSPs.
806 *
807 * F1(z) = product ( 1 - 2 lsp[i] z^-1 + z^-2 )
808 * i=0,2,4,6,8
809 * F2(z) = product ( 1 - 2 lsp[i] z^-1 + z^-2 )
810 * i=1,3,5,7,9
811 *
812 * where lsp[] is the LSP vector in the cosine domain.
813 *
814 * The expansion is performed using the following recursion:
815 *
816 * f[0] = 1
817 * b = -2.0 * lsp[0]
818 * f[1] = b
819 * for i=2 to 5 do
820 * b = -2.0 * lsp[2*i-2];
821 * f[i] = b*f[i-1] + 2.0*f[i-2];
822 * for j=i-1 down to 2 do
823 * f[j] = f[j] + b*f[j-1] + f[j-2];
824 * f[1] = f[1] + b;
825 *
826 * Returns:
827 * void
828 */
829static void Get_lsp_pol(Word32 *lsp, Word32 *f)
830{
831 Word32 f0, f1, f2, f3, f4, f5;
832 Word32 l1, l2, l3, l4;
833
834 /* f[0] = 1.0; */
835 f0 = 16777216L;
836
837 /* f1 = *lsp * -1024; */
838 f1 = -lsp[0] << 10;
839 l1 = lsp[2];
840 l2 = lsp[4];
841 l3 = lsp[6];
842 l4 = lsp[8];
843 f2 = f0 << 1;
844 f2 -= (((f1 >> 16) * l1) + (((f1 & 0xFFFE) * l1) >> 16)) << 2;
845 f1 -= l1 << 10;
846 f3 = f1 << 1;
847 f3 -= (((f2 >> 16) * l2) + (((f2 & 0xFFFE) * l2) >> 16)) << 2;
848 f2 += f0;
849 f2 -= (((f1 >> 16) * l2) + (((f1 & 0xFFFE) * l2) >> 16)) << 2;
850 f1 -= l2 << 10;
851 f4 = f2 << 1;
852 f4 -= (((f3 >> 16) * l3) + (((f3 & 0xFFFE) * l3) >> 16)) << 2;
853 f3 += f1;
854 f3 -= (((f2 >> 16) * l3) + (((f2 & 0xFFFE) * l3) >> 16)) << 2;
855 f2 += f0;
856 f2 -= (((f1 >> 16) * l3) + (((f1 & 0xFFFE) * l3) >> 16)) << 2;
857 f1 -= l3 << 10;
858 f5 = f3 << 1;
859 f5 -= (((f4 >> 16) * l4) + (((f4 & 0xFFFE) * l4) >> 16)) << 2;
860 f4 += f2;
861 f4 -= (((f3 >> 16) * l4) + (((f3 & 0xFFFE) * l4) >> 16)) << 2;
862 f3 += f1;
863 f3 -= (((f2 >> 16) * l4) + (((f2 & 0xFFFE) * l4) >> 16)) << 2;
864 f2 += f0;
865 f2 -= (((f1 >> 16) * l4) + (((f1 & 0xFFFE) * l4) >> 16)) << 2;
866 f1 -= l4 << 10;
867 f[0] = f0;
868 f[1] = f1;
869 f[2] = f2;
870 f[3] = f3;
871 f[4] = f4;
872 f[5] = f5;
873
874 return;
875}
876#if _MSC_VER == 1100
877#pragma optimize( "", on )
878#endif
879
880
881/*
882 * Lsp_Az
883 *
884 *
885 * Parameters:
886 * lsp I: Line spectral frequencies
887 * a O: Predictor coefficients
888 *
889 * Function:
890 * Converts from the line spectral pairs (LSP) to LP coefficients,
891 * for a 10th order filter.
892 *
893 * Find the coefficients of F1(z) and F2(z)
894 * Multiply F1(z) by 1+z^{-1} and F2(z) by 1-z^{-1}
895 * A(z) = ( F1(z) + F2(z) ) / 2
896 *
897 * Returns:
898 * void
899 */
900static void Lsp_Az(Word32 lsp[], Word32 a[])
901{
902 Word32 f1[6], f2[6];
903 Word32 T0, i, j;
904
905
906 Get_lsp_pol(&lsp[0], f1);
907 Get_lsp_pol(&lsp[1], f2);
908
909 for (i = 5; i > 0; i--) {
910 f1[i] += f1[i - 1];
911 f2[i] -= f2[i - 1];
912 }
913 a[0] = 4096;
914
915 for (i = 1, j = 10; i <= 5; i++, j--) {
916 T0 = f1[i] + f2[i];
917 a[i] = (Word16)(T0 >> 13); /* emulate fixed point bug */
918 if ((T0 & 4096) != 0) {
919 a[i]++;
920 }
921 T0 = f1[i] - f2[i];
922 a[j] = (Word16)(T0 >> 13); /* emulate fixed point bug */
923
924 if ((T0 & 4096) != 0) {
925 a[j]++;
926 }
927 }
928 return;
929}
930
931
932/*
933 * A_Refl
934 *
935 *
936 * Parameters:
937 * a I: Directform coefficients
938 * refl O: Reflection coefficients
939 *
940 * Function:
941 * Converts from the directform coefficients to reflection coefficients
942 *
943 * Returns:
944 * void
945 */
946static void A_Refl(Word32 a[], Word32 refl[])
947{
948 /* local variables */
949 int normShift;
950 Word32 aState[M], bState[M];
951 Word32 normProd, acc, temp, mult, scale, i, j;
952
953
954 /* initialize states */
955 memcpy(aState, a, M << 2);
956
957 /* backward Levinson recursion */
958 for (i = M - 1; i >= 0; i--) {
959 if (labs(aState[i]) >= 4096) {
960 goto ExitRefl;
961 }
962 refl[i] = aState[i] << 3;
963 temp = (refl[i] * refl[i]) << 1;
964 acc = (MAX_32 - temp);
965 normShift = 0;
966 if (acc != 0) {
967 temp = acc;
968 while (!(temp & 0x40000000)) {
969 normShift++;
970 temp = temp << 1;
971 }
972 } else {
973 normShift = 0;
974 }
975 scale = 15 - normShift;
976 acc = (acc << normShift);
977 temp = (acc + (Word32)0x00008000L);
978
979 if (temp > 0) {
980 normProd = temp >> 16;
981 mult = 0x20000000L / normProd;
982 } else {
983 mult = 16384;
984 }
985
986 for (j = 0; j < i; j++) {
987 acc = aState[j] << 16;
988 acc -= (refl[i] * aState[i - j - 1]) << 1;
989 temp = (acc + (Word32)0x00008000L) >> 16;
990 temp = (mult * temp) << 1;
991
992 if (scale > 0) {
993 if ((temp & ((Word32)1 << (scale - 1))) != 0) {
994 temp = (temp >> scale) + 1;
995 } else {
996 temp = (temp >> scale);
997 }
998 } else {
999 temp = (temp >> scale);
1000 }
1001
1002 if (labs(temp) > 32767) {
1003 goto ExitRefl;
1004 }
1005 bState[j] = temp;
1006 }
1007 memcpy(aState, bState, i << 2);
1008 }
1009 return;
1010ExitRefl:
1011 memset(refl, 0, M << 2);
1012}
1013
1014
1015/*
1016 * Log2_norm
1017 *
1018 *
1019 * Parameters:
1020 * x I: input value
1021 * exp I: exponent
1022 * exponent O: Integer part of Log2. (range: 0<=val<=30)
1023 * fraction O: Fractional part of Log2. (range: 0<=val<1)
1024 *
1025 * Function:
1026 * Computes log2
1027 *
1028 * Computes log2(L_x, exp), where L_x is positive and
1029 * normalized, and exp is the normalisation exponent
1030 * If L_x is negative or zero, the result is 0.
1031 *
1032 * The function Log2(L_x) is approximated by a table and linear
1033 * interpolation. The following steps are used to compute Log2(L_x)
1034 *
1035 * exponent = 30-normExponent
1036 * i = bit25-b31 of L_x; 32<=i<=63 (because of normalization).
1037 * a = bit10-b24
1038 * i -=32
1039 * fraction = table[i]<<16 - (table[i] - table[i+1]) * a * 2
1040 *
1041 * Returns:
1042 * void
1043 */
1044static void Log2_norm(Word32 x, Word32 exp, Word32 *exponent, Word32 *
1045 fraction)
1046{
1047 Word32 y, i, a;
1048
1049
1050 if (x <= 0) {
1051 *exponent = 0;
1052 *fraction = 0;
1053 return;
1054 }
1055
1056 /* Extract b25-b31 */
1057 i = x >> 25;
1058 i = i - 32;
1059
1060 /* Extract b10-b24 of fraction */
1061 a = x >> 9;
1062 a = a & 0xFFFE; /* 2a */
1063
1064 /* fraction */
1065 y = (log2_table[i] << 16) - a * (log2_table[i] - log2_table[i + 1]);
1066 *fraction = y >> 16;
1067 *exponent = 30 - exp;
1068 return;
1069}
1070
1071
1072/*
1073 * Log2
1074 *
1075 *
1076 * Parameters:
1077 * x I: input value
1078 * exponent O: Integer part of Log2. (range: 0<=val<=30)
1079 * fraction O: Fractional part of Log2. (range: 0<=val<1)
1080 *
1081 * Function:
1082 * Computes log2(L_x)
1083 * If x is negative or zero, the result is 0.
1084 *
1085 * Returns:
1086 * void
1087 */
1088static void Log2(Word32 x, Word32 *exponent, Word32 *fraction)
1089{
1090 int tmp, exp = 0;
1091
1092 if (x != 0) {
1093 tmp = x;
1094 while (!((tmp & 0x80000000) ^((tmp & 0x40000000) << 1))) {
1095 exp++;
1096 tmp = tmp << 1;
1097 }
1098 }
1099 Log2_norm(x << exp, exp, exponent, fraction);
1100}
1101
1102
1103/*
1104 * Pow2
1105 *
1106 *
1107 * Parameters:
1108 * exponent I: Integer part. (range: 0<=val<=30)
1109 * fraction O: Fractional part. (range: 0.0<=val<1.0)
1110 *
1111 * Function:
1112 * pow(2.0, exponent.fraction)
1113 *
1114 * The function Pow2(L_x) is approximated by a table and linear interpolation.
1115 *
1116 * i = bit10-b15 of fraction, 0 <= i <= 31
1117 * a = biT0-b9 of fraction
1118 * x = table[i]<<16 - (table[i] - table[i+1]) * a * 2
1119 * x = L_x >> (30-exponent) (with rounding)
1120 *
1121 * Returns:
1122 * result (range: 0<=val<=0x7fffffff)
1123 */
1124static Word32 Pow2(Word32 exponent, Word32 fraction)
1125{
1126 Word32 i, a, tmp, x, exp;
1127
1128
1129 /* Extract b10-b16 of fraction */
1130 i = fraction >> 10;
1131
1132 /* Extract b0-b9 of fraction */
1133 a = (fraction << 5) & 0x7fff;
1134
1135 /* table[i] << 16 */
1136 x = pow2_table[i] << 16;
1137
1138 /* table[i] - table[i+1] */
1139 tmp = pow2_table[i] - pow2_table[i + 1];
1140
1141 /* L_x -= tmp*a*2 */
1142 x -= (tmp * a) << 1;
1143
1144 if (exponent >= -1) {
1145 exp = (30 - exponent);
1146
1147 /* Rounding */
1148 if ((x & ((Word32)1 << (exp - 1))) != 0) {
1149 x = (x >> exp) + 1;
1150 } else {
1151 x = x >> exp;
1152 }
1153 } else {
1154 x = 0;
1155 }
1156 return(x);
1157}
1158
1159
1160/*
1161 * Build_CN_code
1162 *
1163 *
1164 * Parameters:
1165 * seed B: Old CN generator shift register state
1166 * cod O: Generated CN fixed codebook vector
1167 *
1168 * Function:
1169 * Generate CN fixed codebook vector
1170 *
1171 * Returns:
1172 * void
1173 */
1174static void Build_CN_code(Word32 *seed, Word32 cod[])
1175{
1176 Word32 i, j, k;
1177
1178
1179 memset(cod, 0, L_SUBFR << 2);
1180
1181 for (k = 0; k < 10; k++) {
1182 i = pseudonoise(seed, 2); /* generate pulse position */
1183 i = (i * 20) >> 1;
1184 i = (i + k);
1185 j = pseudonoise(seed, 1); /* generate sign */
1186
1187 if (j > 0) {
1188 cod[i] = 4096;
1189 } else {
1190 cod[i] = -4096;
1191 }
1192 }
1193 return;
1194}
1195
1196
1197/*
1198 * Build_CN_param
1199 *
1200 *
1201 * Parameters:
1202 * seed B: Old CN generator shift register state
1203 * nParam I: number of params
1204 * paramSizeTable I: size of params
1205 * parm O: CN Generated params
1206 *
1207 * Function:
1208 * Generate parameters for comfort noise generation
1209 *
1210 * Returns:
1211 * void
1212 */
1213static void Build_CN_param(Word16 *seed, enum Mode mode, Word16 parm[])
1214{
1215 Word32 i;
1216 const Word32 *p;
1217
1218
1219 *seed = (Word16)((*seed * 31821) + 13849L);
1220 p = &window_200_40[ * seed & 0x7F];
1221
1222 switch (mode) {
1223 case MR122:
1224 for (i = 0; i < PRMNO_MR122; i++) {
1225 parm[i] = (Word16)(*p++ & ~(0xFFFF << bitno_MR122[i]));
1226 }
1227 break;
1228
1229 case MR102:
1230 for (i = 0; i < PRMNO_MR102; i++) {
1231 parm[i] = (Word16)(*p++ & ~(0xFFFF << bitno_MR102[i]));
1232 }
1233 break;
1234
1235 case MR795:
1236 for (i = 0; i < PRMNO_MR795; i++) {
1237 parm[i] = (Word16)(*p++ & ~(0xFFFF << bitno_MR795[i]));
1238 }
1239 break;
1240
1241 case MR74:
1242 for (i = 0; i < PRMNO_MR74; i++) {
1243 parm[i] = (Word16)(*p++ & ~(0xFFFF << bitno_MR74[i]));
1244 }
1245 break;
1246
1247 case MR67:
1248 for (i = 0; i < PRMNO_MR67; i++) {
1249 parm[i] = (Word16)(*p++ & ~(0xFFFF << bitno_MR67[i]));
1250 }
1251 break;
1252
1253 case MR59:
1254 for (i = 0; i < PRMNO_MR59; i++) {
1255 parm[i] = (Word16)(*p++ & ~(0xFFFF << bitno_MR59[i]));
1256 }
1257 break;
1258
1259 case MR515:
1260 for (i = 0; i < PRMNO_MR515; i++) {
1261 parm[i] = (Word16)(*p++ & ~(0xFFFF << bitno_MR515[i]));
1262 }
1263 break;
1264
1265 case MR475:
1266 for (i = 0; i < PRMNO_MR475; i++) {
1267 parm[i] = (Word16)(*p++ & ~(0xFFFF << bitno_MR475[i]));
1268 }
1269 break;
1270 }
1271}
1272
1273
1274/*
1275 * Syn_filt
1276 *
1277 *
1278 * Parameters:
1279 * a I: prediction coefficients [M+1]
1280 * x I: input signal
1281 * y O: output signal
1282 * lg I: size of filtering
1283 * mem B: memory associated with this filtering
1284 * update I: 0=no update, 1=update of memory.
1285 *
1286 * Function:
1287 * Perform synthesis filtering through 1/A(z).
1288 *
1289 * Returns:
1290 * void
1291 */
1292volatile int kk_entery = 0;
1293static Word32 Syn_filt(Word32 a[], Word32 x[], Word32 y[], Word32 lg, Word32 mem[]
1294 , Word32 update)
1295{
1296 Word32 tmp[50]; /* malloc is slow */
1297 Word32 s, a0, overflow = 0;
1298 Word32 *yy, *yy_limit;
1299
1300
1301 /* Copy mem[] to yy[] */
1302 memcpy(tmp, mem, 40);
1303 yy = tmp + M;
1304 yy_limit = yy + lg;
1305 a0 = a[0];
1306 kk_entery++;
1307 /* Do the filtering. */
1308 while (yy < yy_limit) {
1309
1310 s = *x++ * a0;
1311 s -= yy[-1] * a[1];
1312 s -= yy[-2] * a[2];
1313 s -= yy[-3] * a[3];
1314 s -= yy[-4] * a[4];
1315 s -= yy[-5] * a[5];
1316 s -= yy[-6] * a[6];
1317 s -= yy[-7] * a[7];
1318 s -= yy[-8] * a[8];
1319 s -= yy[-9] * a[9];
1320 s -= yy[-10] * a[10];
1321 if (labs(s) < 0x7ffffff) {
1322 *yy = (s + 0x800L) >> 12;
1323 } else if (s > 0) {
1324 *yy = 32767;
1325 overflow = 1;
1326 } else {
1327 *yy = -32768;
1328 overflow = 1;
1329 }
1330 yy++;
1331 }
1332 memcpy(y, &tmp[M], lg << 2);
1333
1334 /* Update of memory if update==1 */
1335 if (update) {
1336 memcpy(mem, &y[lg - M], 40);
1337 }
1338 return overflow;
1339}
1340
1341/*
1342 * Syn_filt_overflow
1343 *
1344 *
1345 * Parameters:
1346 * a I: prediction coefficients [M+1]
1347 * x I: input signal
1348 * y O: output signal
1349 * lg I: size of filtering
1350 * mem B: memory associated with this filtering
1351 * update I: 0=no update, 1=update of memory.
1352 *
1353 * Function:
1354 * Perform synthesis filtering through 1/A(z).
1355 * Saturate after every multiplication.
1356 * Returns:
1357 * void
1358 */
1359static void Syn_filt_overflow(Word32 a[], Word32 x[], Word32 y[], Word32 lg, Word32 mem[]
1360 , Word32 update)
1361{
1362 Word32 tmp[50]; /* malloc is slow */
1363 Word32 i, j, s, a0;
1364 Word32 *yy;
1365
1366
1367 /* Copy mem[] to yy[] */
1368 memcpy(tmp, mem, 40);
1369 yy = tmp + M;
1370 a0 = a[0];
1371
1372 /* Do the filtering. */
1373 for (i = 0; i < lg; i++) {
1374 s = x[i] * a0;
1375
1376 for (j = 1; j <= M; j++) {
1377 s -= a[j] * yy[ - j];
1378 if (s > 1073741823) {
1379 s = 1073741823;
1380 } else if (s < -1073741824) {
1381 s = -1073741824;
1382 }
1383 }
1384
1385 if (labs(s) < 0x7FFE800) {
1386 *yy = (s + 0x800L) >> 12;
1387 } else if (s > 0) {
1388 *yy = 32767;
1389 } else {
1390 *yy = -32768;
1391 }
1392 yy++;
1393 }
1394 memcpy(y, &tmp[M], lg << 2);
1395
1396 /* Update of memory if update==1 */
1397 if (update) {
1398 memcpy(mem, &y[lg - M], 40);
1399 }
1400 return;
1401}
1402
1403/*
1404 * dtx_dec
1405 *
1406 *
1407 * Parameters:
1408 * st B: DTX state struct
1409 * mem_syn I: AMR decoder state
1410 * lsfState B: LSF state struct
1411 * pred_state->past_qua_en O: table of past quantized energies
1412 * pred_state->past_qua_en_MR122 O: table of past quantized energies MR122
1413 * averState->hangVar O:
1414 * averState->hangCount O: hangover variable
1415 * new_state I: new DTX state
1416 * mode I: AMR mode
1417 * parm I: vector of synthesis parameters
1418 * synth O: synthesised speech
1419 * A_t O: decoded LP filter in 4 subframes
1420 *
1421 * Function:
1422 * DTX
1423 *
1424 * Returns:
1425 * void
1426 */
1427static void dtx_dec(dtx_decState *st, Word32 *mem_syn, D_plsfState *lsfState,
1428 gc_predState *pred_state, Cb_gain_averageState *averState, enum
1429 DTXStateType new_state, enum Mode mode, Word16 parm[], Word32 synth[],
1430 Word32 A_t[])
1431{
1432 Word32 ex[L_SUBFR], acoeff[11], acoeff_variab[M + 1], lsp_int[M];
1433 Word32 refl[M], lsf[M], lsf_int[M], lsf_int_variab[M], lsp_int_variab[M];
1434 Word32 i, j, int_fac, log_en_int, pred_err, log_pg_e, log_pg_m, log_pg;
1435 Word32 negative, lsf_mean, lsf_variab_index, lsf_variab_factor, ptr;
1436 Word16 log_en_index, log_en_int_e, log_en_int_m, level, ma_pred_init,
1437 tmp_int_length;
1438
1439
1440 if ((st->dtxHangoverAdded != 0) & (st->sid_frame != 0)) {
1441 /*
1442 * sidFirst after dtx hangover period
1443 * or sidUpd after dtxhangover
1444 */
1445 /* set log_en_adjust to correct value */
1446 st->log_en_adjust = dtx_log_en_adjust[mode];
1447 ptr = st->lsf_hist_ptr + M;
1448
1449 if (ptr == 80) {
1450 ptr = 0;
1451 }
1452 memcpy(&st->lsf_hist[ptr], &st->lsf_hist[st->lsf_hist_ptr], M << 2);
1453 ptr = st->log_en_hist_ptr + 1;
1454
1455 if (ptr == DTX_HIST_SIZE) {
1456 ptr = 0;
1457 }
1458 st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; /* Q11 */
1459
1460 /*
1461 * compute mean log energy and lsp
1462 * from decoded signal (SID_FIRST)
1463 */
1464 st->log_en = 0;
1465 memset(lsf, 0, M << 2);
1466
1467 /* average energy and lsp */
1468 for (i = 0; i < DTX_HIST_SIZE; i++) {
1469 st->log_en = st->log_en + (st->log_en_hist[i] >> 3);
1470
1471 for (j = 0; j < M; j++) {
1472 lsf[j] += st->lsf_hist[i * M + j];
1473 }
1474 }
1475
1476 for (j = 0; j < M; j++) {
1477 lsf[j] = lsf[j] >> 3; /* divide by 8 */
1478 }
1479 Lsf_lsp(lsf, st->lsp);
1480
1481 /*
1482 * make log_en speech coder mode independent
1483 * added again later before synthesis
1484 */
1485 st->log_en = st->log_en - st->log_en_adjust;
1486
1487 /* compute lsf variability vector */
1488 memcpy(st->lsf_hist_mean, st->lsf_hist, 80 << 2);
1489
1490 for (i = 0; i < M; i++) {
1491 lsf_mean = 0;
1492
1493 /* compute mean lsf */
1494 for (j = 0; j < 8; j++) {
1495 lsf_mean += st->lsf_hist_mean[i + j * M];
1496 }
1497 lsf_mean = lsf_mean >> 3;
1498
1499 /*
1500 * subtract mean and limit to within reasonable limits
1501 * moreover the upper lsf's are attenuated
1502 */
1503 for (j = 0; j < 8; j++) {
1504 /* subtract mean */
1505 st->lsf_hist_mean[i + j * M] = st->lsf_hist_mean[i + j * M] -
1506 lsf_mean;
1507
1508 /* attenuate deviation from mean, especially for upper lsf's */
1509 st->lsf_hist_mean[i + j * M] = (st->lsf_hist_mean[i + j * M] *
1510 lsf_hist_mean_scale[i]) >> 15;
1511
1512 /* limit the deviation */
1513 if (st->lsf_hist_mean[i + j * M] < 0) {
1514 negative = 1;
1515 } else {
1516 negative = 0;
1517 }
1518 st->lsf_hist_mean[i + j * M] = labs(st->lsf_hist_mean[i + j * M]);
1519
1520 /* apply soft limit */
1521 if (st->lsf_hist_mean[i + j * M] > 655) {
1522 st->lsf_hist_mean[i + j * M] = 655 + ((st->lsf_hist_mean[i + j
1523 * M] - 655) >> 2);
1524 }
1525
1526 /* apply hard limit */
1527 if (st->lsf_hist_mean[i + j * M] > 1310) {
1528 st->lsf_hist_mean[i + j * M] = 1310;
1529 }
1530
1531 if (negative != 0) {
1532 st->lsf_hist_mean[i + j * M] = -st->lsf_hist_mean[i + j * M];
1533 }
1534 }
1535 }
1536 }
1537
1538 if (st->sid_frame != 0) {
1539 /*
1540 * Set old SID parameters, always shift
1541 * even if there is no new valid_data
1542 */
1543 memcpy(st->lsp_old, st->lsp, M << 2);
1544 st->old_log_en = st->log_en;
1545
1546 if (st->valid_data != 0) { /* new data available (no CRC) */
1547 /* Compute interpolation factor, since the division only works
1548 * for values of since_last_sid < 32 we have to limit the
1549 * interpolation to 32 frames
1550 */
1551 tmp_int_length = st->since_last_sid;
1552 st->since_last_sid = 0;
1553
1554 if (tmp_int_length > 32) {
1555 tmp_int_length = 32;
1556 }
1557
1558 if (tmp_int_length >= 2) {
1559 st->true_sid_period_inv = 0x2000000 / (tmp_int_length
1560 << 10);
1561 } else {
1562 st->true_sid_period_inv = 16384; /* 0.5 it Q15 */
1563 }
1564 memcpy(lsfState->past_r_q, &past_rq_init[parm[0] * M], M << 2);
1565 D_plsf_3(lsfState, MRDTX, 0, &parm[1], st->lsp);
1566
1567 /* reset for next speech frame */
1568 memset(lsfState->past_r_q, 0, M << 2);
1569 log_en_index = parm[4];
1570
1571 /* Q11 and divide by 4 */
1572 st->log_en = (Word16)(log_en_index << 9);
1573
1574 /* Subtract 2.5 in Q11 */
1575 st->log_en = (Word16)(st->log_en - 5120);
1576
1577 /* Index 0 is reserved for silence */
1578 if (log_en_index == 0) {
1579 st->log_en = MIN_16;
1580 }
1581
1582 /*
1583 * no interpolation at startup after coder reset
1584 * or when SID_UPD has been received right after SPEECH
1585 */
1586 if ((st->data_updated == 0) || (st->dtxGlobalState == SPEECH)) {
1587 memcpy(st->lsp_old, st->lsp, M << 2);
1588 st->old_log_en = st->log_en;
1589 }
1590 } /* endif valid_data */
1591
1592 /* initialize gain predictor memory of other modes */
1593 ma_pred_init = (Word16)((st->log_en >> 1) - 9000);
1594
1595 if (ma_pred_init > 0) {
1596 ma_pred_init = 0;
1597 }
1598
1599 if (ma_pred_init < - 14436) {
1600 ma_pred_init = -14436;
1601 }
1602 pred_state->past_qua_en[0] = ma_pred_init;
1603 pred_state->past_qua_en[1] = ma_pred_init;
1604 pred_state->past_qua_en[2] = ma_pred_init;
1605 pred_state->past_qua_en[3] = ma_pred_init;
1606
1607 /* past_qua_en for other modes than MR122 */
1608 ma_pred_init = (Word16)((5443 * ma_pred_init) >> 15);
1609
1610 /* scale down by factor 20*log10(2) in Q15 */
1611 pred_state->past_qua_en_MR122[0] = ma_pred_init;
1612 pred_state->past_qua_en_MR122[1] = ma_pred_init;
1613 pred_state->past_qua_en_MR122[2] = ma_pred_init;
1614 pred_state->past_qua_en_MR122[3] = ma_pred_init;
1615 } /* endif sid_frame */
1616
1617 /*
1618 * CN generation
1619 * recompute level adjustment factor Q11
1620 * st->log_en_adjust = 0.9*st->log_en_adjust +
1621 * 0.1*dtx_log_en_adjust[mode]);
1622 */
1623 st->log_en_adjust = (Word16)(((st->log_en_adjust * 29491) >> 15) + ((
1624 (dtx_log_en_adjust[mode] << 5) * 3277) >> 20));
1625
1626 /* Interpolate SID info */
1627 /* Q10 */
1628 if (st->since_last_sid > 30) {
1629 int_fac = 32767;
1630 } else {
1631 int_fac = (Word16)((st->since_last_sid + 1) << 10);
1632 }
1633
1634 /* Q10 * Q15 -> Q10 */
1635 int_fac = (int_fac * st->true_sid_period_inv) >> 15;
1636
1637 /* Maximize to 1.0 in Q10 */
1638 if (int_fac > 1024) {
1639 int_fac = 1024;
1640 }
1641
1642 /* Q10 -> Q14 */
1643 int_fac = (Word16)(int_fac << 4);
1644
1645 /* Q14 * Q11->Q26 */
1646 log_en_int = (int_fac * st->log_en) << 1;
1647
1648 for (i = 0; i < M; i++) {
1649 /* Q14 * Q15 -> Q14 */
1650 lsp_int[i] = (int_fac * st->lsp[i]) >> 15;
1651 }
1652
1653 /* 1-k in Q14 */
1654 int_fac = 16384 - int_fac;
1655
1656 /* (Q14 * Q11 -> Q26) + Q26 -> Q26 */
1657 log_en_int += (int_fac * st->old_log_en) << 1;
1658
1659 for (i = 0; i < M; i++) {
1660 /* Q14 + (Q14 * Q15 -> Q14) -> Q14 */
1661 lsp_int[i] = lsp_int[i] + ((int_fac * st->lsp_old[i]) >> 15);
1662
1663 /* Q14 -> Q15 */
1664 lsp_int[i] = lsp_int[i] << 1;
1665 }
1666
1667 /* compute the amount of lsf variability */
1668 /* -0.6 in Q12 */
1669 lsf_variab_factor = st->log_pg_mean - 2457;
1670
1671 /* *0.3 Q12*Q15 -> Q12 */
1672 lsf_variab_factor = 4096 - ((lsf_variab_factor * 9830) >> 15);
1673
1674 /* limit to values between 0..1 in Q12 */
1675 if (lsf_variab_factor >= 4096) {
1676 lsf_variab_factor = 32767;
1677 } else if (lsf_variab_factor < 0) {
1678 lsf_variab_factor = 0;
1679 } else {
1680 lsf_variab_factor = lsf_variab_factor << 3; /* -> Q15 */
1681 }
1682
1683 /* get index of vector to do variability with */
1684 lsf_variab_index = pseudonoise(&st->pn_seed_rx, 3);
1685
1686 /* convert to lsf */
1687 Lsp_lsf(lsp_int, lsf_int);
1688
1689 /* apply lsf variability */
1690 memcpy(lsf_int_variab, lsf_int, M << 2);
1691
1692 for (i = 0; i < M; i++) {
1693 lsf_int_variab[i] = lsf_int_variab[i] + ((lsf_variab_factor * st->
1694 lsf_hist_mean[i + lsf_variab_index * M]) >> 15);
1695 }
1696
1697 /* make sure that LSP's are ordered */
1698 Reorder_lsf(lsf_int, LSF_GAP);
1699 Reorder_lsf(lsf_int_variab, LSF_GAP);
1700
1701 /* copy lsf to speech decoders lsf state */
1702 memcpy(lsfState->past_lsf_q, lsf_int, M << 2);
1703
1704 /* convert to lsp */
1705 Lsf_lsp(lsf_int, lsp_int);
1706 Lsf_lsp(lsf_int_variab, lsp_int_variab);
1707
1708 /* Compute acoeffs Q12 acoeff is used for level
1709 * normalization and Post_Filter, acoeff_variab is
1710 * used for synthesis filter
1711 * by doing this we make sure that the level
1712 * in high frequenncies does not jump up and down
1713 */
1714 Lsp_Az(lsp_int, acoeff);
1715 Lsp_Az(lsp_int_variab, acoeff_variab);
1716
1717 /* For use in Post_Filter */
1718 memcpy(&A_t[0], acoeff, MP1 << 2);
1719 memcpy(&A_t[MP1], acoeff, MP1 << 2);
1720 memcpy(&A_t[MP1 << 1], acoeff, MP1 << 2);
1721 memcpy(&A_t[MP1 + MP1 + MP1], acoeff, MP1 << 2);
1722
1723 /* Compute reflection coefficients Q15 */
1724 A_Refl(&acoeff[1], refl);
1725
1726 /* Compute prediction error in Q15 */
1727 /* 0.99997 in Q15 */
1728 pred_err = MAX_16;
1729
1730 for (i = 0; i < M; i++) {
1731 pred_err = (pred_err * (MAX_16 - ((refl[i] * refl[i]) >> 15))) >>
1732 15;
1733 }
1734
1735 /* compute logarithm of prediction gain */
1736 Log2(pred_err, &log_pg_e, &log_pg_m);
1737
1738 /* convert exponent and mantissa to Word16 Q12 */
1739 /* Q12 */
1740 log_pg = (log_pg_e - 15) << 12;
1741 /* saturate */
1742 if (log_pg < -32768) {
1743 log_pg = -32768;
1744 }
1745 log_pg = (-(log_pg + (log_pg_m >> 3))) >> 1;
1746 st->log_pg_mean = (Word16)(((29491 * st->log_pg_mean) >> 15) + ((3277
1747 * log_pg) >> 15));
1748
1749 /* Compute interpolated log energy */
1750 /* Q26 -> Q16 */
1751 log_en_int = log_en_int >> 10;
1752
1753 /* Add 4 in Q16 */
1754 log_en_int += 262144L;
1755
1756 /* subtract prediction gain */
1757 log_en_int = log_en_int - (log_pg << 4);
1758
1759 /* adjust level to speech coder mode */
1760 log_en_int += st->log_en_adjust << 5;
1761 log_en_int_e = (Word16)(log_en_int >> 16);
1762 log_en_int_m = (Word16)((log_en_int - (log_en_int_e << 16)) >> 1);
1763
1764 /* Q4 */
1765 level = (Word16)(Pow2(log_en_int_e, log_en_int_m));
1766
1767 for (i = 0; i < 4; i++) {
1768 /* Compute innovation vector */
1769 Build_CN_code(&st->pn_seed_rx, ex);
1770
1771 for (j = 0; j < L_SUBFR; j++) {
1772 ex[j] = (level * ex[j]) >> 15;
1773 }
1774
1775 /* Synthesize */
1776 Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR, mem_syn, 1);
1777 } /* next i */
1778
1779 /* reset codebook averaging variables */
1780 averState->hangVar = 20;
1781 averState->hangCount = 0;
1782
1783 if (new_state == DTX_MUTE) {
1784 /*
1785 * mute comfort noise as it has been quite a long time since
1786 * last SID update was performed
1787 */
1788 Word32 num, denom;
1789
1790
1791 tmp_int_length = st->since_last_sid;
1792
1793 if (tmp_int_length > 32) {
1794 tmp_int_length = 32;
1795 }
1796
1797 if (tmp_int_length == 1) {
1798 st->true_sid_period_inv = MAX_16;
1799 } else {
1800 num = 1024;
1801 denom = (tmp_int_length << 10);
1802 st->true_sid_period_inv = 0;
1803
1804 for (i = 0; i < 15; i++) {
1805 st->true_sid_period_inv <<= 1;
1806 num <<= 1;
1807
1808 if (num >= denom) {
1809 num = num - denom;
1810 st->true_sid_period_inv += 1;
1811 }
1812 }
1813 }
1814 st->since_last_sid = 0;
1815 memcpy(st->lsp_old, st->lsp, M << 2);
1816 st->old_log_en = st->log_en;
1817
1818 /* subtract 1/8 in Q11 i.e -6/8 dB */
1819 st->log_en = st->log_en - 256;
1820 if (st->log_en < -32768) {
1821 st->log_en = -32768;
1822 }
1823 }
1824
1825 /*
1826 * reset interpolation length timer
1827 * if data has been updated.
1828 */
1829 if ((st->sid_frame != 0) & ((st->valid_data != 0) || ((st->valid_data
1830 == 0) & (st->dtxHangoverAdded != 0)))) {
1831 st->since_last_sid = 0;
1832 st->data_updated = 1;
1833 }
1834 return;
1835}
1836
1837
1838/*
1839 * lsp_avg
1840 *
1841 *
1842 * Parameters:
1843 * st->lsp_meanSave B: LSP averages
1844 * lsp I: LSPs
1845 *
1846 * Function:
1847 * Calculate the LSP averages
1848 *
1849 * Returns:
1850 * void
1851 */
1852static void lsp_avg(lsp_avgState *st, Word32 *lsp)
1853{
1854 Word32 i, tmp;
1855
1856
1857 for (i = 0; i < M; i++) {
1858 /* mean = 0.84*mean */
1859 tmp = (st->lsp_meanSave[i] << 16);
1860 tmp -= (EXPCONST * st->lsp_meanSave[i]) << 1;
1861
1862 /* Add 0.16 of newest LSPs to mean */
1863 tmp += (EXPCONST * lsp[i]) << 1;
1864
1865 /* Save means */
1866 tmp += 0x00008000L;
1867 st->lsp_meanSave[i] = tmp >> 16;
1868 }
1869 return;
1870}
1871
1872
1873/*
1874 * Int_lpc_1and3
1875 *
1876 *
1877 * Parameters:
1878 * lsp_old I: LSP vector at the 4th subfr. of past frame [M]
1879 * lsp_mid I: LSP vector at the 2nd subframe of present frame [M]
1880 * lsp_new I: LSP vector at the 4th subframe of present frame [M]
1881 * Az O: interpolated LP parameters in subframes 1 and 3
1882 * [AZ_SIZE]
1883 *
1884 * Function:
1885 * Interpolates the LSPs and converts to LPC parameters
1886 * to get a different LP filter in each subframe.
1887 *
1888 * The 20 ms speech frame is divided into 4 subframes.
1889 * The LSPs are quantized and transmitted at the 2nd and
1890 * 4th subframes (twice per frame) and interpolated at the
1891 * 1st and 3rd subframe.
1892 *
1893 * Returns:
1894 * void
1895 */
1896static void Int_lpc_1and3(Word32 lsp_old[], Word32 lsp_mid[], Word32 lsp_new[],
1897 Word32 Az[])
1898{
1899 Word32 lsp[M];
1900 Word32 i;
1901
1902 Word32 *Az1;
1903 Az1 = Az;
1904 /* lsp[i] = lsp_mid[i] * 0.5 + lsp_old[i] * 0.5 */
1905 for (i = 0; i < 10; i++) {
1906 lsp[i] = (lsp_mid[i] >> 1) + (lsp_old[i] >> 1);
1907 }
1908
1909 /* Subframe 1 */
1910 Lsp_Az(lsp, Az);
1911 Az += MP1;
1912
1913 /* Subframe 2 */
1914 Lsp_Az(lsp_mid, Az);
1915 Az += MP1;
1916
1917 for (i = 0; i < 10; i++) {
1918 lsp[i] = (lsp_mid[i] >> 1) + (lsp_new[i] >> 1);
1919 }
1920
1921 /* Subframe 3 */
1922 Lsp_Az(lsp, Az);
1923 Az += MP1;
1924
1925 /* Subframe 4 */
1926 Lsp_Az(lsp_new, Az);
1927 return;
1928}
1929
1930
1931/*
1932 * Int_lpc_1to3
1933 *
1934 *
1935 * Parameters:
1936 * lsp_old I: LSP vector at the 4th subframe of past frame [M]
1937 * lsp_new I: LSP vector at the 4th subframe of present frame [M]
1938 * Az O: interpolated LP parameters in all subframes
1939 * [AZ_SIZE]
1940 *
1941 * Function:
1942 * Interpolates the LSPs and converts to LPC parameters to get a different
1943 * LP filter in each subframe.
1944 *
1945 * The 20 ms speech frame is divided into 4 subframes.
1946 * The LSPs are quantized and transmitted at the 4th
1947 * subframes (once per frame) and interpolated at the
1948 * 1st, 2nd and 3rd subframe.
1949 *
1950 * Returns:
1951 * void
1952 */
1953static void Int_lpc_1to3(Word32 lsp_old[], Word32 lsp_new[], Word32 Az[])
1954{
1955 Word32 lsp[M];
1956 Word32 i;
1957
1958
1959 for (i = 0; i < 10; i++) {
1960 lsp[i] = (lsp_new[i] >> 2) + (lsp_old[i] - (lsp_old[i] >> 2));
1961 }
1962
1963 /* Subframe 1 */
1964 Lsp_Az(lsp, Az);
1965 Az += MP1;
1966
1967 for (i = 0; i < 10; i++) {
1968 lsp[i] = (lsp_old[i] >> 1) + (lsp_new[i] >> 1);
1969 }
1970
1971 /* Subframe 2 */
1972 Lsp_Az(lsp, Az);
1973 Az += MP1;
1974
1975 for (i = 0; i < 10; i++) {
1976 lsp[i] = (lsp_old[i] >> 2) + (lsp_new[i] - (lsp_new[i] >> 2));
1977 }
1978
1979 /* Subframe 3 */
1980 Lsp_Az(lsp, Az);
1981 Az += MP1;
1982
1983 /* Subframe 4 */
1984 Lsp_Az(lsp_new, Az);
1985 return;
1986}
1987
1988
1989/*
1990 * D_plsf_5
1991 *
1992 *
1993 * Parameters:
1994 * st->past_lsf_q I: Past dequantized LFSs
1995 * st->past_r_q B: past quantized residual
1996 * bfi B: bad frame indicator
1997 * indice I: quantization indices of 3 submatrices, Q0
1998 * lsp1_q O: quantized 1st LSP vector
1999 * lsp2_q O: quantized 2nd LSP vector
2000 *
2001 * Function:
2002 * Decodes the 2 sets of LSP parameters in a frame
2003 * using the received quantization indices.
2004 *
2005 * Returns:
2006 * void
2007 */
2008static void D_plsf_5(D_plsfState *st, Word16 bfi, Word16 *indice, Word32 *lsp1_q
2009 , Word32 *lsp2_q)
2010{
2011 Word32 lsf1_r[M], lsf2_r[M], lsf1_q[M], lsf2_q[M];
2012 Word32 i, temp1, temp2, sign;
2013 const Word32 *p_dico;
2014
2015
2016 /* if bad frame */
2017 if (bfi != 0) {
2018 /* use the past LSFs slightly shifted towards their mean */
2019 for (i = 0; i < M; i += 2) {
2020 /* lsfi_q[i] = ALPHA*st->past_lsf_q[i] + ONE_ALPHA*meanLsf[i]; */
2021 lsf1_q[i] = ((st->past_lsf_q[i] * ALPHA_122) >> 15) + ((mean_lsf_5[i]
2022 * ONE_ALPHA_122) >> 15);
2023 lsf1_q[i + 1] = ((st->past_lsf_q[i + 1] * ALPHA_122) >> 15) + ((
2024 mean_lsf_5[i + 1] * ONE_ALPHA_122) >> 15);
2025 }
2026 memcpy(lsf2_q, lsf1_q, M << 2);
2027
2028 /* estimate past quantized residual to be used in next frame */
2029 for (i = 0; i < M; i += 2) {
2030 /* temp = meanLsf[i] + st->past_r_q[i] * LSPPpred_facMR122; */
2031 temp1 = mean_lsf_5[i] + ((st->past_r_q[i] * LSP_PRED_FAC_MR122) >>
2032 15);
2033 temp2 = mean_lsf_5[i + 1] + ((st->past_r_q[i + 1] * LSP_PRED_FAC_MR122
2034 ) >> 15);
2035 st->past_r_q[i] = lsf2_q[i] - temp1;
2036 st->past_r_q[i + 1] = lsf2_q[i + 1] - temp2;
2037 }
2038 }
2039
2040 /* if good LSFs received */
2041 else {
2042 /* decode prediction residuals from 5 received indices */
2043 p_dico = &dico1_lsf_5[indice[0] << 2];
2044 lsf1_r[0] = *p_dico++;
2045 lsf1_r[1] = *p_dico++;
2046 lsf2_r[0] = *p_dico++;
2047 lsf2_r[1] = *p_dico++;
2048 p_dico = &dico2_lsf_5[indice[1] << 2];
2049 lsf1_r[2] = *p_dico++;
2050 lsf1_r[3] = *p_dico++;
2051 lsf2_r[2] = *p_dico++;
2052 lsf2_r[3] = *p_dico++;
2053 sign = (Word16)(indice[2] & 1);
2054 i = indice[2] >> 1;
2055 p_dico = &dico3_lsf_5[i << 2];
2056
2057 if (sign == 0) {
2058 lsf1_r[4] = *p_dico++;
2059 lsf1_r[5] = *p_dico++;
2060 lsf2_r[4] = *p_dico++;
2061 lsf2_r[5] = *p_dico++;
2062 } else {
2063 lsf1_r[4] = (Word16)(-(*p_dico++));
2064 lsf1_r[5] = (Word16)(-(*p_dico++));
2065 lsf2_r[4] = (Word16)(-(*p_dico++));
2066 lsf2_r[5] = (Word16)(-(*p_dico++));
2067 }
2068 p_dico = &dico4_lsf_5[(indice[3] << 2)];
2069 lsf1_r[6] = *p_dico++;
2070 lsf1_r[7] = *p_dico++;
2071 lsf2_r[6] = *p_dico++;
2072 lsf2_r[7] = *p_dico++;
2073 p_dico = &dico5_lsf_5[(indice[4] << 2)];
2074 lsf1_r[8] = *p_dico++;
2075 lsf1_r[9] = *p_dico++;
2076 lsf2_r[8] = *p_dico++;
2077 lsf2_r[9] = *p_dico++;
2078
2079 /* Compute quantized LSFs and update the past quantized residual */
2080 for (i = 0; i < M; i++) {
2081 temp1 = mean_lsf_5[i] + ((st->past_r_q[i] * LSP_PRED_FAC_MR122) >>
2082 15);
2083 lsf1_q[i] = lsf1_r[i] + temp1;
2084 lsf2_q[i] = lsf2_r[i] + temp1;
2085 st->past_r_q[i] = lsf2_r[i];
2086 }
2087 }
2088
2089 /* verification that LSFs have minimum distance of LSF_GAP Hz */
2090 Reorder_lsf(lsf1_q, LSF_GAP);
2091 Reorder_lsf(lsf2_q, LSF_GAP);
2092 memcpy(st->past_lsf_q, lsf2_q, M << 2);
2093
2094 /* convert LSFs to the cosine domain */
2095 Lsf_lsp(lsf1_q, lsp1_q);
2096 Lsf_lsp(lsf2_q, lsp2_q);
2097 return;
2098}
2099
2100
2101/*
2102 * Dec_lag3
2103 *
2104 *
2105 * Parameters:
2106 * index I: received pitch index
2107 * t0_min I: minimum of search range
2108 * t0_max I: maximum of search range
2109 * i_subfr I: subframe flag
2110 * T0_prev I: integer pitch delay of last subframe used
2111 * in 2nd and 4th subframes
2112 * T0 O: integer part of pitch lag
2113 * T0_frac O : fractional part of pitch lag
2114 * flag4 I : flag for encoding with 4 bits
2115 * Function:
2116 * Decoding of fractional pitch lag with 1/3 resolution.
2117 * Extract the integer and fraction parts of the pitch lag from
2118 * the received adaptive codebook index.
2119 *
2120 * The fractional lag in 1st and 3rd subframes is encoded with 8 bits
2121 * while that in 2nd and 4th subframes is relatively encoded with 4, 5
2122 * and 6 bits depending on the mode.
2123 *
2124 * Returns:
2125 * void
2126 */
2127static void Dec_lag3(Word32 index, Word32 t0_min, Word32 t0_max, Word32 i_subfr
2128 , Word32 T0_prev, Word32 *T0, Word32 *T0_frac, Word32 flag4)
2129{
2130 Word32 i, tmp_lag;
2131
2132
2133 /* if 1st or 3rd subframe */
2134 if (i_subfr == 0) {
2135 if (index < 197) {
2136 *T0 = (((index + 2) * 10923) >> 15) + 19;
2137 i = *T0 + *T0 + *T0;
2138 *T0_frac = (index - i) + 58;
2139 } else {
2140 *T0 = index - 112;
2141 *T0_frac = 0;
2142 }
2143 }
2144
2145 /* 2nd or 4th subframe */
2146 else {
2147 if (flag4 == 0) {
2148 /* 'normal' decoding: either with 5 or 6 bit resolution */
2149 i = (((index + 2) * 10923) >> 15) - 1;
2150 *T0 = i + t0_min;
2151 i = i + i + i;
2152 *T0_frac = (index - 2) - i;
2153 } else {
2154 /* decoding with 4 bit resolution */
2155 tmp_lag = T0_prev;
2156
2157 if ((tmp_lag - t0_min) > 5) {
2158 tmp_lag = t0_min + 5;
2159 }
2160
2161 if ((t0_max - tmp_lag) > 4) {
2162 tmp_lag = t0_max - 4;
2163 }
2164
2165 if (index < 4) {
2166 i = (tmp_lag - 5);
2167 *T0 = i + index;
2168 *T0_frac = 0;
2169 } else {
2170 if (index < 12) {
2171 i = (((index - 5) * 10923) >> 15) - 1;
2172 *T0 = i + tmp_lag;
2173 i = i + i + i;
2174 *T0_frac = (index - 9) - i;
2175 } else {
2176 i = (index - 12) + tmp_lag;
2177 *T0 = i + 1;
2178 *T0_frac = 0;
2179 }
2180 }
2181 } /* end if (decoding with 4 bit resolution) */
2182 }
2183 return;
2184}
2185
2186
2187/*
2188 * Pred_lt_3or6_40
2189 *
2190 *
2191 * Parameters:
2192 * exc B: excitation buffer
2193 * T0 I: integer pitch lag
2194 * frac I: fraction of lag
2195 * flag3 I: if set, upsampling rate = 3 (6 otherwise)
2196 *
2197 * Function:
2198 * Compute the result of long term prediction with fractional
2199 * interpolation of resolution 1/3 or 1/6. (Interpolated past excitation).
2200 *
2201 * Once the fractional pitch lag is determined,
2202 * the adaptive codebook vector v(n) is computed by interpolating
2203 * the past excitation signal u(n) at the given integer delay k
2204 * and phase (fraction) :
2205 *
2206 * 9 9
2207 * v(n) = SUM[ u(n-k-i) * b60(t+i*6) ] + SUM[ u(n-k+1+i) * b60(6-t+i*6) ],
2208 * i=0 i=0
2209 * n = 0, ...,39, t = 0, ...,5.
2210 *
2211 * The interpolation filter b60 is based on a Hamming windowed sin(x)/x
2212 * function truncated at ± 59 and padded with zeros at ± 60 (b60(60)=0)).
2213 * The filter has a cut-off frequency (-3 dB) at 3 600 Hz in
2214 * the over-sampled domain.
2215 *
2216 * Returns:
2217 * void
2218 */
2219static void Pred_lt_3or6_40(Word32 exc[], Word32 T0, Word32 frac, Word32 flag3)
2220{
2221 Word32 s, i;
2222 Word32 *x0, *x1, *x2;
2223 const Word32 *c1, *c2;
2224
2225
2226 x0 = &exc[ - T0];
2227 frac = -frac;
2228
2229 if (flag3 != 0) {
2230 frac <<= 1; /* inter_3l[k] = inter6[2*k] -> k' = 2*k */
2231 }
2232
2233 if (frac < 0) {
2234 frac += 6;
2235 x0--;
2236 }
2237 c1 = &inter6[frac];
2238 c2 = &inter6[6 - frac];
2239
2240 for (i = 0; i < 40; i++) {
2241 x1 = x0++;
2242 x2 = x0;
2243 s = x1[0] * c1[0];
2244 s += x1[ - 1] * c1[6];
2245 s += x1[ - 2] * c1[12];
2246 s += x1[ - 3] * c1[18];
2247 s += x1[ - 4] * c1[24];
2248 s += x1[ - 5] * c1[30];
2249 s += x1[ - 6] * c1[36];
2250 s += x1[ - 7] * c1[42];
2251 s += x1[ - 8] * c1[48];
2252 s += x1[ - 9] * c1[54];
2253 s += x2[0] * c2[0];
2254 s += x2[1] * c2[6];
2255 s += x2[2] * c2[12];
2256 s += x2[3] * c2[18];
2257 s += x2[4] * c2[24];
2258 s += x2[5] * c2[30];
2259 s += x2[6] * c2[36];
2260 s += x2[7] * c2[42];
2261 s += x2[8] * c2[48];
2262 s += x2[9] * c2[54];
2263 exc[i] = (s + 0x4000) >> 15;
2264
2265 }
2266}
2267
2268
2269/*
2270 * Dec_lag6
2271 *
2272 *
2273 * Parameters:
2274 * index I: received pitch index
2275 * pit_min I: minimum pitch lag
2276 * pit_max I: maximum pitch lag
2277 * i_subfr I: subframe flag
2278 * T0 B: integer part of pitch lag
2279 * T0_frac O : fractional part of pitch lag
2280 *
2281 * Function:
2282 * Decoding of fractional pitch lag with 1/6 resolution.
2283 * Extract the integer and fraction parts of the pitch lag from
2284 * the received adaptive codebook index.
2285 *
2286 * The fractional lag in 1st and 3rd subframes is encoded with 9 bits
2287 * while that in 2nd and 4th subframes is relatively encoded with 6 bits.
2288 * Note that in relative encoding only 61 values are used. If the
2289 * decoder receives 61, 62, or 63 as the relative pitch index, it means
2290 * that a transmission error occurred. In this case, the pitch lag from
2291 * previous subframe (actually from previous frame) is used.
2292 *
2293 * Returns:
2294 * void
2295 */
2296static void Dec_lag6(Word32 index, Word32 pit_min, Word32 pit_max, Word32
2297 i_subfr, Word32 *T0, Word32 *T0_frac)
2298{
2299 Word32 t0_min, t0_max, i;
2300
2301
2302 /* if 1st or 3rd subframe */
2303 if (i_subfr == 0) {
2304 if (index < 463) {
2305 /* T0 = (index+5)/6 + 17 */
2306 *T0 = (index + 5) / 6 + 17;
2307 i = *T0 + *T0 + *T0;
2308
2309 /* *T0_frac = index - T0*6 + 105 */
2310 *T0_frac = (index - (i + i)) + 105;
2311 } else {
2312 *T0 = index - 368;
2313 *T0_frac = 0;
2314 }
2315 }
2316
2317 /* second or fourth subframe */
2318 else {
2319 /* find t0_min and t0_max for 2nd (or 4th) subframe */
2320 t0_min = *T0 - 5;
2321
2322 if (t0_min < pit_min) {
2323 t0_min = pit_min;
2324 }
2325 t0_max = t0_min + 9;
2326
2327 if (t0_max > pit_max) {
2328 t0_max = pit_max;
2329 t0_min = t0_max - 9;
2330 }
2331
2332 /* i = (index+5)/6 - 1 */
2333 i = (index + 5) / 6 - 1;
2334 *T0 = i + t0_min;
2335 i = i + i + i;
2336 *T0_frac = (index - 3) - (i + i);
2337 }
2338}
2339
2340
2341/*
2342 * decompress10
2343 *
2344 *
2345 * Parameters:
2346 * MSBs I: MSB part of the index
2347 * LSBs I: LSB part of the index
2348 * index1 I: index for first pos in posIndex
2349 * index2 I: index for second pos in posIndex
2350 * index3 I: index for third pos in posIndex
2351 * pos_indx O: position of 3 pulses (decompressed)
2352 * Function:
2353 * Decompression of the linear codeword
2354 *
2355 * Returns:
2356 * void
2357 */
2358static void decompress10(Word32 MSBs, Word32 LSBs, Word32 index1, Word32 index2
2359 , Word32 index3, Word32 pos_indx[])
2360{
2361 Word32 divMSB;
2362
2363 if (MSBs > 124) {
2364 MSBs = 124;
2365 }
2366 /*
2367 * pos_indx[index1] = ((MSBs-25*(MSBs/25))%5)*2 + (LSBs-4*(LSBs/4))%2;
2368 * pos_indx[index2] = ((MSBs-25*(MSBs/25))/5)*2 + (LSBs-4*(LSBs/4))/2;
2369 * pos_indx[index3] = (MSBs/25)*2 + LSBs/4;
2370 */
2371 divMSB = MSBs / 25;
2372 pos_indx[index1] = (((MSBs - 25 * (divMSB)) % 5) << 1) + (LSBs & 0x1
2373 );
2374 pos_indx[index2] = (((MSBs - 25 * (divMSB)) / 5) << 1) + ((LSBs &
2375 0x2) >> 1);
2376 pos_indx[index3] = (divMSB << 1) + (LSBs >> 2);
2377 return;
2378}
2379
2380
2381/*
2382 * decompress_codewords
2383 *
2384 *
2385 * Parameters:
2386 * indx I: position of 8 pulses (compressed)
2387 * pos_indx O: position index of 8 pulses (position only)
2388 *
2389 * Function:
2390 * Decompression of the linear codewords to 4+three indeces
2391 * one bit from each pulse is made robust to errors by
2392 * minimizing the phase shift of a bit error.
2393 *
2394 * i0,i4,i1 => one index (7+3) bits, 3 LSBs more robust
2395 * i2,i6,i5 => one index (7+3) bits, 3 LSBs more robust
2396 * i3,i7 => one index (5+2) bits, 2-3 LSbs more robust
2397 *
2398 * Returns:
2399 * void
2400 */
2401static void decompress_codewords(Word16 indx[], Word32 pos_indx[])
2402{
2403 Word32 ia, ib, MSBs, LSBs, MSBs0_24, tmp;
2404
2405
2406 /*
2407 * First index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits
2408 * MSBs = indx[NB_TRACK]/8;
2409 * LSBs = indx[NB_TRACK]%8;
2410 */
2411 MSBs = *indx >> 3;
2412 LSBs = *indx & 0x7;
2413 decompress10(MSBs, LSBs, 0, 4, 1, pos_indx);
2414
2415 /*
2416 * Second index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits
2417 * MSBs = indx[NB_TRACK+1]/8;
2418 * LSBs = indx[NB_TRACK+1]%8;
2419 */
2420 MSBs = indx[1] >> 3;
2421 LSBs = indx[1] & 0x7;
2422 decompress10(MSBs, LSBs, 2, 6, 5, pos_indx);
2423
2424 /*
2425 * Third index: 10x10 -> 2x5x2x5-> 25x2x2 -> 5+1x2 bits
2426 * MSBs = indx[NB_TRACK+2]/4;
2427 * LSBs = indx[NB_TRACK+2]%4;
2428 * MSBs0_24 = (MSBs*25+12)/32;
2429 * if ((MSBs0_24/5)%2==1)
2430 * pos_indx[3] = (4-(MSBs0_24%5))*2 + LSBs%2;
2431 * else
2432 * pos_indx[3] = (MSBs0_24%5)*2 + LSBs%2;
2433 * pos_indx[7] = (MSBs0_24/5)*2 + LSBs/2;
2434 */
2435 MSBs = indx[2] >> 2;
2436 LSBs = indx[2] & 0x3;
2437 MSBs0_24 = (((MSBs * 25) + 12) >> 5);
2438 tmp = (MSBs0_24 * 6554) >> 15;
2439 ia = tmp & 0x1;
2440 ib = (MSBs0_24 - (tmp * 5));
2441
2442 if (ia == 1) {
2443 ib = 4 - ib;
2444 }
2445 pos_indx[3] = (ib << 1) + (LSBs & 0x1);
2446 pos_indx[7] = (tmp << 1) + (LSBs >> 1);
2447}
2448
2449
2450/*
2451 * decode_2i40_9bits
2452 *
2453 *
2454 * Parameters:
2455 * subNr I: subframe number
2456 * sign I: signs of 2 pulses
2457 * index I: Positions of the 2 pulses
2458 * cod O: algebraic (fixed) codebook excitation
2459 *
2460 * Function:
2461 * Algebraic codebook decoder
2462 *
2463 * Returns:
2464 * void
2465 */
2466static void decode_2i40_9bits(Word32 subNr, Word32 sign, Word32 index, Word32
2467 cod[])
2468{
2469 Word32 pos[2];
2470 Word32 i, j, k;
2471
2472
2473 /* Decode the positions */
2474 /* table bit is the MSB */
2475 j = (index & 64) >> 6;
2476 i = index & 7;
2477
2478 /* pos0 =i*5+startPos[j*8+subNr*2] */
2479 i = (i + (i << 2));
2480 k = startPos[(j << 3) + (subNr << 1)];
2481 pos[0] = i + k;
2482 index = index >> 3;
2483 i = index & 7;
2484
2485 /* pos1 =i*5+startPos[j*8+subNr*2+1] */
2486 i = (i + (i << 2));
2487 k = startPos[((j << 3) + (subNr << 1)) + 1];
2488 pos[1] = (Word16)(i + k);
2489
2490 /* decode the signs and build the codeword */
2491 memset(cod, 0, L_SUBFR << 2);
2492
2493 for (j = 0; j < 2; j++) {
2494 i = sign & 1;
2495 sign = sign >> 1;
2496
2497 if (i != 0) {
2498 cod[pos[j]] = 8191; /* +1.0 */
2499 } else {
2500 cod[pos[j]] = -8192; /* -1.0 */
2501 }
2502 }
2503 return;
2504}
2505
2506
2507/*
2508 * decode_2i40_11bits
2509 *
2510 *
2511 * Parameters:
2512 * sign I: signs of 2 pulses
2513 * index I: Positions of the 2 pulses
2514 * cod O: algebraic (fixed) codebook excitation
2515 *
2516 * Function:
2517 * Algebraic codebook decoder
2518 *
2519 * Returns:
2520 * void
2521 */
2522static void decode_2i40_11bits(Word32 sign, Word32 index, Word32 cod[])
2523{
2524 Word32 pos[2];
2525 Word32 i, j;
2526
2527
2528 /* Decode the positions */
2529 j = index & 1;
2530 index = index >> 1;
2531 i = index & 7;
2532
2533 /* pos0 =i*5+1+j*2 */
2534 i = (i + (i << 2));
2535 i = (i + 1);
2536 j = (j << 1);
2537 pos[0] = i + j;
2538 index = index >> 3;
2539 j = index & 3;
2540 index = index >> 2;
2541 i = index & 7;
2542
2543 if (j == 3) {
2544 /* pos1 =i*5+4 */
2545 i = (i + (i << 2));
2546 pos[1] = i + 4;
2547 } else {
2548 /* pos1 =i*5+j */
2549 i = (i + (i << 2));
2550 pos[1] = i + j;
2551 }
2552
2553 /* decode the signs and build the codeword */
2554 memset(cod, 0, L_SUBFR << 2);
2555
2556 for (j = 0; j < 2; j++) {
2557 i = sign & 1;
2558 sign = sign >> 1;
2559
2560 if (i != 0) {
2561 cod[pos[j]] = 8191; /* +1.0 */
2562 } else {
2563 cod[pos[j]] = -8192; /* -1.0 */
2564 }
2565 }
2566 return;
2567}
2568
2569
2570/*
2571 * decode_3i40_14bits
2572 *
2573 *
2574 * Parameters:
2575 * sign I: signs of 3 pulses
2576 * index I: Positions of the 3 pulses
2577 * cod O: algebraic (fixed) codebook excitation
2578 *
2579 * Function:
2580 * Algebraic codebook decoder
2581 *
2582 * Returns:
2583 * void
2584 */
2585static void decode_3i40_14bits(Word32 sign, Word32 index, Word32 cod[])
2586{
2587 Word32 pos[3];
2588 Word32 i, j;
2589
2590
2591 /* Decode the positions */
2592 i = index & 7;
2593
2594 /* pos0 =i*5 */
2595 pos[0] = i + (i << 2);
2596 index = index >> 3;
2597 j = index & 1;
2598 index = index >> 1;
2599 i = index & 7;
2600
2601 /* pos1 =i*5+1+j*2 */
2602 i = (i + (i << 2));
2603 i = (i + 1);
2604 j = (j << 1);
2605 pos[1] = i + j;
2606 index = index >> 3;
2607 j = index & 1;
2608 index = index >> 1;
2609 i = index & 7;
2610
2611 /* pos2 =i*5+2+j*2 */
2612 i = (i + (i << 2));
2613 i = (i + 2);
2614 j = (j << 1);
2615 pos[2] = i + j;
2616
2617 /* decode the signs and build the codeword */
2618 memset(cod, 0, L_SUBFR << 2);
2619
2620 for (j = 0; j < 3; j++) {
2621 i = sign & 1;
2622 sign = sign >> 1;
2623
2624 if (i > 0) {
2625 cod[pos[j]] = 8191; /* +1.0 */
2626 } else {
2627 cod[pos[j]] = -8192; /* -1.0 */
2628 }
2629 }
2630 return;
2631}
2632
2633
2634/*
2635 * decode_3i40_14bits
2636 *
2637 *
2638 * Parameters:
2639 * sign I: signs of 4 pulses
2640 * index I: Positions of the 4 pulses
2641 * cod O: algebraic (fixed) codebook excitation
2642 *
2643 * Function:
2644 * Algebraic codebook decoder
2645 *
2646 * Returns:
2647 * void
2648 */
2649static void decode_4i40_17bits(Word32 sign, Word32 index, Word32 cod[])
2650{
2651 Word32 pos[4];
2652 Word32 i, j;
2653
2654
2655 /* Decode the positions */
2656 i = index & 7;
2657 i = dgray[i];
2658
2659 /* pos0 =i*5 */
2660 pos[0] = i + (i << 2);
2661 index = index >> 3;
2662 i = index & 7;
2663 i = dgray[i];
2664
2665 /* pos1 =i*5+1 */
2666 i = (i + (i << 2));
2667 pos[1] = i + 1;
2668 index = index >> 3;
2669 i = index & 7;
2670 i = dgray[i];
2671
2672 /* pos2 =i*5+1 */
2673 i = (i + (i << 2));
2674 pos[2] = i + 2;
2675 index = index >> 3;
2676 j = index & 1;
2677 index = index >> 1;
2678 i = index & 7;
2679 i = dgray[i];
2680
2681 /* pos3 =i*5+3+j */
2682 i = (i + (i << 2));
2683 i = (i + 3);
2684 pos[3] = i + j;
2685
2686 /* decode the signs and build the codeword */
2687 memset(cod, 0, L_SUBFR << 2);
2688
2689 for (j = 0; j < 4; j++) {
2690 i = sign & 1;
2691 sign = sign >> 1;
2692
2693 if (i != 0) {
2694 cod[pos[j]] = 8191;
2695 } else {
2696 cod[pos[j]] = -8192;
2697 }
2698 }
2699 return;
2700}
2701
2702
2703/*
2704 * decode_8i40_31bits
2705 *
2706 *
2707 * Parameters:
2708 * index I: index of 8 pulses (sign+position)
2709 * cod O: algebraic (fixed) codebook excitation
2710 *
2711 * Function:
2712 * Algebraic codebook decoder
2713 *
2714 * Returns:
2715 * void
2716 */
2717static void decode_8i40_31bits(Word16 index[], Word32 cod[])
2718{
2719 Word32 linear_codewords[8];
2720 Word32 i, j, pos1, pos2, sign;
2721
2722
2723 memset(cod, 0, L_CODE << 2);
2724 decompress_codewords(&index[NB_TRACK_MR102], linear_codewords);
2725
2726 /* decode the positions and signs of pulses and build the codeword */
2727 for (j = 0; j < NB_TRACK_MR102; j++) {
2728 /* compute index i */
2729 i = linear_codewords[j];
2730 i <<= 2;
2731
2732 /* position of pulse "j" */
2733 pos1 = i + j;
2734
2735 if (index[j] == 0) {
2736 sign = POS_CODE; /* +1.0 */
2737 } else {
2738 sign = -NEG_CODE; /* -1.0 */
2739 }
2740
2741 /* compute index i */
2742 i = linear_codewords[j + 4];
2743 i = i << 2;
2744
2745 /* position of pulse "j+4" */
2746 pos2 = i + j;
2747 cod[pos1] = sign;
2748
2749 if (pos2 < pos1) {
2750 sign = -(sign);
2751 }
2752 cod[pos2] = cod[pos2] + sign;
2753 }
2754 return;
2755}
2756
2757
2758/*
2759 * decode_10i40_35bits
2760 *
2761 *
2762 * Parameters:
2763 * index I: index of 10 pulses (sign+position)
2764 * cod O: algebraic (fixed) codebook excitation
2765 *
2766 * Function:
2767 * Algebraic codebook decoder
2768 *
2769 * Returns:
2770 * void
2771 */
2772static void decode_10i40_35bits(Word16 index[], Word32 cod[])
2773{
2774 Word32 i, j, pos1, pos2, sign, tmp;
2775
2776
2777 memset(cod, 0, L_CODE << 2);
2778
2779 /* decode the positions and signs of pulses and build the codeword */
2780 for (j = 0; j < 5; j++) {
2781 /* compute index i */
2782 tmp = index[j];
2783 i = tmp & 7;
2784 i = dgray[i];
2785 i = (i * 5);
2786
2787 /* position of pulse "j" */
2788 pos1 = (i + j);
2789 i = (tmp >> 3) & 1;
2790
2791 if (i == 0) {
2792 sign = 4096; /* +1.0 */
2793 } else {
2794 sign = -4096; /* -1.0 */
2795 }
2796
2797 /* compute index i */
2798 i = index[j + 5] & 7;
2799 i = dgray[i];
2800 i = i * 5;
2801
2802 /* position of pulse "j+5" */
2803 pos2 = (i + j);
2804 cod[pos1] = sign;
2805
2806 if (pos2 < pos1) {
2807 sign = -(sign);
2808 }
2809 cod[pos2] = cod[pos2] + sign;
2810 }
2811 return;
2812}
2813
2814
2815/*
2816 * gmed_n
2817 *
2818 *
2819 * Parameters:
2820 * ind I: values
2821 * n I: The number of gains (odd)
2822 *
2823 * Function:
2824 * Calculates N-point median.
2825 *
2826 * Returns:
2827 * index of the median value
2828 */
2829static Word32 gmed_n(Word32 ind[], Word32 n)
2830{
2831 Word32 tmp[NMAX], tmp2[NMAX];
2832 Word32 max, medianIndex, i, j, ix = 0;
2833
2834
2835 for (i = 0; i < n; i++) {
2836 tmp2[i] = ind[i];
2837 }
2838
2839 for (i = 0; i < n; i++) {
2840 max = -32767;
2841
2842 for (j = 0; j < n; j++) {
2843 if (tmp2[j] >= max) {
2844 max = tmp2[j];
2845 ix = j;
2846 }
2847 }
2848 tmp2[ix] = -32768;
2849 tmp[i] = ix;
2850 }
2851 medianIndex = tmp[(n >> 1)];
2852 return(ind[medianIndex]);
2853}
2854
2855
2856/*
2857 * ec_gain_pitch
2858 *
2859 *
2860 * Parameters:
2861 * st->pbuf I: last five gains
2862 * st->past_gain_pit I: past gain
2863 * state I: state of the state machine
2864 * gain_pitch O: pitch gain
2865 *
2866 * Function:
2867 * Calculates pitch from previous values.
2868 *
2869 * Returns:
2870 * void
2871 */
2872static void ec_gain_pitch(ec_gain_pitchState *st, Word16 state, Word32 *
2873 gain_pitch)
2874{
2875 Word32 tmp;
2876
2877
2878 /* calculate median of last five gains */
2879 tmp = gmed_n(st->pbuf, 5);
2880
2881 /* new gain = minimum(median, past_gain) * pdown[state] */
2882 if (tmp > st->past_gain_pit) {
2883 tmp = st->past_gain_pit;
2884 }
2885 *gain_pitch = (tmp * pdown[state]) >> 15;
2886}
2887
2888
2889/*
2890 * d_gain_pitch
2891 *
2892 *
2893 * Parameters:
2894 * mode I: AMR mode
2895 * index I: index of quantization
2896 *
2897 * Function:
2898 * Decodes the pitch gain using the received index
2899 *
2900 * Returns:
2901 * gain
2902 */
2903static Word32 d_gain_pitch(enum Mode mode, Word32 index)
2904{
2905 Word32 gain;
2906
2907
2908 if (mode == MR122) {
2909 /* clear 2 LSBits */
2910 gain = (qua_gain_pitch[index] >> 2) << 2;
2911 } else {
2912 gain = qua_gain_pitch[index];
2913 }
2914 return gain;
2915}
2916
2917
2918/*
2919 * ec_gain_pitch_update
2920 *
2921 *
2922 * Parameters:
2923 * st->prev_gp B: previous pitch gain
2924 * st->past_gain_pit O: past gain
2925 * st->pbuf B: past gain buffer
2926 * bfi I: bad frame indicator
2927 * prev_bf I: previous frame was bad
2928 * gain_pitch B: pitch gain
2929 *
2930 * Function:
2931 * Update the pitch gain concealment state
2932 * Limit gain_pitch if the previous frame was bad
2933 *
2934 * Returns:
2935 * gain
2936 */
2937static void ec_gain_pitch_update(ec_gain_pitchState *st, Word32 bfi,
2938 Word32 prev_bf, Word32 *gain_pitch)
2939{
2940 if (bfi == 0) {
2941 if (prev_bf != 0) {
2942 if (*gain_pitch > st->prev_gp) {
2943 *gain_pitch = st->prev_gp;
2944 }
2945 }
2946 st->prev_gp = *gain_pitch;
2947 }
2948 st->past_gain_pit = *gain_pitch;
2949
2950 /* if (st->past_gain_pit > 1.0) */
2951 if (st->past_gain_pit > 16384) {
2952 st->past_gain_pit = 16384;
2953 }
2954 st->pbuf[0] = st->pbuf[1];
2955 st->pbuf[1] = st->pbuf[2];
2956 st->pbuf[2] = st->pbuf[3];
2957 st->pbuf[3] = st->pbuf[4];
2958 st->pbuf[4] = st->past_gain_pit;
2959}
2960
2961
2962/*
2963 * gc_pred (366)
2964 *
2965 *
2966 * Parameters:
2967 * st->past_qua_en I: MA predictor
2968 * st->past_qua_en_MR122 I: MA predictor MR122
2969 * mode I: AMR mode
2970 * code I: innovative codebook vector
2971 * exp_gcode0 O: predicted gain factor (exponent)
2972 * frac_gcode0 O: predicted gain factor (fraction)
2973 * exp_en I: innovation energy (MR795) (exponent)
2974 * frac_en I: innovation energy (MR795) (fraction)
2975 *
2976 * Function:
2977 * MA prediction of the innovation energy
2978 *
2979 * Mean removed innovation energy (dB) in subframe n
2980 * N-1
2981 * E(n) = 10*log(gc*gc * SUM[(code(i) * code(i)]/N) - EMean
2982 * i=0
2983 * N=40
2984 *
2985 * Mean innovation energy (dB)
2986 * N-1
2987 * Ei(n) = 10*log(SUM[(code(i) * code(i)]/N)
2988 * i=0
2989 *
2990 * Predicted energy
2991 * 4
2992 * Ep(n) = SUM[b(i) * R(n-i)]
2993 * i=1
2994 * b = [0.68 0.58 0.34 0.19]
2995 * R(k) is quantified prediction error at subframe k
2996 *
2997 * E_Mean = 36 dB (MR122)
2998 *
2999 * Predicted gain gc is found by
3000 *
3001 * gc = POW[10, 0.05 * (Ep(n) + EMean - Ei)]
3002 *
3003 * Returns:
3004 * void
3005 */
3006static void gc_pred(gc_predState *st, enum Mode mode, Word32 *code, Word32 *
3007 exp_gcode0, Word32 *frac_gcode0, Word32 *exp_en, Word32 *frac_en)
3008{
3009 Word32 exp, frac, ener_code = 0, i = 0;
3010
3011
3012 /* energy of code:
3013 * ener_code = sum(code[i]^2)
3014 */
3015 while (i < L_SUBFR) {
3016 ener_code += code[i] * code[i];
3017 i++;
3018 }
3019
3020 if ((0x3fffffff <= ener_code) | (ener_code < 0)) {
3021 ener_code = MAX_32;
3022 } else {
3023 ener_code <<= 1;
3024 }
3025
3026 if (mode == MR122) {
3027 Word32 ener;
3028
3029
3030 /* ener_code = ener_code / lcode; lcode = 40; 1/40 = 26214 Q20 */
3031 ener_code = ((ener_code + 0x00008000L) >> 16) * 52428;
3032
3033 /* Q9 * Q20 -> Q30 */
3034 /* energy of code:
3035 * ener_code(Q17) = 10 * Log10(energy) / constant
3036 * = 1/2 * Log2(energy)
3037 * constant = 20*Log10(2)
3038 */
3039 /* ener_code = 1/2 * Log2(ener_code); Note: Log2=log2+30 */
3040 Log2(ener_code, &exp, &frac);
3041 ener_code = ((exp - 30) << 16) + (frac << 1);
3042
3043 /* Q16 for log(), ->Q17 for 1/2 log() */
3044 /*
3045 * predicted energy:
3046 * ener(Q24) = (Emean + sum{pred[i]*pastEn[i]})/constant
3047 * = MEAN_ENER + sum(pred[i]*past_qua_en[i])
3048 * constant = 20*Log10(2)
3049 */
3050 ener = 0;
3051 i = 0;
3052
3053 while (i < 4) {
3054 ener += st->past_qua_en_MR122[i] * pred_MR122[i];
3055 i++;
3056 }
3057 ener <<= 1;
3058 ener += MEAN_ENER_MR122;
3059
3060 /*
3061 * predicted codebook gain
3062
3063 * gc0 = Pow10( (ener*constant - ener_code*constant) / 20 )
3064 * = Pow2(ener-ener_code)
3065 * = Pow2(int(d)+frac(d))
3066 */
3067 ener = (ener - ener_code) >> 1; /* Q16 */
3068 *exp_gcode0 = ener >> 16;
3069 *frac_gcode0 = (ener >> 1) - (*exp_gcode0 << 15);
3070 }
3071
3072 /* all modes except 12.2 */
3073 else {
3074 Word32 tmp, gcode0;
3075 int exp_code;
3076
3077
3078 /*
3079 * Compute: meansEner - 10log10(ener_code/ LSufr)
3080 */
3081 exp_code = 0;
3082 if (ener_code != 0) {
3083 while (!(ener_code & 0x40000000)) {
3084 exp_code++;
3085 ener_code = ener_code << 1;
3086 }
3087 }
3088
3089 /* Log2 = log2 + 27 */
3090 Log2_norm(ener_code, exp_code, &exp, &frac);
3091
3092 /* fact = 10/log2(10) = 3.01 = 24660 Q13 */
3093 /* Q0.Q15 * Q13 -> Q14 */
3094 tmp = (exp * (-49320)) + (((frac * (-24660)) >> 15) << 1);
3095
3096 /*
3097 * tmp = meansEner - 10log10(ener_code/L_SUBFR)
3098 * = meansEner - 10log10(ener_code) + 10log10(L_SUBFR)
3099 * = K - fact * Log2(ener_code)
3100 * = K - fact * log2(ener_code) - fact*27
3101 *
3102 * ==> K = meansEner + fact*27 + 10log10(L_SUBFR)
3103 *
3104 * meansEner = 33 = 540672 Q14 (MR475, MR515, MR59)
3105 * meansEner = 28.75 = 471040 Q14 (MR67)
3106 * meansEner = 30 = 491520 Q14 (MR74)
3107 * meansEner = 36 = 589824 Q14 (MR795)
3108 * meansEner = 33 = 540672 Q14 (MR102)
3109 * 10log10(L_SUBFR) = 16.02 = 262481.51 Q14
3110 * fact * 27 = 1331640 Q14
3111 * -----------------------------------------
3112 * (MR475, MR515, MR59) K = 2134793.51 Q14 ~= 16678 * 64 * 2
3113 * (MR67) K = 2065161.51 Q14 ~= 32268 * 32 * 2
3114 * (MR74) K = 2085641.51 Q14 ~= 32588 * 32 * 2
3115 * (MR795) K = 2183945.51 Q14 ~= 17062 * 64 * 2
3116 * (MR102) K = 2134793.51 Q14 ~= 16678 * 64 * 2
3117 */
3118 if (mode == MR102) {
3119 /* mean = 33 dB */
3120 tmp += 2134784; /* Q14 */
3121 } else if (mode == MR795) {
3122 /* mean = 36 dB */
3123 tmp += 2183936; /* Q14 */
3124
3125 /*
3126 * ener_code = <xn xn> * 2^27*2^exp_code
3127 * frac_en = ener_code / 2^16
3128 * = <xn xn> * 2^11*2^exp_code
3129 * <xn xn> = <xn xn>*2^11*2^exp * 2^exp_en
3130 * := frac_en * 2^exp_en
3131 *
3132 * ==> exp_en = -11-exp_code;
3133 */
3134 *frac_en = ener_code >> 16;
3135 *exp_en = -11 - exp_code;
3136 } else if (mode == MR74) {
3137 /* mean = 30 dB */
3138 tmp += 2085632; /* Q14 */
3139 } else if (mode == MR67) {
3140 /* mean = 28.75 dB */
3141 tmp += 2065152; /* Q14 */
3142 } else { /* MR59, MR515, MR475 */
3143 /* mean = 33 dB */
3144 tmp += 2134784; /* Q14 */
3145 }
3146
3147 /*
3148 * Compute gcode0
3149 * = Sum(i=0,3) pred[i]*past_qua_en[i] - ener_code + meanEner
3150 */
3151 tmp = tmp << 9; /* Q23 */
3152
3153 /* Q13 * Q10 -> Q23 */
3154 i = 0;
3155
3156 while (i < 4) {
3157 tmp += pred[i] * st->past_qua_en[i];
3158 i++;
3159 }
3160 gcode0 = tmp >> 15; /* Q8 */
3161
3162 /*
3163 * gcode0 = pow(10.0, gcode0/20)
3164 * = pow(2, 3.3219*gcode0/20)
3165 * = pow(2, 0.166*gcode0)
3166 */
3167 /* 5439 Q15 = 0.165985 */
3168 /* (correct: 1/(20*log10(2)) 0.166096 = 5443 Q15) */
3169 /* For IS641 bitexactness */
3170 if (mode == MR74) {
3171 /* Q8 * Q15 -> Q24 */
3172 tmp = gcode0 * 10878;
3173 } else {
3174 /* Q8 * Q15 -> Q24 */
3175 tmp = gcode0 * 10886;
3176 }
3177 tmp = tmp >> 9; /* -> Q15 */
3178
3179 /* -> Q0.Q15 */
3180 *exp_gcode0 = tmp >> 15;
3181 *frac_gcode0 = tmp - (*exp_gcode0 * 32768);
3182 }
3183}
3184
3185
3186/*
3187 * gc_pred_update
3188 *
3189 *
3190 * Parameters:
3191 * st->past_qua_en B: MA predictor
3192 * st->past_qua_en_MR122 B: MA predictor MR122
3193 * qua_ener_MR122 I: quantized energy for update (log2(quaErr))
3194 * qua_ener I: quantized energy for update (20*log10(quaErr))
3195 *
3196 * Function:
3197 * Update MA predictor with last quantized energy
3198 *
3199 * Returns:
3200 * void
3201 */
3202static void gc_pred_update(gc_predState *st, Word32 qua_ener_MR122,
3203 Word32 qua_ener)
3204{
3205 Word32 i;
3206
3207
3208 for (i = 3; i > 0; i--) {
3209 st->past_qua_en[i] = st->past_qua_en[i - 1];
3210 st->past_qua_en_MR122[i] = st->past_qua_en_MR122[i - 1];
3211 }
3212 st->past_qua_en_MR122[0] = qua_ener_MR122; /* log2 (quaErr), Q10 */
3213 st->past_qua_en[0] = qua_ener; /* 20*log10(quaErr), Q10 */
3214}
3215
3216
3217/*
3218 * Dec_gain
3219 *
3220 *
3221 * Parameters:
3222 * pred_state->past_qua_en B: MA predictor
3223 * pred_state->past_qua_en_MR122 B: MA predictor MR122
3224 * mode I: AMR mode
3225 * index I: index of quantization
3226 * code I: Innovative vector
3227 * evenSubfr I: Flag for even subframes
3228 * gain_pit O: Pitch gain
3229 * gain_cod O: Code gain
3230 *
3231 * Function:
3232 * Decode the pitch and codebook gains
3233 *
3234 * Returns:
3235 * void
3236 */
3237static void Dec_gain(gc_predState *pred_state, enum Mode mode, Word32 index,
3238 Word32 code[], Word32 evenSubfr, Word32 *gain_pit, Word32 *gain_cod)
3239{
3240 Word32 frac, gcode0, exp, qua_ener, qua_ener_MR122, g_code, tmp;
3241 const Word32 *p;
3242
3243
3244 /* Read the quantized gains (table depends on mode) */
3245 index = index << 2;
3246
3247 if ((mode == MR102) || (mode == MR74) || (mode == MR67)) {
3248 p = &table_gain_highrates[index];
3249 *gain_pit = *p++;
3250 g_code = *p++;
3251 qua_ener_MR122 = *p++;
3252 qua_ener = *p;
3253 } else {
3254 if (mode == MR475) {
3255 index = index + ((1 - evenSubfr) << 1);
3256 p = &table_gain_MR475[index];
3257 *gain_pit = *p++;
3258 g_code = *p++;
3259
3260 /*
3261 * calculate predictor update values (not stored in 4.75
3262 * quantizer table to save space):
3263 * qua_ener = log2(g)
3264 * qua_ener_MR122 = 20*log10(g)
3265 */
3266 /* Log2(x Q12) = log2(x) + 12 */
3267 Log2(g_code, &exp, &frac);
3268 exp = exp - 12;
3269 tmp = frac >> 5;
3270
3271 if ((frac & ((Word16)1 << 4)) != 0) {
3272 tmp++;
3273 }
3274 qua_ener_MR122 = tmp + (exp << 10);
3275
3276 /* 24660 Q12 ~= 6.0206 = 20*log10(2) */
3277 tmp = exp * 49320;
3278 tmp += (((frac * 24660) >> 15) << 1);
3279
3280 /* Q12 * Q0 = Q13 -> Q10 */
3281 qua_ener = ((tmp << 13) + 0x00008000L) >> 16;
3282 } else {
3283 p = &table_gain_lowrates[index];
3284 *gain_pit = *p++;
3285 g_code = *p++;
3286 qua_ener_MR122 = *p++;
3287 qua_ener = *p;
3288 }
3289 }
3290
3291 /*
3292 * predict codebook gain
3293 * gc0 = Pow2(int(d)+frac(d))
3294 * = 2^exp + 2^frac
3295 * gcode0 (Q14) = 2^14*2^frac = gc0 * 2^(14-exp)
3296 */
3297 gc_pred(pred_state, mode, code, &exp, &frac, NULL, NULL);
3298 gcode0 = Pow2(14, frac);
3299
3300 /*
3301 * read quantized gains, update table of past quantized energies
3302 * st->past_qua_en(Q10) = 20 * Log10(gFac) / constant
3303 * = Log2(gFac)
3304 * = qua_ener
3305 * constant = 20*Log10(2)
3306 */
3307 if (exp < 11) {
3308 *gain_cod = (g_code * gcode0) >> (25 - exp);
3309 } else {
3310 tmp = ((g_code * gcode0) << (exp - 9));
3311
3312 if ((tmp >> (exp - 9)) != (g_code * gcode0)) {
3313 *gain_cod = 0x7FFF;
3314 } else {
3315 *gain_cod = tmp >> 16;
3316 }
3317 }
3318
3319 /* update table of past quantized energies */
3320 gc_pred_update(pred_state, qua_ener_MR122, qua_ener);
3321 return;
3322}
3323
3324
3325/*
3326 * gc_pred_average_limited
3327 *
3328 *
3329 * Parameters:
3330 * st->past_qua_en I: MA predictor
3331 * st->past_qua_en_MR122 I: MA predictor MR122
3332 * ener_avg_MR122 O: everaged quantized energy (log2(quaErr))
3333 * ener_avg O: averaged quantized energy (20*log10(quaErr))
3334 *
3335 * Function:
3336 * Compute average limited quantized energy
3337 * Returns:
3338 * void
3339 */
3340static void gc_pred_average_limited(gc_predState *st, Word32 *ener_avg_MR122,
3341 Word32 *ener_avg)
3342{
3343 Word32 av_pred_en, i;
3344
3345
3346 /* do average in MR122 mode (log2() domain) */
3347 av_pred_en = 0;
3348
3349 for (i = 0; i < NPRED; i++) {
3350 av_pred_en = (av_pred_en + st->past_qua_en_MR122[i]);
3351 }
3352
3353 /* av_pred_en = 0.25*av_pred_en */
3354 av_pred_en = (av_pred_en * 8192) >> 15;
3355
3356 /* if (av_pred_en < -14/(20Log10(2))) av_pred_en = .. */
3357 if (av_pred_en < MIN_ENERGY_MR122) {
3358 av_pred_en = MIN_ENERGY_MR122;
3359 }
3360 *ener_avg_MR122 = (Word16)av_pred_en;
3361
3362 /* do average for other modes (20*log10() domain) */
3363 av_pred_en = 0;
3364
3365 for (i = 0; i < NPRED; i++) {
3366 av_pred_en = (av_pred_en + st->past_qua_en[i]);
3367 if (av_pred_en < -32768) {
3368 av_pred_en = -32768;
3369 } else if (av_pred_en > 32767) {
3370 av_pred_en = 32767;
3371 }
3372 }
3373
3374 /* av_pred_en = 0.25*av_pred_en */
3375 av_pred_en = (av_pred_en * 8192) >> 15;
3376
3377 *ener_avg = av_pred_en;
3378}
3379
3380
3381/*
3382 * ec_gain_code
3383 *
3384 *
3385 * Parameters:
3386 * st->gbuf I: last five gains
3387 * st->past_gain_code I: past gain
3388 * pred_state B: MA predictor state
3389 * state I: state of the state machine
3390 * gain_code O: decoded innovation gain
3391 *
3392 * Function:
3393 * Conceal the codebook gain
3394 *
3395 * Returns:
3396 * void
3397 */
3398static void ec_gain_code(ec_gain_codeState *st, gc_predState *pred_state,
3399 Word16 state, Word32 *gain_code)
3400{
3401 Word32 tmp, qua_ener_MR122, qua_ener;
3402
3403
3404 /* calculate median of last five gain values */
3405 tmp = gmed_n(st->gbuf, 5);
3406
3407 /* new gain = minimum(median, past_gain) * cdown[state] */
3408 if (tmp > st->past_gain_code) {
3409 tmp = st->past_gain_code;
3410 }
3411 tmp = (tmp * cdown[state]) >> 15;
3412 *gain_code = tmp;
3413
3414 /*
3415 * update table of past quantized energies with average of
3416 * current values
3417 */
3418 gc_pred_average_limited(pred_state, &qua_ener_MR122, &qua_ener);
3419 gc_pred_update(pred_state, qua_ener_MR122, qua_ener);
3420}
3421
3422
3423/*
3424 * ec_gain_code_update
3425 *
3426 *
3427 * Parameters:
3428 * st->gbuf B: last five gains
3429 * st->past_gain_code O: past gain
3430 * st->prev_gc B previous gain
3431 * bfi I: bad indicator
3432 * prev_bf I: previous frame bad indicator
3433 * gain_code O: decoded innovation gain
3434 *
3435 * Function:
3436 * Update the codebook gain concealment state
3437 *
3438 * Returns:
3439 * void
3440 */
3441static void ec_gain_code_update(ec_gain_codeState *st, Word16 bfi,
3442 Word16 prev_bf, Word32 *gain_code)
3443{
3444 /* limit gain_code by previous good gain if previous frame was bad */
3445 if (bfi == 0) {
3446 if (prev_bf != 0) {
3447 if (*gain_code > st->prev_gc) {
3448 *gain_code = st->prev_gc;
3449 }
3450 }
3451 st->prev_gc = *gain_code;
3452 }
3453
3454 /* update EC states: previous gain, gain buffer */
3455 st->past_gain_code = *gain_code;
3456 st->gbuf[0] = st->gbuf[1];
3457 st->gbuf[1] = st->gbuf[2];
3458 st->gbuf[2] = st->gbuf[3];
3459 st->gbuf[3] = st->gbuf[4];
3460 st->gbuf[4] = *gain_code;
3461 return;
3462}
3463
3464
3465/*
3466 * d_gain_code
3467 *
3468 *
3469 * Parameters:
3470 * pred_state B: MA predictor state
3471 * mode I: AMR mode (MR795 or MR122)
3472 * index I: received quantization index
3473 * code I: innovation codevector
3474 * gain_code O: decoded innovation gain
3475 *
3476 * Function:
3477 * Decode the fixed codebook gain using the received index
3478 *
3479 * Returns:
3480 * void
3481 */
3482static void d_gain_code(gc_predState *pred_state, enum Mode mode, Word32 index,
3483 Word32 code[], Word32 *gain_code)
3484{
3485 Word32 g_code0, exp, frac, qua_ener_MR122, qua_ener;
3486 Word32 exp_inn_en, frac_inn_en, tmp, tmp2, i;
3487 const Word32 *p;
3488
3489
3490 /*
3491 * Decode codebook gain
3492 */
3493 gc_pred(pred_state, mode, code, &exp, &frac, &exp_inn_en, &frac_inn_en);
3494 p = &qua_gain_code[((index + index) + index)];
3495
3496 /* Different scalings between MR122 and the other modes */
3497 if (mode == MR122) {
3498 /* predicted gain */
3499 g_code0 = Pow2(exp, frac);
3500
3501 if (g_code0 <= 2047) {
3502 g_code0 = g_code0 << 4;
3503 } else {
3504 g_code0 = 32767;
3505 }
3506 *gain_code = ((g_code0 * *p++) >> 15) << 1;
3507 if (*gain_code & 0xFFFF8000) {
3508 *gain_code = 32767;
3509 }
3510
3511 } else {
3512 g_code0 = Pow2(14, frac);
3513 tmp = (*p++ * g_code0) << 1;
3514 exp = 9 - exp;
3515
3516 if (exp > 0) {
3517 tmp = tmp >> exp;
3518 } else {
3519 for (i = exp; i < 0; i++) {
3520 tmp2 = tmp << 1;
3521 if ((tmp ^ tmp2) & 0x80000000) {
3522 tmp = (tmp & 0x80000000) ? 0x80000000 : 0x7FFFFFFF;
3523 break;
3524 } else {
3525 tmp = tmp2;
3526 }
3527 }
3528 }
3529 *gain_code = tmp >> 16;
3530 if (*gain_code & 0xFFFF8000) {
3531 *gain_code = 32767;
3532 }
3533 }
3534
3535 /*
3536 * update table of past quantized energies
3537 */
3538 qua_ener_MR122 = *p++;
3539 qua_ener = *p++;
3540 gc_pred_update(pred_state, qua_ener_MR122, qua_ener);
3541 return;
3542}
3543
3544
3545/*
3546 * Int_lsf
3547 *
3548 *
3549 * Parameters:
3550 * lsf_old I: LSF vector at the 4th subframe of past frame
3551 * lsf_new I: LSF vector at the 4th subframe of present frame
3552 * i_subfr I: current subframe
3553 * lsf_out O: interpolated LSF parameters for current subframe
3554 *
3555 * Function:
3556 * Interpolates the LSFs for selected subframe
3557 *
3558 * The LSFs are interpolated at the 1st, 2nd and 3rd
3559 * ubframe and only forwarded at the 4th subframe.
3560 *
3561 * sf1: 3/4 F0 + 1/4 F1
3562 * sf2: 1/2 F0 + 1/2 F1
3563 * sf3: 1/4 F0 + 3/4 F1
3564 * sf4: F1
3565 *
3566 * Returns:
3567 * void
3568 */
3569static void Int_lsf(Word32 lsf_old[], Word32 lsf_new[], int i_subfr, Word32
3570 lsf_out[])
3571{
3572 Word32 i;
3573
3574
3575 switch (i_subfr) {
3576 case 0:
3577 for (i = 0; i < 10; i++) {
3578 lsf_out[i] = lsf_old[i] - (lsf_old[i] >> 2) + (lsf_new[i] >> 2);
3579 }
3580 break;
3581
3582 case 40:
3583 for (i = 0; i < 10; i++) {
3584 lsf_out[i] = (lsf_old[i] >> 1) + (lsf_new[i] >> 1);
3585 }
3586 break;
3587
3588 case 80:
3589 for (i = 0; i < 10; i++) {
3590 lsf_out[i] = (lsf_old[i] >> 2) - (lsf_new[i] >> 2) +
3591 lsf_new[i];
3592 }
3593 break;
3594
3595 case 120:
3596 memcpy(lsf_out, lsf_new, M << 2);
3597 break;
3598 }
3599}
3600
3601
3602/*
3603 * Cb_gain_average
3604 *
3605 *
3606 * Parameters:
3607 * st->cbGainHistory B: codebook gain history
3608 * st->hangCount B: hangover counter
3609 * mode I: AMR mode
3610 * gain_code I: codebook gain
3611 * lsp I: The LSP for the current frame
3612 * lspAver I: The average of LSP for 8 frames
3613 * bfi I: bad frame indication
3614 * prev_bf I: previous bad frame indication
3615 * pdfi I: potential degraded bad frame indication
3616 * prev_pdf I: previous potential degraded bad frame indication
3617 * inBackgroundNoise I: background noise decision
3618 * voicedHangover I: number of frames after last voiced frame
3619 *
3620 * Function:
3621 * The mixed codebook gain, used to make codebook gain more smooth in background
3622 *
3623 *
3624 * Returns:
3625 * void
3626 */
3627static Word32 Cb_gain_average(Cb_gain_averageState *st, enum Mode mode, Word32
3628 gain_code, Word32 lsp[], Word32 lspAver[], Word16 bfi, Word16 prev_bf,
3629 Word16 pdfi, Word16 prev_pdf, Word32 inBackgroundNoise, Word32
3630 voicedHangover)
3631{
3632 Word32 tmp[M];
3633 Word32 i, cbGainMix, tmp_diff, bgMix, cbGainMean, sum, diff, tmp1, tmp2;
3634 int shift1, shift2, shift;
3635
3636
3637 /* set correct cbGainMix for MR74, MR795, MR122 */
3638 cbGainMix = gain_code;
3639
3640 /*
3641 * Store list of CB gain needed in the CB gain averaging *
3642 */
3643 st->cbGainHistory[0] = st->cbGainHistory[1];
3644 st->cbGainHistory[1] = st->cbGainHistory[2];
3645 st->cbGainHistory[2] = st->cbGainHistory[3];
3646 st->cbGainHistory[3] = st->cbGainHistory[4];
3647 st->cbGainHistory[4] = st->cbGainHistory[5];
3648 st->cbGainHistory[5] = st->cbGainHistory[6];
3649 st->cbGainHistory[6] = gain_code;
3650
3651 /* compute lsp difference */
3652 for (i = 0; i < M; i++) {
3653 tmp1 = labs(lspAver[i] - lsp[i]);
3654 shift1 = 0;
3655 if (tmp1 != 0) {
3656 while (!(tmp1 & 0x2000)) {
3657 shift1++;
3658 tmp1 = tmp1 << 1;
3659 }
3660 }
3661 tmp2 = lspAver[i];
3662 shift2 = 0;
3663 if (tmp2 != 0) {
3664 while (!(tmp2 & 0x4000)) {
3665 shift2++;
3666 tmp2 = tmp2 << 1;
3667 }
3668 }
3669 tmp[i] = (tmp1 << 15) / tmp2;
3670 shift = 2 + shift1 - shift2;
3671
3672 if (shift >= 0) {
3673 tmp[i] = tmp[i] >> shift;
3674 } else {
3675 tmp[i] = tmp[i] << -(shift);
3676 }
3677 }
3678 diff = *tmp + tmp[1] + tmp[2] + tmp[3] + tmp[4] + tmp[5] + tmp[6] + tmp[7] +
3679 tmp[8] + tmp[9];
3680
3681 /* saturate */
3682 if (diff > 32767) {
3683 diff = 32767;
3684 }
3685
3686 /* Compute hangover */
3687 st->hangVar += 1;
3688
3689 if (diff <= 5325) {
3690 st->hangVar = 0;
3691 }
3692
3693 if (st->hangVar > 10) {
3694 /* Speech period, reset hangover variable */
3695 st->hangCount = 0;
3696 }
3697
3698 /* Compute mix constant (bgMix) */
3699 bgMix = 8192;
3700
3701 /* MR475, MR515, MR59, MR67, MR102 */
3702 if ((mode <= MR67) | (mode == MR102)) {
3703 /* disable mix if too short time since */
3704 if ((st->hangCount >= 40) & (diff <= 5325)) { /* 0.65 in Q13 */
3705 /* if errors and presumed noise make smoothing probability stronger */
3706 if (((((pdfi != 0) & (prev_pdf != 0)) | (bfi != 0) | (
3707 prev_bf != 0)) & ((voicedHangover > 1)) & (
3708 inBackgroundNoise != 0) & (mode < MR67))) {
3709 /* bgMix = min(0.25, max(0.0, diff-0.55)) / 0.25; */
3710 tmp_diff = diff - 4506; /* 0.55 in Q13 */
3711
3712 /* max(0.0, diff-0.55) */
3713 tmp1 = 0;
3714
3715 if (tmp_diff > 0) {
3716 tmp1 = tmp_diff;
3717 }
3718
3719 /* min(0.25, tmp1) */
3720 if (2048 >= tmp1) {
3721 bgMix = tmp1 << 2;
3722 }
3723 } else {
3724 /* bgMix = min(0.25, max(0.0, diff-0.40)) / 0.25; */
3725 tmp_diff = diff - 3277; /* 0.4 in Q13 */
3726
3727 /* max(0.0, diff-0.40) */
3728 tmp1 = 0;
3729
3730 if (tmp_diff > 0) {
3731 tmp1 = tmp_diff;
3732 }
3733
3734 /* min(0.25, tmp1) */
3735 if (2048 >= tmp1) {
3736 bgMix = tmp1 << 2;
3737 }
3738 }
3739 }
3740
3741 /*
3742 * Smoothen the cb gain trajectory
3743 * smoothing depends on mix constant bgMix
3744 */
3745 sum = st->cbGainHistory[2] + st->cbGainHistory[3] + st->cbGainHistory[4] +
3746 st->cbGainHistory[5] + st->cbGainHistory[6];
3747
3748 if (sum > 163822) {
3749 cbGainMean = 32767;
3750 } else {
3751 cbGainMean = (3277 * sum + 0x00002000L) >> 14; /* Q1 */
3752 }
3753
3754 /* more smoothing in error and bg noise (NB no DFI used here) */
3755 if (((bfi != 0) | (prev_bf != 0)) & (inBackgroundNoise != 0) & (
3756 mode < MR67)) {
3757 sum = 9362 * (st->cbGainHistory[0] + st->cbGainHistory[1] + st->
3758 cbGainHistory[2] + st->cbGainHistory[3] + st->cbGainHistory[4] +
3759 st->cbGainHistory[5] + st->cbGainHistory[6]);
3760 cbGainMean = (sum + 0x00008000L) >> 16; /* Q1 */
3761 }
3762
3763 /* cbGainMix = bgMix*cbGainMix + (1-bgMix)*cbGainMean; */
3764 sum = bgMix * cbGainMix; /* sum in Q14 */
3765 sum += cbGainMean << 13;
3766 sum -= bgMix * cbGainMean;
3767 cbGainMix = (sum + 0x00001000L) >> 13;
3768
3769 /* Q1 */
3770 }
3771 st->hangCount += 1;
3772 if (st->hangCount & 0x80000000) {
3773 st->hangCount = 40;
3774 }
3775 return cbGainMix;
3776}
3777
3778
3779/*
3780 * ph_disp
3781 *
3782 *
3783 * Parameters:
3784 * state->gainMem B: LTP gain memory
3785 * state->prevCbGain B: Codebook gain memory
3786 * mode I: AMR mode
3787 * x B: LTP excitation signal -> total excitation signal
3788 * cbGain I: Codebook gain
3789 * ltpGain I: LTP gain
3790 * inno B: Innovation vector
3791 * pitch_fac I: pitch factor used to scale the LTP excitation
3792 * tmp_shift I: shift factor applied to sum of scaled LTP ex & innov.
3793 * before rounding
3794 *
3795 * Function:
3796 * Adaptive phase dispersion; forming of total excitation
3797 *
3798 *
3799 * Returns:
3800 * void
3801 */
3802static void ph_disp(ph_dispState *state, enum Mode mode, Word32 x[],
3803 Word32 cbGain, Word32 ltpGain, Word32 inno[],
3804 Word32 pitch_fac, Word32 tmp_shift)
3805{
3806 Word32 inno_sav[L_SUBFR], ps_poss[L_SUBFR];
3807 Word32 i, i1, impNr, temp1, temp2, j, nze, nPulse, ppos;
3808 const Word32 *ph_imp; /* Pointer to phase dispersion filter */
3809
3810
3811 /* Update LTP gain memory */
3812 state->gainMem[4] = state->gainMem[3];
3813 state->gainMem[3] = state->gainMem[2];
3814 state->gainMem[2] = state->gainMem[1];
3815 state->gainMem[1] = state->gainMem[0];
3816 state->gainMem[0] = ltpGain;
3817
3818 /* basic adaption of phase dispersion */
3819 /* no dispersion */
3820 impNr = 2;
3821
3822 /* if (ltpGain < 0.9) */
3823 if (ltpGain < PHDTHR2LTP) {
3824 /* maximum dispersion */
3825 impNr = 0;
3826
3827 /* if (ltpGain > 0.6 */
3828 if (ltpGain > PHDTHR1LTP) {
3829 /* medium dispersion */
3830 impNr = 1;
3831 }
3832 }
3833
3834 /* onset indicator */
3835 /* onset = (cbGain > onFact * cbGainMem[0]) */
3836 temp1 = ((state->prevCbGain * ONFACTPLUS1) + 0x1000) >> 13;
3837
3838 if (cbGain > temp1) {
3839 state->onset = ONLENGTH;
3840 } else {
3841 if (state->onset > 0) {
3842 state->onset--;
3843 }
3844 }
3845
3846 /*
3847 * if not onset, check ltpGain buffer and use max phase dispersion if
3848 * half or more of the ltpGain-parameters say so
3849 */
3850 if (state->onset == 0) {
3851 /* Check LTP gain memory and set filter accordingly */
3852 i1 = 0;
3853
3854 for (i = 0; i < PHDGAINMEMSIZE; i++) {
3855 if (state->gainMem[i] < PHDTHR1LTP) {
3856 i1++;
3857 }
3858 }
3859
3860 if (i1 > 2) {
3861 impNr = 0;
3862 }
3863 }
3864
3865 /* Restrict decrease in phase dispersion to one step if not onset */
3866 if ((impNr > (state->prevState + 1)) & (state->onset == 0)) {
3867 impNr--;
3868 }
3869
3870 /* if onset, use one step less phase dispersion */
3871 if ((impNr < 2) & (state->onset > 0)) {
3872 impNr++;
3873 }
3874
3875 /* disable for very low levels */
3876 if (cbGain < 10) {
3877 impNr = 2;
3878 }
3879
3880 if (state->lockFull == 1) {
3881 impNr = 0;
3882 }
3883
3884 /* update static memory */
3885 state->prevState = impNr;
3886 state->prevCbGain = cbGain;
3887
3888 /*
3889 * do phase dispersion for all modes but 12.2 and 7.4;
3890 * don't modify the innovation if impNr >=2 (= no phase disp)
3891 */
3892 if ((mode != MR122) & (mode != MR102) & (mode != MR74) & (impNr < 2)
3893 ) {
3894 /*
3895 * track pulse positions, save innovation,
3896 * and initialize new innovation
3897 */
3898 nze = 0;
3899
3900 for (i = 0; i < L_SUBFR; i++) {
3901 if (inno[i] != 0) {
3902 ps_poss[nze] = i;
3903 nze++;
3904 }
3905 }
3906 memcpy(inno_sav, inno, L_SUBFR << 2);
3907 memset(inno, 0, L_SUBFR << 2);
3908
3909 /* Choose filter corresponding to codec mode and dispersion criterium */
3910 ph_imp = ph_imp_mid;
3911
3912 if (impNr == 0) {
3913 ph_imp = ph_imp_low;
3914 }
3915
3916 if (mode == MR795) {
3917 ph_imp = ph_imp_mid_MR795;
3918
3919 if (impNr == 0) {
3920 ph_imp = ph_imp_low_MR795;
3921 }
3922 }
3923
3924 /* Do phase dispersion of innovation */
3925 for (nPulse = 0; nPulse < nze; nPulse++) {
3926 ppos = ps_poss[nPulse];
3927
3928 /* circular convolution with impulse response */
3929 j = 0;
3930
3931 for (i = ppos; i < L_SUBFR; i++) {
3932 /* inno[i1] += inno_sav[ppos] * ph_imp[i1-ppos] */
3933 temp1 = (inno_sav[ppos] * ph_imp[j++]) >> 15;
3934 inno[i] = inno[i] + temp1;
3935 }
3936
3937 for (i = 0; i < ppos; i++) {
3938 /* inno[i] += inno_sav[ppos] * ph_imp[L_SUBFR-ppos+i] */
3939 temp1 = (inno_sav[ppos] * ph_imp[j++]) >> 15;
3940 inno[i] = inno[i] + temp1;
3941 }
3942 }
3943 }
3944
3945 /*
3946 * compute total excitation for synthesis part of decoder
3947 * (using modified innovation if phase dispersion is active)
3948 */
3949 for (i = 0; i < L_SUBFR; i++) {
3950 /* x[i] = gain_pit*x[i] + cbGain*code[i]; */
3951 temp1 = x[i] * pitch_fac + inno[i] * cbGain;
3952 temp2 = temp1 << tmp_shift;
3953 x[i] = (temp2 + 0x4000) >> 15;
3954 if (labs(x[i]) > 32767) {
3955 if ((temp1 ^ temp2) & 0x80000000) {
3956 x[i] = (temp1 & 0x80000000) ? -32768 : 32767;
3957 } else {
3958 x[i] = (temp2 & 0x80000000) ? -32768 : 32767;
3959 }
3960 }
3961 }
3962 return;
3963}
3964
3965
3966/*
3967 * sqrt_l_exp
3968 *
3969 *
3970 * Parameters:
3971 * x I: input value
3972 * exp O: right shift to be applied to result
3973 *
3974 * Function:
3975 * Sqrt with exponent value.
3976 *
3977 * y = sqrt(x)
3978 * x = f * 2^-e, 0.5 <= f < 1 (normalization)
3979 * y = sqrt(f) * 2^(-e/2)
3980 *
3981 * a) e = 2k --> y = sqrt(f) * 2^-k
3982 * (k = e div 2, 0.707 <= sqrt(f) < 1)
3983 * b) e = 2k+1 --> y = sqrt(f/2) * 2^-k
3984 * (k = e div 2, 0.5 <= sqrt(f/2) < 0.707)
3985 *
3986 *
3987 * Returns:
3988 * y output value
3989 */
3990static Word32 sqrt_l_exp(Word32 x, Word32 *exp)
3991{
3992 Word32 y, a, i, tmp;
3993 int e;
3994
3995
3996 if (x <= (Word32)0) {
3997 *exp = 0;
3998 return(Word32)0;
3999 }
4000 e = 0;
4001 if (x != 0) {
4002 tmp = x;
4003 while (!(tmp & 0x40000000)) {
4004 e++;
4005 tmp = tmp << 1;
4006 }
4007 }
4008 e = e & 0xFFFE;
4009 x = (x << e);
4010 *exp = (Word16)e;
4011 x = (x >> 9);
4012 i = (Word16)(x >> 16);
4013 x = (x >> 1);
4014 a = x & (Word16)0x7fff;
4015 i = (i - 16);
4016 y = (sqrt_table[i] << 16);
4017 tmp = (sqrt_table[i] - sqrt_table[i + 1]);
4018 y -= (tmp * a) << 1;
4019 return(y);
4020}
4021
4022
4023/*
4024 * Ex_ctrl
4025 *
4026 *
4027 * Parameters:
4028 * excitation B: Current subframe excitation
4029 * excEnergy I: Exc. Energy, sqrt(totEx*totEx)
4030 * exEnergyHist I: History of subframe energies
4031 * voicedHangover I: number of frames after last voiced frame
4032 * prevBFI I: Set i previous bad frame indicators
4033 * carefulFlag I: Restrict dymamic in scaling
4034 *
4035 * Function:
4036 * Charaterice synthesis speech and detect background noise
4037 *
4038 * Returns:
4039 * background noise decision; 0 = no bgn, 1 = bgn
4040 */
4041static Word16 Ex_ctrl(Word32 excitation[], Word32 excEnergy, Word32
4042 exEnergyHist[], Word32 voicedHangover, Word16 prevBFI, Word16 carefulFlag
4043 )
4044{
4045 Word32 i, testEnergy, scaleFactor, avgEnergy, prevEnergy, T0;
4046 int exp;
4047
4048
4049 /* get target level */
4050 avgEnergy = gmed_n(exEnergyHist, 9);
4051 prevEnergy = (exEnergyHist[7] + exEnergyHist[8]) >> 1;
4052
4053 if (exEnergyHist[8] < prevEnergy) {
4054 prevEnergy = exEnergyHist[8];
4055 }
4056
4057 /* upscaling to avoid too rapid energy rises for some cases */
4058 if ((excEnergy<avgEnergy) & (excEnergy>5)) {
4059 /* testEnergy = 4*prevEnergy; */
4060 testEnergy = prevEnergy << 2;
4061
4062 if ((voicedHangover < 7) || prevBFI != 0) {
4063 /* testEnergy = 3*prevEnergy */
4064 testEnergy = testEnergy - prevEnergy;
4065 }
4066
4067 if (avgEnergy > testEnergy) {
4068 avgEnergy = testEnergy;
4069 }
4070
4071 /* scaleFactor=avgEnergy/excEnergy in Q0 */
4072 exp = 0;
4073 if (excEnergy != 0) {
4074 while (!(excEnergy & 0x4000)) {
4075 exp++;
4076 excEnergy = excEnergy << 1;
4077 }
4078 }
4079 excEnergy = 536838144 / excEnergy;
4080 T0 = (avgEnergy * excEnergy) << 1;
4081 T0 = (T0 >> (20 - exp));
4082
4083 if (T0 > 32767) {
4084 /* saturate */
4085 T0 = 32767;
4086 }
4087 scaleFactor = T0;
4088
4089 /* test if scaleFactor > 3.0 */
4090 if ((carefulFlag != 0) & (scaleFactor > 3072)) {
4091 scaleFactor = 3072;
4092 }
4093
4094 /* scale the excitation by scaleFactor */
4095 for (i = 0; i < L_SUBFR; i++) {
4096 T0 = (scaleFactor * excitation[i]) << 1;
4097 T0 = (T0 >> 11);
4098 excitation[i] = T0;
4099 }
4100 }
4101 return 0;
4102}
4103
4104
4105/*
4106 * Inv_sqrt
4107 *
4108 *
4109 * Parameters:
4110 * x I: input value
4111 *
4112 * Function:
4113 * 1/sqrt(x)
4114 *
4115 * Returns:
4116 * y 1/sqrt(x)
4117 */
4118static Word32 Inv_sqrt(Word32 x)
4119{
4120 int i, a, tmp, exp;
4121 Word32 y;
4122
4123
4124 if (x <= (Word32)0) {
4125 return((Word32)0x3fffffffL);
4126 }
4127 exp = 0;
4128 while (!(x & 0x40000000)) {
4129 exp++;
4130 x = x << 1;
4131 }
4132
4133 /* x is normalized */
4134 exp = (30 - exp);
4135
4136 /* If exponent even -> shift right */
4137 if ((exp & 1) == 0) {
4138 x = (x >> 1);
4139 }
4140 exp = (exp >> 1);
4141 exp = (exp + 1);
4142 x = (x >> 9);
4143
4144 /* Extract b25-b31 */
4145 i = (Word16)(x >> 16);
4146
4147 /* Extract b10-b24 */
4148 x = (x >> 1);
4149 a = x & (Word16)0x7fff;
4150 i = (i - 16);
4151
4152 /* table[i] << 16 */
4153 y = inv_sqrt_table[i] << 16;
4154
4155 /* table[i] - table[i+1]) */
4156 tmp = (inv_sqrt_table[i] - inv_sqrt_table[i + 1]);
4157
4158 /* y -= tmp*a*2 */
4159 y -= (tmp * a) << 1;
4160
4161 /* denormalization */
4162 y = (y >> exp);
4163 return(y);
4164}
4165
4166
4167/*
4168 * energy_old
4169 *
4170 *
4171 * Parameters:
4172 * in I: input value
4173 *
4174 * Function:
4175 * Energy of signal
4176 *
4177 * Returns:
4178 * Energy
4179 */
4180static Word32 energy_old(Word32 in[])
4181{
4182 Word32 temp, i, sum = 0;
4183
4184
4185 for (i = 0; i < L_SUBFR; i += 8) {
4186 temp = in[i] >> 2;
4187 sum += temp * temp;
4188 temp = in[i + 1] >> 2;
4189 sum += temp * temp;
4190 temp = in[i + 2] >> 2;
4191 sum += temp * temp;
4192 temp = in[i + 3] >> 2;
4193 sum += temp * temp;
4194 temp = in[i + 4] >> 2;
4195 sum += temp * temp;
4196 temp = in[i + 5] >> 2;
4197 sum += temp * temp;
4198 temp = in[i + 6] >> 2;
4199 sum += temp * temp;
4200 temp = in[i + 7] >> 2;
4201 sum += temp * temp;
4202 }
4203
4204 if (sum & 0xC0000000) {
4205 return 0x7FFFFFFF;
4206 }
4207 return(sum << 1);
4208}
4209
4210
4211/*
4212 * energy_new
4213 *
4214 *
4215 * Parameters:
4216 * in I: input value
4217 *
4218 * Function:
4219 * Energy of signal
4220 *
4221 * Returns:
4222 * Energy
4223 */
4224static Word32 energy_new(Word32 in[])
4225{
4226 Word32 i, s = 0, overflow = 0;
4227
4228 s += in[0] * in[0];
4229 for (i = 1; i < L_SUBFR; i += 3) {
4230 s += in[i] * in[i];
4231 s += in[i + 1] * in[i + 1];
4232 s += in[i + 2] * in[i + 2];
4233
4234
4235 if (s & 0xC0000000) {
4236 overflow = 1;
4237 break;
4238 }
4239 }
4240
4241 /* check for overflow */
4242 if (overflow) {
4243 s = energy_old(in);
4244 } else {
4245 s = (s >> 3);
4246 }
4247 return s;
4248}
4249
4250
4251/*
4252 * agc2
4253 *
4254 *
4255 * Parameters:
4256 * sig_in I: Post_Filter input signal
4257 * sig_out B: Post_Filter output signal
4258 *
4259 * Function:
4260 * Scales the excitation on a subframe basis
4261 *
4262 * Returns:
4263 * Energy
4264 */
4265static void agc2(Word32 *sig_in, Word32 *sig_out)
4266{
4267 Word32 s;
4268 int i, exp;
4269 Word16 gain_in, gain_out, g0;
4270
4271
4272 /* calculate gain_out with exponent */
4273 s = energy_new(sig_out);
4274
4275 if (s == 0) {
4276 return;
4277 }
4278 exp = 0;
4279 while (!(s & 0x20000000)) {
4280 exp++;
4281 s = s << 1;
4282 }
4283
4284 gain_out = (Word16)((s + 0x00008000L) >> 16);
4285
4286 /* calculate gain_in with exponent */
4287 s = energy_new(sig_in);
4288
4289 if (s == 0) {
4290 g0 = 0;
4291 } else {
4292 i = 0;
4293 while (!(s & 0x40000000)) {
4294 i++;
4295 s = s << 1;
4296 }
4297
4298 if (s < 0x7fff7fff) {
4299 gain_in = (Word16)((s + 0x00008000L) >> 16);
4300 } else {
4301 gain_in = 32767;
4302 }
4303 exp = (exp - i);
4304
4305 /*
4306 * g0 = sqrt(gain_in/gain_out);
4307 */
4308 /* s = gain_out / gain_in */
4309 s = (gain_out << 15) / gain_in;
4310 s = (s << 7);
4311
4312 if (exp > 0) {
4313 s = (s >> exp);
4314 } else {
4315 s = (s << (-exp));
4316 }
4317 s = Inv_sqrt(s);
4318 g0 = (Word16)(((s << 9) + 0x00008000L) >> 16);
4319 }
4320
4321 /* sig_out(n) = gain(n) * sig_out(n) */
4322 for (i = 0; i < L_SUBFR; i++) {
4323 sig_out[i] = (sig_out[i] * g0) >> 12;
4324 }
4325 return;
4326}
4327
4328
4329/*
4330 * Bgn_scd
4331 *
4332 *
4333 * Parameters:
4334 * st->frameEnergyHist B: Frame Energy memory
4335 * st->bgHangover B: Background hangover counter
4336 * ltpGainHist I: LTP gain history
4337 * speech I: synthesis speech frame
4338 * voicedHangover O: number of frames after last voiced frame
4339 *
4340 * Function:
4341 * Charaterice synthesis speech and detect background noise
4342 *
4343 * Returns:
4344 * inbgNoise background noise decision; 0 = no bgn, 1 = bgn
4345 */
4346static Word16 Bgn_scd(Bgn_scdState *st, Word32 ltpGainHist[], Word32 speech[],
4347 Word32 *voicedHangover)
4348{
4349 Word32 temp, ltpLimit, frame_energyMin, currEnergy, noiseFloor, maxEnergy,
4350 maxEnergyLastPart, s, i;
4351 Word16 prevVoiced, inbgNoise;
4352
4353
4354 /*
4355 * Update the inBackgroundNoise flag (valid for use in next frame if BFI)
4356 * it now works as a energy detector floating on top
4357 * not as good as a VAD.
4358 */
4359 s = 0;
4360
4361 for (i = 0; i < L_FRAME; i++) {
4362 s += speech[i] * speech[i];
4363 }
4364
4365 if ((s < 0xFFFFFFF) & (s >= 0)) {
4366 currEnergy = s >> 13;
4367 } else {
4368 currEnergy = 32767;
4369 }
4370 frame_energyMin = 32767;
4371
4372 for (i = 0; i < L_ENERGYHIST; i++) {
4373 if (st->frameEnergyHist[i] < frame_energyMin) {
4374 frame_energyMin = st->frameEnergyHist[i];
4375 }
4376 }
4377
4378 /* Frame Energy Margin of 16 */
4379 noiseFloor = frame_energyMin << 4;
4380 maxEnergy = st->frameEnergyHist[0];
4381
4382 for (i = 1; i < L_ENERGYHIST - 4; i++) {
4383 if (maxEnergy < st->frameEnergyHist[i]) {
4384 maxEnergy = st->frameEnergyHist[i];
4385 }
4386 }
4387 maxEnergyLastPart = st->frameEnergyHist[2 * L_ENERGYHIST / 3];
4388
4389 for (i = 2 * L_ENERGYHIST / 3 + 1; i < L_ENERGYHIST; i++) {
4390 if (maxEnergyLastPart < st->frameEnergyHist[i]) {
4391 maxEnergyLastPart = st->frameEnergyHist[i];
4392 }
4393 }
4394
4395 /* false */
4396 inbgNoise = 0;
4397
4398 /*
4399 * Do not consider silence as noise
4400 * Do not consider continuous high volume as noise
4401 * Or if the current noise level is very low
4402 * Mark as noise if under current noise limit
4403 * OR if the maximum energy is below the upper limit
4404 */
4405 if ((maxEnergy > LOWERNOISELIMIT) & (currEnergy < FRAMEENERGYLIMIT) & (
4406 currEnergy > LOWERNOISELIMIT) & ((currEnergy < noiseFloor) || (
4407 maxEnergyLastPart < UPPERNOISELIMIT))) {
4408 if ((st->bgHangover + 1) > 30) {
4409 st->bgHangover = 30;
4410 } else {
4411 st->bgHangover += 1;
4412 }
4413 } else {
4414 st->bgHangover = 0;
4415 }
4416
4417 /* make final decision about frame state, act somewhat cautiosly */
4418 if (st->bgHangover > 1) {
4419 inbgNoise = 1; /* true */
4420 }
4421
4422 for (i = 0; i < L_ENERGYHIST - 1; i++) {
4423 st->frameEnergyHist[i] = st->frameEnergyHist[i + 1];
4424 }
4425 st->frameEnergyHist[L_ENERGYHIST - 1] = currEnergy;
4426
4427 /*
4428 * prepare for voicing decision;
4429 * tighten the threshold after some time in noise
4430 */
4431 ltpLimit = 13926; /* 0.85 Q14 */
4432
4433 if (st->bgHangover > 8) {
4434 ltpLimit = 15565; /* 0.95 Q14 */
4435 }
4436
4437 if (st->bgHangover > 15) {
4438 ltpLimit = 16383; /* 1.00 Q14 */
4439 }
4440
4441 /* weak sort of voicing indication. */
4442 prevVoiced = 0; /* false */
4443
4444 if (gmed_n(&ltpGainHist[4], 5) > ltpLimit) {
4445 prevVoiced = 1; /* true */
4446 }
4447
4448 if (st->bgHangover > 20) {
4449 if (gmed_n(ltpGainHist, 9) > ltpLimit) {
4450 prevVoiced = 1; /* true */
4451 } else {
4452 prevVoiced = 0; /* false */
4453 }
4454 }
4455
4456 if (prevVoiced) {
4457 *voicedHangover = 0;
4458 } else {
4459 temp = *voicedHangover + 1;
4460
4461 if (temp > 10) {
4462 *voicedHangover = 10;
4463 } else {
4464 *voicedHangover = temp;
4465 }
4466 }
4467 return inbgNoise;
4468}
4469
4470
4471/*
4472 * dtx_dec_activity_update
4473 *
4474 *
4475 * Parameters:
4476 * st->lsf_hist_ptr B: LSF history pointer
4477 * st->lsf_hist B: LSF history
4478 * lsf I: lsf
4479 * frame I: noise frame
4480 *
4481 * Function:
4482 * Update lsp history and compute log energy.
4483 *
4484 * Returns:
4485 * void
4486 */
4487static void dtx_dec_activity_update(dtx_decState *st, Word32 lsf[], Word32
4488 frame[])
4489{
4490 Word32 frame_en;
4491 Word32 log_en_e, log_en_m, log_en, i;
4492
4493
4494 /* update lsp history */
4495 st->lsf_hist_ptr += M;
4496
4497 if (st->lsf_hist_ptr == 80) {
4498 st->lsf_hist_ptr = 0;
4499 }
4500 memcpy(&st->lsf_hist[st->lsf_hist_ptr], lsf, M << 2);
4501
4502 /* compute log energy based on frame energy */
4503 frame_en = 0; /* Q0 */
4504
4505 for (i = 0; (i < L_FRAME); i ++) {
4506 frame_en += frame[i] * frame[i];
4507 if (frame_en & 0x80000000) {
4508 break;
4509 }
4510 }
4511
4512 log_en = (frame_en & 0xC0000000) ? 0x7FFFFFFE : (Word32)frame_en << 1;
4513
4514 Log2(log_en , &log_en_e, &log_en_m);
4515
4516 /* convert exponent and mantissa to Word16 Q10 */
4517 log_en = log_en_e << 10; /* Q10 */
4518 log_en = log_en + (log_en_m >> 5);
4519
4520 /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */
4521 log_en = log_en - 8521;
4522
4523 /*
4524 * insert into log energy buffer, no division by two as
4525 * log_en in decoder is Q11
4526 */
4527 st->log_en_hist_ptr += 1;
4528
4529 if (st->log_en_hist_ptr == DTX_HIST_SIZE) {
4530 st->log_en_hist_ptr = 0;
4531 }
4532 st->log_en_hist[st->log_en_hist_ptr] = log_en; /* Q11 */
4533}
4534
4535
4536/*
4537 * Decoder_amr
4538 *
4539 *
4540 * Parameters:
4541 * st B: State variables
4542 * mode I: AMR mode
4543 * parm I: vector of synthesis parameters
4544 * frame_type I: received frame type
4545 * synth O: synthesis speech
4546 * A_t O: decoded LP filter in 4 subframes
4547 *
4548 * Function:
4549 * Speech decoder routine
4550 *
4551 * Returns:
4552 * void
4553 */
4554static void Decoder_amr(Decoder_amrState *st, enum Mode mode, Word16 parm[],
4555 enum RXFrameType frame_type, Word32 synth[], Word32 A_t[])
4556{
4557 /* LSPs */
4558 Word32 lsp_new[M];
4559 Word32 lsp_mid[M];
4560
4561
4562 /* LSFs */
4563 Word32 prev_lsf[M];
4564 Word32 lsf_i[M];
4565
4566
4567 /* Algebraic codevector */
4568 Word32 code[L_SUBFR];
4569
4570
4571 /* excitation */
4572 Word32 excp[L_SUBFR];
4573 Word32 exc_enhanced[L_SUBFR];
4574
4575
4576 /* Scalars */
4577 Word32 i, i_subfr, overflow, T0_frac, index, temp, temp2, subfrNr, excEnergy;
4578 Word32 gain_code, gain_code_mix, pit_sharp, pit_flag, pitch_fac, t0_min, t0_max;
4579 Word32 gain_pit = 0, evenSubfr = 0, T0 = 0, index_mr475 = 0;
4580 Word32 *Az; /* Pointer on A_t */
4581 Word16 flag4, carefulFlag;
4582 Word16 delta_frc_low, delta_frc_range, tmp_shift;
4583 Word16 bfi = 0, pdfi = 0;
4584 /* bad frame indication flag, potential degraded bad frame flag */
4585
4586
4587 enum DTXStateType newDTXState; /* SPEECH , DTX, DTX_MUTE */
4588
4589 /* find the new DTX state SPEECH OR DTX */
4590 newDTXState = rx_dtx_handler(st->dtxDecoderState, frame_type);
4591
4592 /* DTX actions */
4593 if (newDTXState != SPEECH) {
4594 Decoder_amr_reset(st, MRDTX);
4595 dtx_dec(st->dtxDecoderState, st->mem_syn, st->lsfState, st->pred_state,
4596 st->Cb_gain_averState, newDTXState, mode, parm, synth, A_t);
4597
4598 /* update average lsp */
4599 Lsf_lsp(st->lsfState->past_lsf_q, st->lsp_old);
4600 lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q);
4601 goto theEnd;
4602 }
4603
4604 /* SPEECH action state machine */
4605 if (table_speech_bad[frame_type]) {
4606 bfi = 1;
4607
4608 if (frame_type != RX_SPEECH_BAD) {
4609 Build_CN_param(&st->nodataSeed, mode, parm);
4610 }
4611 } else if (frame_type == RX_SPEECH_DEGRADED) {
4612 pdfi = 1;
4613 }
4614
4615 if (bfi != 0) {
4616 st->state += 1;
4617 } else if (st->state == 6) {
4618 st->state = 5;
4619 } else {
4620 st->state = 0;
4621 }
4622
4623 if (st->state > 6) {
4624 st->state = 6;
4625 }
4626
4627 /*
4628 * If this frame is the first speech frame after CNI period,
4629 * set the BFH state machine to an appropriate state depending
4630 * on whether there was DTX muting before start of speech or not
4631 * If there was DTX muting, the first speech frame is muted.
4632 * If there was no DTX muting, the first speech frame is not
4633 * muted. The BFH state machine starts from state 5, however, to
4634 * keep the audible noise resulting from a SID frame which is
4635 * erroneously interpreted as a good speech frame as small as
4636 * possible (the decoder output in this case is quickly muted)
4637 */
4638 if (st->dtxDecoderState->dtxGlobalState == DTX) {
4639 st->state = 5;
4640 st->prev_bf = 0;
4641 } else if (st->dtxDecoderState->dtxGlobalState == DTX_MUTE) {
4642 st->state = 5;
4643 st->prev_bf = 1;
4644 }
4645
4646 /* save old LSFs for CB gain smoothing */
4647 memcpy(prev_lsf, st->lsfState->past_lsf_q, M << 2);
4648
4649 /*
4650 * decode LSF parameters and generate interpolated lpc coefficients
4651 * for the 4 subframes
4652 */
4653 if (mode != MR122) {
4654 D_plsf_3(st->lsfState, mode, bfi, parm, lsp_new);
4655
4656 /* Advance synthesis parameters pointer */
4657 parm += 3;
4658 Int_lpc_1to3(st->lsp_old, lsp_new, A_t);
4659 } else {
4660 D_plsf_5(st->lsfState, bfi, parm, lsp_mid, lsp_new);
4661
4662 /* Advance synthesis parameters pointer */
4663 parm += 5;
4664 Int_lpc_1and3(st->lsp_old, lsp_mid, lsp_new, A_t);
4665 }
4666
4667 /* update the LSPs for the next frame */
4668 memcpy(st->lsp_old, lsp_new, M << 2);
4669
4670 /*
4671 * Loop for every subframe in the analysis frame
4672 *
4673 * The subframe size is L_SUBFR and the loop is repeated
4674 * L_FRAME/L_SUBFR times *
4675 * - decode the pitch delay
4676 * - decode algebraic code
4677 * - decode pitch and codebook gains
4678 * - find the excitation and compute synthesis speech
4679 */
4680 /* pointer to interpolated LPC parameters */
4681 Az = A_t;
4682 evenSubfr = 0;
4683 subfrNr = -1;
4684
4685 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) {
4686 subfrNr += 1;
4687 evenSubfr = 1 - evenSubfr;
4688
4689 /* flag for first and 3th subframe */
4690 pit_flag = i_subfr;
4691
4692 if (i_subfr == L_FRAME_BY2) {
4693 if ((mode != MR475) & (mode != MR515)) {
4694 pit_flag = 0;
4695 }
4696 }
4697
4698 /* pitch index */
4699 index = *parm++;
4700
4701 /*
4702 * decode pitch lag and find adaptive codebook vector.
4703 */
4704 if (mode != MR122) {
4705 /*
4706 * flag4 indicates encoding with 4 bit resolution;
4707 * this is needed for mode MR475, MR515, MR59 and MR67
4708 */
4709 flag4 = 0;
4710
4711 if ((mode == MR475) || (mode == MR515) || (mode == MR59) || (
4712 mode == MR67)) {
4713 flag4 = 1;
4714 }
4715
4716 /*
4717 * get ranges for the t0_min and t0_max
4718 * only needed in delta decoding
4719 */
4720 delta_frc_low = 5;
4721 delta_frc_range = 9;
4722
4723 if (mode == MR795) {
4724 delta_frc_low = 10;
4725 delta_frc_range = 19;
4726 }
4727 t0_min = st->old_T0 - delta_frc_low;
4728
4729 if (t0_min < PIT_MIN) {
4730 t0_min = PIT_MIN;
4731 }
4732 t0_max = t0_min + delta_frc_range;
4733
4734 if (t0_max > PIT_MAX) {
4735 t0_max = PIT_MAX;
4736 t0_min = t0_max - delta_frc_range;
4737 }
4738 Dec_lag3(index, t0_min, t0_max, pit_flag, st->old_T0, &T0, &T0_frac,
4739 flag4);
4740 st->T0_lagBuff = T0;
4741
4742 if (bfi != 0) {
4743 if (st->old_T0 < PIT_MAX) {
4744 /* Graceful pitch degradation */
4745 st->old_T0 += 1;
4746 }
4747 T0 = st->old_T0;
4748 T0_frac = 0;
4749
4750 if ((st->inBackgroundNoise != 0) & (st->voicedHangover > 4) & (
4751 (mode == MR475) || (mode == MR515) || (mode == MR59))) {
4752 T0 = st->T0_lagBuff;
4753 }
4754 }
4755 Pred_lt_3or6_40(st->exc, T0, T0_frac, 1);
4756 } else {
4757 Dec_lag6(index, PIT_MIN_MR122, PIT_MAX, pit_flag, &T0, &T0_frac);
4758
4759 if ((bfi != 0) || ((pit_flag != 0) & (index > 60))) {
4760 st->T0_lagBuff = T0;
4761 T0 = st->old_T0;
4762 T0_frac = 0;
4763 }
4764 Pred_lt_3or6_40(st->exc, T0, T0_frac, 0);
4765 }
4766
4767 /*
4768 * (MR122 only: Decode pitch gain.)
4769 * Decode innovative codebook.
4770 * set pitch sharpening factor
4771 */
4772 /* MR475, MR515 */
4773 if ((mode == MR475) || (mode == MR515)) {
4774 /* index of position */
4775 index = *parm++;
4776
4777 /* signs */
4778 i = *parm++;
4779 decode_2i40_9bits(subfrNr, i, index, code);
4780 pit_sharp = st->sharp << 1;
4781 }
4782
4783 /* MR59 */
4784 else if (mode == MR59) {
4785 /* index of position */
4786 index = *parm++;
4787
4788 /* signs */
4789 i = *parm++;
4790 decode_2i40_11bits(i, index, code);
4791 pit_sharp = st->sharp << 1;
4792 }
4793
4794 /* MR67 */
4795 else if (mode == MR67) {
4796 /* index of position */
4797 index = *parm++;
4798
4799 /* signs */
4800 i = *parm++;
4801 decode_3i40_14bits(i, index, code);
4802 pit_sharp = st->sharp << 1;
4803 }
4804
4805 /* MR74, MR795 */
4806 else if (mode <= MR795) {
4807 /* index of position */
4808 index = *parm++;
4809
4810 /* signs */
4811 i = *parm++;
4812 decode_4i40_17bits(i, index, code);
4813 pit_sharp = st->sharp << 1;
4814 }
4815
4816 /* MR102 */
4817 else if (mode == MR102) {
4818 decode_8i40_31bits(parm, code);
4819 parm += 7;
4820 pit_sharp = st->sharp << 1;
4821 }
4822
4823 /* MR122 */
4824 else {
4825 index = *parm++;
4826
4827 if (bfi != 0) {
4828 ec_gain_pitch(st->ec_gain_p_st, st->state, &gain_pit);
4829 } else {
4830 gain_pit = d_gain_pitch(mode, index);
4831 }
4832 ec_gain_pitch_update(st->ec_gain_p_st, bfi, st->prev_bf, &gain_pit);
4833 decode_10i40_35bits(parm, code);
4834 parm += 10;
4835
4836 /*
4837 * pit_sharp = gain_pit;
4838 * if (pit_sharp > 1.0) pit_sharp = 1.0;
4839 */
4840 pit_sharp = gain_pit;
4841
4842 if (pit_sharp > 16383) {
4843 pit_sharp = 32767;
4844 } else {
4845 pit_sharp *= 2;
4846 }
4847 }
4848
4849 /*
4850 * Add the pitch contribution to code[].
4851 */
4852 for (i = T0; i < L_SUBFR; i++) {
4853 temp = (code[i - T0] * pit_sharp) >> 15;
4854 code[i] = code[i] + temp;
4855 }
4856
4857 /*
4858 * Decode codebook gain (MR122) or both pitch
4859 * gain and codebook gain (all others)
4860 * Update pitch sharpening "sharp" with quantized gain_pit
4861 */
4862 if (mode == MR475) {
4863 /* read and decode pitch and code gain */
4864 if (evenSubfr != 0) {
4865 /* index of gain(s) */
4866 index_mr475 = *parm++;
4867 }
4868
4869 if (bfi == 0) {
4870 Dec_gain(st->pred_state, mode, index_mr475, code, evenSubfr, &
4871 gain_pit, &gain_code);
4872 } else {
4873 ec_gain_pitch(st->ec_gain_p_st, st->state, &gain_pit);
4874 ec_gain_code(st->ec_gain_c_st, st->pred_state, st->state, &
4875 gain_code);
4876 }
4877 ec_gain_pitch_update(st->ec_gain_p_st, bfi, st->prev_bf, &gain_pit);
4878 ec_gain_code_update(st->ec_gain_c_st, bfi, st->prev_bf, &gain_code);
4879 pit_sharp = gain_pit;
4880
4881 if (pit_sharp > SHARPMAX) {
4882 pit_sharp = SHARPMAX;
4883 }
4884 } else if ((mode <= MR74) || (mode == MR102)) {
4885 /* read and decode pitch and code gain */
4886 /* index of gain(s) */
4887 index = *parm++;
4888
4889 if (bfi == 0) {
4890 Dec_gain(st->pred_state, mode, index, code, evenSubfr, &gain_pit, &
4891 gain_code);
4892 } else {
4893 ec_gain_pitch(st->ec_gain_p_st, st->state, &gain_pit);
4894 ec_gain_code(st->ec_gain_c_st, st->pred_state, st->state, &
4895 gain_code);
4896 }
4897 ec_gain_pitch_update(st->ec_gain_p_st, bfi, st->prev_bf, &gain_pit);
4898 ec_gain_code_update(st->ec_gain_c_st, bfi, st->prev_bf, &gain_code);
4899 pit_sharp = gain_pit;
4900
4901 if (pit_sharp > SHARPMAX) {
4902 pit_sharp = SHARPMAX;
4903 }
4904
4905 if (mode == MR102) {
4906 if (st->old_T0 > (L_SUBFR + 5)) {
4907 pit_sharp = pit_sharp >> 2;
4908 }
4909 }
4910 } else {
4911 /* read and decode pitch gain */
4912 /* index of gain(s) */
4913 index = *parm++;
4914
4915 if (mode == MR795) {
4916 /* decode pitch gain */
4917 if (bfi != 0) {
4918 ec_gain_pitch(st->ec_gain_p_st, st->state, &gain_pit);
4919 } else {
4920 gain_pit = d_gain_pitch(mode, index);
4921 }
4922 ec_gain_pitch_update(st->ec_gain_p_st, bfi, st->prev_bf, &gain_pit
4923 );
4924
4925 /* read and decode code gain */
4926 index = *parm++;
4927
4928 if (bfi == 0) {
4929 d_gain_code(st->pred_state, mode, index, code, &gain_code);
4930 } else {
4931 ec_gain_code(st->ec_gain_c_st, st->pred_state, st->state, &
4932 gain_code);
4933 }
4934 ec_gain_code_update(st->ec_gain_c_st, bfi, st->prev_bf, &gain_code
4935 );
4936 pit_sharp = gain_pit;
4937
4938 if (pit_sharp > SHARPMAX) {
4939 pit_sharp = SHARPMAX;
4940 }
4941 } else { /* MR122 */
4942
4943 if (bfi == 0) {
4944 d_gain_code(st->pred_state, mode, index, code, &gain_code);
4945 } else {
4946 ec_gain_code(st->ec_gain_c_st, st->pred_state, st->state, &
4947 gain_code);
4948 }
4949 ec_gain_code_update(st->ec_gain_c_st, bfi, st->prev_bf, &gain_code
4950 );
4951 pit_sharp = gain_pit;
4952 }
4953 }
4954
4955 /*
4956 * store pitch sharpening for next subframe
4957 * (for modes which use the previous pitch gain for
4958 * pitch sharpening in the search phase)
4959 * do not update sharpening in even subframes for MR475
4960 */
4961 if ((mode != MR475) || evenSubfr == 0) {
4962 st->sharp = gain_pit;
4963
4964 if (st->sharp > SHARPMAX) {
4965 st->sharp = SHARPMAX;
4966 }
4967 }
4968
4969 if (pit_sharp > 16383) {
4970 pit_sharp = 32767;
4971 } else {
4972 pit_sharp *= 2;
4973 }
4974
4975 if (pit_sharp > 16384) {
4976 for (i = 0; i < L_SUBFR; i++) {
4977 temp = (st->exc[i] * pit_sharp) >> 15;
4978 temp2 = (temp * gain_pit) << 1;
4979
4980 if (mode == MR122) {
4981 temp2 = (temp2 >> 1);
4982 }
4983 excp[i] = (temp2 + 0x00008000L) >> 16;
4984 }
4985 }
4986
4987 /*
4988 * Store list of LTP gains needed in the source
4989 * characteristic detector (SCD)
4990 */
4991 if (bfi == 0) {
4992 for (i = 0; i < 8; i++) {
4993 st->ltpGainHistory[i] = st->ltpGainHistory[i + 1];
4994 }
4995 st->ltpGainHistory[8] = gain_pit;
4996 }
4997
4998
4999 /*
5000 * Limit gain_pit if in background noise and BFI
5001 * for MR475, MR515, MR59
5002 */
5003 if ((st->prev_bf != 0 || bfi != 0) & (st->inBackgroundNoise != 0) & (
5004 (mode == MR475) || (mode == MR515) || (mode == MR59))) {
5005 /* if (gain_pit > 0.75) in Q14*/
5006 if (gain_pit > 12288)
5007 /* gain_pit = (gain_pit-0.75)/2.0 + 0.75; */
5008 {
5009 gain_pit = ((gain_pit - 12288) >> 1) + 12288;
5010 }
5011
5012 /* if (gain_pit > 0.90) in Q14*/
5013 if (gain_pit > 14745) {
5014 gain_pit = 14745;
5015 }
5016 }
5017
5018 /*
5019 * Calculate CB mixed gain
5020 */
5021 Int_lsf(prev_lsf, st->lsfState->past_lsf_q, i_subfr, lsf_i);
5022 gain_code_mix = Cb_gain_average(st->Cb_gain_averState, mode, gain_code,
5023 lsf_i, st->lsp_avg_st->lsp_meanSave, bfi, st->prev_bf, pdfi, st->
5024 prev_pdf, st->inBackgroundNoise, st->voicedHangover);
5025
5026 /* make sure that MR74, MR795, MR122 have original codeGain*/
5027 /* MR74, MR795, MR122 */
5028 if ((mode > MR67) & (mode != MR102)) {
5029 gain_code_mix = gain_code;
5030 }
5031
5032 /*
5033 * Find the total excitation.
5034 * Find synthesis speech corresponding to st->exc[].
5035 */
5036 /* MR475, MR515, MR59, MR67, MR74, MR795, MR102*/
5037 if (mode <= MR102) {
5038 pitch_fac = gain_pit;
5039 tmp_shift = 1;
5040 }
5041
5042 /* MR122 */
5043 else {
5044 pitch_fac = gain_pit >> 1;
5045 tmp_shift = 2;
5046 }
5047
5048 /*
5049 * copy unscaled LTP excitation to exc_enhanced (used in phase
5050 * dispersion below) and compute total excitation for LTP feedback
5051 */
5052 memcpy(exc_enhanced, st->exc, L_SUBFR << 2);
5053
5054 for (i = 0; i < L_SUBFR; i++) {
5055 /* st->exc[i] = gain_pit*st->exc[i] + gain_code*code[i]; */
5056 temp = (st->exc[i] * pitch_fac) + (code[i] * gain_code);
5057 temp2 = (temp << tmp_shift);
5058 if (((temp2 >> 1) ^ temp2) & 0x40000000) {
5059 if ((temp ^ temp2) & 0x80000000) {
5060 temp2 = (temp & 0x80000000) ? (-1073741824L) : 1073725439;
5061 } else {
5062 temp2 = (temp2 & 0x80000000) ? (-1073741824L) : 1073725439;
5063 }
5064 }
5065 st->exc[i] = (temp2 + 0x00004000L) >> 15;
5066 }
5067 /*
5068 * Adaptive phase dispersion
5069 */
5070
5071 /* free phase dispersion adaption */
5072 st->ph_disp_st->lockFull = 0;
5073
5074 if (((mode == MR475) || (mode == MR515) || (mode == MR59)) & (st
5075 ->voicedHangover > 3) & (st->inBackgroundNoise != 0) & (bfi != 0
5076 )) {
5077 /*
5078 * Always Use full Phase Disp.
5079 * if error in bg noise
5080 */
5081 st->ph_disp_st->lockFull = 1;
5082 }
5083
5084 /*
5085 * apply phase dispersion to innovation (if enabled) and
5086 * compute total excitation for synthesis part
5087 */
5088 ph_disp(st->ph_disp_st, mode, exc_enhanced, gain_code_mix, gain_pit, code
5089 , pitch_fac, tmp_shift);
5090
5091 /*
5092 * The Excitation control module are active during BFI.
5093 * Conceal drops in signal energy if in bg noise.
5094 */
5095 temp2 = 0;
5096
5097 for (i = 0; i < L_SUBFR; i++) {
5098 temp2 += (exc_enhanced[i] * exc_enhanced[i]);
5099 }
5100
5101 if (temp2 > 0x3FFFFFFF) {
5102 excEnergy = 11584;
5103 } else {
5104 temp2 = sqrt_l_exp(temp2, &temp);
5105 temp2 = (temp2 >> ((temp >> 1) + 15));
5106 excEnergy = temp2 >> 2;
5107 }
5108
5109 if (((mode == MR475) || (mode == MR515) || (mode == MR59)) & (st
5110 ->voicedHangover > 5) & (st->inBackgroundNoise != 0) & (st->
5111 state < 4) & (((pdfi != 0) & (st->prev_pdf != 0)) || bfi !=
5112 0 || st->prev_bf != 0)) {
5113 carefulFlag = 0;
5114
5115 if ((pdfi != 0) & (bfi == 0)) {
5116 carefulFlag = 1;
5117 }
5118 Ex_ctrl(exc_enhanced, excEnergy, st->excEnergyHist, st->voicedHangover
5119 , st->prev_bf, carefulFlag);
5120 }
5121
5122 if ((st->inBackgroundNoise != 0) & (bfi != 0 || st->prev_bf != 0) & (
5123 st->state < 4)) {
5124 ; /* do nothing! */
5125 } else {
5126 /* Update energy history for all modes */
5127 for (i = 0; i < 8; i++) {
5128 st->excEnergyHist[i] = st->excEnergyHist[i + 1];
5129 }
5130 st->excEnergyHist[8] = excEnergy;
5131 }
5132
5133 /*
5134 * Excitation control module end.
5135 */
5136 if (pit_sharp > 16384) {
5137 for (i = 0; i < L_SUBFR; i++) {
5138 excp[i] = excp[i] + exc_enhanced[i];
5139 if (labs(excp[i]) > 32767) {
5140 excp[i] = (excp[i] & 0x80000000) ? -32768 : 32767;
5141 }
5142 }
5143 agc2(exc_enhanced, excp);
5144 overflow = Syn_filt(Az, excp, &synth[i_subfr], L_SUBFR, st->mem_syn, 0
5145 );
5146 } else {
5147 overflow = Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, st->
5148 mem_syn, 0);
5149 }
5150
5151 if (overflow) {
5152 for (i = 0; i < PIT_MAX + L_INTERPOL + L_SUBFR; i++) {
5153 st->old_exc[i] = st->old_exc[i] >> 2;
5154 }
5155
5156 for (i = 0; i < L_SUBFR; i++) {
5157 exc_enhanced[i] = exc_enhanced[i] >> 2;
5158 }
5159 Syn_filt_overflow(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, st->mem_syn, 1);
5160 } else {
5161 memcpy(st->mem_syn, &synth[i_subfr + 30], 40);
5162 }
5163
5164 /*
5165 * Update signal for next frame.
5166 * -> shift to the left by L_SUBFR st->exc[]
5167 */
5168 memcpy(&st->old_exc[0], &st->old_exc[L_SUBFR], (PIT_MAX + L_INTERPOL) <<
5169 2);
5170
5171 /* interpolated LPC parameters for next subframe */
5172 Az += MP1;
5173
5174 /* store T0 for next subframe */
5175 st->old_T0 = T0;
5176 }
5177
5178 /*
5179 * Call the Source Characteristic Detector which updates
5180 * st->inBackgroundNoise and st->voicedHangover.
5181 */
5182 st->inBackgroundNoise = Bgn_scd(st->background_state, &(st->ltpGainHistory[
5183 0]), &(synth[0]), &(st->voicedHangover));
5184 dtx_dec_activity_update(st->dtxDecoderState, st->lsfState->past_lsf_q, synth
5185 );
5186
5187 /* store bfi for next subframe */
5188 st->prev_bf = bfi;
5189 st->prev_pdf = pdfi;
5190
5191 /*
5192 * Calculate the LSF averages on the eight
5193 * previous frames
5194 */
5195 lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q);
5196theEnd:
5197 st->dtxDecoderState->dtxGlobalState = newDTXState;
5198 return;
5199}
5200
5201
5202/*
5203 * Residu40
5204 *
5205 *
5206 * Parameters:
5207 * a I: prediction coefficients
5208 * x I: speech signal
5209 * y O: residual signal
5210 *
5211 * Function:
5212 * The LP residual is computed by filtering the input
5213 * speech through the LP inverse filter a(z)
5214 *
5215 * Returns:
5216 * void
5217 */
5218static void Residu40(Word32 a[], Word32 x[], Word32 y[])
5219{
5220 Word32 s, i, j;
5221
5222
5223 for (i = 0; i < 40; i++) {
5224 s = a[0] * x[i] + a[1] * x[i - 1] + a[2] * x[i - 2] + a[3] * x[i - 3];
5225 s += a[4] * x[i - 4] + a[5] * x[i - 5] + a[6] * x[i - 6] + a[7] * x[i - 7]
5226 ;
5227 s += a[8] * x[i - 8] + a[9] * x[i - 9] + a[10] * x[i - 10];
5228 y[i] = (s + 0x800) >> 12;
5229 if (abs(y[i]) > 32767) {
5230 /* go to safe mode */
5231 for (i = 0; i < 40; i++) {
5232 s = a[0] * x[i];
5233 for (j = 1; j <= 10; j++) {
5234 s += a[j] * x[i - j];
5235 if (s > 1073741823) {
5236 s = 1073741823;
5237 } else if (s < -1073741824) {
5238 s = -1073741824;
5239 }
5240 }
5241 y[i] = (s + 0x800) >> 12;
5242 if (abs(y[i]) > 32767) {
5243 y[i] = (y[i] & 0x80000000) ? -32768 : 32767;
5244 }
5245 }
5246 return;
5247 }
5248
5249 }
5250 return;
5251}
5252
5253
5254/*
5255 * agc
5256 *
5257 *
5258 * Parameters:
5259 * st->past_gain B: gain memory
5260 * sig_in I: Post_Filter input signal
5261 * sig_out B: Post_Filter output signal
5262 * agc_fac I: AGC factor
5263 *
5264 * Function:
5265 * Scales the Post_Filter output on a subframe basis
5266 *
5267 * Returns:
5268 * void
5269 */
5270static void agc(agcState *st, Word32 *sig_in, Word32 *sig_out, Word16 agc_fac)
5271{
5272 Word32 s, gain_in, gain_out, g0, gain;
5273 int exp, i;
5274
5275
5276 /* calculate gain_out with exponent */
5277 s = energy_new(sig_out);
5278
5279 if (s == 0) {
5280 st->past_gain = 0;
5281 return;
5282 }
5283 exp = 0;
5284 i = s;
5285 while (!(i & 0x40000000)) {
5286 exp++;
5287 i = i << 1;
5288 }
5289 exp -= 1;
5290 if (exp & 0x80000000) {
5291 s >>= 1;
5292 } else {
5293 s <<= exp;
5294 }
5295 gain_out = (s + 0x00008000L) >> 16;
5296
5297 /* calculate gain_in with exponent */
5298 s = energy_new(sig_in);
5299
5300 if (s == 0) {
5301 g0 = 0;
5302 } else {
5303 i = 0;
5304 while (!(s & 0x40000000)) {
5305 i++;
5306 s = s << 1;
5307 }
5308 s = s + 0x00008000L;
5309
5310 if (s >= 0) {
5311 gain_in = s >> 16;
5312 } else {
5313 gain_in = 32767;
5314 }
5315 exp = (exp - i);
5316
5317 /*
5318 * g0 = (1-agc_fac) * sqrt(gain_in/gain_out);
5319 */
5320 /* s = gain_out / gain_in */
5321 s = (gain_out << 15) / gain_in;
5322 exp = 7 - exp;
5323
5324 if (exp > 0) {
5325 if (exp > 31) {
5326 if (s) {
5327 s = 2147483647;
5328 }
5329 } else {
5330 s = s << exp ;
5331 }
5332 } else {
5333 s = (s >> (-exp));
5334 }
5335 if (s < 0) {
5336 s = 2147483647;
5337 }
5338 s = Inv_sqrt(s);
5339 i = ((s << 9) + 0x00008000L) >> 16;
5340 if (i & 0xFFFF8000) {
5341 i = 32767;
5342 }
5343
5344 /* g0 = i * (1-agc_fac) */
5345 g0 = (i * (32767 - agc_fac)) >> 15;
5346 }
5347
5348 /*
5349 * compute gain[n] = agc_fac * gain[n-1] + (1-agc_fac) * sqrt(gain_in/gain_out)
5350 * sig_out[n] = gain[n] * sig_out[n]
5351 */
5352 gain = st->past_gain;
5353
5354 for (i = 0; i < L_SUBFR; i++) {
5355 gain = (gain * agc_fac) >> 15;
5356 gain = gain + g0;
5357 sig_out[i] = (sig_out[i] * gain) >> 12;
5358 if (labs(sig_out[i]) > 32767) {
5359 sig_out[i] = (sig_out[i] & 0x8000000) ? -32768 : 32767;
5360 }
5361 }
5362 st->past_gain = gain;
5363 return;
5364}
5365
5366
5367/*
5368 * Post_Filter
5369 *
5370 *
5371 * Parameters:
5372 * st B: post filter states
5373 * mode I: AMR mode
5374 * syn B: synthesis speech
5375 * Az_4 I: interpolated LPC parameters in all subfr.
5376 *
5377 * Function:
5378 * Post_Filtering of synthesis speech.
5379 *
5380 * inverse filtering of syn[] through A(z/0.7) to get res2[]
5381 * tilt compensation filtering; 1 - MU*k*z^-1
5382 * synthesis filtering through 1/A(z/0.75)
5383 * adaptive gain control
5384 *
5385 * Returns:
5386 * void
5387 */
5388static void Post_Filter(Post_FilterState *st, enum Mode mode, Word32 *syn,
5389 Word32 *Az_4)
5390{
5391 Word32 h[22], Ap3[MP1], Ap4[MP1]; /* bandwidth expanded LP parameters */
5392 Word32 tmp, i_subfr, i, temp1, temp2, overflow = 0;
5393 Word32 *Az, *p1, *p2, *syn_work = &st->synth_buf[M];
5394 const Word32 *pgamma3 = &gamma3[0];
5395 const Word32 *pgamma4 = &gamma4_gamma3_MR122[0];
5396
5397
5398 /*
5399 * Post filtering
5400 */
5401 memcpy(syn_work, syn, L_FRAME << 2);
5402 Az = Az_4;
5403
5404 if ((mode == MR122) || (mode == MR102)) {
5405 pgamma3 = &gamma4_gamma3_MR122[0];
5406 pgamma4 = &gamma4_MR122[0];
5407 }
5408
5409 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) {
5410 /* Find weighted filter coefficients Ap3[] and Ap[4] */
5411 Ap3[0] = Az[0];
5412 Ap4[0] = Az[0];
5413
5414 for (i = 1; i <= 10; i++) {
5415 Ap3[i] = (Az[i] * pgamma3[i - 1] + 0x4000) >> 15;
5416 Ap4[i] = (Az[i] * pgamma4[i - 1] + 0x4000) >> 15;
5417 }
5418
5419 /* filtering of synthesis speech by A(z/0.7) to find res2[] */
5420 Residu40(Ap3, &syn_work[i_subfr], st->res2);
5421
5422 /* tilt compensation filter */
5423 /* impulse response of A(z/0.7)/A(z/0.75) */
5424 memcpy(h, Ap3, MP1 << 2);
5425 memset(&h[M + 1], 0, (22 - M - 1) << 2);
5426 Syn_filt(Ap4, h, h, 22, &h[M + 1], 0);
5427
5428 /* 1st correlation of h[] */
5429 tmp = 16777216 + h[1] * h[1];
5430
5431 for (i = 2; i < 22; i++) {
5432 tmp += h[i] * h[i];
5433 if (tmp > 0x3FFF8000) {
5434 break;
5435 }
5436 }
5437 temp1 = tmp >> 15;
5438 if (temp1 & 0xFFFF8000) {
5439 temp1 = 32767;
5440 }
5441
5442 tmp = h[0] * h[1];
5443
5444 for (i = 1; i < 21; i++) {
5445 tmp += h[i] * h[i + 1];
5446 if (abs(tmp) > 1073741823) {
5447 tmp = 1073741823;
5448 }
5449 }
5450 temp2 = tmp >> 15;
5451
5452 if (temp2 <= 0) {
5453 temp2 = 0;
5454 } else {
5455 tmp = temp2 * 26214;
5456 temp2 = (tmp & 0xffff8000) / temp1;
5457 }
5458
5459 /* preemphasis */
5460 p1 = st->res2 + 39;
5461 p2 = p1 - 1;
5462 tmp = *p1;
5463
5464 do {
5465 *p1 = *p1 - ((temp2 * *p2--) >> 15);
5466 if (abs(*p1) > 32767) {
5467 *p1 = (*p1 & 0x80000000) ? -32768 : 32767;
5468 }
5469 p1--;
5470 *p1 = *p1 - ((temp2 * *p2--) >> 15);
5471 if (abs(*p1) > 32767) {
5472 *p1 = (*p1 & 0x80000000) ? -32768 : 32767;
5473 }
5474 p1--;
5475 *p1 = *p1 - ((temp2 * *p2--) >> 15);
5476 if (abs(*p1) > 32767) {
5477 *p1 = (*p1 & 0x80000000) ? -32768 : 32767;
5478 }
5479 p1--;
5480 } while (p1 > st->res2);
5481 *p1 = *p1 - ((temp2 * st->preemph_state_mem_pre) >> 15);
5482 if (abs(*p1) > 32767) {
5483 *p1 = (*p1 & 0x80000000) ? -32768 : 32767;
5484 }
5485 st->preemph_state_mem_pre = tmp;
5486
5487 /* filtering through 1/A(z/0.75) */
5488 overflow = Syn_filt(Ap4, st->res2, &syn[i_subfr], L_SUBFR, st->mem_syn_pst, 0);
5489 if (overflow) {
5490 Syn_filt_overflow(Ap4, st->res2, &syn[i_subfr], L_SUBFR, st->mem_syn_pst, 1);
5491 overflow = 0;
5492 } else {
5493 memcpy(st->mem_syn_pst, &syn[i_subfr + 30], 40);
5494 }
5495
5496 /* scale output to input */
5497 agc(st->agc_state, &syn_work[i_subfr], &syn[i_subfr], AGC_FAC);
5498 Az += MP1;
5499 }
5500
5501 /* update syn_work[] buffer */
5502 memcpy(&syn_work[- M], &syn_work[L_FRAME - M], M << 2);
5503 return;
5504}
5505
5506
5507/*
5508 * Post_Process
5509 *
5510 *
5511 * Parameters:
5512 * st B: post filter states
5513 * signal B: signal
5514 *
5515 * Function:
5516 * Postprocessing of input speech.
5517 *
5518 * 2nd order high pass filtering with cut off frequency at 60 Hz.
5519 * Multiplication of output by two.
5520 *
5521 *
5522 * Returns:
5523 * void
5524 */
5525static void Post_Process(Post_ProcessState *st, Word32 signal[])
5526{
5527 Word32 x2, tmp, i = 0;
5528 Word32 mask = 0x40000000;
5529
5530 do {
5531 x2 = st->x1;
5532 st->x1 = st->x0;
5533 st->x0 = signal[i];
5534
5535 /*
5536 * y[i] = b[0]*x[i]*2 + b[1]*x[i-1]*2 + b140[2]*x[i-2]/2
5537 * + a[1]*y[i-1] + a[2] * y[i-2];
5538 */
5539 tmp = (st->y1_hi * 15836) + (((st->y1_lo * 15836) & (Word32)0xffff8000) >> 15);
5540 tmp += (st->y2_hi * -7667) + (((st->y2_lo * (-7667)) & (Word32)0xffff8000) >> 15);
5541 tmp += st->x0 * 7699;
5542 tmp += st->x1 * -15398;
5543 if (((tmp >> 1) ^ tmp) & mask) {
5544 tmp = (tmp & 0x80000000) ? -1073741824 : 1073741823;
5545 }
5546
5547 tmp += x2 * 7699;
5548 if (((tmp >> 1) ^ tmp) & mask) {
5549 tmp = (tmp & 0x80000000) ? -1073741824 : 1073741823;
5550 }
5551
5552 tmp = tmp << 1;
5553 if (((tmp >> 1) ^ tmp) & mask) {
5554 tmp = (tmp & 0x80000000) ? -1073741824 : 1073741823;
5555 }
5556
5557 tmp = tmp << 1;
5558 if (((tmp >> 1) ^ tmp) & mask) {
5559 tmp = (tmp & 0x80000000) ? -1073741824 : 1073741823;
5560 }
5561
5562 if (labs(tmp) < 536862720) {
5563 signal[i++] = (tmp + 0x00002000L) >> 14;
5564 } else if (tmp > 0) {
5565 signal[i++] = 32767;
5566 } else {
5567 signal[i++] = -32768;
5568 }
5569 st->y2_hi = st->y1_hi;
5570 st->y2_lo = st->y1_lo;
5571 st->y1_hi = tmp >> 15;
5572 st->y1_lo = ((tmp << 1) - (st->y1_hi << 16)) >> 1;
5573 } while (i < 160);
5574 return;
5575}
5576
5577
5578/*
5579 * Speech_Decode_Frame
5580 *
5581 *
5582 * Parameters:
5583 * st B: decoder memory
5584 * mode I: AMR mode
5585 * parm I: speech parameters
5586 * frame_type I: Frame type
5587 * synth O: synthesis speech
5588
5589 * Function:
5590 * Decode one frame
5591 *
5592 * Returns:
5593 * void
5594 */
5595void Speech_Decode_Frame(void *st, enum Mode mode, Word16 *parm, enum
5596 RXFrameType frame_type, Word16 *synth)
5597{
5598 Word32 Az_dec[AZ_SIZE]; /* Decoded Az for post-filter in 4 subframes*/
5599 Word32 synth_speech[L_FRAME];
5600 Word32 i;
5601
5602 /* Synthesis */
5603 Decoder_amr(((Speech_Decode_FrameState *) st)->decoder_amrState, mode,
5604 parm, frame_type, synth_speech, Az_dec);
5605 Post_Filter(((Speech_Decode_FrameState *) st)->post_state, mode,
5606 synth_speech, Az_dec);
5607
5608 /* post HP filter, and 15->16 bits */
5609 Post_Process(((Speech_Decode_FrameState *) st)->postHP_state,
5610 synth_speech);
5611
5612 for (i = 0; i < L_FRAME; i++) {
5613#ifndef NO13BIT
5614 /* Truncate to 13 bits */
5615 synth[i] = (Word16)(synth_speech[i] & 0xfff8);
5616#else
5617 synth[i] = (Word16)(synth_speech[i]);
5618#endif
5619 }
5620
5621
5622 return;
5623}
5624
5625
5626/*
5627 * Decoder_amr_exit
5628 *
5629 *
5630 * Parameters:
5631 * state I: state structure
5632 *
5633 * Function:
5634 * The memory used for state memory is freed
5635 *
5636 * Returns:
5637 * Void
5638 */
5639static void Decoder_amr_exit(Decoder_amrState **state)
5640{
5641 if (state == NULL || *state == NULL) {
5642 return;
5643 }
5644 /* tlsf_free( ( *state )->lsfState );
5645 tlsf_free( ( *state )->ec_gain_p_st );
5646 tlsf_free( ( *state )->ec_gain_c_st );
5647 tlsf_free( ( *state )->pred_state );
5648 tlsf_free( ( *state )->background_state );
5649 tlsf_free( ( *state )->ph_disp_st );
5650 tlsf_free( ( *state )->Cb_gain_averState );
5651 tlsf_free( ( *state )->lsp_avg_st );
5652 tlsf_free( ( *state )->dtxDecoderState );
5653 */
5654 /* deallocate memory
5655 tlsf_free( *state );*/
5656 *state = NULL;
5657 return;
5658}
5659
5660
5661/*
5662 * Post_Filter_exit
5663 *
5664 *
5665 * Parameters:
5666 * state I: state structure
5667 *
5668 * Function:
5669 * The memory used for state memory is freed
5670 *
5671 * Returns:
5672 * Void
5673 */
5674static void Post_Filter_exit(Post_FilterState **state)
5675{
5676 if (state == NULL || *state == NULL) {
5677 return;
5678 }
5679 //tlsf_free( ( *state )->agc_state );
5680
5681 /* deallocate memory */
5682 //tlsf_free( *state );
5683 *state = NULL;
5684 return;
5685}
5686
5687
5688/*
5689 * Post_Process_reset
5690 *
5691 *
5692 * Parameters:
5693 * state B: state structure
5694 *
5695 * Function:
5696 * Resets state memory
5697 *
5698 * Returns:
5699 * -1 failure
5700 */
5701static int Post_Process_reset(Post_ProcessState *state)
5702{
5703 if ((Post_ProcessState *)state == NULL) {
5704 //fprintf( stderr, "Post_Process_reset: invalid parameter\n" );
5705 return -1;
5706 }
5707 state->y2_hi = 0;
5708 state->y2_lo = 0;
5709 state->y1_hi = 0;
5710 state->y1_lo = 0;
5711 state->x0 = 0;
5712 state->x1 = 0;
5713 return 0;
5714}
5715
5716
5717/*
5718 * Post_Process_exit
5719 *
5720 *
5721 * Parameters:
5722 * state I: state structure
5723 *
5724 * Function:
5725 * The memory used for state memory is freed
5726 *
5727 * Returns:
5728 * Void
5729 */
5730static void Post_Process_exit(Post_ProcessState **state)
5731{
5732 if (state == NULL || *state == NULL) {
5733 return;
5734 }
5735
5736 /* deallocate memory */
5737 //tlsf_free( *state );
5738 *state = NULL;
5739 return;
5740}
5741
5742
5743/*
5744 * Decoder_amr_init
5745 *
5746 *
5747 * Parameters:
5748 * state O: state structure
5749 *
5750 * Function:
5751 * Allocates state memory and initializes state memory
5752 *
5753 * Returns:
5754 * success = 0
5755 */
5756static Decoder_amrState g_Decoder_amrState;
5757static D_plsfState g_D_plsfState;
5758static ec_gain_pitchState g_ec_gain_pitchState;
5759static ec_gain_codeState g_ec_gain_codeState;
5760static dtx_decState g_dtx_decState;
5761static ph_dispState g_ph_dispState;
5762static Cb_gain_averageState g_Cb_gain_averageState;
5763static lsp_avgState g_lsp_avgState;
5764static Bgn_scdState g_Bgn_scdState;
5765static gc_predState g_gc_predState;
5766static int Decoder_amr_init(Decoder_amrState **state)
5767{
5768 Decoder_amrState * s;
5769
5770 if ((Decoder_amrState *)state == NULL) {
5771 //fprintf( stderr, "Decoder_amr_init: invalid parameter\n" );
5772 return -1;
5773 }
5774 *state = NULL;
5775
5776 /* allocate memory
5777 if ( ( s = ( Decoder_amrState * ) tlsf_malloc( sizeof( Decoder_amrState ) ) ) ==
5778 NULL ) {
5779 fprintf( stderr, "Decoder_amr_init: can not malloc state structure\n" );
5780 return-1;
5781 }*/
5782 s = &g_Decoder_amrState;
5783
5784 /* DPlsf_init */
5785 /* allocate memory
5786 if ( ( s->lsfState = ( D_plsfState * ) tlsf_malloc( sizeof( D_plsfState ) ) ) ==
5787 NULL ) {
5788 fprintf( stderr, "DPlsf_init: can not malloc state structure\n" );
5789 return-1;
5790 }*/
5791 s->lsfState = &g_D_plsfState;
5792
5793 /* ecGainPitchInit */
5794 /* allocate memory
5795 if ( ( s->ec_gain_p_st = ( ec_gain_pitchState * ) tlsf_malloc( sizeof(
5796 ec_gain_pitchState ) ) ) == NULL ) {
5797 fprintf( stderr, "ecGainPitchInit: can not malloc state structure\n" );
5798 return-1;
5799 }*/
5800 s->ec_gain_p_st = &g_ec_gain_pitchState;
5801
5802 /* ecGainCodeInit */
5803 /* allocate memory
5804 if ( ( s->ec_gain_c_st = ( ec_gain_codeState * ) tlsf_malloc( sizeof(
5805 ec_gain_codeState ) ) ) == NULL ) {
5806 fprintf( stderr, "ecGainCodeInit: can not malloc state structure\n" );
5807 return-1;
5808 }*/
5809 s->ec_gain_c_st = &g_ec_gain_codeState;
5810 /* gcPredInit */
5811 /* allocate memory
5812 if ( ( s->pred_state = ( gc_predState * ) tlsf_malloc( sizeof( gc_predState ) ) )
5813 == NULL ) {
5814 fprintf( stderr, "gcPredInit: can not malloc state structure\n" );
5815 return-1;
5816 }*/
5817 s->pred_state = &g_gc_predState;
5818 /* Cb_gain_averageInit */
5819 /* allocate memory
5820 if ( ( s->Cb_gain_averState = ( Cb_gain_averageState * ) tlsf_malloc( sizeof(
5821 Cb_gain_averageState ) ) ) == NULL ) {
5822 fprintf( stderr, "Cb_gain_averageInit: can not malloc state structure\n" )
5823 ;
5824 return-1;
5825 }*/
5826 s->Cb_gain_averState = &g_Cb_gain_averageState;
5827 memset(s->Cb_gain_averState->cbGainHistory, 0, L_CBGAINHIST << 2);
5828
5829 /* Initialize hangover handling */
5830 s->Cb_gain_averState->hangVar = 0;
5831 s->Cb_gain_averState->hangCount = 0;
5832
5833 /* lsp_avgInit */
5834 /* allocate memory
5835 if ( ( s->lsp_avg_st = ( lsp_avgState * ) tlsf_malloc( sizeof( lsp_avgState ) ) )
5836 == NULL ) {
5837 fprintf( stderr, "lsp_avgInit: can not malloc state structure\n" );
5838 return-1;
5839 }*/
5840 s->lsp_avg_st = &g_lsp_avgState;
5841 /* Bgn_scdInit */
5842 /* allocate memory
5843 if ( ( s->background_state = ( Bgn_scdState * ) tlsf_malloc( sizeof( Bgn_scdState
5844 ) ) ) == NULL ) {
5845 fprintf( stderr, "Bgn_scdInit: can not malloc state structure\n" );
5846 return-1;
5847 }*/
5848 s->background_state = &g_Bgn_scdState;
5849 /* phDispInit */
5850 /* allocate memory
5851 if ( ( s->ph_disp_st = ( ph_dispState * ) tlsf_malloc( sizeof( ph_dispState ) ) )
5852 == NULL ) {
5853 fprintf( stderr, "phDispInit: can not malloc state structure\n" );
5854 return-1;
5855 }*/
5856 s->ph_disp_st = &g_ph_dispState;
5857 /* dtxDecInit */
5858 /* allocate memory
5859 if ( ( s->dtxDecoderState = ( dtx_decState * ) tlsf_malloc( sizeof( dtx_decState )
5860 ) ) == NULL ) {
5861 fprintf( stderr, "dtxDecInit: can not malloc state structure\n" );
5862 return-1;
5863 }*/
5864 s->dtxDecoderState = &g_dtx_decState;
5865 Decoder_amr_reset(s, 0);
5866 *state = s;
5867 return 0;
5868}
5869
5870
5871/*
5872 * Post_Filter_reset
5873 *
5874 *
5875 * Parameters:
5876 * state B: state structure
5877 *
5878 * Function:
5879 * Resets state memory
5880 *
5881 * Returns:
5882 * -1 failure
5883 */
5884static int Post_Filter_reset(Post_FilterState *state)
5885{
5886 if ((Post_FilterState *)state == NULL) {
5887 //fprintf( stderr, "Post_Filter_reset: invalid parameter\n" );
5888 return -1;
5889 }
5890 state->preemph_state_mem_pre = 0;
5891 state->agc_state->past_gain = 4096;
5892 memset(state->mem_syn_pst, 0, M << 2);
5893 memset(state->res2, 0, L_SUBFR << 2);
5894 memset(state->synth_buf, 0, (L_FRAME + M) << 2);
5895 return 0;
5896}
5897
5898
5899/*
5900 * Post_Filter_init
5901 *
5902 *
5903 * Parameters:
5904 * state O: state structure
5905 *
5906 * Function:
5907 * Allocates state memory and initializes state memory
5908 *
5909 * Returns:
5910 * success = 0
5911 */
5912static Post_FilterState g_Post_FilterState;
5913static agcState g_agcState;
5914static int Post_Filter_init(Post_FilterState **state)
5915{
5916 Post_FilterState * s;
5917
5918 if ((Post_FilterState *)state == NULL) {
5919 //fprintf( stderr, "F057:invalid parameter\n" );
5920 return -1;
5921 }
5922 *state = NULL;
5923
5924 /* allocate memory
5925 if ( ( s = ( Post_FilterState * ) tlsf_malloc( sizeof( Post_FilterState ) ) ) ==
5926 NULL ) {
5927 fprintf( stderr, "F057:can not malloc filter structure\n" );
5928 return-1;
5929 }*/
5930 s = &g_Post_FilterState;
5931 s->agc_state = NULL;
5932
5933 /* allocate memory
5934 if ( ( s->agc_state = ( agcState * ) tlsf_malloc( sizeof( agcState ) ) ) == NULL )
5935 {
5936 fprintf( stderr, "agcInit: can not malloc state structure\n" );
5937 return-1;
5938 }*/
5939 s->agc_state = &g_agcState;
5940 Post_Filter_reset(s);
5941 *state = s;
5942 return 0;
5943}
5944
5945
5946/*
5947 * Post_Process_init
5948 *
5949 *
5950 * Parameters:
5951 * state O: state structure
5952 *
5953 * Function:
5954 * Allocates state memory and initializes state memory
5955 *
5956 * Returns:
5957 * success = 0
5958 */
5959static Post_ProcessState g_Post_ProcessState;
5960static int Post_Process_init(Post_ProcessState **state)
5961{
5962 Post_ProcessState * s;
5963
5964 if ((Post_ProcessState *)state == NULL) {
5965 //fprintf( stderr, "Post_Process_init: invalid parameter\n" );
5966 return -1;
5967 }
5968 *state = NULL;
5969
5970 /* allocate memory
5971 if ( ( s = ( Post_ProcessState * ) tlsf_malloc( sizeof( Post_ProcessState ) ) ) ==
5972 NULL ) {
5973 fprintf( stderr, "Post_Process_init: can not malloc state structure\n" );
5974 return-1;
5975 }*/
5976 s = &g_Post_ProcessState;
5977 Post_Process_reset(s);
5978 *state = s;
5979 return 0;
5980}
5981
5982
5983/*
5984 * Speech_Decode_Frame_exit
5985 *
5986 *
5987 * Parameters:
5988 * state I: state structure
5989 *
5990 * Function:
5991 * The memory used for state memory is freed
5992 *
5993 * Returns:
5994 * Void
5995 */
5996void Speech_Decode_Frame_exit(void **st)
5997{
5998 if (((Speech_Decode_FrameState *)(st)) == NULL) {
5999 return;
6000 }
6001 Decoder_amr_exit(&(((Speech_Decode_FrameState *) st)->decoder_amrState
6002 ));
6003 Post_Filter_exit(&(((Speech_Decode_FrameState *) st)->post_state));
6004 Post_Process_exit(&(((Speech_Decode_FrameState *) st)->postHP_state))
6005 ;
6006
6007 /* deallocate memory */
6008 //tlsf_free( (( Speech_Decode_FrameState * )st) );
6009 return;
6010}
6011
6012
6013/*
6014 * Speech_Decode_Frame_reset
6015 *
6016 *
6017 * Parameters:
6018 * state B: state structure
6019 *
6020 * Function:
6021 * Resets state memory
6022 *
6023 * Returns:
6024 * -1 = failure
6025 */
6026int Speech_Decode_Frame_reset(void **st)
6027{
6028 Speech_Decode_FrameState * state;
6029
6030 if (st == NULL || *st == NULL) {
6031 return (-1);
6032 }
6033 state = (Speech_Decode_FrameState *)st;
6034 Decoder_amr_reset(state->decoder_amrState, (enum Mode) 0);
6035 Post_Filter_reset(state->post_state);
6036 Post_Process_reset(state->postHP_state);
6037 return 0;
6038}
6039
6040
6041/*
6042 * Speech_Decode_Frame_init
6043 *
6044 *
6045 * Parameters:
6046 * state O: state structure
6047 *
6048 * Function:
6049 * Allocates state memory and initializes state memory
6050 *
6051 * Returns:
6052 * success = 0
6053 */
6054static Speech_Decode_FrameState g_Speech_Decode_FrameState;
6055
6056void * Speech_Decode_Frame_init()
6057{
6058 Speech_Decode_FrameState * s;
6059
6060 /* allocate memory
6061 if ( ( s = ( Speech_Decode_FrameState * ) tlsf_malloc( sizeof(
6062 Speech_Decode_FrameState ) ) ) == NULL ) {
6063 fprintf( stderr, "Speech_Decode_Frame_init: can not malloc state "
6064 "structure\n" );
6065 return NULL;
6066 }*/
6067 s = &g_Speech_Decode_FrameState;
6068 s->decoder_amrState = NULL;
6069 s->post_state = NULL;
6070 s->postHP_state = NULL;
6071
6072 if (Decoder_amr_init(&s->decoder_amrState) || Post_Filter_init(&s->
6073 post_state) || Post_Process_init(&s->postHP_state)) {
6074 Speech_Decode_Frame_exit((void **)(&s));
6075 return NULL;
6076 }
6077 return s;
6078}
6079