comparison miscutil/efr2amr.c @ 106:e7c8d739c4c8

gsm-efr2amr: implement -w option like in gsm-amr2efr
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 27 Nov 2022 06:25:06 +0000
parents 7f43089e9a7a
children 41f1ae68d253
comparison
equal deleted inserted replaced
105:ecfbced76fea 106:e7c8d739c4c8
18 static const char amr_file_hdr[] = "#!AMR\n"; 18 static const char amr_file_hdr[] = "#!AMR\n";
19 19
20 main(argc, argv) 20 main(argc, argv)
21 char **argv; 21 char **argv;
22 { 22 {
23 char *infname, *outfname;
23 FILE *inf, *outf; 24 FILE *inf, *outf;
25 int wrong_flag;
24 uint8_t frm_in[BINFILE_MAX_FRAME], frm_out[32]; 26 uint8_t frm_in[BINFILE_MAX_FRAME], frm_out[32];
25 unsigned bit_a, bit_n, outlen; 27 unsigned bit_a, bit_n, outlen;
26 int rc, bfi; 28 int rc, bfi;
27 29
28 if (argc != 3) { 30 if (argc == 3 && argv[1][0] != '-') {
29 fprintf(stderr, "usage: %s input.gsmx output.amr\n", argv[0]); 31 wrong_flag = 0;
32 infname = argv[1];
33 outfname = argv[2];
34 } else if (argc == 4 && !strcmp(argv[1], "-w")) {
35 wrong_flag = 1;
36 infname = argv[2];
37 outfname = argv[3];
38 } else {
39 fprintf(stderr, "usage: %s [-w] input.gsmx output.amr\n",
40 argv[0]);
30 exit(1); 41 exit(1);
31 } 42 }
32 inf = fopen(argv[1], "r"); 43 inf = fopen(infname, "r");
33 if (!inf) { 44 if (!inf) {
34 perror(argv[1]); 45 perror(infname);
35 exit(1); 46 exit(1);
36 } 47 }
37 outf = fopen(argv[2], "w"); 48 outf = fopen(outfname, "w");
38 if (!outf) { 49 if (!outf) {
39 perror(argv[2]); 50 perror(outfname);
40 exit(1); 51 exit(1);
41 } 52 }
42 fwrite(amr_file_hdr, 1, 6, outf); 53 fwrite(amr_file_hdr, 1, 6, outf);
43 for (;;) { 54 for (;;) {
44 rc = binfile_read_frame(inf, frm_in); 55 rc = binfile_read_frame(inf, frm_in);
45 if (rc < 0) { 56 if (rc < 0) {
46 fprintf(stderr, "error: garbage in %s\n", argv[1]); 57 fprintf(stderr, "error: garbage in %s\n", infname);
47 exit(1); 58 exit(1);
48 } 59 }
49 if (!rc) 60 if (!rc)
50 break; 61 break;
51 if (frm_in[0] == 0xBF) 62 if (frm_in[0] == 0xBF)
53 else if ((frm_in[0] & 0xF0) == 0xC0) 64 else if ((frm_in[0] & 0xF0) == 0xC0)
54 bfi = 0; 65 bfi = 0;
55 else { 66 else {
56 fprintf(stderr, 67 fprintf(stderr,
57 "error: %s is not in EFR codec format\n", 68 "error: %s is not in EFR codec format\n",
58 argv[1]); 69 infname);
59 exit(1); 70 exit(1);
60 } 71 }
61 if (bfi) { 72 if (bfi) {
62 frm_out[0] = 0x78; 73 frm_out[0] = 0x78;
63 outlen = 1; 74 outlen = 1;
64 } else { 75 } else {
65 frm_out[0] = 0x3C; 76 frm_out[0] = 0x3C;
66 bzero(frm_out + 1, 31); 77 bzero(frm_out + 1, 31);
67 for (bit_a = 0; bit_a < 244; bit_a++) { 78 for (bit_a = 0; bit_a < 244; bit_a++) {
68 bit_n = amr_122_bit_order[bit_a]; 79 if (wrong_flag)
80 bit_n = bit_a;
81 else
82 bit_n = amr_122_bit_order[bit_a];
69 msb_set_bit(frm_out + 1, bit_a, 83 msb_set_bit(frm_out + 1, bit_a,
70 msb_get_bit(frm_in, 4 + bit_n)); 84 msb_get_bit(frm_in, 4 + bit_n));
71 } 85 }
72 outlen = 32; 86 outlen = 32;
73 } 87 }