#include "swis.h"
#include "GlobHdr.h"
#include "Channels.h"
#include "Utils.h"
#include "IFileTypes.h"
#include "ICodec.h"
#include "Codecs.h"
#include "IStream.h"
#include "Log.h"

#include <stdio.h>
#include <string.h>

#define codec_waitclearoutput   0x00000001
#define codec_waitstreamready   0x00000002
#define codec_waittotaltime     0x00000004
#define codec_waitall           0x00000007
#define codec_state_fixduration 0x00000008

typedef enum
{ Decode_Init        = 0
, Decode_InfoHdr     = 1
, Decode_InfoData    = 2
, Decode_MetaHdr     = 3
, Decode_MetaData    = 4
, Decode_SeekTable   = 5
, Decode_Comments    = 6
, Decode_Sync        = 7
, Decode_ClearSync   = 8
, Decode_Frame       = 9
, Decode_Finished    = 10
, Decode_FatalError  = 11
} Decode_State;

typedef enum
{ FLAC_Coupling_Left        = 0
, FLAC_Coupling_Right       = 1
, FLAC_Coupling_Middle      = 2
, FLAC_Coupling_Independent = 3
} FLAC_Coupling;

typedef struct
{
	int64_t       samplenr;      // index of first sample in frame
	int           blocksize;     // number of samples in frame
	int           samplerate;    // should match info.samplerate
	int           channels;      // should match info.channels
	FLAC_Coupling coupling;      // stereo coupling mode
	int           bitspersample; // should match info.bitspersample
	bytebuf       bs;            // bit buffer used by decode
} frame;                         // some duplication as frame is readable even without fLaC header

typedef struct
{
	int64_t       samplenr;      // index of first sample in frame
	int64_t       offset;        // offset from start of first frame
} seekpoint;

typedef struct
{
	struct
	{
		int32_t* data;
		int32_t  area;
		int32_t  size;
		int32_t* start;
		int32_t* free;
		int32_t* last;
		volatile int32_t  ostart;
		volatile int32_t  ofree;
		volatile int      finishflag;
	} output;
	struct
	{
		bool      more;               // more metadata blocks follow?
		int       type;               // metadata type
		int       length;             // remaining unread metadata size
	} meta;
	Decode_State state;
	struct
	{
		struct
		{
			int min;                  // mimimal number of samples in a frame
			int max;                  // maximal number of samples in a frame
		} blocksize;
		struct
		{
			int min;                 // mimimal number of bytes used for a frame?
			int max;                 // maximal number of bytes used for a frame?
		} framesize;
		int     samplerate;          // Sample rate in Hz
		int     channels;            // Number of channels
		int     bitspersample;       // Bits per sample
		int64_t	totalsamples;        // Number of samples stored in the file
		int64_t	decodedsamples;
	} info;
	struct
	{
		seekpoint*  table;
		int         count;
		int         size;
		int         offset0;
	} seek;
	frame frame;
	uint8_t* psyncstart;
	uint8_t* psavestart;
	int      newblocksize;
	struct
	{
		int     prec;
		int     shift;
		int32_t pred[32];      // LPC predictors
	} lpc;
	struct
	{
		int     prec;
	} rice;
	int	version;
	int	bitrate;
	int	initstate;
	int64_t	playedsamples; // May be negative when playing last samples
	                       // while we have restarted decoding
	int64_t	totalsamples;
	int32_t samples[65536+8]; // +8 cf ASM
} work_t;

typedef struct
{
	int32_t* ppreds;
	int      orders;
	int32_t* psample;
	int      samples;
	int      shift;
} asm_lpc;
extern void Flac_ASM_LPC32(asm_lpc*);
extern void Flac_ASM_LPC64(asm_lpc*);

#define flac_chunksize	(64*1024)

static const _kernel_oserror Err_NotFLAC =
{ErrNum_CodecError, "Not a FLAC stream."};
static const _kernel_oserror Err_FLAC_Corrupted =
{ErrNum_CodecError, "FLAC stream badly corrupted."};

static const char FLAC_Type[] = "FLAC";


// CRC-8, poly = x^8 + x^2 + x^1 + x^0, init = 0
static uint8_t const crc8[256] =
{ 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15
, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D
, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65
, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D
, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5
, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD
, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85
, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD
, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2
, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA
, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2
, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A
, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32
, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A
, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42
, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A
, 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C
, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4
, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC
, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4
, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C
, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44
, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C
, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34
, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B
, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63
, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B
, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13
, 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB
, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83
, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB
, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
};

// CRC-16, poly = x^16 + x^15 + x^2 + x^0, init = 0
static int crc16[256] =
{ 0x0000,  0x8005,  0x800f,  0x000a,  0x801b,  0x001e,  0x0014,  0x8011
, 0x8033,  0x0036,  0x003c,  0x8039,  0x0028,  0x802d,  0x8027,  0x0022
, 0x8063,  0x0066,  0x006c,  0x8069,  0x0078,  0x807d,  0x8077,  0x0072
, 0x0050,  0x8055,  0x805f,  0x005a,  0x804b,  0x004e,  0x0044,  0x8041
, 0x80c3,  0x00c6,  0x00cc,  0x80c9,  0x00d8,  0x80dd,  0x80d7,  0x00d2
, 0x00f0,  0x80f5,  0x80ff,  0x00fa,  0x80eb,  0x00ee,  0x00e4,  0x80e1
, 0x00a0,  0x80a5,  0x80af,  0x00aa,  0x80bb,  0x00be,  0x00b4,  0x80b1
, 0x8093,  0x0096,  0x009c,  0x8099,  0x0088,  0x808d,  0x8087,  0x0082
, 0x8183,  0x0186,  0x018c,  0x8189,  0x0198,  0x819d,  0x8197,  0x0192
, 0x01b0,  0x81b5,  0x81bf,  0x01ba,  0x81ab,  0x01ae,  0x01a4,  0x81a1
, 0x01e0,  0x81e5,  0x81ef,  0x01ea,  0x81fb,  0x01fe,  0x01f4,  0x81f1
, 0x81d3,  0x01d6,  0x01dc,  0x81d9,  0x01c8,  0x81cd,  0x81c7,  0x01c2
, 0x0140,  0x8145,  0x814f,  0x014a,  0x815b,  0x015e,  0x0154,  0x8151
, 0x8173,  0x0176,  0x017c,  0x8179,  0x0168,  0x816d,  0x8167,  0x0162
, 0x8123,  0x0126,  0x012c,  0x8129,  0x0138,  0x813d,  0x8137,  0x0132
, 0x0110,  0x8115,  0x811f,  0x011a,  0x810b,  0x010e,  0x0104,  0x8101
, 0x8303,  0x0306,  0x030c,  0x8309,  0x0318,  0x831d,  0x8317,  0x0312
, 0x0330,  0x8335,  0x833f,  0x033a,  0x832b,  0x032e,  0x0324,  0x8321
, 0x0360,  0x8365,  0x836f,  0x036a,  0x837b,  0x037e,  0x0374,  0x8371
, 0x8353,  0x0356,  0x035c,  0x8359,  0x0348,  0x834d,  0x8347,  0x0342
, 0x03c0,  0x83c5,  0x83cf,  0x03ca,  0x83db,  0x03de,  0x03d4,  0x83d1
, 0x83f3,  0x03f6,  0x03fc,  0x83f9,  0x03e8,  0x83ed,  0x83e7,  0x03e2
, 0x83a3,  0x03a6,  0x03ac,  0x83a9,  0x03b8,  0x83bd,  0x83b7,  0x03b2
, 0x0390,  0x8395,  0x839f,  0x039a,  0x838b,  0x038e,  0x0384,  0x8381
, 0x0280,  0x8285,  0x828f,  0x028a,  0x829b,  0x029e,  0x0294,  0x8291
, 0x82b3,  0x02b6,  0x02bc,  0x82b9,  0x02a8,  0x82ad,  0x82a7,  0x02a2
, 0x82e3,  0x02e6,  0x02ec,  0x82e9,  0x02f8,  0x82fd,  0x82f7,  0x02f2
, 0x02d0,  0x82d5,  0x82df,  0x02da,  0x82cb,  0x02ce,  0x02c4,  0x82c1
, 0x8243,  0x0246,  0x024c,  0x8249,  0x0258,  0x825d,  0x8257,  0x0252
, 0x0270,  0x8275,  0x827f,  0x027a,  0x826b,  0x026e,  0x0264,  0x8261
, 0x0220,  0x8225,  0x822f,  0x022a,  0x823b,  0x023e,  0x0234,  0x8231
, 0x8213,  0x0216,  0x021c,  0x8219,  0x0208,  0x820d,  0x8207,  0x0202
};

static const _kernel_oserror* FLAC_Accept(IStream* s, const Desc* desc)
{
	IGNORE(s);
	IGNORE(desc);

	return NULL;
}

/*-------------
 * FLAC_Load
 */

static const _kernel_oserror* FLAC_Load(IStream* s)
{
	const _kernel_oserror* e;
	work_t* w;

	if (s->codec.prefn != &FLAC_Codec)
	{
		// File type FLAC?
		if (s->source.os_filetype != IFileType_FLAC)
			return NULL;
	}

	// StrongARM ?
	e = check_hardware();
	if (e) return e;

	// prepare buffer of FLAC stuff
	e = IStream_Alloc(s, (void**) &w, sizeof(*w));
	if (e) return e;

	s->codec.data = w;
	memset(w, 0, sizeof(*w));

	s->info.chunksize = flac_chunksize;

	// prepare type info
	e = IStream_SetText(s, 1, EMeta_StreamType, FLAC_Type, 0);
	if (e) return e;

	// set initial status
	s->codec.fn = &FLAC_Codec;
	w->state = Decode_Init;
	w->initstate = codec_waitall | codec_state_fixduration;
	w->frame.bs.start = NULL;
	w->psyncstart = NULL;
	w->psavestart = NULL;


	return NULL;
}

static const _kernel_oserror* FLAC_Unload(IStream* s)
{
	work_t* w = s->codec.data;

	if (w)
	{
		IStream_DeleteOutputBuffer(s, &w->output.area);
		IStream_Free(s, (void**) &w->seek.table);

		IStream_Free(s, &s->codec.data);
	}

	return NULL;
}

static const _kernel_oserror* FLAC_IsReady(IStream* s, bool* b)
{
	work_t* w = s->codec.data;
	const _kernel_oserror* e;
	int size = 0;
	int64_t t1, t2;

	*b = false;

	if (w->seek.offset0 == 0)
		return NULL;

	// extract info
	w->version = 100;

	w->initstate = codec_state_fixduration;

	w->totalsamples = w->info.totalsamples;
	s->info.Channels = w->info.channels;
	s->info.ChannelsLayout = Channels_GetDefaultLayout(s->info.Channels);
	s->info.SampleRate = w->info.samplerate;
	if (w->info.totalsamples > 0)
	{
		// bit=frq*size*8/samples
		t1 = (int64_t) (uint32_t) (s->source.end - s->source.start);
		t1 *= (uint32_t) (w->info.samplerate*8);
		t2 = w->info.totalsamples;
		t1 += t2>>1; // for rounding
		w->bitrate = (int)(int64_t) (t1 / t2);
	}
	else
		w->bitrate = 0;
	s->info.MeanBitRate = (w->bitrate+500)/1000;

	// Allocate output buffer to use
	e = IStream_NewOutputBuffer(s, &size, &w->output.area, (void**) &w->output.data);
	if (e) return e;
	w->output.size = size / (4*w->info.channels);
	w->output.last = w->output.data + w->output.size - 1;

	// get output buffer info
	Channels_Init32(s, w->output.data, w->output.size, w->info.samplerate);

	// Build stream list
	Channels_List(s, w->info.channels);

	*b = true;

	return NULL;
}

static const _kernel_oserror* FLAC_BufferSizes(IStream* s, int* size, int* filled)
{
	work_t* w = s->codec.data;

	*size = w->output.size;
	*filled = w->output.ofree - w->output.ostart;
	if (*filled < 0) *filled += w->output.size;
	if (w->output.finishflag) *size = *filled;

	return NULL;
}

static const _kernel_oserror* FLAC_InError(IStream* s)
{
	IGNORE(s);

	return NULL;
}

static const _kernel_oserror* FLAC_ReadPos(IStream* s, int64_t* pos)
{
	work_t* w = s->codec.data;
	int64_t t1 = w->playedsamples;

	// take loops into account
	if (t1 < 0) t1 += w->totalsamples;
	t1 *= (uint32_t) 1000; // ms
	t1 /= (uint32_t) w->info.samplerate;

	*pos = t1;

	return NULL;
}

static const _kernel_oserror* FLAC_ReadDuration(IStream* s, int64_t* duration)
{
	work_t* w = s->codec.data;
	uint64_t t1 = w->totalsamples;

	t1 *= (uint32_t) 1000; // ms
	t1 /= (uint32_t) w->info.samplerate;

	*duration = t1;

	return NULL;
}

static const _kernel_oserror* FLAC_SetPos(IStream* s, int64_t position)
{
	const _kernel_oserror* e;
	work_t* w = s->codec.data;
	int ioff;

	ioff = _kernel_irqs_disabled();
	if (!ioff) _kernel_irqs_off();

	// Clear all buffers
	w->output.ostart = 0;
	w->output.ofree = 0;
	w->output.finishflag = 0;
	s->inb->start = 0;
	s->inb->free = 0;
	s->inb->finishflag = 0;

	if (w->state == Decode_Frame)
		w->state = Decode_Sync;

	w->frame.bs.start = NULL;
	w->psyncstart = NULL;
	w->psavestart = NULL;

	IStream_SaveInb(s);

	// reset position parameters
	w->playedsamples = position;
	w->playedsamples *= w->info.samplerate;
	w->playedsamples /= 1000;

	// try to fix song duration only if played from start
	if (position)
		w->initstate &= ~codec_state_fixduration;
	else
		w->initstate |= codec_state_fixduration;

	Channels_Reset(s);

	if (!ioff) _kernel_irqs_on();

	// Move in file to new pos
	if (w->seek.size > 1)
	{
		int i;
		int64_t numa, numb, den;

		position = w->playedsamples;

		// Use seek table
		for (i = 0; i < w->seek.size; i++)
		{
			if (position < w->seek.table[i].samplenr)
				break;
		}

		if (i == 0)
		{
			numa = position;
			numb = w->seek.table[0].offset;
			den  = w->seek.table[0].samplenr;
			position = numa*numb/den;
		}
		else if (i == w->seek.size)
		{
			numa = position - w->seek.table[i-1].samplenr;
			numb = (int64_t)(int)(s->source.end - s->source.start);
			numb -= w->seek.table[i-1].offset;
			den  = w->info.totalsamples - w->seek.table[i-1].samplenr;
			position = numa*numb/den + w->seek.table[i-1].offset;
		}
		else
		{
			numa = position - w->seek.table[i-1].samplenr;
			numb = w->seek.table[i].offset - w->seek.table[i-1].offset;
			den  = w->seek.table[i].samplenr - w->seek.table[i-1].samplenr;
			position = numa*numb/den + w->seek.table[i-1].offset;
		}

		position += w->seek.offset0;
	}
	else
	{
		// Guess from mean bitrate
		position *= s->info.MeanBitRate;
		position >>= 3;
	}
	e = IStream_SetPos(s, (int) position);

	return e;
}

static const _kernel_oserror* FLAC_ClearInput(IStream* s)
{
	// clear input buffer
	work_t* w = s->codec.data;
	int ioff;

	ioff = _kernel_irqs_disabled();
	if (!ioff) _kernel_irqs_off();

	w->output.finishflag = 0;
	s->inb->start = 0;
	s->inb->free = 0;
	s->inb->finishflag = 0;

	w->state = Decode_Sync;
	w->frame.bs.start = NULL;
	w->psyncstart = NULL;
	w->psavestart = NULL;

	if (!ioff) _kernel_irqs_on();

	return NULL;
}

static int32_t* get_ricecodes(bytebuf *b, int N, int32_t* pv, int nv)
{
	int i;
	uint32_t uval;

	if (N)
	{
		for (; nv > 0; nv--)
		{
			i = bs_getUnary(b, 0);

			uval = bs_getbits(b, N) + (i << N);

			*pv++ = (uval >> 1) ^ -(uval & 1);
		}
	}
	else
	{
		for (; nv > 0; nv--)
		{
			uval = bs_getUnary(b, 0);

			*pv++ = (uval >> 1) ^ -(uval & 1);
		}
	}

	return pv;
}

static int64_t get_utf8(bytebuf *b)
{
    int64_t val;
    int ones = bs_getbits(b, 8);
    int bytes = 0;

	while (ones & 0x80)
	{
		bytes++;
		ones <<= 1;
	}

	ones = (ones & 0xff) >> bytes;

	if ((bytes == 1) || (bytes == 8))
		return -1;
	if (bytes) bytes--;
	val = ones;

    while(bytes--)
    {
        int tmp = bs_getbits(b, 8);

        if((tmp >> 6) != 2)
            return -2;
        tmp &= 0x3f;
        val = (val << 6) | tmp;
    }

    return val;
}

static int32_t ilog2(uint32_t v)
{
	int32_t ret = 0;

	while(v)
	{
		ret++;
		v >>= 1;
	}

	return ret;
}

static const int Block_Sizes[16] =
{ 0, 192, 576, 1152, 2304, 4608, 0, 0
, 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15
};

static const int Sample_Rates[16] =
{ 0, 88200, 176400, 192000, 8000, 16000, 22050, 24000
, 32000, 44100, 48000, 96000, 0, 0, 0, -1};

static const int BitsPerSamples[8] =
{ 0, 8, 12, -1, 16, 20, 24, -1};

/*------------------
 * FLAC_DecodeFrame
 */

static void FLAC_DecodeFrame(frame* f, work_t* w)
{
	const uint8_t* pframestart = f->bs.start; // see CRC
	int channel, samples, refsize = f->blocksize;
	int32_t val;
	bool bOK = true;

	// Init buffer
	f->bs.count = f->bs.free - f->bs.start;
	if (f->bs.count < 0) f->bs.count += f->bs.size;
	f->bs.count <<= 3;
	f->bs.bitindex = 0;

	//
	// Frame header
	//
	bs_skipbits(&f->bs, 16);
	// Interpret them later
	f->blocksize = bs_getbits(&f->bs, 4);
	f->samplerate = bs_getbits(&f->bs, 4);
	val = bs_getbits(&f->bs, 4);
	if (val & 8)
	{
		f->channels = 2;
		f->coupling = (FLAC_Coupling) (val & 7);

		if (f->coupling > FLAC_Coupling_Middle)
		{
			Log("Bad Frame Header, Stereo coupling %d", f->coupling);
			return;
		}
	}
	else
	{
		f->channels = 1 + val;
		f->coupling = FLAC_Coupling_Independent;
	}

	if (f->channels != w->info.channels)
	{
		Log("Bad Frame Header, number of channels");
		return;
	}

	f->bitspersample = bs_getbits(&f->bs, 3);

	if (f->bitspersample == 0)
		f->bitspersample = w->info.bitspersample;
	else
		f->bitspersample = BitsPerSamples[f->bitspersample];

	if ((bs_get1bit(&f->bs) != 0)
	||  (f->bitspersample != w->info.bitspersample))
	{
		Log("Bad Frame Header, number of bits per sample");
		return;
	}

	f->samplenr = get_utf8(&f->bs);

	if (f->samplenr < 0)
	{
		Log("Bad Frame Header, invalid sample number");
		return;
	}

	if (w->info.blocksize.min == w->info.blocksize.max)
		f->samplenr *= w->info.blocksize.min;

	switch(f->blocksize)
	{
		case 0: f->blocksize = w->info.blocksize.min; break;
		case 6: f->blocksize = 1 + bs_getbits(&f->bs, 8); break;
		case 7: f->blocksize = 1 + bs_getbits(&f->bs, 16); break;
		default: f->blocksize = Block_Sizes[f->blocksize];
	}

	if ((f->blocksize <= 0)
	||  (w->info.blocksize.max < f->blocksize))
	{
		Log("Bad Frame Header, invalid blocksize");
		return;
	}

	if (refsize != f->blocksize)
	{
		Log("Bad Frame Header, invalid blocksize, internal corruption");
		return;
	}

	switch(f->samplerate)
	{
		case  0: f->samplerate = w->info.samplerate; break;
		case 12: f->samplerate = 1000*bs_getbits(&f->bs, 8); break;
		case 13: f->samplerate = bs_getbits(&f->bs, 16); break;
		case 14: f->samplerate = 10*bs_getbits(&f->bs, 16); break;
		default: f->samplerate = Sample_Rates[f->samplerate];
	}

	if (f->samplerate != w->info.samplerate)
	{
		Log("Bad Frame Header, invalid samplerate");
		return;
	}

	int crc = 0, ncrc;
	const uint8_t* p = pframestart;

	if (p > f->bs.start)
	{
		while (p <= f->bs.last)
			crc = crc8[crc ^ *p++];
		p = f->bs.data;
	}

	while (p < f->bs.start)
		crc = crc8[crc ^ *p++];

	ncrc = bs_getbits(&f->bs, 8);
	if (ncrc != crc)
	{
		Log("Bad Frame Header, CRC %02x, should be %02x", crc, ncrc);
		return;
	}

	// Header is OK, from this point we store f->blocksize samples (zeroed if some error occurs)

	// Fix position
	int ioff = _kernel_irqs_disabled();
	if (!ioff) _kernel_irqs_off();

	w->playedsamples = f->samplenr - (w->output.free - w->output.start);
	if (w->output.free < w->output.start)
		w->playedsamples -= w->output.size;

	if (!ioff) _kernel_irqs_on();

	//
	// SubFrames (one per channel)
	//
	for (channel = 0; channel < w->info.channels; channel++)
	{
		int type;
		int wasted;
		int bps;
		int32_t* psample;

		if (bs_get1bit(&f->bs))
		{
			Log("Bad SubFrame Header %d", channel);
			bOK = false;
			break;
		}

		type = bs_getbits(&f->bs, 6);
		wasted = 0;

		if (bs_get1bit(&f->bs))
		{
			wasted = 1;
			while (!bs_get1bit(&f->bs) && (wasted < 32))
				wasted++;
		}

		if (wasted >= f->bitspersample)
		{
			Log("Bad SubFrame Header %d, Invalid Waste", channel);
			bOK = false;
			break;
		}
		bps = f->bitspersample - wasted;
		if (channel == 0)
		{
			if (f->coupling == FLAC_Coupling_Right)
				bps++;
		}
		else
		{
			if ((f->coupling == FLAC_Coupling_Left)
			||  (f->coupling == FLAC_Coupling_Middle))
				bps++;
		}

		if (type == 0)
		{
			// Constant
			val = bs_getsbits(&f->bs, bps);

			for (samples = f->blocksize, psample = w->samples; samples; samples--)
				*psample++ = val;
		}
		else if (type == 1)
		{
			// Verbatim
			for (samples = f->blocksize, psample = w->samples; samples; samples--)
				*psample++ = bs_getsbits(&f->bs, bps);
		}
		else
		{
			int order;
			int log2partitions;
			int part;

			// Modes with residue
			if (type & 0x20)
			{
				int i;
				int32_t* p;

				// LPC
				order = 1 + (type & 0x1f);

				if (order > f->blocksize)
				{
					Log("Bad SubFrame %d, LPC order %d larger than blocksize %d"
					   , channel, order, f->blocksize);
					bOK = false;
					break;
				}

				// Warmup samples
				for (samples = order, psample = w->samples; samples; samples--)
					*psample++ = bs_getsbits(&f->bs, bps);

				w->lpc.prec = 1 + bs_getbits(&f->bs, 4);
				if (w->lpc.prec == 16)
				{
					Log("Bad SubFrame %d, LPC precision", channel);
					bOK = false;
					break;
				}

				w->lpc.shift = bs_getsbits(&f->bs, 5);

				// Predictors
				for (i = order, p = w->lpc.pred; i; i--)
					*p++ = bs_getsbits(&f->bs, w->lpc.prec);
			}
			else if ((type & 0x38) == 0x08)
			{
				// Fixed
				order = (type & 0x07);

				if ((order > 4) || (order >= f->blocksize))
				{
					Log("Bad SubFrame %d, Fixed Invalid order %d larger than 4, blocksize %d"
					   , channel, order, f->blocksize);
					bOK = false;
					break;
				}

				// Warmup samples
				for (samples = order, psample = w->samples; samples; samples--)
					*psample++ = bs_getsbits(&f->bs, bps);
			}
			else
			{
				Log("Bad SubFrame Header %d Type %d", channel, type);
				bOK = false;
				break;
			}

			// Residues
			val = bs_getbits(&f->bs, 2);
			if (val == 0)
				w->rice.prec = 4;
			else if (val == 1)
				w->rice.prec = 5;
			else
			{
				Log("Bad SubFrame %d, Residual encoding", channel);
				bOK = false;
				break;
			}

			log2partitions = bs_getbits(&f->bs, 4);

			psample = w->samples + order;
			samples = (f->blocksize >> log2partitions) - order;

			if ((samples <= 0)
			|| (((f->blocksize >> log2partitions) << log2partitions) != f->blocksize))
			{
				Log("Bad SubFrame %d, rice order %d incompatible with order %d and blocksize %d"
				   , channel, log2partitions, order, f->blocksize);
				bOK = false;
				break;
			}

			for (part = (1 << log2partitions); part; part--)
			{
				int param = bs_getbits(&f->bs, w->rice.prec);

				if (param == ((1<<w->rice.prec) - 1))
				{
					int bits = bs_getbits(&f->bs, 5);

					for (; samples; samples--)
						*psample++ = bs_getsbits(&f->bs, bits);
				}
				else
				{
					psample = get_ricecodes(&f->bs, param, psample, samples);
				}

				samples = (f->blocksize >> log2partitions);
			}

			// Decode samples from warmup samples
			psample = w->samples + order;
			samples = f->blocksize - order;

			if (type & 0x20)
			{
				// LPC
#if 1
				asm_lpc params = {w->lpc.pred, order, psample, samples, w->lpc.shift};
				if (w->lpc.prec + bps + ilog2(order) < 32)
					Flac_ASM_LPC32(&params);
				else
					Flac_ASM_LPC64(&params);
#else
				if (w->lpc.prec + bps + ilog2(order) < 32)
				{
					for (; samples; samples--)
					{
						int32_t sum = 0;

						for (int i = 0; i < order; i++)
							sum += w->lpc.pred[i] * psample[-i-1];

						sum >>= w->lpc.shift;

						*psample++ += sum;
					}
				}
				else
				{
					for (; samples; samples--)
					{
						int64_t sum = 0;

						for (int i = 0; i < order; i++)
							sum += (int64_t) w->lpc.pred[i] * (int64_t) psample[-i-1];

						sum >>= w->lpc.shift;

						*psample++ += (int32_t) sum;
					}
				}
#endif
			}
			else
			{
				// Fixed
				switch(order)
				{
					case 1:
					{
						int32_t val1 = w->samples[0];

						for (; samples; samples--)
						{
							val1 += *psample;
							*psample++ = val1;
						}
					}
					break;
					case 2:
					{
						int32_t val2 = w->samples[0];
						int32_t val1 = w->samples[1];

						val2 = val1 - val2;

						for (; samples; samples--)
						{
						    val2 += (*psample);
						    val1 += val2;
							*psample++ = val1;
						}
					}
					break;
					case 3:
					{
						int32_t val3 = w->samples[0];
						int32_t val2 = w->samples[1];
						int32_t val1 = w->samples[2];

						val3 = val1 - 2*val2 + val3;
						val2 = val1 - val2;

						for (; samples; samples--)
						{
							val3 += (*psample);
							val2 += val3;
							val1 += val2;
							*psample++ = val1;
						}
					}
					break;
					case 4:
					{
						int32_t val4 = w->samples[0];
						int32_t val3 = w->samples[1];
						int32_t val2 = w->samples[2];
						int32_t val1 = w->samples[3];

						val4 = val1 - 3*val2 + 3*val3 - val4;
						val3 = val1 - 2*val2 + val3;
						val2 = val1 - val2;

						for (; samples; samples--)
						{
							val4 += (*psample);
							val3 += val4;
							val2 += val3;
							val1 += val2;
							*psample++ = val1;
						}
					}
					break;
				}
			}
		}

		if (f->coupling == FLAC_Coupling_Independent)
		{
			int scale = 32 - bps;
			int32_t* p = w->output.free + w->output.size*channel;
			int32_t* plast = w->output.last + w->output.size*channel;

			for (samples = f->blocksize, psample = w->samples; samples; samples--)
			{
				*p++ = (*psample++) << scale;
				if (p > plast) p -= w->output.size;
			}
		}
		else if (!channel)
		{
			int32_t* p = w->output.free;
			int32_t* plast = w->output.last;

			for (samples = f->blocksize, psample = w->samples; samples; samples--)
			{
				*p++ = (*psample++) << wasted;
				if (p > plast) p -= w->output.size;
			}
		}
		else
		{
			int scale = (32 - f->bitspersample);
			int32_t val;
			int32_t* p0 = w->output.free;
			int32_t* p1 = w->output.free + w->output.size;
			int32_t* plast = w->output.last;

			samples = f->blocksize;
			psample = w->samples;

			switch(f->coupling)
			{
				case FLAC_Coupling_Left:
				{
					for (; samples; samples--)
					{
						val = *p0;
						*p0++ = val << scale;
						val -= (*psample++) << wasted;
						*p1++ = val << scale;

						if (p0 > plast)
						{
							p0 -= w->output.size;
							p1 -= w->output.size;
						}
					}
				}
				break;
				case FLAC_Coupling_Right:
				{
					for (; samples; samples--)
					{
						val = (*psample++) << wasted;
						*p1++ = val << scale;
						val += *p0;
						*p0++ = val << scale;

						if (p0 > plast)
						{
							p0 -= w->output.size;
							p1 -= w->output.size;
						}
					}
				}
				break;
				case FLAC_Coupling_Middle:
				{
					uint32_t middle;

					for (; samples; samples--)
					{
						middle = *p0;
						val = (*psample++) << wasted;
						middle -= (val >> 1);
						*p0++ = (middle + val) << scale;
						*p1++ = middle << scale;

						if (p0 > plast)
						{
							p0 -= w->output.size;
							p1 -= w->output.size;
						}
					}
				}
				break;
			}
		}
	}

	//
	// Frame Footer
	//

	if (bOK)
	{
		// Padding to byte
		bs_align(&f->bs);

		// Long CRC
		crc = 0;
		p = pframestart;

		if (p > f->bs.start)
		{
			while (p <= f->bs.last)
				crc = ((crc << 8) ^ crc16[(crc >> 8) ^ *p++]) & 0xffff;
			p = f->bs.data;
		}

		while (p < f->bs.start)
			crc = ((crc << 8) ^ crc16[(crc >> 8) ^ *p++]) & 0xffff;

		ncrc = bs_getbits(&f->bs, 16);
		if (ncrc != crc)
		{
			Log("Frame corrupted, CRC %04x, should be %04x", crc, ncrc);
			bOK = false;
		}
		else if (bs_bitcount(&f->bs) > 0)
			Log("Skipping %d bytes after frame %lld", bs_bitcount(&f->bs) >> 3, f->samplenr);
	}

	if (!bOK)
	{
		Log("Clearing corrupted frame (%d channels, %d samples)", w->info.channels, f->blocksize);
		// void frame
		for (channel = 0; channel < w->info.channels; channel++)
		{
			int32_t* p = w->output.free + w->output.size*channel;
			int32_t* plast = w->output.last + w->output.size*channel;

			for (samples = f->blocksize; samples; samples--)
			{
				*p++ = 0;
				if (p > plast) p -= w->output.size;
			}
		}

	}

	w->info.decodedsamples += f->blocksize;
	w->output.free += f->blocksize;
	if (w->output.free > w->output.last)
		w->output.free -= w->output.size;
}

/*---------------
 * FLAC_Process
 */

static const _kernel_oserror* FLAC_Process(IStream* s)
{
	const _kernel_oserror* e = NULL;
	work_t* w = s->codec.data;
	int ioff = _kernel_irqs_disabled();
	int count;
	bool bMore = true;

	if (w->output.finishflag)
		return NULL;

	// Copy buffers info so that we are not bothered by interrupts
	if (!ioff) _kernel_irqs_off();
	w->output.start = w->output.data + w->output.ostart;
	if (w->output.start < w->output.data) w->output.start = w->output.data;
	if (w->output.start > w->output.last) w->output.start = w->output.last;
	w->output.free  = w->output.data + w->output.ofree;
	if (w->output.free < w->output.data) w->output.free = w->output.data;
	if (w->output.free > w->output.last) w->output.free = w->output.last;
	if (!ioff) _kernel_irqs_on();

	// While some frame remains to be decoded, use internal pointer
	if (w->frame.bs.start || w->psyncstart)
	{
		s->bitb.start = w->psavestart;
	}

	s->bitb.count = s->bitb.free - s->bitb.start;
	if (s->bitb.count < 0) s->bitb.count += s->bitb.size;
	s->bitb.count <<= 3;
	s->bitb.bitindex = 0;

	while (bMore)
	{
		count = bs_bitcount(&s->bitb);

		switch(w->state)
		{
			case Decode_Init:
			{
				int32_t val;

				if (count < 32)
				{
					if (s->inb->finishflag)
					{
						w->state = Decode_FatalError;
						e = &Err_NotFLAC;
					}
					bMore = false;
					break;
				}

				// First 4 bytes must be 'fLaC'
				val = bs_getbits(&s->bitb, 16) << 16;
				val |= bs_getbits(&s->bitb, 16);
				if (val != 0x664c6143)
				{
					Log("Flac INIT 'fLaC' %x != 0x664c6143", val);
					w->state = Decode_FatalError;
					e = &Err_NotFLAC;
					break;
				}
				w->state = Decode_InfoHdr;
			}
			break;
			case Decode_InfoHdr:
			{
				if (count < 32)
				{
					if (s->inb->finishflag)
					{
						w->state = Decode_FatalError;
						e = &Err_NotFLAC;
					}
					bMore = false;
					break;
				}

				w->meta.more = !bs_get1bit(&s->bitb);
				w->meta.type = bs_getbits(&s->bitb, 7);
				w->meta.length = bs_getbits(&s->bitb, 24);

				if ((w->meta.type != 0) || (w->meta.length < 34))
				{
					Log("Bad Flac Meta %d [length %d]", w->meta.type, w->meta.length);
					w->state = Decode_FatalError;
					e = &Err_FLAC_Corrupted;
					break;
				}

				w->state = Decode_InfoData;
			}
			break;
			case Decode_InfoData:
			{
				if (count < (w->meta.length * 8))
				{
					if (s->inb->finishflag)
					{
						w->state = Decode_FatalError;
						e = &Err_FLAC_Corrupted;
					}
					bMore = false;
					break;
				}

				w->info.blocksize.min = bs_getbits(&s->bitb, 16);
				w->info.blocksize.max = bs_getbits(&s->bitb, 16);
				w->info.framesize.min = bs_getbits(&s->bitb, 24);
				w->info.framesize.max = bs_getbits(&s->bitb, 24);
				w->info.samplerate = bs_getbits(&s->bitb, 20);
				w->info.channels = 1 + bs_getbits(&s->bitb, 3);
				w->info.bitspersample = 1 + bs_getbits(&s->bitb, 5);
				w->info.totalsamples = bs_getbits(&s->bitb, 12);
				w->info.totalsamples <<= 24;
				w->info.totalsamples += bs_getbits(&s->bitb, 24);
				bs_skipBytes(&s->bitb, w->meta.length - 18);

				if ((w->info.blocksize.min < 16)
				||  (w->info.samplerate < 1024)
				||  (w->info.channels > 6)
				||  (w->info.bitspersample < 4)
				||  (w->info.bitspersample > 24)
				||  (w->info.bitspersample & 3))
				{
					Log("Bad Flac Info min %d, channels %d, samplerate %d, bitspersample %d"
					   , w->info.blocksize.min, w->info.channels, w->info.samplerate, w->info.bitspersample);
					w->state = Decode_FatalError;
					e = &Err_FLAC_Corrupted;
					break;
				}
				else
					Log("Flac Info min %d, channels %d, samplerate %d, bitspersample %d"
					   , w->info.blocksize.min, w->info.channels, w->info.samplerate, w->info.bitspersample);

				if (w->meta.more)
					w->state = Decode_MetaHdr;
				else
					w->state = Decode_Sync;
			}
			break;
			case Decode_MetaHdr:
			{
				if (count < 32)
				{
					if (s->inb->finishflag)
					{
						w->state = Decode_FatalError;
						e = &Err_FLAC_Corrupted;
					}
					bMore = false;
					break;
				}

				w->meta.more = !bs_get1bit(&s->bitb);
				w->meta.type = bs_getbits(&s->bitb, 7);
				w->meta.length = bs_getbits(&s->bitb, 24);

				if (w->meta.type == 127)
				{
					Log("Bad Flac Meta %d [length %d]", w->meta.type, w->meta.length);
					w->state = Decode_FatalError;
					e = &Err_FLAC_Corrupted;
					break;
				}

				if (w->meta.type == 3)
				{
					// Seek table
					w->seek.size = 1 + (w->meta.length/18);
					e = IStream_Alloc(s, (void**) &w->seek.table, sizeof(seekpoint)*w->seek.size);
					memset(w->seek.table, 0, sizeof(seekpoint)*w->seek.size);
					w->seek.size--;
					if (e)
					{
						w->state = Decode_FatalError;
						break;
					}
					w->state = Decode_SeekTable;
				}
				else if (w->meta.type == 4)
					w->state = Decode_Comments;
				else
					w->state = Decode_MetaData;
			}
			break;
			case Decode_SeekTable:
			{
				while (w->meta.length >= 18)
				{
					if (bs_bitcount(&s->bitb) < 18*8)
						break;

					seekpoint* psp = w->seek.table + w->seek.count;
					psp->samplenr = bs_getbits(&s->bitb, 16);
					psp->samplenr <<= 48;
					psp->samplenr += bs_getbits(&s->bitb, 24);
					psp->samplenr <<= 24;
					psp->samplenr += bs_getbits(&s->bitb, 24);
					psp->offset = bs_getbits(&s->bitb, 16);
					psp->offset <<= 48;
					psp->offset += bs_getbits(&s->bitb, 24);
					psp->offset <<= 24;
					psp->offset += bs_getbits(&s->bitb, 24);
					bs_skipbits(&s->bitb, 16);

					w->meta.length -= 18;
					w->seek.count++;
				}

				if (w->meta.length < 18)
					w->meta.length = IStream_SkipBytes(s, w->meta.length);

				if (w->meta.length > 0)
				{
					if (s->inb->finishflag)
					{
						w->state = Decode_FatalError;
						e = &Err_FLAC_Corrupted;
					}

					bMore = false;
					break;
				}

				// Table completed, check it
				seekpoint* psp = w->seek.table;
				if ((psp[0].samplenr < 0)
				||  (psp[0].samplenr >= w->info.totalsamples)
				||  (psp[0].offset < 0))
					w->seek.size = 0;

				for (int i = 1; i < w->seek.size; i++)
				{
					if ((psp[1].samplenr <= psp[0].samplenr)
					||  (psp[1].samplenr >= w->info.totalsamples)
					||  (psp[1].offset <= psp[0].offset))
						w->seek.size = i - 1;

					psp++;
				}

				if (w->meta.more)
					w->state = Decode_MetaHdr;
				else
					w->state = Decode_Sync;
			}
			break;
			case Decode_Comments:
			{
				int val;
				char buffer[256];

				// Decode them in one go, so do it only if they fit entirely in buffer
				if (w->meta.length >= s->bitb.size)
				{
					w->state = Decode_MetaData;
					break;
				}

				if ((w->meta.length << 3) > count)
				{
					if (s->inb->finishflag)
					{
						w->state = Decode_FatalError;
						e = &Err_FLAC_Corrupted;
					}

					bMore = false;
					break;
				}

				// Warning Vorbis stuff, all size are in reverse byte order from the rest of FLAC length
				// Read vendor size
				w->meta.length -= 4;
				val = bs_getbits(&s->bitb, 8);
				val |= bs_getbits(&s->bitb, 8) << 8;
				val |= bs_getbits(&s->bitb, 8) << 16;
				val |= bs_getbits(&s->bitb, 8) << 24;
				if ((val < 0) || (val > w->meta.length))
				{
					// Corrupted, skip the rest
					Log("Vorbis Vendor Comment corrupted: %d bytes", val);
					w->state = Decode_MetaData;
					break;
				}
				// Skip vendor string and number of comments
				bs_skipBytes(&s->bitb, val + 4);
				w->meta.length -= val + 4;
				while (w->meta.length > 4)
				{
					// Read comment size
					w->meta.length -= 4;
					val = bs_getbits(&s->bitb, 8);
					val |= bs_getbits(&s->bitb, 8) << 8;
					val |= bs_getbits(&s->bitb, 8) << 16;
					val |= bs_getbits(&s->bitb, 8) << 24;
					if ((val < 0) || (val > w->meta.length))
					{
						// Skip the rest
						Log("Vorbis Comment corrupted: %d bytes", val);
						w->state = Decode_MetaData;
						break;
					}
					if (val >= sizeof(buffer))
					{
						Log("Vorbis Comment too long: %d bytes", val);
						bs_skipBytes(&s->bitb, val);
					}
					else
					{
						bs_getBytes(&s->bitb, (uint8_t*) buffer, val);
						buffer[val] = 0;
						w->meta.length -= val;

						e = Vorbis_Comment(s, buffer);
						if (e) break;
					}
				}

				// Skip the rest
				if (e)
					w->state = Decode_FatalError;
				else
					w->state = Decode_MetaData;
			}
			break;
			case Decode_MetaData:
			{
				w->meta.length = IStream_SkipBytes(s, w->meta.length);

				if (w->meta.length > 0)
				{
					if (s->inb->finishflag)
					{
						w->state = Decode_FatalError;
						e = &Err_FLAC_Corrupted;
					}

					bMore = false;
					break;
				}

				if (w->meta.type > 4)
					Log("Flac MetaHdr %d skipped",  w->meta.type);

				if (w->meta.more)
					w->state = Decode_MetaHdr;
				else
					w->state = Decode_Sync;
			}
			break;
			case Decode_Sync:
			{
				if (s->bitb.start > s->bitb.free)
				{
					while (s->bitb.start <= s->bitb.last)
					{
						if (*s->bitb.start == 0xff)
							break;
						s->bitb.start++;
						s->bitb.count -= 8;
					}
					if (s->bitb.start > s->bitb.last)
						s->bitb.start = s->bitb.data;
				}

				if (s->bitb.start < s->bitb.free)
				{
					while (s->bitb.start < s->bitb.free)
					{
						if (*s->bitb.start == 0xff)
							break;
						s->bitb.start++;
						s->bitb.count -= 8;
					}
				}

				if ((bs_bitcount(&s->bitb) >= 17*8) // No fixed header size, max value
				||  ((s->inb->finishflag) && (bs_bitcount(&s->bitb) >= 6*8)))
				{
					if (bs_peekbits(&s->bitb, 14) == 0x3ffe)
					{
						int samplerate, channels, bps;

						w->psyncstart = s->bitb.start;
						bs_skipbits(&s->bitb, 16);
						// Interpret them later
						w->newblocksize = bs_getbits(&s->bitb, 4);
						samplerate = bs_getbits(&s->bitb, 4);
						channels = bs_getbits(&s->bitb, 4);
						if (channels & 8)
						{
							if ((channels & 7) > FLAC_Coupling_Middle)
							{
								w->state = Decode_ClearSync;
								break;
							}
							channels = 2;
						}
						else
						{
							channels++;
						}

						bps = bs_getbits(&s->bitb, 3);
						if (bps == 0)
							bps = w->info.bitspersample;
						else
							bps = BitsPerSamples[bps];

						if (bs_get1bit(&s->bitb))
						{
							w->state = Decode_ClearSync;
							break;
						}

						if (get_utf8(&s->bitb) < 0)
						{
							w->state = Decode_ClearSync;
							break;
						}

						switch(w->newblocksize)
						{
							case 0: w->newblocksize = w->info.blocksize.min; break;
							case 6: w->newblocksize = 1 + bs_getbits(&s->bitb, 8); break;
							case 7: w->newblocksize = 1 + bs_getbits(&s->bitb, 16); break;
							default: w->newblocksize = Block_Sizes[w->newblocksize];
						}

						switch(samplerate)
						{
							case  0: samplerate = w->info.samplerate; break;
							case 12: samplerate = 1000*bs_getbits(&s->bitb, 8); break;
							case 13: samplerate = bs_getbits(&s->bitb, 16); break;
							case 14: samplerate = 10*bs_getbits(&s->bitb, 16); break;
							default: samplerate = Sample_Rates[samplerate];
						}

						int crc = 0, ncrc;
						uint8_t* p = w->psyncstart;

						if (p > s->bitb.start)
						{
							while (p <= s->bitb.last)
								crc = crc8[crc ^ *p++];
							p = s->bitb.data;
						}

						while (p < s->bitb.start)
							crc = crc8[crc ^ *p++];

						ncrc = bs_getbits(&s->bitb, 8);
						if ((ncrc != crc)
						||  (w->newblocksize <= 0)
						||  (w->newblocksize > w->info.blocksize.max)
						||  (w->info.samplerate != samplerate)
						||  (w->info.channels != channels)
						||  (w->info.bitspersample != bps))
						{
							w->state = Decode_ClearSync;
							break;
						}

						// New header found, decode previous frame
						w->state = Decode_Frame;
						break;
					}
					else
					{
						bs_skipbits(&s->bitb, 8);
						break;
					}
				}

				// No frame sync found in current buffer
				if (s->inb->finishflag)
				{
					// Decode a last frame?
					if (w->frame.bs.start)
					{
						w->state = Decode_Frame;
						bs_skipbits(&s->bitb, bs_bitcount(&s->bitb));
					}
					else
					{
						int64_t val;

						w->state = Decode_Finished;
						w->output.finishflag = 1;

						if (bs_bitcount(&s->bitb) > 0)
							Log("Skipping %d bytes", bs_bitcount(&s->bitb) >> 3);

						val = w->info.totalsamples - w->info.decodedsamples;
						if (val) Log("Unexpected total number of samples (diff is %lld)", val);
					}
				}
				else
				{
					// some insurance to avoid buffer saturation
					if (w->frame.bs.start)
					{
						int size = s->bitb.start - w->frame.bs.start;
						if (size < 0) size += s->bitb.size;

						if ((size > w->info.framesize.max)
						&&  (0 < w->info.framesize.max))
						{
							Log("Force decoding of frame while waiting for next sync (%d bytes)", size);
							w->state = Decode_Frame;
						}
					}
				}

				bMore = false;
			}
			break;
			case Decode_ClearSync:
			{
				// Rewind
				s->bitb.start = w->psyncstart + 1;
				s->bitb.count = s->bitb.free - s->bitb.start;
				if (s->bitb.count < 0) s->bitb.count += s->bitb.size;
				s->bitb.count <<= 3;
				s->bitb.bitindex = 0;

				w->state = Decode_Sync;
				w->psyncstart = NULL;
			}
			break;
			case Decode_Frame:
			{
				if (w->frame.bs.start)
				{
					// Check that enough output is available
					// Output cannot be entirely filled
					int max = w->output.start - w->output.free;
					if (max <= 0)
						max += w->output.size;
					max -= 1;
					if (max < w->frame.blocksize)
					{
						// Not free
						bMore = false;
						break;
					}

					// Decode frame
					w->frame.bs.free = w->psyncstart ? w->psyncstart : s->bitb.free;
					w->frame.bs.data = s->bitb.data;
					w->frame.bs.size = s->bitb.size;
					w->frame.bs.last = s->bitb.data + s->bitb.size - 1;
					FLAC_DecodeFrame(&w->frame, w);
				}
				else if ((w->seek.offset0 == 0) && w->psyncstart)
				{
					w->seek.offset0 = s->source.pos - s->source.start;
					if (s->bitb.free < w->psyncstart)
						w->seek.offset0 -= s->bitb.size;
					w->seek.offset0 -= (s->bitb.free - w->psyncstart);
				}

				// sync frame become next frame to decode
				w->frame.bs.start = w->psyncstart;
				w->frame.blocksize = w->newblocksize;
				w->psyncstart = NULL;
				w->state = Decode_Sync;
			}
			break;
			case Decode_Finished:
			case Decode_FatalError:
			{
				w->frame.bs.start = NULL;
				w->psyncstart = NULL;
				bMore = false;
			}
			break;
		}
	}

	// While some frame remains to be decoded, use internal pointer
	if (w->frame.bs.start || w->psyncstart)
	{
		w->psavestart = s->bitb.start;
		if (w->frame.bs.start)
			s->bitb.start = w->frame.bs.start;
		else
			s->bitb.start = w->psyncstart;
	}

	// Update buffers
	if (!ioff) _kernel_irqs_off();
	w->output.ofree = w->output.free - w->output.data;
	if (!ioff) _kernel_irqs_on();

	return e;
}

/*---------------
 * FLAC_Lister
 */

static int FLAC_Lister(IStream* s)
{
	work_t* w = s->codec.data;
	MixStream* stream = &s->ChannelsArray[0];
	int pos, remain, consumed;

	// 1. Update output buffer start from current mixer stream position
	pos = stream->pos;
	consumed = pos - w->output.ostart;
	if (consumed < 0) consumed += w->output.size;
	w->output.ostart = pos;
	w->playedsamples += consumed;

	// 2. Determine filled buffer size remaining in output buffer
	remain = w->output.ofree - w->output.ostart;
	if (remain < 0) remain += w->output.size;

	// 3. Fix stream duration
	if (w->initstate & codec_state_fixduration)
	{
		if (w->output.finishflag)
		{
			w->totalsamples = w->playedsamples + remain;

			if (w->totalsamples > 0)
			{
				int64_t t1, t2;

				// bit=frq*size*8/samples
				t1 = (int64_t) (uint32_t) (s->source.end - s->source.start);
				t1 *= (uint32_t) (w->info.samplerate*8);
				t2 = w->totalsamples;
				t1 += t2>>1; // for rounding
				w->bitrate = (int)(int64_t) (t1 / t2);
				s->info.MeanBitRate = (w->bitrate+500)/1000;
			}

			// Stop fixing song duration
			w->initstate &= ~codec_state_fixduration;
		}
		else
		{
			if (w->totalsamples < w->playedsamples + remain)
				w->totalsamples = w->playedsamples + remain;
		}
	}

	// 4. Treat completely decoded output case
	if (w->output.finishflag)
	{
		// Mark as decoded, but may have to delay
		if (!(s->status & swrk_status_decoded)
		&&  (w->playedsamples >= 0))
		{
			s->status |= swrk_status_decoded;
			w->playedsamples = (int64_t)(int) -remain;
		}
	}

	return remain;
}

ICodec FLAC_Codec =
{
	  FLAC_Type
	, FLAC_Accept
	, FLAC_Load
	, FLAC_Unload
	, FLAC_IsReady
	, FLAC_BufferSizes
	, FLAC_InError
	, FLAC_ReadPos
	, FLAC_ReadDuration
	, FLAC_SetPos
	, FLAC_ClearInput
	, FLAC_Process
	, NULL
	, FLAC_Lister
};
