1 /*
2 	frame: Central data structures and opmitization hooks.
3 
4 	copyright 2007 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 	initially written by Thomas Orgis
7 */
8 
9 #ifndef MPG123_FRAME_H
10 #define MPG123_FRAME_H
11 
12 #include <stdio.h>
13 #include "config.h"
14 #include "mpg123.h"
15 #include "optimize.h"
16 #include "getcpuflags.h"
17 #include "id3.h"
18 #include "icy.h"
19 #include "reader.h"
20 #ifdef FRAME_INDEX
21 #include "index.h"
22 #endif
23 #include "synths.h"
24 
25 #ifdef OPT_DITHER
26 #include "dither.h"
27 int frame_dither_init(mpg123_handle *fr);
28 #endif
29 
30 /* max = 1728 */
31 #define MAXFRAMESIZE 3456
32 
33 struct al_table
34 {
35   short bits;
36   short d;
37 };
38 
39 /* the output buffer, used to be pcm_sample, pcm_point and audiobufsize */
40 struct outbuffer
41 {
42 	unsigned char *data; /* main data pointer, aligned */
43 	unsigned char *p; /* read pointer  */
44 	size_t fill; /* fill from read pointer */
45 	size_t size;
46 	unsigned char *rdata; /* unaligned base pointer */
47 };
48 
49 struct audioformat
50 {
51 	int encoding; /* Final encoding, after post-processing. */
52 	int encsize; /* Size of one sample in bytes, plain int should be fine here... */
53 	int dec_enc;  /* Encoding of decoder synth. */
54 	int dec_encsize; /* Size of one decoder sample. */
55 	int channels;
56 	long rate;
57 };
58 
59 void invalidate_format(struct audioformat *af);
60 
61 struct mpg123_pars_struct
62 {
63 	int verbose;    /* verbose level */
64 	long flags; /* combination of above */
65 #ifndef NO_NTOM
66 	long force_rate;
67 #endif
68 	int down_sample;
69 	int rva; /* (which) rva to do: 0: nothing, 1: radio/mix/track 2: album/audiophile */
70 	long halfspeed;
71 	long doublespeed;
72 	long timeout;
73 #define NUM_CHANNELS 2
74 	char audio_caps[NUM_CHANNELS][MPG123_RATES+1][MPG123_ENCODINGS];
75 /*	long start_frame; */ /* frame offset to begin with */
76 /*	long frame_number;*/ /* number of frames to decode */
77 #ifndef NO_ICY
78 	long icy_interval;
79 #endif
80 	double outscale;
81 	long resync_limit;
82 	long index_size; /* Long, because: negative values have a meaning. */
83 	long preframes;
84 #ifndef NO_FEEDER
85 	long feedpool;
86 	long feedbuffer;
87 #endif
88 	long freeformat_framesize;
89 };
90 
91 enum frame_state_flags
92 {
93 	 FRAME_ACCURATE      = 0x1  /**<     0001 Positions are considered accurate. */
94 	,FRAME_FRANKENSTEIN  = 0x2  /**<     0010 This stream is concatenated. */
95 	,FRAME_FRESH_DECODER = 0x4  /**<     0100 Decoder is fleshly initialized. */
96 	,FRAME_DECODER_LIVE  = 0x8  /**<     1000 Decoder can be used. */
97 };
98 
99 /* There is a lot to condense here... many ints can be merged as flags; though the main space is still consumed by buffers. */
100 struct mpg123_handle_struct
101 {
102 	int fresh; /* to be moved into flags */
103 	int new_format;
104 	real hybrid_block[2][2][SBLIMIT*SSLIMIT];
105 	int hybrid_blc[2];
106 	/* the scratch vars for the decoders, sometimes real, sometimes short... sometimes int/long */
107 	short *short_buffs[2][2];
108 	real *real_buffs[2][2];
109 	unsigned char *rawbuffs;
110 	int rawbuffss;
111 #ifdef OPT_I486
112 	int i486bo[2];
113 #endif
114 	int bo; /* Just have it always here. */
115 #ifdef OPT_DITHER
116 	int ditherindex;
117 	float *dithernoise;
118 #endif
119 	unsigned char* rawdecwin; /* the block with all decwins */
120 	int rawdecwins; /* size of rawdecwin memory */
121 	real *decwin; /* _the_ decode table */
122 #ifdef OPT_MMXORSSE
123 	/* I am not really sure that I need both of them... used in assembler */
124 	float *decwin_mmx;
125 	float *decwins;
126 #endif
127 #ifndef NO_EQUALIZER
128 	int have_eq_settings;
129 	real equalizer[2][32];
130 #endif
131 	/* for halfspeed mode */
132 	unsigned char ssave[34];
133 	int halfphase;
134 #ifndef NO_8BIT
135 	/* a raw buffer and a pointer into the middle for signed short conversion, only allocated on demand */
136 	unsigned char *conv16to8_buf;
137 	unsigned char *conv16to8;
138 #endif
139 	/* There's some possible memory saving for stuff that is not _really_ dynamic. */
140 
141 	/* layer3 */
142 	int longLimit[9][23];
143 	int shortLimit[9][14];
144 #ifdef REAL_IS_FIXED
145 	const real *gainpow2; // Actually static storage elsewhere.
146 #else
147 	real gainpow2[256+118+4]; /* not really dynamic, just different for mmx */
148 #endif
149 	/* layer2 */
150 	real muls[27][64];	/* also used by layer 1 */
151 
152 #ifndef NO_NTOM
153 	/* decode_ntom */
154 	unsigned long ntom_val[2];
155 	unsigned long ntom_step;
156 #endif
157 	/* special i486 fun */
158 #ifdef OPT_I486
159 	int *int_buffs[2][2];
160 #endif
161 	/* special altivec... */
162 #ifdef OPT_ALTIVEC
163 	real *areal_buffs[4][4];
164 #endif
165 	struct synth_s synths;
166 	struct
167 	{
168 #ifdef OPT_MULTI
169 
170 #ifndef NO_LAYER3
171 #if (defined OPT_3DNOW_VINTAGE || defined OPT_3DNOWEXT_VINTAGE || defined OPT_SSE || defined OPT_X86_64 || defined OPT_AVX || defined OPT_NEON || defined OPT_NEON64)
172 		void (*the_dct36)(real *,real *,real *,const real *,real *);
173 #endif
174 #endif
175 
176 #endif
177 		enum optdec type;
178 		enum optcla class;
179 	} cpu_opts;
180 #ifdef OPT_CPU_FLAGS
181 	struct cpuflags cpu_flags;
182 #endif
183 	int verbose;    /* 0: nothing, 1: just print chosen decoder, 2: be verbose */
184 
185 	const struct al_table *alloc;
186 	/* The runtime-chosen decoding, based on input and output format. */
187 	func_synth synth;
188 	func_synth_stereo synth_stereo;
189 	func_synth_mono synth_mono;
190 	/* Yes, this function is runtime-switched, too. */
191 	void (*make_decode_tables)(mpg123_handle *fr); /* That is the volume control. */
192 
193 	int stereo; /* I _think_ 1 for mono and 2 for stereo */
194 	int jsbound;
195 #define SINGLE_STEREO -1
196 #define SINGLE_LEFT    0
197 #define SINGLE_RIGHT   1
198 #define SINGLE_MIX     3
199 	int single;
200 	int II_sblimit;
201 	int down_sample_sblimit;
202 	int lsf; /* 0: MPEG 1.0; 1: MPEG 2.0/2.5 -- both used as bool and array index! */
203 	/* Many flags in disguise as integers... wasting bytes. */
204 	int mpeg25;
205 	int down_sample;
206 	int header_change;
207 	int lay;
208 	long spf; /* cached count of samples per frame */
209 	int (*do_layer)(mpg123_handle *);
210 	int error_protection;
211 	int bitrate_index;
212 	int sampling_frequency;
213 	int padding;
214 	int extension;
215 	int mode;
216 	int mode_ext;
217 	int copyright;
218 	int original;
219 	int emphasis;
220 	int framesize; /* computed framesize */
221 	int freesize;  /* free format frame size */
222 	enum mpg123_vbr vbr; /* 1 if variable bitrate was detected */
223 	off_t num; /* frame offset ... */
224 	off_t input_offset; /* byte offset of this frame in input stream */
225 	off_t playnum; /* playback offset... includes repetitions, reset at seeks */
226 	off_t audio_start; /* The byte offset in the file where audio data begins. */
227 	int state_flags;
228 	char silent_resync; /* Do not complain for the next n resyncs. */
229 	unsigned char* xing_toc; /* The seek TOC from Xing header. */
230 	int freeformat;
231 	long freeformat_framesize;
232 
233 	/* bitstream info; bsi */
234 	int bitindex;
235 	long bits_avail;
236 	unsigned char *wordpointer;
237 	/* temporary storage for getbits stuff */
238 	unsigned long ultmp;
239 	unsigned char uctmp;
240 
241 	/* rva data, used in common.c, set in id3.c */
242 
243 	double maxoutburst; /* The maximum amplitude in current sample represenation. */
244 	double lastscale;
245 	struct
246 	{
247 		int level[2];
248 		float gain[2];
249 		float peak[2];
250 	} rva;
251 
252 	/* input data */
253 	off_t track_frames;
254 	off_t track_samples;
255 	double mean_framesize;
256 	off_t mean_frames;
257 	int fsizeold;
258 	int ssize;
259 	unsigned int bitreservoir;
260 	unsigned char bsspace[2][MAXFRAMESIZE+512+4]; /* MAXFRAMESIZE */
261 	unsigned char *bsbuf;
262 	unsigned char *bsbufold;
263 	int bsnum;
264 	/* That is the header matching the last read frame body. */
265 	unsigned long oldhead;
266 	/* That is the header that is supposedly the first of the stream. */
267 	unsigned long firsthead;
268 	int abr_rate;
269 #ifdef FRAME_INDEX
270 	struct frame_index index;
271 #endif
272 
273 	/* output data */
274 	struct outbuffer buffer;
275 	struct audioformat af;
276 	int own_buffer;
277 	size_t outblock; /* number of bytes that this frame produces (upper bound) */
278 	int to_decode;   /* this frame holds data to be decoded */
279 	int to_ignore;   /* the same, somehow */
280 	off_t firstframe;  /* start decoding from here */
281 	off_t lastframe;   /* last frame to decode (for gapless or num_frames limit) */
282 	off_t ignoreframe; /* frames to decode but discard before firstframe */
283 #ifdef GAPLESS
284 	off_t gapless_frames; /* frame count for the gapless part */
285 	off_t firstoff; /* number of samples to ignore from firstframe */
286 	off_t lastoff;  /* number of samples to use from lastframe */
287 	off_t begin_s;  /* overall begin offset in samples */
288 	off_t begin_os;
289 	off_t end_s;    /* overall end offset in samples */
290 	off_t end_os;
291 	off_t fullend_os; /* gapless_frames translated to output samples */
292 #endif
293 	unsigned int crc; /* Well, I need a safe 16bit type, actually. But wider doesn't hurt. */
294 	struct reader *rd; /* pointer to the reading functions */
295 	struct reader_data rdat; /* reader data and state info */
296 	struct mpg123_pars_struct p;
297 	int err;
298 	int decoder_change;
299 	int delayed_change;
300 	long clip;
301 	/* the meta crap */
302 	int metaflags;
303 	unsigned char id3buf[128];
304 #ifndef NO_ID3V2
305 	mpg123_id3v2 id3v2;
306 #endif
307 	unsigned char *id3v2_raw;
308 	size_t id3v2_size;
309 #ifndef NO_ICY
310 	struct icy_meta icy;
311 #endif
312 	/*
313 		More variables needed for decoders, layerX.c.
314 		This time it is not about static variables but about the need for alignment which cannot be guaranteed on the stack by certain compilers (Sun Studio).
315 		We do not require the compiler to align stuff for our hand-written assembly. We only hope that it's able to align stuff for SSE and similar ops it generates itself.
316 	*/
317 	/*
318 		Those layer-specific structs could actually share memory, as they are not in use simultaneously. One might allocate on decoder switch, too.
319 		They all reside in one lump of memory (after each other), allocated to layerscratch.
320 	*/
321 	real *layerscratch;
322 #ifndef NO_LAYER1
323 	struct
324 	{
325 		real (*fraction)[SBLIMIT]; /* ALIGNED(16) real fraction[2][SBLIMIT]; */
326 	} layer1;
327 #endif
328 #ifndef NO_LAYER2
329 	struct
330 	{
331 		real (*fraction)[4][SBLIMIT]; /* ALIGNED(16) real fraction[2][4][SBLIMIT] */
332 	} layer2;
333 #endif
334 #ifndef NO_LAYER3
335 	/* These are significant chunks of memory already... */
336 	struct
337 	{
338 		real (*hybrid_in)[SBLIMIT][SSLIMIT];  /* ALIGNED(16) real hybridIn[2][SBLIMIT][SSLIMIT]; */
339 		real (*hybrid_out)[SSLIMIT][SBLIMIT]; /* ALIGNED(16) real hybridOut[2][SSLIMIT][SBLIMIT]; */
340 	} layer3;
341 #endif
342 	/* A place for storing additional data for the large file wrapper.
343 	   This is cruft! */
344 	void *wrapperdata;
345 	/* A callback used to properly destruct the wrapper data. */
346 	void (*wrapperclean)(void*);
347 	int enc_delay;
348 	int enc_padding;
349 #ifndef NO_MOREINFO
350 	struct mpg123_moreinfo *pinfo;
351 #endif
352 };
353 
354 /* generic init, does not include dynamic buffers */
355 void frame_init(mpg123_handle *fr);
356 void frame_init_par(mpg123_handle *fr, mpg123_pars *mp);
357 /* output buffer and format */
358 int  frame_outbuffer(mpg123_handle *fr);
359 int  frame_output_format(mpg123_handle *fr);
360 
361 int frame_buffers(mpg123_handle *fr); /* various decoder buffers, needed once */
362 int frame_reset(mpg123_handle* fr);   /* reset for next track */
363 int frame_buffers_reset(mpg123_handle *fr);
364 void frame_exit(mpg123_handle *fr);   /* end, free all buffers */
365 
366 /* Index functions... */
367 /* Well... print it... */
368 int mpg123_print_index(mpg123_handle *fr, FILE* out);
369 /* Find a seek position in index. */
370 off_t frame_index_find(mpg123_handle *fr, off_t want_frame, off_t* get_frame);
371 /* Apply index_size setting. */
372 int frame_index_setup(mpg123_handle *fr);
373 
374 void do_volume(mpg123_handle *fr, double factor);
375 void do_rva(mpg123_handle *fr);
376 
377 /* samples per frame ...
378 Layer I
379 Layer II
380 Layer III
381 MPEG-1
382 384
383 1152
384 1152
385 MPEG-2 LSF
386 384
387 1152
388 576
389 MPEG 2.5
390 384
391 1152
392 576
393 */
394 
395 // Well, I take that one for granted... at least layer3.
396 // The value is needed for mpg123_getstate() in any build.
397 #define GAPLESS_DELAY 529
398 #ifdef GAPLESS
399 void frame_gapless_init(mpg123_handle *fr, off_t framecount, off_t bskip, off_t eskip);
400 void frame_gapless_realinit(mpg123_handle *fr);
401 void frame_gapless_update(mpg123_handle *mh, off_t total_samples);
402 /*void frame_gapless_position(mpg123_handle* fr);
403 void frame_gapless_bytify(mpg123_handle *fr);
404 void frame_gapless_ignore(mpg123_handle *fr, off_t frames);*/
405 /* void frame_gapless_buffercheck(mpg123_handle *fr); */
406 #endif
407 
408 /* Number of samples the decoding of the current frame should yield. */
409 off_t frame_expect_outsamples(mpg123_handle *fr);
410 
411 /* Skip this frame... do some fake action to get away without actually decoding it. */
412 void frame_skip(mpg123_handle *fr);
413 
414 /*
415 	Seeking core functions:
416 	- convert input sample offset to output sample offset
417 	- convert frame offset to output sample offset
418 	- get leading frame offset for output sample offset
419 	The offsets are "unadjusted"/internal; resampling is being taken care of.
420 */
421 off_t frame_ins2outs(mpg123_handle *fr, off_t ins);
422 off_t frame_outs(mpg123_handle *fr, off_t num);
423 /* This one just computes the expected sample count for _this_ frame. */
424 off_t frame_expect_outsampels(mpg123_handle *fr);
425 off_t frame_offset(mpg123_handle *fr, off_t outs);
426 void frame_set_frameseek(mpg123_handle *fr, off_t fe);
427 void frame_set_seek(mpg123_handle *fr, off_t sp);
428 off_t frame_tell_seek(mpg123_handle *fr);
429 /* Take a copy of the Xing VBR TOC for fuzzy seeking. */
430 int frame_fill_toc(mpg123_handle *fr, unsigned char* in);
431 #endif
432