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

📄 common.c

📁 ISO mp3 sources (distribution 10) Layer 1/2/3, C Source, 512 k Sources of the Mpeg 1,2 layer 1,2
💻 C
📖 第 1 页 / 共 4 页
字号:
    ident.name[0] = ident.name[3];    ident.name[3] = holder;    holder = ident.name[1];    ident.name[1] = ident.name[2];    ident.name[2] = holder;    ident.ck_length = _lrotl (ident.ck_length, 8);#endif/* fixed bug in next line, HP 27-may-93 */    while (strncmp (ident.name, IFF_ID_SSND, 4) != 0)    {	dummy = (char *) mem_alloc (ident.ck_length * sizeof (char),  "dummy");	if (fread (dummy, ident.ck_length, 1, file_ptr) != 1)	    return (-1);	mem_free ((void **) &dummy);	if (fread (&ident, sizeof (identifier), 1, file_ptr) != 1)	    return (-1);/* the following lines are not necessary, HP 27-may-93 *//*	{	fseek (file_ptr, 0, SEEK_SET);	if (fread (&ident, sizeof (identifier), 1, file_ptr) != 1)	return (-1);	}*/#ifdef MSDOS	holder = ident.name[0];	ident.name[0] = ident.name[3];	ident.name[3] = holder;	holder = ident.name[1];	ident.name[1] = ident.name[2];	ident.name[2] = holder;	ident.ck_length = _lrotl (ident.ck_length, 8);#endif    }    for (i = 0; i < 4; ++i)	SndDChunk.ckID[i] = ident.name[i];    SndDChunk.ckSize = ident.ck_length;    if (fread (&SndDChunk.offset, sizeof (unsigned long), 1, file_ptr) != 1)	return (-1);    if (fread (&SndDChunk.blockSize, sizeof (unsigned long), 1, file_ptr) != 1)	return (-1);#ifdef MSDOS    SndDChunk.offset = _lrotl (SndDChunk.offset, 8);    SndDChunk.blockSize = _lrotl (SndDChunk.blockSize, 8);#endif/* why seek behinde the SSND Chunk ????, HP 27-may-93 *//*    seek_offset = SndDChunk.ckSize - sizeof (SoundDataChunk) + sizeof (ChunkHeader);    if (fseek (file_ptr, seek_offset, SEEK_CUR) != 0)	return (-1);*/    aiff_ptr->numChannels       = CommChunk.numChannels;    aiff_ptr->numSampleFrames   = CommChunk.numSampleFrames;    aiff_ptr->sampleSize        = CommChunk.sampleSize;    aiff_ptr->blkAlgn.offset    = SndDChunk.offset;    aiff_ptr->blkAlgn.blockSize = SndDChunk.blockSize;    strncpy (aiff_ptr->sampleType, SndDChunk.ckID, 4);     return (0);}/*******************************************************************************  Seek past some Audio Interchange File Format (AIFF) headers to sound data.******************************************************************************/int aiff_seek_to_sound_data (FILE *file_ptr){    if (fseek (file_ptr, sizeof (Chunk) + sizeof (SoundDataChunk), SEEK_SET) != 0)	return (-1);    else	return (0);}/*********************************************************************************  Write Audio Interchange File Format (AIFF) headers.********************************************************************************/int aiff_write_headers (FILE *file_ptr, IFF_AIFF *aiff_ptr){    register char   i;    register long   seek_offset;        char            temp_sampleRate[10];        Chunk           FormChunk;    CommonChunk     CommChunk;    SoundDataChunk  SndDChunk;    strcpy (FormChunk.ckID, IFF_ID_FORM);    strcpy (FormChunk.formType, IFF_ID_AIFF);    strcpy (CommChunk.ckID, IFF_ID_COMM);  /*7/7/93,SR,changed FormChunk to CommChunk*/    double_to_extended (&aiff_ptr->sampleRate, temp_sampleRate);    for (i = 0; i < sizeof (char[10]); i++)	CommChunk.sampleRate[i] = temp_sampleRate[i];    CommChunk.numChannels             = aiff_ptr->numChannels;    CommChunk.numSampleFrames         = aiff_ptr->numSampleFrames;    CommChunk.sampleSize              = aiff_ptr->sampleSize;    SndDChunk.offset                  = aiff_ptr->blkAlgn.offset;    SndDChunk.blockSize               = aiff_ptr->blkAlgn.blockSize;    strncpy (SndDChunk.ckID, aiff_ptr->sampleType,  4);     CommChunk.ckSize = sizeof (CommChunk.numChannels) +		       sizeof (CommChunk.numSampleFrames) +		       sizeof (CommChunk.sampleSize) +		       sizeof (CommChunk.sampleRate);     SndDChunk.ckSize = sizeof (SoundDataChunk) - sizeof (ChunkHeader) +	(CommChunk.sampleSize + BITS_IN_A_BYTE - 1) / BITS_IN_A_BYTE *	CommChunk.numChannels * CommChunk.numSampleFrames;     FormChunk.ckSize = sizeof (Chunk) + SndDChunk.ckSize + sizeof (ChunkHeader) +	CommChunk.ckSize;     if (fseek (file_ptr, 0, SEEK_SET) != 0)	return (-1);     if (fwrite (&FormChunk, sizeof (Chunk), 1, file_ptr) != 1)	return (-1);     if (fwrite (&SndDChunk, sizeof (SoundDataChunk), 1, file_ptr) != 1)	return (-1);     seek_offset = SndDChunk.ckSize - sizeof (SoundDataChunk) + sizeof (ChunkHeader);     if (fseek (file_ptr, seek_offset, SEEK_CUR) != 0)	return (-1);     if (fwrite (CommChunk.ckID, sizeof (ID), 1, file_ptr) != 1)	return (-1);     if (fwrite (&CommChunk.ckSize, sizeof (long), 1, file_ptr) != 1)	return (-1);     if (fwrite (&CommChunk.numChannels, sizeof (short), 1, file_ptr) != 1)	return (-1);     if (fwrite (&CommChunk.numSampleFrames, sizeof (unsigned long), 1, file_ptr) != 1)	return (-1);     if (fwrite (&CommChunk.sampleSize, sizeof (short), 1, file_ptr) != 1)	return (-1);     if (fwrite (CommChunk.sampleRate, sizeof (char[10]), 1, file_ptr) != 1)	return (-1);     return (0);}/*******************************************************************************  bit_stream.c package*  Author:  Jean-Georges Fritsch, C-Cube Microsystems******************************************************************************//********************************************************************  This package provides functions to write (exclusive or read)  information from (exclusive or to) the bit stream.  If the bit stream is opened in read mode only the get functions are  available. If the bit stream is opened in write mode only the put  functions are available.********************************************************************//* refill the buffer from the input device when the buffer becomes empty    */int refill_buffer(Bit_stream_struc *bs)                        /* bit stream structure */{   register int i=bs->buf_size-2-bs->buf_byte_idx;   register unsigned long n;   register int index=0;   char val[2];   while ((i>=0) && (!bs->eob)) {      if (bs->format == BINARY)         n = fread(&bs->buf[i--], sizeof(unsigned char), 1, bs->pt);      else {	  while((index < 2) && n) {		n = fread(&val[index], sizeof(char), 1, bs->pt);		switch (val[index]) {			case 0x30:			case 0x31:			case 0x32:			case 0x33:			case 0x34:			case 0x35:			case 0x36:			case 0x37:			case 0x38:			case 0x39:			case 0x41:			case 0x42:			case 0x43:			case 0x44:			case 0x45:			case 0x46:			index++;			break;			default: break;		}	   }           if (val[0] <= 0x39) bs->buf[i] = (val[0] - 0x30) << 4;                 else  bs->buf[i] = (val[0] - 0x37) << 4;           if (val[1] <= 0x39) bs->buf[i--] |= (val[1] - 0x30);                 else  bs->buf[i--] |= (val[1] - 0x37);	   index = 0;      }      if (!n) {         bs->eob= i+1;      }    }}static char *he = "0123456789ABCDEF";/* empty the buffer to the output device when the buffer becomes full */void empty_buffer(Bit_stream_struc *bs, int minimum)                        /* bit stream structure */                        /* end of the buffer to empty */{   register int i;#if BS_FORMAT == BINARY   for (i=bs->buf_size-1;i>=minimum;i--)      fwrite(&bs->buf[i], sizeof(unsigned char), 1, bs->pt);#else   for (i=bs->buf_size-1;i>=minimum;i--) {       char val[2];       val[0] = he[((bs->buf[i] >> 4) & 0x0F)];       val[1] = he[(bs->buf[i] & 0x0F)];       fwrite(val, sizeof(char), 2, bs->pt);   }#endif   for (i=minimum-1; i>=0; i--)       bs->buf[bs->buf_size - minimum + i] = bs->buf[i];   bs->buf_byte_idx = bs->buf_size -1 - minimum;   bs->buf_bit_idx = 8;}/* open the device to write the bit stream into it */void open_bit_stream_w(Bit_stream_struc *bs, char *bs_filenam, int size)                        /* bit stream structure */                        /* name of the bit stream file */                        /* size of the buffer */{   if ((bs->pt = fopen(bs_filenam, "w+")) == NULL) {      printf("Could not create \"%s\".\n", bs_filenam);      exit(0);   }   alloc_buffer(bs, size);   bs->buf_byte_idx = size-1;   bs->buf_bit_idx=8;   bs->totbit=0;   bs->mode = WRITE_MODE;   bs->eob = FALSE;   bs->eobs = FALSE;}/* open the device to read the bit stream from it */void open_bit_stream_r(Bit_stream_struc *bs, char *bs_filenam, int size)                        /* bit stream structure */                        /* name of the bit stream file */                        /* size of the buffer */{   register unsigned long n;   register int i=0;   register unsigned char flag = 1;   unsigned char val;   if ((bs->pt = fopen(bs_filenam, "rb")) == NULL) {      printf("Could not find \"%s\".\n", bs_filenam);      exit(0);   }   do {     n = fread(&val, sizeof(unsigned char), 1, bs->pt);     switch (val) {      case 0x30:      case 0x31:      case 0x32:      case 0x33:      case 0x34:      case 0x35:      case 0x36:      case 0x37:      case 0x38:      case 0x39:      case 0x41:      case 0x42:      case 0x43:      case 0x44:      case 0x45:      case 0x46:      case 0xa:  /* \n */          break;      default: /* detection of an binary character */          flag--;          i = 300;          break;     }   } while (flag & n);   if (flag) {      if (verbosity >= 2) printf ("the bit stream file %s is an ASCII file\n", bs_filenam);      bs->format = ASCII;   }   else {      bs->format = BINARY;      if (verbosity >= 2) printf ("the bit stream file %s is a BINARY file\n", bs_filenam);   }   fclose(bs->pt);   if ((bs->pt = fopen(bs_filenam, "rb")) == NULL) {      printf("Could not find \"%s\".\n", bs_filenam);      exit(0);   }   alloc_buffer(bs, size);   bs->buf_byte_idx=0;   bs->buf_bit_idx=0;   bs->totbit=0;   bs->mode = READ_MODE;   bs->eob = FALSE;   bs->eobs = FALSE;}/* close the device containing the bit stream after a read process */void close_bit_stream_r (Bit_stream_struc *bs){    fclose (bs->pt);    desalloc_buffer (bs);}/*close the device containing the bit stream after a write process*/void close_bit_stream_w (Bit_stream_struc *bs){    empty_buffer (bs, bs->buf_byte_idx + 1);    fclose (bs->pt);    desalloc_buffer (bs);}/* open and initialize the buffer; */void alloc_buffer (Bit_stream_struc *bs, int size){    bs->buf = (unsigned char *) mem_alloc (size * sizeof (unsigned char), "buffer");    bs->buf_size = size;}/* empty and close the buffer */void desalloc_buffer (Bit_stream_struc *bs){   free (bs->buf);}int putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff};int mask[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};/*read 1 bit from the bit stream */unsigned int get1bit(Bit_stream_struc *bs)                        /* bit stream structure */{   unsigned int bit;   register int i;   bs->totbit++;   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;             }        }   }   bit = bs->buf[bs->buf_byte_idx]&mask[bs->buf_bit_idx-1];   bit = bit >> (bs->buf_bit_idx-1);   bs->buf_bit_idx--;#ifdef	PrintBitDebug   printf ("pos: %5d getbits: %2d code: %4x val: %5d\n",		bs->totbit-1,  1, bit, bit);   fflush (stdout);#endif   return(bit);}/*write 1 bit from the bit stream */void put1bit(Bit_stream_struc *bs, int bit)                        /* bit stream structure */                        /* bit to write into the buffer */{   register int i;#ifdef	PrintBitDebug   printf ("pos: %5d putbits: %2d code: %4x val: %5d\n",		bs->totbit,  1, bit, bit);   fflush (stdout);#endif   bs->totbit++;   bs->buf[bs->buf_byte_idx] |= (bit&0x1) << (bs->buf_bit_idx-1);   bs->buf_bit_idx--;   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;   }}/*read N bit from the bit stream */unsigned long getbits(Bit_stream_struc *bs, int N)                        /* bit stream structure */                        /* 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; }#ifdef	PrintBitDebug printf ("pos: %5d getbits: %2d code: %4x val: %5d\n", 		 bs->totbit - N,  N, val, val); fflush (stdout);#endif return(val);}/*write N bits into the bit stream */void putbits(Bit_stream_struc *bs, unsigned int val, int N)                        /* bit stream structure */                        /* val to write into the buffer */                        /* number of bits of val */{ 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);#ifdef	PrintBitDebug printf ("pos: %5d putbits: %2d code: %4x val: %5d\n", 

⌨️ 快捷键说明

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