ffmpeg解码出的pcm数据使用edit cool播放全是杂音,求大神看看。

jenf 2013-05-31 10:46:23

// Register all formats and codecs
av_register_all();
avcodec_register_all();

//av_log_set_callback(log_callback);

USES_CONVERSION;
string strSrcFile = P2P_W2A(pFileData->strInputFile);

pFormatCtxDec = avformat_alloc_context();
if (NULL == pFormatCtxDec)
{
return;
}

// Open video file
#define av_open_input_file avformat_open_input
if(av_open_input_file(&pFormatCtxDec, strSrcFile.c_str(), NULL, NULL)!=0)
return ; // Couldn't open file

// Retrieve stream information
if(av_find_stream_info(pFormatCtxDec)<0)
return ; // Couldn't find stream information
for(i=0; i<pFormatCtxDec->nb_streams; i++)
{
if(pFormatCtxDec->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
videoStream=i;
}
else if(pFormatCtxDec->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
{
audioStream=i;
}
}
if (audioStream != -1)
{
pACodecCtxDec = pFormatCtxDec->streams[audioStream]->codec;
}
if (pACodecCtxDec)
{
pACodecDec=avcodec_find_decoder(pACodecCtxDec->codec_id);
}
// Open codec
BOOL bOpenAudio = FALSE;
if (pACodecCtxDec && pACodecDec)
{
if(avcodec_open2(pACodecCtxDec, pACodecDec, NULL)>=0)
{
bOpenAudio = TRUE;
}
}
pAFrameDec = avcodec_alloc_frame();
if (audioStream!=-1 && pAFrameDec == NULL)
{
return;
}
while(av_read_frame(pFormatCtxDec, &packet)>=0)
{
if (packet.stream_index==audioStream)
{
int ret = 0;
while(packet.size > 0)
{
ret = avcodec_decode_audio4(pACodecCtxDec,pAFrameDec,&got_output,&packet);//若为音频包,解码该音频包
if(got_output == 0)
{
continue;
}

int data_size = av_samples_get_buffer_size(pAFrameDec->linesize, pACodecCtxDec->channels,
pAFrameDec->nb_samples,
pACodecCtxDec->sample_fmt, 0);

int n = fwrite(pAFrameDec->data[0], 1, pAFrameDec->linesize[0], TmpAudio);
fflush(TmpAudio);
packet.size -= ret;
packet.data += ret;

...全文
612 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangsky2 2013-07-03
  • 打赏
  • 举报
回复
不知道楼主解码的是mp3文件还是amr文件,我之前也遇到这个问题,用下面的代码可以对mp3保存为pcm正常。但是对amr保存为pcm还是不正常 char *data = (char *)malloc(data_size); short *sample_buffer = (short *)frame->data[0];


	////mp3 to pcm
	for (int i = 0; i < data_size/2; i++)
	{
		data[i*2] = (char)(sample_buffer[i/2] & 0xFF);
		data[i*2+1] = (char)((sample_buffer[i/2] >>8) & 0xFF);		

	}
	fwrite(data, data_size, 1, pcm);
	fflush(pcm);
wangzongze1015 2013-06-20
  • 打赏
  • 举报
回复
我之前遇到这问题。有一部分原因是数据真的频率间隔可能造成的,你查查看
jenf 2013-06-05
  • 打赏
  • 举报
回复
嗯,我也是想不是ffmpeg的问题,就想看看是不是哪里的参数设置不对造成的,看看还有没有遇到同样问题的人说说有没有啥解决方法。
sxcong 2013-06-05
  • 打赏
  • 举报
回复
ffmpeg带的example你看了吗?直接用那个试试 /* * Audio decoding. */ static void audio_decode_example(const char *outfilename, const char *filename) { AVCodec *codec; AVCodecContext *c= NULL; int len; FILE *f, *outfile; uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; AVPacket avpkt; AVFrame *decoded_frame = NULL; av_init_packet(&avpkt); printf("Decode audio file %s to %s\n", filename, outfilename); /* find the mpeg audio decoder */ codec = avcodec_find_decoder(AV_CODEC_ID_MP2); if (!codec) { fprintf(stderr, "Codec not found\n"); exit(1); } c = avcodec_alloc_context3(codec); /* open it */ if (avcodec_open2(c, codec, NULL) < 0) { fprintf(stderr, "Could not open codec\n"); exit(1); } f = fopen(filename, "rb"); if (!f) { fprintf(stderr, "Could not open %s\n", filename); exit(1); } outfile = fopen(outfilename, "wb"); if (!outfile) { av_free(c); exit(1); } /* decode until eof */ avpkt.data = inbuf; avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); while (avpkt.size > 0) { int got_frame = 0; if (!decoded_frame) { if (!(decoded_frame = avcodec_alloc_frame())) { fprintf(stderr, "Could not allocate audio frame\n"); exit(1); } } else avcodec_get_frame_defaults(decoded_frame); len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt); if (len < 0) { fprintf(stderr, "Error while decoding\n"); exit(1); } if (got_frame) { /* if a frame has been decoded, output it */ int data_size = av_samples_get_buffer_size(NULL, c->channels, decoded_frame->nb_samples, c->sample_fmt, 1); fwrite(decoded_frame->data[0], 1, data_size, outfile); } avpkt.size -= len; avpkt.data += len; avpkt.dts = avpkt.pts = AV_NOPTS_VALUE; if (avpkt.size < AUDIO_REFILL_THRESH) { /* Refill the input buffer, to avoid trying to decode * incomplete frames. Instead of this, one could also use * a parser, or use a proper container format through * libavformat. */ memmove(inbuf, avpkt.data, avpkt.size); avpkt.data = inbuf; len = fread(avpkt.data + avpkt.size, 1, AUDIO_INBUF_SIZE - avpkt.size, f); if (len > 0) avpkt.size += len; } } fclose(outfile); fclose(f); avcodec_close(c); av_free(c); avcodec_free_frame(&decoded_frame); }
SoftSoftSoft2008 2013-06-03
  • 打赏
  • 举报
回复
我感觉应该不是ffmpeg的问题,可能是别的原因吧。他们发布时候肯定都做了测试的,不会出现这种错误的。
jenf 2013-05-31
  • 打赏
  • 举报
回复
mp3,wma众多格式解码均有问题,求大神指导!!!
jenf 2013-05-31
  • 打赏
  • 举报
回复
有ffmpeg大神么,求帮忙。。。

2,543

社区成员

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

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