本帖最后由 洛月 于 2013-10-10 16:05 编辑
这是我的代码
return 1的时候 开始编码 写入容器后 存在爆音 音频长度被拉长
int Codec::AFrameResample(AVFrame *dst, AVFrame *src)
{
if ((mAInCodeCtx->sample_fmt != mAOutCodeCtx->sample_fmt)||
(mAInCodeCtx->sample_rate != mAOutCodeCtx->sample_rate)||
(mAInCodeCtx->channels != mAOutCodeCtx->channels))
{
int dst_buf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE * 4;
*dst = *src;
dst->linesize[0] = src->linesize[0];
dst->type = 0;
avcodec_fill_audio_frame(dst, mAOutCodeCtx->channels, AV_SAMPLE_FMT_S16, dst->data[0], dst_buf_size, 0);
if(!mASwrCtx)
{
uint64_t in_channel_layout = av_get_default_channel_layout(mAInCodeCtx->channels);
uint64_t out_channel_layout = av_get_default_channel_layout(mAOutCodeCtx->channels);
mASwrCtx = swr_alloc_set_opts(NULL,
out_channel_layout,
AV_SAMPLE_FMT_S16,
mAOutCodeCtx->sample_rate,
in_channel_layout,
mAInCodeCtx->sample_fmt,
mAInCodeCtx->sample_rate,
0,
NULL);
swr_init(mASwrCtx);
}
if(mASwrCtx)
{
int out_count = dst_buf_size / mAOutCodeCtx->channels / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);
int in_count = src->linesize[0] / mAInCodeCtx->channels / av_get_bytes_per_sample(mAInCodeCtx->sample_fmt);
int ret = swr_convert(mASwrCtx,
dst->data,
out_count,
(const uint8_t**)src->data,
in_count);
if (ret < 0)return -1;
src->linesize[0] = ret * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * mAOutCodeCtx->channels;
// memcpy(src->data[0], dst->data[0], src->linesize[0]);
av_fifo_realloc2(mAFifo, av_fifo_size(mAFifo)+dst->linesize[0]);
av_fifo_generic_write(mAFifo, dst->data[0], src->linesize[0], NULL);
}
if(av_fifo_size(mAFifo) < dst->linesize[0])
{
return 0;
}
else
{
av_fifo_generic_read(mAFifo,
dst->data[0],
dst->linesize[0],
NULL);
return 1;
}
}
else
{
dst = src;
return 1;
}
} |