FFMPEG AAC 音频解码问题

zsx19861012 2012-12-25 08:37:49
我这边使用ffmpeg0.11 中avcodec_decode_audio4 去解码AAC LATM 的音频数据,偶尔出现解码后AVFrame 中data[0](PCM 数据)全为0, 但是函数的返回值,包括got_frame 参数返回值显示都是正常的:
具体代码如下:
{
int ret, len, data_size,planar, ch ,plane_size;
AVFrame frame;
int got_frame;
AVPacket i_packet;
int decode_times;
int flush_complete;
PrivAVFrame *i_privframe, *dst_privframe;

if(frames == NULL || packet == NULL)
return -1;
len = 0;
decode_times = 0;
flush_complete = 0;
while(packet->data_size > 0)
{
//frame default value
avcodec_get_frame_defaults(&frame);

if(flush_complete)
break;
av_init_packet(&i_packet);
i_packet.data = packet->data;
i_packet.size = packet->data_size;
#if 0
if(mCodecContext->get_buffer != avcodec_default_get_buffer)
{
mCodecContext->get_buffer = avcodec_default_get_buffer;
mCodecContext->release_buffer = avcodec_default_release_buffer;
}
#endif
len = avcodec_decode_audio4(mCodecContext, &frame, &got_frame, &i_packet);
if(len < 0)
{
OS_log(LVL_ERR, 0, "Decoding audio error");
av_free_packet(&i_packet);
frames->data_size = 0;
return -1;
}

packet->data += len;
packet->data_size -= len;
#if 1
LOGV("ret (%d), size(%d), got_frame(%d)",len, i_packet.size, got_frame);
#endif
if(!got_frame)
{
if(!i_packet.data && mCodecContext->codec->capabilities & CODEC_CAP_DELAY)
flush_complete = 1;
frames->data_size = 0;
continue;
}
#if 0
planar = av_sample_fmt_is_planar(mCodecContext->sample_fmt);
data_size = av_samples_get_buffer_size(&plane_size,
mCodecContext->channels,
frame.nb_samples,
mCodecContext->sample_fmt, 1);
#else
data_size = av_samples_get_buffer_size(NULL,
mCodecContext->channels,
frame.nb_samples,
mCodecContext->sample_fmt, 1);
#endif


if(decode_times == 0)
{
privav_init_frame(frames);
if(frames->data == NULL)
{
ret = privav_new_frame(frames, data_size);
if(ret < 0)
return -1;
}
else
{
if(frames->data_size < data_size)
{
OS_log(LVL_ERR,0,"Output buffer is not enough long(%d < %d)",frames->data_size, data_size);
return -1;
}
}

dst_privframe = frames;
}
else
{
i_privframe = (PrivAVFrame *)malloc(sizeof(PrivAVFrame));
if(i_privframe)
{
ret = privav_new_frame(i_privframe, data_size);
if(ret < 0)
{
free(i_privframe);
i_privframe = NULL;
return -1;
}
i_privframe->alloc_flag = 1;
dst_privframe->next = i_privframe;

dst_privframe = dst_privframe->next;
}
else
{
//give up the last frames;
break;
}

}
#if 1
LOGV("frame size (%d), 0x%x, 0x%x", data_size, frame.data[0][0], frame.data[0][1]);
if(frame.data[0][0] == 0x00 && frame.data[0][1] == 0x00 &&frame.data[0][20] == 0x00&&frame.data[0][21] == 0x00 )
{
//free(dst_privframe);
LOGE("AUDIO is 00, reopen the decoder");
if(mCodecContext)
{
avcodec_close(mCodecContext);
if(avcodec_open2(mCodecContext, mCodec,NULL) < 0)
{
LOGE("open decoder error");
}
LOGV("DECODER channels(%d), sample-rate(%d), sample-fmt(%d)",
mCodecContext->channels,mCodecContext->sample_rate, mCodecContext->sample_fmt);
}
}

#endif
#if 0
//data copying
memcpy(dst_privframe->data, frame.extended_data[0], plane_size);
if(planar && mCodecContext->channels > 1)
{
uint8_t *out = dst_privframe->data + plane_size;
for(ch = 1; ch < mCodecContext->channels; ch++)
{
memcpy(out, frame.extended_data[ch], plane_size);
out += plane_size;
}
}
#else
memcpy(dst_privframe->data, frame.data[0], data_size);
#endif
if(packet->pts > 0)
{
audio_clock = packet->pts;
packet->pts = -1;
}
dst_privframe->pts = audio_clock;
//frames->pos= packet->pos;
dst_privframe->key_frame = frame.key_frame;
//reset the actual size of data
dst_privframe->data_size = data_size;
audio_clock += data_size/
(mCodecContext->channels * mCodecContext->sample_rate *av_get_bytes_per_sample(mCodecContext->sample_fmt));

decode_times++;

//first frame is decoded
//return len;
break;
}
return len;
}

中间红色部分 显示输出结果,为全0, 后面的解码也全都为0,导致整个视频后面没有声音。
这个问题困扰了我几天,希望大神们可以帮忙回答,谢谢!
...全文
691 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxy20121005 2013-02-27
  • 打赏
  • 举报
回复
你用的是哪个版本的,低版本的可以解啊.交流QQ:1049568282

2,543

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧