Subversion Repositories planix.SVN

Rev

Details | Last modification | View Log | RSS feed

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