FreeCalypso > hg > gsm-codec-lib
annotate efrtest/etsi-enc.c @ 147:90b9c7c3fa3b
gsmefr-cod-parse: use factored-out ETSI bit reader
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 14 Dec 2022 08:09:52 +0000 |
parents | ff0372186b59 |
children | a13b1605142b |
rev | line source |
---|---|
114
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * gsmefr-etsi-enc is a test program for our EFR encoder: it reads raw |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * 16-bit PCM (matching ETSI's *.inp) as input and writes ETSI's *.cod |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * format as output, allowing our decoder to be tested with ETSI's |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * official test sequences. |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 * |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 * ETSI input and output files are read and written in LE byte order, |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 * matching the official test sequences in ts_100725v050200p0.zip. |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 */ |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdio.h> |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <stdint.h> |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <stdlib.h> |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include <string.h> |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 #include <strings.h> |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 #include "../libgsmefr/gsm_efr.h" |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 static int |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 read_input(inf, pcm, filename_for_errs) |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 FILE *inf; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 int16_t *pcm; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 char *filename_for_errs; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 uint8_t file_bytes[320], *sp; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 int cc; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 unsigned n; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 cc = fread(file_bytes, 2, 160, inf); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 if (cc == 0) |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 return 0; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 if (cc != 160) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 fprintf(stderr, "error: short read from %s\n", |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 filename_for_errs); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 exit(1); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 sp = file_bytes; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 for (n = 0; n < 160; n++) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 pcm[n] = sp[0] | (sp[1] << 8); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 sp += 2; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 return 1; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 static void |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 frame2bits(frame, bits) |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 uint8_t *frame, *bits; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 unsigned nb, byte, mask, bit; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 for (nb = 0; nb < EFR_RTP_FRAME_LEN; nb++) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 byte = *frame++; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 for (mask = 0x80; mask; mask >>= 1) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (byte & mask) |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 bit = 1; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 else |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 bit = 0; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 *bits++ = bit; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 static void |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 emit_output(outf, bits, nbits) |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 FILE *outf; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 uint8_t *bits; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 unsigned nbits; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 unsigned n; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 for (n = 0; n < nbits; n++) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 putc(bits[n], outf); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 putc(0, outf); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 main(argc, argv) |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 char **argv; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 char *infname, *outfname; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 FILE *inf, *outf; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 struct EFR_encoder_state *state; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 int16_t pcm[160]; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 uint8_t frame[EFR_RTP_FRAME_LEN], bits[250]; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 int dtx, rc, sp, vad; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 if (argc == 3 && argv[1][0] != '-') { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 dtx = 0; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 infname = argv[1]; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 outfname = argv[2]; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 } else if (argc == 4 && !strcmp(argv[1], "-d")) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 dtx = 1; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 infname = argv[2]; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 outfname = argv[3]; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 } else { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 fprintf(stderr, "usage: %s [-d] input.inp output.cod\n", |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 argv[0]); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 exit(1); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 inf = fopen(infname, "r"); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 if (!inf) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 perror(infname); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 exit(1); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 outf = fopen(outfname, "w"); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 if (!outf) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 perror(outfname); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 exit(1); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 state = EFR_encoder_create(dtx); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 if (!state) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 perror("EFR_encoder_create()"); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 exit(1); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
114 for (;;) { |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
115 rc = read_input(inf, pcm, argv[1]); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 if (!rc) |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
117 break; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 EFR_encode_frame(state, pcm, frame, &sp, &vad); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
119 frame2bits(frame, bits); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
120 bits[248] = vad; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
121 bits[249] = sp; |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
122 emit_output(outf, bits + 4, 246); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
123 } |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
124 fclose(outf); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
125 exit(0); |
ff0372186b59
gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
126 } |