annotate dev/s2a-regen.c @ 288:e0b46ac2c326

gsmfr-encode-r: convert to libgsmfr2
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Apr 2024 06:20:04 +0000
parents 3afbc6c64172
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
222
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program regenerates a G.711 A-law encoding table equivalent to the
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * s2a[] table in the toast_alaw.c module in libgsm/toast; the intent is
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * to check that table for correctness. The "engine" function that does
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * the computation is based on alaw_compress() from ITU-T G.191 STL.
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 static unsigned
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 alaw_compress (input)
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 unsigned input;
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 {
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 short ix, iexp;
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 ix = input >= 2048 /* 0 <= ix < 2048 */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 ? ~input & 2047 /* 1's complement for negative values */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 : input;
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 /* Do more, if exponent > 0 */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if (ix > 15) { /* exponent=0 for ix <= 15 */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 iexp = 1; /* first step: */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 while (ix > 16 + 15) { /* find mantissa and exponent */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 ix >>= 1;
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 iexp++;
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 }
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 ix -= 16; /* second step: remove leading '1' */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 ix += iexp << 4; /* now compute encoded value */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (input < 2048)
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 ix |= (0x0080); /* add sign bit */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 return ix ^ (0x0055); /* toggle even bits */
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 main(argc, argv)
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 char **argv;
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 {
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 unsigned input, output;
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 for (input = 0; input < 4096; input++) {
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if ((input & 15) == 0)
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 putchar('\t');
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 output = alaw_compress(input);
223
3afbc6c64172 dev/s2a-regen.c: printf format to match original table
Mychaela Falconia <falcon@freecalypso.org>
parents: 222
diff changeset
47 printf("%3u,", output);
222
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 if ((input & 15) == 15)
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 putchar('\n');
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 exit(0);
842136bbd0da dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }