簡體   English   中英

使用 ffmpeg 解碼 AAC 音頻

[英]Decoding AAC audio with ffmpeg

我正在嘗試在 ADTS 容器中解碼 AAC 音頻 stream,該容器是從外部硬件 H264 編碼器流式傳輸的。

我解析了 ADTS,它告訴我我有一個 2 通道 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 個字節,並且每個通道將被交錯(因此每個第 4 個字節是交替通道)。 這讓我為每個通道(8192 / 32 位 / 2 個通道)保留了 1024 個樣本。

我嘗試將這些數據的多個幀導出到一個文件中,並在 Audacity 中作為原始文件(32 位浮點數,2 通道 44100Hz,小端序)導入以進行完整性檢查。 我得到的不是音樂,而是噪音,檢測到的音頻長度比我預期的要長得多(5 秒轉儲到文件中,但 Audacity 說 22.5 秒)。 我嘗試了各種導入格式設置。 我在這里可能做錯了什么?

我對使用音頻有點陌生,所以我可能會誤解一些東西。

編輯:我嘗試將音頻平移到正確的通道,並將其反映在數據中。 它也看起來像一個重復模式,恰好相隔 1024 個樣本,這向我表明了一個編程錯誤,緩沖區在第一個樣本之后沒有被覆蓋。 12幀

這只不過是一個很難找到的錯誤。 放大 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
}

我將把它留在這里,以向世界展示我的恥辱,並提醒您,最終導致您的錯誤通常是您自己的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM