1 /*
2 ** Copyright 2003-2010, VisualOn, Inc.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17
18 #ifdef LINUX
19 #include <dlfcn.h>
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include "voAMRWB.h"
27 #include "cmnMemory.h"
28
29 #define VOAMRWB_RFC3267_HEADER_INFO "#!AMR-WB\n"
30
31 #define INPUT_SIZE 640
32 #define OUTPUT_SIZE 1024
33 unsigned char InputBuf[INPUT_SIZE];
34 unsigned char OutputBuf[OUTPUT_SIZE];
35
usage(void)36 void usage (void) {
37 printf ("AMR_WB Encoder HELP Displays this text\n");
38 printf ("\n");
39 printf ("Usage:\n");
40 printf ("AMRWBEnc [options] Input_file output_file \n");
41 printf ("\n");
42 printf ("Options +M* +F* +DTX \n");
43 printf ("Support \n");
44 printf ("Options +M* for seting compression bitrate mode, default is 23.85kbps\n");
45 printf (" +M0 = 6.6kbps \n");
46 printf (" +M1 = 8.85kbps \n");
47 printf (" +M2 = 12.65kbps \n");
48 printf (" +M3 = 14.25kbps \n");
49 printf (" +M4 = 15.58kbps \n");
50 printf (" +M5 = 18.25kbps \n");
51 printf (" +M6 = 19.85kbps \n");
52 printf (" +M7 = 23.05kbps \n");
53 printf (" +M8 = 23.85kbps \n");
54 printf ("\n");
55 printf ("Options +F* for setting output frame Type, default is RFC3267 \n");
56 printf ("+F0 for AMR_WB Defualt bit extern short data frame type \n");
57 printf ("+F1 for AMR_WB_ITU bit extern short data frame type \n");
58 printf ("+F2 for RFC3267\n ");
59 printf ("\n");
60 printf ("Options +DTX enable DTX mode, default is disable.\n");
61 printf ("File names, input raw PCM data, and output is AMR_WB bit-stream file.\n");
62 printf ("\n");
63 }
64
GetNextBuf(FILE * inFile,unsigned char * dst,int size)65 int GetNextBuf(FILE* inFile,unsigned char* dst,int size)
66 {
67 int size2 = (int)fread(dst, sizeof(signed char), size,inFile);
68 return size2;
69 }
70
71 typedef int (VO_API * VOGETAUDIOENCAPI) (VO_AUDIO_CODECAPI * pEncHandle);
72
encode(int mode,short allow_dtx,VOAMRWBFRAMETYPE frameType,const char * srcfile,const char * dstfile)73 int encode(
74 int mode,
75 short allow_dtx,
76 VOAMRWBFRAMETYPE frameType,
77 const char* srcfile,
78 const char* dstfile
79 )
80 {
81 int ret = 0;
82 int returnCode;
83 FILE *fsrc = NULL;
84 FILE *fdst = NULL;
85 int framenum = 0;
86 int eofFile = 0;
87 int size1 = 0;
88 int Relens;
89
90 VO_AUDIO_CODECAPI AudioAPI;
91 VO_MEM_OPERATOR moper;
92 VO_CODEC_INIT_USERDATA useData;
93 VO_HANDLE hCodec = NULL;
94 VO_CODECBUFFER inData;
95 VO_CODECBUFFER outData;
96 VO_AUDIO_OUTPUTINFO outFormat;
97
98 unsigned char *inBuf = InputBuf;
99 unsigned char *outBuf = OutputBuf;
100
101
102 #ifdef LINUX
103 void *handle = NULL;
104 void *pfunc;
105 VOGETAUDIOENCAPI pGetAPI;
106 #endif
107
108 clock_t start, finish;
109 double duration = 0.0;
110
111 if ((fsrc = fopen (srcfile, "rb")) == NULL)
112 {
113 ret = -1;
114 goto safe_exit;
115 }
116
117 if ((fdst = fopen (dstfile, "wb")) == NULL)
118 {
119 ret = -1;
120 goto safe_exit;
121 }
122
123 moper.Alloc = cmnMemAlloc;
124 moper.Copy = cmnMemCopy;
125 moper.Free = cmnMemFree;
126 moper.Set = cmnMemSet;
127 moper.Check = cmnMemCheck;
128
129 useData.memflag = VO_IMF_USERMEMOPERATOR;
130 useData.memData = (VO_PTR)(&moper);
131
132 #ifdef LINUX
133 handle = dlopen("libstagefright.so", RTLD_NOW);
134 if(handle == 0)
135 {
136 printf("open dll error......");
137 ret = -1;
138 goto safe_exit;
139 }
140
141 pfunc = dlsym(handle, "voGetAMRWBEncAPI");
142 if(pfunc == 0)
143 {
144 printf("open function error......");
145 ret = -1;
146 goto safe_exit;
147 }
148
149 pGetAPI = (VOGETAUDIOENCAPI)pfunc;
150
151 returnCode = pGetAPI(&AudioAPI);
152 if(returnCode)
153 {
154 printf("get APIs error......");
155 ret = -1;
156 goto safe_exit;
157 }
158 #else
159 ret = voGetAMRWBEncAPI(&AudioAPI);
160 if(ret)
161 {
162 ret = -1;
163 printf("get APIs error......");
164 goto safe_exit;
165 }
166 #endif
167
168 //####################################### Init Encoding Section #########################################
169 ret = AudioAPI.Init(&hCodec, VO_AUDIO_CodingAMRWB, &useData);
170
171 if(ret)
172 {
173 ret = -1;
174 printf("APIs init error......");
175 goto safe_exit;
176 }
177
178 Relens = GetNextBuf(fsrc,InputBuf,INPUT_SIZE);
179 if(Relens!=INPUT_SIZE && !feof(fsrc))
180 {
181 ret = -1; //Invalid magic number
182 printf("get next buffer error......");
183 goto safe_exit;
184 }
185
186 //###################################### set encode Mode ##################################################
187 ret = AudioAPI.SetParam(hCodec, VO_PID_AMRWB_FRAMETYPE, &frameType);
188 ret = AudioAPI.SetParam(hCodec, VO_PID_AMRWB_MODE, &mode);
189 ret = AudioAPI.SetParam(hCodec, VO_PID_AMRWB_DTX, &allow_dtx);
190
191 if(frameType == VOAMRWB_RFC3267)
192 {
193 /* write RFC3267 Header info to indicate single channel AMR file storage format */
194 size1 = (int)strlen(VOAMRWB_RFC3267_HEADER_INFO);
195 memcpy(outBuf, VOAMRWB_RFC3267_HEADER_INFO, size1);
196 outBuf += size1;
197 }
198
199 //####################################### Encoding Section #########################################
200 printf(" \n ---------------- Running -------------------------\n ");
201
202 do{
203 inData.Buffer = (unsigned char *)inBuf;
204 inData.Length = Relens;
205 outData.Buffer = outBuf;
206
207 start = clock();
208
209 /* decode one amr block */
210 returnCode = AudioAPI.SetInputData(hCodec,&inData);
211
212 do {
213 returnCode = AudioAPI.GetOutputData(hCodec,&outData, &outFormat);
214 if(returnCode == 0)
215 {
216 framenum++;
217 printf(" Frames processed: %d\r", framenum);
218 if(framenum == 1)
219 {
220 fwrite(OutputBuf, 1, outData.Length + size1, fdst);
221 fflush(fdst);
222 }
223 else
224 {
225 fwrite(outData.Buffer, 1, outData.Length, fdst);
226 fflush(fdst);
227 }
228 }
229 else if((unsigned)returnCode == VO_ERR_LICENSE_ERROR)
230 {
231 printf("Encoder time reach upper limit......");
232 goto safe_exit;
233 }
234 } while((unsigned)returnCode != VO_ERR_INPUT_BUFFER_SMALL);
235
236 finish = clock();
237 duration += finish - start;
238
239 if (!eofFile) {
240 Relens = GetNextBuf(fsrc, InputBuf, INPUT_SIZE);
241 inBuf = InputBuf;
242 if (feof(fsrc) && Relens == 0)
243 eofFile = 1;
244 }
245 } while (!eofFile && returnCode);
246 //####################################### End Encoding Section #########################################
247
248 safe_exit:
249 returnCode = AudioAPI.Uninit(hCodec);
250
251 printf( "\n%2.5f seconds\n", (double)duration/CLOCKS_PER_SEC);
252
253 if (fsrc)
254 fclose(fsrc);
255 if (fdst)
256 fclose(fdst);
257
258 #ifdef LINUX
259 if (handle)
260 dlclose(handle);
261 #endif
262
263 return ret;
264 }
265
main(int argc,char ** argv)266 int main(int argc, char **argv) // for gcc compiler;
267 {
268 int mode, r;
269 int arg, filename=0;
270 char *inFileName = NULL;
271 char *outFileName = NULL;
272 short allow_dtx;
273 VOAMRWBFRAMETYPE frameType;
274
275 printf("\n");
276 printf("************************Adaptive Multi-Rate Wide Band Encoder (AMR-WB)*******************************\n");
277 printf("***********************************DEFINITIONS:*******************************************************\n");
278 printf("AMR-WB encoder scheme is based on the principle of Algebraic Code Excited Linear Prediction algorithm\n");
279 printf("The AMR-WB encoder compression MONO liner PCM speech input data at 16kHz sampling rate\n");
280 printf("to one of nine data rate modes-6.60, 8.85, 12.65, 14.25, 15.85, 18.25, 19.25, 23.05 and 23.85kbps.\n");
281 printf("The encoder supports output format AMRWB ITU, AMRWB RFC3267.\n");
282 printf("\n");
283
284 /*Encoder Default setting */
285 mode = VOAMRWB_MD2385;
286 allow_dtx = 0;
287 frameType = VOAMRWB_RFC3267;
288
289 if(argc < 3){
290 usage();
291 return 0;
292 }else{
293 for (arg = 1; arg < argc; arg++) {
294 if (argv [arg] [0] == '+') {
295 if(argv[arg][1] == 'M')
296 {
297 switch(argv[arg][2])
298 {
299 case '0': mode = VOAMRWB_MD66;
300 break;
301 case '1': mode = VOAMRWB_MD885;
302 break;
303 case '2': mode = VOAMRWB_MD1265;
304 break;
305 case '3': mode = VOAMRWB_MD1425;
306 break;
307 case '4': mode = VOAMRWB_MD1585;
308 break;
309 case '5': mode = VOAMRWB_MD1825;
310 break;
311 case '6': mode = VOAMRWB_MD1985;
312 break;
313 case '7': mode = VOAMRWB_MD2305;
314 break;
315 case '8': mode = VOAMRWB_MD2385;
316 break;
317 default:
318 usage();
319 printf ("Invalid parameter '%s'.\n", argv [arg]);
320 break;
321 }
322 }else if(argv[arg][1] == 'F')
323 {
324 switch(argv[arg][2])
325 {
326 case '0': frameType = VOAMRWB_DEFAULT;
327 break;
328 case '1': frameType = VOAMRWB_ITU;
329 break;
330 case '2': frameType = VOAMRWB_RFC3267 ;
331 break;
332 default:
333 usage();
334 printf ("Invalid parameter '%s'.\n", argv [arg]);
335 break;
336
337
338 }
339 }else if(strcmp (argv[arg], "+DTX") == 0)
340 {
341 allow_dtx = 1;
342 }
343
344 } else {
345 switch (filename) {
346 case 0:
347 inFileName = argv[arg];
348 break;
349 case 1:
350 outFileName = argv[arg];
351 break;
352 default:
353 usage ();
354 fprintf (stderr, "Invalid parameter '%s'.\n", argv [arg]);
355 return 0;
356 }
357 filename++;
358 }
359 }
360 }
361
362 r = encode(mode, allow_dtx, frameType, inFileName, outFileName);
363 if(r)
364 {
365 fprintf(stderr, "error: %d\n", r);
366 }
367 return r;
368 }
369
370