FreeCalypso > hg > gsm-codec-lib
annotate dev/a2s-regen.c @ 242:f081a6850fb5
libgsmfrp: new refined implementation
The previous implementation exhibited the following defects,
which are now fixed:
1) The last received valid SID was cached forever for the purpose of
handling future invalid SIDs - we could have received some valid
SID ages ago, then lots of speech or NO_DATA, and if we then get
an invalid SID, we would resurrect the last valid SID from ancient
history - a bad design. In our new design, we handle invalid SID
based on the current state, much like BFI.
2) GSM 06.11 spec says clearly that after the second lost SID
(received BFI=1 && TAF=1 in CN state) we need to gradually decrease
the output level, rather than jump directly to emitting silence
frames - we previously failed to implement such logic.
3) Per GSM 06.12 section 5.2, Xmaxc should be the same in all 4 subframes
in a SID frame. What should we do if we receive an otherwise valid
SID frame with different Xmaxc? Our previous approach would
replicate this Xmaxc oddity in every subsequent generated CN frame,
which is rather bad. In our new design, the very first CN frame
(which can be seen as a transformation of the SID frame itself)
retains the original 4 distinct Xmaxc, but all subsequent CN frames
are based on the Xmaxc from the last subframe of the most recent SID.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 09 May 2023 05:16:31 +0000 |
parents | ba737a0203e2 |
children |
rev | line source |
---|---|
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
224
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
2 * This program regenerates a G.711 A-law decoding table equivalent to the |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
3 * a2s[] table in the toast_alaw.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 |
224
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
5 * the computation is based on alaw_expand() 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 |
224
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
12 alaw_expand (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 { |
224
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
15 short ix, mant, iexp; |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
16 |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
17 ix = input ^ (0x0055); /* re-toggle toggled bits */ |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
224
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
19 ix &= (0x007F); /* remove sign bit */ |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
20 iexp = ix >> 4; /* extract exponent */ |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
21 mant = ix & (0x000F); /* now get mantissa */ |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
22 if (iexp > 0) |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
23 mant = mant + 16; /* add leading '1', if exponent > 0 */ |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 |
224
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
25 mant = (mant << 4) + (0x0008); /* now mantissa left justified and */ |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
26 /* 1/2 quantization step added */ |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
27 if (iexp > 1) /* now left shift according exponent */ |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
28 mant = mant << (iexp - 1); |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 |
224
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
30 if (input & 0x80) /* invert, if negative sample */ |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
31 return mant; |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
32 else |
225
ba737a0203e2
dev/a2s-regen.c: fix generation of negative outputs
Mychaela Falconia <falcon@freecalypso.org>
parents:
224
diff
changeset
|
33 return -mant & 0xFFFF; |
222
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 |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 main(argc, argv) |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 char **argv; |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 { |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 unsigned input, output; |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 |
224
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
41 for (input = 0; input < 256; input++) { |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
42 output = alaw_expand(input); |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
43 printf("%6u,", output); |
b502321000aa
dev: new program a2s-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
223
diff
changeset
|
44 if ((input & 7) == 7) |
222
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 putchar('\n'); |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 exit(0); |
842136bbd0da
dev: new program s2a-regen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |