📄 avilib.c
字号:
}/* AVI_print_error: Print most recent error (similar to perror) */char *(avi_errors[]) ={ /* 0 */ "avilib - No Error", /* 1 */ "avilib - AVI file size limit reached", /* 2 */ "avilib - Error opening AVI file", /* 3 */ "avilib - Error reading from AVI file", /* 4 */ "avilib - Error writing to AVI file", /* 5 */ "avilib - Error writing index (file may still be useable)", /* 6 */ "avilib - Error closing AVI file", /* 7 */ "avilib - Operation (_read/_write) not permitted", /* 8 */ "avilib - Out of memory (malloc failed)", /* 9 */ "avilib - Not an AVI file", /* 10 */ "avilib - AVI file has no header list (corrupted?)", /* 11 */ "avilib - AVI file has no MOVI list (corrupted?)", /* 12 */ "avilib - AVI file has no video data", /* 13 */ "avilib - operation needs an index", /* 14 */ "avilib - Unkown Error"};static int num_avi_errors = sizeof(avi_errors)/sizeof(char*);static char error_string[4096];void AVI_print_error(char *str){ int aerrno; aerrno = (AVI_errno>=0 && AVI_errno<num_avi_errors) ? AVI_errno : num_avi_errors-1; fprintf(stderr,"%s: %s\n",str,avi_errors[aerrno]); /* for the following errors, perror should report a more detailed reason: */ if(AVI_errno == AVI_ERR_OPEN || AVI_errno == AVI_ERR_READ || AVI_errno == AVI_ERR_WRITE || AVI_errno == AVI_ERR_WRITE_INDEX || AVI_errno == AVI_ERR_CLOSE ) { perror("REASON"); }}char *AVI_strerror(){ int aerrno; aerrno = (AVI_errno>=0 && AVI_errno<num_avi_errors) ? AVI_errno : num_avi_errors-1; if(AVI_errno == AVI_ERR_OPEN || AVI_errno == AVI_ERR_READ || AVI_errno == AVI_ERR_WRITE || AVI_errno == AVI_ERR_WRITE_INDEX || AVI_errno == AVI_ERR_CLOSE ) { sprintf(error_string,"%s - %s",avi_errors[aerrno],#ifndef _WIN32 strerror(errno)#else _strerror(NULL)#endif ); return error_string; } else { return avi_errors[aerrno]; }}/////////////////////////////////////////////////////////////
int Wav_set_AVI( FILE *fwav,avi_t*AVI,char*buf,long*len)
{
char data[256];
char *dataptr;
char * fmt_data;
long n;
int i ;
if( fread(data,12,1,fwav) != 1 )
{
printf("this is a worry---get 12 bytes worry ");
return 0; //ERR_EXIT(AVI_ERR_READ)
}
dataptr=data;
for(i=0;i<12;i++)
{
printf("%c",dataptr[i]);/////////////////
}
printf("\n");
/*
for(i=0;i<4;i++)
{
printf("%d",dataptr[4+i]);
}
printf("\n");
*/
if( strncmp(dataptr ,"RIFF",4) !=0 ||strncmp(dataptr+8,"WAVE",4) !=0 )
{
printf("this is a worry--->get riff or wav.\n");
return 0; //ERR_EXIT(AVI_ERR_NO_AVI)
}
if(fread(data,8,1,fwav)!=1) ///////已经读到 "fmt+长度" 了
{
printf("0000000");
return 0;
}
////////////////////////////////////////////////////
dataptr=data;
for(i=0;i<8;i++)
{
printf("%c",dataptr[i]);
}
printf("\n");
/////////////////////////////////////////////////////
n=str2ulong(data+4);
printf("the length of fmt chuck is %d\n",n);
n=PAD_EVEN(n);
printf("%d\n",n);
if(strncmp(data,"fmt ",4)==0)
{
printf("this is the fmt chuck \n");
fmt_data=( char *)malloc(n);
if(fread(fmt_data,n,1,fwav)!=1)
{
free(fmt_data);
return 0; //ERR_EXIT(AVI_ERR_NO_MEM)
}
////////////////////////////////get fmt header information//////////////////
AVI->a_fmt=str2ushort(fmt_data);///short wformattag
printf("AVI->a_fmt==%x\n",AVI->a_fmt);
AVI->a_chans=str2ushort(fmt_data+2);////unsigned shor wchannels
printf("AVI->a_chans==%x\n",AVI->a_chans);
AVI->a_samplepersec=str2ulong(fmt_data+4);
printf("AVI->a_samplepersec==%x\n",AVI->a_samplepersec);
AVI->a_rate=str2ulong(fmt_data+8);
printf("AVI->a_rate==%x\n",AVI->a_rate);
AVI->a_nBlockSize=str2ushort(fmt_data+12);
printf("AVI->a_nBlockSize==%x\n",AVI->a_nBlockSize);
AVI->a_bits=str2ushort(fmt_data+14);
printf("AVI->a_bits==%x\n",AVI->a_bits);
if(AVI->a_fmt!=1)
{
AVI->a_cbsize=str2ushort(fmt_data+16);
printf("AVI->a_cbsize==%x\n",AVI->a_cbsize);
}
//////////////////////////////////////////////////////////////////////////////
/* AVI->a_rate=str2ulong(fmt_data+4);
AVI->a_samplepersec=str2ulong(fmt_data+8);
AVI->a_nBlockSize=str2ushort(fmt_data+12);////unsigned short wblockalign
printf("AVI->a_nBlockSize==%x\n",AVI->a_nBlockSize);
AVI->a_bits=str2ushort(fmt_data+14);
*/
/////////////////////////////////////////////////////////////////////
/*
AVI->a_samplepersec=str2ulong(fmt_data+4)*8;/////unsigned long dwsamplespersec
printf("AVI->a_samplepersec==%x\n",AVI->a_samplepersec);
AVI->a_rate=str2ulong(fmt_data+8);/////usigned long dwavbytespersec
printf("AVI->a_rate==%x\n",AVI->a_rate);
AVI->a_nBlockSize=str2ushort(fmt_data+12);////unsigned short wblockalign
printf("AVI->a_nBlockSize==%x\n",AVI->a_nBlockSize);
AVI->a_bits=str2ushort(fmt_data+14); ///unsigned short wbitspersecsample
printf("AVI->a_bits==%x\n",AVI->a_bits);
*/
/////////////////////////////////////////////////////////////////////////////
////////////////////////the above programme is the fmt header information///////////////
////////////////////////////////////////////////////////////////////////////////////////
free(fmt_data);
if(fread(data,8,1,fwav)!=1)
{
return 0;// ERR_EXIT(AVI_ERR_NO_MEM)
}
if(strncmp(data,"fact",4)==0)
{
printf("this is the fact chuch!!!!!!!!!\n");
n=str2ulong(data+4);
printf("the length of fact chuck is %d\n",n);
n=PAD_EVEN(n);
printf("the length of n=PAD_EVEN(n) is %d\n",n);
fmt_data=( char *)malloc(n);
if(fread(fmt_data,n,1,fwav)!=1)
{
free(fmt_data);
return 0; //ERR_EXIT(AVI_ERR_NO_MEM)
}
free(fmt_data);
if(fread(data,8,1,fwav)!=1)
{
return 0;
}
if(strncmp(data,"data",4)==0)
{
n=str2ulong(data+4);
n=PAD_EVEN(n);
*len=n;
printf("this is the data chunk.\n");
printf("the data chunk size is %x\n",*len);
}
}
else if(strncmp(data,"data",4)==0)
{
n=str2ulong(data+4);
n=PAD_EVEN(n);
*len=n;
printf("this is the data chunk.\n");
printf("the data chunk size is %x\n",*len);
/*
if(fread(buf,*len,1,fwav)!=1)
{
printf("getting wav data worry!\n");
return 0;
}
*/
// printf("getting wav data is rigth !\n");
}
return 1;
}
printf("it is not the data chuck.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
return 0;
}
int ReadDataWav(FILE *f, char *audbuf, long *plen)
{
/*0-EOF 1-video 2*/
int len = 0;
int ch;
int state = 0; //00 00 01 b6 1 2 3 4
if (feof(f))
return 0;
// int next_head = 0;
while (1)
{
if (feof(f))
{
if (len != 0)
return 1;
else
return 0;
}
ch = fgetc(f);
*(audbuf) = ch;
audbuf++;
len++;
if (len == 640) //len == 0x240 960
{
*plen = len;
return 1;
}
}//while(1)
}
///////////////////////////////////////////////////
int ReadDataMp4(FILE *f, char *vidbuf, long max_vidbuf,
char *audbuf, long max_audbuf,
long *plen)
{
/*0-EOF 1-video 2*/
int next_head = 0;
int next_head2 = 0;
int state = 0; //00 00 01 b6 1 2 3 4
int state2 = 0; //00 00 01 b6 1 2 3 4
int len = 0;
int ch;
//printf("aaaaaa\n");
if (feof(f))
return 0;
//printf("bbbbbbb\n");
while (1)
{
if (feof(f))
{
if (len != 0)
return 1;
else
return 0;
}
ch = fgetc(f);
*(vidbuf) = ch;
vidbuf++;
len++;
/******检测 00 00 01 B0 *****/
/*
if (ch == 0)
{
if (state == 1 || state == 2)
state = 2;
else
state = 1;
}
else if (ch == 1)
{
if (state == 2)
state = 3;
else state = 0;
}
else if (ch == 0xb0)
{
if (state == 3)
{
state = 4;
next_head++;
if (next_head == 1)
{
state = 0;
if(next_head2==1)
{
state = 0;
state2=0;
next_head = 0;
next_head2 = 0;
fseek(f, -4, SEEK_CUR);
len -= 4;
*plen = len;
return 1;
}
}
}
else
state = 0;
}
else
{
state = 0;
}
*/
/*******检测 00 00 01 B2 ***/
if (ch == 0)
{
if (state2 == 1 || state2 == 2)
state2 = 2;
else
state2 = 1;
}
else if (ch == 1)
{
if (state2 == 2)
state2 = 3;
else state2 = 0;
}
else if (ch == 0xb6)
{
if (state2 == 3)
{
state2 = 4;
next_head2++;
if (next_head2 == 1)
state2 = 0;
}
else
state2 = 0;
}
else
{
state2 = 0;
}
/*************************************/
if ((next_head == 2)||(next_head2 == 2))
{
state = 0;
// state2=0;
next_head = 0;
// next_head2 = 0;
fseek(f, -4, SEEK_CUR);
len -= 4;
*plen = len;
return 1;
}
}
}
int ReadDataMp3(FILE *f, char *audbuf, long *plen)
{
/*0-EOF 1-video 2*/
int state = 0; //00 00 01 b6 1 2 3 4
int len = 0;
int ch;
if (feof(f))
return 0;
// int next_head = 0;
while (1)
{
if (feof(f))
{
if (len != 0)
return 1;
else
return 0;
}
ch = fgetc(f);
*(audbuf) = ch;
audbuf++;
len++;
/////////////////////////////////////////////////////////
/*****************************
我觉得这样判断的话容易丢失帧头
除非把state设置为静态变量
*******************************/
/*
if (len == 0x240 )
{
*plen = len;
return 1;
}
*/
////////////////////////////////////////////////////////
if (ch == 0xff)
{
state = 1;
}
else if (ch == 0xfb)
{
if (state == 1)
state = 2;
else state = 0;
}
else state=0;
if (state == 2)
{
state = 0;
// next_head = 0;
//fseek(f, -2, SEEK_CUR);
len -= 2;
*plen = len;
return 1;
}
}
}
int ReadDataMp2(FILE *f, char *audbuf, long *plen)
{
/*0-EOF 1-video 2*/
int state = 0; //00 00 01 b6 1 2 3 4
int len = 0;
int ch;
if (feof(f))
return 0;
// int next_head = 0;
while (1)
{
if (feof(f))
{
if (len != 0)
return 1;
else
return 0;
}
ch = fgetc(f);
*(audbuf) = ch;
audbuf++;
len++;
/////////////////////////////////////////////////////////
/*****************************
我觉得这样判断的话容易丢失帧头
除非把state设置为静态变量
*******************************/
/*
if (len == 0x240 )
{
*plen = len;
return 1;
}
*/
////////////////////////////////////////////////////////
if (ch == 0xff)
{
state = 1;
}
else if (ch == 0xfd)
{
if (state == 1)
state = 2;
else state = 0;
}
else state=0;
if (state == 2)
{
state = 0;
// next_head = 0;
//fseek(f, -2, SEEK_CUR);
len -= 2;
*plen = len;
return 1;
}
}
}
int ReadDataWav2(FILE *f, char *audbuf, long *plen)
{
/*0-EOF 1-video 2*/
int len = 0;
int ch;
int state = 0; //00 00 01 b6 1 2 3 4
if (feof(f))
return 0;
// int next_head = 0;
while (1)
{
if (feof(f))
{
if (len != 0)
return 1;
else
return 0;
}
ch = fgetc(f);
*(audbuf) = ch;
audbuf++;
len++;
if (len == 480) //len == 0x240 960
{
*plen = len;
return 1;
}
}//while(1)
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -