FreeCalypso > hg > gsm-codec-lib
comparison amrtest/tseq-enc.c @ 422:1ceda5586d01
implement twamr-tseq-enc test program
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Tue, 07 May 2024 06:27:20 +0000 |
| parents | efrtest/etsi-enc.c@a13b1605142b |
| children | cf90077b753c |
comparison
equal
deleted
inserted
replaced
| 421:09534cdc59ec | 422:1ceda5586d01 |
|---|---|
| 1 /* | |
| 2 * twamr-tseq-enc is a test program for our AMR encoder: it reads raw | |
| 3 * 16-bit PCM (matching 3GPP *.inp) as input and writes 3GPP *.cod | |
| 4 * format as output, allowing libtwamr encoder to be tested with the | |
| 5 * official test sequences. | |
| 6 * | |
| 7 * Both input and output are read/written in the host machine's | |
| 8 * native byte order. | |
| 9 */ | |
| 10 | |
| 11 #include <stdio.h> | |
| 12 #include <stdint.h> | |
| 13 #include <stdlib.h> | |
| 14 #include <string.h> | |
| 15 #include <strings.h> | |
| 16 #include <unistd.h> | |
| 17 #include "../libtwamr/tw_amr.h" | |
| 18 | |
| 19 static int | |
| 20 read_input(inf, pcm, filename_for_errs) | |
| 21 FILE *inf; | |
| 22 int16_t *pcm; | |
| 23 char *filename_for_errs; | |
| 24 { | |
| 25 int cc; | |
| 26 | |
| 27 cc = fread(pcm, 2, 160, inf); | |
| 28 if (cc == 0) | |
| 29 return 0; | |
| 30 if (cc != 160) { | |
| 31 fprintf(stderr, "error: short read from %s\n", | |
| 32 filename_for_errs); | |
| 33 exit(1); | |
| 34 } | |
| 35 return 1; | |
| 36 } | |
| 37 | |
| 38 main(argc, argv) | |
| 39 char **argv; | |
| 40 { | |
| 41 char *infname, *outfname, *mode_arg; | |
| 42 FILE *inf, *outf; | |
| 43 struct amr_encoder_state *state; | |
| 44 enum Mode mode_val; | |
| 45 int16_t pcm[160]; | |
| 46 struct amr_param_frame frame; | |
| 47 uint16_t cod_words[AMR_COD_WORDS]; | |
| 48 int opt, dtx = 0, vad2 = 0, use_mode_file = 0, rc; | |
| 49 extern int optind; | |
| 50 | |
| 51 while ((opt = getopt(argc, argv, "d2")) != EOF) { | |
| 52 switch (opt) { | |
| 53 case 'd': | |
| 54 dtx = 1; | |
| 55 continue; | |
| 56 case '2': | |
| 57 vad2 = 1; | |
| 58 continue; | |
| 59 default: | |
| 60 usage: | |
| 61 fprintf(stderr, | |
| 62 "usage: %s [-d] [-2] input.inp mode output.cod\n", | |
| 63 argv[0]); | |
| 64 exit(1); | |
| 65 } | |
| 66 } | |
| 67 if (argc != optind + 3) | |
| 68 goto usage; | |
| 69 infname = argv[optind]; | |
| 70 mode_arg = argv[optind+1]; | |
| 71 outfname = argv[optind+2]; | |
| 72 | |
| 73 inf = fopen(infname, "r"); | |
| 74 if (!inf) { | |
| 75 perror(infname); | |
| 76 exit(1); | |
| 77 } | |
| 78 if (!strncmp(mode_arg, "file:", 5)) { | |
| 79 open_mode_file(mode_arg + 5); | |
| 80 use_mode_file = 1; | |
| 81 } else { | |
| 82 rc = grok_mode_name(mode_arg, &mode_val); | |
| 83 if (rc < 0) { | |
| 84 fprintf(stderr, "error: invalid mode argument \"%s\"\n", | |
| 85 mode_arg); | |
| 86 exit(1); | |
| 87 } | |
| 88 } | |
| 89 outf = fopen(outfname, "w"); | |
| 90 if (!outf) { | |
| 91 perror(outfname); | |
| 92 exit(1); | |
| 93 } | |
| 94 state = amr_encoder_create(dtx, vad2); | |
| 95 if (!state) { | |
| 96 perror("amr_encoder_create()"); | |
| 97 exit(1); | |
| 98 } | |
| 99 for (;;) { | |
| 100 rc = read_input(inf, pcm, infname); | |
| 101 if (!rc) | |
| 102 break; | |
| 103 if (use_mode_file) | |
| 104 read_mode_file_line(&mode_val); | |
| 105 amr_encode_frame(state, mode_val, pcm, &frame); | |
| 106 amr_frame_to_tseq(&frame, cod_words); | |
| 107 fwrite(cod_words, 2, AMR_COD_WORDS, outf); | |
| 108 } | |
| 109 fclose(outf); | |
| 110 exit(0); | |
| 111 } |
