1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11/******************************************************************
12
13	iLBC Speech Coder ANSI-C Source Code
14
15        iLBC_test.c
16
17******************************************************************/
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include "ilbc.h"
23
24/*---------------------------------------------------------------*
25 *  Main program to test iLBC encoding and decoding
26 *
27 *  Usage:
28 *	  exefile_name.exe <infile> <bytefile> <outfile> <channel>
29 *
30 *    <infile>   : Input file, speech for encoder (16-bit pcm file)
31 *    <bytefile> : Bit stream output from the encoder
32 *    <outfile>  : Output file, decoded speech (16-bit pcm file)
33 *    <channel>  : Bit error file, optional (16-bit)
34 *                     1 - Packet received correctly
35 *                     0 - Packet Lost
36 *
37 *--------------------------------------------------------------*/
38
39#define BLOCKL_MAX			240
40#define ILBCNOOFWORDS_MAX	25
41
42
43int main(int argc, char* argv[])
44{
45
46  FILE *ifileid,*efileid,*ofileid, *cfileid;
47  int16_t data[BLOCKL_MAX];
48  int16_t encoded_data[ILBCNOOFWORDS_MAX], decoded_data[BLOCKL_MAX];
49  int len;
50  short pli, mode;
51  int blockcount = 0;
52  int packetlosscount = 0;
53  int frameLen;
54  int16_t speechType;
55  iLBC_encinst_t *Enc_Inst;
56  iLBC_decinst_t *Dec_Inst;
57
58#ifdef __ILBC_WITH_40BITACC
59  /* Doublecheck that long long exists */
60  if (sizeof(long)>=sizeof(long long)) {
61    fprintf(stderr, "40-bit simulation is not be supported on this platform\n");
62    exit(0);
63  }
64#endif
65
66  /* get arguments and open files */
67
68  if ((argc!=5) && (argc!=6)) {
69    fprintf(stderr,
70            "\n*-----------------------------------------------*\n");
71    fprintf(stderr,
72            "   %s <20,30> input encoded decoded (channel)\n\n",
73            argv[0]);
74    fprintf(stderr,
75            "   mode    : Frame size for the encoding/decoding\n");
76    fprintf(stderr,
77            "                 20 - 20 ms\n");
78    fprintf(stderr,
79            "                 30 - 30 ms\n");
80    fprintf(stderr,
81            "   input   : Speech for encoder (16-bit pcm file)\n");
82    fprintf(stderr,
83            "   encoded : Encoded bit stream\n");
84    fprintf(stderr,
85            "   decoded : Decoded speech (16-bit pcm file)\n");
86    fprintf(stderr,
87            "   channel : Packet loss pattern, optional (16-bit)\n");
88    fprintf(stderr,
89            "                  1 - Packet received correctly\n");
90    fprintf(stderr,
91            "                  0 - Packet Lost\n");
92    fprintf(stderr,
93            "*-----------------------------------------------*\n\n");
94    exit(1);
95  }
96  mode=atoi(argv[1]);
97  if (mode != 20 && mode != 30) {
98    fprintf(stderr,"Wrong mode %s, must be 20, or 30\n",
99            argv[1]);
100    exit(2);
101  }
102  if ( (ifileid=fopen(argv[2],"rb")) == NULL) {
103    fprintf(stderr,"Cannot open input file %s\n", argv[2]);
104    exit(2);}
105  if ( (efileid=fopen(argv[3],"wb")) == NULL) {
106    fprintf(stderr, "Cannot open encoded file file %s\n",
107            argv[3]); exit(1);}
108  if ( (ofileid=fopen(argv[4],"wb")) == NULL) {
109    fprintf(stderr, "Cannot open decoded file %s\n",
110            argv[4]); exit(1);}
111  if (argc==6) {
112    if( (cfileid=fopen(argv[5],"rb")) == NULL) {
113      fprintf(stderr, "Cannot open channel file %s\n",
114              argv[5]);
115      exit(1);
116    }
117  } else {
118    cfileid=NULL;
119  }
120
121  /* print info */
122
123  fprintf(stderr, "\n");
124  fprintf(stderr,
125          "*---------------------------------------------------*\n");
126  fprintf(stderr,
127          "*                                                   *\n");
128  fprintf(stderr,
129          "*      iLBC test program                            *\n");
130  fprintf(stderr,
131          "*                                                   *\n");
132  fprintf(stderr,
133          "*                                                   *\n");
134  fprintf(stderr,
135          "*---------------------------------------------------*\n");
136  fprintf(stderr,"\nMode           : %2d ms\n", mode);
137  fprintf(stderr,"Input file     : %s\n", argv[2]);
138  fprintf(stderr,"Encoded file   : %s\n", argv[3]);
139  fprintf(stderr,"Output file    : %s\n", argv[4]);
140  if (argc==6) {
141    fprintf(stderr,"Channel file   : %s\n", argv[5]);
142  }
143  fprintf(stderr,"\n");
144
145  /* Create structs */
146  WebRtcIlbcfix_EncoderCreate(&Enc_Inst);
147  WebRtcIlbcfix_DecoderCreate(&Dec_Inst);
148
149
150  /* Initialization */
151
152  WebRtcIlbcfix_EncoderInit(Enc_Inst, mode);
153  WebRtcIlbcfix_DecoderInit(Dec_Inst, mode);
154  frameLen = mode*8;
155
156  /* loop over input blocks */
157
158  while (((int16_t)fread(data,sizeof(int16_t),frameLen,ifileid))==
159         frameLen) {
160
161    blockcount++;
162
163    /* encoding */
164
165    fprintf(stderr, "--- Encoding block %i --- ",blockcount);
166    len=WebRtcIlbcfix_Encode(Enc_Inst, data, (int16_t)frameLen, encoded_data);
167    fprintf(stderr, "\r");
168
169    /* write byte file */
170
171    if (fwrite(encoded_data, sizeof(int16_t),
172               ((len+1)/sizeof(int16_t)), efileid) !=
173        (size_t)(((len+1)/sizeof(int16_t)))) {
174      return -1;
175    }
176
177    /* get channel data if provided */
178    if (argc==6) {
179      if (fread(&pli, sizeof(int16_t), 1, cfileid)) {
180        if ((pli!=0)&&(pli!=1)) {
181          fprintf(stderr, "Error in channel file\n");
182          exit(0);
183        }
184        if (pli==0) {
185          /* Packet loss -> remove info from frame */
186          memset(encoded_data, 0,
187                 sizeof(int16_t)*ILBCNOOFWORDS_MAX);
188          packetlosscount++;
189        }
190      } else {
191        fprintf(stderr, "Error. Channel file too short\n");
192        exit(0);
193      }
194    } else {
195      pli=1;
196    }
197
198    /* decoding */
199
200    fprintf(stderr, "--- Decoding block %i --- ",blockcount);
201    if (pli==1) {
202      len=WebRtcIlbcfix_Decode(Dec_Inst, encoded_data,
203                               (int16_t)len, decoded_data,&speechType);
204    } else {
205      len=WebRtcIlbcfix_DecodePlc(Dec_Inst, decoded_data, 1);
206    }
207    fprintf(stderr, "\r");
208
209    /* write output file */
210
211    if (fwrite(decoded_data, sizeof(int16_t), len,
212               ofileid) != (size_t)len) {
213      return -1;
214    }
215  }
216
217  /* close files */
218
219  fclose(ifileid);  fclose(efileid); fclose(ofileid);
220  if (argc==6) {
221    fclose(cfileid);
222  }
223
224  /* Free structs */
225  WebRtcIlbcfix_EncoderFree(Enc_Inst);
226  WebRtcIlbcfix_DecoderFree(Dec_Inst);
227
228
229  printf("\nDone with simulation\n\n");
230
231  return(0);
232}
233