1 /*
2 	mpg123lib_intern: Common non-public stuff for libmpg123
3 
4 	copyright 1995-2008 by the mpg123 project - free software under the terms of the LGPL 2.1
5 	see COPYING and AUTHORS files in distribution or http://mpg123.org
6 
7 	derived from the old mpg123.h
8 */
9 
10 #ifndef MPG123_H_INTERN
11 #define MPG123_H_INTERN
12 
13 #define MPG123_RATES 9
14 #define MPG123_ENCODINGS 12
15 
16 #include "config.h" /* Load this before _anything_ */
17 #include "intsym.h" /* Prefixing of internal symbols that still are public in a static lib. */
18 
19 #include "abi_align.h"
20 
21 /* export DLL symbols */
22 #if defined(WIN32) && defined(DYNAMIC_BUILD)
23 #define BUILD_MPG123_DLL
24 #endif
25 #include "compat.h"
26 #include "mpg123.h"
27 
28 #define SKIP_JUNK 1
29 
30 #ifndef M_PI
31 # define M_PI       3.14159265358979323846
32 #endif
33 #ifndef M_SQRT2
34 # define M_SQRT2	1.41421356237309504880
35 #endif
36 
37 #ifdef SUNOS
38 #define memmove(dst,src,size) bcopy(src,dst,size)
39 #endif
40 
41 /* We don't really do long double... there are 3 options for REAL:
42    float, long and double. */
43 
44 #ifdef REAL_IS_FLOAT
45 #  define real float
46 #elif defined(REAL_IS_FIXED)
47 
48 # define real  int32_t
49 # define dreal int64_t
50 
51 /*
52   for fixed-point decoders, use pre-calculated tables to avoid expensive floating-point maths
53   undef this macro for run-time calculation
54 */
55 #define PRECALC_TABLES
56 
57 # define REAL_RADIX				24
58 # define REAL_FACTOR			16777216.0
59 
double_to_long_rounded(double x,double scalefac)60 static inline int32_t double_to_long_rounded(double x, double scalefac)
61 {
62 	x *= scalefac;
63 	x += (x > 0) ? 0.5 : -0.5;
64 	return (int32_t)x;
65 }
66 
scale_rounded(int32_t x,int shift)67 static inline int32_t scale_rounded(int32_t x, int shift)
68 {
69 	x += (x >> 31);
70 	x >>= (shift - 1);
71 	x += (x & 1);
72 	return (x >> 1);
73 }
74 
75 # ifdef __GNUC__
76 #  if defined(OPT_I386)
77 /* for i386_nofpu decoder */
78 #   define REAL_MUL_ASM(x, y, radix) \
79 ({ \
80 	long _x=(x), _y=(y); \
81 	__asm__ ( \
82 		"imull %1 \n\t" \
83 		"shrdl %2, %%edx, %0 \n\t" \
84 		: "+&a" (_x) \
85 		: "mr" (_y), "I" (radix) \
86 		: "%edx", "cc" \
87 	); \
88 	_x; \
89 })
90 
91 #   define REAL_MUL_SCALE_LAYER3_ASM(x, y, radix) \
92 ({ \
93 	long _x=(x), _y=(y), _radix=(radix); \
94 	__asm__ ( \
95 		"imull %1 \n\t" \
96 		"shrdl %%cl, %%edx, %0 \n\t" \
97 		: "+&a" (_x) \
98 		: "mr" (_y), "c" (_radix) \
99 		: "%edx", "cc" \
100 	); \
101 	_x; \
102 })
103 #  elif defined(OPT_PPC)
104 /* for powerpc */
105 #   define REAL_MUL_ASM(x, y, radix) \
106 ({ \
107 	long _x=(x), _y=(y), _mull, _mulh; \
108 	__asm__ ( \
109 		"mullw %0, %2, %3 \n\t" \
110 		"mulhw %1, %2, %3 \n\t" \
111 		"srwi %0, %0, %4 \n\t" \
112 		"rlwimi %0, %1, %5, 0, %6 \n\t" \
113 		: "=&r" (_mull), "=&r" (_mulh) \
114 		: "r" (_x), "r" (_y), "i" (radix), "i" (32-(radix)), "i" ((radix)-1) \
115 	); \
116 	_mull; \
117 })
118 
119 #   define REAL_MUL_SCALE_LAYER3_ASM(x, y, radix) \
120 ({ \
121 	long _x=(x), _y=(y), _radix=(radix), _mull, _mulh, _radix2; \
122 	__asm__ ( \
123 		"mullw %0, %3, %4 \n\t" \
124 		"mulhw %1, %3, %4 \n\t" \
125 		"subfic %2, %5, 32 \n\t" \
126 		"srw %0, %0, %5 \n\t" \
127 		"slw %1, %1, %2 \n\t" \
128 		"or %0, %0, %1 \n\t" \
129 		: "=&r" (_mull), "=&r" (_mulh), "=&r" (_radix2) \
130 		: "r" (_x), "r" (_y), "r" (_radix) \
131 		: "cc" \
132 	); \
133 	_mull; \
134 })
135 #  elif defined(OPT_ARM)
136 /* for arm */
137 #   define REAL_MUL_ASM(x, y, radix) \
138 ({ \
139 	long _x=(x), _y=(y), _mull, _mulh; \
140 	__asm__ ( \
141 		"smull %0, %1, %2, %3 \n\t" \
142 		"mov %0, %0, lsr %4 \n\t" \
143 		"orr %0, %0, %1, lsl %5 \n\t" \
144 		: "=&r" (_mull), "=&r" (_mulh) \
145 		: "r" (_x), "r" (_y), "M" (radix), "M" (32-(radix)) \
146 	); \
147 	_mull; \
148 })
149 
150 #   define REAL_MUL_SCALE_LAYER3_ASM(x, y, radix) \
151 ({ \
152 	long _x=(x), _y=(y), _radix=(radix), _mull, _mulh, _radix2; \
153 	__asm__ ( \
154 		"smull %0, %1, %3, %4 \n\t" \
155 		"mov %0, %0, lsr %5 \n\t" \
156 		"rsb %2, %5, #32 \n\t" \
157 		"mov %1, %1, lsl %2 \n\t" \
158 		"orr %0, %0, %1 \n\t" \
159 		: "=&r" (_mull), "=&r" (_mulh), "=&r" (_radix2) \
160 		: "r" (_x), "r" (_y), "r" (_radix) \
161 	); \
162 	_mull; \
163 })
164 #  endif
165 # endif
166 
167 /* I just changed the (int) to (real) there... seemed right. */
168 # define DOUBLE_TO_REAL(x)					(double_to_long_rounded(x, REAL_FACTOR))
169 # define DOUBLE_TO_REAL_15(x)				(double_to_long_rounded(x, 32768.0))
170 # define DOUBLE_TO_REAL_POW43(x)			(double_to_long_rounded(x, 8192.0))
171 # define DOUBLE_TO_REAL_SCALE_LAYER12(x)	(double_to_long_rounded(x, 1073741824.0))
172 # define DOUBLE_TO_REAL_SCALE_LAYER3(x, y)	(double_to_long_rounded(x, pow(2.0,gainpow2_scale[y])))
173 # define REAL_TO_DOUBLE(x)					((double)(x) / REAL_FACTOR)
174 # ifdef REAL_MUL_ASM
175 #  define REAL_MUL(x, y)					REAL_MUL_ASM(x, y, REAL_RADIX)
176 #  define REAL_MUL_15(x, y)					REAL_MUL_ASM(x, y, 15)
177 #  define REAL_MUL_SCALE_LAYER12(x, y)		REAL_MUL_ASM(x, y, 15 + 30 - REAL_RADIX)
178 # else
179 #  define REAL_MUL(x, y)					(((dreal)(x) * (dreal)(y)) >> REAL_RADIX)
180 #  define REAL_MUL_15(x, y)					(((dreal)(x) * (dreal)(y)) >> 15)
181 #  define REAL_MUL_SCALE_LAYER12(x, y)		(((dreal)(x) * (dreal)(y)) >> (15 + 30 - REAL_RADIX))
182 # endif
183 # ifdef REAL_MUL_SCALE_LAYER3_ASM
184 #  define REAL_MUL_SCALE_LAYER3(x, y, z)	REAL_MUL_SCALE_LAYER3_ASM(x, y, 13 + gainpow2_scale[z] - REAL_RADIX)
185 # else
186 #  define REAL_MUL_SCALE_LAYER3(x, y, z)	(((dreal)(x) * (dreal)(y)) >> (13 + gainpow2_scale[z] - REAL_RADIX))
187 # endif
188 # define REAL_SCALE_LAYER12(x)				((real)((x) >> (30 - REAL_RADIX)))
189 # define REAL_SCALE_LAYER3(x, y)			((real)((x) >> (gainpow2_scale[y] - REAL_RADIX)))
190 # ifdef ACCURATE_ROUNDING
191 #  define REAL_MUL_SYNTH(x, y)				REAL_MUL(x, y)
192 #  define REAL_SCALE_DCT64(x)				(x)
193 #  define REAL_SCALE_WINDOW(x)				(x)
194 # else
195 #  define REAL_MUL_SYNTH(x, y)				((x) * (y))
196 #  define REAL_SCALE_DCT64(x)				((x) >> 8)
197 #  define REAL_SCALE_WINDOW(x)				scale_rounded(x, 16)
198 # endif
199 
200 #else
201 /* Just define a symbol to make things clear.
202    Existing code still uses (not (float or fixed)) for that. */
203 #  define REAL_IS_DOUBLE
204 #  define real double
205 #endif
206 
207 #ifndef REAL_IS_FIXED
208 # if (defined SIZEOF_INT32_T) && (SIZEOF_INT32_T != 4)
209 #  error "Bad 32bit types!!!"
210 # endif
211 #endif
212 
213 #ifndef DOUBLE_TO_REAL
214 # define DOUBLE_TO_REAL(x)					(real)(x)
215 #endif
216 #ifndef DOUBLE_TO_REAL_15
217 # define DOUBLE_TO_REAL_15(x)				(real)(x)
218 #endif
219 #ifndef DOUBLE_TO_REAL_POW43
220 # define DOUBLE_TO_REAL_POW43(x)			(real)(x)
221 #endif
222 #ifndef DOUBLE_TO_REAL_SCALE_LAYER12
223 # define DOUBLE_TO_REAL_SCALE_LAYER12(x)	(real)(x)
224 #endif
225 #ifndef DOUBLE_TO_REAL_SCALE_LAYER3
226 # define DOUBLE_TO_REAL_SCALE_LAYER3(x, y)	(real)(x)
227 #endif
228 #ifndef REAL_TO_DOUBLE
229 # define REAL_TO_DOUBLE(x)					(x)
230 #endif
231 
232 #ifndef REAL_MUL
233 # define REAL_MUL(x, y)						((x) * (y))
234 #endif
235 #ifndef REAL_MUL_SYNTH
236 # define REAL_MUL_SYNTH(x, y)				((x) * (y))
237 #endif
238 #ifndef REAL_MUL_15
239 # define REAL_MUL_15(x, y)					((x) * (y))
240 #endif
241 #ifndef REAL_MUL_SCALE_LAYER12
242 # define REAL_MUL_SCALE_LAYER12(x, y)		((x) * (y))
243 #endif
244 #ifndef REAL_MUL_SCALE_LAYER3
245 # define REAL_MUL_SCALE_LAYER3(x, y, z)		((x) * (y))
246 #endif
247 #ifndef REAL_SCALE_LAYER12
248 # define REAL_SCALE_LAYER12(x)				(x)
249 #endif
250 #ifndef REAL_SCALE_LAYER3
251 # define REAL_SCALE_LAYER3(x, y)			(x)
252 #endif
253 #ifndef REAL_SCALE_DCT64
254 # define REAL_SCALE_DCT64(x)				(x)
255 #endif
256 
257 /* used to be: AUDIOBUFSIZE = n*64 with n=1,2,3 ...
258    now: factor on minimum frame buffer size (which takes upsampling into account) */
259 #define		AUDIOBUFSIZE		2
260 
261 #include "true.h"
262 
263 #define         MAX_NAME_SIZE           81
264 #define         SBLIMIT                 32
265 #define         SCALE_BLOCK             12
266 #define         SSLIMIT                 18
267 
268 /* Same as MPG_M_* */
269 #define         MPG_MD_STEREO           0
270 #define         MPG_MD_JOINT_STEREO     1
271 #define         MPG_MD_DUAL_CHANNEL     2
272 #define         MPG_MD_MONO             3
273 
274 /* We support short or float output samples...
275    Short integer amplitude is scaled by this. */
276 #define SHORT_SCALE 32768
277 /* That scales a short-scaled value to a 32bit integer scaled one
278    value = 2**31/2**15 */
279 #define S32_RESCALE 65536
280 
281 /* Pre Shift fo 16 to 8 bit converter table */
282 #define AUSHIFT (3)
283 
284 #include "optimize.h"
285 #include "decode.h"
286 #include "parse.h"
287 #include "frame.h"
288 
289 /* fr is a mpg123_handle* by convention here... */
290 #define NOQUIET  (!(fr->p.flags & MPG123_QUIET))
291 #define VERBOSE  (NOQUIET && fr->p.verbose)
292 #define VERBOSE2 (NOQUIET && fr->p.verbose > 1)
293 #define VERBOSE3 (NOQUIET && fr->p.verbose > 2)
294 #define VERBOSE4 (NOQUIET && fr->p.verbose > 3)
295 #define PVERB(mp, level) (!((mp)->flags & MPG123_QUIET) && (mp)->verbose >= (level))
296 
297 int decode_update(mpg123_handle *mh);
298 /* residing in format.c  */
299 off_t decoder_synth_bytes(mpg123_handle *fr , off_t s);
300 off_t samples_to_bytes(mpg123_handle *fr , off_t s);
301 off_t bytes_to_samples(mpg123_handle *fr , off_t b);
302 off_t outblock_bytes(mpg123_handle *fr, off_t s);
303 /* Postprocessing format conversion of freshly decoded buffer. */
304 void postprocess_buffer(mpg123_handle *fr);
305 
306 int open_fixed_pre(mpg123_handle *mh, int channels, int encoding);
307 int open_fixed_post(mpg123_handle *mh, int channels, int encoding);
308 
309 /* If networking is enabled and we really mean internal networking, the timeout_read function is available. */
310 #if defined (NETWORK) && !defined (WANT_WIN32_SOCKETS)
311 /* Does not work with win32 */
312 #define TIMEOUT_READ
313 #endif
314 
315 #endif
316