comparison trau-decode/trau-sync8.c @ 27:f508dacf2cf9

trau-decode: new programs dump-1bit and trau-sync8
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 18 Aug 2024 06:26:23 +0000
parents trau-decode/parse-main.c@4d1732e4a143
children
comparison
equal deleted inserted replaced
26:f80e64139670 27:f508dacf2cf9
1 /*
2 * This program reads a 64 kbit/s timeslot recording file, examines one
3 * of the eight 8 kbit/s subslots (selected), looks for the sync pattern of
4 * GSM 08.61 (HRv1 speech or HR data), and dumps each synced frame
5 * as a hex line.
6 */
7
8 #include <sys/types.h>
9 #include <sys/file.h>
10 #include <sys/stat.h>
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <strings.h>
16 #include <unistd.h>
17
18 static uint8_t *filebuf;
19 static unsigned total_size;
20
21 static void
22 read_ts_file(filename, subslot_arg)
23 char *filename, *subslot_arg;
24 {
25 FILE *inf;
26 struct stat st;
27 int subslot, right_shift;
28 unsigned n;
29 uint8_t *dp;
30 int b;
31
32 inf = fopen(filename, "r");
33 if (!inf) {
34 perror(filename);
35 exit(1);
36 }
37 fstat(fileno(inf), &st);
38 if (!S_ISREG(st.st_mode)) {
39 fprintf(stderr, "error: %s is not a regular file\n", filename);
40 exit(1);
41 }
42 total_size = st.st_size;
43 if (total_size < 160) {
44 fprintf(stderr, "error: %s is too short\n", filename);
45 exit(1);
46 }
47 filebuf = malloc(total_size);
48 if (!filebuf) {
49 perror("malloc of file size");
50 exit(1);
51 }
52 subslot = atoi(subslot_arg);
53 if (subslot < 0 || subslot > 7) {
54 fprintf(stderr, "error: invalid subslot argument\n");
55 exit(1);
56 }
57 right_shift = 7 - subslot;
58 dp = filebuf;
59 for (n = 0; n < total_size; n++) {
60 b = getc(inf);
61 if (b < 0) {
62 fprintf(stderr,
63 "error: getc() returned EOF contrary to st_size\n");
64 exit(1);
65 }
66 *dp++ = (b >> right_shift) & 1;
67 }
68 fclose(inf);
69 }
70
71 static int
72 check_sync(pos)
73 unsigned pos;
74 {
75 uint8_t *cand = filebuf + pos;
76 unsigned n;
77
78 for (n = 0; n < 8; n++) {
79 if (cand[n])
80 return 0;
81 }
82 if (!cand[8])
83 return 0;
84 if (cand[16])
85 return 0;
86 if (!cand[17])
87 return 0;
88 for (n = 3; n < 20; n++) {
89 if (!cand[n * 8])
90 return 0;
91 }
92 return 1;
93 }
94
95 static void
96 process_frame(pos)
97 unsigned pos;
98 {
99 uint8_t *sp = filebuf + pos;
100 unsigned n, m, d;
101
102 for (n = 0; n < 40; n++) {
103 d = 0;
104 for (m = 0; m < 4; m++) {
105 d <<= 1;
106 d |= *sp++;
107 }
108 printf("%x", d);
109 }
110 putchar('\n');
111 }
112
113 static void
114 process_filebuf()
115 {
116 unsigned p, endp;
117 int sync = 0, match;
118
119 endp = total_size - 160;
120 for (p = 0; p < endp; ) {
121 match = check_sync(p);
122 if (match != sync) {
123 printf("# %s frame sync at file offset 0x%x\n",
124 match ? "Acquired" : "Lost", p);
125 }
126 if (match) {
127 process_frame(p);
128 if (!filebuf[p+158] && !filebuf[p+159]) {
129 printf(
130 "# both T bits equal 0, shifting frame alignment\n");
131 p += 158;
132 } else
133 p += 160;
134 } else
135 p++;
136 sync = match;
137 }
138 }
139
140 main(argc, argv)
141 char **argv;
142 {
143 if (argc != 3) {
144 fprintf(stderr, "usage: %s binfile subslot\n", argv[0]);
145 exit(1);
146 }
147 read_ts_file(argv[1], argv[2]);
148 process_filebuf();
149 exit(0);
150 }