1 /*
2  *  Copyright (c) 2011 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 <math.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <time.h>
24 #include "ilbc.h"
25 
26 //#define JUNK_DATA
27 #ifdef JUNK_DATA
28 #define SEED_FILE "randseed.txt"
29 #endif
30 
31 
32 /*----------------------------------------------------------------*
33 *  Main program to test iLBC encoding and decoding
34 *
35 *  Usage:
36 *		exefile_name.exe <infile> <bytefile> <outfile>
37 *
38 *---------------------------------------------------------------*/
39 
main(int argc,char * argv[])40 int main(int argc, char* argv[])
41 {
42   FILE *ifileid,*efileid,*ofileid, *chfileid;
43   short encoded_data[55], data[240], speechType;
44   short len, mode, pli;
45   int blockcount = 0;
46 
47   IlbcEncoderInstance *Enc_Inst;
48   IlbcDecoderInstance *Dec_Inst;
49 #ifdef JUNK_DATA
50   int i;
51   FILE *seedfile;
52   unsigned int random_seed = (unsigned int) time(NULL);//1196764538
53 #endif
54 
55   /* Create structs */
56   WebRtcIlbcfix_EncoderCreate(&Enc_Inst);
57   WebRtcIlbcfix_DecoderCreate(&Dec_Inst);
58 
59   /* get arguments and open files */
60 
61   if (argc != 6 ) {
62     fprintf(stderr, "%s mode inputfile bytefile outputfile channelfile\n",
63             argv[0]);
64     fprintf(stderr, "Example:\n");
65     fprintf(stderr, "%s <30,20> in.pcm byte.dat out.pcm T30.0.dat\n", argv[0]);
66     exit(1);
67   }
68   mode=atoi(argv[1]);
69   if (mode != 20 && mode != 30) {
70     fprintf(stderr,"Wrong mode %s, must be 20, or 30\n", argv[1]);
71     exit(2);
72   }
73   if ( (ifileid=fopen(argv[2],"rb")) == NULL) {
74     fprintf(stderr,"Cannot open input file %s\n", argv[2]);
75     exit(2);}
76   if ( (efileid=fopen(argv[3],"wb")) == NULL) {
77     fprintf(stderr, "Cannot open channelfile file %s\n",
78             argv[3]); exit(3);}
79   if( (ofileid=fopen(argv[4],"wb")) == NULL) {
80     fprintf(stderr, "Cannot open output file %s\n",
81             argv[4]); exit(3);}
82   if ( (chfileid=fopen(argv[5],"rb")) == NULL) {
83     fprintf(stderr,"Cannot open channel file file %s\n", argv[5]);
84     exit(2);
85   }
86   /* print info */
87   fprintf(stderr, "\n");
88   fprintf(stderr,
89           "*---------------------------------------------------*\n");
90   fprintf(stderr,
91           "*                                                   *\n");
92   fprintf(stderr,
93           "*      iLBCtest                                     *\n");
94   fprintf(stderr,
95           "*                                                   *\n");
96   fprintf(stderr,
97           "*                                                   *\n");
98   fprintf(stderr,
99 		"*---------------------------------------------------*\n");
100 #ifdef SPLIT_10MS
101   fprintf(stderr,"\n10ms split with raw mode: %2d ms\n", mode);
102 #else
103   fprintf(stderr,"\nMode          : %2d ms\n", mode);
104 #endif
105   fprintf(stderr,"\nInput file    : %s\n", argv[2]);
106   fprintf(stderr,"Coded file    : %s\n", argv[3]);
107   fprintf(stderr,"Output file   : %s\n\n", argv[4]);
108   fprintf(stderr,"Channel file  : %s\n\n", argv[5]);
109 
110 #ifdef JUNK_DATA
111   srand(random_seed);
112 
113   if ( (seedfile = fopen(SEED_FILE, "a+t") ) == NULL ) {
114     fprintf(stderr, "Error: Could not open file %s\n", SEED_FILE);
115   }
116   else {
117     fprintf(seedfile, "%u\n", random_seed);
118     fclose(seedfile);
119   }
120 #endif
121 
122   /* Initialization */
123   WebRtcIlbcfix_EncoderInit(Enc_Inst, mode);
124   WebRtcIlbcfix_DecoderInit(Dec_Inst, mode);
125 
126   /* loop over input blocks */
127 #ifdef SPLIT_10MS
128   while(fread(data, sizeof(short), 80, ifileid) == 80) {
129 #else
130   while((short)fread(data,sizeof(short),(mode<<3),ifileid)==(mode<<3)) {
131 #endif
132     blockcount++;
133 
134     /* encoding */
135     fprintf(stderr, "--- Encoding block %i --- ",blockcount);
136 #ifdef SPLIT_10MS
137     len=WebRtcIlbcfix_Encode(Enc_Inst, data, 80, encoded_data);
138 #else
139     len=WebRtcIlbcfix_Encode(Enc_Inst, data, (short)(mode<<3), encoded_data);
140 #endif
141     fprintf(stderr, "\r");
142 
143 #ifdef JUNK_DATA
144     for ( i = 0; i < len; i++) {
145       encoded_data[i] = (short) (encoded_data[i] + (short) rand());
146     }
147 #endif
148     /* write byte file */
149     if(len != 0){ //len may be 0 in 10ms split case
150       fwrite(encoded_data,1,len,efileid);
151     }
152 
153     if(len != 0){ //len may be 0 in 10ms split case
154       /* get channel data if provided */
155       if (argc==6) {
156         if (fread(&pli, sizeof(int16_t), 1, chfileid)) {
157           if ((pli!=0)&&(pli!=1)) {
158             fprintf(stderr, "Error in channel file\n");
159             exit(0);
160           }
161           if (pli==0) {
162             /* Packet loss -> remove info from frame */
163             memset(encoded_data, 0, sizeof(int16_t)*25);
164           }
165         } else {
166           fprintf(stderr, "Error. Channel file too short\n");
167           exit(0);
168         }
169       } else {
170         pli=1;
171       }
172 
173       /* decoding */
174       fprintf(stderr, "--- Decoding block %i --- ",blockcount);
175       if (pli==1) {
176         len=WebRtcIlbcfix_Decode(Dec_Inst, encoded_data, len, data, &speechType);
177       } else {
178         len=WebRtcIlbcfix_DecodePlc(Dec_Inst, data, 1);
179       }
180       fprintf(stderr, "\r");
181 
182       /* write output file */
183       fwrite(data,sizeof(short),len,ofileid);
184     }
185   }
186 
187 #ifdef JUNK_DATA
188   if ( (seedfile = fopen(SEED_FILE, "a+t") ) == NULL ) {
189     fprintf(stderr, "Error: Could not open file %s\n", SEED_FILE);
190   }
191   else {
192     fprintf(seedfile, "ok\n\n");
193     fclose(seedfile);
194   }
195 #endif
196 
197   /* free structs */
198   WebRtcIlbcfix_EncoderFree(Enc_Inst);
199   WebRtcIlbcfix_DecoderFree(Dec_Inst);
200 
201   /* close files */
202   fclose(ifileid);
203   fclose(efileid);
204   fclose(ofileid);
205 
206   return 0;
207 }
208