# HG changeset patch # User Mychaela Falconia # Date 1715063240 0 # Node ID 1ceda5586d016ff7105bae4432f79cb7b46c8171 # Parent 09534cdc59ec35b356c867d5289b2d6235df1807 implement twamr-tseq-enc test program diff -r 09534cdc59ec -r 1ceda5586d01 .hgignore --- a/.hgignore Tue May 07 05:40:10 2024 +0000 +++ b/.hgignore Tue May 07 06:27:20 2024 +0000 @@ -9,6 +9,8 @@ ^amrconv/gsm-amr2efr$ ^amrconv/gsm-efr2amr$ +^amrtest/twamr-tseq-enc$ + ^dev/a2s-regen$ ^dev/a2s-regen\.out$ ^dev/efr-bit-packing$ diff -r 09534cdc59ec -r 1ceda5586d01 Makefile --- a/Makefile Tue May 07 05:40:10 2024 +0000 +++ b/Makefile Tue May 07 06:27:20 2024 +0000 @@ -2,7 +2,7 @@ CFLAGS= -O2 SUBDIR_LIBPROD= libgsmefr libgsmfr2 -SUBDIR_UTILS= amrconv efrtest frtest miscutil +SUBDIR_UTILS= amrconv amrtest efrtest frtest miscutil SUBDIR_INT= dev libtest libtwamr SUBDIR= ${SUBDIR_LIBPROD} ${SUBDIR_UTILS} ${SUBDIR_INT} @@ -10,6 +10,7 @@ all: ${SUBDIR} amrconv: libtest +amrtest: libtwamr efrtest: libgsmefr libtest frtest: libgsmfr2 libtest miscutil: libgsmefr libgsmfr2 libtest diff -r 09534cdc59ec -r 1ceda5586d01 amrtest/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/amrtest/Makefile Tue May 07 06:27:20 2024 +0000 @@ -0,0 +1,17 @@ +CC= gcc +CFLAGS= -O2 +PROGS= twamr-tseq-enc +LIBAMR= ../libtwamr/libtwamr.a +INSTBIN=/opt/freecalypso/bin + +all: ${PROGS} + +twamr-tseq-enc: mode_file.o mode_kw.o tseq-enc.o ${LIBAMR} + ${CC} ${CFLAGS} -o $@ mode_file.o mode_kw.o tseq-enc.o ${LIBAMR} + +install: + mkdir -p ${INSTBIN} + install -c ${PROGS} ${INSTBIN} + +clean: + rm -f *.o *.out ${PROGS} diff -r 09534cdc59ec -r 1ceda5586d01 amrtest/mode_file.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/amrtest/mode_file.c Tue May 07 06:27:20 2024 +0000 @@ -0,0 +1,55 @@ +/* + * The functions in this module implement reading per-frame encoder mode + * instructions from a file, as in 3GPP test sequences and code. + */ + +#include +#include +#include +#include +#include "../libtwamr/tw_amr.h" + +static FILE *mode_file; +static int lineno; +static char *filename_for_errs; + +void +open_mode_file(filename) + char *filename; +{ + mode_file = fopen(filename, "r"); + if (!mode_file) { + perror(filename); + exit(1); + } +} + +void +read_mode_file_line(mode_out) + enum Mode *mode_out; +{ + char linebuf[16], *cp; + int rc; + + if (!fgets(linebuf, sizeof linebuf, mode_file)) { + fprintf(stderr, "error: %s ends before speech input\n", + filename_for_errs); + exit(1); + } + lineno++; + cp = index(linebuf, '\n'); + if (!cp) { + fprintf(stderr, "%s line %d: too long or missing newline\n", + filename_for_errs, lineno); + exit(1); + } + if (cp > linebuf && cp[-1] == '\r') + cp--; + *cp = '\0'; + rc = grok_mode_name(linebuf, mode_out); + if (rc < 0) { + fprintf(stderr, "%s line %d: invalid mode setting\n", + filename_for_errs, lineno); + exit(1); + } +} diff -r 09534cdc59ec -r 1ceda5586d01 amrtest/mode_kw.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/amrtest/mode_kw.c Tue May 07 06:27:20 2024 +0000 @@ -0,0 +1,34 @@ +/* + * The function in this module groks user-input mode strings like "MR122" + * and returns the mode number. + */ + +#include +#include +#include +#include "../libtwamr/tw_amr.h" + +grok_mode_name(mode_str, mode_out) + char *mode_str; + enum Mode *mode_out; +{ + if (!strcmp(mode_str, "MR475")) + *mode_out = MR475; + else if (!strcmp(mode_str, "MR515")) + *mode_out = MR515; + else if (!strcmp(mode_str, "MR59")) + *mode_out = MR59; + else if (!strcmp(mode_str, "MR67")) + *mode_out = MR67; + else if (!strcmp(mode_str, "MR74")) + *mode_out = MR74; + else if (!strcmp(mode_str, "MR795")) + *mode_out = MR795; + else if (!strcmp(mode_str, "MR102")) + *mode_out = MR102; + else if (!strcmp(mode_str, "MR122")) + *mode_out = MR122; + else + return -1; + return 0; +} diff -r 09534cdc59ec -r 1ceda5586d01 amrtest/tseq-enc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/amrtest/tseq-enc.c Tue May 07 06:27:20 2024 +0000 @@ -0,0 +1,111 @@ +/* + * twamr-tseq-enc is a test program for our AMR encoder: it reads raw + * 16-bit PCM (matching 3GPP *.inp) as input and writes 3GPP *.cod + * format as output, allowing libtwamr encoder to be tested with the + * official test sequences. + * + * Both input and output are read/written in the host machine's + * native byte order. + */ + +#include +#include +#include +#include +#include +#include +#include "../libtwamr/tw_amr.h" + +static int +read_input(inf, pcm, filename_for_errs) + FILE *inf; + int16_t *pcm; + char *filename_for_errs; +{ + int cc; + + cc = fread(pcm, 2, 160, inf); + if (cc == 0) + return 0; + if (cc != 160) { + fprintf(stderr, "error: short read from %s\n", + filename_for_errs); + exit(1); + } + return 1; +} + +main(argc, argv) + char **argv; +{ + char *infname, *outfname, *mode_arg; + FILE *inf, *outf; + struct amr_encoder_state *state; + enum Mode mode_val; + int16_t pcm[160]; + struct amr_param_frame frame; + uint16_t cod_words[AMR_COD_WORDS]; + int opt, dtx = 0, vad2 = 0, use_mode_file = 0, rc; + extern int optind; + + while ((opt = getopt(argc, argv, "d2")) != EOF) { + switch (opt) { + case 'd': + dtx = 1; + continue; + case '2': + vad2 = 1; + continue; + default: + usage: + fprintf(stderr, + "usage: %s [-d] [-2] input.inp mode output.cod\n", + argv[0]); + exit(1); + } + } + if (argc != optind + 3) + goto usage; + infname = argv[optind]; + mode_arg = argv[optind+1]; + outfname = argv[optind+2]; + + inf = fopen(infname, "r"); + if (!inf) { + perror(infname); + exit(1); + } + if (!strncmp(mode_arg, "file:", 5)) { + open_mode_file(mode_arg + 5); + use_mode_file = 1; + } else { + rc = grok_mode_name(mode_arg, &mode_val); + if (rc < 0) { + fprintf(stderr, "error: invalid mode argument \"%s\"\n", + mode_arg); + exit(1); + } + } + outf = fopen(outfname, "w"); + if (!outf) { + perror(outfname); + exit(1); + } + state = amr_encoder_create(dtx, vad2); + if (!state) { + perror("amr_encoder_create()"); + exit(1); + } + for (;;) { + rc = read_input(inf, pcm, infname); + if (!rc) + break; + if (use_mode_file) + read_mode_file_line(&mode_val); + amr_encode_frame(state, mode_val, pcm, &frame); + amr_frame_to_tseq(&frame, cod_words); + fwrite(cod_words, 2, AMR_COD_WORDS, outf); + } + fclose(outf); + exit(0); +}