FreeCalypso > hg > gsm-codec-lib
comparison libtwamr/q_gain_c.c @ 368:3a25bdfad0d8
libtwamr: integrate q_gain_c.c
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 06 May 2024 03:18:47 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
367:bd4f660eb75a | 368:3a25bdfad0d8 |
---|---|
1 /* | |
2 ******************************************************************************** | |
3 * | |
4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001 | |
5 * R99 Version 3.3.0 | |
6 * REL-4 Version 4.1.0 | |
7 * | |
8 ******************************************************************************** | |
9 * | |
10 * File : q_gain_c.c | |
11 * Purpose : Scalar quantization of the innovative | |
12 * : codebook gain. | |
13 * | |
14 ******************************************************************************** | |
15 */ | |
16 | |
17 /* | |
18 ******************************************************************************** | |
19 * MODULE INCLUDE FILE AND VERSION ID | |
20 ******************************************************************************** | |
21 */ | |
22 #include "namespace.h" | |
23 #include "q_gain_c.h" | |
24 | |
25 /* | |
26 ******************************************************************************** | |
27 * INCLUDE FILES | |
28 ******************************************************************************** | |
29 */ | |
30 #include "tw_amr.h" | |
31 #include "typedef.h" | |
32 #include "basic_op.h" | |
33 #include "oper_32b.h" | |
34 #include "no_count.h" | |
35 #include "log2.h" | |
36 #include "pow2.h" | |
37 #include "gains_tab.h" | |
38 | |
39 /* | |
40 ******************************************************************************** | |
41 * PUBLIC PROGRAM CODE | |
42 ******************************************************************************** | |
43 */ | |
44 | |
45 /*--------------------------------------------------------------------------* | |
46 * Function q_gain_code() * | |
47 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * | |
48 * Scalar quantization of the innovative codebook gain. * | |
49 * * | |
50 *--------------------------------------------------------------------------*/ | |
51 Word16 q_gain_code ( /* o : quantization index, Q0 */ | |
52 enum Mode mode, /* i : AMR mode */ | |
53 Word16 exp_gcode0, /* i : predicted CB gain (exponent), Q0 */ | |
54 Word16 frac_gcode0, /* i : predicted CB gain (fraction), Q15 */ | |
55 Word16 *gain, /* i/o: quantized fixed codebook gain, Q1 */ | |
56 Word16 *qua_ener_MR122, /* o : quantized energy error, Q10 */ | |
57 /* (for MR122 MA predictor update) */ | |
58 Word16 *qua_ener /* o : quantized energy error, Q10 */ | |
59 /* (for other MA predictor update) */ | |
60 ) | |
61 { | |
62 const Word16 *p; | |
63 Word16 i, index; | |
64 Word16 gcode0, err, err_min; | |
65 Word16 g_q0; | |
66 | |
67 test (); | |
68 g_q0 = 0; move16 (); | |
69 test (); | |
70 if (sub(mode, MR122) == 0) | |
71 { | |
72 g_q0 = shr (*gain, 1); /* Q1 -> Q0 */ | |
73 } | |
74 | |
75 /*-------------------------------------------------------------------* | |
76 * predicted codebook gain * | |
77 * ~~~~~~~~~~~~~~~~~~~~~~~ * | |
78 * gc0 = Pow2(int(d)+frac(d)) * | |
79 * = 2^exp + 2^frac * | |
80 * * | |
81 *-------------------------------------------------------------------*/ | |
82 | |
83 gcode0 = extract_l (Pow2 (exp_gcode0, frac_gcode0)); /* predicted gain */ | |
84 | |
85 test (); | |
86 if (sub(mode, MR122) == 0) | |
87 { | |
88 gcode0 = shl (gcode0, 4); | |
89 } | |
90 else | |
91 { | |
92 gcode0 = shl (gcode0, 5); | |
93 } | |
94 | |
95 /*-------------------------------------------------------------------* | |
96 * Search for best quantizer * | |
97 *-------------------------------------------------------------------*/ | |
98 | |
99 p = &qua_gain_code[0]; move16 (); | |
100 test (); | |
101 if (sub(mode, MR122) == 0) | |
102 { | |
103 err_min = abs_s (sub (g_q0, mult (gcode0, *p++))); | |
104 } | |
105 else | |
106 { | |
107 err_min = abs_s (sub (*gain, mult (gcode0, *p++))); | |
108 } | |
109 p += 2; /* skip quantized energy errors */ | |
110 index = 0; move16 (); | |
111 | |
112 for (i = 1; i < NB_QUA_CODE; i++) | |
113 { | |
114 test (); | |
115 if (sub(mode, MR122) == 0) | |
116 { | |
117 err = abs_s (sub (g_q0, mult (gcode0, *p++))); | |
118 } | |
119 else | |
120 { | |
121 err = abs_s (sub (*gain, mult (gcode0, *p++))); | |
122 } | |
123 | |
124 p += 2; /* skip quantized energy error */ | |
125 | |
126 test (); | |
127 if (sub (err, err_min) < 0) | |
128 { | |
129 err_min = err; move16 (); | |
130 index = i; move16 (); | |
131 } | |
132 } | |
133 | |
134 p = &qua_gain_code[add (add (index,index), index)]; move16 (); | |
135 test (); | |
136 if (sub(mode, MR122) == 0) | |
137 { | |
138 *gain = shl (mult (gcode0, *p++), 1); | |
139 } | |
140 else | |
141 { | |
142 *gain = mult (gcode0, *p++); | |
143 } | |
144 move16 (); | |
145 | |
146 /* quantized error energies (for MA predictor update) */ | |
147 *qua_ener_MR122 = *p++; move16 (); | |
148 *qua_ener = *p; move16 (); | |
149 | |
150 return index; | |
151 } |