/*初始化*/ jlong Java_com_vvku_aacencoder_heaacEncInterface_init(JNIEnv* env, jobject thiz, jint samplerate, jint channels, jint bitrate, jint bandwidth, jlongArray param_out, jstring input_file) { __android_log_print(ANDROID_LOG_INFO, "encoderInterface native", "begin init"); jlong *info = (jlong*)(*env)->GetLongArrayElements(env, param_out, 0); Encoder * en = (Encoder *) malloc(sizeof(Encoder)); en->inputInfo = (WavInfo *) malloc(sizeof(WavInfo)); en->bitrate = 16000; en->sampleRateAAC = 44100; unsigned char* wav_file = (char*)(*env)->GetStringUTFChars(env, input_file, 0); FILE *inputfile; inputfile = AuChannelOpen(wav_file, en->inputInfo); if(bitrate > 0){ en->bitrate = bitrate * 1000; } if(!inputfile){ en->inputInfo->nChannels = 2; en->inputInfo->sampleRate = 32000; } if(samplerate > 0){ en->inputInfo->sampleRate = samplerate; } if(channels > 0){ en->inputInfo->nChannels = channels; } en->hEncoder = aacplusEncOpen(en->inputInfo->sampleRate, en->inputInfo->nChannels, &en->inputSamples, &en->maxOutputBytes); info[0] = en->inputSamples*2; info[1] = en->maxOutputBytes; en->cfg = aacplusEncGetCurrentConfiguration(en->hEncoder); en->cfg->bitRate = en->bitrate; en->cfg->bandWidth = 0; en->cfg->outputFormat = 0; // 设置为1的话,会加上adts头,直接保存成aac文件的时候需要 en->cfg->nChannelsOut = en->inputInfo->nChannels; //en->cfg->inputFormat = AACPLUS_INPUT_FLOAT; int ret = 0; if((ret = aacplusEncSetConfiguration(en->hEncoder, en->cfg)) == 0) { __android_log_print(ANDROID_LOG_INFO, "encoderInterface native", "Init failed."); if(inputfile) AuChannelClose(inputfile); (*env)->ReleaseLongArrayElements(env, param_out, info, 0); (*env)->ReleaseStringUTFChars(env, input_file, wav_file); return -2; } if(inputfile) AuChannelClose(inputfile); (*env)->ReleaseLongArrayElements(env, param_out, info, 0); (*env)->ReleaseStringUTFChars(env, input_file, wav_file); __android_log_print(ANDROID_LOG_INFO, "encoderInterface native", "init success."); return (jlong) en; }
LRESULT CMainDlg::OnBtnTest(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { HANDLE_MP4_FILE hMp4File; hAudioChannel inputFile = NULL; AuChanInfo inputInfo; AuChanMode auFlags = AU_CHAN_READ; AuChanType auType = TYPE_AUTODETECT ; /* must be set */ inputInfo.bitsPerSample = 16 ; /* only relevant if valid == 1 */ inputInfo.sampleRate = 44100 ; /* only relevant if valid == 1 */ inputInfo.nChannels = 2 ; /* only relevant if valid == 1 */ inputInfo.nSamples = 0 ; /* only relevant if valid == 1 */ inputInfo.isLittleEndian = 1; inputInfo.fpScaleFactor = AACENC_PCM_LEVEL ; /* must be set */ inputInfo.valid = 1 ; /* must be set */ inputInfo.useWaveExt = 0; char file_path[MAX_PATH] = {0}; GetDlgItemTextA(m_hWnd, IDC_EDT_SOURCE, file_path, MAX_PATH); int ret = AuChannelOpen (&inputFile, file_path, auFlags, &auType, &inputInfo); struct aac_encoder_t *encoder = 0; open_aac_encoder(encoder, inputInfo.sampleRate, inputInfo.sampleRate, inputInfo.nChannels, inputInfo.nChannels); //AACENC_CONFIG config; //AacInitDefaultConfig(&config); //unsigned char ASConfigBuffer[80]; //unsigned int nConfigBits; //unsigned int nConfigBytes; // //memset (ASConfigBuffer, 0, 80); //if (GetMPEG4ASConfig(22050, // 1, // ASConfigBuffer, // &nConfigBits, // 1, // 1) ) { // fprintf(stderr, "\nCould not initialize Audio Specific Config\n"); // exit(10); //} if (encoder) close_aac_encoder(encoder); if (inputFile) AuChannelClose (inputFile); return 0; }
int main(int argc, char *argv[]) { WavInfo inputInfo; FILE *inputFile = NULL; FILE *hADTSFile; int error; int bEncodeMono = 0; int frmCnt = 0; /* * parse command line arguments */ if (argc != 5) { fprintf(stderr, "\nUsage: %s <wav_file> <bitstream_file> <bitrate> <(m)ono/(s)tereo>\n", argv[0]); fprintf(stderr, "\nExample: %s input.wav out.aac 24000 s\n", argv[0]); return 0; } if ( strcmp (argv[4],"m") == 0 ) { bEncodeMono = 1; } else { if ( strcmp (argv[4],"s") != 0 ) { fprintf(stderr, "\nWrong mode %s, use either (m)ono or (s)tereo\n", argv[4]); return 0; } } fflush(stdout); inputFile = AuChannelOpen (argv[1], &inputInfo); if(inputFile == NULL){ fprintf(stderr,"could not open %s\n",argv[1]); exit(10); } if (inputInfo.nChannels==1 && !bEncodeMono) { fprintf(stderr,"Need stereo input for stereo coding mode !\n"); exit(10); } if (strcmp(argv[2],"-")==0) hADTSFile=stdout; else hADTSFile = fopen(argv[2], "wb"); if(!hADTSFile) { fprintf(stderr, "\nFailed to create ADTS file\n") ; exit(10); } /* Be verbose */ unsigned long inputSamples=0; unsigned long maxOutputBytes=0; aacplusEncHandle hEncoder = aacplusEncOpen(inputInfo.sampleRate, inputInfo.nChannels, &inputSamples, &maxOutputBytes); aacplusEncConfiguration *cfg = aacplusEncGetCurrentConfiguration(hEncoder); cfg->bitRate = atoi(argv[3]); cfg->bandWidth = 0; cfg->outputFormat = 1; cfg->nChannelsOut = bEncodeMono ? 1 : inputInfo.nChannels; if(inputInfo.aFmt == WAV_FORMAT_FLOAT){ cfg->inputFormat = AACPLUS_INPUT_FLOAT; } fprintf(stdout,"input file %s: \nsr = %d, nc = %d fmt = %d\n\n", argv[1], inputInfo.sampleRate, inputInfo.nChannels, inputInfo.aFmt); fprintf(stdout,"output file %s: \nbr = %d inputSamples = %lu maxOutputBytes = %lu nc = %d m = %d\n\n", argv[2], cfg->bitRate, inputSamples, maxOutputBytes, cfg->nChannelsOut, bEncodeMono); fflush(stdout); int ret = 0; if((ret = aacplusEncSetConfiguration(hEncoder, cfg)) == 0) { fprintf(stdout,"setting cfg failed\n", ret); return -1; } uint8_t *outputBuffer = malloc(maxOutputBytes); int32_t *TimeDataPcm; if(inputInfo.aFmt == WAV_FORMAT_FLOAT) { TimeDataPcm = calloc(inputSamples, sizeof(float)); } else { TimeDataPcm = calloc(inputSamples, sizeof(short)); } int stopLoop = 0; int bytes = 0; do { int numSamplesRead = 0; if(inputInfo.aFmt == WAV_FORMAT_FLOAT) { if ( AuChannelReadFloat(inputFile, (float *) TimeDataPcm, inputSamples, &numSamplesRead) > 0) { stopLoop = 1; break; } } else { if ( AuChannelReadShort(inputFile, (short *) TimeDataPcm, inputSamples, &numSamplesRead) > 0) { stopLoop = 1; break; } } if(numSamplesRead < inputSamples) { stopLoop = 1; break; } bytes = aacplusEncEncode(hEncoder, (int32_t *) TimeDataPcm, numSamplesRead, outputBuffer, maxOutputBytes); if(bytes > 0) fwrite(outputBuffer, bytes, 1, hADTSFile); frmCnt++; fprintf(stderr,"[%d]\r",frmCnt); fflush(stderr); } while (!stopLoop && bytes >= 0); fprintf(stderr,"\n"); fflush(stderr); printf("\nencoding finished\n"); aacplusEncClose(hEncoder); fclose(hADTSFile); free(outputBuffer); free(TimeDataPcm); return 0; }
int main(int argc, char* argv[]) { HANDLE_MP4_FILE hMp4File = 0; FILE *pcm_fp = 0; hAudioChannel inputFile = 0; AuChanInfo inputInfo; AuChanType auType = TYPE_AUTODETECT ; /* must be set */ AuChanMode auFlags = AU_CHAN_READ; short TimeDataPcm[AACENC_BLOCKSIZE * 2 * MAX_CHANNELS]; inputInfo.bitsPerSample = 16 ; /* only relevant if valid == 1 */ inputInfo.sampleRate = 44100 ; /* only relevant if valid == 1 */ inputInfo.nChannels = 2 ; /* only relevant if valid == 1 */ inputInfo.nSamples = 0 ; /* only relevant if valid == 1 */ inputInfo.isLittleEndian = 1; inputInfo.fpScaleFactor = AACENC_PCM_LEVEL ; /* must be set */ inputInfo.valid = 1 ; /* must be set */ inputInfo.useWaveExt = 0; pcm_fp = fopen("f:/temp/im/aac_pcm.pcm", "wb"); int ret = AuChannelOpen (&inputFile, "c:\\audio\\ave_maria_48.wav", auFlags, &auType, &inputInfo); IHEAAC_ENC* enc = CreateAACEnc(); assert(enc && enc->Init(inputInfo.sampleRate, inputInfo.nChannels, 32000)); IHEAAC_DEC* dec = CreateAACDec(); assert(dec && dec->Init(inputInfo.sampleRate, 26000)); int inSamples = AACENC_BLOCKSIZE * inputInfo.nChannels; int numSamplesRead = 0; inSamples = enc->GetSampleCount(); std::string xxx, out_pcm; while (1) { if (AuChannelReadShort(inputFile, TimeDataPcm, inSamples, &numSamplesRead) != AU_CHAN_OK) { fprintf(stderr, "failed to read source file!\n"); return 0; } enc->Enc(TimeDataPcm, xxx); dec->Dec(xxx, out_pcm, true); fwrite(out_pcm.c_str(), 1, out_pcm.length(), pcm_fp); } if (enc) DestroyAACEnc(enc); if (inputFile) AuChannelClose(inputFile); if (pcm_fp) fclose(pcm_fp); system("pause"); return 0; }