FreeCalypso > hg > gsm-codec-lib
annotate dev/s2u-regen.c @ 226:84d22eb72196
dev: new program s2u-regen
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 23 Apr 2023 05:17:06 +0000 |
parents | dev/s2a-regen.c@3afbc6c64172 |
children | a5200ad12d58 |
rev | line source |
---|---|
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
226
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
2 * This program regenerates a G.711 mu-law encoding table equivalent to the |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
3 * s2u[] table in the toast_ulaw.c module in libgsm/toast; the intent is |
222
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 |
226
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
5 * the computation is based on ulaw_compress() from ITU-T G.191 STL. |
222
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 |
226
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
12 ulaw_compress(input) |
222
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 { |
226
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
15 short i; /* aux.var. */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
16 short absno; /* absolute value of linear (input) sample */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
17 short segno; /* segment (Table 2/G711, column 1) */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
18 short low_nibble; /* low nibble of log companded sample */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
19 short high_nibble; /* high nibble of log companded sample */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
20 unsigned output; |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
226
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
22 /* -------------------------------------------------------------------- */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
23 /* Change from 14 bit left justified to 14 bit right justified */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
24 /* Compute absolute value; adjust for easy processing */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
25 /* -------------------------------------------------------------------- */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
26 absno = input >= 4096 /* compute 1's complement in case of */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
27 ? ((~input & 4095) << 1) + 33 /* negative samples */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
28 : (input << 1) + 33; /* NB: 33 is the difference value */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
29 /* between the thresholds for */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
30 /* A-law and u-law. */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
31 if (absno > (0x1FFF)) /* limitation to "absno" < 8192 */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
32 absno = (0x1FFF); |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 |
226
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
34 /* Determination of sample's segment */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
35 i = absno >> 6; |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
36 segno = 1; |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
37 while (i != 0) { |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
38 segno++; |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
39 i >>= 1; |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
40 } |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
41 |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
42 /* Mounting the high-nibble of the log-PCM sample */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
43 high_nibble = (0x0008) - segno; |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 |
226
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
45 /* Mounting the low-nibble of the log PCM sample */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
46 low_nibble = (absno >> segno) /* right shift of mantissa and */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
47 &(0x000F); /* masking away leading '1' */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
48 low_nibble = (0x000F) - low_nibble; |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 |
226
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
50 /* Joining the high-nibble and the low-nibble of the log PCM sample */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
51 output = (high_nibble << 4) | low_nibble; |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
52 |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
53 /* Add sign bit */ |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
54 if (input < 4096) |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
55 output = output | (0x0080); |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
56 |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
57 return output; |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 main(argc, argv) |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 char **argv; |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 { |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 unsigned input, output; |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 |
226
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
65 for (input = 0; input < 8192; input++) { |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
66 output = ulaw_compress(input); |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
67 printf("%04o,", output); |
84d22eb72196
dev: new program s2u-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
68 if ((input % 15) == 14 || input == 8191) |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 putchar('\n'); |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 } |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 exit(0); |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 } |