Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/g722/g722_dec.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 /*
20 * Based on implementation found in Carnegie Mellon Speech Group Software
21 * depository (ftp://ftp.cs.cmu.edu/project/fgdata/index.html). No copyright
22 * was claimed in the original source codes.
23 */
24 #include <pjmedia/errno.h>
25 #include <pj/assert.h>
26 #include <pj/pool.h>
27
28 #include "g722_dec.h"
29
30 #if defined(PJMEDIA_HAS_G722_CODEC) && (PJMEDIA_HAS_G722_CODEC != 0)
31
32 #define MODE 1
33
34 #define SATURATE(v, max, min) \
35 if (v>max) v = max; \
36 else if (v<min) v = min
37
38 extern const int g722_qmf_coeff[24];
39
40 static const int qm4[16] =
41 {
42 0, -20456, -12896, -8968,
43 -6288, -4240, -2584, -1200,
44 20456, 12896, 8968, 6288,
45 4240, 2584, 1200, 0
46 };
47 static const int ilb[32] = {
48 2048, 2093, 2139, 2186, 2233, 2282, 2332,
49 2383, 2435, 2489, 2543, 2599, 2656, 2714,
50 2774, 2834, 2896, 2960, 3025, 3091, 3158,
51 3228, 3298, 3371, 3444, 3520, 3597, 3676,
52 3756, 3838, 3922, 4008
53 };
54
55
56 static int block2l (int il, int detl)
57 {
58 int dlt ;
59 int ril, wd2 ;
60
61 /* INVQAL */
62 ril = il >> 2 ;
63 wd2 = qm4[ril] ;
64 dlt = (detl * wd2) >> 15 ;
65
66 return (dlt) ;
67 }
68
69
70 static int block3l (g722_dec_t *dec, int il)
71 {
72 int detl ;
73 int ril, il4, wd, wd1, wd2, wd3, nbpl, depl ;
74 static const int wl[8] = {
75 -60, -30, 58, 172, 334, 538, 1198, 3042
76 };
77 static const int rl42[16] = {
78 0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
79 };
80
81 /* LOGSCL */
82 ril = il >> 2 ;
83 il4 = rl42[ril] ;
84 wd = (dec->nbl * 32512) >> 15 ;
85 nbpl = wd + wl[il4] ;
86
87 if (nbpl < 0) nbpl = 0 ;
88 if (nbpl > 18432) nbpl = 18432 ;
89
90 /* SCALEL */
91 wd1 = (nbpl >> 6) & 31 ;
92 wd2 = nbpl >> 11 ;
93 if ((8 - wd2) < 0) wd3 = ilb[wd1] << (wd2 - 8) ;
94 else wd3 = ilb[wd1] >> (8 - wd2) ;
95 depl = wd3 << 2 ;
96
97 /* DELAYA */
98 dec->nbl = nbpl ;
99 /* DELAYL */
100 detl = depl ;
101
102 return (detl) ;
103 }
104
105
106 static int block4l (g722_dec_t *dec, int dl)
107 {
108 int sl = dec->slow ;
109 int i ;
110 int wd, wd1, wd2, wd3, wd4, wd5/*, wd6 */;
111
112 dec->dlt[0] = dl;
113
114 /* RECONS */
115 dec->rlt[0] = sl + dec->dlt[0] ;
116 SATURATE(dec->rlt[0], 32767, -32768);
117
118 /* PARREC */
119 dec->plt[0] = dec->dlt[0] + dec->szl ;
120 SATURATE(dec->plt[0], 32767, -32768);
121
122 /* UPPOL2 */
123 dec->sgl[0] = dec->plt[0] >> 15 ;
124 dec->sgl[1] = dec->plt[1] >> 15 ;
125 dec->sgl[2] = dec->plt[2] >> 15 ;
126
127 // orig: wd1 = dec->al[1] << 2;
128 wd1 = dec->al[1] * 4;
129 SATURATE(wd1, 32767, -32768);
130
131 if ( dec->sgl[0] == dec->sgl[1] ) wd2 = - wd1 ;
132 else wd2 = wd1 ;
133 if (wd2 > 32767) wd2 = 32767;
134 wd2 = wd2 >> 7 ;
135
136 if ( dec->sgl[0] == dec->sgl[2] ) wd3 = 128 ;
137 else wd3 = - 128 ;
138
139 wd4 = wd2 + wd3 ;
140 wd5 = (dec->al[2] * 32512) >> 15 ;
141
142 dec->apl[2] = wd4 + wd5 ;
143 SATURATE(dec->apl[2], 12288, -12288);
144
145 /* UPPOL1 */
146 dec->sgl[0] = dec->plt[0] >> 15 ;
147 dec->sgl[1] = dec->plt[1] >> 15 ;
148
149 if ( dec->sgl[0] == dec->sgl[1] ) wd1 = 192 ;
150 else wd1 = - 192 ;
151
152 wd2 = (dec->al[1] * 32640) >> 15 ;
153
154 dec->apl[1] = wd1 + wd2 ;
155 SATURATE(dec->apl[1], 32767, -32768);
156
157 wd3 = (15360 - dec->apl[2]) ;
158 SATURATE(wd3, 32767, -32768);
159 if ( dec->apl[1] > wd3) dec->apl[1] = wd3 ;
160 if ( dec->apl[1] < -wd3) dec->apl[1] = -wd3 ;
161
162 /* UPZERO */
163 if ( dec->dlt[0] == 0 ) wd1 = 0 ;
164 else wd1 = 128 ;
165
166 dec->sgl[0] = dec->dlt[0] >> 15 ;
167
168 for ( i = 1; i < 7; i++ ) {
169 dec->sgl[i] = dec->dlt[i] >> 15 ;
170 if ( dec->sgl[i] == dec->sgl[0] ) wd2 = wd1 ;
171 else wd2 = - wd1 ;
172 wd3 = (dec->bl[i] * 32640) >> 15 ;
173 dec->bpl[i] = wd2 + wd3 ;
174 SATURATE(dec->bpl[i], 32767, -32768);
175 }
176
177 /* DELAYA */
178 for ( i = 6; i > 0; i-- ) {
179 dec->dlt[i] = dec->dlt[i-1] ;
180 dec->bl[i] = dec->bpl[i] ;
181 }
182
183 for ( i = 2; i > 0; i-- ) {
184 dec->rlt[i] = dec->rlt[i-1] ;
185 dec->plt[i] = dec->plt[i-1] ;
186 dec->al[i] = dec->apl[i] ;
187 }
188
189 /* FILTEP */
190 // orig: wd1 = dec->rlt[1] << 1;
191 wd1 = dec->rlt[1] + dec->rlt[1];
192 SATURATE(wd1, 32767, -32768);
193 wd1 = ( dec->al[1] * wd1 ) >> 15 ;
194
195 // orig: wd2 = dec->rlt[2] << 1;
196 wd2 = dec->rlt[2] + dec->rlt[2];
197 SATURATE(wd2, 32767, -32768);
198 wd2 = ( dec->al[2] * wd2 ) >> 15 ;
199
200 dec->spl = wd1 + wd2 ;
201 SATURATE(dec->spl, 32767, -32768);
202
203 /* FILTEZ */
204 dec->szl = 0 ;
205 for (i=6; i>0; i--) {
206 // orig: wd = dec->dlt[i] << 1;
207 wd = dec->dlt[i] + dec->dlt[i];
208 SATURATE(wd, 32767, -32768);
209 dec->szl += (dec->bl[i] * wd) >> 15 ;
210 SATURATE(dec->szl, 32767, -32768);
211 }
212
213 /* PREDIC */
214 sl = dec->spl + dec->szl ;
215 SATURATE(sl, 32767, -32768);
216
217 return (sl) ;
218 }
219
220 static int block5l (int ilr, int sl, int detl, int mode)
221 {
222 int yl ;
223 int ril, dl, wd2 = 0;
224 static const int qm5[32] = {
225 -280, -280, -23352, -17560,
226 -14120, -11664, -9752, -8184,
227 -6864, -5712, -4696, -3784,
228 -2960, -2208, -1520, -880,
229 23352, 17560, 14120, 11664,
230 9752, 8184, 6864, 5712,
231 4696, 3784, 2960, 2208,
232 1520, 880, 280, -280
233 };
234 static const int qm6[64] = {
235 -136, -136, -136, -136,
236 -24808, -21904, -19008, -16704,
237 -14984, -13512, -12280, -11192,
238 -10232, -9360, -8576, -7856,
239 -7192, -6576, -6000, -5456,
240 -4944, -4464, -4008, -3576,
241 -3168, -2776, -2400, -2032,
242 -1688, -1360, -1040, -728,
243 24808, 21904, 19008, 16704,
244 14984, 13512, 12280, 11192,
245 10232, 9360, 8576, 7856,
246 7192, 6576, 6000, 5456,
247 4944, 4464, 4008, 3576,
248 3168, 2776, 2400, 2032,
249 1688, 1360, 1040, 728,
250 432, 136, -432, -136
251 };
252
253 /* INVQBL */
254 if (mode == 1) {
255 ril = ilr ;
256 wd2 = qm6[ril] ;
257 }
258
259 if (mode == 2) {
260 ril = ilr >> 1 ;
261 wd2 = qm5[ril] ;
262 }
263
264 if (mode == 3) {
265 ril = ilr >> 2 ;
266 wd2 = qm4[ril] ;
267 }
268
269 dl = (detl * wd2 ) >> 15 ;
270
271 /* RECONS */
272 yl = sl + dl ;
273 SATURATE(yl, 32767, -32768);
274
275 return (yl) ;
276 }
277
278 static int block6l (int yl)
279 {
280 int rl ;
281
282 rl = yl ;
283 SATURATE(rl, 16383, -16384);
284
285 return (rl) ;
286 }
287
288 static int block2h (int ih, int deth)
289 {
290 int dh ;
291 int wd2 ;
292 static const int qm2[4] = {-7408, -1616, 7408, 1616} ;
293
294 /* INVQAH */
295 wd2 = qm2[ih] ;
296 dh = (deth * wd2) >> 15 ;
297
298 return (dh) ;
299 }
300
301 static int block3h (g722_dec_t *dec, int ih)
302 {
303 int deth ;
304 int ih2, wd, wd1, wd2, wd3, nbph, deph ;
305 static const int wh[3] = {0, -214, 798} ;
306 static const int rh2[4] = {2, 1, 2, 1} ;
307
308 /* LOGSCH */
309 ih2 = rh2[ih] ;
310 wd = (dec->nbh * 32512) >> 15 ;
311 nbph = wd + wh[ih2] ;
312
313 if (nbph < 0) nbph = 0 ;
314 if (nbph > 22528) nbph = 22528 ;
315
316
317 /* SCALEH */
318 wd1 = (nbph >> 6) & 31 ;
319 wd2 = nbph >> 11 ;
320 if ((10 - wd2) < 0) wd3 = ilb[wd1] << (wd2 - 10) ;
321 else wd3 = ilb[wd1] >> (10 - wd2) ;
322 deph = wd3 << 2 ;
323
324 /* DELAYA */
325 dec->nbh = nbph ;
326
327 /* DELAYH */
328 deth = deph ;
329
330 return (deth) ;
331 }
332
333 static int block4h (g722_dec_t *dec, int d)
334 {
335 int sh = dec->shigh;
336 int i ;
337 int wd, wd1, wd2, wd3, wd4, wd5/*, wd6 */;
338
339 dec->dh[0] = d;
340
341 /* RECONS */
342 dec->rh[0] = sh + dec->dh[0] ;
343 SATURATE(dec->rh[0], 32767, -32768);
344
345 /* PARREC */
346 dec->ph[0] = dec->dh[0] + dec->szh ;
347 SATURATE(dec->ph[0], 32767, -32768);
348
349 /* UPPOL2 */
350 dec->sgh[0] = dec->ph[0] >> 15 ;
351 dec->sgh[1] = dec->ph[1] >> 15 ;
352 dec->sgh[2] = dec->ph[2] >> 15 ;
353
354 // orig: wd1 = dec->ah[1] << 2;
355 wd1 = dec->ah[1] * 4;
356 SATURATE(wd1, 32767, -32768);
357
358 if ( dec->sgh[0] == dec->sgh[1] ) wd2 = - wd1 ;
359 else wd2 = wd1 ;
360 if (wd2 > 32767) wd2 = 32767;
361
362 wd2 = wd2 >> 7 ;
363
364 if ( dec->sgh[0] == dec->sgh[2] ) wd3 = 128 ;
365 else wd3 = - 128 ;
366
367 wd4 = wd2 + wd3 ;
368 wd5 = (dec->ah[2] * 32512) >> 15 ;
369
370 dec->aph[2] = wd4 + wd5 ;
371 SATURATE(dec->aph[2], 12288, -12288);
372
373 /* UPPOL1 */
374 dec->sgh[0] = dec->ph[0] >> 15 ;
375 dec->sgh[1] = dec->ph[1] >> 15 ;
376
377 if ( dec->sgh[0] == dec->sgh[1] ) wd1 = 192 ;
378 else wd1 = - 192 ;
379
380 wd2 = (dec->ah[1] * 32640) >> 15 ;
381
382 dec->aph[1] = wd1 + wd2 ;
383 SATURATE(dec->aph[1], 32767, -32768);
384 //dec->aph[2]?
385 //if (aph[2] > 32767) aph[2] = 32767;
386 //if (aph[2] < -32768) aph[2] = -32768;
387
388 wd3 = (15360 - dec->aph[2]) ;
389 SATURATE(wd3, 32767, -32768);
390 if ( dec->aph[1] > wd3) dec->aph[1] = wd3 ;
391 if ( dec->aph[1] < -wd3) dec->aph[1] = -wd3 ;
392
393 /* UPZERO */
394 if ( dec->dh[0] == 0 ) wd1 = 0 ;
395 if ( dec->dh[0] != 0 ) wd1 = 128 ;
396
397 dec->sgh[0] = dec->dh[0] >> 15 ;
398
399 for ( i = 1; i < 7; i++ ) {
400 dec->sgh[i] = dec->dh[i] >> 15 ;
401 if ( dec->sgh[i] == dec->sgh[0] ) wd2 = wd1 ;
402 else wd2 = - wd1 ;
403 wd3 = (dec->bh[i] * 32640) >> 15 ;
404 dec->bph[i] = wd2 + wd3 ;
405 }
406
407 /* DELAYA */
408 for ( i = 6; i > 0; i-- ) {
409 dec->dh[i] = dec->dh[i-1] ;
410 dec->bh[i] = dec->bph[i] ;
411 }
412
413 for ( i = 2; i > 0; i-- ) {
414 dec->rh[i] = dec->rh[i-1] ;
415 dec->ph[i] = dec->ph[i-1] ;
416 dec->ah[i] = dec->aph[i] ;
417 }
418
419 /* FILTEP */
420 // orig: wd1 = dec->rh[1] << 1 ;
421 wd1 = dec->rh[1] + dec->rh[1] ;
422 SATURATE(wd1, 32767, -32768);
423 wd1 = ( dec->ah[1] * wd1 ) >> 15 ;
424
425 // orig: wd2 = dec->rh[2] << 1;
426 wd2 = dec->rh[2] + dec->rh[2];
427 SATURATE(wd2, 32767, -32768);
428 wd2 = ( dec->ah[2] * wd2 ) >> 15 ;
429
430 dec->sph = wd1 + wd2 ;
431 SATURATE(dec->sph, 32767, -32768);
432
433 /* FILTEZ */
434 dec->szh = 0 ;
435 for (i=6; i>0; i--) {
436 // orig: wd = dec->dh[i] << 1;
437 wd = dec->dh[i] + dec->dh[i];
438 SATURATE(wd, 32767, -32768);
439 dec->szh += (dec->bh[i] * wd) >> 15 ;
440 SATURATE(dec->szh, 32767, -32768);
441 }
442
443 /* PREDIC */
444 sh = dec->sph + dec->szh ;
445 SATURATE(sh, 32767, -32768);
446
447 return (sh) ;
448 }
449
450 static int block5h (int dh, int sh)
451 {
452 int rh ;
453
454 rh = dh + sh;
455 SATURATE(rh, 16383, -16384);
456
457 return (rh) ;
458 }
459
460 static void rx_qmf(g722_dec_t *dec, int rl, int rh, int *xout1, int *xout2)
461 {
462 int i;
463
464 pj_memmove(&dec->xd[1], dec->xd, 11*sizeof(dec->xd[0]));
465 pj_memmove(&dec->xs[1], dec->xs, 11*sizeof(dec->xs[0]));
466
467 /* RECA */
468 dec->xd[0] = rl - rh ;
469 if (dec->xd[0] > 16383) dec->xd[0] = 16383;
470 else if (dec->xd[0] < -16384) dec->xd[0] = -16384;
471
472 /* RECB */
473 dec->xs[0] = rl + rh ;
474 if (dec->xs[0] > 16383) dec->xs[0] = 16383;
475 else if (dec->xs[0] < -16384) dec->xs[0] = -16384;
476
477 /* ACCUMC */
478 *xout1 = 0;
479 for (i=0; i<12; ++i) *xout1 += dec->xd[i] * g722_qmf_coeff[2*i];
480 *xout1 = *xout1 >> 12 ;
481 if (*xout1 > 16383) *xout1 = 16383 ;
482 else if (*xout1 < -16384) *xout1 = -16384 ;
483
484 /* ACCUMD */
485 *xout2 = 0;
486 for (i=0; i<12; ++i) *xout2 += dec->xs[i] * g722_qmf_coeff[2*i+1];
487 *xout2 = *xout2 >> 12 ;
488 if (*xout2 > 16383) *xout2 = 16383 ;
489 else if (*xout2 < -16384) *xout2 = -16384 ;
490 }
491
492
493 PJ_DEF(pj_status_t) g722_dec_init(g722_dec_t *dec)
494 {
495 PJ_ASSERT_RETURN(dec, PJ_EINVAL);
496
497 pj_bzero(dec, sizeof(g722_dec_t));
498
499 dec->detlow = 32;
500 dec->dethigh = 8;
501
502 return PJ_SUCCESS;
503 }
504
505 PJ_DEF(pj_status_t) g722_dec_decode( g722_dec_t *dec,
506 void *in,
507 pj_size_t in_size,
508 pj_int16_t out[],
509 pj_size_t *nsamples)
510 {
511 unsigned i;
512 int ilowr, ylow, rlow, dlowt;
513 int ihigh, rhigh, dhigh;
514 int pcm1, pcm2;
515 pj_uint8_t *in_ = (pj_uint8_t*) in;
516
517 PJ_ASSERT_RETURN(dec && in && in_size && out && nsamples, PJ_EINVAL);
518 PJ_ASSERT_RETURN(*nsamples >= (in_size << 1), PJ_ETOOSMALL);
519
520 for(i = 0; i < in_size; ++i) {
521 ilowr = in_[i] & 63;
522 ihigh = (in_[i] >> 6) & 3;
523
524 /* low band decoder */
525 ylow = block5l (ilowr, dec->slow, dec->detlow, MODE) ;
526 rlow = block6l (ylow) ;
527 dlowt = block2l (ilowr, dec->detlow) ;
528 dec->detlow = block3l (dec, ilowr) ;
529 dec->slow = block4l (dec, dlowt) ;
530 /* rlow <= output low band pcm */
531
532 /* high band decoder */
533 dhigh = block2h (ihigh, dec->dethigh) ;
534 rhigh = block5h (dhigh, dec->shigh) ;
535 dec->dethigh = block3h (dec, ihigh) ;
536 dec->shigh = block4h (dec, dhigh) ;
537 /* rhigh <= output high band pcm */
538
539 rx_qmf(dec, rlow, rhigh, &pcm1, &pcm2);
540 out[i*2] = (pj_int16_t)pcm1;
541 out[i*2+1] = (pj_int16_t)pcm2;
542 }
543
544 *nsamples = in_size << 1;
545
546 return PJ_SUCCESS;
547 }
548
549 PJ_DEF(pj_status_t) g722_dec_deinit(g722_dec_t *dec)
550 {
551 pj_bzero(dec, sizeof(g722_dec_t));
552
553 return PJ_SUCCESS;
554 }
555
556 #endif