c++ - 使用 ffmpeg 解码 AAC 音频

标签 c++ audio ffmpeg pcm aac

我正在尝试解码 ADTS 容器中的 AAC 音频流,该音频流是从外部硬件 H264 编码器流式传输的。

我已经解析了 ADTS,它告诉我我有一个 2 channel 、44100 AAC 主配置文件框架。我为 ffmpeg 解码器设置了额外的数据字节并成功解码了帧?如下:

(伪 C++ 代码)

设置解码器:

avcodec_find_decoder(codec_id);
avcodec_alloc_context3(context->codec);
avcodec_open2(context->av_codec_context, context->codec, nullptr);
av_init_packet(&context->av_raw_packet);

设置额外的数据字节:

// AOT_MAIN, 44.1kHz, Stereo
// 00001010 00010000
// extradata = 0x0A, 0X10
memcpy(context->av_codec_context->extradata, extradata, extradataLength);
avcodec_open2(context->av_codec_context, context->codec, nullptr);

然后解码帧:

// decode frame
const int len = avcodec_decode_audio4(context->av_codec_context, context->frame, &got_frame, &context->av_raw_packet);
*sampleRate = context->av_codec_context->sample_rate;
*sampleFormat = context->av_codec_context->sample_format;
*bitsPerSample = av_get_bytes_per_sample(context->av_codec_context->sample_fmt) * 8;
*channels = context->av_codec_context->channels;
*channelLayout = context->av_codec_context->channelLayout;
// get frame
*outDataSize = av_samples_get_buffer_size(nullptr, context->av_codec_context->channels, context->frame->nb_samples, context->av_codec_context->sample_fmt, 1);

解码后的帧:

// array of 8192 bytes, context info is as expected:
context->av_codec_context->channels = 2
context->av_codec_context->channelLayout = 3 (AV_CH_LAYOUT_STEREO)
context->frame->sample_fmt = 8 (AV_SAMPLE_FMT_FLTP) // float, planar
context->frame->sample_rate = 44100

现在,据我了解,32 位原始格式中的每个帧每个样本将有 4 个字节,并且每个 channel 将被交错(因此每 4 个字节是交替 channel )。这使得每个 channel 有 1024 个样本(8192/32 位/2 个 channel )。

我尝试将此数据的多个帧导出到文件中,并在 Audacity 中作为原始文件(32 位浮点、2 channel 44​​100Hz、小端)导入以进行完整性检查。我得到的不是音乐,而是噪音,并且检测到的音频长度比我预期的要长得多(转储到文件中的时间为 5 秒,但 Audacity 说是 22.5 秒)。我尝试了多种导入格式设置。我在这里可能做错了什么?

我对音频处理有点陌生,所以我可能误解了一些东西。

编辑:我尝试将音频平移到正确的 channel ,其反射(reflect)在数据中。它看起来也像是一个间隔正好 1024 个样本的重复模式,这对我来说表明存在编程错误,缓冲区在第一个样本之后没有被覆盖。 12 frames

最佳答案

这只不过是一个很难发现的错误。放大 Audacity 中的音频样本,可以发现 1024 个样本宽的重复模式。

缓冲区实际上没有正确更新,我一遍又一遍地处理相同的音频帧:

for(var offset = 0; offset < packet.Length; offset++) {
  var frame = ReadAdtsFrame();
  // offset += frame.Length; 
  // ^ essentially this was missing, so the frame buffer was always the first frame
}

我将把这个留在这里是为了向世界展示我的耻辱,并提醒大家,最常见的是你自己的错误导致了你的结局。

关于c++ - 使用 ffmpeg 解码 AAC 音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65744845/

相关文章:

ffmpeg youtube 直播在一段时间后停止

python-3.x - PyAV 从 h264 帧解析数据包时不一致

c++ - 对 C++ 的哪些更改使复制初始化适用于具有显式构造函数的类?

c++ - C++ 中右值引用的分配和不变性

Java - 音频 URL

qt - PyQt4 QtGui.QSound 作为命令行程序播放?

c++ - 将指向类对象的智能指针与类对象混合

c++ - 如果根本不使用单个取消引用运算符,指向指针的指针会打印什么值?

java - 为什么我添加的 SPI jar 不能在我的 Netbeans 平台项目中注册?

python - Pydub安装和ffmpeg