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

zhouguangying zhouguangying 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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;

}



...全文
评论 3
收藏 已收藏
分享
返回顶部
zhouguangying
C语言 70,040 成员 243,237 内容