comparison amrtest/encode-r.c @ 445:30cff6b60890

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