comparison amrtest/decode.c @ 444:fe4983b05230

amrtest: implement twamr-decode
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 09 May 2024 23:35:25 +0000
parents amrtest/tseq-dec.c@edbbbf1c4ab1
children
comparison
equal deleted inserted replaced
443:526a7f0e027d 444:fe4983b05230
1 /*
2 * twamr-decode is a speech decoder utility going from *.amr input
3 * to WAV output. It logically replaces amrnb-dec from opencore-amr
4 * package.
5 */
6
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include "../libtwamr/tw_amr.h"
13 #include "../libtest/wavwriter.h"
14 #include "../libtest/pcmwrite.h"
15
16 main(argc, argv)
17 char **argv;
18 {
19 FILE *inf;
20 void *wav;
21 uint8_t frm_in[AMR_IETF_MAX_PL];
22 struct amr_decoder_state *state;
23 struct amr_param_frame frame;
24 int16_t pcm[160];
25 int rc;
26
27 if (argc != 3) {
28 fprintf(stderr, "usage: %s input.amr output.wav\n", argv[0]);
29 exit(1);
30 }
31 inf = fopen(argv[1], "r");
32 if (!inf) {
33 perror(argv[1]);
34 exit(1);
35 }
36 if (fread(frm_in, 1, AMR_IETF_HDR_LEN, inf) != AMR_IETF_HDR_LEN ||
37 bcmp(frm_in, amr_file_header_magic, AMR_IETF_HDR_LEN)) {
38 fprintf(stderr, "error: %s is not in IETF AMR format\n",
39 argv[1]);
40 exit(1);
41 }
42 wav = wav_write_open(argv[2], 8000, 16, 1);
43 if (!wav) {
44 perror(argv[2]);
45 exit(1);
46 }
47 state = amr_decoder_create();
48 if (!state) {
49 perror("amr_decoder_create()");
50 exit(1);
51 }
52 for (;;) {
53 rc = getc(inf);
54 if (rc < 0)
55 break;
56 frm_in[0] = rc;
57 rc = amr_ietf_grok_first_octet(frm_in[0]);
58 if (rc < 0) {
59 inv: fprintf(stderr, "error: %s contains invalid AMR data\n",
60 argv[1]);
61 exit(1);
62 }
63 if (rc && fread(frm_in+1, 1, rc, inf) != rc)
64 goto inv;
65 amr_frame_from_ietf(frm_in, &frame);
66 amr_decode_frame(state, &frame, pcm);
67 write_pcm_to_wav(wav, pcm);
68 }
69 wav_write_close(wav);
70 exit(0);
71 }