1 /*
2  *	TwoLAME: an optimized MPEG Audio Layer Two encoder
3  *
4  *	Copyright (C) 2001-2004 Michael Cheng
5  *	Copyright (C) 2004-2006 The TwoLAME Project
6  *
7  *	This library is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU Lesser General Public
9  *	License as published by the Free Software Foundation; either
10  *	version 2.1 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  *	Lesser General Public License for more details.
16  *
17  *	You should have received a copy of the GNU Lesser General Public
18  *	License along with this library; if not, write to the Free Software
19  *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *  $Id$
22  *
23  */
24 
25 
26 #ifndef _COMMON_H
27 #define _COMMON_H
28 
29 #ifdef _WIN32
30 # include "../win32/configwin.h"
31 #else
32 # include "config.h"
33 #endif
34 
35 #include "twolame.h"
36 
37 
38 
39 /***************************************************************************************
40  Common Definitions
41 ****************************************************************************************/
42 
43 #ifndef FLOAT
44 #define			FLOAT					double
45 #endif
46 
47 #define			NULL_CHAR				'\0'
48 
49 #define			MAX_U_32_NUM			0xFFFFFFFF
50 #ifndef PI
51 #define			PI						3.14159265358979
52 #endif
53 #ifndef E
54 #define			E						2.71828182845
55 #endif
56 #define			PI2						PI/2
57 #define			PI4						PI/4
58 #define			PI64					PI/64
59 #define			LN_TO_LOG10				0.2302585093
60 
61 #define			BITS_IN_A_BYTE			8
62 #define			WORD					16
63 #define			MAX_NAME_SIZE			255
64 #define			SBLIMIT					32
65 #define			SSLIMIT					18
66 #define			FFT_SIZE				1024
67 #define			HAN_SIZE				512
68 #define			SCALE_BLOCK				12
69 #define			SCALE_RANGE				64
70 #define			SCALE					32768
71 #define			CRC16_POLYNOMIAL		0x8005
72 #define			CRC8_POLYNOMIAL			0x1D
73 
74 #define			MIN(A, B)		((A) < (B) ? (A) : (B))
75 #define			MAX(A, B)		((A) > (B) ? (A) : (B))
76 
77 
78 /* This is the smallest MNR a subband can have before it is counted
79    as 'noisy' by the logic which chooses the number of JS subbands */
80 
81 #define NOISY_MIN_MNR	0.0
82 
83 
84 /***************************************************************************************
85   Psychacoustic Model 1/3 Definitions
86 ****************************************************************************************/
87 
88 #define NOISE			10
89 #define TONE			20
90 #define DBMIN			-200.0
91 #define LAST			-1
92 #define STOP			-100
93 #define POWERNORM		90.3090 /* = 20 * log10(32768) to normalize */
94 /* max output power to 96 dB per spec */
95 
96 
97 /***************************************************************************************
98   Psychoacoustic Model 2/4 Definitions
99 ****************************************************************************************/
100 #define LXMIN			32.0
101 
102 
103 
104 /***************************************************************************************
105 psycho 0 mem struct
106 ****************************************************************************************/
107 
108 typedef struct psycho_0_mem_struct {
109     FLOAT ath_min[SBLIMIT];
110 } psycho_0_mem;
111 
112 
113 
114 /***************************************************************************************
115 psycho 1 mem struct
116 ****************************************************************************************/
117 #define DBTAB 1000
118 
119 typedef struct {
120     int line;
121     FLOAT bark, hear, x;
122 } g_thres, *g_ptr;
123 
124 typedef struct {
125     FLOAT x;
126     int type, next, map;
127 } mask, *mask_ptr;
128 
129 typedef struct psycho_1_mem_struct {
130     int off[2];
131     FLOAT fft_buf[2][1408];
132     int *cbound;
133     int crit_band;
134     int sub_size;
135     mask_ptr power;
136     g_ptr ltg;
137     FLOAT dbtable[DBTAB];
138 } psycho_1_mem;
139 
140 
141 
142 /***************************************************************************************
143 Psycho3 memory structure
144 ****************************************************************************************/
145 #define HBLKSIZE 513
146 
147 #define SUBSIZE 136
148 typedef struct psycho_3_mem_struct {
149     int off[2];
150     int freq_subset[SUBSIZE];
151     FLOAT bark[HBLKSIZE];
152     FLOAT ath[HBLKSIZE];
153     FLOAT fft_buf[2][1408];
154 #define CRITBANDMAX 32          /* this is much higher than it needs to be. really only about 24 */
155     int cbands;                 /* How many critical bands there really are */
156     int cbandindex[CRITBANDMAX];    /* The spectral line index of the start of each critical band */
157     FLOAT dbtable[DBTAB];
158 } psycho_3_mem;
159 
160 
161 
162 /***************************************************************************************
163 Psycho2 & 4 memory structure
164 ****************************************************************************************/
165 
166 #define LOGBLKSIZE		10
167 #define BLKSIZE			1024
168 #define HBLKSIZE		513
169 #define CBANDS			64
170 #define TRIGTABLESIZE	6284
171 #define TRIGTABLESCALE	2000.0
172 typedef int ICB[CBANDS];
173 typedef int IHBLK[HBLKSIZE];
174 typedef FLOAT F32[32];
175 typedef FLOAT F2_32[2][32];
176 typedef FLOAT FCB[CBANDS];
177 typedef FLOAT FCBCB[CBANDS][CBANDS];
178 typedef FLOAT FBLK[BLKSIZE];
179 typedef FLOAT FHBLK[HBLKSIZE];
180 typedef FLOAT F2HBLK[2][HBLKSIZE];
181 typedef FLOAT F22HBLK[2][2][HBLKSIZE];
182 typedef FLOAT DCB[CBANDS];
183 
184 typedef struct psycho_4_mem_struct {
185     int new;
186     int old;
187     int oldest;
188 
189     int flush;
190     int sync_flush;
191     int syncsize;
192 
193     FLOAT grouped_c[CBANDS];
194     FLOAT grouped_e[CBANDS];
195     FLOAT nb[CBANDS];
196     FLOAT cb[CBANDS];
197     FLOAT tb[CBANDS];
198     FLOAT ecb[CBANDS];
199     FLOAT bc[CBANDS];
200     FLOAT cbval[CBANDS];
201     FLOAT rnorm[CBANDS];
202     FLOAT wsamp_r[BLKSIZE], phi[BLKSIZE], energy[BLKSIZE], window[BLKSIZE];
203     FLOAT ath[HBLKSIZE], thr[HBLKSIZE], c[HBLKSIZE];
204     FLOAT fthr[HBLKSIZE], absthr[HBLKSIZE]; // psy2 only
205     int numlines[CBANDS];
206     int partition[HBLKSIZE];
207     FLOAT *tmn;
208     FCB *s;
209     FHBLK *lthr;
210     F2HBLK *r, *phi_sav;
211     FLOAT snrtmp[2][32];
212     FLOAT cos_table[TRIGTABLESIZE];
213 } psycho_4_mem, psycho_2_mem;
214 
215 
216 /***************************************************************************************
217  Subband utility structures
218 ****************************************************************************************/
219 
220 typedef struct subband_mem_struct {
221     FLOAT x[2][512];
222     FLOAT m[16][32];
223     int off[2];
224     int half[2];
225 } subband_mem;
226 
227 
228 
229 /***************************************************************************************
230  Header and frame information
231 ****************************************************************************************/
232 
233 /* Raw Header Information Structure */
234 typedef struct {
235     int version;
236     int lay;
237     int error_protection;
238     int bitrate_index;
239     int samplerate_idx;
240     int padding;
241     int private_bit;
242     int mode;
243     int mode_ext;
244     int copyright;
245     int original;
246     int emphasis;
247 } frame_header;
248 
249 
250 
251 typedef unsigned int subband_t[2][3][SCALE_BLOCK][SBLIMIT];
252 typedef FLOAT jsb_sample_t[3][SCALE_BLOCK][SBLIMIT];
253 typedef FLOAT sb_sample_t[2][3][SCALE_BLOCK][SBLIMIT];
254 
255 
256 
257 /***************************************************************************************
258  twolame Global Options structure.
259  Defaults shown in []
260  ++ means it is an advanced option. Only use it if you know what you're doing.
261 ****************************************************************************************/
262 struct twolame_options_struct {
263     // Input PCM audio File Information
264     int samplerate_in;          // mpeg1: 32000 [44100] 48000
265     // mpeg2: 16000 22050 24000
266     int samplerate_out;
267     int num_channels_in;        // Number of channels on the input stream
268     int num_channels_out;       // Number of channels on the output stream
269 
270     // Output MP2 File Information
271     TWOLAME_MPEG_version version;   // 0 mpeg2 [1] mpeg1
272     int bitrate;                // for mpeg1:32, 48, 56, 64, 80, 96,112,128,160,[192], 224, 256,
273     // 320, 384
274     // for mpeg2: 8, 16, 24, 32, 40, 48, 56, 64, 80, [96], 112, 128, 144, 160
275     TWOLAME_MPEG_mode mode;
276     TWOLAME_Padding padding;    // [PAD_NO]
277     int do_energy_levels;       // Write energy level information into the end of the frame [FALSE]
278     int num_ancillary_bits;     // Number of reserved ancillary bits [0] (Currently only available
279     // for non-VBR modes)
280 
281     // Psychoacoustic Model options
282     int psymodel;               // -1, 0, 1, 2, [3], 4 Psy model number
283     FLOAT athlevel;             // Adjust the Absolute Threshold of Hearing curve by [0] dB
284     int quickmode;              // Only calculate psy model ever X frames [FALSE]
285     int quickcount;             // Only calculate psy model every [10] frames
286 
287     // VBR Options
288     int vbr;                    // turn on VBR mode TRUE [FALSE]
289     int vbr_upper_index;        // ++ [0] means no upper bitrate set for VBR mode. valid 1-15
290     // depending on mode
291     int vbr_max_bitrate;
292     FLOAT vbrlevel;             // Set VBR quality. [0.0] (sensible range -10.0 -> 10.0)
293 
294     // Miscellaneous Options That Nobody Ever Uses
295     TWOLAME_Emphasis emphasis;  // [n]one, 5(50/15 microseconds), c(ccitt j.17)
296     int copyright;              // [FALSE]
297     int original;               // [FALSE]
298     int private_bit;            // [0] Your very own bit in the header.
299     int error_protection;       // [FALSE]
300 
301     // Digital Audio Broadcasting Extensions
302     unsigned int do_dab;        // Allocate space for the DigitalAudioBroadcasting info [FALSE]
303     unsigned int dab_crc_len;   // Number of CRC bytes for DAB [2], 4
304     unsigned int dab_crc[4];    // DAB CRC bytes are inserted here. User must insert them in frame
305     unsigned int dab_xpad_len;  // Number of bytes in the XPAD
306 
307     // Processing Options
308     int verbosity;              // Verbosity of output 0(never output a thing) [2] 100(output
309     // everything)
310 
311 
312     // Scaling
313     FLOAT scale;
314     FLOAT scale_left;
315     FLOAT scale_right;
316 
317 
318 
319     // Bit allocation stuff
320     int lower_index;
321     int upper_index;
322     int bitrateindextobits[15];
323     int vbr_frame_count;        // Used for debugging VBR
324 
325 
326     // Used by twolame_encode_frame
327     int twolame_init;
328     short int buffer[2][TWOLAME_SAMPLES_PER_FRAME]; // Sample buffer
329     unsigned int samples_in_buffer; // Number of samples currently in buffer
330     unsigned int psycount;
331     unsigned int num_crc_bits;  // Number of bits CRC is calculated on
332 
333     unsigned int bit_alloc[2][SBLIMIT];
334     unsigned int scfsi[2][SBLIMIT];
335     unsigned int scalar[2][3][SBLIMIT];
336     unsigned int j_scale[3][SBLIMIT];
337     FLOAT smrdef[2][32];
338     FLOAT smr[2][SBLIMIT];
339     FLOAT max_sc[2][SBLIMIT];
340 
341     subband_t *subband;
342     jsb_sample_t *j_sample;
343     sb_sample_t *sb_sample;
344 
345 
346 
347     /* Resampling stuff */
348     FLOAT resample_ratio;
349     void *resample_handle[2];
350 
351 
352     // memory for psycho models
353     psycho_0_mem *p0mem;
354     psycho_1_mem *p1mem;
355     psycho_2_mem *p2mem;
356     psycho_3_mem *p3mem;
357     psycho_4_mem *p4mem;
358 
359 
360     // memory for subband
361     subband_mem smem;
362 
363     // Frame info
364     frame_header header;
365     int jsbound;                // first band of joint stereo coding
366     int sblimit;                // total number of sub bands
367     int tablenum;
368 
369     int vbrstats[15];
370 };
371 
372 #endif                          // _COMMON_H
373 
374 
375 // vim:ts=4:sw=4:nowrap:
376