我使用libavcodec(最新的git,截至3/3/10)将raw pcm编码到aac
(启用了libfaac支持).我通过调用avcodec_encode_audio来做到这一点
每次都重复使用codec_context-> frame_size样本.前四
调用回调成功,但第五个调用从不返回.当我使用gdb
打破,堆栈腐败.
(启用了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对于我的目的来说足够好,所以我可以向前迈进.
相关文章
翻译自:https://stackoverflow.com/questions/2410459/encode-audio-to-aac-with-libavcodec
转载注明原文:使用libavcodec编码音频到aac