2009-08-17 03:39:53 +00:00
|
|
|
using System;
|
|
|
|
|
|
2009-08-30 21:58:54 +00:00
|
|
|
namespace CUETools.Codecs
|
2009-08-17 03:39:53 +00:00
|
|
|
{
|
2011-10-24 00:13:35 +00:00
|
|
|
public class BitWriter
|
|
|
|
|
{
|
2013-03-26 19:50:44 -04:00
|
|
|
private ushort crc16_m;
|
2013-03-25 22:33:21 -04:00
|
|
|
private ulong bit_buf_m;
|
|
|
|
|
private int bit_left_m;
|
2011-10-24 00:13:35 +00:00
|
|
|
private byte[] buffer;
|
2013-03-25 22:33:21 -04:00
|
|
|
private int buf_start, buf_ptr_m, buf_end;
|
2011-10-24 00:13:35 +00:00
|
|
|
private bool eof;
|
|
|
|
|
|
|
|
|
|
public byte[] Buffer
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Length
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
return buf_ptr_m - buf_start;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
flush();
|
2013-03-25 22:33:21 -04:00
|
|
|
buf_ptr_m = buf_start + value;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int BitLength
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
return buf_ptr_m * 8 + 64 - bit_left_m;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 19:50:44 -04:00
|
|
|
public ushort get_crc16()
|
|
|
|
|
{
|
|
|
|
|
return crc16_m;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-24 00:13:35 +00:00
|
|
|
public BitWriter(byte[] buf, int pos, int len)
|
|
|
|
|
{
|
|
|
|
|
buffer = buf;
|
|
|
|
|
buf_start = pos;
|
2013-03-25 22:33:21 -04:00
|
|
|
buf_ptr_m = pos;
|
2011-10-24 00:13:35 +00:00
|
|
|
buf_end = pos + len;
|
2013-03-25 22:33:21 -04:00
|
|
|
bit_left_m = 64;
|
|
|
|
|
bit_buf_m = 0;
|
2013-03-26 19:50:44 -04:00
|
|
|
crc16_m = 0;
|
2011-10-24 00:13:35 +00:00
|
|
|
eof = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
buf_ptr_m = buf_start;
|
|
|
|
|
bit_left_m = 64;
|
|
|
|
|
bit_buf_m = 0;
|
2013-03-26 19:50:44 -04:00
|
|
|
crc16_m = 0;
|
2011-10-24 00:13:35 +00:00
|
|
|
eof = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void writebytes(int bytes, byte c)
|
|
|
|
|
{
|
|
|
|
|
for (; bytes > 0; bytes--)
|
|
|
|
|
{
|
|
|
|
|
writebits(8, c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unsafe void writeints(int len, int pos, byte* buf)
|
|
|
|
|
{
|
|
|
|
|
int old_pos = BitLength;
|
|
|
|
|
int start = old_pos / 8;
|
|
|
|
|
int start1 = pos / 8;
|
|
|
|
|
int end = (old_pos + len) / 8;
|
|
|
|
|
int end1 = (pos + len) / 8;
|
|
|
|
|
flush();
|
|
|
|
|
byte start_val = old_pos % 8 != 0 ? buffer[start] : (byte)0;
|
|
|
|
|
fixed (byte* buf1 = &buffer[0])
|
2013-03-26 19:50:44 -04:00
|
|
|
{
|
|
|
|
|
if (old_pos % 8 != 0)
|
Fix typos found by codespell
- Typos were found by codespell v1.17.0.dev0 (commit 44fea6d)
- Command used:
codespell -q 2 \
-L ba,bloc,blocs,doubleclick,dur,fille,frmat,numer,optin,passtime \
-L pres,strack,te,tim,tre,uint,whn \
--skip="*.de-DE.resx,./Bwg*,./Freedb,./MusicBrainz,./ProgressODoom" \
--skip="./ThirdParty"
2020-02-13 21:42:55 +01:00
|
|
|
crc16_m = Crc16.Subtract(crc16_m, 0, 1);
|
2013-03-26 19:50:44 -04:00
|
|
|
crc16_m = Crc16.ComputeChecksum(crc16_m, buf + start1, end - start);
|
2011-10-24 00:13:35 +00:00
|
|
|
AudioSamples.MemCpy(buf1 + start, buf + start1, end - start);
|
2013-03-26 19:50:44 -04:00
|
|
|
buf1[start] |= start_val;
|
|
|
|
|
}
|
2013-03-25 22:33:21 -04:00
|
|
|
buf_ptr_m = end;
|
2011-10-24 00:13:35 +00:00
|
|
|
if ((old_pos + len) % 8 != 0)
|
|
|
|
|
writebits((old_pos + len) % 8, buf[end1] >> (8 - ((old_pos + len) % 8)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void write(params char[] chars)
|
|
|
|
|
{
|
|
|
|
|
foreach (char c in chars)
|
|
|
|
|
writebits(8, (byte)c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void write(string s)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < s.Length; i++)
|
|
|
|
|
writebits(8, (byte)s[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-08 22:18:34 -05:00
|
|
|
public void write(byte[] s)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < s.Length; i++)
|
|
|
|
|
writebits(8, s[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-24 00:13:35 +00:00
|
|
|
public void writebits_signed(int bits, int val)
|
|
|
|
|
{
|
|
|
|
|
writebits(bits, val & ((1 << bits) - 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void writebits_signed(uint bits, int val)
|
|
|
|
|
{
|
|
|
|
|
writebits((int)bits, val & ((1 << (int)bits) - 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void writebits(int bits, int val)
|
|
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
writebits(bits, (ulong)val);
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void writebits(DateTime val)
|
|
|
|
|
{
|
|
|
|
|
TimeSpan span = val.ToUniversalTime() - new DateTime(1904, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
2013-03-25 22:33:21 -04:00
|
|
|
writebits(32, (ulong)span.TotalSeconds);
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-25 22:33:21 -04:00
|
|
|
public void writebits(int bits, uint val)
|
2011-10-24 00:13:35 +00:00
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
writebits(bits, (ulong)val);
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-25 22:33:21 -04:00
|
|
|
public void writebits(int bits, ulong val)
|
2011-10-24 00:13:35 +00:00
|
|
|
{
|
|
|
|
|
//assert(bits == 32 || val < (1U << bits));
|
|
|
|
|
|
|
|
|
|
if (bits == 0 || eof) return;
|
2013-05-08 21:31:03 -04:00
|
|
|
if (bits < bit_left_m)
|
2011-10-24 00:13:35 +00:00
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
bit_left_m -= bits;
|
|
|
|
|
bit_buf_m |= val << bit_left_m;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
ulong bb = bit_buf_m | (val >> (bits - bit_left_m));
|
2011-10-24 00:13:35 +00:00
|
|
|
if (buffer != null)
|
|
|
|
|
{
|
2013-03-26 19:50:44 -04:00
|
|
|
if (buf_ptr_m + 8 > buf_end)
|
|
|
|
|
{
|
|
|
|
|
eof = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
crc16_m = (ushort)((crc16_m << 8) ^ Crc16.table[(crc16_m >> 8) ^ (byte)(bb >> 56)]);
|
|
|
|
|
crc16_m = (ushort)((crc16_m << 8) ^ Crc16.table[(crc16_m >> 8) ^ (byte)(bb >> 48)]);
|
|
|
|
|
crc16_m = (ushort)((crc16_m << 8) ^ Crc16.table[(crc16_m >> 8) ^ (byte)(bb >> 40)]);
|
|
|
|
|
crc16_m = (ushort)((crc16_m << 8) ^ Crc16.table[(crc16_m >> 8) ^ (byte)(bb >> 32)]);
|
|
|
|
|
crc16_m = (ushort)((crc16_m << 8) ^ Crc16.table[(crc16_m >> 8) ^ (byte)(bb >> 24)]);
|
|
|
|
|
crc16_m = (ushort)((crc16_m << 8) ^ Crc16.table[(crc16_m >> 8) ^ (byte)(bb >> 16)]);
|
|
|
|
|
crc16_m = (ushort)((crc16_m << 8) ^ Crc16.table[(crc16_m >> 8) ^ (byte)(bb >> 8)]);
|
|
|
|
|
crc16_m = (ushort)((crc16_m << 8) ^ Crc16.table[(crc16_m >> 8) ^ (byte)(bb )]);
|
|
|
|
|
|
2013-03-25 22:33:21 -04:00
|
|
|
buffer[buf_ptr_m + 7] = (byte)(bb & 0xFF); bb >>= 8;
|
|
|
|
|
buffer[buf_ptr_m + 6] = (byte)(bb & 0xFF); bb >>= 8;
|
|
|
|
|
buffer[buf_ptr_m + 5] = (byte)(bb & 0xFF); bb >>= 8;
|
|
|
|
|
buffer[buf_ptr_m + 4] = (byte)(bb & 0xFF); bb >>= 8;
|
|
|
|
|
buffer[buf_ptr_m + 3] = (byte)(bb & 0xFF); bb >>= 8;
|
|
|
|
|
buffer[buf_ptr_m + 2] = (byte)(bb & 0xFF); bb >>= 8;
|
|
|
|
|
buffer[buf_ptr_m + 1] = (byte)(bb & 0xFF); bb >>= 8;
|
|
|
|
|
buffer[buf_ptr_m + 0] = (byte)(bb & 0xFF);
|
|
|
|
|
buf_ptr_m += 8;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
2013-05-08 21:31:03 -04:00
|
|
|
// cannot do this in one shift, because bit_left_m can be 64,
|
|
|
|
|
//
|
|
|
|
|
bit_left_m += 64 - bits;
|
|
|
|
|
bit_buf_m = bit_left_m == 64 ? 0 : val << bit_left_m;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Assumes there's enough space, buffer != null and bits is in range 1..31
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bits"></param>
|
|
|
|
|
/// <param name="val"></param>
|
|
|
|
|
// unsafe void writebits_fast(int bits, uint val, ref byte* buf)
|
|
|
|
|
// {
|
|
|
|
|
//#if DEBUG
|
|
|
|
|
// if ((buf_ptr + 3) >= buf_end)
|
|
|
|
|
// {
|
|
|
|
|
// eof = true;
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
//#endif
|
|
|
|
|
// if (bits < bit_left)
|
|
|
|
|
// {
|
|
|
|
|
// bit_buf = (bit_buf << bits) | val;
|
|
|
|
|
// bit_left -= bits;
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// uint bb = (bit_buf << bit_left) | (val >> (bits - bit_left));
|
|
|
|
|
// bit_left += (32 - bits);
|
|
|
|
|
|
|
|
|
|
// *(buf++) = (byte)(bb >> 24);
|
|
|
|
|
// *(buf++) = (byte)(bb >> 16);
|
|
|
|
|
// *(buf++) = (byte)(bb >> 8);
|
|
|
|
|
// *(buf++) = (byte)(bb);
|
|
|
|
|
|
|
|
|
|
// bit_buf = val;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public void write_utf8(int val)
|
|
|
|
|
{
|
|
|
|
|
write_utf8((uint)val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void write_utf8(uint val)
|
|
|
|
|
{
|
|
|
|
|
if (val < 0x80)
|
|
|
|
|
{
|
|
|
|
|
writebits(8, val);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int bytes = (BitReader.log2i(val) + 4) / 5;
|
|
|
|
|
int shift = (bytes - 1) * 6;
|
|
|
|
|
writebits(8, (256U - (256U >> bytes)) | (val >> shift));
|
|
|
|
|
while (shift >= 6)
|
|
|
|
|
{
|
|
|
|
|
shift -= 6;
|
|
|
|
|
writebits(8, 0x80 | ((val >> shift) & 0x3F));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void write_unary_signed(int val)
|
|
|
|
|
{
|
|
|
|
|
// convert signed to unsigned
|
|
|
|
|
int v = -2 * val - 1;
|
|
|
|
|
v ^= (v >> 31);
|
|
|
|
|
|
|
|
|
|
// write quotient in unary
|
|
|
|
|
int q = v + 1;
|
|
|
|
|
while (q > 31)
|
|
|
|
|
{
|
|
|
|
|
writebits(31, 0);
|
|
|
|
|
q -= 31;
|
|
|
|
|
}
|
|
|
|
|
writebits(q, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void write_rice_signed(int k, int val)
|
|
|
|
|
{
|
|
|
|
|
// convert signed to unsigned
|
|
|
|
|
int v = -2 * val - 1;
|
|
|
|
|
v ^= (v >> 31);
|
|
|
|
|
|
|
|
|
|
// write quotient in unary
|
|
|
|
|
int q = (v >> k) + 1;
|
|
|
|
|
while (q + k > 31)
|
|
|
|
|
{
|
|
|
|
|
int b = Math.Min(q + k - 31, 31);
|
|
|
|
|
writebits(b, 0);
|
|
|
|
|
q -= b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// write remainder in binary using 'k' bits
|
|
|
|
|
writebits(k + q, (v & ((1 << k) - 1)) | (1 << k));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unsafe void write_rice_block_signed(byte* fixedbuf, int k, int* residual, int count)
|
|
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
byte* buf = &fixedbuf[buf_ptr_m];
|
|
|
|
|
ulong bit_buf = bit_buf_m;
|
|
|
|
|
int bit_left = bit_left_m;
|
2013-03-26 19:50:44 -04:00
|
|
|
ushort crc16 = crc16_m;
|
|
|
|
|
fixed (ushort *crc16_t = Crc16.table)
|
2013-03-25 22:33:21 -04:00
|
|
|
for (int i = count; i > 0; i--)
|
2011-10-24 00:13:35 +00:00
|
|
|
{
|
2014-09-19 01:15:20 -04:00
|
|
|
int vi = *(residual++);
|
|
|
|
|
uint v = (uint)((vi << 1) ^ (vi >> 31));
|
2011-10-24 00:13:35 +00:00
|
|
|
|
2013-03-25 22:33:21 -04:00
|
|
|
// write quotient in unary
|
2014-09-19 01:15:20 -04:00
|
|
|
int q = (int)(v >> k) + 1;
|
2013-03-25 22:33:21 -04:00
|
|
|
int bits = k + q;
|
2013-05-08 21:31:03 -04:00
|
|
|
while (bits > 64)
|
2013-03-25 22:33:21 -04:00
|
|
|
{
|
2009-12-24 16:09:20 +00:00
|
|
|
#if DEBUG
|
2013-03-25 22:33:21 -04:00
|
|
|
if (buf + 1 > fixedbuf + buf_end)
|
|
|
|
|
{
|
|
|
|
|
eof = true;
|
|
|
|
|
return;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
2013-03-25 22:33:21 -04:00
|
|
|
#endif
|
2014-09-19 22:01:20 -04:00
|
|
|
crc16 = (ushort)((crc16 << 8) ^ crc16_t[(crc16 >> 8) ^ (*(buf++) = (byte)(bit_buf >> 56))]);
|
2013-03-25 22:33:21 -04:00
|
|
|
bit_buf <<= 8;
|
|
|
|
|
bits -= 8;
|
|
|
|
|
}
|
2009-08-28 13:00:27 +00:00
|
|
|
|
2013-03-25 22:33:21 -04:00
|
|
|
// write remainder in binary using 'k' bits
|
|
|
|
|
//writebits_fast(k + q, (uint)((v & ((1 << k) - 1)) | (1 << k)), ref buf);
|
2014-09-19 01:15:20 -04:00
|
|
|
ulong val = (uint)((v & ((1U << k) - 1)) | (1U << k));
|
2013-05-08 21:31:03 -04:00
|
|
|
if (bits < bit_left)
|
2013-03-25 22:33:21 -04:00
|
|
|
{
|
|
|
|
|
bit_left -= bits;
|
|
|
|
|
bit_buf |= val << bit_left;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ulong bb = bit_buf | (val >> (bits - bit_left));
|
2009-12-24 16:09:20 +00:00
|
|
|
#if DEBUG
|
2013-03-25 22:33:21 -04:00
|
|
|
if (buf + 8 > fixedbuf + buf_end)
|
2011-10-24 00:13:35 +00:00
|
|
|
{
|
|
|
|
|
eof = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-12-24 16:09:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
2014-09-19 22:01:20 -04:00
|
|
|
crc16 = (ushort)((crc16 << 8) ^ crc16_t[(crc16 >> 8) ^ (*(buf++) = (byte)(bb >> 56))]);
|
|
|
|
|
crc16 = (ushort)((crc16 << 8) ^ crc16_t[(crc16 >> 8) ^ (*(buf++) = (byte)(bb >> 48))]);
|
|
|
|
|
crc16 = (ushort)((crc16 << 8) ^ crc16_t[(crc16 >> 8) ^ (*(buf++) = (byte)(bb >> 40))]);
|
|
|
|
|
crc16 = (ushort)((crc16 << 8) ^ crc16_t[(crc16 >> 8) ^ (*(buf++) = (byte)(bb >> 32))]);
|
|
|
|
|
crc16 = (ushort)((crc16 << 8) ^ crc16_t[(crc16 >> 8) ^ (*(buf++) = (byte)(bb >> 24))]);
|
|
|
|
|
crc16 = (ushort)((crc16 << 8) ^ crc16_t[(crc16 >> 8) ^ (*(buf++) = (byte)(bb >> 16))]);
|
|
|
|
|
crc16 = (ushort)((crc16 << 8) ^ crc16_t[(crc16 >> 8) ^ (*(buf++) = (byte)(bb >> 8))]);
|
|
|
|
|
crc16 = (ushort)((crc16 << 8) ^ crc16_t[(crc16 >> 8) ^ (*(buf++) = (byte)(bb))]);
|
|
|
|
|
|
2013-05-08 21:31:03 -04:00
|
|
|
bit_left += 64 - bits;
|
|
|
|
|
bit_buf = (val << bit_left - 1) << 1;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-03-26 19:50:44 -04:00
|
|
|
crc16_m = crc16;
|
2013-03-25 22:33:21 -04:00
|
|
|
buf_ptr_m = (int)(buf - fixedbuf);
|
|
|
|
|
bit_buf_m = bit_buf;
|
|
|
|
|
bit_left_m = bit_left;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void flush()
|
|
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
while (bit_left_m < 64 && !eof)
|
2011-10-24 00:13:35 +00:00
|
|
|
{
|
2013-03-25 22:33:21 -04:00
|
|
|
if (buf_ptr_m >= buf_end)
|
2011-10-24 00:13:35 +00:00
|
|
|
{
|
|
|
|
|
eof = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (buffer != null)
|
2013-03-26 19:50:44 -04:00
|
|
|
{
|
|
|
|
|
byte b = (byte)(bit_buf_m >> 56);
|
|
|
|
|
crc16_m = (ushort)((crc16_m << 8) ^ Crc16.table[(crc16_m >> 8) ^ b]);
|
|
|
|
|
buffer[buf_ptr_m] = b;
|
|
|
|
|
}
|
2013-03-25 22:33:21 -04:00
|
|
|
buf_ptr_m++;
|
|
|
|
|
bit_buf_m <<= 8;
|
|
|
|
|
bit_left_m += 8;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
2013-03-25 22:33:21 -04:00
|
|
|
bit_left_m = 64;
|
|
|
|
|
bit_buf_m = 0;
|
2011-10-24 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-08-17 03:39:53 +00:00
|
|
|
}
|