comparison libtwamr/q_gain_p.c @ 369:a01de4e40540

libtwamr: integrate q_gain_p.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 03:22:07 +0000
parents
children
comparison
equal deleted inserted replaced
368:3a25bdfad0d8 369:a01de4e40540
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_p.c
11 * Purpose : Scalar quantization of the pitch gain
12 *
13 ********************************************************************************
14 */
15 /*
16 ********************************************************************************
17 * MODULE INCLUDE FILE AND VERSION ID
18 ********************************************************************************
19 */
20 #include "namespace.h"
21 #include "q_gain_p.h"
22
23 /*
24 ********************************************************************************
25 * INCLUDE FILES
26 ********************************************************************************
27 */
28 #include "tw_amr.h"
29 #include "typedef.h"
30 #include "basic_op.h"
31 #include "oper_32b.h"
32 #include "no_count.h"
33 #include "cnst.h"
34 #include "gains_tab.h"
35
36 /*
37 ********************************************************************************
38 * PUBLIC PROGRAM CODE
39 ********************************************************************************
40 */
41 Word16 q_gain_pitch ( /* Return index of quantization */
42 enum Mode mode, /* i : AMR mode */
43 Word16 gp_limit, /* i : pitch gain limit */
44 Word16 *gain, /* i/o: Pitch gain (unquant/quant), Q14 */
45 Word16 gain_cand[], /* o : pitch gain candidates (3), MR795 only, Q14 */
46 Word16 gain_cind[] /* o : pitch gain cand. indices (3),MR795 only, Q0 */
47 )
48 {
49 Word16 i, index, err, err_min;
50
51 err_min = abs_s (sub (*gain, qua_gain_pitch[0]));
52 index = 0; move16 ();
53
54 for (i = 1; i < NB_QUA_PITCH; i++)
55 {
56 test ();
57 if (sub (qua_gain_pitch[i], gp_limit) <= 0)
58 {
59 err = abs_s (sub (*gain, qua_gain_pitch[i]));
60
61 test ();
62 if (sub (err, err_min) < 0)
63 {
64 err_min = err; move16 ();
65 index = i; move16 ();
66 }
67 }
68 }
69
70 test ();
71 if (sub (mode, MR795) == 0)
72 {
73 /* in MR795 mode, compute three gain_pit candidates around the index
74 * found in the quantization loop: the index found and the two direct
75 * neighbours, except for the extreme cases (i=0 or i=NB_QUA_PITCH-1),
76 * where the direct neighbour and the neighbour to that is used.
77 */
78 Word16 ii;
79
80 test ();
81 if (index == 0)
82 {
83 ii = index; move16 ();
84 }
85 else
86 {
87 test (); test ();
88 if ( sub (index, NB_QUA_PITCH-1) == 0
89 || sub (qua_gain_pitch[index+1], gp_limit) > 0)
90 {
91 ii = sub (index, 2);
92 }
93 else
94 {
95 ii = sub (index, 1);
96 }
97 }
98
99 /* store candidate indices and values */
100 for (i = 0; i < 3; i++)
101 {
102 gain_cind[i] = ii; move16 ();
103 gain_cand[i] = qua_gain_pitch[ii]; move16 ();
104 ii = add (ii, 1);
105 }
106
107 *gain = qua_gain_pitch[index]; move16 ();
108 }
109 else
110 {
111 /* in MR122 mode, just return the index and gain pitch found.
112 * If bitexactness is required, mask away the two LSBs (because
113 * in the original EFR, gain_pit was scaled Q12)
114 */
115 test ();
116 if (sub(mode, MR122) == 0)
117 {
118 /* clear 2 LSBits */
119 *gain = qua_gain_pitch[index] & 0xFFFC; logic16 (); move16 ();
120 }
121 else
122 {
123 *gain = qua_gain_pitch[index]; move16 ();
124 }
125 }
126 return index;
127 }