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