annotate libtwamr/ec_gains.c @ 483:4f13db3a7086

doc/Utils-overview: document new utilities
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 20 May 2024 01:26:12 +0000
parents 9cbd1b5d061f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
362
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 ********************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * R99 Version 3.3.0
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * REL-4 Version 4.1.0
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 ********************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * File : ec_gains.c
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * Purpose: : Error concealment for pitch and codebook gains
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 ********************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 ********************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 * MODULE INCLUDE FILE AND VERSION ID
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 ********************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 #include "namespace.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 #include "ec_gains.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 ********************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 * INCLUDE FILES
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 ********************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 #include "typedef.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 #include "basic_op.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 #include "oper_32b.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 #include "no_count.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 #include "cnst.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 #include "gmed_n.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 #include "gc_pred.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 #include "gains_tab.h"
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 ********************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 * PUBLIC PROGRAM CODE
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 ********************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 * Function : ec_gain_code_reset
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 * Purpose : Resets state memory
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 void ec_gain_code_reset (ec_gain_codeState *state)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 Word16 i;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 for ( i = 0; i < 5; i++)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 state->gbuf[i] = 1;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 state->past_gain_code = 0;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 state->prev_gc = 1;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 * Function : ec_gain_code
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 * Purpose : conceal the codebook gain
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 * Call this function only in BFI (instead of normal gain
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 * decoding function)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 void ec_gain_code (
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 ec_gain_codeState *st, /* i/o : State struct */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 gc_predState *pred_state, /* i/o : MA predictor state */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 Word16 state, /* i : state of the state machine */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 Word16 *gain_code /* o : decoded innovation gain */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 )
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 static const Word16 cdown[7] =
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 32767, 32112, 32112, 32112,
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 32112, 32112, 22937
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 };
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 Word16 tmp;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 Word16 qua_ener_MR122;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 Word16 qua_ener;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 /* calculate median of last five gain values */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 tmp = gmed_n (st->gbuf,5); move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 /* new gain = minimum(median, past_gain) * cdown[state] */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 test ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 if (sub (tmp, st->past_gain_code) > 0)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 tmp = st->past_gain_code; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 tmp = mult (tmp, cdown[state]);
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 *gain_code = tmp; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 /* update table of past quantized energies with average of
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 * current values
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 gc_pred_average_limited(pred_state, &qua_ener_MR122, &qua_ener);
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 gc_pred_update(pred_state, qua_ener_MR122, qua_ener);
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 * Function : ec_gain_code_update
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 * Purpose : update the codebook gain concealment state;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 * limit gain_code if the previous frame was bad
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 * Call this function always after decoding (or concealing)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 * the gain
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 void ec_gain_code_update (
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 ec_gain_codeState *st, /* i/o : State struct */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 Word16 bfi, /* i : flag: frame is bad */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 Word16 prev_bf, /* i : flag: previous frame was bad */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 Word16 *gain_code /* i/o : decoded innovation gain */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 )
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 Word16 i;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 /* limit gain_code by previous good gain if previous frame was bad */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 test ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 if (bfi == 0)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 test ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 if (prev_bf != 0)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 test ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 if (sub (*gain_code, st->prev_gc) > 0)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 *gain_code = st->prev_gc; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 st->prev_gc = *gain_code; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 /* update EC states: previous gain, gain buffer */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 st->past_gain_code = *gain_code; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 for (i = 1; i < 5; i++)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 st->gbuf[i - 1] = st->gbuf[i]; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 st->gbuf[4] = *gain_code; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 return;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 * Function: ec_gain_pitch_reset
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 * Purpose: Resets state memory
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 void ec_gain_pitch_reset (ec_gain_pitchState *state)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 Word16 i;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 for(i = 0; i < 5; i++)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 state->pbuf[i] = 1640;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 state->past_gain_pit = 0;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 state->prev_gp = 16384;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 * Function : ec_gain_pitch
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 * Purpose : conceal the pitch gain
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 * Call this function only in BFI (instead of normal gain
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 * decoding function)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 void ec_gain_pitch (
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 ec_gain_pitchState *st, /* i/o : state variables */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 Word16 state, /* i : state of the state machine */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 Word16 *gain_pitch /* o : pitch gain (Q14) */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 )
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 static const Word16 pdown[7] =
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 32767, 32112, 32112, 26214,
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 9830, 6553, 6553
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 };
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 Word16 tmp;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 /* calculate median of last five gains */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 tmp = gmed_n (st->pbuf, 5); move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 /* new gain = minimum(median, past_gain) * pdown[state] */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 test ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 if (sub (tmp, st->past_gain_pit) > 0)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 tmp = st->past_gain_pit; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 *gain_pitch = mult (tmp, pdown[state]);
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 /*
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 * Function : ec_gain_pitch_update
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 * Purpose : update the pitch gain concealment state;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 * limit gain_pitch if the previous frame was bad
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 * Call this function always after decoding (or concealing)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 * the gain
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 *
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 **************************************************************************
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 void ec_gain_pitch_update (
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 ec_gain_pitchState *st, /* i/o : state variables */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 Word16 bfi, /* i : flag: frame is bad */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 Word16 prev_bf, /* i : flag: previous frame was bad */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 Word16 *gain_pitch /* i/o : pitch gain */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 )
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 Word16 i;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 test ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 if (bfi == 0)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233 test ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 if (prev_bf != 0)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236 test ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 if (sub (*gain_pitch, st->prev_gp) > 0)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 *gain_pitch = st->prev_gp;
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 st->prev_gp = *gain_pitch; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 st->past_gain_pit = *gain_pitch; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247 test ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
248 if (sub (st->past_gain_pit, 16384) > 0) /* if (st->past_gain_pit > 1.0) */
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
249 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
250 st->past_gain_pit = 16384; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
251 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
252 for (i = 1; i < 5; i++)
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
253 {
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
254 st->pbuf[i - 1] = st->pbuf[i]; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
255 }
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
256 st->pbuf[4] = st->past_gain_pit; move16 ();
9cbd1b5d061f libtwamr: integrate ec_gains.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
257 }