Sigrid Solveig Haflínudóttirfirst69b97135 years agoHistory
938 lines · 17.6 KB · C
RawDownload
/*Copyright (C) 2014, Jeffrey Lim. All Rights Reserved. Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice,   this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice,   this list of conditions and the following disclaimer in the documentation   and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products   derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORBE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OFSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESSINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER INCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF SUCH DAMAGE.*/ #include "it_struc.h" const char *HeaderMsg = "File Header";const char *InstrumentMsg = "Instrument \375D";const char *SampleMsg = "Sample \375D";const char *SHLoadMsg = "Sample Header \375D";const char *PatternMsg = "Pattern \375D"; // TODO: prevent buffer read overruns// note, it's entirely likely that I'm slightly off for exceptional behaviour --GMvoid D_Decompress16BitData(uint16_t *dest, uint8_t *src, uint16_t len){	// Register usage:	// BX = LastValue	// CH = Bitdepth	// CL = 16-Bitdepth, 0 for Bitdepth > 16	// DL = Bitsread	// DH = scratch 	int counter = len>>1;	uint8_t bitdepth = 17;	uint8_t ibitdepth = 0;	uint16_t lastvalue = 0;	uint8_t bitsread = 0;	uint8_t scratch = 0;	USED(scratch); 	while(counter != 0)	{		// Push    CX 		uint32_t eax = *(uint32_t *)src;		eax >>= bitsread; 		bitsread += bitdepth;		src += bitsread>>3;		bitsread &= 7; 		// Pop     CX 		if(bitdepth <= 6)		{			eax <<= ibitdepth; 			if((eax & 0xFFFF) != 0x8000)			{				lastvalue += (uint16_t)(((int16_t)eax)>>ibitdepth);				*(dest++) = lastvalue;				counter--;				continue;			} 			uint8_t newbits = (eax>>16) & 15;			newbits++; 			// Advance bits			bitsread += 4;			if(newbits >= bitdepth)				newbits += 1; 			ibitdepth = 16;			bitdepth = newbits;			if(ibitdepth < bitdepth)			{				ibitdepth -= bitdepth;				ibitdepth++;			} else {				ibitdepth -= bitdepth;			} 		} else if(bitdepth <= 16) { 			// Push    DX 			uint16_t tmp_dx = 0xFFFF;			tmp_dx >>= ibitdepth;			uint16_t tmp_ax = (uint16_t)eax;			tmp_ax &= (uint16_t)tmp_dx;			tmp_dx >>= 1;			tmp_dx += 8;			tmp_dx -= 16;			if(tmp_ax > (uint16_t)(tmp_dx+16) || tmp_ax <= (uint16_t)tmp_dx)			{				eax <<= ibitdepth;				lastvalue += (uint16_t)(((int16_t)eax)>>ibitdepth);				*(dest++) = lastvalue;				counter--;				continue;			} 			// Pop     DX 			uint8_t newbits = (uint8_t)(tmp_ax - tmp_dx);			if(newbits >= bitdepth)				newbits += 1; 			ibitdepth = 16;			bitdepth = newbits;			if(ibitdepth < bitdepth)			{				ibitdepth -= bitdepth;				ibitdepth++;			} else {				ibitdepth -= bitdepth;			}		} else { 			if((eax & 0x10000) == 0)			{				lastvalue += (uint16_t)eax;				*(dest++) = lastvalue;				counter--;				continue;			} 			eax++; // Inc AX actually			bitdepth = (uint8_t)(eax&0xFF);			ibitdepth = 16 - bitdepth;		}	} } void D_Decompress8BitData(uint8_t *dest, uint8_t *src, uint16_t len){	// DS:SI = source	// ES:DI = destination	// CX = count. 	// Register usage:	// BH = Bitdepth	// BL = lastvalue	// CL = 8-bitdepth, undefined for bitdepth > 8	// CH = Bitsread 	// DX = scratch 	int counter = len; // BP = counter;	uint8_t bitdepth = 9;	uint8_t ibitdepth = 0;	uint8_t lastvalue = 0;	uint8_t bitsread = 0;	uint16_t scratch = 0; 	USED(scratch); 	while(counter != 0)	{		// Get bits loaded into AX properly.		uint16_t ax = *(uint16_t *)src;		ax >>= bitsread; 		// Advance SI as necessary.		bitsread += bitdepth;		src += bitsread>>3;		bitsread &= 7; 		uint8_t tmp_al;		if(bitdepth <= 6)		{			ax <<= ibitdepth; 			if((ax & 0xFF) != 0x80)			{				lastvalue += (uint8_t)(((int8_t)ax)>>ibitdepth);				*(dest++) = lastvalue;				counter--;				continue;			} 			tmp_al = (ax>>8);			bitsread += 3; 			tmp_al &= 7;			scratch = bitsread;			bitsread &= 7;			scratch >>= 3; 			src += scratch; 		} else {			tmp_al = (ax & 0xFF); 			if(bitdepth > 8)			{				// 9 bit representation				ax &= 0x1FF; 				if((ax & 0x100) == 0)				{					lastvalue += (uint8_t)tmp_al;					*(dest++) = lastvalue;					counter--;					continue;				} 			} else if(bitdepth == 8) {				if(tmp_al < 0x7C || tmp_al > 0x83)				{					lastvalue += (uint8_t)tmp_al;					*(dest++) = lastvalue;					counter--;					continue;				} 				tmp_al -= 0x7C; 			} else { 				tmp_al <<= 1;				if(tmp_al < 0x78 || tmp_al > 0x86)				{					lastvalue += (uint8_t)(((int8_t)tmp_al)>>1);					*(dest++) = lastvalue;					counter--;					continue;				} 				tmp_al >>= 1;				tmp_al -= 0x3C; 			}		} 		ibitdepth = 8;		tmp_al++; 		//Cmp     AL, BH		//SBB     AL, 0FFh		//Mov     BH, AL		//Sub     CL, AL		//AdC     CL, 0		if(tmp_al >= bitdepth)			tmp_al++;		bitdepth = tmp_al; 		if(ibitdepth < tmp_al)		{			ibitdepth -= tmp_al;			ibitdepth++;		} else {			ibitdepth -= tmp_al;		} 		continue; 	}} int D_LoadSampleData(it_engine *ite, int fp, uint16_t ax){	// DS:SI points to sample header	// AX = sample number (0 based) 	it_sample *smp = &ite->smp[ax]; 	uint32_t edx = smp->Length;	if(edx == 0) return 0; 	uint8_t cl = smp->Flg;	uint16_t bp = ((uint16_t)smp->Cvt) | (((uint16_t)smp->DfP)<<8); 	smp->Flg &= ~0x0C;	smp->Cvt = 1;	bp &= 0xFF; 	bp <<= 1;	uint8_t ch = cl;	ch &= 0x8; cl &= 0x02;	ch <<= 4;	cl >>= 1;	bp |= ((uint16_t)ch)<<8;	bp |= ((uint16_t)cl); 	// BP flags:	// 1: 16 bit	// 2: Convert unsigned->signed	// 4: Swap bytes	// 8: Delta values.	// 16: Byte delta values	// 32: 12-bit sample.	// 64: Stereo prompt.	// 8000h: Compressed.  	if((bp & 1) != 0)		edx *= 2; 	if((bp & 64) != 0)	{		/*		Cmp     CS:DisableStereoMenu, 0		JNE     D_NoStereoMenu		{ 			PushAD			Push    DS ES 			Mov     DI, Offset O1_StereoSampleList			Mov     CX, 0FFFFh			Call    M_Object1List 			Mov     CS:TempData, DX 			Pop     ES DS			PopAD 			Or      BP, CS:TempData                 // 64 = left								// 64+128 = right		}		D_NoStereoMenu:		*/		edx *= 2;	} 	uint8_t *data = Music_AllocateSample(ite, ax, edx);	ax++;	USED(ax); 	int32_t edi = edx; 	if(data == NULL)	{		seek(fp, smp->Length, 1); // Advance file ptr.		// (pretty sure there's a bug somewhere --GM) 		//PEFunction_OutOfMemoryMessage(ite); 		return 1;	} 	// AX = sample no.	// CH = page no.	// EDI = bytes remaining	// SI = delta accumulator	// BX = file pointer // using variable "fp" instead --GM 	ch = 0; // Start with page 0	uint16_t si = 0; 	uint8_t *srcdata = data;	for(; edi > 0; edi -= 32768)	{		uint32_t ecx = 0;		int is8bit = 0;		USED(ecx, is8bit);		//uint8_t *srcdata = Music_GetSampleLocation(ite, ax, &ecx, &is8bit); 		uint32_t buflen = 32768; 		if(edi < buflen)			buflen = edi; 		if((bp & 32) != 0) // 12 bit format?		{			buflen &= 0xFFFF;			buflen *= 3;			buflen += 3;			buflen >>= 2; // = bytes to read.		} 		uint8_t *newsrcdata = srcdata + buflen;		uint16_t packed_len;		uint8_t packed_data[0x10000];		size_t packed_len_long; 		// DS:DX point to buffer. For compressed samples, use patterndata area.		if((bp & 0x8000) != 0)		{			/*			Push    DS			Push    CX			Push    DX 			Mov     DX, Pattern			Mov     DS, DX				Assume DS:Pattern 			Mov     DS, Word Ptr [PatternDataArea]				Assume DS:Nothing 			Xor     DX, DX			Mov     CX, 2			Mov     AH, 3Fh			Int     21h			Mov     CX, [DS:0]              // Bytes to read.			Xor     DX, DX                  // Compressed chunk.			*/			readn(fp, &packed_len, 2);			packed_len_long = packed_len;			packed_len_long = read(fp, packed_data, packed_len_long);			USED(packed_len_long); 		} else {			buflen = read(fp, srcdata, buflen);		} 		// Now to decompress samples, if required.		if((bp & 0x8000) != 0)		{			// TODO			if((bp & 1) == 0)				D_Decompress8BitData(srcdata, packed_data, buflen); // 8 bit decode.			else				D_Decompress16BitData((uint16_t *)srcdata, packed_data, buflen); // 16 bit decode 			si = 0;		} 		// flag skipped if sample compressed		if((bp & 0x8000) == 0 && (bp & 32) != 0) // 12-bit sample?		{			// TODO!			abort();			/*			// CX = number of bytes read.			//    = 3*2 number of sample read			buflen = (buflen + 2) / 3; 			// SI = AX * 3			LEA     SI, [EAX*2+EAX]         // SI = AX * 3			LEA     DI, [EAX*4+EDX]			Add     SI, DX 			Test    CX, CX			JZ      ConvertTXSample2 			Push    CX 		ConvertTXSample1:			Sub     SI, 3			Sub     DI, 4 			Mov     AX, [SI+1]			ShL     AL, 4			ShL     EAX, 16			Mov     AH, [SI]			Mov     AL, [SI+1]			And     AL, 0F0h			Mov     [DI], EAX 			Loop    ConvertTXSample1 			Pop     CX			Pop     SI			ShL     CX, 1			Jmp     D_LoadSampleData10 		ConvertTXSample2:			Pop     SI			*/		} else {			// CX = number of bytes read 		//SecondDelta:			if((bp & 1) != 0) // 16 bit?				buflen >>= 1; 		//D_LoadSampleData10:			if((bp & 5) == 5) // 16 bit and BSwap?			{				uint16_t ctr = buflen;				uint8_t *dfol = srcdata; 				while(ctr-- != 0)				{					uint8_t datl = dfol[0];					uint8_t dath = dfol[1];					dfol[0] = dath;					dfol[1] = datl; 					dfol += 2;

Lines 1–500 of 938.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t