xref: /plan9/sys/src/games/mp3enc/pcm.h (revision 8f5875f3e9b20916b4c52ad4336922bc8653eb7b)
1 /*{{{ #defines                        */
2 
3 #include <limits.h>
4 #include "lame.h"
5 #include "util.h"
6 
7 #define ENCDELAY      576
8 #define MDCTDELAY     48
9 #define BLKSIZE       1024
10 #define HBLKSIZE      (BLKSIZE/2 + 1)
11 #define BLKSIZE_s     256
12 #define HBLKSIZE_s    (BLKSIZE_s/2 + 1)
13 #define MAX_TABLES    1002
14 #define TAPS          32
15 #define WINDOW_SIZE   15.5
16 #define WINDOW        hanning
17 #define inline        __inline
18 
19 #define MIN(a,b)      ((a) < (b) ? (a) : (b))
20 #define MAX(a,b)      ((a) > (b) ? (a) : (b))
21 
22 #ifndef M_PIl
23 # define M_PIl          3.1415926535897932384626433832795029L
24 #endif
25 
26 #define SIN             sin
27 #define COS             cos
28 
29 /*}}}*/
30 /*{{{ object ID's                     */
31 
32 #define RESAMPLE_ID     0x52455341LU
33 #define PSYCHO_ID       0x50535943LU
34 #define BITSTREAM_ID    0x42495453LU
35 
36 /*}}}*/
37 /*{{{ typedef's                       */
38 
39 typedef float         float32_t;                   // IEEE-754 32 bit floating point
40 typedef double        float64_t;                   // IEEE-754 64 bit floating point
41 typedef long double   float80_t;                   // IEEE-854 80 bit floating point, if available
42 typedef long double   float_t;                     // temporarly results of float operations
43 typedef long double   double_t;                    // temporarly results of double operations
44 typedef long double   longdouble_t;                // temporarly results of long double operations
45 
46 typedef float_t (*scalar_t)  ( const sample_t* p, const sample_t* q );
47 typedef float_t (*scalarn_t) ( const sample_t* p, const sample_t* q, size_t len );
48 
49 /*}}}*/
50 /*{{{ data direction attributes       */
51 
52 /*
53  * These are data stream direction attributes like used in Ada83/Ada95 and in RPC
54  * The data direction is seen from the caller to the calling function.
55  * Examples:
56  *
57  *    size_t  fread  ( void INOUT* buffer, size_t items, size_t itemsize, FILE INOUT* fp );
58  *    size_t  fwrite ( void OUT  * buffer, size_t items, size_t itemsize, FILE INOUT* fp );
59  *    size_t  memset ( void IN   * buffer, unsigned char value, size_t size );
60  *
61  * Return values are implizit IN (note that here C uses the opposite attribute).
62  * Arguments not transmitted via references are implizit OUT.
63  */
64 
65 #define OUT            /* [out]   */ const
66 #define INOUT          /* [inout] */
67 #define IN             /* [in]    */
68 #define OUTTR          /* [out]: data is modified like [inout], but you don't get any useful back */
69 
70 /*}}}*/
71 /*{{{ Test some error conditions      */
72 
73 #ifndef __LOC__
74 # define _STR2(x)      #x
75 # define _STR1(x)      _STR2(x)
76 # define __LOC__       __FILE__ "(" _STR1(__LINE__) ") : warning: "
77 #endif
78 
79 /* The current code doesn't work on machines with non 8 bit char's in any way, so abort */
80 
81 #if CHAR_BIT != 8
82 # pragma message ( __LOC__ "Machines with CHAR_BIT != 8 not yet supported" )
83 # pragma error
84 #endif
85 
86 /*}}}*/
87 
88 /*
89  *  Now some information how PCM data can be specified. PCM data
90  *  is specified by 3 attributes: pointer, length information
91  *  and attributes.
92  *    - Audio is always stored in 2D arrays, which are collapsing to 1D
93  *      in the case of monaural input
94  *    - 2D arrays can be stored as 2D arrays or as pointers to 1D arrays.
95  *    - 2D data can be stored as samples*channels or as channels*samples
96  *    - This gives 4 kinds of storing PCM data:
97  *      + pcm [samples][channels]       (LAME_INTERLEAVED)
98  *      + pcm [channels][samples]       (LAME_CHAINED)
99  *      + (*pcm) [samples]              (LAME_INDIRECT)
100  *      + (*pcm) [channels]
101  *    - The last I have never seen and it have a huge overhead (67% ... 200%),
102  *      so the first three are implemented.
103  *    - The monaural 1D cases can also be handled by the first two attributes
104  */
105 
106 #define  LAME_INTERLEAVED       0x10000000
107 #define  LAME_CHAINED           0x20000000
108 #define  LAME_INDIRECT          0x30000000
109 
110 /*
111  *  Now we need some information about the byte order of the data.
112  *  There are 4 cases possible (if you are not fully support such strange
113  *  Machines like the PDPs):
114  *    - You know the absolute byte order of the data (LAME_LITTLE_ENDIAN, LAME_BIG_ENDIAN)
115  *    - You know the byte order from the view of the current machine
116  *      (LAME_NATIVE_ENDIAN, LAME_OPPOSITE_ENDIAN)
117  *    - The use of LAME_OPPOSITE_ENDIAN is NOT recommended because it is
118  *      is a breakable attribute, use LAME_LITTLE_ENDIAN or LAME_BIG_ENDIAN
119  *      instead
120  */
121 
122 #define  LAME_NATIVE_ENDIAN     0x00000000
123 #define  LAME_OPPOSITE_ENDIAN   0x01000000
124 #define  LAME_LITTLE_ENDIAN     0x02000000
125 #define  LAME_BIG_ENDIAN        0x03000000
126 
127 /*
128  *  The next attribute is the data type of the input data.
129  *  There are currently 2 kinds of input data:
130  *    - C based:
131  *          LAME_{SHORT,INT,LONG}
132  *          LAME_{FLOAT,DOUBLE,LONGDOUBLE}
133  *    - Binary representation based:
134  *          LAME_{UINT,INT}{8,16,24,32}
135  *          LAME_{A,U}LAW
136  *          LAME_FLOAT{32,64,80_ALIGN{2,4,8}}
137  *
138  *  Don't use the C based for external data.
139  */
140 
141 #define  LAME_SILENCE           0x00010000
142 #define  LAME_UINT8             0x00020000
143 #define  LAME_INT8              0x00030000
144 #define  LAME_UINT16            0x00040000
145 #define  LAME_INT16             0x00050000
146 #define  LAME_UINT24            0x00060000
147 #define  LAME_INT24             0x00070000
148 #define  LAME_UINT32            0x00080000
149 #define  LAME_INT32             0x00090000
150 #define  LAME_FLOAT32           0x00140000
151 #define  LAME_FLOAT64           0x00180000
152 #define  LAME_FLOAT80_ALIGN2    0x001A0000
153 #define  LAME_FLOAT80_ALIGN4    0x001C0000
154 #define  LAME_FLOAT80_ALIGN8    0x00200000
155 #define  LAME_SHORT             0x00210000
156 #define  LAME_INT               0x00220000
157 #define  LAME_LONG              0x00230000
158 #define  LAME_FLOAT             0x00240000
159 #define  LAME_DOUBLE            0x00250000
160 #define  LAME_LONGDOUBLE        0x00260000
161 #define  LAME_ALAW              0x00310000
162 #define  LAME_ULAW              0x00320000
163 
164 /*
165  *  The last attribute is the number of input channels. Currently
166  *  1...65535 channels are possible, but only 1 and 2 are supported.
167  *  So matrixing or MPEG-2 MultiChannelSupport are not a big problem.
168  *
169  *  Note: Some people use the word 'stereo' for 2 channel stereophonic sound.
170  *        But stereophonic sound is a collection of ALL methods to restore the
171  *        stereophonic sound field starting from 2 channels up to audio
172  *        holography.
173  */
174 
175 #define LAME_MONO               0x00000001
176 #define LAME_STEREO             0x00000002
177 #define LAME_STEREO_2_CHANNELS  0x00000002
178 #define LAME_STEREO_3_CHANNELS  0x00000003
179 #define LAME_STEREO_4_CHANNELS  0x00000004
180 #define LAME_STEREO_5_CHANNELS  0x00000005
181 #define LAME_STEREO_6_CHANNELS  0x00000006
182 #define LAME_STEREO_7_CHANNELS  0x00000007
183 #define LAME_STEREO_65535_CHANNELS\
184                                 0x0000FFFF
185 
186 
187 extern scalar_t   scalar4;
188 extern scalar_t   scalar8;
189 extern scalar_t   scalar12;
190 extern scalar_t   scalar16;
191 extern scalar_t   scalar20;
192 extern scalar_t   scalar24;
193 extern scalar_t   scalar32;
194 extern scalarn_t  scalar4n;
195 extern scalarn_t  scalar1n;
196 
197 float_t  scalar04_float32_i387  ( const float32_t* p, const float32_t* q );
198 float_t  scalar08_float32_i387  ( const float32_t* p, const float32_t* q );
199 float_t  scalar12_float32_i387  ( const float32_t* p, const float32_t* q );
200 float_t  scalar16_float32_i387  ( const float32_t* p, const float32_t* q );
201 float_t  scalar20_float32_i387  ( const float32_t* p, const float32_t* q );
202 float_t  scalar24_float32_i387  ( const float32_t* p, const float32_t* q );
203 float_t  scalar32_float32_i387  ( const float32_t* p, const float32_t* q );
204 float_t  scalar4n_float32_i387  ( const float32_t* p, const float32_t* q, const size_t len );
205 float_t  scalar1n_float32_i387  ( const float32_t* p, const float32_t* q, const size_t len );
206 
207 float_t  scalar04_float32_3DNow ( const float32_t* p, const float32_t* q );
208 float_t  scalar08_float32_3DNow ( const float32_t* p, const float32_t* q );
209 float_t  scalar12_float32_3DNow ( const float32_t* p, const float32_t* q );
210 float_t  scalar16_float32_3DNow ( const float32_t* p, const float32_t* q );
211 float_t  scalar20_float32_3DNow ( const float32_t* p, const float32_t* q );
212 float_t  scalar24_float32_3DNow ( const float32_t* p, const float32_t* q );
213 float_t  scalar32_float32_3DNow ( const float32_t* p, const float32_t* q );
214 float_t  scalar4n_float32_3DNow ( const float32_t* p, const float32_t* q, const size_t len );
215 float_t  scalar1n_float32_3DNow ( const float32_t* p, const float32_t* q, const size_t len );
216 
217 float_t  scalar04_float32_SIMD  ( const float32_t* p, const float32_t* q );
218 float_t  scalar08_float32_SIMD  ( const float32_t* p, const float32_t* q );
219 float_t  scalar12_float32_SIMD  ( const float32_t* p, const float32_t* q );
220 float_t  scalar16_float32_SIMD  ( const float32_t* p, const float32_t* q );
221 float_t  scalar20_float32_SIMD  ( const float32_t* p, const float32_t* q );
222 float_t  scalar24_float32_SIMD  ( const float32_t* p, const float32_t* q );
223 float_t  scalar32_float32_SIMD  ( const float32_t* p, const float32_t* q );
224 float_t  scalar4n_float32_SIMD  ( const float32_t* p, const float32_t* q, const size_t len );
225 float_t  scalar1n_float32_SIMD  ( const float32_t* p, const float32_t* q, const size_t len );
226 
227 float_t  scalar04_float32       ( const float32_t* p, const float32_t* q );
228 float_t  scalar08_float32       ( const float32_t* p, const float32_t* q );
229 float_t  scalar12_float32       ( const float32_t* p, const float32_t* q );
230 float_t  scalar16_float32       ( const float32_t* p, const float32_t* q );
231 float_t  scalar20_float32       ( const float32_t* p, const float32_t* q );
232 float_t  scalar24_float32       ( const float32_t* p, const float32_t* q );
233 float_t  scalar32_float32       ( const float32_t* p, const float32_t* q );
234 float_t  scalar4n_float32       ( const float32_t* p, const float32_t* q, const size_t len );
235 float_t  scalar1n_float32       ( const float32_t* p, const float32_t* q, const size_t len );
236 
237 
238 /*{{{ some prototypes                 */
239 
240 resample_t*  resample_open (
241         OUT long double  sampfreq_in,    // [Hz]
242         OUT long double  sampfreq_out,   // [Hz]
243         OUT double       lowpass_freq,   // [Hz] or <0 for auto mode
244         OUT int          quality );      // Proposal: 0 default, 1 sample select, 2 linear interpol, 4 4-point interpolation, 32 32-point interpolation
245 
246 int  resample_buffer (                          // return code, 0 for success
247         INOUT resample_t *const   r,            // internal structure
248         IN    sample_t   *const   out,          // where to write the output data
249         INOUT size_t     *const   out_req_len,  // requested output data len/really written output data len
250         OUT   sample_t   *const   in,           // where are the input data?
251         INOUT size_t     *const   in_avail_len, // available input data len/consumed input data len
252         OUT   size_t              channel );    // number of the channel (needed for buffering)
253 
254 int  resample_close ( INOUT resample_t* const r );
255 
256 void         init_scalar_functions   ( OUT lame_t* const lame );
257 long double  unround_samplefrequency ( OUT long double freq );
258 
259 #if 0
260 int  lame_encode_mp3_frame (             // return code, 0 for success
261         INOUT lame_global_flags*,        // internal context structure
262         OUTTR sample_t * inbuf_l,        // data for left  channel
263         OUTTR sample_t * inbuf_r,        // data for right channel
264         IN    uint8_t  * mp3buf,         // where to write the coded data
265         OUT   size_t     mp3buf_size );  // maximum size of coded data
266 #endif
267 
268 int  lame_encode_ogg_frame (             // return code, 0 for success
269         INOUT lame_global_flags*,        // internal context structure
270         OUT   sample_t * inbuf_l,        // data for left  channel
271         OUT   sample_t * inbuf_r,        // data for right channel
272         IN    uint8_t  * mp3buf,         // where to write the coded data
273         OUT   size_t     mp3buf_size );  // maximum size of coded data
274 
275 /*}}}*/
276 
277 /* end of pcm.h */
278