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