FreeCalypso > hg > gsm-codec-lib
annotate libgsmfrp/comfort_noise.c @ 87:41d8e8f4058d
libgsmefr: pstfilt2.c compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 26 Nov 2022 10:27:34 +0000 |
parents | 286d5f097eb4 |
children | 3b64f255689a |
rev | line source |
---|---|
4
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * In this module we implement comfort noise generation per GSM 06.12 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * or 3GPP TS 46.012. |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdint.h> |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <string.h> |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include "gsm_fr_preproc.h" |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include "internal.h" |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 static const uint8_t fold_table_8to6[24] = { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 1, 2, 3, 4, 5, 6, 1, 2, |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 1, 2, 3, 4, 5, 6, 3, 4, |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 1, 2, 3, 4, 5, 6, 5, 6, |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 }; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 static const uint8_t bc[4] = {0, 0, 0, 0}; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 static const uint8_t Nc[4] = {40, 120, 40, 120}; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 /* pseudonoise() function is based on ETSI EFR code */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 static uint16_t pseudonoise(struct gsmfr_preproc_state *st, uint16_t no_bits) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 uint16_t noise_bits, Sn, i; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 noise_bits = 0; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 for (i = 0; i < no_bits; i++) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 /* State n == 31 */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 if ((st->cn_random_lfsr & 0x00000001L) != 0) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 Sn = 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 else |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 Sn = 0; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 /* State n == 3 */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 if ((st->cn_random_lfsr & 0x10000000L) != 0) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 Sn = Sn ^ 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 else |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 Sn = Sn ^ 0; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 noise_bits = noise_bits << 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 noise_bits = noise_bits | st->cn_random_lfsr & 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 st->cn_random_lfsr >>= 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (Sn & 1) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 st->cn_random_lfsr |= 0x40000000L; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 return noise_bits; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 static uint8_t random_1to6(struct gsmfr_preproc_state *st) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 uint8_t range8, range6; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 range8 = pseudonoise(st, 3); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 range6 = fold_table_8to6[(st->cn_random_6fold << 3) | range8]; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 st->cn_random_6fold++; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 if (st->cn_random_6fold >= 3) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 st->cn_random_6fold = 0; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 return range6; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 void gsmfr_preproc_gen_cn(struct gsmfr_preproc_state *st, gsm_byte *frame) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 unsigned sub, pulse; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 uint8_t Mc, xmc[13]; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 gsm_byte *c; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 /* global bytes (magic and LARc) are fixed */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 memcpy(frame, st->sid_prefix, 5); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 c = frame + 5; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 /* now do the 4 subframes, mostly PRNG output */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 for (sub = 0; sub < 4; sub++) { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 Mc = pseudonoise(st, 2); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 for (pulse = 0; pulse < 13; pulse++) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 xmc[pulse] = random_1to6(st); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 /* packing code from libgsm */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 *c++ = ((Nc[sub] & 0x7F) << 1) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 | ((bc[sub] >> 1) & 0x1); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 *c++ = ((bc[sub] & 0x1) << 7) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 | ((Mc & 0x3) << 5) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 | ((st->sid_xmaxc[sub] >> 1) & 0x1F); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 *c++ = ((st->sid_xmaxc[sub] & 0x1) << 7) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 | ((xmc[0] & 0x7) << 4) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 | ((xmc[1] & 0x7) << 1) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 | ((xmc[2] >> 2) & 0x1); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 *c++ = ((xmc[2] & 0x3) << 6) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 | ((xmc[3] & 0x7) << 3) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 | (xmc[4] & 0x7); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 *c++ = ((xmc[5] & 0x7) << 5) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 | ((xmc[6] & 0x7) << 2) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 | ((xmc[7] >> 1) & 0x3); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 *c++ = ((xmc[7] & 0x1) << 7) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 | ((xmc[8] & 0x7) << 4) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 | ((xmc[9] & 0x7) << 1) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 | ((xmc[10] >> 2) & 0x1); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 *c++ = ((xmc[10] & 0x3) << 6) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 | ((xmc[11] & 0x7) << 3) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 | (xmc[12] & 0x7); |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 } |