faac 移植到android

在项目中需要应用到faac 压缩音频传输,有网友提供了移植方法和库.so 测试了声音不对,所以参考了他的移植方法 封装的接口。


可以在资源下载自己修改http://download.csdn.net/detail/undiif123/5494433

jint
Java_com_sinaapp_bashell_AacEncoder_AACEncoderOpen(JNIEnv* env,
                                             jobject this,jint sampleRate,jint channels)
{

   
    faacEncConfigurationPtr myFormat;
   
    unsigned int mpegVersion = MPEG4;
    unsigned int objectType = LOW;
    unsigned int useMidSide = 1;
unsigned long quantqual = 100;
static unsigned int useTns = 1;
int rawBits = 16;
int rawRate =sampleRate;
int rawEndian = 1;
int shortctl = SHORTCTL_NORMAL;


    g_hEncoder = faacEncOpen(sampleRate, channels,
        &samplesInput, &maxOutputBytes);
   
    myFormat = faacEncGetCurrentConfiguration(g_hEncoder);
    myFormat->allowMidside = useMidSide;
    myFormat->aacObjectType = objectType;
    myFormat->mpegVersion = mpegVersion;
    myFormat->outputFormat =1;
    myFormat->useTns = useTns;
    myFormat->useLfe = 0;
    myFormat->quantqual =quantqual;
    myFormat->bandWidth = 0;
myFormat->shortctl = shortctl;
    faacEncSetConfiguration(g_hEncoder, myFormat);
    sprintf(g_szFileName,"/storage/sdcard0/new_test1.aac");//"/mnt/sdcard/new.aac");///data/IPVOD/
   // sprintf(g_szFileName,"/storage/sdcard0/new_test1.pcm");//"/mnt/sdcard/new.aac");///data/IPVOD/


LOGI("samplesInput:%d,maxOutputBytes:%d",samplesInput,maxOutputBytes);


g_OutFile = fopen(g_szFileName, "wb");
if(g_OutFile == NULL) {LOGI("open aacfile failed!"); return 1;};
sprintf(g_szFileName,"/storage/sdcard0/new_test0.pcm");//"/mnt/sdcard/new.aac");///data/IPVOD/
g_OutFile0 = fopen(g_szFileName, "wb");
if(g_OutFile0 == NULL) {LOGI("open aacfile failed!"); return 1;};
   return 1;
}




jint
Java_com_sinaapp_bashell_AacEncoder_AacWrite(JNIEnv* env,jobject this,jbyteArray jBuffer, jint jBufferSize) {
   
   int *pcmbuf;
   unsigned char *bitbuf;
   jbyte* bBuffer = (*env)->GetByteArrayElements(env,jBuffer,0);


   pcmbuf = (short*)malloc(samplesInput*sizeof(int));
   bitbuf = (unsigned char*)malloc(maxOutputBytes*sizeof(unsigned char));
  
   unsigned int bytesWritten;
   if(g_hEncoder != NULL) {


   unsigned int nBufferSize = (unsigned int)jBufferSize/2;
   int nByteCount=0;
   unsigned short* buf=(unsigned short*)bBuffer;
   fwrite(jBuffer, 1, jBufferSize, g_OutFile0);
   while(nByteCount < nBufferSize)
   {
    int audioLength=samplesInput;
    if((nByteCount+samplesInput) >= nBufferSize) {
    audioLength=nBufferSize-nByteCount;
}
    int i;
    for (i = 0; i < audioLength; i++)
    {
   int s = ((int16_t *)buf+nByteCount)[i];
   pcmbuf[i] = s << 8;
    }


nByteCount+=samplesInput;


    bytesWritten = faacEncEncode(g_hEncoder,
                        pcmbuf,
                        audioLength,
                        bitbuf,
                        maxOutputBytes);
if (bytesWritten < 0)continue;

fwrite(bitbuf, 1, bytesWritten, g_OutFile);
   }




  }


  if(bitbuf) free(bitbuf);
  if(pcmbuf) free(pcmbuf);
  return 1;
}
/*
jint
Java_com_sinaapp_bashell_AacEncoder_AACEncoderEncode(JNIEnv* env,
                                      jobject this,jint hEncoder, jbyteArray inputBuffer, jint inputBufferSize)
{




   int *pcmbuf;
   unsigned char *bitbuf;
   jbyte* bBuffer = (*env)->GetByteArrayElements(env,inputBuffer,0);
   jsize jLen = (*env)->GetArrayLength(env,inputBuffer);
   int bytesInput = (int)jLen;


   pcmbuf = (short*)malloc(samplesInput*sizeof(int));
   bitbuf = (unsigned char*)malloc(maxOutputBytes*sizeof(unsigned char));
  
   unsigned int bytesWritten;
   if(g_hEncoder != NULL) {


   unsigned int nBufferSize = (unsigned int)inputBufferSize/2;
   int nByteCount=0;
   unsigned short* buf=(unsigned short*)bBuffer;
   fwrite(bBuffer, 1, inputBufferSize, g_OutFile0);
   while(nByteCount < nBufferSize)
   {
     int audioLength

jint
Java_com_sinaapp_bashell_AacEncoder_AACEncoderClose(JNIEnv* env,jobject this)
{


 if(g_hEncoder != NULL) {
  faacEncClose(g_hEncoder);
 }
 if(g_OutFile)fclose(g_OutFile);
  return 1;
}

你可能感兴趣的:(android)