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