comparison amrtest/encode.c @ 443:526a7f0e027d

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