/*1frame: Central data structures and opmitization hooks.23copyright 2007-2023 by the mpg123 project - free software under the terms of the LGPL 2.14see COPYING and AUTHORS files in distribution or http://mpg123.org5initially written by Thomas Orgis6*/78#ifndef MPG123_FRAME_H9#define MPG123_FRAME_H1011#include <stdio.h>12#include "config.h"13#include "mpg123.h"14#include "optimize.h"15#include "getcpuflags.h"16#include "id3.h"17#include "icy.h"18#include "reader.h"19#ifdef FRAME_INDEX20#include "index.h"21#endif22#include "synths.h"2324#ifdef OPT_DITHER25#include "dither.h"26int INT123_frame_dither_init(mpg123_handle *fr);27#endif2829/* max = 1728 */30#define MAXFRAMESIZE 34563132struct al_table33{34short bits;35short d;36};3738/* the output buffer, used to be pcm_sample, pcm_point and audiobufsize */39struct outbuffer40{41unsigned char *data; /* main data pointer, aligned */42unsigned char *p; /* read pointer */43size_t fill; /* fill from read pointer */44size_t size;45unsigned char *rdata; /* unaligned base pointer */46};4748struct audioformat49{50int encoding; /* Final encoding, after post-processing. */51int encsize; /* Size of one sample in bytes, plain int should be fine here... */52int dec_enc; /* Encoding of decoder synth. */53int dec_encsize; /* Size of one decoder sample. */54int channels;55long rate;56};5758void INT123_invalidate_format(struct audioformat *af);5960struct mpg123_pars_struct61{62int verbose; /* verbose level */63long flags; /* combination of above */64#ifndef NO_NTOM65long force_rate;66#endif67int down_sample;68int rva; /* (which) rva to do: 0: nothing, 1: radio/mix/track 2: album/audiophile */69long halfspeed;70long doublespeed;71long timeout;72#define NUM_CHANNELS 273char 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_ICY77long icy_interval;78#endif79double outscale;80long resync_limit;81long index_size; /* Long, because: negative values have a meaning. */82long preframes;83#ifndef NO_FEEDER84long feedpool;85long feedbuffer;86#endif87long freeformat_framesize;88};8990enum frame_state_flags91{92FRAME_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,FRAME_DECODER_LIVE = 0x8 /**< 1000 Decoder can be used. */96};9798// separate frame header structure for safe decoding of headers without99// modifying the main frame struct before we are sure that we can read a100// frame into it101struct frame_header102{103int lay;104// lots of flags that could share storage, should reform that105int lsf; /* 0: MPEG 1.0; 1: MPEG 2.0/2.5 -- both used as bool and array index! */106int mpeg25;107int error_protection;108int bitrate_index;109int sampling_frequency;110int padding;111int extension;112int mode;113int mode_ext;114int copyright;115int original;116int emphasis;117// Even 16 bit int is enough for MAXFRAMESIZE118int framesize; /* computed framesize */119int freeformat;120int freeformat_framesize;121// Derived from header and checked against the above.122int ssize;123};124125/* There is a lot to condense here... many ints can be merged as flags; though the main space is still consumed by buffers. */126struct mpg123_handle_struct127{128int fresh; /* to be moved into flags */129int new_format;130real hybrid_block[2][2][SBLIMIT*SSLIMIT];131int hybrid_blc[2];132/* the scratch vars for the decoders, sometimes real, sometimes short... sometimes int/long */133short *short_buffs[2][2];134real *real_buffs[2][2];135unsigned char *rawbuffs;136int rawbuffss;137#ifdef OPT_I486138int i486bo[2];139#endif140int bo; /* Just have it always here. */141#ifdef OPT_DITHER142int ditherindex;143float *dithernoise;144#endif145unsigned char* rawdecwin; /* the block with all decwins */146int rawdecwins; /* size of rawdecwin memory */147real *decwin; /* _the_ decode table */148#ifdef OPT_MMXORSSE149/* I am not really sure that I need both of them... used in assembler */150float *decwin_mmx;151float *decwins;152#endif153#ifndef NO_EQUALIZER154int have_eq_settings;155real equalizer[2][32];156#endif157/* for halfspeed mode */158unsigned char ssave[34];159int halfphase;160#ifndef NO_8BIT161/* a raw buffer and a pointer into the middle for signed short conversion, only allocated on demand */162unsigned char *conv16to8_buf;163unsigned char *conv16to8;164#endif165/* There's some possible memory saving for stuff that is not _really_ dynamic. */166167/* layer3 */168int longLimit[9][23];169int shortLimit[9][14];170#ifdef REAL_IS_FIXED171const real *gainpow2; // Actually static storage elsewhere.172#else173real gainpow2[256+118+4]; /* not really dynamic, just different for mmx */174#endif175/* layer2 */176real muls[27][64]; /* also used by layer 1 */177178#ifndef NO_NTOM179/* decode_ntom */180unsigned long INT123_ntom_val[2];181unsigned long ntom_step;182#endif183/* special i486 fun */184#ifdef OPT_I486185int *int_buffs[2][2];186#endif187/* special altivec... */188#ifdef OPT_ALTIVEC189real *areal_buffs[4][4];190#endif191struct synth_s synths;192struct193{194#ifdef OPT_MULTI195196#ifdef OPT_THE_DCT36197void (*the_dct36)(real *,real *,real *,const real *,real *);198#endif199200#endif201enum optdec type;202enum optcla class;203} cpu_opts;204#ifdef OPT_CPU_FLAGS205struct cpuflags cpu_flags;206#endif207int verbose; /* 0: nothing, 1: just print chosen decoder, 2: be verbose */208209const struct al_table *alloc;210/* The runtime-chosen decoding, based on input and output format. */211func_synth synth;212func_synth_stereo synth_stereo;213func_synth_mono synth_mono;214/* Yes, this function is runtime-switched, too. */215void (*INT123_make_decode_tables)(mpg123_handle *fr); /* That is the volume control. */216217int stereo; /* I _think_ 1 for mono and 2 for stereo */218int jsbound;219#define SINGLE_STEREO -1220#define SINGLE_LEFT 0221#define SINGLE_RIGHT 1222#define SINGLE_MIX 3223int single;224int II_sblimit;225int down_sample_sblimit;226/* Many flags in disguise as integers... wasting bytes. */227int down_sample;228int header_change;229struct frame_header hdr;230long spf; /* cached count of samples per frame */231int (*do_layer)(mpg123_handle *);232enum mpg123_vbr vbr; /* 1 if variable bitrate was detected */233int64_t num; /* frame offset ... */234int64_t input_offset; /* byte offset of this frame in input stream */235int64_t playnum; /* playback offset... includes repetitions, reset at seeks */236int64_t audio_start; /* The byte offset in the file where audio data begins. */237int state_flags;238char silent_resync; /* Do not complain for the next n resyncs. */239unsigned char* xing_toc; /* The seek TOC from Xing header. */240241/* bitstream info; bsi */242int bitindex;243long bits_avail;244unsigned char *wordpointer;245/* temporary storage for getbits stuff */246unsigned long ultmp;247unsigned char uctmp;248249/* rva data, used in common.c, set in id3.c */250251double maxoutburst; /* The maximum amplitude in current sample represenation. */252double lastscale;253struct254{255int level[2];256float gain[2];257float peak[2];258} rva;259260/* input data */261int64_t track_frames;262int64_t track_samples;263double mean_framesize;264int64_t mean_frames;265int fsizeold;266unsigned int bitreservoir;267unsigned char bsspace[2][MAXFRAMESIZE+512+4]; /* MAXFRAMESIZE */268unsigned char *bsbuf;269unsigned char *bsbufold;270int bsnum;271/* That is the header matching the last read frame body. */272unsigned long oldhead;273/* That is the header that is supposedly the first of the stream. */274unsigned long firsthead;275int abr_rate;276#ifdef FRAME_INDEX277struct frame_index index;278#endif279280/* output data */281struct outbuffer buffer;282struct audioformat af;283int own_buffer;284size_t outblock; /* number of bytes that this frame produces (upper bound) */285int to_decode; /* this frame holds data to be decoded */286int to_ignore; /* the same, somehow */287int64_t firstframe; /* start decoding from here */288int64_t lastframe; /* last frame to decode (for gapless or num_frames limit) */289int64_t ignoreframe; /* frames to decode but discard before firstframe */290#ifdef GAPLESS291int64_t gapless_frames; /* frame count for the gapless part */292int64_t firstoff; /* number of samples to ignore from firstframe */293int64_t lastoff; /* number of samples to use from lastframe */294int64_t begin_s; /* overall begin offset in samples */295int64_t begin_os;296int64_t end_s; /* overall end offset in samples */297int64_t end_os;298int64_t fullend_os; /* gapless_frames translated to output samples */299#endif300unsigned int crc; /* Well, I need a safe 16bit type, actually. But wider doesn't hurt. */301struct reader *rd; /* pointer to the reading functions */302struct reader_data rdat; /* reader data and state info */303struct mpg123_pars_struct p;304int err;305int decoder_change;306int delayed_change;307long clip;308/* the meta crap */309int metaflags;310unsigned char id3buf[128];311#ifndef NO_ID3V2312mpg123_id3v2 id3v2;313#endif314unsigned char *id3v2_raw;315size_t id3v2_size;316#ifndef NO_ICY317struct icy_meta icy;318#endif319/*320More variables needed for decoders, layerX.c.321This 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).322We 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.323*/324/*325Those layer-specific structs could actually share memory, as they are not in use simultaneously. One might allocate on decoder switch, too.326They all reside in one lump of memory (after each other), allocated to layerscratch.327*/328real *layerscratch;329#ifndef NO_LAYER1330struct331{332real (*fraction)[SBLIMIT]; /* ALIGNED(16) real fraction[2][SBLIMIT]; */333} layer1;334#endif335#ifndef NO_LAYER2336struct337{338real (*fraction)[4][SBLIMIT]; /* ALIGNED(16) real fraction[2][4][SBLIMIT] */339} layer2;340#endif341#ifndef NO_LAYER3342/* These are significant chunks of memory already... */343struct344{345real (*hybrid_in)[SBLIMIT][SSLIMIT]; /* ALIGNED(16) real hybridIn[2][SBLIMIT][SSLIMIT]; */346real (*hybrid_out)[SSLIMIT][SBLIMIT]; /* ALIGNED(16) real hybridOut[2][SSLIMIT][SBLIMIT]; */347} layer3;348#endif349/* A place for storing additional data for the large file wrapper.350This is cruft! */351void *wrapperdata;352int enc_delay;353int enc_padding;354#ifndef NO_MOREINFO355struct mpg123_moreinfo *pinfo;356#endif357};358359/* generic init, does not include dynamic buffers */360void INT123_frame_init(mpg123_handle *fr);361void INT123_frame_init_par(mpg123_handle *fr, mpg123_pars *mp);362/* output buffer and format */363int INT123_frame_outbuffer(mpg123_handle *fr);364int INT123_frame_output_format(mpg123_handle *fr);365366int INT123_frame_buffers(mpg123_handle *fr); /* various decoder buffers, needed once */367int INT123_frame_reset(mpg123_handle* fr); /* reset for next track */368int INT123_frame_buffers_reset(mpg123_handle *fr);369void INT123_frame_exit(mpg123_handle *fr); /* end, free all buffers */370371/* Index functions... */372/* Well... print it... */373int mpg123_print_index(mpg123_handle *fr, FILE* out);374/* Find a seek position in index. */375int64_t INT123_frame_index_find(mpg123_handle *fr, int64_t want_frame, int64_t* get_frame);376/* Apply index_size setting. */377int INT123_frame_index_setup(mpg123_handle *fr);378379void INT123_do_volume(mpg123_handle *fr, double factor);380void INT123_do_rva(mpg123_handle *fr);381382/* samples per frame ...383Layer I384Layer II385Layer III386MPEG-138738438811523891152390MPEG-2 LSF3913843921152393576394MPEG 2.53953843961152397576398*/399400// Well, I take that one for granted... at least layer3.401// The value is needed for mpg123_getstate() in any build.402#define GAPLESS_DELAY 529403#ifdef GAPLESS404void INT123_frame_gapless_init(mpg123_handle *fr, int64_t framecount, int64_t bskip, int64_t eskip);405void INT123_frame_gapless_realinit(mpg123_handle *fr);406void INT123_frame_gapless_update(mpg123_handle *mh, int64_t total_samples);407/*void frame_gapless_position(mpg123_handle* fr);408void INT123_frame_gapless_bytify(mpg123_handle *fr);409void INT123_frame_gapless_ignore(mpg123_handle *fr, int64_t frames);*/410/* void frame_gapless_buffercheck(mpg123_handle *fr); */411#endif412413/* Number of samples the decoding of the current frame should yield. */414int64_t INT123_frame_expect_outsamples(mpg123_handle *fr);415416/* Skip this frame... do some fake action to get away without actually decoding it. */417void INT123_frame_skip(mpg123_handle *fr);418419/*420Seeking core functions:421- convert input sample offset to output sample offset422- convert frame offset to output sample offset423- get leading frame offset for output sample offset424The offsets are "unadjusted"/internal; resampling is being taken care of.425*/426int64_t INT123_frame_ins2outs(mpg123_handle *fr, int64_t ins);427int64_t INT123_frame_outs(mpg123_handle *fr, int64_t num);428/* This one just computes the expected sample count for _this_ frame. */429int64_t INT123_frame_expect_outsampels(mpg123_handle *fr);430int64_t INT123_frame_offset(mpg123_handle *fr, int64_t outs);431void INT123_frame_set_frameseek(mpg123_handle *fr, int64_t fe);432void INT123_frame_set_seek(mpg123_handle *fr, int64_t sp);433int64_t INT123_frame_tell_seek(mpg123_handle *fr);434/* Take a copy of the Xing VBR TOC for fuzzy seeking. */435int INT123_frame_fill_toc(mpg123_handle *fr, unsigned char* in);436#endif437438439