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