2
/******************************************************************
4
iLBC Speech Coder ANSI-C Source Code
8
Copyright (C) The Internet Society (2004).
11
******************************************************************/
17
#include "iLBC_define.h"
18
#include "iLBC_encode.h"
19
#include "iLBC_decode.h"
21
/* Runtime statistics */
24
#define ILBCNOOFWORDS_MAX (NO_OF_BYTES_30MS/2)
26
/*----------------------------------------------------------------*
27
* Encoder interface function
33
*---------------------------------------------------------------*/
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*/
41
float block[BLOCKL_MAX];
44
/* convert signal to float */
46
for (k=0; k<iLBCenc_inst->blockl; k++)
47
block[k] = (float)data[k];
49
/* do the actual encoding */
51
iLBC_encode((unsigned char *)encoded_data, block, iLBCenc_inst);
54
return (iLBCenc_inst->no_of_bytes);
57
/*----------------------------------------------------------------*
58
* Decoder interface function
59
*---------------------------------------------------------------*/
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 */
68
float decblock[BLOCKL_MAX], dtmp;
70
/* check if mode is valid */
72
if (mode<0 || mode>1) {
73
printf("\nERROR - Wrong mode - 0, 1 allowed\n"); exit(3);}
75
/* do actual decoding of block */
77
iLBC_decode(decblock, (unsigned char *)encoded_data,
80
/* convert to short */
86
for (k=0; k<iLBCdec_inst->blockl; k++){
91
else if (dtmp>MAX_SAMPLE)
93
decoded_data[k] = (short) dtmp;
96
return (iLBCdec_inst->blockl);
99
/*---------------------------------------------------------------*
100
* Main program to test iLBC encoding and decoding
103
* exefile_name.exe <infile> <bytefile> <outfile> <channel>
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
112
*--------------------------------------------------------------*/
114
int main(int argc, char* argv[])
117
/* Runtime statistics */
123
FILE *ifileid,*efileid,*ofileid, *cfileid;
124
short data[BLOCKL_MAX];
125
short encoded_data[ILBCNOOFWORDS_MAX], decoded_data[BLOCKL_MAX];
129
int packetlosscount = 0;
132
iLBC_Enc_Inst_t Enc_Inst;
133
iLBC_Dec_Inst_t Dec_Inst;
139
/* get arguments and open files */
141
if ((argc!=5) && (argc!=6)) {
143
"\n*-----------------------------------------------*\n");
145
" %s <20,30> input encoded decoded (channel)\n\n",
148
" mode : Frame size for the encoding/decoding\n");
154
" input : Speech for encoder (16-bit pcm file)\n");
156
" encoded : Encoded bit stream\n");
158
" decoded : Decoded speech (16-bit pcm file)\n");
160
" channel : Packet loss pattern, optional (16-bit)\n");
162
" 1 - Packet received correctly\n");
164
" 0 - Packet Lost\n");
166
"*-----------------------------------------------*\n\n");
170
if (mode != 20 && mode != 30) {
171
fprintf(stderr,"Wrong mode %s, must be 20, or 30\n",
175
if ( (ifileid=fopen(argv[2],"rb")) == NULL) {
176
fprintf(stderr,"Cannot open input file %s\n", argv[2]);
178
if ( (efileid=fopen(argv[3],"wb")) == NULL) {
179
fprintf(stderr, "Cannot open encoded file %s\n",
181
if ( (ofileid=fopen(argv[4],"wb")) == NULL) {
182
fprintf(stderr, "Cannot open decoded file %s\n",
185
if( (cfileid=fopen(argv[5],"rb")) == NULL) {
186
fprintf(stderr, "Cannot open channel file %s\n",
201
fprintf(stderr, "\n");
203
"*---------------------------------------------------*\n");
207
"* iLBC test program *\n");
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]);
219
fprintf(stderr,"Channel file : %s\n", argv[5]);
221
fprintf(stderr,"\n");
225
initEncode(&Enc_Inst, mode);
226
initDecode(&Dec_Inst, mode, 1);
228
/* Runtime statistics */
230
starttime=clock()/(float)CLOCKS_PER_SEC;
232
/* loop over input blocks */
234
while (fread(data,sizeof(short),Enc_Inst.blockl,ifileid)==
235
(size_t)Enc_Inst.blockl) {
245
fprintf(stderr, "--- Encoding block %i --- ",blockcount);
246
len=encode(&Enc_Inst, encoded_data, data);
247
fprintf(stderr, "\r");
249
/* write byte file */
251
fwrite(encoded_data, sizeof(unsigned char), len, efileid);
253
/* get channel data if provided */
255
if (fread(&pli, sizeof(short), 1, cfileid)) {
256
if ((pli!=0)&&(pli!=1)) {
257
fprintf(stderr, "Error in channel file\n");
261
/* Packet loss -> remove info from frame */
262
memset(encoded_data, 0,
263
sizeof(short)*ILBCNOOFWORDS_MAX);
267
fprintf(stderr, "Error. Channel file too short\n");
276
fprintf(stderr, "--- Decoding block %i --- ",blockcount);
278
len=decode(&Dec_Inst, decoded_data, encoded_data, pli);
279
fprintf(stderr, "\r");
281
/* write output file */
283
fwrite(decoded_data,sizeof(short),len,ofileid);
286
/* Runtime statistics */
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);
298
printf("Time to run iLBC :");
299
printf(" %.1f s (%.1f %% of realtime)\n\n", runtime,
300
(100*runtime/outtime));
304
fclose(ifileid); fclose(efileid); fclose(ofileid);