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