00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
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
00121
00122
00123
00124 typedef struct {
00125 void *aligned;
00126 void *pointer;
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
00140
00141 typedef struct bit_stream_struc {
00142 unsigned char *buf;
00143 int buf_size;
00144 int totbit;
00145 int buf_byte_idx;
00146 int buf_bit_idx;
00147
00148
00149 } Bit_stream_struc;
00150
00151
00152
00153
00154 typedef struct {
00155
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
00163 FLOAT attackthre;
00164 FLOAT attackthre_s;
00165 } nsPsy_t;
00166
00167
00168 typedef struct {
00169 int sum;
00170 int seen;
00171 int want;
00172 int pos;
00173 int size;
00174 int *bag;
00175 unsigned int nVbrNumFrames;
00176 unsigned long nBytesWritten;
00177
00178 unsigned int TotalFrameSize;
00179 } VBR_seek_info_t;
00180
00181
00186 typedef struct {
00187 int use_adjust;
00188 FLOAT aa_sensitivity_p;
00189
00190
00191 FLOAT adjust;
00192 FLOAT adjust_limit;
00193 FLOAT decay;
00194 FLOAT floor;
00195 FLOAT l[SBMAX_l];
00196 FLOAT s[SBMAX_s];
00197 FLOAT psfb21[PSFB21];
00198 FLOAT psfb12[PSFB12];
00199 FLOAT cb_l[CBANDS];
00200 FLOAT cb_s[CBANDS];
00201 FLOAT eql_w[BLKSIZE / 2];
00202 } ATH_t;
00203
00207 typedef struct {
00208 FLOAT mask_adjust;
00209 FLOAT mask_adjust_short;
00210 FLOAT cwlimit;
00211
00212 FLOAT bo_l_weight[SBMAX_l];
00213 FLOAT bo_s_weight[SBMAX_s];
00214 } PSY_t;
00215
00216
00217 #define MAX_CHANNELS 2
00218
00219
00220
00221 struct lame_internal_flags {
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
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;
00261 int channels_in;
00262 int channels_out;
00263 FLOAT resample_ratio;
00264
00265 int mf_samples_to_encode;
00266 int mf_size;
00267 int VBR_min_bitrate;
00268 int VBR_max_bitrate;
00269 int bitrate_index;
00270 int samplerate_index;
00271 int mode_ext;
00272
00273
00274
00275 FLOAT lowpass1, lowpass2;
00276 FLOAT highpass1, highpass2;
00277
00278 int filter_type;
00279 int quantization;
00280 int noise_shaping;
00281
00282
00283
00284
00285 int noise_shaping_amp;
00286
00287
00288
00289
00290 int substep_shaping;
00291
00292
00293
00294
00295
00296
00297 int psymodel;
00298 int noise_shaping_stop;
00299
00300
00301
00302
00303
00304
00305 int subblock_gain;
00306 int use_best_huffman;
00307
00308 int full_outer_loop;
00309
00310
00311
00312 Bit_stream_struc bs;
00313 III_side_info_t l3_side;
00314 FLOAT ms_ratio[2];
00315
00316
00317 int padding;
00318 int frac_SpF;
00319 int slot_lag;
00320
00321
00322
00323 struct id3tag_spec tag_spec;
00324 uint16_t nMusicCRC;
00325
00326
00327
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;
00336
00337
00338
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
00346 FLOAT sb_sample[2][2][18][SBLIMIT];
00347 FLOAT amp_filter[32];
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 #define MAX_HEADER_BUF 256
00358 #define MAX_HEADER_LEN 40
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
00370 int ResvSize;
00371 int ResvMax;
00372
00373 scalefac_struct scalefac_band;
00374
00375
00376
00377
00378
00379
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
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
00401 FLOAT tot_ener[4];
00402
00403
00404 FLOAT loudness_sq[2][2];
00405 FLOAT loudness_sq_save[2];
00406
00407
00408
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
00422 FLOAT pe[4];
00423 FLOAT ms_ratio_s_old, ms_ratio_l_old;
00424 FLOAT ms_ener_ratio_old;
00425
00426
00427 int blocktype_old[2];
00428
00429
00430 struct {
00431 unsigned int MMX:1;
00432
00433 unsigned int AMD_3DNow:1;
00434 unsigned int SSE:1;
00435 unsigned int SSE2:1;
00436 } CPU_features;
00437
00438
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;
00448
00449 VBR_seek_info_t VBR_seek_table;
00450
00451 ATH_t *ATH;
00452 PSY_t *PSY;
00453
00454 int nogap_total;
00455 int nogap_current;
00456
00457
00458
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;
00468 FLOAT noclipScale;
00469
00470
00471
00472 int bitrate_stereoMode_Hist[16][4 + 1];
00473 int bitrate_blockType_Hist[16][4 + 1 + 1];
00474
00475
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
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
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
00523
00524
00525
00526
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
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