comparison libgsmfrp/comfort_noise.c @ 4:286d5f097eb4

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