📄 util.c
字号:
#include "util.h"#include <assert.h>#ifdef LAME_STD_PRINT#include <stdarg.h>#endif/************************************************************************* Global Variable Definitions************************************************************************//* 1: MPEG-1, 0: MPEG-2 LSF, 1995-07-11 shn */int bitrate_table[2][15] = { {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160}, {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}};enum byte_order NativeByteOrder = order_unknown;/************************************************************************* Global Function Definitions************************************************************************/FLOAT8 ATHformula(FLOAT8 f){ FLOAT8 ath; f = Max(0.01, f); f = Min(18.0,f); /* from Painter & Spanias, 1997 */ /* minimum: (i=77) 3.3kHz = -5db */ ath = 3.640 * pow(f,-0.8) - 6.500 * exp(-0.6*pow(f-3.3,2.0)) + 0.001 * pow(f,4.0); return ath;}FLOAT8 freq2bark(FLOAT8 freq){ /* input: freq in hz output: barks */ freq = freq * 0.001; return 13.0*atan(.76*freq) + 3.5*atan(freq*freq/(7.5*7.5));}/*********************************************************************** * compute bitsperframe and mean_bits for a layer III frame **********************************************************************/void getframebits(lame_global_flags *gfp,int *bitsPerFrame, int *mean_bits) { int whole_SpF; lame_internal_flags *gfc=gfp->internal_flags;#if 0 FLOAT8 bit_rate,samp; samp = gfp->out_samplerate/1000.0; if (gfc->bitrate_index) bit_rate = bitrate_table[gfp->version][gfc->bitrate_index]; else bit_rate = gfp->brate; /* -f fast-math option causes some strange rounding here, be carefull: */ whole_SpF = floor( (gfp->framesize /samp)*(bit_rate / 8.0) + 1e-9);#else long bit_rate; if (gfc->bitrate_index) bit_rate = bitrate_table[gfp->version][gfc->bitrate_index]; else bit_rate = gfp->brate; whole_SpF=((gfp->version+1)*72000L*bit_rate) / gfp->out_samplerate;#endif *bitsPerFrame = 8 * (whole_SpF + gfc->padding); *mean_bits = (*bitsPerFrame - 8*gfc->sideinfo_len) / gfc->mode_gr;}void display_bitrates(FILE *out_fh){ int index,version; version = 1; fprintf(out_fh,"\n"); fprintf(out_fh,"MPEG1 layer III samplerates(kHz): 32 44.1 48 \n"); fprintf(out_fh,"bitrates(kbs): "); for (index=1;index<15;index++) { fprintf(out_fh,"%i ",bitrate_table[version][index]); } fprintf(out_fh,"\n"); version = 0; fprintf(out_fh,"\n"); fprintf(out_fh,"MPEG2 layer III samplerates(kHz): 16 22.05 24 \n"); fprintf(out_fh,"bitrates(kbs): "); for (index=1;index<15;index++) { fprintf(out_fh,"%i ",bitrate_table[version][index]); } fprintf(out_fh,"\n"); version = 0; fprintf(out_fh,"\n"); fprintf(out_fh,"MPEG2.5 layer III samplerates(kHz): 8 11.025 12 \n"); fprintf(out_fh,"bitrates(kbs): "); for (index=1;index<15;index++) { fprintf(out_fh,"%i ",bitrate_table[version][index]); } fprintf(out_fh,"\n");}#define ABS(A) (((A)>0) ? (A) : -(A))int FindNearestBitrate(int bRate, /* legal rates from 32 to 448 */int version, /* MPEG-1 or MPEG-2 LSF */int samplerate) /* convert bitrate in kbps to index */{ int index = 0; int bitrate = 10000; while( index<15) { if( ABS( bitrate_table[version][index] - bRate) < ABS(bitrate-bRate) ) { bitrate = bitrate_table[version][index]; } ++index; } return bitrate;}/* map samplerate to a valid samplerate * * Robert.Hegemann@gmx.de 2000-07-01 */long validSamplerate(long samplerate){ if (samplerate<= 8000) return 8000; if (samplerate<=11025) return 11025; if (samplerate<=12000) return 12000; if (samplerate<=16000) return 16000; if (samplerate<=22050) return 22050; if (samplerate<=24000) return 24000; if (samplerate<=32000) return 32000; if (samplerate<=44100) return 44100; return 48000;}int BitrateIndex(int bRate, /* legal rates from 32 to 448 */int version, /* MPEG-1 or MPEG-2 LSF */int samplerate) /* convert bitrate in kbps to index */{int index = 0;int found = 0; while(!found && index<15) { if(bitrate_table[version][index] == bRate) found = 1; else ++index; } if(found) return(index); else { ERRORF("Bitrate %dkbs not legal for %iHz output sampling.\n", bRate, samplerate); return(-1); /* Error! */ }}int SmpFrqIndex( /* convert samp frq in Hz to index */long sRate, /* legal rates 16000, 22050, 24000, 32000, 44100, 48000 */int *version){ /* Assign default value */ *version=0; if (sRate == 44100L) { *version = 1; return(0); } else if (sRate == 48000L) { *version = 1; return(1); } else if (sRate == 32000L) { *version = 1; return(2); } else if (sRate == 24000L) { *version = 0; return(1); } else if (sRate == 22050L) { *version = 0; return(0); } else if (sRate == 16000L) { *version = 0; return(2); } else if (sRate == 12000L) { *version = 0; return(1); } else if (sRate == 11025L) { *version = 0; return(0); } else if (sRate == 8000L) { *version = 0; return(2); } else { ERRORF("SmpFrqIndex: %ldHz is not a legal sample rate\n", sRate); return(-1); /* Error! */ }}/******************************************************************************* Routines to determine byte order and swap bytes******************************************************************************/enum byte_order DetermineByteOrder(void){ 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]; }}/******************************************************************************* 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.********************************************************************//*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 look_ahead(); grep the next N bits in the bit stream without*//* changing the buffer pointer *//*putbits(); write N bits from the bit stream *//*int seek_sync(); return 1 if a sync word was found in the bit stream *//* otherwise returns 0 */void empty_buffer(Bit_stream_struc *bs){ /* brain damaged ISO buffer type - counts down */ int minimum=1+bs->buf_byte_idx; /* end of the buffer to empty */ if (bs->buf_size-minimum <= 0) return; bs->buf_byte_idx = bs->buf_size -1; bs->buf_bit_idx = 8; bs->buf[bs->buf_byte_idx] = 0; /* what does this do? */}int copy_buffer(char *buffer,int size,Bit_stream_struc *bs){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -