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 File: AAC_E_SAMPLES.h 18 19 Content: sample code for AAC encoder 20 21 *******************************************************************************/ 22 23 #include <dlfcn.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <time.h> 28 #include "voAAC.h" 29 #include "cmnMemory.h" 30 31 #define VO_AAC_E_OUTPUT 1 32 #define READ_SIZE (1024*8) 33 unsigned char outBuf[1024*8]; 34 unsigned char inBuf[READ_SIZE]; 35 36 const char* HelpString = 37 "VisualOn AAC encoder Usage:\n" 38 "voAACEncTest -if <inputfile.pcm> -of <outputfile.aac> -sr <samplerate> -ch <channel> -br <bitrate> -adts <adts> \n" 39 "-if input file name \n" 40 "-of output file name \n" 41 "-sr input pcm samplerate, default 44100 \n" 42 "-ch input pcm channel, default 2 channel \n" 43 "-br encoded aac bitrate, default 64000 * (samplerate/100)*channel/441(480)\n" 44 "-adts add or no adts header, default add adts header\n" 45 "For example: \n" 46 "./voAACEncTest -if raw.pcm -of raw.aac -sr 44100 -ch 2 -br 128000\n"; 47 48 static int parsecmdline(int argc, char **argv,char **input_filename, char **output_filename, AACENC_PARAM *param) 49 { 50 // notice that: 51 // bitRate/nChannels > 8000 52 // bitRate/nChannels < 160000 53 // bitRate/nChannels < sampleRate*6 54 param->adtsUsed = 1; 55 param->bitRate = 0; 56 param->nChannels = 2; 57 param->sampleRate = 44100; 58 59 if(argc < 5 || argc > 13) 60 { 61 return -1; 62 } 63 64 argc--; 65 argv++; 66 while (argc > 0) 67 { 68 if (!strcmp(*argv, "-if")) 69 { 70 argv++; 71 argc--; 72 *input_filename = *argv; 73 } 74 else if (!strcmp(*argv, "-of")) 75 { 76 argv++; 77 argc--; 78 *output_filename = *argv; 79 } 80 else if (!strcmp(*argv, "-sr")) 81 { 82 argv++; 83 argc--; 84 param->sampleRate = atoi(*argv); 85 } 86 else if (!strcmp(*argv, "-ch")) 87 { 88 argv++; 89 argc--; 90 param->nChannels = atoi(*argv); 91 } 92 else if (!strcmp(*argv, "-br")) 93 { 94 argv++; 95 argc--; 96 param->bitRate = atoi(*argv); 97 } 98 else if(!strcmp(*argv, "-adts")) 99 { 100 argv++; 101 argc--; 102 param->adtsUsed = atoi(*argv); 103 } 104 else 105 { 106 return -1; 107 } 108 109 argv++; 110 argc--; 111 } 112 113 if(param->bitRate == 0) 114 { 115 int scale = 441; 116 if(param->sampleRate%8000 == 0) 117 scale = 480; 118 param->bitRate = 640*param->nChannels*param->sampleRate/scale; 119 } 120 121 return 0; 122 } 123 124 int ReadFile2Buf(FILE* infile,unsigned char* dest,int readSize) 125 { 126 int readBytes = 0; 127 readBytes = fread(dest, 1, readSize, infile); 128 return readBytes; 129 } 130 131 typedef int (VO_API * VOGETAUDIODECAPI) (VO_AUDIO_CODECAPI * pDecHandle); 132 133 int main(int argc, char **argv) 134 { 135 FILE *infile, *outfile; 136 int t1, t2; 137 VO_AUDIO_CODECAPI AudioAPI; 138 VO_MEM_OPERATOR moper; 139 VO_CODEC_INIT_USERDATA useData; 140 VO_HANDLE hCodec; 141 VO_CODECBUFFER inData; 142 VO_CODECBUFFER outData; 143 VO_AUDIO_OUTPUTINFO outInfo; 144 int firstWrite = 1; 145 int eofFile = 0; 146 int *info=(int*)inBuf; 147 int bytesLeft, nRead; 148 int EncoderdFrame = 0; 149 int total = 0; 150 int isOutput = 1; 151 int returnCode; 152 AACENC_PARAM aacpara; 153 void *handle; 154 void *pfunc; 155 VOGETAUDIODECAPI pGetAPI; 156 const char *infileName = NULL; 157 const char *outfileName = NULL; 158 159 returnCode = parsecmdline(argc,argv, &infileName, &outfileName, &aacpara); 160 if(returnCode) 161 { 162 printf("%s", HelpString); 163 return 0; 164 } 165 166 /* open input file */ 167 infile = fopen(infileName, "rb"); 168 if (!infile) { 169 printf("Open input file fail..."); 170 return -1; 171 } 172 173 /* open output file */ 174 if(isOutput) 175 { 176 outfile = fopen(outfileName, "wb"); 177 if (!outfile) { 178 printf("Open output file fail..."); 179 return -1; 180 } 181 } 182 // set memory operators; 183 moper.Alloc = cmnMemAlloc; 184 moper.Copy = cmnMemCopy; 185 moper.Free = cmnMemFree; 186 moper.Set = cmnMemSet; 187 moper.Check = cmnMemCheck; 188 useData.memflag = VO_IMF_USERMEMOPERATOR; 189 useData.memData = (VO_PTR)(&moper); 190 // open encoder dll; 191 handle = dlopen("libstagefright.so", RTLD_NOW); 192 if(handle == 0) 193 { 194 printf("open dll error......"); 195 return -1; 196 } 197 // Get API; 198 pfunc = dlsym(handle, "voGetAACEncAPI"); 199 if(pfunc == 0) 200 { 201 printf("open function error......"); 202 return -1; 203 } 204 pGetAPI = (VOGETAUDIODECAPI)pfunc; 205 returnCode = pGetAPI(&AudioAPI); 206 if(returnCode) 207 return -1; 208 209 210 //####################################### Init Encoding Section ######################################### 211 returnCode = AudioAPI.Init(&hCodec, VO_AUDIO_CodingAAC, &useData); 212 if(returnCode < 0) 213 { 214 printf("#### VOI_Error2:fail to initialize the Encoderr###\n"); 215 return -1; 216 } 217 218 returnCode = AudioAPI.SetParam(hCodec, VO_PID_AAC_ENCPARAM, &aacpara); 219 220 inData.Buffer = inBuf; 221 bytesLeft = ReadFile2Buf(infile,inData.Buffer,READ_SIZE); 222 223 //####################################### Encoding Section ######################################### 224 225 do { 226 227 inData.Length = bytesLeft; 228 outData.Buffer = outBuf; 229 outData.Length = 1024*8; 230 231 t1 = clock(); 232 233 returnCode = AudioAPI.SetInputData(hCodec,&inData); 234 235 do { 236 outData.Buffer = outBuf; 237 outData.Length = 1024*8; 238 239 returnCode = AudioAPI.GetOutputData(hCodec,&outData, &outInfo); 240 241 if(returnCode == 0) 242 EncoderdFrame++; 243 if(returnCode == VO_ERR_LICENSE_ERROR) 244 break; 245 246 #if VO_AAC_E_OUTPUT 247 if (isOutput && returnCode == 0) 248 { 249 fwrite(outData.Buffer, 1, outData.Length, outfile); 250 } 251 #endif 252 } while(returnCode != (VO_ERR_INPUT_BUFFER_SMALL)); 253 254 if(returnCode == VO_ERR_LICENSE_ERROR) 255 break; 256 257 t2 = clock(); 258 total += t2 - t1; 259 260 if (!eofFile) { 261 nRead = ReadFile2Buf(infile, inBuf,READ_SIZE); 262 bytesLeft = nRead; 263 inData.Buffer = inBuf; 264 if (feof(infile)) 265 eofFile = 1; 266 } 267 268 } while (!eofFile && returnCode); 269 270 271 //################################################ End Encoding Section ####################################################### 272 returnCode = AudioAPI.Uninit(hCodec); 273 274 fclose(infile); 275 if (outfile) 276 { 277 fclose(outfile); 278 } 279 dlclose(handle); 280 return 0; 281 } 282 283 284