util.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * lame utility library include file
  11. *
  12. * Copyright (c) 1999 Albert L Faber
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Library General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Library General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Library General Public
  25. * License along with this library; if not, write to the
  26. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  27. * Boston, MA 02111-1307, USA.
  28. */
  29. #ifndef LAME_UTIL_H
  30. #define LAME_UTIL_H
  31. #ifdef HUGE_VAL /* math.h already seen? */
  32. #ifndef fabs
  33. #define fabs(x) ((double)((x) < 0? -(x): (x)))
  34. #endif
  35. #endif
  36. /***********************************************************************
  37. *
  38. * Global Include Files
  39. *
  40. ***********************************************************************/
  41. #include "machine.h"
  42. #include "encoder.h"
  43. #include "lame.h"
  44. #include "lame-analysis.h"
  45. #include "id3tag.h"
  46. /***********************************************************************
  47. *
  48. * Global Definitions
  49. *
  50. ***********************************************************************/
  51. #ifndef FALSE
  52. #define FALSE 0
  53. #endif
  54. #ifndef TRUE
  55. #define TRUE (!FALSE)
  56. #endif
  57. #ifdef UINT_MAX
  58. # define MAX_U_32_NUM UINT_MAX
  59. #else
  60. # define MAX_U_32_NUM 0xFFFFFFFF
  61. #endif
  62. #ifndef PI
  63. # ifdef M_PI
  64. # define PI M_PI
  65. # else
  66. # define PI 3.14159265358979323846
  67. # endif
  68. #endif
  69. #ifdef M_LN2
  70. # define LOG2 M_LN2
  71. #else
  72. # define LOG2 0.69314718055994530942
  73. #endif
  74. #ifdef M_LN10
  75. # define LOG10 M_LN10
  76. #else
  77. # define LOG10 2.30258509299404568402
  78. #endif
  79. #ifdef M_SQRT2
  80. # define SQRT2 M_SQRT2
  81. #else
  82. # define SQRT2 1.41421356237309504880
  83. #endif
  84. #define HAN_SIZE 512
  85. #define CRC16_POLYNOMIAL 0x8005
  86. #define MAX_BITS 4095
  87. /* "bit_stream.h" Definitions */
  88. #define BUFFER_SIZE LAME_MAXMP3BUFFER
  89. #define Min(A, B) ((A) < (B) ? (A) : (B))
  90. #define Max(A, B) ((A) > (B) ? (A) : (B))
  91. /***********************************************************************
  92. *
  93. * Global Type Definitions
  94. *
  95. ***********************************************************************/
  96. /* "bit_stream.h" Type Definitions */
  97. typedef struct bit_stream_struc {
  98. unsigned char *buf; /* bit stream buffer */
  99. int buf_size; /* size of buffer (in number of bytes) */
  100. int totbit; /* bit counter of bit stream */
  101. int buf_byte_idx; /* pointer to top byte in buffer */
  102. int buf_bit_idx; /* pointer to top bit of top byte in buffer */
  103. /* format of file in rd mode (BINARY/ASCII) */
  104. } Bit_stream_struc;
  105. #include "l3side.h"
  106. /* variables used for --nspsytune */
  107. typedef struct {
  108. int use; /* indicates the use of exp_nspsytune */
  109. int safejoint; /* safe joint stereo mode */
  110. FLOAT last_en_subshort[4][9];
  111. FLOAT last_attack_intensity[4][9];
  112. FLOAT last_thm[4][SBMAX_s][3];
  113. int last_attacks[4][3];
  114. FLOAT pe_l[4],pe_s[4];
  115. FLOAT pefirbuf[19];
  116. FLOAT bass,alto,treble;
  117. } nsPsy_t;
  118. typedef struct
  119. {
  120. int sum; // what we have seen so far
  121. int seen; // how many frames we have seen in this chunk
  122. int want; // how many frames we want to collect into one chunk
  123. int pos; // actual position in our bag
  124. int size; // size of our bag
  125. int *bag; // pointer to our bag
  126. } VBR_seek_info_t;
  127. /**
  128. * ATH related stuff, if something new ATH related has to be added,
  129. * please plugg it here into the ATH_t struct
  130. */
  131. typedef struct
  132. {
  133. int use_adjust; // do we want to use the auto adjustment yes/no
  134. FLOAT8 adjust; // lowering based on peak volume, 1 = no lowering
  135. FLOAT8 adjust_limit; // limit for dynamic ATH adjust
  136. FLOAT8 decay; // determined to lower x dB each second
  137. FLOAT8 l[SBMAX_l]; // ATH for sfbs in long blocks
  138. FLOAT8 s[SBMAX_s]; // ATH for sfbs in short blocks
  139. FLOAT8 cb[CBANDS]; // ATH for convolution bands
  140. } ATH_t;
  141. /* Guest structure, only temporarly here */
  142. typedef enum {
  143. coding_MPEG_Layer_1 = 1,
  144. coding_MPEG_Layer_2 = 2,
  145. coding_MPEG_Layer_3 = 3,
  146. coding_MPEG_AAC = 4,
  147. coding_Ogg_Vorbis = 5,
  148. coding_MPEG_plus = 6
  149. } coding_t;
  150. #define MAX_CHANNELS 2
  151. typedef struct {
  152. unsigned long Class_ID; /* Class ID to recognize a resample_t
  153. object */
  154. long double sample_freq_in; /* Input sample frequency in Hz */
  155. long double sample_freq_out; /* requested Output sample frequency in Hz */
  156. float lowpass_freq; /* lowpass frequency, this is the -6 dB
  157. point */
  158. int scale_in; /* the resampling is actually done by
  159. scale_out: */
  160. int scale_out; /* frequency is
  161. samplefreq_in * scale_out / scal */
  162. int taps; /* number of taps for every FIR resample
  163. filter */
  164. sample_t** fir; /* the FIR resample filters:
  165. fir [scale_out] [taps */
  166. void* firfree; /* start address of the alloced memory for
  167. fir, */
  168. unsigned char* src_step;
  169. sample_t* in_old [MAX_CHANNELS];
  170. // uint64_t sample_count [MAX_CHANNELS];
  171. unsigned fir_stepper [MAX_CHANNELS];
  172. int inp_stepper [MAX_CHANNELS];
  173. } resample_t;
  174. typedef struct {
  175. /********************************************************************
  176. * internal variables NOT set by calling program, and should not be *
  177. * modified by the calling program *
  178. ********************************************************************/
  179. /*
  180. * Some remarks to the Class_ID field:
  181. * The Class ID is an Identifier for a pointer to this struct.
  182. * It is very unlikely that a pointer to lame_global_flags has the same 32 bits
  183. * in it's structure (large and other special properties, for instance prime).
  184. *
  185. * To test that the structure is right and initialized, use:
  186. * if ( gfc -> Class_ID == LAME_ID ) ...
  187. * Other remark:
  188. * If you set a flag to 0 for uninit data and 1 for init data, the right test
  189. * should be "if (flag == 1)" and NOT "if (flag)". Unintended modification
  190. * of this element will be otherwise misinterpreted as an init.
  191. */
  192. #define LAME_ID 0xFFF88E3B
  193. unsigned long Class_ID;
  194. struct {
  195. void (*msgf) (const char *format, va_list ap);
  196. void (*debugf)(const char *format, va_list ap);
  197. void (*errorf)(const char *format, va_list ap);
  198. } report;
  199. int lame_encode_frame_init;
  200. int iteration_init_init;
  201. int fill_buffer_resample_init;
  202. int psymodel_init;
  203. int padding; /* padding for the current frame? */
  204. int mode_gr; /* granules per frame */
  205. int channels_in; /* number of channels in the input data stream (PCM or decoded PCM) */
  206. int channels_out; /* number of channels in the output data stream (not used for decoding) */
  207. resample_t* resample_in; /* context for coding (PCM=>MP3) resampling */
  208. resample_t* resample_out; /* context for decoding (MP3=>PCM) resampling */
  209. long double samplefreq_in;
  210. long double samplefreq_out;
  211. #ifndef MFSIZE
  212. # define MFSIZE ( 3*1152 + ENCDELAY - MDCTDELAY )
  213. #endif
  214. #ifdef KLEMM_44
  215. sample_t* mfbuf [MAX_CHANNELS];
  216. #else
  217. sample_t mfbuf [2] [MFSIZE];
  218. #endif
  219. size_t frame_size; /* size of one frame in samples per channel */
  220. lame_global_flags* gfp; /* needed as long as the frame encoding functions must access gfp (all needed information can be added to gfc) */
  221. coding_t coding; /* MPEG Layer 1/2/3, Ogg Vorbis, MPEG AAC, ... */
  222. unsigned long frame_count; /* Number of frames coded, 2^32 > 3 years */
  223. int mf_samples_to_encode;
  224. int mf_size;
  225. float ampl; /* amplification at the end of the current chunk (1. = 0 dB) */
  226. float last_ampl; /* amplification at the end of the last chunk (1. = 0 dB) */
  227. int VBR_min_bitrate; /* min bitrate index */
  228. int VBR_max_bitrate; /* max bitrate index */
  229. float resample_ratio; /* input_samp_rate/output_samp_rate */
  230. int bitrate_index;
  231. int samplerate_index;
  232. int mode_ext;
  233. /* lowpass and highpass filter control */
  234. float lowpass1,lowpass2; /* normalized frequency bounds of passband */
  235. float highpass1,highpass2; /* normalized frequency bounds of passband */
  236. /* polyphase filter (filter_type=0) */
  237. int lowpass_band; /* zero bands >= lowpass_band in the polyphase filterbank */
  238. int highpass_band; /* zero bands <= highpass_band */
  239. int lowpass_start_band; /* amplify bands between start */
  240. int lowpass_end_band; /* and end for lowpass */
  241. int highpass_start_band; /* amplify bands between start */
  242. int highpass_end_band; /* and end for highpass */
  243. int filter_type; /* 0=polyphase filter, 1= FIR filter 2=MDCT filter(bad)*/
  244. int quantization; /* 0 = ISO formual, 1=best amplitude */
  245. int noise_shaping; /* 0 = none
  246. 1 = ISO AAC model
  247. 2 = allow scalefac_select=1
  248. */
  249. int noise_shaping_amp; /* 0 = ISO model: amplify all distorted bands
  250. 1 = amplify only most distorted band
  251. 2 = amplify bands using?
  252. 3 = amplify bands using?
  253. */
  254. int psymodel; /* 1 = gpsycho. 0 = none */
  255. int noise_shaping_stop; /* 0 = stop at over=0, all scalefacs amplified or
  256. a scalefac has reached max value
  257. 1 = stop when all scalefacs amplified or
  258. a scalefac has reached max value
  259. 2 = stop when all scalefacs amplified
  260. */
  261. int use_best_huffman; /* 0 = no. 1=outside loop 2=inside loop(slow) */
  262. /* variables used by lame.c */
  263. Bit_stream_struc bs;
  264. III_side_info_t l3_side;
  265. FLOAT8 ms_ratio[2];
  266. /* used for padding */
  267. int frac_SpF;
  268. int slot_lag;
  269. /* optional ID3 tags, used in id3tag.c */
  270. struct id3tag_spec tag_spec;
  271. /* variables used by quantize.c */
  272. int OldValue[2];
  273. int CurrentStep;
  274. FLOAT8 decay;
  275. FLOAT8 masking_lower;
  276. char bv_scf[576];
  277. int sfb21_extra; /* will be set in lame_init_params */
  278. int is_mpeg1; /* 1 for MPEG-1, 0 for MPEG-2(.5) */
  279. #ifndef KLEMM_44
  280. /* variables used by util.c */
  281. /* BPC = maximum number of filter convolution windows to precompute */
  282. #define BPC 320
  283. sample_t *inbuf_old [2];
  284. sample_t *blackfilt [2*BPC+1];
  285. FLOAT8 itime[2];
  286. #endif
  287. int sideinfo_len;
  288. /* variables for newmdct.c */
  289. FLOAT8 sb_sample[2][2][18][SBLIMIT];
  290. FLOAT8 amp_lowpass[32];
  291. FLOAT8 amp_highpass[32];
  292. /* variables for bitstream.c */
  293. /* mpeg1: buffer=511 bytes smallest frame: 96-38(sideinfo)=58
  294. * max number of frames in reservoir: 8
  295. * mpeg2: buffer=255 bytes. smallest frame: 24-23bytes=1
  296. * with VBR, if you are encoding all silence, it is possible to
  297. * have 8kbs/24khz frames with 1byte of data each, which means we need
  298. * to buffer up to 255 headers! */
  299. /* also, max_header_buf has to be a power of two */
  300. #define MAX_HEADER_BUF 256
  301. #define MAX_HEADER_LEN 40 /* max size of header is 38 */
  302. struct {
  303. int write_timing;
  304. int ptr;
  305. char buf[MAX_HEADER_LEN];
  306. } header[MAX_HEADER_BUF];
  307. int h_ptr;
  308. int w_ptr;
  309. int ancillary_flag;
  310. /* variables for reservoir.c */
  311. int ResvSize; /* in bits */
  312. int ResvMax; /* in bits */
  313. scalefac_struct scalefac_band;
  314. /* DATA FROM PSYMODEL.C */
  315. /* The static variables "r", "phi_sav", "new", "old" and "oldest" have */
  316. /* to be remembered for the unpredictability measure. For "r" and */
  317. /* "phi_sav", the first index from the left is the channel select and */
  318. /* the second index is the "age" of the data. */
  319. FLOAT8 minval[CBANDS];
  320. FLOAT8 nb_1[4][CBANDS], nb_2[4][CBANDS];
  321. FLOAT8 s3_s[CBANDS][CBANDS];
  322. FLOAT8 s3_l[CBANDS][CBANDS];
  323. III_psy_xmin thm[4];
  324. III_psy_xmin en[4];
  325. /* unpredictability calculation
  326. */
  327. int cw_upper_index;
  328. int cw_lower_index;
  329. FLOAT ax_sav[4][2][HBLKSIZE];
  330. FLOAT bx_sav[4][2][HBLKSIZE];
  331. FLOAT rx_sav[4][2][HBLKSIZE];
  332. FLOAT cw[HBLKSIZE];
  333. /* fft and energy calculation */
  334. FLOAT wsamp_L[2][BLKSIZE];
  335. FLOAT energy[HBLKSIZE];
  336. FLOAT wsamp_S[2][3][BLKSIZE_s];
  337. FLOAT energy_s[3][HBLKSIZE_s];
  338. FLOAT tot_ener[4];
  339. /* fft.c */
  340. FLOAT window[BLKSIZE], window_s[BLKSIZE_s/2];
  341. /* Scale Factor Bands */
  342. FLOAT8 w1_l[SBMAX_l], w2_l[SBMAX_l];
  343. FLOAT8 w1_s[SBMAX_s], w2_s[SBMAX_s];
  344. FLOAT8 mld_l[SBMAX_l],mld_s[SBMAX_s];
  345. int bu_l[SBMAX_l],bo_l[SBMAX_l] ;
  346. int bu_s[SBMAX_s],bo_s[SBMAX_s] ;
  347. int npart_l,npart_s;
  348. int npart_l_orig,npart_s_orig;
  349. int s3ind[CBANDS][2];
  350. int s3ind_s[CBANDS][2];
  351. FLOAT8 SNR_s[CBANDS];
  352. int numlines_s[CBANDS];
  353. int numlines_l[CBANDS];
  354. /* frame analyzer */
  355. FLOAT energy_save[4][HBLKSIZE];
  356. FLOAT8 pe_save[4];
  357. FLOAT8 ers_save[4];
  358. /* simple statistics */
  359. int bitrate_stereoMode_Hist [16] [4+1];
  360. /* ratios */
  361. FLOAT8 pe[4];
  362. FLOAT8 ms_ratio_s_old,ms_ratio_l_old;
  363. FLOAT8 ms_ener_ratio_old;
  364. /* block type */
  365. int blocktype_old[2];
  366. /* used by the frame analyzer */
  367. plotting_data *pinfo;
  368. /* CPU features */
  369. struct {
  370. unsigned int i387 : 1; /* FPU is a normal Intel CPU */
  371. unsigned int MMX : 1; /* Pentium MMX, Pentium II...IV, K6, K6-2,
  372. K6-III, Athlon */
  373. unsigned int AMD_3DNow : 1; /* K6-2, K6-III, Athlon */
  374. unsigned int SIMD : 1; /* Pentium III, Pentium 4 */
  375. unsigned int SIMD2 : 1; /* Pentium 4, K8 */
  376. } CPU_features;
  377. /* functions to replace with CPU feature optimized versions in takehiro.c */
  378. int (*choose_table)(const int *ix, const int *end, int *s);
  379. nsPsy_t nsPsy; /* variables used for --nspsytune */
  380. unsigned crcvalue;
  381. VBR_seek_info_t VBR_seek_table; // used for Xing VBR header
  382. ATH_t *ATH; // all ATH related stuff
  383. } lame_internal_flags;
  384. /***********************************************************************
  385. *
  386. * Global Function Prototype Declarations
  387. *
  388. ***********************************************************************/
  389. void freegfc(lame_internal_flags *gfc);
  390. extern int BitrateIndex(int, int,int);
  391. extern int FindNearestBitrate(int,int,int);
  392. extern int map2MP3Frequency(int freq);
  393. extern int SmpFrqIndex(int, int*);
  394. extern FLOAT8 ATHformula(FLOAT8 f,lame_global_flags *gfp);
  395. extern FLOAT8 freq2bark(FLOAT8 freq);
  396. extern FLOAT8 freq2cbw(FLOAT8 freq);
  397. extern void freorder(int scalefac_band[],FLOAT8 ix_orig[576]);
  398. void disable_FPE(void);
  399. extern void
  400. getframebits(lame_global_flags *gfp, int *bitsPerFrame, int *mean_bits);
  401. void fill_buffer(lame_global_flags *gfp,
  402. sample_t *mfbuf[2],
  403. sample_t *in_buffer[2],
  404. int nsamples, int *n_in, int *n_out);
  405. int fill_buffer_resample (
  406. lame_global_flags *gfp,
  407. sample_t* outbuf,
  408. int desired_len,
  409. sample_t* inbuf,
  410. int len,
  411. int* num_used,
  412. int channels );
  413. extern int has_i387 ( void );
  414. extern int has_MMX ( void );
  415. extern int has_3DNow ( void );
  416. extern int has_SIMD ( void );
  417. extern int has_SIMD2 ( void );
  418. extern void updateStats (lame_internal_flags *gfc);
  419. /***********************************************************************
  420. *
  421. * Macros about Message Printing and Exit
  422. *
  423. ***********************************************************************/
  424. extern void lame_errorf(const lame_internal_flags *gfc, const char *, ...);
  425. extern void lame_debugf(const lame_internal_flags *gfc, const char *, ...);
  426. extern void lame_msgf (const lame_internal_flags *gfc, const char *, ...);
  427. #define DEBUGF lame_debugf
  428. #define ERRORF lame_errorf
  429. #define MSGF lame_msgf
  430. int select_kth_int(int b[], int N, int k);
  431. #endif /* LAME_UTIL_H */