2 * G.723.1 compatible decoder
3 * Copyright (c) 2006 Benjamin Larsson
4 * Copyright (c) 2010 Mohamed Naufal Basheer
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/common.h"
27 #include "acelp_vectors.h"
29 #include "celp_math.h"
32 int ff_g723_1_scale_vector(int16_t *dst
, const int16_t *vector
, int length
)
37 for (i
= 0; i
< length
; i
++)
38 max
|= FFABS(vector
[i
]);
40 max
= FFMIN(max
, 0x7FFF);
41 bits
= ff_g723_1_normalize_bits(max
, 15);
43 for (i
= 0; i
< length
; i
++)
44 dst
[i
] = vector
[i
] << bits
>> 3;
49 int ff_g723_1_normalize_bits(int num
, int width
)
51 return width
- av_log2(num
) - 1;
54 int ff_g723_1_dot_product(const int16_t *a
, const int16_t *b
, int length
)
58 for (i
= 0; i
< length
; i
++) {
59 int prod
= a
[i
] * b
[i
];
60 sum
= av_sat_dadd32(sum
, prod
);
65 void ff_g723_1_get_residual(int16_t *residual
, int16_t *prev_excitation
,
68 int offset
= PITCH_MAX
- PITCH_ORDER
/ 2 - lag
;
71 residual
[0] = prev_excitation
[offset
];
72 residual
[1] = prev_excitation
[offset
+ 1];
75 for (i
= 2; i
< SUBFRAME_LEN
+ PITCH_ORDER
- 1; i
++)
76 residual
[i
] = prev_excitation
[offset
+ (i
- 2) % lag
];
79 void ff_g723_1_gen_dirac_train(int16_t *buf
, int pitch_lag
)
81 int16_t vector
[SUBFRAME_LEN
];
84 memcpy(vector
, buf
, SUBFRAME_LEN
* sizeof(*vector
));
85 for (i
= pitch_lag
; i
< SUBFRAME_LEN
; i
+= pitch_lag
) {
86 for (j
= 0; j
< SUBFRAME_LEN
- i
; j
++)
87 buf
[i
+ j
] += vector
[j
];
91 void ff_g723_1_gen_acb_excitation(int16_t *vector
, int16_t *prev_excitation
,
92 int pitch_lag
, G723_1_Subframe
*subfrm
,
95 int16_t residual
[SUBFRAME_LEN
+ PITCH_ORDER
- 1];
96 const int16_t *cb_ptr
;
97 int lag
= pitch_lag
+ subfrm
->ad_cb_lag
- 1;
102 ff_g723_1_get_residual(residual
, prev_excitation
, lag
);
104 /* Select quantization table */
105 if (cur_rate
== RATE_6300
&& pitch_lag
< SUBFRAME_LEN
- 2)
106 cb_ptr
= adaptive_cb_gain85
;
108 cb_ptr
= adaptive_cb_gain170
;
110 /* Calculate adaptive vector */
111 cb_ptr
+= subfrm
->ad_cb_gain
* 20;
112 for (i
= 0; i
< SUBFRAME_LEN
; i
++) {
113 sum
= ff_g723_1_dot_product(residual
+ i
, cb_ptr
, PITCH_ORDER
);
114 vector
[i
] = av_sat_dadd32(1 << 15, sum
) >> 16;
119 * Convert LSP frequencies to LPC coefficients.
121 * @param lpc buffer for LPC coefficients
123 static void lsp2lpc(int16_t *lpc
)
125 int f1
[LPC_ORDER
/ 2 + 1];
126 int f2
[LPC_ORDER
/ 2 + 1];
129 /* Calculate negative cosine */
130 for (j
= 0; j
< LPC_ORDER
; j
++) {
131 int index
= (lpc
[j
] >> 7) & 0x1FF;
132 int offset
= lpc
[j
] & 0x7f;
133 int temp1
= cos_tab
[index
] << 16;
134 int temp2
= (cos_tab
[index
+ 1] - cos_tab
[index
]) *
135 ((offset
<< 8) + 0x80) << 1;
137 lpc
[j
] = -(av_sat_dadd32(1 << 15, temp1
+ temp2
) >> 16);
141 * Compute sum and difference polynomial coefficients
142 * (bitexact alternative to lsp2poly() in lsp.c)
144 /* Initialize with values in Q28 */
146 f1
[1] = (lpc
[0] << 14) + (lpc
[2] << 14);
147 f1
[2] = lpc
[0] * lpc
[2] + (2 << 28);
150 f2
[1] = (lpc
[1] << 14) + (lpc
[3] << 14);
151 f2
[2] = lpc
[1] * lpc
[3] + (2 << 28);
154 * Calculate and scale the coefficients by 1/2 in
155 * each iteration for a final scaling factor of Q25
157 for (i
= 2; i
< LPC_ORDER
/ 2; i
++) {
158 f1
[i
+ 1] = f1
[i
- 1] + MULL2(f1
[i
], lpc
[2 * i
]);
159 f2
[i
+ 1] = f2
[i
- 1] + MULL2(f2
[i
], lpc
[2 * i
+ 1]);
161 for (j
= i
; j
>= 2; j
--) {
162 f1
[j
] = MULL2(f1
[j
- 1], lpc
[2 * i
]) +
163 (f1
[j
] >> 1) + (f1
[j
- 2] >> 1);
164 f2
[j
] = MULL2(f2
[j
- 1], lpc
[2 * i
+ 1]) +
165 (f2
[j
] >> 1) + (f2
[j
- 2] >> 1);
170 f1
[1] = ((lpc
[2 * i
] << 16 >> i
) + f1
[1]) >> 1;
171 f2
[1] = ((lpc
[2 * i
+ 1] << 16 >> i
) + f2
[1]) >> 1;
174 /* Convert polynomial coefficients to LPC coefficients */
175 for (i
= 0; i
< LPC_ORDER
/ 2; i
++) {
176 int64_t ff1
= f1
[i
+ 1] + f1
[i
];
177 int64_t ff2
= f2
[i
+ 1] - f2
[i
];
179 lpc
[i
] = av_clipl_int32(((ff1
+ ff2
) << 3) +
181 lpc
[LPC_ORDER
- i
- 1] = av_clipl_int32(((ff1
- ff2
) << 3) +
186 void ff_g723_1_lsp_interpolate(int16_t *lpc
, int16_t *cur_lsp
,
190 int16_t *lpc_ptr
= lpc
;
192 /* cur_lsp * 0.25 + prev_lsp * 0.75 */
193 ff_acelp_weighted_vector_sum(lpc
, cur_lsp
, prev_lsp
,
194 4096, 12288, 1 << 13, 14, LPC_ORDER
);
195 ff_acelp_weighted_vector_sum(lpc
+ LPC_ORDER
, cur_lsp
, prev_lsp
,
196 8192, 8192, 1 << 13, 14, LPC_ORDER
);
197 ff_acelp_weighted_vector_sum(lpc
+ 2 * LPC_ORDER
, cur_lsp
, prev_lsp
,
198 12288, 4096, 1 << 13, 14, LPC_ORDER
);
199 memcpy(lpc
+ 3 * LPC_ORDER
, cur_lsp
, LPC_ORDER
* sizeof(*lpc
));
201 for (i
= 0; i
< SUBFRAMES
; i
++) {
203 lpc_ptr
+= LPC_ORDER
;
207 void ff_g723_1_inverse_quant(int16_t *cur_lsp
, int16_t *prev_lsp
,
208 uint8_t *lsp_index
, int bad_frame
)
211 int i
, j
, temp
, stable
;
213 /* Check for frame erasure */
220 lsp_index
[0] = lsp_index
[1] = lsp_index
[2] = 0;
223 /* Get the VQ table entry corresponding to the transmitted index */
224 cur_lsp
[0] = lsp_band0
[lsp_index
[0]][0];
225 cur_lsp
[1] = lsp_band0
[lsp_index
[0]][1];
226 cur_lsp
[2] = lsp_band0
[lsp_index
[0]][2];
227 cur_lsp
[3] = lsp_band1
[lsp_index
[1]][0];
228 cur_lsp
[4] = lsp_band1
[lsp_index
[1]][1];
229 cur_lsp
[5] = lsp_band1
[lsp_index
[1]][2];
230 cur_lsp
[6] = lsp_band2
[lsp_index
[2]][0];
231 cur_lsp
[7] = lsp_band2
[lsp_index
[2]][1];
232 cur_lsp
[8] = lsp_band2
[lsp_index
[2]][2];
233 cur_lsp
[9] = lsp_band2
[lsp_index
[2]][3];
235 /* Add predicted vector & DC component to the previously quantized vector */
236 for (i
= 0; i
< LPC_ORDER
; i
++) {
237 temp
= ((prev_lsp
[i
] - dc_lsp
[i
]) * pred
+ (1 << 14)) >> 15;
238 cur_lsp
[i
] += dc_lsp
[i
] + temp
;
241 for (i
= 0; i
< LPC_ORDER
; i
++) {
242 cur_lsp
[0] = FFMAX(cur_lsp
[0], 0x180);
243 cur_lsp
[LPC_ORDER
- 1] = FFMIN(cur_lsp
[LPC_ORDER
- 1], 0x7e00);
245 /* Stability check */
246 for (j
= 1; j
< LPC_ORDER
; j
++) {
247 temp
= min_dist
+ cur_lsp
[j
- 1] - cur_lsp
[j
];
250 cur_lsp
[j
- 1] -= temp
;
255 for (j
= 1; j
< LPC_ORDER
; j
++) {
256 temp
= cur_lsp
[j
- 1] + min_dist
- cur_lsp
[j
] - 4;
266 memcpy(cur_lsp
, prev_lsp
, LPC_ORDER
* sizeof(*cur_lsp
));