comparison efrtest/etsi-dec.c @ 96:9cf1355bc071

gsmefr-etsi-dec test program written
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 26 Nov 2022 19:55:59 +0000
parents
children e5ee684c6d29
comparison
equal deleted inserted replaced
95:f80a3202c8ff 96:9cf1355bc071
1 /*
2 * gsmefr-etsi-dec is a test program for our EFR decoder: it reads ETSI's
3 * .dec format as input and writes raw 16-bit PCM (same as ETSI's *.out)
4 * as output, allowing our decoder to be tested with ETSI's official test
5 * sequences.
6 *
7 * ETSI input and output files are read and written in LE byte order,
8 * matching the official test sequences in ts_100725v050200p0.zip.
9 */
10
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <strings.h>
16 #include "../libgsmefr/gsm_efr.h"
17
18 #define ETSI_DEC_NWORDS 247
19
20 static int
21 read_input(inf, bitvec, filename_for_errs)
22 FILE *inf;
23 uint8_t *bitvec;
24 char *filename_for_errs;
25 {
26 uint8_t file_bytes[ETSI_DEC_NWORDS * 2], *sp;
27 int cc;
28 unsigned n;
29
30 cc = fread(file_bytes, 2, ETSI_DEC_NWORDS, inf);
31 if (cc == 0)
32 return 0;
33 if (cc != ETSI_DEC_NWORDS) {
34 fprintf(stderr, "error: short read from %s\n",
35 filename_for_errs);
36 exit(1);
37 }
38 sp = file_bytes;
39 for (n = 0; n < ETSI_DEC_NWORDS; n++) {
40 if (sp[1]) {
41 fprintf(stderr,
42 "error in %s: non-zero in what should be LE upper byte\n",
43 filename_for_errs);
44 exit(1);
45 }
46 bitvec[n] = sp[0];
47 sp += 2;
48 }
49 return 1;
50 }
51
52 static void
53 bits2frame(input_bits, frame, filename_for_errs, frame_no)
54 uint8_t *input_bits, *frame;
55 char *filename_for_errs;
56 unsigned frame_no;
57 {
58 uint8_t bits[248], *sp, *dp;
59 unsigned nb, byte, mask;
60
61 bits[0] = 1;
62 bits[1] = 1;
63 bits[2] = 0;
64 bits[3] = 0;
65 bcopy(input_bits, bits + 4, 244);
66 sp = bits;
67 dp = frame;
68 for (nb = 0; nb < EFR_RTP_FRAME_LEN; nb++) {
69 byte = 0;
70 for (mask = 0x80; mask; mask >>= 1) {
71 if (*sp > 1) {
72 fprintf(stderr,
73 "error in %s frame #%u: data bit > 1\n",
74 filename_for_errs, frame_no);
75 exit(1);
76 }
77 if (*sp)
78 byte |= mask;
79 sp++;
80 }
81 *dp++ = byte;
82 }
83 }
84
85 static void
86 write_pcm_le(outf, pcm)
87 FILE *outf;
88 int16_t *pcm;
89 {
90 uint8_t bytes[320], *dp;
91 int16_t samp;
92 unsigned n;
93
94 dp = bytes;
95 for (n = 0; n < 160; n++) {
96 samp = pcm[n];
97 *dp++ = samp & 0xFF;
98 *dp++ = (samp >> 8) & 0xFF;
99 }
100 fwrite(bytes, 2, 160, outf);
101 }
102
103 main(argc, argv)
104 char **argv;
105 {
106 FILE *inf, *outf;
107 struct EFR_decoder_state *state;
108 unsigned frame_no;
109 uint8_t input_bits[ETSI_DEC_NWORDS], frame[EFR_RTP_FRAME_LEN];
110 int16_t pcm[160];
111 int rc;
112
113 if (argc != 3) {
114 fprintf(stderr, "usage: %s input.dec output.out\n", argv[0]);
115 exit(1);
116 }
117 inf = fopen(argv[1], "r");
118 if (!inf) {
119 perror(argv[1]);
120 exit(1);
121 }
122 outf = fopen(argv[2], "w");
123 if (!outf) {
124 perror(argv[2]);
125 exit(1);
126 }
127 state = EFR_decoder_create();
128 if (!state) {
129 perror("EFR_decoder_create()");
130 exit(1);
131 }
132 for (frame_no = 0; ; frame_no++) {
133 rc = read_input(inf, input_bits, argv[1]);
134 if (!rc)
135 break;
136 if (input_bits[0] > 1) {
137 fprintf(stderr, "error in %s frame #%u: BFI > 1\n",
138 argv[1], frame_no);
139 exit(1);
140 }
141 bits2frame(input_bits, frame, argv[1], frame_no);
142 if (input_bits[245] > 2) {
143 fprintf(stderr, "error in %s frame #%u: SID > 2\n",
144 argv[1], frame_no);
145 exit(1);
146 }
147 if (input_bits[246] > 1) {
148 fprintf(stderr, "error in %s frame #%u: TAF > 1\n",
149 argv[1], frame_no);
150 exit(1);
151 }
152 rc = EFR_sid_classify(frame);
153 if (input_bits[245] != rc) {
154 fprintf(stderr,
155 "warning: frame #%u has mismatching SID (file says %u, analysis yields %d)\n",
156 frame_no, input_bits[245], rc);
157 }
158 EFR_decode_frame(state, frame, input_bits[0], input_bits[246],
159 pcm);
160 write_pcm_le(outf, pcm);
161 }
162 fclose(outf);
163 exit(0);
164 }