在iOS上用fdk编码AAC-HE-V1时如何获得正确的频道?

时间:2016-09-12 13:11:03

标签: ios audio encoding aac

我使用fdk v2.0.101通过xcode版本8.0(8A218a)对iOS上的AAC-HE-V1进行编码,但即使我将mono设置为通道,我总是得到立体声通道aac数据。

如何在通过fdk编码AAC-HE-V1时获得正确的频道?

PS:AAC-LC没问题。在带有iOS 9.3的iPhone 6上测试。

以下是代码段:

初始化部分

    UINT channels = 1;
    HANDLE_AACENCODER encoder_handle;
    AACENC_InfoStruct encoder_info;

    int HE_AACv1 = 5;

    TRANSPORT_TYPE transport_type = TT_MP4_ADTS;

    CHANNEL_MODE mode;

    switch (channels) {
        case 1: mode = MODE_1;       break;
        case 2: mode = MODE_2;       break;
        case 3: mode = MODE_1_2;     break;
        case 4: mode = MODE_1_2_1;   break;
        case 5: mode = MODE_1_2_2;   break;
        case 6: mode = MODE_1_2_2_1; break;
    }

    CheckError(aacEncOpen(&encoder_handle, 0, channels), @"Unable to open encoder : %i");
    CheckError(aacEncoder_SetParam(encoder_handle, AACENC_AOT, HE_AACv1), @"Unable to set the AOT : %i");
    CheckError(aacEncoder_SetParam(encoder_handle, AACENC_SAMPLERATE, 44100),
               @"Unable to set the sample rate : %i");
    CheckError(aacEncoder_SetParam(encoder_handle, AACENC_CHANNELMODE, mode),
               @"Unable to set the channel mode : %i");
    CheckError(aacEncoder_SetParam(encoder_handle, AACENC_CHANNELORDER, 1),
               @"Unable to set the wav channel order : %i");
    CheckError(aacEncoder_SetParam(encoder_handle, AACENC_BITRATE, 90000),
               @"Unable to set the bitrate : %i");
    CheckError(aacEncoder_SetParam(encoder_handle, AACENC_TRANSMUX, transport_type),
               @"Unable to set the ADTS transmux : %i");
    CheckError(aacEncoder_SetParam(encoder_handle, AACENC_AFTERBURNER, 1),
               @"Unable to set the afterburner mode : %i");
    CheckError(aacEncEncode(encoder_handle, NULL, NULL, NULL, NULL),
               @"Unable to initialize the encoder : %i");
    CheckError(aacEncInfo(encoder_handle, &encoder_info), @"Unable to get the encoder info : %i");

编码部分:

  AudioBuffer originBuffer = ...;

  AACENC_OutArgs out_args = { 0 };
  AACENC_ERROR err;

  int out_identifier = OUT_BITSTREAM_DATA;

  uint8_t *output_data = calloc(encoder_info.maxOutBufBytes, 1);

  int read = originBuffer.mDataByteSize;

  void *in_ptr = originBuffer.mData;
  int in_size = read;
  int in_elem_size = _sampleBytes;

  int in_identifier = IN_AUDIO_DATA;

  AACENC_InArgs in_args = { 0 };
  in_args.numInSamples = read / _sampleBytes;

  AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
  in_buf.numBufs = 1;
  in_buf.bufs = &in_ptr;
  in_buf.bufferIdentifiers = &in_identifier;
  in_buf.bufSizes = &in_size;
  in_buf.bufElSizes = &in_elem_size;

  void *out_ptr;
  int out_size, out_elem_size;

  out_ptr = output_data;
  out_size = _encoder_info.maxOutBufBytes;
  out_elem_size = _sampleBytes;
  out_buf.numBufs = 1;
  out_buf.bufs = &out_ptr;
  out_buf.bufferIdentifiers = &out_identifier;
  out_buf.bufSizes = &out_size;
  out_buf.bufElSizes = &out_elem_size;

  AACENC_ERROR result = aacEncEncode(_encoder_handle, &in_buf, &out_buf, &in_args, &out_args);

  if (result != AACENC_OK) {
      free(output_data);
      NSLog(@"FDK-AAC encode fail %i", result);
      return;
  }
  if (out_args.numOutBytes > 0) {

      // handle output_data

  } else {
      free(output_data);
  }

1 个答案:

答案 0 :(得分:0)

我认为CHANNEL_MODE是原始的音频模式。不是aac模式

iOS不支持AAC-HE-V2。

您不必使用fdk转换音频。

以下是示例代码。

https://developer.apple.com/library/content/samplecode/iPhoneACFileConvertTest/Introduction/Intro.html

我也需要一些关于fdk-aac ios的帮助。链接在这里。希望你能看看它。 ios fdk-aac pcm to aac sample