1. 程式人生 > >ios libfdk-aac encode

ios libfdk-aac encode

使用libfdk-aac編碼。

#import "FDKAACEncoder.h"
#include "aacenc_lib.h"

#define PROFILE_AAC_LC 2
#define PROFILE_AAC_HE 5
#define PROFILE_AAC_HE_v2 29
#define PROFILE_AAC_LD 23
#define PROFILE_AAC_ELD 39


@interface FDKAACEncoder(){
    HANDLE_AACENCODER _encoder;
}

@end


@implementation FDKAACEncoder

- (int)initEncoder:(int)bitrate samplerate:(int)samplerate channel:(int)channel{
    AACENC_ERROR rt = aacEncOpen(&_encoder, 0, 0);
    if (rt != AACENC_OK) {
        NSLog(@"aac enc open error %zd",rt);
        return -1;
    }
    aacEncoder_SetParam(_encoder, AACENC_AOT, PROFILE_AAC_LC);
    aacEncoder_SetParam(_encoder, AACENC_BITRATE, bitrate);
    aacEncoder_SetParam(_encoder, AACENC_SAMPLERATE, samplerate);
    aacEncoder_SetParam(_encoder, AACENC_CHANNELMODE, MODE_2);

    return 0;
}

- (int)encodeToAAC:(char*)pcm len:(int)len{
    
    AACENC_BufDesc in = {0};
    {
        INT bid = IN_AUDIO_DATA;
        INT elSize = 2;
        void *buf[1] = {pcm};
        in.bufs = buf;
        in.numBufs = 1;
        in.bufferIdentifiers = &bid;
        in.bufElSizes = &elSize;
    }
    
    AACENC_BufDesc out = {0};
    {
        INT size = 1024;
        INT bid = OUT_BITSTREAM_DATA;
        void *buf[1] = {malloc(1024)};
        INT elSize = 1;
        
        out.bufs = buf;
        out.bufSizes = &size;
        out.numBufs = 1;
        out.bufferIdentifiers = &bid;
        out.bufElSizes = &elSize;
    }
    
    AACENC_InArgs inArgs = {0};
    inArgs.numInSamples = (INT)len/2;
    
    AACENC_OutArgs outArgs = {0};
    
    AACENC_ERROR rt = aacEncEncode(_encoder, &in, &out, &inArgs, &outArgs);
    if (rt != AACENC_OK) {
        NSLog(@"aac enc encode error %zd",rt);
    }else{
        NSLog(@"aac enc encode size %zd %zd",outArgs.numOutBytes, out.bufSizes[0]);
    }

    if (_delegate && [_delegate respondsToSelector:@selector(onAudioEncBuf:len:)]) {
        [_delegate onAudioEncBuf:out.bufs[0] len:outArgs.numOutBytes];
    }
    
    if (out.bufs[0]) {
        free(out.bufs[0]);
    }
    
    return 0;
}

- (void)free{
    
    if (_encoder) {
        aacEncClose(&_encoder);
        _encoder = NULL;
    }
}

@end