Asterisk - The Open Source Telephony Project  18.5.0
iLBC_test.c
Go to the documentation of this file.
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  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)==
235  Enc_Inst.blockl) {
236 
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  if (fwrite(encoded_data, sizeof(unsigned char), len, efileid) != len) {
252  fprintf(stderr, "Failure in fwritef\n");
253  }
254 
255  /* get channel data if provided */
256  if (argc==6) {
257  if (fread(&pli, sizeof(short), 1, cfileid)) {
258  if ((pli!=0)&&(pli!=1)) {
259  fprintf(stderr, "Error in channel file\n");
260  exit(0);
261  }
262  if (pli==0) {
263  /* Packet loss -> remove info from frame */
264  memset(encoded_data, 0,
265  sizeof(short)*ILBCNOOFWORDS_MAX);
266  packetlosscount++;
267  }
268  } else {
269  fprintf(stderr, "Error. Channel file too short\n");
270  exit(0);
271  }
272  } else {
273  pli=1;
274  }
275 
276  /* decoding */
277 
278  fprintf(stderr, "--- Decoding block %i --- ",blockcount);
279 
280  len=decode(&Dec_Inst, decoded_data, encoded_data, pli);
281  fprintf(stderr, "\r");
282 
283  /* write output file */
284 
285  if (fwrite(decoded_data,sizeof(short),len,ofileid) != len) {
286  fprintf(stderr, "Failure in fwritef\n");
287  }
288  }
289 
290  /* Runtime statistics */
291 
292  runtime = (float)(clock()/(float)CLOCKS_PER_SEC-starttime);
293  outtime = (float)((float)blockcount*(float)mode/1000.0);
294  printf("\n\nLength of speech file: %.1f s\n", outtime);
295  printf("Packet loss : %.1f%%\n",
296  100.0*(float)packetlosscount/(float)blockcount);
297 
298 
299 
300 
301 
302  printf("Time to run iLBC :");
303  printf(" %.1f s (%.1f %% of realtime)\n\n", runtime,
304  (100*runtime/outtime));
305 
306  /* close files */
307 
308  fclose(ifileid); fclose(efileid); fclose(ofileid);
309  if (argc==6) {
310  fclose(cfileid);
311  }
312  return(0);
313  }
short decode(iLBC_Dec_Inst_t *iLBCdec_inst, short *decoded_data, short *encoded_data, short mode)
Definition: iLBC_test.c:61
Time-related functions and macros.
#define BLOCKL_MAX
Definition: iLBC_define.h:21
void iLBC_decode(float *decblock, unsigned char *bytes, iLBC_Dec_Inst_t *iLBCdec_inst, int mode)
Definition: iLBC_decode.c:326
#define NULL
Definition: resample.c:96
void iLBC_encode(unsigned char *bytes, float *block, iLBC_Enc_Inst_t *iLBCenc_inst)
Definition: iLBC_encode.c:89
#define ILBCNOOFWORDS_MAX
Definition: iLBC_test.c:24
int main(int argc, char *argv[])
Definition: iLBC_test.c:114
short initDecode(iLBC_Dec_Inst_t *iLBCdec_inst, int mode, int use_enhancer)
Definition: iLBC_decode.c:33
short encode(iLBC_Enc_Inst_t *iLBCenc_inst, short *encoded_data, short *data)
Definition: iLBC_test.c:35
#define MAX_SAMPLE
Definition: iLBC_define.h:114
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
short initEncode(iLBC_Enc_Inst_t *iLBCenc_inst, int mode)
Definition: iLBC_encode.c:35
#define MIN_SAMPLE
Definition: iLBC_define.h:113