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