void Java_com_girfa_apps_teamtalk4mobile_api_jni_AACEncoder_encode( JNIEnv* env, jobject thiz, jbyteArray inputArray) { LOG("writing to handle: %x", handle); jbyte* buffer = (*env)->GetByteArrayElements(env, inputArray, (jboolean) 0); int inputSize = (*env)->GetArrayLength(env, inputArray); VO_CODECBUFFER input = { 0 }, output = { 0 }; VO_AUDIO_OUTPUTINFO output_info = { 0 }; int readSize = params.nChannels * 2 * 1024; uint16_t* outbuf = (uint16_t*) malloc(readSize * 2); LOG("input buffer: %d", inputSize); /* GET OUTPUT DATA */ int i; for(i = 0; i < inputSize; i += readSize) { input.Buffer = buffer + i; input.Length = readSize; codec_api.SetInputData(handle, &input); output.Buffer = outbuf; output.Length = readSize * 2; int status = codec_api.GetOutputData(handle, &output, &output_info); if (status == VO_ERR_INPUT_BUFFER_SMALL) break; if (status == VO_ERR_OUTPUT_BUFFER_SMALL) { LOG("output buffer was too small, read %d", output_info.InputUsed); } else if (status != VO_ERR_NONE) { char message[100]; sprintf(message, "Unable to encode frame: %x", status); throwException(env, "java/lang/RuntimeException", message); return; } fwrite(outbuf, 1, output.Length, outfile); } LOG("finished output"); (*env)->ReleaseByteArrayElements(env, inputArray, buffer, JNI_ABORT); free(outbuf); }
void Java_com_todoroo_aacenc_AACEncoder_init( JNIEnv* env, jobject thiz, int bitrate, int channels, int sampleRate, int bitsPerSample, jstring outputFile) { if (bitsPerSample != 16) { throwException(env, "java/lang/IllegalArgumentException", "Unsupported sample depth. Only 16 bits per sample is supported"); return; } voGetAACEncAPI(&codec_api); mem_operator.Alloc = cmnMemAlloc; mem_operator.Copy = cmnMemCopy; mem_operator.Free = cmnMemFree; mem_operator.Set = cmnMemSet; mem_operator.Check = cmnMemCheck; user_data.memflag = VO_IMF_USERMEMOPERATOR; user_data.memData = &mem_operator; codec_api.Init(&handle, VO_AUDIO_CodingAAC, &user_data); params.sampleRate = sampleRate; params.bitRate = bitrate; params.nChannels = channels; params.adtsUsed = 1; if (codec_api.SetParam(handle, VO_PID_AAC_ENCPARAM, ¶ms) != VO_ERR_NONE) { throwException(env, "java/lang/IllegalArgumentException", "Unable to set encoding parameters"); return; } const char* output_file = (*env)->GetStringUTFChars(env, outputFile, (jboolean) 0); outfile = fopen(output_file, "wb"); LOG("writing to %s", output_file); (*env)->ReleaseStringUTFChars(env, outputFile, output_file); LOG("initialized handle: %x", handle); }
void Java_com_girfa_apps_teamtalk4mobile_api_jni_AACEncoder_uninit( JNIEnv* env, jobject thiz) { fclose(outfile); codec_api.Uninit(handle); }
void Java_com_todoroo_aacenc_AACEncoder_uninit( JNIEnv* env, jobject thiz) { fclose(outfile); codec_api.Uninit(handle); }
int main(int argc, char **argv) { FILE *infile, *outfile; int t1, t2; VO_AUDIO_CODECAPI AudioAPI; VO_MEM_OPERATOR moper; VO_CODEC_INIT_USERDATA useData; VO_HANDLE hCodec; VO_CODECBUFFER inData; VO_CODECBUFFER outData; VO_AUDIO_OUTPUTINFO outInfo; int firstWrite = 1; int eofFile = 0; int *info=(int*)inBuf; int bytesLeft, nRead; int EncoderdFrame = 0; int total = 0; int isOutput = 1; int returnCode; AACENC_PARAM aacpara; void *handle; void *pfunc; VOGETAUDIODECAPI pGetAPI; const char *infileName = NULL; const char *outfileName = NULL; returnCode = parsecmdline(argc,argv, &infileName, &outfileName, &aacpara); if(returnCode) { printf("%s", HelpString); return 0; } /* open input file */ infile = fopen(infileName, "rb"); if (!infile) { printf("Open input file fail..."); return -1; } /* open output file */ if(isOutput) { outfile = fopen(outfileName, "wb"); if (!outfile) { printf("Open output file fail..."); return -1; } } // set memory operators; moper.Alloc = cmnMemAlloc; moper.Copy = cmnMemCopy; moper.Free = cmnMemFree; moper.Set = cmnMemSet; moper.Check = cmnMemCheck; useData.memflag = VO_IMF_USERMEMOPERATOR; useData.memData = (VO_PTR)(&moper); // open encoder dll; handle = dlopen("libstagefright.so", RTLD_NOW); if(handle == 0) { printf("open dll error......"); return -1; } // Get API; pfunc = dlsym(handle, "voGetAACEncAPI"); if(pfunc == 0) { printf("open function error......"); return -1; } pGetAPI = (VOGETAUDIODECAPI)pfunc; returnCode = pGetAPI(&AudioAPI); if(returnCode) return -1; //####################################### Init Encoding Section ######################################### returnCode = AudioAPI.Init(&hCodec, VO_AUDIO_CodingAAC, &useData); if(returnCode < 0) { printf("#### VOI_Error2:fail to initialize the Encoderr###\n"); return -1; } returnCode = AudioAPI.SetParam(hCodec, VO_PID_AAC_ENCPARAM, &aacpara); inData.Buffer = inBuf; bytesLeft = ReadFile2Buf(infile,inData.Buffer,READ_SIZE); //####################################### Encoding Section ######################################### do { inData.Length = bytesLeft; outData.Buffer = outBuf; outData.Length = 1024*8; t1 = clock(); returnCode = AudioAPI.SetInputData(hCodec,&inData); do { outData.Buffer = outBuf; outData.Length = 1024*8; returnCode = AudioAPI.GetOutputData(hCodec,&outData, &outInfo); if(returnCode == 0) EncoderdFrame++; if(returnCode == VO_ERR_LICENSE_ERROR) break; #if VO_AAC_E_OUTPUT if (isOutput && returnCode == 0) { fwrite(outData.Buffer, 1, outData.Length, outfile); } #endif } while(returnCode != (VO_ERR_INPUT_BUFFER_SMALL)); if(returnCode == VO_ERR_LICENSE_ERROR) break; t2 = clock(); total += t2 - t1; if (!eofFile) { nRead = ReadFile2Buf(infile, inBuf,READ_SIZE); bytesLeft = nRead; inData.Buffer = inBuf; if (feof(infile)) eofFile = 1; } } while (!eofFile && returnCode); //################################################ End Encoding Section ####################################################### returnCode = AudioAPI.Uninit(hCodec); fclose(infile); if (outfile) { fclose(outfile); } dlclose(handle); return 0; }
int encode( int mode, short allow_dtx, VOAMRWBFRAMETYPE frameType, const char* srcfile, const char* dstfile ) { int ret = 0; int returnCode; FILE *fsrc = NULL; FILE *fdst = NULL; int framenum = 0; int eofFile = 0; int size1 = 0; int Relens; VO_AUDIO_CODECAPI AudioAPI; VO_MEM_OPERATOR moper; VO_CODEC_INIT_USERDATA useData; VO_HANDLE hCodec; VO_CODECBUFFER inData; VO_CODECBUFFER outData; VO_AUDIO_OUTPUTINFO outFormat; unsigned char *inBuf = InputBuf; unsigned char *outBuf = OutputBuf; #ifdef LINUX void *handle = NULL; void *pfunc; VOGETAUDIOENCAPI pGetAPI; #endif clock_t start, finish; double duration = 0.0; if ((fsrc = fopen (srcfile, "rb")) == NULL) { ret = -1; goto safe_exit; } if ((fdst = fopen (dstfile, "wb")) == NULL) { ret = -1; goto safe_exit; } moper.Alloc = cmnMemAlloc; moper.Copy = cmnMemCopy; moper.Free = cmnMemFree; moper.Set = cmnMemSet; moper.Check = cmnMemCheck; useData.memflag = VO_IMF_USERMEMOPERATOR; useData.memData = (VO_PTR)(&moper); #ifdef LINUX handle = dlopen("/data/local/tmp/voAMRWBEnc.so", RTLD_NOW); if(handle == 0) { printf("open dll error......"); return -1; } pfunc = dlsym(handle, "voGetAMRWBEncAPI"); if(pfunc == 0) { printf("open function error......"); return -1; } pGetAPI = (VOGETAUDIOENCAPI)pfunc; returnCode = pGetAPI(&AudioAPI); if(returnCode) { printf("get APIs error......"); return -1; } #else ret = voGetAMRWBEncAPI(&AudioAPI); if(ret) { ret = -1; printf("get APIs error......"); goto safe_exit; } #endif //####################################### Init Encoding Section ######################################### ret = AudioAPI.Init(&hCodec, VO_AUDIO_CodingAMRWB, &useData); if(ret) { ret = -1; printf("APIs init error......"); goto safe_exit; } Relens = GetNextBuf(fsrc,InputBuf,INPUT_SIZE); if(Relens!=INPUT_SIZE && !feof(fsrc)) { ret = -1; //Invalid magic number printf("get next buffer error......"); goto safe_exit; } //###################################### set encode Mode ################################################## ret = AudioAPI.SetParam(hCodec, VO_PID_AMRWB_FRAMETYPE, &frameType); ret = AudioAPI.SetParam(hCodec, VO_PID_AMRWB_MODE, &mode); ret = AudioAPI.SetParam(hCodec, VO_PID_AMRWB_DTX, &allow_dtx); if(frameType == VOAMRWB_RFC3267) { /* write RFC3267 Header info to indicate single channel AMR file storage format */ size1 = (int)strlen(VOAMRWB_RFC3267_HEADER_INFO); memcpy(outBuf, VOAMRWB_RFC3267_HEADER_INFO, size1); outBuf += size1; } //####################################### Encoding Section ######################################### printf(" \n ---------------- Running -------------------------\n "); do{ inData.Buffer = (unsigned char *)inBuf; inData.Length = Relens; outData.Buffer = outBuf; start = clock(); /* decode one amr block */ returnCode = AudioAPI.SetInputData(hCodec,&inData); do { returnCode = AudioAPI.GetOutputData(hCodec,&outData, &outFormat); if(returnCode == 0) { framenum++; printf(" Frames processed: %hd\r", framenum); if(framenum == 1) { fwrite(OutputBuf, 1, outData.Length + size1, fdst); fflush(fdst); } else { fwrite(outData.Buffer, 1, outData.Length, fdst); fflush(fdst); } } else if(returnCode == VO_ERR_LICENSE_ERROR) { printf("Encoder time reach upper limit......"); goto safe_exit; } } while(returnCode != VO_ERR_INPUT_BUFFER_SMALL); finish = clock(); duration += finish - start; if (!eofFile) { Relens = GetNextBuf(fsrc, InputBuf, INPUT_SIZE); inBuf = InputBuf; if (feof(fsrc) && Relens == 0) eofFile = 1; } } while (!eofFile && returnCode); //####################################### End Encoding Section ######################################### safe_exit: returnCode = AudioAPI.Uninit(hCodec); printf( "\n%2.5f seconds\n", (double)duration/CLOCKS_PER_SEC); if (fsrc) fclose(fsrc); if (fdst) fclose(fdst); #ifdef LINUX dlclose(handle); #endif return ret; }