FreeCalypso > hg > vband-misc
annotate efr-sid/etsi-bit-rd.c @ 53:637ee027999b
efr-sid/Unit-test-desc: blurb written
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 12 Aug 2024 03:39:42 +0000 |
parents | 8bfc517fda3b |
children |
rev | line source |
---|---|
43
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * In this module we implement utility functions for reading ETSI *.cod |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * and *.dec files in either LE or BE format. |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdio.h> |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdint.h> |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdlib.h> |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <string.h> |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <strings.h> |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <gsm_efr.h> |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include "etsi.h" |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 read_etsi_bits(inf, big_endian, bitvec, nwords, filename_for_errs) |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 FILE *inf; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 uint8_t *bitvec; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 unsigned nwords; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 char *filename_for_errs; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 uint8_t file_bytes[ETSI_DEC_NWORDS * 2], *sp; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 int cc; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 unsigned n, upper; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 cc = fread(file_bytes, 2, nwords, inf); |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 if (cc == 0) |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 return 0; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (cc != nwords) { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 fprintf(stderr, "error: short read from %s\n", |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 filename_for_errs); |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 exit(1); |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 sp = file_bytes; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 for (n = 0; n < nwords; n++) { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 if (big_endian) { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 upper = sp[0]; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 bitvec[n] = sp[1]; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 } else { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 bitvec[n] = sp[0]; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 upper = sp[1]; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 } |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 if (upper) { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 fprintf(stderr, |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 "error in %s: non-zero in what should be %s upper byte\n", |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 filename_for_errs, big_endian ? "BE" : "LE"); |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 exit(1); |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 sp += 2; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 return 1; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 void |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 bits2frame(input_bits, frame, filename_for_errs, frame_no) |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 uint8_t *input_bits, *frame; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 char *filename_for_errs; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 unsigned frame_no; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 uint8_t bits[248], *sp, *dp; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 unsigned nb, byte, mask; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 bits[0] = 1; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 bits[1] = 1; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 bits[2] = 0; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 bits[3] = 0; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 bcopy(input_bits, bits + 4, 244); |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 sp = bits; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 dp = frame; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 for (nb = 0; nb < EFR_RTP_FRAME_LEN; nb++) { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 byte = 0; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 for (mask = 0x80; mask; mask >>= 1) { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 if (*sp > 1) { |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 fprintf(stderr, |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 "error in %s frame #%u: data bit > 1\n", |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 filename_for_errs, frame_no); |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 exit(1); |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 } |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 if (*sp) |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 byte |= mask; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 sp++; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 } |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 *dp++ = byte; |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 } |
8bfc517fda3b
efr-sid: hack created
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 } |