|
小弟最近在学习ffmpeg音频的编解码,在用pcm编码为ulaw的时候遇到了下面的问题,下面是我的源码
avcodec *codec = avcodec_find_encoder(AV_CODEC_ID_PCM_MULAW);
avCodecContext *c = avcodec_alloc_context3(codec);
c->bit_rate = 128000;
c->codec_type = AVMEDIA_TYPE_AUDIO;
c->codec_id = AV_CODEC_ID_PCM_MULAW;
c->sample_fmt = AV_SAMPLE_FMT_S16;
c->channels = 1;
c->sample_rate = 8000;
c->channel_layout = 1 ;
if(avcodec_open2(c, codec, NULL)<0)
{
error();
}
AVFrame *frame = av_frame_alloc();
frame->nb_samples = c->frame_size;
frame->format = c->sample_fmt;
frame->channel_layout = c->channel_layout;
int buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size, c->sample_fmt, 0);
samples = av_malloc(buffer_size);
int ret = avcodec_fill_audio_frame(c.frame, c->channels, c->sample_fmt, (const uint8_t *)samples, buffer_size, 0);
av_init_packet(&packet);
while(!feof(fp))
{
packet.data = NULL;
packet.size = 0;
fread(samples, buffer_size, 1, fp);
ret = avcodec_encode_audio2(c.context, &packet, c.frame, &got_output);
if(got_output)
{
fwrite(packet.data, 1, packet.size, fp);
}
}
大概就是这样,一些出错处理和初始化略过不表,现在遇到的问题就是avcodec_open2之后的frame_size为0.导致av_samples_get_buffer_size失败
然后我自己设置buffer_size = sample_rate ×16/2×channels;结果编码出来的格式也不对,请大家教教我应该怎么做,然后这个frame_size是否就是一个pcm sample的大小?多谢,望回复!
|
|