Mercurial > hg > audiostuff
comparison intercom/ilbc/iLBC_test.c @ 2:13be24d74cd2
import intercom-0.4.1
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
|---|---|
| date | Fri, 25 Jun 2010 09:57:52 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:9cadc470e3da | 2:13be24d74cd2 |
|---|---|
| 1 | |
| 2 /****************************************************************** | |
| 3 | |
| 4 iLBC Speech Coder ANSI-C Source Code | |
| 5 | |
| 6 iLBC_test.c | |
| 7 | |
| 8 Copyright (C) The Internet Society (2004). | |
| 9 All Rights Reserved. | |
| 10 | |
| 11 ******************************************************************/ | |
| 12 | |
| 13 #include <math.h> | |
| 14 #include <stdlib.h> | |
| 15 #include <stdio.h> | |
| 16 #include <string.h> | |
| 17 #include "iLBC_define.h" | |
| 18 #include "iLBC_encode.h" | |
| 19 #include "iLBC_decode.h" | |
| 20 | |
| 21 /* Runtime statistics */ | |
| 22 #include <time.h> | |
| 23 | |
| 24 #define ILBCNOOFWORDS_MAX (NO_OF_BYTES_30MS/2) | |
| 25 | |
| 26 /*----------------------------------------------------------------* | |
| 27 * Encoder interface function | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 *---------------------------------------------------------------*/ | |
| 34 | |
| 35 short encode( /* (o) Number of bytes encoded */ | |
| 36 iLBC_Enc_Inst_t * iLBCenc_inst, | |
| 37 /* (i/o) Encoder instance */ | |
| 38 short *encoded_data, /* (o) The encoded bytes */ | |
| 39 short *data /* (i) The signal block to encode */ | |
| 40 ) | |
| 41 { | |
| 42 float block[BLOCKL_MAX]; | |
| 43 int k; | |
| 44 | |
| 45 /* convert signal to float */ | |
| 46 | |
| 47 for (k = 0; k < iLBCenc_inst->blockl; k++) | |
| 48 block[k] = (float) data[k]; | |
| 49 | |
| 50 /* do the actual encoding */ | |
| 51 | |
| 52 iLBC_encode((unsigned char *) encoded_data, block, iLBCenc_inst); | |
| 53 | |
| 54 | |
| 55 return (iLBCenc_inst->no_of_bytes); | |
| 56 } | |
| 57 | |
| 58 /*----------------------------------------------------------------* | |
| 59 * Decoder interface function | |
| 60 *---------------------------------------------------------------*/ | |
| 61 | |
| 62 short decode( /* (o) Number of decoded samples */ | |
| 63 iLBC_Dec_Inst_t * iLBCdec_inst, /* (i/o) Decoder instance */ | |
| 64 short *decoded_data, /* (o) Decoded signal block */ | |
| 65 short *encoded_data, /* (i) Encoded bytes */ | |
| 66 short mode /* (i) 0=PL, 1=Normal */ | |
| 67 ) | |
| 68 { | |
| 69 int k; | |
| 70 float decblock[BLOCKL_MAX], dtmp; | |
| 71 | |
| 72 /* check if mode is valid */ | |
| 73 | |
| 74 if (mode < 0 || mode > 1) { | |
| 75 printf("\nERROR - Wrong mode - 0, 1 allowed\n"); | |
| 76 exit(3); | |
| 77 } | |
| 78 | |
| 79 /* do actual decoding of block */ | |
| 80 | |
| 81 iLBC_decode(decblock, (unsigned char *) encoded_data, | |
| 82 iLBCdec_inst, mode); | |
| 83 | |
| 84 /* convert to short */ | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 for (k = 0; k < iLBCdec_inst->blockl; k++) { | |
| 91 dtmp = decblock[k]; | |
| 92 | |
| 93 if (dtmp < MIN_SAMPLE) | |
| 94 dtmp = MIN_SAMPLE; | |
| 95 else if (dtmp > MAX_SAMPLE) | |
| 96 dtmp = MAX_SAMPLE; | |
| 97 decoded_data[k] = (short) dtmp; | |
| 98 } | |
| 99 | |
| 100 return (iLBCdec_inst->blockl); | |
| 101 } | |
| 102 | |
| 103 /*---------------------------------------------------------------* | |
| 104 * Main program to test iLBC encoding and decoding | |
| 105 * | |
| 106 * Usage: | |
| 107 * exefile_name.exe <infile> <bytefile> <outfile> <channel> | |
| 108 * | |
| 109 * <infile> : Input file, speech for encoder (16-bit pcm file) | |
| 110 * <bytefile> : Bit stream output from the encoder | |
| 111 * <outfile> : Output file, decoded speech (16-bit pcm file) | |
| 112 * <channel> : Bit error file, optional (16-bit) | |
| 113 * 1 - Packet received correctly | |
| 114 * 0 - Packet Lost | |
| 115 * | |
| 116 *--------------------------------------------------------------*/ | |
| 117 | |
| 118 int main(int argc, char *argv[]) | |
| 119 { | |
| 120 | |
| 121 /* Runtime statistics */ | |
| 122 | |
| 123 float starttime; | |
| 124 float runtime; | |
| 125 float outtime; | |
| 126 | |
| 127 FILE *ifileid, *efileid, *ofileid, *cfileid; | |
| 128 short data[BLOCKL_MAX]; | |
| 129 short encoded_data[ILBCNOOFWORDS_MAX], decoded_data[BLOCKL_MAX]; | |
| 130 int len; | |
| 131 short pli, mode; | |
| 132 int blockcount = 0; | |
| 133 int packetlosscount = 0; | |
| 134 | |
| 135 /* Create structs */ | |
| 136 iLBC_Enc_Inst_t Enc_Inst; | |
| 137 iLBC_Dec_Inst_t Dec_Inst; | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 /* get arguments and open files */ | |
| 144 | |
| 145 if ((argc != 5) && (argc != 6)) { | |
| 146 fprintf(stderr, | |
| 147 "\n*-----------------------------------------------*\n"); | |
| 148 fprintf(stderr, | |
| 149 " %s <20,30> input encoded decoded (channel)\n\n", argv[0]); | |
| 150 fprintf(stderr, | |
| 151 " mode : Frame size for the encoding/decoding\n"); | |
| 152 fprintf(stderr, " 20 - 20 ms\n"); | |
| 153 fprintf(stderr, " 30 - 30 ms\n"); | |
| 154 fprintf(stderr, | |
| 155 " input : Speech for encoder (16-bit pcm file)\n"); | |
| 156 fprintf(stderr, " encoded : Encoded bit stream\n"); | |
| 157 fprintf(stderr, " decoded : Decoded speech (16-bit pcm file)\n"); | |
| 158 fprintf(stderr, | |
| 159 " channel : Packet loss pattern, optional (16-bit)\n"); | |
| 160 fprintf(stderr, | |
| 161 " 1 - Packet received correctly\n"); | |
| 162 fprintf(stderr, " 0 - Packet Lost\n"); | |
| 163 fprintf(stderr, | |
| 164 "*-----------------------------------------------*\n\n"); | |
| 165 exit(1); | |
| 166 } | |
| 167 mode = atoi(argv[1]); | |
| 168 if (mode != 20 && mode != 30) { | |
| 169 fprintf(stderr, "Wrong mode %s, must be 20, or 30\n", argv[1]); | |
| 170 exit(2); | |
| 171 } | |
| 172 if ((ifileid = fopen(argv[2], "rb")) == NULL) { | |
| 173 fprintf(stderr, "Cannot open input file %s\n", argv[2]); | |
| 174 exit(2); | |
| 175 } | |
| 176 if ((efileid = fopen(argv[3], "wb")) == NULL) { | |
| 177 fprintf(stderr, "Cannot open encoded file %s\n", argv[3]); | |
| 178 exit(1); | |
| 179 } | |
| 180 if ((ofileid = fopen(argv[4], "wb")) == NULL) { | |
| 181 fprintf(stderr, "Cannot open decoded file %s\n", argv[4]); | |
| 182 exit(1); | |
| 183 } | |
| 184 if (argc == 6) { | |
| 185 if ((cfileid = fopen(argv[5], "rb")) == NULL) { | |
| 186 fprintf(stderr, "Cannot open channel file %s\n", argv[5]); | |
| 187 exit(1); | |
| 188 } | |
| 189 } else { | |
| 190 cfileid = NULL; | |
| 191 } | |
| 192 | |
| 193 /* print info */ | |
| 194 | |
| 195 fprintf(stderr, "\n"); | |
| 196 fprintf(stderr, | |
| 197 "*---------------------------------------------------*\n"); | |
| 198 fprintf(stderr, | |
| 199 "* *\n"); | |
| 200 fprintf(stderr, | |
| 201 "* iLBC test program *\n"); | |
| 202 fprintf(stderr, | |
| 203 "* *\n"); | |
| 204 fprintf(stderr, | |
| 205 "* *\n"); | |
| 206 fprintf(stderr, | |
| 207 "*---------------------------------------------------*\n"); | |
| 208 fprintf(stderr, "\nMode : %2d ms\n", mode); | |
| 209 fprintf(stderr, "Input file : %s\n", argv[2]); | |
| 210 fprintf(stderr, "Encoded file : %s\n", argv[3]); | |
| 211 fprintf(stderr, "Output file : %s\n", argv[4]); | |
| 212 if (argc == 6) { | |
| 213 fprintf(stderr, "Channel file : %s\n", argv[5]); | |
| 214 } | |
| 215 fprintf(stderr, "\n"); | |
| 216 | |
| 217 /* Initialization */ | |
| 218 | |
| 219 initEncode(&Enc_Inst, mode); | |
| 220 initDecode(&Dec_Inst, mode, 1); | |
| 221 | |
| 222 /* Runtime statistics */ | |
| 223 | |
| 224 starttime = clock() / (float) CLOCKS_PER_SEC; | |
| 225 | |
| 226 /* loop over input blocks */ | |
| 227 | |
| 228 while (fread(data, sizeof(short), Enc_Inst.blockl, ifileid) == | |
| 229 Enc_Inst.blockl) { | |
| 230 | |
| 231 blockcount++; | |
| 232 | |
| 233 /* encoding */ | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 fprintf(stderr, "--- Encoding block %i --- ", blockcount); | |
| 240 len = encode(&Enc_Inst, encoded_data, data); | |
| 241 fprintf(stderr, "\r"); | |
| 242 | |
| 243 /* write byte file */ | |
| 244 | |
| 245 fwrite(encoded_data, sizeof(unsigned char), len, efileid); | |
| 246 | |
| 247 /* get channel data if provided */ | |
| 248 if (argc == 6) { | |
| 249 if (fread(&pli, sizeof(short), 1, cfileid)) { | |
| 250 if ((pli != 0) && (pli != 1)) { | |
| 251 fprintf(stderr, "Error in channel file\n"); | |
| 252 exit(0); | |
| 253 } | |
| 254 if (pli == 0) { | |
| 255 /* Packet loss -> remove info from frame */ | |
| 256 memset(encoded_data, 0, sizeof(short) * ILBCNOOFWORDS_MAX); | |
| 257 packetlosscount++; | |
| 258 } | |
| 259 } else { | |
| 260 fprintf(stderr, "Error. Channel file too short\n"); | |
| 261 exit(0); | |
| 262 } | |
| 263 } else { | |
| 264 pli = 1; | |
| 265 } | |
| 266 | |
| 267 /* decoding */ | |
| 268 | |
| 269 fprintf(stderr, "--- Decoding block %i --- ", blockcount); | |
| 270 | |
| 271 len = decode(&Dec_Inst, decoded_data, encoded_data, pli); | |
| 272 fprintf(stderr, "\r"); | |
| 273 | |
| 274 /* write output file */ | |
| 275 | |
| 276 fwrite(decoded_data, sizeof(short), len, ofileid); | |
| 277 } | |
| 278 | |
| 279 /* Runtime statistics */ | |
| 280 | |
| 281 runtime = (float) (clock() / (float) CLOCKS_PER_SEC - starttime); | |
| 282 outtime = (float) ((float) blockcount * (float) mode / 1000.0); | |
| 283 printf("\n\nLength of speech file: %.1f s\n", outtime); | |
| 284 printf("Packet loss : %.1f%%\n", | |
| 285 100.0 * (float) packetlosscount / (float) blockcount); | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 printf("Time to run iLBC :"); | |
| 292 printf(" %.1f s (%.1f %% of realtime)\n\n", runtime, | |
| 293 (100 * runtime / outtime)); | |
| 294 | |
| 295 /* close files */ | |
| 296 | |
| 297 fclose(ifileid); | |
| 298 fclose(efileid); | |
| 299 fclose(ofileid); | |
| 300 if (argc == 6) { | |
| 301 fclose(cfileid); | |
| 302 } | |
| 303 return (0); | |
| 304 } |
