如何使用AudioConverterFillComplexBuffer在iOS上将AAC压缩帧解码为PCM

6
我想在我的应用程序中实现SIP呼叫,首先需要解决的问题是将带有ADTS头的压缩AAC格式音频转换为线性PCM。
我的输入数据是一个NSMutableData类型的ADTS帧的NSArray,每个帧的帧大小都不同。每个帧的格式和采样率相同,唯一的区别是帧大小。
我尝试了Igor Rotaru在this issue中建议的示例代码,但无法使其正常工作。
现在我的代码看起来像这样。首先,我配置AudioConverter:
- (void)configureAudioConverter {
    AudioStreamBasicDescription inFormat;
    memset(&inFormat, 0, sizeof(inFormat));
    inputFormat.mBitsPerChannel = 0;
    inputFormat.mBytesPerFrame = 0;
    inputFormat.mBytesPerPacket = 0;
    inputFormat.mChannelsPerFrame = 1;
    inputFormat.mFormatFlags = kMPEG4Object_AAC_LC;
    inputFormat.mFormatID = kAudioFormatMPEG4AAC;
    inputFormat.mFramesPerPacket = 1024;
    inputFormat.mReserved = 0;
    inputFormat.mSampleRate = 22050;

    AudioStreamBasicDescription outputFormat;
    memset(&outputFormat, 0, sizeof(outputFormat));
    outputFormat.mSampleRate       = inputFormat.mSampleRate;
    outputFormat.mFormatID         = kAudioFormatLinearPCM;
    outputFormat.mFormatFlags      = kLinearPCMFormatFlagIsSignedInteger;
    outputFormat.mBytesPerPacket   = 2;
    outputFormat.mFramesPerPacket  = 1;
    outputFormat.mBytesPerFrame    = 2;
    outputFormat.mChannelsPerFrame = 1;
    outputFormat.mBitsPerChannel   = 16;
    outputFormat.mReserved         = 0;

    AudioClassDescription *description = [self
                                      getAudioClassDescriptionWithType:kAudioFormatMPEG4AAC
                                      fromManufacturer:kAppleSoftwareAudioCodecManufacturer];

    OSStatus status =  AudioConverterNewSpecific(&inputFormat, &outputFormat, 1, description, &_audioConverter);

    if (status != 0) {
        printf("setup converter error, status: %i\n", (int)status);
    }
}

之后我编写了回调函数:

struct MyUserData {
    UInt32 mChannels;
    UInt32 mDataSize;
    const void* mData;
    AudioStreamPacketDescription mPacket;
};

OSStatus inInputDataProc(AudioConverterRef inAudioConverter,
                         UInt32 *ioNumberDataPackets,
                         AudioBufferList *ioData,
                         AudioStreamPacketDescription **outDataPacketDescription,
                         void *inUserData)
{
    struct MyUserData* userData = (struct MyUserData*)(inUserData);

    if (!userData->mDataSize) {
        *ioNumberDataPackets = 0;
        return kNoMoreDataError;
    }

    if (outDataPacketDescription) {
        userData->mPacket.mStartOffset = 0;
        userData->mPacket.mVariableFramesInPacket = 0;
        userData->mPacket.mDataByteSize = userData->mDataSize;
        *outDataPacketDescription = &userData->mPacket;
    }

    ioData->mBuffers[0].mNumberChannels = userData->mChannels;
    ioData->mBuffers[0].mDataByteSize = userData->mDataSize;
    ioData->mBuffers[0].mData = (void *)userData->mData;

    // No more data to provide following this run.
    userData->mDataSize = 0;

    return noErr;
}

我的解码帧函数如下所示:
- (void)startDecodingAudio {
    if (!_converterConfigured){
        return;
    }

    while (true){
        if ([self hasFramesToDecode]){
            struct MyUserData userData = {1, (UInt32)_decoderBuffer[_currPosInDecoderBuf].length, _decoderBuffer[_currPosInDecoderBuf].bytes};

            uint8_t *buffer = (uint8_t *)malloc(128 * sizeof(short int));
            AudioBufferList decBuffer;
            decBuffer.mNumberBuffers = 1;
            decBuffer.mBuffers[0].mNumberChannels = 1;
            decBuffer.mBuffers[0].mDataByteSize = 128 * sizeof(short int);
            decBuffer.mBuffers[0].mData = buffer;

            UInt32 numFrames = 128;

            AudioStreamPacketDescription outPacketDescription;
            memset(&outPacketDescription, 0, sizeof(AudioStreamPacketDescription));
            outPacketDescription.mDataByteSize = 128;
            outPacketDescription.mStartOffset = 0;
            outPacketDescription.mVariableFramesInPacket = 0;

            OSStatus status = AudioConverterFillComplexBuffer(_audioConverter,
                                                              inInputDataProc,
                                                              &userData,
                                                              &numFrames,
                                                              &decBuffer,
                                                              &outPacketDescription);

            NSError *error = nil;

            if (status == kNoMoreDataError) {
                NSLog(@"%u bytes decoded", (unsigned int)decBuffer.mBuffers[0].mDataByteSize);
                [_decodedData appendData:[NSData dataWithBytes:decBuffer.mBuffers[0].mData length:decBuffer.mBuffers[0].mDataByteSize]];
                _currPosInDecoderBuf += 1;
            } else {
                error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
            }
        } else {
            break;
        }
    }
}

每次,AudioConverterFillComplexBuffer返回状态1852797029,根据苹果API,这是kAudioCodecIllegalOperationError。如果有人成功转换这样的格式,请分享一些示例或建议。

你已经解决了你的问题吗? - Vlad Rudskoy
@VladislavRudskoy 是的,看下面我的回答。 - avsmirnov567
1个回答

4

最后,我使用StreamingKit库对我的字节进行解码(原始存储库可以在这里找到)。


嘿@avsmirnov567,你尝试过转换AAC文件吗?我从实时套接字中获取到AAC块的字节数组,但无法确定这个库是否对我有用。 - Oz Shabat
@OzShabat 我为使用套接字流与此库编写了自定义数据源。让我看看,如果我仍然在本地拥有这个代码,我将更新我的上面的答案。如果没有,恐怕我已经忘记了这种情况。 - avsmirnov567
1
嗨,@OzShabat,我在我的Github上找到了我的库的分支。请查看STKInputStreamDataSource类。如果有帮助的话,我会非常高兴。 - avsmirnov567
我现在已经为你的分支工作了一天以上。非常聪明的解决方案!不幸的是,我收到的音频数据包没有配置为支持带ADTS的AAC,所以我认为这个库对我没有帮助。 - Oz Shabat
我还不知道你是如何从StreamingKit获取PCM数据的。我可以使用StreamingKit播放AAC文件,但是我找不到PCM数据。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接