⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.c

📁 mepg 1 layer3软解码源代码,实现了对MP3文件的软件解码
💻 C
📖 第 1 页 / 共 4 页
字号:
{ unsigned long val=0; register int j = N; register int k, tmp; register int bit_idx = bs->buf_bit_idx; register int byte_idx = bs->buf_byte_idx; if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); while (j > 0) {    if (!bit_idx) {        bit_idx = 8;        byte_idx--;    }    k = MIN (j, bit_idx);    tmp = bs->buf[byte_idx]&putmask[bit_idx];    tmp = tmp >> (bit_idx-k);    val |= tmp << (j-k);    bit_idx -= k;    j -= k; } return(val);}/*read N bit from the bit stream */unsigned long getbits(bs, N)Bit_stream_struc *bs;   /* bit stream structure */int N;                  /* number of bits to read from the bit stream */{ unsigned long val=0; register int i; register int j = N; register int k, tmp; if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); bs->totbit += N; while (j > 0) {   if (!bs->buf_bit_idx) {        bs->buf_bit_idx = 8;        bs->buf_byte_idx--;        if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {             if (bs->eob)                bs->eobs = TRUE;             else {                for (i=bs->buf_byte_idx; i>=0;i--)                   bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];                refill_buffer(bs);                bs->buf_byte_idx = bs->buf_size-1;             }        }   }   k = MIN (j, bs->buf_bit_idx);   tmp = bs->buf[bs->buf_byte_idx]&putmask[bs->buf_bit_idx];   tmp = tmp >> (bs->buf_bit_idx-k);   val |= tmp << (j-k);   bs->buf_bit_idx -= k;   j -= k; } return(val);}/*write N bits into the bit stream */void putbits(bs, val, N)Bit_stream_struc *bs;   /* bit stream structure */unsigned int val;       /* val to write into the buffer */int N;                  /* number of bits of val */{ register int j = N; register int k, tmp; if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); bs->totbit += N; while (j > 0) {   k = MIN(j, bs->buf_bit_idx);   tmp = val >> (j-k);   bs->buf[bs->buf_byte_idx] |= (tmp&putmask[k]) << (bs->buf_bit_idx-k);   bs->buf_bit_idx -= k;   if (!bs->buf_bit_idx) {       bs->buf_bit_idx = 8;       bs->buf_byte_idx--;       if (bs->buf_byte_idx < 0)          empty_buffer(bs, MINIMUM);       bs->buf[bs->buf_byte_idx] = 0;   }   j -= k; }}/*write N bits byte aligned into the bit stream */void byte_ali_putbits(bs, val, N)Bit_stream_struc *bs;   /* bit stream structure */unsigned int val;       /* val to write into the buffer */int N;                  /* number of bits of val */{ unsigned long aligning, sstell(); if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); aligning = sstell(bs)%8; if (aligning)     putbits(bs, (unsigned int)0, (int)(8-aligning));  putbits(bs, val, N);}/*read the next bute aligned N bits from the bit stream */unsigned long byte_ali_getbits(bs, N)Bit_stream_struc *bs;   /* bit stream structure */int N;                  /* number of bits of val */{ unsigned long aligning, sstell(); if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); aligning = sstell(bs)%8; if (aligning)    getbits(bs, (int)(8-aligning)); return(getbits(bs, N));}/*return the current bit stream length (in bits)*/unsigned long sstell(bs)Bit_stream_struc *bs;   /* bit stream structure */{  return(bs->totbit);}/*return the status of the bit stream*//* returns 1 if end of bit stream was reached *//* returns 0 if end of bit stream was not reached */int end_bs(bs)Bit_stream_struc *bs;   /* bit stream structure */{  return(bs->eobs);}/*this function seeks for a byte aligned sync word in the bit stream and  places the bit stream pointer right after the sync.  This function returns 1 if the sync was found otherwise it returns 0  */int seek_sync(bs, sync, N)Bit_stream_struc *bs;   /* bit stream structure */long sync;      /* sync word maximum 32 bits */int N;          /* sync word length */{ double pow(); unsigned long aligning, stell(); unsigned long val; long maxi = (int)pow(2.0, (FLOAT)N) - 1; aligning = sstell(bs)%ALIGNING; if (aligning)    getbits(bs, (int)(ALIGNING-aligning));  val = getbits(bs, N);  while (((val&maxi) != sync) && (!end_bs(bs))) {        val <<= ALIGNING;        val |= getbits(bs, ALIGNING);  } if (end_bs(bs)) return(0); else return(1);}/*******************************************************************************  End of bit_stream.c package******************************************************************************//*******************************************************************************  CRC error protection package******************************************************************************/void I_CRC_calc(fr_ps, bit_alloc, crc)frame_params *fr_ps;unsigned int bit_alloc[2][SBLIMIT];unsigned int *crc;{        int i, k;        layer *info = fr_ps->header;        int stereo  = fr_ps->stereo;        int jsbound = fr_ps->jsbound;        *crc = 0xffff; /* changed from '0' 92-08-11 shn */        update_CRC(info->bitrate_index, 4, crc);        update_CRC(info->sampling_frequency, 2, crc);        update_CRC(info->padding, 1, crc);        update_CRC(info->extension, 1, crc);        update_CRC(info->mode, 2, crc);        update_CRC(info->mode_ext, 2, crc);        update_CRC(info->copyright, 1, crc);        update_CRC(info->original, 1, crc);        update_CRC(info->emphasis, 2, crc);        for (i=0;i<SBLIMIT;i++)                for (k=0;k<((i<jsbound)?stereo:1);k++)                        update_CRC(bit_alloc[k][i], 4, crc);}void II_CRC_calc(fr_ps, bit_alloc, scfsi, crc)frame_params *fr_ps;unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];unsigned int *crc;{        int i, k;        layer *info = fr_ps->header;        int stereo  = fr_ps->stereo;        int sblimit = fr_ps->sblimit;        int jsbound = fr_ps->jsbound;        al_table *alloc = fr_ps->alloc;        *crc = 0xffff; /* changed from '0' 92-08-11 shn */        update_CRC(info->bitrate_index, 4, crc);        update_CRC(info->sampling_frequency, 2, crc);        update_CRC(info->padding, 1, crc);        update_CRC(info->extension, 1, crc);        update_CRC(info->mode, 2, crc);        update_CRC(info->mode_ext, 2, crc);        update_CRC(info->copyright, 1, crc);        update_CRC(info->original, 1, crc);        update_CRC(info->emphasis, 2, crc);        for (i=0;i<sblimit;i++)                for (k=0;k<((i<jsbound)?stereo:1);k++)                        update_CRC(bit_alloc[k][i], (*alloc)[i][0].bits, crc);        for (i=0;i<sblimit;i++)                for (k=0;k<stereo;k++)                        if (bit_alloc[k][i])                                update_CRC(scfsi[k][i], 2, crc);}void update_CRC(data, length, crc)unsigned int data, length, *crc;{        unsigned int  masking, carry;        masking = 1 << length;        while((masking >>= 1)){                carry = *crc & 0x8000;                *crc <<= 1;                if (!carry ^ !(data & masking))                        *crc ^= CRC16_POLYNOMIAL;        }        *crc &= 0xffff;}/*******************************************************************************  End of CRC error protection package******************************************************************************/#ifdef  MACINTOSH/*******************************************************************************  Set Macintosh file attributes.******************************************************************************/void    set_mac_file_attr(fileName, vRefNum, creator, fileType)char    fileName[MAX_NAME_SIZE];short   vRefNum;OsType  creator;OsType  fileType;{short   theFile;char    pascal_fileName[MAX_NAME_SIZE];FInfo   fndrInfo;        CtoPstr(strcpy(pascal_fileName, fileName));        FSOpen(pascal_fileName, vRefNum, &theFile);        GetFInfo(pascal_fileName, vRefNum, &fndrInfo);        fndrInfo.fdCreator = creator;        fndrInfo.fdType = fileType;        SetFInfo(pascal_fileName, vRefNum, &fndrInfo);        FSClose(theFile);}#endif#ifdef  MS_DOS/* ------------------------------------------------------------------------new_ext.Puts a new extension name on a file name <filename>.Removes the last extension name, if any.92-08-19 shn------------------------------------------------------------------------ */char *new_ext(char *filename, char *extname){  int found, dotpos;  char newname[80];  /* First, strip the extension */  dotpos=strlen(filename); found=0;  do  {    switch (filename[dotpos])    {      case '.' : found=1; break;      case '\\':                  /* used by MS-DOS */      case '/' :                  /* used by UNIX */      case ':' : found=-1; break; /* used by MS-DOS in drive designation */      default  : dotpos--; if (dotpos<0) found=-1; break;    }  } while (found==0);  if (found==-1) strcpy(newname,filename);  if (found== 1) strncpy(newname,filename,dotpos); newname[dotpos]='\0';  strcat(newname,extname);  return(newname);}#endif#define BUFSIZE 4096static unsigned long offset,totbit=0, buf_byte_idx=0;static unsigned int buf[BUFSIZE];static unsigned int buf_bit_idx=8;/*return the current bit stream length (in bits)*/unsigned long hsstell(){  return(totbit);}/* int putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff}; */extern int putmask[9];/*read N bit from the bit stream */unsigned long hgetbits(N)int N;                  /* number of bits to read from the bit stream */{ unsigned long val=0; register int j = N; register int k, tmp;/* if (N > MAX_LENGTH)     printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);*/ totbit += N; while (j > 0) {   if (!buf_bit_idx) {        buf_bit_idx = 8;        buf_byte_idx++;	if (buf_byte_idx > offset)	  { printf("Buffer overflow !!\n");exit(3); }   }   k = MIN (j, buf_bit_idx);   tmp = buf[buf_byte_idx%BUFSIZE]&putmask[buf_bit_idx];   tmp = tmp >> (buf_bit_idx-k);   val |= tmp << (j-k);   buf_bit_idx -= k;   j -= k; } return(val);}unsigned int hget1bit(){return(hgetbits(1));}/*write N bits into the bit stream */void hputbuf(val, N)unsigned int val;       /* val to write into the buffer */int N;                  /* number of bits of val */{  if (N != 8) { printf("Not Supported yet!!\n"); exit(-3); }  buf[offset % BUFSIZE] = val;  offset++;}void rewindNbits( N )int N;{   totbit -= N;   buf_bit_idx += N;   while( buf_bit_idx >= 8 )   {  buf_bit_idx -= 8;      buf_byte_idx--;   }}void rewindNbytes( N )int N;{   totbit -= N*8;   buf_byte_idx -= N;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -