annotate amrconv/ietf-parse.c @ 357:fb001496ca8c

libtwamr: integrate dec_lag3.c
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 05 May 2024 22:29:09 +0000
parents 934cf92a1c45
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
214
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program reads an AMR recording in IETF RFC 4867 format and parses it
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * into a human-readable form.
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdint.h>
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "amr_defs.h"
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 extern char *amr_mode_names[16];
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern const char amr_file_hdr[IETF_HDR_LEN];
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 extern const uint8_t extra_bytes_per_ft[9];
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 main(argc, argv)
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 char **argv;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 {
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 char *infname;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 FILE *inf;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 uint8_t frm_in[MAX_IF1_BYTES];
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 unsigned frame_no, mode, qbit, sti, sid_mode;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 uint8_t ser_bits[MAX_SERIAL_SIZE];
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 uint16_t params[MAX_PRM_SIZE];
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 int rc;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (argc != 2) {
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 fprintf(stderr, "usage: %s amr_file\n", argv[0]);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 exit(1);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 infname = argv[1];
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 inf = fopen(infname, "r");
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 if (!inf) {
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 perror(infname);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 exit(1);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 }
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (fread(frm_in, 1, IETF_HDR_LEN, inf) != IETF_HDR_LEN ||
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 bcmp(frm_in, amr_file_hdr, IETF_HDR_LEN)) {
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 fprintf(stderr, "error: %s is not in IETF AMR format\n",
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 infname);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 exit(1);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 for (frame_no = 0; ; frame_no++) {
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 rc = getc(inf);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (rc < 0)
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 break;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 mode = (rc & 0x78) >> 3;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 qbit = (rc & 4) >> 2;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 printf("#%u: FT=%u (%s) Q=%u\n", frame_no, mode,
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 amr_mode_names[mode], qbit);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (mode == MODE_NO_DATA)
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 continue;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (mode > MRDTX) {
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 fprintf(stderr, "error in frame #%u: invalid FT=%u\n",
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 frame_no, mode);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 exit(1);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 rc = fread(frm_in, 1, extra_bytes_per_ft[mode], inf);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (rc != extra_bytes_per_ft[mode]) {
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 fprintf(stderr,
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 "error: short read from %s on frame #%u\n",
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 infname, frame_no);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 exit(1);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 amr_if1_unpack(frm_in, ser_bits, mode);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 reassemble_amr_params(ser_bits, params, mode);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 dump_amr_params(params, mode);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if (mode == MRDTX) {
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 sti = (frm_in[4] & 0x10) >> 4;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 sid_mode = 0;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (frm_in[4] & 0x08)
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 sid_mode |= 1;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (frm_in[4] & 0x04)
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 sid_mode |= 2;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 if (frm_in[4] & 0x02)
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 sid_mode |= 4;
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 printf(" SID_%s Mode=%u\n", sti ? "UPDATE" : "FIRST",
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 sid_mode);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 exit(0);
934cf92a1c45 amrconv: new program amr-ietf-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 }