-
Notifications
You must be signed in to change notification settings - Fork 14
/
dsd_file_.c
320 lines (277 loc) · 8.03 KB
/
dsd_file_.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright (C) 2010 DSD Author
* GPG Key ID: 0x3F1D7FD0 (74EF 430D F7F2 0A48 FCE6 F630 FAA2 635D 3F1D 7FD0)
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsd.h"
#include "mbe_const.h"
void saveAmbe2450Data (dsd_opts * opts, dsd_state * state, char *ambe_d)
{
int i, j, k;
unsigned char b, buf[8];
unsigned char err;
err = (unsigned char) state->errs2;
buf[0] = err;
k = 0;
for (i = 0; i < 6; i++) {
b = 0;
for (j = 0; j < 8; j++) {
b = b << 1;
b = b + ambe_d[k];
k++;
}
buf[i+1] = b;
}
b = ambe_d[48];
buf[7] = b;
write(opts->mbe_out_fd, buf, 8);
}
void saveImbe4400Data (dsd_opts * opts, dsd_state * state, char *imbe_d)
{
int i, j, k;
unsigned char b, buf[12];
unsigned char err;
err = (unsigned char) state->errs2;
buf[0] = err;
k = 0;
for (i = 0; i < 11; i++) {
b = 0;
for (j = 0; j < 8; j++) {
b = b << 1;
b = b + imbe_d[k];
k++;
}
buf[i+1] = b;
}
write(opts->mbe_out_fd, buf, 12);
}
static void mbe_demodulateImbe7200x4400Data (char imbe[8][23])
{
int i, j = 0, k;
unsigned short pr[115], foo = 0;
// create pseudo-random modulator
for (i = 11; i >= 0; i--) {
foo <<= 1;
foo |= imbe[0][11+i];
}
pr[0] = (16 * foo);
for (i = 1; i < 115; i++) {
pr[i] = (173 * pr[i - 1]) + 13849 - (65536 * (((173 * pr[i - 1]) + 13849) >> 16));
}
for (i = 1; i < 115; i++) {
pr[i] >>= 15;
}
// demodulate imbe with pr
k = 1;
for (i = 1; i < 4; i++) {
for (j = 22; j >= 0; j--)
imbe[i][j] ^= pr[k++];
}
for (i = 4; i < 7; i++) {
for (j = 14; j >= 0; j--)
imbe[i][j] ^= pr[k++];
}
}
static int mbe_eccImbe7200x4400Data (char imbe_fr[8][23], char *imbe_d)
{
int i, j = 0, errs = 0;
unsigned int hin, block;
char *imbe = imbe_d;
for (j = 11; j >= 0; j--) {
*imbe++ = imbe_fr[0][j+11];
}
for (i = 1; i < 4; i++) {
block = 0;
for (j = 22; j >= 0; j--) {
block <<= 1;
block |= imbe_fr[i][j];
}
errs += Golay23_CorrectAndGetErrCount(&block);
for (j = 0; j < 12; j++) {
*imbe++ = ((block >> j) & 1);
}
}
for (i = 4; i < 7; i++) {
hin = 0;
for (j = 0; j < 15; j++) {
hin <<= 1;
hin |= imbe_fr[i][14-j];
}
block = hin;
p25_Hamming15_11_3_Correct(&block);
errs += ((hin >> 4) != block);
for (j = 14; j >= 4; j--) {
*imbe++ = ((block & 0x0400) >> 10);
block = block << 1;
}
}
for (j = 6; j >= 0; j--) {
*imbe++ = imbe_fr[7][j];
}
return (errs);
}
void process_IMBE (dsd_opts* opts, dsd_state* state, unsigned char imbe_dibits[72])
{
unsigned int i, dibit;
char imbe_fr[8][23];
for (i = 0; i < 72; i++) {
dibit = imbe_dibits[i];
imbe_fr[iW[i]][iX[i]] = (1 & (dibit >> 1)); // bit 1
imbe_fr[iY[i]][iZ[i]] = (1 & dibit); // bit 0
}
//if (state->p25kid == 0)
{
// Check for a non-standard c0 transmitted. This is explained here: https://github.com/szechyjs/dsd/issues/24
unsigned int nsw = 0x00038000;
unsigned int block = 0;
int j;
for (j = 22; j >= 0; j--) {
block <<= 1;
block |= imbe_fr[0][j];
}
if (block == nsw) {
// Skip this particular value. If we let it pass it will be signaled as an erroneus IMBE
printf("(Non-standard IMBE c0 detected, skipped)\n");
} else {
char imbe_d[88];
state->errs2 = Golay23_CorrectAndGetErrCount(&block);
for (j = 11; j >= 0; j--) {
imbe_fr[0][j+11] = (block & 0x0800) >> 11;
block <<= 1;
}
for (j = 0; j < 88; j++) {
imbe_d[j] = 0;
}
mbe_demodulateImbe7200x4400Data (imbe_fr);
state->errs2 += mbe_eccImbe7200x4400Data (imbe_fr, imbe_d);
processIMBEFrame (opts, state, imbe_d);
}
}
}
void demodAmbe3600x24x0Data (int *errs2, char ambe_fr[4][24], char *ambe_d)
{
int i, j, k;
unsigned int block = 0, errs = 0;
unsigned short pr[115], foo;
char *ambe = ambe_d;
for (j = 22; j >= 0; j--) {
block <<= 1;
block |= ambe_fr[0][j+1];
}
errs = Golay23_CorrectAndGetErrCount(&block);
// create pseudo-random modulator
foo = block;
pr[0] = (16 * foo);
for (i = 1; i < 24; i++) {
pr[i] = (173 * pr[i - 1]) + 13849 - (65536 * (((173 * pr[i - 1]) + 13849) >> 16));
}
for (i = 1; i < 24; i++) {
pr[i] >>= 15;
}
// just copy C0
for (j = 11; j >= 0; j--) {
*ambe++ = ((block >> j) & 1);
}
// demodulate C1 with pr
// then, ecc and copy C1
k = 1;
for (j = 22; j >= 0; j--) {
block <<= 1;
block |= (ambe_fr[1][j] ^ pr[k++]);
}
errs += Golay23_CorrectAndGetErrCount(&block);
for (j = 11; j >= 0; j--) {
*ambe++ = ((block >> j) & 1);
}
// just copy C2
for (j = 10; j >= 0; j--) {
*ambe++ = ambe_fr[2][j];
}
// just copy C3
for (j = 13; j >= 0; j--) {
*ambe++ = ambe_fr[3][j];
}
*errs2 = errs;
}
void mbe_printVersion (char *str)
{
strcpy(str, "Dummy mbelib, .amb/.imb file write only, no decoding to audio supported");
}
void mbe_initMbeParms (mbe_parms * cur_mp, mbe_parms * prev_mp, mbe_parms * prev_mp_enhanced)
{
}
void
processAMBEFrame (dsd_opts * opts, dsd_state * state, unsigned char ambe_dibits[36])
{
unsigned int i, dibit;
char ambe_fr[4][24];
char ambe_d[49];
for (i = 0; i < 36; i++) {
dibit = ambe_dibits[i];
ambe_fr[rW[i]][rX[i]] = (1 & (dibit >> 1)); // bit 1
ambe_fr[rY[i]][rZ[i]] = (1 & dibit); // bit 0
}
demodAmbe3600x24x0Data (&state->errs2, ambe_fr, ambe_d);
if (opts->mbe_out_fd != -1) {
saveAmbe2450Data (opts, state, ambe_d);
}
state->debug_audio_errors += state->errs2;
}
void
processIMBEFrame (dsd_opts * opts, dsd_state * state, char imbe_d[88])
{
if (opts->mbe_out_fd != -1) {
saveImbe4400Data (opts, state, imbe_d);
}
state->debug_audio_errors += state->errs2;
}
void
closeMbeOutFile (dsd_opts * opts, dsd_state * state)
{
unsigned int is_imbe = !(state->synctype & ~1U); // Write IMBE magic if synctype == 0 or 1
char new_path[1024];
time_t tv_sec;
struct tm timep;
int result;
if (opts->mbe_out_fd != -1) {
tv_sec = opts->mbe_out_last_timeval;
close(opts->mbe_out_fd);
opts->mbe_out_fd = -1;
gmtime_r(&tv_sec, &timep);
snprintf (new_path, 1023, "%s/nac0-%04u-%02u-%02u-%02u:%02u:%02u-tg%u-src%u.%cmb", opts->mbe_out_dir,
timep.tm_year + 1900, timep.tm_mon + 1, timep.tm_mday,
timep.tm_hour, timep.tm_min, timep.tm_sec, state->talkgroup, state->radio_id, (is_imbe ? 'i' : 'a'));
result = rename (opts->mbe_out_path, new_path);
}
}
void
openMbeOutFile (dsd_opts * opts, dsd_state * state)
{
struct timeval tv;
unsigned int is_imbe = !(state->synctype & ~1U); // Write IMBE magic if synctype == 0 or 1
char magic[4] = { '.', 'a', 'm', 'b' };
gettimeofday (&tv, NULL);
opts->mbe_out_last_timeval = tv.tv_sec;
snprintf (opts->mbe_out_path, 1023, "%s/%ld.%cmb", opts->mbe_out_dir, tv.tv_sec, (is_imbe ? 'i' : 'a'));
if ((opts->mbe_out_fd = open (opts->mbe_out_path, O_WRONLY | O_CREAT, 0644)) < 0) {
printf ("Error, couldn't open %s\n", opts->mbe_out_path);
return;
}
// write magic
// 0x626D612E / 0x626D692E
if ((state->synctype == 0) || (state->synctype == 1)) {
magic[1] = 'i';
}
write (opts->mbe_out_fd, magic, 4);
}