1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
layer1.c: the layer 1 decoder
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
initially written by Michael Hipp
may have a few bugs after last optimization ...
*/
#include "mpg123lib_intern.h"
#include "getbits.h"
#include "debug.h"
/*
Allocation value is not allowed to be 15. Initially, libmad showed me the
error that mpg123 used to ignore. Then, I found a quote on that in
Shlien, S. (1994): Guide to MPEG-1 Audio Standard.
IEEE Transactions on Broadcasting 40, 4
"To avoid conflicts with the synchronization code, code '1111' is defined
to be illegal."
*/
static int check_balloc(mpg123_handle *fr, unsigned int *balloc, unsigned int *end)
{
unsigned int *ba;
for(ba=balloc; ba != end; ++ba)
if(*ba == 15)
{
if(NOQUIET) error("Illegal bit allocation value.");
return -1;
}
return 0;
}
#define NEED_BITS(fr, num) \
if((fr)->bits_avail < num) \
{ \
if(NOQUIET) \
error2("%u bits needed, %li available", num, (fr)->bits_avail); \
return -1; \
} \
static int 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 == 2)
{
int i;
int jsbound = fr->jsbound;
unsigned int needbits = jsbound*2*4 + (SBLIMIT-jsbound)*4;
NEED_BITS(fr, needbits);
needbits = 0;
for(i=0;i<jsbound;i++)
{
ba[0] = getbits_fast(fr, 4);
ba[1] = getbits_fast(fr, 4);
needbits += ((ba[0]?1:0)+(ba[1]?1:0))*6;
ba+=2;
}
for(i=jsbound;i<SBLIMIT;i++)
{
*ba = getbits_fast(fr, 4);
needbits += (*ba?1:0)*12;
++ba;
}
if(check_balloc(fr, balloc, ba)) return -1;
ba = balloc;
NEED_BITS(fr, needbits)
for(i=0;i<jsbound;i++)
{
if ((*ba++))
*sca++ = getbits_fast(fr, 6);
if ((*ba++))
*sca++ = getbits_fast(fr, 6);
}
for (i=jsbound;i<SBLIMIT;i++) if((*ba++))
{
*sca++ = getbits_fast(fr, 6);
*sca++ = getbits_fast(fr, 6);
}
}
else
{
int i;
unsigned int needbits = SBLIMIT*4;
NEED_BITS(fr, needbits)
needbits = 0;
for(i=0;i<SBLIMIT;i++)
{
*ba = getbits_fast(fr, 4);
needbits += (*ba?1:0)*6;
++ba;
}
if(check_balloc(fr, balloc, ba)) return -1;
ba = balloc;
NEED_BITS(fr, needbits)
for (i=0;i<SBLIMIT;i++)
if ((*ba++))
*sca++ = getbits_fast(fr, 6);
}
return 0;
}
/* Something sane in place of undefined (-1)<<n. Well, not really. */
#define MINUS_SHIFT(n) ( (int)(((unsigned int)-1)<<(n)) )
static int I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr)
{
int i,n;
int smpb[2*SBLIMIT]; /* values: 0-65535 */
int *sample;
register unsigned int *ba;
register unsigned int *sca = (unsigned int *) scale_index;
if(fr->stereo == 2)
{
unsigned int needbits = 0;
int jsbound = fr->jsbound;
register real *f0 = fraction[0];
register real *f1 = fraction[1];
ba = balloc;
for(sample=smpb,i=0;i<jsbound;i++)
{
if((n=*ba++))
needbits += n+1;
if((n=*ba++))
needbits += n+1;
}
for(i=jsbound;i<SBLIMIT;i++)
if((n = *ba++))
needbits += n+1;
NEED_BITS(fr, needbits)
ba = balloc;
for(sample=smpb,i=0;i<jsbound;i++)
{
if((n = *ba++)) *sample++ = getbits(fr, n+1);
if((n = *ba++)) *sample++ = getbits(fr, n+1);
}
for(i=jsbound;i<SBLIMIT;i++)
if((n = *ba++))
*sample++ = getbits(fr, n+1);
ba = balloc;
for(sample=smpb,i=0;i<jsbound;i++)
{
if((n=*ba++))
*f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
else *f0++ = DOUBLE_TO_REAL(0.0);
if((n=*ba++))
*f1++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
else *f1++ = DOUBLE_TO_REAL(0.0);
}
for(i=jsbound;i<SBLIMIT;i++)
{
if((n=*ba++))
{
real samp = DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1);
*f0++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[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
{
unsigned int needbits = 0;
register real *f0 = fraction[0];
ba = balloc;
for(sample=smpb,i=0;i<SBLIMIT;i++)
if((n = *ba++))
needbits += n+1;
NEED_BITS(fr, needbits);
ba = balloc;
for(sample=smpb,i=0;i<SBLIMIT;i++)
if ((n = *ba++))
*sample++ = getbits(fr, n+1);
ba = balloc;
for(sample=smpb,i=0;i<SBLIMIT;i++)
{
if((n=*ba++))
*f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[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);
}
return 0;
}
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)[SBLIMIT] = fr->layer1.fraction; /* fraction[2][SBLIMIT] */
int single = fr->single;
fr->jsbound = (fr->hdr.mode == MPG_MD_JOINT_STEREO) ? (fr->hdr.mode_ext<<2)+4 : 32;
if(stereo == 1 || single == SINGLE_MIX) /* I don't see mixing handled here */
single = SINGLE_LEFT;
if(I_step_one(balloc,scale_index,fr))
{
if(NOQUIET)
error("Aborting layer I decoding after step one.");
return clip;
}
for(i=0;i<SCALE_BLOCK;i++)
{
if(I_step_two(fraction,balloc,scale_index,fr))
{
if(NOQUIET)
error("Aborting layer I decoding after step two.");
return clip;
}
if(single != SINGLE_STEREO)
clip += (fr->synth_mono)(fraction[single], fr);
else
clip += (fr->synth_stereo)(fraction[0], fraction[1], fr);
}
return clip;
}
|