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

问题描述:

我想在我的应用程序中实现SIP调用,而我需要解决的第一个问题是将音频从ADTS标头压缩的AAC格式转换为线性PCM。如何使用AudioConverterFillComplexBuffer将AAC压缩帧解码为PCM iOS

我的输入数据是具有不同帧尺寸的ADTS帧的NSArray。每个框架都是typeof NSMutableData。每一帧的格式和采样率都是相同的,唯一不同的是帧大小。

我试图实现示例代码,由伊戈尔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。如果有人成功转换这种格式,请分享一些例子或建议。

+0

你已经解决了你的问题? – vladiqtx

+0

@VladislavRudskoy是的,请参阅下面的答案。 –

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