我的代码为什么会一直出现Fatal error decoding file

zhouguangying 2009-02-11 06:06:23
void faaddec(unsigned char *p_src,unsigned long size_src,short *p_dst,unsigned long *size_dst)
{
unsigned long samplerate, channels;

faacDecHandle hDecoder=faacDecOpen();

faacDecConfigurationPtr config=faacDecGetCurrentConfiguration(hDecoder);

faacDecSetConfiguration(hDecoder,config);

unsigned long buffercount=0 ;


if((buffercount =faacDecInit(hDecoder,p_src, &samplerate, &channels))<0)
{

fprintf(stderr, "Error initializing decoder library.\n");

}


short *sample;
sample=(short*)malloc(1024*channels*sizeof(short));
unsigned char *buffer;
buffer=(unsigned char* )malloc(768*channels*sizeof(unsigned char));
memcpy(buffer,p_src,768*channels);
unsigned long bytesconsumed,bytecount, samples, nsamples;
nsamples=0;
bytesconsumed=0;
samples=0;
bytecount=0;
int result;

do
{
if ( bytesconsumed> 0) {

memcpy(buffer,buffer+bytesconsumed,768*channels - bytesconsumed);

if ((size_src-bytecount)>bytesconsumed)
memcpy(buffer + (768*channels) - bytesconsumed, p_src+bytecount, bytesconsumed);
else
memcpy(buffer + (768*channels) - bytesconsumed, p_src+bytecount,(size_src-bytecount));

bytesconsumed = 0;
}

result = faacDecDecode(hDecoder, buffer, &bytesconsumed, sample, &samples);

if (result == FAAD_FATAL_ERROR)
fprintf(stderr, "Fatal error decoding file\n");
if (result == FAAD_ERROR)
fprintf(stderr, "Error decoding frame\n");


bytecount += bytesconsumed;

if ( samples > 0)//(result == FAAD_OK) &&
{
memcpy(p_dst+nsamples,sample,samples);

nsamples=nsamples+samples;
samples=0;

}

} while ((size_src-bytecount)>0);
*size_dst=nsamples;
}



int _tmain(int argc, _TCHAR* argv[])
{
FILE* hfile;
errno_t er = _wfopen_s( &hfile, L"E:\\tt\\aa22.aac", L"rb" );
if( er != 0 )
{
printf("open file failed.\n");
return 1;
}

FILE *outfile;

char * pcmfilename="E:\\tt\\aa22.pcm";
er=fopen_s( &outfile, pcmfilename, "wb" );

if( er != 0 )
{
fprintf(stderr, "Couldn't create output file %s\n",pcmfilename );

return 1;
}
unsigned char *buffer;
buffer = (unsigned char*)malloc(338060*sizeof(unsigned char));
memset(buffer, 0, 338060);
short *sample_Buffer;
sample_Buffer=(short*)malloc(3380600*sizeof(short));
fseek(hfile, 0, SEEK_END);
unsigned long fileread = ftell(hfile);
fseek(hfile, 0, SEEK_SET);

fread(buffer, 1,338060, hfile);
unsigned long dest_size;

faaddec(buffer,fileread,sample_Buffer,&dest_size);
cout<<"dest_size "<< dest_size;
fwrite(sample_Buffer,sizeof(short),dest_size, outfile);
fclose(hfile);
fclose(outfile);

return 0;
}
上面的代码会出现Fatal error decoding file
可是下面的代码就是正确的 请帮我分析一下
int _tmain(int argc, _TCHAR* argv[])
{
FILE* hfile;
errno_t er = _wfopen_s( &hfile, L"E:\\tt\\aa22.aac", L"rb" );
if( er != 0 )
{
printf("open file failed.\n");
return 1;
}

FILE *outfile;

char * pcmfilename="E:\\tt\\aa22.pcm";
er=fopen_s( &outfile, pcmfilename, "wb" );

if( er != 0 )
{
fprintf(stderr, "Couldn't create output file %s\n",pcmfilename );

return 1;
}
int result;
unsigned long bytesconsumed, samples, bytecount,buffercount;
unsigned char *buffer;
buffer = (unsigned char*)malloc(768*2*sizeof(unsigned char));
memset(buffer, 0, 768*2);
unsigned long samplerate, channels,fileread;

short *sample_Buffer;
sample_Buffer=(short*)malloc(1024*2*sizeof(short));
fseek(hfile, 0, SEEK_END);
fileread = ftell(hfile);
fseek(hfile, 0, SEEK_SET);
buffercount = bytecount = 0;
fread(buffer, 1, 768*2, hfile);


faacDecHandle hDecoder=faacDecOpen();

faacDecConfigurationPtr config=faacDecGetCurrentConfiguration(hDecoder);

faacDecSetConfiguration(hDecoder,config);



if((buffercount = faacDecInit(hDecoder, buffer, &samplerate, &channels)) < 0)
{
/* If some error initializing occured, skip the file */
fprintf(stderr, "Error initializing decoder library.\n");
return 1;
}
cout<<" samplerate "<<samplerate<<" channels "<<channels;

do
{
if (buffercount > 0) {
for (int k = 0; k < (768*2 - buffercount); k++)
buffer[k] = buffer[k + buffercount];

fread(buffer + (768*2) - buffercount, 1, buffercount, hfile);
buffercount = 0;
}

result = faacDecDecode(hDecoder, buffer, &bytesconsumed, sample_Buffer, &samples);
cout<<"samples "<<samples;
if (result == FAAD_FATAL_ERROR)
fprintf(stderr, "Fatal error decoding file\n");
if (result == FAAD_ERROR)
fprintf(stderr, "Error decoding frame\n");

buffercount += bytesconsumed;

bytecount += bytesconsumed;
if (bytecount >= fileread)
result = 9999;/* to make sure it stops now */
if ((result == FAAD_OK) && (samples > 0))
{


fwrite(sample_Buffer, sizeof(short), 1024*2, outfile);
}

} while (result <= FAAD_OK_CHUPDATE);

faacDecClose(hDecoder);



fclose(hfile);
fclose(outfile);

return 0;

}



...全文
332 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
alexhilton 2009-02-12
  • 打赏
  • 举报
回复
what is it all about?
coding standards!!!
zhouguangying 2009-02-12
  • 打赏
  • 举报
回复
谢谢你们 我解决了 是faad 解码 测试程序 是memcpy 那里出错
lzonline01 2009-02-11
  • 打赏
  • 举报
回复
写的太乱了 而且也没什么注释

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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