mirror of
https://github.com/crskycode/GARbro.git
synced 2024-11-27 15:44:00 +08:00
(HcaReader): moved around initialization stuff.
aimed at implementing async decoder.
This commit is contained in:
parent
ed365530a6
commit
dbb3384e02
@ -67,18 +67,19 @@ namespace GameRes.Formats.Cri
|
|||||||
|
|
||||||
internal sealed class HcaReader : IDisposable
|
internal sealed class HcaReader : IDisposable
|
||||||
{
|
{
|
||||||
BigEndianReader m_input;
|
|
||||||
WaveFormat m_format;
|
WaveFormat m_format;
|
||||||
uint m_key1;
|
byte[] m_input;
|
||||||
uint m_key2;
|
|
||||||
|
|
||||||
public WaveFormat Format { get { return m_format; } }
|
public WaveFormat Format { get { return m_format; } }
|
||||||
|
|
||||||
public HcaReader (Stream input, uint key1, uint key2)
|
public HcaReader (Stream input, uint key1, uint key2)
|
||||||
{
|
{
|
||||||
m_input = new BigEndianReader (input, Encoding.UTF8, true);
|
using (var file = new BigEndianReader (input, Encoding.UTF8, true))
|
||||||
m_key1 = key1;
|
ParseHeader (file);
|
||||||
m_key2 = key2;
|
m_ath = new AthTable (m_ath_type.Value, m_format.SamplesPerSecond);
|
||||||
|
m_cipher = new Cipher (m_ciph_type.Value, key1, key2);
|
||||||
|
|
||||||
|
InitBuffer (input);
|
||||||
}
|
}
|
||||||
|
|
||||||
delegate void SampleConverter (float f, BinaryWriter output);
|
delegate void SampleConverter (float f, BinaryWriter output);
|
||||||
@ -93,7 +94,8 @@ namespace GameRes.Formats.Cri
|
|||||||
};
|
};
|
||||||
|
|
||||||
int m_version;
|
int m_version;
|
||||||
uint m_fmt_block_count;
|
int m_data_offset;
|
||||||
|
uint? m_fmt_block_count;
|
||||||
int? m_ciph_type;
|
int? m_ciph_type;
|
||||||
int? m_ath_type;
|
int? m_ath_type;
|
||||||
CompParams m_comp;
|
CompParams m_comp;
|
||||||
@ -105,67 +107,90 @@ namespace GameRes.Formats.Cri
|
|||||||
|
|
||||||
public byte[] Unpack (ConversionFormat target)
|
public byte[] Unpack (ConversionFormat target)
|
||||||
{
|
{
|
||||||
ParseHeader (target);
|
InitWaveFormat (target);
|
||||||
m_ath = new AthTable (m_ath_type.Value, m_format.SamplesPerSecond);
|
|
||||||
m_cipher = new Cipher (m_ciph_type.Value, m_key1, m_key2);
|
|
||||||
|
|
||||||
m_block = new byte[m_comp.BlockSize];
|
m_block = new byte[m_comp.BlockSize];
|
||||||
var output = new byte[m_fmt_block_count * 0x400 * m_format.BlockAlign];
|
var output = new byte[m_fmt_block_count.Value * 0x400 * m_format.BlockAlign];
|
||||||
using (var mem = new MemoryStream (output))
|
using (var mem = new MemoryStream (output))
|
||||||
using (var writer = new BinaryWriter (mem))
|
using (var writer = new BinaryWriter (mem))
|
||||||
Decode (writer, ConversionMap[target]);
|
Decode (writer, ConversionMap[target]);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParseHeader (ConversionFormat target)
|
void InitWaveFormat (ConversionFormat target)
|
||||||
{
|
{
|
||||||
uint signature = ReadSignature();
|
m_format.FormatTag = (ushort)target;
|
||||||
|
m_format.BitsPerSample = FormatBpsMap[target];
|
||||||
|
m_format.BlockAlign = (ushort)(m_format.Channels * m_format.BitsPerSample / 8);
|
||||||
|
m_format.AverageBytesPerSecond = m_format.SamplesPerSecond * m_format.BlockAlign;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitBuffer (Stream input)
|
||||||
|
{
|
||||||
|
var mem = input as MemoryStream;
|
||||||
|
if (null == mem)
|
||||||
|
{
|
||||||
|
m_input = new byte[input.Length];
|
||||||
|
input.Position = m_data_offset;
|
||||||
|
input.Read (m_input, m_data_offset, m_input.Length-m_data_offset);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m_input = mem.GetBuffer();
|
||||||
|
}
|
||||||
|
catch (UnauthorizedAccessException)
|
||||||
|
{
|
||||||
|
m_input = mem.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParseHeader (BigEndianReader input)
|
||||||
|
{
|
||||||
|
uint signature = ReadSignature (input);
|
||||||
if (signature != 0x48434100) // 'HCA'
|
if (signature != 0x48434100) // 'HCA'
|
||||||
throw new InvalidFormatException();
|
throw new InvalidFormatException();
|
||||||
m_version = m_input.ReadUInt16();
|
m_version = input.ReadUInt16();
|
||||||
int header_size = m_input.ReadUInt16();
|
m_data_offset = input.ReadUInt16();
|
||||||
while (m_input.Position < header_size)
|
while (input.Position < m_data_offset)
|
||||||
{
|
{
|
||||||
signature = ReadSignature();
|
signature = ReadSignature (input);
|
||||||
switch (signature)
|
switch (signature)
|
||||||
{
|
{
|
||||||
case 0x666D7400: // 'fmt'
|
case 0x666D7400: // 'fmt'
|
||||||
uint format = m_input.ReadUInt32();
|
uint format = input.ReadUInt32();
|
||||||
m_format.FormatTag = (ushort)target;
|
|
||||||
m_format.Channels = (byte)(format >> 24);
|
m_format.Channels = (byte)(format >> 24);
|
||||||
m_format.SamplesPerSecond = format & 0xFFFFFF;
|
m_format.SamplesPerSecond = format & 0xFFFFFF;
|
||||||
m_format.BitsPerSample = FormatBpsMap[target];
|
m_fmt_block_count = input.ReadUInt32();
|
||||||
m_format.BlockAlign = (ushort)(m_format.Channels * m_format.BitsPerSample / 8);
|
input.Skip (4);
|
||||||
m_format.AverageBytesPerSecond = m_format.SamplesPerSecond * m_format.BlockAlign;
|
|
||||||
m_fmt_block_count = m_input.ReadUInt32();
|
|
||||||
m_input.Skip (4);
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case 0x636F6D70: // 'comp'
|
case 0x636F6D70: // 'comp'
|
||||||
m_comp = new CompParams { BlockSize = m_input.ReadUInt16() };
|
m_comp = new CompParams { BlockSize = input.ReadUInt16() };
|
||||||
m_input.Read (m_comp.R, 0, 8);
|
input.Read (m_comp.R, 0, 8);
|
||||||
m_input.Skip (2);
|
input.Skip (2);
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case 0x6C6F6F70: // 'loop'
|
case 0x6C6F6F70: // 'loop'
|
||||||
m_input.Skip (12);
|
input.Skip (12);
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case 0x63697068: // 'ciph'
|
case 0x63697068: // 'ciph'
|
||||||
m_ciph_type = m_input.ReadUInt16();
|
m_ciph_type = input.ReadUInt16();
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case 0x72766100: // 'rva'
|
case 0x72766100: // 'rva'
|
||||||
m_rva_volume = m_input.ReadSingle();
|
m_rva_volume = input.ReadSingle();
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case 0x61746800: // 'ath'
|
case 0x61746800: // 'ath'
|
||||||
m_ath_type = m_input.ReadUInt16();
|
m_ath_type = input.ReadUInt16();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break; // unknown section encountered
|
break; // unknown section encountered
|
||||||
}
|
}
|
||||||
if (0 == m_format.FormatTag || null == m_comp)
|
if (null == m_fmt_block_count || null == m_comp)
|
||||||
throw new NotSupportedException ("Not supported HCA format");
|
throw new NotSupportedException ("Not supported HCA format");
|
||||||
if (m_comp.BlockSize < 8)
|
if (m_comp.BlockSize < 8)
|
||||||
throw new InvalidFormatException ("Invalid HCA block size");
|
throw new InvalidFormatException ("Invalid HCA block size");
|
||||||
@ -190,7 +215,6 @@ namespace GameRes.Formats.Cri
|
|||||||
if (0 == m_comp.R[2])
|
if (0 == m_comp.R[2])
|
||||||
m_comp.R[2] = 1;
|
m_comp.R[2] = 1;
|
||||||
InitChannels();
|
InitChannels();
|
||||||
m_input.Position = header_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitChannels ()
|
void InitChannels ()
|
||||||
@ -244,10 +268,13 @@ namespace GameRes.Formats.Cri
|
|||||||
|
|
||||||
void Decode (BinaryWriter output, SampleConverter pack_sample)
|
void Decode (BinaryWriter output, SampleConverter pack_sample)
|
||||||
{
|
{
|
||||||
int bytes_per_sample = m_format.BitsPerSample / 8;
|
int block_offset = m_data_offset;
|
||||||
for (uint i = 0; i < m_fmt_block_count; ++i)
|
for (uint i = 0; i < m_fmt_block_count; ++i)
|
||||||
{
|
{
|
||||||
m_input.Read (m_block, 0, m_block.Length);
|
if (block_offset + m_comp.BlockSize > m_input.Length)
|
||||||
|
throw new EndOfStreamException();
|
||||||
|
Buffer.BlockCopy (m_input, block_offset, m_block, 0, m_comp.BlockSize);
|
||||||
|
block_offset += m_comp.BlockSize;
|
||||||
DecodeBlock();
|
DecodeBlock();
|
||||||
for (int j = 0; j < 8; ++j)
|
for (int j = 0; j < 8; ++j)
|
||||||
for (int k = 0; k < 0x80; ++k)
|
for (int k = 0; k < 0x80; ++k)
|
||||||
@ -308,12 +335,12 @@ namespace GameRes.Formats.Cri
|
|||||||
output.Write ((short)v);
|
output.Write ((short)v);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint ReadSignature ()
|
static uint ReadSignature (BigEndianReader input)
|
||||||
{
|
{
|
||||||
return m_input.ReadUInt32() & 0x7F7F7F7F;
|
return input.ReadUInt32() & 0x7F7F7F7F;
|
||||||
}
|
}
|
||||||
|
|
||||||
ushort CheckSum (byte[] data, ushort sum = 0)
|
static ushort CheckSum (byte[] data, ushort sum = 0)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < data.Length; ++i)
|
for (int i = 0; i < data.Length; ++i)
|
||||||
sum = (ushort)((sum << 8) ^ CheckSumTable[(sum >> 8) ^ data[i]]);
|
sum = (ushort)((sum << 8) ^ CheckSumTable[(sum >> 8) ^ data[i]]);
|
||||||
@ -955,7 +982,6 @@ namespace GameRes.Formats.Cri
|
|||||||
{
|
{
|
||||||
if (!_disposed)
|
if (!_disposed)
|
||||||
{
|
{
|
||||||
m_input.Dispose();
|
|
||||||
_disposed = true;
|
_disposed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user