Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions
iLBC_test.c File Reference
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "iLBC_define.h"
#include "iLBC_encode.h"
#include "iLBC_decode.h"
#include <time.h>
Include dependency graph for iLBC_test.c:

Go to the source code of this file.

Macros

#define ILBCNOOFWORDS_MAX   (NO_OF_BYTES_30MS/2)
 

Functions

short decode (iLBC_Dec_Inst_t *iLBCdec_inst, short *decoded_data, short *encoded_data, short mode)
 
short encode (iLBC_Enc_Inst_t *iLBCenc_inst, short *encoded_data, short *data)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ ILBCNOOFWORDS_MAX

#define ILBCNOOFWORDS_MAX   (NO_OF_BYTES_30MS/2)

Definition at line 24 of file iLBC_test.c.

Referenced by main().

Function Documentation

◆ decode()

short decode ( iLBC_Dec_Inst_t iLBCdec_inst,
short *  decoded_data,
short *  encoded_data,
short  mode 
)

Definition at line 61 of file iLBC_test.c.

References iLBC_Dec_Inst_t_::blockl, BLOCKL_MAX, iLBC_decode(), MAX_SAMPLE, and MIN_SAMPLE.

Referenced by main().

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  }
#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 MAX_SAMPLE
Definition: iLBC_define.h:114
#define MIN_SAMPLE
Definition: iLBC_define.h:113

◆ encode()

short encode ( iLBC_Enc_Inst_t iLBCenc_inst,
short *  encoded_data,
short *  data 
)

Definition at line 35 of file iLBC_test.c.

References iLBC_Enc_Inst_t_::blockl, BLOCKL_MAX, iLBC_encode(), and iLBC_Enc_Inst_t_::no_of_bytes.

Referenced by main().

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  }
#define BLOCKL_MAX
Definition: iLBC_define.h:21
void iLBC_encode(unsigned char *bytes, float *block, iLBC_Enc_Inst_t *iLBCenc_inst)
Definition: iLBC_encode.c:89

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 114 of file iLBC_test.c.

References iLBC_Enc_Inst_t_::blockl, BLOCKL_MAX, decode(), encode(), ILBCNOOFWORDS_MAX, initDecode(), initEncode(), len(), and NULL.

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
#define BLOCKL_MAX
Definition: iLBC_define.h:21
#define NULL
Definition: resample.c:96
#define ILBCNOOFWORDS_MAX
Definition: iLBC_test.c:24
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
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