#include <string.h>
#include "stream.h"
#include "decoder.h"
#include "Log.h"

void hdr_to_frps(layer12* layer, const frame_params* fr_ps);
void do_layer1(stream* S);
void do_layer2(stream* S);

// ***************** Layer II stuff ***********************

// Number of bit alloc tables
#define NALLOCTAB 5

// alloc[][x][0] is number of allocated bits
// alloc[][x][y] is quantisation class number

static const al_table alloc[NALLOCTAB] =
{
// table 0, 27 bands
	{
	{4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
	{4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
	{4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{2, 0, 1, 16},
	{2, 0, 1, 16},
	{2, 0, 1, 16},
	{2, 0, 1, 16}
	},
	// table 1, 30 bands
	{
	{4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
	{4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
	{4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{4, 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{3, 0, 1, 2, 3, 4, 5, 16},
	{2, 0, 1, 16},
	{2, 0, 1, 16},
	{2, 0, 1, 16},
	{2, 0, 1, 16},
	{2, 0, 1, 16},
	{2, 0, 1, 16},
	{2, 0, 1, 16}
	},
	// table 2, 8 bands
	{
	{4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
	{4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7}
	},
	// table 3, 12 bands
	{
	{4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
	{4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7}
	},
	// table 4, 30 bands LSF
	{
	{4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
	{4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
	{4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
	{4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{3, 0, 1, 3, 4, 5, 6, 7},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3},
	{2, 0, 1, 3}
	}
};

// Subband limits for each table
static const unsigned int sblim[NALLOCTAB] = {27, 30, 8, 12, 30};

void init_layer12(layer12* layer)
{
	memset(layer, 0, sizeof(layer12));
	layer->alloc = &alloc[0];
	layer->tab_num = 0;
}

/***************************************************************
 *
 * This module contains the core of the decoder ie all the
 * computational routines. (Layer I and II only)
 * Functions are common to both layer unless
 * otherwise specified.
 *
 * bit_alloc[i*2+j], i = [0, layer->sblimit[, j = [0, fr_ps->channels[
 * scfsi[i] i = 0, i++ if bit_alloc[]
 * sample[i*n], n=channels*(layer=2?3:1) i = 0, i++ if bit_alloc[]
 *
 ***************************************************************/


/***********************************************************************
 *
 * Using the decoded info the appropriate possible quantization per
 * subband table is loaded
 *
 **********************************************************************/

static const unsigned int jsb_table[4] = { 4, 8, 12, 16 };

/*
 * Interpret data in hdr str to fields in fr_ps
 */

void hdr_to_frps(layer12* layer, const frame_params* fr_ps)
{
	if (fr_ps->lay == 2)
	{
		if (fr_ps->version == MPEG_AUDIO_ID)
		{	// MPEG-1
			// decision rules refer to per-channel bitrates (kbits/sec/chan)
			int br_per_ch = fr_ps->bitrate / fr_ps->channels;

			if ((fr_ps->samplerate == 48000 && br_per_ch >= 56)
			||  (br_per_ch >= 56 && br_per_ch <= 80))
				layer->tab_num = 0;
			else if (fr_ps->samplerate != 48000 && br_per_ch >= 96) layer->tab_num = 1;
			else if (fr_ps->samplerate != 32000 && br_per_ch <= 48) layer->tab_num = 2;
			else layer->tab_num = 3;
		}
		else
		{	// MPEG-2 LSF
			layer->tab_num = 4;
		}

		layer->alloc = &alloc[layer->tab_num];

		layer->sblimit = sblim[layer->tab_num];
	}
	else
		layer->sblimit = SBLIMIT;

	if (fr_ps->mode == MPG_MD_JOINT_STEREO)
		layer->jsbound = jsb_table[fr_ps->mode_ext];
	else
		layer->jsbound = layer->sblimit;

	if (layer->jsbound > layer->sblimit)
		layer->jsbound = layer->sblimit;
}

/*
 * Scale factors table in Layer I and II
 *
 * Each factor maps to a value of s = 2^((3-n)/3).
 * Let n = 3*a + b
 * The value now becomes s = 2^((b/3) - a) or s = (2^(b/3)) >> a
 * During unquantisation x = q * s = q * ((2^(b/3)) >> a) = (q * 2^(b/3)) >> a
 * which can be evaluation quickly and with great precision in fixed point.
 *
 * Each scale factor is reencoded as (b + (a << 2)) for use by the assembler unquantisation routine EXPn3.
 */

static const unsigned char imul[64] =
{
	  3+( 0<<2), 2+( 0<<2), 1+( 0<<2)
	, 3+( 1<<2), 2+( 1<<2), 1+( 1<<2)
	, 3+( 2<<2), 2+( 2<<2), 1+( 2<<2)
	, 3+( 3<<2), 2+( 3<<2), 1+( 3<<2)
	, 3+( 4<<2), 2+( 4<<2), 1+( 4<<2)
	, 3+( 5<<2), 2+( 5<<2), 1+( 5<<2)
	, 3+( 6<<2), 2+( 6<<2), 1+( 6<<2)
	, 3+( 7<<2), 2+( 7<<2), 1+( 7<<2)
	, 3+( 8<<2), 2+( 8<<2), 1+( 8<<2)
	, 3+( 9<<2), 2+( 9<<2), 1+( 9<<2)
	, 3+(10<<2), 2+(10<<2), 1+(10<<2)
	, 3+(11<<2), 2+(11<<2), 1+(11<<2)
	, 3+(12<<2), 2+(12<<2), 1+(12<<2)
	, 3+(13<<2), 2+(13<<2), 1+(13<<2)
	, 3+(14<<2), 2+(14<<2), 1+(14<<2)
	, 3+(15<<2), 2+(15<<2), 1+(15<<2)
	, 3+(16<<2), 2+(16<<2), 1+(16<<2)
	, 3+(17<<2), 2+(17<<2), 1+(17<<2)
	, 3+(18<<2), 2+(18<<2), 1+(18<<2)
	, 3+(19<<2), 2+(19<<2), 1+(19<<2)
	, 3+(20<<2), 2+(20<<2), 1+(20<<2)
	, 3+(21<<2)
};

extern int /* 9.23 fixedp */ EXPn3(int q /* 2.30 fixedp */, unsigned char scale);

/************************** Layer I stuff ************************/

static void I_decode(layer12* layer, bytebuf* bs, const frame_params* fr_ps)
{
	unsigned char* bit_alloc;
    unsigned char* scl0;
	unsigned int val;
	int i;

	// Read and decode bit allocation table of 4 bits per subband
	//   one per channel per band in the lower subbands
	//   one per band in the intensity stereo subbands
	bit_alloc = &layer->bit_alloc[0];

	for (i = (SBLIMIT - layer->jsbound) + layer->jsbound * fr_ps->channels; i; i--)
	{
		val = bs_getbits8(bs, 4);
		if (val) val++;
		*bit_alloc++ = val;
	}

	// Read and decode scale factors table, 6 bit per scale factor
	bit_alloc = &layer->bit_alloc[0];
	scl0 = &layer->scale_index[0][0];

	// one per per bit allocated channel in the lower subbands
	for (i = layer->jsbound * fr_ps->channels; i; i--)
		if (*bit_alloc++)
			*scl0++ = imul[bs_getbits8(bs,6)];

	// one per per bit allocated channel too in the intensity stereo subbands
	// but here allocation counts for both channels
	for (i = (SBLIMIT - layer->jsbound); i; i--)
	{
		if (*bit_alloc++)
		{
			*scl0++ = imul[bs_getbits8(bs,6)];
			*scl0++ = imul[bs_getbits8(bs,6)];
		}
	}
}

/*************************** Layer II stuff ***************************/

static void II_decode(layer12* layer, bytebuf* bs, const frame_params* fr_ps)
{
	unsigned char* bit_alloc;
	unsigned char* scf;
	unsigned char* scl0;
	unsigned char* scl1;
	unsigned char* scl2;
	unsigned int val;
	int i, j;

	// Read and decode bit allocation table of up to 4 bits per subband
	//   one per channel per band in the lower subbands
	//   one per band in the intensity stereo subbands
	bit_alloc = &layer->bit_alloc[0];

	// Normal bands
	for (i = 0; i < layer->jsbound; i++)
	{
		val = (*layer->alloc)[i][0];
		for (j = 0; j < fr_ps->channels; j++)
			*bit_alloc++ = bs_getbits8(bs, val);
	}

	// Intensity stereo bands
	for (; i < layer->sblimit; i++)
	{
		val = bs_getbits8(bs, (*layer->alloc)[i][0]);
		*bit_alloc++ = val;
		*bit_alloc++ = val;
	}

	// Read scale factor mappings, 2 bits per channel per for each subband
	bit_alloc = &layer->bit_alloc[0];

	// use scale_index[0] as temporary holder
	scf = &layer->scale_index[0][0];
	for (i = layer->sblimit*fr_ps->channels; i; i--)
	{
		if (*bit_alloc++)
			*scf++ = bs_getbits8(bs,2); // 2 bit scfsi
	}

	// Read the scale factors corresponding to each defined mapping
	// note: scf now points just after the last defined mapping
	scl0 = &layer->scale_index[0][0];
	scl1 = &layer->scale_index[1][0];
	scl2 = &layer->scale_index[2][0];

	for (; scl0 < scf;)
	{
		switch (*scl0)
		{
			case 0:
				// All three scale factors transmitted
				*scl0++ = imul[bs_getbits8(bs,6)];
				*scl1++ = imul[bs_getbits8(bs,6)];
				*scl2++ = imul[bs_getbits8(bs,6)];
			break;
			case 1:
				// Scale factor 1 & 3 transmitted
				*scl0++ =
				*scl1++ = imul[bs_getbits8(bs,6)];
				*scl2++ = imul[bs_getbits8(bs,6)];
			break;
			case 2 :
				// Only one scale factor transmitted
				*scl0++ =
				*scl1++ =
				*scl2++ = imul[bs_getbits8(bs,6)];
			break;
			case 3 :
				// Scale factor 1 & 2 transmitted
				*scl0++ = imul[bs_getbits8(bs,6)];
				*scl1++ =
				*scl2++ = imul[bs_getbits8(bs,6)];
			break;
		}
	}
}

/*
 * Sample decompression in layer I and II
 *
 * Each sample is read in a format encoding 2*n+1 different possible values called steps.
 * These codes in range [0, steps - 1] must then be mapped into range [-1.0, 1.0].
 * This means recentering the sample to range [-n, n] and multiplying by 1/n.
 *
 */

/***************************** Layer I stuff ***********************/

typedef struct
{
	int n;     // steps >> 1
	int c;     // 2^31/steps
} quant_class1;

static const quant_class1 quant1[17] =
{
	{     0, 0x00000000},
	{     0, 0x00000000},
	{     1, 0x2AAAAAAA},
	{     3, 0x12492492},
	{     7, 0x08888888},
	{    15, 0x04210842},
	{    31, 0x02082082},
	{    63, 0x01020408},
	{   127, 0x00808080},
	{   255, 0x00402010},
	{   511, 0x00200802},
	{  1023, 0x00100200},
	{  2047, 0x00080080},
	{  4095, 0x00040020},
	{  8191, 0x00020008},
	{ 16383, 0x00010002},
	{ 32767, 0x00008000},
};

static void I_dequantize_sample(const layer12*        layer
							   , int                  fraction[2][3][SBLIMIT]
							   , bytebuf*             bs
							   , const unsigned char* bit_alloc
							   , const unsigned char* scale_index
							   , const frame_params*  fr_ps)
{
	const quant_class1* pqc;
	int i, j, nb, dd;

	// Normal bands
	for (i = 0; i < layer->jsbound; i++)
	{
		for (j = 0; j < fr_ps->channels; j++)
		{
			nb = *bit_alloc++;
			if (nb)
			{
				pqc = &quant1[nb];
				dd = bs_getbits(bs, nb);
				dd -= pqc->n;
				dd *= pqc->c;
				fraction[j][0][i] = EXPn3(dd, *scale_index++);
			}
			else
				fraction[j][0][i] = 0;
		}
	}

	// Intensity stereo bands
	for (; i < SBLIMIT; i++)
	{
		nb = *bit_alloc++;
		if (nb)
		{
			pqc = &quant1[nb];
			dd = bs_getbits(bs, nb);
			dd -= pqc->n;
			dd *= pqc->c;
			fraction[0][0][i] = EXPn3(dd, *scale_index++);
			fraction[1][0][i] = EXPn3(dd, *scale_index++);
		}
		else
		{
			fraction[0][0][i] = 0;
			fraction[1][0][i] = 0;
		}
	}
}

/*************************** Layer II stuff ************************/

typedef struct
{
	int          steps;
	int          c;     // 2^30/steps
	unsigned int group;
	unsigned int bits;
} quant_class2;

static const quant_class2 quant2[17] =
{
	{    3, 0x2AAAAAAA, 3,  5},
	{    5, 0x19999999, 3,  7},
	{    7, 0x12492492, 1,  3},
	{    9, 0x0E38E38E, 3, 10},
	{   15, 0x08888888, 1,  4},
	{   31, 0x04210842, 1,  5},
	{   63, 0x02082082, 1,  6},
	{  127, 0x01020408, 1,  7},
	{  255, 0x00808080, 1,  8},
	{  511, 0x00402010, 1,  9},
	{ 1023, 0x00200802, 1, 10},
	{ 2047, 0x00100200, 1, 11},
	{ 4095, 0x00080080, 1, 12},
	{ 8191, 0x00040020, 1, 13},
	{16383, 0x00020008, 1, 14},
	{32767, 0x00010002, 1, 15},
	{65535, 0x00008000, 1, 16}
};

static void II_buffer_dequantize( const layer12*       layer
								, bytebuf*             bs
								, int                  fraction[2][3][SBLIMIT]
								, const unsigned char* bit_alloc
								, const unsigned char* scale_index
								, const frame_params*  fr_ps)
{
	const quant_class2* pqc;
	int i, j, v, val;

	// Normal bands
	for (i = 0; i < layer->jsbound; i++)
	{
		for (j = 0; j < fr_ps->channels; j++)
		{
			v = *bit_alloc++;

			if (v)
			{
				val = *scale_index++;
				pqc = &quant2[(*layer->alloc)[i][v]];

				// Check for grouping in subband
				if (pqc->group != 3)
				{
					// No grouping, read 3 samples
					unsigned int k = pqc->bits;

					v = bs_getbits(bs, k);
					v -= pqc->steps>>1;
					v *= pqc->c;
					fraction[j][0][i] = EXPn3(v, val);

					v = bs_getbits(bs, k);
					v -= pqc->steps>>1;
					v *= pqc->c;
					fraction[j][1][i] = EXPn3(v, val);

					v = bs_getbits(bs, k);
					v -= pqc->steps>>1;
					v *= pqc->c;
					fraction[j][2][i] = EXPn3(v, val);
				}
				else
				{
					// Grouping, read combined samples
					unsigned int c = bs_getbits(bs, pqc->bits);

					v = c % pqc->steps;
					v -= pqc->steps>>1;
					v *= pqc->c;
					fraction[j][0][i] = EXPn3(v, val);

					c /= pqc->steps;

					v = c % pqc->steps;
					v -= pqc->steps>>1;
					v *= pqc->c;
					fraction[j][1][i] = EXPn3(v, val);

					c /= pqc->steps;

					v = c % pqc->steps;
					v -= pqc->steps>>1;
					v *= pqc->c;
					fraction[j][2][i] = EXPn3(v, val);
				}
			}
			else
			{
				fraction[j][0][i] = 0;
				fraction[j][1][i] = 0;
				fraction[j][2][i] = 0;
			}
		}
	}

	// Intensity stereo bands
	for (; i < layer->sblimit; i++)
	{
		v = *bit_alloc;
		bit_alloc += 2; // was duplicated for easy scale factor decode

		if (v)
		{
			pqc = &quant2[(*layer->alloc)[i][v]];

			// Check for grouping in subband
			if (pqc->group != 3)
			{
				// No grouping, read 3 samples
				unsigned int k = pqc->bits;

				v = bs_getbits(bs, k);
				v -= pqc->steps>>1;
				fraction[0][0][i] = v * pqc->c;

				v = bs_getbits(bs, k);
				v -= pqc->steps>>1;
				fraction[0][1][i] = v * pqc->c;

				v = bs_getbits(bs, k);
				v -= pqc->steps>>1;
				fraction[0][2][i] = v * pqc->c;
			}
			else
			{
				// Grouping, read combined samples
				unsigned int c = bs_getbits(bs, pqc->bits);

				v = c % pqc->steps;
				v -= pqc->steps>>1;
				fraction[0][0][i] = v * pqc->c;

				c /= pqc->steps;

				v = c % pqc->steps;
				v -= pqc->steps>>1;
				fraction[0][1][i] = v * pqc->c;

				c /= pqc->steps;

				v = c % pqc->steps;
				v -= pqc->steps>>1;
				fraction[0][2][i] = v * pqc->c;
			}
			val = scale_index[1];
			fraction[1][0][i] = EXPn3(fraction[0][0][i], val);
			fraction[1][1][i] = EXPn3(fraction[0][1][i], val);
			fraction[1][2][i] = EXPn3(fraction[0][2][i], val);
			val = scale_index[0];
			scale_index += 2;
			fraction[0][0][i] = EXPn3(fraction[0][0][i], val);
			fraction[0][1][i] = EXPn3(fraction[0][1][i], val);
			fraction[0][2][i] = EXPn3(fraction[0][2][i], val);
		}
		else
		{
			fraction[0][0][i] = 0;
			fraction[0][1][i] = 0;
			fraction[0][2][i] = 0;
			fraction[1][0][i] = 0;
			fraction[1][1][i] = 0;
			fraction[1][2][i] = 0;
		}
	}

	for (j = 0; j < fr_ps->channels; j++)
		for (v = 0; v < 3; v++)
			for (i = layer->sblimit; i < SBLIMIT; i++)
				fraction[j][v][i] = 0;
}

void do_layer1(stream* S)
{
	int		i, j;
	int		clip;
	layer12*	layer = &S->l.lay12;

	I_decode(layer, &S->mp3b, &S->fr_ps);

	clip = 0;
	for (i = SCALE_BLOCK; i; i--)
	{
		I_dequantize_sample(layer, layer->fraction, &S->mp3b, layer->bit_alloc, layer->scale_index[0], &S->fr_ps);

		for (j = 0; j < S->fr_ps.channels; j++)
		{
			int16_t* psample = S->sndb.free + j;
			clip += SubBandSynthesis(S, &(layer->fraction[j][0][0]), j, psample, S->fr_ps.channels);
		}
		S->sndb.free += S->fr_ps.channels*SBLIMIT;
		if (S->sndb.free >= S->sndb.last)
			S->sndb.free -= S->sndb.size;
	}

#ifdef MAKEABS
	if (clip) Log("clipped %d at frame %u\n", clip, S->frameNum);
#endif
}

void do_layer2(stream* S)
{
	int		i, j, k;
	int		clip;
	layer12*	layer = &S->l.lay12;

	II_decode(layer, &S->mp3b, &S->fr_ps);

	clip = 0;
	for (i = 0; i < SCALE_BLOCK; i++)
	{	// SCALE_BLOCK = 12
		II_buffer_dequantize(layer, &S->mp3b, layer->fraction, layer->bit_alloc, layer->scale_index[i>>2], &S->fr_ps);

		for (j = 0; j < 3; j++)
		{
			for (k = 0; k < S->fr_ps.channels; k++)
			{
				int16_t* psample = S->sndb.free + k;
				clip += SubBandSynthesis(S, &(layer->fraction[k][j][0]), k, psample, S->fr_ps.channels);
			}
			S->sndb.free += S->fr_ps.channels*SBLIMIT;
			if (S->sndb.free >= S->sndb.last)
				S->sndb.free -= S->sndb.size;
		}
	}

#ifdef MAKEABS
	if (clip) Log("clipped %d at frame %u\n", clip, S->frameNum);
#endif
}
