2
|
1
|
|
2 /*
|
|
3 RFC3551:
|
|
4
|
|
5 name of sampling default
|
|
6 encoding sample/frame bits/sample rate ms/frame ms/packet
|
|
7 __________________________________________________________________
|
|
8 GSM frame N/A 8,000 20 20
|
|
9
|
|
10
|
|
11 field field name bits field field name bits
|
|
12 ________________________________________________
|
|
13 1 LARc[0] 6 39 xmc[22] 3
|
|
14 2 LARc[1] 6 40 xmc[23] 3
|
|
15 3 LARc[2] 5 41 xmc[24] 3
|
|
16 4 LARc[3] 5 42 xmc[25] 3
|
|
17 5 LARc[4] 4 43 Nc[2] 7
|
|
18 6 LARc[5] 4 44 bc[2] 2
|
|
19 7 LARc[6] 3 45 Mc[2] 2
|
|
20 8 LARc[7] 3 46 xmaxc[2] 6
|
|
21 9 Nc[0] 7 47 xmc[26] 3
|
|
22 10 bc[0] 2 48 xmc[27] 3
|
|
23 11 Mc[0] 2 49 xmc[28] 3
|
|
24 12 xmaxc[0] 6 50 xmc[29] 3
|
|
25 13 xmc[0] 3 51 xmc[30] 3
|
|
26 14 xmc[1] 3 52 xmc[31] 3
|
|
27 15 xmc[2] 3 53 xmc[32] 3
|
|
28 16 xmc[3] 3 54 xmc[33] 3
|
|
29 17 xmc[4] 3 55 xmc[34] 3
|
|
30 18 xmc[5] 3 56 xmc[35] 3
|
|
31 19 xmc[6] 3 57 xmc[36] 3
|
|
32 20 xmc[7] 3 58 xmc[37] 3
|
|
33 21 xmc[8] 3 59 xmc[38] 3
|
|
34 22 xmc[9] 3 60 Nc[3] 7
|
|
35 23 xmc[10] 3 61 bc[3] 2
|
|
36 24 xmc[11] 3 62 Mc[3] 2
|
|
37 25 xmc[12] 3 63 xmaxc[3] 6
|
|
38 26 Nc[1] 7 64 xmc[39] 3
|
|
39 27 bc[1] 2 65 xmc[40] 3
|
|
40 28 Mc[1] 2 66 xmc[41] 3
|
|
41 29 xmaxc[1] 6 67 xmc[42] 3
|
|
42 30 xmc[13] 3 68 xmc[43] 3
|
|
43 31 xmc[14] 3 69 xmc[44] 3
|
|
44 32 xmc[15] 3 70 xmc[45] 3
|
|
45 33 xmc[16] 3 71 xmc[46] 3
|
|
46 34 xmc[17] 3 72 xmc[47] 3
|
|
47 35 xmc[18] 3 73 xmc[48] 3
|
|
48 36 xmc[19] 3 74 xmc[49] 3
|
|
49 37 xmc[20] 3 75 xmc[50] 3
|
|
50 38 xmc[21] 3 76 xmc[51] 3
|
|
51
|
|
52 Table 2: Ordering of GSM variables
|
|
53
|
|
54
|
|
55 Octet Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7
|
|
56 _____________________________________________________________________
|
|
57 0 1 1 0 1 LARc0.0 LARc0.1 LARc0.2 LARc0.3
|
|
58 1 LARc0.4 LARc0.5 LARc1.0 LARc1.1 LARc1.2 LARc1.3 LARc1.4 LARc1.5
|
|
59 2 LARc2.0 LARc2.1 LARc2.2 LARc2.3 LARc2.4 LARc3.0 LARc3.1 LARc3.2
|
|
60 3 LARc3.3 LARc3.4 LARc4.0 LARc4.1 LARc4.2 LARc4.3 LARc5.0 LARc5.1
|
|
61 4 LARc5.2 LARc5.3 LARc6.0 LARc6.1 LARc6.2 LARc7.0 LARc7.1 LARc7.2
|
|
62 5 Nc0.0 Nc0.1 Nc0.2 Nc0.3 Nc0.4 Nc0.5 Nc0.6 bc0.0
|
|
63 6 bc0.1 Mc0.0 Mc0.1 xmaxc00 xmaxc01 xmaxc02 xmaxc03 xmaxc04
|
|
64 7 xmaxc05 xmc0.0 xmc0.1 xmc0.2 xmc1.0 xmc1.1 xmc1.2 xmc2.0
|
|
65 8 xmc2.1 xmc2.2 xmc3.0 xmc3.1 xmc3.2 xmc4.0 xmc4.1 xmc4.2
|
|
66 9 xmc5.0 xmc5.1 xmc5.2 xmc6.0 xmc6.1 xmc6.2 xmc7.0 xmc7.1
|
|
67 10 xmc7.2 xmc8.0 xmc8.1 xmc8.2 xmc9.0 xmc9.1 xmc9.2 xmc10.0
|
|
68 11 xmc10.1 xmc10.2 x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maxc30 xmaxc31 xmaxc32 xmaxc33 xmaxc34
|
|
85 28 xmaxc35 xmc39.0 xmc39.1 xmc39.2 xmc40.0 xmc40.1 xmc40.2 xmc41.0
|
|
86 29 xmc41.1 xmc41.2 xmc42.0 xmc42.1 xmc42.2 xmc43.0 xmc43.1 xmc43.2
|
|
87 30 xmc44.0 xmc44.1 xmc44.2 xmc45.0 xmc45.1 xmc45.2 xmc46.0 xmc46.1
|
|
88 31 xmc46.2 xmc47.0 xmc47.1 xmc47.2 xmc48.0 xmc48.1 xmc48.2 xmc49.0
|
|
89 32 xmc49.1 xmc49.2 xmc50.0 xmc50.1 xmc50.2 xmc51.0 xmc51.1 xmc51.2
|
|
90
|
|
91 Table 3: GSM payload format
|
|
92
|
|
93 In the GSM packing used by RTP, the bits SHALL be packed beginning
|
|
94 from the most significant bit. Every 160 sample GSM frame is coded
|
|
95 into one 33 octet (264 bit) buffer. Every such buffer begins with a
|
|
96 4 bit signature (0xD), followed by the MSB encoding of the fields of
|
|
97 the frame. The first octet thus contains 1101 in the 4 most
|
|
98 significant bits (0-3) and the 4 most significant bits of F1 (0-3) in
|
|
99 the 4 least significant bits (4-7).
|
|
100
|
|
101
|
|
102 PT encoding media type clock rate channels
|
|
103 name (Hz)
|
|
104 ___________________________________________________
|
|
105 3 GSM A 8,000 1
|
|
106
|
|
107 */
|
|
108
|
|
109 typedef unsigned char Byte;
|
|
110
|
|
111 static Byte byte;
|
|
112 static unsigned mask;
|
|
113 static Byte *packed;
|
|
114 static int gsmbits[76] = {
|
|
115 6, 6, 5, 5, 4, 4, 3, 3, 7, 2, 2, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
116 3, 3, 7, 2, 2, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
117 3, 3, 3, 3, 7, 2, 2, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 2,
|
|
118 2, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
|
|
119 };
|
|
120
|
|
121 static void pack(short data, int len)
|
|
122 {
|
|
123 unsigned srcmask;
|
|
124
|
|
125 for (srcmask = (1 << len-1); srcmask > 0; srcmask /= 2) {
|
|
126 if (data & srcmask) {
|
|
127 byte += mask;
|
|
128 }
|
|
129 mask /= 2; // shift right
|
|
130 if (0 == mask) {
|
|
131 *packed = byte;
|
|
132 ++packed;
|
|
133 mask = 128;
|
|
134 byte = 0;
|
|
135 }
|
|
136 }
|
|
137 }
|
|
138
|
|
139 int gsmpack(Byte *gsm_frame, short *rpe_frame)
|
|
140 {
|
|
141 int i;
|
|
142
|
|
143 byte = 0;
|
|
144 mask = 128;
|
|
145 packed = gsm_frame;
|
|
146
|
|
147 pack(0xD, 4); // GSM signature
|
|
148 for (i = 0; i < 76; ++i, ++rpe_frame) {
|
|
149 pack(*rpe_frame, gsmbits[i]);
|
|
150 }
|
|
151 *packed = byte;
|
|
152
|
|
153 return 33;
|
|
154 }
|
|
155
|
|
156 static short unpack(int len)
|
|
157 {
|
|
158 unsigned srcmask;
|
|
159 short data = 0;
|
|
160
|
|
161 for (srcmask = (1 << len-1); srcmask > 0; srcmask /= 2) {
|
|
162 if ((*packed) & mask) {
|
|
163 data += srcmask;
|
|
164 }
|
|
165 mask /= 2; // shift right
|
|
166 if (0 == mask) {
|
|
167 ++packed;
|
|
168 mask = 128;
|
|
169 }
|
|
170 }
|
|
171
|
|
172 // fprintf(stderr, "data =%x\n", data);
|
|
173 return data;
|
|
174 }
|
|
175
|
|
176 int gsmunpack(short *rpe_frame, Byte *gsm_frame)
|
|
177 {
|
|
178 int i;
|
|
179
|
|
180 byte = 0;
|
|
181 mask = 128;
|
|
182 packed = gsm_frame;
|
|
183
|
|
184 if (unpack(4) != 0xD) {
|
|
185 // fprintf(stderr, "error: gsmunpack\n");
|
|
186 return 0; // not GSM signature
|
|
187 }
|
|
188 for (i = 0; i < 76; ++i, ++rpe_frame) {
|
|
189 *rpe_frame = unpack(gsmbits[i]);
|
|
190 }
|
|
191 // exit(1) ;
|
|
192 return 160; // number of PCM samples
|
|
193 }
|
|
194
|
|
195
|
|
196
|