#include "tn_ra.h"

int32 TNPlug_DecodeFramePlayer (struct TNPlugIFace *Self, struct TuneNet *inTuneNet,
	int16 *outbuffer)
{
	struct TNDecHandle *handle = inTuneNet->handle;
	uint32 s, size, x, bufsiz, ab;
	uint8 *in = handle->in;
	uint8 *deint = handle->deint;
	uint32 blockSize = handle->blockSize;
	void *Real = handle->Real;
	int16 *pcm = inTuneNet->pcm[0];
	//unsigned int freq = handle->freq;

	if ((s = size = ReadPackets(handle->inHandle, handle->ReadFunc, in, handle->chunkSize,
		&handle->packets, &handle->streamid, &handle->rmf)) > 0)
	{
		bufsiz = 0;
		if (!(size /= blockSize)) return 0;

		switch (handle->codec) {
			case 1:
				for (x = 0; x < size; x++) {
					decode_144(Real, in, pcm);
					in += blockSize;
					bufsiz += AUDIOBLOCK;
					pcm += AUDIOBLOCK;
				}
				break;
			case 2:
				deinterleave(in,deint,s);
				for (x = 0; x < size; x++) {
					decode_288(Real, deint, pcm);
					deint += blockSize;
					bufsiz += AUDIOBLOCK;
					pcm += AUDIOBLOCK;
				}
				break;
			case 3:
				swapbytes(in,deint,s);
				for (x = 0; x < size; x++) {
					ab = decode_dnet(Real, deint, pcm, &handle->freq, handle->chans);
					deint += blockSize;
					bufsiz += ab;
					pcm += ab;
				}
				break;
		}

		inTuneNet->dec_frequency = handle->freq;
		#if 0
		if (handle->freq != freq) {
			dbug(("freq change: %ld -> %ld\n", handle->freq, freq));
			handle->freq = freq;
		}
		#endif
		return bufsiz / handle->chans;
	}

	return 0;
}

int32 TNPlug_SeekPlayer (struct TNPlugIFace *Self, struct TuneNet *inTuneNet, uint32 seconds) {
	return 0;
}
