gpt4 book ai didi

c - 使用 libavcodec 将音频编码为 aac

转载 作者:太空狗 更新时间:2023-10-29 16:39:04 25 4
gpt4 key购买 nike

我正在使用 libavcodec(截至 2010 年 3 月 3 日的最新 git)将原始 pcm 编码为 aac(启用 libfaac 支持)。我通过调用 avcodec_encode_audio 来做到这一点每次重复使用 codec_context->frame_size 样本。前四名调用成功返回,但第五次调用永远不会返回。当我使用 gdb中断,堆栈已损坏。

如果我使用 audacity 将 pcm 数据导出到一个 .wav 文件,那么我可以使用命令行 ffmpeg 可以毫无问题地转换为 aac,所以我确定它是我做错了什么。

我编写了一个小型测试程序来重现我的问题。它读取来自文件的测试数据,可在此处获得: http://birdie.protoven.com/audio.pcm (~2 秒的签名 16 位 LE pcm)

如果我直接使用 FAAC,我可以让它全部工作,但如果我可以只使用 libavcodec,代码会更清晰一些,因为我也在编码视频,并将两者写入 mp4。

ffmpeg 版本信息:

FFmpeg version git-c280040, Copyright (c) 2000-2010 the FFmpeg developers
built on Mar 3 2010 15:40:46 with gcc 4.4.1
configuration: --enable-libfaac --enable-gpl --enable-nonfree --enable-version3 --enable-postproc --enable-pthreads --enable-debug=3 --enable-shared
libavutil 50.10. 0 / 50.10. 0
libavcodec 52.55. 0 / 52.55. 0
libavformat 52.54. 0 / 52.54. 0
libavdevice 52. 2. 0 / 52. 2. 0
libswscale 0.10. 0 / 0.10. 0
libpostproc 51. 2. 0 / 51. 2. 0

我的编解码器中是否有我没有设置或设置不正确的东西上下文,也许?非常感谢任何帮助!

这是我的测试代码:

#include <stdio.h>
#include <libavcodec/avcodec.h>

void EncodeTest(int sampleRate, int channels, int audioBitrate,
uint8_t *audioData, size_t audioSize)
{
AVCodecContext *audioCodec;
AVCodec *codec;
uint8_t *buf;
int bufSize, frameBytes;

avcodec_register_all();

//Set up audio encoder
codec = avcodec_find_encoder(CODEC_ID_AAC);
if (codec == NULL) return;
audioCodec = avcodec_alloc_context();
audioCodec->bit_rate = audioBitrate;
audioCodec->sample_fmt = SAMPLE_FMT_S16;
audioCodec->sample_rate = sampleRate;
audioCodec->channels = channels;
audioCodec->profile = FF_PROFILE_AAC_MAIN;
audioCodec->time_base = (AVRational){1, sampleRate};
audioCodec->codec_type = CODEC_TYPE_AUDIO;
if (avcodec_open(audioCodec, codec) < 0) return;

bufSize = FF_MIN_BUFFER_SIZE * 10;
buf = (uint8_t *)malloc(bufSize);
if (buf == NULL) return;

frameBytes = audioCodec->frame_size * audioCodec->channels * 2;
while (audioSize >= frameBytes)
{
int packetSize;

packetSize = avcodec_encode_audio(audioCodec, buf, bufSize, (short *)audioData);
printf("encoder returned %d bytes of data\n", packetSize);
audioData += frameBytes;
audioSize -= frameBytes;
}
}

int main()
{
FILE *stream = fopen("audio.pcm", "rb");
size_t size;
uint8_t *buf;

if (stream == NULL)
{
printf("Unable to open file\n");
return 1;
}

fseek(stream, 0, SEEK_END);
size = ftell(stream);
fseek(stream, 0, SEEK_SET);
buf = (uint8_t *)malloc(size);
fread(buf, sizeof(uint8_t), size, stream);
fclose(stream);

EncodeTest(32000, 2, 448000, buf, size);
}

最佳答案

如果比特率低于 386000,问题似乎就会消失。不知道为什么会这样,因为我可以以比直接使用 FAAC 更高的比特率进行编码。但 128000 足以满足我的目的,所以我可以继续前进。

关于c - 使用 libavcodec 将音频编码为 aac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2410459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com