annotate amrtest/encode.c @ 478:936a08cc73ce

doc/AMR-library-API: describe the decoder
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 19 May 2024 21:32:31 +0000
parents 526a7f0e027d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
2 * twamr-encode is a speech encoder utility going from WAV input
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
3 * to RFC 4867 storage format (.amr) output. It logically replaces
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
4 * amrnb-enc from opencore-amr package, but its command line structure
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
5 * for specifying AMR modes follows that of twamr-tseq-enc.
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdint.h>
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <string.h>
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
151
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
13 #include <unistd.h>
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
14 #include "../libtwamr/tw_amr.h"
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
15 #include "../libtest/wavreader.h"
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
16 #include "../libtest/wavrdhelp.h"
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 main(argc, argv)
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char **argv;
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
21 char *infname, *outfname, *mode_arg;
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
22 void *wav;
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
23 FILE *outf;
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
24 struct amr_encoder_state *state;
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
25 enum Mode mode_val;
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 int16_t pcm[160];
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
27 struct amr_param_frame frame;
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
28 uint8_t out_bytes[AMR_IETF_MAX_PL];
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
29 int opt, dtx = 0, vad2 = 0, use_mode_file = 0, rc;
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
30 unsigned nbytes;
151
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
31 extern int optind;
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
33 while ((opt = getopt(argc, argv, "d2")) != EOF) {
151
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
34 switch (opt) {
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
35 case 'd':
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
36 dtx = 1;
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
37 continue;
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
38 case '2':
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
39 vad2 = 1;
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
40 continue;
151
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
41 default:
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
42 usage:
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
43 fprintf(stderr,
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
44 "usage: %s [-d] [-2] input.wav mode output.amr\n",
151
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
45 argv[0]);
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
46 exit(1);
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
47 }
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
49 if (argc != optind + 3)
151
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
50 goto usage;
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
51 infname = argv[optind];
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
52 mode_arg = argv[optind+1];
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
53 outfname = argv[optind+2];
151
a13b1605142b gsmefr-etsi-enc: add BE support
Mychaela Falconia <falcon@freecalypso.org>
parents: 114
diff changeset
54
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
55 wav = wav_read_open(infname);
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
56 if (!wav) {
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 perror(infname);
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 exit(1);
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
60 rc = wavrd_check_header(wav, infname);
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
61 if (rc < 0)
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
62 exit(1); /* error msg already printed */
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
63 if (!strncmp(mode_arg, "file:", 5)) {
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
64 open_mode_file(mode_arg + 5);
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
65 use_mode_file = 1;
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
66 } else {
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
67 rc = grok_mode_name(mode_arg, &mode_val);
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
68 if (rc < 0) {
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
69 fprintf(stderr, "error: invalid mode argument \"%s\"\n",
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
70 mode_arg);
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
71 exit(1);
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
72 }
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
73 }
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 outf = fopen(outfname, "w");
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (!outf) {
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 perror(outfname);
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 exit(1);
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
79 state = amr_encoder_create(dtx, vad2);
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 if (!state) {
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
81 perror("amr_encoder_create()");
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 exit(1);
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 }
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
84 fwrite(amr_file_header_magic, 1, AMR_IETF_HDR_LEN, outf);
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 for (;;) {
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
86 rc = wavrd_get_pcm_block(wav, pcm);
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (!rc)
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 break;
422
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
89 if (use_mode_file)
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
90 read_mode_file_line(&mode_val);
1ceda5586d01 implement twamr-tseq-enc test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 151
diff changeset
91 amr_encode_frame(state, mode_val, pcm, &frame);
443
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
92 nbytes = amr_frame_to_ietf(&frame, out_bytes);
526a7f0e027d amrtest: implement twamr-encode
Mychaela Falconia <falcon@freecalypso.org>
parents: 423
diff changeset
93 fwrite(out_bytes, 1, nbytes, outf);
114
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 }
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 fclose(outf);
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 exit(0);
ff0372186b59 gsmefr-etsi-enc test program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }