comparison libtwamr/enc_lag3.c @ 363:0349de7c45b7

libtwamr: integrate enc_lag[36].c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 02:46:19 +0000
parents
children
comparison
equal deleted inserted replaced
362:9cbd1b5d061f 363:0349de7c45b7
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 : enc_lag3.c
11 * Purpose : Encoding of fractional pitch lag with 1/3 resolution.
12 *
13 ********************************************************************************
14 */
15 /*
16 ********************************************************************************
17 * MODULE INCLUDE FILE AND VERSION ID
18 ********************************************************************************
19 */
20 #include "namespace.h"
21 #include "enc_lag3.h"
22
23 /*
24 ********************************************************************************
25 * INCLUDE FILES
26 ********************************************************************************
27 */
28 #include "typedef.h"
29 #include "basic_op.h"
30 #include "no_count.h"
31 #include "cnst.h"
32
33 /*
34 ********************************************************************************
35 * PUBLIC PROGRAM CODE
36 ********************************************************************************
37 */
38 /*************************************************************************
39 *
40 * FUNCTION: Enc_lag3
41 *
42 * PURPOSE: Encoding of fractional pitch lag with 1/3 resolution.
43 *
44 * DESCRIPTION:
45 * First and third subframes:
46 * --------------------------
47 * The pitch range is divided as follows:
48 * 19 1/3 to 84 2/3 resolution 1/3
49 * 85 to 143 resolution 1
50 *
51 * The period is encoded with 8 bits.
52 * For the range with fractions:
53 * index = (T-19)*3 + frac - 1;
54 * where T=[19..85] and frac=[-1,0,1]
55 * and for the integer only range
56 * index = (T - 85) + 197; where T=[86..143]
57 *
58 * Second and fourth subframes:
59 * ----------------------------
60 * For the 2nd and 4th subframes a resolution of 1/3 is always used,
61 * and the search range is relative to the lag in previous subframe.
62 * If t0 is the lag in the previous subframe then
63 * t_min=t0-5 and t_max=t0+4 and the range is given by
64 * t_min - 2/3 to t_max + 2/3
65 *
66 * The period in the 2nd (and 4th) subframe is encoded with 5 bits:
67 * index = (T-(t_min-1))*3 + frac - 1;
68 * where T=[t_min-1..t_max+1]
69 *
70 *************************************************************************/
71 Word16
72 Enc_lag3( /* o : Return index of encoding */
73 Word16 T0, /* i : Pitch delay */
74 Word16 T0_frac, /* i : Fractional pitch delay */
75 Word16 T0_prev, /* i : Integer pitch delay of last subframe */
76 Word16 T0_min, /* i : minimum of search range */
77 Word16 T0_max, /* i : maximum of search range */
78 Word16 delta_flag, /* i : Flag for 1st (or 3rd) subframe */
79 Word16 flag4 /* i : Flag for encoding with 4 bits */
80 )
81 {
82 Word16 index, i, tmp_ind, uplag;
83 Word16 tmp_lag;
84
85 test ();
86 if (delta_flag == 0)
87 { /* if 1st or 3rd subframe */
88
89 /* encode pitch delay (with fraction) */
90
91 test ();
92 if (sub (T0, 85) <= 0)
93 {
94 /* index = T0*3 - 58 + T0_frac */
95 i = add (add (T0, T0), T0);
96 index = add (sub (i, 58), T0_frac);
97 }
98 else
99 {
100 index = add (T0, 112);
101 }
102 }
103 else
104 { /* if second or fourth subframe */
105 test ();
106 if (flag4 == 0) {
107
108 /* 'normal' encoding: either with 5 or 6 bit resolution */
109
110 /* index = 3*(T0 - T0_min) + 2 + T0_frac */
111 i = sub (T0, T0_min);
112 i = add (add (i, i), i);
113 index = add (add (i, 2), T0_frac);
114 }
115 else {
116
117 /* encoding with 4 bit resolution */
118
119 tmp_lag = T0_prev; move16 ();
120
121 test ();
122 if ( sub( sub(tmp_lag, T0_min), 5) > 0)
123 tmp_lag = add (T0_min, 5);
124 test ();
125 if ( sub( sub(T0_max, tmp_lag), 4) > 0)
126 tmp_lag = sub (T0_max, 4);
127
128 uplag = add (add (add (T0, T0), T0), T0_frac);
129
130 i = sub (tmp_lag, 2);
131 tmp_ind = add (add (i, i), i);
132
133 test ();
134 if (sub (tmp_ind, uplag) >= 0) {
135 index = add (sub (T0, tmp_lag), 5);
136 }
137 else
138 {
139 i = add (tmp_lag, 1);
140 i = add (add (i, i), i);
141
142 test ();
143 if (sub (i, uplag) > 0) {
144 index = add ( sub (uplag, tmp_ind), 3);
145 }
146 else {
147 index = add (sub (T0, tmp_lag), 11);
148 }
149 }
150
151 } /* end if (encoding with 4 bit resolution) */
152 } /* end if (second of fourth subframe) */
153
154 return index;
155 }