2 * lame utility library include file
4 * Copyright (c) 1999 Albert L Faber
5 * Copyright (c) 2008 Robert Hegemann
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.
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.
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.
28 #include "lame_global_flags.h"
34 /***********************************************************************
38 ***********************************************************************/
49 # define MAX_U_32_NUM UINT_MAX
51 # define MAX_U_32_NUM 0xFFFFFFFF
58 # define PI 3.14159265358979323846
66 # define LOG2 0.69314718055994530942
72 # define LOG10 2.30258509299404568402
77 # define SQRT2 M_SQRT2
79 # define SQRT2 1.41421356237309504880
83 #define CRC16_POLYNOMIAL 0x8005
85 #define MAX_BITS_PER_CHANNEL 4095
86 #define MAX_BITS_PER_GRANULE 7680
88 /* "bit_stream.h" Definitions */
89 #define BUFFER_SIZE LAME_MAXMP3BUFFER
91 #define Min(A, B) ((A) < (B) ? (A) : (B))
92 #define Max(A, B) ((A) > (B) ? (A) : (B))
94 /* log/log10 approximations */
96 #define FAST_LOG10(x) (fast_log2(x)*(LOG2/LOG10))
97 #define FAST_LOG(x) (fast_log2(x)*LOG2)
98 #define FAST_LOG10_X(x,y) (fast_log2(x)*(LOG2/LOG10*(y)))
99 #define FAST_LOG_X(x,y) (fast_log2(x)*(LOG2*(y)))
101 #define FAST_LOG10(x) log10(x)
102 #define FAST_LOG(x) log(x)
103 #define FAST_LOG10_X(x,y) (log10(x)*(y))
104 #define FAST_LOG_X(x,y) (log(x)*(y))
108 struct replaygain_data;
109 #ifndef replaygain_data_defined
110 #define replaygain_data_defined
111 typedef struct replaygain_data replaygain_t;
113 struct plotting_data;
114 #ifndef plotting_data_defined
115 #define plotting_data_defined
116 typedef struct plotting_data plotting_data;
119 /***********************************************************************
121 * Global Type Definitions
123 ***********************************************************************/
126 void *aligned; /* pointer to ie. 128 bit aligned memory */
127 void *pointer; /* to use with malloc/free */
130 typedef void (*iteration_loop_t) (lame_internal_flags * gfc, const FLOAT pe[2][2],
131 const FLOAT ms_ratio[2], const III_psy_ratio ratio[2][2]);
134 /* "bit_stream.h" Type Definitions */
136 typedef struct bit_stream_struc {
137 unsigned char *buf; /* bit stream buffer */
138 int buf_size; /* size of buffer (in number of bytes) */
139 int totbit; /* bit counter of bit stream */
140 int buf_byte_idx; /* pointer to top byte in buffer */
141 int buf_bit_idx; /* pointer to top bit of top byte in buffer */
143 /* format of file in rd mode (BINARY/ASCII) */
149 int sum; /* what we have seen so far */
150 int seen; /* how many frames we have seen in this chunk */
151 int want; /* how many frames we want to collect into one chunk */
152 int pos; /* actual position in our bag */
153 int size; /* size of our bag */
154 int *bag; /* pointer to our bag */
155 unsigned int nVbrNumFrames;
156 unsigned long nBytesWritten;
158 unsigned int TotalFrameSize;
163 * ATH related stuff, if something new ATH related has to be added,
164 * please plugg it here into the ATH_t struct
167 int use_adjust; /* method for the auto adjustment */
168 FLOAT aa_sensitivity_p; /* factor for tuning the (sample power)
169 point below which adaptive threshold
170 of hearing adjustment occurs */
171 FLOAT adjust_factor; /* lowering based on peak volume, 1 = no lowering */
172 FLOAT adjust_limit; /* limit for dynamic ATH adjust */
173 FLOAT decay; /* determined to lower x dB each second */
174 FLOAT floor; /* lowest ATH value */
175 FLOAT l[SBMAX_l]; /* ATH for sfbs in long blocks */
176 FLOAT s[SBMAX_s]; /* ATH for sfbs in short blocks */
177 FLOAT psfb21[PSFB21]; /* ATH for partitionned sfb21 in long blocks */
178 FLOAT psfb12[PSFB12]; /* ATH for partitionned sfb12 in short blocks */
179 FLOAT cb_l[CBANDS]; /* ATH for long block convolution bands */
180 FLOAT cb_s[CBANDS]; /* ATH for short block convolution bands */
181 FLOAT eql_w[BLKSIZE / 2]; /* equal loudness weights (based on ATH) */
185 * PSY Model related stuff
189 FLOAT masking_lower[CBANDS];
190 FLOAT minval[CBANDS];
191 FLOAT rnumlines[CBANDS];
192 FLOAT mld_cb[CBANDS];
193 FLOAT mld[Max(SBMAX_l,SBMAX_s)];
194 FLOAT bo_weight[Max(SBMAX_l,SBMAX_s)]; /* band weight long scalefactor bands, at transition */
195 FLOAT attack_threshold; /* short block tuning */
196 int s3ind[CBANDS][2];
197 int numlines[CBANDS];
198 int bm[Max(SBMAX_l,SBMAX_s)];
199 int bo[Max(SBMAX_l,SBMAX_s)];
201 int n_sb; /* SBMAX_l or SBMAX_s */
207 * global data constants
212 PsyConst_CB2SB_t l_to_s;
213 FLOAT attack_threshold[4];
215 int force_short_block_calc;
221 FLOAT nb_l1[4][CBANDS], nb_l2[4][CBANDS];
222 FLOAT nb_s1[4][CBANDS], nb_s2[4][CBANDS];
227 /* loudness calculation (for adaptive threshold of hearing) */
228 FLOAT loudness_sq_save[2]; /* account for granule delay of L3psycho_anal */
232 FLOAT last_en_subshort[4][9];
235 int blocktype_old[2];
240 /* loudness calculation (for adaptive threshold of hearing) */
241 FLOAT loudness_sq[2][2]; /* loudness^2 approx. per granule and channel */
245 /* variables used by encoder.c */
247 /* variables for newmdct.c */
248 FLOAT sb_sample[2][2][18][SBLIMIT];
249 FLOAT amp_filter[32];
251 /* variables used by util.c */
252 /* BPC = maximum number of filter convolution windows to precompute */
254 double itime[2]; /* float precision seems to be not enough */
255 sample_t *inbuf_old[2];
256 sample_t *blackfilt[2 * BPC + 1];
260 /* used for padding */
264 /* variables for bitstream.c */
265 /* mpeg1: buffer=511 bytes smallest frame: 96-38(sideinfo)=58
266 * max number of frames in reservoir: 8
267 * mpeg2: buffer=255 bytes. smallest frame: 24-23bytes=1
268 * with VBR, if you are encoding all silence, it is possible to
269 * have 8kbs/24khz frames with 1byte of data each, which means we need
270 * to buffer up to 255 headers! */
271 /* also, max_header_buf has to be a power of two */
272 #define MAX_HEADER_BUF 256
273 #define MAX_HEADER_LEN 40 /* max size of header is 38 */
277 char buf[MAX_HEADER_LEN];
278 } header[MAX_HEADER_BUF];
284 /* variables for reservoir.c */
285 int ResvSize; /* in bits */
286 int ResvMax; /* in bits */
288 int in_buffer_nsamples;
289 sample_t *in_buffer_0;
290 sample_t *in_buffer_1;
293 # define MFSIZE ( 3*1152 + ENCDELAY - MDCTDELAY )
295 sample_t mfbuf[2][MFSIZE];
297 int canary; /* DEBUG: some part of the encoding process is overrunning mfbuf[] */
299 int mf_samples_to_encode;
306 /* simple statistics */
307 int bitrate_channelmode_hist[16][4 + 1];
308 int bitrate_blocktype_hist[16][4 + 1 + 1]; /*norm/start/short/stop/mixed(short)/sum */
311 int frame_number; /* number of frames encoded */
312 int padding; /* padding for the current frame? */
315 int encoder_padding; /* number of samples of padding appended to input */
319 /* variables used by quantize.c */
321 /* variables for nspsytune */
322 FLOAT longfact[SBMAX_l];
323 FLOAT shortfact[SBMAX_s];
325 FLOAT mask_adjust; /* the dbQ stuff */
326 FLOAT mask_adjust_short; /* the dbQ stuff */
329 int pseudohalf[SFBMAX];
330 int sfb21_extra; /* will be set in lame_init_params */
331 int substep_shaping; /* 0 = no substep
332 1 = use substep shaping at last step(VBR only)
333 (not implemented yet)
334 2 = use substep inside loop
335 3 = use substep inside loop and last step
344 replaygain_t *rgdata;
350 FLOAT noclipScale; /* user-specified scale factor required for preventing clipping */
353 int noclipGainChange; /* gain change required for preventing clipping */
358 int version; /* 0=MPEG-2/2.5 1=MPEG-1 */
359 int samplerate_index;
362 int noise_shaping; /* 0 = none
364 2 = allow scalefac_select=1
367 int subblock_gain; /* 0 = no, 1 = yes */
368 int use_best_huffman; /* 0 = no. 1=outside loop 2=inside loop(slow) */
369 int noise_shaping_amp; /* 0 = ISO model: amplify all distorted bands
370 1 = amplify within 50% of max (on db scale)
371 2 = amplify only most distorted band
372 3 = method 1 and refine with method 2
375 int noise_shaping_stop; /* 0 = stop at over=0, all scalefacs amplified or
376 a scalefac has reached max value
377 1 = stop when all scalefacs amplified or
378 a scalefac has reached max value
379 2 = stop when all scalefacs amplified
383 int full_outer_loop; /* 0 = stop early after 0 distortion found. 1 = full search */
387 int samplerate_in; /* input_samp_rate in Hz. default=44.1 kHz */
388 int samplerate_out; /* output_samp_rate. */
389 int channels_in; /* number of channels in the input data stream (PCM or decoded PCM) */
390 int channels_out; /* number of channels in the output data stream (not used for decoding) */
391 int mode_gr; /* granules per frame */
392 int force_ms; /* force M/S mode. requires mode=1 */
395 int quant_comp_short;
397 int use_temporal_masking_effect;
398 int use_safe_joint_stereo;
403 int vbr_avg_bitrate_kbps;
404 int vbr_min_bitrate_index; /* min bitrate index */
405 int vbr_max_bitrate_index; /* max bitrate index */
407 int enforce_min_bitrate; /* strictly enforce VBR_min_bitrate normaly, it will be violated for analog silence */
409 int findReplayGain; /* find the RG value? default=0 */
411 int decode_on_the_fly; /* decode on the fly? default=0 */
413 int disable_reservoir;
414 int buffer_constraint; /* enforce ISO spec as much as possible */
416 int write_lame_tag; /* add Xing VBR tag? */
418 int error_protection; /* use 2 bytes per frame for a CRC checksum. default=0 */
419 int copyright; /* mark as copyright. default=0 */
420 int original; /* mark as original. default=1 */
421 int extension; /* the MP3 'private extension' bit. Meaningless */
422 int emphasis; /* Input PCM is emphased PCM (for
423 instance from one of the rarely
424 emphased CDs), it is STRONGLY not
425 recommended to use this, because
426 psycho does not take it into account,
427 and last but not least many decoders
428 don't care about these bits */
432 short_block_t short_blocks;
435 float msfix; /* Naoki's adjustment of Mid/Side maskings */
436 float ATH_offset_db;/* add to ATH this many db */
437 float ATH_offset_factor;/* change ATH by this factor, derived from ATH_offset_db */
438 float ATHcurve; /* change ATH formula 4 shape */
440 int ATHonly; /* only use ATH */
441 int ATHshort; /* only use ATH for short blocks */
442 int noATH; /* disable ATH */
446 float adjust_alto_db;
447 float adjust_bass_db;
448 float adjust_treble_db;
449 float adjust_sfb21_db;
451 float compression_ratio; /* sizeof(wav file)/sizeof(mp3 file) */
453 /* lowpass and highpass filter control */
454 FLOAT lowpass1, lowpass2; /* normalized frequency bounds of passband */
455 FLOAT highpass1, highpass2; /* normalized frequency bounds of passband */
457 /* scale input by this amount before encoding at least not used for MP3 decoding */
458 FLOAT pcm_transform[2][2];
464 struct lame_internal_flags {
466 /********************************************************************
467 * internal variables NOT set by calling program, and should not be *
468 * modified by the calling program *
469 ********************************************************************/
472 * Some remarks to the Class_ID field:
473 * The Class ID is an Identifier for a pointer to this struct.
474 * It is very unlikely that a pointer to lame_global_flags has the same 32 bits
475 * in it's structure (large and other special properties, for instance prime).
477 * To test that the structure is right and initialized, use:
478 * if ( gfc -> Class_ID == LAME_ID ) ...
480 * If you set a flag to 0 for uninit data and 1 for init data, the right test
481 * should be "if (flag == 1)" and NOT "if (flag)". Unintended modification
482 * of this element will be otherwise misinterpreted as an init.
484 # define LAME_ID 0xFFF88E3B
485 unsigned long class_id;
487 int lame_encode_frame_init;
488 int iteration_init_init;
489 int fill_buffer_resample_init;
493 /* variables used by lame.c */
495 III_side_info_t l3_side;
497 scalefac_struct scalefac_band;
499 PsyStateVar_t sv_psy; /* DATA FROM PSYMODEL.C */
501 EncStateVar_t sv_enc; /* DATA FROM ENCODER.C */
503 QntStateVar_t sv_qnt; /* DATA FROM QUANTIZE.C */
505 RpgStateVar_t sv_rpg;
508 /* optional ID3 tags, used in id3tag.c */
509 struct id3tag_spec tag_spec;
516 unsigned int MMX:1; /* Pentium MMX, Pentium II...IV, K6, K6-2,
518 unsigned int AMD_3DNow:1; /* K6-2, K6-III, Athlon */
519 unsigned int SSE:1; /* Pentium III, Pentium 4 */
520 unsigned int SSE2:1; /* Pentium 4, K8 */
521 unsigned int _unused:28;
525 VBR_seek_info_t VBR_seek_table; /* used for Xing VBR header */
527 ATH_t *ATH; /* all ATH related stuff */
531 /* used by the frame analyzer */
532 plotting_data *pinfo;
535 iteration_loop_t iteration_loop;
537 /* functions to replace with CPU feature optimized versions in takehiro.c */
538 int (*choose_table) (const int *ix, const int *const end, int *const s);
539 void (*fft_fht) (FLOAT *, int);
540 void (*init_xrpow_core) (gr_info * const cod_info, FLOAT xrpow[576], int upper,
543 lame_report_function report_msg;
544 lame_report_function report_dbg;
545 lame_report_function report_err;
548 #ifndef lame_internal_flags_defined
549 #define lame_internal_flags_defined
550 typedef struct lame_internal_flags lame_internal_flags;
554 /***********************************************************************
556 * Global Function Prototype Declarations
558 ***********************************************************************/
559 void freegfc(lame_internal_flags * const gfc);
560 void free_id3tag(lame_internal_flags * const gfc);
561 extern int BitrateIndex(int, int, int);
562 extern int FindNearestBitrate(int, int, int);
563 extern int map2MP3Frequency(int freq);
564 extern int SmpFrqIndex(int, int *const);
565 extern int nearestBitrateFullIndex(uint16_t brate);
566 extern FLOAT ATHformula(SessionConfig_t const *cfg, FLOAT freq);
567 extern FLOAT freq2bark(FLOAT freq);
568 void disable_FPE(void);
570 /* log/log10 approximations */
571 extern void init_log_table(void);
572 extern ieee754_float32_t fast_log2(ieee754_float32_t x);
574 int isResamplingNecessary(SessionConfig_t const* cfg);
576 void fill_buffer(lame_internal_flags * gfc,
577 sample_t *const mfbuf[2],
578 sample_t const *const in_buffer[2], int nsamples, int *n_in, int *n_out);
580 /* same as lame_decode1 (look in lame.h), but returns
581 unclipped raw floating-point samples. It is declared
582 here, not in lame.h, because it returns LAME's
583 internal type sample_t. No more than 1152 samples
584 per channel are allowed. */
585 int hip_decode1_unclipped(hip_t hip, unsigned char *mp3buf,
586 size_t len, sample_t pcm_l[], sample_t pcm_r[]);
589 extern int has_MMX(void);
590 extern int has_3DNow(void);
591 extern int has_SSE(void);
592 extern int has_SSE2(void);
596 /***********************************************************************
598 * Macros about Message Printing and Exit
600 ***********************************************************************/
602 extern void lame_report_def(const char* format, va_list args);
603 extern void lame_report_fnc(lame_report_function print_f, const char *, ...);
604 extern void lame_errorf(const lame_internal_flags * gfc, const char *, ...);
605 extern void lame_debugf(const lame_internal_flags * gfc, const char *, ...);
606 extern void lame_msgf(const lame_internal_flags * gfc, const char *, ...);
607 #define DEBUGF lame_debugf
608 #define ERRORF lame_errorf
609 #define MSGF lame_msgf
611 int is_lame_internal_flags_valid(const lame_internal_flags * gfp);
613 extern void hip_set_pinfo(hip_t hip, plotting_data* pinfo);
618 #endif /* LAME_UTIL_H */