├── .gitignore ├── Makefile ├── README.md ├── main.c └── pdmp3.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | # Libraries 7 | *.lib 8 | *.a 9 | *.la 10 | *.lo 11 | # Shared objects (inc. Windows DLLs) 12 | *.dll 13 | *.so 14 | *.so.* 15 | *.dylib 16 | # Executables 17 | *.exe 18 | *.out 19 | *.app 20 | *.i*86 21 | *.x86_64 22 | *.hex 23 | pdmp3 24 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Linux 3 | # 4 | CC = gcc 5 | 6 | # 7 | # Flags: 8 | # 9 | # DEBUG_CHECK Extra checking of bitstream data 10 | # OUTPUT_SOUND Write sound data to /dev/dsp 11 | # OUTPUT_RAW Write sound data to .raw 12 | # OUTPUT_DBG Write clear-text debug dumps to stdout 13 | 14 | #CFLAGS = -g -O4 -funroll-loops -Wall -ansi -DOUTPUT_SOUND 15 | #CFLAGS = -O4 -funroll-loops -Wall -ansi -DOUTPUT_RAW 16 | #CFLAGS = -O4 -funroll-loops -Wall -ansi -DOUTPUT_DBG 17 | CFLAGS = -Os -ffunction-sections -fdata-sections \ 18 | -finline-small-functions -finline-functions-called-once \ 19 | -fno-unwind-tables -fno-asynchronous-unwind-tables \ 20 | -ffast-math -fassociative-math -fomit-frame-pointer -ffinite-math-only \ 21 | -fno-math-errno -fno-trapping-math -freciprocal-math -frounding-math \ 22 | -funsafe-loop-optimizations -funsafe-math-optimizations \ 23 | -DOUTPUT_SOUND -DIMDCT_TABLES -DIMDCT_NTABLES -DPOW34_TABLE 24 | LDFLAGS = -Wl,--gc-sections,--as-needed,-s 25 | 26 | OBJS = pdmp3.o main.o 27 | 28 | all: pdmp3 29 | 30 | pdmp3: $(OBJS) 31 | $(CC) $(CFLAGS) -o pdmp3 $(OBJS) $(LDFLAGS) -lm 32 | @echo 33 | @echo "********** Made pdmp3 **********" 34 | @echo 35 | 36 | 37 | # 38 | # Install the decoder and utilities to /usr/local/bin. 39 | # This probably needs to be done as root. 40 | # 41 | install: pdmp3 42 | cp pdmp3 /usr/local/bin 43 | 44 | depend: 45 | gcc -MM $(CFLAGS) *.c > make.depend 46 | 47 | clean: 48 | -rm -f *.o *~ core TAGS *.wav *.bin 49 | 50 | realclean: clean 51 | -rm -f pdmp3 *.pdf *.ps *.bit 52 | 53 | etags: 54 | etags *.c *.h 55 | 56 | print: 57 | -rm -f app_b.ps 58 | a2ps --medium=a4 -G2r --left-title="" \ 59 | --header="Appendix B, Reference Decoder and Simulation Source Code" \ 60 | --file-align=fill \ 61 | main.c -oapp_b.ps pdmp3.c 62 | -rm -f app_b.pdf 63 | ps2pdf app_b.ps 64 | 65 | 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PDMP3 2 | ===== 3 | 4 | Public domain mp3 decoder 5 | 6 | This project is a fork of the mp3 decoder written by Krister Lagerstrom as part of a master's thesis. This version implements a subset op the libmpg123 API which should be a 100% compatible replacement. If it's not that is considered a bug. In theory more libmpg123 compatible functions could be implemented quite easily. 7 | 8 | Available functions for the streaming API are: 9 | 10 | pdmp3_handle * pdmp3_new(const char *decoder,int *error); 11 | void pdmp3_delete(pdmp3_handle * id); 12 | int pdmp3_open_feed(pdmp3_handle * id); 13 | int pdmp3_feed(pdmp3_handle * id,const unsigned char * in,size_t size); 14 | int pdmp3_read(pdmp3_handle * id,unsigned char * outmemory,size_t outsize,size_t * done); 15 | int pdmp3_decode(pdmp3_handle * id,const unsigned char * in,size_t insize,unsigned char * out,size_t outsize,size_t * done); 16 | int pdmp3_getformat(pdmp3_handle * id,long * rate,int * channels,int * encoding); 17 | 18 | 19 | TODO 20 | ---- 21 | * cleanup, 22 | * switch to bitfields, 23 | * fix some of the horribly nested logic 24 | 25 | > License: My work is placed in the public domain. You may do whatever you wish with it, including using it for commercial applications. 26 | 27 | The thesis and original code are available at: 28 | * https://sites.google.com/a/kmlager.com/www/projects 29 | 30 | From the abstract: 31 | 32 | > Digital compression of audio data is important due to the bandwidth and storage limitations inherent in networks and computers. Algorithms based on perceptual coding are effective and have become feasible with faster computers. The ISO standard 11172-3 MPEG-1 layer III (a.k.a. MP3) is a perceptual codec that is presently very common for compression of CD quality music. An MP3 decoder has a complex structure and is computationally demanding. 33 | 34 | > The purpose of this master's thesis is to present a tutorial on the standard. We have analysed several algorithms suitable for implementing an MP3 decoder, their advantages and disadvantages with respect to speed, memory demands and implementation complexity. We have also designed and implemented a portable reference MP3 decoder in C. 35 | 36 | If you are trying to implement an mp3 decoder and found the 11172-3 reference to be vague, the thesis is a a good read. 37 | 38 | Some other good references are: 39 | * http://blog.bjrn.se/2008/10/lets-build-mp3-decoder.html 40 | * http://www.multiweb.cz/twoinches/mp3inside.htm 41 | * http://www.mp3-converter.com/mp3codec/ 42 | * http://oreilly.com/catalog/9781565926615 43 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | void pdmp3(char * const *mp3s); 2 | int main(int ac, char **av){ 3 | if (ac < 2) return 1; 4 | pdmp3(++av); //don't pass the command name 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /pdmp3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Public Domain (www.unlicense.org) 3 | This is free and unencumbered software released into the public domain. 4 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 5 | software, either in source code form or as a compiled binary, for any purpose, 6 | commercial or non-commercial, and by any means. 7 | In jurisdictions that recognize copyright laws, the author or authors of this 8 | software dedicate any and all copyright interest in the software to the public 9 | domain. We make this dedication for the benefit of the public at large and to 10 | the detriment of our heirs and successors. We intend this dedication to be an 11 | overt act of relinquishment in perpetuity of all present and future rights to 12 | this software under copyright law. 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 17 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 18 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | 20 | Original version written by Krister Lagerström(krister@kmlager.com) 21 | Website: https://sites.google.com/a/kmlager.com/www/projects 22 | 23 | Contributors: 24 | technosaurus 25 | Erik Hofman (added a subset of the libmpg123 compatible streaming API) 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #ifdef OUTPUT_SOUND 38 | #include 39 | #endif 40 | 41 | /* Types used in the frame header */ 42 | typedef enum { /* Layer number */ 43 | mpeg1_layer_reserved = 0, 44 | mpeg1_layer_3 = 1, 45 | mpeg1_layer_2 = 2, 46 | mpeg1_layer_1 = 3 47 | } 48 | t_mpeg1_layer; 49 | typedef enum { /* Modes */ 50 | mpeg1_mode_stereo = 0, 51 | mpeg1_mode_joint_stereo, 52 | mpeg1_mode_dual_channel, 53 | mpeg1_mode_single_channel 54 | } 55 | t_mpeg1_mode; 56 | typedef struct { /* MPEG1 Layer 1-3 frame header */ 57 | unsigned id; /* 1 bit */ 58 | t_mpeg1_layer layer; /* 2 bits */ 59 | unsigned protection_bit; /* 1 bit */ 60 | unsigned bitrate_index; /* 4 bits */ 61 | unsigned sampling_frequency; /* 2 bits */ 62 | unsigned padding_bit; /* 1 bit */ 63 | unsigned private_bit; /* 1 bit */ 64 | t_mpeg1_mode mode; /* 2 bits */ 65 | unsigned mode_extension; /* 2 bits */ 66 | unsigned copyright; /* 1 bit */ 67 | unsigned original_or_copy; /* 1 bit */ 68 | unsigned emphasis; /* 2 bits */ 69 | } 70 | t_mpeg1_header; 71 | typedef struct { /* MPEG1 Layer 3 Side Information : [2][2] means [gr][ch] */ 72 | unsigned main_data_begin; /* 9 bits */ 73 | unsigned private_bits; /* 3 bits in mono,5 in stereo */ 74 | unsigned scfsi[2][4]; /* 1 bit */ 75 | unsigned part2_3_length[2][2]; /* 12 bits */ 76 | unsigned big_values[2][2]; /* 9 bits */ 77 | unsigned global_gain[2][2]; /* 8 bits */ 78 | unsigned scalefac_compress[2][2]; /* 4 bits */ 79 | unsigned win_switch_flag[2][2]; /* 1 bit */ 80 | /* if(win_switch_flag[][]) */ //use a union dammit 81 | unsigned block_type[2][2]; /* 2 bits */ 82 | unsigned mixed_block_flag[2][2]; /* 1 bit */ 83 | unsigned table_select[2][2][3]; /* 5 bits */ 84 | unsigned subblock_gain[2][2][3]; /* 3 bits */ 85 | /* else */ 86 | /* table_select[][][] */ 87 | unsigned region0_count[2][2]; /* 4 bits */ 88 | unsigned region1_count[2][2]; /* 3 bits */ 89 | /* end */ 90 | unsigned preflag[2][2]; /* 1 bit */ 91 | unsigned scalefac_scale[2][2]; /* 1 bit */ 92 | unsigned count1table_select[2][2];/* 1 bit */ 93 | unsigned count1[2][2]; /* Not in file,calc. by huff.dec.! */ 94 | } 95 | t_mpeg1_side_info; 96 | typedef struct { /* MPEG1 Layer 3 Main Data */ 97 | unsigned scalefac_l[2][2][21]; /* 0-4 bits */ 98 | unsigned scalefac_s[2][2][12][3]; /* 0-4 bits */ 99 | float is[2][2][576]; /* Huffman coded freq. lines */ 100 | } 101 | t_mpeg1_main_data; 102 | typedef struct hufftables{ 103 | const unsigned short * hufftable; 104 | uint16_t treelen; 105 | uint8_t linbits; 106 | } 107 | hufftables; 108 | typedef struct { /* Scale factor band indices,for long and short windows */ 109 | unsigned l[23]; 110 | unsigned s[14]; 111 | } 112 | t_sf_band_indices; 113 | 114 | /** define a subset of a libmpg123 compatible streaming API */ 115 | #define PDMP3_OK 0 116 | #define PDMP3_ERR -1 117 | #define PDMP3_NEED_MORE -10 118 | #define PDMP3_NEW_FORMAT -11 119 | #define PDMP3_NO_SPACE 7 120 | 121 | #define PDMP3_ENC_SIGNED_16 (0x080|0x040|0x10) 122 | 123 | #define INBUF_SIZE (4*4096) 124 | typedef struct 125 | { 126 | size_t processed; 127 | unsigned istart,iend,ostart; 128 | unsigned char in[INBUF_SIZE]; 129 | unsigned out[2][576]; 130 | t_mpeg1_header g_frame_header; 131 | t_mpeg1_side_info g_side_info; /* < 100 words */ 132 | t_mpeg1_main_data g_main_data; 133 | 134 | unsigned hsynth_init; 135 | unsigned synth_init; 136 | /* Bit reservoir for main data */ 137 | unsigned g_main_data_vec[2*1024];/* Large static data */ 138 | unsigned *g_main_data_ptr;/* Pointer into the reservoir */ 139 | unsigned g_main_data_idx;/* Index into the current byte(0-7) */ 140 | unsigned g_main_data_top;/* Number of bytes in reservoir(0-1024) */ 141 | /* Bit reservoir for side info */ 142 | unsigned side_info_vec[32+4]; 143 | unsigned *side_info_ptr; /* Pointer into the reservoir */ 144 | unsigned side_info_idx; /* Index into the current byte(0-7) */ 145 | 146 | char new_header; 147 | } 148 | pdmp3_handle; 149 | 150 | pdmp3_handle* pdmp3_new(const char *decoder,int *error); 151 | void pdmp3_delete(pdmp3_handle *id); 152 | int pdmp3_open_feed(pdmp3_handle *id); 153 | int pdmp3_feed(pdmp3_handle *id,const unsigned char *in,size_t size); 154 | int pdmp3_read(pdmp3_handle *id,unsigned char *outmemory,size_t outsize,size_t *done); 155 | int pdmp3_decode(pdmp3_handle *id,const unsigned char *in,size_t insize,unsigned char *out,size_t outsize,size_t *done); 156 | int pdmp3_getformat(pdmp3_handle *id,long *rate,int *channels,int *encoding); 157 | /** end of the subset of a libmpg123 compatible streaming API */ 158 | 159 | void pdmp3(char * const *mp3s); 160 | 161 | #ifndef PDMP3_HEADER_ONLY 162 | #define SIM_UNIX 163 | #define TRUE 1 164 | #define FALSE 0 165 | #define C_SYNC 0xfff00000 166 | #define C_EOF 0xffffffff 167 | #define C_PI 3.14159265358979323846 168 | #define C_INV_SQRT_2 0.70710678118654752440 169 | #define Hz 1 170 | #define kHz 1000*Hz 171 | #define bit_s 1 172 | #define kbit_s 1000*bit_s 173 | #define FRAG_SIZE_LN2 0x0011 /* 2^17=128kb */ 174 | #define FRAG_NUMS 0x0004 175 | 176 | #ifndef NDEBUG 177 | #define DBG(str,args...) { printf(str,## args); printf("\n"); } 178 | #define ERR(str,args...) { fprintf(stderr,str,## args) ; fprintf(stderr,"\n"); } 179 | #else 180 | #define DBG(str,args...) 181 | #define ERR(str,args...) 182 | #endif 183 | 184 | 185 | #ifdef DEBUG //debug functions 186 | void dmp_fr(t_mpeg1_header *hdr); 187 | void dmp_si(t_mpeg1_header *hdr,t_mpeg1_side_info *si); 188 | void dmp_scf(t_mpeg1_side_info *si,t_mpeg1_main_data *md,int gr,int ch); 189 | void dmp_huff(t_mpeg1_main_data *md,int gr,int ch); 190 | void dmp_samples(t_mpeg1_main_data *md,int gr,int ch,int type); 191 | #else 192 | #define dmp_fr(...) do{}while(0) 193 | #define dmp_si(...) do{}while(0) 194 | #define dmp_scf(...) do{}while(0) 195 | #define dmp_huff(...) do{}while(0) 196 | #define dmp_samples(...) do{}while(0) 197 | #endif 198 | static int Decode_L3(pdmp3_handle *id); 199 | static int Get_Bytes(pdmp3_handle *id,unsigned no_of_bytes,unsigned data_vec[]); 200 | static int Get_Main_Data(pdmp3_handle *id,unsigned main_data_size,unsigned main_data_begin); 201 | static int Huffman_Decode(pdmp3_handle *id,unsigned table_num,int32_t *x,int32_t *y,int32_t *v,int32_t *w); 202 | static int Read_Audio_L3(pdmp3_handle *id); 203 | static int Read_CRC(pdmp3_handle *id); 204 | static int Read_Frame(pdmp3_handle *id); 205 | static int Search_Header(pdmp3_handle *id); 206 | static int Read_Main_L3(pdmp3_handle *id); 207 | static int Set_Main_Pos(pdmp3_handle *id,unsigned bit_pos); 208 | 209 | static unsigned Get_Inbuf_Filled(pdmp3_handle *id); 210 | static unsigned Get_Inbuf_Free(pdmp3_handle *id); 211 | 212 | static unsigned Get_Byte(pdmp3_handle *id); 213 | static unsigned Get_Main_Bit(pdmp3_handle *id); 214 | static unsigned Get_Main_Bits(pdmp3_handle *id,unsigned number_of_bits); 215 | static unsigned Get_Main_Pos(pdmp3_handle *id); 216 | static unsigned Get_Side_Bits(pdmp3_handle *id,unsigned number_of_bits); 217 | static unsigned Get_Filepos(pdmp3_handle *id); 218 | 219 | static void Error(const char *s,int e); 220 | static void Get_Sideinfo(pdmp3_handle *id,unsigned sideinfo_size); 221 | static void IMDCT_Win(float in[18],float out[36],unsigned block_type); 222 | static void L3_Antialias(pdmp3_handle *id,unsigned gr,unsigned ch); 223 | static void L3_Frequency_Inversion(pdmp3_handle *id,unsigned gr,unsigned ch); 224 | static void L3_Hybrid_Synthesis(pdmp3_handle *id,unsigned gr,unsigned ch); 225 | static void L3_Requantize(pdmp3_handle *id,unsigned gr,unsigned ch); 226 | static void L3_Reorder(pdmp3_handle *id,unsigned gr,unsigned ch); 227 | static void L3_Stereo(pdmp3_handle *id,unsigned gr); 228 | static void L3_Subband_Synthesis(pdmp3_handle *id,unsigned gr,unsigned ch,unsigned outdata[576]); 229 | static void Read_Huffman(pdmp3_handle *id,unsigned part_2_start,unsigned gr,unsigned ch); 230 | static void Requantize_Process_Long(pdmp3_handle *id,unsigned gr,unsigned ch,unsigned is_pos,unsigned sfb); 231 | static void Requantize_Process_Short(pdmp3_handle *id,unsigned gr,unsigned ch,unsigned is_pos,unsigned sfb,unsigned win); 232 | static void Stereo_Process_Intensity_Long(pdmp3_handle *id,unsigned gr,unsigned sfb); 233 | static void Stereo_Process_Intensity_Short(pdmp3_handle *id,unsigned gr,unsigned sfb); 234 | 235 | static const unsigned short g_huffman_table[] = { 236 | //g_huffman_table_1[7] = { 237 | 0x0201,0x0000,0x0201,0x0010,0x0201,0x0001,0x0011, 238 | //},g_huffman_table_2[17] = { 239 | 0x0201,0x0000,0x0401,0x0201,0x0010,0x0001,0x0201,0x0011,0x0401,0x0201,0x0020, 240 | 0x0021,0x0201,0x0012,0x0201,0x0002,0x0022, 241 | //},g_huffman_table_3[17] = { 242 | 0x0401,0x0201,0x0000,0x0001,0x0201,0x0011,0x0201,0x0010,0x0401,0x0201,0x0020, 243 | 0x0021,0x0201,0x0012,0x0201,0x0002,0x0022, 244 | //},g_huffman_table_5[31] = { 245 | 0x0201,0x0000,0x0401,0x0201,0x0010,0x0001,0x0201,0x0011,0x0801,0x0401,0x0201, 246 | 0x0020,0x0002,0x0201,0x0021,0x0012,0x0801,0x0401,0x0201,0x0022,0x0030,0x0201, 247 | 0x0003,0x0013,0x0201,0x0031,0x0201,0x0032,0x0201,0x0023,0x0033, 248 | //},g_huffman_table_6[31] = { 249 | 0x0601,0x0401,0x0201,0x0000,0x0010,0x0011,0x0601,0x0201,0x0001,0x0201,0x0020, 250 | 0x0021,0x0601,0x0201,0x0012,0x0201,0x0002,0x0022,0x0401,0x0201,0x0031,0x0013, 251 | 0x0401,0x0201,0x0030,0x0032,0x0201,0x0023,0x0201,0x0003,0x0033, 252 | //},g_huffman_table_7[71] = { 253 | 0x0201,0x0000,0x0401,0x0201,0x0010,0x0001,0x0801,0x0201,0x0011,0x0401,0x0201, 254 | 0x0020,0x0002,0x0021,0x1201,0x0601,0x0201,0x0012,0x0201,0x0022,0x0030,0x0401, 255 | 0x0201,0x0031,0x0013,0x0401,0x0201,0x0003,0x0032,0x0201,0x0023,0x0004,0x0a01, 256 | 0x0401,0x0201,0x0040,0x0041,0x0201,0x0014,0x0201,0x0042,0x0024,0x0c01,0x0601, 257 | 0x0401,0x0201,0x0033,0x0043,0x0050,0x0401,0x0201,0x0034,0x0005,0x0051,0x0601, 258 | 0x0201,0x0015,0x0201,0x0052,0x0025,0x0401,0x0201,0x0044,0x0035,0x0401,0x0201, 259 | 0x0053,0x0054,0x0201,0x0045,0x0055, 260 | //},g_huffman_table_8[71] = { 261 | 0x0601,0x0201,0x0000,0x0201,0x0010,0x0001,0x0201,0x0011,0x0401,0x0201,0x0021, 262 | 0x0012,0x0e01,0x0401,0x0201,0x0020,0x0002,0x0201,0x0022,0x0401,0x0201,0x0030, 263 | 0x0003,0x0201,0x0031,0x0013,0x0e01,0x0801,0x0401,0x0201,0x0032,0x0023,0x0201, 264 | 0x0040,0x0004,0x0201,0x0041,0x0201,0x0014,0x0042,0x0c01,0x0601,0x0201,0x0024, 265 | 0x0201,0x0033,0x0050,0x0401,0x0201,0x0043,0x0034,0x0051,0x0601,0x0201,0x0015, 266 | 0x0201,0x0005,0x0052,0x0601,0x0201,0x0025,0x0201,0x0044,0x0035,0x0201,0x0053, 267 | 0x0201,0x0045,0x0201,0x0054,0x0055, 268 | //},g_huffman_table_9[71] = { 269 | 0x0801,0x0401,0x0201,0x0000,0x0010,0x0201,0x0001,0x0011,0x0a01,0x0401,0x0201, 270 | 0x0020,0x0021,0x0201,0x0012,0x0201,0x0002,0x0022,0x0c01,0x0601,0x0401,0x0201, 271 | 0x0030,0x0003,0x0031,0x0201,0x0013,0x0201,0x0032,0x0023,0x0c01,0x0401,0x0201, 272 | 0x0041,0x0014,0x0401,0x0201,0x0040,0x0033,0x0201,0x0042,0x0024,0x0a01,0x0601, 273 | 0x0401,0x0201,0x0004,0x0050,0x0043,0x0201,0x0034,0x0051,0x0801,0x0401,0x0201, 274 | 0x0015,0x0052,0x0201,0x0025,0x0044,0x0601,0x0401,0x0201,0x0005,0x0054,0x0053, 275 | 0x0201,0x0035,0x0201,0x0045,0x0055, 276 | //},g_huffman_table_10[127] = { 277 | 0x0201,0x0000,0x0401,0x0201,0x0010,0x0001,0x0a01,0x0201,0x0011,0x0401,0x0201, 278 | 0x0020,0x0002,0x0201,0x0021,0x0012,0x1c01,0x0801,0x0401,0x0201,0x0022,0x0030, 279 | 0x0201,0x0031,0x0013,0x0801,0x0401,0x0201,0x0003,0x0032,0x0201,0x0023,0x0040, 280 | 0x0401,0x0201,0x0041,0x0014,0x0401,0x0201,0x0004,0x0033,0x0201,0x0042,0x0024, 281 | 0x1c01,0x0a01,0x0601,0x0401,0x0201,0x0050,0x0005,0x0060,0x0201,0x0061,0x0016, 282 | 0x0c01,0x0601,0x0401,0x0201,0x0043,0x0034,0x0051,0x0201,0x0015,0x0201,0x0052, 283 | 0x0025,0x0401,0x0201,0x0026,0x0036,0x0071,0x1401,0x0801,0x0201,0x0017,0x0401, 284 | 0x0201,0x0044,0x0053,0x0006,0x0601,0x0401,0x0201,0x0035,0x0045,0x0062,0x0201, 285 | 0x0070,0x0201,0x0007,0x0064,0x0e01,0x0401,0x0201,0x0072,0x0027,0x0601,0x0201, 286 | 0x0063,0x0201,0x0054,0x0055,0x0201,0x0046,0x0073,0x0801,0x0401,0x0201,0x0037, 287 | 0x0065,0x0201,0x0056,0x0074,0x0601,0x0201,0x0047,0x0201,0x0066,0x0075,0x0401, 288 | 0x0201,0x0057,0x0076,0x0201,0x0067,0x0077, 289 | //},g_huffman_table_11[127] = { 290 | 0x0601,0x0201,0x0000,0x0201,0x0010,0x0001,0x0801,0x0201,0x0011,0x0401,0x0201, 291 | 0x0020,0x0002,0x0012,0x1801,0x0801,0x0201,0x0021,0x0201,0x0022,0x0201,0x0030, 292 | 0x0003,0x0401,0x0201,0x0031,0x0013,0x0401,0x0201,0x0032,0x0023,0x0401,0x0201, 293 | 0x0040,0x0004,0x0201,0x0041,0x0014,0x1e01,0x1001,0x0a01,0x0401,0x0201,0x0042, 294 | 0x0024,0x0401,0x0201,0x0033,0x0043,0x0050,0x0401,0x0201,0x0034,0x0051,0x0061, 295 | 0x0601,0x0201,0x0016,0x0201,0x0006,0x0026,0x0201,0x0062,0x0201,0x0015,0x0201, 296 | 0x0005,0x0052,0x1001,0x0a01,0x0601,0x0401,0x0201,0x0025,0x0044,0x0060,0x0201, 297 | 0x0063,0x0036,0x0401,0x0201,0x0070,0x0017,0x0071,0x1001,0x0601,0x0401,0x0201, 298 | 0x0007,0x0064,0x0072,0x0201,0x0027,0x0401,0x0201,0x0053,0x0035,0x0201,0x0054, 299 | 0x0045,0x0a01,0x0401,0x0201,0x0046,0x0073,0x0201,0x0037,0x0201,0x0065,0x0056, 300 | 0x0a01,0x0601,0x0401,0x0201,0x0055,0x0057,0x0074,0x0201,0x0047,0x0066,0x0401, 301 | 0x0201,0x0075,0x0076,0x0201,0x0067,0x0077, 302 | //},g_huffman_table_12[127] = { 303 | 0x0c01,0x0401,0x0201,0x0010,0x0001,0x0201,0x0011,0x0201,0x0000,0x0201,0x0020, 304 | 0x0002,0x1001,0x0401,0x0201,0x0021,0x0012,0x0401,0x0201,0x0022,0x0031,0x0201, 305 | 0x0013,0x0201,0x0030,0x0201,0x0003,0x0040,0x1a01,0x0801,0x0401,0x0201,0x0032, 306 | 0x0023,0x0201,0x0041,0x0033,0x0a01,0x0401,0x0201,0x0014,0x0042,0x0201,0x0024, 307 | 0x0201,0x0004,0x0050,0x0401,0x0201,0x0043,0x0034,0x0201,0x0051,0x0015,0x1c01, 308 | 0x0e01,0x0801,0x0401,0x0201,0x0052,0x0025,0x0201,0x0053,0x0035,0x0401,0x0201, 309 | 0x0060,0x0016,0x0061,0x0401,0x0201,0x0062,0x0026,0x0601,0x0401,0x0201,0x0005, 310 | 0x0006,0x0044,0x0201,0x0054,0x0045,0x1201,0x0a01,0x0401,0x0201,0x0063,0x0036, 311 | 0x0401,0x0201,0x0070,0x0007,0x0071,0x0401,0x0201,0x0017,0x0064,0x0201,0x0046, 312 | 0x0072,0x0a01,0x0601,0x0201,0x0027,0x0201,0x0055,0x0073,0x0201,0x0037,0x0056, 313 | 0x0801,0x0401,0x0201,0x0065,0x0074,0x0201,0x0047,0x0066,0x0401,0x0201,0x0075, 314 | 0x0057,0x0201,0x0076,0x0201,0x0067,0x0077, 315 | //},g_huffman_table_13[511] = { 316 | 0x0201,0x0000,0x0601,0x0201,0x0010,0x0201,0x0001,0x0011,0x1c01,0x0801,0x0401, 317 | 0x0201,0x0020,0x0002,0x0201,0x0021,0x0012,0x0801,0x0401,0x0201,0x0022,0x0030, 318 | 0x0201,0x0003,0x0031,0x0601,0x0201,0x0013,0x0201,0x0032,0x0023,0x0401,0x0201, 319 | 0x0040,0x0004,0x0041,0x4601,0x1c01,0x0e01,0x0601,0x0201,0x0014,0x0201,0x0033, 320 | 0x0042,0x0401,0x0201,0x0024,0x0050,0x0201,0x0043,0x0034,0x0401,0x0201,0x0051, 321 | 0x0015,0x0401,0x0201,0x0005,0x0052,0x0201,0x0025,0x0201,0x0044,0x0053,0x0e01, 322 | 0x0801,0x0401,0x0201,0x0060,0x0006,0x0201,0x0061,0x0016,0x0401,0x0201,0x0080, 323 | 0x0008,0x0081,0x1001,0x0801,0x0401,0x0201,0x0035,0x0062,0x0201,0x0026,0x0054, 324 | 0x0401,0x0201,0x0045,0x0063,0x0201,0x0036,0x0070,0x0601,0x0401,0x0201,0x0007, 325 | 0x0055,0x0071,0x0201,0x0017,0x0201,0x0027,0x0037,0x4801,0x1801,0x0c01,0x0401, 326 | 0x0201,0x0018,0x0082,0x0201,0x0028,0x0401,0x0201,0x0064,0x0046,0x0072,0x0801, 327 | 0x0401,0x0201,0x0084,0x0048,0x0201,0x0090,0x0009,0x0201,0x0091,0x0019,0x1801, 328 | 0x0e01,0x0801,0x0401,0x0201,0x0073,0x0065,0x0201,0x0056,0x0074,0x0401,0x0201, 329 | 0x0047,0x0066,0x0083,0x0601,0x0201,0x0038,0x0201,0x0075,0x0057,0x0201,0x0092, 330 | 0x0029,0x0e01,0x0801,0x0401,0x0201,0x0067,0x0085,0x0201,0x0058,0x0039,0x0201, 331 | 0x0093,0x0201,0x0049,0x0086,0x0601,0x0201,0x00a0,0x0201,0x0068,0x000a,0x0201, 332 | 0x00a1,0x001a,0x4401,0x1801,0x0c01,0x0401,0x0201,0x00a2,0x002a,0x0401,0x0201, 333 | 0x0095,0x0059,0x0201,0x00a3,0x003a,0x0801,0x0401,0x0201,0x004a,0x0096,0x0201, 334 | 0x00b0,0x000b,0x0201,0x00b1,0x001b,0x1401,0x0801,0x0201,0x00b2,0x0401,0x0201, 335 | 0x0076,0x0077,0x0094,0x0601,0x0401,0x0201,0x0087,0x0078,0x00a4,0x0401,0x0201, 336 | 0x0069,0x00a5,0x002b,0x0c01,0x0601,0x0401,0x0201,0x005a,0x0088,0x00b3,0x0201, 337 | 0x003b,0x0201,0x0079,0x00a6,0x0601,0x0401,0x0201,0x006a,0x00b4,0x00c0,0x0401, 338 | 0x0201,0x000c,0x0098,0x00c1,0x3c01,0x1601,0x0a01,0x0601,0x0201,0x001c,0x0201, 339 | 0x0089,0x00b5,0x0201,0x005b,0x00c2,0x0401,0x0201,0x002c,0x003c,0x0401,0x0201, 340 | 0x00b6,0x006b,0x0201,0x00c4,0x004c,0x1001,0x0801,0x0401,0x0201,0x00a8,0x008a, 341 | 0x0201,0x00d0,0x000d,0x0201,0x00d1,0x0201,0x004b,0x0201,0x0097,0x00a7,0x0c01, 342 | 0x0601,0x0201,0x00c3,0x0201,0x007a,0x0099,0x0401,0x0201,0x00c5,0x005c,0x00b7, 343 | 0x0401,0x0201,0x001d,0x00d2,0x0201,0x002d,0x0201,0x007b,0x00d3,0x3401,0x1c01, 344 | 0x0c01,0x0401,0x0201,0x003d,0x00c6,0x0401,0x0201,0x006c,0x00a9,0x0201,0x009a, 345 | 0x00d4,0x0801,0x0401,0x0201,0x00b8,0x008b,0x0201,0x004d,0x00c7,0x0401,0x0201, 346 | 0x007c,0x00d5,0x0201,0x005d,0x00e0,0x0a01,0x0401,0x0201,0x00e1,0x001e,0x0401, 347 | 0x0201,0x000e,0x002e,0x00e2,0x0801,0x0401,0x0201,0x00e3,0x006d,0x0201,0x008c, 348 | 0x00e4,0x0401,0x0201,0x00e5,0x00ba,0x00f0,0x2601,0x1001,0x0401,0x0201,0x00f1, 349 | 0x001f,0x0601,0x0401,0x0201,0x00aa,0x009b,0x00b9,0x0201,0x003e,0x0201,0x00d6, 350 | 0x00c8,0x0c01,0x0601,0x0201,0x004e,0x0201,0x00d7,0x007d,0x0201,0x00ab,0x0201, 351 | 0x005e,0x00c9,0x0601,0x0201,0x000f,0x0201,0x009c,0x006e,0x0201,0x00f2,0x002f, 352 | 0x2001,0x1001,0x0601,0x0401,0x0201,0x00d8,0x008d,0x003f,0x0601,0x0201,0x00f3, 353 | 0x0201,0x00e6,0x00ca,0x0201,0x00f4,0x004f,0x0801,0x0401,0x0201,0x00bb,0x00ac, 354 | 0x0201,0x00e7,0x00f5,0x0401,0x0201,0x00d9,0x009d,0x0201,0x005f,0x00e8,0x1e01, 355 | 0x0c01,0x0601,0x0201,0x006f,0x0201,0x00f6,0x00cb,0x0401,0x0201,0x00bc,0x00ad, 356 | 0x00da,0x0801,0x0201,0x00f7,0x0401,0x0201,0x007e,0x007f,0x008e,0x0601,0x0401, 357 | 0x0201,0x009e,0x00ae,0x00cc,0x0201,0x00f8,0x008f,0x1201,0x0801,0x0401,0x0201, 358 | 0x00db,0x00bd,0x0201,0x00ea,0x00f9,0x0401,0x0201,0x009f,0x00eb,0x0201,0x00be, 359 | 0x0201,0x00cd,0x00fa,0x0e01,0x0401,0x0201,0x00dd,0x00ec,0x0601,0x0401,0x0201, 360 | 0x00e9,0x00af,0x00dc,0x0201,0x00ce,0x00fb,0x0801,0x0401,0x0201,0x00bf,0x00de, 361 | 0x0201,0x00cf,0x00ee,0x0401,0x0201,0x00df,0x00ef,0x0201,0x00ff,0x0201,0x00ed, 362 | 0x0201,0x00fd,0x0201,0x00fc,0x00fe, 363 | //},g_huffman_table_15[511] = { 364 | 0x1001,0x0601,0x0201,0x0000,0x0201,0x0010,0x0001,0x0201,0x0011,0x0401,0x0201, 365 | 0x0020,0x0002,0x0201,0x0021,0x0012,0x3201,0x1001,0x0601,0x0201,0x0022,0x0201, 366 | 0x0030,0x0031,0x0601,0x0201,0x0013,0x0201,0x0003,0x0040,0x0201,0x0032,0x0023, 367 | 0x0e01,0x0601,0x0401,0x0201,0x0004,0x0014,0x0041,0x0401,0x0201,0x0033,0x0042, 368 | 0x0201,0x0024,0x0043,0x0a01,0x0601,0x0201,0x0034,0x0201,0x0050,0x0005,0x0201, 369 | 0x0051,0x0015,0x0401,0x0201,0x0052,0x0025,0x0401,0x0201,0x0044,0x0053,0x0061, 370 | 0x5a01,0x2401,0x1201,0x0a01,0x0601,0x0201,0x0035,0x0201,0x0060,0x0006,0x0201, 371 | 0x0016,0x0062,0x0401,0x0201,0x0026,0x0054,0x0201,0x0045,0x0063,0x0a01,0x0601, 372 | 0x0201,0x0036,0x0201,0x0070,0x0007,0x0201,0x0071,0x0055,0x0401,0x0201,0x0017, 373 | 0x0064,0x0201,0x0072,0x0027,0x1801,0x1001,0x0801,0x0401,0x0201,0x0046,0x0073, 374 | 0x0201,0x0037,0x0065,0x0401,0x0201,0x0056,0x0080,0x0201,0x0008,0x0074,0x0401, 375 | 0x0201,0x0081,0x0018,0x0201,0x0082,0x0028,0x1001,0x0801,0x0401,0x0201,0x0047, 376 | 0x0066,0x0201,0x0083,0x0038,0x0401,0x0201,0x0075,0x0057,0x0201,0x0084,0x0048, 377 | 0x0601,0x0401,0x0201,0x0090,0x0019,0x0091,0x0401,0x0201,0x0092,0x0076,0x0201, 378 | 0x0067,0x0029,0x5c01,0x2401,0x1201,0x0a01,0x0401,0x0201,0x0085,0x0058,0x0401, 379 | 0x0201,0x0009,0x0077,0x0093,0x0401,0x0201,0x0039,0x0094,0x0201,0x0049,0x0086, 380 | 0x0a01,0x0601,0x0201,0x0068,0x0201,0x00a0,0x000a,0x0201,0x00a1,0x001a,0x0401, 381 | 0x0201,0x00a2,0x002a,0x0201,0x0095,0x0059,0x1a01,0x0e01,0x0601,0x0201,0x00a3, 382 | 0x0201,0x003a,0x0087,0x0401,0x0201,0x0078,0x00a4,0x0201,0x004a,0x0096,0x0601, 383 | 0x0401,0x0201,0x0069,0x00b0,0x00b1,0x0401,0x0201,0x001b,0x00a5,0x00b2,0x0e01, 384 | 0x0801,0x0401,0x0201,0x005a,0x002b,0x0201,0x0088,0x0097,0x0201,0x00b3,0x0201, 385 | 0x0079,0x003b,0x0801,0x0401,0x0201,0x006a,0x00b4,0x0201,0x004b,0x00c1,0x0401, 386 | 0x0201,0x0098,0x0089,0x0201,0x001c,0x00b5,0x5001,0x2201,0x1001,0x0601,0x0401, 387 | 0x0201,0x005b,0x002c,0x00c2,0x0601,0x0401,0x0201,0x000b,0x00c0,0x00a6,0x0201, 388 | 0x00a7,0x007a,0x0a01,0x0401,0x0201,0x00c3,0x003c,0x0401,0x0201,0x000c,0x0099, 389 | 0x00b6,0x0401,0x0201,0x006b,0x00c4,0x0201,0x004c,0x00a8,0x1401,0x0a01,0x0401, 390 | 0x0201,0x008a,0x00c5,0x0401,0x0201,0x00d0,0x005c,0x00d1,0x0401,0x0201,0x00b7, 391 | 0x007b,0x0201,0x001d,0x0201,0x000d,0x002d,0x0c01,0x0401,0x0201,0x00d2,0x00d3, 392 | 0x0401,0x0201,0x003d,0x00c6,0x0201,0x006c,0x00a9,0x0601,0x0401,0x0201,0x009a, 393 | 0x00b8,0x00d4,0x0401,0x0201,0x008b,0x004d,0x0201,0x00c7,0x007c,0x4401,0x2201, 394 | 0x1201,0x0a01,0x0401,0x0201,0x00d5,0x005d,0x0401,0x0201,0x00e0,0x000e,0x00e1, 395 | 0x0401,0x0201,0x001e,0x00e2,0x0201,0x00aa,0x002e,0x0801,0x0401,0x0201,0x00b9, 396 | 0x009b,0x0201,0x00e3,0x00d6,0x0401,0x0201,0x006d,0x003e,0x0201,0x00c8,0x008c, 397 | 0x1001,0x0801,0x0401,0x0201,0x00e4,0x004e,0x0201,0x00d7,0x007d,0x0401,0x0201, 398 | 0x00e5,0x00ba,0x0201,0x00ab,0x005e,0x0801,0x0401,0x0201,0x00c9,0x009c,0x0201, 399 | 0x00f1,0x001f,0x0601,0x0401,0x0201,0x00f0,0x006e,0x00f2,0x0201,0x002f,0x00e6, 400 | 0x2601,0x1201,0x0801,0x0401,0x0201,0x00d8,0x00f3,0x0201,0x003f,0x00f4,0x0601, 401 | 0x0201,0x004f,0x0201,0x008d,0x00d9,0x0201,0x00bb,0x00ca,0x0801,0x0401,0x0201, 402 | 0x00ac,0x00e7,0x0201,0x007e,0x00f5,0x0801,0x0401,0x0201,0x009d,0x005f,0x0201, 403 | 0x00e8,0x008e,0x0201,0x00f6,0x00cb,0x2201,0x1201,0x0a01,0x0601,0x0401,0x0201, 404 | 0x000f,0x00ae,0x006f,0x0201,0x00bc,0x00da,0x0401,0x0201,0x00ad,0x00f7,0x0201, 405 | 0x007f,0x00e9,0x0801,0x0401,0x0201,0x009e,0x00cc,0x0201,0x00f8,0x008f,0x0401, 406 | 0x0201,0x00db,0x00bd,0x0201,0x00ea,0x00f9,0x1001,0x0801,0x0401,0x0201,0x009f, 407 | 0x00dc,0x0201,0x00cd,0x00eb,0x0401,0x0201,0x00be,0x00fa,0x0201,0x00af,0x00dd, 408 | 0x0e01,0x0601,0x0401,0x0201,0x00ec,0x00ce,0x00fb,0x0401,0x0201,0x00bf,0x00ed, 409 | 0x0201,0x00de,0x00fc,0x0601,0x0401,0x0201,0x00cf,0x00fd,0x00ee,0x0401,0x0201, 410 | 0x00df,0x00fe,0x0201,0x00ef,0x00ff, 411 | //},g_huffman_table_16[511] = { 412 | 0x0201,0x0000,0x0601,0x0201,0x0010,0x0201,0x0001,0x0011,0x2a01,0x0801,0x0401, 413 | 0x0201,0x0020,0x0002,0x0201,0x0021,0x0012,0x0a01,0x0601,0x0201,0x0022,0x0201, 414 | 0x0030,0x0003,0x0201,0x0031,0x0013,0x0a01,0x0401,0x0201,0x0032,0x0023,0x0401, 415 | 0x0201,0x0040,0x0004,0x0041,0x0601,0x0201,0x0014,0x0201,0x0033,0x0042,0x0401, 416 | 0x0201,0x0024,0x0050,0x0201,0x0043,0x0034,0x8a01,0x2801,0x1001,0x0601,0x0401, 417 | 0x0201,0x0005,0x0015,0x0051,0x0401,0x0201,0x0052,0x0025,0x0401,0x0201,0x0044, 418 | 0x0035,0x0053,0x0a01,0x0601,0x0401,0x0201,0x0060,0x0006,0x0061,0x0201,0x0016, 419 | 0x0062,0x0801,0x0401,0x0201,0x0026,0x0054,0x0201,0x0045,0x0063,0x0401,0x0201, 420 | 0x0036,0x0070,0x0071,0x2801,0x1201,0x0801,0x0201,0x0017,0x0201,0x0007,0x0201, 421 | 0x0055,0x0064,0x0401,0x0201,0x0072,0x0027,0x0401,0x0201,0x0046,0x0065,0x0073, 422 | 0x0a01,0x0601,0x0201,0x0037,0x0201,0x0056,0x0008,0x0201,0x0080,0x0081,0x0601, 423 | 0x0201,0x0018,0x0201,0x0074,0x0047,0x0201,0x0082,0x0201,0x0028,0x0066,0x1801, 424 | 0x0e01,0x0801,0x0401,0x0201,0x0083,0x0038,0x0201,0x0075,0x0084,0x0401,0x0201, 425 | 0x0048,0x0090,0x0091,0x0601,0x0201,0x0019,0x0201,0x0009,0x0076,0x0201,0x0092, 426 | 0x0029,0x0e01,0x0801,0x0401,0x0201,0x0085,0x0058,0x0201,0x0093,0x0039,0x0401, 427 | 0x0201,0x00a0,0x000a,0x001a,0x0801,0x0201,0x00a2,0x0201,0x0067,0x0201,0x0057, 428 | 0x0049,0x0601,0x0201,0x0094,0x0201,0x0077,0x0086,0x0201,0x00a1,0x0201,0x0068, 429 | 0x0095,0xdc01,0x7e01,0x3201,0x1a01,0x0c01,0x0601,0x0201,0x002a,0x0201,0x0059, 430 | 0x003a,0x0201,0x00a3,0x0201,0x0087,0x0078,0x0801,0x0401,0x0201,0x00a4,0x004a, 431 | 0x0201,0x0096,0x0069,0x0401,0x0201,0x00b0,0x000b,0x00b1,0x0a01,0x0401,0x0201, 432 | 0x001b,0x00b2,0x0201,0x002b,0x0201,0x00a5,0x005a,0x0601,0x0201,0x00b3,0x0201, 433 | 0x00a6,0x006a,0x0401,0x0201,0x00b4,0x004b,0x0201,0x000c,0x00c1,0x1e01,0x0e01, 434 | 0x0601,0x0401,0x0201,0x00b5,0x00c2,0x002c,0x0401,0x0201,0x00a7,0x00c3,0x0201, 435 | 0x006b,0x00c4,0x0801,0x0201,0x001d,0x0401,0x0201,0x0088,0x0097,0x003b,0x0401, 436 | 0x0201,0x00d1,0x00d2,0x0201,0x002d,0x00d3,0x1201,0x0601,0x0401,0x0201,0x001e, 437 | 0x002e,0x00e2,0x0601,0x0401,0x0201,0x0079,0x0098,0x00c0,0x0201,0x001c,0x0201, 438 | 0x0089,0x005b,0x0e01,0x0601,0x0201,0x003c,0x0201,0x007a,0x00b6,0x0401,0x0201, 439 | 0x004c,0x0099,0x0201,0x00a8,0x008a,0x0601,0x0201,0x000d,0x0201,0x00c5,0x005c, 440 | 0x0401,0x0201,0x003d,0x00c6,0x0201,0x006c,0x009a,0x5801,0x5601,0x2401,0x1001, 441 | 0x0801,0x0401,0x0201,0x008b,0x004d,0x0201,0x00c7,0x007c,0x0401,0x0201,0x00d5, 442 | 0x005d,0x0201,0x00e0,0x000e,0x0801,0x0201,0x00e3,0x0401,0x0201,0x00d0,0x00b7, 443 | 0x007b,0x0601,0x0401,0x0201,0x00a9,0x00b8,0x00d4,0x0201,0x00e1,0x0201,0x00aa, 444 | 0x00b9,0x1801,0x0a01,0x0601,0x0401,0x0201,0x009b,0x00d6,0x006d,0x0201,0x003e, 445 | 0x00c8,0x0601,0x0401,0x0201,0x008c,0x00e4,0x004e,0x0401,0x0201,0x00d7,0x00e5, 446 | 0x0201,0x00ba,0x00ab,0x0c01,0x0401,0x0201,0x009c,0x00e6,0x0401,0x0201,0x006e, 447 | 0x00d8,0x0201,0x008d,0x00bb,0x0801,0x0401,0x0201,0x00e7,0x009d,0x0201,0x00e8, 448 | 0x008e,0x0401,0x0201,0x00cb,0x00bc,0x009e,0x00f1,0x0201,0x001f,0x0201,0x000f, 449 | 0x002f,0x4201,0x3801,0x0201,0x00f2,0x3401,0x3201,0x1401,0x0801,0x0201,0x00bd, 450 | 0x0201,0x005e,0x0201,0x007d,0x00c9,0x0601,0x0201,0x00ca,0x0201,0x00ac,0x007e, 451 | 0x0401,0x0201,0x00da,0x00ad,0x00cc,0x0a01,0x0601,0x0201,0x00ae,0x0201,0x00db, 452 | 0x00dc,0x0201,0x00cd,0x00be,0x0601,0x0401,0x0201,0x00eb,0x00ed,0x00ee,0x0601, 453 | 0x0401,0x0201,0x00d9,0x00ea,0x00e9,0x0201,0x00de,0x0401,0x0201,0x00dd,0x00ec, 454 | 0x00ce,0x003f,0x00f0,0x0401,0x0201,0x00f3,0x00f4,0x0201,0x004f,0x0201,0x00f5, 455 | 0x005f,0x0a01,0x0201,0x00ff,0x0401,0x0201,0x00f6,0x006f,0x0201,0x00f7,0x007f, 456 | 0x0c01,0x0601,0x0201,0x008f,0x0201,0x00f8,0x00f9,0x0401,0x0201,0x009f,0x00fa, 457 | 0x00af,0x0801,0x0401,0x0201,0x00fb,0x00bf,0x0201,0x00fc,0x00cf,0x0401,0x0201, 458 | 0x00fd,0x00df,0x0201,0x00fe,0x00ef, 459 | //},g_huffman_table_24[512] = { 460 | 0x3c01,0x0801,0x0401,0x0201,0x0000,0x0010,0x0201,0x0001,0x0011,0x0e01,0x0601, 461 | 0x0401,0x0201,0x0020,0x0002,0x0021,0x0201,0x0012,0x0201,0x0022,0x0201,0x0030, 462 | 0x0003,0x0e01,0x0401,0x0201,0x0031,0x0013,0x0401,0x0201,0x0032,0x0023,0x0401, 463 | 0x0201,0x0040,0x0004,0x0041,0x0801,0x0401,0x0201,0x0014,0x0033,0x0201,0x0042, 464 | 0x0024,0x0601,0x0401,0x0201,0x0043,0x0034,0x0051,0x0601,0x0401,0x0201,0x0050, 465 | 0x0005,0x0015,0x0201,0x0052,0x0025,0xfa01,0x6201,0x2201,0x1201,0x0a01,0x0401, 466 | 0x0201,0x0044,0x0053,0x0201,0x0035,0x0201,0x0060,0x0006,0x0401,0x0201,0x0061, 467 | 0x0016,0x0201,0x0062,0x0026,0x0801,0x0401,0x0201,0x0054,0x0045,0x0201,0x0063, 468 | 0x0036,0x0401,0x0201,0x0071,0x0055,0x0201,0x0064,0x0046,0x2001,0x0e01,0x0601, 469 | 0x0201,0x0072,0x0201,0x0027,0x0037,0x0201,0x0073,0x0401,0x0201,0x0070,0x0007, 470 | 0x0017,0x0a01,0x0401,0x0201,0x0065,0x0056,0x0401,0x0201,0x0080,0x0008,0x0081, 471 | 0x0401,0x0201,0x0074,0x0047,0x0201,0x0018,0x0082,0x1001,0x0801,0x0401,0x0201, 472 | 0x0028,0x0066,0x0201,0x0083,0x0038,0x0401,0x0201,0x0075,0x0057,0x0201,0x0084, 473 | 0x0048,0x0801,0x0401,0x0201,0x0091,0x0019,0x0201,0x0092,0x0076,0x0401,0x0201, 474 | 0x0067,0x0029,0x0201,0x0085,0x0058,0x5c01,0x2201,0x1001,0x0801,0x0401,0x0201, 475 | 0x0093,0x0039,0x0201,0x0094,0x0049,0x0401,0x0201,0x0077,0x0086,0x0201,0x0068, 476 | 0x00a1,0x0801,0x0401,0x0201,0x00a2,0x002a,0x0201,0x0095,0x0059,0x0401,0x0201, 477 | 0x00a3,0x003a,0x0201,0x0087,0x0201,0x0078,0x004a,0x1601,0x0c01,0x0401,0x0201, 478 | 0x00a4,0x0096,0x0401,0x0201,0x0069,0x00b1,0x0201,0x001b,0x00a5,0x0601,0x0201, 479 | 0x00b2,0x0201,0x005a,0x002b,0x0201,0x0088,0x00b3,0x1001,0x0a01,0x0601,0x0201, 480 | 0x0090,0x0201,0x0009,0x00a0,0x0201,0x0097,0x0079,0x0401,0x0201,0x00a6,0x006a, 481 | 0x00b4,0x0c01,0x0601,0x0201,0x001a,0x0201,0x000a,0x00b0,0x0201,0x003b,0x0201, 482 | 0x000b,0x00c0,0x0401,0x0201,0x004b,0x00c1,0x0201,0x0098,0x0089,0x4301,0x2201, 483 | 0x1001,0x0801,0x0401,0x0201,0x001c,0x00b5,0x0201,0x005b,0x00c2,0x0401,0x0201, 484 | 0x002c,0x00a7,0x0201,0x007a,0x00c3,0x0a01,0x0601,0x0201,0x003c,0x0201,0x000c, 485 | 0x00d0,0x0201,0x00b6,0x006b,0x0401,0x0201,0x00c4,0x004c,0x0201,0x0099,0x00a8, 486 | 0x1001,0x0801,0x0401,0x0201,0x008a,0x00c5,0x0201,0x005c,0x00d1,0x0401,0x0201, 487 | 0x00b7,0x007b,0x0201,0x001d,0x00d2,0x0901,0x0401,0x0201,0x002d,0x00d3,0x0201, 488 | 0x003d,0x00c6,0x55fa,0x0401,0x0201,0x006c,0x00a9,0x0201,0x009a,0x00d4,0x2001, 489 | 0x1001,0x0801,0x0401,0x0201,0x00b8,0x008b,0x0201,0x004d,0x00c7,0x0401,0x0201, 490 | 0x007c,0x00d5,0x0201,0x005d,0x00e1,0x0801,0x0401,0x0201,0x001e,0x00e2,0x0201, 491 | 0x00aa,0x00b9,0x0401,0x0201,0x009b,0x00e3,0x0201,0x00d6,0x006d,0x1401,0x0a01, 492 | 0x0601,0x0201,0x003e,0x0201,0x002e,0x004e,0x0201,0x00c8,0x008c,0x0401,0x0201, 493 | 0x00e4,0x00d7,0x0401,0x0201,0x007d,0x00ab,0x00e5,0x0a01,0x0401,0x0201,0x00ba, 494 | 0x005e,0x0201,0x00c9,0x0201,0x009c,0x006e,0x0801,0x0201,0x00e6,0x0201,0x000d, 495 | 0x0201,0x00e0,0x000e,0x0401,0x0201,0x00d8,0x008d,0x0201,0x00bb,0x00ca,0x4a01, 496 | 0x0201,0x00ff,0x4001,0x3a01,0x2001,0x1001,0x0801,0x0401,0x0201,0x00ac,0x00e7, 497 | 0x0201,0x007e,0x00d9,0x0401,0x0201,0x009d,0x00e8,0x0201,0x008e,0x00cb,0x0801, 498 | 0x0401,0x0201,0x00bc,0x00da,0x0201,0x00ad,0x00e9,0x0401,0x0201,0x009e,0x00cc, 499 | 0x0201,0x00db,0x00bd,0x1001,0x0801,0x0401,0x0201,0x00ea,0x00ae,0x0201,0x00dc, 500 | 0x00cd,0x0401,0x0201,0x00eb,0x00be,0x0201,0x00dd,0x00ec,0x0801,0x0401,0x0201, 501 | 0x00ce,0x00ed,0x0201,0x00de,0x00ee,0x000f,0x0401,0x0201,0x00f0,0x001f,0x00f1, 502 | 0x0401,0x0201,0x00f2,0x002f,0x0201,0x00f3,0x003f,0x1201,0x0801,0x0401,0x0201, 503 | 0x00f4,0x004f,0x0201,0x00f5,0x005f,0x0401,0x0201,0x00f6,0x006f,0x0201,0x00f7, 504 | 0x0201,0x007f,0x008f,0x0a01,0x0401,0x0201,0x00f8,0x00f9,0x0401,0x0201,0x009f, 505 | 0x00af,0x00fa,0x0801,0x0401,0x0201,0x00fb,0x00bf,0x0201,0x00fc,0x00cf,0x0401, 506 | 0x0201,0x00fd,0x00df,0x0201,0x00fe,0x00ef, 507 | //},g_huffman_table_32[31] = { 508 | 0x0201,0x0000,0x0801,0x0401,0x0201,0x0008,0x0004,0x0201,0x0001,0x0002,0x0801, 509 | 0x0401,0x0201,0x000c,0x000a,0x0201,0x0003,0x0006,0x0601,0x0201,0x0009,0x0201, 510 | 0x0005,0x0007,0x0401,0x0201,0x000e,0x000d,0x0201,0x000f,0x000b, 511 | //},g_huffman_table_33[31] = { 512 | 0x1001,0x0801,0x0401,0x0201,0x0000,0x0001,0x0201,0x0002,0x0003,0x0401,0x0201, 513 | 0x0004,0x0005,0x0201,0x0006,0x0007,0x0801,0x0401,0x0201,0x0008,0x0009,0x0201, 514 | 0x000a,0x000b,0x0401,0x0201,0x000c,0x000d,0x0201,0x000e,0x000f, 515 | }; 516 | 517 | static const unsigned g_mpeg1_bitrates[3 /* layer 1-3 */][15 /* header bitrate_index */] = { 518 | { /* Layer 1 */ 519 | 0, 32000, 64000, 96000,128000,160000,192000,224000, 520 | 256000,288000,320000,352000,384000,416000,448000 521 | },{ /* Layer 2 */ 522 | 0, 32000, 48000, 56000, 64000, 80000, 96000,112000, 523 | 128000,160000,192000,224000,256000,320000,384000 524 | },{ /* Layer 3 */ 525 | 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 526 | 112000,128000,160000,192000,224000,256000,320000 527 | } 528 | }, 529 | g_sampling_frequency[3] = { 44100 * Hz,48000 * Hz,32000 * Hz }, 530 | mpeg1_scalefac_sizes[16][2 /* slen1,slen2 */] = { 531 | {0,0},{0,1},{0,2},{0,3},{3,0},{1,1},{1,2},{1,3}, 532 | {2,1},{2,2},{2,3},{3,1},{3,2},{3,3},{4,2},{4,3} 533 | }; 534 | 535 | hufftables g_huffman_main [34] = { 536 | {NULL , 0, 0 }, /* Table 0 */ 537 | {g_huffman_table , 7, 0 }, /* Table 1 */ 538 | {g_huffman_table +7 , 17, 0 }, /* Table 2 */ 539 | {g_huffman_table +24 , 17, 0 }, /* Table 3 */ 540 | {NULL , 0, 0 }, /* Table 4 */ 541 | {g_huffman_table +41 , 31, 0 }, /* Table 5 */ 542 | {g_huffman_table +72 , 31, 0 }, /* Table 6 */ 543 | {g_huffman_table +103 , 71, 0 }, /* Table 7 */ 544 | {g_huffman_table +174 , 71, 0 }, /* Table 8 */ 545 | {g_huffman_table +245 , 71, 0 }, /* Table 9 */ 546 | {g_huffman_table +316 ,127, 0 }, /* Table 10 */ 547 | {g_huffman_table +443 ,127, 0 }, /* Table 11 */ 548 | {g_huffman_table +570 ,127, 0 }, /* Table 12 */ 549 | {g_huffman_table +697 ,511, 0 }, /* Table 13 */ 550 | {NULL , 0, 0 }, /* Table 14 */ 551 | {g_huffman_table +1208 ,511, 0 }, /* Table 15 */ 552 | {g_huffman_table +1719 ,511, 1 }, /* Table 16 */ 553 | {g_huffman_table +1719 ,511, 2 }, /* Table 17 */ 554 | {g_huffman_table +1719 ,511, 3 }, /* Table 18 */ 555 | {g_huffman_table +1719 ,511, 4 }, /* Table 19 */ 556 | {g_huffman_table +1719 ,511, 6 }, /* Table 20 */ 557 | {g_huffman_table +1719 ,511, 8 }, /* Table 21 */ 558 | {g_huffman_table +1719 ,511,10 }, /* Table 22 */ 559 | {g_huffman_table +1719 ,511,13 }, /* Table 23 */ 560 | {g_huffman_table +2230 ,512, 4 }, /* Table 24 */ 561 | {g_huffman_table +2230 ,512, 5 }, /* Table 25 */ 562 | {g_huffman_table +2230 ,512, 6 }, /* Table 26 */ 563 | {g_huffman_table +2230 ,512, 7 }, /* Table 27 */ 564 | {g_huffman_table +2230 ,512, 8 }, /* Table 28 */ 565 | {g_huffman_table +2230 ,512, 9 }, /* Table 29 */ 566 | {g_huffman_table +2230 ,512,11 }, /* Table 30 */ 567 | {g_huffman_table +2230 ,512,13 }, /* Table 31 */ 568 | {g_huffman_table +2742 , 31, 0 }, /* Table 32 */ 569 | {g_huffman_table +2261 , 31, 0 }, /* Table 33 */ 570 | }; 571 | 572 | static const float // ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037}, 573 | cs[8]={0.857493,0.881742,0.949629,0.983315,0.995518,0.999161,0.999899,0.999993}, 574 | ca[8]={-0.514496,-0.471732,-0.313377,-0.181913,-0.094574,-0.040966,-0.014199,-0.003700}, 575 | is_ratios[6] = {0.000000f,0.267949f,0.577350f,1.000000f,1.732051f,3.732051f}, 576 | #ifdef IMDCT_TABLES 577 | g_imdct_win[4][36] = { 578 | {0.043619f,0.130526f,0.216440f,0.300706f,0.382683f,0.461749f, 579 | 0.537300f,0.608761f,0.675590f,0.737277f,0.793353f,0.843391f, 580 | 0.887011f,0.923880f,0.953717f,0.976296f,0.991445f,0.999048f, 581 | 0.999048f,0.991445f,0.976296f,0.953717f,0.923879f,0.887011f, 582 | 0.843391f,0.793353f,0.737277f,0.675590f,0.608761f,0.537299f, 583 | 0.461748f,0.382683f,0.300706f,0.216439f,0.130526f,0.043619f 584 | },{0.043619f,0.130526f,0.216440f,0.300706f,0.382683f,0.461749f, 585 | 0.537300f,0.608761f,0.675590f,0.737277f,0.793353f,0.843391f, 586 | 0.887011f,0.923880f,0.953717f,0.976296f,0.991445f,0.999048f, 587 | 1.000000f,1.000000f,1.000000f,1.000000f,1.000000f,1.000000f, 588 | 0.991445f,0.923880f,0.793353f,0.608761f,0.382683f,0.130526f, 589 | 0.000000f,0.000000f,0.000000f,0.000000f,0.000000f,0.000000f 590 | },{0.130526f,0.382683f,0.608761f,0.793353f,0.923880f,0.991445f, 591 | 0.991445f,0.923880f,0.793353f,0.608761f,0.382683f,0.130526f, 592 | 0.000000f,0.000000f,0.000000f,0.000000f,0.000000f,0.000000f, 593 | 0.000000f,0.000000f,0.000000f,0.000000f,0.000000f,0.000000f, 594 | 0.000000f,0.000000f,0.000000f,0.000000f,0.000000f,0.000000f, 595 | 0.000000f,0.000000f,0.000000f,0.000000f,0.000000f,0.000000f, 596 | },{0.000000f,0.000000f,0.000000f,0.000000f,0.000000f,0.000000f, 597 | 0.130526f,0.382683f,0.608761f,0.793353f,0.923880f,0.991445f, 598 | 1.000000f,1.000000f,1.000000f,1.000000f,1.000000f,1.000000f, 599 | 0.999048f,0.991445f,0.976296f,0.953717f,0.923879f,0.887011f, 600 | 0.843391f,0.793353f,0.737277f,0.675590f,0.608761f,0.537299f, 601 | 0.461748f,0.382683f,0.300706f,0.216439f,0.130526f,0.043619f, 602 | } 603 | }, 604 | #endif 605 | #ifdef IMDCT_NTABLES 606 | cos_N12[6][12] = { 607 | { 0.608761f,0.382683f,0.130526f,-0.130526f,-0.382683f,-0.608761f, 608 | -0.793353f,-0.923880f,-0.991445f,-0.991445f,-0.923879f,-0.793353f 609 | },{-0.923880f,-0.923879f,-0.382683f,0.382684f,0.923880f,0.923879f, 610 | 0.382683f,-0.382684f,-0.923880f,-0.923879f,-0.382683f,0.382684f 611 | },{-0.130526f,0.923880f,0.608761f,-0.608762f,-0.923879f,0.130526f, 612 | 0.991445f,0.382683f,-0.793354f,-0.793353f,0.382684f,0.991445f 613 | },{ 0.991445f,-0.382684f,-0.793353f,0.793354f,0.382683f,-0.991445f, 614 | 0.130527f,0.923879f,-0.608762f,-0.608761f,0.923880f,0.130525f 615 | },{-0.382684f,-0.382683f,0.923879f,-0.923880f,0.382684f,0.382683f, 616 | -0.923879f,0.923880f,-0.382684f,-0.382683f,0.923879f,-0.923880f 617 | },{-0.793353f,0.923879f,-0.991445f,0.991445f,-0.923880f,0.793354f, 618 | -0.608762f,0.382684f,-0.130527f,-0.130525f,0.382682f,-0.608761f,}, 619 | }, 620 | cos_N36[18][36] = { 621 | { 0.675590f,0.608761f,0.537300f,0.461749f,0.382683f,0.300706f, 622 | 0.216440f,0.130526f,0.043619f,-0.043619f,-0.130526f,-0.216440f, 623 | -0.300706f,-0.382684f,-0.461749f,-0.537300f,-0.608762f,-0.675590f, 624 | -0.737277f,-0.793353f,-0.843392f,-0.887011f,-0.923880f,-0.953717f, 625 | -0.976296f,-0.991445f,-0.999048f,-0.999048f,-0.991445f,-0.976296f, 626 | -0.953717f,-0.923879f,-0.887011f,-0.843391f,-0.793353f,-0.737277f 627 | },{-0.793353f,-0.923880f,-0.991445f,-0.991445f,-0.923879f,-0.793353f, 628 | -0.608761f,-0.382683f,-0.130526f,0.130526f,0.382684f,0.608762f, 629 | 0.793354f,0.923880f,0.991445f,0.991445f,0.923879f,0.793353f, 630 | 0.608761f,0.382683f,0.130526f,-0.130527f,-0.382684f,-0.608762f, 631 | -0.793354f,-0.923880f,-0.991445f,-0.991445f,-0.923879f,-0.793353f, 632 | -0.608761f,-0.382683f,-0.130526f,0.130527f,0.382684f,0.608762f 633 | },{-0.537299f,-0.130526f,0.300706f,0.675590f,0.923880f,0.999048f, 634 | 0.887011f,0.608761f,0.216439f,-0.216440f,-0.608762f,-0.887011f, 635 | -0.999048f,-0.923879f,-0.675590f,-0.300705f,0.130527f,0.537300f, 636 | 0.843392f,0.991445f,0.953717f,0.737277f,0.382683f,-0.043620f, 637 | -0.461749f,-0.793354f,-0.976296f,-0.976296f,-0.793353f,-0.461748f, 638 | -0.043618f,0.382684f,0.737278f,0.953717f,0.991445f,0.843391f 639 | },{ 0.887011f,0.991445f,0.737277f,0.216439f,-0.382684f,-0.843392f, 640 | -0.999048f,-0.793353f,-0.300705f,0.300706f,0.793354f,0.999048f, 641 | 0.843391f,0.382683f,-0.216440f,-0.737278f,-0.991445f,-0.887010f, 642 | -0.461748f,0.130527f,0.675591f,0.976296f,0.923879f,0.537299f, 643 | -0.043621f,-0.608762f,-0.953717f,-0.953717f,-0.608760f,-0.043618f, 644 | 0.537301f,0.923880f,0.976296f,0.675589f,0.130525f,-0.461750f 645 | },{ 0.382683f,-0.382684f,-0.923880f,-0.923879f,-0.382683f,0.382684f, 646 | 0.923880f,0.923879f,0.382683f,-0.382684f,-0.923880f,-0.923879f, 647 | -0.382683f,0.382684f,0.923880f,0.923879f,0.382682f,-0.382685f, 648 | -0.923880f,-0.923879f,-0.382682f,0.382685f,0.923880f,0.923879f, 649 | 0.382682f,-0.382685f,-0.923880f,-0.923879f,-0.382682f,0.382685f, 650 | 0.923880f,0.923879f,0.382682f,-0.382685f,-0.923880f,-0.923879f 651 | },{-0.953717f,-0.793353f,0.043620f,0.843392f,0.923879f,0.216439f, 652 | -0.675591f,-0.991445f,-0.461748f,0.461749f,0.991445f,0.675589f, 653 | -0.216441f,-0.923880f,-0.843391f,-0.043618f,0.793354f,0.953717f, 654 | 0.300704f,-0.608763f,-0.999048f,-0.537298f,0.382685f,0.976296f, 655 | 0.737276f,-0.130528f,-0.887012f,-0.887010f,-0.130524f,0.737279f, 656 | 0.976296f,0.382681f,-0.537301f,-0.999048f,-0.608760f,0.300708f 657 | },{-0.216439f,0.793354f,0.887010f,-0.043620f,-0.923880f,-0.737277f, 658 | 0.300707f,0.991445f,0.537299f,-0.537301f,-0.991445f,-0.300705f, 659 | 0.737278f,0.923879f,0.043618f,-0.887012f,-0.793352f,0.216441f, 660 | 0.976296f,0.608760f,-0.461750f,-0.999048f,-0.382682f,0.675592f, 661 | 0.953716f,0.130524f,-0.843393f,-0.843390f,0.130529f,0.953718f, 662 | 0.675588f,-0.382686f,-0.999048f,-0.461746f,0.608764f,0.976295f 663 | },{ 0.991445f,0.382683f,-0.793354f,-0.793353f,0.382684f,0.991445f, 664 | 0.130525f,-0.923880f,-0.608760f,0.608763f,0.923879f,-0.130528f, 665 | -0.991445f,-0.382682f,0.793354f,0.793352f,-0.382685f,-0.991445f, 666 | -0.130524f,0.923880f,0.608760f,-0.608763f,-0.923879f,0.130529f, 667 | 0.991445f,0.382681f,-0.793355f,-0.793352f,0.382686f,0.991444f, 668 | 0.130523f,-0.923881f,-0.608759f,0.608764f,0.923878f,-0.130529f 669 | },{ 0.043619f,-0.991445f,-0.216439f,0.953717f,0.382682f,-0.887011f, 670 | -0.537299f,0.793354f,0.675589f,-0.675591f,-0.793352f,0.537301f, 671 | 0.887010f,-0.382685f,-0.953716f,0.216442f,0.991445f,-0.043622f, 672 | -0.999048f,-0.130524f,0.976297f,0.300703f,-0.923881f,-0.461746f, 673 | 0.843393f,0.608759f,-0.737279f,-0.737275f,0.608764f,0.843390f, 674 | -0.461752f,-0.923878f,0.300709f,0.976295f,-0.130530f,-0.999048f 675 | },{-0.999048f,0.130527f,0.976296f,-0.300707f,-0.923879f,0.461750f, 676 | 0.843391f,-0.608763f,-0.737276f,0.737279f,0.608760f,-0.843392f, 677 | -0.461747f,0.923880f,0.300704f,-0.976297f,-0.130524f,0.999048f, 678 | -0.043622f,-0.991445f,0.216442f,0.953716f,-0.382686f,-0.887009f, 679 | 0.537302f,0.793351f,-0.675593f,-0.675588f,0.793355f,0.537297f, 680 | -0.887013f,-0.382680f,0.953718f,0.216436f,-0.991445f,-0.043615f 681 | },{ 0.130527f,0.923879f,-0.608762f,-0.608760f,0.923880f,0.130525f, 682 | -0.991445f,0.382685f,0.793352f,-0.793355f,-0.382682f,0.991445f, 683 | -0.130528f,-0.923879f,0.608763f,0.608759f,-0.923881f,-0.130523f, 684 | 0.991444f,-0.382686f,-0.793351f,0.793355f,0.382680f,-0.991445f, 685 | 0.130530f,0.923878f,-0.608764f,-0.608758f,0.923881f,0.130522f, 686 | -0.991444f,0.382687f,0.793351f,-0.793356f,-0.382679f,0.991445f 687 | },{ 0.976296f,-0.608762f,-0.461747f,0.999048f,-0.382685f,-0.675589f, 688 | 0.953717f,-0.130528f,-0.843390f,0.843393f,0.130524f,-0.953716f, 689 | 0.675592f,0.382681f,-0.999048f,0.461751f,0.608759f,-0.976297f, 690 | 0.216443f,0.793351f,-0.887012f,-0.043616f,0.923878f,-0.737280f, 691 | -0.300702f,0.991444f,-0.537303f,-0.537296f,0.991445f,-0.300710f, 692 | -0.737274f,0.923881f,-0.043624f,-0.887009f,0.793356f,0.216435f 693 | },{-0.300707f,-0.608760f,0.999048f,-0.537301f,-0.382682f,0.976296f, 694 | -0.737279f,-0.130524f,0.887010f,-0.887012f,0.130529f,0.737276f, 695 | -0.976297f,0.382686f,0.537297f,-0.999048f,0.608764f,0.300703f, 696 | -0.953716f,0.793355f,0.043616f,-0.843389f,0.923881f,-0.216444f, 697 | -0.675587f,0.991445f,-0.461752f,-0.461745f,0.991444f,-0.675594f, 698 | -0.216435f,0.923878f,-0.843394f,0.043625f,0.793350f,-0.953719f 699 | },{-0.923879f,0.923880f,-0.382685f,-0.382682f,0.923879f,-0.923880f, 700 | 0.382685f,0.382681f,-0.923879f,0.923880f,-0.382686f,-0.382681f, 701 | 0.923878f,-0.923881f,0.382686f,0.382680f,-0.923878f,0.923881f, 702 | -0.382687f,-0.382680f,0.923878f,-0.923881f,0.382687f,0.382679f, 703 | -0.923878f,0.923881f,-0.382688f,-0.382679f,0.923878f,-0.923881f, 704 | 0.382688f,0.382678f,-0.923877f,0.923882f,-0.382689f,-0.382678f 705 | },{ 0.461750f,0.130525f,-0.675589f,0.976296f,-0.923880f,0.537301f, 706 | 0.043617f,-0.608760f,0.953716f,-0.953718f,0.608764f,-0.043622f, 707 | -0.537297f,0.923878f,-0.976297f,0.675593f,-0.130530f,-0.461745f, 708 | 0.887009f,-0.991445f,0.737280f,-0.216444f,-0.382679f,0.843389f, 709 | -0.999048f,0.793356f,-0.300711f,-0.300701f,0.793350f,-0.999048f, 710 | 0.843394f,-0.382689f,-0.216434f,0.737273f,-0.991444f,0.887014f 711 | },{ 0.843391f,-0.991445f,0.953717f,-0.737279f,0.382685f,0.043617f, 712 | -0.461747f,0.793352f,-0.976295f,0.976297f,-0.793355f,0.461751f, 713 | -0.043623f,-0.382680f,0.737275f,-0.953716f,0.991445f,-0.843394f, 714 | 0.537303f,-0.130530f,-0.300702f,0.675587f,-0.923878f,0.999048f, 715 | -0.887013f,0.608766f,-0.216445f,-0.216434f,0.608757f,-0.887008f, 716 | 0.999048f,-0.923882f,0.675595f,-0.300712f,-0.130520f,0.537294f 717 | },{-0.608763f,0.382685f,-0.130528f,-0.130524f,0.382681f,-0.608760f, 718 | 0.793352f,-0.923879f,0.991444f,-0.991445f,0.923881f,-0.793355f, 719 | 0.608764f,-0.382687f,0.130530f,0.130522f,-0.382680f,0.608758f, 720 | -0.793351f,0.923878f,-0.991444f,0.991446f,-0.923881f,0.793357f, 721 | -0.608766f,0.382689f,-0.130532f,-0.130520f,0.382678f,-0.608756f, 722 | 0.793349f,-0.923877f,0.991444f,-0.991446f,0.923882f,-0.793358f 723 | },{-0.737276f,0.793352f,-0.843390f,0.887010f,-0.923879f,0.953716f, 724 | -0.976295f,0.991444f,-0.999048f,0.999048f,-0.991445f,0.976297f, 725 | -0.953718f,0.923881f,-0.887013f,0.843394f,-0.793356f,0.737280f, 726 | -0.675594f,0.608765f,-0.537304f,0.461753f,-0.382688f,0.300711f, 727 | -0.216445f,0.130532f,-0.043625f,-0.043613f,0.130520f,-0.216433f, 728 | 0.300699f,-0.382677f,0.461742f,-0.537293f,0.608755f,-0.675585f 729 | }}, 730 | #endif 731 | #ifdef POW34_ITERATE 732 | static const float powtab34[32] = { 733 | 0.000000f,1.000000f,2.519842f,4.326749f,6.349605f,8.549880f,10.902724f, 734 | 13.390519f,16.000001f,18.720756f,21.544349f,24.463783f,27.473145f,30.567354f, 735 | 33.741995f,36.993185f,40.317478f,43.711792f,47.173351f,50.699637f,54.288359f, 736 | 57.937415f,61.644873f,65.408949f,69.227988f,73.100453f,77.024908f,81.000011f, 737 | 85.024502f,89.097200f,93.216988f,97.382814f 738 | }, 739 | #endif 740 | g_synth_dtbl[512] = { 741 | 0.000000000,-0.000015259,-0.000015259,-0.000015259, 742 | -0.000015259,-0.000015259,-0.000015259,-0.000030518, 743 | -0.000030518,-0.000030518,-0.000030518,-0.000045776, 744 | -0.000045776,-0.000061035,-0.000061035,-0.000076294, 745 | -0.000076294,-0.000091553,-0.000106812,-0.000106812, 746 | -0.000122070,-0.000137329,-0.000152588,-0.000167847, 747 | -0.000198364,-0.000213623,-0.000244141,-0.000259399, 748 | -0.000289917,-0.000320435,-0.000366211,-0.000396729, 749 | -0.000442505,-0.000473022,-0.000534058,-0.000579834, 750 | -0.000625610,-0.000686646,-0.000747681,-0.000808716, 751 | -0.000885010,-0.000961304,-0.001037598,-0.001113892, 752 | -0.001205444,-0.001296997,-0.001388550,-0.001480103, 753 | -0.001586914,-0.001693726,-0.001785278,-0.001907349, 754 | -0.002014160,-0.002120972,-0.002243042,-0.002349854, 755 | -0.002456665,-0.002578735,-0.002685547,-0.002792358, 756 | -0.002899170,-0.002990723,-0.003082275,-0.003173828, 757 | 0.003250122, 0.003326416, 0.003387451, 0.003433228, 758 | 0.003463745, 0.003479004, 0.003479004, 0.003463745, 759 | 0.003417969, 0.003372192, 0.003280640, 0.003173828, 760 | 0.003051758, 0.002883911, 0.002700806, 0.002487183, 761 | 0.002227783, 0.001937866, 0.001617432, 0.001266479, 762 | 0.000869751, 0.000442505,-0.000030518,-0.000549316, 763 | -0.001098633,-0.001693726,-0.002334595,-0.003005981, 764 | -0.003723145,-0.004486084,-0.005294800,-0.006118774, 765 | -0.007003784,-0.007919312,-0.008865356,-0.009841919, 766 | -0.010848999,-0.011886597,-0.012939453,-0.014022827, 767 | -0.015121460,-0.016235352,-0.017349243,-0.018463135, 768 | -0.019577026,-0.020690918,-0.021789551,-0.022857666, 769 | -0.023910522,-0.024932861,-0.025909424,-0.026840210, 770 | -0.027725220,-0.028533936,-0.029281616,-0.029937744, 771 | -0.030532837,-0.031005859,-0.031387329,-0.031661987, 772 | -0.031814575,-0.031845093,-0.031738281,-0.031478882, 773 | 0.031082153, 0.030517578, 0.029785156, 0.028884888, 774 | 0.027801514, 0.026535034, 0.025085449, 0.023422241, 775 | 0.021575928, 0.019531250, 0.017257690, 0.014801025, 776 | 0.012115479, 0.009231567, 0.006134033, 0.002822876, 777 | -0.000686646,-0.004394531,-0.008316040,-0.012420654, 778 | -0.016708374,-0.021179199,-0.025817871,-0.030609131, 779 | -0.035552979,-0.040634155,-0.045837402,-0.051132202, 780 | -0.056533813,-0.061996460,-0.067520142,-0.073059082, 781 | -0.078628540,-0.084182739,-0.089706421,-0.095169067, 782 | -0.100540161,-0.105819702,-0.110946655,-0.115921021, 783 | -0.120697021,-0.125259399,-0.129562378,-0.133590698, 784 | -0.137298584,-0.140670776,-0.143676758,-0.146255493, 785 | -0.148422241,-0.150115967,-0.151306152,-0.151962280, 786 | -0.152069092,-0.151596069,-0.150497437,-0.148773193, 787 | -0.146362305,-0.143264771,-0.139450073,-0.134887695, 788 | -0.129577637,-0.123474121,-0.116577148,-0.108856201, 789 | 0.100311279, 0.090927124, 0.080688477, 0.069595337, 790 | 0.057617188, 0.044784546, 0.031082153, 0.016510010, 791 | 0.001068115,-0.015228271,-0.032379150,-0.050354004, 792 | -0.069168091,-0.088775635,-0.109161377,-0.130310059, 793 | -0.152206421,-0.174789429,-0.198059082,-0.221984863, 794 | -0.246505737,-0.271591187,-0.297210693,-0.323318481, 795 | -0.349868774,-0.376800537,-0.404083252,-0.431655884, 796 | -0.459472656,-0.487472534,-0.515609741,-0.543823242, 797 | -0.572036743,-0.600219727,-0.628295898,-0.656219482, 798 | -0.683914185,-0.711318970,-0.738372803,-0.765029907, 799 | -0.791213989,-0.816864014,-0.841949463,-0.866363525, 800 | -0.890090942,-0.913055420,-0.935195923,-0.956481934, 801 | -0.976852417,-0.996246338,-1.014617920,-1.031936646, 802 | -1.048156738,-1.063217163,-1.077117920,-1.089782715, 803 | -1.101211548,-1.111373901,-1.120223999,-1.127746582, 804 | -1.133926392,-1.138763428,-1.142211914,-1.144287109, 805 | 1.144989014, 1.144287109, 1.142211914, 1.138763428, 806 | 1.133926392, 1.127746582, 1.120223999, 1.111373901, 807 | 1.101211548, 1.089782715, 1.077117920, 1.063217163, 808 | 1.048156738, 1.031936646, 1.014617920, 0.996246338, 809 | 0.976852417, 0.956481934, 0.935195923, 0.913055420, 810 | 0.890090942, 0.866363525, 0.841949463, 0.816864014, 811 | 0.791213989, 0.765029907, 0.738372803, 0.711318970, 812 | 0.683914185, 0.656219482, 0.628295898, 0.600219727, 813 | 0.572036743, 0.543823242, 0.515609741, 0.487472534, 814 | 0.459472656, 0.431655884, 0.404083252, 0.376800537, 815 | 0.349868774, 0.323318481, 0.297210693, 0.271591187, 816 | 0.246505737, 0.221984863, 0.198059082, 0.174789429, 817 | 0.152206421, 0.130310059, 0.109161377, 0.088775635, 818 | 0.069168091, 0.050354004, 0.032379150, 0.015228271, 819 | -0.001068115,-0.016510010,-0.031082153,-0.044784546, 820 | -0.057617188,-0.069595337,-0.080688477,-0.090927124, 821 | 0.100311279, 0.108856201, 0.116577148, 0.123474121, 822 | 0.129577637, 0.134887695, 0.139450073, 0.143264771, 823 | 0.146362305, 0.148773193, 0.150497437, 0.151596069, 824 | 0.152069092, 0.151962280, 0.151306152, 0.150115967, 825 | 0.148422241, 0.146255493, 0.143676758, 0.140670776, 826 | 0.137298584, 0.133590698, 0.129562378, 0.125259399, 827 | 0.120697021, 0.115921021, 0.110946655, 0.105819702, 828 | 0.100540161, 0.095169067, 0.089706421, 0.084182739, 829 | 0.078628540, 0.073059082, 0.067520142, 0.061996460, 830 | 0.056533813, 0.051132202, 0.045837402, 0.040634155, 831 | 0.035552979, 0.030609131, 0.025817871, 0.021179199, 832 | 0.016708374, 0.012420654, 0.008316040, 0.004394531, 833 | 0.000686646,-0.002822876,-0.006134033,-0.009231567, 834 | -0.012115479,-0.014801025,-0.017257690,-0.019531250, 835 | -0.021575928,-0.023422241,-0.025085449,-0.026535034, 836 | -0.027801514,-0.028884888,-0.029785156,-0.030517578, 837 | 0.031082153, 0.031478882, 0.031738281, 0.031845093, 838 | 0.031814575, 0.031661987, 0.031387329, 0.031005859, 839 | 0.030532837, 0.029937744, 0.029281616, 0.028533936, 840 | 0.027725220, 0.026840210, 0.025909424, 0.024932861, 841 | 0.023910522, 0.022857666, 0.021789551, 0.020690918, 842 | 0.019577026, 0.018463135, 0.017349243, 0.016235352, 843 | 0.015121460, 0.014022827, 0.012939453, 0.011886597, 844 | 0.010848999, 0.009841919, 0.008865356, 0.007919312, 845 | 0.007003784, 0.006118774, 0.005294800, 0.004486084, 846 | 0.003723145, 0.003005981, 0.002334595, 0.001693726, 847 | 0.001098633, 0.000549316, 0.000030518,-0.000442505, 848 | -0.000869751,-0.001266479,-0.001617432,-0.001937866, 849 | -0.002227783,-0.002487183,-0.002700806,-0.002883911, 850 | -0.003051758,-0.003173828,-0.003280640,-0.003372192, 851 | -0.003417969,-0.003463745,-0.003479004,-0.003479004, 852 | -0.003463745,-0.003433228,-0.003387451,-0.003326416, 853 | 0.003250122, 0.003173828, 0.003082275, 0.002990723, 854 | 0.002899170, 0.002792358, 0.002685547, 0.002578735, 855 | 0.002456665, 0.002349854, 0.002243042, 0.002120972, 856 | 0.002014160, 0.001907349, 0.001785278, 0.001693726, 857 | 0.001586914, 0.001480103, 0.001388550, 0.001296997, 858 | 0.001205444, 0.001113892, 0.001037598, 0.000961304, 859 | 0.000885010, 0.000808716, 0.000747681, 0.000686646, 860 | 0.000625610, 0.000579834, 0.000534058, 0.000473022, 861 | 0.000442505, 0.000396729, 0.000366211, 0.000320435, 862 | 0.000289917, 0.000259399, 0.000244141, 0.000213623, 863 | 0.000198364, 0.000167847, 0.000152588, 0.000137329, 864 | 0.000122070, 0.000106812, 0.000106812, 0.000091553, 865 | 0.000076294, 0.000076294, 0.000061035, 0.000061035, 866 | 0.000045776, 0.000045776, 0.000030518, 0.000030518, 867 | 0.000030518, 0.000030518, 0.000015259, 0.000015259, 868 | 0.000015259, 0.000015259, 0.000015259, 0.000015259, 869 | //},g_synth_n_win[64][32]={ 870 | }; 871 | 872 | 873 | /* Scale factor band indices 874 | * 875 | * One table per sample rate. Each table contains the frequency indices 876 | * for the 12 short and 21 long scalefactor bands. The short indices 877 | * must be multiplied by 3 to get the actual index. 878 | */ 879 | static const t_sf_band_indices g_sf_band_indices[3 /* Sampling freq. */] = { 880 | { 881 | {0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576}, 882 | {0,4,8,12,16,22,30,40,52,66,84,106,136,192} 883 | }, 884 | { 885 | {0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576}, 886 | {0,4,8,12,16,22,28,38,50,64,80,100,126,192} 887 | }, 888 | { 889 | {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576}, 890 | {0,4,8,12,16,22,30,42,58,78,104,138,180,192} 891 | } 892 | }; 893 | 894 | #ifdef DEBUG 895 | static void dmp_fr(t_mpeg1_header *hdr){ 896 | printf("rate %d,sfreq %d,pad %d,mod %d,modext %d,emph %d\n", 897 | hdr->bitrate_index,hdr->sampling_frequency,hdr->padding_bit, 898 | hdr->mode,hdr->mode_extension,hdr->emphasis); 899 | } 900 | 901 | static void dmp_si(t_mpeg1_header *hdr,t_mpeg1_side_info *si){ 902 | int nch,ch,gr; 903 | 904 | nch = hdr->mode == mpeg1_mode_single_channel ? 1 : 2; 905 | printf("main_data_begin %d,priv_bits %d\n",si->main_data_begin,si->private_bits); 906 | for(ch = 0; ch < nch; ch++) { 907 | printf("scfsi %d %d %d %d\n",si->scfsi[ch][0],si->scfsi[ch][1],si->scfsi[ch][2],si->scfsi[ch][3]); 908 | for(gr = 0; gr < 2; gr++) { 909 | printf("p23l %d,bv %d,gg %d,scfc %d,wsf %d,bt %d\n", 910 | si->part2_3_length[gr][ch],si->big_values[gr][ch], 911 | si->global_gain[gr][ch],si->scalefac_compress[gr][ch], 912 | si->win_switch_flag[gr][ch],si->block_type[gr][ch]); 913 | if(si->win_switch_flag[gr][ch]) { 914 | printf("mbf %d,ts1 %d,ts2 %d,sbg1 %d,sbg2 %d,sbg3 %d\n", 915 | si->mixed_block_flag[gr][ch],si->table_select[gr][ch][0], 916 | si->table_select[gr][ch][1],si->subblock_gain[gr][ch][0], 917 | si->subblock_gain[gr][ch][1],si->subblock_gain[gr][ch][2]); 918 | }else{ 919 | printf("ts1 %d,ts2 %d,ts3 %d\n",si->table_select[gr][ch][0], 920 | si->table_select[gr][ch][1],si->table_select[gr][ch][2]); 921 | } 922 | printf("r0c %d,r1c %d\n",si->region0_count[gr][ch],si->region1_count[gr][ch]); 923 | printf("pf %d,scfs %d,c1ts %d\n",si->preflag[gr][ch],si->scalefac_scale[gr][ch],si->count1table_select[gr][ch]); 924 | } 925 | } 926 | } 927 | 928 | static void dmp_scf(t_mpeg1_side_info *si,t_mpeg1_main_data *md,int gr,int ch){ 929 | int sfb,win; 930 | 931 | if((si->win_switch_flag[gr][ch] != 0) &&(si->block_type[gr][ch] == 2)) { 932 | if(si->mixed_block_flag[gr][ch] != 0) { /* First the long block scalefacs */ 933 | for(sfb = 0; sfb < 8; sfb++) 934 | printf("scfl%d %d%s",sfb,md->scalefac_l[gr][ch][sfb],(sfb==7)?"\n":","); 935 | for(sfb = 3; sfb < 12; sfb++) /* And next the short block scalefacs */ 936 | for(win = 0; win < 3; win++) 937 | printf("scfs%d,%d %d%s",sfb,win,md->scalefac_s[gr][ch][sfb][win],(win==2)?"\n":","); 938 | }else{ /* Just short blocks */ 939 | for(sfb = 0; sfb < 12; sfb++) 940 | for(win = 0; win < 3; win++) 941 | printf("scfs%d,%d %d%s",sfb,win,md->scalefac_s[gr][ch][sfb][win],(win==2)?"\n":","); 942 | } 943 | }else for(sfb = 0; sfb < 21; sfb++) /* Just long blocks; scalefacs first */ 944 | printf("scfl%d %d%s",sfb,md->scalefac_l[gr][ch][sfb](sfb == 20)?"\n":","); 945 | } 946 | 947 | static void dmp_huff(t_mpeg1_main_data *md,int gr,int ch){ 948 | int i; 949 | printf("HUFFMAN\n"); 950 | for(i = 0; i < 576; i++) printf("%d: %d\n",i,(int) md->is[gr][ch][i]); 951 | } 952 | 953 | static void dmp_samples(t_mpeg1_main_data *md,int gr,int ch,int type){ 954 | int i,val; 955 | extern double rint(double); 956 | 957 | printf("SAMPLES%d\n",type); 958 | for(i = 0; i < 576; i++) { 959 | val =(int) rint(md->is[gr][ch][i] * 32768.0); 960 | if(val >= 32768) val = 32767; 961 | if(val < -32768) val = -32768; 962 | printf("%d: %d\n",i,val); 963 | } 964 | } 965 | #endif 966 | 967 | /**Description: calculates y=x^(4/3) when requantizing samples. 968 | * Parameters: TBD 969 | * Return value: TBD 970 | * Author: Krister Lagerström(krister@kmlager.com) **/ 971 | static inline float Requantize_Pow_43(unsigned is_pos){ 972 | #ifdef POW34_TABLE 973 | static float powtab34[8207]; 974 | static int init = 0; 975 | int i; 976 | 977 | if(init == 0) { /* First time initialization */ 978 | for(i = 0; i < 8207; i++) 979 | powtab34[i] = pow((float) i,4.0 / 3.0); 980 | init = 1; 981 | } 982 | #ifdef DEBUG 983 | if(is_pos > 8206) { 984 | ERR("is_pos = %d larger than 8206!",is_pos); 985 | is_pos = 8206; 986 | } 987 | #endif /* DEBUG */ 988 | return(powtab34[is_pos]); /* Done */ 989 | #elif defined POW34_ITERATE 990 | float a4,a2,x,x2,x3,x_next,is_f1,is_f2,is_f3; 991 | unsigned i; 992 | //static unsigned init = 0; 993 | //static float powtab34[32]; 994 | static float coeff[3] = {-1.030797119e+02,6.319399834e+00,2.395095071e-03}; 995 | //if(init == 0) { /* First time initialization */ 996 | // for(i = 0; i < 32; i++) powtab34[i] = pow((float) i,4.0 / 3.0); 997 | // init = 1; 998 | //} 999 | /* We use a table for 0g_frame_header.mode == mpeg1_mode_single_channel ? 1 : 2); 1029 | for(gr = 0; gr < 2; gr++) { 1030 | for(ch = 0; ch < nch; ch++) { 1031 | dmp_scf(&id->g_side_info,&id->g_main_data,gr,ch); //noop unless debug 1032 | dmp_huff(&id->g_main_data,gr,ch); //noop unless debug 1033 | L3_Requantize(id,gr,ch); /* Requantize samples */ 1034 | dmp_samples(&id->g_main_data,gr,ch,0); //noop unless debug 1035 | L3_Reorder(id,gr,ch); /* Reorder short blocks */ 1036 | } /* end for(ch... */ 1037 | L3_Stereo(id,gr); /* Stereo processing */ 1038 | dmp_samples(&id->g_main_data,gr,0,1); //noop unless debug 1039 | dmp_samples(&id->g_main_data,gr,1,1); //noop unless debug 1040 | for(ch = 0; ch < nch; ch++) { 1041 | L3_Antialias(id,gr,ch); /* Antialias */ 1042 | dmp_samples(&id->g_main_data,gr,ch,2); //noop unless debug 1043 | L3_Hybrid_Synthesis(id,gr,ch); /*(IMDCT,windowing,overlapp add) */ 1044 | L3_Frequency_Inversion(id,gr,ch); /* Frequency inversion */ 1045 | dmp_samples(&id->g_main_data,gr,ch,3); //noop unless debug 1046 | L3_Subband_Synthesis(id,gr,ch,id->out[gr]); /* Polyphase subband synthesis */ 1047 | } /* end for(ch... */ 1048 | #ifdef DEBUG 1049 | { 1050 | int i,ctr = 0; 1051 | printf("PCM:\n"); 1052 | for(i = 0; i < 576; i++) { 1053 | printf("%d: %d\n",ctr++,(out[i] >> 16) & 0xffff); 1054 | if(nch == 2) printf("%d: %d\n",ctr++,out[i] & 0xffff); 1055 | } 1056 | } 1057 | #endif /* DEBUG */ 1058 | } /* end for(gr... */ 1059 | return(PDMP3_OK); /* Done */ 1060 | } 1061 | 1062 | static unsigned Get_Inbuf_Filled(pdmp3_handle *id) { 1063 | return (id->istart<=id->iend)?(id->iend-id->istart):(INBUF_SIZE-id->istart+id->iend); 1064 | } 1065 | 1066 | static unsigned Get_Inbuf_Free(pdmp3_handle *id) { 1067 | return (id->iendistart)?(id->istart-id->iend):(INBUF_SIZE-id->iend+id->istart); 1068 | } 1069 | 1070 | 1071 | /** Description: reads 'no_of_bytes' from input stream into 'data_vec[]'. 1072 | * Parameters: Stream handle,number of bytes to read,vector pointer where to 1073 | store them. 1074 | * Return value: PDMP3_OK or PDMP3_ERR if the operation couldn't be performed. 1075 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1076 | static int Get_Bytes(pdmp3_handle *id,unsigned no_of_bytes,unsigned data_vec[]){ 1077 | int i; 1078 | unsigned val; 1079 | 1080 | for(i = 0; i < no_of_bytes; i++) { 1081 | val = Get_Byte(id); 1082 | if(val == C_EOF) return(C_EOF); 1083 | else data_vec[i] = val; 1084 | } 1085 | return(PDMP3_OK); 1086 | } 1087 | 1088 | /** Description: This function assembles the main data buffer with data from 1089 | * this frame and the previous two frames into a local buffer 1090 | * used by the Get_Main_Bits function. 1091 | * Parameters: Stream handle,main_data_begin indicates how many bytes from 1092 | * previous frames that should be used. main_data_size indicates the 1093 | * number of data bytes in this frame. 1094 | * Return value: Status 1095 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1096 | static int Get_Main_Data(pdmp3_handle *id,unsigned main_data_size,unsigned main_data_begin){ 1097 | int i; 1098 | 1099 | if(main_data_size > 1500) ERR("main_data_size = %d\n",main_data_size); 1100 | /* Check that there's data available from previous frames if needed */ 1101 | if(main_data_begin > id->g_main_data_top) { 1102 | /* No,there is not,so we skip decoding this frame,but we have to 1103 | * read the main_data bits from the bitstream in case they are needed 1104 | * for decoding the next frame. */ 1105 | (void) Get_Bytes(id,main_data_size,&(id->g_main_data_vec[id->g_main_data_top])); 1106 | /* Set up pointers */ 1107 | id->g_main_data_ptr = &(id->g_main_data_vec[0]); 1108 | id->g_main_data_idx = 0; 1109 | id->g_main_data_top += main_data_size; 1110 | return(PDMP3_NEED_MORE); /* This frame cannot be decoded! */ 1111 | } 1112 | for(i = 0; i < main_data_begin; i++) { /* Copy data from previous frames */ 1113 | id->g_main_data_vec[i] = id->g_main_data_vec[id->g_main_data_top - main_data_begin + i]; 1114 | } 1115 | /* Read the main_data from file */ 1116 | (void) Get_Bytes(id,main_data_size,&(id->g_main_data_vec[main_data_begin])); 1117 | /* Set up pointers */ 1118 | id->g_main_data_ptr = &(id->g_main_data_vec[0]); 1119 | id->g_main_data_idx = 0; 1120 | id->g_main_data_top = main_data_begin + main_data_size; 1121 | return(PDMP3_OK); /* Done */ 1122 | } 1123 | 1124 | /**Description: Reads audio and main data from bitstream into a buffer. main 1125 | * data is taken from this frame and up to 2 previous frames. 1126 | * Parameters: Stream handle. 1127 | * Return value: PDMP3_OK or PDMP3_ERR if data could not be read,or contains errors. 1128 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1129 | static int Read_Audio_L3(pdmp3_handle *id){ 1130 | unsigned framesize,sideinfo_size,main_data_size,nch,ch,gr,scfsi_band,region,window; 1131 | 1132 | /* Number of channels(1 for mono and 2 for stereo) */ 1133 | nch =(id->g_frame_header.mode == mpeg1_mode_single_channel ? 1 : 2); 1134 | /* Calculate header audio data size */ 1135 | framesize = (144 * 1136 | g_mpeg1_bitrates[id->g_frame_header.layer-1][id->g_frame_header.bitrate_index]) / 1137 | g_sampling_frequency[id->g_frame_header.sampling_frequency] + 1138 | id->g_frame_header.padding_bit; 1139 | if(framesize > 2000) { 1140 | ERR("framesize = %d\n",framesize); 1141 | return(PDMP3_ERR); 1142 | } 1143 | /* Sideinfo is 17 bytes for one channel and 32 bytes for two */ 1144 | sideinfo_size =(nch == 1 ? 17 : 32); 1145 | /* Main data size is the rest of the frame,including ancillary data */ 1146 | main_data_size = framesize - sideinfo_size - 4 /* sync+header */; 1147 | /* CRC is 2 bytes */ 1148 | if(id->g_frame_header.protection_bit == 0) main_data_size -= 2; 1149 | /* DBG("framesize = %d\n",framesize); */ 1150 | /* DBG("sideinfo_size = %d\n",sideinfo_size); */ 1151 | /* DBG("main_data_size = %d\n",main_data_size); */ 1152 | /* Read sideinfo from bitstream into buffer used by Get_Side_Bits() */ 1153 | Get_Sideinfo(id,sideinfo_size); 1154 | if(Get_Filepos(id) == C_EOF) return(PDMP3_ERR); 1155 | /* Parse audio data */ 1156 | /* Pointer to where we should start reading main data */ 1157 | id->g_side_info.main_data_begin = Get_Side_Bits(id,9); 1158 | /* Get private bits. Not used for anything. */ 1159 | if(id->g_frame_header.mode == mpeg1_mode_single_channel) 1160 | id->g_side_info.private_bits = Get_Side_Bits(id,5); 1161 | else id->g_side_info.private_bits = Get_Side_Bits(id,3); 1162 | /* Get scale factor selection information */ 1163 | for(ch = 0; ch < nch; ch++) 1164 | for(scfsi_band = 0; scfsi_band < 4; scfsi_band++) 1165 | id->g_side_info.scfsi[ch][scfsi_band] = Get_Side_Bits(id,1); 1166 | /* Get the rest of the side information */ 1167 | for(gr = 0; gr < 2; gr++) { 1168 | for(ch = 0; ch < nch; ch++) { 1169 | id->g_side_info.part2_3_length[gr][ch] = Get_Side_Bits(id,12); 1170 | id->g_side_info.big_values[gr][ch] = Get_Side_Bits(id,9); 1171 | id->g_side_info.global_gain[gr][ch] = Get_Side_Bits(id,8); 1172 | id->g_side_info.scalefac_compress[gr][ch] = Get_Side_Bits(id,4); 1173 | id->g_side_info.win_switch_flag[gr][ch] = Get_Side_Bits(id,1); 1174 | if(id->g_side_info.win_switch_flag[gr][ch] == 1) { 1175 | id->g_side_info.block_type[gr][ch] = Get_Side_Bits(id,2); 1176 | id->g_side_info.mixed_block_flag[gr][ch] = Get_Side_Bits(id,1); 1177 | for(region = 0; region < 2; region++) 1178 | id->g_side_info.table_select[gr][ch][region] = Get_Side_Bits(id,5); 1179 | for(window = 0; window < 3; window++) 1180 | id->g_side_info.subblock_gain[gr][ch][window] = Get_Side_Bits(id,3); 1181 | if((id->g_side_info.block_type[gr][ch]==2)&&(id->g_side_info.mixed_block_flag[gr][ch]==0)) 1182 | id->g_side_info.region0_count[gr][ch] = 8; /* Implicit */ 1183 | else id->g_side_info.region0_count[gr][ch] = 7; /* Implicit */ 1184 | /* The standard is wrong on this!!! */ /* Implicit */ 1185 | id->g_side_info.region1_count[gr][ch] = 20 - id->g_side_info.region0_count[gr][ch]; 1186 | }else{ 1187 | for(region = 0; region < 3; region++) 1188 | id->g_side_info.table_select[gr][ch][region] = Get_Side_Bits(id,5); 1189 | id->g_side_info.region0_count[gr][ch] = Get_Side_Bits(id,4); 1190 | id->g_side_info.region1_count[gr][ch] = Get_Side_Bits(id,3); 1191 | id->g_side_info.block_type[gr][ch] = 0; /* Implicit */ 1192 | } /* end if ... */ 1193 | id->g_side_info.preflag[gr][ch] = Get_Side_Bits(id,1); 1194 | id->g_side_info.scalefac_scale[gr][ch] = Get_Side_Bits(id,1); 1195 | id->g_side_info.count1table_select[gr][ch] = Get_Side_Bits(id,1); 1196 | } /* end for(channel... */ 1197 | } /* end for(granule... */ 1198 | return(PDMP3_OK);/* Done */ 1199 | 1200 | } 1201 | 1202 | /**Description: Reads 16 CRC bits 1203 | * Parameters: Stream handle. 1204 | * Return value: PDMP3_OK or PDMP3_ERR if CRC could not be read. 1205 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1206 | static int Read_CRC(pdmp3_handle *id){ 1207 | /* Get next two bytes from bitstream, If we got an End Of File we're done */ 1208 | if((Get_Byte(id)==C_EOF)||(Get_Byte(id)==C_EOF)) return(FALSE); 1209 | return(PDMP3_OK); /* Done */ 1210 | } 1211 | 1212 | /**Description: Search for next frame and read it into buffer. Main data in 1213 | this frame is saved for two frames since it might be needed by them. 1214 | * Parameters: Stream handle. 1215 | * Return value: PDMP3_OK if a frame is successfully read,PDMP3_ERR otherwise. 1216 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1217 | static int Read_Frame(pdmp3_handle *id){ 1218 | /* Try to find the next frame in the bitstream and decode it */ 1219 | if(Search_Header(id) != PDMP3_OK) return(PDMP3_ERR); 1220 | #ifdef DEBUG 1221 | { static int framenum = 0; 1222 | printf("\nFrame %d\n",framenum++); 1223 | dmp_fr(&id->g_frame_header); 1224 | } 1225 | DBG("Starting decode,Layer: %d,Rate: %6d,Sfreq: %05d", 1226 | id->g_frame_header.layer, 1227 | g_mpeg1_bitrates[id->g_frame_header.layer - 1][id->g_frame_header.bitrate_index], 1228 | g_sampling_frequency[id->g_frame_header.sampling_frequency]); 1229 | #endif 1230 | /* Get CRC word if present */ 1231 | if((id->g_frame_header.protection_bit==0)&&(Read_CRC(id)!=PDMP3_OK)) return(PDMP3_ERR); 1232 | if(id->g_frame_header.layer == 3) { /* Get audio data */ 1233 | Read_Audio_L3(id); /* Get side info */ 1234 | dmp_si(&id->g_frame_header,&id->g_side_info); /* DEBUG */ 1235 | /* If there's not enough main data in the bit reservoir, 1236 | * signal to calling function so that decoding isn't done! */ 1237 | /* Get main data(scalefactors and Huffman coded frequency data) */ 1238 | return(Read_Main_L3(id)); 1239 | }else{ 1240 | ERR("Only layer 3(!= %d) is supported!\n",id->g_frame_header.layer); 1241 | return(PDMP3_ERR); 1242 | } 1243 | return(PDMP3_OK); 1244 | } 1245 | 1246 | /**Description: Scans bitstream for syncword until we find it or EOF. The 1247 | syncword must be byte-aligned. It then reads and parses audio header. 1248 | * Parameters: Stream handle. 1249 | * Return value: PDMP3_OK or PDMP3_ERR if the syncword can't be found,or the header 1250 | * contains impossible values. 1251 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1252 | static int Read_Header(pdmp3_handle *id) { 1253 | unsigned b1,b2,b3,b4,header; 1254 | /* Get the next four bytes from the bitstream */ 1255 | b1 = Get_Byte(id); 1256 | b2 = Get_Byte(id); 1257 | b3 = Get_Byte(id); 1258 | b4 = Get_Byte(id); 1259 | /* If we got an End Of File condition we're done */ 1260 | if((b1==C_EOF)||(b2==C_EOF)||(b3==C_EOF)||(b4==C_EOF)) 1261 | return(PDMP3_ERR); 1262 | header =(b1 << 24) |(b2 << 16) |(b3 << 8) |(b4 << 0); 1263 | /* Are the high 12 bits the syncword(0xfff)? */ 1264 | while((header & 0xfff00000) != C_SYNC) { 1265 | /* No,so scan the bitstream one byte at a time until we find it or EOF */ 1266 | /* Shift the values one byte to the left */ 1267 | b1 = b2; 1268 | b2 = b3; 1269 | b3 = b4; 1270 | /* Get one new byte from the bitstream */ 1271 | b4 = Get_Byte(id); 1272 | /* If we got an End Of File condition we're done */ 1273 | if(b4 == C_EOF) return(PDMP3_ERR); 1274 | /* Make up the new header */ 1275 | header =(b1 << 24) |(b2 << 16) |(b3 << 8) |(b4 << 0); 1276 | } /* while... */ 1277 | /* If we get here we've found the sync word,and can decode the header 1278 | * which is in the low 20 bits of the 32-bit sync+header word. */ 1279 | /* Decode the header */ 1280 | id->g_frame_header.id =(header & 0x00080000) >> 19; 1281 | id->g_frame_header.layer =(header & 0x00060000) >> 17; 1282 | id->g_frame_header.protection_bit =(header & 0x00010000) >> 16; 1283 | id->g_frame_header.bitrate_index =(header & 0x0000f000) >> 12; 1284 | id->g_frame_header.sampling_frequency =(header & 0x00000c00) >> 10; 1285 | id->g_frame_header.padding_bit =(header & 0x00000200) >> 9; 1286 | id->g_frame_header.private_bit =(header & 0x00000100) >> 8; 1287 | id->g_frame_header.mode =(header & 0x000000c0) >> 6; 1288 | id->g_frame_header.mode_extension =(header & 0x00000030) >> 4; 1289 | id->g_frame_header.copyright =(header & 0x00000008) >> 3; 1290 | id->g_frame_header.original_or_copy =(header & 0x00000004) >> 2; 1291 | id->g_frame_header.emphasis =(header & 0x00000003) >> 0; 1292 | /* Check for invalid values and impossible combinations */ 1293 | if(id->g_frame_header.id != 1) { 1294 | ERR("ID must be 1\nHeader word is 0x%08x at file pos %d\n",header,Get_Filepos(id)); 1295 | return(PDMP3_ERR); 1296 | } 1297 | if(id->g_frame_header.bitrate_index == 0) { 1298 | ERR("Free bitrate format NIY!\nHeader word is 0x%08x at file pos %d\n",header,Get_Filepos(id)); 1299 | return(PDMP3_ERR); // exit(1); 1300 | } 1301 | if(id->g_frame_header.bitrate_index == 15) { 1302 | ERR("bitrate_index = 15 is invalid!\nHeader word is 0x%08x at file pos %d\n",header,Get_Filepos(id)); 1303 | return(PDMP3_ERR); 1304 | } 1305 | if(id->g_frame_header.sampling_frequency == 3) { 1306 | ERR("sampling_frequency = 3 is invalid!\n"); 1307 | ERR("Header word is 0x%08x at file pos %d\n",header,Get_Filepos(id)); 1308 | return(PDMP3_ERR); 1309 | } 1310 | if(id->g_frame_header.layer == 0) { 1311 | ERR("layer = 0 is invalid!\n"); 1312 | ERR("Header word is 0x%08x at file pos %d\n",header, 1313 | Get_Filepos(id)); 1314 | return(PDMP3_ERR); 1315 | } 1316 | id->g_frame_header.layer = 4 - id->g_frame_header.layer; 1317 | /* DBG("Header = 0x%08x\n",header); */ 1318 | if(!id->new_header) id->new_header = 1; 1319 | return(PDMP3_OK); /* Done */ 1320 | } 1321 | 1322 | static int Search_Header(pdmp3_handle *id) { 1323 | unsigned pos = id->processed; 1324 | unsigned mark = id->istart; 1325 | int res = PDMP3_NEED_MORE; 1326 | int cnt = 0; 1327 | while(Get_Inbuf_Filled(id) > 4) { 1328 | res = Read_Header(id); 1329 | if (id->g_frame_header.layer == 3) { 1330 | if(res == PDMP3_OK || res == PDMP3_NEW_FORMAT) break; 1331 | } 1332 | if (++mark == INBUF_SIZE) { 1333 | mark = 0; 1334 | } 1335 | id->istart = mark; 1336 | id->processed = pos; 1337 | if (++cnt > (2*576)) return(PDMP3_ERR); /* more than one frame and still no header */ 1338 | } 1339 | return res; 1340 | } 1341 | 1342 | /**Description: reads main data for layer 3 from main_data bit reservoir. 1343 | * Parameters: Stream handle. 1344 | * Return value: PDMP3_OK or PDMP3_ERR if the data contains errors. 1345 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1346 | static int Read_Main_L3(pdmp3_handle *id){ 1347 | unsigned framesize,sideinfo_size,main_data_size,gr,ch,nch,sfb,win,slen1,slen2,nbits,part_2_start; 1348 | int res; 1349 | 1350 | /* Number of channels(1 for mono and 2 for stereo) */ 1351 | nch =(id->g_frame_header.mode == mpeg1_mode_single_channel ? 1 : 2); 1352 | 1353 | /* Calculate header audio data size */ 1354 | framesize = (144 * 1355 | g_mpeg1_bitrates[id->g_frame_header.layer-1][id->g_frame_header.bitrate_index]) / 1356 | g_sampling_frequency[id->g_frame_header.sampling_frequency] + 1357 | id->g_frame_header.padding_bit; 1358 | 1359 | if(framesize > 2000) { 1360 | ERR("framesize = %d\n",framesize); 1361 | return(PDMP3_ERR); 1362 | } 1363 | /* Sideinfo is 17 bytes for one channel and 32 bytes for two */ 1364 | sideinfo_size =(nch == 1 ? 17 : 32); 1365 | /* Main data size is the rest of the frame,including ancillary data */ 1366 | main_data_size = framesize - sideinfo_size - 4 /* sync+header */; 1367 | /* CRC is 2 bytes */ 1368 | if(id->g_frame_header.protection_bit == 0) main_data_size -= 2; 1369 | /* Assemble main data buffer with data from this frame and the previous 1370 | * two frames. main_data_begin indicates how many bytes from previous 1371 | * frames that should be used. This buffer is later accessed by the 1372 | * Get_Main_Bits function in the same way as the side info is. 1373 | */ 1374 | res = Get_Main_Data(id,main_data_size,id->g_side_info.main_data_begin); 1375 | if(res != PDMP3_OK) return(res); /* This could be due to not enough data in reservoir */ 1376 | for(gr = 0; gr < 2; gr++) { 1377 | for(ch = 0; ch < nch; ch++) { 1378 | part_2_start = Get_Main_Pos(id); 1379 | /* Number of bits in the bitstream for the bands */ 1380 | slen1 = mpeg1_scalefac_sizes[id->g_side_info.scalefac_compress[gr][ch]][0]; 1381 | slen2 = mpeg1_scalefac_sizes[id->g_side_info.scalefac_compress[gr][ch]][1]; 1382 | if((id->g_side_info.win_switch_flag[gr][ch] != 0)&&(id->g_side_info.block_type[gr][ch] == 2)) { 1383 | if(id->g_side_info.mixed_block_flag[gr][ch] != 0) { 1384 | for(sfb = 0; sfb < 8; sfb++) 1385 | id->g_main_data.scalefac_l[gr][ch][sfb] = Get_Main_Bits(id,slen1); 1386 | for(sfb = 3; sfb < 12; sfb++) { 1387 | nbits = (sfb < 6)?slen1:slen2;/*slen1 for band 3-5,slen2 for 6-11*/ 1388 | for(win = 0; win < 3; win++) 1389 | id->g_main_data.scalefac_s[gr][ch][sfb][win]=Get_Main_Bits(id,nbits); 1390 | } 1391 | }else{ 1392 | for(sfb = 0; sfb < 12; sfb++){ 1393 | nbits = (sfb < 6)?slen1:slen2;/*slen1 for band 3-5,slen2 for 6-11*/ 1394 | for(win = 0; win < 3; win++) 1395 | id->g_main_data.scalefac_s[gr][ch][sfb][win]=Get_Main_Bits(id,nbits); 1396 | } 1397 | } 1398 | }else{ /* block_type == 0 if winswitch == 0 */ 1399 | /* Scale factor bands 0-5 */ 1400 | if((id->g_side_info.scfsi[ch][0] == 0) ||(gr == 0)) { 1401 | for(sfb = 0; sfb < 6; sfb++) 1402 | id->g_main_data.scalefac_l[gr][ch][sfb] = Get_Main_Bits(id,slen1); 1403 | }else if((id->g_side_info.scfsi[ch][0] == 1) &&(gr == 1)) { 1404 | /* Copy scalefactors from granule 0 to granule 1 */ 1405 | for(sfb = 0; sfb < 6; sfb++) 1406 | id->g_main_data.scalefac_l[1][ch][sfb]=id->g_main_data.scalefac_l[0][ch][sfb]; 1407 | } 1408 | /* Scale factor bands 6-10 */ 1409 | if((id->g_side_info.scfsi[ch][1] == 0) ||(gr == 0)) { 1410 | for(sfb = 6; sfb < 11; sfb++) 1411 | id->g_main_data.scalefac_l[gr][ch][sfb] = Get_Main_Bits(id,slen1); 1412 | }else if((id->g_side_info.scfsi[ch][1] == 1) &&(gr == 1)) { 1413 | /* Copy scalefactors from granule 0 to granule 1 */ 1414 | for(sfb = 6; sfb < 11; sfb++) 1415 | id->g_main_data.scalefac_l[1][ch][sfb]=id->g_main_data.scalefac_l[0][ch][sfb]; 1416 | } 1417 | /* Scale factor bands 11-15 */ 1418 | if((id->g_side_info.scfsi[ch][2] == 0) ||(gr == 0)) { 1419 | for(sfb = 11; sfb < 16; sfb++) 1420 | id->g_main_data.scalefac_l[gr][ch][sfb] = Get_Main_Bits(id,slen2); 1421 | } else if((id->g_side_info.scfsi[ch][2] == 1) &&(gr == 1)) { 1422 | /* Copy scalefactors from granule 0 to granule 1 */ 1423 | for(sfb = 11; sfb < 16; sfb++) 1424 | id->g_main_data.scalefac_l[1][ch][sfb]=id->g_main_data.scalefac_l[0][ch][sfb]; 1425 | } 1426 | /* Scale factor bands 16-20 */ 1427 | if((id->g_side_info.scfsi[ch][3] == 0) ||(gr == 0)) { 1428 | for(sfb = 16; sfb < 21; sfb++) 1429 | id->g_main_data.scalefac_l[gr][ch][sfb] = Get_Main_Bits(id,slen2); 1430 | }else if((id->g_side_info.scfsi[ch][3] == 1) &&(gr == 1)) { 1431 | /* Copy scalefactors from granule 0 to granule 1 */ 1432 | for(sfb = 16; sfb < 21; sfb++) 1433 | id->g_main_data.scalefac_l[1][ch][sfb]=id->g_main_data.scalefac_l[0][ch][sfb]; 1434 | } 1435 | } 1436 | /* Read Huffman coded data. Skip stuffing bits. */ 1437 | Read_Huffman(id,part_2_start,gr,ch); 1438 | } /* end for(gr... */ 1439 | } /* end for(ch... */ 1440 | /* The ancillary data is stored here,but we ignore it. */ 1441 | return(PDMP3_OK); /* Done */ 1442 | } 1443 | 1444 | /**Description: sets position of next bit to be read from main data bitstream. 1445 | * Parameters: Stream handle,Bit position. 0 = start,8 = start of byte 1,etc. 1446 | * Return value: PDMP3_OK or PDMP3_ERR if bit_pos is past end of main data for this frame. 1447 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1448 | static int Set_Main_Pos(pdmp3_handle *id,unsigned bit_pos){ 1449 | 1450 | id->g_main_data_ptr = &(id->g_main_data_vec[bit_pos >> 3]); 1451 | id->g_main_data_idx = bit_pos & 0x7; 1452 | 1453 | return(PDMP3_OK); 1454 | 1455 | } 1456 | 1457 | /**Description: returns next byte from bitstream, or EOF. 1458 | * If we're not on an byte-boundary, bits remaining until next boundary are 1459 | * discarded before getting that byte. 1460 | * Parameters: Stream handle. 1461 | * Return value: The next byte in bitstream in the lowest 8 bits,or C_EOF. 1462 | * Original Author: Krister Lagerström(krister@kmlager.com) 1463 | * Author: Erik Hofman(erik@ehofman.com) **/ 1464 | static unsigned Get_Byte(pdmp3_handle *id){ 1465 | unsigned val = C_EOF; 1466 | if(id->istart != id->iend){ 1467 | val = id->in[id->istart++]; // && 0xff; 1468 | if(id->istart == INBUF_SIZE){ 1469 | id->istart=0; 1470 | } 1471 | id->processed++; 1472 | } 1473 | return(val); 1474 | } 1475 | 1476 | /**Description: returns current file position in bytes. 1477 | * Parameters: Stream handle. 1478 | * Return value: File pos in bytes,or 0 if no file open. 1479 | * Original Author: Krister Lagerström(krister@kmlager.com) 1480 | * Author: Erik Hofman(erik@ehofman.com) **/ 1481 | static unsigned Get_Filepos(pdmp3_handle *id){ 1482 | return(id->processed); 1483 | } 1484 | 1485 | /**Description: gets one bit from the local buffer which contains main_data. 1486 | * Parameters: Stream handle. 1487 | * Return value: The bit is returned in the LSB of the return value. 1488 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1489 | static unsigned Get_Main_Bit(pdmp3_handle *id){ 1490 | unsigned tmp; 1491 | 1492 | tmp = id->g_main_data_ptr[0] >>(7 - id->g_main_data_idx); 1493 | tmp &= 0x01; 1494 | id->g_main_data_ptr +=(id->g_main_data_idx + 1) >> 3; 1495 | id->g_main_data_idx =(id->g_main_data_idx + 1) & 0x07; 1496 | return(tmp); /* Done */ 1497 | } 1498 | 1499 | /**Description: reads 'number_of_bits' from local buffer containing main_data. 1500 | * Parameters: Stream handle,number_of_bits to read(max 24) 1501 | * Return value: The bits are returned in the LSB of the return value. 1502 | * 1503 | ******************************************************************************/ 1504 | static unsigned Get_Main_Bits(pdmp3_handle *id,unsigned number_of_bits){ 1505 | unsigned tmp; 1506 | 1507 | 1508 | if(number_of_bits == 0) return(0); 1509 | 1510 | /* Form a word of the next four bytes */ 1511 | tmp =(id->g_main_data_ptr[0] << 24) |(id->g_main_data_ptr[1] << 16) | 1512 | (id->g_main_data_ptr[2] << 8) |(id->g_main_data_ptr[3] << 0); 1513 | 1514 | /* Remove bits already used */ 1515 | tmp = tmp << id->g_main_data_idx; 1516 | 1517 | /* Remove bits after the desired bits */ 1518 | tmp = tmp >>(32 - number_of_bits); 1519 | 1520 | /* Update pointers */ 1521 | id->g_main_data_ptr +=(id->g_main_data_idx + number_of_bits) >> 3; 1522 | id->g_main_data_idx =(id->g_main_data_idx + number_of_bits) & 0x07; 1523 | 1524 | /* Done */ 1525 | return(tmp); 1526 | 1527 | } 1528 | 1529 | /**Description: returns pos. of next bit to be read from main data bitstream. 1530 | * Parameters: Stream handle. 1531 | * Return value: Bit position. 1532 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1533 | static unsigned Get_Main_Pos(pdmp3_handle *id){ 1534 | unsigned pos; 1535 | 1536 | pos =((size_t) id->g_main_data_ptr) -((size_t) &(id->g_main_data_vec[0])); 1537 | pos /= 4; /* Divide by four to get number of bytes */ 1538 | pos *= 8; /* Multiply by 8 to get number of bits */ 1539 | pos += id->g_main_data_idx; /* Add current bit index */ 1540 | return(pos); 1541 | } 1542 | 1543 | /**Description: reads 'number_of_bits' from buffer which contains side_info. 1544 | * Parameters: Stream handle,number_of_bits to read(max 16) 1545 | * Return value: The bits are returned in the LSB of the return value. 1546 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1547 | static unsigned Get_Side_Bits(pdmp3_handle *id,unsigned number_of_bits){ 1548 | unsigned tmp; 1549 | 1550 | /* Form a word of the next four bytes */ //TODO endianness? 1551 | tmp =(id->side_info_ptr[0] << 24) |(id->side_info_ptr[1] << 16) | 1552 | (id->side_info_ptr[2] << 8) |(id->side_info_ptr[3] << 0); 1553 | /* Remove bits already used */ 1554 | tmp = tmp << id->side_info_idx; 1555 | /* Remove bits after the desired bits */ 1556 | tmp = tmp >>(32 - number_of_bits); 1557 | /* Update pointers */ 1558 | id->side_info_ptr +=(id->side_info_idx + number_of_bits) >> 3; 1559 | id->side_info_idx =(id->side_info_idx + number_of_bits) & 0x07; 1560 | return(tmp); 1561 | } 1562 | 1563 | /**Description: TBD 1564 | * Parameters: TBD 1565 | * Return value: TBD 1566 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1567 | static void Error(const char *s,int e){ 1568 | (void) fwrite(s,1,strlen(s),stderr); 1569 | exit(e); 1570 | } 1571 | 1572 | /**Description: Reads sideinfo from bitstream into buffer for Get_Side_Bits. 1573 | * Parameters: Stream handle,TBD 1574 | * Return value: TBD 1575 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1576 | static void Get_Sideinfo(pdmp3_handle *id,unsigned sideinfo_size){ 1577 | if(Get_Bytes(id,sideinfo_size,id->side_info_vec) != PDMP3_OK) { 1578 | ERR("\nCouldn't read sideinfo %d bytes at pos %d\n", 1579 | sideinfo_size,Get_Filepos(id)); 1580 | return; 1581 | } 1582 | 1583 | id->side_info_ptr = &(id->side_info_vec[0]); 1584 | id->side_info_idx = 0; 1585 | 1586 | } 1587 | 1588 | /**Description: reads/decodes next Huffman code word from main_data reservoir. 1589 | * Parameters: Stream handle,Huffman table number and four pointers for the 1590 | return values. 1591 | * Return value: Two(x,y) or four(x,y,v,w) decoded Huffman words. 1592 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1593 | static int Huffman_Decode(pdmp3_handle *id,unsigned table_num,int32_t *x,int32_t *y,int32_t *v,int32_t *w){ 1594 | unsigned point=0,error=1,bitsleft=32, //=16?? 1595 | treelen = g_huffman_main[table_num].treelen, 1596 | linbits = g_huffman_main[table_num].linbits; 1597 | 1598 | treelen = g_huffman_main[table_num].treelen; 1599 | if(treelen == 0) { /* Check for empty tables */ 1600 | *x = *y = *v = *w = 0; 1601 | return(PDMP3_OK); 1602 | } 1603 | const unsigned short *htptr = g_huffman_main[table_num].hufftable; 1604 | do { /* Start reading the Huffman code word,bit by bit */ 1605 | /* Check if we've matched a code word */ 1606 | if((htptr[point] & 0xff00) == 0) { 1607 | error = 0; 1608 | *x =(htptr[point] >> 4) & 0xf; 1609 | *y = htptr[point] & 0xf; 1610 | break; 1611 | } 1612 | if(Get_Main_Bit(id)) { /* Go right in tree */ 1613 | while((htptr[point] & 0xff) >= 250) 1614 | point += htptr[point] & 0xff; 1615 | point += htptr[point] & 0xff; 1616 | }else{ /* Go left in tree */ 1617 | while((htptr[point] >> 8) >= 250) 1618 | point += htptr[point] >> 8; 1619 | point += htptr[point] >> 8; 1620 | } 1621 | } while((--bitsleft > 0) &&(point < treelen)); 1622 | if(error) { /* Check for error. */ 1623 | ERR("Illegal Huff code in data. bleft = %d,point = %d. tab = %d.", 1624 | bitsleft,point,table_num); 1625 | *x = *y = 0; 1626 | } 1627 | if(table_num > 31) { /* Process sign encodings for quadruples tables. */ 1628 | *v =(*y >> 3) & 1; 1629 | *w =(*y >> 2) & 1; 1630 | *x =(*y >> 1) & 1; 1631 | *y = *y & 1; 1632 | if((*v > 0)&&(Get_Main_Bit(id) == 1)) *v = -*v; 1633 | if((*w > 0)&&(Get_Main_Bit(id) == 1)) *w = -*w; 1634 | if((*x > 0)&&(Get_Main_Bit(id) == 1)) *x = -*x; 1635 | if((*y > 0)&&(Get_Main_Bit(id) == 1)) *y = -*y; 1636 | }else{ 1637 | if((linbits > 0)&&(*x == 15))*x += Get_Main_Bits(id,linbits);/* Get linbits */ 1638 | if((*x > 0)&&(Get_Main_Bit(id) == 1)) *x = -*x; /* Get sign bit */ 1639 | if((linbits > 0)&&(*y == 15))*y += Get_Main_Bits(id,linbits);/* Get linbits */ 1640 | if((*y > 0)&&(Get_Main_Bit(id) == 1)) *y = -*y;/* Get sign bit */ 1641 | } 1642 | return(error ? PDMP3_ERR : PDMP3_OK); /* Done */ 1643 | } 1644 | 1645 | /**Description: Does inverse modified DCT and windowing. 1646 | * Parameters: TBD 1647 | * Return value: TBD 1648 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1649 | static void IMDCT_Win(float in[18],float out[36],unsigned block_type){ 1650 | unsigned i,m,N,p; 1651 | float sum,tin[18]; 1652 | #ifndef IMDCT_TABLES 1653 | static float g_imdct_win[4][36]; 1654 | static unsigned init = 1; 1655 | //TODO : move to separate init function 1656 | if(init) { /* Setup the four(one for each block type) window vectors */ 1657 | for(i = 0; i < 36; i++) g_imdct_win[0][i] = sin(C_PI/36 *(i + 0.5)); //0 1658 | for(i = 0; i < 18; i++) g_imdct_win[1][i] = sin(C_PI/36 *(i + 0.5)); //1 1659 | for(i = 18; i < 24; i++) g_imdct_win[1][i] = 1.0; 1660 | for(i = 24; i < 30; i++) g_imdct_win[1][i] = sin(C_PI/12 *(i + 0.5 - 18.0)); 1661 | for(i = 30; i < 36; i++) g_imdct_win[1][i] = 0.0; 1662 | for(i = 0; i < 12; i++) g_imdct_win[2][i] = sin(C_PI/12 *(i + 0.5)); //2 1663 | for(i = 12; i < 36; i++) g_imdct_win[2][i] = 0.0; 1664 | for(i = 0; i < 6; i++) g_imdct_win[3][i] = 0.0; //3 1665 | for(i = 6; i < 12; i++) g_imdct_win[3][i] = sin(C_PI/12 *(i + 0.5 - 6.0)); 1666 | for(i = 12; i < 18; i++) g_imdct_win[3][i] = 1.0; 1667 | for(i = 18; i < 36; i++) g_imdct_win[3][i] = sin(C_PI/36 *(i + 0.5)); 1668 | init = 0; 1669 | } /* end of init */ 1670 | #endif 1671 | for(i = 0; i < 36; i++) out[i] = 0.0; 1672 | for(i = 0; i < 18; i++) tin[i] = in[i]; 1673 | if(block_type == 2) { /* 3 short blocks */ 1674 | N = 12; 1675 | for(i = 0; i < 3; i++) { 1676 | for(p = 0; p < N; p++) { 1677 | sum = 0.0; 1678 | for(m = 0;m < N/2; m++) 1679 | #ifdef IMDCT_NTABLES 1680 | sum += tin[i+3*m] * cos_N12[m][p]; 1681 | #else 1682 | sum += tin[i+3*m] * cos(C_PI/(2*N)*(2*p+1+N/2)*(2*m+1)); 1683 | #endif 1684 | out[6*i+p+6] += sum * g_imdct_win[block_type][p]; //TODO FIXME +=? 1685 | } 1686 | } /* end for(i... */ 1687 | }else{ /* block_type != 2 */ 1688 | N = 36; 1689 | for(p = 0; p < N; p++){ 1690 | sum = 0.0; 1691 | for(m = 0; m < N/2; m++) 1692 | #ifdef IMDCT_NTABLES 1693 | sum += in[m] * cos_N36[m][p]; 1694 | #else 1695 | sum += in[m] * cos(C_PI/(2*N)*(2*p+1+N/2)*(2*m+1)); 1696 | #endif 1697 | out[p] = sum * g_imdct_win[block_type][p]; 1698 | } 1699 | } 1700 | } 1701 | 1702 | /**Description: TBD 1703 | * Parameters: Stream handle,TBD 1704 | * Return value: TBD 1705 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1706 | static void L3_Antialias(pdmp3_handle *id,unsigned gr,unsigned ch){ 1707 | unsigned sb /* subband of 18 samples */,i,sblim,ui,li; 1708 | float ub,lb; 1709 | 1710 | /* No antialiasing is done for short blocks */ 1711 | if((id->g_side_info.win_switch_flag[gr][ch] == 1) && 1712 | (id->g_side_info.block_type[gr][ch] == 2) && 1713 | (id->g_side_info.mixed_block_flag[gr][ch]) == 0) { 1714 | return; /* Done */ 1715 | } 1716 | /* Setup the limit for how many subbands to transform */ 1717 | sblim =((id->g_side_info.win_switch_flag[gr][ch] == 1) && 1718 | (id->g_side_info.block_type[gr][ch] == 2) && 1719 | (id->g_side_info.mixed_block_flag[gr][ch] == 1))?2:32; 1720 | /* Do the actual antialiasing */ 1721 | for(sb = 1; sb < sblim; sb++) { 1722 | for(i = 0; i < 8; i++) { 1723 | li = 18*sb-1-i; 1724 | ui = 18*sb+i; 1725 | lb = id->g_main_data.is[gr][ch][li]*cs[i] - id->g_main_data.is[gr][ch][ui]*ca[i]; 1726 | ub = id->g_main_data.is[gr][ch][ui]*cs[i] + id->g_main_data.is[gr][ch][li]*ca[i]; 1727 | id->g_main_data.is[gr][ch][li] = lb; 1728 | id->g_main_data.is[gr][ch][ui] = ub; 1729 | } 1730 | } 1731 | return; /* Done */ 1732 | } 1733 | 1734 | /**Description: TBD 1735 | * Parameters: Stream handle,TBD 1736 | * Return value: TBD 1737 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1738 | static void L3_Frequency_Inversion(pdmp3_handle *id,unsigned gr,unsigned ch){ 1739 | unsigned sb,i; 1740 | 1741 | for(sb = 1; sb < 32; sb += 2) { //OPT? : for(sb = 18; sb < 576; sb += 36) 1742 | for(i = 1; i < 18; i += 2) 1743 | id->g_main_data.is[gr][ch][sb*18 + i] = -id->g_main_data.is[gr][ch][sb*18 + i]; 1744 | } 1745 | return; /* Done */ 1746 | } 1747 | 1748 | /**Description: TBD 1749 | * Parameters: Stream handle,TBD 1750 | * Return value: TBD 1751 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1752 | static void L3_Hybrid_Synthesis(pdmp3_handle *id,unsigned gr,unsigned ch){ 1753 | unsigned sb,i,j,bt; 1754 | float rawout[36]; 1755 | static float store[2][32][18]; 1756 | 1757 | if(id->hsynth_init) { /* Clear stored samples vector. OPT? use memset */ 1758 | for(j = 0; j < 2; j++) { 1759 | for(sb = 0; sb < 32; sb++) { 1760 | for(i = 0; i < 18; i++) { 1761 | store[j][sb][i] = 0.0; 1762 | } 1763 | } 1764 | } 1765 | id->hsynth_init = 0; 1766 | } /* end if(hsynth_init) */ 1767 | for(sb = 0; sb < 32; sb++) { /* Loop through all 32 subbands */ 1768 | /* Determine blocktype for this subband */ 1769 | bt =((id->g_side_info.win_switch_flag[gr][ch] == 1) && 1770 | (id->g_side_info.mixed_block_flag[gr][ch] == 1) &&(sb < 2)) 1771 | ? 0 : id->g_side_info.block_type[gr][ch]; 1772 | /* Do the inverse modified DCT and windowing */ 1773 | IMDCT_Win(&(id->g_main_data.is[gr][ch][sb*18]),rawout,bt); 1774 | for(i = 0; i < 18; i++) { /* Overlapp add with stored vector into main_data vector */ 1775 | id->g_main_data.is[gr][ch][sb*18 + i] = rawout[i] + store[ch][sb][i]; 1776 | store[ch][sb][i] = rawout[i + 18]; 1777 | } /* end for(i... */ 1778 | } /* end for(sb... */ 1779 | return; /* Done */ 1780 | } 1781 | 1782 | /**Description: TBD 1783 | * Parameters: Stream handle,TBD 1784 | * Return value: TBD 1785 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1786 | static void L3_Reorder(pdmp3_handle *id,unsigned gr,unsigned ch){ 1787 | unsigned sfreq,i,j,next_sfb,sfb,win_len,win; 1788 | float re[576]; 1789 | 1790 | sfreq = id->g_frame_header.sampling_frequency; /* Setup sampling freq index */ 1791 | /* Only reorder short blocks */ 1792 | if((id->g_side_info.win_switch_flag[gr][ch] == 1) && 1793 | (id->g_side_info.block_type[gr][ch] == 2)) { /* Short blocks */ 1794 | /* Check if the first two subbands 1795 | *(=2*18 samples = 8 long or 3 short sfb's) uses long blocks */ 1796 | sfb = (id->g_side_info.mixed_block_flag[gr][ch] != 0)?3:0; /* 2 longbl. sb first */ 1797 | next_sfb = g_sf_band_indices[sfreq].s[sfb+1] * 3; 1798 | win_len = g_sf_band_indices[sfreq].s[sfb+1] - g_sf_band_indices[sfreq].s[sfb]; 1799 | for(i =((sfb == 0) ? 0 : 36); i < 576; /* i++ done below! */) { 1800 | /* Check if we're into the next scalefac band */ 1801 | if(i == next_sfb) { /* Yes */ 1802 | /* Copy reordered data back to the original vector */ 1803 | for(j = 0; j < 3*win_len; j++) 1804 | id->g_main_data.is[gr][ch][3*g_sf_band_indices[sfreq].s[sfb] + j] = re[j]; 1805 | /* Check if this band is above the rzero region,if so we're done */ 1806 | if(i >= id->g_side_info.count1[gr][ch]) return; /* Done */ 1807 | sfb++; 1808 | next_sfb = g_sf_band_indices[sfreq].s[sfb+1] * 3; 1809 | win_len = g_sf_band_indices[sfreq].s[sfb+1] - g_sf_band_indices[sfreq].s[sfb]; 1810 | } /* end if(next_sfb) */ 1811 | for(win = 0; win < 3; win++) { /* Do the actual reordering */ 1812 | for(j = 0; j < win_len; j++) { 1813 | re[j*3 + win] = id->g_main_data.is[gr][ch][i]; 1814 | i++; 1815 | } /* end for(j... */ 1816 | } /* end for(win... */ 1817 | } /* end for(i... */ 1818 | /* Copy reordered data of last band back to original vector */ 1819 | for(j = 0; j < 3*win_len; j++) 1820 | id->g_main_data.is[gr][ch][3 * g_sf_band_indices[sfreq].s[12] + j] = re[j]; 1821 | } /* end else(only long blocks) */ 1822 | return; /* Done */ 1823 | } 1824 | 1825 | /**Description: TBD 1826 | * Parameters: Stream handle,TBD 1827 | * Return value: TBD 1828 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1829 | static void L3_Requantize(pdmp3_handle *id,unsigned gr,unsigned ch){ 1830 | unsigned sfb /* scalefac band index */,next_sfb /* frequency of next sfb */, 1831 | sfreq,i,j,win,win_len; 1832 | 1833 | /* Setup sampling frequency index */ 1834 | sfreq = id->g_frame_header.sampling_frequency; 1835 | /* Determine type of block to process */ 1836 | if((id->g_side_info.win_switch_flag[gr][ch] == 1) && (id->g_side_info.block_type[gr][ch] == 2)) { /* Short blocks */ 1837 | /* Check if the first two subbands 1838 | *(=2*18 samples = 8 long or 3 short sfb's) uses long blocks */ 1839 | if(id->g_side_info.mixed_block_flag[gr][ch] != 0) { /* 2 longbl. sb first */ 1840 | /* First process the 2 long block subbands at the start */ 1841 | sfb = 0; 1842 | next_sfb = g_sf_band_indices[sfreq].l[sfb+1]; 1843 | for(i = 0; i < 36; i++) { 1844 | if(i == next_sfb) { 1845 | sfb++; 1846 | next_sfb = g_sf_band_indices[sfreq].l[sfb+1]; 1847 | } /* end if */ 1848 | Requantize_Process_Long(id,gr,ch,i,sfb); 1849 | } 1850 | /* And next the remaining,non-zero,bands which uses short blocks */ 1851 | sfb = 3; 1852 | next_sfb = g_sf_band_indices[sfreq].s[sfb+1] * 3; 1853 | win_len = g_sf_band_indices[sfreq].s[sfb+1] - 1854 | g_sf_band_indices[sfreq].s[sfb]; 1855 | 1856 | for(i = 36; i < id->g_side_info.count1[gr][ch]; /* i++ done below! */) { 1857 | /* Check if we're into the next scalefac band */ 1858 | if(i == next_sfb) { /* Yes */ 1859 | sfb++; 1860 | next_sfb = g_sf_band_indices[sfreq].s[sfb+1] * 3; 1861 | win_len = g_sf_band_indices[sfreq].s[sfb+1] - 1862 | g_sf_band_indices[sfreq].s[sfb]; 1863 | } /* end if(next_sfb) */ 1864 | for(win = 0; win < 3; win++) { 1865 | for(j = 0; j < win_len; j++) { 1866 | Requantize_Process_Short(id,gr,ch,i,sfb,win); 1867 | i++; 1868 | } /* end for(j... */ 1869 | } /* end for(win... */ 1870 | 1871 | } /* end for(i... */ 1872 | }else{ /* Only short blocks */ 1873 | sfb = 0; 1874 | next_sfb = g_sf_band_indices[sfreq].s[sfb+1] * 3; 1875 | win_len = g_sf_band_indices[sfreq].s[sfb+1] - 1876 | g_sf_band_indices[sfreq].s[sfb]; 1877 | for(i = 0; i < id->g_side_info.count1[gr][ch]; /* i++ done below! */) { 1878 | /* Check if we're into the next scalefac band */ 1879 | if(i == next_sfb) { /* Yes */ 1880 | sfb++; 1881 | next_sfb = g_sf_band_indices[sfreq].s[sfb+1] * 3; 1882 | win_len = g_sf_band_indices[sfreq].s[sfb+1] - 1883 | g_sf_band_indices[sfreq].s[sfb]; 1884 | } /* end if(next_sfb) */ 1885 | for(win = 0; win < 3; win++) { 1886 | for(j = 0; j < win_len; j++) { 1887 | Requantize_Process_Short(id,gr,ch,i,sfb,win); 1888 | i++; 1889 | } /* end for(j... */ 1890 | } /* end for(win... */ 1891 | } /* end for(i... */ 1892 | } /* end else(only short blocks) */ 1893 | }else{ /* Only long blocks */ 1894 | sfb = 0; 1895 | next_sfb = g_sf_band_indices[sfreq].l[sfb+1]; 1896 | for(i = 0; i < id->g_side_info.count1[gr][ch]; i++) { 1897 | if(i == next_sfb) { 1898 | sfb++; 1899 | next_sfb = g_sf_band_indices[sfreq].l[sfb+1]; 1900 | } /* end if */ 1901 | Requantize_Process_Long(id,gr,ch,i,sfb); 1902 | } 1903 | } /* end else(only long blocks) */ 1904 | return; /* Done */ 1905 | } 1906 | 1907 | /**Description: TBD 1908 | * Parameters: Stream handle,TBD 1909 | * Return value: TBD 1910 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1911 | static void L3_Stereo(pdmp3_handle *id,unsigned gr){ 1912 | unsigned max_pos,i,sfreq,sfb /* scalefac band index */; 1913 | float left,right; 1914 | 1915 | /* Do nothing if joint stereo is not enabled */ 1916 | if((id->g_frame_header.mode != 1)||(id->g_frame_header.mode_extension == 0)) return; 1917 | /* Do Middle/Side("normal") stereo processing */ 1918 | if(id->g_frame_header.mode_extension & 0x2) { 1919 | /* Determine how many frequency lines to transform */ 1920 | max_pos = id->g_side_info.count1[gr][!!(id->g_side_info.count1[gr][0] > id->g_side_info.count1[gr][1])]; 1921 | /* Do the actual processing */ 1922 | for(i = 0; i < max_pos; i++) { 1923 | left =(id->g_main_data.is[gr][0][i] + id->g_main_data.is[gr][1][i]) 1924 | *(C_INV_SQRT_2); 1925 | right =(id->g_main_data.is[gr][0][i] - id->g_main_data.is[gr][1][i]) 1926 | *(C_INV_SQRT_2); 1927 | id->g_main_data.is[gr][0][i] = left; 1928 | id->g_main_data.is[gr][1][i] = right; 1929 | } /* end for(i... */ 1930 | } /* end if(ms_stereo... */ 1931 | /* Do intensity stereo processing */ 1932 | if(id->g_frame_header.mode_extension & 0x1) { 1933 | /* Setup sampling frequency index */ 1934 | sfreq = id->g_frame_header.sampling_frequency; 1935 | /* First band that is intensity stereo encoded is first band scale factor 1936 | * band on or above count1 frequency line. N.B.: Intensity stereo coding is 1937 | * only done for higher subbands, but logic is here for lower subbands. */ 1938 | /* Determine type of block to process */ 1939 | if((id->g_side_info.win_switch_flag[gr][0] == 1) && 1940 | (id->g_side_info.block_type[gr][0] == 2)) { /* Short blocks */ 1941 | /* Check if the first two subbands 1942 | *(=2*18 samples = 8 long or 3 short sfb's) uses long blocks */ 1943 | if(id->g_side_info.mixed_block_flag[gr][0] != 0) { /* 2 longbl. sb first */ 1944 | for(sfb = 0; sfb < 8; sfb++) {/* First process 8 sfb's at start */ 1945 | /* Is this scale factor band above count1 for the right channel? */ 1946 | if(g_sf_band_indices[sfreq].l[sfb] >= id->g_side_info.count1[gr][1]) 1947 | Stereo_Process_Intensity_Long(id,gr,sfb); 1948 | } /* end if(sfb... */ 1949 | /* And next the remaining bands which uses short blocks */ 1950 | for(sfb = 3; sfb < 12; sfb++) { 1951 | /* Is this scale factor band above count1 for the right channel? */ 1952 | if(g_sf_band_indices[sfreq].s[sfb]*3 >= id->g_side_info.count1[gr][1]) 1953 | Stereo_Process_Intensity_Short(id,gr,sfb); /* intensity stereo processing */ 1954 | } 1955 | }else{ /* Only short blocks */ 1956 | for(sfb = 0; sfb < 12; sfb++) { 1957 | /* Is this scale factor band above count1 for the right channel? */ 1958 | if(g_sf_band_indices[sfreq].s[sfb]*3 >= id->g_side_info.count1[gr][1]) 1959 | Stereo_Process_Intensity_Short(id,gr,sfb); /* intensity stereo processing */ 1960 | } 1961 | } /* end else(only short blocks) */ 1962 | }else{ /* Only long blocks */ 1963 | for(sfb = 0; sfb < 21; sfb++) { 1964 | /* Is this scale factor band above count1 for the right channel? */ 1965 | if(g_sf_band_indices[sfreq].l[sfb] >= id->g_side_info.count1[gr][1]) { 1966 | /* Perform the intensity stereo processing */ 1967 | Stereo_Process_Intensity_Long(id,gr,sfb); 1968 | } 1969 | } 1970 | } /* end else(only long blocks) */ 1971 | } /* end if(intensity_stereo processing) */ 1972 | } 1973 | 1974 | /**Description: TBD 1975 | * Parameters: Stream handle,TBD 1976 | * Return value: TBD 1977 | * Author: Krister Lagerström(krister@kmlager.com) **/ 1978 | static void L3_Subband_Synthesis(pdmp3_handle *id,unsigned gr,unsigned ch,unsigned outdata[576]){ 1979 | float u_vec[512],s_vec[32],sum; /* u_vec can be used insted of s_vec */ 1980 | int32_t samp; 1981 | static unsigned init = 1; 1982 | unsigned i,j,ss,nch; 1983 | static float g_synth_n_win[64][32],v_vec[2 /* ch */][1024]; 1984 | 1985 | /* Number of channels(1 for mono and 2 for stereo) */ 1986 | nch =(id->g_frame_header.mode == mpeg1_mode_single_channel) ? 1 : 2 ; 1987 | /* Setup the n_win windowing vector and the v_vec intermediate vector */ 1988 | 1989 | if(init) { 1990 | for(i = 0; i < 64; i++) { 1991 | for(j = 0; j < 32; j++) /*TODO: put in lookup table*/ 1992 | g_synth_n_win[i][j] = cos(((float)(16+i)*(2*j+1)) *(C_PI/64.0)); 1993 | } 1994 | for(i = 0; i < 2; i++) /* Setup the v_vec intermediate vector */ 1995 | for(j = 0; j < 1024; j++) v_vec[i][j] = 0.0; /*TODO: memset */ 1996 | init = 0; 1997 | } /* end if(init) */ 1998 | 1999 | if(id->synth_init) { 2000 | for(i = 0; i < 2; i++) /* Setup the v_vec intermediate vector */ 2001 | for(j = 0; j < 1024; j++) v_vec[i][j] = 0.0; /*TODO: memset*/ 2002 | id->synth_init = 0; 2003 | } /* end if(synth_init) */ 2004 | 2005 | for(ss = 0; ss < 18; ss++){ /* Loop through 18 samples in 32 subbands */ 2006 | for(i = 1023; i > 63; i--) /* Shift up the V vector */ 2007 | v_vec[ch][i] = v_vec[ch][i-64]; 2008 | for(i = 0; i < 32; i++) /* Copy next 32 time samples to a temp vector */ 2009 | s_vec[i] =((float) id->g_main_data.is[gr][ch][i*18 + ss]); 2010 | for(i = 0; i < 64; i++){ /* Matrix multiply input with n_win[][] matrix */ 2011 | sum = 0.0; 2012 | for(j = 0; j < 32; j++) sum += g_synth_n_win[i][j] * s_vec[j]; 2013 | v_vec[ch][i] = sum; 2014 | } /* end for(i... */ 2015 | for(i = 0; i < 8; i++) { /* Build the U vector */ 2016 | for(j = 0; j < 32; j++) { /* <<7 == *128 */ 2017 | u_vec[(i << 6) + j] = v_vec[ch][(i << 7) + j]; 2018 | u_vec[(i << 6) + j + 32] = v_vec[ch][(i << 7) + j + 96]; 2019 | } 2020 | } /* end for(i... */ 2021 | for(i = 0; i < 512; i++) /* Window by u_vec[i] with g_synth_dtbl[i] */ 2022 | u_vec[i] = u_vec[i] * g_synth_dtbl[i]; 2023 | for(i = 0; i < 32; i++) { /* Calc 32 samples,store in outdata vector */ 2024 | sum = 0.0; 2025 | for(j = 0; j < 16; j++) /* sum += u_vec[j*32 + i]; */ 2026 | sum += u_vec[(j << 5) + i]; 2027 | /* sum now contains time sample 32*ss+i. Convert to 16-bit signed int */ 2028 | samp =(int32_t)(sum * 32767.0); 2029 | if(samp > 32767) samp = 32767; 2030 | else if(samp < -32767) samp = -32767; 2031 | samp &= 0xffff; 2032 | if(ch == 0) { /* This function must be called for channel 0 first */ 2033 | /* We always run in stereo mode,& duplicate channels here for mono */ 2034 | if(nch == 1) { 2035 | outdata[32*ss + i] =(samp << 16) |(samp); 2036 | }else{ 2037 | outdata[32*ss + i] = samp << 16; 2038 | } 2039 | }else{ 2040 | outdata[32*ss + i] |= samp; 2041 | } 2042 | } /* end for(i... */ 2043 | } /* end for(ss... */ 2044 | return; /* Done */ 2045 | } 2046 | 2047 | /**Description: called by Read_Main_L3 to read Huffman coded data from bitstream. 2048 | * Parameters: Stream handle,TBD 2049 | * Return value: None. The data is stored in id->g_main_data.is[ch][gr][freqline]. 2050 | * Author: Krister Lagerström(krister@kmlager.com) **/ 2051 | static void Read_Huffman(pdmp3_handle *id,unsigned part_2_start,unsigned gr,unsigned ch){ 2052 | int32_t x,y,v,w; 2053 | unsigned table_num,is_pos,bit_pos_end,sfreq; 2054 | unsigned region_1_start,region_2_start; /* region_0_start = 0 */ 2055 | 2056 | /* Check that there is any data to decode. If not,zero the array. */ 2057 | if(id->g_side_info.part2_3_length[gr][ch] == 0) { 2058 | for(is_pos = 0; is_pos < 576; is_pos++) 2059 | id->g_main_data.is[gr][ch][is_pos] = 0.0; 2060 | return; 2061 | } 2062 | /* Calculate bit_pos_end which is the index of the last bit for this part. */ 2063 | bit_pos_end = part_2_start + id->g_side_info.part2_3_length[gr][ch] - 1; 2064 | /* Determine region boundaries */ 2065 | if((id->g_side_info.win_switch_flag[gr][ch] == 1)&& 2066 | (id->g_side_info.block_type[gr][ch] == 2)) { 2067 | region_1_start = 36; /* sfb[9/3]*3=36 */ 2068 | region_2_start = 576; /* No Region2 for short block case. */ 2069 | }else{ 2070 | sfreq = id->g_frame_header.sampling_frequency; 2071 | region_1_start = 2072 | g_sf_band_indices[sfreq].l[id->g_side_info.region0_count[gr][ch] + 1]; 2073 | region_2_start = 2074 | g_sf_band_indices[sfreq].l[id->g_side_info.region0_count[gr][ch] + 2075 | id->g_side_info.region1_count[gr][ch] + 2]; 2076 | } 2077 | /* Read big_values using tables according to region_x_start */ 2078 | for(is_pos = 0; is_pos < id->g_side_info.big_values[gr][ch] * 2; is_pos++) { 2079 | if(is_pos < region_1_start) { 2080 | table_num = id->g_side_info.table_select[gr][ch][0]; 2081 | } else if(is_pos < region_2_start) { 2082 | table_num = id->g_side_info.table_select[gr][ch][1]; 2083 | }else table_num = id->g_side_info.table_select[gr][ch][2]; 2084 | /* Get next Huffman coded words */ 2085 | (void) Huffman_Decode(id,table_num,&x,&y,&v,&w); 2086 | /* In the big_values area there are two freq lines per Huffman word */ 2087 | id->g_main_data.is[gr][ch][is_pos++] = x; 2088 | id->g_main_data.is[gr][ch][is_pos] = y; 2089 | } 2090 | /* Read small values until is_pos = 576 or we run out of huffman data */ 2091 | table_num = id->g_side_info.count1table_select[gr][ch] + 32; 2092 | for(is_pos = id->g_side_info.big_values[gr][ch] * 2; 2093 | (is_pos <= 572) &&(Get_Main_Pos(id) <= bit_pos_end); is_pos++) { 2094 | /* Get next Huffman coded words */ 2095 | (void) Huffman_Decode(id,table_num,&x,&y,&v,&w); 2096 | id->g_main_data.is[gr][ch][is_pos++] = v; 2097 | if(is_pos >= 576) break; 2098 | id->g_main_data.is[gr][ch][is_pos++] = w; 2099 | if(is_pos >= 576) break; 2100 | id->g_main_data.is[gr][ch][is_pos++] = x; 2101 | if(is_pos >= 576) break; 2102 | id->g_main_data.is[gr][ch][is_pos] = y; 2103 | } 2104 | /* Check that we didn't read past the end of this section */ 2105 | if(Get_Main_Pos(id) >(bit_pos_end+1)) /* Remove last words read */ 2106 | is_pos -= 4; 2107 | /* Setup count1 which is the index of the first sample in the rzero reg. */ 2108 | id->g_side_info.count1[gr][ch] = is_pos; 2109 | /* Zero out the last part if necessary */ 2110 | for(/* is_pos comes from last for-loop */; is_pos < 576; is_pos++) 2111 | id->g_main_data.is[gr][ch][is_pos] = 0.0; 2112 | /* Set the bitpos to point to the next part to read */ 2113 | (void) Set_Main_Pos(id,bit_pos_end+1); 2114 | return; /* Done */ 2115 | } 2116 | 2117 | /**Description: requantize sample in subband that uses long blocks. 2118 | * Parameters: Stream handle,TBD 2119 | * Return value: TBD 2120 | * Author: Krister Lagerström(krister@kmlager.com) **/ 2121 | static void Requantize_Process_Long(pdmp3_handle *id,unsigned gr,unsigned ch,unsigned is_pos,unsigned sfb){ 2122 | float tmp1,tmp2,tmp3,sf_mult,pf_x_pt; 2123 | static float pretab[21] = { 0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2 }; 2124 | 2125 | sf_mult = id->g_side_info.scalefac_scale[gr][ch] ? 1.0 : 0.5; 2126 | pf_x_pt = id->g_side_info.preflag[gr][ch] * pretab[sfb]; 2127 | tmp1 = pow(2.0,-(sf_mult *(id->g_main_data.scalefac_l[gr][ch][sfb] + pf_x_pt))); 2128 | tmp2 = pow(2.0,0.25 *((int32_t) id->g_side_info.global_gain[gr][ch] - 210)); 2129 | if(id->g_main_data.is[gr][ch][is_pos] < 0.0) 2130 | tmp3 = -Requantize_Pow_43(-id->g_main_data.is[gr][ch][is_pos]); 2131 | else tmp3 = Requantize_Pow_43(id->g_main_data.is[gr][ch][is_pos]); 2132 | id->g_main_data.is[gr][ch][is_pos] = tmp1 * tmp2 * tmp3; 2133 | return; /* Done */ 2134 | } 2135 | 2136 | /**Description: requantize sample in subband that uses short blocks. 2137 | * Parameters: Stream handle,TBD 2138 | * Return value: TBD 2139 | * Author: Krister Lagerström(krister@kmlager.com) **/ 2140 | static void Requantize_Process_Short(pdmp3_handle *id,unsigned gr,unsigned ch,unsigned is_pos,unsigned sfb,unsigned win){ 2141 | float tmp1,tmp2,tmp3,sf_mult; 2142 | 2143 | sf_mult = id->g_side_info.scalefac_scale[gr][ch] ? 1.0f : 0.5f; 2144 | tmp1 = pow(2.0f,-(sf_mult * id->g_main_data.scalefac_s[gr][ch][sfb][win])); 2145 | tmp2 = pow(2.0f,0.25f *((float) id->g_side_info.global_gain[gr][ch] - 210.0f - 2146 | 8.0f *(float) id->g_side_info.subblock_gain[gr][ch][win])); 2147 | tmp3 =(id->g_main_data.is[gr][ch][is_pos] < 0.0) 2148 | ? -Requantize_Pow_43(-id->g_main_data.is[gr][ch][is_pos]) 2149 | : Requantize_Pow_43(id->g_main_data.is[gr][ch][is_pos]); 2150 | id->g_main_data.is[gr][ch][is_pos] = tmp1 * tmp2 * tmp3; 2151 | return; /* Done */ 2152 | } 2153 | 2154 | /**Description: intensity stereo processing for entire subband with long blocks. 2155 | * Parameters: Stream handle,TBD 2156 | * Return value: TBD 2157 | * Author: Krister Lagerström(krister@kmlager.com) **/ 2158 | static void Stereo_Process_Intensity_Long(pdmp3_handle *id,unsigned gr,unsigned sfb){ 2159 | unsigned i,sfreq,sfb_start,sfb_stop,is_pos; 2160 | float is_ratio_l,is_ratio_r,left,right; 2161 | 2162 | /* Check that((is_pos[sfb]=scalefac) != 7) => no intensity stereo */ 2163 | if((is_pos = id->g_main_data.scalefac_l[gr][0][sfb]) != 7) { 2164 | sfreq = id->g_frame_header.sampling_frequency; /* Setup sampling freq index */ 2165 | sfb_start = g_sf_band_indices[sfreq].l[sfb]; 2166 | sfb_stop = g_sf_band_indices[sfreq].l[sfb+1]; 2167 | if(is_pos == 6) { /* tan((6*PI)/12 = PI/2) needs special treatment! */ 2168 | is_ratio_l = 1.0f; 2169 | is_ratio_r = 0.0f; 2170 | }else{ 2171 | is_ratio_l = is_ratios[is_pos] /(1.0f + is_ratios[is_pos]); 2172 | is_ratio_r = 1.0f /(1.0f + is_ratios[is_pos]); 2173 | } 2174 | /* Now decode all samples in this scale factor band */ 2175 | for(i = sfb_start; i < sfb_stop; i++) { 2176 | left = is_ratio_l * id->g_main_data.is[gr][0][i]; 2177 | right = is_ratio_r * id->g_main_data.is[gr][0][i]; 2178 | id->g_main_data.is[gr][0][i] = left; 2179 | id->g_main_data.is[gr][1][i] = right; 2180 | } 2181 | } 2182 | return; /* Done */ 2183 | } /* end Stereo_Process_Intensity_Long() */ 2184 | 2185 | /**Description: This function is used to perform intensity stereo processing 2186 | * for an entire subband that uses short blocks. 2187 | * Parameters: Stream handle,TBD 2188 | * Return value: TBD 2189 | * Author: Krister Lagerström(krister@kmlager.com) **/ 2190 | static void Stereo_Process_Intensity_Short(pdmp3_handle *id,unsigned gr,unsigned sfb){ 2191 | unsigned sfb_start,sfb_stop,is_pos,is_ratio_l,is_ratio_r,i,sfreq,win,win_len; 2192 | float left,right; 2193 | 2194 | sfreq = id->g_frame_header.sampling_frequency; /* Setup sampling freq index */ 2195 | /* The window length */ 2196 | win_len = g_sf_band_indices[sfreq].s[sfb+1] - g_sf_band_indices[sfreq].s[sfb]; 2197 | /* The three windows within the band has different scalefactors */ 2198 | for(win = 0; win < 3; win++) { 2199 | /* Check that((is_pos[sfb]=scalefac) != 7) => no intensity stereo */ 2200 | if((is_pos = id->g_main_data.scalefac_s[gr][0][sfb][win]) != 7) { 2201 | sfb_start = g_sf_band_indices[sfreq].s[sfb]*3 + win_len*win; 2202 | sfb_stop = sfb_start + win_len; 2203 | if(is_pos == 6) { /* tan((6*PI)/12 = PI/2) needs special treatment! */ 2204 | is_ratio_l = 1.0; 2205 | is_ratio_r = 0.0; 2206 | }else{ 2207 | is_ratio_l = is_ratios[is_pos] /(1.0 + is_ratios[is_pos]); 2208 | is_ratio_r = 1.0 /(1.0 + is_ratios[is_pos]); 2209 | } 2210 | /* Now decode all samples in this scale factor band */ 2211 | for(i = sfb_start; i < sfb_stop; i++) { 2212 | left = is_ratio_l = id->g_main_data.is[gr][0][i]; 2213 | right = is_ratio_r = id->g_main_data.is[gr][0][i]; 2214 | id->g_main_data.is[gr][0][i] = left; 2215 | id->g_main_data.is[gr][1][i] = right; 2216 | } 2217 | } /* end if(not illegal is_pos) */ 2218 | } /* end for(win... */ 2219 | return; /* Done */ 2220 | } /* end Stereo_Process_Intensity_Short() */ 2221 | 2222 | #ifdef OUTPUT_RAW 2223 | /****************************************************************************** 2224 | * 2225 | * Name: audio_write_raw 2226 | * Author: Krister Lagerström(krister@unidata.se) 2227 | * Description: This function is used to output raw data 2228 | * Parameters: Stream handle,file name,pointers to the samples,the number of 2229 | samples 2230 | * Return value: None 2231 | * Revision History: 2232 | * Author Date Change 2233 | * krister 010101 Initial revision 2234 | * 2235 | ******************************************************************************/ 2236 | static void audio_write_raw(const char *filename,unsigned *samples,unsigned nbytes){ 2237 | static int init = 0,fd; 2238 | char fname[1024]; 2239 | 2240 | if(init == 0) { 2241 | init = 1; 2242 | if(strcmp(filename,"-")) { 2243 | snprintf(fname,1023,"%s.raw",filename); 2244 | fd = open(fname,O_WRONLY | O_CREAT,0666); 2245 | if(fd == -1) { 2246 | perror(fname); 2247 | exit(-1); 2248 | } 2249 | } else { 2250 | fd = 1; 2251 | } 2252 | } 2253 | 2254 | if(write(fd,samples,nbytes) != nbytes) 2255 | Error("Unable to write raw data\n",-1); 2256 | return; 2257 | } /* audio_write_raw() */ 2258 | #endif 2259 | 2260 | /**Description: output audio data 2261 | * Parameters: Stream handle,audio device name,file name. 2262 | * Return value: None 2263 | * Author: Krister Lagerström(krister@kmlager.com) **/ 2264 | static void audio_write(pdmp3_handle *id,const char *audio_name,const char *filename,unsigned char *samples,size_t nbytes){ 2265 | #ifdef OUTPUT_SOUND 2266 | static int init = 0,audio,curr_sample_rate = 0; 2267 | int format = AFMT_S16_LE,tmp,dsp_speed = 44100,dsp_stereo = 2; 2268 | int sample_rate = g_sampling_frequency[id->g_frame_header.sampling_frequency]; 2269 | 2270 | if(init == 0) { 2271 | init = 1; 2272 | audio = open(audio_name,O_WRONLY,0); 2273 | if(audio == -1) { 2274 | perror(audio_name); 2275 | exit(-1); 2276 | } 2277 | tmp = format; 2278 | ioctl(audio,SNDCTL_DSP_SETFMT,&format); 2279 | if(tmp != format) 2280 | Error("Unable to set the audio format\n",-1); 2281 | if(ioctl(audio,SNDCTL_DSP_CHANNELS,&dsp_stereo) == -1) 2282 | Error("Unable to set mono/stereo\n",-1); 2283 | } 2284 | 2285 | if(curr_sample_rate != sample_rate) { 2286 | curr_sample_rate = sample_rate; 2287 | if(ioctl(audio,SNDCTL_DSP_SPEED,&dsp_speed) == -1) 2288 | Error("Unable to set audio speed\n",-1); 2289 | } 2290 | 2291 | if(write(audio,samples,nbytes) != nbytes) 2292 | Error("Unable to write audio data\n",-1); 2293 | #endif /* OUTPUT_SOUND */ 2294 | #ifdef OUTPUT_RAW 2295 | audio_write_raw(filename,samples,nbytes); 2296 | #endif /* OUTPUT_RAW */ 2297 | return; 2298 | } /* audio_write() */ 2299 | 2300 | 2301 | /*############################################################################# 2302 | * Stream API - Added for AeonWave Audio (http://www.adalin.com) 2303 | * This is a subset of the libmpg123 API and should by 100% compatible. 2304 | * 2305 | * Au0thor: Erik Hofman(erik@ehofman.com) 2306 | */ 2307 | static void Convert_Frame_S16(pdmp3_handle *id,unsigned char *outbuf,size_t buflen,size_t *done) 2308 | { 2309 | short *s = (short *)outbuf; 2310 | unsigned lo,hi,nsamps,framesz; 2311 | int q,i,nch,gr; 2312 | 2313 | nch = (id->g_frame_header.mode == mpeg1_mode_single_channel ? 1 : 2); 2314 | framesz = sizeof(short)*nch; 2315 | 2316 | nsamps = buflen / framesz; 2317 | if (nsamps > (2*576 - id->ostart)) { 2318 | nsamps = 2*576 - id->ostart; 2319 | } 2320 | *done = nsamps * framesz; 2321 | 2322 | /* copy to outbuf */ 2323 | i = id->ostart % 576; 2324 | gr = id->ostart/576; 2325 | for (q = 0; q < nsamps; ++q) { 2326 | if(nch == 1) { 2327 | lo = id->out[gr][i] & 0xffff; 2328 | s[q] = lo; 2329 | } else { 2330 | lo = id->out[gr][i] & 0xffff; 2331 | hi =(id->out[gr][i] & 0xffff0000) >> 16; 2332 | s[2*q] = hi; 2333 | s[2*q+1] = lo; 2334 | } 2335 | if (++i == 576) { 2336 | ++gr; 2337 | i = 0; 2338 | } 2339 | } 2340 | 2341 | id->ostart += nsamps; 2342 | if (id->ostart == (2*576)) { 2343 | id->ostart = 0; 2344 | } 2345 | } 2346 | 2347 | /**Description: Create a new streaming handle 2348 | * Parameters: None 2349 | * Return value: Stream handle 2350 | * Author: Erik Hofman(erik@ehofman.com) **/ 2351 | pdmp3_handle* pdmp3_new(const char *decoder,int *error){ 2352 | return malloc(sizeof(pdmp3_handle)); 2353 | } 2354 | 2355 | 2356 | /**Description: Free a streaming handle 2357 | * Parameters: Streaming handle 2358 | * Return value: None 2359 | * Author: Erik Hofman(erik@ehofman.com) **/ 2360 | void pdmp3_delete(pdmp3_handle *id){ 2361 | free(id); 2362 | } 2363 | 2364 | 2365 | /**Description: Resets the stream handle. 2366 | * Parameters: Stream handle 2367 | * Return value: PDMP3_OK or PDMP3_ERR 2368 | * Author: Erik Hofman(erik@ehofman.com) **/ 2369 | int pdmp3_open_feed(pdmp3_handle *id){ 2370 | if(id) { 2371 | id->ostart = 0; 2372 | id->istart = 0; 2373 | id->iend = 0; 2374 | id->processed = 0; 2375 | id->new_header = 0; 2376 | 2377 | id->hsynth_init = 1; 2378 | id->synth_init = 1; 2379 | id->g_main_data_top = 0; 2380 | 2381 | return(PDMP3_OK); 2382 | } 2383 | return(PDMP3_ERR); 2384 | } 2385 | 2386 | /**Description: Feed new data to the MP3 decoder 2387 | * Parameters: Streaming handle,data buffer containging MP3 data, size fo the 2388 | data buffer. 2389 | * Return value: PDMP3_OK or an error 2390 | * Author: Erik Hofman(erik@ehofman.com) **/ 2391 | int pdmp3_feed(pdmp3_handle *id,const unsigned char *in,size_t size){ 2392 | if(id && in && size) { 2393 | int free = Get_Inbuf_Free(id); 2394 | if(size<=free) 2395 | { 2396 | int res; 2397 | if(id->iendistart) 2398 | { 2399 | res = id->istart-id->iend; 2400 | if(sizein+id->iend,in,res); 2402 | id->iend += res; 2403 | } 2404 | else 2405 | { 2406 | res = INBUF_SIZE-id->iend; 2407 | if(sizein+id->iend,in,res); 2410 | id->iend += res; 2411 | size-= res; 2412 | } 2413 | if(size) { 2414 | memcpy(id->in,in+res,size); 2415 | id->iend = size; 2416 | } 2417 | } 2418 | return(PDMP3_OK); 2419 | } 2420 | return(PDMP3_NO_SPACE); 2421 | } 2422 | return(PDMP3_ERR); 2423 | } 2424 | 2425 | /**Description: Convert MP3 data to PCM data 2426 | * Parameters: Stream handle,a pointer to a buffer for the PCM data,the size of 2427 | the PCM buffer in bytes,a pointer to return the number of 2428 | converted bytes. 2429 | * Return value: PDMP3_OK or an error. 2430 | * Author: Erik Hofman(erik@ehofman.com) **/ 2431 | int pdmp3_read(pdmp3_handle *id,unsigned char *outmemory,size_t outsize,size_t *done){ 2432 | if(id && outmemory && outsize && done) { 2433 | *done = 0; 2434 | if(outsize) { 2435 | int res = PDMP3_ERR; 2436 | 2437 | if (id->ostart) { 2438 | Convert_Frame_S16(id,outmemory,outsize,done); 2439 | outmemory += *done; 2440 | outsize -= *done; 2441 | res = PDMP3_OK; 2442 | } 2443 | 2444 | while(outsize) { 2445 | if (Get_Inbuf_Filled(id) >= (2*576)) { 2446 | size_t pos = id->processed; 2447 | unsigned mark = id->istart; 2448 | 2449 | res = Read_Frame(id); 2450 | if(res == PDMP3_OK || res == PDMP3_NEW_FORMAT) { 2451 | size_t batch; 2452 | 2453 | Decode_L3(id); 2454 | Convert_Frame_S16(id,outmemory,outsize,&batch); 2455 | outmemory += batch; 2456 | outsize -= batch; 2457 | *done += batch; 2458 | } 2459 | else { 2460 | id->processed = pos; 2461 | id->istart = mark; 2462 | break; 2463 | } 2464 | } 2465 | else { 2466 | res = PDMP3_NEED_MORE; 2467 | break; 2468 | } 2469 | } /* outsize */ 2470 | if(id->new_header == 1 && res == PDMP3_OK) { 2471 | res = PDMP3_NEW_FORMAT; 2472 | } 2473 | return(res); 2474 | } 2475 | else if(outsize < (2*576)) { 2476 | return(PDMP3_NO_SPACE); 2477 | } 2478 | return(PDMP3_NEED_MORE); 2479 | } 2480 | return(PDMP3_ERR); 2481 | } 2482 | 2483 | /**Description: Feed new data to the MP3 decoder and optionally convert it 2484 | to PCM data. 2485 | * Parameters: Stream handle,a pinter to the MP3 data,size of the MP3 buffer, 2486 | a pointer to a buffer for the PCM data or NULL,the size of 2487 | the PCM buffer in bytes,a pointer to return the number of 2488 | d->ostart converted bytes. 2489 | * Return value: PDMP3_OK or an error. 2490 | * Author: Erik Hofman(erik@ehofman.com) **/ 2491 | int pdmp3_decode(pdmp3_handle *id,const unsigned char *in,size_t insize,unsigned char *out,size_t outsize,size_t *done) 2492 | { 2493 | int free = Get_Inbuf_Free(id); 2494 | int res; 2495 | 2496 | *done = 0; 2497 | if(free > insize) free = insize; 2498 | res = pdmp3_feed(id,in,free); 2499 | 2500 | if(res == PDMP3_OK) 2501 | { 2502 | size_t avail; 2503 | if(out && outsize) { 2504 | res = pdmp3_read(id,out,outsize,&avail); 2505 | *done = avail; 2506 | } 2507 | else if(Get_Filepos(id) == 0) { 2508 | unsigned pos = id->processed; 2509 | unsigned mark = id->istart; 2510 | res = Search_Header(id); 2511 | id->processed = pos; 2512 | id->istart = mark; 2513 | 2514 | if(id->new_header == 1) { 2515 | res = PDMP3_NEW_FORMAT; 2516 | } 2517 | } 2518 | } 2519 | return res; 2520 | } 2521 | 2522 | /**Description: Get the current output format written to the addresses given. 2523 | * Parameters: Stream handle,pointers to store rate, channels and encoding. 2524 | * Return value: PDMP3_OK or an error 2525 | * Author: Erik Hofman(erik@ehofman.com) **/ 2526 | int pdmp3_getformat(pdmp3_handle *id,long *rate,int *channels,int *encoding){ 2527 | if(id && rate && channels && encoding) { 2528 | *encoding = PDMP3_ENC_SIGNED_16; 2529 | *rate = g_sampling_frequency[id->g_frame_header.sampling_frequency]; 2530 | *channels = (id->g_frame_header.mode == mpeg1_mode_single_channel ? 1 : 2); 2531 | id->new_header = -1; 2532 | return(PDMP3_OK); 2533 | } 2534 | return(PDMP3_ERR); 2535 | } 2536 | 2537 | /*############################################################################# 2538 | * mp3s must be NULL terminated 2539 | */ 2540 | void pdmp3(char * const *mp3s){ 2541 | static const char *filename,*audio_name = "/dev/dsp"; 2542 | static FILE *fp =(FILE *) NULL; 2543 | unsigned char out[INBUF_SIZE]; 2544 | pdmp3_handle *id; 2545 | size_t done; 2546 | int res; 2547 | 2548 | if(!strncmp("/dev/dsp",*mp3s,8)){ 2549 | audio_name = *mp3s++; 2550 | } 2551 | 2552 | id = pdmp3_new(NULL,NULL); 2553 | if(id == 0) 2554 | Error("Cannot open stream API (out of memory)",0); 2555 | 2556 | while(*mp3s){ 2557 | filename = *mp3s++; 2558 | if(!strcmp(filename,"-")) fp=stdin; 2559 | else fp = fopen(filename,"r"); 2560 | if(fp == (FILE *) NULL) 2561 | Error("Cannot open file\n",0); 2562 | 2563 | pdmp3_open_feed(id); 2564 | while((res = pdmp3_read(id,out,INBUF_SIZE,&done)) != PDMP3_ERR){ 2565 | audio_write(id,audio_name,filename,out,done); 2566 | if(res == PDMP3_OK || res == PDMP3_NEW_FORMAT) { 2567 | #ifdef DEBUG 2568 | if(res == PDMP3_NEW_FORMAT) { 2569 | int enc,channels; 2570 | long rate; 2571 | 2572 | pdmp3_getformat(id,&rate,&channels,&enc); 2573 | DBG("sample rate: %li Hz, no. channels: %i",rate,channels); 2574 | } 2575 | #endif 2576 | } 2577 | else if(res == PDMP3_NEED_MORE){ 2578 | unsigned char in[4096]; 2579 | 2580 | res = fread(in,1,4096,fp); 2581 | if(!res) break; 2582 | 2583 | res = pdmp3_feed(id,in,res); 2584 | } 2585 | } 2586 | fclose(fp); 2587 | } 2588 | pdmp3_delete(id); 2589 | } 2590 | #endif /* !definend(PDMP3_HEADER_ONLY) */ 2591 | --------------------------------------------------------------------------------