comparison intercom/g726/g726.c @ 2:13be24d74cd2

import intercom-0.4.1
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 09:57:52 +0200
parents
children
comparison
equal deleted inserted replaced
1:9cadc470e3da 2:13be24d74cd2
1 /* v2.0 24.Jan.2000
2 =============================================================================
3
4 U U GGG SSSS TTTTT
5 U U G S T
6 U U G GG SSSS T
7 U U G G S T
8 UUU GG SSS T
9
10 ========================================
11 ITU-T - USER'S GROUP ON SOFTWARE TOOLS
12 ========================================
13
14
15 =============================================================
16 COPYRIGHT NOTE: This source code, and all of its derivations,
17 is subject to the "ITU-T General Public License". Please have
18 it read in the distribution disk, or in the ITU-T
19 Recommendation G.191 on "SOFTWARE TOOLS FOR SPEECH AND AUDIO
20 CODING STANDARDS".
21 =============================================================
22
23
24 MODULE: G726.C ADPCM AT 40, 32, 24, AND 16 KBIT/S MODULE
25
26 ORIGINAL BY:
27 Jose' Sindi Yamamoto (Fortran version of the G.721)
28 <tdsindi@venus.cpqd.ansp.br>
29 Simao Ferraz de Campos Neto (C translation, adaptation&testing of the G.721)
30 <simao@venus.cpqd.ansp.br>
31 Fernando Tofolli Queiroz (Extension of the G.721 to the other rates=>G.726)
32 <tofolli@venus.cpqd.ansp.br>
33 Simao Ferraz de Campos Neto (Adaptation and testing of the G.726)
34 <simao@venus.cpqd.ansp.br>
35
36 HISTORY:
37 28.Feb.1994 v1.0c Version 1.0 in C, by translating Fortran to C (f2c)
38 24.Jan.2000 v2.0 Corrected bug G726_compress() that caused incorrect
39 processing of test vector ri40fa. Corrected code
40 provided by Jayesh Patel <jayesh@dspse.com>.
41 Verified by <simao.campos@labs.comsat.com>
42
43 FUNCTIONS:
44 Public:
45 G726_encode ..... G726 encoder function;
46
47 G726_decode ..... G726 decoder function;
48
49 Private:
50 G726_accum ...... addition of predictor outputs to form the partial
51 signal estimate (from the sixth order predictor) and
52 the signal estimate.
53
54 G726_adda ....... addition of scale factor to logarithmic version of
55 quantized difference signal.
56
57 G726_addb ....... addition of quantized difference signal and signal
58 estimate to form reconstructed signal.
59
60 G726_addc....... obtain sign of addition of quantized difference signal
61 and partial signal estimate.
62
63 G726_antilog .... convert quantized difference signal from the
64 logarithmic to the linear domain.
65
66 G726_compress ... convert from uniform pcm to either a-law or u-law pcm.
67
68 G726_delaya ..... memory block.
69
70 G726_delayb ..... memory block.
71
72 G726_delayc ..... memory block.
73
74 G726_delayd ..... memory block.
75
76 G726_expand ..... convert either a-law (law=1) or u-law (law=0) to
77 uniform pcm.
78
79 G726_filta ...... update of short term average of f(i).
80
81 G726_filtb ...... update of long term average of f(i).
82
83 G726_filtc ...... low pass filter of speed control parameter.
84
85 G726_filtd ...... update of fast quantizer scale factor.
86
87 G726_filte ...... update of slow quantizer scale factor.
88
89 G726_floata ..... convert 15-bit signed magnitude to floating point.
90
91 G726_floatb ..... convert 16-bit two's complement to floating point.
92
93 G726_fmult ...... multiply predictor coefficients with corresponding
94 quantized difference signal or reconstructed signal.
95 Multiplication is done in floating point format.
96
97 G726_functf ..... map quantizer output into the f(i) function.
98
99 G726_functw ..... map quantizer output into logarithmic version of scale
100 factor multiplier.
101
102 G726_lima ....... limit speed control parameter.
103
104 G726_limb ....... limit quantizer scale factor.
105
106 G726_limc ....... limits on a2 coefficient of second order predictor.
107
108 G726_limd ....... limits on a1 coefficient of second order predictor.
109
110 G726_log ........ convert difference signal from linear to the
111 logarithmic domain.
112
113 G726_mix ........ form linear combination of fast and slow quantizer
114 scale factors.
115
116 G726_quan ....... quantize difference signal in logarithmic domain.
117
118 G726_reconst .... reconstruction of quantized difference signal in the
119 logarithmic domain.
120
121 G726_subta ...... compute difference signal by subtracting signal
122 estimate from input signal (or quantized reconstructed
123 signal in decoder).
124
125 G726_subtb ...... scale logarithmic version of difference signal by
126 subtracting scale factor.
127
128 G726_subtc ...... compute magnitude of the difference of short and
129 long-term function of quantizer output sequence and
130 then perform threshold comparison for quantizing speed
131 control parameter.
132
133 G726_sync ....... re-encode output pcm sample in decoder for synchronous
134 tandem coding.
135
136 G726_tone ....... partial band signal detection.
137
138 G726_trans ...... transition detector.
139
140 G726_triga ...... speed control trigger block.
141
142 G726_trigb ...... predictor trigger block.
143
144 G726_upa1 ....... update a1 coefficient of second order predictor.
145
146 G726_upa2 ....... update a2 coefficient of second order predictor.
147
148 G726_upb ........ update for coefficients of sixth order predictor.
149
150 G726_xor ........ one bit "exclusive or" of sign of difference signal
151 and sign of delayed difference signal.
152
153 =============================================================================
154 */
155
156 /*
157 * .................. INCLUDES ..................
158 */
159 #include "g726.h"
160
161
162 /*
163 * .................. FUNCTIONS ..................
164 */
165
166 /*
167 ----------------------------------------------------------------------------
168
169 void G726_encode (short *inp_buf, short *out_buf, long smpno,
170 ~~~~~~~~~~~~~~~~ char *law, short rate, short r, G726_state *state);
171
172 Description:
173 ~~~~~~~~~~~~
174
175 Simulation of the ITU-T G.726 ADPCM encoder. Takes the A or mu
176 law input array of shorts `inp_buf' (16 bit, right- justified,
177 without sign extension) of length `smpno', and saves the
178 encoded samples in the array of shorts `out_buf', with the
179 same number of samples and right-justified.
180
181 The state variables are saved in the structure `state', and the
182 reset can be stablished by making r equal to 1. The law is A if
183 `law'=='1', and mu law if `law'=='0'.
184
185
186 Return value:
187 ~~~~~~~~~~~~~
188 None.
189
190 Prototype: in file g726.h
191 ~~~~~~~~~~
192
193 History:
194 ~~~~~~~~
195 31.Jan.91 v1.0f Version 1.0 in Fortran
196 <tdsindi@venus.cpqd.ansp.br>
197 05.Feb.92 v1.0c Version 1.0 in C, by translating Fortran to C (f2c)
198 <tdsimao@venus.cpqd.ansp.br>
199
200 ----------------------------------------------------------------------------
201 */
202 void G726_encode(inp_buf, out_buf, smpno, law, rate, r, state)
203 unsigned char *inp_buf;
204 short *out_buf;
205 long smpno;
206 char *law;
207 short r;
208 short rate;
209 G726_state *state;
210 {
211 short s;
212 short d, i;
213 short y;
214 short sigpk;
215 short sr, tr;
216 short yu;
217 short al, fi, dl, ap, dq, ds, se, ax, td, sl, wi;
218 short u1, u2, u3, u4, u5, u6;
219 short a1, a2, b1, b2, b3, b4, b5, b6;
220 short dqln;
221 short a1p, a2p, a1t, a2t, b1p, b2p, b3p, b4p, b5p, b6p, dq6, pk2,
222 sr2, wa1, wa2, wb1, wb2, wb3, wb4, wb5, wb6;
223 short dml, dln, app, dql, dms;
224 short dqs, tdp;
225 short sez;
226 short yut;
227 long yl;
228
229 long j;
230
231 /* Invert even bits if A law */
232 if (*law == '1') {
233 for (j = 0; j < smpno; j++)
234 inp_buf[j] ^= 85;
235 }
236
237 /* Process all desired samples in inp_buf to out_buf; The comments about
238 * general blocks are given as in G.726, and refer to: 4.1.1 Input PCM
239 * format conversion and difference signal computation 4.1.2 Adaptive
240 * quantizer 4.1.3 Inverse adaptive quantizer 4.1.4 Quantizer scale factor
241 * adaptation 4.1.5 Adaptation speed control 4.1.6 Adaptive predictor and
242 * reconstructed signal calculator 4.1.7 Tone and transition detector 4.1.8
243 * (Only in the decoder) */
244 for (j = 0; j < smpno; j++, r = 0) {
245 s = inp_buf[j];
246
247 /* Process `known-state' part of 4.2.6 */
248 G726_delayd(&r, &state->sr1, &sr2);
249 G726_delayd(&r, &state->sr0, &state->sr1);
250 G726_delaya(&r, &state->a2r, &a2);
251 G726_delaya(&r, &state->a1r, &a1);
252 G726_fmult(&a2, &sr2, &wa2);
253 G726_fmult(&a1, &state->sr1, &wa1);
254
255 G726_delayd(&r, &state->dq5, &dq6);
256 G726_delayd(&r, &state->dq4, &state->dq5);
257 G726_delayd(&r, &state->dq3, &state->dq4);
258 G726_delayd(&r, &state->dq2, &state->dq3);
259 G726_delayd(&r, &state->dq1, &state->dq2);
260 G726_delayd(&r, &state->dq0, &state->dq1);
261
262 G726_delaya(&r, &state->b1r, &b1);
263 G726_delaya(&r, &state->b2r, &b2);
264 G726_delaya(&r, &state->b3r, &b3);
265 G726_delaya(&r, &state->b4r, &b4);
266 G726_delaya(&r, &state->b5r, &b5);
267 G726_delaya(&r, &state->b6r, &b6);
268
269 G726_fmult(&b1, &state->dq1, &wb1);
270 G726_fmult(&b2, &state->dq2, &wb2);
271 G726_fmult(&b3, &state->dq3, &wb3);
272 G726_fmult(&b4, &state->dq4, &wb4);
273 G726_fmult(&b5, &state->dq5, &wb5);
274 G726_fmult(&b6, &dq6, &wb6);
275
276 G726_accum(&wa1, &wa2, &wb1, &wb2, &wb3, &wb4, &wb5, &wb6, &se,
277 &sez);
278
279 /* Process 4.2.1 */
280 G726_expand(&s, law, &sl);
281 G726_subta(&sl, &se, &d);
282
283 /* Process delays and `know-state' part of 4.2.5 */
284 G726_delaya(&r, &state->dmsp, &dms);
285 G726_delaya(&r, &state->dmlp, &dml);
286 G726_delaya(&r, &state->apr, &ap);
287 G726_lima(&ap, &al);
288
289 /* Process `know-state' parts of 4.2.4 */
290 G726_delayb(&r, &state->yup, &yu);
291 G726_delayc(&r, &state->ylp, &yl);
292 G726_mix(&al, &yu, &yl, &y);
293
294 /* Process 4.2.2 */
295 G726_log(&d, &dl, &ds);
296 G726_subtb(&dl, &y, &dln);
297 G726_quan(rate, &dln, &ds, &i);
298
299 /* Save ADPCM quantized sample into output buffer */
300 out_buf[j] = i;
301
302 /* Process 4.2.3 */
303 G726_reconst(rate, &i, &dqln, &dqs);
304 G726_adda(&dqln, &y, &dql);
305 G726_antilog(&dql, &dqs, &dq);
306
307 /* Part of 4.2.5 */
308 G726_functf(rate, &i, &fi);
309 G726_filta(&fi, &dms, &state->dmsp);
310 G726_filtb(&fi, &dml, &state->dmlp);
311
312 /* Remaining part of 4.2.4 */
313 G726_functw(rate, &i, &wi);
314 G726_filtd(&wi, &y, &yut);
315 G726_limb(&yut, &state->yup);
316 G726_filte(&state->yup, &yl, &state->ylp);
317
318 /* Process `known-state' part of 4.2.7 */
319 G726_delaya(&r, &state->tdr, &td);
320 G726_trans(&td, &yl, &dq, &tr);
321
322 /* More `known-state' parts of 4.2.6: update of `pk's */
323 G726_delaya(&r, &state->pk1, &pk2);
324 G726_delaya(&r, &state->pk0, &state->pk1);
325 G726_addc(&dq, &sez, &state->pk0, &sigpk);
326
327 /* 4.2.6: find sr0 */
328 G726_addb(&dq, &se, &sr);
329 G726_floatb(&sr, &state->sr0);
330
331 /* 4.2.6: find dq0 */
332 G726_floata(&dq, &state->dq0);
333
334 /* 4.2.6: prepar a2(r) */
335 G726_upa2(&state->pk0, &state->pk1, &pk2, &a2, &a1, &sigpk, &a2t);
336 G726_limc(&a2t, &a2p);
337 G726_trigb(&tr, &a2p, &state->a2r);
338
339 /* 4.2.6: prepar a1(r) */
340 G726_upa1(&state->pk0, &state->pk1, &a1, &sigpk, &a1t);
341 G726_limd(&a1t, &a2p, &a1p);
342 G726_trigb(&tr, &a1p, &state->a1r);
343
344 /* Remaining of 4.2.7 */
345 G726_tone(&a2p, &tdp);
346 G726_trigb(&tr, &tdp, &state->tdr);
347
348 /* Remaining of 4.2.5 */
349 G726_subtc(&state->dmsp, &state->dmlp, &tdp, &y, &ax);
350 G726_filtc(&ax, &ap, &app);
351 G726_triga(&tr, &app, &state->apr);
352
353 /* Remaining of 4.2.6: update of all `b's */
354 G726_xor(&state->dq1, &dq, &u1); /* Here, b1 */
355 G726_upb(rate, &u1, &b1, &dq, &b1p);
356 G726_trigb(&tr, &b1p, &state->b1r);
357
358 G726_xor(&state->dq2, &dq, &u2); /* Here, b2 */
359 G726_upb(rate, &u2, &b2, &dq, &b2p);
360 G726_trigb(&tr, &b2p, &state->b2r);
361
362 G726_xor(&state->dq3, &dq, &u3); /* Here, b3 */
363 G726_upb(rate, &u3, &b3, &dq, &b3p);
364 G726_trigb(&tr, &b3p, &state->b3r);
365
366 G726_xor(&state->dq4, &dq, &u4); /* Here, b4 */
367 G726_upb(rate, &u4, &b4, &dq, &b4p);
368 G726_trigb(&tr, &b4p, &state->b4r);
369
370 G726_xor(&state->dq5, &dq, &u5); /* Here, b5 */
371 G726_upb(rate, &u5, &b5, &dq, &b5p);
372 G726_trigb(&tr, &b5p, &state->b5r);
373
374 G726_xor(&dq6, &dq, &u6); /* At last, b6 */
375 G726_upb(rate, &u6, &b6, &dq, &b6p);
376 G726_trigb(&tr, &b6p, &state->b6r);
377 }
378 }
379
380 /* ........................ end of G726_encode() ....................... */
381
382
383 /*
384 ----------------------------------------------------------------------------
385
386 void G726_decode (short *inp_buf, short *out_buf, long smpno,
387 ~~~~~~~~~~~~~~~~ char *law, short rate, short r, G726_state *state);
388
389 Description:
390 ~~~~~~~~~~~~
391
392 Simulation of the ITU-T G.726 ADPCM decoder. Takes the ADPCM
393 input array of shorts `inp_buf' (16 bit, right- justified,
394 without sign extension) of length `smpno', and saves the
395 decoded samples (A or mu law) in the array of shorts
396 `out_buf', with the same number of samples and
397 right-justified.
398
399 The state variables are saved in the structure `state', and the
400 reset can be stablished by making r equal to 1. The law is A if
401 `law'=='1', and mu law if `law'=='0'.
402
403
404 Return value:
405 ~~~~~~~~~~~~~
406 None.
407
408 Prototype: in file g726.h
409 ~~~~~~~~~~
410
411 History:
412 ~~~~~~~~
413 31.Jan.91 v1.0f Version 1.0 in Fortran
414 <tdsindi@venus.cpqd.ansp.br>
415 05.Feb.92 v1.0c Version 1.0 in C, by translating Fortran to C (f2c)
416 <tdsimao@venus.cpqd.ansp.br>
417
418 ----------------------------------------------------------------------------
419 */
420 void G726_decode(inp_buf, out_buf, smpno, law, rate, r, state)
421 short *inp_buf;
422 unsigned char *out_buf;
423 long smpno;
424 char *law;
425 short r;
426 short rate;
427 G726_state *state;
428 {
429 short i;
430 short y;
431 short sigpk;
432 short sr, tr;
433 short sp, dlnx, dsx, sd, slx, dlx, dx; /* these are unique to
434 * the decoder */
435 long yl;
436 short yu;
437 short al, fi, ap, dq, se, ax, td, wi;
438 short u1, u2, u3, u4, u5, u6;
439 short a1, a2, b1, b2, b3, b4, b5, b6;
440 short dqln;
441 short a1p, a2p, a1t, a2t, b1p, b2p, b3p, b4p, b5p, b6p, dq6, pk2,
442 sr2, wa1, wa2, wb1, wb2, wb3, wb4, wb5, wb6;
443 short dml, app, dql, dms;
444 short dqs, tdp;
445 short sez;
446 short yut;
447 long j;
448
449 /* Process all desired samples in inp_buf to out_buf; The comments about
450 * general blocks are given as in G.726, and refer to: 4.1.1 Input PCM
451 * format conversion and difference signal computation 4.1.2 Adaptive
452 * quantizer 4.1.3 Inverse adaptive quantizer 4.1.4 Quantizer scale factor
453 * adaptation 4.1.5 Adaptation speed control 4.1.6 Adaptive predictor and
454 * reconstructed signal calculator 4.1.7 Tone and transition detector 4.1.8
455 * Output PCM format conversion and synchronous coding adjustment */
456 for (j = 0; j < smpno; j++, r = 0) {
457 /* Process `known-state' part of 4.2.6 */
458 G726_delayd(&r, &state->sr1, &sr2);
459 G726_delayd(&r, &state->sr0, &state->sr1);
460 G726_delaya(&r, &state->a2r, &a2);
461 G726_delaya(&r, &state->a1r, &a1);
462 G726_fmult(&a2, &sr2, &wa2);
463 G726_fmult(&a1, &state->sr1, &wa1);
464
465 G726_delayd(&r, &state->dq5, &dq6);
466 G726_delayd(&r, &state->dq4, &state->dq5);
467 G726_delayd(&r, &state->dq3, &state->dq4);
468 G726_delayd(&r, &state->dq2, &state->dq3);
469 G726_delayd(&r, &state->dq1, &state->dq2);
470 G726_delayd(&r, &state->dq0, &state->dq1);
471
472 G726_delaya(&r, &state->b1r, &b1);
473 G726_delaya(&r, &state->b2r, &b2);
474 G726_delaya(&r, &state->b3r, &b3);
475 G726_delaya(&r, &state->b4r, &b4);
476 G726_delaya(&r, &state->b5r, &b5);
477 G726_delaya(&r, &state->b6r, &b6);
478
479 G726_fmult(&b1, &state->dq1, &wb1);
480 G726_fmult(&b2, &state->dq2, &wb2);
481 G726_fmult(&b3, &state->dq3, &wb3);
482 G726_fmult(&b4, &state->dq4, &wb4);
483 G726_fmult(&b5, &state->dq5, &wb5);
484 G726_fmult(&b6, &dq6, &wb6);
485
486 G726_accum(&wa1, &wa2, &wb1, &wb2, &wb3, &wb4, &wb5, &wb6, &se,
487 &sez);
488
489 /* Process delays and `know-state' part of 4.2.5 */
490 G726_delaya(&r, &state->dmsp, &dms);
491 G726_delaya(&r, &state->dmlp, &dml);
492 G726_delaya(&r, &state->apr, &ap);
493 G726_lima(&ap, &al);
494
495 /* Process `know-state' parts of 4.2.4 */
496 G726_delayb(&r, &state->yup, &yu);
497 G726_delayc(&r, &state->ylp, &yl);
498 G726_mix(&al, &yu, &yl, &y);
499
500 /* Retrieve ADPCM sample from input buffer */
501 i = inp_buf[j];
502
503 /* Process 4.2.3 */
504 G726_reconst(rate, &i, &dqln, &dqs);
505 G726_adda(&dqln, &y, &dql);
506 G726_antilog(&dql, &dqs, &dq);
507
508 /* Process `known-state' part of 4.2.7 */
509 G726_delaya(&r, &state->tdr, &td);
510 G726_trans(&td, &yl, &dq, &tr);
511
512 /* Part of 4.2.5 */
513 G726_functf(rate, &i, &fi);
514 G726_filta(&fi, &dms, &state->dmsp);
515 G726_filtb(&fi, &dml, &state->dmlp);
516
517 /* Remaining part of 4.2.4 */
518 G726_functw(rate, &i, &wi);
519 G726_filtd(&wi, &y, &yut);
520 G726_limb(&yut, &state->yup);
521 G726_filte(&state->yup, &yl, &state->ylp);
522
523 /* More `known-state' parts of 4.2.6: update of `pk's */
524 G726_delaya(&r, &state->pk1, &pk2);
525 G726_delaya(&r, &state->pk0, &state->pk1);
526 G726_addc(&dq, &sez, &state->pk0, &sigpk);
527
528 /* 4.2.6: find sr0 */
529 G726_addb(&dq, &se, &sr);
530 G726_floatb(&sr, &state->sr0);
531
532 /* 4.2.6: find dq0 */
533 G726_floata(&dq, &state->dq0);
534
535 /* Process 4.2.8 */
536 G726_compress(&sr, law, &sp);
537 G726_expand(&sp, law, &slx);
538 G726_subta(&slx, &se, &dx);
539 G726_log(&dx, &dlx, &dsx);
540 G726_subtb(&dlx, &y, &dlnx);
541 G726_sync(rate, &i, &sp, &dlnx, &dsx, law, &sd);
542
543 /* Save output PCM word in output buffer */
544 out_buf[j] = sd;
545
546 /* 4.2.6: prepar a2(r) */
547 G726_upa2(&state->pk0, &state->pk1, &pk2, &a2, &a1, &sigpk, &a2t);
548 G726_limc(&a2t, &a2p);
549 G726_trigb(&tr, &a2p, &state->a2r);
550
551 /* 4.2.6: prepar a1(r) */
552 G726_upa1(&state->pk0, &state->pk1, &a1, &sigpk, &a1t);
553 G726_limd(&a1t, &a2p, &a1p);
554 G726_trigb(&tr, &a1p, &state->a1r);
555
556 /* Remaining of 4.2.7 */
557 G726_tone(&a2p, &tdp);
558 G726_trigb(&tr, &tdp, &state->tdr);
559
560 /* Remaining of 4.2.5 */
561 G726_subtc(&state->dmsp, &state->dmlp, &tdp, &y, &ax);
562 G726_filtc(&ax, &ap, &app);
563 G726_triga(&tr, &app, &state->apr);
564
565 /* Remaining of 4.2.6: update of all `b's */
566 G726_xor(&state->dq1, &dq, &u1); /* Here, b1 */
567 G726_upb(rate, &u1, &b1, &dq, &b1p);
568 G726_trigb(&tr, &b1p, &state->b1r);
569
570 G726_xor(&state->dq2, &dq, &u2); /* Here, b2 */
571 G726_upb(rate, &u2, &b2, &dq, &b2p);
572 G726_trigb(&tr, &b2p, &state->b2r);
573
574 G726_xor(&state->dq3, &dq, &u3); /* Here, b3 */
575 G726_upb(rate, &u3, &b3, &dq, &b3p);
576 G726_trigb(&tr, &b3p, &state->b3r);
577
578 G726_xor(&state->dq4, &dq, &u4); /* Here, b4 */
579 G726_upb(rate, &u4, &b4, &dq, &b4p);
580 G726_trigb(&tr, &b4p, &state->b4r);
581
582 G726_xor(&state->dq5, &dq, &u5); /* Here, b5 */
583 G726_upb(rate, &u5, &b5, &dq, &b5p);
584 G726_trigb(&tr, &b5p, &state->b5r);
585
586 G726_xor(&dq6, &dq, &u6); /* At last, b6 */
587 G726_upb(rate, &u6, &b6, &dq, &b6p);
588 G726_trigb(&tr, &b6p, &state->b6r);
589 }
590
591 /* Invert even bits if A law */
592 if (*law == '1') {
593 for (j = 0; j < smpno; j++)
594 out_buf[j] ^= 85;
595 }
596
597 }
598
599 /* ...................... end of G726_decode() ...................... */
600
601
602 /*
603 ----------------------------------------------------------------------
604
605 void G726_expand (short *s, char *law, short *sl);
606 ~~~~~~~~~~~~~~~~
607
608 Description:
609 ~~~~~~~~~~~~
610
611 Convert either a-law (law=1) or u-law (law=0) to uniform pcm.
612
613 Inputs: s (sp in decoder), law
614 Output: sl (slx in decoder)
615
616 Return value: none.
617 ~~~~~~~~~~~~~
618
619 Prototype: in g726.h
620 ~~~~~~~~~~
621
622 History:
623 ~~~~~~~~
624 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
625 <tdsindi@venus.cpqd.ansp.br>
626 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
627 <tdsimao@venus.cpqd.ansp.br>
628
629 ----------------------------------------------------------------------
630 */
631 void G726_expand(s, law, sl)
632 short *s;
633 char *law;
634 short *sl;
635 {
636 long mant, iexp;
637 short s1, ss, sig, ssm, ssq, sss;
638
639 s1 = *s;
640
641 if (*law == '1') {
642 /* Invert sign bit */
643 s1 ^= 128;
644 if (s1 >= 128) {
645 s1 += -128;
646 sig = 4096;
647 } else {
648 sig = 0;
649
650 }
651 iexp = s1 / 16;
652
653 mant = s1 - (iexp << 4);
654 ss = (iexp == 0) ?
655 ((mant << 1) + 1 + sig) :
656 ((1 << (iexp - 1)) * ((mant << 1) + 33) + sig);
657
658 sss = ss / 4096;
659 ssm = ss & 4095;
660 ssq = ssm << 1;
661 } else {
662 /* Invert sign bit */
663 s1 ^= 128;
664 if (s1 >= 128) {
665 s1 += -128;
666 s1 ^= 127;
667 sig = 8192;
668 } else {
669 sig = 0;
670 s1 ^= 127;
671 }
672 iexp = s1 / 16;
673
674 mant = s1 - (iexp << 4);
675
676 ss = (iexp == 0) ?
677 ((mant << 1) + sig) :
678 ((1 << iexp) * ((mant << 1) + 33) - 33 + sig);
679
680 sss = ss / 8192;
681 ssq = ss & 8191;
682 }
683
684 *sl = (sss == 0) ? ssq : ((16384 - ssq) & 16383);
685
686 }
687
688 /* ...................... end of G726_expand() ...................... */
689
690
691 /*
692 ----------------------------------------------------------------------
693
694 void G726_subta (short *sl, short *se, short *d);
695 ~~~~~~~~~~~~~~~
696
697 Description:
698 ~~~~~~~~~~~~
699
700 Compute difference signal by subtracting signal estimate from
701 input signal (or quantized reconstructed signal in decoder).
702
703 Inputs: sl (slx in decoder), se
704 Output: d (dx in decoder)
705
706
707 Return value: none.
708 ~~~~~~~~~~~~~
709
710 Prototype: in g726.h
711 ~~~~~~~~~~
712
713 History:
714 ~~~~~~~~
715 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
716 <tdsindi@venus.cpqd.ansp.br>
717 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
718 <tdsimao@venus.cpqd.ansp.br>
719
720
721 ----------------------------------------------------------------------
722 */
723 void G726_subta(sl, se, d)
724 short *sl, *se, *d;
725 {
726 long se1;
727 long sl1, sei, sli;
728 short ses, sls;
729
730 sls = (*sl >> 13);
731
732 sl1 = *sl;
733 se1 = *se;
734
735 /* Sign extension */
736 sli = (sls == 0) ? sl1 : (sl1 + 49152);
737
738 ses = (*se >> 14);
739
740 /* Sign extension */
741 sei = (ses == 0) ? se1 : (se1 + 32768);
742
743 /* 16 bit TC */
744 *d = (short) ((sli + 65536 - sei) & 65535);
745
746 }
747
748 /* ......................... end of G726_subta() ......................... */
749
750
751 /*
752 --------------------------------------------------------------------------
753
754 void G726_log (short *d, short *dl, short *ds);
755 ~~~~~~~~~~~~~
756
757 Description:
758 ~~~~~~~~~~~~
759
760 Convert difference signal from linear to the logarithmic domain.
761
762 Prototype: in g726.h
763 ~~~~~~~~~~
764
765 History:
766 ~~~~~~~~
767 31.Jan.91 v1.0f 1st release to UGST, in Fortran.
768 <tdsindi@venus.cpqd.ansp.br>
769 13.Feb.92 v1.0c release of 1st version, in C.
770 <tdsimao@venus.cpqd.ansp.br>
771
772 --------------------------------------------------------------------------
773 */
774 void G726_log(d, dl, ds)
775 short *d, *dl, *ds;
776 {
777 long mant;
778 long d1;
779 long dqm, exp_;
780
781 *ds = (*d >> 15);
782
783 d1 = *d;
784
785 /* Convert from 2-complement to signed magnitude */
786 dqm = (*ds) ? ((65536 - d1) & 32767) : d1;
787
788 /* Compute exponent */
789 if (dqm >= 16384)
790 exp_ = 14;
791 else if (dqm >= 8192)
792 exp_ = 13;
793 else if (dqm >= 4096)
794 exp_ = 12;
795 else if (dqm >= 2048)
796 exp_ = 11;
797 else if (dqm >= 1024)
798 exp_ = 10;
799 else if (dqm >= 512)
800 exp_ = 9;
801 else if (dqm >= 256)
802 exp_ = 8;
803 else if (dqm >= 128)
804 exp_ = 7;
805 else if (dqm >= 64)
806 exp_ = 6;
807 else if (dqm >= 32)
808 exp_ = 5;
809 else if (dqm >= 16)
810 exp_ = 4;
811 else if (dqm >= 8)
812 exp_ = 3;
813 else if (dqm >= 4)
814 exp_ = 2;
815 else if (dqm >= 2)
816 exp_ = 1;
817 else
818 exp_ = 0;
819
820 /* Compute approximation log2(1+x) = x */
821 mant = ((dqm << 7) >> exp_) & 127;
822
823 /* Combine mantissa and exponent (7 and 4) bits into a 11-bit word */
824 *dl = (short) ((exp_ << 7) + mant);
825
826 }
827
828 /* ........................ end of G726_log() ....................... */
829
830
831 /*
832 ----------------------------------------------------------------------
833
834 void G726_quan (short rate, short *dln, short *ds, short *i);
835 ~~~~~~~~~~~~~~
836
837 Description:
838 ~~~~~~~~~~~~
839
840 Quantize difference signal in logarithmic
841 domain.
842
843 Inputs: dln, ds, rate
844 Output: i
845
846
847 Return value: none.
848 ~~~~~~~~~~~~~
849
850 Prototype: in g726.h
851 ~~~~~~~~~~
852
853 History:
854 ~~~~~~~~
855 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
856 <tdsindi@venus.cpqd.ansp.br>
857 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
858 <tdsimao@venus.cpqd.ansp.br>
859
860 ----------------------------------------------------------------------
861 */
862 void G726_quan(rate, dln, ds, i)
863 short *dln, *ds, *i;
864 short rate;
865 {
866 if (rate == 4) {
867 if (*dln >= 3972)
868 *i = 1;
869 else if (*dln >= 2048)
870 *i = 15;
871 else if (*dln >= 400)
872 *i = 7;
873 else if (*dln >= 349)
874 *i = 6;
875 else if (*dln >= 300)
876 *i = 5;
877 else if (*dln >= 246)
878 *i = 4;
879 else if (*dln >= 178)
880 *i = 3;
881 else if (*dln >= 80)
882 *i = 2;
883 else
884 *i = 1;
885
886 /* Adjust for sign */
887 if (*ds)
888 *i = 15 - *i;
889
890 if (*i == 0)
891 *i = 15;
892 }
893
894 /* ......... end of 32 kbit part ........... */
895 else if (rate == 3) {
896 if (*dln >= 2048)
897 *i = 7;
898 else if (*dln >= 331)
899 *i = 3;
900 else if (*dln >= 218)
901 *i = 2;
902 else if (*dln >= 8)
903 *i = 1;
904 else if (*dln >= 0)
905 *i = 7;
906
907 /* Adjust for sign */
908 if (*ds)
909 *i = 7 - *i;
910
911 if (*i == 0)
912 *i = 7;
913 }
914 /* ......... end of 24 kbit part ........... */
915 else if (rate == 2) {
916 if (*dln >= 2048)
917 *i = 0;
918 else if (*dln >= 261)
919 *i = 1;
920 else
921 *i = 0;
922
923 /* Adjust for sign */
924 if (*ds)
925 *i = 3 - *i;
926 }
927 /* ......... end of 16 kbit part ........... */
928 else {
929 if (*dln >= 4080)
930 *i = 2;
931 else if (*dln >= 3974)
932 *i = 1;
933 else if (*dln >= 2048)
934 *i = 31;
935 else if (*dln >= 553)
936 *i = 15;
937 else if (*dln >= 528)
938 *i = 14;
939 else if (*dln >= 502)
940 *i = 13;
941 else if (*dln >= 475)
942 *i = 12;
943 else if (*dln >= 445)
944 *i = 11;
945 else if (*dln >= 413)
946 *i = 10;
947 else if (*dln >= 378)
948 *i = 9;
949 else if (*dln >= 339)
950 *i = 8;
951 else if (*dln >= 298)
952 *i = 7;
953 else if (*dln >= 250)
954 *i = 6;
955 else if (*dln >= 198)
956 *i = 5;
957 else if (*dln >= 139)
958 *i = 4;
959 else if (*dln >= 68)
960 *i = 3;
961 else if (*dln >= 0)
962 *i = 2;
963
964 if (*ds)
965 *i = 31 - *i;
966
967 if (*i == 0)
968 *i = 31;
969
970 } /* ......... end of 40 kbit part ........... */
971
972 }
973
974 /* ........................ end of G726_quan() ........................ */
975
976
977 /*
978 ----------------------------------------------------------------------
979
980 void G726_subtb (short *dl, short *y, short *dln);
981 ~~~~~~~~~~~~~~~
982
983 Description:
984 ~~~~~~~~~~~~
985
986 Scale logarithmic version of difference signal by subtracting
987 scale factor.
988
989 Inputs: dl (dlx no decodificador), y
990 Output: dln (dlnx no decodificador)
991
992
993 Return value: none.
994 ~~~~~~~~~~~~~
995
996 Prototype: in g726.h
997 ~~~~~~~~~~
998
999 History:
1000 ~~~~~~~~
1001 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1002 <tdsindi@venus.cpqd.ansp.br>
1003 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1004 <tdsimao@venus.cpqd.ansp.br>
1005
1006 ----------------------------------------------------------------------
1007 */
1008 void G726_subtb(dl, y, dln)
1009 short *dl, *y, *dln;
1010 {
1011 *dln = (*dl + 4096 - (*y >> 2)) & 4095;
1012
1013 }
1014
1015 /* ........................ end of G726_subtb() ........................ */
1016
1017
1018 /*
1019 ----------------------------------------------------------------------
1020
1021 void G726_adda (short *dqln, short *y, short *dql);
1022 ~~~~~~~~~~~~~~
1023
1024 Description:
1025 ~~~~~~~~~~~~
1026
1027 Addition of scale factor to logarithmic version of quantized
1028 difference signal.
1029
1030 Inputs: dqln, y
1031 Output: dql
1032
1033
1034 Return value: none.
1035 ~~~~~~~~~~~~~
1036
1037 Prototype: in g726.h
1038 ~~~~~~~~~~
1039
1040 History:
1041 ~~~~~~~~
1042 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1043 <tdsindi@venus.cpqd.ansp.br>
1044 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1045 <tdsimao@venus.cpqd.ansp.br>
1046
1047 ----------------------------------------------------------------------
1048 */
1049 void G726_adda(dqln, y, dql)
1050 short *dqln, *y, *dql;
1051 {
1052 *dql = (*dqln + (*y >> 2)) & 4095;
1053
1054 }
1055
1056 /* ....................... end of G726_adda() ....................... */
1057
1058
1059 /*
1060 ----------------------------------------------------------------------
1061
1062 void G726_antilog (short *dql, short *dqs, short *dq);
1063 ~~~~~~~~~~~~~~~~~
1064
1065 Description:
1066 ~~~~~~~~~~~~
1067
1068 Convert quantized difference signal from the logarithmic to
1069 the linear domain.
1070
1071 Inputs: dql, dqs
1072 Output: dq
1073
1074 Return value: none.
1075 ~~~~~~~~~~~~~
1076
1077 Prototype: in g726.h
1078 ~~~~~~~~~~
1079
1080 History:
1081 ~~~~~~~~
1082 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1083 <tdsindi@venus.cpqd.ansp.br>
1084 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1085 <tdsimao@venus.cpqd.ansp.br>
1086
1087 ----------------------------------------------------------------------
1088 */
1089 void G726_antilog(dql, dqs, dq)
1090 short *dql, *dqs, *dq;
1091 {
1092 long dqmag;
1093 long ds, dmn, dex, dqt;
1094
1095 /* Extract 4-bit exponent */
1096 ds = (*dql >> 11);
1097 dex = (*dql >> 7) & 15;
1098
1099 /* Extract 7-bit mantissa */
1100 dmn = *dql & 127;
1101
1102 dqt = dmn + 128;
1103
1104 /* Convert mantissa to linear using the approx. 2**x = 1+x */
1105 dqmag = ds ? 0 : ((dqt << 7) >> (14 - dex));
1106
1107 /* Attach sign bit to signed mag. word */
1108 *dq = (short) (*dqs << 15) + dqmag;
1109 }
1110
1111 /* ..................... end of G726_antilog() ..................... */
1112
1113
1114 /*
1115 ----------------------------------------------------------------------
1116
1117 void G726_reconst (short rate, short *i, short *dqln, short *dqs);
1118 ~~~~~~~~~~~~~~~~~
1119
1120 Description:
1121 ~~~~~~~~~~~~
1122
1123 Reconstruction of quantized difference signal in the
1124 logarithmic domain.
1125
1126 Inputs: i, rate
1127 Outputs: dqln, dqs
1128
1129 Return value: none.
1130 ~~~~~~~~~~~~~
1131
1132 Prototype: in g726.h
1133 ~~~~~~~~~~
1134
1135 History:
1136 ~~~~~~~~
1137 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1138 <tdsindi@venus.cpqd.ansp.br>
1139 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1140 <tdsimao@venus.cpqd.ansp.br>
1141
1142 ----------------------------------------------------------------------
1143 */
1144 void G726_reconst(rate, i, dqln, dqs)
1145 short *i, *dqln, *dqs;
1146 short rate;
1147 {
1148 if (rate == 4) {
1149 /* Initialized data */
1150 static short tab[16] = { 2048, 4, 135, 213, 273, 323, 373, 425,
1151 425, 373, 323, 273, 213, 135, 4, 2048
1152 };
1153
1154 /* Extract sign */
1155 *dqs = (*i >> 3);
1156
1157 /* Table look-up */
1158 *dqln = tab[*i];
1159 }
1160 /* ............... end of 32 kbit part
1161 * ................. */
1162 else if (rate == 3) {
1163 /* Initialized data */
1164 static short tab[8] = { 2048, 135, 273, 373, 373, 273, 135, 2048 };
1165
1166 *dqs = (*i >> 2);
1167
1168 /* Table look-up */
1169 *dqln = tab[*i];
1170
1171 }
1172
1173 /* ............... end of 24 kbit part
1174 * ................. */
1175 else if (rate == 2) {
1176 /* Initialized data */
1177 static short tab[4] = { 116, 365, 365, 116 };
1178
1179 *dqs = (*i >> 1);
1180
1181 /* Table look-up */
1182 *dqln = tab[*i];
1183
1184 } /* ............... end of 16 kbit part
1185 * ................. */
1186 else {
1187 /* Initialized data */
1188 static short tab[32] =
1189 { 2048, 4030, 28, 104, 169, 224, 274, 318, 358, 395, 429,
1190 459, 488, 514, 539, 566, 566, 539, 514, 488, 459, 429, 395, 358,
1191 318, 274, 224,
1192 169, 104, 28, 4030, 2048
1193 };
1194
1195 *dqs = (*i >> 4);
1196
1197 /* Table look-up */
1198 *dqln = tab[*i];
1199
1200 } /* ................ end of 40 kbit part
1201 * ................... */
1202
1203
1204 }
1205
1206 /* ....................... end of G726_reconst() ....................... */
1207
1208
1209 /*
1210 ----------------------------------------------------------------------
1211
1212 void G726_delaya (short *r, short *x, short *y);
1213 ~~~~~~~~~~~~~~~~
1214
1215 Description:
1216 ~~~~~~~~~~~~
1217
1218 Memory block.
1219
1220 Inputs: r, x
1221 Output: y
1222
1223 Return value: none.
1224 ~~~~~~~~~~~~~
1225
1226 Prototype: in g726.h
1227 ~~~~~~~~~~
1228
1229 History:
1230 ~~~~~~~~
1231 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1232 <tdsindi@venus.cpqd.ansp.br>
1233 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1234 <tdsimao@venus.cpqd.ansp.br>
1235
1236 ----------------------------------------------------------------------
1237 */
1238 void G726_delaya(r, x, y)
1239 short *r, *x, *y;
1240 {
1241 *y = (*r == 0) ? *x : 0;
1242
1243 }
1244
1245 /* ....................... end of G726_delaya() ....................... */
1246
1247
1248 /*
1249 ----------------------------------------------------------------------
1250
1251 void G726_delayb (short *r, short *x, short *y);
1252 ~~~~~~~~~~~~~~~~
1253
1254 Description:
1255 ~~~~~~~~~~~~
1256
1257 Memory block.
1258
1259 Inputs: r, x
1260 Output: y
1261
1262 Return value: none.
1263 ~~~~~~~~~~~~~
1264
1265 Prototype: in g726.h
1266 ~~~~~~~~~~
1267
1268 History:
1269 ~~~~~~~~
1270 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1271 <tdsindi@venus.cpqd.ansp.br>
1272 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1273 <tdsimao@venus.cpqd.ansp.br>
1274
1275 ----------------------------------------------------------------------
1276 */
1277 void G726_delayb(r, x, y)
1278 short *r, *x, *y;
1279 {
1280 *y = (*r == 0) ? *x : 544;
1281
1282 }
1283
1284 /* ....................... end of G726_delayb() ....................... */
1285
1286
1287 /*
1288 ----------------------------------------------------------------------
1289
1290 void G726_delayc (short *r, long *x, long *y);
1291 ~~~~~~~~~~~~~~~~
1292
1293 Description:
1294 ~~~~~~~~~~~~
1295
1296 Memory block.
1297
1298 Inputs: r, x
1299 Output: y
1300
1301 Return value: none.
1302 ~~~~~~~~~~~~~
1303
1304 Prototype: in g726.h
1305 ~~~~~~~~~~
1306
1307 History:
1308 ~~~~~~~~
1309 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1310 <tdsindi@venus.cpqd.ansp.br>
1311 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1312 <tdsimao@venus.cpqd.ansp.br>
1313
1314 ----------------------------------------------------------------------
1315 */
1316 void G726_delayc(r, x, y)
1317 short *r;
1318 long *x, *y;
1319 {
1320 *y = (*r == 0) ? *x : 34816;
1321
1322 }
1323
1324 /* ....................... end of G726_delayc() ....................... */
1325
1326
1327 /*
1328 ----------------------------------------------------------------------
1329
1330 void G726_delayd (short *r, short *x, short *y);
1331 ~~~~~~~~~~~~~~~~
1332
1333 Description:
1334 ~~~~~~~~~~~~
1335
1336 Memory block
1337
1338 Inputs: r, x
1339 Output: y
1340
1341 Return value: none.
1342 ~~~~~~~~~~~~~
1343
1344 Prototype: in g726.h
1345 ~~~~~~~~~~
1346
1347 History:
1348 ~~~~~~~~
1349 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1350 <tdsindi@venus.cpqd.ansp.br>
1351 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1352 <tdsimao@venus.cpqd.ansp.br>
1353
1354 ----------------------------------------------------------------------
1355 */
1356 void G726_delayd(r, x, y)
1357 short *r, *x, *y;
1358 {
1359 *y = (*r == 0) ? *x : 32;
1360
1361 }
1362
1363 /* ....................... end of G726_delayd() ....................... */
1364
1365
1366 /*
1367 ----------------------------------------------------------------------
1368
1369 void G726_filtd (short *wi, short *y, short *yut);
1370 ~~~~~~~~~~~~~~~
1371
1372 Description:
1373 ~~~~~~~~~~~~
1374
1375 Update of fast quantizer scale factor.
1376
1377 Inputs: wi, y
1378 Output: yut
1379
1380 Return value: none.
1381 ~~~~~~~~~~~~~
1382
1383 Prototype: in g726.h
1384 ~~~~~~~~~~
1385
1386 History:
1387 ~~~~~~~~
1388 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1389 <tdsindi@venus.cpqd.ansp.br>
1390 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1391 <tdsimao@venus.cpqd.ansp.br>
1392
1393 ----------------------------------------------------------------------
1394 */
1395 void G726_filtd(wi, y, yut)
1396 short *wi, *y, *yut;
1397 {
1398 long difs, difsx;
1399 long y1;
1400 long wi1, dif;
1401
1402 /* Compute difference */
1403 wi1 = *wi;
1404 y1 = *y;
1405 dif = ((wi1 << 5) + 131072 - y1) & 131071;
1406 difs = (dif >> 16);
1407
1408 /* Time constant is 1/32; sign extension */
1409 difsx = (difs == 0) ? (dif >> 5) : ((dif >> 5) + 4096);
1410
1411 *yut = (short) ((y1 + difsx) & 8191);
1412
1413 }
1414
1415 /* ....................... end of G726_filte() ....................... */
1416
1417
1418 /*
1419 ----------------------------------------------------------------------
1420
1421 void G726_filte (short *yup, long *yl, long *ylp);
1422 ~~~~~~~~~~~~~~~
1423
1424 Description:
1425 ~~~~~~~~~~~~
1426
1427 Update of slow quantizer scale factor.
1428
1429
1430 Inputs: yup, yl
1431 Output: ylp
1432
1433 Return value: none.
1434 ~~~~~~~~~~~~~
1435
1436 Prototype: in g726.h
1437 ~~~~~~~~~~
1438
1439 History:
1440 ~~~~~~~~
1441 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1442 <tdsindi@venus.cpqd.ansp.br>
1443 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1444 <tdsimao@venus.cpqd.ansp.br>
1445
1446 ----------------------------------------------------------------------
1447 */
1448 void G726_filte(yup, yl, ylp)
1449 short *yup;
1450 long *yl, *ylp;
1451 {
1452 long difs, difsx;
1453 long dif, dif1, yup1;
1454
1455 /* Compute difference */
1456 yup1 = *yup;
1457 dif1 = 1048576 - *yl;
1458 dif = (yup1 + (dif1 >> 6)) & 16383;
1459 difs = (dif >> 13);
1460
1461 /* Sign extension */
1462 difsx = (difs == 0) ? dif : (dif + 507904);
1463
1464 *ylp = (*yl + difsx) & 524287;
1465
1466 }
1467
1468 /* ....................... end of G726_filte() ....................... */
1469
1470
1471 /*
1472 ----------------------------------------------------------------------
1473
1474 void G726_functw (short rate, short *i, short *wi);
1475 ~~~~~~~~~~~~~~~~
1476
1477 Description:
1478 ~~~~~~~~~~~~
1479
1480 Map quantizer output into logarithmic version of scale factor
1481 multiplier.
1482
1483 Inputs: i, rate
1484 Output: wi
1485
1486 Return value: none.
1487 ~~~~~~~~~~~~~
1488
1489 Prototype: in g726.h
1490 ~~~~~~~~~~
1491
1492 History:
1493 ~~~~~~~~
1494 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1495 <tdsindi@venus.cpqd.ansp.br>
1496 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1497 <tdsimao@venus.cpqd.ansp.br>
1498
1499 ----------------------------------------------------------------------
1500 */
1501 void G726_functw(rate, i, wi)
1502 short *i, *wi;
1503 short rate;
1504 {
1505 if (rate == 4) {
1506 /* Initialized data */
1507 static short tab[8] = { 4084, 18, 41, 64, 112, 198, 355, 1122 };
1508 short im, is;
1509
1510 is = (*i >> 3);
1511
1512 im = (is == 0) ? (*i & 7) : ((15 - *i) & 7);
1513
1514 /* Scale factor multiplier */
1515 *wi = tab[im];
1516
1517
1518 }
1519
1520
1521
1522
1523 /* ................. end of 32 kbit part
1524 * .............. */
1525 else if (rate == 3) {
1526
1527 /* Initialized data */
1528 static short tab[4] = { 4092, 30, 137, 582 };
1529 short im, is;
1530
1531
1532 is = (*i >> 2);
1533
1534 im = (is == 0) ? (*i & 3) : ((7 - *i) & 3);
1535
1536 *wi = tab[im];
1537
1538
1539
1540 }
1541
1542
1543
1544
1545 /* ................. end of 24 kbit part
1546 * .............. */
1547 else if (rate == 2) {
1548 /* Initialized data */
1549 static short tab[2] = { 4074, 439 };
1550 short im, is;
1551
1552
1553 is = (*i >> 1);
1554
1555 im = (is == 0) ? (*i & 1) : ((3 - *i) & 1);
1556
1557 *wi = tab[im];
1558
1559 }
1560
1561
1562
1563
1564 /* ................. end of 16 kbit part
1565 * .............. */
1566 else {
1567 /* Initialized data */
1568 static short tab[16] =
1569 { 14, 14, 24, 39, 40, 41, 58, 100, 141, 179, 219, 280, 358,
1570 440, 529, 696
1571 };
1572 short im, is;
1573
1574 is = (*i >> 4);
1575
1576 im = (is == 0) ? (*i & 15) : ((31 - *i) & 15);
1577
1578 *wi = tab[im];
1579 } /* ................. end of 40 kbit part
1580 * .............. */
1581 }
1582
1583 /* ....................... end of G726_functw() ....................... */
1584
1585
1586 /*
1587 ----------------------------------------------------------------------
1588
1589 void G726_limb (short *yut, short *yup);
1590 ~~~~~~~~~~~~~~
1591
1592 Description:
1593 ~~~~~~~~~~~~
1594
1595 Limit quantizer scale factor.
1596
1597 Inputs: yut
1598 Output: yup
1599
1600 Return value: none.
1601 ~~~~~~~~~~~~~
1602
1603 Prototype: in g726.h
1604 ~~~~~~~~~~
1605
1606 History:
1607 ~~~~~~~~
1608 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1609 <tdsindi@venus.cpqd.ansp.br>
1610 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1611 <tdsimao@venus.cpqd.ansp.br>
1612
1613 ----------------------------------------------------------------------
1614 */
1615 void G726_limb(yut, yup)
1616 short *yut, *yup;
1617 {
1618 short gell, geul;
1619
1620 geul = ((*yut + 11264) & 16383) >> 13;
1621 gell = ((*yut + 15840) & 16383) >> 13;
1622
1623 if (gell == 1)
1624 *yup = 544; /* Lower limit is 1.06 */
1625 else if (geul == 0)
1626 *yup = 5120; /* Upper limit is 10.0 */
1627 else
1628 *yup = *yut;
1629
1630 }
1631
1632 /* ....................... end of G726_limb() ....................... */
1633
1634
1635 /*
1636 ----------------------------------------------------------------------
1637
1638 void G726_mix (short *al, short *yu, long *yl, short *y);
1639 ~~~~~~~~~~~~~
1640
1641 Description:
1642 ~~~~~~~~~~~~
1643
1644 Form linear combination of fast and slow quantizer scale
1645 factors.
1646
1647 Inputs: al, yu, yl
1648 Output: y
1649
1650
1651 Return value: none.
1652 ~~~~~~~~~~~~~
1653
1654 Prototype: in g726.h
1655 ~~~~~~~~~~
1656
1657 History:
1658 ~~~~~~~~
1659 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1660 <tdsindi@venus.cpqd.ansp.br>
1661 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1662 <tdsimao@venus.cpqd.ansp.br>
1663
1664 ----------------------------------------------------------------------
1665 */
1666 void G726_mix(al, yu, yl, y)
1667 short *al, *yu;
1668 long *yl;
1669 short *y;
1670 {
1671 long difm, difs, prod;
1672 long prodm, al1;
1673 long yu1, dif;
1674
1675 /* Preamble */
1676 al1 = *al;
1677 yu1 = *yu;
1678
1679 /* Compute difference */
1680 dif = (yu1 + 16384 - (*yl >> 6)) & 16383;
1681 difs = (dif >> 13);
1682
1683 /* Compute magnitude of difference */
1684 difm = (difs == 0) ? dif : ((16384 - dif) & 8191);
1685
1686 /* Compute magnitude of product */
1687 prodm = ((difm * al1) >> 6);
1688
1689 /* Convert magnitude to two's complement */
1690 prod = (difs == 0) ? prodm : ((16384 - prodm) & 16383);
1691
1692 *y = (short) (((*yl >> 6) + prod) & 8191);
1693
1694 }
1695
1696 /* ....................... end of G726_mix() ....................... */
1697
1698
1699 /*
1700 ----------------------------------------------------------------------
1701
1702 void G726_filta (short *fi, short *dms, short *dmsp);
1703 ~~~~~~~~~~~~~~~
1704
1705 Description:
1706 ~~~~~~~~~~~~
1707
1708 Update of short term average of f(i).
1709
1710 Inputs: fi, dms
1711 Output: dmsp
1712
1713 Return value: none.
1714 ~~~~~~~~~~~~~
1715
1716 Prototype: in g726.h
1717 ~~~~~~~~~~
1718
1719 History:
1720 ~~~~~~~~
1721 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1722 <tdsindi@venus.cpqd.ansp.br>
1723 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1724 <tdsimao@venus.cpqd.ansp.br>
1725
1726 ----------------------------------------------------------------------
1727 */
1728 void G726_filta(fi, dms, dmsp)
1729 short *fi, *dms, *dmsp;
1730 {
1731 short difs, difsx;
1732 short dif;
1733
1734 /* Compute difference */
1735 dif = ((*fi << 9) + 8192 - *dms) & 8191;
1736 difs = (dif >> 12);
1737
1738 /* Time constant is 1/32, sign extension */
1739 difsx = (difs == 0) ? (dif >> 5) : ((dif >> 5) + 3840);
1740
1741 *dmsp = (difsx + *dms) & 4095;
1742
1743 }
1744
1745 /* ....................... end of G726_filta() ....................... */
1746
1747
1748 /*
1749 ----------------------------------------------------------------------
1750
1751 void G726_filtb (short *fi, short *dml, short *dmlp);
1752 ~~~~~~~~~~~~~~~
1753
1754 Description:
1755 ~~~~~~~~~~~~
1756
1757 Update of long term average of f(i).
1758
1759 Inputs: fi, dml
1760 Output: dmlp
1761
1762 Return value: none.
1763 ~~~~~~~~~~~~~
1764
1765 Prototype: in g726.h
1766 ~~~~~~~~~~
1767
1768 History:
1769 ~~~~~~~~
1770 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1771 <tdsindi@venus.cpqd.ansp.br>
1772 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1773 <tdsimao@venus.cpqd.ansp.br>
1774
1775 ----------------------------------------------------------------------
1776 */
1777 void G726_filtb(fi, dml, dmlp)
1778 short *fi, *dml, *dmlp;
1779 {
1780 long difs, difsx;
1781 long fi1;
1782 long dif, dml1;
1783
1784 /* Preamble */
1785 fi1 = *fi;
1786 dml1 = *dml;
1787
1788 /* Compute difference */
1789 dif = ((fi1 << 11) + 32768 - dml1) & 32767;
1790 difs = (dif >> 14);
1791
1792 /* Time constant is 1/28, sign extension */
1793 difsx = (difs == 0) ? (dif >> 7) : ((dif >> 7) + 16128);
1794
1795 *dmlp = (short) ((difsx + dml1) & 16383);
1796
1797 }
1798
1799 /* ....................... end of G726_filtb() ....................... */
1800
1801
1802 /*
1803 ----------------------------------------------------------------------
1804
1805 void G726_filtc (short *ax, short *ap, short *app);
1806 ~~~~~~~~~~~~~~~
1807
1808 Description:
1809 ~~~~~~~~~~~~
1810
1811 Low pass filter of speed control parameter.
1812
1813 Inputs: ax, ap
1814 Output: app
1815
1816 Return value: none.
1817 ~~~~~~~~~~~~~
1818
1819 Prototype: in g726.h
1820 ~~~~~~~~~~
1821
1822 History:
1823 ~~~~~~~~
1824 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1825 <tdsindi@venus.cpqd.ansp.br>
1826 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1827 <tdsimao@venus.cpqd.ansp.br>
1828
1829 ----------------------------------------------------------------------
1830 */
1831 void G726_filtc(ax, ap, app)
1832 short *ax, *ap, *app;
1833 {
1834 short difs, difsx;
1835 short dif;
1836
1837 /* Compute difference */
1838 dif = ((*ax << 9) + 2048 - *ap) & 2047;
1839 difs = (dif >> 10);
1840
1841 /* Time constant is 1/16, sign extension */
1842 difsx = (difs == 0) ? (dif >> 4) : ((dif >> 4) + 896);
1843
1844 *app = (difsx + *ap) & 1023;
1845
1846 }
1847
1848 /* .................... end of G726_filtc() .................... */
1849
1850
1851 /*
1852 ----------------------------------------------------------------------
1853
1854 void G726_functf (short rate, short *i, short *fi);
1855 ~~~~~~~~~~~~~~~~
1856
1857 Description:
1858 ~~~~~~~~~~~~
1859
1860 Map quantizer output into the f(i) function.
1861
1862 Inputs: i, rate
1863 Output: fi
1864
1865 Return value: none.
1866 ~~~~~~~~~~~~~
1867
1868 Prototype: in g726.h
1869 ~~~~~~~~~~
1870
1871 History:
1872 ~~~~~~~~
1873 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1874 <tdsindi@venus.cpqd.ansp.br>
1875 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1876 <tdsimao@venus.cpqd.ansp.br>
1877
1878 ----------------------------------------------------------------------
1879 */
1880 void G726_functf(rate, i, fi)
1881 short *i, *fi;
1882 short rate;
1883
1884 {
1885 short im, is;
1886
1887 if (rate == 4) {
1888 /* Initialized data */
1889 static short tab[8] = { 0, 0, 0, 1, 1, 1, 3, 7 };
1890
1891 is = (*i >> 3);
1892
1893 im = (is == 0) ? (*i & 7) : ((15 - *i) & 7);
1894
1895 *fi = tab[im];
1896 }
1897 /* ................ end of 32 kbit part
1898 * ................. */
1899 else if (rate == 3) {
1900 /* Initialized data */
1901 static short tab[4] = { 0, 1, 2, 7 };
1902
1903 is = (*i >> 2);
1904
1905 im = (is == 0) ? (*i & 3) : ((7 - *i) & 3);
1906
1907 *fi = tab[im];
1908
1909 } /* ................ end of 24 kbit part
1910 * ................. */
1911 else if (rate == 2) {
1912 /* Initialized data */
1913 static short tab[2] = { 0, 7 };
1914
1915
1916 is = (*i >> 1);
1917
1918 im = (is == 0) ? (*i & 1) : ((3 - *i) & 1);
1919
1920 *fi = tab[im];
1921
1922 }
1923 /* ................ end of 16 kbit part
1924 * ................. */
1925 else {
1926 /* Initialized data */
1927 static short tab[16] =
1928 { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6 };
1929
1930
1931 is = (*i >> 4);
1932
1933 im = (is == 0) ? (*i & 15) : ((31 - *i) & 15);
1934
1935 *fi = tab[im];
1936
1937 } /* ................ end of 40 kbit part
1938 * ................. */
1939 }
1940
1941 /* ...................... end of G726_functf() ...................... */
1942
1943
1944 /*
1945 ----------------------------------------------------------------------
1946
1947 void G726_lima (short *ap, short *al);
1948 ~~~~~~~~~~~~~~
1949
1950 Description:
1951 ~~~~~~~~~~~~
1952
1953 Limit speed control parameter.
1954
1955 Inputs: ap
1956 Output: al
1957
1958 Return value: none.
1959 ~~~~~~~~~~~~~
1960
1961 Prototype: in g726.h
1962 ~~~~~~~~~~
1963
1964 History:
1965 ~~~~~~~~
1966 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
1967 <tdsindi@venus.cpqd.ansp.br>
1968 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
1969 <tdsimao@venus.cpqd.ansp.br>
1970
1971 ----------------------------------------------------------------------
1972 */
1973 void G726_lima(ap, al)
1974 short *ap, *al;
1975 {
1976 *al = (*ap >= 256) ? 64 : (*ap >> 2);
1977
1978 }
1979
1980 /* ....................... end of G726_lima() ....................... */
1981
1982
1983 /*
1984 ----------------------------------------------------------------------
1985
1986 void G726_subtc (short *dmsp, short *dmlp, short *tdp,
1987 ~~~~~~~~~~~~~~~ short *y, short *ax);
1988
1989 Description:
1990 ~~~~~~~~~~~~
1991
1992 Functions of quantizer output sequence and then perform
1993 threshold comparison for quantizing speed control parameter.
1994 compute magnitude of the difference of short and long-term
1995
1996 Inputs: dmsp, dmlp, tdp, y
1997 Output: ax
1998
1999 Return value: none.
2000 ~~~~~~~~~~~~~
2001
2002 Prototype: in g726.h
2003 ~~~~~~~~~~
2004
2005 History:
2006 ~~~~~~~~
2007 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2008 <tdsindi@venus.cpqd.ansp.br>
2009 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2010 <tdsimao@venus.cpqd.ansp.br>
2011
2012 ----------------------------------------------------------------------
2013 */
2014 void G726_subtc(dmsp, dmlp, tdp, y, ax)
2015 short *dmsp, *dmlp, *tdp, *y, *ax;
2016 {
2017 long difm, difs, dthr, dmlp1, dmsp1;
2018 long dif;
2019
2020 /* Preamble */
2021 dmsp1 = *dmsp;
2022 dmlp1 = *dmlp;
2023
2024 /* Compute difference */
2025 dif = ((dmsp1 << 2) + 32768 - dmlp1) & 32767;
2026 difs = (dif >> 14);
2027
2028 /* Compute magnitude of difference */
2029 difm = (difs == 0) ? dif : ((32768 - dif) & 16383);
2030
2031 /* Compute threshold */
2032 dthr = (dmlp1 >> 3);
2033
2034 /* Quantize speed control parameter */
2035 *ax = (*y >= 1536 && difm < dthr && *tdp == 0) ? 0 : 1;
2036
2037 }
2038
2039 /* ....................... end of G726_subtc() ....................... */
2040
2041
2042 /*
2043 ----------------------------------------------------------------------
2044
2045 void G726_triga (short *tr, short *app, short *apr);
2046 ~~~~~~~~~~~~~~~
2047
2048 Description:
2049 ~~~~~~~~~~~~
2050
2051 Speed control trigger block.
2052
2053 Inputs: tr, app
2054 Output: apr
2055
2056 Return value: none.
2057 ~~~~~~~~~~~~~
2058
2059 Prototype: in g726.h
2060 ~~~~~~~~~~
2061
2062 History:
2063 ~~~~~~~~
2064 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2065 <tdsindi@venus.cpqd.ansp.br>
2066 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2067 <tdsimao@venus.cpqd.ansp.br>
2068
2069 ----------------------------------------------------------------------
2070 */
2071 void G726_triga(tr, app, apr)
2072 short *tr, *app, *apr;
2073 {
2074 *apr = (*tr == 0) ? (*app) : 256;
2075
2076 }
2077
2078 /* ....................... end of G726_triga() ....................... */
2079
2080
2081 /*
2082 ----------------------------------------------------------------------
2083
2084 void G726_accum (short *wa1, short *wa2, short *wb1,
2085 ~~~~~~~~~~~~~~~ short *wb2, short *wb3, short *wb4,
2086 short *wb5, short *wb6, short *se, short *sez);
2087
2088 Description:
2089 ~~~~~~~~~~~~
2090
2091 Addition of predictor outputs to form the partial signal
2092 estimate (from the sixth order predictor) and the signal
2093 estimate.
2094
2095 Inputs: wa1, wa2, wb1, wb2, wb3, wb4, wb5, wb6
2096 Output: se, sez
2097
2098 Return value: none.
2099 ~~~~~~~~~~~~~
2100
2101 Prototype: in g726.h
2102 ~~~~~~~~~~
2103
2104 History:
2105 ~~~~~~~~
2106 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2107 <tdsindi@venus.cpqd.ansp.br>
2108 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2109 <tdsimao@venus.cpqd.ansp.br>
2110
2111 ----------------------------------------------------------------------
2112 */
2113 void G726_accum(wa1, wa2, wb1, wb2, wb3, wb4, wb5, wb6, se, sez)
2114 short *wa1, *wa2, *wb1, *wb2, *wb3, *wb4, *wb5, *wb6, *se, *sez;
2115 {
2116 unsigned long sezi;
2117 unsigned long wa11, wa21, wb11, wb21, wb31, wb41, wb51, wb61, sei;
2118
2119 /* Preamble */
2120 wa11 = *wa1;
2121 wa21 = *wa2;
2122 wb11 = *wb1;
2123 wb21 = *wb2;
2124 wb31 = *wb3;
2125 wb41 = *wb4;
2126 wb51 = *wb5;
2127 wb61 = *wb6;
2128
2129 /* Sum of partial signal estimate */
2130 sezi = (((((((((wb11 + wb21) & 65535) + wb31) & 65535)
2131 + wb41) & 65535) + wb51) & 65535) + wb61) & 65535;
2132
2133 /* Complete sum for signal estimate */
2134 sei = (((sezi + wa21) & 65535) + wa11) & 65535;
2135
2136 *sez = (short) (sezi >> 1);
2137 *se = (short) (sei >> 1);
2138
2139 }
2140
2141 /* ....................... end of G726_accum() ....................... */
2142
2143
2144 /*
2145 ----------------------------------------------------------------------
2146
2147 void G726_addb (short *dq, short *se, short *sr);
2148 ~~~~~~~~~~~~~~
2149
2150 Description:
2151 ~~~~~~~~~~~~
2152
2153 Addition of quantized difference signal and signal estimate
2154 to form reconstructed signal.
2155
2156 Inputs: dq, se
2157 Output: sr
2158
2159 Return value: none.
2160 ~~~~~~~~~~~~~
2161
2162 Prototype: in g726.h
2163 ~~~~~~~~~~
2164
2165 History:
2166 ~~~~~~~~
2167 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2168 <tdsindi@venus.cpqd.ansp.br>
2169 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2170 <tdsimao@venus.cpqd.ansp.br>
2171
2172 ----------------------------------------------------------------------
2173 */
2174 void G726_addb(dq, se, sr)
2175 short *dq, *se, *sr;
2176 {
2177 unsigned long dq1, se1;
2178 unsigned long dqi, sei;
2179 short dqs, ses;
2180
2181 /* Preamble */
2182 dq1 = *dq & 65535;
2183 se1 = *se;
2184
2185 /* Sign */
2186 dqs = (*dq >> 15) & 1;
2187
2188 /* Convert signed magnitude to 2's complement */
2189 dqi = (dqs == 0) ? dq1 : ((65536 - (dq1 & 32767)) & 65535);
2190
2191 ses = (*se >> 14);
2192
2193 /* Sign extension */
2194 sei = (ses == 0) ? se1 : ((1 << 15) + se1);
2195
2196 *sr = (short) ((dqi + sei) & 65535);
2197
2198 }
2199
2200 /* ....................... end of G726_addb() ....................... */
2201
2202
2203 /*
2204 ----------------------------------------------------------------------
2205
2206 void G726_addc (short *dq, short *sez, short *pk0,
2207 ~~~~~~~~~~~~~~ short *sigpk);
2208
2209 Description:
2210 ~~~~~~~~~~~~
2211
2212 Obtain sign of addition of quantized difference signal and
2213 partial signal estimate.
2214
2215 Inputs: dq, sez
2216 Outputs: pk0, sigpk
2217
2218 Return value: none.
2219 ~~~~~~~~~~~~~
2220
2221 Prototype: in g726.h
2222 ~~~~~~~~~~
2223
2224 History:
2225 ~~~~~~~~
2226 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2227 <tdsindi@venus.cpqd.ansp.br>
2228 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2229 <tdsimao@venus.cpqd.ansp.br>
2230
2231 ----------------------------------------------------------------------
2232 */
2233 void G726_addc(dq, sez, pk0, sigpk)
2234 short *dq, *sez, *pk0, *sigpk;
2235 {
2236 unsigned long sezi;
2237 short sezs;
2238 unsigned long dqsez, dq1;
2239 unsigned long dqi;
2240 short dqs;
2241 unsigned long sez1;
2242
2243 /* Preamble */
2244 dq1 = *dq & 65535;
2245 sez1 = *sez;
2246
2247 /* Get sign */
2248 dqs = (*dq >> 15) & 1;
2249
2250 /* Convert signed magnitude to 2's compelemnent */
2251 dqi = (dqs == 0) ? dq1 : ((65536 - (dq1 & 32767)) & 65535);
2252
2253 sezs = (*sez >> 14);
2254
2255 /* Sign extension */
2256 sezi = (sezs == 0) ? sez1 : (sez1 + 32768);
2257
2258 dqsez = (dqi + sezi) & 65535;
2259
2260 *pk0 = (short) (dqsez >> 15);
2261 *sigpk = (dqsez == 0) ? 1 : 0;
2262
2263 }
2264
2265 /* ....................... end of G726_addc() ....................... */
2266
2267
2268 /*
2269 ----------------------------------------------------------------------
2270
2271 void G726_floata (short *dq, short *dq0);
2272 ~~~~~~~~~~~~~~~~
2273
2274 Description:
2275 ~~~~~~~~~~~~
2276
2277 Convert 16-bit signed magnitude to floating point.
2278
2279 Inputs: dq
2280 Output: dq0
2281
2282 Return value: none.
2283 ~~~~~~~~~~~~~
2284
2285 Prototype: in g726.h
2286 ~~~~~~~~~~
2287
2288 History:
2289 ~~~~~~~~
2290 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2291 <tdsindi@venus.cpqd.ansp.br>
2292 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2293 <tdsimao@venus.cpqd.ansp.br>
2294
2295 ----------------------------------------------------------------------
2296 */
2297 void G726_floata(dq, dq0)
2298 short *dq, *dq0;
2299 {
2300 long mant;
2301 long mag, exp_;
2302 long dqs;
2303
2304 dqs = (*dq >> 15) & 1;
2305
2306 /* Compute magnitude */
2307 mag = *dq & 32767;
2308
2309 /* Exponent */
2310 if (mag >= 16384)
2311 exp_ = 15;
2312 else if (mag >= 8192)
2313 exp_ = 14;
2314 else if (mag >= 4096)
2315 exp_ = 13;
2316 else if (mag >= 2048)
2317 exp_ = 12;
2318 else if (mag >= 1024)
2319 exp_ = 11;
2320 else if (mag >= 512)
2321 exp_ = 10;
2322 else if (mag >= 256)
2323 exp_ = 9;
2324 else if (mag >= 128)
2325 exp_ = 8;
2326 else if (mag >= 64)
2327 exp_ = 7;
2328 else if (mag >= 32)
2329 exp_ = 6;
2330 else if (mag >= 16)
2331 exp_ = 5;
2332 else if (mag >= 8)
2333 exp_ = 4;
2334 else if (mag >= 4)
2335 exp_ = 3;
2336 else if (mag >= 2)
2337 exp_ = 2;
2338 else if (mag == 1)
2339 exp_ = 1;
2340 else
2341 exp_ = 0;
2342
2343 /* Compute mantissa w/a 1 in the most sig. bit */
2344 mant = (mag == 0) ? (1 << 5) : ((mag << 6) >> exp_);
2345 /* Combine sign, exponent and mantissa (1,4,6) bits in a word */
2346 *dq0 = (short) ((dqs << 10) + (exp_ << 6) + mant);
2347
2348 }
2349
2350 /* ....................... end of G726_floata() ....................... */
2351
2352
2353 /*
2354 ----------------------------------------------------------------------
2355
2356 void G726_floatb (short *sr, short *sr0);
2357 ~~~~~~~~~~~~~~~~
2358
2359 Description:
2360 ~~~~~~~~~~~~
2361
2362 Convert 16-bit two's complement to floating point.
2363
2364 Inputs: sr
2365 Output: sr0
2366
2367 Return value: none.
2368 ~~~~~~~~~~~~~
2369
2370 Prototype: in g726.h
2371 ~~~~~~~~~~
2372
2373 History:
2374 ~~~~~~~~
2375 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2376 <tdsindi@venus.cpqd.ansp.br>
2377 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2378 <tdsimao@venus.cpqd.ansp.br>
2379
2380 ----------------------------------------------------------------------
2381 */
2382 void G726_floatb(sr, sr0)
2383 short *sr, *sr0;
2384 {
2385 long mant;
2386 long mag, exp_, srr, srs;
2387
2388 /* Preamble */
2389 srr = *sr & 65535;
2390
2391 /* Sign */
2392 srs = (srr >> 15);
2393
2394 /* Compute magnitude */
2395 mag = (srs == 0) ? srr : ((65536 - srr) & 32767);
2396
2397 /* Exponent */
2398 if (mag >= 16384)
2399 exp_ = 15;
2400 else if (mag >= 8192)
2401 exp_ = 14;
2402 else if (mag >= 4096)
2403 exp_ = 13;
2404 else if (mag >= 2048)
2405 exp_ = 12;
2406 else if (mag >= 1024)
2407 exp_ = 11;
2408 else if (mag >= 512)
2409 exp_ = 10;
2410 else if (mag >= 256)
2411 exp_ = 9;
2412 else if (mag >= 128)
2413 exp_ = 8;
2414 else if (mag >= 64)
2415 exp_ = 7;
2416 else if (mag >= 32)
2417 exp_ = 6;
2418 else if (mag >= 16)
2419 exp_ = 5;
2420 else if (mag >= 8)
2421 exp_ = 4;
2422 else if (mag >= 4)
2423 exp_ = 3;
2424 else if (mag >= 2)
2425 exp_ = 2;
2426 else if (mag == 1)
2427 exp_ = 1;
2428 else
2429 exp_ = 0;
2430
2431 /* Compute mantissa w/a 1 in the most sig. bit */
2432 mant = (mag == 0) ? (1 << 5) : ((mag << 6) >> exp_);
2433
2434 /* Combine sign, exponent and mantissa (1,4,6) bits in a word */
2435 *sr0 = (short) ((srs << 10) + (exp_ << 6) + mant);
2436
2437 }
2438
2439 /* ....................... end of G726_floatb() ....................... */
2440
2441
2442 /*
2443 ----------------------------------------------------------------------
2444
2445 void G726_fmult (short *a, short *srn, short *wa);
2446 ~~~~~~~~~~~~~~~
2447
2448 Description:
2449 ~~~~~~~~~~~~
2450
2451 Multiply predictor coefficients with corresponding quantized
2452 difference signal or reconstructed signal. multiplication is
2453 done in floating piont format
2454
2455 Inputs: a (or b), srn (or dqn)
2456 Outputs: wa (or wb)
2457
2458 Return value: none.
2459 ~~~~~~~~~~~~~
2460
2461 Prototype: in g726.h
2462 ~~~~~~~~~~
2463
2464 History:
2465 ~~~~~~~~
2466 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2467 <tdsindi@venus.cpqd.ansp.br>
2468 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2469 <tdsimao@venus.cpqd.ansp.br>
2470
2471 ----------------------------------------------------------------------
2472 */
2473 void G726_fmult(An, SRn, WAn)
2474 short *An, *SRn, *WAn;
2475 {
2476 long anmag, anexp, wanmag, anmant;
2477 long wanexp, srnexp, an, ans, wanmant, srnmant;
2478 long wan, wans, srns, srn1;
2479
2480 /* Preamble */
2481 an = *An & 65535;
2482 srn1 = *SRn & 65535;
2483
2484 /* Sign */
2485 ans = an & 32768;
2486 ans = (ans >> 15);
2487
2488 /* Convert 2's complement to signed magnitude */
2489 anmag = (ans == 0) ? (an >> 2) : ((16384 - (an >> 2)) & 8191);
2490
2491 /* Exponent */
2492 if (anmag >= 4096)
2493 anexp = 13;
2494 else if (anmag >= 2048)
2495 anexp = 12;
2496 else if (anmag >= 1024)
2497 anexp = 11;
2498 else if (anmag >= 512)
2499 anexp = 10;
2500 else if (anmag >= 256)
2501 anexp = 9;
2502 else if (anmag >= 128)
2503 anexp = 8;
2504 else if (anmag >= 64)
2505 anexp = 7;
2506 else if (anmag >= 32)
2507 anexp = 6;
2508 else if (anmag >= 16)
2509 anexp = 5;
2510 else if (anmag >= 8)
2511 anexp = 4;
2512 else if (anmag >= 4)
2513 anexp = 3;
2514 else if (anmag >= 2)
2515 anexp = 2;
2516 else if (anmag == 1)
2517 anexp = 1;
2518 else
2519 anexp = 0;
2520
2521 /* Compute mantissa w/a 1 in the most sig. bit */
2522 anmant = (anmag == 0) ? (1 << 5) : ((anmag << 6) >> anexp);
2523
2524 /* Split floating point word into sign, exponent and mantissa */
2525 srns = (srn1 >> 10);
2526 srnexp = (srn1 >> 6) & 15;
2527 srnmant = srn1 & 63;
2528
2529 /* Floating point multiplication */
2530 wans = srns ^ ans;
2531 wanexp = srnexp + anexp;
2532 wanmant = ((srnmant * anmant) + 48) >> 4;
2533
2534 /* Convert floating point to magnitude */
2535 wanmag = (wanexp <= 26) ?
2536 (wanmant << 7) >> (26 - wanexp) :
2537 ((wanmant << 7) << (wanexp - 26)) & 32767;
2538
2539 /* Convert mag. to 2's complement */
2540 wan = (wans == 0) ? wanmag : ((65536 - wanmag) & 65535);
2541
2542 *WAn = (short) wan;
2543
2544 }
2545
2546 /* ....................... end of G726_fmult() ....................... */
2547
2548
2549 /*
2550 ----------------------------------------------------------------------
2551
2552 void G726_limc (short *a2t, short *a2p);
2553 ~~~~~~~~~~~~~~
2554
2555 Description:
2556 ~~~~~~~~~~~~
2557
2558 Limits on a2 coefficient of second order predictor.
2559
2560 Inputs: a2t
2561 Output: a2p
2562
2563 Return value: none.
2564 ~~~~~~~~~~~~~
2565
2566 Prototype: in g726.h
2567 ~~~~~~~~~~
2568
2569 History:
2570 ~~~~~~~~
2571 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2572 <tdsindi@venus.cpqd.ansp.br>
2573 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2574 <tdsimao@venus.cpqd.ansp.br>
2575
2576 ----------------------------------------------------------------------
2577 */
2578
2579 void G726_limc(a2t, a2p)
2580 short *a2t, *a2p;
2581 {
2582 long a2p1, a2t1, a2ll, a2ul;
2583
2584 a2t1 = *a2t & 65535;
2585
2586 a2ul = 12288; /* Upper limit of +.75 */
2587 a2ll = 53248; /* Lower limit of -.75 */
2588
2589 if (a2t1 >= 32768 && a2t1 <= a2ll)
2590 a2p1 = a2ll;
2591 else if (a2t1 >= a2ul && a2t1 <= 32767)
2592 a2p1 = a2ul;
2593 else
2594 a2p1 = a2t1;
2595
2596 *a2p = (short) a2p1;
2597
2598 }
2599
2600 /* ....................... end of G726_limc() ....................... */
2601
2602
2603 /*
2604 ----------------------------------------------------------------------
2605
2606 void G726_limd (short *a1t, short *a2p, short *a1p);
2607 ~~~~~~~~~~~~~~
2608
2609 Description:
2610 ~~~~~~~~~~~~
2611
2612 Limits on a1 coefficient of second order predictor.
2613
2614 Inputs: a1t, a2p
2615 Output: a1p
2616
2617 Return value: none.
2618 ~~~~~~~~~~~~~
2619
2620 Prototype: in g726.h
2621 ~~~~~~~~~~
2622
2623 History:
2624 ~~~~~~~~
2625 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2626 <tdsindi@venus.cpqd.ansp.br>
2627 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2628 <tdsimao@venus.cpqd.ansp.br>
2629
2630 ----------------------------------------------------------------------
2631 */
2632 void G726_limd(a1t, a2p, a1p)
2633 short *a1t, *a2p, *a1p;
2634 {
2635 long a1p1, a2p1, a1t1, ome, a1ll, a1ul;
2636
2637 /* Preamble */
2638 a1t1 = *a1t & 65535;
2639 a2p1 = *a2p & 65535;
2640
2641 /* (1-epsilon), where epsilon = (1/16) */
2642 ome = 15360;
2643
2644 /* Compute upper limit */
2645 a1ul = (ome + 65536 - a2p1) & 65535;
2646
2647 /* Compute lower limit */
2648 a1ll = (a2p1 + 65536 - ome) & 65535;
2649
2650 if (a1t1 >= 32768 && a1t1 <= a1ll)
2651 a1p1 = a1ll;
2652 else if (a1t1 >= a1ul && a1t1 <= 32767)
2653 a1p1 = a1ul;
2654 else
2655 a1p1 = a1t1;
2656
2657 *a1p = (short) a1p1;
2658
2659 }
2660
2661 /* ....................... end of G726_limd() ....................... */
2662
2663
2664 /*
2665 ----------------------------------------------------------------------
2666
2667 void G726_trigb (short *tr, short *ap, short *ar);
2668 ~~~~~~~~~~~~~~~
2669
2670 Description:
2671 ~~~~~~~~~~~~
2672
2673 Predictor trigger block.
2674
2675 Inputs: tr, ap (ou bp ou tdp)
2676 Output: ar (ou br ou tdr)
2677
2678 Return value: none.
2679 ~~~~~~~~~~~~~
2680
2681 Prototype: in g726.h
2682 ~~~~~~~~~~
2683
2684 History:
2685 ~~~~~~~~
2686 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2687 <tdsindi@venus.cpqd.ansp.br>
2688 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2689 <tdsimao@venus.cpqd.ansp.br>
2690
2691 ----------------------------------------------------------------------
2692 */
2693 void G726_trigb(tr, ap, ar)
2694 short *tr, *ap, *ar;
2695 {
2696 *ar = (*tr == 0) ? *ap : 0;
2697
2698 }
2699
2700 /* ....................... end of G726_trigb() ....................... */
2701
2702
2703 /*
2704 ----------------------------------------------------------------------
2705
2706 void G726_upa1 (short *pk0, short *pk1, short *a1,
2707 ~~~~~~~~~~~~~~ short *sigpk, short *a1t);
2708
2709 Description:
2710 ~~~~~~~~~~~~
2711
2712 Update a1 coefficient of second order predictor.
2713
2714 Inputs: pk0, pk1, a1, sigpk
2715 Output: a1t
2716
2717 Return value: none.
2718 ~~~~~~~~~~~~~
2719
2720 Prototype: in g726.h
2721 ~~~~~~~~~~
2722
2723 History:
2724 ~~~~~~~~
2725 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2726 <tdsindi@venus.cpqd.ansp.br>
2727 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2728 <tdsimao@venus.cpqd.ansp.br>
2729
2730 ----------------------------------------------------------------------
2731 */
2732 void G726_upa1(pk0, pk1, a1, sigpk, a1t)
2733 short *pk0, *pk1, *a1, *sigpk, *a1t;
2734 {
2735 long a11, a1s, ua1;
2736 long ash;
2737 short pks;
2738 long uga1, ula1;
2739
2740 /* Preamble */
2741 a11 = *a1 & 65535;
2742
2743 pks = (*pk0) ^ (*pk1);
2744
2745 /* Gain is +/- (3/256) */
2746 uga1 = (*sigpk == 1) ? 0 : ((pks == 0) ? 192 : 65344);
2747
2748 a1s = (a11 >> 15);
2749
2750 /* Leak factor is (1/256) */
2751 ash = (a11 >> 8);
2752 ula1 = ((a1s == 0) ? (65536 - ash) : (65536 - (ash + 65280))) & 65535;
2753
2754 /* Compute update */
2755 ua1 = (uga1 + ula1) & 65535;
2756 *a1t = (short) ((a11 + ua1) & 65535);
2757
2758 }
2759
2760 /* ....................... end of G726_upa1() ....................... */
2761
2762
2763 /*
2764 ----------------------------------------------------------------------
2765
2766 void G726_upa2 (short *pk0, short *pk1, short *pk2,
2767 ~~~~~~~~~~~~~~ short *a2, short *a1, short *sigpk, short *a2t);
2768
2769 Description:
2770 ~~~~~~~~~~~~
2771
2772 Update a2 coefficient of second order predictor.
2773
2774 Inputs: pk0, pk1, pk2, a2, a1, sigpk
2775 Output: a2t
2776
2777 Return value: none.
2778 ~~~~~~~~~~~~~
2779
2780 Prototype: in g726.h
2781 ~~~~~~~~~~
2782
2783 History:
2784 ~~~~~~~~
2785 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2786 <tdsindi@venus.cpqd.ansp.br>
2787 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2788 <tdsimao@venus.cpqd.ansp.br>
2789
2790 ----------------------------------------------------------------------
2791 */
2792 void G726_upa2(pk0, pk1, pk2, a2, a1, sigpk, a2t)
2793 short *pk0, *pk1, *pk2, *a2, *a1, *sigpk, *a2t;
2794 {
2795 long uga2a, uga2b, uga2s;
2796 long a11, a21, fa, fa1;
2797 short a1s, a2s;
2798 long ua2;
2799 long uga2, ula2;
2800 short pks1, pks2;
2801
2802 /* Preamble */
2803 a11 = *a1 & 65535;
2804 a21 = *a2 & 65535;
2805
2806 /* 1 bit xors */
2807 pks1 = (*pk0 ^ *pk1);
2808 pks2 = (*pk0 ^ *pk2);
2809
2810 uga2a = (pks2 == 0) ? 16384 : 114688;
2811
2812 a1s = (*a1 >> 15);
2813
2814 /* Implement f(a1) w/ limiting at +/-(1/2) */
2815 if (a1s == 0)
2816 fa1 = (a11 <= 8191) ? (a11 << 2) : (8191 << 2);
2817 else
2818 fa1 = (a11 >= 57345) ? ((a11 << 2) & 131071) : (24577 << 2);
2819
2820 /* Attach sign to the result of f(a1) */
2821 fa = (pks1) ? fa1 : ((131072 - fa1) & 131071);
2822
2823 uga2b = (uga2a + fa) & 131071;
2824 uga2s = (uga2b >> 16);
2825
2826 uga2 = (*sigpk == 1) ? 0 :
2827 ((uga2s) ? ((uga2b >> 7) + 64512) : (uga2b >> 7));
2828
2829 a2s = (*a2 >> 15);
2830
2831 ula2 = (a2s == 0) ? (65536 - (a21 >> 7)) & 65535 :
2832 (65536 - ((a21 >> 7) + 65024)) & 65535;
2833
2834 /* Compute update */
2835 ua2 = (uga2 + ula2) & 65535;
2836 *a2t = (short) ((a21 + ua2) & 65535);
2837
2838 }
2839
2840 /* ....................... end of G726_upa2() ....................... */
2841
2842
2843 /*
2844 ----------------------------------------------------------------------
2845
2846 void G726_upb (short rate, short *u, short *b, short *dq, short *bp);
2847 ~~~~~~~~~~~~~
2848
2849 Description:
2850 ~~~~~~~~~~~~
2851
2852 Update for coefficients of sixth order predictor.
2853
2854 Inputs: u, b, dq, rate
2855 Output: bp
2856
2857 Return value: none.
2858 ~~~~~~~~~~~~~
2859
2860 Prototype: in g726.h
2861 ~~~~~~~~~~
2862
2863 History:
2864 ~~~~~~~~
2865 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2866 <tdsindi@venus.cpqd.ansp.br>
2867 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2868 <tdsimao@venus.cpqd.ansp.br>
2869
2870 ----------------------------------------------------------------------
2871 */
2872 void G726_upb(rate, u, b, dq, bp)
2873 short *u, *b, *dq, *bp;
2874 short rate;
2875 {
2876 short dqmag;
2877 long bb, bs, ub;
2878 long ugb, ulb;
2879 short param;
2880 short leak;
2881
2882 /* Preamble */
2883 bb = *b & 65535;
2884
2885 dqmag = *dq & 32767;
2886 if (rate != 5) {
2887 leak = 8;
2888 param = 65280;
2889 } else {
2890 leak = 9;
2891 param = 65408;
2892 }
2893 /* gain is 0 or +/- (1/128) */
2894 ugb = (dqmag == 0) ? 0 : ((*u == 0) ? 128 : 65408);
2895
2896 bs = (bb >> 15);
2897
2898 /* Leak factor is (1/256 or 1/512 for 40 kbit/s) */
2899
2900 ulb = (bs == 0) ?
2901 ((65536 - (bb >> leak)) & 65535) :
2902 ((65536 - ((bb >> leak) + param)) & 65535);
2903
2904 /* Compute update */
2905 ub = (ugb + ulb) & 65535;
2906 /* aux = bb + ub;*/
2907
2908 *bp = (short) ((bb + ub) & 65535);
2909
2910 }
2911
2912 /* ....................... end of G726_upb() ....................... */
2913
2914
2915 /*
2916 ----------------------------------------------------------------------
2917
2918 void G726_xor (short *dqn, short *dq, short *u);
2919 ~~~~~~~~~~~~~
2920
2921 Description:
2922 ~~~~~~~~~~~~
2923
2924 One bit "exclusive or" of sign of difference signal and sign
2925 of delayed difference signal.
2926
2927 Inputs: dqn, dq
2928 Output: u
2929
2930
2931 Return value: none.
2932 ~~~~~~~~~~~~~
2933
2934 Prototype: in g726.h
2935 ~~~~~~~~~~
2936
2937 History:
2938 ~~~~~~~~
2939 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2940 <tdsindi@venus.cpqd.ansp.br>
2941 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2942 <tdsimao@venus.cpqd.ansp.br>
2943
2944 ----------------------------------------------------------------------
2945 */
2946 void G726_xor(dqn, dq, u)
2947 short *dqn, *dq, *u;
2948 {
2949 short dqns;
2950 short dqs;
2951
2952 dqs = (*dq >> 15) & 1;
2953
2954 dqns = (*dqn >> 10);
2955
2956 *u = (dqs ^ dqns);
2957
2958 }
2959
2960 /* ....................... end of G726_xor() ....................... */
2961
2962
2963 /*
2964 ----------------------------------------------------------------------
2965
2966 void G726_tone (short *a2p, short *tdp);
2967 ~~~~~~~~~~~~~~
2968
2969 Description:
2970 ~~~~~~~~~~~~
2971
2972 Partial band signal detection.
2973
2974 Inputs: a2p
2975 Output: tdp
2976
2977 Return value: none.
2978 ~~~~~~~~~~~~~
2979
2980 Prototype: in g726.h
2981 ~~~~~~~~~~
2982
2983 History:
2984 ~~~~~~~~
2985 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
2986 <tdsindi@venus.cpqd.ansp.br>
2987 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
2988 <tdsimao@venus.cpqd.ansp.br>
2989
2990 ----------------------------------------------------------------------
2991 */
2992 void G726_tone(a2p, tdp)
2993 short *a2p, *tdp;
2994 {
2995 long a2p1;
2996
2997 a2p1 = *a2p & 65535;
2998
2999 *tdp = (a2p1 >= 32768 && a2p1 < 53760) ? 1 : 0;
3000
3001 }
3002
3003 /* ....................... end of G726_tone() ....................... */
3004
3005
3006 /*
3007 ----------------------------------------------------------------------
3008
3009 void G726_trans (short *td, long *yl, short *dq, short *tr);
3010 ~~~~~~~~~~~~~~~
3011
3012 Description:
3013 ~~~~~~~~~~~~
3014
3015 Transition detector.
3016
3017 Inputs: td, yl, dq
3018 Output: tr
3019
3020 Return value: none.
3021 ~~~~~~~~~~~~~
3022
3023 Prototype: in g726.h
3024 ~~~~~~~~~~
3025
3026 History:
3027 ~~~~~~~~
3028 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
3029 <tdsindi@venus.cpqd.ansp.br>
3030 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
3031 <tdsimao@venus.cpqd.ansp.br>
3032
3033 ----------------------------------------------------------------------
3034 */
3035 void G726_trans(td, yl, dq, tr)
3036 short *td;
3037 long *yl;
3038 short *dq, *tr;
3039 {
3040 short dqmag;
3041 long dqthr;
3042 short ylint;
3043 long dqmag1;
3044 short ylfrac;
3045 long thr1, thr2;
3046
3047 dqmag = *dq & 32767;
3048
3049 ylint = (*yl >> 15);
3050
3051 ylfrac = (*yl >> 10) & 31;
3052
3053 thr1 = (ylfrac + 32) << ylint;
3054
3055 thr2 = (ylint > 9) ? 31744 : thr1;
3056
3057 dqthr = (thr2 + (thr2 >> 1)) >> 1;
3058
3059 dqmag1 = dqmag;
3060
3061 *tr = (dqmag1 > dqthr && *td == 1) ? 1 : 0;
3062
3063 }
3064
3065 /* ....................... end of G726_trans() ....................... */
3066
3067
3068 /*
3069 ----------------------------------------------------------------------
3070
3071 void G726_compress (short *sr, char *law, short *sp);
3072 ~~~~~~~~~~~~~~~~~~
3073
3074 Description:
3075 ~~~~~~~~~~~~
3076
3077 Convert from uniform pcm to either a-law or u-law pcm
3078
3079 Inputs: sr, law
3080 Output: sp
3081
3082 Return value: none.
3083 ~~~~~~~~~~~~~
3084
3085 Prototype: in g726.h
3086 ~~~~~~~~~~
3087
3088 History:
3089 ~~~~~~~~
3090 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
3091 <tdsindi@venus.cpqd.ansp.br>
3092 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
3093 <tdsimao@venus.cpqd.ansp.br>
3094 24.Jan.00 v2.0 Corrected im calculation that caused incorrect
3095 processing of test vector ri40fa. Corrected
3096 code provided by Jayesh Patel <jayesh@dspse.com>.
3097 Verified by <simao.campos@labs.comsat.com>
3098 ----------------------------------------------------------------------
3099 */
3100 void G726_compress(sr, law, sp)
3101 short *sr;
3102 char *law;
3103 short *sp;
3104 {
3105 short imag, iesp, ofst;
3106 short ofst1;
3107 long i;
3108 long im;
3109 short is;
3110 long srr;
3111
3112 is = (*sr >> 15);
3113 srr = (*sr & 65535);
3114
3115 /* Convert 2-complement to signed magnitude */
3116 im = (is == 0) ? srr : ((65536 - srr) & 32767);
3117
3118 /* Compress ... */
3119 if (*law == '1') {
3120
3121 /* Next line added by J.Patel to fix a with test vector ri40fa.o */
3122 im = (*sr == -32768) ? 2 : im; /* *** */
3123
3124 imag = (is == 0) ? (im >> 1) : ((im + 1) >> 1);
3125
3126 if (is)
3127 --imag;
3128
3129 /* Saturation */
3130 if (imag > 4095)
3131 imag = 4095;
3132
3133 iesp = 7;
3134 for (i = 1; i <= 7; ++i) {
3135 imag += imag;
3136 if (imag >= 4096)
3137 break;
3138 iesp = 7 - i;
3139 }
3140
3141 imag &= 4095;
3142
3143 imag = (imag >> 8);
3144 *sp = (is == 0) ? imag + (iesp << 4) : imag + (iesp << 4) + 128;
3145
3146 /* Sign bit inversion */
3147 *sp ^= 128;
3148 } else {
3149 imag = im;
3150
3151 if (imag > 8158)
3152 imag = 8158; /* Saturation */
3153
3154 ++imag;
3155 iesp = 0;
3156 ofst = 31;
3157
3158 if (imag > ofst) {
3159 for (iesp = 1; iesp <= 8; ++iesp) {
3160 ofst1 = ofst;
3161 ofst += (1 << (iesp + 5));
3162 if (imag <= ofst)
3163 break;
3164 }
3165 imag -= ofst1 + 1;
3166 }
3167
3168 imag /= (1 << (iesp + 1));
3169
3170 *sp = (is == 0) ? (imag + (iesp << 4)) : (imag + (iesp << 4) + 128);
3171
3172 /* Sign bit inversion */
3173 *sp ^= 128;
3174 *sp ^= 127;
3175 }
3176
3177 }
3178
3179 /* ....................... end of G726_compress()....................... */
3180
3181
3182 /*
3183 ----------------------------------------------------------------------
3184
3185 void G726_sync (short rate, short *i, short *sp, short *dlnx,
3186 ~~~~~~~~~~~~~~ short *dsx, char *law, short *sd);
3187
3188 Description:
3189 ~~~~~~~~~~~~
3190
3191 Re-encode output pcm sample in decoder for synchronous tandem
3192 coding.
3193
3194 Inputs: i, sp, dlnx, dsx, law, rate
3195 Output: sd
3196
3197 Return value: none.
3198 ~~~~~~~~~~~~~
3199
3200 Prototype: in g726.h
3201 ~~~~~~~~~~
3202
3203 History:
3204 ~~~~~~~~
3205 31.Jan.91 v1.0f Release of 1st Fortran version to UGST.
3206 <tdsindi@venus.cpqd.ansp.br>
3207 13.Feb.92 v1.0c 1st version in C translated from Fortran (f2c)
3208 <tdsimao@venus.cpqd.ansp.br>
3209
3210 ----------------------------------------------------------------------
3211 */
3212 void G726_sync(rate, i, sp, dlnx, dsx, law, sd)
3213 short *i, *sp, *dlnx, *dsx;
3214 char *law;
3215 short *sd;
3216 short rate;
3217 {
3218 short mask, id, im, is, ss;
3219
3220 if (rate == 4) {
3221 is = (*i >> 3);
3222
3223 im = (is == 0) ? (*i + 8) : (*i & 7);
3224
3225 /* Find value of `id' as in Table 17/G.726 */
3226 if (*dlnx >= 3972)
3227 id = 9;
3228 else if (*dlnx >= 2048)
3229 id = 7;
3230 else if (*dlnx >= 400)
3231 id = 15;
3232 else if (*dlnx >= 349)
3233 id = 14;
3234 else if (*dlnx >= 300)
3235 id = 13;
3236 else if (*dlnx >= 246)
3237 id = 12;
3238 else if (*dlnx >= 178)
3239 id = 11;
3240 else if (*dlnx >= 80)
3241 id = 10;
3242 else
3243 id = 9;
3244
3245 /* Account for the negative part of the table */
3246 if (*dsx)
3247 id = 15 - id;
3248
3249 if (id == 8)
3250 id = 7;
3251 } /* ............... end of 32 kbit part
3252 * ................. */
3253 else if (rate == 3) {
3254 is = (*i >> 2);
3255
3256 im = (is == 0) ? (*i + 4) : (*i & 3);
3257
3258 /* Find value of `id' as in the Table 18/G.726 */
3259 if (*dlnx >= 2048)
3260 id = 3;
3261 else if (*dlnx >= 331)
3262 id = 7;
3263 else if (*dlnx >= 218)
3264 id = 6;
3265 else if (*dlnx >= 8)
3266 id = 5;
3267 else if (*dlnx >= 0)
3268 id = 3;
3269
3270
3271 if (*dsx)
3272 id = 7 - id;
3273
3274 if (id == 4)
3275 id = 3;
3276 } /* ............... end of 24 kbit part
3277 * ................. */
3278 else if (rate == 2) {
3279 is = (*i >> 1);
3280
3281 im = (is == 0) ? (*i + 2) : (*i & 1);
3282
3283 /* Find value of `id' as in the Table 19/G.726 */
3284 if (*dlnx >= 2048)
3285 id = 2;
3286 else if (*dlnx >= 261)
3287 id = 3;
3288 else if (*dlnx >= 0)
3289 id = 2;
3290
3291 if (*dsx)
3292 id = 3 - id;
3293
3294 } /* ............... end of 16 kbit part
3295 * ................. */
3296 else {
3297 is = (*i >> 4);
3298
3299 im = (is == 0) ? (*i + 16) : (*i & 15);
3300
3301 /* Find value of `id' as in the Table 16/G.726 */
3302
3303 if (*dlnx >= 4080)
3304 id = 18;
3305 else if (*dlnx >= 3974)
3306 id = 17;
3307 else if (*dlnx >= 2048)
3308 id = 15;
3309 else if (*dlnx >= 553)
3310 id = 31;
3311 else if (*dlnx >= 528)
3312 id = 30;
3313 else if (*dlnx >= 502)
3314 id = 29;
3315 else if (*dlnx >= 475)
3316 id = 28;
3317 else if (*dlnx >= 445)
3318 id = 27;
3319 else if (*dlnx >= 413)
3320 id = 26;
3321 else if (*dlnx >= 378)
3322 id = 25;
3323 else if (*dlnx >= 339)
3324 id = 24;
3325 else if (*dlnx >= 298)
3326 id = 23;
3327 else if (*dlnx >= 250)
3328 id = 22;
3329 else if (*dlnx >= 198)
3330 id = 21;
3331 else if (*dlnx >= 139)
3332 id = 20;
3333 else if (*dlnx >= 68)
3334 id = 19;
3335 else if (*dlnx >= 0)
3336 id = 18;
3337
3338 if (*dsx)
3339 id = 31 - id;
3340
3341 if (id == 16)
3342 id = 15;
3343
3344 } /* ............... end of 40 kbit part
3345 * ................. */
3346
3347 /* Choose sd as sp, sp+ or sp- */
3348
3349 ss = (*sp & 128) >> 7;
3350 mask = (*sp & 127);
3351
3352 if (*law == '1') { /* ......... A-law */
3353 if (id > im && ss == 1 && mask == 0)
3354 ss = 0;
3355 else if (id > im && ss == 1 && mask != 0)
3356 mask--;
3357 else if (id > im && ss == 0 && mask != 127)
3358 mask++;
3359 else if (id < im && ss == 1 && mask != 127)
3360 mask++;
3361 else if (id < im && ss == 0 && mask == 0)
3362 ss = 1;
3363 else if (id < im && ss == 0 && mask != 0)
3364 mask--;
3365 } else { /* ......... u-law */
3366 if (id > im && ss == 1 && mask == 127) {
3367 ss = 0;
3368 mask--;
3369 } else if (id > im && ss == 1 && mask != 127)
3370 mask++;
3371 else if (id > im && ss == 0 && mask != 0)
3372 mask--;
3373 else if (id < im && ss == 1 && mask != 0)
3374 mask--;
3375 else if (id < im && ss == 0 && mask == 127)
3376 ss = 1;
3377 else if (id < im && ss == 0 && mask != 127)
3378 mask++;
3379 }
3380
3381 *sd = mask + (ss << 7);
3382
3383 }
3384
3385 /* ........................ end of G726_sync() ........................ */
3386
3387 /* ************************* END OF G726.C ************************* */

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.