util.h 21 KB

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