📄 common.c
字号:
/********************************************************************************* Allocate number of bytes of memory equal to "block".********************************************************************************/void FAR *mem_alloc(block, item)unsigned long block;char *item;{ void *ptr;#ifdef MACINTOSH ptr = NewPtr(block);#endif#ifdef MSC60 /*ptr = (void FAR *) _fmalloc((unsigned int)block);*/ /* far memory, 92-07-08 sr */ ptr = (void FAR *) malloc((unsigned int)block); /* far memory, 93-08-24 ss */#endif#if ! defined (MACINTOSH) && ! defined (MSC60) ptr = (void FAR *) malloc(block);#endif if (ptr != NULL){#ifdef MSC60 _fmemset(ptr, 0, (unsigned int)block); /* far memory, 92-07-08 sr */#else memset(ptr, 0, block);#endif } else{ printf("Unable to allocate %s\n", item); exit(0); } return(ptr);}/****************************************************************************** Free memory pointed to by "*ptr_addr".******************************************************************************/void mem_free(ptr_addr)void **ptr_addr;{ if (*ptr_addr != NULL){#ifdef MACINTOSH DisposPtr(*ptr_addr);#else free(*ptr_addr);#endif *ptr_addr = NULL; }}/********************************************************************************* Check block of memory all equal to a single byte, else return FALSE********************************************************************************/int memcheck(array, test, num)char *array;int test; /* but only tested as a char (bottom 8 bits) */int num;{ int i=0; while (array[i] == test && i<num) i++; if (i==num) return TRUE; else return FALSE;}/******************************************************************************* Routines to determine byte order and swap bytes******************************************************************************/enum byte_order NativeByteOrder = order_unknown;enum byte_order DetermineByteOrder(){ char s[ sizeof(long) + 1 ]; union { long longval; char charval[ sizeof(long) ]; } probe; probe.longval = 0x41424344L; /* ABCD in ASCII */ strncpy( s, probe.charval, sizeof(long) ); s[ sizeof(long) ] = '\0'; /* fprintf( stderr, "byte order is %s\n", s ); */ if ( strcmp(s, "ABCD") == 0 ) return order_bigEndian; else if ( strcmp(s, "DCBA") == 0 ) return order_littleEndian; else return order_unknown;}void SwapBytesInWords( short *loc, int words ){ int i; short thisval; char *dst, *src; src = (char *) &thisval; for ( i = 0; i < words; i++ ) { thisval = *loc; dst = (char *) loc++; dst[0] = src[1]; dst[1] = src[0]; }}/***************************************************************************** * * Read Audio Interchange File Format (AIFF) headers. * *****************************************************************************/int aiff_read_headers( FILE *file_ptr, IFF_AIFF *aiff_ptr ){ int i, chunkSize, subSize, sound_position; if ( fseek(file_ptr, 0, SEEK_SET) != 0 ) return -1; if ( Read32BitsHighLow(file_ptr) != IFF_ID_FORM ) return -1; chunkSize = Read32BitsHighLow( file_ptr ); if ( Read32BitsHighLow(file_ptr) != IFF_ID_AIFF ) return -1; sound_position = 0; while ( chunkSize > 0 ) { chunkSize -= 4; switch ( Read32BitsHighLow(file_ptr) ) { case IFF_ID_COMM: chunkSize -= subSize = Read32BitsHighLow( file_ptr ); aiff_ptr->numChannels = Read16BitsHighLow( file_ptr ); subSize -= 2; aiff_ptr->numSampleFrames = Read32BitsHighLow( file_ptr ); subSize -= 4; aiff_ptr->sampleSize = Read16BitsHighLow( file_ptr ); subSize -= 2; aiff_ptr->sampleRate = ReadIeeeExtendedHighLow( file_ptr ); subSize -= 10; while ( subSize > 0 ) { getc( file_ptr ); subSize -= 1; } break; case IFF_ID_SSND: chunkSize -= subSize = Read32BitsHighLow( file_ptr ); aiff_ptr->blkAlgn.offset = Read32BitsHighLow( file_ptr ); subSize -= 4; aiff_ptr->blkAlgn.blockSize = Read32BitsHighLow( file_ptr ); subSize -= 4; sound_position = ftell( file_ptr ) + aiff_ptr->blkAlgn.offset; if ( fseek(file_ptr, (long) subSize, SEEK_CUR) != 0 ) return -1; aiff_ptr->sampleType = IFF_ID_SSND; break; default: chunkSize -= subSize = Read32BitsHighLow( file_ptr ); while ( subSize > 0 ) { getc( file_ptr ); subSize -= 1; } break; } } return sound_position;}/***************************************************************************** * * 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, AIFF_FORM_HEADER_SIZE + AIFF_SSND_HEADER_SIZE, SEEK_SET) != 0 ) return(-1); return(0);}/******************************************************************************* * * Write Audio Interchange File Format (AIFF) headers. * *******************************************************************************/int aiff_write_headers( FILE *file_ptr, IFF_AIFF *aiff_ptr ){ int chunkSize; int sampleBytes = (aiff_ptr->sampleSize / 8) + (aiff_ptr->sampleSize % 8 ? 1 : 0); if ( fseek(file_ptr, 0L, SEEK_SET) != 0 ) return -1; /* write FORM chunk */ chunkSize = 8 + 18 + 8 + aiff_ptr->numChannels * aiff_ptr->numSampleFrames * sampleBytes; Write32BitsHighLow( file_ptr, IFF_ID_FORM ); Write32BitsHighLow( file_ptr, chunkSize ); Write32BitsHighLow( file_ptr, IFF_ID_AIFF ); /* write COMM chunk */ Write32BitsHighLow( file_ptr, IFF_ID_COMM ); Write32BitsHighLow( file_ptr, 18 ); /* chunk size */ Write16BitsHighLow( file_ptr, aiff_ptr->numChannels ); Write32BitsHighLow( file_ptr, aiff_ptr->numSampleFrames ); Write16BitsHighLow( file_ptr, aiff_ptr->sampleSize ); WriteIeeeExtendedHighLow( file_ptr, aiff_ptr->sampleRate ); /* write SSND chunk header */ chunkSize = 8 + aiff_ptr->numChannels * aiff_ptr->numSampleFrames * sampleBytes; Write32BitsHighLow( file_ptr, IFF_ID_SSND ); Write32BitsHighLow( file_ptr, chunkSize ); Write32BitsHighLow( file_ptr, 0 ); /* offset */ Write32BitsHighLow( file_ptr, 0 ); /* block size */ 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.********************************************************************//*open_bit_stream_w(); open the device to write the bit stream into it *//*open_bit_stream_r(); open the device to read the bit stream from it *//*close_bit_stream(); close the device containing the bit stream *//*alloc_buffer(); open and initialize the buffer; *//*desalloc_buffer(); empty and close the buffer *//*back_track_buffer(); goes back N bits in the buffer *//*unsigned int get1bit(); read 1 bit from the bit stream *//*unsigned long getbits(); read N bits from the bit stream *//*unsigned long byte_ali_getbits(); read the next byte aligned N bits from*//* the bit stream *//*unsigned long look_ahead(); grep the next N bits in the bit stream without*//* changing the buffer pointer *//*put1bit(); write 1 bit from the bit stream *//*put1bit(); write 1 bit from the bit stream *//*putbits(); write N bits from the bit stream *//*byte_ali_putbits(); write byte aligned the next N bits into the bit stream*//*unsigned long sstell(); return the current bit stream length (in bits) *//*int end_bs(); return 1 if the end of bit stream reached otherwise 0 *//*int seek_sync(); return 1 if a sync word was found in the bit stream *//* otherwise returns 0 *//* refill the buffer from the input device when the buffer becomes empty */int refill_buffer(bs)Bit_stream_struc *bs; /* bit stream structure */{ register int i=bs->buf_size-2-bs->buf_byte_idx; register unsigned long n=1; 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(bs, minimum)Bit_stream_struc *bs; /* bit stream structure */int minimum; /* 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(bs, bs_filenam, size)Bit_stream_struc *bs; /* bit stream structure */char *bs_filenam; /* name of the bit stream file */int size; /* size of the buffer */{ if ((bs->pt = fopen(bs_filenam, "wb")) == NULL) { printf("Could not create \"%s\".\n", bs_filenam); exit(1); } 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(bs, bs_filenam, size)Bit_stream_struc *bs; /* bit stream structure */char *bs_filenam; /* name of the bit stream file */int size; /* size of the buffer */{ register unsigned long n; 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(1); } 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 */ case 0xd: /* cr */ case 0x1a: /* sub */ break; default: /* detection of an binary character */ flag--; break; } } while (flag & n); if (flag) { printf ("the bit stream file %s is an ASCII file\n", bs_filenam); bs->format = ASCII; } else { bs->format = BINARY; 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(1); } 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(bs)Bit_stream_struc *bs; /* bit stream structure */{ fclose(bs->pt); desalloc_buffer(bs);}/*close the device containing the bit stream after a write process*/void close_bit_stream_w(bs)Bit_stream_struc *bs; /* bit stream structure */{ empty_buffer(bs, bs->buf_byte_idx); fclose(bs->pt); desalloc_buffer(bs);}/*open and initialize the buffer; */void alloc_buffer(bs, size)Bit_stream_struc *bs; /* bit stream structure */int size;{ bs->buf = (unsigned char FAR *) mem_alloc(size*sizeof(unsigned char), "buffer"); bs->buf_size = size;}/*empty and close the buffer */void desalloc_buffer(bs)Bit_stream_struc *bs; /* bit stream structure */{ free(bs->buf);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -