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