From 8644ad3c0ee12133b66a32649d1633f4652e9846 Mon Sep 17 00:00:00 2001 From: Aric Stewart Date: Thu, 13 Aug 2009 09:14:39 -0500 Subject: [PATCH] winemp3.acm: Update to libmpg123 1.8.1 To: wine-patches This is the minimum set of files from libmpg123 required to enable mpg123 decoding support, modified as little as possible to allow make and makedepend. Additionally: Memory allocations have been changed to HeapAlloc/HeapFree Wine Debugging is used including adding linefeeds to all the debugging lines. Some unneeded header file includes have been removed from the code LGPL 2.1 licence header added to each file. Defines in the Makefile help limit the features compiled and looked for. From here a code cleanup may opt to remove the several large blocks that end up #ifdef'ed out. Also several other large blocks can be removed as they are not used by winemp3.acm --- dlls/winemp3.acm/Makefile.in | 16 +- dlls/winemp3.acm/common.c | 261 --- dlls/winemp3.acm/dct64.c | 188 ++ dlls/winemp3.acm/dct64_i386.c | 261 ++-- dlls/winemp3.acm/decode.h | 273 +++ dlls/winemp3.acm/decode_i386.c | 164 -- dlls/winemp3.acm/equalizer.c | 31 + dlls/winemp3.acm/format.c | 411 +++++ dlls/winemp3.acm/frame.c | 873 +++++++++ dlls/winemp3.acm/frame.h | 364 ++++ dlls/winemp3.acm/getbits.h | 114 ++ dlls/winemp3.acm/huffman.h | 68 +- dlls/winemp3.acm/id3.c | 921 ++++++++++ dlls/winemp3.acm/id3.h | 42 + dlls/winemp3.acm/interface.c | 254 --- dlls/winemp3.acm/layer1.c | 280 ++-- dlls/winemp3.acm/layer2.c | 594 ++++--- dlls/winemp3.acm/layer3.c | 3463 ++++++++++++++++++++--------------- dlls/winemp3.acm/libmpg123.c | 1480 +++++++++++++++ dlls/winemp3.acm/mpegl3.c | 85 +- dlls/winemp3.acm/mpg123.h | 984 +++++++++-- dlls/winemp3.acm/mpg123lib_intern.h | 193 ++ dlls/winemp3.acm/mpglib.h | 69 - dlls/winemp3.acm/optimize.c | 877 +++++++++ dlls/winemp3.acm/optimize.h | 226 +++ dlls/winemp3.acm/parse.c | 1031 +++++++++++ dlls/winemp3.acm/parse.h | 39 + dlls/winemp3.acm/reader.h | 120 ++ dlls/winemp3.acm/readers.c | 1049 +++++++++++ dlls/winemp3.acm/sample.h | 132 ++ dlls/winemp3.acm/synth.c | 557 ++++++ dlls/winemp3.acm/synth.h | 210 +++ dlls/winemp3.acm/synth_mono.h | 78 + dlls/winemp3.acm/synth_real.c | 331 ++++ dlls/winemp3.acm/synth_s32.c | 335 ++++ dlls/winemp3.acm/synths.h | 67 + dlls/winemp3.acm/tabinit.c | 236 ++- 37 files changed, 13674 insertions(+), 3003 deletions(-) delete mode 100644 dlls/winemp3.acm/common.c create mode 100644 dlls/winemp3.acm/dct64.c create mode 100644 dlls/winemp3.acm/decode.h delete mode 100644 dlls/winemp3.acm/decode_i386.c create mode 100644 dlls/winemp3.acm/equalizer.c create mode 100644 dlls/winemp3.acm/format.c create mode 100644 dlls/winemp3.acm/frame.c create mode 100644 dlls/winemp3.acm/frame.h create mode 100644 dlls/winemp3.acm/getbits.h create mode 100644 dlls/winemp3.acm/id3.c create mode 100644 dlls/winemp3.acm/id3.h delete mode 100644 dlls/winemp3.acm/interface.c create mode 100644 dlls/winemp3.acm/libmpg123.c create mode 100644 dlls/winemp3.acm/mpg123lib_intern.h delete mode 100644 dlls/winemp3.acm/mpglib.h create mode 100644 dlls/winemp3.acm/optimize.c create mode 100644 dlls/winemp3.acm/optimize.h create mode 100644 dlls/winemp3.acm/parse.c create mode 100644 dlls/winemp3.acm/parse.h create mode 100644 dlls/winemp3.acm/reader.h create mode 100644 dlls/winemp3.acm/readers.c create mode 100644 dlls/winemp3.acm/sample.h create mode 100644 dlls/winemp3.acm/synth.c create mode 100644 dlls/winemp3.acm/synth.h create mode 100644 dlls/winemp3.acm/synth_mono.h create mode 100644 dlls/winemp3.acm/synth_real.c create mode 100644 dlls/winemp3.acm/synth_s32.c create mode 100644 dlls/winemp3.acm/synths.h diff --git a/dlls/winemp3.acm/Makefile.in b/dlls/winemp3.acm/Makefile.in index 38b5ace..2929866 100644 --- a/dlls/winemp3.acm/Makefile.in +++ b/dlls/winemp3.acm/Makefile.in @@ -4,16 +4,26 @@ SRCDIR = @srcdir@ VPATH = @srcdir@ MODULE = winemp3.acm IMPORTS = winmm user32 kernel32 +EXTRADEFS = -DWIN32 -DGAPLESS -DNO_NTOM -DNO_DOWNSAMPLE -DNO_8BIT -DNO_ID3V2 -DNO_ICY C_SRCS = \ - common.c \ dct64_i386.c \ - decode_i386.c \ - interface.c \ + dct64.c \ + equalizer.c \ + frame.c \ + format.c \ + id3.c \ layer1.c \ layer2.c \ layer3.c \ + libmpg123.c \ mpegl3.c \ + optimize.c \ + parse.c \ + readers.c \ + synth.c \ + synth_real.c \ + synth_s32.c \ tabinit.c @MAKE_DLL_RULES@ diff --git a/dlls/winemp3.acm/common.c b/dlls/winemp3.acm/common.c deleted file mode 100644 index 91ba51a..0000000 --- a/dlls/winemp3.acm/common.c +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Copyright (c) Michael Hipp and other authors of the mpglib project. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#include "config.h" -#include -#include -#include - -#include -#ifdef HAVE_SYS_STAT_H -# include -#endif -#include - -#include "mpg123.h" -#include "wine/debug.h" - -WINE_DEFAULT_DEBUG_CHANNEL(mpeg3); - -static const struct parameter param = { 1 , 1 , 0 , 0 }; - -static const int tabsel_123[2][3][16] = { - { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,}, - {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,}, - {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} }, - - { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,}, - {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}, - {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} } -}; - -static const long freqs[9] = { 44100, 48000, 32000, - 22050, 24000, 16000 , - 11025 , 12000 , 8000 }; - -int bitindex; -unsigned char *wordpointer; -unsigned char *pcm_sample; -int pcm_point = 0; - - -#define HDRCMPMASK 0xfffffd00 - -static int head_check(unsigned long head) -{ - if( (head & 0xffe00000) != 0xffe00000) - return FALSE; - if(!((head>>17)&3)) - return FALSE; - if( ((head>>12)&0xf) == 0xf) - return FALSE; - if( ((head>>10)&0x3) == 0x3 ) - return FALSE; - return TRUE; -} - - -/* - * decode a header and write the information - * into the frame structure - */ -int decode_header(struct frame *fr,unsigned long newhead) -{ - if(head_check(newhead) == FALSE) - return (0); - - if( newhead & (1<<20) ) { - fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1; - fr->mpeg25 = 0; - } - else { - fr->lsf = 1; - fr->mpeg25 = 1; - } - - fr->lay = 4-((newhead>>17)&3); - if(fr->mpeg25) { - fr->sampling_frequency = 6 + ((newhead>>10)&0x3); - } - else - fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3); - fr->error_protection = ((newhead>>16)&0x1)^0x1; - - if(fr->mpeg25) /* allow Bitrate change for 2.5 ... */ - fr->bitrate_index = ((newhead>>12)&0xf); - - fr->bitrate_index = ((newhead>>12)&0xf); - fr->padding = ((newhead>>9)&0x1); - fr->extension = ((newhead>>8)&0x1); - fr->mode = ((newhead>>6)&0x3); - fr->mode_ext = ((newhead>>4)&0x3); - fr->copyright = ((newhead>>3)&0x1); - fr->original = ((newhead>>2)&0x1); - fr->emphasis = newhead & 0x3; - - fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2; - - if(!fr->bitrate_index) - { - FIXME("Free format not supported.\n"); - return (0); - } - - switch(fr->lay) - { - case 1: -#ifdef LAYER1 -#if 0 - fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? - (fr->mode_ext<<2)+4 : 32; -#endif - fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000; - fr->framesize /= freqs[fr->sampling_frequency]; - fr->framesize = ((fr->framesize+fr->padding)<<2)-4; - break; -#else - FIXME("Layer 1 not supported!\n"); - return 0; -#endif - case 2: -#ifdef LAYER2 -#if 0 - fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? - (fr->mode_ext<<2)+4 : fr->II_sblimit; -#endif - fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000; - fr->framesize /= freqs[fr->sampling_frequency]; - fr->framesize += fr->padding - 4; - break; -#else - FIXME("Layer 2 not supported!\n"); - return 0; -#endif - case 3: -#if 0 - fr->do_layer = do_layer3; - if(fr->lsf) - ssize = (fr->stereo == 1) ? 9 : 17; - else - ssize = (fr->stereo == 1) ? 17 : 32; -#endif - -#if 0 - if(fr->error_protection) - ssize += 2; -#endif - fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000; - fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf); - fr->framesize = fr->framesize + fr->padding - 4; - break; - default: - FIXME("Unknown layer type: %d\n", fr->lay); - return (0); - } - return 1; -} - -#if 0 -void print_header(struct frame *fr) -{ - static const char * const modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" }; - static const char * const layers[4] = { "Unknown" , "I", "II", "III" }; - - FIXME("MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n", - fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"), - layers[fr->lay],freqs[fr->sampling_frequency], - modes[fr->mode],fr->mode_ext,fr->framesize+4); - FIXME("Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n", - fr->stereo,fr->copyright?"Yes":"No", - fr->original?"Yes":"No",fr->error_protection?"Yes":"No", - fr->emphasis); - FIXME("Bitrate: %d Kbits/s, Extension value: %d\n", - tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension); -} - -void print_header_compact(struct frame *fr) -{ - static const char * const modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" }; - static const char * const layers[4] = { "Unknown" , "I", "II", "III" }; - - FIXME("MPEG %s layer %s, %d kbit/s, %ld Hz %s\n", - fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"), - layers[fr->lay], - tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index], - freqs[fr->sampling_frequency], modes[fr->mode]); -} - -#endif - -unsigned int getbits(int number_of_bits) -{ - unsigned long rval; - - if(!number_of_bits) - return 0; - - { - rval = wordpointer[0]; - rval <<= 8; - rval |= wordpointer[1]; - rval <<= 8; - rval |= wordpointer[2]; - rval <<= bitindex; - rval &= 0xffffff; - - bitindex += number_of_bits; - - rval >>= (24-number_of_bits); - - wordpointer += (bitindex>>3); - bitindex &= 7; - } - return rval; -} - -unsigned int getbits_fast(int number_of_bits) -{ - unsigned long rval; - - { - rval = wordpointer[0]; - rval <<= 8; - rval |= wordpointer[1]; - rval <<= bitindex; - rval &= 0xffff; - bitindex += number_of_bits; - - rval >>= (16-number_of_bits); - - wordpointer += (bitindex>>3); - bitindex &= 7; - } - return rval; -} - -unsigned int get1bit(void) -{ - unsigned char rval; - rval = *wordpointer << bitindex; - - bitindex++; - wordpointer += (bitindex>>3); - bitindex &= 7; - - return rval>>7; -} diff --git a/dlls/winemp3.acm/dct64.c b/dlls/winemp3.acm/dct64.c new file mode 100644 index 0000000..0b2d74d --- /dev/null +++ b/dlls/winemp3.acm/dct64.c @@ -0,0 +1,188 @@ +/* + dct64.c: DCT64, the plain C version + + copyright ?-2006 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Michael Hipp + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/* + * Discrete Cosine Tansform (DCT) for subband synthesis + * + * -funroll-loops (for gcc) will remove the loops for better performance + * using loops in the source-code enhances readabillity + * + * + * TODO: write an optimized version for the down-sampling modes + * (in these modes the bands 16-31 (2:1) or 8-31 (4:1) are zero + */ + +#include "mpg123lib_intern.h" + +void dct64(real *out0,real *out1,real *samples) +{ + real bufs[64]; + + { + register int i,j; + register real *b1,*b2,*bs,*costab; + + b1 = samples; + bs = bufs; + costab = pnts[0]+16; + b2 = b1 + 32; + + for(i=15;i>=0;i--) + *bs++ = (*b1++ + *--b2); + for(i=15;i>=0;i--) + *bs++ = REAL_MUL((*--b2 - *b1++), *--costab); + + b1 = bufs; + costab = pnts[1]+8; + b2 = b1 + 16; + + { + for(i=7;i>=0;i--) + *bs++ = (*b1++ + *--b2); + for(i=7;i>=0;i--) + *bs++ = REAL_MUL((*--b2 - *b1++), *--costab); + b2 += 32; + costab += 8; + for(i=7;i>=0;i--) + *bs++ = (*b1++ + *--b2); + for(i=7;i>=0;i--) + *bs++ = REAL_MUL((*b1++ - *--b2), *--costab); + b2 += 32; + } + + bs = bufs; + costab = pnts[2]; + b2 = b1 + 8; + + for(j=2;j;j--) + { + for(i=3;i>=0;i--) + *bs++ = (*b1++ + *--b2); + for(i=3;i>=0;i--) + *bs++ = REAL_MUL((*--b2 - *b1++), costab[i]); + b2 += 16; + for(i=3;i>=0;i--) + *bs++ = (*b1++ + *--b2); + for(i=3;i>=0;i--) + *bs++ = REAL_MUL((*b1++ - *--b2), costab[i]); + b2 += 16; + } + + b1 = bufs; + costab = pnts[3]; + b2 = b1 + 4; + + for(j=4;j;j--) + { + *bs++ = (*b1++ + *--b2); + *bs++ = (*b1++ + *--b2); + *bs++ = REAL_MUL((*--b2 - *b1++), costab[1]); + *bs++ = REAL_MUL((*--b2 - *b1++), costab[0]); + b2 += 8; + *bs++ = (*b1++ + *--b2); + *bs++ = (*b1++ + *--b2); + *bs++ = REAL_MUL((*b1++ - *--b2), costab[1]); + *bs++ = REAL_MUL((*b1++ - *--b2), costab[0]); + b2 += 8; + } + bs = bufs; + costab = pnts[4]; + + for(j=8;j;j--) + { + real v0,v1; + v0=*b1++; v1 = *b1++; + *bs++ = (v0 + v1); + *bs++ = REAL_MUL((v0 - v1), (*costab)); + v0=*b1++; v1 = *b1++; + *bs++ = (v0 + v1); + *bs++ = REAL_MUL((v1 - v0), (*costab)); + } + + } + + + { + register real *b1; + register int i; + + for(b1=bufs,i=8;i;i--,b1+=4) + b1[2] += b1[3]; + + for(b1=bufs,i=4;i;i--,b1+=8) + { + b1[4] += b1[6]; + b1[6] += b1[5]; + b1[5] += b1[7]; + } + + for(b1=bufs,i=2;i;i--,b1+=16) + { + b1[8] += b1[12]; + b1[12] += b1[10]; + b1[10] += b1[14]; + b1[14] += b1[9]; + b1[9] += b1[13]; + b1[13] += b1[11]; + b1[11] += b1[15]; + } + } + + + out0[0x10*16] = bufs[0]; + out0[0x10*15] = bufs[16+0] + bufs[16+8]; + out0[0x10*14] = bufs[8]; + out0[0x10*13] = bufs[16+8] + bufs[16+4]; + out0[0x10*12] = bufs[4]; + out0[0x10*11] = bufs[16+4] + bufs[16+12]; + out0[0x10*10] = bufs[12]; + out0[0x10* 9] = bufs[16+12] + bufs[16+2]; + out0[0x10* 8] = bufs[2]; + out0[0x10* 7] = bufs[16+2] + bufs[16+10]; + out0[0x10* 6] = bufs[10]; + out0[0x10* 5] = bufs[16+10] + bufs[16+6]; + out0[0x10* 4] = bufs[6]; + out0[0x10* 3] = bufs[16+6] + bufs[16+14]; + out0[0x10* 2] = bufs[14]; + out0[0x10* 1] = bufs[16+14] + bufs[16+1]; + out0[0x10* 0] = bufs[1]; + + out1[0x10* 0] = bufs[1]; + out1[0x10* 1] = bufs[16+1] + bufs[16+9]; + out1[0x10* 2] = bufs[9]; + out1[0x10* 3] = bufs[16+9] + bufs[16+5]; + out1[0x10* 4] = bufs[5]; + out1[0x10* 5] = bufs[16+5] + bufs[16+13]; + out1[0x10* 6] = bufs[13]; + out1[0x10* 7] = bufs[16+13] + bufs[16+3]; + out1[0x10* 8] = bufs[3]; + out1[0x10* 9] = bufs[16+3] + bufs[16+11]; + out1[0x10*10] = bufs[11]; + out1[0x10*11] = bufs[16+11] + bufs[16+7]; + out1[0x10*12] = bufs[7]; + out1[0x10*13] = bufs[16+7] + bufs[16+15]; + out1[0x10*14] = bufs[15]; + out1[0x10*15] = bufs[16+15]; + +} + + diff --git a/dlls/winemp3.acm/dct64_i386.c b/dlls/winemp3.acm/dct64_i386.c index 42166ba..8151e4e 100644 --- a/dlls/winemp3.acm/dct64_i386.c +++ b/dlls/winemp3.acm/dct64_i386.c @@ -1,12 +1,10 @@ /* - * Copyright (c) Michael Hipp and other authors of the mpglib project. - * - * Discrete Cosine Transform (DCT) for subband synthesis - * optimized for machines with no auto-increment. - * The performance is highly compiler dependent. Maybe - * the dct64.c version for 'normal' processor may be faster - * even for Intel processors. - * + dct64_i386.c: DCT64, a C variant for i386 + + copyright ?-2006 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Michael Hipp + * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -22,61 +20,61 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -#include "mpg123.h" +/* + * Discrete Cosine Tansform (DCT) for subband synthesis + * optimized for machines with no auto-increment. + * The performance is highly compiler dependend. Maybe + * the dct64.c version for 'normal' processor may be faster + * even for Intel processors. + */ + +#include "mpg123lib_intern.h" static void dct64_1(real *out0,real *out1,real *b1,real *b2,real *samples) { - { register real *costab = pnts[0]; b1[0x00] = samples[0x00] + samples[0x1F]; - b1[0x1F] = (samples[0x00] - samples[0x1F]) * costab[0x0]; - b1[0x01] = samples[0x01] + samples[0x1E]; - b1[0x1E] = (samples[0x01] - samples[0x1E]) * costab[0x1]; + b1[0x1F] = REAL_MUL(samples[0x00] - samples[0x1F], costab[0x0]); + b1[0x1E] = REAL_MUL(samples[0x01] - samples[0x1E], costab[0x1]); b1[0x02] = samples[0x02] + samples[0x1D]; - b1[0x1D] = (samples[0x02] - samples[0x1D]) * costab[0x2]; - b1[0x03] = samples[0x03] + samples[0x1C]; - b1[0x1C] = (samples[0x03] - samples[0x1C]) * costab[0x3]; + b1[0x1D] = REAL_MUL(samples[0x02] - samples[0x1D], costab[0x2]); + b1[0x1C] = REAL_MUL(samples[0x03] - samples[0x1C], costab[0x3]); b1[0x04] = samples[0x04] + samples[0x1B]; - b1[0x1B] = (samples[0x04] - samples[0x1B]) * costab[0x4]; - b1[0x05] = samples[0x05] + samples[0x1A]; - b1[0x1A] = (samples[0x05] - samples[0x1A]) * costab[0x5]; + b1[0x1B] = REAL_MUL(samples[0x04] - samples[0x1B], costab[0x4]); + b1[0x1A] = REAL_MUL(samples[0x05] - samples[0x1A], costab[0x5]); b1[0x06] = samples[0x06] + samples[0x19]; - b1[0x19] = (samples[0x06] - samples[0x19]) * costab[0x6]; - b1[0x07] = samples[0x07] + samples[0x18]; - b1[0x18] = (samples[0x07] - samples[0x18]) * costab[0x7]; + b1[0x19] = REAL_MUL(samples[0x06] - samples[0x19], costab[0x6]); + b1[0x18] = REAL_MUL(samples[0x07] - samples[0x18], costab[0x7]); b1[0x08] = samples[0x08] + samples[0x17]; - b1[0x17] = (samples[0x08] - samples[0x17]) * costab[0x8]; - b1[0x09] = samples[0x09] + samples[0x16]; - b1[0x16] = (samples[0x09] - samples[0x16]) * costab[0x9]; + b1[0x17] = REAL_MUL(samples[0x08] - samples[0x17], costab[0x8]); + b1[0x16] = REAL_MUL(samples[0x09] - samples[0x16], costab[0x9]); b1[0x0A] = samples[0x0A] + samples[0x15]; - b1[0x15] = (samples[0x0A] - samples[0x15]) * costab[0xA]; - b1[0x0B] = samples[0x0B] + samples[0x14]; - b1[0x14] = (samples[0x0B] - samples[0x14]) * costab[0xB]; + b1[0x15] = REAL_MUL(samples[0x0A] - samples[0x15], costab[0xA]); + b1[0x14] = REAL_MUL(samples[0x0B] - samples[0x14], costab[0xB]); b1[0x0C] = samples[0x0C] + samples[0x13]; - b1[0x13] = (samples[0x0C] - samples[0x13]) * costab[0xC]; - b1[0x0D] = samples[0x0D] + samples[0x12]; - b1[0x12] = (samples[0x0D] - samples[0x12]) * costab[0xD]; + b1[0x13] = REAL_MUL(samples[0x0C] - samples[0x13], costab[0xC]); + b1[0x12] = REAL_MUL(samples[0x0D] - samples[0x12], costab[0xD]); b1[0x0E] = samples[0x0E] + samples[0x11]; - b1[0x11] = (samples[0x0E] - samples[0x11]) * costab[0xE]; - b1[0x0F] = samples[0x0F] + samples[0x10]; - b1[0x10] = (samples[0x0F] - samples[0x10]) * costab[0xF]; + b1[0x11] = REAL_MUL(samples[0x0E] - samples[0x11], costab[0xE]); + b1[0x10] = REAL_MUL(samples[0x0F] - samples[0x10], costab[0xF]); + } @@ -84,78 +82,86 @@ static void dct64_1(real *out0,real *out1,real *b1,real *b2,real *samples) register real *costab = pnts[1]; b2[0x00] = b1[0x00] + b1[0x0F]; - b2[0x0F] = (b1[0x00] - b1[0x0F]) * costab[0]; b2[0x01] = b1[0x01] + b1[0x0E]; - b2[0x0E] = (b1[0x01] - b1[0x0E]) * costab[1]; + b2[0x0F] = REAL_MUL(b1[0x00] - b1[0x0F], costab[0]); + b2[0x0E] = REAL_MUL(b1[0x01] - b1[0x0E], costab[1]); + b2[0x02] = b1[0x02] + b1[0x0D]; - b2[0x0D] = (b1[0x02] - b1[0x0D]) * costab[2]; b2[0x03] = b1[0x03] + b1[0x0C]; - b2[0x0C] = (b1[0x03] - b1[0x0C]) * costab[3]; + b2[0x0D] = REAL_MUL(b1[0x02] - b1[0x0D], costab[2]); + b2[0x0C] = REAL_MUL(b1[0x03] - b1[0x0C], costab[3]); + b2[0x04] = b1[0x04] + b1[0x0B]; - b2[0x0B] = (b1[0x04] - b1[0x0B]) * costab[4]; b2[0x05] = b1[0x05] + b1[0x0A]; - b2[0x0A] = (b1[0x05] - b1[0x0A]) * costab[5]; + b2[0x0B] = REAL_MUL(b1[0x04] - b1[0x0B], costab[4]); + b2[0x0A] = REAL_MUL(b1[0x05] - b1[0x0A], costab[5]); + b2[0x06] = b1[0x06] + b1[0x09]; - b2[0x09] = (b1[0x06] - b1[0x09]) * costab[6]; b2[0x07] = b1[0x07] + b1[0x08]; - b2[0x08] = (b1[0x07] - b1[0x08]) * costab[7]; + b2[0x09] = REAL_MUL(b1[0x06] - b1[0x09], costab[6]); + b2[0x08] = REAL_MUL(b1[0x07] - b1[0x08], costab[7]); + + /* */ b2[0x10] = b1[0x10] + b1[0x1F]; - b2[0x1F] = (b1[0x1F] - b1[0x10]) * costab[0]; b2[0x11] = b1[0x11] + b1[0x1E]; - b2[0x1E] = (b1[0x1E] - b1[0x11]) * costab[1]; + b2[0x1F] = REAL_MUL(b1[0x1F] - b1[0x10], costab[0]); + b2[0x1E] = REAL_MUL(b1[0x1E] - b1[0x11], costab[1]); + b2[0x12] = b1[0x12] + b1[0x1D]; - b2[0x1D] = (b1[0x1D] - b1[0x12]) * costab[2]; b2[0x13] = b1[0x13] + b1[0x1C]; - b2[0x1C] = (b1[0x1C] - b1[0x13]) * costab[3]; + b2[0x1D] = REAL_MUL(b1[0x1D] - b1[0x12], costab[2]); + b2[0x1C] = REAL_MUL(b1[0x1C] - b1[0x13], costab[3]); + b2[0x14] = b1[0x14] + b1[0x1B]; - b2[0x1B] = (b1[0x1B] - b1[0x14]) * costab[4]; b2[0x15] = b1[0x15] + b1[0x1A]; - b2[0x1A] = (b1[0x1A] - b1[0x15]) * costab[5]; + b2[0x1B] = REAL_MUL(b1[0x1B] - b1[0x14], costab[4]); + b2[0x1A] = REAL_MUL(b1[0x1A] - b1[0x15], costab[5]); + b2[0x16] = b1[0x16] + b1[0x19]; - b2[0x19] = (b1[0x19] - b1[0x16]) * costab[6]; b2[0x17] = b1[0x17] + b1[0x18]; - b2[0x18] = (b1[0x18] - b1[0x17]) * costab[7]; + b2[0x19] = REAL_MUL(b1[0x19] - b1[0x16], costab[6]); + b2[0x18] = REAL_MUL(b1[0x18] - b1[0x17], costab[7]); } { register real *costab = pnts[2]; b1[0x00] = b2[0x00] + b2[0x07]; - b1[0x07] = (b2[0x00] - b2[0x07]) * costab[0]; + b1[0x07] = REAL_MUL(b2[0x00] - b2[0x07], costab[0]); b1[0x01] = b2[0x01] + b2[0x06]; - b1[0x06] = (b2[0x01] - b2[0x06]) * costab[1]; + b1[0x06] = REAL_MUL(b2[0x01] - b2[0x06], costab[1]); b1[0x02] = b2[0x02] + b2[0x05]; - b1[0x05] = (b2[0x02] - b2[0x05]) * costab[2]; + b1[0x05] = REAL_MUL(b2[0x02] - b2[0x05], costab[2]); b1[0x03] = b2[0x03] + b2[0x04]; - b1[0x04] = (b2[0x03] - b2[0x04]) * costab[3]; + b1[0x04] = REAL_MUL(b2[0x03] - b2[0x04], costab[3]); b1[0x08] = b2[0x08] + b2[0x0F]; - b1[0x0F] = (b2[0x0F] - b2[0x08]) * costab[0]; + b1[0x0F] = REAL_MUL(b2[0x0F] - b2[0x08], costab[0]); b1[0x09] = b2[0x09] + b2[0x0E]; - b1[0x0E] = (b2[0x0E] - b2[0x09]) * costab[1]; + b1[0x0E] = REAL_MUL(b2[0x0E] - b2[0x09], costab[1]); b1[0x0A] = b2[0x0A] + b2[0x0D]; - b1[0x0D] = (b2[0x0D] - b2[0x0A]) * costab[2]; + b1[0x0D] = REAL_MUL(b2[0x0D] - b2[0x0A], costab[2]); b1[0x0B] = b2[0x0B] + b2[0x0C]; - b1[0x0C] = (b2[0x0C] - b2[0x0B]) * costab[3]; + b1[0x0C] = REAL_MUL(b2[0x0C] - b2[0x0B], costab[3]); b1[0x10] = b2[0x10] + b2[0x17]; - b1[0x17] = (b2[0x10] - b2[0x17]) * costab[0]; + b1[0x17] = REAL_MUL(b2[0x10] - b2[0x17], costab[0]); b1[0x11] = b2[0x11] + b2[0x16]; - b1[0x16] = (b2[0x11] - b2[0x16]) * costab[1]; + b1[0x16] = REAL_MUL(b2[0x11] - b2[0x16], costab[1]); b1[0x12] = b2[0x12] + b2[0x15]; - b1[0x15] = (b2[0x12] - b2[0x15]) * costab[2]; + b1[0x15] = REAL_MUL(b2[0x12] - b2[0x15], costab[2]); b1[0x13] = b2[0x13] + b2[0x14]; - b1[0x14] = (b2[0x13] - b2[0x14]) * costab[3]; + b1[0x14] = REAL_MUL(b2[0x13] - b2[0x14], costab[3]); b1[0x18] = b2[0x18] + b2[0x1F]; - b1[0x1F] = (b2[0x1F] - b2[0x18]) * costab[0]; + b1[0x1F] = REAL_MUL(b2[0x1F] - b2[0x18], costab[0]); b1[0x19] = b2[0x19] + b2[0x1E]; - b1[0x1E] = (b2[0x1E] - b2[0x19]) * costab[1]; + b1[0x1E] = REAL_MUL(b2[0x1E] - b2[0x19], costab[1]); b1[0x1A] = b2[0x1A] + b2[0x1D]; - b1[0x1D] = (b2[0x1D] - b2[0x1A]) * costab[2]; + b1[0x1D] = REAL_MUL(b2[0x1D] - b2[0x1A], costab[2]); b1[0x1B] = b2[0x1B] + b2[0x1C]; - b1[0x1C] = (b2[0x1C] - b2[0x1B]) * costab[3]; + b1[0x1C] = REAL_MUL(b2[0x1C] - b2[0x1B], costab[3]); } { @@ -163,104 +169,104 @@ static void dct64_1(real *out0,real *out1,real *b1,real *b2,real *samples) register real const cos1 = pnts[3][1]; b2[0x00] = b1[0x00] + b1[0x03]; - b2[0x03] = (b1[0x00] - b1[0x03]) * cos0; + b2[0x03] = REAL_MUL(b1[0x00] - b1[0x03], cos0); b2[0x01] = b1[0x01] + b1[0x02]; - b2[0x02] = (b1[0x01] - b1[0x02]) * cos1; + b2[0x02] = REAL_MUL(b1[0x01] - b1[0x02], cos1); b2[0x04] = b1[0x04] + b1[0x07]; - b2[0x07] = (b1[0x07] - b1[0x04]) * cos0; + b2[0x07] = REAL_MUL(b1[0x07] - b1[0x04], cos0); b2[0x05] = b1[0x05] + b1[0x06]; - b2[0x06] = (b1[0x06] - b1[0x05]) * cos1; + b2[0x06] = REAL_MUL(b1[0x06] - b1[0x05], cos1); b2[0x08] = b1[0x08] + b1[0x0B]; - b2[0x0B] = (b1[0x08] - b1[0x0B]) * cos0; + b2[0x0B] = REAL_MUL(b1[0x08] - b1[0x0B], cos0); b2[0x09] = b1[0x09] + b1[0x0A]; - b2[0x0A] = (b1[0x09] - b1[0x0A]) * cos1; + b2[0x0A] = REAL_MUL(b1[0x09] - b1[0x0A], cos1); b2[0x0C] = b1[0x0C] + b1[0x0F]; - b2[0x0F] = (b1[0x0F] - b1[0x0C]) * cos0; + b2[0x0F] = REAL_MUL(b1[0x0F] - b1[0x0C], cos0); b2[0x0D] = b1[0x0D] + b1[0x0E]; - b2[0x0E] = (b1[0x0E] - b1[0x0D]) * cos1; + b2[0x0E] = REAL_MUL(b1[0x0E] - b1[0x0D], cos1); b2[0x10] = b1[0x10] + b1[0x13]; - b2[0x13] = (b1[0x10] - b1[0x13]) * cos0; + b2[0x13] = REAL_MUL(b1[0x10] - b1[0x13], cos0); b2[0x11] = b1[0x11] + b1[0x12]; - b2[0x12] = (b1[0x11] - b1[0x12]) * cos1; + b2[0x12] = REAL_MUL(b1[0x11] - b1[0x12], cos1); b2[0x14] = b1[0x14] + b1[0x17]; - b2[0x17] = (b1[0x17] - b1[0x14]) * cos0; + b2[0x17] = REAL_MUL(b1[0x17] - b1[0x14], cos0); b2[0x15] = b1[0x15] + b1[0x16]; - b2[0x16] = (b1[0x16] - b1[0x15]) * cos1; + b2[0x16] = REAL_MUL(b1[0x16] - b1[0x15], cos1); b2[0x18] = b1[0x18] + b1[0x1B]; - b2[0x1B] = (b1[0x18] - b1[0x1B]) * cos0; + b2[0x1B] = REAL_MUL(b1[0x18] - b1[0x1B], cos0); b2[0x19] = b1[0x19] + b1[0x1A]; - b2[0x1A] = (b1[0x19] - b1[0x1A]) * cos1; + b2[0x1A] = REAL_MUL(b1[0x19] - b1[0x1A], cos1); b2[0x1C] = b1[0x1C] + b1[0x1F]; - b2[0x1F] = (b1[0x1F] - b1[0x1C]) * cos0; + b2[0x1F] = REAL_MUL(b1[0x1F] - b1[0x1C], cos0); b2[0x1D] = b1[0x1D] + b1[0x1E]; - b2[0x1E] = (b1[0x1E] - b1[0x1D]) * cos1; + b2[0x1E] = REAL_MUL(b1[0x1E] - b1[0x1D], cos1); } { register real const cos0 = pnts[4][0]; b1[0x00] = b2[0x00] + b2[0x01]; - b1[0x01] = (b2[0x00] - b2[0x01]) * cos0; + b1[0x01] = REAL_MUL(b2[0x00] - b2[0x01], cos0); b1[0x02] = b2[0x02] + b2[0x03]; - b1[0x03] = (b2[0x03] - b2[0x02]) * cos0; + b1[0x03] = REAL_MUL(b2[0x03] - b2[0x02], cos0); b1[0x02] += b1[0x03]; b1[0x04] = b2[0x04] + b2[0x05]; - b1[0x05] = (b2[0x04] - b2[0x05]) * cos0; + b1[0x05] = REAL_MUL(b2[0x04] - b2[0x05], cos0); b1[0x06] = b2[0x06] + b2[0x07]; - b1[0x07] = (b2[0x07] - b2[0x06]) * cos0; + b1[0x07] = REAL_MUL(b2[0x07] - b2[0x06], cos0); b1[0x06] += b1[0x07]; b1[0x04] += b1[0x06]; b1[0x06] += b1[0x05]; b1[0x05] += b1[0x07]; b1[0x08] = b2[0x08] + b2[0x09]; - b1[0x09] = (b2[0x08] - b2[0x09]) * cos0; + b1[0x09] = REAL_MUL(b2[0x08] - b2[0x09], cos0); b1[0x0A] = b2[0x0A] + b2[0x0B]; - b1[0x0B] = (b2[0x0B] - b2[0x0A]) * cos0; + b1[0x0B] = REAL_MUL(b2[0x0B] - b2[0x0A], cos0); b1[0x0A] += b1[0x0B]; b1[0x0C] = b2[0x0C] + b2[0x0D]; - b1[0x0D] = (b2[0x0C] - b2[0x0D]) * cos0; + b1[0x0D] = REAL_MUL(b2[0x0C] - b2[0x0D], cos0); b1[0x0E] = b2[0x0E] + b2[0x0F]; - b1[0x0F] = (b2[0x0F] - b2[0x0E]) * cos0; + b1[0x0F] = REAL_MUL(b2[0x0F] - b2[0x0E], cos0); b1[0x0E] += b1[0x0F]; b1[0x0C] += b1[0x0E]; b1[0x0E] += b1[0x0D]; b1[0x0D] += b1[0x0F]; b1[0x10] = b2[0x10] + b2[0x11]; - b1[0x11] = (b2[0x10] - b2[0x11]) * cos0; + b1[0x11] = REAL_MUL(b2[0x10] - b2[0x11], cos0); b1[0x12] = b2[0x12] + b2[0x13]; - b1[0x13] = (b2[0x13] - b2[0x12]) * cos0; + b1[0x13] = REAL_MUL(b2[0x13] - b2[0x12], cos0); b1[0x12] += b1[0x13]; b1[0x14] = b2[0x14] + b2[0x15]; - b1[0x15] = (b2[0x14] - b2[0x15]) * cos0; + b1[0x15] = REAL_MUL(b2[0x14] - b2[0x15], cos0); b1[0x16] = b2[0x16] + b2[0x17]; - b1[0x17] = (b2[0x17] - b2[0x16]) * cos0; + b1[0x17] = REAL_MUL(b2[0x17] - b2[0x16], cos0); b1[0x16] += b1[0x17]; b1[0x14] += b1[0x16]; b1[0x16] += b1[0x15]; b1[0x15] += b1[0x17]; b1[0x18] = b2[0x18] + b2[0x19]; - b1[0x19] = (b2[0x18] - b2[0x19]) * cos0; + b1[0x19] = REAL_MUL(b2[0x18] - b2[0x19], cos0); b1[0x1A] = b2[0x1A] + b2[0x1B]; - b1[0x1B] = (b2[0x1B] - b2[0x1A]) * cos0; + b1[0x1B] = REAL_MUL(b2[0x1B] - b2[0x1A], cos0); b1[0x1A] += b1[0x1B]; b1[0x1C] = b2[0x1C] + b2[0x1D]; - b1[0x1D] = (b2[0x1C] - b2[0x1D]) * cos0; + b1[0x1D] = REAL_MUL(b2[0x1C] - b2[0x1D], cos0); b1[0x1E] = b2[0x1E] + b2[0x1F]; - b1[0x1F] = (b2[0x1F] - b2[0x1E]) * cos0; + b1[0x1F] = REAL_MUL(b2[0x1F] - b2[0x1E], cos0); b1[0x1E] += b1[0x1F]; b1[0x1C] += b1[0x1E]; b1[0x1E] += b1[0x1D]; @@ -277,6 +283,16 @@ static void dct64_1(real *out0,real *out1,real *b1,real *b2,real *samples) out1[0x10* 8] = b1[0x03]; out1[0x10*12] = b1[0x07]; +#if 1 + out0[0x10*14] = b1[0x08] + b1[0x0C]; + out0[0x10*10] = b1[0x0C] + b1[0x0a]; + out0[0x10* 6] = b1[0x0A] + b1[0x0E]; + out0[0x10* 2] = b1[0x0E] + b1[0x09]; + out1[0x10* 2] = b1[0x09] + b1[0x0D]; + out1[0x10* 6] = b1[0x0D] + b1[0x0B]; + out1[0x10*10] = b1[0x0B] + b1[0x0F]; + out1[0x10*14] = b1[0x0F]; +#else b1[0x08] += b1[0x0C]; out0[0x10*14] = b1[0x08]; b1[0x0C] += b1[0x0a]; @@ -292,38 +308,43 @@ static void dct64_1(real *out0,real *out1,real *b1,real *b2,real *samples) b1[0x0B] += b1[0x0F]; out1[0x10*10] = b1[0x0B]; out1[0x10*14] = b1[0x0F]; +#endif - b1[0x18] += b1[0x1C]; - out0[0x10*15] = b1[0x10] + b1[0x18]; - out0[0x10*13] = b1[0x18] + b1[0x14]; - b1[0x1C] += b1[0x1a]; - out0[0x10*11] = b1[0x14] + b1[0x1C]; - out0[0x10* 9] = b1[0x1C] + b1[0x12]; - b1[0x1A] += b1[0x1E]; - out0[0x10* 7] = b1[0x12] + b1[0x1A]; - out0[0x10* 5] = b1[0x1A] + b1[0x16]; - b1[0x1E] += b1[0x19]; - out0[0x10* 3] = b1[0x16] + b1[0x1E]; - out0[0x10* 1] = b1[0x1E] + b1[0x11]; - b1[0x19] += b1[0x1D]; - out1[0x10* 1] = b1[0x11] + b1[0x19]; - out1[0x10* 3] = b1[0x19] + b1[0x15]; - b1[0x1D] += b1[0x1B]; - out1[0x10* 5] = b1[0x15] + b1[0x1D]; - out1[0x10* 7] = b1[0x1D] + b1[0x13]; - b1[0x1B] += b1[0x1F]; - out1[0x10* 9] = b1[0x13] + b1[0x1B]; - out1[0x10*11] = b1[0x1B] + b1[0x17]; + { + real tmp; + tmp = b1[0x18] + b1[0x1C]; + out0[0x10*15] = tmp + b1[0x10]; + out0[0x10*13] = tmp + b1[0x14]; + tmp = b1[0x1C] + b1[0x1A]; + out0[0x10*11] = tmp + b1[0x14]; + out0[0x10* 9] = tmp + b1[0x12]; + tmp = b1[0x1A] + b1[0x1E]; + out0[0x10* 7] = tmp + b1[0x12]; + out0[0x10* 5] = tmp + b1[0x16]; + tmp = b1[0x1E] + b1[0x19]; + out0[0x10* 3] = tmp + b1[0x16]; + out0[0x10* 1] = tmp + b1[0x11]; + tmp = b1[0x19] + b1[0x1D]; + out1[0x10* 1] = tmp + b1[0x11]; + out1[0x10* 3] = tmp + b1[0x15]; + tmp = b1[0x1D] + b1[0x1B]; + out1[0x10* 5] = tmp + b1[0x15]; + out1[0x10* 7] = tmp + b1[0x13]; + tmp = b1[0x1B] + b1[0x1F]; + out1[0x10* 9] = tmp + b1[0x13]; + out1[0x10*11] = tmp + b1[0x17]; out1[0x10*13] = b1[0x17] + b1[0x1F]; out1[0x10*15] = b1[0x1F]; + } } /* * the call via dct64 is a trick to force GCC to use * (new) registers for the b1,b2 pointer to the bufs[xx] field */ -void dct64(real *a,real *b,real *c) +void dct64_i386(real *a,real *b,real *c) { real bufs[0x40]; dct64_1(a,b,bufs,bufs+0x20,c); } + diff --git a/dlls/winemp3.acm/decode.h b/dlls/winemp3.acm/decode.h new file mode 100644 index 0000000..7d2a9a8 --- /dev/null +++ b/dlls/winemp3.acm/decode.h @@ -0,0 +1,273 @@ +/* + decode.h: common definitions for decode functions + + This file is strongly tied with optimize.h concerning the synth functions. + Perhaps one should restructure that a bit. + + copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Thomas Orgis, taking WRITE_SAMPLE from decode.c + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ +#ifndef MPG123_DECODE_H +#define MPG123_DECODE_H + +/* Selection of class of output routines for basic format. */ +#ifndef REAL_IS_FIXED +#define OUT_FORMATS 4 /* Basic output formats: 16bit, 8bit, real and s32 */ +#else +#define OUT_FORMATS 2 /* Only up to 16bit */ +#endif + +#define OUT_16 0 +#define OUT_8 1 +/* Those are defined but not supported for fixed point decoding! */ +#define OUT_REAL 2 /* Write a floating point sample (that is, one matching the internal real type). */ +#define OUT_S32 3 + +#ifdef NO_NTOM +#define NTOM_MAX 1 +#else +#define NTOM_MAX 8 /* maximum allowed factor for upsampling */ +#define NTOM_MAX_FREQ 96000 /* maximum frequency to upsample to / downsample from */ +#define NTOM_MUL (32768) +#endif + +/* Let's collect all possible synth functions here, for an overview. + If they are actually defined and used depends on preprocessor machinery. + See synth.c and optimize.h for that, also some special C and assembler files. */ + +/* The call of left and right plain synth, wrapped. + This may be replaced by a direct stereo optimized synth. */ +int synth_stereo_wrap(real*, real*, mpg123_handle*); + +#ifndef NO_16BIT +/* The signed-16bit-producing variants. */ +int synth_1to1 (real*, int, mpg123_handle*, int); +int synth_1to1_dither (real*, int, mpg123_handle*, int); +int synth_1to1_i386 (real*, int, mpg123_handle*, int); +int synth_1to1_i586 (real*, int, mpg123_handle*, int); +int synth_1to1_i586_dither(real*, int, mpg123_handle*, int); +int synth_1to1_mmx (real*, int, mpg123_handle*, int); +int synth_1to1_3dnow (real*, int, mpg123_handle*, int); +int synth_1to1_sse (real*, int, mpg123_handle*, int); +int synth_1to1_stereo_sse (real*, real*, mpg123_handle*); +int synth_1to1_3dnowext (real*, int, mpg123_handle*, int); +int synth_1to1_altivec (real*, int, mpg123_handle*, int); +int synth_1to1_stereo_altivec(real*, real*, mpg123_handle*); +int synth_1to1_x86_64 (real*, int, mpg123_handle*, int); +int synth_1to1_stereo_x86_64(real*, real*, mpg123_handle*); +/* This is different, special usage in layer3.c only. + Hence, the name... and now forget about it. + Never use it outside that special portion of code inside layer3.c! */ +int absynth_1to1_i486(real*, int, mpg123_handle*, int); +/* These mono/stereo converters use one of the above for the grunt work. */ +int synth_1to1_mono (real*, mpg123_handle*); +int synth_1to1_mono2stereo(real*, mpg123_handle*); + +/* Sample rate decimation comes in less flavours. */ +#ifndef NO_DOWNSAMPLE +int synth_2to1 (real*, int, mpg123_handle*, int); +int synth_2to1_dither (real*, int, mpg123_handle*, int); +int synth_2to1_i386 (real*, int, mpg123_handle*, int); +int synth_2to1_mono (real*, mpg123_handle*); +int synth_2to1_mono2stereo(real*, mpg123_handle*); +int synth_4to1 (real *,int, mpg123_handle*, int); +int synth_4to1_dither (real *,int, mpg123_handle*, int); +int synth_4to1_i386 (real*, int, mpg123_handle*, int); +int synth_4to1_mono (real*, mpg123_handle*); +int synth_4to1_mono2stereo(real*, mpg123_handle*); +#endif +#ifndef NO_NTOM +/* NtoM is really just one implementation. */ +int synth_ntom (real *,int, mpg123_handle*, int); +int synth_ntom_mono (real *, mpg123_handle *); +int synth_ntom_mono2stereo (real *, mpg123_handle *); +#endif +#endif + +#ifndef NO_8BIT +/* The 8bit-producing variants. */ +/* There are direct 8-bit synths and wrappers over a possibly optimized 16bit one. */ +int synth_1to1_8bit (real*, int, mpg123_handle*, int); +int synth_1to1_8bit_i386 (real*, int, mpg123_handle*, int); +#ifndef NO_16BIT +int synth_1to1_8bit_wrap (real*, int, mpg123_handle*, int); +int synth_1to1_8bit_mono (real*, mpg123_handle*); +#endif +int synth_1to1_8bit_mono2stereo(real*, mpg123_handle*); +#ifndef NO_16BIT +int synth_1to1_8bit_wrap_mono (real*, mpg123_handle*); +int synth_1to1_8bit_wrap_mono2stereo(real*, mpg123_handle*); +#endif +#ifndef NO_DOWNSAMPLE +int synth_2to1_8bit (real*, int, mpg123_handle*, int); +int synth_2to1_8bit_i386 (real*, int, mpg123_handle*, int); +int synth_2to1_8bit_mono (real*, mpg123_handle*); +int synth_2to1_8bit_mono2stereo(real*, mpg123_handle*); +int synth_4to1_8bit (real*, int, mpg123_handle*, int); +int synth_4to1_8bit_i386 (real*, int, mpg123_handle*, int); +int synth_4to1_8bit_mono (real*, mpg123_handle*); +int synth_4to1_8bit_mono2stereo(real*, mpg123_handle*); +#endif +#ifndef NO_NTOM +int synth_ntom_8bit (real*, int, mpg123_handle*, int); +int synth_ntom_8bit_mono (real*, mpg123_handle*); +int synth_ntom_8bit_mono2stereo(real*, mpg123_handle*); +void ntom_set_ntom(mpg123_handle *fr, off_t num); +#endif +#endif + +#ifndef REAL_IS_FIXED + +#ifndef NO_REAL +/* The real-producing variants. */ +int synth_1to1_real (real*, int, mpg123_handle*, int); +int synth_1to1_real_i386 (real*, int, mpg123_handle*, int); +int synth_1to1_real_sse (real*, int, mpg123_handle*, int); +int synth_1to1_real_stereo_sse (real*, real*, mpg123_handle*); +int synth_1to1_real_x86_64 (real*, int, mpg123_handle*, int); +int synth_1to1_real_stereo_x86_64(real*, real*, mpg123_handle*); +int synth_1to1_real_altivec (real*, int, mpg123_handle*, int); +int synth_1to1_real_stereo_altivec(real*, real*, mpg123_handle*); +int synth_1to1_real_mono (real*, mpg123_handle*); +int synth_1to1_real_mono2stereo(real*, mpg123_handle*); +#ifndef NO_DOWNSAMPLE +int synth_2to1_real (real*, int, mpg123_handle*, int); +int synth_2to1_real_i386 (real*, int, mpg123_handle*, int); +int synth_2to1_real_mono (real*, mpg123_handle*); +int synth_2to1_real_mono2stereo(real*, mpg123_handle*); +int synth_4to1_real (real*, int, mpg123_handle*, int); +int synth_4to1_real_i386 (real*, int, mpg123_handle*, int); +int synth_4to1_real_mono (real*, mpg123_handle*); +int synth_4to1_real_mono2stereo(real*, mpg123_handle*); +#endif +#ifndef NO_NTOM +int synth_ntom_real (real*, int, mpg123_handle*, int); +int synth_ntom_real_mono (real*, mpg123_handle*); +int synth_ntom_real_mono2stereo(real*, mpg123_handle*); +#endif +#endif + +#ifndef NO_32BIT +/* 32bit integer */ +int synth_1to1_s32 (real*, int, mpg123_handle*, int); +int synth_1to1_s32_i386 (real*, int, mpg123_handle*, int); +int synth_1to1_s32_sse (real*, int, mpg123_handle*, int); +int synth_1to1_s32_stereo_sse (real*, real*, mpg123_handle*); +int synth_1to1_s32_x86_64 (real*, int, mpg123_handle*, int); +int synth_1to1_s32_stereo_x86_64(real*, real*, mpg123_handle*); +int synth_1to1_s32_altivec (real*, int, mpg123_handle*, int); +int synth_1to1_s32_stereo_altivec(real*, real*, mpg123_handle*); +int synth_1to1_s32_mono (real*, mpg123_handle*); +int synth_1to1_s32_mono2stereo(real*, mpg123_handle*); +#ifndef NO_DOWNSAMPLE +int synth_2to1_s32 (real*, int, mpg123_handle*, int); +int synth_2to1_s32_i386 (real*, int, mpg123_handle*, int); +int synth_2to1_s32_mono (real*, mpg123_handle*); +int synth_2to1_s32_mono2stereo(real*, mpg123_handle*); +int synth_4to1_s32 (real*, int, mpg123_handle*, int); +int synth_4to1_s32_i386 (real*, int, mpg123_handle*, int); +int synth_4to1_s32_mono (real*, mpg123_handle*); +int synth_4to1_s32_mono2stereo(real*, mpg123_handle*); +#endif +#ifndef NO_NTOM +int synth_ntom_s32 (real*, int, mpg123_handle*, int); +int synth_ntom_s32_mono (real*, mpg123_handle*); +int synth_ntom_s32_mono2stereo(real*, mpg123_handle*); +#endif +#endif + +#endif /* FIXED */ + + +/* Inside these synth functions, some dct64 variants may be used. + The special optimized ones that only appear in assembler code are not mentioned here. + And, generally, these functions are only employed in a matching synth function. */ +void dct64 (real *,real *,real *); +void dct64_i386 (real *,real *,real *); +void dct64_altivec(real *,real *,real *); +void dct64_i486(int*, int* , real*); /* Yeah, of no use outside of synth_i486.c .*/ + +/* This is used by the layer 3 decoder, one generic function and 3DNow variants. */ +void dct36 (real *,real *,real *,real *,real *); +void dct36_3dnow (real *,real *,real *,real *,real *); +void dct36_3dnowext(real *,real *,real *,real *,real *); + +/* Tools for NtoM resampling synth, defined in ntom.c . */ +int synth_ntom_set_step(mpg123_handle *fr); /* prepare ntom decoding */ +unsigned long ntom_val(mpg123_handle *fr, off_t frame); /* compute ntom_val for frame offset */ +/* Frame and sample offsets. */ +#ifndef NO_NTOM +off_t ntom_frmouts(mpg123_handle *fr, off_t frame); +off_t ntom_ins2outs(mpg123_handle *fr, off_t ins); +off_t ntom_frameoff(mpg123_handle *fr, off_t soff); +#endif + +/* Initialization of any static data that majy be needed at runtime. + Make sure you call these once before it is too late. */ +#ifndef NO_LAYER3 +void init_layer3(void); +real init_layer3_gainpow2(mpg123_handle *fr, int i); +void init_layer3_stuff(mpg123_handle *fr, real (*gainpow2)(mpg123_handle *fr, int i)); +#endif +#ifndef NO_LAYER12 +void init_layer12(void); +real* init_layer12_table(mpg123_handle *fr, real *table, double m); +void init_layer12_stuff(mpg123_handle *fr, real* (*init_table)(mpg123_handle *fr, real *table, double m)); +#endif + +void prepare_decode_tables(void); + +extern real *pnts[5]; /* tabinit provides, dct64 needs */ + +/* Runtime (re)init functions; needed more often. */ +void make_decode_tables(mpg123_handle *fr); /* For every volume change. */ +/* Stuff needed after updating synth setup (see set_synth_functions()). */ + +#ifdef OPT_MMXORSSE +/* Special treatment for mmx-like decoders, these functions go into the slots below. */ +void make_decode_tables_mmx(mpg123_handle *fr); +#ifndef NO_LAYER3 +real init_layer3_gainpow2_mmx(mpg123_handle *fr, int i); +#endif +#ifndef NO_LAYER12 +real* init_layer12_table_mmx(mpg123_handle *fr, real *table, double m); +#endif +#endif + +#ifndef NO_8BIT +/* Needed when switching to 8bit output. */ +int make_conv16to8_table(mpg123_handle *fr); +#endif + +/* These are the actual workers. + They operate on the parsed frame data and handle decompression to audio samples. + The synth functions defined above are called from inside the layer handlers. */ + +#ifndef NO_LAYER3 +int do_layer3(mpg123_handle *fr); +#endif +#ifndef NO_LAYER2 +int do_layer2(mpg123_handle *fr); +#endif +#ifndef NO_LAYER1 +int do_layer1(mpg123_handle *fr); +#endif +/* There's an 3DNow counterpart in asm. */ +void do_equalizer(real *bandPtr,int channel, real equalizer[2][32]); + +#endif diff --git a/dlls/winemp3.acm/decode_i386.c b/dlls/winemp3.acm/decode_i386.c deleted file mode 100644 index 0029a92..0000000 --- a/dlls/winemp3.acm/decode_i386.c +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Mpeg Layer-1,2,3 audio decoder - * ------------------------------ - * copyright (c) 1995,1996,1997 by Michael Hipp, All rights reserved. - * See also 'README' - * - * Slightly optimized for machines without autoincrement/decrement. - * The performance is highly compiler dependent. Maybe - * the decode.c version for 'normal' processor may be faster - * even for Intel processors. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#include -#include -#include - -#include "mpg123.h" -#include "mpglib.h" - - /* old WRITE_SAMPLE */ -#define WRITE_SAMPLE(samples,sum,clip) \ - if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \ - else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \ - else { *(samples) = sum; } - -int synth_1to1_mono(struct mpstr *mp,real *bandPtr,unsigned char *samples,int *pnt) -{ - short samples_tmp[64]; - short *tmp1 = samples_tmp; - int i,ret; - int pnt1 = 0; - - ret = synth_1to1(mp,bandPtr,0,(unsigned char *) samples_tmp,&pnt1); - samples += *pnt; - - for(i=0;i<32;i++) { - *( (short *) samples) = *tmp1; - samples += 2; - tmp1 += 2; - } - *pnt += 64; - - return ret; -} - - -int synth_1to1(struct mpstr *mp,real *bandPtr,int channel,unsigned char *out,int *pnt) -{ - static const int step = 2; - int bo; - short *samples = (short *) (out + *pnt); - - real *b0,(*buf)[0x110]; - int clip = 0; - int bo1; - - bo = mp->synth_bo; - - if(!channel) { - bo--; - bo &= 0xf; - buf = mp->synth_buffs[0]; - } - else { - samples++; - buf = mp->synth_buffs[1]; - } - - if(bo & 0x1) { - b0 = buf[0]; - bo1 = bo; - dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr); - } - else { - b0 = buf[1]; - bo1 = bo+1; - dct64(buf[0]+bo,buf[1]+bo+1,bandPtr); - } - - mp->synth_bo = bo; - - { - register int j; - real *window = decwin + 16 - bo1; - - for (j=16;j;j--,b0+=0x10,window+=0x20,samples+=step) - { - real sum; - sum = window[0x0] * b0[0x0]; - sum -= window[0x1] * b0[0x1]; - sum += window[0x2] * b0[0x2]; - sum -= window[0x3] * b0[0x3]; - sum += window[0x4] * b0[0x4]; - sum -= window[0x5] * b0[0x5]; - sum += window[0x6] * b0[0x6]; - sum -= window[0x7] * b0[0x7]; - sum += window[0x8] * b0[0x8]; - sum -= window[0x9] * b0[0x9]; - sum += window[0xA] * b0[0xA]; - sum -= window[0xB] * b0[0xB]; - sum += window[0xC] * b0[0xC]; - sum -= window[0xD] * b0[0xD]; - sum += window[0xE] * b0[0xE]; - sum -= window[0xF] * b0[0xF]; - - WRITE_SAMPLE(samples,sum,clip); - } - - { - real sum; - sum = window[0x0] * b0[0x0]; - sum += window[0x2] * b0[0x2]; - sum += window[0x4] * b0[0x4]; - sum += window[0x6] * b0[0x6]; - sum += window[0x8] * b0[0x8]; - sum += window[0xA] * b0[0xA]; - sum += window[0xC] * b0[0xC]; - sum += window[0xE] * b0[0xE]; - WRITE_SAMPLE(samples,sum,clip); - b0-=0x10,window-=0x20,samples+=step; - } - window += bo1<<1; - - for (j=15;j;j--,b0-=0x10,window-=0x20,samples+=step) - { - real sum; - sum = -window[-0x1] * b0[0x0]; - sum -= window[-0x2] * b0[0x1]; - sum -= window[-0x3] * b0[0x2]; - sum -= window[-0x4] * b0[0x3]; - sum -= window[-0x5] * b0[0x4]; - sum -= window[-0x6] * b0[0x5]; - sum -= window[-0x7] * b0[0x6]; - sum -= window[-0x8] * b0[0x7]; - sum -= window[-0x9] * b0[0x8]; - sum -= window[-0xA] * b0[0x9]; - sum -= window[-0xB] * b0[0xA]; - sum -= window[-0xC] * b0[0xB]; - sum -= window[-0xD] * b0[0xC]; - sum -= window[-0xE] * b0[0xD]; - sum -= window[-0xF] * b0[0xE]; - sum -= window[-0x0] * b0[0xF]; - - WRITE_SAMPLE(samples,sum,clip); - } - } - *pnt += 128; - - return clip; -} diff --git a/dlls/winemp3.acm/equalizer.c b/dlls/winemp3.acm/equalizer.c new file mode 100644 index 0000000..0b32c0b --- /dev/null +++ b/dlls/winemp3.acm/equalizer.c @@ -0,0 +1,31 @@ +/* + equalizer.c: equalizer settings + + copyright ?-2006 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Michael Hipp + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ + + +#include "mpg123lib_intern.h" + +void do_equalizer(real *bandPtr,int channel, real equalizer[2][32]) +{ + int i; + for(i=0;i<32;i++) + bandPtr[i] = REAL_MUL(bandPtr[i], equalizer[channel][i]); +} diff --git a/dlls/winemp3.acm/format.c b/dlls/winemp3.acm/format.c new file mode 100644 index 0000000..ee8a9bf --- /dev/null +++ b/dlls/winemp3.acm/format.c @@ -0,0 +1,411 @@ +/* + format:routines to deal with audio (output) format + + copyright 2008-9 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Thomas Orgis, starting with parts of the old audio.c, with only faintly manage to show now + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ + +#include "mpg123lib_intern.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(mpeg3); + +/* static int chans[NUM_CHANNELS] = { 1 , 2 }; */ +static const long my_rates[MPG123_RATES] = /* only the standard rates */ +{ + 8000, 11025, 12000, + 16000, 22050, 24000, + 32000, 44100, 48000, +}; + +static const int my_encodings[MPG123_ENCODINGS] = +{ + MPG123_ENC_SIGNED_16, + MPG123_ENC_UNSIGNED_16, + MPG123_ENC_SIGNED_32, + MPG123_ENC_UNSIGNED_32, + MPG123_ENC_FLOAT_32, + MPG123_ENC_FLOAT_64, + MPG123_ENC_SIGNED_8, + MPG123_ENC_UNSIGNED_8, + MPG123_ENC_ULAW_8, + MPG123_ENC_ALAW_8 +}; + +/* Only one type of float is supported. */ +# ifdef REAL_IS_FLOAT +# define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_32 +# else +# define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_64 +# endif + +/* The list of actually possible encodings. */ +static const int good_encodings[] = +{ +#ifndef NO_16BIT + MPG123_ENC_SIGNED_16, + MPG123_ENC_UNSIGNED_16, +#endif +#ifndef NO_32BIT + MPG123_ENC_SIGNED_32, + MPG123_ENC_UNSIGNED_32, +#endif +#ifndef NO_REAL + MPG123_FLOAT_ENC, +#endif +#ifndef NO_8BIT + MPG123_ENC_SIGNED_8, + MPG123_ENC_UNSIGNED_8, + MPG123_ENC_ULAW_8, + MPG123_ENC_ALAW_8 +#endif +}; + +/* Check if encoding is a valid one in this build. + ...lazy programming: linear search. */ +static int good_enc(const int enc) +{ + size_t i; + for(i=0; iforce_rate != 0 && mp->force_rate == r) return MPG123_RATES; +#endif + + return -1; +} + +static int enc2num(int encoding) +{ + int i; + for(i=0;ichannels-1; + int rn = rate2num(&fr->p, nf->rate); + if(rn >= 0) for(i=f0;ip.audio_caps[c][rn][i]) + { + nf->encoding = my_encodings[i]; + return 1; + } + } + return 0; +} + +static int freq_fit(mpg123_handle *fr, struct audioformat *nf, int f0, int f2) +{ + nf->rate = frame_freq(fr)>>fr->p.down_sample; + if(cap_fit(fr,nf,f0,f2)) return 1; + nf->rate>>=1; + if(cap_fit(fr,nf,f0,f2)) return 1; + nf->rate>>=1; + if(cap_fit(fr,nf,f0,f2)) return 1; +#ifndef NO_NTOM + /* If nothing worked, try the other rates, only without constrains from user. + In case you didn't guess: We enable flexible resampling if we find a working rate. */ + if(!fr->p.force_rate && fr->p.down_sample == 0) + { + int i; + int c = nf->channels-1; + int rn = rate2num(&fr->p, frame_freq(fr)); + int rrn; + if(rn < 0) return 0; + /* Try higher rates first. */ + for(i=f0;ip.audio_caps[c][rrn][i]) + { + nf->rate = my_rates[rrn]; + nf->encoding = my_encodings[i]; + return 1; + } + /* Then lower rates. */ + for(i=f0;i=0; --rrn) + if(fr->p.audio_caps[c][rrn][i]) + { + nf->rate = my_rates[rrn]; + nf->encoding = my_encodings[i]; + return 1; + } + } +#endif + + return 0; +} + +/* match constraints against supported audio formats, store possible setup in frame + return: -1: error; 0: no format change; 1: format change */ +int frame_output_format(mpg123_handle *fr) +{ + struct audioformat nf; + int f0=0; + int f2=MPG123_ENCODINGS; /* Omit the 32bit and float encodings. */ + mpg123_pars *p = &fr->p; + /* initialize new format, encoding comes later */ + nf.channels = fr->stereo; + + /* All this forcing should be removed in favour of the capabilities table... */ + if(p->flags & MPG123_FORCE_8BIT) + { + f0 = 6; + f2 = 10; + } + if(p->flags & MPG123_FORCE_FLOAT) + { + f0 = 4; + f2 = 6; + } + + /* force stereo is stronger */ + if(p->flags & MPG123_FORCE_MONO) nf.channels = 1; + if(p->flags & MPG123_FORCE_STEREO) nf.channels = 2; + +#ifndef NO_NTOM + if(p->force_rate) + { + nf.rate = p->force_rate; + if(cap_fit(fr,&nf,f0,2)) goto end; /* 16bit encodings */ + if(cap_fit(fr,&nf,f0<=2 ? 2 : f0,f2)) goto end; /* 8bit encodings */ + + /* try again with different stereoness */ + if(nf.channels == 2 && !(p->flags & MPG123_FORCE_STEREO)) nf.channels = 1; + else if(nf.channels == 1 && !(p->flags & MPG123_FORCE_MONO)) nf.channels = 2; + + if(cap_fit(fr,&nf,f0,2)) goto end; /* 16bit encodings */ + if(cap_fit(fr,&nf,f0<=2 ? 2 : f0,f2)) goto end; /* 8bit encodings */ + + if(NOQUIET) + ERR( "Unable to set up output format! Constraints: %s%s%liHz.\n", + ( p->flags & MPG123_FORCE_STEREO ? "stereo, " : + (p->flags & MPG123_FORCE_MONO ? "mono, " : "") ), + (p->flags & MPG123_FORCE_8BIT ? "8bit, " : ""), + p->force_rate ); +/* if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */ + + fr->err = MPG123_BAD_OUTFORMAT; + return -1; + } +#endif + + if(freq_fit(fr, &nf, f0, 2)) goto end; /* try rates with 16bit */ + if(freq_fit(fr, &nf, f0<=2 ? 2 : f0, f2)) goto end; /* ... 8bit */ + + /* try again with different stereoness */ + if(nf.channels == 2 && !(p->flags & MPG123_FORCE_STEREO)) nf.channels = 1; + else if(nf.channels == 1 && !(p->flags & MPG123_FORCE_MONO)) nf.channels = 2; + + if(freq_fit(fr, &nf, f0, 2)) goto end; /* try rates with 16bit */ + if(freq_fit(fr, &nf, f0<=2 ? 2 : f0, f2)) goto end; /* ... 8bit */ + + /* Here is the _bad_ end. */ + if(NOQUIET) + { + ERR( "Unable to set up output format! Constraints: %s%s%li, %li or %liHz.\n", + ( p->flags & MPG123_FORCE_STEREO ? "stereo, " : + (p->flags & MPG123_FORCE_MONO ? "mono, " : "") ), + (p->flags & MPG123_FORCE_8BIT ? "8bit, " : ""), + frame_freq(fr), frame_freq(fr)>>1, frame_freq(fr)>>2 ); + } +/* if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */ + + fr->err = MPG123_BAD_OUTFORMAT; + return -1; + +end: /* Here is the _good_ end. */ + /* we had a successful match, now see if there's a change */ + if(nf.rate == fr->af.rate && nf.channels == fr->af.channels && nf.encoding == fr->af.encoding) + { + TRACE("Old format with %i channels, and FORCE_MONO=%li\n", nf.channels, p->flags & MPG123_FORCE_MONO); + return 0; /* the same format as before */ + } + else /* a new format */ + { + TRACE("New format with %i channels!\n", nf.channels); + fr->af.rate = nf.rate; + fr->af.channels = nf.channels; + fr->af.encoding = nf.encoding; + /* Cache the size of one sample in bytes, for ease of use. */ + if(fr->af.encoding & MPG123_ENC_8) + fr->af.encsize = 1; + else if(fr->af.encoding & MPG123_ENC_16) + fr->af.encsize = 2; + else if(fr->af.encoding & MPG123_ENC_32 || fr->af.encoding == MPG123_ENC_FLOAT_32) + fr->af.encsize = 4; + else if(fr->af.encoding == MPG123_ENC_FLOAT_64) + fr->af.encsize = 8; + else + { + if(NOQUIET) ERR("Some unknown encoding??? (%i)\n", fr->af.encoding); + + fr->err = MPG123_BAD_OUTFORMAT; + return -1; + } + return 1; + } +} + +int attribute_align_arg mpg123_format_none(mpg123_handle *mh) +{ + int r; + if(mh == NULL) return MPG123_ERR; + + r = mpg123_fmt_none(&mh->p); + if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; } + + return r; +} + +int attribute_align_arg mpg123_fmt_none(mpg123_pars *mp) +{ + if(mp == NULL) return MPG123_BAD_PARS; + + if(PVERB(mp,3)) TRACE("Note: Disabling all formats.\n"); + + memset(mp->audio_caps,0,sizeof(mp->audio_caps)); + return MPG123_OK; +} + +int attribute_align_arg mpg123_format_all(mpg123_handle *mh) +{ + int r; + if(mh == NULL) return MPG123_ERR; + + r = mpg123_fmt_all(&mh->p); + if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; } + + return r; +} + +int attribute_align_arg mpg123_fmt_all(mpg123_pars *mp) +{ + size_t rate, ch, enc; + if(mp == NULL) return MPG123_BAD_PARS; + + if(PVERB(mp,3)) TRACE("Note: Enabling all formats.\n"); + + for(ch=0; ch < NUM_CHANNELS; ++ch) + for(rate=0; rate < MPG123_RATES+1; ++rate) + for(enc=0; enc < MPG123_ENCODINGS; ++enc) + mp->audio_caps[ch][rate][enc] = good_enc(my_encodings[enc]) ? 1 : 0; + + return MPG123_OK; +} + +int attribute_align_arg mpg123_format(mpg123_handle *mh, long rate, int channels, int encodings) +{ + int r; + if(mh == NULL) return MPG123_ERR; + r = mpg123_fmt(&mh->p, rate, channels, encodings); + if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; } + + return r; +} + +int attribute_align_arg mpg123_fmt(mpg123_pars *mp, long rate, int channels, int encodings) +{ + int ie, ic, ratei; + int ch[2] = {0, 1}; + if(mp == NULL) return MPG123_BAD_PARS; + if(!(channels & (MPG123_MONO|MPG123_STEREO))) return MPG123_BAD_CHANNEL; + + if(PVERB(mp,3)) TRACE("Note: Want to enable format %li/%i for encodings 0x%x.\n", rate, channels, encodings); + + if(!(channels & MPG123_STEREO)) ch[1] = 0; /* {0,0} */ + else if(!(channels & MPG123_MONO)) ch[0] = 1; /* {1,1} */ + ratei = rate2num(mp, rate); + if(ratei < 0) return MPG123_BAD_RATE; + + /* now match the encodings */ + for(ic = 0; ic < 2; ++ic) + { + for(ie = 0; ie < MPG123_ENCODINGS; ++ie) + if(good_enc(my_encodings[ie]) && ((my_encodings[ie] & encodings) == my_encodings[ie])) + mp->audio_caps[ch[ic]][ratei][ie] = 1; + + if(ch[0] == ch[1]) break; /* no need to do it again */ + } + + return MPG123_OK; +} + +int attribute_align_arg mpg123_format_support(mpg123_handle *mh, long rate, int encoding) +{ + if(mh == NULL) return 0; + else return mpg123_fmt_support(&mh->p, rate, encoding); +} + +int attribute_align_arg mpg123_fmt_support(mpg123_pars *mp, long rate, int encoding) +{ + int ch = 0; + int ratei, enci; + ratei = rate2num(mp, rate); + enci = enc2num(encoding); + if(mp == NULL || ratei < 0 || enci < 0) return 0; + if(mp->audio_caps[0][ratei][enci]) ch |= MPG123_MONO; + if(mp->audio_caps[1][ratei][enci]) ch |= MPG123_STEREO; + return ch; +} + +/* Call this one to ensure that any valid format will be something different than this. */ +void invalidate_format(struct audioformat *af) +{ + af->encoding = 0; + af->rate = 0; + af->channels = 0; +} + +/* take into account: channels, bytes per sample -- NOT resampling!*/ +off_t samples_to_bytes(mpg123_handle *fr , off_t s) +{ + return s * fr->af.encsize * fr->af.channels; +} + +off_t bytes_to_samples(mpg123_handle *fr , off_t b) +{ + return b / fr->af.encsize / fr->af.channels; +} diff --git a/dlls/winemp3.acm/frame.c b/dlls/winemp3.acm/frame.c new file mode 100644 index 0000000..0c02930 --- /dev/null +++ b/dlls/winemp3.acm/frame.c @@ -0,0 +1,873 @@ +/* + frame: Heap of routines dealing with the core mpg123 data structure. + + copyright 2008-9 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Thomas Orgis + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ +#include +#include +#include "windef.h" +#include "winbase.h" + +#include "mpg123lib_intern.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(mpeg3); + +static void frame_fixed_reset(mpg123_handle *fr); + +/* that's doubled in decode_ntom.c */ +#define NTOM_MUL (32768) +#define aligned_pointer(p,type,alignment) \ + (((char*)(p)-(char*)NULL) % (alignment)) \ + ? (type*)((char*)(p) + (alignment) - (((char*)(p)-(char*)NULL) % (alignment))) \ + : (type*)(p) +void frame_default_pars(mpg123_pars *mp) +{ + mp->outscale = 1.0; +#ifdef GAPLESS + mp->flags = MPG123_GAPLESS; +#else + mp->flags = 0; +#endif +#ifndef NO_NTOM + mp->force_rate = 0; +#endif + mp->down_sample = 0; + mp->rva = 0; + mp->halfspeed = 0; + mp->doublespeed = 0; + mp->verbose = 0; +#ifndef NO_ICY + mp->icy_interval = 0; +#endif +#ifndef WIN32 + mp->timeout = 0; +#endif + mp->resync_limit = 1024; +#ifdef FRAME_INDEX + mp->index_size = INDEX_SIZE; +#endif + mp->preframes = 4; /* That's good for layer 3 ISO compliance bitstream. */ + mpg123_fmt_all(mp); +} + +void frame_init(mpg123_handle *fr) +{ + frame_init_par(fr, NULL); +} + +void frame_init_par(mpg123_handle *fr, mpg123_pars *mp) +{ + fr->own_buffer = FALSE; + fr->buffer.data = NULL; + fr->rawbuffs = NULL; + fr->rawbuffss = 0; + fr->rawdecwin = NULL; + fr->rawdecwins = 0; +#ifndef NO_8BIT + fr->conv16to8_buf = NULL; +#endif + fr->xing_toc = NULL; + fr->cpu_opts.type = defdec(); + fr->cpu_opts.class = decclass(fr->cpu_opts.type); +#ifndef NO_NTOM + /* these two look unnecessary, check guarantee for synth_ntom_set_step (in control_generic, even)! */ + fr->ntom_val[0] = NTOM_MUL>>1; + fr->ntom_val[1] = NTOM_MUL>>1; + fr->ntom_step = NTOM_MUL; +#endif + /* unnecessary: fr->buffer.size = fr->buffer.fill = 0; */ + mpg123_reset_eq(fr); +#ifndef NO_ICY + init_icy(&fr->icy); +#endif + init_id3(fr); + /* frame_outbuffer is missing... */ + /* frame_buffers is missing... that one needs cpu opt setting! */ + /* after these... frame_reset is needed before starting full decode */ + invalidate_format(&fr->af); + fr->rdat.r_read = NULL; + fr->rdat.r_lseek = NULL; + fr->decoder_change = 1; + fr->err = MPG123_OK; + if(mp == NULL) frame_default_pars(&fr->p); + else memcpy(&fr->p, mp, sizeof(struct mpg123_pars_struct)); + + fr->down_sample = 0; /* Initialize to silence harmless errors when debugging. */ + frame_fixed_reset(fr); /* Reset only the fixed data, dynamic buffers are not there yet! */ + fr->synth = NULL; + fr->synth_mono = NULL; + fr->make_decode_tables = NULL; +#ifdef FRAME_INDEX + fi_init(&fr->index); + frame_index_setup(fr); /* Apply the size setting. */ +#endif +#ifdef OPT_DITHER + /* The idea is to read that at runtime in the future... to avoid bloating the binary. */ + fr->dithernoise = dithernoise; +#endif +} + +mpg123_pars attribute_align_arg *mpg123_new_pars(int *error) +{ + mpg123_pars *mp = HeapAlloc(GetProcessHeap(),0,sizeof(struct mpg123_pars_struct)); + if(mp != NULL){ frame_default_pars(mp); if(error != NULL) *error = MPG123_OK; } + else if(error != NULL) *error = MPG123_OUT_OF_MEM; + return mp; +} + +void attribute_align_arg mpg123_delete_pars(mpg123_pars* mp) +{ + if(mp != NULL) HeapFree(GetProcessHeap(),0,mp); +} + +int attribute_align_arg mpg123_reset_eq(mpg123_handle *mh) +{ + int i; + mh->have_eq_settings = 0; + for(i=0; i < 32; ++i) mh->equalizer[0][i] = mh->equalizer[1][i] = DOUBLE_TO_REAL(1.0); + + return MPG123_OK; +} + +int frame_outbuffer(mpg123_handle *fr) +{ + size_t size = mpg123_safe_buffer()*AUDIOBUFSIZE; + if(!fr->own_buffer) fr->buffer.data = NULL; + if(fr->buffer.data != NULL && fr->buffer.size != size) + { + HeapFree(GetProcessHeap(),0,fr->buffer.data); + fr->buffer.data = NULL; + } + fr->buffer.size = size; + if(fr->buffer.data == NULL) fr->buffer.data = (unsigned char*) HeapAlloc(GetProcessHeap(),0,fr->buffer.size); + if(fr->buffer.data == NULL) + { + fr->err = MPG123_OUT_OF_MEM; + return -1; + } + fr->own_buffer = TRUE; + fr->buffer.fill = 0; + return 0; +} + +int attribute_align_arg mpg123_replace_buffer(mpg123_handle *mh, unsigned char *data, size_t size) +{ + if(data == NULL || size < mpg123_safe_buffer()) + { + mh->err = MPG123_BAD_BUFFER; + return MPG123_ERR; + } + if(mh->own_buffer && mh->buffer.data != NULL) HeapFree(GetProcessHeap(),0,mh->buffer.data); + mh->own_buffer = FALSE; + mh->buffer.data = data; + mh->buffer.size = size; + mh->buffer.fill = 0; + return MPG123_OK; +} + +#ifdef FRAME_INDEX +int frame_index_setup(mpg123_handle *fr) +{ + int ret = MPG123_ERR; + if(fr->p.index_size >= 0) + { /* Simple fixed index. */ + fr->index.grow_size = 0; + TRACE("resizing index to %li\n", fr->p.index_size); + ret = fi_resize(&fr->index, (size_t)fr->p.index_size); + TRACE("index resized... %lu at %p\n", (unsigned long)fr->index.size, (void*)fr->index.data); + } + else + { /* A growing index. We give it a start, though. */ + fr->index.grow_size = (size_t)(- fr->p.index_size); + if(fr->index.size < fr->index.grow_size) + ret = fi_resize(&fr->index, fr->index.grow_size); + else + ret = MPG123_OK; /* We have minimal size already... and since growing is OK... */ + } + TRACE("set up frame index of size %lu (ret=%i)\n", (unsigned long)fr->index.size, ret); + + return ret; +} +#endif + +static void frame_decode_buffers_reset(mpg123_handle *fr) +{ + memset(fr->rawbuffs, 0, fr->rawbuffss); +} + +int frame_buffers(mpg123_handle *fr) +{ + int buffssize = 0; + TRACE("frame %p buffer\n", (void*)fr); +/* + the used-to-be-static buffer of the synth functions, has some subtly different types/sizes + + 2to1, 4to1, ntom, generic, i386: real[2][2][0x110] + mmx, sse: short[2][2][0x110] + i586(_dither): 4352 bytes; int/long[2][2][0x110] + i486: int[2][2][17*FIR_BUFFER_SIZE] + altivec: static real __attribute__ ((aligned (16))) buffs[4][4][0x110] + + Huh, altivec looks like fun. Well, let it be large... then, the 16 byte alignment seems to be implicit on MacOSX malloc anyway. + Let's make a reasonable attempt to allocate enough memory... + Keep in mind: biggest ones are i486 and altivec (mutually exclusive!), then follows i586 and normal real. + mmx/sse use short but also real for resampling. + Thus, minimum is 2*2*0x110*sizeof(real). +*/ + if(fr->cpu_opts.type == altivec) buffssize = 4*4*0x110*sizeof(real); +#ifdef OPT_I486 + else if(fr->cpu_opts.type == ivier) buffssize = 2*2*17*FIR_BUFFER_SIZE*sizeof(int); +#endif + else if(fr->cpu_opts.type == ifuenf || fr->cpu_opts.type == ifuenf_dither || fr->cpu_opts.type == dreidnow) + buffssize = 2*2*0x110*4; /* don't rely on type real, we need 4352 bytes */ + + if(2*2*0x110*sizeof(real) > buffssize) + buffssize = 2*2*0x110*sizeof(real); + buffssize += 15; /* For 16-byte alignment (SSE likes that). */ + + if(fr->rawbuffs != NULL && fr->rawbuffss != buffssize) + { + HeapFree(GetProcessHeap(),0,fr->rawbuffs); + fr->rawbuffs = NULL; + } + + if(fr->rawbuffs == NULL) fr->rawbuffs = (unsigned char*) HeapAlloc(GetProcessHeap(),0,buffssize); + if(fr->rawbuffs == NULL) return -1; + fr->rawbuffss = buffssize; + fr->short_buffs[0][0] = aligned_pointer(fr->rawbuffs,short,16); + fr->short_buffs[0][1] = fr->short_buffs[0][0] + 0x110; + fr->short_buffs[1][0] = fr->short_buffs[0][1] + 0x110; + fr->short_buffs[1][1] = fr->short_buffs[1][0] + 0x110; + fr->real_buffs[0][0] = aligned_pointer(fr->rawbuffs,real,16); + fr->real_buffs[0][1] = fr->real_buffs[0][0] + 0x110; + fr->real_buffs[1][0] = fr->real_buffs[0][1] + 0x110; + fr->real_buffs[1][1] = fr->real_buffs[1][0] + 0x110; +#ifdef OPT_I486 + if(fr->cpu_opts.type == ivier) + { + fr->int_buffs[0][0] = (int*) fr->rawbuffs; + fr->int_buffs[0][1] = fr->int_buffs[0][0] + 17*FIR_BUFFER_SIZE; + fr->int_buffs[1][0] = fr->int_buffs[0][1] + 17*FIR_BUFFER_SIZE; + fr->int_buffs[1][1] = fr->int_buffs[1][0] + 17*FIR_BUFFER_SIZE; + } +#endif +#ifdef OPT_ALTIVEC + if(fr->cpu_opts.type == altivec) + { + int i,j; + fr->areal_buffs[0][0] = (real*) fr->rawbuffs; + for(i=0; i<4; ++i) for(j=0; j<4; ++j) + fr->areal_buffs[i][j] = fr->areal_buffs[0][0] + (i*4+j)*0x110; + } +#endif + /* now the different decwins... all of the same size, actually */ + /* The MMX ones want 32byte alignment, which I'll try to ensure manually */ + { + int decwin_size = (512+32)*sizeof(real); +#ifdef OPT_MMXORSSE +#ifdef OPT_MULTI + if(fr->cpu_opts.class == mmxsse) + { +#endif + /* decwin_mmx will share, decwins will be appended ... sizeof(float)==4 */ + if(decwin_size < (512+32)*4) decwin_size = (512+32)*4; + + /* the second window + alignment zone -- we align for 32 bytes for SSE as + requirement, 64 byte for matching cache line size (that matters!) */ + decwin_size += (512+32)*4 + 63; + /* (512+32)*4/32 == 2176/32 == 68, so one decwin block retains alignment for 32 or 64 bytes */ +#ifdef OPT_MULTI + } +#endif +#endif +#ifdef OPT_ALTIVEC + if(decwin_size < (512+32)*4) decwin_size = (512+32)*4; + decwin_size += 512*4; +#endif + /* Hm, that's basically realloc() ... */ + if(fr->rawdecwin != NULL && fr->rawdecwins != decwin_size) + { + HeapFree(GetProcessHeap(),0,fr->rawdecwin); + fr->rawdecwin = NULL; + } + + if(fr->rawdecwin == NULL) + fr->rawdecwin = (unsigned char*) HeapAlloc(GetProcessHeap(),0,decwin_size); + + if(fr->rawdecwin == NULL) return -1; + + fr->rawdecwins = decwin_size; + fr->decwin = (real*) fr->rawdecwin; +#ifdef OPT_MMXORSSE +#ifdef OPT_MULTI + if(fr->cpu_opts.class == mmxsse) + { +#endif + /* align decwin, assign that to decwin_mmx, append decwins */ + /* I need to add to decwin what is missing to the next full 64 byte -- also I want to make gcc -pedantic happy... */ + fr->decwin = aligned_pointer(fr->rawdecwin,real,64); + TRACE("aligned decwin: %p\n", (void*)fr->decwin); + fr->decwin_mmx = (float*)fr->decwin; + fr->decwins = fr->decwin_mmx+512+32; +#ifdef OPT_MULTI + } + else TRACE("no decwins/decwin_mmx for that class\n"); +#endif +#endif + } + /* Only reset the buffers we created just now. */ + frame_decode_buffers_reset(fr); + + TRACE("frame %p buffer done\n", (void*)fr); + return 0; +} + +int frame_buffers_reset(mpg123_handle *fr) +{ + fr->buffer.fill = 0; /* hm, reset buffer fill... did we do a flush? */ + fr->bsnum = 0; + /* Wondering: could it be actually _wanted_ to retain buffer contents over different files? (special gapless / cut stuff) */ + fr->bsbuf = fr->bsspace[1]; + fr->bsbufold = fr->bsbuf; + fr->bitreservoir = 0; /* Not entirely sure if this is the right place for that counter. */ + frame_decode_buffers_reset(fr); + memset(fr->bsspace, 0, 2*(MAXFRAMESIZE+512)); + memset(fr->ssave, 0, 34); + fr->hybrid_blc[0] = fr->hybrid_blc[1] = 0; + memset(fr->hybrid_block, 0, sizeof(real)*2*2*SBLIMIT*SSLIMIT); + return 0; +} + +void frame_icy_reset(mpg123_handle* fr) +{ +#ifndef NO_ICY + if(fr->icy.data != NULL) HeapFree(GetProcessHeap(),0,fr->icy.data); + fr->icy.data = NULL; + fr->icy.interval = 0; + fr->icy.next = 0; +#endif +} + +void frame_free_toc(mpg123_handle *fr) +{ + if(fr->xing_toc != NULL){ HeapFree(GetProcessHeap(),0,fr->xing_toc); fr->xing_toc = NULL; } +} + +/* Just copy the Xing TOC over... */ +int frame_fill_toc(mpg123_handle *fr, unsigned char* in) +{ + if(fr->xing_toc == NULL) fr->xing_toc = HeapAlloc(GetProcessHeap(),0,100); + if(fr->xing_toc != NULL) + { + memcpy(fr->xing_toc, in, 100); +#ifdef DEBUG + TRACE("Got a TOC! Showing the values...\n"); + { + int i; + for(i=0; i<100; ++i) + TRACE("entry %i = %i\n", i, fr->xing_toc[i]); + } +#endif + return TRUE; + } + return FALSE; +} + +/* Prepare the handle for a new track. + Reset variables, buffers... */ +int frame_reset(mpg123_handle* fr) +{ + frame_buffers_reset(fr); + frame_fixed_reset(fr); + frame_free_toc(fr); +#ifdef FRAME_INDEX + fi_reset(&fr->index); +#endif + + return 0; +} + +/* Reset everythign except dynamic memory. */ +static void frame_fixed_reset(mpg123_handle *fr) +{ +#ifndef NO_ICY + frame_icy_reset(fr); +#endif + open_bad(fr); + fr->to_decode = FALSE; + fr->to_ignore = FALSE; + fr->metaflags = 0; + fr->outblock = mpg123_safe_buffer(); + fr->num = -1; + fr->playnum = -1; + fr->accurate = TRUE; + fr->silent_resync = 0; + fr->audio_start = 0; + fr->clip = 0; + fr->oldhead = 0; + fr->firsthead = 0; + fr->vbr = MPG123_CBR; + fr->abr_rate = 0; + fr->track_frames = 0; + fr->track_samples = -1; + fr->framesize=0; + fr->mean_frames = 0; + fr->mean_framesize = 0; + fr->freesize = 0; + fr->lastscale = -1; + fr->rva.level[0] = -1; + fr->rva.level[1] = -1; + fr->rva.gain[0] = 0; + fr->rva.gain[1] = 0; + fr->rva.peak[0] = 0; + fr->rva.peak[1] = 0; + fr->fsizeold = 0; + fr->firstframe = 0; + fr->ignoreframe = fr->firstframe-fr->p.preframes; + fr->lastframe = -1; + fr->fresh = 1; + fr->new_format = 0; +#ifdef GAPLESS + frame_gapless_init(fr,0,0); + fr->lastoff = 0; + fr->firstoff = 0; +#endif +#ifdef OPT_I486 + fr->i486bo[0] = fr->i486bo[1] = FIR_SIZE-1; +#endif + fr->bo = 1; /* the usual bo */ +#ifdef OPT_DITHER + fr->ditherindex = 0; +#endif + reset_id3(fr); +#ifndef NO_ICY + reset_icy(&fr->icy); + /* ICY stuff should go into icy.c, eh? */ + fr->icy.interval = 0; + fr->icy.next = 0; +#endif + fr->halfphase = 0; /* here or indeed only on first-time init? */ + fr->error_protection = 0; +} + +void frame_free_buffers(mpg123_handle *fr) +{ + if(fr->rawbuffs != NULL) HeapFree(GetProcessHeap(),0,fr->rawbuffs); + fr->rawbuffs = NULL; + fr->rawbuffss = 0; + if(fr->rawdecwin != NULL) HeapFree(GetProcessHeap(),0,fr->rawdecwin); + fr->rawdecwin = NULL; + fr->rawdecwins = 0; +#ifndef NO_8BIT + if(fr->conv16to8_buf != NULL) HeapFree(GetProcessHeap(),0,fr->conv16to8_buf); + fr->conv16to8_buf = NULL; +#endif +} + +void frame_exit(mpg123_handle *fr) +{ + if(fr->own_buffer && fr->buffer.data != NULL) + { + TRACE("freeing buffer at %p\n", (void*)fr->buffer.data); + HeapFree(GetProcessHeap(),0,fr->buffer.data); + } + fr->buffer.data = NULL; + frame_free_buffers(fr); + frame_free_toc(fr); +#ifdef FRAME_INDEX + fi_exit(&fr->index); +#endif + exit_id3(fr); +#ifndef NO_ICY + clear_icy(&fr->icy); +#endif +} + +int attribute_align_arg mpg123_info(mpg123_handle *mh, struct mpg123_frameinfo *mi) +{ + if(mh == NULL) return MPG123_ERR; + if(mi == NULL) + { + mh->err = MPG123_ERR_NULL; + return MPG123_ERR; + } + mi->version = mh->mpeg25 ? MPG123_2_5 : (mh->lsf ? MPG123_2_0 : MPG123_1_0); + mi->layer = mh->lay; + mi->rate = frame_freq(mh); + switch(mh->mode) + { + case 0: mi->mode = MPG123_M_STEREO; break; + case 1: mi->mode = MPG123_M_JOINT; break; + case 2: mi->mode = MPG123_M_DUAL; break; + case 3: mi->mode = MPG123_M_MONO; break; + default: ERR("That mode cannot be!\n"); + } + mi->mode_ext = mh->mode_ext; + mi->framesize = mh->framesize+4; /* Include header. */ + mi->flags = 0; + if(mh->error_protection) mi->flags |= MPG123_CRC; + if(mh->copyright) mi->flags |= MPG123_COPYRIGHT; + if(mh->extension) mi->flags |= MPG123_PRIVATE; + if(mh->original) mi->flags |= MPG123_ORIGINAL; + mi->emphasis = mh->emphasis; + mi->bitrate = frame_bitrate(mh); + mi->abr_rate = mh->abr_rate; + mi->vbr = mh->vbr; + return MPG123_OK; +} + + +/* + Fuzzy frame offset searching (guessing). + When we don't have an accurate position, we may use an inaccurate one. + Possibilities: + - use approximate positions from Xing TOC (not yet parsed) + - guess wildly from mean framesize and offset of first frame / beginning of file. +*/ + +off_t frame_fuzzy_find(mpg123_handle *fr, off_t want_frame, off_t* get_frame) +{ + /* Default is to go to the beginning. */ + off_t ret = fr->audio_start; + *get_frame = 0; + + /* But we try to find something better. */ + /* Xing VBR TOC works with relative positions, both in terms of audio frames and stream bytes. + Thus, it only works when whe know the length of things. + Oh... I assume the offsets are relative to the _total_ file length. */ + if(fr->xing_toc != NULL && fr->track_frames > 0 && fr->rdat.filelen > 0) + { + /* One could round... */ + int toc_entry = (int) ((double)want_frame*100./fr->track_frames); + /* It is an index in the 100-entry table. */ + if(toc_entry < 0) toc_entry = 0; + if(toc_entry > 99) toc_entry = 99; + + /* Now estimate back what frame we get. */ + *get_frame = (off_t) ((double)toc_entry/100. * fr->track_frames); + fr->accurate = FALSE; + fr->silent_resync = 1; + /* Question: Is the TOC for whole file size (with/without ID3) or the "real" audio data only? + ID3v1 info could also matter. */ + ret = (off_t) ((double)fr->xing_toc[toc_entry]/256.* fr->rdat.filelen); + } + else if(fr->mean_framesize > 0) + { /* Just guess with mean framesize (may be exact with CBR files). */ + /* Query filelen here or not? */ + fr->accurate = FALSE; /* Fuzzy! */ + fr->silent_resync = 1; + *get_frame = want_frame; + ret = (off_t) (fr->audio_start+fr->mean_framesize*want_frame); + } + TRACE("fuzzy: want %li of %li, get %li at %li B of %li B\n", + (long)want_frame, (long)fr->track_frames, (long)*get_frame, (long)ret, (long)(fr->rdat.filelen-fr->audio_start)); + return ret; +} + +/* + find the best frame in index just before the wanted one, seek to there + then step to just before wanted one with read_frame + do not care tabout the stuff that was in buffer but not played back + everything that left the decoder is counted as played + + Decide if you want low latency reaction and accurate timing info or stable long-time playback with buffer! +*/ + +off_t frame_index_find(mpg123_handle *fr, off_t want_frame, off_t* get_frame) +{ + /* default is file start if no index position */ + off_t gopos = 0; + *get_frame = 0; +#ifdef FRAME_INDEX + /* Possibly use VBRI index, too? I'd need an example for this... */ + if(fr->index.fill) + { + /* find in index */ + size_t fi; + /* at index fi there is frame step*fi... */ + fi = want_frame/fr->index.step; + if(fi >= fr->index.fill) /* If we are beyond the end of frame index...*/ + { + /* When fuzzy seek is allowed, we have some limited tolerance for the frames we want to read rather then jump over. */ + if(fr->p.flags & MPG123_FUZZY && want_frame - (fr->index.fill-1)*fr->index.step > 10) + { + gopos = frame_fuzzy_find(fr, want_frame, get_frame); + if(gopos > fr->audio_start) return gopos; /* Only in that case, we have a useful guess. */ + /* Else... just continue, fuzzyness didn't help. */ + } + /* Use the last available position, slowly advancing from that one. */ + fi = fr->index.fill - 1; + } + /* We have index position, that yields frame and byte offsets. */ + *get_frame = fi*fr->index.step; + gopos = fr->index.data[fi]; + fr->accurate = TRUE; /* When using the frame index, we are accurate. */ + } + else + { +#endif + if(fr->p.flags & MPG123_FUZZY) + return frame_fuzzy_find(fr, want_frame, get_frame); + /* A bit hackish here... but we need to be fresh when looking for the first header again. */ + fr->firsthead = 0; + fr->oldhead = 0; +#ifdef FRAME_INDEX + } +#endif + TRACE("index: 0x%lx for frame %li\n", (unsigned long)gopos, (long) *get_frame); + return gopos; +} + +off_t frame_ins2outs(mpg123_handle *fr, off_t ins) +{ + off_t outs = 0; + switch(fr->down_sample) + { + case 0: +# ifndef NO_DOWNSAMPLE + case 1: + case 2: +# endif + outs = ins>>fr->down_sample; + break; +# ifndef NO_NTOM + case 3: outs = ntom_ins2outs(fr, ins); break; +# endif + default: ERR("Bad down_sample (%i) ... should not be possible!!\n", fr->down_sample); + } + return outs; +} + +off_t frame_outs(mpg123_handle *fr, off_t num) +{ + off_t outs = 0; + switch(fr->down_sample) + { + case 0: +# ifndef NO_DOWNSAMPLE + case 1: + case 2: +# endif + outs = (spf(fr)>>fr->down_sample)*num; + break; +#ifndef NO_NTOM + case 3: outs = ntom_frmouts(fr, num); break; +#endif + default: ERR("Bad down_sample (%i) ... should not be possible!!\n", fr->down_sample); + } + return outs; +} + +off_t frame_offset(mpg123_handle *fr, off_t outs) +{ + off_t num = 0; + switch(fr->down_sample) + { + case 0: +# ifndef NO_DOWNSAMPLE + case 1: + case 2: +# endif + num = outs/(spf(fr)>>fr->down_sample); + break; +#ifndef NO_NTOM + case 3: num = ntom_frameoff(fr, outs); break; +#endif + default: ERR("Bad down_sample ... should not be possible!!\n"); + } + return num; +} + +#ifdef GAPLESS +/* input in _input_ samples */ +void frame_gapless_init(mpg123_handle *fr, off_t b, off_t e) +{ + fr->begin_s = b; + fr->end_s = e; + /* These will get proper values later, from above plus resampling info. */ + fr->begin_os = 0; + fr->end_os = 0; + TRACE("frame_gapless_init: from %lu to %lu samples\n", (long unsigned)fr->begin_s, (long unsigned)fr->end_s); +} + +void frame_gapless_realinit(mpg123_handle *fr) +{ + fr->begin_os = frame_ins2outs(fr, fr->begin_s); + fr->end_os = frame_ins2outs(fr, fr->end_s); + TRACE("frame_gapless_realinit: from %lu to %lu samples\n", (long unsigned)fr->begin_os, (long unsigned)fr->end_os); +} +#endif + +/* Compute the needed frame to ignore from, for getting accurate/consistent output for intended firstframe. */ +static off_t ignoreframe(mpg123_handle *fr) +{ + off_t preshift = fr->p.preframes; + /* Layer 3 _really_ needs at least one frame before. */ + if(fr->lay==3 && preshift < 1) preshift = 1; + /* Layer 1 & 2 reall do not need more than 2. */ + if(fr->lay!=3 && preshift > 2) preshift = 2; + + return fr->firstframe - preshift; +} + +/* The frame seek... This is not simply the seek to fe*spf(fr) samples in output because we think of _input_ frames here. + Seek to frame offset 1 may be just seek to 200 samples offset in output since the beginning of first frame is delay/padding. + Hm, is that right? OK for the padding stuff, but actually, should the decoder delay be better totally hidden or not? + With gapless, even the whole frame position could be advanced further than requested (since Homey don't play dat). */ +void frame_set_frameseek(mpg123_handle *fr, off_t fe) +{ + fr->firstframe = fe; +#ifdef GAPLESS + if(fr->p.flags & MPG123_GAPLESS) + { + /* Take care of the beginning... */ + off_t beg_f = frame_offset(fr, fr->begin_os); + if(fe <= beg_f) + { + fr->firstframe = beg_f; + fr->firstoff = fr->begin_os - frame_outs(fr, beg_f); + } + else fr->firstoff = 0; + /* The end is set once for a track at least, on the frame_set_frameseek called in get_next_frame() */ + if(fr->end_os > 0) + { + fr->lastframe = frame_offset(fr,fr->end_os); + fr->lastoff = fr->end_os - frame_outs(fr, fr->lastframe); + } else fr->lastoff = 0; + } else { fr->firstoff = fr->lastoff = 0; fr->lastframe = -1; } +#endif + fr->ignoreframe = ignoreframe(fr); +#ifdef GAPLESS + TRACE("frame_set_frameseek: begin at %li frames and %li samples, end at %li and %li; ignore from %li\n", + (long) fr->firstframe, (long) fr->firstoff, + (long) fr->lastframe, (long) fr->lastoff, (long) fr->ignoreframe); +#else + TRACE("frame_set_frameseek: begin at %li frames, end at %li; ignore from %li\n", + (long) fr->firstframe, (long) fr->lastframe, (long) fr->ignoreframe); +#endif +} + +void frame_skip(mpg123_handle *fr) +{ +#ifndef NO_LAYER3 + if(fr->lay == 3) set_pointer(fr, 512); +#endif +} + +/* Sample accurate seek prepare for decoder. */ +/* This gets unadjusted output samples and takes resampling into account */ +void frame_set_seek(mpg123_handle *fr, off_t sp) +{ + fr->firstframe = frame_offset(fr, sp); +#ifndef NO_NTOM + if(fr->down_sample == 3) ntom_set_ntom(fr, fr->firstframe); +#endif + fr->ignoreframe = ignoreframe(fr); +#ifdef GAPLESS /* The sample offset is used for non-gapless mode, too! */ + fr->firstoff = sp - frame_outs(fr, fr->firstframe); + TRACE("frame_set_seek: begin at %li frames and %li samples, end at %li and %li; ignore from %li\n", + (long) fr->firstframe, (long) fr->firstoff, + (long) fr->lastframe, (long) fr->lastoff, (long) fr->ignoreframe); +#else + TRACE("frame_set_seek: begin at %li frames, end at %li; ignore from %li\n", + (long) fr->firstframe, (long) fr->lastframe, (long) fr->ignoreframe); +#endif +} + +int attribute_align_arg mpg123_volume_change(mpg123_handle *mh, double change) +{ + if(mh == NULL) return MPG123_ERR; + return mpg123_volume(mh, change + (double) mh->p.outscale); +} + +int attribute_align_arg mpg123_volume(mpg123_handle *mh, double vol) +{ + if(mh == NULL) return MPG123_ERR; + + if(vol >= 0) mh->p.outscale = vol; + else mh->p.outscale = 0.; + + do_rva(mh); + return MPG123_OK; +} + +static int get_rva(mpg123_handle *fr, double *peak, double *gain) +{ + double p = -1; + double g = 0; + int ret = 0; + if(fr->p.rva) + { + int rt = 0; + /* Should one assume a zero RVA as no RVA? */ + if(fr->p.rva == 2 && fr->rva.level[1] != -1) rt = 1; + if(fr->rva.level[rt] != -1) + { + p = fr->rva.peak[rt]; + g = fr->rva.gain[rt]; + ret = 1; /* Success. */ + } + } + if(peak != NULL) *peak = p; + if(gain != NULL) *gain = g; + return ret; +} + +/* adjust the volume, taking both fr->outscale and rva values into account */ +void do_rva(mpg123_handle *fr) +{ + double peak = 0; + double gain = 0; + double newscale; + double rvafact = 1; + if(get_rva(fr, &peak, &gain)) + { + if(NOQUIET && fr->p.verbose > 1) TRACE("Note: doing RVA with gain %f\n", gain); + rvafact = pow(10,gain/20); + } + + newscale = fr->p.outscale*rvafact; + + /* if peak is unknown (== 0) this check won't hurt */ + if((peak*newscale) > 1.0) + { + newscale = 1.0/peak; + WARN("limiting scale value to %f to prevent clipping with indicated peak factor of %f\n", newscale, peak); + } + /* first rva setting is forced with fr->lastscale < 0 */ + if(newscale != fr->lastscale || fr->decoder_change) + { + TRACE("changing scale value from %f to %f (peak estimated to %f)\n", fr->lastscale != -1 ? fr->lastscale : fr->p.outscale, newscale, (double) (newscale*peak)); + fr->lastscale = newscale; + /* It may be too early, actually. */ + if(fr->make_decode_tables != NULL) fr->make_decode_tables(fr); /* the actual work */ + } +} + + +int attribute_align_arg mpg123_getvolume(mpg123_handle *mh, double *base, double *really, double *rva_db) +{ + if(mh == NULL) return MPG123_ERR; + if(base) *base = mh->p.outscale; + if(really) *really = mh->lastscale; + get_rva(mh, NULL, rva_db); + return MPG123_OK; +} + diff --git a/dlls/winemp3.acm/frame.h b/dlls/winemp3.acm/frame.h new file mode 100644 index 0000000..e4d288c --- /dev/null +++ b/dlls/winemp3.acm/frame.h @@ -0,0 +1,364 @@ +/* + frame: Central data structures and opmitization hooks. + + copyright 2007 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Thomas Orgis + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ + +#ifndef MPG123_FRAME_H +#define MPG123_FRAME_H + +#include +#include "mpg123.h" +#include "optimize.h" +#include "id3.h" +#ifndef NO_ICY +/*#include "icy.h"*/ +#endif +#include "reader.h" +#ifdef FRAME_INDEX +/*#include "index.h"*/ +#endif +#include "synths.h" + +/* max = 1728 */ +#define MAXFRAMESIZE 3456 + +/* the output buffer, used to be pcm_sample, pcm_point and audiobufsize */ +struct outbuffer +{ + unsigned char *data; + unsigned char *p; /* read pointer */ + size_t fill; /* fill from read pointer */ + size_t size; /* that's actually more like a safe size, after we have more than that, flush it */ +}; + +struct audioformat +{ + int encoding; + int encsize; /* Size of one sample in bytes, plain int should be fine here... */ + int channels; + long rate; +}; + +void invalidate_format(struct audioformat *af); + +struct mpg123_pars_struct +{ + int verbose; /* verbose level */ + long flags; /* combination of above */ +#ifndef NO_NTOM + long force_rate; +#endif + int down_sample; + int rva; /* (which) rva to do: 0: nothing, 1: radio/mix/track 2: album/audiophile */ + long halfspeed; + long doublespeed; +#ifndef WIN32 + long timeout; +#endif +#define NUM_CHANNELS 2 + char audio_caps[NUM_CHANNELS][MPG123_RATES+1][MPG123_ENCODINGS]; +/* long start_frame; */ /* frame offset to begin with */ +/* long frame_number;*/ /* number of frames to decode */ +#ifndef NO_ICY + long icy_interval; +#endif + double outscale; + long resync_limit; + long index_size; /* Long, because: negative values have a meaning. */ + long preframes; +}; + + + +/* There is a lot to condense here... many ints can be merged as flags; though the main space is still consumed by buffers. */ +struct mpg123_handle_struct +{ + int fresh; /* to be moved into flags */ + int new_format; + real hybrid_block[2][2][SBLIMIT*SSLIMIT]; + int hybrid_blc[2]; + /* the scratch vars for the decoders, sometimes real, sometimes short... sometimes int/long */ + short *short_buffs[2][2]; + real *real_buffs[2][2]; + unsigned char *rawbuffs; + int rawbuffss; +#ifdef OPT_I486 + int i486bo[2]; +#endif + int bo; /* Just have it always here. */ +#ifdef OPT_DITHER + int ditherindex; + float *dithernoise; +#endif + unsigned char* rawdecwin; /* the block with all decwins */ + int rawdecwins; /* size of rawdecwin memory */ + real *decwin; /* _the_ decode table */ +#ifdef OPT_MMXORSSE + /* I am not really sure that I need both of them... used in assembler */ + float *decwin_mmx; + float *decwins; +#endif + int have_eq_settings; + real equalizer[2][32]; + + /* for halfspeed mode */ + unsigned char ssave[34]; + int halfphase; +#ifndef NO_8BIT + /* a raw buffer and a pointer into the middle for signed short conversion, only allocated on demand */ + unsigned char *conv16to8_buf; + unsigned char *conv16to8; +#endif + /* There's some possible memory saving for stuff that is not _really_ dynamic. */ + + /* layer3 */ + int longLimit[9][23]; + int shortLimit[9][14]; + real gainpow2[256+118+4]; /* not really dynamic, just different for mmx */ + + /* layer2 */ + real muls[27][64]; /* also used by layer 1 */ + +#ifndef NO_NTOM + /* decode_ntom */ + unsigned long ntom_val[2]; + unsigned long ntom_step; +#endif + /* special i486 fun */ +#ifdef OPT_I486 + int *int_buffs[2][2]; +#endif + /* special altivec... */ +#ifdef OPT_ALTIVEC + real *areal_buffs[4][4]; +#endif + struct synth_s synths; + struct + { +#ifdef OPT_MULTI + +#ifndef NO_LAYER3 +#if (defined OPT_3DNOW || defined OPT_3DNOWEXT) + void (*dct36)(real *,real *,real *,real *,real *); +#endif +#endif + +#endif + enum optdec type; + enum optcla class; + } cpu_opts; + + int verbose; /* 0: nothing, 1: just print chosen decoder, 2: be verbose */ + + const struct al_table *alloc; + /* The runtime-chosen decoding, based on input and output format. */ + func_synth synth; + func_synth_stereo synth_stereo; + func_synth_mono synth_mono; + /* Yes, this function is runtime-switched, too. */ + void (*make_decode_tables)(mpg123_handle *fr); /* That is the volume control. */ + + int stereo; /* I _think_ 1 for mono and 2 for stereo */ + int jsbound; +#define SINGLE_STEREO -1 +#define SINGLE_LEFT 0 +#define SINGLE_RIGHT 1 +#define SINGLE_MIX 3 + int single; + int II_sblimit; + int down_sample_sblimit; + int lsf; /* 0: MPEG 1.0; 1: MPEG 2.0/2.5 -- both used as bool and array index! */ + /* Many flags in disguise as integers... wasting bytes. */ + int mpeg25; + int down_sample; + int header_change; + int lay; + int (*do_layer)(mpg123_handle *); + int error_protection; + int bitrate_index; + int sampling_frequency; + int padding; + int extension; + int mode; + int mode_ext; + int copyright; + int original; + int emphasis; + int framesize; /* computed framesize */ + int freesize; /* free format frame size */ + enum mpg123_vbr vbr; /* 1 if variable bitrate was detected */ + off_t num; /* frame offset ... */ + off_t playnum; /* playback offset... includes repetitions, reset at seeks */ + off_t audio_start; /* The byte offset in the file where audio data begins. */ + char accurate; /* Flag to see if we trust the frame number. */ + char silent_resync; /* Do not complain for the next n resyncs. */ + unsigned char* xing_toc; /* The seek TOC from Xing header. */ + + /* bitstream info; bsi */ + int bitindex; + unsigned char *wordpointer; + /* temporary storage for getbits stuff */ + unsigned long ultmp; + unsigned char uctmp; + + /* rva data, used in common.c, set in id3.c */ + + double maxoutburst; /* The maximum amplitude in current sample represenation. */ + double lastscale; + struct + { + int level[2]; + float gain[2]; + float peak[2]; + } rva; + + /* input data */ + off_t track_frames; + off_t track_samples; + double mean_framesize; + off_t mean_frames; + int fsizeold; + int ssize; + unsigned int bitreservoir; + unsigned char bsspace[2][MAXFRAMESIZE+512]; /* MAXFRAMESIZE */ + unsigned char *bsbuf; + unsigned char *bsbufold; + int bsnum; + unsigned long oldhead; + unsigned long firsthead; + int abr_rate; +#ifdef FRAME_INDEX + struct frame_index index; +#endif + + /* output data */ + struct outbuffer buffer; + struct audioformat af; + int own_buffer; + size_t outblock; /* number of bytes that this frame produces (upper bound) */ + int to_decode; /* this frame holds data to be decoded */ + int to_ignore; /* the same, somehow */ + off_t firstframe; /* start decoding from here */ + off_t lastframe; /* last frame to decode (for gapless or num_frames limit) */ + off_t ignoreframe; /* frames to decode but discard before firstframe */ +#ifdef GAPLESS + off_t firstoff; /* number of samples to ignore from firstframe */ + off_t lastoff; /* number of samples to use from lastframe */ + off_t begin_s; /* overall begin offset in samples */ + off_t begin_os; + off_t end_s; /* overall end offset in samples */ + off_t end_os; +#endif + unsigned int crc; /* Well, I need a safe 16bit type, actually. But wider doesn't hurt. */ + struct reader *rd; /* pointer to the reading functions */ + struct reader_data rdat; /* reader data and state info */ + struct mpg123_pars_struct p; + int err; + int decoder_change; + int delayed_change; + long clip; + /* the meta crap */ + int metaflags; + unsigned char id3buf[128]; +#ifndef NO_ID3V2 + mpg123_id3v2 id3v2; +#endif +#ifndef NO_ICY + struct icy_meta icy; +#endif +}; + +/* generic init, does not include dynamic buffers */ +void frame_init(mpg123_handle *fr); +void frame_init_par(mpg123_handle *fr, mpg123_pars *mp); +/* output buffer and format */ +int frame_outbuffer(mpg123_handle *fr); +int frame_output_format(mpg123_handle *fr); + +int frame_buffers(mpg123_handle *fr); /* various decoder buffers, needed once */ +int frame_reset(mpg123_handle* fr); /* reset for next track */ +int frame_buffers_reset(mpg123_handle *fr); +void frame_exit(mpg123_handle *fr); /* end, free all buffers */ + +/* Index functions... */ +/* Well... print it... */ +int mpg123_print_index(mpg123_handle *fr, FILE* out); +/* Find a seek position in index. */ +off_t frame_index_find(mpg123_handle *fr, off_t want_frame, off_t* get_frame); +/* Apply index_size setting. */ +int frame_index_setup(mpg123_handle *fr); + +void do_volume(mpg123_handle *fr, double factor); +void do_rva(mpg123_handle *fr); + +/* samples per frame ... +Layer I +Layer II +Layer III +MPEG-1 +384 +1152 +1152 +MPEG-2 LSF +384 +1152 +576 +MPEG 2.5 +384 +1152 +576 +*/ +#define spf(fr) ((fr)->lay == 1 ? 384 : ((fr)->lay==2 ? 1152 : ((fr)->lsf || (fr)->mpeg25 ? 576 : 1152))) + +#ifdef GAPLESS +/* well, I take that one for granted... at least layer3 */ +#define GAPLESS_DELAY 529 +/* still fine-tuning the "real music" window... see read_frame */ +void frame_gapless_init(mpg123_handle *fr, off_t b, off_t e); +void frame_gapless_realinit(mpg123_handle *fr); +/*void frame_gapless_position(mpg123_handle* fr); +void frame_gapless_bytify(mpg123_handle *fr); +void frame_gapless_ignore(mpg123_handle *fr, off_t frames);*/ +/* void frame_gapless_buffercheck(mpg123_handle *fr); */ +#endif + +/* Skip this frame... do some fake action to get away without actually decoding it. */ +void frame_skip(mpg123_handle *fr); + +/* + Seeking core functions: + - convert input sample offset to output sample offset + - convert frame offset to output sample offset + - get leading frame offset for output sample offset + The offsets are "unadjusted"/internal; resampling is being taken care of. +*/ +off_t frame_ins2outs(mpg123_handle *fr, off_t ins); +off_t frame_outs(mpg123_handle *fr, off_t num); +off_t frame_offset(mpg123_handle *fr, off_t outs); +void frame_set_frameseek(mpg123_handle *fr, off_t fe); +void frame_set_seek(mpg123_handle *fr, off_t sp); +off_t frame_tell_seek(mpg123_handle *fr); +/* Take a copy of the Xing VBR TOC for fuzzy seeking. */ +int frame_fill_toc(mpg123_handle *fr, unsigned char* in); + + +/* adjust volume to current outscale and rva values if wanted */ +void do_rva(mpg123_handle *fr); +#endif diff --git a/dlls/winemp3.acm/getbits.h b/dlls/winemp3.acm/getbits.h new file mode 100644 index 0000000..124165e --- /dev/null +++ b/dlls/winemp3.acm/getbits.h @@ -0,0 +1,114 @@ +/* + getbits + + copyright ?-2009 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Michael Hipp + + All code is in the header to suggest/force inlining of these small often-used functions. + This indeed has some impact on performance. + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ + +#ifndef _MPG123_GETBITS_H_ +#define _MPG123_GETBITS_H_ + +#include "mpg123lib_intern.h" + +#define backbits(fr,nob) ((void)( \ + fr->bitindex -= nob, \ + fr->wordpointer += (fr->bitindex>>3), \ + fr->bitindex &= 0x7 )) + +#define getbitoffset(fr) ((-fr->bitindex)&0x7) +#define getbyte(fr) (*fr->wordpointer++) + +/* There is something wrong with that macro... the function below works also for the layer1 test case. */ +#define macro_getbits(fr, nob) ( \ + fr->ultmp = fr->wordpointer[0],\ + fr->ultmp <<= 8, \ + fr->ultmp |= fr->wordpointer[1], \ + fr->ultmp <<= 8, \ + fr->ultmp |= fr->wordpointer[2], \ + fr->ultmp <<= fr->bitindex, \ + fr->ultmp &= 0xffffff, \ + fr->bitindex += nob, \ + fr->ultmp >>= (24-nob), \ + fr->wordpointer += (fr->bitindex>>3), \ + fr->bitindex &= 7, \ + fr->ultmp) + +static unsigned int getbits(mpg123_handle *fr, int number_of_bits) +{ + unsigned long rval; + +#ifdef DEBUG_GETBITS +fprintf(stderr,"g%d",number_of_bits); +#endif + +/* This is actually slow: if(!number_of_bits) + return 0; */ + +#if 0 + check_buffer_range(number_of_bits+fr->bitindex); +#endif + + { + rval = fr->wordpointer[0]; + rval <<= 8; + rval |= fr->wordpointer[1]; + rval <<= 8; + rval |= fr->wordpointer[2]; + + rval <<= fr->bitindex; + rval &= 0xffffff; + + fr->bitindex += number_of_bits; + + rval >>= (24-number_of_bits); + + fr->wordpointer += (fr->bitindex>>3); + fr->bitindex &= 7; + } + +#ifdef DEBUG_GETBITS +fprintf(stderr,":%lx\n",rval); +#endif + + return rval; +} + + +#define skipbits(fr, nob) fr->ultmp = ( \ + fr->ultmp = fr->wordpointer[0], fr->ultmp <<= 8, fr->ultmp |= fr->wordpointer[1], \ + fr->ultmp <<= 8, fr->ultmp |= fr->wordpointer[2], fr->ultmp <<= fr->bitindex, \ + fr->ultmp &= 0xffffff, fr->bitindex += nob, \ + fr->ultmp >>= (24-nob), fr->wordpointer += (fr->bitindex>>3), \ + fr->bitindex &= 7 ) + +#define getbits_fast(fr, nob) ( \ + fr->ultmp = (unsigned char) (fr->wordpointer[0] << fr->bitindex), \ + fr->ultmp |= ((unsigned long) fr->wordpointer[1]<bitindex)>>8, \ + fr->ultmp <<= nob, fr->ultmp >>= 8, \ + fr->bitindex += nob, fr->wordpointer += (fr->bitindex>>3), \ + fr->bitindex &= 7, fr->ultmp ) + +#define get1bit(fr) ( \ + fr->uctmp = *fr->wordpointer << fr->bitindex, fr->bitindex++, \ + fr->wordpointer += (fr->bitindex>>3), fr->bitindex &= 7, fr->uctmp>>7 ) + + +#endif diff --git a/dlls/winemp3.acm/huffman.h b/dlls/winemp3.acm/huffman.h index 8adac04..97e6879 100644 --- a/dlls/winemp3.acm/huffman.h +++ b/dlls/winemp3.acm/huffman.h @@ -1,12 +1,10 @@ /* - * Copyright (c) Michael Hipp and other authors of the mpglib project. - * - * huffman tables ... recalculated to work with my optimized - * decoder scheme (MH) - * - * probably we could save a few bytes of memory, because the - * smaller tables are often part of a bigger table - * + huffman.h: huffman tables ... recalcualted to work with optimzed decoder scheme (MH) + + copyright ?-2006 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Michael Hipp + * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -20,51 +18,58 @@ * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ + + probably we could save a few bytes of memory, because the + smaller tables are often the part of a bigger table +*/ + + +#ifndef _MPG123_HUFFMAN_H_ +#define _MPG123_HUFFMAN_H_ struct newhuff { unsigned int linbits; - const short *table; + short *table; }; -static const short tab0[] = +static short tab0[] = { 0 }; -static const short tab1[] = +static short tab1[] = { -5, -3, -1, 17, 1, 16, 0 }; -static const short tab2[] = +static short tab2[] = { -15, -11, -9, -5, -3, -1, 34, 2, 18, -1, 33, 32, 17, -1, 1, 16, 0 }; -static const short tab3[] = +static short tab3[] = { -13, -11, -9, -5, -3, -1, 34, 2, 18, -1, 33, 32, 16, 17, -1, 1, 0 }; -static const short tab5[] = +static short tab5[] = { -29, -25, -23, -15, -7, -5, -3, -1, 51, 35, 50, 49, -3, -1, 19, 3, -1, 48, 34, -3, -1, 18, 33, -1, 2, 32, 17, -1, 1, 16, 0 }; -static const short tab6[] = +static short tab6[] = { -25, -19, -13, -9, -5, -3, -1, 51, 3, 35, -1, 50, 48, -1, 19, 49, -3, -1, 34, 2, 18, -3, -1, 33, 32, 1, -1, 17, -1, 16, 0 }; -static const short tab7[] = +static short tab7[] = { -69, -65, -57, -39, -29, -17, -11, -7, -3, -1, 85, 69, -1, 84, 83, -1, 53, 68, -3, -1, 37, 82, 21, -5, -1, 81, -1, 5, 52, -1, @@ -73,7 +78,7 @@ static const short tab7[] = -5, -1, 33, -1, 2, 32, 17, -1, 1, 16, 0 }; -static const short tab8[] = +static short tab8[] = { -65, -63, -59, -45, -31, -19, -13, -7, -5, -3, -1, 85, 84, 69, 83, -3, -1, 53, 68, 37, -3, -1, 82, 5, 21, -5, -1, 81, -1, 52, @@ -82,7 +87,7 @@ static const short tab8[] = 2, 32, -1, 18, 33, 17, -3, -1, 1, 16, 0 }; -static const short tab9[] = +static short tab9[] = { -63, -53, -41, -29, -19, -11, -5, -3, -1, 85, 69, 53, -1, 83, -1, 84, 5, -3, -1, 68, 37, -1, 82, 21, -3, -1, 81, 52, -1, 67, @@ -91,7 +96,7 @@ static const short tab9[] = 18, -1, 33, 32, -3, -1, 17, 1, -1, 16, 0 }; -static const short tab10[] = +static short tab10[] = { -125,-121,-111, -83, -55, -35, -21, -13, -7, -3, -1, 119, 103, -1, 118, 87, -3, -1, 117, 102, 71, -3, -1, 116, 86, -1, 101, 55, -9, -3, @@ -104,7 +109,7 @@ static const short tab10[] = 2, 32, 17, -1, 1, 16, 0 }; -static const short tab11[] = +static short tab11[] = { -121,-113, -89, -59, -43, -27, -17, -7, -3, -1, 119, 103, -1, 118, 117, -3, -1, 102, 71, -1, 116, -1, 87, 85, -5, -3, -1, 86, 101, 55, @@ -117,7 +122,7 @@ static const short tab11[] = 32, 17, -3, -1, 1, 16, 0 }; -static const short tab12[] = +static short tab12[] = { -115, -99, -73, -45, -27, -17, -9, -5, -3, -1, 119, 103, 118, -1, 87, 117, -3, -1, 102, 71, -1, 116, 101, -3, -1, 86, 55, -3, -1, 115, @@ -130,7 +135,7 @@ static const short tab12[] = 2, 32, 0, 17, -1, 1, 16 }; -static const short tab13[] = +static short tab13[] = { -509,-503,-475,-405,-333,-265,-205,-153,-115, -83, -53, -35, -21, -13, -9, -7, -5, -3, -1, 254, 252, 253, 237, 255, -1, 239, 223, -3, -1, 238, @@ -169,7 +174,7 @@ static const short tab13[] = 0 }; -static const short tab15[] = +static short tab15[] = { -495,-445,-355,-263,-183,-115, -77, -43, -27, -13, -7, -3, -1, 255, 239, -1, 254, 223, -1, 238, -1, 253, 207, -7, -3, -1, 252, 222, -1, 237, @@ -208,7 +213,7 @@ static const short tab15[] = 0 }; -static const short tab16[] = +static short tab16[] = { -509,-503,-461,-323,-103, -37, -27, -15, -7, -3, -1, 239, 254, -1, 223, 253, -3, -1, 207, 252, -1, 191, 251, -5, -1, 175, -1, 250, 159, -3, @@ -247,7 +252,7 @@ static const short tab16[] = 0 }; -static const short tab24[] = +static short tab24[] = { -451,-117, -43, -25, -15, -7, -3, -1, 239, 254, -1, 223, 253, -3, -1, 207, 252, -1, 191, 251, -5, -1, 250, -1, 175, 159, -1, 249, 248, -9, @@ -286,14 +291,14 @@ static const short tab24[] = 0 }; -static const short tab_c0[] = +static short tab_c0[] = { -29, -21, -13, -7, -3, -1, 11, 15, -1, 13, 14, -3, -1, 7, 5, 9, -3, -1, 6, 3, -1, 10, 12, -3, -1, 2, 1, -1, 4, 8, 0 }; -static const short tab_c1[] = +static short tab_c1[] = { -15, -7, -3, -1, 15, 14, -1, 13, 12, -3, -1, 11, 10, -1, 9, 8, -7, -3, -1, 7, 6, -1, 5, 4, -3, -1, 3, 2, -1, 1, @@ -302,7 +307,7 @@ static const short tab_c1[] = -static const struct newhuff ht[] = +static struct newhuff ht[] = { { /* 0 */ 0 , tab0 } , { /* 2 */ 0 , tab1 } , @@ -339,8 +344,11 @@ static const struct newhuff ht[] = { /* 16 */ 13, tab24 } }; -static const struct newhuff htc[] = +static struct newhuff htc[] = { { /* 1 , 1 , */ 0 , tab_c0 } , { /* 1 , 1 , */ 0 , tab_c1 } }; + + +#endif diff --git a/dlls/winemp3.acm/id3.c b/dlls/winemp3.acm/id3.c new file mode 100644 index 0000000..05022fe --- /dev/null +++ b/dlls/winemp3.acm/id3.c @@ -0,0 +1,921 @@ +/* + id3: ID3v2.3 and ID3v2.4 parsing (a relevant subset) + + copyright 2006-2008 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Thomas Orgis + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ +#include +#include "windef.h" +#include "winbase.h" + +#include "mpg123lib_intern.h" +#include "id3.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(mpeg3); + +#ifndef NO_ID3V2 /* Only the main parsing routine will always be there. */ + +/* We know the usual text frames plus some specifics. */ +#define KNOWN_FRAMES 4 +static const char frame_type[KNOWN_FRAMES][5] = { "COMM", "TXXX", "RVA2", "USLT" }; +enum frame_types { unknown = -2, text = -1, comment, extra, rva2, uslt }; + +/* UTF support definitions */ + +typedef void (*text_converter)(mpg123_string *sb, unsigned char* source, size_t len); + +static void convert_latin1 (mpg123_string *sb, unsigned char* source, size_t len); +static void convert_utf16 (mpg123_string *sb, unsigned char* source, size_t len, int str_be); +static void convert_utf16bom(mpg123_string *sb, unsigned char* source, size_t len); +static void convert_utf16be (mpg123_string *sb, unsigned char* source, size_t len); +static void convert_utf8 (mpg123_string *sb, unsigned char* source, size_t len); + +static const text_converter text_converters[4] = +{ + convert_latin1, + convert_utf16bom, + convert_utf16be, + convert_utf8 +}; + +const unsigned int encoding_widths[4] = { 1, 2, 2, 1 }; + +/* the code starts here... */ + +static void null_id3_links(mpg123_handle *fr) +{ + fr->id3v2.title = NULL; + fr->id3v2.artist = NULL; + fr->id3v2.album = NULL; + fr->id3v2.year = NULL; + fr->id3v2.genre = NULL; + fr->id3v2.comment = NULL; +} + +void init_id3(mpg123_handle *fr) +{ + fr->id3v2.version = 0; /* nothing there */ + null_id3_links(fr); + fr->id3v2.comments = 0; + fr->id3v2.comment_list = NULL; + fr->id3v2.texts = 0; + fr->id3v2.text = NULL; + fr->id3v2.extras = 0; + fr->id3v2.extra = NULL; +} + +/* Managing of the text, comment and extra lists. */ + +/* Initialize one element. */ +static void init_mpg123_text(mpg123_text *txt) +{ + mpg123_init_string(&txt->text); + mpg123_init_string(&txt->description); + txt->id[0] = 0; + txt->id[1] = 0; + txt->id[2] = 0; + txt->id[3] = 0; + txt->lang[0] = 0; + txt->lang[1] = 0; + txt->lang[2] = 0; +} + +/* Free memory of one element. */ +static void free_mpg123_text(mpg123_text *txt) +{ + mpg123_free_string(&txt->text); + mpg123_free_string(&txt->description); +} + +/* Free memory of whole list. */ +#define free_comment(mh) free_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments)) +#define free_text(mh) free_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts)) +#define free_extra(mh) free_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras)) +static void free_id3_text(mpg123_text **list, size_t *size) +{ + size_t i; + for(i=0; i<*size; ++i) free_mpg123_text(&((*list)[i])); + + free(*list); + *list = NULL; + *size = 0; +} + +/* Add items to the list. */ +#define add_comment(mh) add_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments)) +#define add_text(mh) add_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts)) +#define add_extra(mh) add_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras)) +static mpg123_text *add_id3_text(mpg123_text **list, size_t *size) +{ + mpg123_text *x = HeapReAlloc(GetProcessHeap(),0,*list, sizeof(mpg123_text)*(*size+1)); + if(x == NULL) return NULL; /* bad */ + + *list = x; + *size += 1; + init_mpg123_text(&((*list)[*size-1])); + + return &((*list)[*size-1]); /* Return pointer to the added text. */ +} + +/* Remove the last item. */ +#define pop_comment(mh) pop_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments)) +#define pop_text(mh) pop_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts)) +#define pop_extra(mh) pop_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras)) +static void pop_id3_text(mpg123_text **list, size_t *size) +{ + mpg123_text *x; + if(*size < 1) return; + + free_mpg123_text(&((*list)[*size-1])); + if(*size > 1) + { + x = HeapReAlloc(GetProcessHeap(),0,*list, sizeof(mpg123_text)*(*size-1)); + if(x != NULL){ *list = x; *size -= 1; } + } + else + { + HeapFree(GetProcessHeap(),0,*list); + *list = NULL; + *size = 0; + } +} + +/* OK, back t the higher level functions. */ + +void exit_id3(mpg123_handle *fr) +{ + free_comment(fr); + free_extra(fr); + free_text(fr); +} + +void reset_id3(mpg123_handle *fr) +{ + exit_id3(fr); + init_id3(fr); +} + +/* Set the id3v2.artist id3v2.title ... links to elements of the array. */ +void id3_link(mpg123_handle *fr) +{ + size_t i; + mpg123_id3v2 *v2 = &fr->id3v2; + TRACE("linking ID3v2\n"); + null_id3_links(fr); + for(i=0; itexts; ++i) + { + mpg123_text *entry = &v2->text[i]; + if (!strncmp("TIT2", entry->id, 4)) v2->title = &entry->text; + else if(!strncmp("TALB", entry->id, 4)) v2->album = &entry->text; + else if(!strncmp("TPE1", entry->id, 4)) v2->artist = &entry->text; + else if(!strncmp("TYER", entry->id, 4)) v2->year = &entry->text; + else if(!strncmp("TCON", entry->id, 4)) v2->genre = &entry->text; + } + for(i=0; icomments; ++i) + { + mpg123_text *entry = &v2->comment_list[i]; + if(entry->description.fill == 0 || entry->description.p[0] == 0) + v2->comment = &entry->text; + } + /* When no generic comment found, use the last non-generic one. */ + if(v2->comment == NULL && v2->comments > 0) + v2->comment = &v2->comment_list[v2->comments-1].text; +} + +/* + Store any text in UTF8 encoding; preserve the zero string separator (I don't need strlen for the total size). + ID3v2 standard says that there should be one text frame of specific type per tag, and subsequent tags overwrite old values. + So, I always replace the text that may be stored already (perhaps with a list of zero-separated strings, though). +*/ +void store_id3_text(mpg123_string *sb, char *source, size_t source_size, const int noquiet) +{ + unsigned int encoding; + unsigned int bwidth; + if(!source_size) + { + TRACE("Empty id3 data!\n"); + return; + } + encoding = (unsigned int) source[0]; + ++source; + --source_size; + TRACE("encoding: %u\n", encoding); + /* A note: ID3v2.3 uses UCS-2 non-variable 16bit encoding, v2.4 uses UTF16. + UTF-16 uses a reserved/private range in UCS-2 to add the magic, so we just always treat it as UTF. */ + if(encoding > 3) + { + if(noquiet) WARN("Unknown text encoding %u, assuming ISO8859-1 - I will probably screw a bit up!\n", encoding); + encoding = 0; + } + bwidth = encoding_widths[encoding]; + /* Hack! I've seen a stray zero byte before BOM. Is that supposed to happen? */ + while(source_size > bwidth && source[0] == 0) + { + --source_size; + ++source; + TRACE("skipped leading zero\n"); + } + if(source_size % bwidth) + { + /* When we need two bytes for a character, it's strange to have an uneven bytestream length. */ + if(noquiet) WARN("Weird tag size %d for encoding %u - I will probably trim too early or something but I think the MP3 is broken.\n", (int)source_size, encoding); + source_size -= source_size % bwidth; + } + text_converters[encoding](sb, (unsigned char*)source, source_size); + if(sb->size) TRACE("UTF-8 string (the first one): %s\n", debugstr_a(sb->p)); + else if(noquiet) ERR("unable to convert string to UTF-8 (out of memory, junk input?)!\n"); +} + +char *next_text(char* prev, int encoding, size_t limit) +{ + char *text = prev; + unsigned long neednull = encoding_widths[encoding]; + /* So I go lengths to find zero or double zero... */ + while(text-prev < (int)limit) + { + if(text[0] == 0) + { + if(neednull <= limit-(text-prev)) + { + unsigned long i = 1; + for(; iid, id, 4); + store_id3_text(&t->text, realdata, realsize, NOQUIET); + if(VERBOSE4) TRACE("Note: ID3v2 %c%c%c%c text frame: %s\n", id[0], id[1], id[2], id[3], t->text.p); +} + +/* Store a new comment that perhaps is a RVA / RVA_ALBUM/AUDIOPHILE / RVA_MIX/RADIO one + Special gimmik: It also stores USLT to the texts. Stucture is the same as for comments. */ +static void process_comment(mpg123_handle *fr, enum frame_types tt, char *realdata, size_t realsize, int rva_level, char *id) +{ + /* Text encoding $xx */ + /* Language $xx xx xx */ + /* Short description (encoded!) $00 (00) */ + /* Then the comment text (encoded) ... */ + char encoding = realdata[0]; + char *lang = realdata+1; /* I'll only use the 3 bytes! */ + char *descr = realdata+4; + char *text = NULL; + mpg123_text *xcom = NULL; + if((int)realsize < descr-realdata) + { + if(NOQUIET) ERR("Invalid frame size of %lu (too small for anything).\n", (unsigned long)realsize); + return; + } + xcom = (tt == uslt ? add_text(fr) : add_comment(fr)); + if(VERBOSE4) TRACE("Note: Storing comment from %s encoding\n", enc_name(realdata[0])); + if(xcom == NULL) + { + if(NOQUIET) ERR("Unable to attach new comment!\n"); + return; + } + memcpy(xcom->lang, lang, 3); + memcpy(xcom->id, id, 4); + /* Now I can abuse a byte from lang for the encoding. */ + descr[-1] = encoding; + /* Be careful with finding the end of description, I have to honor encoding here. */ + text = next_text(descr, encoding, realsize-(descr-realdata)); + if(text == NULL) + { + if(NOQUIET) ERR("No comment text / valid description?\n"); + pop_comment(fr); + return; + } + store_id3_text(&xcom->description, descr-1, text-descr+1, NOQUIET); + text[-1] = encoding; + store_id3_text(&xcom->text, text-1, realsize+1-(text-realdata), NOQUIET); + + if(VERBOSE4) + { + TRACE("Note: ID3 comm/uslt desc: %s\n", xcom->description.fill > 0 ? xcom->description.p : ""); + TRACE("Note: ID3 comm/uslt text: %s\n", xcom->text.fill > 0 ? xcom->text.p : ""); + } + /* Look out for RVA info only when we really deal with a straight comment. */ + if(tt == comment && xcom->description.fill > 0 && xcom->text.fill > 0) + { + int rva_mode = -1; /* mix / album */ + if( !strcasecmp(xcom->description.p, "rva") + || !strcasecmp(xcom->description.p, "rva_mix") + || !strcasecmp(xcom->description.p, "rva_track") + || !strcasecmp(xcom->description.p, "rva_radio")) + rva_mode = 0; + else if( !strcasecmp(xcom->description.p, "rva_album") + || !strcasecmp(xcom->description.p, "rva_audiophile") + || !strcasecmp(xcom->description.p, "rva_user")) + rva_mode = 1; + if((rva_mode > -1) && (fr->rva.level[rva_mode] <= rva_level)) + { + fr->rva.gain[rva_mode] = (float) atof(xcom->text.p); + if(VERBOSE3) TRACE("Note: RVA value %fdB\n", fr->rva.gain[rva_mode]); + fr->rva.peak[rva_mode] = 0; + fr->rva.level[rva_mode] = rva_level; + } + } +} + +void process_extra(mpg123_handle *fr, char* realdata, size_t realsize, int rva_level, char *id) +{ + /* Text encoding $xx */ + /* Description ... $00 (00) */ + /* Text ... */ + char encoding = realdata[0]; + char *descr = realdata+1; /* remember, the encoding is descr[-1] */ + char *text; + mpg123_text *xex; + if((int)realsize < descr-realdata) + { + if(NOQUIET) ERR("Invalid frame size of %lu (too small for anything).\n", (unsigned long)realsize); + return; + } + text = next_text(descr, encoding, realsize-(descr-realdata)); + if(VERBOSE4) TRACE("Note: Storing extra from %s encoding\n", enc_name(realdata[0])); + if(text == NULL) + { + if(NOQUIET) ERR("No extra frame text / valid description?\n"); + return; + } + xex = add_extra(fr); + if(xex == NULL) + { + if(NOQUIET) ERR("Unable to attach new extra text!\n"); + return; + } + memcpy(xex->id, id, 4); + store_id3_text(&xex->description, descr-1, text-descr+1, NOQUIET); + text[-1] = encoding; + store_id3_text(&xex->text, text-1, realsize-(text-realdata)+1, NOQUIET); + if(xex->description.fill > 0) + { + int is_peak = 0; + int rva_mode = -1; /* mix / album */ + + if(!strncasecmp(xex->description.p, "replaygain_track_",17)) + { + if(VERBOSE3) TRACE("Note: RVA ReplayGain track gain/peak\n"); + + rva_mode = 0; + if(!strcasecmp(xex->description.p, "replaygain_track_peak")) is_peak = 1; + else if(strcasecmp(xex->description.p, "replaygain_track_gain")) rva_mode = -1; + } + else + if(!strncasecmp(xex->description.p, "replaygain_album_",17)) + { + if(VERBOSE3) TRACE("Note: RVA ReplayGain album gain/peak\n"); + + rva_mode = 1; + if(!strcasecmp(xex->description.p, "replaygain_album_peak")) is_peak = 1; + else if(strcasecmp(xex->description.p, "replaygain_album_gain")) rva_mode = -1; + } + if((rva_mode > -1) && (fr->rva.level[rva_mode] <= rva_level)) + { + if(xex->text.fill > 0) + { + if(is_peak) + { + fr->rva.peak[rva_mode] = (float) atof(xex->text.p); + if(VERBOSE3) TRACE("Note: RVA peak %f\n", fr->rva.peak[rva_mode]); + } + else + { + fr->rva.gain[rva_mode] = (float) atof(xex->text.p); + if(VERBOSE3) TRACE("Note: RVA gain %fdB\n", fr->rva.gain[rva_mode]); + } + fr->rva.level[rva_mode] = rva_level; + } + } + } +} + +/* Make a ID3v2.3+ 4-byte ID from a ID3v2.2 3-byte ID + Note that not all frames survived to 2.4; the mapping goes to 2.3 . + A notable miss is the old RVA frame, which is very unspecific anyway. + This function returns -1 when a not known 3 char ID was encountered, 0 otherwise. */ +int promote_framename(mpg123_handle *fr, char *id) /* fr because of VERBOSE macros */ +{ + size_t i; + const char *old[] = + { + "COM", "TAL", "TBP", "TCM", "TCO", "TCR", "TDA", "TDY", "TEN", "TFT", + "TIM", "TKE", "TLA", "TLE", "TMT", "TOA", "TOF", "TOL", "TOR", "TOT", + "TP1", "TP2", "TP3", "TP4", "TPA", "TPB", "TRC", "TDA", "TRK", "TSI", + "TSS", "TT1", "TT2", "TT3", "TXT", "TXX", "TYE" + }; + const char *new[] = + { + "COMM", "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDAT", "TDLY", "TENC", "TFLT", + "TIME", "TKEY", "TLAN", "TLEN", "TMED", "TOPE", "TOFN", "TOLY", "TORY", "TOAL", + "TPE1", "TPE2", "TPE3", "TPE4", "TPOS", "TPUB", "TSRC", "TRDA", "TRCK", "TSIZ", + "TSSE", "TIT1", "TIT2", "TIT3", "TEXT", "TXXX", "TYER" + }; + for(i=0; ird->read_frame_body(fr, buf, 6)) < 0) /* read more header information */ + return ret2; + + if(buf[0] == 0xff) return 0; /* Revision, will never be 0xff. */ + + /* second new byte are some nice flags, if these are invalid skip the whole thing */ + flags = buf[1]; + TRACE("ID3v2: flags 0x%08x\n", flags); + /* use 4 bytes from buf to construct 28bit uint value and return 1; return 0 if bytes are not synchsafe */ + #define synchsafe_to_long(buf,res) \ + ( \ + (((buf)[0]|(buf)[1]|(buf)[2]|(buf)[3]) & 0x80) ? 0 : \ + (res = (((unsigned long) (buf)[0]) << 21) \ + | (((unsigned long) (buf)[1]) << 14) \ + | (((unsigned long) (buf)[2]) << 7) \ + | ((unsigned long) (buf)[3]) \ + ,1) \ + ) + /* id3v2.3 does not store synchsafe frame sizes, but synchsafe tag size - doh! */ + #define bytes_to_long(buf,res) \ + ( \ + major == 3 ? \ + (res = (((unsigned long) (buf)[0]) << 24) \ + | (((unsigned long) (buf)[1]) << 16) \ + | (((unsigned long) (buf)[2]) << 8) \ + | ((unsigned long) (buf)[3]) \ + ,1) : synchsafe_to_long(buf,res) \ + ) + /* for id3v2.2 only */ + #define threebytes_to_long(buf,res) \ + ( \ + res = (((unsigned long) (buf)[0]) << 16) \ + | (((unsigned long) (buf)[1]) << 8) \ + | ((unsigned long) (buf)[2]) \ + ,1 \ + ) + + /* length-10 or length-20 (footer present); 4 synchsafe integers == 28 bit number */ + /* we have already read 10 bytes, so left are length or length+10 bytes belonging to tag */ + if(!synchsafe_to_long(buf+2,length)) + { + if(NOQUIET) ERR("Bad tag length (not synchsafe): 0x%02x%02x%02x%02x; You got a bad ID3 tag here.\n", buf[2],buf[3],buf[4],buf[5]); + return 0; + } + TRACE("ID3v2: tag data length %lu\n", length); +#ifndef NO_ID3V2 + if(VERBOSE2) TRACE("Note: ID3v2.%i rev %i tag of %lu bytes\n", major, buf[0], length); + /* skip if unknown version/scary flags, parse otherwise */ + if((flags & UNKNOWN_FLAGS) || (major > 4) || (major < 2)) + { + /* going to skip because there are unknown flags set */ + if(NOQUIET) WARN("ID3v2: Won't parse the ID3v2 tag with major version %u and flags 0x%xu - some extra code may be needed\n", major, flags); +#endif + if((ret2 = fr->rd->skip_bytes(fr,length)) < 0) /* will not store data in backbuff! */ + ret = ret2; +#ifndef NO_ID3V2 + } + else + { + fr->id3v2.version = major; + /* try to interpret that beast */ + if((tagdata = (unsigned char*) HeapAlloc(GetProcessHeap(),0,length+1)) != NULL) + { + TRACE("ID3v2: analysing frames...\n"); + if((ret2 = fr->rd->read_frame_body(fr,tagdata,length)) > 0) + { + unsigned long tagpos = 0; + TRACE("ID3v2: have read at all %lu bytes for the tag now\n", (unsigned long)length+6); + /* going to apply strlen for strings inside frames, make sure that it doesn't overflow! */ + tagdata[length] = 0; + if(flags & EXTHEAD_FLAG) + { + TRACE("ID3v2: skipping extended header\n"); + if(!bytes_to_long(tagdata, tagpos)) + { + ret = 0; + if(NOQUIET) ERR("Bad (non-synchsafe) tag offset: 0x%02x%02x%02x%02x\n", tagdata[0], tagdata[1], tagdata[2], tagdata[3]); + } + } + if(ret > 0) + { + char id[5]; + unsigned long framesize; + unsigned long fflags; /* need 16 bits, actually */ + id[4] = 0; + /* pos now advanced after ext head, now a frame has to follow */ + while(tagpos < length-10) /* I want to read at least a full header */ + { + int i = 0; + unsigned long pos = tagpos; + int head_part = fr->id3v2.version == 2 ? 3 : 4; /* bytes of frame title and of framesize value */ + /* level 1,2,3 - 0 is info from lame/info tag! */ + /* rva tags with ascending significance, then general frames */ + enum frame_types tt = unknown; + /* we may have entered the padding zone or any other strangeness: check if we have valid frame id characters */ + for(i=0; i< head_part; ++i) + if( !( ((tagdata[tagpos+i] > 47) && (tagdata[tagpos+i] < 58)) + || ((tagdata[tagpos+i] > 64) && (tagdata[tagpos+i] < 91)) ) ) + { + TRACE("ID3v2: real tag data apparently ended after %lu bytes with 0x%02x%02x%02x%02x\n", tagpos, tagdata[tagpos], tagdata[tagpos+1], tagdata[tagpos+2], tagdata[tagpos+3]); + /* This is no hard error... let's just hope that we got something meaningful already (ret==1 in that case). */ + goto tagparse_cleanup; /* Need to escape two loops here. */ + } + if(ret > 0) + { + /* 4 or 3 bytes id */ + lstrcpynA(id, (char*) tagdata+pos, head_part); + pos += head_part; + tagpos += head_part; + /* size as 32 bits or 28 bits */ + if(fr->id3v2.version == 2) threebytes_to_long(tagdata+pos, framesize); + else + if(!bytes_to_long(tagdata+pos, framesize)) + { + /* Just assume that up to now there was some good data. */ + if(NOQUIET) ERR("ID3v2: non-syncsafe size of %s frame, skipping the remainder of tag\n", id); + break; + } + if(VERBOSE3) TRACE("Note: ID3v2 %s frame of size %lu\n", id, framesize); + tagpos += head_part + framesize; /* the important advancement in whole tag */ + if(tagpos > length) + { + if(NOQUIET) ERR("Whoa! ID3v2 frame claims to be larger than the whole rest of the tag.\n"); + break; + } + pos += head_part; + if(fr->id3v2.version > 2) + { + fflags = (((unsigned long) tagdata[pos]) << 8) | ((unsigned long) tagdata[pos+1]); + pos += 2; + tagpos += 2; + } + else fflags = 0; + /* for sanity, after full parsing tagpos should be == pos */ + /* debug4("ID3v2: found %s frame, size %lu (as bytes: 0x%08lx), flags 0x%016lx", id, framesize, framesize, fflags); */ + /* %0abc0000 %0h00kmnp */ + #define BAD_FFLAGS (unsigned long) 36784 + #define PRES_TAG_FFLAG 16384 + #define PRES_FILE_FFLAG 8192 + #define READ_ONLY_FFLAG 4096 + #define GROUP_FFLAG 64 + #define COMPR_FFLAG 8 + #define ENCR_FFLAG 4 + #define UNSYNC_FFLAG 2 + #define DATLEN_FFLAG 1 + if(head_part < 4 && promote_framename(fr, id) != 0) continue; + + /* shall not or want not handle these */ + if(fflags & (BAD_FFLAGS | COMPR_FFLAG | ENCR_FFLAG)) + { + if(NOQUIET) WARN("ID3v2: skipping invalid/unsupported frame\n"); + continue; + } + + for(i = 0; i < KNOWN_FRAMES; ++i) + if(!strncmp(frame_type[i], id, 4)){ tt = i; break; } + + if(id[0] == 'T' && tt != extra) tt = text; + + if(tt != unknown) + { + int rva_mode = -1; /* mix / album */ + unsigned long realsize = framesize; + unsigned char* realdata = tagdata+pos; + if((flags & UNSYNC_FLAG) || (fflags & UNSYNC_FFLAG)) + { + unsigned long ipos = 0; + unsigned long opos = 0; + TRACE("Id3v2: going to de-unsync the frame data\n"); + /* de-unsync: FF00 -> FF; real FF00 is simply represented as FF0000 ... */ + /* damn, that means I have to delete bytes from withing the data block... thus need temporal storage */ + /* standard mandates that de-unsync should always be safe if flag is set */ + realdata = (unsigned char*) HeapAlloc(GetProcessHeap(),0,framesize); /* will need <= bytes */ + if(realdata == NULL) + { + if(NOQUIET) ERR("ID3v2: unable to allocate working buffer for de-unsync\n"); + continue; + } + /* now going byte per byte through the data... */ + realdata[0] = tagdata[pos]; + opos = 1; + for(ipos = pos+1; ipos < pos+framesize; ++ipos) + { + if(!((tagdata[ipos] == 0) && (tagdata[ipos-1] == 0xff))) + { + realdata[opos++] = tagdata[ipos]; + } + } + realsize = opos; + TRACE("ID3v2: de-unsync made %lu out of %lu bytes\n", realsize, framesize); + } + pos = 0; /* now at the beginning again... */ + switch(tt) + { + case comment: + case uslt: + process_comment(fr, tt, (char*)realdata, realsize, comment+1, id); + break; + case extra: /* perhaps foobar2000's work */ + process_extra(fr, (char*)realdata, realsize, extra+1, id); + break; + case rva2: /* "the" RVA tag */ + { + /* starts with null-terminated identification */ + if(VERBOSE3) TRACE("Note: RVA2 identification \"%s\"\n", realdata); + /* default: some individual value, mix mode */ + rva_mode = 0; + if( !strncasecmp((char*)realdata, "album", 5) + || !strncasecmp((char*)realdata, "audiophile", 10) + || !strncasecmp((char*)realdata, "user", 4)) + rva_mode = 1; + if(fr->rva.level[rva_mode] <= rva2+1) + { + pos += strlen((char*) realdata) + 1; + if(realdata[pos] == 1) + { + ++pos; + /* only handle master channel */ + TRACE("ID3v2: it is for the master channel\n"); + /* two bytes adjustment, one byte for bits representing peak - n bytes, eh bits, for peak */ + /* 16 bit signed integer = dB * 512 ... the double cast is needed to preserve the sign of negative values! */ + fr->rva.gain[rva_mode] = (float) ( (((short)((signed char)realdata[pos])) << 8) | realdata[pos+1] ) / 512; + pos += 2; + if(VERBOSE3) TRACE("Note: RVA value %fdB\n", fr->rva.gain[rva_mode]); + /* heh, the peak value is represented by a number of bits - but in what manner? Skipping that part */ + fr->rva.peak[rva_mode] = 0; + fr->rva.level[rva_mode] = rva2+1; + } + } + } + break; + /* non-rva metainfo, simply store... */ + case text: + process_text(fr, (char*)realdata, realsize, id); + break; + default: if(NOQUIET) ERR("ID3v2: unknown frame type %i\n", tt); + } + if((flags & UNSYNC_FLAG) || (fflags & UNSYNC_FFLAG)) HeapFree(GetProcessHeap(),0,realdata); + } + #undef BAD_FFLAGS + #undef PRES_TAG_FFLAG + #undef PRES_FILE_FFLAG + #undef READ_ONLY_FFLAG + #undef GROUP_FFLAG + #undef COMPR_FFLAG + #undef ENCR_FFLAG + #undef UNSYNC_FFLAG + #undef DATLEN_FFLAG + } + else break; + #undef KNOWN_FRAMES + } + } + } + else + { + if(NOQUIET) ERR("ID3v2: Duh, not able to read ID3v2 tag data.\n"); + ret = ret2; + } +tagparse_cleanup: + HeapFree(GetProcessHeap(),0,tagdata); + } + else + { + if(NOQUIET) ERR("ID3v2: Arrg! Unable to allocate %lu bytes for interpreting ID3v2 data - trying to skip instead.\n", length); + if((ret2 = fr->rd->skip_bytes(fr,length)) < 0) ret = ret2; /* will not store data in backbuff! */ + else ret = 0; + } + } +#endif /* NO_ID3V2 */ + /* skip footer if present */ + if((ret > 0) && (flags & FOOTER_FLAG) && ((ret2 = fr->rd->skip_bytes(fr,length)) < 0)) ret = ret2; + + return ret; + #undef UNSYNC_FLAG + #undef EXTHEAD_FLAG + #undef EXP_FLAG + #undef FOOTER_FLAG + #undef UNKOWN_FLAGS +} + +#ifndef NO_ID3V2 /* Disabling all the rest... */ + +static void convert_latin1(mpg123_string *sb, unsigned char* s, size_t l) +{ + size_t length = l; + size_t i; + unsigned char *p; + /* determine real length, a latin1 character can at most take 2 in UTF8 */ + for(i=0; i= 0x80) ++length; + + TRACE("UTF-8 length: %lu\n", (unsigned long)length); + /* one extra zero byte for paranoia */ + if(!mpg123_resize_string(sb, length+1)){ mpg123_free_string(sb); return ; } + + p = (unsigned char*) sb->p; /* Signedness doesn't matter but it shows I thought about the non-issue */ + for(i=0; i>6); + *(p+1) = 0x80 | (s[i] & 0x3f); + p+=2; + } + + sb->p[length] = 0; + sb->fill = length+1; +} + +#define FULLPOINT(f,s) ( (((f)&0x3ff)<<10) + ((s)&0x3ff) + 0x10000 ) +/* Remember: There's a limit at 0x1ffff. */ +#define UTF8LEN(x) ( (x)<0x80 ? 1 : ((x)<0x800 ? 2 : ((x)<0x10000 ? 3 : 4))) +static void convert_utf16(mpg123_string *sb, unsigned char* s, size_t l, int str_be) +{ + size_t i; + unsigned char *p; + size_t length = 0; /* the resulting UTF-8 length */ + /* Determine real length... extreme case can be more than utf-16 length. */ + size_t high = 0; + size_t low = 1; + TRACE("convert_utf16 with length %lu\n", (unsigned long)l); + + if(l < 1){ mpg123_set_string(sb, ""); return; } + + if(!str_be) /* little-endian */ + { + high = 1; /* The second byte is the high byte. */ + low = 0; /* The first byte is the low byte. */ + } + /* first: get length, check for errors -- stop at first one */ + for(i=0; i < l-1; i+=2) + { + unsigned long point = ((unsigned long) s[i+high]<<8) + s[i+low]; + if((point & 0xd800) == 0xd800) /* lead surrogate */ + { + unsigned short second = (i+3 < l) ? (s[i+2+high]<<8) + s[i+2+low] : 0; + if((second & 0xdc00) == 0xdc00) /* good... */ + { + point = FULLPOINT(point,second); + length += UTF8LEN(point); /* possibly 4 bytes */ + i+=2; /* We overstepped one word. */ + } + else /* if no valid pair, break here */ + { + TRACE("Invalid UTF16 surrogate pair at %li.\n", (unsigned long)i); + l = i; /* Forget the half pair, END! */ + break; + } + } + else length += UTF8LEN(point); /* 1,2 or 3 bytes */ + } + + if(!mpg123_resize_string(sb, length+1)){ mpg123_free_string(sb); return ; } + + /* Now really convert, skip checks as these have been done just before. */ + p = (unsigned char*) sb->p; /* Signedness doesn't matter but it shows I thought about the non-issue */ + for(i=0; i < l-1; i+=2) + { + unsigned long codepoint = ((unsigned long) s[i+high]<<8) + s[i+low]; + if((codepoint & 0xd800) == 0xd800) /* lead surrogate */ + { + unsigned short second = (s[i+2+high]<<8) + s[i+2+low]; + codepoint = FULLPOINT(codepoint,second); + i+=2; /* We overstepped one word. */ + } + if(codepoint < 0x80) *p++ = (unsigned char) codepoint; + else if(codepoint < 0x800) + { + *p++ = (unsigned char) (0xc0 | (codepoint>>6)); + *p++ = (unsigned char) (0x80 | (codepoint & 0x3f)); + } + else if(codepoint < 0x10000) + { + *p++ = (unsigned char) (0xe0 | (codepoint>>12)); + *p++ = 0x80 | ((codepoint>>6) & 0x3f); + *p++ = 0x80 | (codepoint & 0x3f); + } + else if (codepoint < 0x200000) + { + *p++ = (unsigned char) (0xf0 | codepoint>>18); + *p++ = (unsigned char) (0x80 | ((codepoint>>12) & 0x3f)); + *p++ = (unsigned char) (0x80 | ((codepoint>>6) & 0x3f)); + *p++ = (unsigned char) (0x80 | (codepoint & 0x3f)); + } /* ignore bigger ones (that are not possible here anyway) */ + } + sb->p[sb->size-1] = 0; /* paranoia... */ + sb->fill = sb->size; +} +#undef UTF8LEN +#undef FULLPOINT + +static void convert_utf16be(mpg123_string *sb, unsigned char* source, size_t len) +{ + convert_utf16(sb, source, len, 1); +} + +static void convert_utf16bom(mpg123_string *sb, unsigned char* source, size_t len) +{ + if(len < 2){ mpg123_free_string(sb); return; } + + if(source[0] == 0xff && source[1] == 0xfe) /* Little-endian */ + convert_utf16(sb, source + 2, len - 2, 0); + else /* Big-endian */ + convert_utf16(sb, source + 2, len - 2, 1); +} + +static void convert_utf8(mpg123_string *sb, unsigned char* source, size_t len) +{ + if(mpg123_resize_string(sb, len+1)) + { + memcpy(sb->p, source, len); + sb->p[len] = 0; + sb->fill = len+1; + } + else mpg123_free_string(sb); +} + +#endif diff --git a/dlls/winemp3.acm/id3.h b/dlls/winemp3.acm/id3.h new file mode 100644 index 0000000..b48d30e --- /dev/null +++ b/dlls/winemp3.acm/id3.h @@ -0,0 +1,42 @@ +/* + id3: ID3v2.3 and ID3v2.4 parsing (a relevant subset) + + copyright 2006-2007 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Thomas Orgis + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ + +#ifndef MPG123_ID3_H +#define MPG123_ID3_H + +/* really need it _here_! */ +#include "frame.h" + +#ifdef NO_ID3V2 +# define init_id3(fr) +# define exit_id3(fr) +# define reset_id3(fr) +# define id3_link(fr) +#else +void init_id3(mpg123_handle *fr); +void exit_id3(mpg123_handle *fr); +void reset_id3(mpg123_handle *fr); +void id3_link(mpg123_handle *fr); +#endif +int parse_new_id3(mpg123_handle *fr, unsigned long first4bytes); + +#endif diff --git a/dlls/winemp3.acm/interface.c b/dlls/winemp3.acm/interface.c deleted file mode 100644 index 6592902..0000000 --- a/dlls/winemp3.acm/interface.c +++ /dev/null @@ -1,254 +0,0 @@ -/* - * Copyright (c) Michael Hipp and other authors of the mpglib project. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#include "config.h" -#include -#include -#include - -#include "windef.h" -#include "winbase.h" -#include "wine/debug.h" -#include "mpg123.h" -#include "mpglib.h" - -WINE_DEFAULT_DEBUG_CHANNEL(mpeg3); - -BOOL InitMP3(struct mpstr *mp) -{ - static int init = 0; - - memset(mp,0,sizeof(struct mpstr)); - - mp->framesize = 0; - mp->fsizeold = -1; - mp->bsize = 0; - mp->head = mp->tail = NULL; - mp->fr.single = -1; - mp->bsnum = 0; - mp->synth_bo = 1; - mp->fr.mp = mp; - - if(!init) { - init = 1; - make_decode_tables(32767); - init_layer2(); - init_layer3(SBLIMIT); - } - - return !0; -} - -void ClearMP3Buffer(struct mpstr *mp) -{ - struct buf *b,*bn; - - b = mp->tail; - while(b) { - HeapFree(GetProcessHeap(), 0, b->pnt); - bn = b->next; - HeapFree(GetProcessHeap(), 0, b); - b = bn; - } - mp->tail = NULL; - mp->head = NULL; - mp->bsize = 0; -} - -static struct buf *addbuf(struct mpstr *mp,const unsigned char *buf,int size) -{ - struct buf *nbuf; - - nbuf = HeapAlloc(GetProcessHeap(), 0, sizeof(struct buf)); - if(!nbuf) { - WARN("Out of memory!\n"); - return NULL; - } - nbuf->pnt = HeapAlloc(GetProcessHeap(), 0, size); - if(!nbuf->pnt) { - HeapFree(GetProcessHeap(), 0, nbuf); - WARN("Out of memory!\n"); - return NULL; - } - nbuf->size = size; - memcpy(nbuf->pnt,buf,size); - nbuf->next = NULL; - nbuf->prev = mp->head; - nbuf->pos = 0; - - if(!mp->tail) { - mp->tail = nbuf; - } - else { - mp->head->next = nbuf; - } - - mp->head = nbuf; - mp->bsize += size; - - return nbuf; -} - -static void remove_buf(struct mpstr *mp) -{ - struct buf *buf = mp->tail; - - mp->tail = buf->next; - if(mp->tail) - mp->tail->prev = NULL; - else { - mp->tail = mp->head = NULL; - } - - HeapFree(GetProcessHeap(), 0, buf->pnt); - HeapFree(GetProcessHeap(), 0, buf); - -} - -static int read_buf_byte(struct mpstr *mp) -{ - unsigned int b; - - int pos; - - pos = mp->tail->pos; - while(pos >= mp->tail->size) { - remove_buf(mp); - pos = mp->tail->pos; - } - - b = mp->tail->pnt[pos]; - mp->bsize--; - mp->tail->pos++; - - - return b; -} - -static void read_head(struct mpstr *mp) -{ - unsigned long head; - - head = read_buf_byte(mp); - head <<= 8; - head |= read_buf_byte(mp); - head <<= 8; - head |= read_buf_byte(mp); - head <<= 8; - head |= read_buf_byte(mp); - - mp->header = head; -} - -int decodeMP3(struct mpstr *mp,const unsigned char *in,int isize,unsigned char *out, - int osize,int *done) -{ - int len; - - if(osize < 4608) { - ERR("Output buffer too small\n"); - return MP3_ERR; - } - - if(in) { - if(addbuf(mp,in,isize) == NULL) { - return MP3_ERR; - } - } - - /* First decode header */ - if(mp->framesize == 0) { - int ret; - if(mp->bsize < 4) { - return MP3_NEED_MORE; - } - read_head(mp); - while (!(ret = decode_header(&mp->fr,mp->header)) && mp->bsize) - { - mp->header = mp->header << 8; - mp->header |= read_buf_byte(mp); - } - - if (!ret) { - return MP3_NEED_MORE; - } - mp->framesize = mp->fr.framesize; - } - - if(mp->fr.framesize > mp->bsize) - return MP3_NEED_MORE; - - wordpointer = mp->bsspace[mp->bsnum] + 512; - mp->bsnum = (mp->bsnum + 1) & 0x1; - bitindex = 0; - - len = 0; - while(len < mp->framesize) { - int nlen; - int blen = mp->tail->size - mp->tail->pos; - if( (mp->framesize - len) <= blen) { - nlen = mp->framesize-len; - } - else { - nlen = blen; - } - memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen); - len += nlen; - mp->tail->pos += nlen; - mp->bsize -= nlen; - if(mp->tail->pos == mp->tail->size) { - remove_buf(mp); - } - } - - *done = 0; - if(mp->fr.error_protection) - getbits(16); - switch(mp->fr.lay) { - case 1: - do_layer1(&mp->fr,out,done); - break; - case 2: - do_layer2(&mp->fr,out,done); - break; - case 3: - do_layer3(&mp->fr,out,done); - break; - } - - mp->fsizeold = mp->framesize; - mp->framesize = 0; - - return MP3_OK; -} - -int set_pointer(struct mpstr *mp, long backstep) -{ - unsigned char *bsbufold; - if(mp->fsizeold < 0 && backstep > 0) { - /* This is not a bug if we just did seeking, the first frame is dropped then */ - WARN("Can't step back %ld!\n",backstep); - return MP3_ERR; - } - bsbufold = mp->bsspace[mp->bsnum] + 512; - wordpointer -= backstep; - if (backstep) - memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep); - bitindex = 0; - return MP3_OK; -} diff --git a/dlls/winemp3.acm/layer1.c b/dlls/winemp3.acm/layer1.c index 392176a..0b112f6 100644 --- a/dlls/winemp3.acm/layer1.c +++ b/dlls/winemp3.acm/layer1.c @@ -1,12 +1,10 @@ /* - * Mpeg Layer-1 audio decoder - * -------------------------- - * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README' - * This file has been copied from mpglib. - * near unoptimzed ... - * - * may have a few bugs after last optimization ... - * + layer1.c: the layer 1 decoder + + copyright 1995-2008 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Michael Hipp + * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -20,140 +18,152 @@ * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ -#include "mpg123.h" + may have a few bugs after last optimization ... +*/ + +#include "mpg123lib_intern.h" +#include "getbits.h" -static void I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],struct frame *fr) +void I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr) { - unsigned int *ba=balloc; - unsigned int *sca = (unsigned int *) scale_index; - - if(fr->stereo) { - int i; - int jsbound = fr->jsbound; - for (i=0;istereo == 2) + { + int i; + int jsbound = fr->jsbound; + for(i=0;istereo) { - int jsbound = fr->jsbound; - register real *f0 = fraction[0]; - register real *f1 = fraction[1]; - ba = balloc; - for (sample=smpb,i=0;istereo == 2) + { + int jsbound = fr->jsbound; + register real *f0 = fraction[0]; + register real *f1 = fraction[1]; + ba = balloc; + for(sample=smpb,i=0;imuls[n+1][*sca++]); + else *f0++ = DOUBLE_TO_REAL(0.0); + + if((n=*ba++)) + *f1++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15( ((-1)<muls[n+1][*sca++]); + else *f1++ = DOUBLE_TO_REAL(0.0); + } + for(i=jsbound;imuls[n+1][*sca++]); + *f1++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]); + } + else *f0++ = *f1++ = DOUBLE_TO_REAL(0.0); + } + for(i=fr->down_sample_sblimit;i<32;i++) + fraction[0][i] = fraction[1][i] = 0.0; + } + else + { + register real *f0 = fraction[0]; + ba = balloc; + for(sample=smpb,i=0;imuls[n+1][*sca++]); + else *f0++ = DOUBLE_TO_REAL(0.0); + } + for(i=fr->down_sample_sblimit;i<32;i++) + fraction[0][i] = DOUBLE_TO_REAL(0.0); + } } -int do_layer1(struct frame *fr,unsigned char *pcm_sample,int *pcm_point) +int do_layer1(mpg123_handle *fr) { - int clip=0; - int i,stereo = fr->stereo; - unsigned int balloc[2*SBLIMIT]; - unsigned int scale_index[2][SBLIMIT]; - real fraction[2][SBLIMIT]; - int single = fr->single; - - fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32; - - if(stereo == 1 || single == 3) - single = 0; - - I_step_one(balloc,scale_index,fr); - - for (i=0;i= 0) { - clip += synth_1to1_mono(fr->mp,fraction[single],pcm_sample,pcm_point); - } - else { - int p1 = *pcm_point; - clip += synth_1to1(fr->mp,fraction[0],0,pcm_sample,&p1); - clip += synth_1to1(fr->mp,fraction[1],1,pcm_sample,pcm_point); - } - } - - return clip; + int clip=0; + int i,stereo = fr->stereo; + unsigned int balloc[2*SBLIMIT]; + unsigned int scale_index[2][SBLIMIT]; + ALIGNED(16) real fraction[2][SBLIMIT]; + int single = fr->single; + + fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32; + + if(stereo == 1 || single == SINGLE_MIX) /* I don't see mixing handled here */ + single = SINGLE_LEFT; + + I_step_one(balloc,scale_index,fr); + + for(i=0;isynth_mono)(fraction[single], fr); + else + clip += (fr->synth_stereo)(fraction[0], fraction[1], fr); + } + + return clip; } + + diff --git a/dlls/winemp3.acm/layer2.c b/dlls/winemp3.acm/layer2.c index 535d90c..60b457b 100644 --- a/dlls/winemp3.acm/layer2.c +++ b/dlls/winemp3.acm/layer2.c @@ -1,9 +1,10 @@ /* - * Mpeg Layer-2 audio decoder - * -------------------------- - * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README' - * This file has been copied from mpglib. - * + layer2.c: the layer 2 decoder, root of mpg123 + + copyright 1994-2008 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Michael Hipp + * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -17,285 +18,360 @@ * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ -#include "mpg123.h" + mpg123 started as mp2 decoder a long time ago... + part of this file is required for layer 1, too. +*/ + +#include + +#include "mpg123lib_intern.h" +#ifndef NO_LAYER2 #include "l2tables.h" +#endif +#include "getbits.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(mpeg3); + +#ifndef NO_LAYER12 /* Stuff needed for layer I and II. */ static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */ static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */ static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */ -real muls[27][64]; /* also used by layer 1 */ +static const double mulmul[27] = +{ + 0.0 , -2.0/3.0 , 2.0/3.0 , + 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 , + 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 , + 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 , + -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 , + -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 +}; -void init_layer2(void) +void init_layer12(void) { - static const double mulmul[27] = { - 0.0 , -2.0/3.0 , 2.0/3.0 , - 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 , - 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 , - 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 , - -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 , - -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 }; - static const int base[3][9] = { - { 1 , 0, 2 , } , - { 17, 18, 0 , 19, 20 , } , - { 21, 1, 22, 23, 0, 24, 25, 2, 26 } }; - int i,j,k,l,len; - real *table; - static const int tablen[3] = { 3 , 5 , 9 }; - static int *itable,*tables[3] = { grp_3tab , grp_5tab , grp_9tab }; - - for(i=0;i<3;i++) - { - itable = tables[i]; - len = tablen[i]; - for(j=0;jmuls[k], mulmul[k]); + *table++ = 0.0; + } +} -static void II_step_one(unsigned int *bit_alloc,int *scale,struct frame *fr) +real* init_layer12_table(mpg123_handle *fr, real *table, double m) { - int stereo = fr->stereo-1; - int sblimit = fr->II_sblimit; - int jsbound = fr->jsbound; - int sblimit2 = fr->II_sblimit<alloc; - int i; - static unsigned int scfsi_buf[64]; - unsigned int *scfsi,*bita; - int sc,step; - - bita = bit_alloc; - if(stereo) - { - for (i=jsbound;i;i--,alloc1+=(1<bits); - *bita++ = (char) getbits(step); - } - for (i=sblimit-jsbound;i;i--,alloc1+=(1<bits); - bita[1] = bita[0]; - bita+=2; - } - bita = bit_alloc; - scfsi=scfsi_buf; - for (i=sblimit2;i;i--) - if (*bita++) - *scfsi++ = (char) getbits_fast(2); - } - else /* mono */ - { - for (i=sblimit;i;i--,alloc1+=(1<bits); - bita = bit_alloc; - scfsi=scfsi_buf; - for (i=sblimit;i;i--) - if (*bita++) - *scfsi++ = (char) getbits_fast(2); - } - - bita = bit_alloc; - scfsi=scfsi_buf; - for (i=sblimit2;i;i--) - if (*bita++) - switch (*scfsi++) - { - case 0: - *scale++ = getbits_fast(6); - *scale++ = getbits_fast(6); - *scale++ = getbits_fast(6); - break; - case 1 : - *scale++ = sc = getbits_fast(6); - *scale++ = sc; - *scale++ = getbits_fast(6); - break; - case 2: - *scale++ = sc = getbits_fast(6); - *scale++ = sc; - *scale++ = sc; - break; - default: /* case 3 */ - *scale++ = getbits_fast(6); - *scale++ = sc = getbits_fast(6); - *scale++ = sc; - break; - } + int i,j; + for(j=3,i=0;i<63;i++,j--) + *table++ = DOUBLE_TO_REAL_SCALE_LAYER12(m * pow(2.0,(double) j / 3.0)); + return table; +} + +#ifdef OPT_MMXORSSE +real* init_layer12_table_mmx(mpg123_handle *fr, real *table, double m) +{ + int i,j; + if(!fr->p.down_sample) + { + for(j=3,i=0;i<63;i++,j--) + *table++ = DOUBLE_TO_REAL(16384 * m * pow(2.0,(double) j / 3.0)); + } + else + { + for(j=3,i=0;i<63;i++,j--) + *table++ = DOUBLE_TO_REAL(m * pow(2.0,(double) j / 3.0)); + } + return table; } +#endif -static void II_step_two(unsigned int *bit_alloc, real fraction[2][4][SBLIMIT], - int *scale, struct frame *fr, int x1) +#endif /* NO_LAYER12 */ + +/* The rest is the actual decoding of layer II data. */ + +#ifndef NO_LAYER2 + +void II_step_one(unsigned int *bit_alloc,int *scale,mpg123_handle *fr) { - int i,j,k,ba; - int stereo = fr->stereo; - int sblimit = fr->II_sblimit; - int jsbound = fr->jsbound; - const struct al_table *alloc2,*alloc1 = fr->alloc; - unsigned int *bita=bit_alloc; - int d1,step; - - for (i=0;ibits; - for (j=0;jbits; - if( (d1=alloc2->d) < 0) - { - real cm=muls[k][scale[x1]]; - fraction[j][0][i] = ((real) ((int)getbits(k) + d1)) * cm; - fraction[j][1][i] = ((real) ((int)getbits(k) + d1)) * cm; - fraction[j][2][i] = ((real) ((int)getbits(k) + d1)) * cm; - } - else - { - static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; - unsigned int idx,*tab,m=scale[x1]; - idx = getbits(k); - tab = (unsigned int *) (table[d1] + idx + idx + idx); - fraction[j][0][i] = muls[*tab++][m]; - fraction[j][1][i] = muls[*tab++][m]; - fraction[j][2][i] = muls[*tab][m]; - } - scale+=3; - } - else - fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0; - } - } - - for (i=jsbound;ibits; - bita++; /* channel 1 and channel 2 bitalloc are the same */ - if ( (ba=*bita++) ) - { - k=(alloc2 = alloc1+ba)->bits; - if( (d1=alloc2->d) < 0) - { - real cm; - cm=muls[k][scale[x1+3]]; - fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(k) + d1) ) * cm; - fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(k) + d1) ) * cm; - fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(k) + d1) ) * cm; - cm=muls[k][scale[x1]]; - fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm; - } - else - { - static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; - unsigned int idx,*tab,m1,m2; - m1 = scale[x1]; m2 = scale[x1+3]; - idx = getbits(k); - tab = (unsigned int *) (table[d1] + idx + idx + idx); - fraction[0][0][i] = muls[*tab][m1]; fraction[1][0][i] = muls[*tab++][m2]; - fraction[0][1][i] = muls[*tab][m1]; fraction[1][1][i] = muls[*tab++][m2]; - fraction[0][2][i] = muls[*tab][m1]; fraction[1][2][i] = muls[*tab][m2]; - } - scale+=6; - } - else { - fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = - fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0; - } + int stereo = fr->stereo-1; + int sblimit = fr->II_sblimit; + int jsbound = fr->jsbound; + int sblimit2 = fr->II_sblimit<alloc; + int i; + unsigned int scfsi_buf[64]; + unsigned int *scfsi,*bita; + int sc,step; + + bita = bit_alloc; + if(stereo) + { + for(i=jsbound;i;i--,alloc1+=(1<bits; + *bita++ = (char) getbits(fr, step); + *bita++ = (char) getbits(fr, step); + } + for(i=sblimit-jsbound;i;i--,alloc1+=(1<bits; + bita[0] = (char) getbits(fr, step); + bita[1] = bita[0]; + bita+=2; + } + bita = bit_alloc; + scfsi=scfsi_buf; + + for(i=sblimit2;i;i--) + if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2); + } + else /* mono */ + { + for(i=sblimit;i;i--,alloc1+=(1<bits; + *bita++ = (char) getbits(fr, step); + } + bita = bit_alloc; + scfsi=scfsi_buf; + for(i=sblimit;i;i--) + if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2); + } + + bita = bit_alloc; + scfsi=scfsi_buf; + for(i=sblimit2;i;i--) + if(*bita++) + switch(*scfsi++) + { + case 0: + *scale++ = getbits_fast(fr, 6); + *scale++ = getbits_fast(fr, 6); + *scale++ = getbits_fast(fr, 6); + break; + case 1 : + *scale++ = sc = getbits_fast(fr, 6); + *scale++ = sc; + *scale++ = getbits_fast(fr, 6); + break; + case 2: + *scale++ = sc = getbits_fast(fr, 6); + *scale++ = sc; + *scale++ = sc; + break; + default: /* case 3 */ + *scale++ = getbits_fast(fr, 6); + *scale++ = sc = getbits_fast(fr, 6); + *scale++ = sc; + break; + } +} + + +void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,mpg123_handle *fr,int x1) +{ + int i,j,k,ba; + int stereo = fr->stereo; + int sblimit = fr->II_sblimit; + int jsbound = fr->jsbound; + const struct al_table *alloc2,*alloc1 = fr->alloc; + unsigned int *bita=bit_alloc; + int d1,step; + + for(i=0;ibits; + for(j=0;jbits; + if( (d1=alloc2->d) < 0) + { + real cm=fr->muls[k][scale[x1]]; + fraction[j][0][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm); + fraction[j][1][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm); + fraction[j][2][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm); + } + else + { + const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; + unsigned int idx,*tab,m=scale[x1]; + idx = (unsigned int) getbits(fr, k); + tab = (unsigned int *) (table[d1] + idx + idx + idx); + fraction[j][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]); + fraction[j][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]); + fraction[j][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m]); + } + scale+=3; + } + else + fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0); + } + } + + for(i=jsbound;ibits; + bita++; /* channel 1 and channel 2 bitalloc are the same */ + if( (ba=*bita++) ) + { + k=(alloc2 = alloc1+ba)->bits; + if( (d1=alloc2->d) < 0) + { + real cm; + cm=fr->muls[k][scale[x1+3]]; + fraction[0][0][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1); + fraction[0][1][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1); + fraction[0][2][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1); + fraction[1][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm); + fraction[1][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm); + fraction[1][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm); + cm=fr->muls[k][scale[x1]]; + fraction[0][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm); + fraction[0][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm); + fraction[0][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm); + } + else + { + const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; + unsigned int idx,*tab,m1,m2; + m1 = scale[x1]; m2 = scale[x1+3]; + idx = (unsigned int) getbits(fr, k); + tab = (unsigned int *) (table[d1] + idx + idx + idx); + fraction[0][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]); + fraction[0][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]); + fraction[0][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m2]); + } + scale+=6; + } + else + { + fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = + fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = DOUBLE_TO_REAL(0.0); + } /* - should we use individual scalefac for channel 2 or - is the current way the right one , where we just copy channel 1 to - channel 2 ?? - The current 'strange' thing is, that we throw away the scalefac - values for the second channel ...!! --> changed .. now we use the scalefac values of channel one !! + Historic comment... + should we use individual scalefac for channel 2 or + is the current way the right one , where we just copy channel 1 to + channel 2 ?? + The current 'strange' thing is, that we throw away the scalefac + values for the second channel ...!! + -> changed .. now we use the scalefac values of channel one !! */ - } + } - for(i=sblimit;i (fr->down_sample_sblimit) ) + sblimit = fr->down_sample_sblimit; + for(i=sblimit;ilsf) - table = 4; - else - table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index]; - sblim = sblims[table]; - - fr->alloc = tables[table]; - fr->II_sblimit = sblim; + const int translate[3][2][16] = + { + { + { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 }, + { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } + }, + { + { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 }, + { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } + }, + { + { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 }, + { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } + } + }; + + int table,sblim; + const struct al_table *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 }; + const int sblims[5] = { 27 , 30 , 8, 12 , 30 }; + + if(fr->sampling_frequency >= 3) /* Or equivalent: (fr->lsf == 1) */ + table = 4; + else + table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index]; + + sblim = sblims[table]; + fr->alloc = tables[table]; + fr->II_sblimit = sblim; } -int do_layer2(struct frame *fr,unsigned char *pcm_sample,int *pcm_point) + +int do_layer2(mpg123_handle *fr) { - int clip=0; - int i,j; - int stereo = fr->stereo; - real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */ - unsigned int bit_alloc[64]; - int scale[192]; - int single = fr->single; - - II_select_table(fr); - fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? - (fr->mode_ext<<2)+4 : fr->II_sblimit; - - if(stereo == 1 || single == 3) - single = 0; - - II_step_one(bit_alloc, scale, fr); - - for (i=0;i>2); - for (j=0;j<3;j++) { - if(single >= 0) { - clip += synth_1to1_mono(fr->mp,fraction[0][j],pcm_sample,pcm_point); - } - else { - int p1 = *pcm_point; - clip += synth_1to1(fr->mp,fraction[0][j],0,pcm_sample,&p1); - clip += synth_1to1(fr->mp,fraction[1][j],1,pcm_sample,pcm_point); - } - - } - } - - return clip; + int clip=0; + int i,j; + int stereo = fr->stereo; + ALIGNED(16) real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */ + unsigned int bit_alloc[64]; + int scale[192]; + int single = fr->single; + + II_select_table(fr); + fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : fr->II_sblimit; + + if(fr->jsbound > fr->II_sblimit) + { + TRACE("Truncating stereo boundary to sideband limit.\n"); + fr->jsbound=fr->II_sblimit; + } + + /* TODO: What happens with mono mixing, actually? */ + if(stereo == 1 || single == SINGLE_MIX) /* also, mix not really handled */ + single = SINGLE_LEFT; + + II_step_one(bit_alloc, scale, fr); + + for(i=0;i>2); + for(j=0;j<3;j++) + { + if(single != SINGLE_STEREO) + clip += (fr->synth_mono)(fraction[single][j], fr); + else + clip += (fr->synth_stereo)(fraction[0][j], fraction[1][j], fr); + } + } + + return clip; } + +#endif /* NO_LAYER2 */ diff --git a/dlls/winemp3.acm/layer3.c b/dlls/winemp3.acm/layer3.c index 17f5a7a..9aebf8e 100644 --- a/dlls/winemp3.acm/layer3.c +++ b/dlls/winemp3.acm/layer3.c @@ -1,10 +1,10 @@ /* - * Mpeg Layer-3 audio decoder - * -------------------------- - * copyright (c) 1995,1996,1997 by Michael Hipp. - * All rights reserved. See also 'README' - * This file has been copied from mpglib. - * + leyer3.c: the layer 3 decoder + + copyright 1995-2008 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Michael Hipp + * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -18,85 +18,141 @@ * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ -#include "config.h" -#include -#include "wine/debug.h" -#include "mpg123.h" -#include "mpglib.h" + Dear visitor: + If you feel you don't understand fully the works of this file, your feeling might be correct. + + Optimize-TODO: put short bands into the band-field without the stride of 3 reals + Length-optimze: unify long and short band code where it is possible + + The int-vs-pointer situation has to be cleaned up. +*/ +#include + +#include "mpg123lib_intern.h" #include "huffman.h" +#include "getbits.h" +#include "wine/debug.h" -#define MPEG1 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3); +/* define CUT_SFB21 if you want to cut-off the frequency above 16kHz */ +#if 0 +#define CUT_SFB21 +#endif + +/* static one-time calculated tables... or so */ static real ispow[8207]; static real aa_ca[8],aa_cs[8]; static real COS1[12][6]; static real win[4][36]; static real win1[4][36]; -static real gainpow2[256+118+4]; -static real COS9[9]; +real COS9[9]; /* dct36_3dnow wants to use that */ static real COS6_1,COS6_2; -static real tfcos36[9]; +real tfcos36[9]; /* dct36_3dnow wants to use that */ static real tfcos12[3]; +#define NEW_DCT9 +#ifdef NEW_DCT9 +static real cos9[3],cos18[3]; +#endif -struct bandInfoStruct { - short longIdx[23]; - short longDiff[22]; - short shortIdx[14]; - short shortDiff[13]; +/* Decoder state data, living on the stack of do_layer3. */ + +struct gr_info_s +{ + int scfsi; + unsigned part2_3_length; + unsigned big_values; + unsigned scalefac_compress; + unsigned block_type; + unsigned mixed_block_flag; + unsigned table_select[3]; + unsigned subblock_gain[3]; + unsigned maxband[3]; + unsigned maxbandl; + unsigned maxb; + unsigned region1start; + unsigned region2start; + unsigned preflag; + unsigned scalefac_scale; + unsigned count1table_select; + real *full_gain[3]; + real *pow2gain; }; -static int longLimit[9][23]; -static int shortLimit[9][14]; - -static const struct bandInfoStruct bandInfo[9] = { - -/* MPEG 1.0 */ - { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576}, - {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158}, - {0,4*3,8*3,12*3,16*3,22*3,30*3,40*3,52*3,66*3, 84*3,106*3,136*3,192*3}, - {4,4,4,4,6,8,10,12,14,18,22,30,56} } , - - { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576}, - {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192}, - {0,4*3,8*3,12*3,16*3,22*3,28*3,38*3,50*3,64*3, 80*3,100*3,126*3,192*3}, - {4,4,4,4,6,6,10,12,14,16,20,26,66} } , - - { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} , - {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} , - {0,4*3,8*3,12*3,16*3,22*3,30*3,42*3,58*3,78*3,104*3,138*3,180*3,192*3} , - {4,4,4,4,6,8,12,16,20,26,34,42,12} } , - -/* MPEG 2.0 */ - { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, - {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } , - {0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} , - {4,4,4,6,6,8,10,14,18,26,32,42,18 } } , - - { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576}, - {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } , - {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,136*3,180*3,192*3} , - {4,4,4,6,8,10,12,14,18,24,32,44,12 } } , - - { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, - {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 }, - {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3}, - {4,4,4,6,8,10,12,14,18,24,30,40,18 } } , -/* MPEG 2.5 */ - { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} , - {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54}, - {0,12,24,36,54,78,108,144,186,240,312,402,522,576}, - {4,4,4,6,8,10,12,14,18,24,30,40,18} }, - { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} , - {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54}, - {0,12,24,36,54,78,108,144,186,240,312,402,522,576}, - {4,4,4,6,8,10,12,14,18,24,30,40,18} }, - { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576}, - {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2}, - {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576}, - {8,8,8,12,16,20,24,28,36,2,2,2,26} } , +struct III_sideinfo +{ + unsigned main_data_begin; + unsigned private_bits; + /* Hm, funny... struct inside struct... */ + struct { struct gr_info_s gr[2]; } ch[2]; +}; + +struct bandInfoStruct +{ + int longIdx[23]; + int longDiff[22]; + int shortIdx[14]; + int shortDiff[13]; +}; + +/* Techy details about our friendly MPEG data. Fairly constant over the years;-) */ +const struct bandInfoStruct bandInfo[9] = +{ + { /* MPEG 1.0 */ + {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576}, + {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158}, + {0,4*3,8*3,12*3,16*3,22*3,30*3,40*3,52*3,66*3, 84*3,106*3,136*3,192*3}, + {4,4,4,4,6,8,10,12,14,18,22,30,56} + }, + { + {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576}, + {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192}, + {0,4*3,8*3,12*3,16*3,22*3,28*3,38*3,50*3,64*3, 80*3,100*3,126*3,192*3}, + {4,4,4,4,6,6,10,12,14,16,20,26,66} + }, + { + {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576}, + {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26}, + {0,4*3,8*3,12*3,16*3,22*3,30*3,42*3,58*3,78*3,104*3,138*3,180*3,192*3}, + {4,4,4,4,6,8,12,16,20,26,34,42,12} + }, + { /* MPEG 2.0 */ + {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, + {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } , + {0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} , + {4,4,4,6,6,8,10,14,18,26,32,42,18 } + }, + { /* Twiddling 3 values here (not just 330->332!) fixed bug 1895025. */ + {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576}, + {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36 }, + {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,136*3,180*3,192*3}, + {4,4,4,6,8,10,12,14,18,24,32,44,12 } + }, + { + {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, + {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 }, + {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3}, + {4,4,4,6,8,10,12,14,18,24,30,40,18 } + }, + { /* MPEG 2.5 */ + {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, + {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54}, + {0,12,24,36,54,78,108,144,186,240,312,402,522,576}, + {4,4,4,6,8,10,12,14,18,24,30,40,18} + }, + { + {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, + {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54}, + {0,12,24,36,54,78,108,144,186,240,312,402,522,576}, + {4,4,4,6,8,10,12,14,18,24,30,40,18} + }, + { + {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576}, + {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2}, + {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576}, + {8,8,8,12,16,20,24,28,36,2,2,2,26} + } }; static int mapbuf0[9][152]; @@ -111,1503 +167,1918 @@ static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */ static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16]; static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16]; -/* - * init tables for layer-3 - */ -void init_layer3(int down_sample_sblimit) +#ifdef REAL_IS_FIXED +static const char gainpow2_scale[256+118+4+1] = +{ + 19,19,19,20,20,20,20,21,21,21,21,22,22,22,22,23,23,23,23,24,24,24,24,25,25,25,25,26,26,26,26,27, + 27,27,27,28,28,28,28,29,29,29,29,30,30,30,30,31,31,31,31,32,32,32,32,33,33,33,33,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, + 34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,0 +}; +#endif + +/* Some helpers used in init_layer3 */ + +#ifdef OPT_MMXORSSE +real init_layer3_gainpow2_mmx(mpg123_handle *fr, int i) { - int i,j,k,l; - - for(i=-256;i<118+4;i++) - gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) ); - - for(i=0;i<8207;i++) - ispow[i] = pow((double)i,(double)4.0/3.0); - - for (i=0;i<8;i++) - { - static const double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037}; - double sq=sqrt(1.0+Ci[i]*Ci[i]); - aa_cs[i] = 1.0/sq; - aa_ca[i] = Ci[i]/sq; - } - - for(i=0;i<18;i++) - { - win[0][i] = win[1][i] = 0.5 * sin( M_PI / 72.0 * (double) (2*(i+0) +1) ) / cos ( M_PI * (double) (2*(i+0) +19) / 72.0 ); - win[0][i+18] = win[3][i+18] = 0.5 * sin( M_PI / 72.0 * (double) (2*(i+18)+1) ) / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 ); - } - for(i=0;i<6;i++) - { - win[1][i+18] = 0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 ); - win[3][i+12] = 0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 ); - win[1][i+24] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+13) ) / cos ( M_PI * (double) (2*(i+24)+19) / 72.0 ); - win[1][i+30] = win[3][i] = 0.0; - win[3][i+6 ] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*(i+6 )+19) / 72.0 ); - } - - for(i=0;i<9;i++) - COS9[i] = cos( M_PI / 18.0 * (double) i); - - for(i=0;i<9;i++) - tfcos36[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 ); - for(i=0;i<3;i++) - tfcos12[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 ); - - COS6_1 = cos( M_PI / 6.0 * (double) 1); - COS6_2 = cos( M_PI / 6.0 * (double) 2); - - for(i=0;i<12;i++) - { - win[2][i] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 ); - for(j=0;j<6;j++) - COS1[i][j] = cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) ); - } - - for(j=0;j<4;j++) { - static const int len[4] = { 36,36,12,36 }; - for(i=0;i 0) { - if( i & 1 ) - p1 = pow(base,(i+1.0)*0.5); - else - p2 = pow(base,i*0.5); - } - pow1_1[j][i] = p1; - pow2_1[j][i] = p2; - pow1_2[j][i] = M_SQRT2 * p1; - pow2_2[j][i] = M_SQRT2 * p2; - } - } - - for(j=0;j<9;j++) - { - const struct bandInfoStruct *bi = &bandInfo[j]; - int *mp; - int cb,lwin; - const short *bdf; - - mp = map[j][0] = mapbuf0[j]; - bdf = bi->longDiff; - for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) { - *mp++ = (*bdf) >> 1; - *mp++ = i; - *mp++ = 3; - *mp++ = cb; - } - bdf = bi->shortDiff+3; - for(cb=3;cb<13;cb++) { - int l = (*bdf++) >> 1; - for(lwin=0;lwin<3;lwin++) { - *mp++ = l; - *mp++ = i + lwin; - *mp++ = lwin; - *mp++ = cb; - } - i += 6*l; - } - mapend[j][0] = mp; - - mp = map[j][1] = mapbuf1[j]; - bdf = bi->shortDiff+0; - for(i=0,cb=0;cb<13;cb++) { - int l = (*bdf++) >> 1; - for(lwin=0;lwin<3;lwin++) { - *mp++ = l; - *mp++ = i + lwin; - *mp++ = lwin; - *mp++ = cb; - } - i += 6*l; - } - mapend[j][1] = mp; - - mp = map[j][2] = mapbuf2[j]; - bdf = bi->longDiff; - for(cb = 0; cb < 22 ; cb++) { - *mp++ = (*bdf++) >> 1; - *mp++ = cb; - } - mapend[j][2] = mp; - - } - - for(j=0;j<9;j++) { - for(i=0;i<23;i++) { - longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1; - if(longLimit[j][i] > (down_sample_sblimit) ) - longLimit[j][i] = down_sample_sblimit; - } - for(i=0;i<14;i++) { - shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1; - if(shortLimit[j][i] > (down_sample_sblimit) ) - shortLimit[j][i] = down_sample_sblimit; - } - } - - for(i=0;i<5;i++) { - for(j=0;j<6;j++) { - for(k=0;k<6;k++) { - int n = k + j * 6 + i * 36; - i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12); - } - } - } - for(i=0;i<4;i++) { - for(j=0;j<4;j++) { - for(k=0;k<4;k++) { - int n = k + j * 4 + i * 16; - i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12); - } - } - } - for(i=0;i<4;i++) { - for(j=0;j<3;j++) { - int n = j + i * 3; - i_slen2[n+244] = i|(j<<3) | (5<<12); - n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15); - } - } - - for(i=0;i<5;i++) { - for(j=0;j<5;j++) { - for(k=0;k<4;k++) { - for(l=0;l<4;l++) { - int n = l + k * 4 + j * 16 + i * 80; - n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12); - } - } - } - } - for(i=0;i<5;i++) { - for(j=0;j<5;j++) { - for(k=0;k<4;k++) { - int n = k + j * 4 + i * 20; - n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12); - } - } - } + if(!fr->p.down_sample) return DOUBLE_TO_REAL(16384.0 * pow((double)2.0,-0.25 * (double) (i+210) )); + else return DOUBLE_TO_REAL(pow((double)2.0,-0.25 * (double) (i+210))); } +#endif -/* - * read additional side information - */ -#ifdef MPEG1 -static int III_get_side_info_1(struct III_sideinfo *si,int stereo, - int ms_stereo,long sfreq,int single) +real init_layer3_gainpow2(mpg123_handle *fr, int i) { - int ch, gr; - int powdiff = (single == 3) ? 4 : 0; - - si->main_data_begin = getbits(9); - if (stereo == 1) - si->private_bits = getbits_fast(5); - else - si->private_bits = getbits_fast(3); - - for (ch=0; chch[ch].gr[0].scfsi = -1; - si->ch[ch].gr[1].scfsi = getbits_fast(4); - } - - for (gr=0; gr<2; gr++) - { - for (ch=0; chch[ch].gr[gr]); - - gr_info->part2_3_length = getbits(12); - gr_info->big_values = getbits_fast(9); - if(gr_info->big_values > 288) { - FIXME("big_values (%d) too large!\n", gr_info->big_values); - gr_info->big_values = 288; - } - gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff; - if(ms_stereo) - gr_info->pow2gain += 2; - gr_info->scalefac_compress = getbits_fast(4); -/* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */ - if(get1bit()) - { - int i; - gr_info->block_type = getbits_fast(2); - gr_info->mixed_block_flag = get1bit(); - gr_info->table_select[0] = getbits_fast(5); - gr_info->table_select[1] = getbits_fast(5); - /* - * table_select[2] not needed, because there is no region2, - * but to satisfy some verifications tools we set it either. - */ - gr_info->table_select[2] = 0; - for(i=0;i<3;i++) - gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3); - - if(gr_info->block_type == 0) { - FIXME("Blocktype == 0 and window-switching == 1 not allowed.\n"); - return 0; - } - /* region_count/start parameters are implicit in this case. */ - gr_info->region1start = 36>>1; - gr_info->region2start = 576>>1; - } - else - { - int i,r0c,r1c; - for (i=0; i<3; i++) - gr_info->table_select[i] = getbits_fast(5); - r0c = getbits_fast(4); - r1c = getbits_fast(3); - gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ; - gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; - gr_info->block_type = 0; - gr_info->mixed_block_flag = 0; - } - gr_info->preflag = get1bit(); - gr_info->scalefac_scale = get1bit(); - gr_info->count1table_select = get1bit(); - } - } - return !0; + return DOUBLE_TO_REAL_SCALE_LAYER3(pow((double)2.0,-0.25 * (double) (i+210)),i+256); } + + +/* init tables for layer-3 ... specific with the downsampling... */ +void init_layer3(void) +{ + int i,j,k,l; + + for(i=0;i<8207;i++) + ispow[i] = DOUBLE_TO_REAL_POW43(pow((double)i,(double)4.0/3.0)); + + for(i=0;i<8;i++) + { + const double Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037}; + double sq = sqrt(1.0+Ci[i]*Ci[i]); + aa_cs[i] = DOUBLE_TO_REAL(1.0/sq); + aa_ca[i] = DOUBLE_TO_REAL(Ci[i]/sq); + } + + for(i=0;i<18;i++) + { + win[0][i] = win[1][i] = + DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+0) +1)) / cos(M_PI * (double)(2*(i+0) +19) / 72.0) ); + win[0][i+18] = win[3][i+18] = + DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+18)+1)) / cos(M_PI * (double)(2*(i+18)+19) / 72.0) ); + } + for(i=0;i<6;i++) + { + win[1][i+18] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 )); + win[3][i+12] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 )); + win[1][i+24] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+13) ) / cos ( M_PI * (double) (2*(i+24)+19) / 72.0 )); + win[1][i+30] = win[3][i] = DOUBLE_TO_REAL(0.0); + win[3][i+6 ] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1 ) ) / cos ( M_PI * (double) (2*(i+6 )+19) / 72.0 )); + } + + for(i=0;i<9;i++) + COS9[i] = DOUBLE_TO_REAL(cos( M_PI / 18.0 * (double) i)); + + for(i=0;i<9;i++) + tfcos36[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 )); + + for(i=0;i<3;i++) + tfcos12[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 )); + + COS6_1 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 1)); + COS6_2 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 2)); + +#ifdef NEW_DCT9 + cos9[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/9.0)); + cos9[1] = DOUBLE_TO_REAL(cos(5.0*M_PI/9.0)); + cos9[2] = DOUBLE_TO_REAL(cos(7.0*M_PI/9.0)); + cos18[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/18.0)); + cos18[1] = DOUBLE_TO_REAL(cos(11.0*M_PI/18.0)); + cos18[2] = DOUBLE_TO_REAL(cos(13.0*M_PI/18.0)); #endif -/* - * Side Info for MPEG 2.0 / LSF - */ -static int III_get_side_info_2(struct III_sideinfo *si,int stereo, - int ms_stereo,long sfreq,int single) + for(i=0;i<12;i++) + { + win[2][i] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 )); + for(j=0;j<6;j++) + COS1[i][j] = DOUBLE_TO_REAL(cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) )); + } + + for(j=0;j<4;j++) + { + const int len[4] = { 36,36,12,36 }; + for(i=0;i 0) + { + if( i & 1 ) p1 = pow(base,(i+1.0)*0.5); + else p2 = pow(base,i*0.5); + } + pow1_1[j][i] = DOUBLE_TO_REAL_15(p1); + pow2_1[j][i] = DOUBLE_TO_REAL_15(p2); + pow1_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p1); + pow2_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p2); + } + } + + for(j=0;j<9;j++) + { + const struct bandInfoStruct *bi = &bandInfo[j]; + int *mp; + int cb,lwin; + const int *bdf; + + mp = map[j][0] = mapbuf0[j]; + bdf = bi->longDiff; + for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) + { + *mp++ = (*bdf) >> 1; + *mp++ = i; + *mp++ = 3; + *mp++ = cb; + } + bdf = bi->shortDiff+3; + for(cb=3;cb<13;cb++) + { + int l = (*bdf++) >> 1; + for(lwin=0;lwin<3;lwin++) + { + *mp++ = l; + *mp++ = i + lwin; + *mp++ = lwin; + *mp++ = cb; + } + i += 6*l; + } + mapend[j][0] = mp; + + mp = map[j][1] = mapbuf1[j]; + bdf = bi->shortDiff+0; + for(i=0,cb=0;cb<13;cb++) + { + int l = (*bdf++) >> 1; + for(lwin=0;lwin<3;lwin++) + { + *mp++ = l; + *mp++ = i + lwin; + *mp++ = lwin; + *mp++ = cb; + } + i += 6*l; + } + mapend[j][1] = mp; + + mp = map[j][2] = mapbuf2[j]; + bdf = bi->longDiff; + for(cb = 0; cb < 22 ; cb++) + { + *mp++ = (*bdf++) >> 1; + *mp++ = cb; + } + mapend[j][2] = mp; + } + + /* Now for some serious loopings! */ + for(i=0;i<5;i++) + for(j=0;j<6;j++) + for(k=0;k<6;k++) + { + int n = k + j * 6 + i * 36; + i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12); + } + for(i=0;i<4;i++) + for(j=0;j<4;j++) + for(k=0;k<4;k++) + { + int n = k + j * 4 + i * 16; + i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12); + } + for(i=0;i<4;i++) + for(j=0;j<3;j++) + { + int n = j + i * 3; + i_slen2[n+244] = i|(j<<3) | (5<<12); + n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15); + } + for(i=0;i<5;i++) + for(j=0;j<5;j++) + for(k=0;k<4;k++) + for(l=0;l<4;l++) + { + int n = l + k * 4 + j * 16 + i * 80; + n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12); + } + for(i=0;i<5;i++) + for(j=0;j<5;j++) + for(k=0;k<4;k++) + { + int n = k + j * 4 + i * 20; + n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12); + } +} + + +void init_layer3_stuff(mpg123_handle *fr, real (*gainpow2)(mpg123_handle *fr, int i)) { - int ch; - int powdiff = (single == 3) ? 4 : 0; - - si->main_data_begin = getbits(8); - if (stereo == 1) - si->private_bits = get1bit(); - else - si->private_bits = getbits_fast(2); - - for (ch=0; chch[ch].gr[0]); - - gr_info->part2_3_length = getbits(12); - gr_info->big_values = getbits_fast(9); - if(gr_info->big_values > 288) { - FIXME("big_values(%d) too large!\n", gr_info->big_values); - gr_info->big_values = 288; - } - gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff; - if(ms_stereo) - gr_info->pow2gain += 2; - gr_info->scalefac_compress = getbits(9); -/* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */ - if(get1bit()) - { - int i; - gr_info->block_type = getbits_fast(2); - gr_info->mixed_block_flag = get1bit(); - gr_info->table_select[0] = getbits_fast(5); - gr_info->table_select[1] = getbits_fast(5); - /* - * table_select[2] not needed, because there is no region2, - * but to satisfy some verifications tools we set it either. - */ - gr_info->table_select[2] = 0; - for(i=0;i<3;i++) - gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3); - - if(gr_info->block_type == 0) { - FIXME("Blocktype == 0 and window-switching == 1 not allowed.\n"); - return 0; - } - /* region_count/start parameters are implicit in this case. */ -/* check this again! */ - if(gr_info->block_type == 2) - gr_info->region1start = 36>>1; - else if(sfreq == 8) -/* check this for 2.5 and sfreq=8 */ - gr_info->region1start = 108>>1; - else - gr_info->region1start = 54>>1; - gr_info->region2start = 576>>1; - } - else - { - int i,r0c,r1c; - for (i=0; i<3; i++) - gr_info->table_select[i] = getbits_fast(5); - r0c = getbits_fast(4); - r1c = getbits_fast(3); - gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ; - gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; - gr_info->block_type = 0; - gr_info->mixed_block_flag = 0; - } - gr_info->scalefac_scale = get1bit(); - gr_info->count1table_select = get1bit(); - } - return !0; + int i,j; + + for(i=-256;i<118+4;i++) fr->gainpow2[i+256] = gainpow2(fr,i); + + for(j=0;j<9;j++) + { + for(i=0;i<23;i++) + { + fr->longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1; + if(fr->longLimit[j][i] > (fr->down_sample_sblimit) ) + fr->longLimit[j][i] = fr->down_sample_sblimit; + } + for(i=0;i<14;i++) + { + fr->shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1; + if(fr->shortLimit[j][i] > (fr->down_sample_sblimit) ) + fr->shortLimit[j][i] = fr->down_sample_sblimit; + } + } } /* - * read scalefactors - */ -#ifdef MPEG1 -static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info) + Observe! + Now come the actualy decoding routines. +*/ + +/* read additional side information (for MPEG 1 and MPEG 2) */ +static int III_get_side_info(mpg123_handle *fr, struct III_sideinfo *si,int stereo, int ms_stereo,long sfreq,int single) +{ + int ch, gr; + int powdiff = (single == SINGLE_MIX) ? 4 : 0; + + const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } }; + const int *tab = tabs[fr->lsf]; + + si->main_data_begin = getbits(fr, tab[1]); + + if(si->main_data_begin > fr->bitreservoir) + { + if(VERBOSE2) TRACE("Note: missing %d bytes in bit reservoir for frame %li\n", (int)(si->main_data_begin - fr->bitreservoir), (long)fr->num); + + /* overwrite main_data_begin for the really available bit reservoir */ + backbits(fr, tab[1]); + if(fr->lsf == 0) + { + fr->wordpointer[0] = (unsigned char) (fr->bitreservoir >> 1); + fr->wordpointer[1] = (unsigned char) ((fr->bitreservoir & 1) << 7); + } + else fr->wordpointer[0] = (unsigned char) fr->bitreservoir; + + /* zero "side-info" data for a silence-frame + without touching audio data used as bit reservoir for following frame */ + memset(fr->wordpointer+2, 0, fr->ssize-2); + + /* reread the new bit reservoir offset */ + si->main_data_begin = getbits(fr, tab[1]); + } + + /* Keep track of the available data bytes for the bit reservoir. + Think: Substract the 2 crc bytes in parser already? */ + fr->bitreservoir = fr->bitreservoir + fr->framesize - fr->ssize - (fr->error_protection ? 2 : 0); + /* Limit the reservoir to the max for MPEG 1.0 or 2.x . */ + if(fr->bitreservoir > (unsigned int) (fr->lsf == 0 ? 511 : 255)) + fr->bitreservoir = (fr->lsf == 0 ? 511 : 255); + + /* Now back into less commented territory. It's code. It works. */ + + if (stereo == 1) + si->private_bits = getbits_fast(fr, tab[2]); + else + si->private_bits = getbits_fast(fr, tab[3]); + + if(!fr->lsf) for(ch=0; chch[ch].gr[0].scfsi = -1; + si->ch[ch].gr[1].scfsi = getbits_fast(fr, 4); + } + + for (gr=0; grch[ch].gr[gr]); + + gr_info->part2_3_length = getbits(fr, 12); + gr_info->big_values = getbits(fr, 9); + if(gr_info->big_values > 288) + { + ERR("big_values too large!\n"); + gr_info->big_values = 288; + } + gr_info->pow2gain = fr->gainpow2+256 - getbits_fast(fr, 8) + powdiff; + if(ms_stereo) gr_info->pow2gain += 2; + + gr_info->scalefac_compress = getbits(fr, tab[4]); + + if(get1bit(fr)) + { /* window switch flag */ + int i; + gr_info->block_type = getbits_fast(fr, 2); + gr_info->mixed_block_flag = get1bit(fr); + gr_info->table_select[0] = getbits_fast(fr, 5); + gr_info->table_select[1] = getbits_fast(fr, 5); + /* + table_select[2] not needed, because there is no region2, + but to satisfy some verification tools we set it either. + */ + gr_info->table_select[2] = 0; + for(i=0;i<3;i++) + gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(fr, 3)<<3); + + if(gr_info->block_type == 0) + { + ERR("Blocktype == 0 and window-switching == 1 not allowed.\n"); + return 1; + } + + /* region_count/start parameters are implicit in this case. */ + if( (!fr->lsf || (gr_info->block_type == 2)) && !fr->mpeg25) + { + gr_info->region1start = 36>>1; + gr_info->region2start = 576>>1; + } + else + { + if(fr->mpeg25) + { + int r0c,r1c; + if((gr_info->block_type == 2) && (!gr_info->mixed_block_flag) ) r0c = 5; + else r0c = 7; + + r1c = 20 - r0c; + gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ; + gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; + } + else + { + gr_info->region1start = 54>>1; + gr_info->region2start = 576>>1; + } + } + } + else + { + int i,r0c,r1c; + for (i=0; i<3; i++) + gr_info->table_select[i] = getbits_fast(fr, 5); + + r0c = getbits_fast(fr, 4); + r1c = getbits_fast(fr, 3); + gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ; + gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; + + if(r0c + r1c + 2 > 22) gr_info->region2start = 576>>1; + else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; + + gr_info->block_type = 0; + gr_info->mixed_block_flag = 0; + } + if(!fr->lsf) gr_info->preflag = get1bit(fr); + + gr_info->scalefac_scale = get1bit(fr); + gr_info->count1table_select = get1bit(fr); + } + return 0; +} + + +/* read scalefactors */ +static int III_get_scale_factors_1(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int ch,int gr) { - static const unsigned char slen[2][16] = { - {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4}, - {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3} - }; - int numbits; - int num0 = slen[0][gr_info->scalefac_compress]; - int num1 = slen[1][gr_info->scalefac_compress]; - - if (gr_info->block_type == 2) { - int i=18; - numbits = (num0 + num1) * 18; - - if (gr_info->mixed_block_flag) { - for (i=8;i;i--) - *scf++ = getbits_fast(num0); - i = 9; - numbits -= num0; /* num0 * 17 + num1 * 18 */ - } - - for (;i;i--) - *scf++ = getbits_fast(num0); - for (i = 18; i; i--) - *scf++ = getbits_fast(num1); - *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */ - } - else { - int i; - int scfsi = gr_info->scfsi; - - if(scfsi < 0) { /* scfsi < 0 => granule == 0 */ - for(i=11;i;i--) - *scf++ = getbits_fast(num0); - for(i=10;i;i--) - *scf++ = getbits_fast(num1); - numbits = (num0 + num1) * 10 + num0; - *scf++ = 0; - } - else { - numbits = 0; - if(!(scfsi & 0x8)) { - for (i=0;i<6;i++) - *scf++ = getbits_fast(num0); - numbits += num0 * 6; - } - else { - scf += 6; - } - - if(!(scfsi & 0x4)) { - for (i=0;i<5;i++) - *scf++ = getbits_fast(num0); - numbits += num0 * 5; - } - else { - scf += 5; - } - - if(!(scfsi & 0x2)) { - for(i=0;i<5;i++) - *scf++ = getbits_fast(num1); - numbits += num1 * 5; - } - else { - scf += 5; - } - - if(!(scfsi & 0x1)) { - for (i=0;i<5;i++) - *scf++ = getbits_fast(num1); - numbits += num1 * 5; - } - else { - scf += 5; - } - *scf++ = 0; /* no l[21] in original sources */ - } - } - return numbits; + const unsigned char slen[2][16] = + { + {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4}, + {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3} + }; + int numbits; + int num0 = slen[0][gr_info->scalefac_compress]; + int num1 = slen[1][gr_info->scalefac_compress]; + + if(gr_info->block_type == 2) + { + int i=18; + numbits = (num0 + num1) * 18; + + if(gr_info->mixed_block_flag) + { + for (i=8;i;i--) + *scf++ = getbits_fast(fr, num0); + + i = 9; + numbits -= num0; /* num0 * 17 + num1 * 18 */ + } + + for(;i;i--) *scf++ = getbits_fast(fr, num0); + + for(i = 18; i; i--) *scf++ = getbits_fast(fr, num1); + + *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */ + } + else + { + int i; + int scfsi = gr_info->scfsi; + + if(scfsi < 0) + { /* scfsi < 0 => granule == 0 */ + for(i=11;i;i--) *scf++ = getbits_fast(fr, num0); + + for(i=10;i;i--) *scf++ = getbits_fast(fr, num1); + + numbits = (num0 + num1) * 10 + num0; + *scf++ = 0; + } + else + { + numbits = 0; + if(!(scfsi & 0x8)) + { + for (i=0;i<6;i++) *scf++ = getbits_fast(fr, num0); + + numbits += num0 * 6; + } + else scf += 6; + + if(!(scfsi & 0x4)) + { + for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num0); + + numbits += num0 * 5; + } + else scf += 5; + + if(!(scfsi & 0x2)) + { + for(i=0;i<5;i++) *scf++ = getbits_fast(fr, num1); + + numbits += num1 * 5; + } + else scf += 5; + + if(!(scfsi & 0x1)) + { + for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num1); + + numbits += num1 * 5; + } + else scf += 5; + + *scf++ = 0; /* no l[21] in original sources */ + } + } + return numbits; } -#endif -static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo) +static int III_get_scale_factors_2(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int i_stereo) { - const unsigned char *pnt; - int i,j; - unsigned int slen; - int n = 0; - int numbits = 0; - - static const unsigned char stab[3][6][4] = { - { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} , - { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } , - { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} , - {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } , - { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} , - { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } }; - - if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */ - slen = i_slen2[gr_info->scalefac_compress>>1]; - else - slen = n_slen2[gr_info->scalefac_compress]; - - gr_info->preflag = (slen>>15) & 0x1; - - n = 0; - if( gr_info->block_type == 2 ) { - n++; - if(gr_info->mixed_block_flag) - n++; - } - - pnt = stab[n][(slen>>12)&0x7]; - - for(i=0;i<4;i++) { - int num = slen & 0x7; - slen >>= 3; - if(num) { - for(j=0;j<(int)(pnt[i]);j++) - *scf++ = getbits_fast(num); - numbits += pnt[i] * num; - } - else { - for(j=0;j<(int)(pnt[i]);j++) - *scf++ = 0; - } - } - - n = (n << 1) + 1; - for(i=0;i do_layer3() checks this */ + slen = i_slen2[gr_info->scalefac_compress>>1]; + else + slen = n_slen2[gr_info->scalefac_compress]; + + gr_info->preflag = (slen>>15) & 0x1; + + n = 0; + if( gr_info->block_type == 2 ) + { + n++; + if(gr_info->mixed_block_flag) n++; + } + + pnt = stab[n][(slen>>12)&0x7]; + + for(i=0;i<4;i++) + { + int num = slen & 0x7; + slen >>= 3; + if(num) + { + for(j=0;j<(int)(pnt[i]);j++) *scf++ = getbits_fast(fr, num); + + numbits += pnt[i] * num; + } + else + for(j=0;j<(int)(pnt[i]);j++) *scf++ = 0; + } + + n = (n << 1) + 1; + for(i=0;iscalefac_scale; - real *xrpnt = (real *) xr; - int l[3],l3; - int part2remain = gr_info->part2_3_length - part2bits; - int *me; - - { - int bv = gr_info->big_values; - int region1 = gr_info->region1start; - int region2 = gr_info->region2start; - - l3 = ((576>>1)-bv)>>1; /* - * we may lose the 'odd' bit here !! - * check this later again - */ - if(bv <= region1) { - l[0] = bv; l[1] = 0; l[2] = 0; - } - else { - l[0] = region1; - if(bv <= region2) { - l[1] = bv - l[0]; l[2] = 0; - } - else { - l[1] = region2 - l[0]; l[2] = bv - region2; - } - } - } - - if(gr_info->block_type == 2) { - /* - * decoding with short or mixed mode BandIndex table - */ - int i,max[4]; - int step=0,lwin=0,cb=0; - register real v = 0.0; - register int *m,mc; - - if(gr_info->mixed_block_flag) { - max[3] = -1; - max[0] = max[1] = max[2] = 2; - m = map[sfreq][0]; - me = mapend[sfreq][0]; - } - else { - max[0] = max[1] = max[2] = max[3] = -1; - /* max[3] not really needed in this case */ - m = map[sfreq][1]; - me = mapend[sfreq][1]; - } - - mc = 0; - for(i=0;i<2;i++) { - int lp = l[i]; - const struct newhuff *h = ht+gr_info->table_select[i]; - for(;lp;lp--,mc--) { - register int x,y; - if( (!mc) ) { - mc = *m++; - xrpnt = ((real *) xr) + (*m++); - lwin = *m++; - cb = *m++; - if(lwin == 3) { - v = gr_info->pow2gain[(*scf++) << shift]; - step = 1; - } - else { - v = gr_info->full_gain[lwin][(*scf++) << shift]; - step = 3; - } - } - { - register const short *val = h->table; - while((y=*val++)<0) { - if (get1bit()) - val -= y; - part2remain--; - } - x = y >> 4; - y &= 0xf; - } - if(x == 15) { - max[lwin] = cb; - part2remain -= h->linbits+1; - x += getbits(h->linbits); - if(get1bit()) - *xrpnt = -ispow[x] * v; - else - *xrpnt = ispow[x] * v; - } - else if(x) { - max[lwin] = cb; - if(get1bit()) - *xrpnt = -ispow[x] * v; - else - *xrpnt = ispow[x] * v; - part2remain--; - } - else - *xrpnt = 0.0; - xrpnt += step; - if(y == 15) { - max[lwin] = cb; - part2remain -= h->linbits+1; - y += getbits(h->linbits); - if(get1bit()) - *xrpnt = -ispow[y] * v; - else - *xrpnt = ispow[y] * v; - } - else if(y) { - max[lwin] = cb; - if(get1bit()) - *xrpnt = -ispow[y] * v; - else - *xrpnt = ispow[y] * v; - part2remain--; - } - else - *xrpnt = 0.0; - xrpnt += step; - } - } - for(;l3 && (part2remain > 0);l3--) { - const struct newhuff *h = htc+gr_info->count1table_select; - const short *val = h->table; - short a; - - while((a=*val++)<0) { - part2remain--; - if(part2remain < 0) { - part2remain++; - a = 0; - break; - } - if (get1bit()) - val -= a; - } - - for(i=0;i<4;i++) { - if(!(i & 1)) { - if(!mc) { - mc = *m++; - xrpnt = ((real *) xr) + (*m++); - lwin = *m++; - cb = *m++; - if(lwin == 3) { - v = gr_info->pow2gain[(*scf++) << shift]; - step = 1; - } - else { - v = gr_info->full_gain[lwin][(*scf++) << shift]; - step = 3; - } - } - mc--; - } - if( (a & (0x8>>i)) ) { - max[lwin] = cb; - part2remain--; - if(part2remain < 0) { - part2remain++; - break; - } - if(get1bit()) - *xrpnt = -v; - else - *xrpnt = v; - } - else - *xrpnt = 0.0; - xrpnt += step; - } - } - - while( m < me ) { - if(!mc) { - mc = *m++; - xrpnt = ((real *) xr) + *m++; - if( (*m++) == 3) - step = 1; - else - step = 3; - m++; /* cb */ - } - mc--; - *xrpnt = 0.0; - xrpnt += step; - *xrpnt = 0.0; - xrpnt += step; -/* we could add a little opt. here: - * if we finished a band for window 3 or a long band - * further bands could copied in a simple loop without a - * special 'map' decoding - */ - } - - gr_info->maxband[0] = max[0]+1; - gr_info->maxband[1] = max[1]+1; - gr_info->maxband[2] = max[2]+1; - gr_info->maxbandl = max[3]+1; - - { - int rmax = max[0] > max[1] ? max[0] : max[1]; - rmax = (rmax > max[2] ? rmax : max[2]) + 1; - gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1]; - } - - } - else { - /* - * decoding with 'long' BandIndex table (block_type != 2) - */ - const int *pretab = gr_info->preflag ? pretab1 : pretab2; - int i,max = -1; - int cb = 0; - register int *m = map[sfreq][2]; - register real v = 0.0; - register int mc = 0; -#if 0 - me = mapend[sfreq][2]; + Dequantize samples + ...includes Huffman decoding +*/ + +/* 24 is enough because tab13 has max. a 19 bit huffvector */ +#define BITSHIFT ((sizeof(long)-1)*8) +#define REFRESH_MASK \ + while(num < BITSHIFT) { \ + mask |= ((unsigned long)getbyte(fr))<<(BITSHIFT-num); \ + num += 8; \ + part2remain -= 8; } + +static int III_dequantize_sample(mpg123_handle *fr, real xr[SBLIMIT][SSLIMIT],int *scf, struct gr_info_s *gr_info,int sfreq,int part2bits) +{ + int shift = 1 + gr_info->scalefac_scale; + real *xrpnt = (real *) xr; + int l[3],l3; + int part2remain = gr_info->part2_3_length - part2bits; + int *me; +#ifdef REAL_IS_FIXED + int gainpow2_scale_idx = 378; #endif - /* - * long hash table values - */ - for(i=0;i<3;i++) { - int lp = l[i]; - const struct newhuff *h = ht+gr_info->table_select[i]; - - for(;lp;lp--,mc--) { - int x,y; - - if(!mc) { - mc = *m++; - v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift]; - cb = *m++; - } - { - register const short *val = h->table; - while((y=*val++)<0) { - if (get1bit()) - val -= y; - part2remain--; - } - x = y >> 4; - y &= 0xf; - } - if (x == 15) { - max = cb; - part2remain -= h->linbits+1; - x += getbits(h->linbits); - if(get1bit()) - *xrpnt++ = -ispow[x] * v; - else - *xrpnt++ = ispow[x] * v; - } - else if(x) { - max = cb; - if(get1bit()) - *xrpnt++ = -ispow[x] * v; - else - *xrpnt++ = ispow[x] * v; - part2remain--; - } - else - *xrpnt++ = 0.0; - - if (y == 15) { - max = cb; - part2remain -= h->linbits+1; - y += getbits(h->linbits); - if(get1bit()) - *xrpnt++ = -ispow[y] * v; - else - *xrpnt++ = ispow[y] * v; - } - else if(y) { - max = cb; - if(get1bit()) - *xrpnt++ = -ispow[y] * v; - else - *xrpnt++ = ispow[y] * v; - part2remain--; - } - else - *xrpnt++ = 0.0; - } - } - - /* - * short (count1table) values - */ - for(;l3 && (part2remain > 0);l3--) { - const struct newhuff *h = htc+gr_info->count1table_select; - const short *val = h->table; - short a; - - while((a=*val++)<0) { - part2remain--; - if(part2remain < 0) { - part2remain++; - a = 0; - break; - } - if (get1bit()) - val -= a; - } - - for(i=0;i<4;i++) { - if(!(i & 1)) { - if(!mc) { - mc = *m++; - cb = *m++; - v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift]; - } - mc--; - } - if ( (a & (0x8>>i)) ) { - max = cb; - part2remain--; - if(part2remain < 0) { - part2remain++; - break; - } - if(get1bit()) - *xrpnt++ = -v; - else - *xrpnt++ = v; - } - else - *xrpnt++ = 0.0; - } - } - - /* - * zero part - */ - for(i=(&xr[SBLIMIT][0]-xrpnt)>>1;i;i--) { - *xrpnt++ = 0.0; - *xrpnt++ = 0.0; - } - - gr_info->maxbandl = max+1; - gr_info->maxb = longLimit[sfreq][gr_info->maxbandl]; - } - - while( part2remain > 16 ) { - getbits(16); /* Dismiss stuffing Bits */ - part2remain -= 16; - } - if(part2remain > 0) - getbits(part2remain); - else if(part2remain < 0) { - FIXME("mpg123: Can't rewind stream by %d bits!\n",-part2remain); - return 1; /* -> error */ - } - return 0; + /* mhipp tree has this split up a bit... */ + int num=getbitoffset(fr); + long mask; + /* We must split this, because for num==0 the shift is undefined if you do it in one step. */ + mask = ((unsigned long) getbits(fr, num))<big_values; + int region1 = gr_info->region1start; + int region2 = gr_info->region2start; + if(region1 > region2) + { + /* + That's not optimal: it fixes a segfault with fuzzed data, but also apparently triggers where it shouldn't, see bug 1641196. + The benefit of not crashing / having this security risk is bigger than these few frames of a lame-3.70 file that aren't audible anyway. + But still, I want to know if indeed this check or the old lame is at fault. + */ + ERR("You got some really nasty file there... region1>region2!\n"); + return 1; + } + l3 = ((576>>1)-bv)>>1; + + /* we may lose the 'odd' bit here !! check this later again */ + if(bv <= region1) + { + l[0] = bv; + l[1] = 0; + l[2] = 0; + } + else + { + l[0] = region1; + if(bv <= region2) + { + l[1] = bv - l[0]; + l[2] = 0; + } + else + { + l[1] = region2 - l[0]; + l[2] = bv - region2; + } + } + } + + if(gr_info->block_type == 2) + { + /* decoding with short or mixed mode BandIndex table */ + int i,max[4]; + int step=0,lwin=3,cb=0; + register real v = 0.0; + register int *m,mc; + + if(gr_info->mixed_block_flag) + { + max[3] = -1; + max[0] = max[1] = max[2] = 2; + m = map[sfreq][0]; + me = mapend[sfreq][0]; + } + else + { + max[0] = max[1] = max[2] = max[3] = -1; + /* max[3] not really needed in this case */ + m = map[sfreq][1]; + me = mapend[sfreq][1]; + } + + mc = 0; + for(i=0;i<2;i++) + { + int lp = l[i]; + struct newhuff *h = ht+gr_info->table_select[i]; + for(;lp;lp--,mc--) + { + register int x,y; + if( (!mc) ) + { + mc = *m++; + xrpnt = ((real *) xr) + (*m++); + lwin = *m++; + cb = *m++; + if(lwin == 3) + { +#ifdef REAL_IS_FIXED + gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); +#endif + v = gr_info->pow2gain[(*scf++) << shift]; + step = 1; + } + else + { +#ifdef REAL_IS_FIXED + gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2); +#endif + v = gr_info->full_gain[lwin][(*scf++) << shift]; + step = 3; + } + } + { + register short *val = h->table; + REFRESH_MASK; + while((y=*val++)<0) + { + if (mask < 0) val -= y; + + num--; + mask <<= 1; + } + x = y >> 4; + y &= 0xf; + } + if(x == 15 && h->linbits) + { + max[lwin] = cb; + REFRESH_MASK; + x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits); + num -= h->linbits+1; + mask <<= h->linbits; + if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); + else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); + + mask <<= 1; + } + else if(x) + { + max[lwin] = cb; + if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); + else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); + + num--; + mask <<= 1; + } + else *xrpnt = DOUBLE_TO_REAL(0.0); + + xrpnt += step; + if(y == 15 && h->linbits) + { + max[lwin] = cb; + REFRESH_MASK; + y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits); + num -= h->linbits+1; + mask <<= h->linbits; + if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); + else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); + + mask <<= 1; + } + else if(y) + { + max[lwin] = cb; + if(mask < 0) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); + else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); + + num--; + mask <<= 1; + } + else *xrpnt = DOUBLE_TO_REAL(0.0); + + xrpnt += step; + } + } + + for(;l3 && (part2remain+num > 0);l3--) + { + struct newhuff* h; + register short* val; + register short a; + /* + This is only a humble hack to prevent a special segfault. + More insight into the real workings is still needed. + Especially why there are (valid?) files that make xrpnt exceed the array with 4 bytes without segfaulting, more seems to be really bad, though. + */ + #ifdef DEBUG + if(!(xrpnt < &xr[SBLIMIT][0])) + { + if(VERBOSE) TRACE("attempted soft xrpnt overflow (%p !< %p) ?\n", (void*) xrpnt, (void*) &xr[SBLIMIT][0]); + } + #endif + if(!(xrpnt < &xr[SBLIMIT][0]+5)) + { + if(NOQUIET) ERR("attempted xrpnt overflow (%p !< %p)\n", (void*) xrpnt, (void*) &xr[SBLIMIT][0]); + return 2; + } + h = htc+gr_info->count1table_select; + val = h->table; + + REFRESH_MASK; + while((a=*val++)<0) + { + if(mask < 0) val -= a; + + num--; + mask <<= 1; + } + if(part2remain+num <= 0) + { + num -= part2remain+num; + break; + } + + for(i=0;i<4;i++) + { + if(!(i & 1)) + { + if(!mc) + { + mc = *m++; + xrpnt = ((real *) xr) + (*m++); + lwin = *m++; + cb = *m++; + if(lwin == 3) + { +#ifdef REAL_IS_FIXED + gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); +#endif + v = gr_info->pow2gain[(*scf++) << shift]; + step = 1; + } + else + { +#ifdef REAL_IS_FIXED + gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2); +#endif + v = gr_info->full_gain[lwin][(*scf++) << shift]; + step = 3; + } + } + mc--; + } + if( (a & (0x8>>i)) ) + { + max[lwin] = cb; + if(part2remain+num <= 0) + break; + + if(mask < 0) *xrpnt = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx); + else *xrpnt = REAL_SCALE_LAYER3(v, gainpow2_scale_idx); + + num--; + mask <<= 1; + } + else *xrpnt = DOUBLE_TO_REAL(0.0); + + xrpnt += step; + } + } + + if(lwin < 3) + { /* short band? */ + while(1) + { + for(;mc > 0;mc--) + { + *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; /* short band -> step=3 */ + *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; + } + if(m >= me) + break; + + mc = *m++; + xrpnt = ((real *) xr) + *m++; + if(*m++ == 0) + break; /* optimize: field will be set to zero at the end of the function */ + + m++; /* cb */ + } + } + + gr_info->maxband[0] = max[0]+1; + gr_info->maxband[1] = max[1]+1; + gr_info->maxband[2] = max[2]+1; + gr_info->maxbandl = max[3]+1; + + { + int rmax = max[0] > max[1] ? max[0] : max[1]; + rmax = (rmax > max[2] ? rmax : max[2]) + 1; + gr_info->maxb = rmax ? fr->shortLimit[sfreq][rmax] : fr->longLimit[sfreq][max[3]+1]; + } + + } + else + { + /* decoding with 'long' BandIndex table (block_type != 2) */ + const int *pretab = gr_info->preflag ? pretab1 : pretab2; + int i,max = -1; + int cb = 0; + int *m = map[sfreq][2]; + register real v = 0.0; + int mc = 0; + + /* long hash table values */ + for(i=0;i<3;i++) + { + int lp = l[i]; + struct newhuff *h = ht+gr_info->table_select[i]; + + for(;lp;lp--,mc--) + { + int x,y; + if(!mc) + { + mc = *m++; + cb = *m++; +#ifdef CUT_SFB21 + if(cb == 21) + v = 0.0; + else +#endif + { +#ifdef REAL_IS_FIXED + gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); +#endif + v = gr_info->pow2gain[(*(scf++) + (*pretab++)) << shift]; + } + } + { + register short *val = h->table; + REFRESH_MASK; + while((y=*val++)<0) + { + if (mask < 0) val -= y; + + num--; + mask <<= 1; + } + x = y >> 4; + y &= 0xf; + } + + if(x == 15 && h->linbits) + { + max = cb; + REFRESH_MASK; + x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits); + num -= h->linbits+1; + mask <<= h->linbits; + if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); + else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); + + mask <<= 1; + } + else if(x) + { + max = cb; + if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx); + else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx); + num--; + + mask <<= 1; + } + else *xrpnt++ = DOUBLE_TO_REAL(0.0); + + if(y == 15 && h->linbits) + { + max = cb; + REFRESH_MASK; + y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits); + num -= h->linbits+1; + mask <<= h->linbits; + if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); + else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); + + mask <<= 1; + } + else if(y) + { + max = cb; + if(mask < 0) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx); + else *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx); + + num--; + mask <<= 1; + } + else *xrpnt++ = DOUBLE_TO_REAL(0.0); + } + } + + /* short (count1table) values */ + for(;l3 && (part2remain+num > 0);l3--) + { + struct newhuff *h = htc+gr_info->count1table_select; + register short *val = h->table,a; + + REFRESH_MASK; + while((a=*val++)<0) + { + if (mask < 0) val -= a; + + num--; + mask <<= 1; + } + if(part2remain+num <= 0) + { + num -= part2remain+num; + break; + } + + for(i=0;i<4;i++) + { + if(!(i & 1)) + { + if(!mc) + { + mc = *m++; + cb = *m++; +#ifdef CUT_SFB21 + if(cb == 21) + v = 0.0; + else +#endif + { +#ifdef REAL_IS_FIXED + gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2); +#endif + v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift]; + } + } + mc--; + } + if( (a & (0x8>>i)) ) + { + max = cb; + if(part2remain+num <= 0) + break; + + if(mask < 0) *xrpnt++ = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx); + else *xrpnt++ = REAL_SCALE_LAYER3(v, gainpow2_scale_idx); + + num--; + mask <<= 1; + } + else *xrpnt++ = DOUBLE_TO_REAL(0.0); + } + } + + gr_info->maxbandl = max+1; + gr_info->maxb = fr->longLimit[sfreq][gr_info->maxbandl]; + } + + part2remain += num; + backbits(fr, num); + num = 0; + + while(xrpnt < &xr[SBLIMIT][0]) + *xrpnt++ = DOUBLE_TO_REAL(0.0); + + while( part2remain > 16 ) + { + skipbits(fr, 16); /* Dismiss stuffing Bits */ + part2remain -= 16; + } + if(part2remain > 0) skipbits(fr, part2remain); + else if(part2remain < 0) + { + TRACE("Can't rewind stream by %d bits!\n",-part2remain); + return 1; /* -> error */ + } + return 0; } -/* - * III_stereo: calculate real channel values for Joint-I-Stereo-mode - */ -static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac, - struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf) + +/* calculate real channel values for Joint-I-Stereo-mode */ +static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac, struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf) { - real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf; - const struct bandInfoStruct *bi = &bandInfo[sfreq]; - real *tab1,*tab2; - - if(lsf) { - int p = gr_info->scalefac_compress & 0x1; - if(ms_stereo) { - tab1 = pow1_2[p]; tab2 = pow2_2[p]; - } - else { - tab1 = pow1_1[p]; tab2 = pow2_1[p]; - } - } - else { - if(ms_stereo) { - tab1 = tan1_2; tab2 = tan2_2; - } - else { - tab1 = tan1_1; tab2 = tan2_1; - } - } - - if (gr_info->block_type == 2) - { - int lwin,do_l = 0; - if( gr_info->mixed_block_flag ) - do_l = 1; - - for (lwin=0;lwin<3;lwin++) /* process each window */ - { - /* get first band with zero values */ - int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */ - if(sfb > 3) - do_l = 0; - - for(;sfb<12;sfb++) - { - is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ - if(is_p != 7) { - real t1,t2; - sb = bi->shortDiff[sfb]; - idx = bi->shortIdx[sfb] + lwin; - t1 = tab1[is_p]; t2 = tab2[is_p]; - for (; sb > 0; sb--,idx+=3) - { - real v = xr[0][idx]; - xr[0][idx] = v * t1; - xr[1][idx] = v * t2; - } - } - } + real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf; + const struct bandInfoStruct *bi = &bandInfo[sfreq]; + + const real *tab1,*tab2; + +#if 1 + int tab; +/* TODO: optimize as static */ + const real *tabs[3][2][2] = + { + { { tan1_1,tan2_1 } , { tan1_2,tan2_2 } }, + { { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } }, + { { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } } + }; + + tab = lsf + (gr_info->scalefac_compress & lsf); + tab1 = tabs[tab][ms_stereo][0]; + tab2 = tabs[tab][ms_stereo][1]; +#else + if(lsf) + { + int p = gr_info->scalefac_compress & 0x1; + if(ms_stereo) + { + tab1 = pow1_2[p]; + tab2 = pow2_2[p]; + } + else + { + tab1 = pow1_1[p]; + tab2 = pow2_1[p]; + } + } + else + { + if(ms_stereo) + { + tab1 = tan1_2; + tab2 = tan2_2; + } + else + { + tab1 = tan1_1; + tab2 = tan2_1; + } + } +#endif + + if(gr_info->block_type == 2) + { + int lwin,do_l = 0; + if( gr_info->mixed_block_flag ) do_l = 1; + + for(lwin=0;lwin<3;lwin++) + { /* process each window */ + /* get first band with zero values */ + int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */ + if(sfb > 3) do_l = 0; + + for(;sfb<12;sfb++) + { + is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ + if(is_p != 7) + { + real t1,t2; + sb = bi->shortDiff[sfb]; + idx = bi->shortIdx[sfb] + lwin; + t1 = tab1[is_p]; t2 = tab2[is_p]; + for (; sb > 0; sb--,idx+=3) + { + real v = xr[0][idx]; + xr[0][idx] = REAL_MUL_15(v, t1); + xr[1][idx] = REAL_MUL_15(v, t2); + } + } + } #if 1 /* in the original: copy 10 to 11 , here: copy 11 to 12 maybe still wrong??? (copy 12 to 13?) */ - is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ - sb = bi->shortDiff[12]; - idx = bi->shortIdx[12] + lwin; + is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ + sb = bi->shortDiff[12]; + idx = bi->shortIdx[12] + lwin; #else - is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ - sb = bi->shortDiff[11]; - idx = bi->shortIdx[11] + lwin; + is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ + sb = bi->shortDiff[11]; + idx = bi->shortIdx[11] + lwin; #endif - if(is_p != 7) - { - real t1,t2; - t1 = tab1[is_p]; t2 = tab2[is_p]; - for ( ; sb > 0; sb--,idx+=3 ) - { - real v = xr[0][idx]; - xr[0][idx] = v * t1; - xr[1][idx] = v * t2; - } - } - } /* end for(lwin; .. ; . ) */ - - if (do_l) - { -/* also check l-part, if ALL bands in the three windows are 'empty' - * and mode = mixed_mode - */ - int sfb = gr_info->maxbandl; - int idx; - if(sfb > 21) return; /* similarity fix related to CVE-2006-1655 */ - idx = bi->longIdx[sfb]; - for ( ; sfb<8; sfb++ ) - { - int sb = bi->longDiff[sfb]; - int is_p = scalefac[sfb]; /* scale: 0-15 */ - if(is_p != 7) { - real t1,t2; - t1 = tab1[is_p]; t2 = tab2[is_p]; - for ( ; sb > 0; sb--,idx++) - { - real v = xr[0][idx]; - xr[0][idx] = v * t1; - xr[1][idx] = v * t2; - } - } - else - idx += sb; - } - } - } - else /* ((gr_info->block_type != 2)) */ - { - int sfb = gr_info->maxbandl; - int is_p,idx; - if (sfb > 21) return; /* tightened fix for CVE-2006-1655 */ - idx = bi->longIdx[sfb]; - for ( ; sfb<21; sfb++) - { - int sb = bi->longDiff[sfb]; - is_p = scalefac[sfb]; /* scale: 0-15 */ - if(is_p != 7) { - real t1,t2; - t1 = tab1[is_p]; t2 = tab2[is_p]; - for ( ; sb > 0; sb--,idx++) - { - real v = xr[0][idx]; - xr[0][idx] = v * t1; - xr[1][idx] = v * t2; - } - } - else - idx += sb; - } - - is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */ - if(is_p != 7) - { - int sb; - real t1 = tab1[is_p],t2 = tab2[is_p]; - - for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ ) - { - real v = xr[0][idx]; - xr[0][idx] = v * t1; - xr[1][idx] = v * t2; - } - } - } /* ... */ + if(is_p != 7) + { + real t1,t2; + t1 = tab1[is_p]; t2 = tab2[is_p]; + for( ; sb > 0; sb--,idx+=3 ) + { + real v = xr[0][idx]; + xr[0][idx] = REAL_MUL_15(v, t1); + xr[1][idx] = REAL_MUL_15(v, t2); + } + } + } /* end for(lwin; .. ; . ) */ + + /* also check l-part, if ALL bands in the three windows are 'empty' and mode = mixed_mode */ + if(do_l) + { + int sfb = gr_info->maxbandl; + int idx; + if(sfb > 21) return; /* similarity fix related to CVE-2006-1655 */ + + idx = bi->longIdx[sfb]; + for( ; sfb<8; sfb++ ) + { + int sb = bi->longDiff[sfb]; + int is_p = scalefac[sfb]; /* scale: 0-15 */ + if(is_p != 7) + { + real t1,t2; + t1 = tab1[is_p]; t2 = tab2[is_p]; + for( ; sb > 0; sb--,idx++) + { + real v = xr[0][idx]; + xr[0][idx] = REAL_MUL_15(v, t1); + xr[1][idx] = REAL_MUL_15(v, t2); + } + } + else idx += sb; + } + } + } + else + { /* ((gr_info->block_type != 2)) */ + int sfb = gr_info->maxbandl; + int is_p,idx; + if(sfb > 21) return; /* tightened fix for CVE-2006-1655 */ + + idx = bi->longIdx[sfb]; + for ( ; sfb<21; sfb++) + { + int sb = bi->longDiff[sfb]; + is_p = scalefac[sfb]; /* scale: 0-15 */ + if(is_p != 7) + { + real t1,t2; + t1 = tab1[is_p]; t2 = tab2[is_p]; + for( ; sb > 0; sb--,idx++) + { + real v = xr[0][idx]; + xr[0][idx] = REAL_MUL_15(v, t1); + xr[1][idx] = REAL_MUL_15(v, t2); + } + } + else idx += sb; + } + + is_p = scalefac[20]; + if(is_p != 7) + { /* copy l-band 20 to l-band 21 */ + int sb; + real t1 = tab1[is_p],t2 = tab2[is_p]; + + for( sb = bi->longDiff[21]; sb > 0; sb--,idx++ ) + { + real v = xr[0][idx]; + xr[0][idx] = REAL_MUL_15(v, t1); + xr[1][idx] = REAL_MUL_15(v, t2); + } + } + } } + static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info) { - int sblim; - - if(gr_info->block_type == 2) - { - if(!gr_info->mixed_block_flag) - return; - sblim = 1; - } - else { - sblim = gr_info->maxb-1; - } - - /* 31 alias-reduction operations between each pair of sub-bands */ - /* with 8 butterflies between each pair */ - - { - int sb; - real *xr1=(real *) xr[1]; - - for(sb=sblim;sb;sb--,xr1+=10) - { - int ss; - real *cs=aa_cs,*ca=aa_ca; - real *xr2 = xr1; - - for(ss=7;ss>=0;ss--) - { /* upper and lower butterfly inputs */ - register real bu = *--xr2,bd = *xr1; - *xr2 = (bu * (*cs) ) - (bd * (*ca) ); - *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) ); - } - } - } + int sblim; + + if(gr_info->block_type == 2) + { + if(!gr_info->mixed_block_flag) return; + + sblim = 1; + } + else sblim = gr_info->maxb-1; + + /* 31 alias-reduction operations between each pair of sub-bands */ + /* with 8 butterflies between each pair */ + + { + int sb; + real *xr1=(real *) xr[1]; + + for(sb=sblim; sb; sb--,xr1+=10) + { + int ss; + real *cs=aa_cs,*ca=aa_ca; + real *xr2 = xr1; + + for(ss=7;ss>=0;ss--) + { /* upper and lower butterfly inputs */ + register real bu = *--xr2,bd = *xr1; + *xr2 = REAL_MUL(bu, *cs) - REAL_MUL(bd, *ca); + *xr1++ = REAL_MUL(bd, *cs++) + REAL_MUL(bu, *ca++); + } + } + } } /* - DCT inspired by Jeff Tsay's DCT from the maplay package - this is an optimized version with manual unroll. + This is an optimized DCT from Jeff Tsay's maplay 1.2+ package. + Saved one multiplication by doing the 'twiddle factor' stuff + together with the window mul. (MH) + + This uses Byeong Gi Lee's Fast Cosine Transform algorithm, but the + 9 point IDCT needs to be reduced further. Unfortunately, I don't + know how to do that, because 9 is not an even number. - Jeff. + + Original Message: - References: - [1] S. Winograd: "On Computing the Discrete Fourier Transform", - Mathematics of Computation, Volume 32, Number 141, January 1978, - Pages 175-199 + 9 Point Inverse Discrete Cosine Transform + + This piece of code is Copyright 1997 Mikko Tommila and is freely usable + by anybody. The algorithm itself is of course in the public domain. + + Again derived heuristically from the 9-point WFTA. + + The algorithm is optimized (?) for speed, not for small rounding errors or + good readability. + + 36 additions, 11 multiplications + + Again this is very likely sub-optimal. + + The code is optimized to use a minimum number of temporary variables, + so it should compile quite well even on 8-register Intel x86 processors. + This makes the code quite obfuscated and very difficult to understand. + + References: + [1] S. Winograd: "On Computing the Discrete Fourier Transform", + Mathematics of Computation, Volume 32, Number 141, January 1978, + Pages 175-199 */ -static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf) +/* Calculation of the inverse MDCT + used to be static without 3dnow - does that really matter? */ +void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf) { - { - register real *in = inbuf; +#ifdef NEW_DCT9 + real tmp[18]; +#endif - in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14]; - in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11]; - in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8]; - in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5]; - in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2]; - in[2] +=in[1]; in[1] +=in[0]; + { + register real *in = inbuf; + + in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14]; + in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11]; + in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8]; + in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5]; + in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2]; + in[2] +=in[1]; in[1] +=in[0]; + + in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9]; + in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1]; + + +#ifdef NEW_DCT9 +#if 1 + { + real t3; + { + real t0, t1, t2; + + t0 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4])); + t1 = REAL_MUL(COS6_2, in[12]); + + t3 = in[0]; + t2 = t3 - t1 - t1; + tmp[1] = tmp[7] = t2 - t0; + tmp[4] = t2 + t0 + t0; + t3 += t1; + + t2 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2])); + tmp[1] -= t2; + tmp[7] += t2; + } + { + real t0, t1, t2; + + t0 = REAL_MUL(cos9[0], (in[4] + in[8] )); + t1 = REAL_MUL(cos9[1], (in[8] - in[16])); + t2 = REAL_MUL(cos9[2], (in[4] + in[16])); + + tmp[2] = tmp[6] = t3 - t0 - t2; + tmp[0] = tmp[8] = t3 + t0 + t1; + tmp[3] = tmp[5] = t3 - t1 + t2; + } + } + { + real t1, t2, t3; + + t1 = REAL_MUL(cos18[0], (in[2] + in[10])); + t2 = REAL_MUL(cos18[1], (in[10] - in[14])); + t3 = REAL_MUL(COS6_1, in[6]); + + { + real t0 = t1 + t2 + t3; + tmp[0] += t0; + tmp[8] -= t0; + } + + t2 -= t3; + t1 -= t3; + + t3 = REAL_MUL(cos18[2], (in[2] + in[14])); + + t1 += t3; + tmp[3] += t1; + tmp[5] -= t1; + + t2 -= t3; + tmp[2] += t2; + tmp[6] -= t2; + } + +#else + { + real t0, t1, t2, t3, t4, t5, t6, t7; - in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9]; - in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1]; + t1 = REAL_MUL(COS6_2, in[12]); + t2 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4])); + t3 = in[0] + t1; + t4 = in[0] - t1 - t1; + t5 = t4 - t2; + tmp[4] = t4 + t2 + t2; - { + t0 = REAL_MUL(cos9[0], (in[4] + in[8])); + t1 = REAL_MUL(cos9[1], (in[8] - in[16])); + + t2 = REAL_MUL(cos9[2], (in[4] + in[16])); + + t6 = t3 - t0 - t2; + t0 += t3 + t1; + t3 += t2 - t1; + + t2 = REAL_MUL(cos18[0], (in[2] + in[10])); + t4 = REAL_MUL(cos18[1], (in[10] - in[14])); + t7 = REAL_MUL(COS6_1, in[6]); + + t1 = t2 + t4 + t7; + tmp[0] = t0 + t1; + tmp[8] = t0 - t1; + t1 = REAL_MUL(cos18[2], (in[2] + in[14])); + t2 += t1 - t7; + + tmp[3] = t3 + t2; + t0 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2])); + tmp[5] = t3 - t2; + + t4 -= t1 + t7; + + tmp[1] = t5 - t0; + tmp[7] = t5 + t0; + tmp[2] = t6 + t4; + tmp[6] = t6 - t4; + } +#endif + + { + real t0, t1, t2, t3, t4, t5, t6, t7; + + t1 = REAL_MUL(COS6_2, in[13]); + t2 = REAL_MUL(COS6_2, (in[9] + in[17] - in[5])); + + t3 = in[1] + t1; + t4 = in[1] - t1 - t1; + t5 = t4 - t2; + + t0 = REAL_MUL(cos9[0], (in[5] + in[9])); + t1 = REAL_MUL(cos9[1], (in[9] - in[17])); + + tmp[13] = REAL_MUL((t4 + t2 + t2), tfcos36[17-13]); + t2 = REAL_MUL(cos9[2], (in[5] + in[17])); + + t6 = t3 - t0 - t2; + t0 += t3 + t1; + t3 += t2 - t1; + + t2 = REAL_MUL(cos18[0], (in[3] + in[11])); + t4 = REAL_MUL(cos18[1], (in[11] - in[15])); + t7 = REAL_MUL(COS6_1, in[7]); + + t1 = t2 + t4 + t7; + tmp[17] = REAL_MUL((t0 + t1), tfcos36[17-17]); + tmp[9] = REAL_MUL((t0 - t1), tfcos36[17-9]); + t1 = REAL_MUL(cos18[2], (in[3] + in[15])); + t2 += t1 - t7; + + tmp[14] = REAL_MUL((t3 + t2), tfcos36[17-14]); + t0 = REAL_MUL(COS6_1, (in[11] + in[15] - in[3])); + tmp[12] = REAL_MUL((t3 - t2), tfcos36[17-12]); + + t4 -= t1 + t7; + + tmp[16] = REAL_MUL((t5 - t0), tfcos36[17-16]); + tmp[10] = REAL_MUL((t5 + t0), tfcos36[17-10]); + tmp[15] = REAL_MUL((t6 + t4), tfcos36[17-15]); + tmp[11] = REAL_MUL((t6 - t4), tfcos36[17-11]); + } + +#define MACRO(v) { \ + real tmpval; \ + tmpval = tmp[(v)] + tmp[17-(v)]; \ + out2[9+(v)] = REAL_MUL(tmpval, w[27+(v)]); \ + out2[8-(v)] = REAL_MUL(tmpval, w[26-(v)]); \ + tmpval = tmp[(v)] - tmp[17-(v)]; \ + ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(tmpval, w[8-(v)]); \ + ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(tmpval, w[9+(v)]); } + + { + register real *out2 = o2; + register real *w = wintab; + register real *out1 = o1; + register real *ts = tsbuf; + + MACRO(0); + MACRO(1); + MACRO(2); + MACRO(3); + MACRO(4); + MACRO(5); + MACRO(6); + MACRO(7); + MACRO(8); + } + +#else + + { #define MACRO0(v) { \ - real tmp; \ - out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \ - out2[8-(v)] = tmp * w[26-(v)]; } \ - sum0 -= sum1; \ - ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \ - ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)]; + real tmp; \ + out2[9+(v)] = REAL_MUL((tmp = sum0 + sum1), w[27+(v)]); \ + out2[8-(v)] = REAL_MUL(tmp, w[26-(v)]); } \ + sum0 -= sum1; \ + ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(sum0, w[8-(v)]); \ + ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(sum0, w[9+(v)]); #define MACRO1(v) { \ real sum0,sum1; \ - sum0 = tmp1a + tmp2a; \ - sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \ + sum0 = tmp1a + tmp2a; \ + sum1 = REAL_MUL((tmp1b + tmp2b), tfcos36[(v)]); \ MACRO0(v); } #define MACRO2(v) { \ - real sum0,sum1; \ - sum0 = tmp2a - tmp1a; \ - sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \ + real sum0,sum1; \ + sum0 = tmp2a - tmp1a; \ + sum1 = REAL_MUL((tmp2b - tmp1b), tfcos36[(v)]); \ MACRO0(v); } - register const real *c = COS9; - register real *out2 = o2; - register real *w = wintab; - register real *out1 = o1; - register real *ts = tsbuf; - - real ta33,ta66,tb33,tb66; - - ta33 = in[2*3+0] * c[3]; - ta66 = in[2*6+0] * c[6]; - tb33 = in[2*3+1] * c[3]; - tb66 = in[2*6+1] * c[6]; - - { - real tmp1a,tmp2a,tmp1b,tmp2b; - tmp1a = in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7]; - tmp1b = in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7]; - tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8]; - tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8]; - - MACRO1(0); - MACRO2(8); - } - - { - real tmp1a,tmp2a,tmp1b,tmp2b; - tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3]; - tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3]; - tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0]; - tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1]; - - MACRO1(1); - MACRO2(7); - } - - { - real tmp1a,tmp2a,tmp1b,tmp2b; - tmp1a = in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1]; - tmp1b = in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1]; - tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4]; - tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4]; - - MACRO1(2); - MACRO2(6); - } - - { - real tmp1a,tmp2a,tmp1b,tmp2b; - tmp1a = in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5]; - tmp1b = in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5]; - tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2]; - tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2]; - - MACRO1(3); - MACRO2(5); - } + register const real *c = COS9; + register real *out2 = o2; + register real *w = wintab; + register real *out1 = o1; + register real *ts = tsbuf; + + real ta33,ta66,tb33,tb66; + + ta33 = REAL_MUL(in[2*3+0], c[3]); + ta66 = REAL_MUL(in[2*6+0], c[6]); + tb33 = REAL_MUL(in[2*3+1], c[3]); + tb66 = REAL_MUL(in[2*6+1], c[6]); + + { + real tmp1a,tmp2a,tmp1b,tmp2b; + tmp1a = REAL_MUL(in[2*1+0], c[1]) + ta33 + REAL_MUL(in[2*5+0], c[5]) + REAL_MUL(in[2*7+0], c[7]); + tmp1b = REAL_MUL(in[2*1+1], c[1]) + tb33 + REAL_MUL(in[2*5+1], c[5]) + REAL_MUL(in[2*7+1], c[7]); + tmp2a = REAL_MUL(in[2*2+0], c[2]) + REAL_MUL(in[2*4+0], c[4]) + ta66 + REAL_MUL(in[2*8+0], c[8]); + tmp2b = REAL_MUL(in[2*2+1], c[2]) + REAL_MUL(in[2*4+1], c[4]) + tb66 + REAL_MUL(in[2*8+1], c[8]); + + MACRO1(0); + MACRO2(8); + } + + { + real tmp1a,tmp2a,tmp1b,tmp2b; + tmp1a = REAL_MUL(( in[2*1+0] - in[2*5+0] - in[2*7+0] ), c[3]); + tmp1b = REAL_MUL(( in[2*1+1] - in[2*5+1] - in[2*7+1] ), c[3]); + tmp2a = REAL_MUL(( in[2*2+0] - in[2*4+0] - in[2*8+0] ), c[6]) - in[2*6+0] + in[2*0+0]; + tmp2b = REAL_MUL(( in[2*2+1] - in[2*4+1] - in[2*8+1] ), c[6]) - in[2*6+1] + in[2*0+1]; + + MACRO1(1); + MACRO2(7); + } + + { + real tmp1a,tmp2a,tmp1b,tmp2b; + tmp1a = REAL_MUL(in[2*1+0], c[5]) - ta33 - REAL_MUL(in[2*5+0], c[7]) + REAL_MUL(in[2*7+0], c[1]); + tmp1b = REAL_MUL(in[2*1+1], c[5]) - tb33 - REAL_MUL(in[2*5+1], c[7]) + REAL_MUL(in[2*7+1], c[1]); + tmp2a = - REAL_MUL(in[2*2+0], c[8]) - REAL_MUL(in[2*4+0], c[2]) + ta66 + REAL_MUL(in[2*8+0], c[4]); + tmp2b = - REAL_MUL(in[2*2+1], c[8]) - REAL_MUL(in[2*4+1], c[2]) + tb66 + REAL_MUL(in[2*8+1], c[4]); + + MACRO1(2); + MACRO2(6); + } + + { + real tmp1a,tmp2a,tmp1b,tmp2b; + tmp1a = REAL_MUL(in[2*1+0], c[7]) - ta33 + REAL_MUL(in[2*5+0], c[1]) - REAL_MUL(in[2*7+0], c[5]); + tmp1b = REAL_MUL(in[2*1+1], c[7]) - tb33 + REAL_MUL(in[2*5+1], c[1]) - REAL_MUL(in[2*7+1], c[5]); + tmp2a = - REAL_MUL(in[2*2+0], c[4]) + REAL_MUL(in[2*4+0], c[8]) + ta66 - REAL_MUL(in[2*8+0], c[2]); + tmp2b = - REAL_MUL(in[2*2+1], c[4]) + REAL_MUL(in[2*4+1], c[8]) + tb66 - REAL_MUL(in[2*8+1], c[2]); + + MACRO1(3); + MACRO2(5); + } + + { + real sum0,sum1; + sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0]; + sum1 = REAL_MUL((in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ), tfcos36[4]); + MACRO0(4); + } + } +#endif - { - real sum0,sum1; - sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0]; - sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4]; - MACRO0(4); } - } - - } } -/* - * new DCT12 - */ + +/* new DCT12 */ static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts) { #define DCT12_PART1 \ - in5 = in[5*3]; \ - in5 += (in4 = in[4*3]); \ - in4 += (in3 = in[3*3]); \ - in3 += (in2 = in[2*3]); \ - in2 += (in1 = in[1*3]); \ - in1 += (in0 = in[0*3]); \ - \ - in5 += in3; in3 += in1; \ - \ - in2 *= COS6_1; \ - in3 *= COS6_1; \ + in5 = in[5*3]; \ + in5 += (in4 = in[4*3]); \ + in4 += (in3 = in[3*3]); \ + in3 += (in2 = in[2*3]); \ + in2 += (in1 = in[1*3]); \ + in1 += (in0 = in[0*3]); \ + \ + in5 += in3; in3 += in1; \ + \ + in2 = REAL_MUL(in2, COS6_1); \ + in3 = REAL_MUL(in3, COS6_1); #define DCT12_PART2 \ - in0 += in4 * COS6_2; \ - \ - in4 = in0 + in2; \ - in0 -= in2; \ - \ - in1 += in5 * COS6_2; \ - \ - in5 = (in1 + in3) * tfcos12[0]; \ - in1 = (in1 - in3) * tfcos12[2]; \ - \ - in3 = in4 + in5; \ - in4 -= in5; \ - \ - in2 = in0 + in1; \ - in0 -= in1; - - - { - real in0,in1,in2,in3,in4,in5; - register real *out1 = rawout1; - ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2]; - ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5]; - - DCT12_PART1 - - { - real tmp0,tmp1 = (in0 - in4); - { - real tmp2 = (in1 - in5) * tfcos12[1]; - tmp0 = tmp1 + tmp2; - tmp1 -= tmp2; - } - ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1]; - ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1]; - ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1]; - ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1]; - } - - DCT12_PART2 - - ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0]; - ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0]; - ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2]; - ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2]; - - ts[(6+0)*SBLIMIT] = out1[6+0] + in0 * wi[0]; - ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0]; - ts[(6+2)*SBLIMIT] = out1[6+2] + in4 * wi[2]; - ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2]; - } - - in++; - - { - real in0,in1,in2,in3,in4,in5; - register real *out2 = rawout2; - - DCT12_PART1 - - { - real tmp0,tmp1 = (in0 - in4); - { - real tmp2 = (in1 - in5) * tfcos12[1]; - tmp0 = tmp1 + tmp2; - tmp1 -= tmp2; - } - out2[5-1] = tmp0 * wi[11-1]; - out2[0+1] = tmp0 * wi[6+1]; - ts[(12+1)*SBLIMIT] += tmp1 * wi[1]; - ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1]; - } - - DCT12_PART2 - - out2[5-0] = in2 * wi[11-0]; - out2[0+0] = in2 * wi[6+0]; - out2[0+2] = in3 * wi[6+2]; - out2[5-2] = in3 * wi[11-2]; - - ts[(12+0)*SBLIMIT] += in0 * wi[0]; - ts[(17-0)*SBLIMIT] += in0 * wi[5-0]; - ts[(12+2)*SBLIMIT] += in4 * wi[2]; - ts[(17-2)*SBLIMIT] += in4 * wi[5-2]; - } - - in++; - - { - real in0,in1,in2,in3,in4,in5; - register real *out2 = rawout2; - out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0; - - DCT12_PART1 - - { - real tmp0,tmp1 = (in0 - in4); - { - real tmp2 = (in1 - in5) * tfcos12[1]; - tmp0 = tmp1 + tmp2; - tmp1 -= tmp2; - } - out2[11-1] = tmp0 * wi[11-1]; - out2[6 +1] = tmp0 * wi[6+1]; - out2[0+1] += tmp1 * wi[1]; - out2[5-1] += tmp1 * wi[5-1]; - } - - DCT12_PART2 - - out2[11-0] = in2 * wi[11-0]; - out2[6 +0] = in2 * wi[6+0]; - out2[6 +2] = in3 * wi[6+2]; - out2[11-2] = in3 * wi[11-2]; - - out2[0+0] += in0 * wi[0]; - out2[5-0] += in0 * wi[5-0]; - out2[0+2] += in4 * wi[2]; - out2[5-2] += in4 * wi[5-2]; - } + in0 += REAL_MUL(in4, COS6_2); \ + \ + in4 = in0 + in2; \ + in0 -= in2; \ + \ + in1 += REAL_MUL(in5, COS6_2); \ + \ + in5 = REAL_MUL((in1 + in3), tfcos12[0]); \ + in1 = REAL_MUL((in1 - in3), tfcos12[2]); \ + \ + in3 = in4 + in5; \ + in4 -= in5; \ + \ + in2 = in0 + in1; \ + in0 -= in1; + + { + real in0,in1,in2,in3,in4,in5; + register real *out1 = rawout1; + ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2]; + ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5]; + + DCT12_PART1 + + { + real tmp0,tmp1 = (in0 - in4); + { + real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]); + tmp0 = tmp1 + tmp2; + tmp1 -= tmp2; + } + ts[(17-1)*SBLIMIT] = out1[17-1] + REAL_MUL(tmp0, wi[11-1]); + ts[(12+1)*SBLIMIT] = out1[12+1] + REAL_MUL(tmp0, wi[6+1]); + ts[(6 +1)*SBLIMIT] = out1[6 +1] + REAL_MUL(tmp1, wi[1]); + ts[(11-1)*SBLIMIT] = out1[11-1] + REAL_MUL(tmp1, wi[5-1]); + } + + DCT12_PART2 + + ts[(17-0)*SBLIMIT] = out1[17-0] + REAL_MUL(in2, wi[11-0]); + ts[(12+0)*SBLIMIT] = out1[12+0] + REAL_MUL(in2, wi[6+0]); + ts[(12+2)*SBLIMIT] = out1[12+2] + REAL_MUL(in3, wi[6+2]); + ts[(17-2)*SBLIMIT] = out1[17-2] + REAL_MUL(in3, wi[11-2]); + + ts[(6 +0)*SBLIMIT] = out1[6+0] + REAL_MUL(in0, wi[0]); + ts[(11-0)*SBLIMIT] = out1[11-0] + REAL_MUL(in0, wi[5-0]); + ts[(6 +2)*SBLIMIT] = out1[6+2] + REAL_MUL(in4, wi[2]); + ts[(11-2)*SBLIMIT] = out1[11-2] + REAL_MUL(in4, wi[5-2]); + } + + in++; + + { + real in0,in1,in2,in3,in4,in5; + register real *out2 = rawout2; + + DCT12_PART1 + + { + real tmp0,tmp1 = (in0 - in4); + { + real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]); + tmp0 = tmp1 + tmp2; + tmp1 -= tmp2; + } + out2[5-1] = REAL_MUL(tmp0, wi[11-1]); + out2[0+1] = REAL_MUL(tmp0, wi[6+1]); + ts[(12+1)*SBLIMIT] += REAL_MUL(tmp1, wi[1]); + ts[(17-1)*SBLIMIT] += REAL_MUL(tmp1, wi[5-1]); + } + + DCT12_PART2 + + out2[5-0] = REAL_MUL(in2, wi[11-0]); + out2[0+0] = REAL_MUL(in2, wi[6+0]); + out2[0+2] = REAL_MUL(in3, wi[6+2]); + out2[5-2] = REAL_MUL(in3, wi[11-2]); + + ts[(12+0)*SBLIMIT] += REAL_MUL(in0, wi[0]); + ts[(17-0)*SBLIMIT] += REAL_MUL(in0, wi[5-0]); + ts[(12+2)*SBLIMIT] += REAL_MUL(in4, wi[2]); + ts[(17-2)*SBLIMIT] += REAL_MUL(in4, wi[5-2]); + } + + in++; + + { + real in0,in1,in2,in3,in4,in5; + register real *out2 = rawout2; + out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0; + + DCT12_PART1 + + { + real tmp0,tmp1 = (in0 - in4); + { + real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]); + tmp0 = tmp1 + tmp2; + tmp1 -= tmp2; + } + out2[11-1] = REAL_MUL(tmp0, wi[11-1]); + out2[6 +1] = REAL_MUL(tmp0, wi[6+1]); + out2[0+1] += REAL_MUL(tmp1, wi[1]); + out2[5-1] += REAL_MUL(tmp1, wi[5-1]); + } + + DCT12_PART2 + + out2[11-0] = REAL_MUL(in2, wi[11-0]); + out2[6 +0] = REAL_MUL(in2, wi[6+0]); + out2[6 +2] = REAL_MUL(in3, wi[6+2]); + out2[11-2] = REAL_MUL(in3, wi[11-2]); + + out2[0+0] += REAL_MUL(in0, wi[0]); + out2[5-0] += REAL_MUL(in0, wi[5-0]); + out2[0+2] += REAL_MUL(in4, wi[2]); + out2[5-2] += REAL_MUL(in4, wi[5-2]); + } } -/* - * III_hybrid - */ -static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT], - int ch,struct gr_info_s *gr_info,struct mpstr *mp) + +static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT], int ch,struct gr_info_s *gr_info, mpg123_handle *fr) { - real *tspnt = (real *) tsOut; - real (*block)[2][SBLIMIT*SSLIMIT] = mp->hybrid_block; - int *blc = mp->hybrid_blc; - real *rawout1,*rawout2; - int bt; - unsigned int sb = 0; - - { - int b = blc[ch]; - rawout1=block[b][ch]; - b=-b+1; - rawout2=block[b][ch]; - blc[ch] = b; - } - - - if(gr_info->mixed_block_flag) { - sb = 2; - dct36(fsIn[0],rawout1,rawout2,win[0],tspnt); - dct36(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1); - rawout1 += 36; rawout2 += 36; tspnt += 2; - } - - bt = gr_info->block_type; - if(bt == 2) { - for (; sbmaxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) { - dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt); - dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1); - } - } - else { - for (; sbmaxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) { - dct36(fsIn[sb],rawout1,rawout2,win[bt],tspnt); - dct36(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1); - } - } - - for(;sbhybrid_block; + int *blc = fr->hybrid_blc; + + real *tspnt = (real *) tsOut; + real *rawout1,*rawout2; + int bt = 0; + size_t sb = 0; + + { + int b = blc[ch]; + rawout1=block[b][ch]; + b=-b+1; + rawout2=block[b][ch]; + blc[ch] = b; + } + + if(gr_info->mixed_block_flag) + { + sb = 2; + opt_dct36(fr)(fsIn[0],rawout1,rawout2,win[0],tspnt); + opt_dct36(fr)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1); + rawout1 += 36; rawout2 += 36; tspnt += 2; + } + + bt = gr_info->block_type; + if(bt == 2) + { + for(; sbmaxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) + { + dct12(fsIn[sb] ,rawout1 ,rawout2 ,win[2] ,tspnt); + dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1); + } + } + else + { + for(; sbmaxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) + { + opt_dct36(fr)(fsIn[sb],rawout1,rawout2,win[bt],tspnt); + opt_dct36(fr)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1); + } + } + + for(;sbstereo; - int single = fr->single; - int ms_stereo,i_stereo; - int sfreq = fr->sampling_frequency; - int stereo1,granules; - - if(stereo == 1) { /* stream is mono */ - stereo1 = 1; - single = 0; - } - else if(single >= 0) /* stream is stereo, but force to mono */ - stereo1 = 1; - else - stereo1 = 2; - - if(fr->mode == MPG_MD_JOINT_STEREO) { - ms_stereo = fr->mode_ext & 0x2; - i_stereo = fr->mode_ext & 0x1; - } - else - ms_stereo = i_stereo = 0; - - if(fr->lsf) { - granules = 1; - if(!III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single)) - return -1; - } - else { - granules = 2; -#ifdef MPEG1 - if(!III_get_side_info_1(&sideinfo,stereo,ms_stereo,sfreq,single)) - return -1; -#else - FIXME("Not supported\n"); -#endif - } - - if(set_pointer(fr->mp,sideinfo.main_data_begin) == MP3_ERR) - return -1; - - for (gr=0;grlsf) - part2bits = III_get_scale_factors_2(scalefacs[0],gr_info,0); - else { -#ifdef MPEG1 - part2bits = III_get_scale_factors_1(scalefacs[0],gr_info); -#else - FIXME("Not supported\n"); + int gr, ch, ss,clip=0; + int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */ + struct III_sideinfo sideinfo; + int stereo = fr->stereo; + int single = fr->single; + int ms_stereo,i_stereo; + int sfreq = fr->sampling_frequency; + int stereo1,granules; + + if(stereo == 1) + { /* stream is mono */ + stereo1 = 1; + single = SINGLE_LEFT; + } + else if(single != SINGLE_STEREO) /* stream is stereo, but force to mono */ + stereo1 = 1; + else + stereo1 = 2; + + if(fr->mode == MPG_MD_JOINT_STEREO) + { + ms_stereo = (fr->mode_ext & 0x2)>>1; + i_stereo = fr->mode_ext & 0x1; + } + else ms_stereo = i_stereo = 0; + + granules = fr->lsf ? 1 : 2; + + /* quick hack to keep the music playing */ + /* after having seen this nasty test file... */ + if(III_get_side_info(fr, &sideinfo,stereo,ms_stereo,sfreq,single)) + { + if(NOQUIET) ERR("bad frame - unable to get valid sideinfo\n"); + return clip; + } + + set_pointer(fr,sideinfo.main_data_begin); + + for(gr=0;grlsf) + part2bits = III_get_scale_factors_2(fr, scalefacs[0],gr_info,0); + else + part2bits = III_get_scale_factors_1(fr, scalefacs[0],gr_info,0,gr); + + if(III_dequantize_sample(fr, hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits)) + { + if(VERBOSE2) ERR("dequantization failed!\n"); + return clip; + } + } + + if(stereo == 2) + { + struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]); + long part2bits; + if(fr->lsf) + part2bits = III_get_scale_factors_2(fr, scalefacs[1],gr_info,i_stereo); + else + part2bits = III_get_scale_factors_1(fr, scalefacs[1],gr_info,1,gr); + + if(III_dequantize_sample(fr, hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits)) + { + if(VERBOSE2) ERR("dequantization failed!\n"); + return clip; + } + + if(ms_stereo) + { + int i; + unsigned int maxb = sideinfo.ch[0].gr[gr].maxb; + if(sideinfo.ch[1].gr[gr].maxb > maxb) maxb = sideinfo.ch[1].gr[gr].maxb; + + for(i=0;ilsf); + + if(ms_stereo || i_stereo || (single == SINGLE_MIX) ) + { + if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb) + sideinfo.ch[0].gr[gr].maxb = gr_info->maxb; + else + gr_info->maxb = sideinfo.ch[0].gr[gr].maxb; + } + + switch(single) + { + case SINGLE_MIX: + { + register int i; + register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; + for(i=0;imaxb;i++,in0++) + *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */ + } + break; + case SINGLE_RIGHT: + { + register int i; + register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; + for(i=0;imaxb;i++) + *in0++ = *in1++; + } + break; + } + } + + for(ch=0;chaf.encoding != MPG123_ENC_SIGNED_16 || fr->down_sample != 0) + { #endif - } - if(III_dequantize_sample(hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits)) - return clip; - } - if(stereo == 2) { - struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]); - long part2bits; - if(fr->lsf) - part2bits = III_get_scale_factors_2(scalefacs[1],gr_info,i_stereo); - else { -#ifdef MPEG1 - part2bits = III_get_scale_factors_1(scalefacs[1],gr_info); -#else - FIXME("Not supported\n"); + for(ss=0;sssynth_mono)(hybridOut[0][ss], fr); + else + clip += (fr->synth_stereo)(hybridOut[0][ss], hybridOut[1][ss], fr); + + } +#ifdef OPT_I486 + } else + { + /* Only stereo, 16 bits benefit from the 486 optimization. */ + ss=0; + while(ss < SSLIMIT) + { + int n; + n=(fr->buffer.size - fr->buffer.fill) / (2*2*32); + if(n > (SSLIMIT-ss)) n=SSLIMIT-ss; + + /* Clip counting makes no sense with this function. */ + absynth_1to1_i486(hybridOut[0][ss], 0, fr, n); + absynth_1to1_i486(hybridOut[1][ss], 1, fr, n); + ss+=n; + fr->buffer.fill+=(2*2*32)*n; + } + } #endif - } - - if(III_dequantize_sample(hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits)) - return clip; - - if(ms_stereo) { - int i; - for(i=0;ilsf); - - if(ms_stereo || i_stereo || (single == 3) ) { - if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb) - sideinfo.ch[0].gr[gr].maxb = gr_info->maxb; - else - gr_info->maxb = sideinfo.ch[0].gr[gr].maxb; - } - - switch(single) { - case 3: - { - register unsigned int i; - register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; - for(i=0;imaxb;i++,in0++) - *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */ - } - break; - case 1: - { - register unsigned int i; - register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1]; - for(i=0;imaxb;i++) - *in0++ = *in1++; - } - break; - } - } - - for(ch=0;chmp); - } - - for(ss=0;ss= 0) { - clip += synth_1to1_mono(fr->mp,hybridOut[0][ss],pcm_sample,pcm_point); - } - else { - int p1 = *pcm_point; - clip += synth_1to1(fr->mp,hybridOut[0][ss],0,pcm_sample,&p1); - clip += synth_1to1(fr->mp,hybridOut[1][ss],1,pcm_sample,pcm_point); - } - } - } - - return clip; + } + + return clip; } diff --git a/dlls/winemp3.acm/libmpg123.c b/dlls/winemp3.acm/libmpg123.c new file mode 100644 index 0000000..a8ff01e --- /dev/null +++ b/dlls/winemp3.acm/libmpg123.c @@ -0,0 +1,1480 @@ +/* + libmpg123: MPEG Audio Decoder library + + copyright 1995-2009 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ +#include +#include "windef.h" +#include "winbase.h" + +#include "mpg123lib_intern.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(mpeg3); + +#ifdef GAPLESS +#define SAMPLE_ADJUST(x) ((x) - ((mh->p.flags & MPG123_GAPLESS) ? mh->begin_os : 0)) +#define SAMPLE_UNADJUST(x) ((x) + ((mh->p.flags & MPG123_GAPLESS) ? mh->begin_os : 0)) +#else +#define SAMPLE_ADJUST(x) (x) +#define SAMPLE_UNADJUST(x) (x) +#endif + +#define SEEKFRAME(mh) ((mh)->ignoreframe < 0 ? 0 : (mh)->ignoreframe) + +static int initialized = 0; + +#define ALIGNCHECK(mh) +#define ALIGNCHECKK +/* On compilers that support data alignment but not the automatic stack realignment. + We check for properly aligned stack before risking a crash because of badly compiled + client program. */ +#if (defined CCALIGN) && (defined NEED_ALIGNCHECK) && ((defined DEBUG) || (defined CHECK_ALIGN)) + +/* Common building block. */ +#define ALIGNMAINPART \ + /* minimum size of 16 bytes, not all compilers would align a smaller piece of data */ \ + double ALIGNED(16) altest[2]; \ + TRACE("testing alignment, with %lu %% 16 = %lu\n", \ + (unsigned long)altest, (unsigned long)((size_t)altest % 16)); \ + if((size_t)altest % 16 != 0) + +#undef ALIGNCHECK +#define ALIGNCHECK(mh) \ + ALIGNMAINPART \ + { \ + ERR("Stack variable is not aligned! Your combination of compiler/library is dangerous!\n"); \ + if(mh != NULL) mh->err = MPG123_BAD_ALIGN; \ +\ + return MPG123_ERR; \ + } +#undef ALIGNCHECKK +#define ALIGNCHECKK \ + ALIGNMAINPART \ + { \ + ERR("Stack variable is not aligned! Your combination of compiler/library is dangerous!\n"); \ + return MPG123_BAD_ALIGN; \ + } + +#endif + +#ifdef GAPLESS +/* + Take the buffer after a frame decode (strictly: it is the data from frame fr->num!) and cut samples out. + fr->buffer.fill may then be smaller than before... +*/ +static void frame_buffercheck(mpg123_handle *fr) +{ + /* When we have no accurate position, gapless code does not make sense. */ + if(!fr->accurate) return; + + /* The first interesting frame: Skip some leading samples. */ + if(fr->firstoff && fr->num == fr->firstframe) + { + off_t byteoff = samples_to_bytes(fr, fr->firstoff); + if((off_t)fr->buffer.fill > byteoff) + { + fr->buffer.fill -= byteoff; + /* buffer.p != buffer.data only for own buffer */ + TRACE("cutting %li samples/%li bytes on begin, own_buffer=%i at %p=%p, buf[1]=%i\n", + (long)fr->firstoff, (long)byteoff, fr->own_buffer, (void*)fr->buffer.p, (void*)fr->buffer.data, ((short*)fr->buffer.p)[2]); + if(fr->own_buffer) fr->buffer.p = fr->buffer.data + byteoff; + else memmove(fr->buffer.data, fr->buffer.data + byteoff, fr->buffer.fill); + TRACE("done cutting, buffer at %p =? %p, buf[1]=%i\n", + (void*)fr->buffer.p, (void*)fr->buffer.data, ((short*)fr->buffer.p)[2]); + } + else fr->buffer.fill = 0; + fr->firstoff = 0; /* Only enter here once... when you seek, firstoff should be reset. */ + } + /* The last interesting (planned) frame: Only use some leading samples. */ + if(fr->lastoff && fr->num == fr->lastframe) + { + off_t byteoff = samples_to_bytes(fr, fr->lastoff); + if((off_t)fr->buffer.fill > byteoff) + { + fr->buffer.fill = byteoff; + } + fr->lastoff = 0; /* Only enter here once... when you seek, lastoff should be reset. */ + } +} +#endif + +int attribute_align_arg mpg123_init(void) +{ + ALIGNCHECKK + if((sizeof(short) != 2) || (sizeof(long) < 4)) return MPG123_BAD_TYPES; + + if(initialized) return MPG123_OK; /* no need to initialize twice */ + +#ifndef NO_LAYER12 + init_layer12(); /* inits also shared tables with layer1 */ +#endif +#ifndef NO_LAYER3 + init_layer3(); +#endif + prepare_decode_tables(); + check_decoders(); + initialized = 1; + return MPG123_OK; +} + +void attribute_align_arg mpg123_exit(void) +{ + /* nothing yet, but something later perhaps */ +} + +/* create a new handle with specified decoder, decoder can be "", "auto" or NULL for auto-detection */ +mpg123_handle attribute_align_arg *mpg123_new(const char* decoder, int *error) +{ + return mpg123_parnew(NULL, decoder, error); +} + +/* ...the full routine with optional initial parameters to override defaults. */ +mpg123_handle attribute_align_arg *mpg123_parnew(mpg123_pars *mp, const char* decoder, int *error) +{ + mpg123_handle *fr = NULL; + int err = MPG123_OK; +#if (defined CCALIGN) && (defined NEED_ALIGNCHECK) && ((defined DEBUG) || (defined CHECK_ALIGN)) +#ifdef CCALIGN + double ALIGNED(16) altest[4]; + if(((size_t)altest) % 16 != 0) + { + ERR("Stack variable is not aligned! Your combination of compiler/library is dangerous!\n"); + *error = MPG123_BAD_ALIGN; + return NULL; + } +#endif +#endif + if(initialized) fr = (mpg123_handle*) HeapAlloc(GetProcessHeap(),0,sizeof(mpg123_handle)); + else err = MPG123_NOT_INITIALIZED; + if(fr != NULL) + { + frame_init_par(fr, mp); + TRACE("cpu opt setting\n"); + if(frame_cpu_opt(fr, decoder) != 1) + { + err = MPG123_BAD_DECODER; + frame_exit(fr); + HeapFree(GetProcessHeap(),0,fr); + fr = NULL; + } + } + if(fr != NULL) + { + /* Cleanup that mess! ... use mpg123_decoder / decode_update! */ + if(frame_outbuffer(fr) != 0) + { + err = MPG123_NO_BUFFERS; + frame_exit(fr); + HeapFree(GetProcessHeap(),0,fr); + fr = NULL; + } + else + { + /* I smell cleanup here... with get_next_frame() */ +/* if(decode_update(fr) != 0) + { + err = fr->err != MPG123_OK ? fr->err : MPG123_BAD_DECODER; + frame_exit(fr); + free(fr); + fr = NULL; + } + else */ + fr->decoder_change = 1; + } + } + else if(err == MPG123_OK) err = MPG123_OUT_OF_MEM; + + if(error != NULL) *error = err; + return fr; +} + +int attribute_align_arg mpg123_decoder(mpg123_handle *mh, const char* decoder) +{ + enum optdec dt = dectype(decoder); + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + + if(dt == nodec) + { + mh->err = MPG123_BAD_DECODER; + return MPG123_ERR; + } + if(dt == mh->cpu_opts.type) return MPG123_OK; + + /* Now really change. */ + /* frame_exit(mh); + frame_init(mh); */ + TRACE("cpu opt setting\n"); + if(frame_cpu_opt(mh, decoder) != 1) + { + mh->err = MPG123_BAD_DECODER; + frame_exit(mh); + return MPG123_ERR; + } + /* New buffers for decoder are created in frame_buffers() */ + if((frame_outbuffer(mh) != 0)) + { + mh->err = MPG123_NO_BUFFERS; + frame_exit(mh); + return MPG123_ERR; + } + /* I smell cleanup here... with get_next_frame() */ + decode_update(mh); + mh->decoder_change = 1; + return MPG123_OK; +} + +int attribute_align_arg mpg123_param(mpg123_handle *mh, enum mpg123_parms key, long val, double fval) +{ + int r; + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + r = mpg123_par(&mh->p, key, val, fval); + if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; } + else + { /* Special treatment for some settings. */ +#ifdef FRAME_INDEX + if(key == MPG123_INDEX_SIZE) + { /* Apply frame index size and grow property on the fly. */ + r = frame_index_setup(mh); + if(r != MPG123_OK) mh->err = MPG123_INDEX_FAIL; + } +#endif + } + return r; +} + +int attribute_align_arg mpg123_par(mpg123_pars *mp, enum mpg123_parms key, long val, double fval) +{ + int ret = MPG123_OK; + ALIGNCHECKK + if(mp == NULL) return MPG123_BAD_PARS; + switch(key) + { + case MPG123_VERBOSE: + mp->verbose = val; + break; + case MPG123_FLAGS: +#ifndef GAPLESS + if(val & MPG123_GAPLESS) ret = MPG123_NO_GAPLESS; +#endif + if(ret == MPG123_OK) mp->flags = val; + TRACE("set flags to 0x%lx\n", (unsigned long) mp->flags); + break; + case MPG123_ADD_FLAGS: +#ifndef GAPLESS + /* Enabling of gapless mode doesn't work when it's not there, but disabling (below) is no problem. */ + if(val & MPG123_GAPLESS) ret = MPG123_NO_GAPLESS; + else +#endif + mp->flags |= val; + TRACE("set flags to 0x%lx\n", (unsigned long) mp->flags); + break; + case MPG123_REMOVE_FLAGS: + mp->flags &= ~val; + TRACE("set flags to 0x%lx\n", (unsigned long) mp->flags); + break; + case MPG123_FORCE_RATE: /* should this trigger something? */ +#ifdef NO_NTOM + if(val > 0) + ret = MPG123_BAD_RATE; +#else + if(val > 96000) ret = MPG123_BAD_RATE; + else mp->force_rate = val < 0 ? 0 : val; /* >0 means enable, 0 disable */ +#endif + break; + case MPG123_DOWN_SAMPLE: +#ifdef NO_DOWNSAMPLE + if(val != 0) ret = MPG123_BAD_RATE; +#else + if(val < 0 || val > 2) ret = MPG123_BAD_RATE; + else mp->down_sample = (int)val; +#endif + break; + case MPG123_RVA: + if(val < 0 || val > MPG123_RVA_MAX) ret = MPG123_BAD_RVA; + else mp->rva = (int)val; + break; + case MPG123_DOWNSPEED: + mp->halfspeed = val < 0 ? 0 : val; + break; + case MPG123_UPSPEED: + mp->doublespeed = val < 0 ? 0 : val; + break; + case MPG123_ICY_INTERVAL: +#ifndef NO_ICY + mp->icy_interval = val > 0 ? val : 0; +#else + if(val > 0) ret = MPG123_BAD_PARAM; +#endif + break; + case MPG123_OUTSCALE: + /* Choose the value that is non-zero, if any. + Downscaling integers to 1.0 . */ + mp->outscale = val == 0 ? fval : (double)val/SHORT_SCALE; + break; + case MPG123_TIMEOUT: +#ifndef WIN32 + mp->timeout = val >= 0 ? val : 0; +#else + ret = MPG123_NO_TIMEOUT; +#endif + break; + case MPG123_RESYNC_LIMIT: + mp->resync_limit = val; + break; + case MPG123_INDEX_SIZE: +#ifdef FRAME_INDEX + mp->index_size = val; +#else + ret = MPG123_NO_INDEX; +#endif + break; + case MPG123_PREFRAMES: + if(val >= 0) mp->preframes = val; + else ret = MPG123_BAD_VALUE; + break; + default: + ret = MPG123_BAD_PARAM; + } + return ret; +} + +int attribute_align_arg mpg123_getparam(mpg123_handle *mh, enum mpg123_parms key, long *val, double *fval) +{ + int r; + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + r = mpg123_getpar(&mh->p, key, val, fval); + if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; } + return r; +} + +int attribute_align_arg mpg123_getpar(mpg123_pars *mp, enum mpg123_parms key, long *val, double *fval) +{ + int ret = 0; + ALIGNCHECKK + if(mp == NULL) return MPG123_BAD_PARS; + switch(key) + { + case MPG123_VERBOSE: + if(val) *val = mp->verbose; + break; + case MPG123_FLAGS: + case MPG123_ADD_FLAGS: + if(val) *val = mp->flags; + break; + case MPG123_FORCE_RATE: + if(val) +#ifdef NO_NTOM + *val = 0; +#else + *val = mp->force_rate; +#endif + break; + case MPG123_DOWN_SAMPLE: + if(val) *val = mp->down_sample; + break; + case MPG123_RVA: + if(val) *val = mp->rva; + break; + case MPG123_DOWNSPEED: + if(val) *val = mp->halfspeed; + break; + case MPG123_UPSPEED: + if(val) *val = mp->doublespeed; + break; + case MPG123_ICY_INTERVAL: +#ifndef NO_ICY + if(val) *val = (long)mp->icy_interval; +#else + if(val) *val = 0; +#endif + break; + case MPG123_OUTSCALE: + if(fval) *fval = mp->outscale; + if(val) *val = (long)(mp->outscale*SHORT_SCALE); + break; + case MPG123_RESYNC_LIMIT: + if(val) *val = mp->resync_limit; + break; + case MPG123_INDEX_SIZE: + if(val) +#ifdef FRAME_INDEX + *val = mp->index_size; +#else + *val = 0; /* graceful fallback: no index is index of zero size */ +#endif + break; + case MPG123_PREFRAMES: + *val = mp->preframes; + break; + default: + ret = MPG123_BAD_PARAM; + } + return ret; +} + +int attribute_align_arg mpg123_getstate(mpg123_handle *mh, enum mpg123_state key, long *val, double *fval) +{ + int ret = MPG123_OK; + long theval = 0; + double thefval = 0.; + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + + switch(key) + { + case MPG123_ACCURATE: + theval = mh->accurate; + break; + default: + mh->err = MPG123_BAD_KEY; + ret = MPG123_ERR; + } + + if(val != NULL) *val = theval; + if(fval != NULL) *fval = thefval; + + return ret; +} + +int attribute_align_arg mpg123_eq(mpg123_handle *mh, enum mpg123_channels channel, int band, double val) +{ + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + if(band < 0 || band > 31){ mh->err = MPG123_BAD_BAND; return MPG123_ERR; } + switch(channel) + { + case MPG123_LEFT|MPG123_RIGHT: + mh->equalizer[0][band] = mh->equalizer[1][band] = DOUBLE_TO_REAL(val); + break; + case MPG123_LEFT: mh->equalizer[0][band] = DOUBLE_TO_REAL(val); break; + case MPG123_RIGHT: mh->equalizer[1][band] = DOUBLE_TO_REAL(val); break; + default: + mh->err=MPG123_BAD_CHANNEL; + return MPG123_ERR; + } + mh->have_eq_settings = TRUE; + return MPG123_OK; +} + +double attribute_align_arg mpg123_geteq(mpg123_handle *mh, enum mpg123_channels channel, int band) +{ + double ret = 0.; + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + + /* Handle this gracefully. When there is no band, it has no volume. */ + if(band > -1 && band < 32) + switch(channel) + { + case MPG123_LEFT|MPG123_RIGHT: + ret = 0.5*(REAL_TO_DOUBLE(mh->equalizer[0][band])+REAL_TO_DOUBLE(mh->equalizer[1][band])); + break; + case MPG123_LEFT: ret = REAL_TO_DOUBLE(mh->equalizer[0][band]); break; + case MPG123_RIGHT: ret = REAL_TO_DOUBLE(mh->equalizer[1][band]); break; + /* Default case is already handled: ret = 0 */ + } + + return ret; +} + +/* plain file access, no http! */ +int attribute_align_arg mpg123_open(mpg123_handle *mh, const char *path) +{ + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + + mpg123_close(mh); + frame_reset(mh); + return open_stream(mh, path, -1); +} + +int attribute_align_arg mpg123_open_fd(mpg123_handle *mh, int fd) +{ + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + + mpg123_close(mh); + frame_reset(mh); + return open_stream(mh, NULL, fd); +} + +int attribute_align_arg mpg123_open_feed(mpg123_handle *mh) +{ + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + + mpg123_close(mh); + frame_reset(mh); + return open_feed(mh); +} + +int attribute_align_arg mpg123_replace_reader( mpg123_handle *mh, + ssize_t (*r_read) (int, void *, size_t), + off_t (*r_lseek)(int, off_t, int) ) +{ + ALIGNCHECK(mh); + if(mh == NULL) return MPG123_ERR; + mh->rdat.r_read = r_read; + mh->rdat.r_lseek = r_lseek; + return MPG123_OK; +} + +int decode_update(mpg123_handle *mh) +{ + long native_rate; + int b; + ALIGNCHECK(mh); + native_rate = frame_freq(mh); + + b = frame_output_format(mh); /* Select the new output format based on given constraints. */ + if(b < 0) return MPG123_ERR; + + if(b == 1) mh->new_format = 1; /* Store for later... */ + + TRACE("updating decoder structure with native rate %li and af.rate %li (new format: %i)\n", native_rate, mh->af.rate, mh->new_format); + if(mh->af.rate == native_rate) mh->down_sample = 0; + else if(mh->af.rate == native_rate>>1) mh->down_sample = 1; + else if(mh->af.rate == native_rate>>2) mh->down_sample = 2; + else mh->down_sample = 3; /* flexible (fixed) rate */ + switch(mh->down_sample) + { + case 0: + case 1: + case 2: + mh->down_sample_sblimit = SBLIMIT>>(mh->down_sample); + /* With downsampling I get less samples per frame */ + mh->outblock = samples_to_bytes(mh, (spf(mh)>>mh->down_sample)); + break; +#ifndef NO_NTOM + case 3: + { + if(synth_ntom_set_step(mh) != 0) return -1; + if(frame_freq(mh) > mh->af.rate) + { + mh->down_sample_sblimit = SBLIMIT * mh->af.rate; + mh->down_sample_sblimit /= frame_freq(mh); + } + else mh->down_sample_sblimit = SBLIMIT; + mh->outblock = mh->af.encsize * mh->af.channels * + ( ( NTOM_MUL-1+spf(mh) + * (((size_t)NTOM_MUL*mh->af.rate)/frame_freq(mh)) + )/NTOM_MUL ); + } + break; +#endif + } + + if(!(mh->p.flags & MPG123_FORCE_MONO)) + { + if(mh->af.channels == 1) mh->single = SINGLE_MIX; + else mh->single = SINGLE_STEREO; + } + else mh->single = (mh->p.flags & MPG123_FORCE_MONO)-1; + if(set_synth_functions(mh) != 0) return -1;; + + do_rva(mh); + TRACE("done updating decoder structure with native rate %li and af.rate %li and down_sample %i\n", frame_freq(mh), mh->af.rate, mh->down_sample); + + return 0; +} + +size_t attribute_align_arg mpg123_safe_buffer() +{ + /* real is the largest possible output (it's 32bit float, 32bit int or 64bit double). */ + return sizeof(real)*2*1152*NTOM_MAX; +} + +size_t attribute_align_arg mpg123_outblock(mpg123_handle *mh) +{ + if(mh != NULL) return mh->outblock; + else return mpg123_safe_buffer(); +} + +static int get_next_frame(mpg123_handle *mh) +{ + int change = mh->decoder_change; + do + { + int b; + /* Decode & discard some frame(s) before beginning. */ + if(mh->to_ignore && mh->num < mh->firstframe && mh->num >= mh->ignoreframe) + { + TRACE("ignoring frame %li\n", (long)mh->num); + /* Decoder structure must be current! decode_update has been called before... */ + (mh->do_layer)(mh); mh->buffer.fill = 0; +#ifndef NO_NTOM + /* The ignored decoding may have failed. Make sure ntom stays consistent. */ + if(mh->down_sample == 3) ntom_set_ntom(mh, mh->num+1); +#endif + mh->to_ignore = mh->to_decode = FALSE; + } + /* Read new frame data; possibly breaking out here for MPG123_NEED_MORE. */ + TRACE("read frame\n"); + mh->to_decode = FALSE; + b = read_frame(mh); /* That sets to_decode only if a full frame was read. */ + TRACE("read of frame %li returned %i (to_decode=%i) at sample %li\n", (long)mh->num, b, mh->to_decode, (long)mpg123_tell(mh)); + if(b == MPG123_NEED_MORE) return MPG123_NEED_MORE; /* need another call with data */ + else if(b <= 0) + { + /* More sophisticated error control? */ + if(b==0 || mh->rdat.filepos == mh->rdat.filelen) + { /* We simply reached the end. */ + mh->track_frames = mh->num + 1; + return MPG123_DONE; + } + else return MPG123_ERR; /* Some real error. */ + } + /* Now, there should be new data to decode ... and also possibly new stream properties */ + if(mh->header_change > 1) + { + TRACE("big header change\n"); + change = 1; + } + /* Now some accounting: Look at the numbers and decide if we want this frame. */ + ++mh->playnum; + /* Plain skipping without decoding, only when frame is not ignored on next cycle. */ + if(mh->num < mh->firstframe || (mh->p.doublespeed && (mh->playnum % mh->p.doublespeed))) + { + if(!(mh->to_ignore && mh->num < mh->firstframe && mh->num >= mh->ignoreframe)) + { + frame_skip(mh); + /* Should one fix NtoM here or not? + It is not work the trouble for doublespeed, but what with leading frames? */ + } + } + /* Or, we are finally done and have a new frame. */ + else break; + } while(1); + /* When we start actually using the CRC, this could move into the loop... */ + /* A question of semantics ... should I fold start_frame and frame_number into firstframe/lastframe? */ + if(mh->lastframe >= 0 && mh->num > mh->lastframe) + { + mh->to_decode = mh->to_ignore = FALSE; + return MPG123_DONE; + } + if(change) + { + if(decode_update(mh) < 0) /* dito... */ + return MPG123_ERR; + +TRACE("new format: %i\n", mh->new_format); + + mh->decoder_change = 0; +#ifdef GAPLESS + if(mh->fresh) + { + int b=0; + /* Prepare offsets for gapless decoding. */ + TRACE("preparing gapless stuff with native rate %li\n", frame_freq(mh)); + frame_gapless_realinit(mh); + frame_set_frameseek(mh, mh->num); + mh->fresh = 0; + /* Could this possibly happen? With a real big gapless offset... */ + if(mh->num < mh->firstframe) b = get_next_frame(mh); + if(b < 0) return b; /* Could be error, need for more, new format... */ + } +#endif + } + return MPG123_OK; +} + +/* + Not part of the api. This just decodes the frame and fills missing bits with zeroes. + There can be frames that are broken and thus make do_layer() fail. +*/ +void decode_the_frame(mpg123_handle *fr) +{ + size_t needed_bytes = samples_to_bytes(fr, frame_outs(fr, fr->num+1)-frame_outs(fr, fr->num)); + fr->clip += (fr->do_layer)(fr); + /*fprintf(stderr, "frame %"OFF_P": got %"SIZE_P" / %"SIZE_P"\n", fr->num,(size_p)fr->buffer.fill, (size_p)needed_bytes);*/ + /* There could be less data than promised. + Also, then debugging, we look out for coding errors that could result in _more_ data than expected. */ +#ifdef DEBUG + if(fr->buffer.fill != needed_bytes) + { +#endif + if(fr->buffer.fill < needed_bytes) + { + if(VERBOSE2) + TRACE("Note: broken frame %li, filling up with zeroes\n", (long)fr->num ); + + /* One could do a loop with individual samples instead... but zero is zero. */ + memset(fr->buffer.data + fr->buffer.fill, 0, needed_bytes - fr->buffer.fill); + fr->buffer.fill = needed_bytes; +#ifndef NO_NTOM + /* ntom_val will be wrong when the decoding wasn't carried out completely */ + ntom_set_ntom(fr, fr->num+1); +#endif + } +#ifdef DEBUG + else + { + if(NOQUIET) + ERR("I got _more_ bytes than expected (%li / %li), that should not be possible!\n", fr->buffer.fill, needed_bytes); + } + } +#endif + /* Handle unsigned output formats via reshifting after decode here. */ +#ifndef NO_32BIT + if(fr->af.encoding == MPG123_ENC_UNSIGNED_32) + { /* 32bit signed -> unsigned */ + size_t i; + int32_t *ssamples; + UINT32 *usamples; + ssamples = (int32_t*)fr->buffer.data; + usamples = (UINT32*)fr->buffer.data; + TRACE("converting output to unsigned 32 bit integer\n"); + for(i=0; ibuffer.fill/sizeof(int32_t); ++i) + { + /* Different strategy since we don't have a larger type at hand. + Also watch out for silly +-1 fun because integer constants are signed in C90! */ + if(ssamples[i] >= 0) + usamples[i] = (UINT32)ssamples[i] + 2147483647+1; + /* The smalles value goes zero. */ + else if(ssamples[i] == ((int32_t)-2147483647-1)) + usamples[i] = 0; + /* Now -value is in the positive range of signed int ... so it's a possible value at all. */ + else + usamples[i] = (UINT32)2147483647+1 - (UINT32)(-ssamples[i]); + } + } +#endif +#ifndef NO_16BIT + if(fr->af.encoding == MPG123_ENC_UNSIGNED_16) + { + size_t i; + short *ssamples; + unsigned short *usamples; + ssamples = (short*)fr->buffer.data; + usamples = (unsigned short*)fr->buffer.data; + TRACE("converting output to unsigned 16 bit integer\n"); + for(i=0; ibuffer.fill/sizeof(short); ++i) + { + long tmp = (long)ssamples[i]+32768; + usamples[i] = (unsigned short)tmp; + } + } +#endif +} + +/* + Put _one_ decoded frame into the frame structure's buffer, accessible at the location stored in