annotate amrconv/efr2amr.c @ 490:4d80730683d4

libgsmhr1: implement TS 101 318 packing and unpacking
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 15 Jun 2024 05:33:35 +0000
parents b092a510141e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Our gsm-efr2amr utility reads in an EFR speech recording in our
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * extended-libgsm file format and converts it to AMR, inserting
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * an AMR NO_DATA frame in the place of every BFI in the input
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * but NOT performing any special handling or even detection of
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * EFR SID frames.
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 */
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdint.h>
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <strings.h>
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "../libtest/binreader.h"
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 extern const uint8_t amr_122_bit_order[244];
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 static const char amr_file_hdr[] = "#!AMR\n";
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 main(argc, argv)
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 char **argv;
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 {
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
23 char *infname, *outfname;
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 FILE *inf, *outf;
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
25 int wrong_flag;
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 uint8_t frm_in[BINFILE_MAX_FRAME], frm_out[32];
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 unsigned bit_a, bit_n, outlen;
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 int rc, bfi;
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
30 if (argc == 3 && argv[1][0] != '-') {
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
31 wrong_flag = 0;
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
32 infname = argv[1];
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
33 outfname = argv[2];
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
34 } else if (argc == 4 && !strcmp(argv[1], "-w")) {
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
35 wrong_flag = 1;
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
36 infname = argv[2];
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
37 outfname = argv[3];
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
38 } else {
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
39 fprintf(stderr, "usage: %s [-w] input.gsmx output.amr\n",
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
40 argv[0]);
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 exit(1);
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
43 inf = fopen(infname, "r");
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (!inf) {
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
45 perror(infname);
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 exit(1);
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
48 outf = fopen(outfname, "w");
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (!outf) {
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
50 perror(outfname);
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 exit(1);
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
104
7f43089e9a7a gsm-efr2amr: forgot AMR file header again
Mychaela Falconia <falcon@freecalypso.org>
parents: 103
diff changeset
53 fwrite(amr_file_hdr, 1, 6, outf);
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 for (;;) {
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 rc = binfile_read_frame(inf, frm_in);
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (rc < 0) {
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
57 fprintf(stderr, "error: garbage in %s\n", infname);
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 exit(1);
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (!rc)
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 break;
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (frm_in[0] == 0xBF)
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 bfi = 1;
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 else if ((frm_in[0] & 0xF0) == 0xC0)
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 bfi = 0;
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 else {
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 fprintf(stderr,
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 "error: %s is not in EFR codec format\n",
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
69 infname);
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 exit(1);
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (bfi) {
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 frm_out[0] = 0x78;
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 outlen = 1;
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 } else {
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 frm_out[0] = 0x3C;
107
41f1ae68d253 amr2efr & efr2amr cosmetic: cleaner approach to bit reshuffling
Mychaela Falconia <falcon@freecalypso.org>
parents: 106
diff changeset
77 frm_out[31] = 0;
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 for (bit_a = 0; bit_a < 244; bit_a++) {
106
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
79 if (wrong_flag)
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
80 bit_n = bit_a;
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
81 else
e7c8d739c4c8 gsm-efr2amr: implement -w option like in gsm-amr2efr
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
82 bit_n = amr_122_bit_order[bit_a];
103
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 msb_set_bit(frm_out + 1, bit_a,
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 msb_get_bit(frm_in, 4 + bit_n));
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 }
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 outlen = 32;
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 }
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 fwrite(frm_out, 1, outlen, outf);
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 fclose(outf);
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 exit(0);
0123ca1f1402 gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }