【问题标题】:How to decode AAC compressed frames to PCM using AudioConverterFillComplexBuffer iOS如何使用 AudioConverterFillComplexBuffer iOS 将 AAC 压缩帧解码为 PCM
【发布时间】:2017-03-22 17:55:38
【问题描述】:

我想在我的应用程序中实现 SIP 呼叫,我需要解决的第一个问题是将音频从带有 ADTS 标头的压缩 AAC 格式转换为线性 PCM。

我的输入数据是具有不同帧大小的 ADTS 帧的 NSArray。每一帧都是 NSMutableData 类型。每一帧的格式和采样率都是一样的,只是帧大小不同。

我尝试实现 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,根据 Apple API,kAudioCodecIllegalOperationError。如果有人成功转换了此类格式,请分享一些示例或建议。

【问题讨论】:

  • 你的问题已经解决了吗?
  • @VladislavRudskoy 是的,请参阅下面的答案。

标签: ios objective-c pcm aac adts


【解决方案1】:

最后,我使用 StreamingKit 库解码了我的字节(原始存储库可以在 here 找到)。

【讨论】:

  • 嘿@avsmirnov567 你试过转换AAC文件吗?我从实时套接字获取字节数组中的 AAC 块,但不知道这个库对我是否有用
  • @OzShabat 我编写了自定义数据源,以便将此库与套接字流一起使用。让我看看,如果我在本地还有这段代码,我会在上面更新我的答案。如果没有,恐怕我已经忘记了这个案子。
  • 嘿,@OzShabat,我找到了my fork of library at my github。请参阅课程STKInputStreamDataSource。如果有帮助,我会很高兴。
  • 我正在为你的叉子工作超过一天。非常聪明的解决方案!不幸的是,我收到的音频数据包没有配置为支持带有广告的 AAC,所以我认为这个库对我没有帮助
猜你喜欢
  • 2011-10-11
  • 2018-12-23
  • 1970-01-01
  • 2013-02-02
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多