util.h

Go to the documentation of this file.
00001 /*
00002  *      lame utility library include file
00003  *
00004  *      Copyright (c) 1999 Albert L Faber
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #ifndef LAME_UTIL_H
00023 #define LAME_UTIL_H
00024 
00025 #include "l3side.h"
00026 #include "id3tag.h"
00027 
00028 #ifdef __cplusplus
00029 extern  "C" {
00030 #endif
00031 
00032 /***********************************************************************
00033 *
00034 *  Global Definitions
00035 *
00036 ***********************************************************************/
00037 
00038 #ifndef FALSE
00039 #define         FALSE                   0
00040 #endif
00041 
00042 #ifndef TRUE
00043 #define         TRUE                    (!FALSE)
00044 #endif
00045 
00046 #ifdef UINT_MAX
00047 # define         MAX_U_32_NUM            UINT_MAX
00048 #else
00049 # define         MAX_U_32_NUM            0xFFFFFFFF
00050 #endif
00051 
00052 #ifndef PI
00053 # ifdef M_PI
00054 #  define       PI                      M_PI
00055 # else
00056 #  define       PI                      3.14159265358979323846
00057 # endif
00058 #endif
00059 
00060 
00061 #ifdef M_LN2
00062 # define        LOG2                    M_LN2
00063 #else
00064 # define        LOG2                    0.69314718055994530942
00065 #endif
00066 
00067 #ifdef M_LN10
00068 # define        LOG10                   M_LN10
00069 #else
00070 # define        LOG10                   2.30258509299404568402
00071 #endif
00072 
00073 
00074 #ifdef M_SQRT2
00075 # define        SQRT2                   M_SQRT2
00076 #else
00077 # define        SQRT2                   1.41421356237309504880
00078 #endif
00079 
00080 
00081 #define         HAN_SIZE                512
00082 #define         CRC16_POLYNOMIAL        0x8005
00083     
00084 #define MAX_BITS_PER_CHANNEL 4095
00085 #define MAX_BITS_PER_GRANULE 7680
00086 
00087 /* "bit_stream.h" Definitions */
00088 #define         BUFFER_SIZE     LAME_MAXMP3BUFFER
00089 
00090 #define         Min(A, B)       ((A) < (B) ? (A) : (B))
00091 #define         Max(A, B)       ((A) > (B) ? (A) : (B))
00092 
00093 /* log/log10 approximations */
00094 #ifdef USE_FAST_LOG
00095 #define         FAST_LOG10(x)       (fast_log2(x)*(LOG2/LOG10))
00096 #define         FAST_LOG(x)         (fast_log2(x)*LOG2)
00097 #define         FAST_LOG10_X(x,y)   (fast_log2(x)*(LOG2/LOG10*(y)))
00098 #define         FAST_LOG_X(x,y)     (fast_log2(x)*(LOG2*(y)))
00099 #else
00100 #define         FAST_LOG10(x)       log10(x)
00101 #define         FAST_LOG(x)         log(x)
00102 #define         FAST_LOG10_X(x,y)   (log10(x)*(y))
00103 #define         FAST_LOG_X(x,y)     (log(x)*(y))
00104 #endif
00105 
00106 
00107     struct replaygain_data;
00108 #ifndef replaygain_data_defined
00109 #define replaygain_data_defined
00110     typedef struct replaygain_data replaygain_t;
00111 #endif
00112     struct plotting_data;
00113 #ifndef plotting_data_defined
00114 #define plotting_data_defined
00115     typedef struct plotting_data plotting_data;
00116 #endif
00117 
00118 /***********************************************************************
00119 *
00120 *  Global Type Definitions
00121 *
00122 ***********************************************************************/
00123 
00124     typedef struct {
00125         void   *aligned;     /* pointer to ie. 128 bit aligned memory */
00126         void   *pointer;     /* to use with malloc/free */
00127     } aligned_pointer_t;
00128 
00129     void    malloc_aligned(aligned_pointer_t * ptr, unsigned int size, unsigned int bytes);
00130     void    free_aligned(aligned_pointer_t * ptr);
00131 
00132 
00133 
00134     typedef void (*iteration_loop_t) (lame_global_flags const * gfp,
00135                                       FLOAT const pe[2][2], FLOAT const ms_ratio[2],
00136                                       III_psy_ratio const ratio[2][2]);
00137 
00138 
00139     /* "bit_stream.h" Type Definitions */
00140 
00141     typedef struct bit_stream_struc {
00142         unsigned char *buf;  /* bit stream buffer */
00143         int     buf_size;    /* size of buffer (in number of bytes) */
00144         int     totbit;      /* bit counter of bit stream */
00145         int     buf_byte_idx; /* pointer to top byte in buffer */
00146         int     buf_bit_idx; /* pointer to top bit of top byte in buffer */
00147 
00148         /* format of file in rd mode (BINARY/ASCII) */
00149     } Bit_stream_struc;
00150 
00151 
00152 
00153     /* variables used for --nspsytune */
00154     typedef struct {
00155         /* variables for nspsytune */
00156         FLOAT   last_en_subshort[4][9];
00157         int     last_attacks[4];
00158         FLOAT   pefirbuf[19];
00159         FLOAT   longfact[SBMAX_l];
00160         FLOAT   shortfact[SBMAX_s];
00161 
00162         /* short block tuning */
00163         FLOAT   attackthre;
00164         FLOAT   attackthre_s;
00165     } nsPsy_t;
00166 
00167 
00168     typedef struct {
00169         int     sum;         /* what we have seen so far */
00170         int     seen;        /* how many frames we have seen in this chunk */
00171         int     want;        /* how many frames we want to collect into one chunk */
00172         int     pos;         /* actual position in our bag */
00173         int     size;        /* size of our bag */
00174         int    *bag;         /* pointer to our bag */
00175         unsigned int nVbrNumFrames;
00176         unsigned long nBytesWritten;
00177         /* VBR tag data */
00178         unsigned int TotalFrameSize;
00179     } VBR_seek_info_t;
00180 
00181 
00186     typedef struct {
00187         int     use_adjust;  /* method for the auto adjustment  */
00188         FLOAT   aa_sensitivity_p; /* factor for tuning the (sample power)
00189                                      point below which adaptive threshold
00190                                      of hearing adjustment occurs */
00191         FLOAT   adjust;      /* lowering based on peak volume, 1 = no lowering */
00192         FLOAT   adjust_limit; /* limit for dynamic ATH adjust */
00193         FLOAT   decay;       /* determined to lower x dB each second */
00194         FLOAT   floor;       /* lowest ATH value */
00195         FLOAT   l[SBMAX_l];  /* ATH for sfbs in long blocks */
00196         FLOAT   s[SBMAX_s];  /* ATH for sfbs in short blocks */
00197         FLOAT   psfb21[PSFB21]; /* ATH for partitionned sfb21 in long blocks */
00198         FLOAT   psfb12[PSFB12]; /* ATH for partitionned sfb12 in short blocks */
00199         FLOAT   cb_l[CBANDS]; /* ATH for long block convolution bands */
00200         FLOAT   cb_s[CBANDS]; /* ATH for short block convolution bands */
00201         FLOAT   eql_w[BLKSIZE / 2]; /* equal loudness weights (based on ATH) */
00202     } ATH_t;
00203 
00207     typedef struct {
00208         FLOAT   mask_adjust; /* the dbQ stuff */
00209         FLOAT   mask_adjust_short; /* the dbQ stuff */
00210         FLOAT   cwlimit;
00211         /* at transition from one scalefactor band to next */
00212         FLOAT   bo_l_weight[SBMAX_l]; /* band weight long scalefactor bands */
00213         FLOAT   bo_s_weight[SBMAX_s]; /* band weight short scalefactor bands */
00214     } PSY_t;
00215 
00216 
00217 #define MAX_CHANNELS  2
00218 
00219 
00220 
00221     struct lame_internal_flags {
00222 
00223   /********************************************************************
00224    * internal variables NOT set by calling program, and should not be *
00225    * modified by the calling program                                  *
00226    ********************************************************************/
00227 
00228         /*
00229          * Some remarks to the Class_ID field:
00230          * The Class ID is an Identifier for a pointer to this struct.
00231          * It is very unlikely that a pointer to lame_global_flags has the same 32 bits
00232          * in it's structure (large and other special properties, for instance prime).
00233          *
00234          * To test that the structure is right and initialized, use:
00235          *     if ( gfc -> Class_ID == LAME_ID ) ...
00236          * Other remark:
00237          *     If you set a flag to 0 for uninit data and 1 for init data, the right test
00238          *     should be "if (flag == 1)" and NOT "if (flag)". Unintended modification
00239          *     of this element will be otherwise misinterpreted as an init.
00240          */
00241 #  define  LAME_ID   0xFFF88E3B
00242         unsigned long Class_ID;
00243 
00244         int     lame_encode_frame_init;
00245         int     iteration_init_init;
00246         int     fill_buffer_resample_init;
00247 
00248 #ifndef  MFSIZE
00249 # define MFSIZE  ( 3*1152 + ENCDELAY - MDCTDELAY )
00250 #endif
00251         sample_t mfbuf[2][MFSIZE];
00252 
00253 
00254         struct {
00255             void    (*msgf) (const char *format, va_list ap);
00256             void    (*debugf) (const char *format, va_list ap);
00257             void    (*errorf) (const char *format, va_list ap);
00258         } report;
00259 
00260         int     mode_gr;     /* granules per frame */
00261         int     channels_in; /* number of channels in the input data stream (PCM or decoded PCM) */
00262         int     channels_out; /* number of channels in the output data stream (not used for decoding) */
00263         FLOAT   resample_ratio; /* input_samp_rate/output_samp_rate */
00264 
00265         int     mf_samples_to_encode;
00266         int     mf_size;
00267         int     VBR_min_bitrate; /* min bitrate index */
00268         int     VBR_max_bitrate; /* max bitrate index */
00269         int     bitrate_index;
00270         int     samplerate_index;
00271         int     mode_ext;
00272 
00273 
00274         /* lowpass and highpass filter control */
00275         FLOAT   lowpass1, lowpass2; /* normalized frequency bounds of passband */
00276         FLOAT   highpass1, highpass2; /* normalized frequency bounds of passband */
00277 
00278         int     filter_type; /* 0=polyphase filter, 1= FIR filter 2=MDCT filter(bad) */
00279         int     quantization; /* 0 = ISO formual,  1=best amplitude */
00280         int     noise_shaping; /* 0 = none
00281                                   1 = ISO AAC model
00282                                   2 = allow scalefac_select=1
00283                                 */
00284 
00285         int     noise_shaping_amp; /*  0 = ISO model: amplify all distorted bands
00286                                       1 = amplify within 50% of max (on db scale)
00287                                       2 = amplify only most distorted band
00288                                       3 = method 1 and refine with method 2
00289                                     */
00290         int     substep_shaping; /* 0 = no substep
00291                                     1 = use substep shaping at last step(VBR only)
00292                                     (not implemented yet)
00293                                     2 = use substep inside loop
00294                                     3 = use substep inside loop and last step
00295                                   */
00296 
00297         int     psymodel;    /* 1 = gpsycho. 0 = none */
00298         int     noise_shaping_stop; /* 0 = stop at over=0, all scalefacs amplified or
00299                                        a scalefac has reached max value
00300                                        1 = stop when all scalefacs amplified or
00301                                        a scalefac has reached max value
00302                                        2 = stop when all scalefacs amplified
00303                                      */
00304 
00305         int     subblock_gain; /*  0 = no, 1 = yes */
00306         int     use_best_huffman; /* 0 = no.  1=outside loop  2=inside loop(slow) */
00307 
00308         int     full_outer_loop; /* 0 = stop early after 0 distortion found. 1 = full search */
00309 
00310 
00311         /* variables used by lame.c */
00312         Bit_stream_struc bs;
00313         III_side_info_t l3_side;
00314         FLOAT   ms_ratio[2];
00315 
00316         /* used for padding */
00317         int     padding;     /* padding for the current frame? */
00318         int     frac_SpF;
00319         int     slot_lag;
00320 
00321 
00322         /* optional ID3 tags, used in id3tag.c  */
00323         struct id3tag_spec tag_spec;
00324         uint16_t nMusicCRC;
00325 
00326 
00327         /* variables used by quantize.c */
00328         int     OldValue[2];
00329         int     CurrentStep[2];
00330 
00331         FLOAT   masking_lower;
00332         char    bv_scf[576];
00333         int     pseudohalf[SFBMAX];
00334 
00335         int     sfb21_extra; /* will be set in lame_init_params */
00336 
00337         /* variables used by util.c */
00338         /* BPC = maximum number of filter convolution windows to precompute */
00339 #define BPC 320
00340         sample_t *inbuf_old[2];
00341         sample_t *blackfilt[2 * BPC + 1];
00342         FLOAT   itime[2];
00343         int     sideinfo_len;
00344 
00345         /* variables for newmdct.c */
00346         FLOAT   sb_sample[2][2][18][SBLIMIT];
00347         FLOAT   amp_filter[32];
00348 
00349         /* variables for bitstream.c */
00350         /* mpeg1: buffer=511 bytes  smallest frame: 96-38(sideinfo)=58
00351          * max number of frames in reservoir:  8
00352          * mpeg2: buffer=255 bytes.  smallest frame: 24-23bytes=1
00353          * with VBR, if you are encoding all silence, it is possible to
00354          * have 8kbs/24khz frames with 1byte of data each, which means we need
00355          * to buffer up to 255 headers! */
00356         /* also, max_header_buf has to be a power of two */
00357 #define MAX_HEADER_BUF 256
00358 #define MAX_HEADER_LEN 40    /* max size of header is 38 */
00359         struct {
00360             int     write_timing;
00361             int     ptr;
00362             char    buf[MAX_HEADER_LEN];
00363         } header[MAX_HEADER_BUF];
00364 
00365         int     h_ptr;
00366         int     w_ptr;
00367         int     ancillary_flag;
00368 
00369         /* variables for reservoir.c */
00370         int     ResvSize;    /* in bits */
00371         int     ResvMax;     /* in bits */
00372 
00373         scalefac_struct scalefac_band;
00374 
00375         /* DATA FROM PSYMODEL.C */
00376 /* The static variables "r", "phi_sav", "new", "old" and "oldest" have    */
00377 /* to be remembered for the unpredictability measure.  For "r" and        */
00378 /* "phi_sav", the first index from the left is the channel select and     */
00379 /* the second index is the "age" of the data.                             */
00380         FLOAT   minval_l[CBANDS];
00381         FLOAT   minval_s[CBANDS];
00382         FLOAT   nb_1[4][CBANDS], nb_2[4][CBANDS];
00383         FLOAT   nb_s1[4][CBANDS], nb_s2[4][CBANDS];
00384         FLOAT  *s3_ss;
00385         FLOAT  *s3_ll;
00386         FLOAT   decay;
00387 
00388         III_psy_xmin thm[4];
00389         III_psy_xmin en[4];
00390 
00391         /* unpredictability calculation
00392          */
00393         int     cw_upper_index;
00394         FLOAT   ax_sav[4][2][HBLKSIZE];
00395         FLOAT   bx_sav[4][2][HBLKSIZE];
00396         FLOAT   rx_sav[4][2][HBLKSIZE];
00397         FLOAT   cw[HBLKSIZE];
00398 
00399 
00400         /* fft and energy calculation    */
00401         FLOAT   tot_ener[4];
00402 
00403         /* loudness calculation (for adaptive threshold of hearing) */
00404         FLOAT   loudness_sq[2][2]; /* loudness^2 approx. per granule and channel */
00405         FLOAT   loudness_sq_save[2]; /* account for granule delay of L3psycho_anal */
00406 
00407 
00408         /* Scale Factor Bands    */
00409         FLOAT   mld_l[SBMAX_l], mld_s[SBMAX_s];
00410         int     bm_l[SBMAX_l], bo_l[SBMAX_l];
00411         int     bm_s[SBMAX_s], bo_s[SBMAX_s];
00412         int     npart_l, npart_s;
00413 
00414         int     s3ind[CBANDS][2];
00415         int     s3ind_s[CBANDS][2];
00416 
00417         int     numlines_s[CBANDS];
00418         int     numlines_l[CBANDS];
00419         FLOAT   rnumlines_l[CBANDS];
00420 
00421         /* ratios  */
00422         FLOAT   pe[4];
00423         FLOAT   ms_ratio_s_old, ms_ratio_l_old;
00424         FLOAT   ms_ener_ratio_old;
00425 
00426         /* block type */
00427         int     blocktype_old[2];
00428 
00429         /* CPU features */
00430         struct {
00431             unsigned int MMX:1; /* Pentium MMX, Pentium II...IV, K6, K6-2,
00432                                    K6-III, Athlon */
00433             unsigned int AMD_3DNow:1; /* K6-2, K6-III, Athlon      */
00434             unsigned int SSE:1; /* Pentium III, Pentium 4    */
00435             unsigned int SSE2:1; /* Pentium 4, K8             */
00436         } CPU_features;
00437 
00438         /* functions to replace with CPU feature optimized versions in takehiro.c */
00439         int     (*choose_table) (const int *ix, const int *const end, int *const s);
00440         void    (*fft_fht) (FLOAT *, int);
00441         void    (*quantize_lines_xrpow) (int l, FLOAT istep, const FLOAT * xp, int *pi);
00442         void    (*init_xrpow_core) (gr_info * const cod_info, FLOAT xrpow[576], int upper,
00443                                     FLOAT * sum);
00444 
00445 
00446 
00447         nsPsy_t nsPsy;       /* variables used for --nspsytune */
00448 
00449         VBR_seek_info_t VBR_seek_table; /* used for Xing VBR header */
00450 
00451         ATH_t  *ATH;         /* all ATH related stuff */
00452         PSY_t  *PSY;
00453 
00454         int     nogap_total;
00455         int     nogap_current;
00456 
00457 
00458         /* ReplayGain */
00459         unsigned int decode_on_the_fly:1;
00460         unsigned int findReplayGain:1;
00461         unsigned int findPeakSample:1;
00462         sample_t PeakSample;
00463         int     RadioGain;
00464         int     AudiophileGain;
00465         replaygain_t *rgdata;
00466 
00467         int     noclipGainChange; /* gain change required for preventing clipping */
00468         FLOAT   noclipScale; /* user-specified scale factor required for preventing clipping */
00469 
00470 
00471         /* simple statistics */
00472         int     bitrate_stereoMode_Hist[16][4 + 1];
00473         int     bitrate_blockType_Hist[16][4 + 1 + 1]; /*norm/start/short/stop/mixed(short)/sum */
00474 
00475         /* used by the frame analyzer */
00476         plotting_data *pinfo;
00477 
00478         int     in_buffer_nsamples;
00479         sample_t *in_buffer_0;
00480         sample_t *in_buffer_1;
00481 
00482         iteration_loop_t iteration_loop;
00483     };
00484 
00485 #ifndef lame_internal_flags_defined
00486 #define lame_internal_flags_defined
00487     typedef struct lame_internal_flags lame_internal_flags;
00488 #endif
00489 
00490 
00491 /***********************************************************************
00492 *
00493 *  Global Function Prototype Declarations
00494 *
00495 ***********************************************************************/
00496     void    freegfc(lame_internal_flags * const gfc);
00497     void    free_id3tag(lame_internal_flags * const gfc);
00498     extern int BitrateIndex(int, int, int);
00499     extern int FindNearestBitrate(int, int, int);
00500     extern int map2MP3Frequency(int freq);
00501     extern int SmpFrqIndex(int, int *const);
00502     extern int nearestBitrateFullIndex(const int brate);
00503     extern FLOAT ATHformula(FLOAT freq, lame_global_flags const *gfp);
00504     extern FLOAT freq2bark(FLOAT freq);
00505     extern FLOAT freq2cbw(FLOAT freq);
00506     void    disable_FPE(void);
00507 
00508 /* log/log10 approximations */
00509     extern void init_log_table(void);
00510     extern ieee754_float32_t fast_log2(ieee754_float32_t x);
00511 
00512 
00513     void    fill_buffer(lame_global_flags const *gfp,
00514                         sample_t * mfbuf[2],
00515                         sample_t const *in_buffer[2], int nsamples, int *n_in, int *n_out);
00516 
00517     int     fill_buffer_resample(lame_global_flags const *gfp,
00518                                  sample_t * outbuf,
00519                                  int desired_len,
00520                                  sample_t const *inbuf, int len, int *num_used, int channels);
00521 
00522 /* same as lame_decode1 (look in lame.h), but returns
00523    unclipped raw floating-point samples. It is declared
00524    here, not in lame.h, because it returns LAME's
00525    internal type sample_t. No more than 1152 samples
00526    per channel are allowed. */
00527     int     lame_decode1_unclipped(unsigned char *mp3buf,
00528                                    int len, sample_t pcm_l[], sample_t pcm_r[]);
00529 
00530 
00531     extern int has_MMX(void);
00532     extern int has_3DNow(void);
00533     extern int has_SSE(void);
00534     extern int has_SSE2(void);
00535 
00536 
00537 
00538 /***********************************************************************
00539 *
00540 *  Macros about Message Printing and Exit
00541 *
00542 ***********************************************************************/
00543     extern void lame_errorf(const lame_internal_flags * gfc, const char *, ...);
00544     extern void lame_debugf(const lame_internal_flags * gfc, const char *, ...);
00545     extern void lame_msgf(const lame_internal_flags * gfc, const char *, ...);
00546 #define DEBUGF  lame_debugf
00547 #define ERRORF  lame_errorf
00548 #define MSGF    lame_msgf
00549 
00550 
00551 #ifdef __cplusplus
00552 }
00553 #endif
00554 #endif                       /* LAME_UTIL_H */

Generated on Sun Dec 2 11:34:20 2007 for LAME by  doxygen 1.5.2