comparison 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
comparison
equal deleted inserted replaced
225:ba737a0203e2 226:84d22eb72196
1 /*
2 * This program regenerates a G.711 mu-law encoding table equivalent to the
3 * s2u[] table in the toast_ulaw.c module in libgsm/toast; the intent is
4 * to check that table for correctness. The "engine" function that does
5 * the computation is based on ulaw_compress() from ITU-T G.191 STL.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 static unsigned
12 ulaw_compress(input)
13 unsigned input;
14 {
15 short i; /* aux.var. */
16 short absno; /* absolute value of linear (input) sample */
17 short segno; /* segment (Table 2/G711, column 1) */
18 short low_nibble; /* low nibble of log companded sample */
19 short high_nibble; /* high nibble of log companded sample */
20 unsigned output;
21
22 /* -------------------------------------------------------------------- */
23 /* Change from 14 bit left justified to 14 bit right justified */
24 /* Compute absolute value; adjust for easy processing */
25 /* -------------------------------------------------------------------- */
26 absno = input >= 4096 /* compute 1's complement in case of */
27 ? ((~input & 4095) << 1) + 33 /* negative samples */
28 : (input << 1) + 33; /* NB: 33 is the difference value */
29 /* between the thresholds for */
30 /* A-law and u-law. */
31 if (absno > (0x1FFF)) /* limitation to "absno" < 8192 */
32 absno = (0x1FFF);
33
34 /* Determination of sample's segment */
35 i = absno >> 6;
36 segno = 1;
37 while (i != 0) {
38 segno++;
39 i >>= 1;
40 }
41
42 /* Mounting the high-nibble of the log-PCM sample */
43 high_nibble = (0x0008) - segno;
44
45 /* Mounting the low-nibble of the log PCM sample */
46 low_nibble = (absno >> segno) /* right shift of mantissa and */
47 &(0x000F); /* masking away leading '1' */
48 low_nibble = (0x000F) - low_nibble;
49
50 /* Joining the high-nibble and the low-nibble of the log PCM sample */
51 output = (high_nibble << 4) | low_nibble;
52
53 /* Add sign bit */
54 if (input < 4096)
55 output = output | (0x0080);
56
57 return output;
58 }
59
60 main(argc, argv)
61 char **argv;
62 {
63 unsigned input, output;
64
65 for (input = 0; input < 8192; input++) {
66 output = ulaw_compress(input);
67 printf("%04o,", output);
68 if ((input % 15) == 14 || input == 8191)
69 putchar('\n');
70 }
71 exit(0);
72 }