xref: /aosp_15_r20/external/webrtc/modules/audio_coding/codecs/ilbc/test/iLBC_test.c (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "modules/audio_coding/codecs/ilbc/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 
main(int argc,char * argv[])43 int main(int argc, char* argv[])
44 {
45 
46   FILE *ifileid,*efileid,*ofileid, *cfileid;
47   int16_t data[BLOCKL_MAX];
48   uint8_t encoded_data[2 * ILBCNOOFWORDS_MAX];
49   int16_t decoded_data[BLOCKL_MAX];
50   int len_int, mode;
51   short pli;
52   int blockcount = 0;
53   size_t frameLen, len, len_i16s;
54   int16_t speechType;
55   IlbcEncoderInstance *Enc_Inst;
56   IlbcDecoderInstance *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 = (size_t)(mode*8);
155 
156   /* loop over input blocks */
157 
158   while (fread(data,sizeof(int16_t),frameLen,ifileid) == frameLen) {
159 
160     blockcount++;
161 
162     /* encoding */
163 
164     fprintf(stderr, "--- Encoding block %i --- ",blockcount);
165     len_int = WebRtcIlbcfix_Encode(Enc_Inst, data, frameLen, encoded_data);
166     if (len_int < 0) {
167       fprintf(stderr, "Error encoding\n");
168       exit(0);
169     }
170     len = (size_t)len_int;
171     fprintf(stderr, "\r");
172 
173     /* write byte file */
174 
175     len_i16s = (len + 1) / sizeof(int16_t);
176     if (fwrite(encoded_data, sizeof(int16_t), len_i16s, efileid) != len_i16s) {
177       return -1;
178     }
179 
180     /* get channel data if provided */
181     if (argc==6) {
182       if (fread(&pli, sizeof(int16_t), 1, cfileid)) {
183         if ((pli!=0)&&(pli!=1)) {
184           fprintf(stderr, "Error in channel file\n");
185           exit(0);
186         }
187         if (pli==0) {
188           /* Packet loss -> remove info from frame */
189           memset(encoded_data, 0,
190                  sizeof(int16_t)*ILBCNOOFWORDS_MAX);
191         }
192       } else {
193         fprintf(stderr, "Error. Channel file too short\n");
194         exit(0);
195       }
196     } else {
197       pli=1;
198     }
199 
200     /* decoding */
201 
202     fprintf(stderr, "--- Decoding block %i --- ",blockcount);
203     if (pli==1) {
204       len_int=WebRtcIlbcfix_Decode(Dec_Inst, encoded_data,
205                                    len, decoded_data,&speechType);
206       if (len_int < 0) {
207         fprintf(stderr, "Error decoding\n");
208         exit(0);
209       }
210       len = (size_t)len_int;
211     } else {
212       len=WebRtcIlbcfix_DecodePlc(Dec_Inst, decoded_data, 1);
213     }
214     fprintf(stderr, "\r");
215 
216     /* write output file */
217 
218     if (fwrite(decoded_data, sizeof(int16_t), len, ofileid) != len) {
219       return -1;
220     }
221   }
222 
223   /* close files */
224 
225   fclose(ifileid);  fclose(efileid); fclose(ofileid);
226   if (argc==6) {
227     fclose(cfileid);
228   }
229 
230   /* Free structs */
231   WebRtcIlbcfix_EncoderFree(Enc_Inst);
232   WebRtcIlbcfix_DecoderFree(Dec_Inst);
233 
234 
235   printf("\nDone with simulation\n\n");
236 
237   return(0);
238 }
239