annotate amrconv/amr2efr.c @ 492:cc3a831712a4

libgsmhr1: implement arbitrary RTP input
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 15 Jun 2024 06:52:37 +0000
parents b092a510141e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Our gsm-amr2efr utility reads in a speech recording in .amr format
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * (the mode must be MR122 for every frame, no DTX) and converts it into
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * our ad hoc .gsmx format for EFR. The conversion is a bit reshuffling
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * only, no transcoding takes place.
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdint.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <string.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern const uint8_t amr_122_bit_order[244];
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
102
dd88f9312170 gsm-amr2efr: forgot about AMR file header
Mychaela Falconia <falcon@freecalypso.org>
parents: 101
diff changeset
16 static const char amr_file_hdr[] = "#!AMR\n";
dd88f9312170 gsm-amr2efr: forgot about AMR file header
Mychaela Falconia <falcon@freecalypso.org>
parents: 101
diff changeset
17
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 main(argc, argv)
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char **argv;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
21 char *infname, *outfname;
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 FILE *inf, *outf;
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
23 int wrong_flag, hdrb;
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 uint8_t frm_in[31], frm_out[31];
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 unsigned bit_a, bit_n;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
27 if (argc == 3 && argv[1][0] != '-') {
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
28 wrong_flag = 0;
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
29 infname = argv[1];
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
30 outfname = argv[2];
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
31 } else if (argc == 4 && !strcmp(argv[1], "-w")) {
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
32 wrong_flag = 1;
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
33 infname = argv[2];
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
34 outfname = argv[3];
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
35 } else {
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
36 fprintf(stderr, "usage: %s [-w] input.amr output.gsmx\n",
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
37 argv[0]);
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
40 inf = fopen(infname, "r");
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (!inf) {
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
42 perror(infname);
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
102
dd88f9312170 gsm-amr2efr: forgot about AMR file header
Mychaela Falconia <falcon@freecalypso.org>
parents: 101
diff changeset
45 if (fread(frm_in, 1, 6, inf) != 6 || bcmp(frm_in, amr_file_hdr, 6)) {
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
46 fprintf(stderr, "error: %s is not in AMR format\n", infname);
102
dd88f9312170 gsm-amr2efr: forgot about AMR file header
Mychaela Falconia <falcon@freecalypso.org>
parents: 101
diff changeset
47 exit(1);
dd88f9312170 gsm-amr2efr: forgot about AMR file header
Mychaela Falconia <falcon@freecalypso.org>
parents: 101
diff changeset
48 }
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
49 outf = fopen(outfname, "w");
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (!outf) {
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
51 perror(outfname);
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 for (;;) {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 hdrb = getc(inf);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (hdrb < 0)
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 break;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if ((hdrb & 0x78) != 0x38) {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 fprintf(stderr, "error: unexpected content in %s\n",
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
60 infname);
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (fread(frm_in, 1, 31, inf) != 31) {
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
64 fprintf(stderr, "error: short read from %s\n", infname);
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 frm_out[0] = 0xC0;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 for (bit_a = 0; bit_a < 244; bit_a++) {
105
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
69 if (wrong_flag)
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
70 bit_n = bit_a;
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
71 else
ecfbced76fea gsm-amr2efr: add -w option to simulate common wrong implementation
Mychaela Falconia <falcon@freecalypso.org>
parents: 102
diff changeset
72 bit_n = amr_122_bit_order[bit_a];
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 msb_set_bit(frm_out, 4 + bit_n,
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 msb_get_bit(frm_in, bit_a));
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 fwrite(frm_out, 1, 31, outf);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 fclose(outf);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 exit(0);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }