iPhone:使用AudioConverterFillComplexBuffer将32KHz PCM编码为96Kbit AAC的问题

时间:2011-07-22 15:52:00

标签: iphone ios audio encoding aac

有没有人在iPhone / iOS上成功将32KHz PCM转换为96Kbit AAC?

我无法在任何硬件设备上正常工作。我写的代码只能在模拟器中正常工作。当在当代iPad / iPod / iPhone上运行时,我的代码“跳过”了大量的音频。

生成的编码流包含~640ms的“良好”音频的重复模式,接着是~640ms的“坏”音频。

对16位线性和8.24定点PCM进行编码产生了相同的结果。

以下是设置音频转换器以编码MPEG4-AAC 96kbits @ 32KHz的代码:

AudioStreamBasicDescription descPCMFormat;
descPCMFormat.mSampleRate       = 32000;
descPCMFormat.mChannelsPerFrame = 1;
descPCMFormat.mBitsPerChannel   = sizeof(AudioUnitSampleType) * 8;
descPCMFormat.mBytesPerPacket   = sizeof(AudioUnitSampleType);
descPCMFormat.mFramesPerPacket  = 1;
descPCMFormat.mBytesPerFrame    = sizeof(AudioUnitSampleType);
descPCMFormat.mFormatID         = kAudioFormatLinearPCM;
descPCMFormat.mFormatFlags      = kAudioFormatFlagsAudioUnitCanonical;

AudioStreamBasicDescription descAACFormat;
descAACFormat.mSampleRate       = 32000;
descAACFormat.mChannelsPerFrame = 1;
descAACFormat.mBitsPerChannel   = 0;
descAACFormat.mBytesPerPacket   = 0;
descAACFormat.mFramesPerPacket  = 1024;
descAACFormat.mBytesPerFrame    = 0;
descAACFormat.mFormatID         = kAudioFormatMPEG4AAC;
descAACFormat.mFormatFlags      = 0;

AudioConverterNew(& descPCMFormat, & descAACFormat, &m_hCodec);

UInt32 ulBitRate = 96000;
UInt32 ulSize = sizeof(ulBitRate);
AudioConverterSetProperty(m_hCodec, kAudioConverterEncodeBitRate, ulSize, & ulBitRate);

简单的转换程序。这个例程每隔32ms调用一次,包含1024个PCM样本,并且需要384个字节的编码AAC:

OSStatus CMyObj::Convert(
    const AudioUnitSampleType * pSrc,
    const size_t        ulSrc,
    uint8_t           * pDst,
    size_t            & ulDst)
{
    // error and sanity checking removed.. 
    // assume caller is converting 1024 samples to at most 384 bytes

    OSStatus osStatus;

    m_pSrcPtr  = (uint8_t*)pSrc;
    m_ulSrcLen = ulSrc;    // verified to be 1024*sizeof(AudioUnitSampleType);    

    AudioBufferList destBuffers;
    destBuffers.mNumberBuffers              = 1;
    destBuffers.mBuffers[0].mNumberChannels = 1;
    destBuffers.mBuffers[0].mDataByteSize   = 384;
    destBuffers.mBuffers[0].mData           = pDst;

    AudioStreamPacketDescription destDescription;
    destDescription.mStartOffset            = 0;
    destDescription.mVariableFramesInPacket = 0;
    destDescription.mDataByteSize           = 384;

    UInt32 ulDstPackets                     = 1;

    osStatus = AudioConverterFillComplexBuffer(
                   m_hCodec,
                   InputDataProc, 
                   this, 
                   & ulDstPackets,
                   & destBuffers,
                   & destDescription);

    ulDst = destBuffers.mBuffers[0].mDataByteSize;

    return osStatus;
}

输入数据只是向编码器提供1024个样本:

static OSStatus CMyObj::InputDataProc(
    AudioConverterRef               hCodec, 
    UInt32                         *pulSrcPackets, 
    AudioBufferList                *pSrcBuffers, 
    AudioStreamPacketDescription  **ppPacketDescription,
    void                           *pUserData)
{
    // error and sanity checking removed
    CMyObj *pThis = (CMyObj*)pUserData;

    const UInt32 ulMaxSrcPackets = pThis->m_ulSrcLen / sizeof(AudioUnitSampleType);

    const UInt32 ulRetSrcPackets = min(ulMaxSrcPackets, *pulSrcPackets);
    if( ulRetSrcPackets )
    {
        UInt32 ulRetSrcBytes = ulRetSrcPackets * sizeof(AudioUnitSampleType);

        *pulSrcPackets = ulRetSrcPackets;

        pSrcBuffers->mBuffers[0].mData           = pThis->m_pSrcPtr;
        pSrcBuffers->mBuffers[0].mDataByteSize   = ulRetSrcBytes;
        pSrcBuffers->mBuffers[0].mNumberChannels = 1;

        pThis->m_pSrcPtr   += ulRetSrcBytes;
        pThis-> m_ulSrcLen -= ulRetSrcBytes;

        return noErr;
    }

    *pulSrcPackets = 0;

    pSrcBuffers->mBuffers[0].mData           = NULL;
    pSrcBuffers->mBuffers[0].mDataByteSize   = 0;
    pSrcBuffers->mBuffers[0].mNumberChannels = 1;
    return 500; // local error code to signal end-of-packet
}

在模拟器上运行时一切正常。

但是,在设备上运行时,不会一致地调用InputDataProc。连续多达20次,对AudioConverterFillComplexBuffer的调用会激发对InputDataProc的调用,一切看起来都很好。然后,对于下一次~21次调用AudioConverterFillComplexBuffer,将不会调用InputDataProc。这种模式永远重复:

-> Convert 
  -> AudioConverterFillComplexBuffer
     -> InputDataProc
       -> results in 384 bytes of 'good' AAC
-> Convert 
  -> AudioConverterFillComplexBuffer
     -> InputDataProc
       -> results in 384 bytes of 'good' AAC
.. repeats up to 18 more times

-> Convert 
  -> AudioConverterFillComplexBuffer
    -> results in 384 bytes of 'bad' AAC
-> Convert 
  -> AudioConverterFillComplexBuffer
    -> results in 384 bytes of 'bad' AAC
.. repeats up to 18 more times

转换器在哪里获取输入数据以创建'坏'AAC,因为它没有调用InputDataProc?

有没有人看到这种方法有什么明显的错误?

是否需要在硬件编解码器上进行任何特殊设置(MagicCookies或?)?

HW AAC编解码器是否支持32000采样率?

1 个答案:

答案 0 :(得分:0)

我发现:32KHz-input-PCM的默认outputBitRate是48000位,44.1KHz-input-PCM的默认outputBitRate是64000位。 当使用默认的outputBitRate时,32KHz输入会产生巨大的噪音。 即使使用these codes from apple`s sample ,44.1KHz输入也会产生一点噪音。

然后我将outputBitRate固定为64kbs,32KHz& 44.1KHz都运行良好。

UInt32 outputBitRate = 64000; // 64kbs
UInt32 propSize = sizeof(outputBitRate);
if (AudioConverterSetProperty(m_converter, kAudioConverterEncodeBitRate, propSize, &outputBitRate) != noErr) {
} else {
    NSLog(@"upyun.com uplivesdk  UPAACEncoder error 102");
}