恩code音频AAC与libav codeC [英] Encode audio to aac with libavcodec
问题描述
我使用libav codeC(最新的git为10年3月3日),以EN code原始PCM到AAC
(libfaac支持启用)。我致电AV codec_en code_audio做到这一点
多次与codec_context-> frame_size样品各一次。前四
调用返回成功,但第五调用永远不会返回。当我使用GDB
打破,堆栈已损坏。
如果我用厚颜无耻到PCM数据导出到一个.wav文件,然后我可以使用
命令行的ffmpeg转换为AAC没有任何问题,所以我敢肯定,这
什么我做错了。
我已经写了复制我的问题,一个小的测试程序。它读取
从文件中,这是可以在这里的测试数据:
http://birdie.protoven.com/audio.pcm (约2秒符号16位PCM LE的)
我可以把它所有的工作,如果我使用FAAC直接,但code将是一个少许清洁剂,如果我可以只使用libav codeC,因为我也对视频进行编码,并写入不仅能够MP4。
ffmpeg的版本信息:
FFmpeg的版本混帐c280040,版权所有(C)2000-2010 FFmpeg的开发者
建于2010年3月3日15时40分46秒用gcc 4.4.1
配置:--enable-libfaac --enable-GPL --enable-非自由--enable-版本3 --enable-postproc --enable-pthreads的--enable-调试= 3 --enable-共享
libavutil 50.10。 0 / 50.10。 0
libav codeC 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
有什么我没有设置,或在我的codeC设置不正确
的背景下,也许?任何帮助是极大AP preciated!
下面是我的测试code:
的#include<&stdio.h中GT;
#包括LT&; libav codeC / AV codec.h>恩作废codeTEST(INT采样率,诠释渠道,INT audioBitrate,
uint8_t有* audioData,为size_t audioSize)
{
AV codecContext *音频codeC;
AV codeC * codeC;
uint8_t有* BUF;
INT由bufSize,frameBytes; AV codec_register_all(); //设置音频连接codeR
codeC = AV codec_find_en codeR(codeC_ID_AAC);
如果(codeC == NULL)回报;
音频codeC = AV codec_alloc_context();
音频codeC-> BIT_RATE = audioBitrate;
音频codeC-> sample_fmt = SAMPLE_FMT_S16;
音频codeC-> SAMPLE_RATE =采样率;
音频codeC->渠道=途径;
音频codeC->型材= FF_PROFILE_AAC_MAIN;
音频codeC->那么time_base =(AVRational){1,}采样率;
音频codeC-> codeC_TYPE = codeC_TYPE_AUDIO;
如果(AV codec_open(音频codeC,codeC)℃的)回报; 由bufSize = FF_MIN_BUFFER_SIZE * 10;
BUF =(uint8_t有*)malloc的(由bufSize);
如果(BUF == NULL)回报; frameBytes =音频codeC-> frame_size *音频codeC->渠道* 2;
而(audioSize> = frameBytes)
{
INT PACKETSIZE; PACKETSIZE = AV codec_en code_audio(音频codeC,BUF,由bufSize,(短*)audioData);
的printf(EN codeR返回%d字节数据的\\ n,PACKETSIZE);
audioData + = frameBytes;
audioSize - = frameBytes;
}
}诠释的main()
{
FILE *流=的fopen(audio.pcm,RB);
为size_t的大小;
uint8_t有* BUF; 如果(流== NULL)
{
的printf(无法打开文件\\ n);
返回1;
} fseek的(流,0,SEEK_END);
大小= FTELL(流);
fseek的(流,0,SEEK_SET);
BUF =(uint8_t有*)的malloc(大小);
FREAD(BUF,sizeof的(uint8_t有),尺寸,流);
FCLOSE(流); 恩codeTEST(32000,2,448000,BUF,大小);
}
这个问题似乎走的路程,如果比特率小于386000.不知道这是为什么,我可以带code。在码率高于直接使用FAAC。但128000是我的目的不够好,所以我能够继续前进。
I'm using libavcodec (latest git as of 3/3/10) to encode raw pcm to aac (libfaac support enabled). I do this by calling avcodec_encode_audio repeatedly with codec_context->frame_size samples each time. The first four calls return successfully, but the fifth call never returns. When I use gdb to break, the stack is corrupt.
If I use audacity to export the pcm data to a .wav file, then I can use command-line ffmpeg to convert to aac without any issues, so I'm sure it's something I'm doing wrong.
I've written a small test program that duplicates my problem. It reads the test data from a file, which is available here: http://birdie.protoven.com/audio.pcm (~2 seconds of signed 16 bit LE pcm)
I can make it all work if I use FAAC directly, but the code would be a little cleaner if I could just use libavcodec, as I'm also encoding video, and writing both to an mp4.
ffmpeg version info:
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
Is there something I'm not setting, or setting incorrectly in my codec context, maybe? Any help is greatly appreciated!
Here is my test code:
#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);
}
The problem seems to go away if the bitrate is less than 386000. Not sure why this is, as I can encode at bitrates higher than that using FAAC directly. But 128000 is good enough for my purposes, so I'm able to move forward.
这篇关于恩code音频AAC与libav codeC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!