1 /* 2 * lame utility library include file 3 * 4 * Copyright (c) 1999 Albert L Faber 5 * Copyright (c) 2008 Robert Hegemann 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Library General Public 9 * License as published by the Free Software Foundation; either 10 * version 2 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Library General Public License for more details. 16 * 17 * You should have received a copy of the GNU Library General Public 18 * License along with this library; if not, write to the 19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 * Boston, MA 02111-1307, USA. 21 */ 22 23 #ifndef LAME_UTIL_H 24 #define LAME_UTIL_H 25 26 #include "l3side.h" 27 #include "id3tag.h" 28 #include "lame_global_flags.h" 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 /*********************************************************************** 35 * 36 * Global Definitions 37 * 38 ***********************************************************************/ 39 40 #ifndef FALSE 41 #define FALSE 0 42 #endif 43 44 #ifndef TRUE 45 #define TRUE (!FALSE) 46 #endif 47 48 #ifdef UINT_MAX 49 # define MAX_U_32_NUM UINT_MAX 50 #else 51 # define MAX_U_32_NUM 0xFFFFFFFF 52 #endif 53 54 #ifndef PI 55 # ifdef M_PI 56 # define PI M_PI 57 # else 58 # define PI 3.14159265358979323846 59 # endif 60 #endif 61 62 63 #ifdef M_LN2 64 # define LOG2 M_LN2 65 #else 66 # define LOG2 0.69314718055994530942 67 #endif 68 69 #ifdef M_LN10 70 # define LOG10 M_LN10 71 #else 72 # define LOG10 2.30258509299404568402 73 #endif 74 75 76 #ifdef M_SQRT2 77 # define SQRT2 M_SQRT2 78 #else 79 # define SQRT2 1.41421356237309504880 80 #endif 81 82 #ifndef HAVE_IEEE754_FLOAT32_T 83 typedef float ieee754_float32_t; 84 #endif 85 86 #define CRC16_POLYNOMIAL 0x8005 87 88 #define MAX_BITS_PER_CHANNEL 4095 89 #define MAX_BITS_PER_GRANULE 7680 90 91 /* "bit_stream.h" Definitions */ 92 #define BUFFER_SIZE LAME_MAXMP3BUFFER 93 94 #define Min(A, B) ((A) < (B) ? (A) : (B)) 95 #define Max(A, B) ((A) > (B) ? (A) : (B)) 96 97 /* log/log10 approximations */ 98 #ifdef USE_FAST_LOG 99 #define FAST_LOG10(x) (fast_log2(x)*(LOG2/LOG10)) 100 #define FAST_LOG(x) (fast_log2(x)*LOG2) 101 #define FAST_LOG10_X(x,y) (fast_log2(x)*(LOG2/LOG10*(y))) 102 #define FAST_LOG_X(x,y) (fast_log2(x)*(LOG2*(y))) 103 #else 104 #define FAST_LOG10(x) log10(x) 105 #define FAST_LOG(x) log(x) 106 #define FAST_LOG10_X(x,y) (log10(x)*(y)) 107 #define FAST_LOG_X(x,y) (log(x)*(y)) 108 #endif 109 110 111 struct replaygain_data; 112 #ifndef replaygain_data_defined 113 #define replaygain_data_defined 114 typedef struct replaygain_data replaygain_t; 115 #endif 116 struct plotting_data; 117 #ifndef plotting_data_defined 118 #define plotting_data_defined 119 typedef struct plotting_data plotting_data; 120 #endif 121 122 /*********************************************************************** 123 * 124 * Global Type Definitions 125 * 126 ***********************************************************************/ 127 128 typedef struct { 129 void *aligned; /* pointer to ie. 128 bit aligned memory */ 130 void *pointer; /* to use with malloc/free */ 131 } aligned_pointer_t; 132 133 void calloc_aligned(aligned_pointer_t * ptr, unsigned int size, unsigned int bytes); 134 void free_aligned(aligned_pointer_t * ptr); 135 136 137 /* "bit_stream.h" Type Definitions */ 138 139 typedef struct bit_stream_struc { 140 unsigned char *buf; /* bit stream buffer */ 141 int buf_size; /* size of buffer (in number of bytes) */ 142 int totbit; /* bit counter of bit stream */ 143 int buf_byte_idx; /* pointer to top byte in buffer */ 144 int buf_bit_idx; /* pointer to top bit of top byte in buffer */ 145 146 /* format of file in rd mode (BINARY/ASCII) */ 147 } Bit_stream_struc; 148 149 150 151 typedef struct { 152 int sum; /* what we have seen so far */ 153 int seen; /* how many frames we have seen in this chunk */ 154 int want; /* how many frames we want to collect into one chunk */ 155 int pos; /* actual position in our bag */ 156 int size; /* size of our bag */ 157 int *bag; /* pointer to our bag */ 158 unsigned int nVbrNumFrames; 159 unsigned long nBytesWritten; 160 /* VBR tag data */ 161 unsigned int TotalFrameSize; 162 } VBR_seek_info_t; 163 164 165 /** 166 * ATH related stuff, if something new ATH related has to be added, 167 * please plugg it here into the ATH_t struct 168 */ 169 typedef struct { 170 int use_adjust; /* method for the auto adjustment */ 171 FLOAT aa_sensitivity_p; /* factor for tuning the (sample power) 172 point below which adaptive threshold 173 of hearing adjustment occurs */ 174 FLOAT adjust_factor; /* lowering based on peak volume, 1 = no lowering */ 175 FLOAT adjust_limit; /* limit for dynamic ATH adjust */ 176 FLOAT decay; /* determined to lower x dB each second */ 177 FLOAT floor; /* lowest ATH value */ 178 FLOAT l[SBMAX_l]; /* ATH for sfbs in long blocks */ 179 FLOAT s[SBMAX_s]; /* ATH for sfbs in short blocks */ 180 FLOAT psfb21[PSFB21]; /* ATH for partitionned sfb21 in long blocks */ 181 FLOAT psfb12[PSFB12]; /* ATH for partitionned sfb12 in short blocks */ 182 FLOAT cb_l[CBANDS]; /* ATH for long block convolution bands */ 183 FLOAT cb_s[CBANDS]; /* ATH for short block convolution bands */ 184 FLOAT eql_w[BLKSIZE / 2]; /* equal loudness weights (based on ATH) */ 185 } ATH_t; 186 187 /** 188 * PSY Model related stuff 189 */ 190 191 typedef struct { 192 FLOAT masking_lower[CBANDS]; 193 FLOAT minval[CBANDS]; 194 FLOAT rnumlines[CBANDS]; 195 FLOAT mld_cb[CBANDS]; 196 FLOAT mld[Max(SBMAX_l,SBMAX_s)]; 197 FLOAT bo_weight[Max(SBMAX_l,SBMAX_s)]; /* band weight long scalefactor bands, at transition */ 198 FLOAT attack_threshold; /* short block tuning */ 199 int s3ind[CBANDS][2]; 200 int numlines[CBANDS]; 201 int bm[Max(SBMAX_l,SBMAX_s)]; 202 int bo[Max(SBMAX_l,SBMAX_s)]; 203 int npart; 204 int n_sb; /* SBMAX_l or SBMAX_s */ 205 FLOAT *s3; 206 } PsyConst_CB2SB_t; 207 208 209 /** 210 * global data constants 211 */ 212 typedef struct { 213 FLOAT window[BLKSIZE], window_s[BLKSIZE_s / 2]; 214 PsyConst_CB2SB_t l; 215 PsyConst_CB2SB_t s; 216 PsyConst_CB2SB_t l_to_s; 217 FLOAT attack_threshold[4]; 218 FLOAT decay; 219 int force_short_block_calc; 220 } PsyConst_t; 221 222 223 typedef struct { 224 225 FLOAT nb_l1[4][CBANDS], nb_l2[4][CBANDS]; 226 FLOAT nb_s1[4][CBANDS], nb_s2[4][CBANDS]; 227 228 III_psy_xmin thm[4]; 229 III_psy_xmin en[4]; 230 231 /* loudness calculation (for adaptive threshold of hearing) */ 232 FLOAT loudness_sq_save[2]; /* account for granule delay of L3psycho_anal */ 233 234 FLOAT tot_ener[4]; 235 236 FLOAT last_en_subshort[4][9]; 237 int last_attacks[4]; 238 239 int blocktype_old[2]; 240 } PsyStateVar_t; 241 242 243 typedef struct { 244 /* loudness calculation (for adaptive threshold of hearing) */ 245 FLOAT loudness_sq[2][2]; /* loudness^2 approx. per granule and channel */ 246 } PsyResult_t; 247 248 249 /* variables used by encoder.c */ 250 typedef struct { 251 /* variables for newmdct.c */ 252 FLOAT sb_sample[2][2][18][SBLIMIT]; 253 FLOAT amp_filter[32]; 254 255 /* variables used by util.c */ 256 /* BPC = maximum number of filter convolution windows to precompute */ 257 #define BPC 320 258 double itime[2]; /* float precision seems to be not enough */ 259 sample_t *inbuf_old[2]; 260 sample_t *blackfilt[2 * BPC + 1]; 261 262 FLOAT pefirbuf[19]; 263 264 /* used for padding */ 265 int frac_SpF; 266 int slot_lag; 267 268 /* variables for bitstream.c */ 269 /* mpeg1: buffer=511 bytes smallest frame: 96-38(sideinfo)=58 270 * max number of frames in reservoir: 8 271 * mpeg2: buffer=255 bytes. smallest frame: 24-23bytes=1 272 * with VBR, if you are encoding all silence, it is possible to 273 * have 8kbs/24khz frames with 1byte of data each, which means we need 274 * to buffer up to 255 headers! */ 275 /* also, max_header_buf has to be a power of two */ 276 #define MAX_HEADER_BUF 256 277 #define MAX_HEADER_LEN 40 /* max size of header is 38 */ 278 struct { 279 int write_timing; 280 int ptr; 281 char buf[MAX_HEADER_LEN]; 282 } header[MAX_HEADER_BUF]; 283 284 int h_ptr; 285 int w_ptr; 286 int ancillary_flag; 287 288 /* variables for reservoir.c */ 289 int ResvSize; /* in bits */ 290 int ResvMax; /* in bits */ 291 292 int in_buffer_nsamples; 293 sample_t *in_buffer_0; 294 sample_t *in_buffer_1; 295 296 #ifndef MFSIZE 297 # define MFSIZE ( 3*1152 + ENCDELAY - MDCTDELAY ) 298 #endif 299 sample_t mfbuf[2][MFSIZE]; 300 301 int mf_samples_to_encode; 302 int mf_size; 303 304 } EncStateVar_t; 305 306 307 typedef struct { 308 /* simple statistics */ 309 int bitrate_channelmode_hist[16][4 + 1]; 310 int bitrate_blocktype_hist[16][4 + 1 + 1]; /*norm/start/short/stop/mixed(short)/sum */ 311 312 int bitrate_index; 313 int frame_number; /* number of frames encoded */ 314 int padding; /* padding for the current frame? */ 315 int mode_ext; 316 int encoder_delay; 317 int encoder_padding; /* number of samples of padding appended to input */ 318 } EncResult_t; 319 320 321 /* variables used by quantize.c */ 322 typedef struct { 323 /* variables for nspsytune */ 324 FLOAT longfact[SBMAX_l]; 325 FLOAT shortfact[SBMAX_s]; 326 FLOAT masking_lower; 327 FLOAT mask_adjust; /* the dbQ stuff */ 328 FLOAT mask_adjust_short; /* the dbQ stuff */ 329 int OldValue[2]; 330 int CurrentStep[2]; 331 int pseudohalf[SFBMAX]; 332 int sfb21_extra; /* will be set in lame_init_params */ 333 int substep_shaping; /* 0 = no substep 334 1 = use substep shaping at last step(VBR only) 335 (not implemented yet) 336 2 = use substep inside loop 337 3 = use substep inside loop and last step 338 */ 339 340 341 char bv_scf[576]; 342 } QntStateVar_t; 343 344 345 typedef struct { 346 replaygain_t *rgdata; 347 /* ReplayGain */ 348 } RpgStateVar_t; 349 350 351 typedef struct { 352 FLOAT noclipScale; /* user-specified scale factor required for preventing clipping */ 353 sample_t PeakSample; 354 int RadioGain; 355 int noclipGainChange; /* gain change required for preventing clipping */ 356 } RpgResult_t; 357 358 359 typedef struct { 360 int version; /* 0=MPEG-2/2.5 1=MPEG-1 */ 361 int samplerate_index; 362 int sideinfo_len; 363 364 int noise_shaping; /* 0 = none 365 1 = ISO AAC model 366 2 = allow scalefac_select=1 367 */ 368 369 int subblock_gain; /* 0 = no, 1 = yes */ 370 int use_best_huffman; /* 0 = no. 1=outside loop 2=inside loop(slow) */ 371 int noise_shaping_amp; /* 0 = ISO model: amplify all distorted bands 372 1 = amplify within 50% of max (on db scale) 373 2 = amplify only most distorted band 374 3 = method 1 and refine with method 2 375 */ 376 377 int noise_shaping_stop; /* 0 = stop at over=0, all scalefacs amplified or 378 a scalefac has reached max value 379 1 = stop when all scalefacs amplified or 380 a scalefac has reached max value 381 2 = stop when all scalefacs amplified 382 */ 383 384 385 int full_outer_loop; /* 0 = stop early after 0 distortion found. 1 = full search */ 386 387 int lowpassfreq; 388 int highpassfreq; 389 int samplerate_in; /* input_samp_rate in Hz. default=44.1 kHz */ 390 int samplerate_out; /* output_samp_rate. */ 391 int channels_in; /* number of channels in the input data stream (PCM or decoded PCM) */ 392 int channels_out; /* number of channels in the output data stream (not used for decoding) */ 393 int mode_gr; /* granules per frame */ 394 int force_ms; /* force M/S mode. requires mode=1 */ 395 396 int quant_comp; 397 int quant_comp_short; 398 399 int use_temporal_masking_effect; 400 int use_safe_joint_stereo; 401 402 int preset; 403 404 vbr_mode vbr; 405 int vbr_avg_bitrate_kbps; 406 int vbr_min_bitrate_index; /* min bitrate index */ 407 int vbr_max_bitrate_index; /* max bitrate index */ 408 int avg_bitrate; 409 int enforce_min_bitrate; /* strictly enforce VBR_min_bitrate normaly, it will be violated for analog silence */ 410 411 int findReplayGain; /* find the RG value? default=0 */ 412 int findPeakSample; 413 int decode_on_the_fly; /* decode on the fly? default=0 */ 414 int analysis; 415 int disable_reservoir; 416 int buffer_constraint; /* enforce ISO spec as much as possible */ 417 int free_format; 418 int write_lame_tag; /* add Xing VBR tag? */ 419 420 int error_protection; /* use 2 bytes per frame for a CRC checksum. default=0 */ 421 int copyright; /* mark as copyright. default=0 */ 422 int original; /* mark as original. default=1 */ 423 int extension; /* the MP3 'private extension' bit. Meaningless */ 424 int emphasis; /* Input PCM is emphased PCM (for 425 instance from one of the rarely 426 emphased CDs), it is STRONGLY not 427 recommended to use this, because 428 psycho does not take it into account, 429 and last but not least many decoders 430 don't care about these bits */ 431 432 433 MPEG_mode mode; 434 short_block_t short_blocks; 435 436 float interChRatio; 437 float msfix; /* Naoki's adjustment of Mid/Side maskings */ 438 float ATH_offset_db;/* add to ATH this many db */ 439 float ATH_offset_factor;/* change ATH by this factor, derived from ATH_offset_db */ 440 float ATHcurve; /* change ATH formula 4 shape */ 441 int ATHtype; 442 int ATHonly; /* only use ATH */ 443 int ATHshort; /* only use ATH for short blocks */ 444 int noATH; /* disable ATH */ 445 446 float ATHfixpoint; 447 448 float adjust_alto_db; 449 float adjust_bass_db; 450 float adjust_treble_db; 451 float adjust_sfb21_db; 452 453 float compression_ratio; /* sizeof(wav file)/sizeof(mp3 file) */ 454 455 /* lowpass and highpass filter control */ 456 FLOAT lowpass1, lowpass2; /* normalized frequency bounds of passband */ 457 FLOAT highpass1, highpass2; /* normalized frequency bounds of passband */ 458 459 /* scale input by this amount before encoding at least not used for MP3 decoding */ 460 FLOAT pcm_transform[2][2]; 461 462 FLOAT minval; 463 } SessionConfig_t; 464 465 466 struct lame_internal_flags { 467 468 /******************************************************************** 469 * internal variables NOT set by calling program, and should not be * 470 * modified by the calling program * 471 ********************************************************************/ 472 473 /* 474 * Some remarks to the Class_ID field: 475 * The Class ID is an Identifier for a pointer to this struct. 476 * It is very unlikely that a pointer to lame_global_flags has the same 32 bits 477 * in it's structure (large and other special properties, for instance prime). 478 * 479 * To test that the structure is right and initialized, use: 480 * if ( gfc -> Class_ID == LAME_ID ) ... 481 * Other remark: 482 * If you set a flag to 0 for uninit data and 1 for init data, the right test 483 * should be "if (flag == 1)" and NOT "if (flag)". Unintended modification 484 * of this element will be otherwise misinterpreted as an init. 485 */ 486 # define LAME_ID 0xFFF88E3B 487 unsigned long class_id; 488 489 int lame_init_params_successful; 490 int lame_encode_frame_init; 491 int iteration_init_init; 492 int fill_buffer_resample_init; 493 494 SessionConfig_t cfg; 495 496 /* variables used by lame.c */ 497 Bit_stream_struc bs; 498 III_side_info_t l3_side; 499 500 scalefac_struct scalefac_band; 501 502 PsyStateVar_t sv_psy; /* DATA FROM PSYMODEL.C */ 503 PsyResult_t ov_psy; 504 EncStateVar_t sv_enc; /* DATA FROM ENCODER.C */ 505 EncResult_t ov_enc; 506 QntStateVar_t sv_qnt; /* DATA FROM QUANTIZE.C */ 507 508 RpgStateVar_t sv_rpg; 509 RpgResult_t ov_rpg; 510 511 /* optional ID3 tags, used in id3tag.c */ 512 struct id3tag_spec tag_spec; 513 uint16_t nMusicCRC; 514 515 uint16_t _unused; 516 517 /* CPU features */ 518 struct { 519 unsigned int MMX:1; /* Pentium MMX, Pentium II...IV, K6, K6-2, 520 K6-III, Athlon */ 521 unsigned int AMD_3DNow:1; /* K6-2, K6-III, Athlon */ 522 unsigned int SSE:1; /* Pentium III, Pentium 4 */ 523 unsigned int SSE2:1; /* Pentium 4, K8 */ 524 unsigned int _unused:28; 525 } CPU_features; 526 527 528 VBR_seek_info_t VBR_seek_table; /* used for Xing VBR header */ 529 530 ATH_t *ATH; /* all ATH related stuff */ 531 532 PsyConst_t *cd_psy; 533 534 /* used by the frame analyzer */ 535 plotting_data *pinfo; 536 hip_t hip; 537 538 /* functions to replace with CPU feature optimized versions in takehiro.c */ 539 int (*choose_table) (const int *ix, const int *const end, int *const s); 540 void (*fft_fht) (FLOAT *, int); 541 void (*init_xrpow_core) (gr_info * const cod_info, FLOAT xrpow[576], int upper, 542 FLOAT * sum); 543 544 lame_report_function report_msg; 545 lame_report_function report_dbg; 546 lame_report_function report_err; 547 }; 548 549 #ifndef lame_internal_flags_defined 550 #define lame_internal_flags_defined 551 typedef struct lame_internal_flags lame_internal_flags; 552 #endif 553 554 555 /*********************************************************************** 556 * 557 * Global Function Prototype Declarations 558 * 559 ***********************************************************************/ 560 void freegfc(lame_internal_flags * const gfc); 561 void free_id3tag(lame_internal_flags * const gfc); 562 extern int BitrateIndex(int, int, int); 563 extern int FindNearestBitrate(int, int, int); 564 extern int map2MP3Frequency(int freq); 565 extern int SmpFrqIndex(int, int *const); 566 extern int nearestBitrateFullIndex(uint16_t bitrate); 567 extern FLOAT ATHformula(SessionConfig_t const *cfg, FLOAT freq); 568 extern FLOAT freq2bark(FLOAT freq); 569 void disable_FPE(void); 570 571 /* log/log10 approximations */ 572 extern void init_log_table(void); 573 extern ieee754_float32_t fast_log2(ieee754_float32_t x); 574 575 int isResamplingNecessary(SessionConfig_t const* cfg); 576 577 void fill_buffer(lame_internal_flags * gfc, 578 sample_t *const mfbuf[2], 579 sample_t const *const in_buffer[2], int nsamples, int *n_in, int *n_out); 580 581 /* same as lame_decode1 (look in lame.h), but returns 582 unclipped raw floating-point samples. It is declared 583 here, not in lame.h, because it returns LAME's 584 internal type sample_t. No more than 1152 samples 585 per channel are allowed. */ 586 int hip_decode1_unclipped(hip_t hip, unsigned char *mp3buf, 587 size_t len, sample_t pcm_l[], sample_t pcm_r[]); 588 589 590 extern int has_MMX(void); 591 extern int has_3DNow(void); 592 extern int has_SSE(void); 593 extern int has_SSE2(void); 594 595 596 597 /*********************************************************************** 598 * 599 * Macros about Message Printing and Exit 600 * 601 ***********************************************************************/ 602 603 extern void lame_report_def(const char* format, va_list args); 604 extern void lame_report_fnc(lame_report_function print_f, const char *, ...); 605 extern void lame_errorf(const lame_internal_flags * gfc, const char *, ...); 606 extern void lame_debugf(const lame_internal_flags * gfc, const char *, ...); 607 extern void lame_msgf(const lame_internal_flags * gfc, const char *, ...); 608 #define DEBUGF lame_debugf 609 #define ERRORF lame_errorf 610 #define MSGF lame_msgf 611 612 int is_lame_internal_flags_valid(const lame_internal_flags * gfp); 613 614 extern void hip_set_pinfo(hip_t hip, plotting_data* pinfo); 615 extern void hip_finish_pinfo(hip_t hip); 616 617 #ifdef __cplusplus 618 } 619 #endif 620 #endif /* LAME_UTIL_H */ 621