1 | /*
|
---|
2 | * DSP Group TrueSpeech compatible decoder
|
---|
3 | * Copyright (c) 2005 Konstantin Shishkov
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | */
|
---|
19 | #include "avcodec.h"
|
---|
20 |
|
---|
21 | #include "truespeech_data.h"
|
---|
22 | /**
|
---|
23 | * @file truespeech.c
|
---|
24 | * TrueSpeech decoder.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * TrueSpeech decoder context
|
---|
29 | */
|
---|
30 | typedef struct {
|
---|
31 | /* input data */
|
---|
32 | int16_t vector[8]; //< input vector: 5/5/4/4/4/3/3/3
|
---|
33 | int offset1[2]; //< 8-bit value, used in one copying offset
|
---|
34 | int offset2[4]; //< 7-bit value, encodes offsets for copying and for two-point filter
|
---|
35 | int pulseoff[4]; //< 4-bit offset of pulse values block
|
---|
36 | int pulsepos[4]; //< 27-bit variable, encodes 7 pulse positions
|
---|
37 | int pulseval[4]; //< 7x2-bit pulse values
|
---|
38 | int flag; //< 1-bit flag, shows how to choose filters
|
---|
39 | /* temporary data */
|
---|
40 | int filtbuf[146]; // some big vector used for storing filters
|
---|
41 | int prevfilt[8]; // filter from previous frame
|
---|
42 | int16_t tmp1[8]; // coefficients for adding to out
|
---|
43 | int16_t tmp2[8]; // coefficients for adding to out
|
---|
44 | int16_t tmp3[8]; // coefficients for adding to out
|
---|
45 | int16_t cvector[8]; // correlated input vector
|
---|
46 | int filtval; // gain value for one function
|
---|
47 | int16_t newvec[60]; // tmp vector
|
---|
48 | int16_t filters[32]; // filters for every subframe
|
---|
49 | } TSContext;
|
---|
50 |
|
---|
51 | static int truespeech_decode_init(AVCodecContext * avctx)
|
---|
52 | {
|
---|
53 | // TSContext *c = avctx->priv_data;
|
---|
54 |
|
---|
55 | return 0;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static void truespeech_read_frame(TSContext *dec, uint8_t *input)
|
---|
59 | {
|
---|
60 | uint32_t t;
|
---|
61 |
|
---|
62 | /* first dword */
|
---|
63 | t = LE_32(input);
|
---|
64 | input += 4;
|
---|
65 |
|
---|
66 | dec->flag = t & 1;
|
---|
67 |
|
---|
68 | dec->vector[0] = ts_codebook[0][(t >> 1) & 0x1F];
|
---|
69 | dec->vector[1] = ts_codebook[1][(t >> 6) & 0x1F];
|
---|
70 | dec->vector[2] = ts_codebook[2][(t >> 11) & 0xF];
|
---|
71 | dec->vector[3] = ts_codebook[3][(t >> 15) & 0xF];
|
---|
72 | dec->vector[4] = ts_codebook[4][(t >> 19) & 0xF];
|
---|
73 | dec->vector[5] = ts_codebook[5][(t >> 23) & 0x7];
|
---|
74 | dec->vector[6] = ts_codebook[6][(t >> 26) & 0x7];
|
---|
75 | dec->vector[7] = ts_codebook[7][(t >> 29) & 0x7];
|
---|
76 |
|
---|
77 | /* second dword */
|
---|
78 | t = LE_32(input);
|
---|
79 | input += 4;
|
---|
80 |
|
---|
81 | dec->offset2[0] = (t >> 0) & 0x7F;
|
---|
82 | dec->offset2[1] = (t >> 7) & 0x7F;
|
---|
83 | dec->offset2[2] = (t >> 14) & 0x7F;
|
---|
84 | dec->offset2[3] = (t >> 21) & 0x7F;
|
---|
85 |
|
---|
86 | dec->offset1[0] = ((t >> 28) & 0xF) << 4;
|
---|
87 |
|
---|
88 | /* third dword */
|
---|
89 | t = LE_32(input);
|
---|
90 | input += 4;
|
---|
91 |
|
---|
92 | dec->pulseval[0] = (t >> 0) & 0x3FFF;
|
---|
93 | dec->pulseval[1] = (t >> 14) & 0x3FFF;
|
---|
94 |
|
---|
95 | dec->offset1[1] = (t >> 28) & 0x0F;
|
---|
96 |
|
---|
97 | /* fourth dword */
|
---|
98 | t = LE_32(input);
|
---|
99 | input += 4;
|
---|
100 |
|
---|
101 | dec->pulseval[2] = (t >> 0) & 0x3FFF;
|
---|
102 | dec->pulseval[3] = (t >> 14) & 0x3FFF;
|
---|
103 |
|
---|
104 | dec->offset1[1] |= ((t >> 28) & 0x0F) << 4;
|
---|
105 |
|
---|
106 | /* fifth dword */
|
---|
107 | t = LE_32(input);
|
---|
108 | input += 4;
|
---|
109 |
|
---|
110 | dec->pulsepos[0] = (t >> 4) & 0x7FFFFFF;
|
---|
111 |
|
---|
112 | dec->pulseoff[0] = (t >> 0) & 0xF;
|
---|
113 |
|
---|
114 | dec->offset1[0] |= (t >> 31) & 1;
|
---|
115 |
|
---|
116 | /* sixth dword */
|
---|
117 | t = LE_32(input);
|
---|
118 | input += 4;
|
---|
119 |
|
---|
120 | dec->pulsepos[1] = (t >> 4) & 0x7FFFFFF;
|
---|
121 |
|
---|
122 | dec->pulseoff[1] = (t >> 0) & 0xF;
|
---|
123 |
|
---|
124 | dec->offset1[0] |= ((t >> 31) & 1) << 1;
|
---|
125 |
|
---|
126 | /* seventh dword */
|
---|
127 | t = LE_32(input);
|
---|
128 | input += 4;
|
---|
129 |
|
---|
130 | dec->pulsepos[2] = (t >> 4) & 0x7FFFFFF;
|
---|
131 |
|
---|
132 | dec->pulseoff[2] = (t >> 0) & 0xF;
|
---|
133 |
|
---|
134 | dec->offset1[0] |= ((t >> 31) & 1) << 2;
|
---|
135 |
|
---|
136 | /* eighth dword */
|
---|
137 | t = LE_32(input);
|
---|
138 | input += 4;
|
---|
139 |
|
---|
140 | dec->pulsepos[3] = (t >> 4) & 0x7FFFFFF;
|
---|
141 |
|
---|
142 | dec->pulseoff[3] = (t >> 0) & 0xF;
|
---|
143 |
|
---|
144 | dec->offset1[0] |= ((t >> 31) & 1) << 3;
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 | static void truespeech_correlate_filter(TSContext *dec)
|
---|
149 | {
|
---|
150 | int16_t tmp[8];
|
---|
151 | int i, j;
|
---|
152 |
|
---|
153 | for(i = 0; i < 8; i++){
|
---|
154 | if(i > 0){
|
---|
155 | memcpy(tmp, dec->cvector, i * 2);
|
---|
156 | for(j = 0; j < i; j++)
|
---|
157 | dec->cvector[j] = ((tmp[i - j - 1] * dec->vector[i]) +
|
---|
158 | (dec->cvector[j] << 15) + 0x4000) >> 15;
|
---|
159 | }
|
---|
160 | dec->cvector[i] = (8 - dec->vector[i]) >> 3;
|
---|
161 | }
|
---|
162 | for(i = 0; i < 8; i++)
|
---|
163 | dec->cvector[i] = (dec->cvector[i] * ts_230[i]) >> 15;
|
---|
164 |
|
---|
165 | dec->filtval = dec->vector[0];
|
---|
166 | }
|
---|
167 |
|
---|
168 | static void truespeech_filters_merge(TSContext *dec)
|
---|
169 | {
|
---|
170 | int i;
|
---|
171 |
|
---|
172 | if(!dec->flag){
|
---|
173 | for(i = 0; i < 8; i++){
|
---|
174 | dec->filters[i + 0] = dec->prevfilt[i];
|
---|
175 | dec->filters[i + 8] = dec->prevfilt[i];
|
---|
176 | }
|
---|
177 | }else{
|
---|
178 | for(i = 0; i < 8; i++){
|
---|
179 | dec->filters[i + 0]=(dec->cvector[i] * 21846 + dec->prevfilt[i] * 10923 + 16384) >> 15;
|
---|
180 | dec->filters[i + 8]=(dec->cvector[i] * 10923 + dec->prevfilt[i] * 21846 + 16384) >> 15;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | for(i = 0; i < 8; i++){
|
---|
184 | dec->filters[i + 16] = dec->cvector[i];
|
---|
185 | dec->filters[i + 24] = dec->cvector[i];
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | static void truespeech_apply_twopoint_filter(TSContext *dec, int quart)
|
---|
190 | {
|
---|
191 | int16_t tmp[146 + 60], *ptr0, *ptr1;
|
---|
192 | const int16_t *filter;
|
---|
193 | int i, t, off;
|
---|
194 |
|
---|
195 | t = dec->offset2[quart];
|
---|
196 | if(t == 127){
|
---|
197 | memset(dec->newvec, 0, 60 * 2);
|
---|
198 | return;
|
---|
199 | }
|
---|
200 | for(i = 0; i < 146; i++)
|
---|
201 | tmp[i] = dec->filtbuf[i];
|
---|
202 | off = (t / 25) + dec->offset1[quart >> 1] + 18;
|
---|
203 | ptr0 = tmp + 145 - off;
|
---|
204 | ptr1 = tmp + 146;
|
---|
205 | filter = (const int16_t*)ts_240 + (t % 25) * 2;
|
---|
206 | for(i = 0; i < 60; i++){
|
---|
207 | t = (ptr0[0] * filter[0] + ptr0[1] * filter[1] + 0x2000) >> 14;
|
---|
208 | ptr0++;
|
---|
209 | dec->newvec[i] = t;
|
---|
210 | ptr1[i] = t;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | static void truespeech_place_pulses(TSContext *dec, int16_t *out, int quart)
|
---|
215 | {
|
---|
216 | int16_t tmp[7];
|
---|
217 | int i, j, t;
|
---|
218 | const int16_t *ptr1;
|
---|
219 | int16_t *ptr2;
|
---|
220 | int coef;
|
---|
221 |
|
---|
222 | memset(out, 0, 60 * 2);
|
---|
223 | for(i = 0; i < 7; i++) {
|
---|
224 | t = dec->pulseval[quart] & 3;
|
---|
225 | dec->pulseval[quart] >>= 2;
|
---|
226 | tmp[6 - i] = ts_562[dec->pulseoff[quart] * 4 + t];
|
---|
227 | }
|
---|
228 |
|
---|
229 | coef = dec->pulsepos[quart] >> 15;
|
---|
230 | ptr1 = (const int16_t*)ts_140 + 30;
|
---|
231 | ptr2 = tmp;
|
---|
232 | for(i = 0, j = 3; (i < 30) && (j > 0); i++){
|
---|
233 | t = *ptr1++;
|
---|
234 | if(coef >= t)
|
---|
235 | coef -= t;
|
---|
236 | else{
|
---|
237 | out[i] = *ptr2++;
|
---|
238 | ptr1 += 30;
|
---|
239 | j--;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | coef = dec->pulsepos[quart] & 0x7FFF;
|
---|
243 | ptr1 = (const int16_t*)ts_140;
|
---|
244 | for(i = 30, j = 4; (i < 60) && (j > 0); i++){
|
---|
245 | t = *ptr1++;
|
---|
246 | if(coef >= t)
|
---|
247 | coef -= t;
|
---|
248 | else{
|
---|
249 | out[i] = *ptr2++;
|
---|
250 | ptr1 += 30;
|
---|
251 | j--;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | }
|
---|
256 |
|
---|
257 | static void truespeech_update_filters(TSContext *dec, int16_t *out, int quart)
|
---|
258 | {
|
---|
259 | int i;
|
---|
260 |
|
---|
261 | for(i = 0; i < 86; i++)
|
---|
262 | dec->filtbuf[i] = dec->filtbuf[i + 60];
|
---|
263 | for(i = 0; i < 60; i++){
|
---|
264 | dec->filtbuf[i + 86] = out[i] + dec->newvec[i] - (dec->newvec[i] >> 3);
|
---|
265 | out[i] += dec->newvec[i];
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | static void truespeech_synth(TSContext *dec, int16_t *out, int quart)
|
---|
270 | {
|
---|
271 | int i,k;
|
---|
272 | int t[8];
|
---|
273 | int16_t *ptr0, *ptr1;
|
---|
274 |
|
---|
275 | ptr0 = dec->tmp1;
|
---|
276 | ptr1 = dec->filters + quart * 8;
|
---|
277 | for(i = 0; i < 60; i++){
|
---|
278 | int sum = 0;
|
---|
279 | for(k = 0; k < 8; k++)
|
---|
280 | sum += ptr0[k] * ptr1[k];
|
---|
281 | sum = (sum + (out[i] << 12) + 0x800) >> 12;
|
---|
282 | out[i] = clip(sum, -0x7FFE, 0x7FFE);
|
---|
283 | for(k = 7; k > 0; k--)
|
---|
284 | ptr0[k] = ptr0[k - 1];
|
---|
285 | ptr0[0] = out[i];
|
---|
286 | }
|
---|
287 |
|
---|
288 | for(i = 0; i < 8; i++)
|
---|
289 | t[i] = (ts_5E2[i] * ptr1[i]) >> 15;
|
---|
290 |
|
---|
291 | ptr0 = dec->tmp2;
|
---|
292 | for(i = 0; i < 60; i++){
|
---|
293 | int sum = 0;
|
---|
294 | for(k = 0; k < 8; k++)
|
---|
295 | sum += ptr0[k] * t[k];
|
---|
296 | for(k = 7; k > 0; k--)
|
---|
297 | ptr0[k] = ptr0[k - 1];
|
---|
298 | ptr0[0] = out[i];
|
---|
299 | out[i] = ((out[i] << 12) - sum) >> 12;
|
---|
300 | }
|
---|
301 |
|
---|
302 | for(i = 0; i < 8; i++)
|
---|
303 | t[i] = (ts_5F2[i] * ptr1[i]) >> 15;
|
---|
304 |
|
---|
305 | ptr0 = dec->tmp3;
|
---|
306 | for(i = 0; i < 60; i++){
|
---|
307 | int sum = out[i] << 12;
|
---|
308 | for(k = 0; k < 8; k++)
|
---|
309 | sum += ptr0[k] * t[k];
|
---|
310 | for(k = 7; k > 0; k--)
|
---|
311 | ptr0[k] = ptr0[k - 1];
|
---|
312 | ptr0[0] = clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE);
|
---|
313 |
|
---|
314 | sum = ((ptr0[1] * (dec->filtval - (dec->filtval >> 2))) >> 4) + sum;
|
---|
315 | sum = sum - (sum >> 3);
|
---|
316 | out[i] = clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE);
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | static void truespeech_save_prevvec(TSContext *c)
|
---|
321 | {
|
---|
322 | int i;
|
---|
323 |
|
---|
324 | for(i = 0; i < 8; i++)
|
---|
325 | c->prevfilt[i] = c->cvector[i];
|
---|
326 | }
|
---|
327 |
|
---|
328 | static int truespeech_decode_frame(AVCodecContext *avctx,
|
---|
329 | void *data, int *data_size,
|
---|
330 | uint8_t *buf, int buf_size)
|
---|
331 | {
|
---|
332 | TSContext *c = avctx->priv_data;
|
---|
333 |
|
---|
334 | int i;
|
---|
335 | short *samples = data;
|
---|
336 | int consumed = 0;
|
---|
337 | int16_t out_buf[240];
|
---|
338 |
|
---|
339 | if (!buf_size)
|
---|
340 | return 0;
|
---|
341 |
|
---|
342 | while (consumed < buf_size) {
|
---|
343 | truespeech_read_frame(c, buf + consumed);
|
---|
344 | consumed += 32;
|
---|
345 |
|
---|
346 | truespeech_correlate_filter(c);
|
---|
347 | truespeech_filters_merge(c);
|
---|
348 |
|
---|
349 | memset(out_buf, 0, 240 * 2);
|
---|
350 | for(i = 0; i < 4; i++) {
|
---|
351 | truespeech_apply_twopoint_filter(c, i);
|
---|
352 | truespeech_place_pulses(c, out_buf + i * 60, i);
|
---|
353 | truespeech_update_filters(c, out_buf + i * 60, i);
|
---|
354 | truespeech_synth(c, out_buf + i * 60, i);
|
---|
355 | }
|
---|
356 |
|
---|
357 | truespeech_save_prevvec(c);
|
---|
358 |
|
---|
359 | /* finally output decoded frame */
|
---|
360 | for(i = 0; i < 240; i++)
|
---|
361 | *samples++ = out_buf[i];
|
---|
362 |
|
---|
363 | }
|
---|
364 |
|
---|
365 | *data_size = consumed * 15;
|
---|
366 |
|
---|
367 | return buf_size;
|
---|
368 | }
|
---|
369 |
|
---|
370 | AVCodec truespeech_decoder = {
|
---|
371 | "truespeech",
|
---|
372 | CODEC_TYPE_AUDIO,
|
---|
373 | CODEC_ID_TRUESPEECH,
|
---|
374 | sizeof(TSContext),
|
---|
375 | truespeech_decode_init,
|
---|
376 | NULL,
|
---|
377 | NULL,
|
---|
378 | truespeech_decode_frame,
|
---|
379 | };
|
---|