FreeCalypso > hg > gsm-codec-lib
annotate amrconv/cod2ietf.c @ 307:6b33f3ba4289
libtwamr: go for single-chunk-state approach
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 16 Apr 2024 17:18:04 +0000 |
parents | 4c4649a5fec3 |
children |
rev | line source |
---|---|
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
2 * This program converts an AMR-encoded speech recording from the .cod |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
3 * format used by 3GPP test sequences into the more common .amr format |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
4 * of IETF RFC 4867. |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 */ |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdio.h> |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdint.h> |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdlib.h> |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <string.h> |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <strings.h> |
211
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
12 #include "amr_defs.h" |
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
13 |
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
14 extern unsigned amr_bit_lengths[9]; |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
15 extern const char amr_file_hdr[IETF_HDR_LEN]; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
16 extern const uint8_t extra_bytes_per_ft[9]; |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 main(argc, argv) |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 char **argv; |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 { |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
21 char *infname, *outfname; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
22 FILE *inf, *outf; |
148
bbe5669f0f29
gsmefr-cod-parse: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
147
diff
changeset
|
23 int big_endian; |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
24 unsigned frame_no, type, mode, outlen; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
25 uint8_t input_bits[COD_FORMAT_NWORDS], frm_out[MAX_IF1_BYTES+1]; |
211
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
26 int rc; |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
28 if (argc == 3 && argv[1][0] != '-') { |
148
bbe5669f0f29
gsmefr-cod-parse: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
147
diff
changeset
|
29 big_endian = 0; |
bbe5669f0f29
gsmefr-cod-parse: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
147
diff
changeset
|
30 infname = argv[1]; |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
31 outfname = argv[2]; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
32 } else if (argc == 4 && !strcmp(argv[1], "-b")) { |
148
bbe5669f0f29
gsmefr-cod-parse: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
147
diff
changeset
|
33 big_endian = 1; |
bbe5669f0f29
gsmefr-cod-parse: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
147
diff
changeset
|
34 infname = argv[2]; |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
35 outfname = argv[3]; |
148
bbe5669f0f29
gsmefr-cod-parse: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
147
diff
changeset
|
36 } else { |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
37 fprintf(stderr, "usage: %s [-b] input.cod output.amr\n", |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
38 argv[0]); |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 exit(1); |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 } |
148
bbe5669f0f29
gsmefr-cod-parse: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
147
diff
changeset
|
41 inf = fopen(infname, "r"); |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 if (!inf) { |
148
bbe5669f0f29
gsmefr-cod-parse: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
147
diff
changeset
|
43 perror(infname); |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 exit(1); |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 } |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
46 outf = fopen(outfname, "w"); |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
47 if (!outf) { |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
48 perror(outfname); |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
49 exit(1); |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
50 } |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
51 fwrite(amr_file_hdr, 1, IETF_HDR_LEN, outf); |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 for (frame_no = 0; ; frame_no++) { |
211
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
53 rc = read_cod_bits(inf, big_endian, input_bits, infname); |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 if (!rc) |
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 break; |
211
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
56 type = input_bits[0]; |
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
57 mode = input_bits[245]; |
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
58 switch (type) { |
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
59 case TX_SPEECH_GOOD: |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
60 if (mode > MR122) { |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
61 invalid_mode: fprintf(stderr, |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
62 "error in %s frame #%u: invalid mode\n", |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
63 infname, frame_no); |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
64 exit(1); |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
65 } |
211
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
66 preen_frame_bits(input_bits+1, amr_bit_lengths[mode], |
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
67 infname, frame_no); |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
68 frm_out[0] = (mode << 3) | 0x04; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
69 amr_if1_pack(frm_out+1, input_bits+1, mode); |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
70 outlen = extra_bytes_per_ft[mode] + 1; |
211
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
71 break; |
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
72 case TX_SID_FIRST: |
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
73 case TX_SID_UPDATE: |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
74 if (mode > MR122) |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
75 goto invalid_mode; |
211
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
76 preen_frame_bits(input_bits+1, AMR_NBITS_SID, infname, |
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
77 frame_no); |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
78 frm_out[0] = 0x44; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
79 amr_if1_pack(frm_out+1, input_bits+1, MRDTX); |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
80 if (type == TX_SID_UPDATE) |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
81 frm_out[5] |= 0x10; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
82 if (mode & 1) |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
83 frm_out[5] |= 0x08; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
84 if (mode & 2) |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
85 frm_out[5] |= 0x04; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
86 if (mode & 4) |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
87 frm_out[5] |= 0x02; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
88 outlen = 6; |
211
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
89 break; |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
90 case TX_NO_DATA: |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
91 frm_out[0] = 0x7C; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
92 outlen = 1; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
93 break; |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
94 default: |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
95 fprintf(stderr, "error in %s frame #%u: invalid type\n", |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
96 infname, frame_no); |
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
97 exit(1); |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 } |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
99 fwrite(frm_out, 1, outlen, outf); |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 } |
215
4c4649a5fec3
amrconv: new program amr-cod2ietf
Mychaela Falconia <falcon@freecalypso.org>
parents:
212
diff
changeset
|
101 fclose(outf); |
211
78d1a6513393
amrconv: new program amr-cod-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
148
diff
changeset
|
102 exit(0); |
115
5a63294fa321
gsmefr-cod-parse test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 } |