1 |
|
|
/* |
2 |
|
|
* G.723.1 compatible decoder |
3 |
|
|
* Copyright (c) 2006 Benjamin Larsson |
4 |
|
|
* Copyright (c) 2010 Mohamed Naufal Basheer |
5 |
|
|
* |
6 |
|
|
* This file is part of FFmpeg. |
7 |
|
|
* |
8 |
|
|
* FFmpeg 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. |
12 |
|
|
* |
13 |
|
|
* FFmpeg 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. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU Lesser General Public |
19 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
20 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* @file |
25 |
|
|
* G.723.1 compatible decoder |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
#include "libavutil/channel_layout.h" |
29 |
|
|
#include "libavutil/mem.h" |
30 |
|
|
#include "libavutil/opt.h" |
31 |
|
|
|
32 |
|
|
#define BITSTREAM_READER_LE |
33 |
|
|
#include "acelp_vectors.h" |
34 |
|
|
#include "avcodec.h" |
35 |
|
|
#include "celp_filters.h" |
36 |
|
|
#include "celp_math.h" |
37 |
|
|
#include "get_bits.h" |
38 |
|
|
#include "internal.h" |
39 |
|
|
#include "g723_1.h" |
40 |
|
|
|
41 |
|
|
#define CNG_RANDOM_SEED 12345 |
42 |
|
|
|
43 |
|
|
/** |
44 |
|
|
* Postfilter gain weighting factors scaled by 2^15 |
45 |
|
|
*/ |
46 |
|
|
static const int16_t ppf_gain_weight[2] = {0x1800, 0x2000}; |
47 |
|
|
|
48 |
|
|
static const int16_t pitch_contrib[340] = { |
49 |
|
|
60, 0, 0, 2489, 60, 0, 0, 5217, |
50 |
|
|
1, 6171, 0, 3953, 0, 10364, 1, 9357, |
51 |
|
|
-1, 8843, 1, 9396, 0, 5794, -1, 10816, |
52 |
|
|
2, 11606, -2, 12072, 0, 8616, 1, 12170, |
53 |
|
|
0, 14440, 0, 7787, -1, 13721, 0, 18205, |
54 |
|
|
0, 14471, 0, 15807, 1, 15275, 0, 13480, |
55 |
|
|
-1, 18375, -1, 0, 1, 11194, -1, 13010, |
56 |
|
|
1, 18836, -2, 20354, 1, 16233, -1, 0, |
57 |
|
|
60, 0, 0, 12130, 0, 13385, 1, 17834, |
58 |
|
|
1, 20875, 0, 21996, 1, 0, 1, 18277, |
59 |
|
|
-1, 21321, 1, 13738, -1, 19094, -1, 20387, |
60 |
|
|
-1, 0, 0, 21008, 60, 0, -2, 22807, |
61 |
|
|
0, 15900, 1, 0, 0, 17989, -1, 22259, |
62 |
|
|
1, 24395, 1, 23138, 0, 23948, 1, 22997, |
63 |
|
|
2, 22604, -1, 25942, 0, 26246, 1, 25321, |
64 |
|
|
0, 26423, 0, 24061, 0, 27247, 60, 0, |
65 |
|
|
-1, 25572, 1, 23918, 1, 25930, 2, 26408, |
66 |
|
|
-1, 19049, 1, 27357, -1, 24538, 60, 0, |
67 |
|
|
-1, 25093, 0, 28549, 1, 0, 0, 22793, |
68 |
|
|
-1, 25659, 0, 29377, 0, 30276, 0, 26198, |
69 |
|
|
1, 22521, -1, 28919, 0, 27384, 1, 30162, |
70 |
|
|
-1, 0, 0, 24237, -1, 30062, 0, 21763, |
71 |
|
|
1, 30917, 60, 0, 0, 31284, 0, 29433, |
72 |
|
|
1, 26821, 1, 28655, 0, 31327, 2, 30799, |
73 |
|
|
1, 31389, 0, 32322, 1, 31760, -2, 31830, |
74 |
|
|
0, 26936, -1, 31180, 1, 30875, 0, 27873, |
75 |
|
|
-1, 30429, 1, 31050, 0, 0, 0, 31912, |
76 |
|
|
1, 31611, 0, 31565, 0, 25557, 0, 31357, |
77 |
|
|
60, 0, 1, 29536, 1, 28985, -1, 26984, |
78 |
|
|
-1, 31587, 2, 30836, -2, 31133, 0, 30243, |
79 |
|
|
-1, 30742, -1, 32090, 60, 0, 2, 30902, |
80 |
|
|
60, 0, 0, 30027, 0, 29042, 60, 0, |
81 |
|
|
0, 31756, 0, 24553, 0, 25636, -2, 30501, |
82 |
|
|
60, 0, -1, 29617, 0, 30649, 60, 0, |
83 |
|
|
0, 29274, 2, 30415, 0, 27480, 0, 31213, |
84 |
|
|
-1, 28147, 0, 30600, 1, 31652, 2, 29068, |
85 |
|
|
60, 0, 1, 28571, 1, 28730, 1, 31422, |
86 |
|
|
0, 28257, 0, 24797, 60, 0, 0, 0, |
87 |
|
|
60, 0, 0, 22105, 0, 27852, 60, 0, |
88 |
|
|
60, 0, -1, 24214, 0, 24642, 0, 23305, |
89 |
|
|
60, 0, 60, 0, 1, 22883, 0, 21601, |
90 |
|
|
60, 0, 2, 25650, 60, 0, -2, 31253, |
91 |
|
|
-2, 25144, 0, 17998 |
92 |
|
|
}; |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* Size of the MP-MLQ fixed excitation codebooks |
96 |
|
|
*/ |
97 |
|
|
static const int32_t max_pos[4] = {593775, 142506, 593775, 142506}; |
98 |
|
|
|
99 |
|
|
/** |
100 |
|
|
* 0.65^i (Zero part) and 0.75^i (Pole part) scaled by 2^15 |
101 |
|
|
*/ |
102 |
|
|
static const int16_t postfilter_tbl[2][LPC_ORDER] = { |
103 |
|
|
/* Zero */ |
104 |
|
|
{21299, 13844, 8999, 5849, 3802, 2471, 1606, 1044, 679, 441}, |
105 |
|
|
/* Pole */ |
106 |
|
|
{24576, 18432, 13824, 10368, 7776, 5832, 4374, 3281, 2460, 1845} |
107 |
|
|
}; |
108 |
|
|
|
109 |
|
|
static const int cng_adaptive_cb_lag[4] = { 1, 0, 1, 3 }; |
110 |
|
|
|
111 |
|
|
static const int cng_filt[4] = { 273, 998, 499, 333 }; |
112 |
|
|
|
113 |
|
|
static const int cng_bseg[3] = { 2048, 18432, 231233 }; |
114 |
|
|
|
115 |
|
20 |
static av_cold int g723_1_decode_init(AVCodecContext *avctx) |
116 |
|
|
{ |
117 |
|
20 |
G723_1_Context *s = avctx->priv_data; |
118 |
|
|
|
119 |
|
20 |
avctx->sample_fmt = AV_SAMPLE_FMT_S16P; |
120 |
✓✗✗✓
|
20 |
if (avctx->channels < 1 || avctx->channels > 2) { |
121 |
|
|
av_log(avctx, AV_LOG_ERROR, "Only mono and stereo are supported (requested channels: %d).\n", avctx->channels); |
122 |
|
|
return AVERROR(EINVAL); |
123 |
|
|
} |
124 |
✓✗ |
20 |
avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; |
125 |
✓✓ |
40 |
for (int ch = 0; ch < avctx->channels; ch++) { |
126 |
|
20 |
G723_1_ChannelContext *p = &s->ch[ch]; |
127 |
|
|
|
128 |
|
20 |
p->pf_gain = 1 << 12; |
129 |
|
|
|
130 |
|
20 |
memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(*p->prev_lsp)); |
131 |
|
20 |
memcpy(p->sid_lsp, dc_lsp, LPC_ORDER * sizeof(*p->sid_lsp)); |
132 |
|
|
|
133 |
|
20 |
p->cng_random_seed = CNG_RANDOM_SEED; |
134 |
|
20 |
p->past_frame_type = SID_FRAME; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
20 |
return 0; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
/** |
141 |
|
|
* Unpack the frame into parameters. |
142 |
|
|
* |
143 |
|
|
* @param p the context |
144 |
|
|
* @param buf pointer to the input buffer |
145 |
|
|
* @param buf_size size of the input buffer |
146 |
|
|
*/ |
147 |
|
626 |
static int unpack_bitstream(G723_1_ChannelContext *p, const uint8_t *buf, |
148 |
|
|
int buf_size) |
149 |
|
|
{ |
150 |
|
|
GetBitContext gb; |
151 |
|
|
int ad_cb_len; |
152 |
|
|
int temp, info_bits, i; |
153 |
|
|
int ret; |
154 |
|
|
|
155 |
|
626 |
ret = init_get_bits8(&gb, buf, buf_size); |
156 |
✗✓ |
626 |
if (ret < 0) |
157 |
|
|
return ret; |
158 |
|
|
|
159 |
|
|
/* Extract frame type and rate info */ |
160 |
|
626 |
info_bits = get_bits(&gb, 2); |
161 |
|
|
|
162 |
✓✓ |
626 |
if (info_bits == 3) { |
163 |
|
42 |
p->cur_frame_type = UNTRANSMITTED_FRAME; |
164 |
|
42 |
return 0; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
/* Extract 24 bit lsp indices, 8 bit for each band */ |
168 |
|
584 |
p->lsp_index[2] = get_bits(&gb, 8); |
169 |
|
584 |
p->lsp_index[1] = get_bits(&gb, 8); |
170 |
|
584 |
p->lsp_index[0] = get_bits(&gb, 8); |
171 |
|
|
|
172 |
✓✓ |
584 |
if (info_bits == 2) { |
173 |
|
7 |
p->cur_frame_type = SID_FRAME; |
174 |
|
7 |
p->subframe[0].amp_index = get_bits(&gb, 6); |
175 |
|
7 |
return 0; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
/* Extract the info common to both rates */ |
179 |
|
577 |
p->cur_rate = info_bits ? RATE_5300 : RATE_6300; |
180 |
|
577 |
p->cur_frame_type = ACTIVE_FRAME; |
181 |
|
|
|
182 |
|
577 |
p->pitch_lag[0] = get_bits(&gb, 7); |
183 |
✓✓ |
577 |
if (p->pitch_lag[0] > 123) /* test if forbidden code */ |
184 |
|
1 |
return -1; |
185 |
|
576 |
p->pitch_lag[0] += PITCH_MIN; |
186 |
|
576 |
p->subframe[1].ad_cb_lag = get_bits(&gb, 2); |
187 |
|
|
|
188 |
|
576 |
p->pitch_lag[1] = get_bits(&gb, 7); |
189 |
✓✓ |
576 |
if (p->pitch_lag[1] > 123) |
190 |
|
1 |
return -1; |
191 |
|
575 |
p->pitch_lag[1] += PITCH_MIN; |
192 |
|
575 |
p->subframe[3].ad_cb_lag = get_bits(&gb, 2); |
193 |
|
575 |
p->subframe[0].ad_cb_lag = 1; |
194 |
|
575 |
p->subframe[2].ad_cb_lag = 1; |
195 |
|
|
|
196 |
✓✓ |
2875 |
for (i = 0; i < SUBFRAMES; i++) { |
197 |
|
|
/* Extract combined gain */ |
198 |
|
2300 |
temp = get_bits(&gb, 12); |
199 |
|
2300 |
ad_cb_len = 170; |
200 |
|
2300 |
p->subframe[i].dirac_train = 0; |
201 |
✓✓✓✓
|
2300 |
if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) { |
202 |
|
1790 |
p->subframe[i].dirac_train = temp >> 11; |
203 |
|
1790 |
temp &= 0x7FF; |
204 |
|
1790 |
ad_cb_len = 85; |
205 |
|
|
} |
206 |
|
2300 |
p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS); |
207 |
✓✗ |
2300 |
if (p->subframe[i].ad_cb_gain < ad_cb_len) { |
208 |
|
2300 |
p->subframe[i].amp_index = temp - p->subframe[i].ad_cb_gain * |
209 |
|
|
GAIN_LEVELS; |
210 |
|
|
} else { |
211 |
|
|
return -1; |
212 |
|
|
} |
213 |
|
|
} |
214 |
|
|
|
215 |
|
575 |
p->subframe[0].grid_index = get_bits1(&gb); |
216 |
|
575 |
p->subframe[1].grid_index = get_bits1(&gb); |
217 |
|
575 |
p->subframe[2].grid_index = get_bits1(&gb); |
218 |
|
575 |
p->subframe[3].grid_index = get_bits1(&gb); |
219 |
|
|
|
220 |
✓✓ |
575 |
if (p->cur_rate == RATE_6300) { |
221 |
|
543 |
skip_bits1(&gb); /* skip reserved bit */ |
222 |
|
|
|
223 |
|
|
/* Compute pulse_pos index using the 13-bit combined position index */ |
224 |
|
543 |
temp = get_bits(&gb, 13); |
225 |
|
543 |
p->subframe[0].pulse_pos = temp / 810; |
226 |
|
|
|
227 |
|
543 |
temp -= p->subframe[0].pulse_pos * 810; |
228 |
|
543 |
p->subframe[1].pulse_pos = FASTDIV(temp, 90); |
229 |
|
|
|
230 |
|
543 |
temp -= p->subframe[1].pulse_pos * 90; |
231 |
|
543 |
p->subframe[2].pulse_pos = FASTDIV(temp, 9); |
232 |
|
543 |
p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9; |
233 |
|
|
|
234 |
|
1086 |
p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) + |
235 |
|
543 |
get_bits(&gb, 16); |
236 |
|
1086 |
p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) + |
237 |
|
543 |
get_bits(&gb, 14); |
238 |
|
1086 |
p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) + |
239 |
|
543 |
get_bits(&gb, 16); |
240 |
|
1086 |
p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) + |
241 |
|
543 |
get_bits(&gb, 14); |
242 |
|
|
|
243 |
|
543 |
p->subframe[0].pulse_sign = get_bits(&gb, 6); |
244 |
|
543 |
p->subframe[1].pulse_sign = get_bits(&gb, 5); |
245 |
|
543 |
p->subframe[2].pulse_sign = get_bits(&gb, 6); |
246 |
|
543 |
p->subframe[3].pulse_sign = get_bits(&gb, 5); |
247 |
|
|
} else { /* 5300 bps */ |
248 |
|
32 |
p->subframe[0].pulse_pos = get_bits(&gb, 12); |
249 |
|
32 |
p->subframe[1].pulse_pos = get_bits(&gb, 12); |
250 |
|
32 |
p->subframe[2].pulse_pos = get_bits(&gb, 12); |
251 |
|
32 |
p->subframe[3].pulse_pos = get_bits(&gb, 12); |
252 |
|
|
|
253 |
|
32 |
p->subframe[0].pulse_sign = get_bits(&gb, 4); |
254 |
|
32 |
p->subframe[1].pulse_sign = get_bits(&gb, 4); |
255 |
|
32 |
p->subframe[2].pulse_sign = get_bits(&gb, 4); |
256 |
|
32 |
p->subframe[3].pulse_sign = get_bits(&gb, 4); |
257 |
|
|
} |
258 |
|
|
|
259 |
|
575 |
return 0; |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
/** |
263 |
|
|
* Bitexact implementation of sqrt(val/2). |
264 |
|
|
*/ |
265 |
|
4175 |
static int16_t square_root(unsigned val) |
266 |
|
|
{ |
267 |
|
|
av_assert2(!(val & 0x80000000)); |
268 |
|
|
|
269 |
|
4175 |
return (ff_sqrt(val << 1) >> 1) & (~1); |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
/** |
273 |
|
|
* Generate fixed codebook excitation vector. |
274 |
|
|
* |
275 |
|
|
* @param vector decoded excitation vector |
276 |
|
|
* @param subfrm current subframe |
277 |
|
|
* @param cur_rate current bitrate |
278 |
|
|
* @param pitch_lag closed loop pitch lag |
279 |
|
|
* @param index current subframe index |
280 |
|
|
*/ |
281 |
|
2300 |
static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm, |
282 |
|
|
enum Rate cur_rate, int pitch_lag, int index) |
283 |
|
|
{ |
284 |
|
|
int temp, i, j; |
285 |
|
|
|
286 |
|
2300 |
memset(vector, 0, SUBFRAME_LEN * sizeof(*vector)); |
287 |
|
|
|
288 |
✓✓ |
2300 |
if (cur_rate == RATE_6300) { |
289 |
✓✓ |
2172 |
if (subfrm->pulse_pos >= max_pos[index]) |
290 |
|
3 |
return; |
291 |
|
|
|
292 |
|
|
/* Decode amplitudes and positions */ |
293 |
|
2169 |
j = PULSE_MAX - pulses[index]; |
294 |
|
2169 |
temp = subfrm->pulse_pos; |
295 |
✓✗ |
47580 |
for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) { |
296 |
|
47580 |
temp -= ff_g723_1_combinatorial_table[j][i]; |
297 |
✓✓ |
47580 |
if (temp >= 0) |
298 |
|
35652 |
continue; |
299 |
|
11928 |
temp += ff_g723_1_combinatorial_table[j++][i]; |
300 |
✓✓ |
11928 |
if (subfrm->pulse_sign & (1 << (PULSE_MAX - j))) { |
301 |
|
5889 |
vector[subfrm->grid_index + GRID_SIZE * i] = |
302 |
|
5889 |
-ff_g723_1_fixed_cb_gain[subfrm->amp_index]; |
303 |
|
|
} else { |
304 |
|
6039 |
vector[subfrm->grid_index + GRID_SIZE * i] = |
305 |
|
6039 |
ff_g723_1_fixed_cb_gain[subfrm->amp_index]; |
306 |
|
|
} |
307 |
✓✓ |
11928 |
if (j == PULSE_MAX) |
308 |
|
2169 |
break; |
309 |
|
|
} |
310 |
✓✓ |
2169 |
if (subfrm->dirac_train == 1) |
311 |
|
1085 |
ff_g723_1_gen_dirac_train(vector, pitch_lag); |
312 |
|
|
} else { /* 5300 bps */ |
313 |
|
128 |
int cb_gain = ff_g723_1_fixed_cb_gain[subfrm->amp_index]; |
314 |
|
128 |
int cb_shift = subfrm->grid_index; |
315 |
|
128 |
int cb_sign = subfrm->pulse_sign; |
316 |
|
128 |
int cb_pos = subfrm->pulse_pos; |
317 |
|
|
int offset, beta, lag; |
318 |
|
|
|
319 |
✓✓ |
640 |
for (i = 0; i < 8; i += 2) { |
320 |
|
512 |
offset = ((cb_pos & 7) << 3) + cb_shift + i; |
321 |
✓✓ |
512 |
vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain; |
322 |
|
512 |
cb_pos >>= 3; |
323 |
|
512 |
cb_sign >>= 1; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
/* Enhance harmonic components */ |
327 |
|
128 |
lag = pitch_contrib[subfrm->ad_cb_gain << 1] + pitch_lag + |
328 |
|
128 |
subfrm->ad_cb_lag - 1; |
329 |
|
128 |
beta = pitch_contrib[(subfrm->ad_cb_gain << 1) + 1]; |
330 |
|
|
|
331 |
✓✓ |
128 |
if (lag < SUBFRAME_LEN - 2) { |
332 |
✓✓ |
4154 |
for (i = lag; i < SUBFRAME_LEN; i++) |
333 |
|
4050 |
vector[i] += beta * vector[i - lag] >> 15; |
334 |
|
|
} |
335 |
|
|
} |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
/** |
339 |
|
|
* Estimate maximum auto-correlation around pitch lag. |
340 |
|
|
* |
341 |
|
|
* @param buf buffer with offset applied |
342 |
|
|
* @param offset offset of the excitation vector |
343 |
|
|
* @param ccr_max pointer to the maximum auto-correlation |
344 |
|
|
* @param pitch_lag decoded pitch lag |
345 |
|
|
* @param length length of autocorrelation |
346 |
|
|
* @param dir forward lag(1) / backward lag(-1) |
347 |
|
|
*/ |
348 |
|
4919 |
static int autocorr_max(const int16_t *buf, int offset, int *ccr_max, |
349 |
|
|
int pitch_lag, int length, int dir) |
350 |
|
|
{ |
351 |
|
4919 |
int limit, ccr, lag = 0; |
352 |
|
|
int i; |
353 |
|
|
|
354 |
|
4919 |
pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag); |
355 |
✓✓ |
4919 |
if (dir > 0) |
356 |
|
2172 |
limit = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3); |
357 |
|
|
else |
358 |
|
2747 |
limit = pitch_lag + 3; |
359 |
|
|
|
360 |
✓✓ |
34905 |
for (i = pitch_lag - 3; i <= limit; i++) { |
361 |
|
29986 |
ccr = ff_g723_1_dot_product(buf, buf + dir * i, length); |
362 |
|
|
|
363 |
✓✓ |
29986 |
if (ccr > *ccr_max) { |
364 |
|
11007 |
*ccr_max = ccr; |
365 |
|
11007 |
lag = i; |
366 |
|
|
} |
367 |
|
|
} |
368 |
|
4919 |
return lag; |
369 |
|
|
} |
370 |
|
|
|
371 |
|
|
/** |
372 |
|
|
* Calculate pitch postfilter optimal and scaling gains. |
373 |
|
|
* |
374 |
|
|
* @param lag pitch postfilter forward/backward lag |
375 |
|
|
* @param ppf pitch postfilter parameters |
376 |
|
|
* @param cur_rate current bitrate |
377 |
|
|
* @param tgt_eng target energy |
378 |
|
|
* @param ccr cross-correlation |
379 |
|
|
* @param res_eng residual energy |
380 |
|
|
*/ |
381 |
|
2162 |
static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate, |
382 |
|
|
int tgt_eng, int ccr, int res_eng) |
383 |
|
|
{ |
384 |
|
|
int pf_residual; /* square of postfiltered residual */ |
385 |
|
|
int temp1, temp2; |
386 |
|
|
|
387 |
|
2162 |
ppf->index = lag; |
388 |
|
|
|
389 |
|
2162 |
temp1 = tgt_eng * res_eng >> 1; |
390 |
|
2162 |
temp2 = ccr * ccr << 1; |
391 |
|
|
|
392 |
✓✓ |
2162 |
if (temp2 > temp1) { |
393 |
✓✓ |
1711 |
if (ccr >= res_eng) { |
394 |
|
327 |
ppf->opt_gain = ppf_gain_weight[cur_rate]; |
395 |
|
|
} else { |
396 |
|
1384 |
ppf->opt_gain = (ccr << 15) / res_eng * |
397 |
|
1384 |
ppf_gain_weight[cur_rate] >> 15; |
398 |
|
|
} |
399 |
|
|
/* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */ |
400 |
|
1711 |
temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1); |
401 |
|
1711 |
temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng; |
402 |
|
1711 |
pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16; |
403 |
|
|
|
404 |
✓✓ |
1711 |
if (tgt_eng >= pf_residual << 1) { |
405 |
|
4 |
temp1 = 0x7fff; |
406 |
|
|
} else { |
407 |
|
1707 |
temp1 = (tgt_eng << 14) / pf_residual; |
408 |
|
|
} |
409 |
|
|
|
410 |
|
|
/* scaling_gain = sqrt(tgt_eng/pf_res^2) */ |
411 |
|
1711 |
ppf->sc_gain = square_root(temp1 << 16); |
412 |
|
|
} else { |
413 |
|
451 |
ppf->opt_gain = 0; |
414 |
|
451 |
ppf->sc_gain = 0x7fff; |
415 |
|
|
} |
416 |
|
|
|
417 |
|
2162 |
ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15); |
418 |
|
2162 |
} |
419 |
|
|
|
420 |
|
|
/** |
421 |
|
|
* Calculate pitch postfilter parameters. |
422 |
|
|
* |
423 |
|
|
* @param p the context |
424 |
|
|
* @param offset offset of the excitation vector |
425 |
|
|
* @param pitch_lag decoded pitch lag |
426 |
|
|
* @param ppf pitch postfilter parameters |
427 |
|
|
* @param cur_rate current bitrate |
428 |
|
|
*/ |
429 |
|
2172 |
static void comp_ppf_coeff(G723_1_ChannelContext *p, int offset, int pitch_lag, |
430 |
|
|
PPFParam *ppf, enum Rate cur_rate) |
431 |
|
|
{ |
432 |
|
|
|
433 |
|
|
int16_t scale; |
434 |
|
|
int i; |
435 |
|
|
int temp1, temp2; |
436 |
|
|
|
437 |
|
|
/* |
438 |
|
|
* 0 - target energy |
439 |
|
|
* 1 - forward cross-correlation |
440 |
|
|
* 2 - forward residual energy |
441 |
|
|
* 3 - backward cross-correlation |
442 |
|
|
* 4 - backward residual energy |
443 |
|
|
*/ |
444 |
|
2172 |
int energy[5] = {0, 0, 0, 0, 0}; |
445 |
|
2172 |
int16_t *buf = p->audio + LPC_ORDER + offset; |
446 |
|
2172 |
int fwd_lag = autocorr_max(buf, offset, &energy[1], pitch_lag, |
447 |
|
|
SUBFRAME_LEN, 1); |
448 |
|
2172 |
int back_lag = autocorr_max(buf, offset, &energy[3], pitch_lag, |
449 |
|
|
SUBFRAME_LEN, -1); |
450 |
|
|
|
451 |
|
2172 |
ppf->index = 0; |
452 |
|
2172 |
ppf->opt_gain = 0; |
453 |
|
2172 |
ppf->sc_gain = 0x7fff; |
454 |
|
|
|
455 |
|
|
/* Case 0, Section 3.6 */ |
456 |
✓✓✓✓
|
2172 |
if (!back_lag && !fwd_lag) |
457 |
|
10 |
return; |
458 |
|
|
|
459 |
|
|
/* Compute target energy */ |
460 |
|
2162 |
energy[0] = ff_g723_1_dot_product(buf, buf, SUBFRAME_LEN); |
461 |
|
|
|
462 |
|
|
/* Compute forward residual energy */ |
463 |
✓✓ |
2162 |
if (fwd_lag) |
464 |
|
1537 |
energy[2] = ff_g723_1_dot_product(buf + fwd_lag, buf + fwd_lag, |
465 |
|
|
SUBFRAME_LEN); |
466 |
|
|
|
467 |
|
|
/* Compute backward residual energy */ |
468 |
✓✓ |
2162 |
if (back_lag) |
469 |
|
2156 |
energy[4] = ff_g723_1_dot_product(buf - back_lag, buf - back_lag, |
470 |
|
|
SUBFRAME_LEN); |
471 |
|
|
|
472 |
|
|
/* Normalize and shorten */ |
473 |
|
2162 |
temp1 = 0; |
474 |
✓✓ |
12972 |
for (i = 0; i < 5; i++) |
475 |
|
10810 |
temp1 = FFMAX(energy[i], temp1); |
476 |
|
|
|
477 |
|
2162 |
scale = ff_g723_1_normalize_bits(temp1, 31); |
478 |
✓✓ |
12972 |
for (i = 0; i < 5; i++) |
479 |
|
10810 |
energy[i] = (energy[i] << scale) >> 16; |
480 |
|
|
|
481 |
✓✓✓✓
|
2162 |
if (fwd_lag && !back_lag) { /* Case 1 */ |
482 |
|
6 |
comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1], |
483 |
|
|
energy[2]); |
484 |
✓✓ |
2156 |
} else if (!fwd_lag) { /* Case 2 */ |
485 |
|
625 |
comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3], |
486 |
|
|
energy[4]); |
487 |
|
|
} else { /* Case 3 */ |
488 |
|
|
|
489 |
|
|
/* |
490 |
|
|
* Select the largest of energy[1]^2/energy[2] |
491 |
|
|
* and energy[3]^2/energy[4] |
492 |
|
|
*/ |
493 |
|
1531 |
temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15); |
494 |
|
1531 |
temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15); |
495 |
✓✓ |
1531 |
if (temp1 >= temp2) { |
496 |
|
662 |
comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1], |
497 |
|
|
energy[2]); |
498 |
|
|
} else { |
499 |
|
869 |
comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3], |
500 |
|
|
energy[4]); |
501 |
|
|
} |
502 |
|
|
} |
503 |
|
|
} |
504 |
|
|
|
505 |
|
|
/** |
506 |
|
|
* Classify frames as voiced/unvoiced. |
507 |
|
|
* |
508 |
|
|
* @param p the context |
509 |
|
|
* @param pitch_lag decoded pitch_lag |
510 |
|
|
* @param exc_eng excitation energy estimation |
511 |
|
|
* @param scale scaling factor of exc_eng |
512 |
|
|
* |
513 |
|
|
* @return residual interpolation index if voiced, 0 otherwise |
514 |
|
|
*/ |
515 |
|
575 |
static int comp_interp_index(G723_1_ChannelContext *p, int pitch_lag, |
516 |
|
|
int *exc_eng, int *scale) |
517 |
|
|
{ |
518 |
|
575 |
int offset = PITCH_MAX + 2 * SUBFRAME_LEN; |
519 |
|
575 |
int16_t *buf = p->audio + LPC_ORDER; |
520 |
|
|
|
521 |
|
|
int index, ccr, tgt_eng, best_eng, temp; |
522 |
|
|
|
523 |
|
575 |
*scale = ff_g723_1_scale_vector(buf, p->excitation, FRAME_LEN + PITCH_MAX); |
524 |
|
575 |
buf += offset; |
525 |
|
|
|
526 |
|
|
/* Compute maximum backward cross-correlation */ |
527 |
|
575 |
ccr = 0; |
528 |
|
575 |
index = autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1); |
529 |
|
575 |
ccr = av_sat_add32(ccr, 1 << 15) >> 16; |
530 |
|
|
|
531 |
|
|
/* Compute target energy */ |
532 |
|
575 |
tgt_eng = ff_g723_1_dot_product(buf, buf, SUBFRAME_LEN * 2); |
533 |
|
575 |
*exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16; |
534 |
|
|
|
535 |
✓✓ |
575 |
if (ccr <= 0) |
536 |
|
1 |
return 0; |
537 |
|
|
|
538 |
|
|
/* Compute best energy */ |
539 |
|
574 |
best_eng = ff_g723_1_dot_product(buf - index, buf - index, |
540 |
|
|
SUBFRAME_LEN * 2); |
541 |
|
574 |
best_eng = av_sat_add32(best_eng, 1 << 15) >> 16; |
542 |
|
|
|
543 |
|
574 |
temp = best_eng * *exc_eng >> 3; |
544 |
|
|
|
545 |
✓✓ |
574 |
if (temp < ccr * ccr) { |
546 |
|
489 |
return index; |
547 |
|
|
} else |
548 |
|
85 |
return 0; |
549 |
|
|
} |
550 |
|
|
|
551 |
|
|
/** |
552 |
|
|
* Perform residual interpolation based on frame classification. |
553 |
|
|
* |
554 |
|
|
* @param buf decoded excitation vector |
555 |
|
|
* @param out output vector |
556 |
|
|
* @param lag decoded pitch lag |
557 |
|
|
* @param gain interpolated gain |
558 |
|
|
* @param rseed seed for random number generator |
559 |
|
|
*/ |
560 |
|
2 |
static void residual_interp(int16_t *buf, int16_t *out, int lag, |
561 |
|
|
int gain, int *rseed) |
562 |
|
|
{ |
563 |
|
|
int i; |
564 |
✓✗ |
2 |
if (lag) { /* Voiced */ |
565 |
|
2 |
int16_t *vector_ptr = buf + PITCH_MAX; |
566 |
|
|
/* Attenuate */ |
567 |
✓✓ |
80 |
for (i = 0; i < lag; i++) |
568 |
|
78 |
out[i] = vector_ptr[i - lag] * 3 >> 2; |
569 |
|
2 |
av_memcpy_backptr((uint8_t*)(out + lag), lag * sizeof(*out), |
570 |
|
2 |
(FRAME_LEN - lag) * sizeof(*out)); |
571 |
|
|
} else { /* Unvoiced */ |
572 |
|
|
for (i = 0; i < FRAME_LEN; i++) { |
573 |
|
|
*rseed = (int16_t)(*rseed * 521 + 259); |
574 |
|
|
out[i] = gain * *rseed >> 15; |
575 |
|
|
} |
576 |
|
|
memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(*buf)); |
577 |
|
|
} |
578 |
|
2 |
} |
579 |
|
|
|
580 |
|
|
/** |
581 |
|
|
* Perform IIR filtering. |
582 |
|
|
* |
583 |
|
|
* @param fir_coef FIR coefficients |
584 |
|
|
* @param iir_coef IIR coefficients |
585 |
|
|
* @param src source vector |
586 |
|
|
* @param dest destination vector |
587 |
|
|
* @param width width of the output, 16 bits(0) / 32 bits(1) |
588 |
|
|
*/ |
589 |
|
|
#define iir_filter(fir_coef, iir_coef, src, dest, width)\ |
590 |
|
|
{\ |
591 |
|
|
int m, n;\ |
592 |
|
|
int res_shift = 16 & ~-(width);\ |
593 |
|
|
int in_shift = 16 - res_shift;\ |
594 |
|
|
\ |
595 |
|
|
for (m = 0; m < SUBFRAME_LEN; m++) {\ |
596 |
|
|
int64_t filter = 0;\ |
597 |
|
|
for (n = 1; n <= LPC_ORDER; n++) {\ |
598 |
|
|
filter -= (fir_coef)[n - 1] * (src)[m - n] -\ |
599 |
|
|
(iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\ |
600 |
|
|
}\ |
601 |
|
|
\ |
602 |
|
|
(dest)[m] = av_clipl_int32(((src)[m] * 65536) + (filter * 8) +\ |
603 |
|
|
(1 << 15)) >> res_shift;\ |
604 |
|
|
}\ |
605 |
|
|
} |
606 |
|
|
|
607 |
|
|
/** |
608 |
|
|
* Adjust gain of postfiltered signal. |
609 |
|
|
* |
610 |
|
|
* @param p the context |
611 |
|
|
* @param buf postfiltered output vector |
612 |
|
|
* @param energy input energy coefficient |
613 |
|
|
*/ |
614 |
|
2376 |
static void gain_scale(G723_1_ChannelContext *p, int16_t * buf, int energy) |
615 |
|
|
{ |
616 |
|
|
int num, denom, gain, bits1, bits2; |
617 |
|
|
int i; |
618 |
|
|
|
619 |
|
2376 |
num = energy; |
620 |
|
2376 |
denom = 0; |
621 |
✓✓ |
144936 |
for (i = 0; i < SUBFRAME_LEN; i++) { |
622 |
|
142560 |
int temp = buf[i] >> 2; |
623 |
|
142560 |
temp *= temp; |
624 |
|
142560 |
denom = av_sat_dadd32(denom, temp); |
625 |
|
|
} |
626 |
|
|
|
627 |
✓✓✓✓
|
2376 |
if (num && denom) { |
628 |
|
2368 |
bits1 = ff_g723_1_normalize_bits(num, 31); |
629 |
|
2368 |
bits2 = ff_g723_1_normalize_bits(denom, 31); |
630 |
|
2368 |
num = num << bits1 >> 1; |
631 |
|
2368 |
denom <<= bits2; |
632 |
|
|
|
633 |
|
2368 |
bits2 = 5 + bits1 - bits2; |
634 |
|
2368 |
bits2 = av_clip_uintp2(bits2, 5); |
635 |
|
|
|
636 |
|
2368 |
gain = (num >> 1) / (denom >> 16); |
637 |
|
2368 |
gain = square_root(gain << 16 >> bits2); |
638 |
|
|
} else { |
639 |
|
8 |
gain = 1 << 12; |
640 |
|
|
} |
641 |
|
|
|
642 |
✓✓ |
144936 |
for (i = 0; i < SUBFRAME_LEN; i++) { |
643 |
|
142560 |
p->pf_gain = (15 * p->pf_gain + gain + (1 << 3)) >> 4; |
644 |
|
142560 |
buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) + |
645 |
|
|
(1 << 10)) >> 11); |
646 |
|
|
} |
647 |
|
2376 |
} |
648 |
|
|
|
649 |
|
|
/** |
650 |
|
|
* Perform formant filtering. |
651 |
|
|
* |
652 |
|
|
* @param p the context |
653 |
|
|
* @param lpc quantized lpc coefficients |
654 |
|
|
* @param buf input buffer |
655 |
|
|
* @param dst output buffer |
656 |
|
|
*/ |
657 |
|
594 |
static void formant_postfilter(G723_1_ChannelContext *p, int16_t *lpc, |
658 |
|
|
int16_t *buf, int16_t *dst) |
659 |
|
|
{ |
660 |
|
|
int16_t filter_coef[2][LPC_ORDER]; |
661 |
|
|
int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr; |
662 |
|
|
int i, j, k; |
663 |
|
|
|
664 |
|
594 |
memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(*buf)); |
665 |
|
594 |
memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(*filter_signal)); |
666 |
|
|
|
667 |
✓✓ |
2970 |
for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) { |
668 |
✓✓ |
26136 |
for (k = 0; k < LPC_ORDER; k++) { |
669 |
|
23760 |
filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] + |
670 |
|
23760 |
(1 << 14)) >> 15; |
671 |
|
23760 |
filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] + |
672 |
|
23760 |
(1 << 14)) >> 15; |
673 |
|
|
} |
674 |
✓✓✓✓
|
1570536 |
iir_filter(filter_coef[0], filter_coef[1], buf + i, filter_signal + i, 1); |
675 |
|
2376 |
lpc += LPC_ORDER; |
676 |
|
|
} |
677 |
|
|
|
678 |
|
594 |
memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(int16_t)); |
679 |
|
594 |
memcpy(p->iir_mem, filter_signal + FRAME_LEN, LPC_ORDER * sizeof(int)); |
680 |
|
|
|
681 |
|
594 |
buf += LPC_ORDER; |
682 |
|
594 |
signal_ptr = filter_signal + LPC_ORDER; |
683 |
✓✓ |
2970 |
for (i = 0; i < SUBFRAMES; i++) { |
684 |
|
|
int temp; |
685 |
|
|
int auto_corr[2]; |
686 |
|
|
int scale, energy; |
687 |
|
|
|
688 |
|
|
/* Normalize */ |
689 |
|
2376 |
scale = ff_g723_1_scale_vector(dst, buf, SUBFRAME_LEN); |
690 |
|
|
|
691 |
|
|
/* Compute auto correlation coefficients */ |
692 |
|
2376 |
auto_corr[0] = ff_g723_1_dot_product(dst, dst + 1, SUBFRAME_LEN - 1); |
693 |
|
2376 |
auto_corr[1] = ff_g723_1_dot_product(dst, dst, SUBFRAME_LEN); |
694 |
|
|
|
695 |
|
|
/* Compute reflection coefficient */ |
696 |
|
2376 |
temp = auto_corr[1] >> 16; |
697 |
✓✗ |
2376 |
if (temp) { |
698 |
|
2376 |
temp = (auto_corr[0] >> 2) / temp; |
699 |
|
|
} |
700 |
|
2376 |
p->reflection_coef = (3 * p->reflection_coef + temp + 2) >> 2; |
701 |
|
2376 |
temp = -p->reflection_coef >> 1 & ~3; |
702 |
|
|
|
703 |
|
|
/* Compensation filter */ |
704 |
✓✓ |
144936 |
for (j = 0; j < SUBFRAME_LEN; j++) { |
705 |
|
285120 |
dst[j] = av_sat_dadd32(signal_ptr[j], |
706 |
|
142560 |
(signal_ptr[j - 1] >> 16) * temp) >> 16; |
707 |
|
|
} |
708 |
|
|
|
709 |
|
|
/* Compute normalized signal energy */ |
710 |
|
2376 |
temp = 2 * scale + 4; |
711 |
✓✓ |
2376 |
if (temp < 0) { |
712 |
|
218 |
energy = av_clipl_int32((int64_t)auto_corr[1] << -temp); |
713 |
|
|
} else |
714 |
|
2158 |
energy = auto_corr[1] >> temp; |
715 |
|
|
|
716 |
|
2376 |
gain_scale(p, dst, energy); |
717 |
|
|
|
718 |
|
2376 |
buf += SUBFRAME_LEN; |
719 |
|
2376 |
signal_ptr += SUBFRAME_LEN; |
720 |
|
2376 |
dst += SUBFRAME_LEN; |
721 |
|
|
} |
722 |
|
594 |
} |
723 |
|
|
|
724 |
|
7 |
static int sid_gain_to_lsp_index(int gain) |
725 |
|
|
{ |
726 |
✓✗ |
7 |
if (gain < 0x10) |
727 |
|
7 |
return gain << 6; |
728 |
|
|
else if (gain < 0x20) |
729 |
|
|
return gain - 8 << 7; |
730 |
|
|
else |
731 |
|
|
return gain - 20 << 8; |
732 |
|
|
} |
733 |
|
|
|
734 |
|
1470 |
static inline int cng_rand(int *state, int base) |
735 |
|
|
{ |
736 |
|
1470 |
*state = (*state * 521 + 259) & 0xFFFF; |
737 |
|
1470 |
return (*state & 0x7FFF) * base >> 15; |
738 |
|
|
} |
739 |
|
|
|
740 |
|
|
static int estimate_sid_gain(G723_1_ChannelContext *p) |
741 |
|
|
{ |
742 |
|
|
int i, shift, seg, seg2, t, val, val_add, x, y; |
743 |
|
|
|
744 |
|
|
shift = 16 - p->cur_gain * 2; |
745 |
|
|
if (shift > 0) { |
746 |
|
|
if (p->sid_gain == 0) { |
747 |
|
|
t = 0; |
748 |
|
|
} else if (shift >= 31 || (int32_t)((uint32_t)p->sid_gain << shift) >> shift != p->sid_gain) { |
749 |
|
|
if (p->sid_gain < 0) t = INT32_MIN; |
750 |
|
|
else t = INT32_MAX; |
751 |
|
|
} else |
752 |
|
|
t = p->sid_gain * (1 << shift); |
753 |
|
|
} else if(shift < -31) { |
754 |
|
|
t = (p->sid_gain < 0) ? -1 : 0; |
755 |
|
|
}else |
756 |
|
|
t = p->sid_gain >> -shift; |
757 |
|
|
x = av_clipl_int32(t * (int64_t)cng_filt[0] >> 16); |
758 |
|
|
|
759 |
|
|
if (x >= cng_bseg[2]) |
760 |
|
|
return 0x3F; |
761 |
|
|
|
762 |
|
|
if (x >= cng_bseg[1]) { |
763 |
|
|
shift = 4; |
764 |
|
|
seg = 3; |
765 |
|
|
} else { |
766 |
|
|
shift = 3; |
767 |
|
|
seg = (x >= cng_bseg[0]); |
768 |
|
|
} |
769 |
|
|
seg2 = FFMIN(seg, 3); |
770 |
|
|
|
771 |
|
|
val = 1 << shift; |
772 |
|
|
val_add = val >> 1; |
773 |
|
|
for (i = 0; i < shift; i++) { |
774 |
|
|
t = seg * 32 + (val << seg2); |
775 |
|
|
t *= t; |
776 |
|
|
if (x >= t) |
777 |
|
|
val += val_add; |
778 |
|
|
else |
779 |
|
|
val -= val_add; |
780 |
|
|
val_add >>= 1; |
781 |
|
|
} |
782 |
|
|
|
783 |
|
|
t = seg * 32 + (val << seg2); |
784 |
|
|
y = t * t - x; |
785 |
|
|
if (y <= 0) { |
786 |
|
|
t = seg * 32 + (val + 1 << seg2); |
787 |
|
|
t = t * t - x; |
788 |
|
|
val = (seg2 - 1) * 16 + val; |
789 |
|
|
if (t >= y) |
790 |
|
|
val++; |
791 |
|
|
} else { |
792 |
|
|
t = seg * 32 + (val - 1 << seg2); |
793 |
|
|
t = t * t - x; |
794 |
|
|
val = (seg2 - 1) * 16 + val; |
795 |
|
|
if (t >= y) |
796 |
|
|
val--; |
797 |
|
|
} |
798 |
|
|
|
799 |
|
|
return val; |
800 |
|
|
} |
801 |
|
|
|
802 |
|
49 |
static void generate_noise(G723_1_ChannelContext *p) |
803 |
|
|
{ |
804 |
|
|
int i, j, idx, t; |
805 |
|
|
int off[SUBFRAMES]; |
806 |
|
|
int signs[SUBFRAMES / 2 * 11], pos[SUBFRAMES / 2 * 11]; |
807 |
|
|
int tmp[SUBFRAME_LEN * 2]; |
808 |
|
|
int16_t *vector_ptr; |
809 |
|
|
int64_t sum; |
810 |
|
|
int b0, c, delta, x, shift; |
811 |
|
|
|
812 |
|
49 |
p->pitch_lag[0] = cng_rand(&p->cng_random_seed, 21) + 123; |
813 |
|
49 |
p->pitch_lag[1] = cng_rand(&p->cng_random_seed, 19) + 123; |
814 |
|
|
|
815 |
✓✓ |
245 |
for (i = 0; i < SUBFRAMES; i++) { |
816 |
|
196 |
p->subframe[i].ad_cb_gain = cng_rand(&p->cng_random_seed, 50) + 1; |
817 |
|
196 |
p->subframe[i].ad_cb_lag = cng_adaptive_cb_lag[i]; |
818 |
|
|
} |
819 |
|
|
|
820 |
✓✓ |
147 |
for (i = 0; i < SUBFRAMES / 2; i++) { |
821 |
|
98 |
t = cng_rand(&p->cng_random_seed, 1 << 13); |
822 |
|
98 |
off[i * 2] = t & 1; |
823 |
|
98 |
off[i * 2 + 1] = ((t >> 1) & 1) + SUBFRAME_LEN; |
824 |
|
98 |
t >>= 2; |
825 |
✓✓ |
1176 |
for (j = 0; j < 11; j++) { |
826 |
|
1078 |
signs[i * 11 + j] = ((t & 1) * 2 - 1) * (1 << 14); |
827 |
|
1078 |
t >>= 1; |
828 |
|
|
} |
829 |
|
|
} |
830 |
|
|
|
831 |
|
49 |
idx = 0; |
832 |
✓✓ |
245 |
for (i = 0; i < SUBFRAMES; i++) { |
833 |
✓✓ |
6076 |
for (j = 0; j < SUBFRAME_LEN / 2; j++) |
834 |
|
5880 |
tmp[j] = j; |
835 |
|
196 |
t = SUBFRAME_LEN / 2; |
836 |
✓✓ |
1274 |
for (j = 0; j < pulses[i]; j++, idx++) { |
837 |
|
1078 |
int idx2 = cng_rand(&p->cng_random_seed, t); |
838 |
|
|
|
839 |
|
1078 |
pos[idx] = tmp[idx2] * 2 + off[i]; |
840 |
|
1078 |
tmp[idx2] = tmp[--t]; |
841 |
|
|
} |
842 |
|
|
} |
843 |
|
|
|
844 |
|
49 |
vector_ptr = p->audio + LPC_ORDER; |
845 |
|
49 |
memcpy(vector_ptr, p->prev_excitation, |
846 |
|
|
PITCH_MAX * sizeof(*p->excitation)); |
847 |
✓✓ |
147 |
for (i = 0; i < SUBFRAMES; i += 2) { |
848 |
|
98 |
ff_g723_1_gen_acb_excitation(vector_ptr, vector_ptr, |
849 |
|
98 |
p->pitch_lag[i >> 1], &p->subframe[i], |
850 |
|
|
p->cur_rate); |
851 |
|
98 |
ff_g723_1_gen_acb_excitation(vector_ptr + SUBFRAME_LEN, |
852 |
|
|
vector_ptr + SUBFRAME_LEN, |
853 |
|
98 |
p->pitch_lag[i >> 1], &p->subframe[i + 1], |
854 |
|
|
p->cur_rate); |
855 |
|
|
|
856 |
|
98 |
t = 0; |
857 |
✓✓ |
11858 |
for (j = 0; j < SUBFRAME_LEN * 2; j++) |
858 |
|
11760 |
t |= FFABS(vector_ptr[j]); |
859 |
|
98 |
t = FFMIN(t, 0x7FFF); |
860 |
✗✓ |
98 |
if (!t) { |
861 |
|
|
shift = 0; |
862 |
|
|
} else { |
863 |
|
98 |
shift = -10 + av_log2(t); |
864 |
✓✓ |
98 |
if (shift < -2) |
865 |
|
96 |
shift = -2; |
866 |
|
|
} |
867 |
|
98 |
sum = 0; |
868 |
✓✓ |
98 |
if (shift < 0) { |
869 |
✓✓ |
11616 |
for (j = 0; j < SUBFRAME_LEN * 2; j++) { |
870 |
|
11520 |
t = vector_ptr[j] * (1 << -shift); |
871 |
|
11520 |
sum += t * t; |
872 |
|
11520 |
tmp[j] = t; |
873 |
|
|
} |
874 |
|
|
} else { |
875 |
✓✓ |
242 |
for (j = 0; j < SUBFRAME_LEN * 2; j++) { |
876 |
|
240 |
t = vector_ptr[j] >> shift; |
877 |
|
240 |
sum += t * t; |
878 |
|
240 |
tmp[j] = t; |
879 |
|
|
} |
880 |
|
|
} |
881 |
|
|
|
882 |
|
98 |
b0 = 0; |
883 |
✓✓ |
1176 |
for (j = 0; j < 11; j++) |
884 |
|
1078 |
b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j]; |
885 |
|
98 |
b0 = b0 * 2 * 2979LL + (1 << 29) >> 30; // approximated division by 11 |
886 |
|
|
|
887 |
|
98 |
c = p->cur_gain * (p->cur_gain * SUBFRAME_LEN >> 5); |
888 |
✓✓ |
98 |
if (shift * 2 + 3 >= 0) |
889 |
|
2 |
c >>= shift * 2 + 3; |
890 |
|
|
else |
891 |
|
96 |
c <<= -(shift * 2 + 3); |
892 |
|
98 |
c = (av_clipl_int32(sum << 1) - c) * 2979LL >> 15; |
893 |
|
|
|
894 |
|
98 |
delta = b0 * b0 * 2 - c; |
895 |
✓✓ |
98 |
if (delta <= 0) { |
896 |
|
2 |
x = -b0; |
897 |
|
|
} else { |
898 |
|
96 |
delta = square_root(delta); |
899 |
|
96 |
x = delta - b0; |
900 |
|
96 |
t = delta + b0; |
901 |
✓✓ |
96 |
if (FFABS(t) < FFABS(x)) |
902 |
|
44 |
x = -t; |
903 |
|
|
} |
904 |
|
98 |
shift++; |
905 |
✓✓ |
98 |
if (shift < 0) |
906 |
|
96 |
x >>= -shift; |
907 |
|
|
else |
908 |
|
2 |
x *= 1 << shift; |
909 |
|
98 |
x = av_clip(x, -10000, 10000); |
910 |
|
|
|
911 |
✓✓ |
1176 |
for (j = 0; j < 11; j++) { |
912 |
|
1078 |
idx = (i / 2) * 11 + j; |
913 |
|
1078 |
vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] + |
914 |
|
1078 |
(x * signs[idx] >> 15)); |
915 |
|
|
} |
916 |
|
|
|
917 |
|
|
/* copy decoded data to serve as a history for the next decoded subframes */ |
918 |
|
98 |
memcpy(vector_ptr + PITCH_MAX, vector_ptr, |
919 |
|
|
sizeof(*vector_ptr) * SUBFRAME_LEN * 2); |
920 |
|
98 |
vector_ptr += SUBFRAME_LEN * 2; |
921 |
|
|
} |
922 |
|
|
/* Save the excitation for the next frame */ |
923 |
|
49 |
memcpy(p->prev_excitation, p->audio + LPC_ORDER + FRAME_LEN, |
924 |
|
|
PITCH_MAX * sizeof(*p->excitation)); |
925 |
|
49 |
} |
926 |
|
|
|
927 |
|
626 |
static int g723_1_decode_frame(AVCodecContext *avctx, void *data, |
928 |
|
|
int *got_frame_ptr, AVPacket *avpkt) |
929 |
|
|
{ |
930 |
|
626 |
G723_1_Context *s = avctx->priv_data; |
931 |
|
626 |
AVFrame *frame = data; |
932 |
|
626 |
const uint8_t *buf = avpkt->data; |
933 |
|
626 |
int buf_size = avpkt->size; |
934 |
|
626 |
int dec_mode = buf[0] & 3; |
935 |
|
|
|
936 |
|
|
PPFParam ppf[SUBFRAMES]; |
937 |
|
|
int16_t cur_lsp[LPC_ORDER]; |
938 |
|
|
int16_t lpc[SUBFRAMES * LPC_ORDER]; |
939 |
|
|
int16_t acb_vector[SUBFRAME_LEN]; |
940 |
|
|
int16_t *out; |
941 |
|
626 |
int bad_frame = 0, i, j, ret; |
942 |
|
|
|
943 |
✗✓ |
626 |
if (buf_size < frame_size[dec_mode] * avctx->channels) { |
944 |
|
|
if (buf_size) |
945 |
|
|
av_log(avctx, AV_LOG_WARNING, |
946 |
|
|
"Expected %d bytes, got %d - skipping packet\n", |
947 |
|
|
frame_size[dec_mode], buf_size); |
948 |
|
|
*got_frame_ptr = 0; |
949 |
|
|
return buf_size; |
950 |
|
|
} |
951 |
|
|
|
952 |
|
626 |
frame->nb_samples = FRAME_LEN; |
953 |
✗✓ |
626 |
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
954 |
|
|
return ret; |
955 |
|
|
|
956 |
✓✓ |
1252 |
for (int ch = 0; ch < avctx->channels; ch++) { |
957 |
|
626 |
G723_1_ChannelContext *p = &s->ch[ch]; |
958 |
|
626 |
int16_t *audio = p->audio; |
959 |
|
|
|
960 |
✓✓ |
626 |
if (unpack_bitstream(p, buf + ch * (buf_size / avctx->channels), |
961 |
|
626 |
buf_size / avctx->channels) < 0) { |
962 |
|
2 |
bad_frame = 1; |
963 |
✓✗ |
2 |
if (p->past_frame_type == ACTIVE_FRAME) |
964 |
|
2 |
p->cur_frame_type = ACTIVE_FRAME; |
965 |
|
|
else |
966 |
|
|
p->cur_frame_type = UNTRANSMITTED_FRAME; |
967 |
|
|
} |
968 |
|
|
|
969 |
|
626 |
out = (int16_t *)frame->extended_data[ch]; |
970 |
|
|
|
971 |
✓✓ |
626 |
if (p->cur_frame_type == ACTIVE_FRAME) { |
972 |
✓✓ |
577 |
if (!bad_frame) |
973 |
|
575 |
p->erased_frames = 0; |
974 |
✓✗ |
2 |
else if (p->erased_frames != 3) |
975 |
|
2 |
p->erased_frames++; |
976 |
|
|
|
977 |
|
577 |
ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame); |
978 |
|
577 |
ff_g723_1_lsp_interpolate(lpc, cur_lsp, p->prev_lsp); |
979 |
|
|
|
980 |
|
|
/* Save the lsp_vector for the next frame */ |
981 |
|
577 |
memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(*p->prev_lsp)); |
982 |
|
|
|
983 |
|
|
/* Generate the excitation for the frame */ |
984 |
|
577 |
memcpy(p->excitation, p->prev_excitation, |
985 |
|
|
PITCH_MAX * sizeof(*p->excitation)); |
986 |
✓✓ |
577 |
if (!p->erased_frames) { |
987 |
|
575 |
int16_t *vector_ptr = p->excitation + PITCH_MAX; |
988 |
|
|
|
989 |
|
|
/* Update interpolation gain memory */ |
990 |
|
575 |
p->interp_gain = ff_g723_1_fixed_cb_gain[(p->subframe[2].amp_index + |
991 |
|
575 |
p->subframe[3].amp_index) >> 1]; |
992 |
✓✓ |
2875 |
for (i = 0; i < SUBFRAMES; i++) { |
993 |
|
2300 |
gen_fcb_excitation(vector_ptr, &p->subframe[i], p->cur_rate, |
994 |
|
2300 |
p->pitch_lag[i >> 1], i); |
995 |
|
2300 |
ff_g723_1_gen_acb_excitation(acb_vector, |
996 |
|
2300 |
&p->excitation[SUBFRAME_LEN * i], |
997 |
|
2300 |
p->pitch_lag[i >> 1], |
998 |
|
|
&p->subframe[i], p->cur_rate); |
999 |
|
|
/* Get the total excitation */ |
1000 |
✓✓ |
140300 |
for (j = 0; j < SUBFRAME_LEN; j++) { |
1001 |
|
138000 |
int v = av_clip_int16(vector_ptr[j] * 2); |
1002 |
|
138000 |
vector_ptr[j] = av_clip_int16(v + acb_vector[j]); |
1003 |
|
|
} |
1004 |
|
2300 |
vector_ptr += SUBFRAME_LEN; |
1005 |
|
|
} |
1006 |
|
|
|
1007 |
|
575 |
vector_ptr = p->excitation + PITCH_MAX; |
1008 |
|
|
|
1009 |
|
575 |
p->interp_index = comp_interp_index(p, p->pitch_lag[1], |
1010 |
|
|
&p->sid_gain, &p->cur_gain); |
1011 |
|
|
|
1012 |
|
|
/* Perform pitch postfiltering */ |
1013 |
✓✓ |
575 |
if (s->postfilter) { |
1014 |
|
543 |
i = PITCH_MAX; |
1015 |
✓✓ |
2715 |
for (j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) |
1016 |
|
2172 |
comp_ppf_coeff(p, i, p->pitch_lag[j >> 1], |
1017 |
|
2172 |
ppf + j, p->cur_rate); |
1018 |
|
|
|
1019 |
✓✓ |
2715 |
for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) |
1020 |
|
2172 |
ff_acelp_weighted_vector_sum(p->audio + LPC_ORDER + i, |
1021 |
|
2172 |
vector_ptr + i, |
1022 |
|
2172 |
vector_ptr + i + ppf[j].index, |
1023 |
|
2172 |
ppf[j].sc_gain, |
1024 |
|
2172 |
ppf[j].opt_gain, |
1025 |
|
|
1 << 14, 15, SUBFRAME_LEN); |
1026 |
|
|
} else { |
1027 |
|
32 |
audio = vector_ptr - LPC_ORDER; |
1028 |
|
|
} |
1029 |
|
|
|
1030 |
|
|
/* Save the excitation for the next frame */ |
1031 |
|
575 |
memcpy(p->prev_excitation, p->excitation + FRAME_LEN, |
1032 |
|
|
PITCH_MAX * sizeof(*p->excitation)); |
1033 |
|
|
} else { |
1034 |
|
2 |
p->interp_gain = (p->interp_gain * 3 + 2) >> 2; |
1035 |
✗✓ |
2 |
if (p->erased_frames == 3) { |
1036 |
|
|
/* Mute output */ |
1037 |
|
|
memset(p->excitation, 0, |
1038 |
|
|
(FRAME_LEN + PITCH_MAX) * sizeof(*p->excitation)); |
1039 |
|
|
memset(p->prev_excitation, 0, |
1040 |
|
|
PITCH_MAX * sizeof(*p->excitation)); |
1041 |
|
|
memset(frame->data[0], 0, |
1042 |
|
|
(FRAME_LEN + LPC_ORDER) * sizeof(int16_t)); |
1043 |
|
|
} else { |
1044 |
|
2 |
int16_t *buf = p->audio + LPC_ORDER; |
1045 |
|
|
|
1046 |
|
|
/* Regenerate frame */ |
1047 |
|
2 |
residual_interp(p->excitation, buf, p->interp_index, |
1048 |
|
|
p->interp_gain, &p->random_seed); |
1049 |
|
|
|
1050 |
|
|
/* Save the excitation for the next frame */ |
1051 |
|
2 |
memcpy(p->prev_excitation, buf + (FRAME_LEN - PITCH_MAX), |
1052 |
|
|
PITCH_MAX * sizeof(*p->excitation)); |
1053 |
|
|
} |
1054 |
|
|
} |
1055 |
|
577 |
p->cng_random_seed = CNG_RANDOM_SEED; |
1056 |
|
|
} else { |
1057 |
✓✓ |
49 |
if (p->cur_frame_type == SID_FRAME) { |
1058 |
|
7 |
p->sid_gain = sid_gain_to_lsp_index(p->subframe[0].amp_index); |
1059 |
|
7 |
ff_g723_1_inverse_quant(p->sid_lsp, p->prev_lsp, p->lsp_index, 0); |
1060 |
✗✓ |
42 |
} else if (p->past_frame_type == ACTIVE_FRAME) { |
1061 |
|
|
p->sid_gain = estimate_sid_gain(p); |
1062 |
|
|
} |
1063 |
|
|
|
1064 |
✓✓ |
49 |
if (p->past_frame_type == ACTIVE_FRAME) |
1065 |
|
3 |
p->cur_gain = p->sid_gain; |
1066 |
|
|
else |
1067 |
|
46 |
p->cur_gain = (p->cur_gain * 7 + p->sid_gain) >> 3; |
1068 |
|
49 |
generate_noise(p); |
1069 |
|
49 |
ff_g723_1_lsp_interpolate(lpc, p->sid_lsp, p->prev_lsp); |
1070 |
|
|
/* Save the lsp_vector for the next frame */ |
1071 |
|
49 |
memcpy(p->prev_lsp, p->sid_lsp, LPC_ORDER * sizeof(*p->prev_lsp)); |
1072 |
|
|
} |
1073 |
|
|
|
1074 |
|
626 |
p->past_frame_type = p->cur_frame_type; |
1075 |
|
|
|
1076 |
|
626 |
memcpy(p->audio, p->synth_mem, LPC_ORDER * sizeof(*p->audio)); |
1077 |
✓✓ |
3130 |
for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) |
1078 |
|
2504 |
ff_celp_lp_synthesis_filter(p->audio + i, &lpc[j * LPC_ORDER], |
1079 |
|
2504 |
audio + i, SUBFRAME_LEN, LPC_ORDER, |
1080 |
|
|
0, 1, 1 << 12); |
1081 |
|
626 |
memcpy(p->synth_mem, p->audio + FRAME_LEN, LPC_ORDER * sizeof(*p->audio)); |
1082 |
|
|
|
1083 |
✓✓ |
626 |
if (s->postfilter) { |
1084 |
|
594 |
formant_postfilter(p, lpc, p->audio, out); |
1085 |
|
|
} else { // if output is not postfiltered it should be scaled by 2 |
1086 |
✓✓ |
7712 |
for (i = 0; i < FRAME_LEN; i++) |
1087 |
|
7680 |
out[i] = av_clip_int16(2 * p->audio[LPC_ORDER + i]); |
1088 |
|
|
} |
1089 |
|
|
} |
1090 |
|
|
|
1091 |
|
626 |
*got_frame_ptr = 1; |
1092 |
|
|
|
1093 |
|
626 |
return frame_size[dec_mode] * avctx->channels; |
1094 |
|
|
} |
1095 |
|
|
|
1096 |
|
|
#define OFFSET(x) offsetof(G723_1_Context, x) |
1097 |
|
|
#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM |
1098 |
|
|
|
1099 |
|
|
static const AVOption options[] = { |
1100 |
|
|
{ "postfilter", "enable postfilter", OFFSET(postfilter), AV_OPT_TYPE_BOOL, |
1101 |
|
|
{ .i64 = 1 }, 0, 1, AD }, |
1102 |
|
|
{ NULL } |
1103 |
|
|
}; |
1104 |
|
|
|
1105 |
|
|
|
1106 |
|
|
static const AVClass g723_1dec_class = { |
1107 |
|
|
.class_name = "G.723.1 decoder", |
1108 |
|
|
.item_name = av_default_item_name, |
1109 |
|
|
.option = options, |
1110 |
|
|
.version = LIBAVUTIL_VERSION_INT, |
1111 |
|
|
}; |
1112 |
|
|
|
1113 |
|
|
AVCodec ff_g723_1_decoder = { |
1114 |
|
|
.name = "g723_1", |
1115 |
|
|
.long_name = NULL_IF_CONFIG_SMALL("G.723.1"), |
1116 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
1117 |
|
|
.id = AV_CODEC_ID_G723_1, |
1118 |
|
|
.priv_data_size = sizeof(G723_1_Context), |
1119 |
|
|
.init = g723_1_decode_init, |
1120 |
|
|
.decode = g723_1_decode_frame, |
1121 |
|
|
.capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1, |
1122 |
|
|
.priv_class = &g723_1dec_class, |
1123 |
|
|
}; |