forked from f4exb/dsdcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsd_main.cpp
443 lines (412 loc) · 15.6 KB
/
dsd_main.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016 Edouard Griffiths, F4EXB. //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include "dsd_decoder.h"
#include "dsd_upsample.h"
#ifdef DSD_USE_SERIALDV
#include "dvcontroller.h"
#endif
int exitflag;
static void usage ();
static void sigfun (int sig);
void usage()
{
fprintf(stderr, "\n");
fprintf(stderr, "Usage:\n");
fprintf(stderr, " dsd [options] Live scanner mode\n");
fprintf(stderr, " dsd -h Show help\n");
fprintf(stderr, "\n");
fprintf(stderr, "Display Options:\n");
fprintf(stderr, " -e Show Frame Info and errorbars (default)\n");
fprintf(stderr, " -pe Show P25 encryption sync bits\n");
fprintf(stderr, " -pl Show P25 link control bits\n");
fprintf(stderr, " -ps Show P25 status bits and low speed data\n");
fprintf(stderr, " -pt Show P25 talkgroup info\n");
fprintf(stderr, " -q Don't show Frame Info/errorbars\n");
fprintf(stderr, " -s Datascope (disables other display options)\n");
fprintf(stderr, " -t Show symbol timing during sync\n");
fprintf(stderr, " -v <num> Frame information Verbosity\n");
fprintf(stderr, " -z <num> Frame rate for datascope\n");
fprintf(stderr, "\n");
fprintf(stderr, "Input/Output options:\n");
fprintf(stderr,
" -i <device> Audio input device (default is /dev/audio, - for piped stdin)\n");
fprintf(stderr, " -o <device> Audio output device (default is /dev/audio, - for stdout)\n");
fprintf(stderr, " -g <num> Audio output gain (default = 0 = auto, disable = -1)\n");
fprintf(stderr, " -U <num> Audio output upsampling\n");
fprintf(stderr, " 0: no upsampling (8k) default\n");
fprintf(stderr, " 6: normal upsampling to 48k\n");
fprintf(stderr, " 7: 7x upsampling to trade audio drops against bad audio quality\n");
fprintf(stderr, " -n Do not send synthesized speech to audio output device\n");
fprintf(stderr, " -L <filename> Log messages to file with file name <filename>. Default is stderr\n");
fprintf(stderr, " If file name is invalid messages will go to stderr\n");
#ifdef DSD_USE_SERIALDV
fprintf(stderr, " -D <device> Use DVSI AMBE3000 based device for AMBE decoding (e.g. ThumbDV)\n");
fprintf(stderr, " You must have compiled with serialDV support (see Readme.md)\n");
fprintf(stderr, " Device name is the corresponding TTY USB device e.g /dev/ttyUSB0\n");
#endif
fprintf(stderr, "\n");
fprintf(stderr, "Scanner control options:\n");
fprintf(stderr,
" -R <num> Resume scan after <num> TDULC frames or any PDU or TSDU\n");
fprintf(stderr, "\n");
fprintf(stderr, "Decoder options:\n");
fprintf(stderr, " -d <num> Set data rate:");
fprintf(stderr, " 0 2400 bauds");
fprintf(stderr, " 1 4800 bauds (default)");
fprintf(stderr, " 2 9800 bauds");
fprintf(stderr, " -fa Auto-detect frame type (default)\n");
fprintf(stderr, " -fr Decode only DMR/MOTOTRBO\n");
fprintf(stderr, " -fd Decode only D-STAR\n");
fprintf(stderr, " -f1 Decode only P25 Phase 1\n");
fprintf(stderr, " -fi Decode only NXDN48 (6.25 kHz) / IDAS*\n");
fprintf(stderr, " -fn Decode only NXDN96 (12.5 kHz)\n");
fprintf(stderr, " -fp Decode only ProVoice\n");
fprintf(stderr, " -fx Decode only X2-TDMA\n");
fprintf(stderr, " -fm Decode only DPMR Tier 1 or 2 (6.25 kHz)\n");
fprintf(stderr, " -fy Decode only YSF\n");
fprintf(stderr, " -l Disable DMR/MOTOTRBO and NXDN input filtering\n");
fprintf(stderr, " -pu Unmute Encrypted P25\n");
fprintf(stderr, " -u <num> Unvoiced speech quality (default=3)\n");
fprintf(stderr, "\n");
fprintf(stderr, "Advanced decoder options:\n");
fprintf(stderr,
" -A <num> QPSK modulation auto detection threshold (default=26)\n");
fprintf(stderr,
" -S <num> Symbol buffer size for QPSK decision point tracking\n");
fprintf(stderr, " (default=36)\n");
fprintf(stderr,
" -M <num> Min/Max buffer size for QPSK decision point tracking\n");
fprintf(stderr, " (default=15)\n");
exit(0);
}
void sigfun(int sig)
{
exitflag = 1;
signal(SIGINT, SIG_DFL);
}
int main(int argc, char **argv)
{
int c;
extern char *optarg;
extern int optind, opterr, optopt;
DSDcc::DSDDecoder dsdDecoder;
DSDcc::DSDUpsampler upsamplingEngine;
char in_file[1023];
int in_file_fd = -1;
char out_file[1023];
int out_file_fd = -1;
char log_file[1023];
log_file[0] = '\0';
char serialDevice[16];
std::string dvSerialDevice;
int dvGain_dB = 0;
fprintf(stderr, "Digital Speech Decoder DSDcc\n");
exitflag = 0;
signal(SIGINT, sigfun);
while ((c = getopt(argc, argv,
"hep:qstv:z:i:o:g:nR:f:u:U:A:S:M:lL:D:d:")) != -1)
{
opterr = 0;
switch (c)
{
case 'h':
usage();
exit(0);
case 'e':
dsdDecoder.showErrorBars();
break;
case 'p':
if (optarg[0] == 'e')
{
dsdDecoder.setP25DisplayOptions(DSDcc::DSDDecoder::DSDShowP25EncryptionSyncBits, true);
}
else if (optarg[0] == 'l')
{
dsdDecoder.setP25DisplayOptions(DSDcc::DSDDecoder::DSDShowP25LinkControlBits, true);
}
else if (optarg[0] == 's')
{
dsdDecoder.setP25DisplayOptions(DSDcc::DSDDecoder::DSDShowP25EncryptionSyncBits, true);
}
else if (optarg[0] == 't')
{
dsdDecoder.setP25DisplayOptions(DSDcc::DSDDecoder::DSDShowP25TalkGroupInfo, true);
}
else if (optarg[0] == 'u')
{
dsdDecoder.muteEncryptedP25(false);
}
break;
case 'q':
dsdDecoder.setQuiet();
break;
case 's':
dsdDecoder.showDatascope();
break;
case 't':
dsdDecoder.showSymbolTiming();
break;
case 'v':
int verbosity;
sscanf(optarg, "%d", &verbosity);
dsdDecoder.setLogVerbosity(verbosity);
break;
case 'z':
int frameRate;
sscanf(optarg, "%d", &frameRate);
dsdDecoder.setDatascopeFrameRate(frameRate);
break;
case 'L':
strncpy(log_file, (const char *) optarg, 1023);
log_file[1023] = '\0';
break;
case 'i':
strncpy(in_file, (const char *) optarg, 1023);
in_file[1023] = '\0';
break;
case 'o':
strncpy(out_file, (const char *) optarg, 1023);
out_file[1023] = '\0';
break;
#ifdef DSD_USE_SERIALDV
case 'D':
strncpy(serialDevice, (const char *) optarg, 16);
serialDevice[16] = '\0';
dvSerialDevice = serialDevice;
break;
#endif
case 'g':
float gain;
sscanf(optarg, "%f", &gain);
dsdDecoder.setAudioGain(gain);
#ifdef DSD_USE_SERIALDV
if (gain > 0) {
dvGain_dB = (int) (10.0f * log10f(gain));
}
#endif
break;
case 'n':
dsdDecoder.enableAudioOut(false);
break;
case 'R':
int resume;
sscanf(optarg, "%d", &resume);
dsdDecoder.enableScanResumeAfterTDULCFrames(resume);
break;
case 'd':
int dataRateIndex;
sscanf(optarg, "%d", &dataRateIndex);
if ((dataRateIndex >= 0) && (dataRateIndex <= 2))
{
dsdDecoder.setDataRate((DSDcc::DSDDecoder::DSDRate) dataRateIndex);
}
break;
case 'f':
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeNone, true);
if (optarg[0] == 'a') // auto detect
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeAuto, true);
}
else if (optarg[0] == 'r') // DMR/MOTOTRBO
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeDMR, true);
}
else if (optarg[0] == 'd') // D-Star
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeDStar, true);
}
else if (optarg[0] == 'x') // X2-TDMA
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeX2TDMA, true);
}
else if (optarg[0] == 'p') // ProVoice
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeProVoice, true);
}
else if (optarg[0] == '0') // P25 Phase 1
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeP25P1, true);
}
else if (optarg[0] == 'i') // NXDN48 IDAS
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeNXDN48, true);
}
else if (optarg[0] == 'n') // NXDN96
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeNXDN96, true);
}
else if (optarg[0] == 'm') // DPMR Tier 1 or 2
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeDPMR, true);
}
else if (optarg[0] == 'y') // YSF
{
dsdDecoder.setDecodeMode(DSDcc::DSDDecoder::DSDDecodeYSF, true);
}
break;
case 'u':
int uvquality;
sscanf(optarg, "%i", &uvquality);
dsdDecoder.setUvQuality(uvquality);
break;
case 'U':
int upsampling;
sscanf(optarg, "%d", &upsampling);
dsdDecoder.setUpsampling(upsampling);
break;
case 'A':
int threshold;
sscanf(optarg, "%i", &threshold);
dsdDecoder.setAutoDetectionThreshold(threshold);
break;
case 'S':
int ssize;
sscanf(optarg, "%i", &ssize);
dsdDecoder.setQPSKSymbolBufferSize(ssize);
break;
case 'M':
int msize;
sscanf(optarg, "%i", &msize);
dsdDecoder.setQPSKMinMaxBufferSize(msize);
break;
case 'l':
dsdDecoder.enableCosineFiltering(false);
break;
default:
usage();
exit(0);
}
}
if (strlen(log_file) > 0) {
dsdDecoder.setLogFile(log_file);
}
if (strncmp(in_file, (const char *) "-", 1) == 0)
{
in_file_fd = STDIN_FILENO;
}
else
{
in_file_fd = open(in_file, O_RDONLY);
}
if (in_file_fd > -1)
{
fprintf(stderr, "Opened %s for input.\n", in_file);
}
else
{
fprintf(stderr, "Cannot open %s for input. Aborting\n", in_file);
return 0;
}
if (strncmp(out_file, (const char *) "-", 1) == 0)
{
out_file_fd = STDOUT_FILENO;
}
else
{
out_file_fd = open(out_file, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
}
if (out_file_fd > -1)
{
fprintf(stderr, "Opened %s for output.\n", in_file);
}
else
{
if (in_file_fd != STDIN_FILENO) {
close(in_file_fd);
}
fprintf(stderr, "Cannot open %s for output. Aborting\n", out_file);
return 0;
}
#ifdef DSD_USE_SERIALDV
SerialDV::DVController dvController;
short dvAudioSamples[SerialDV::MBE_AUDIO_BLOCK_SIZE * 8];
unsigned char dvMbeSamples[SerialDV::MBE_FRAME_LENGTH_BYTES];
if (!dvSerialDevice.empty())
{
if (dvController.open(dvSerialDevice))
{
fprintf(stderr, "SerialDV up. Disable mbelib support\n");
dsdDecoder.enableMbelib(false); // de-activate mbelib decoding
}
}
#endif
while (exitflag == 0)
{
short sample;
int nbAudioSamples;
short *audioSamples;
int result = read(in_file_fd, (void *) &sample, sizeof(short));
if (result == 0)
{
fprintf(stderr, "No more input\n");
break;
}
dsdDecoder.run(sample);
#ifdef DSD_USE_SERIALDV
if (dvController.isOpen())
{
if (dsdDecoder.mbeDVReady1())
{
dvController.decode(dvAudioSamples, (const unsigned char *) dsdDecoder.getMbeDVFrame1(), (SerialDV::DVRate) dsdDecoder.getMbeRate(), dvGain_dB);
if (dsdDecoder.upsampling())
{
upsamplingEngine.upsample(dsdDecoder.upsampling(), dvAudioSamples, &dvAudioSamples[SerialDV::MBE_AUDIO_BLOCK_SIZE], SerialDV::MBE_AUDIO_BLOCK_SIZE);
result = write(out_file_fd, (const void *) &dvAudioSamples[SerialDV::MBE_AUDIO_BLOCK_SIZE], SerialDV::MBE_AUDIO_BLOCK_BYTES * dsdDecoder.upsampling());
}
else
{
result = write(out_file_fd, (const void *) dvAudioSamples, SerialDV::MBE_AUDIO_BLOCK_BYTES); // TODO: upsampling
}
dsdDecoder.resetMbeDV1();
}
}
else
#endif
{
audioSamples = dsdDecoder.getAudio1(nbAudioSamples);
if (nbAudioSamples > 0)
{
result = write(out_file_fd, (const void *) audioSamples, sizeof(short) * nbAudioSamples);
if (result == -1) {
fprintf(stderr, "Error writing to output\n");
} else if (result != sizeof(short) * nbAudioSamples) {
fprintf(stderr, "Written %d out of %d audio samples\n", result/2, nbAudioSamples);
}
dsdDecoder.resetAudio1();
}
}
}
fprintf(stderr, "End of process\n");
#ifdef DSD_USE_SERIALDV
if (dvController.isOpen()) {
dvController.close();
}
#endif
if ((out_file_fd > -1) && (out_file_fd != STDOUT_FILENO)) {
close(out_file_fd);
}
if ((in_file_fd > -1) && (in_file_fd != STDIN_FILENO)) {
close(in_file_fd);
}
return (0);
}