private static int DecodeLiteral(BitStreamReader bitStream, PkLibCompressionType compressionType) { Contract.Requires(bitStream != null); // Return values: // 0x000 to 0x0ff: One byte from compressed file. // 0x100 to 0x305: Copy previous block (0x100 = 1 byte). // -1: End of stream. switch (bitStream.ReadBits(1)) { case -1: return(-1); case 1: // The next bits are positions in buffers. int pos = _sPosition2[bitStream.PeekByte()]; // Skip the bits we just used. var numBits = _sLenBits[pos]; Contract.Assume(numBits < BitStreamReader.MaxBitCount); if (bitStream.ReadBits(numBits) == -1) { return(-1); } var nBits = _sExLenBits[pos]; if (nBits != 0) { Contract.Assume(nBits < BitStreamReader.MaxBitCount); var val2 = bitStream.ReadBits(nBits); if (val2 == -1 && (pos + val2 != 0x10e)) { return(-1); } pos = _sLenBase[pos] + val2; } return(pos + 0x100); // Return number of bytes to repeat. case 0: if (compressionType == PkLibCompressionType.Binary) { return(bitStream.ReadBits(sizeof(byte) * 8)); } // TODO: Implement ASCII mode. throw new NotImplementedException("ASCII mode is not yet implemented."); default: return(0); } }
public override void Parse(TimeSpan timeOffset, ArraySegment <byte> byteSegment, bool markerBit) { int auHeadersBitLength = BigEndianConverter.ReadUInt16(byteSegment.Array, byteSegment.Offset); int auHeadersLengthBytes = (auHeadersBitLength + 7) / 8; int headerBitSize = _codecInfo.SizeLength + _codecInfo.IndexLength; int audioBitsAvail = auHeadersBitLength - headerBitSize; if (audioBitsAvail < 0 || headerBitSize <= 0) { return; } int framesCount = 1 + audioBitsAvail / (_codecInfo.SizeLength + _codecInfo.IndexDeltaLength); _bitStreamReader.ReInitialize(byteSegment.SubSegment(2)); int offset = byteSegment.Offset + auHeadersLengthBytes; for (int i = 0; i < framesCount; ++i) { int frameSize = _bitStreamReader.ReadBits(_codecInfo.SizeLength); if (i == 0) { _bitStreamReader.ReadBits(_codecInfo.IndexLength); } else if (_codecInfo.IndexDeltaLength != 0) { _bitStreamReader.ReadBits(_codecInfo.IndexDeltaLength); } Debug.Assert(byteSegment.Array != null, "byteSegment.Array != null"); var frameBytes = new ArraySegment <byte>(byteSegment.Array, offset, frameSize); DateTime timestamp = GetFrameTimestamp(timeOffset); var aacFrame = new RawAACFrame(timestamp, frameBytes, new ArraySegment <byte>(_codecInfo.ConfigBytes)); OnFrameGenerated(aacFrame); offset += frameSize; } }
private static int DecodeDistance(BitStreamReader bitStream, int length, int dictSizeBits) { Contract.Requires(bitStream != null); Contract.Requires(length >= 0); Contract.Requires(dictSizeBits >= 0); Contract.Requires(dictSizeBits < BitStreamReader.MaxBitCount); if (bitStream.EnsureBits(8) == false) { return(0); } var pos = (int)_sPosition1[bitStream.PeekByte()]; var skip = _sDistBits[pos]; // Number of bits to skip. // Skip the appropriate number of bits Contract.Assume(skip < BitStreamReader.MaxBitCount); if (bitStream.ReadBits(skip) == -1) { return(0); } if (length == 2) { if (bitStream.EnsureBits(2) == false) { return(0); } pos = (pos << 2) | bitStream.ReadBits(2); } else { if (bitStream.EnsureBits(dictSizeBits) == false) { return(0); } pos = ((pos << dictSizeBits)) | bitStream.ReadBits(dictSizeBits); } return(pos + 1); }
public void ReadBits_ReadSeveralBits_ValidResult() { var bytes = new byte[] { 0x31 }; var reader = new BitStreamReader(); reader.ReInitialize(new ArraySegment <byte>(bytes)); int bits = reader.ReadBits(5); Assert.AreEqual(6, bits); }
public static ulong ReadDynamicLength(BitStreamReader aReader) { if (aReader.ReadBit() == 0) { return(aReader.ReadBits(7)); } if (aReader.ReadBit() == 0) { return(aReader.ReadBits(8) + 128UL); } ulong val = aReader.ReadBits(8); int shift = 8; while (aReader.ReadBit() == 1) { val |= aReader.ReadBits(4) << shift; shift += 4; } return(val + 384UL); }
private static INode GenerateTree(BitStreamReader aReader) { var bit = aReader.ReadBit(); if (bit != 0) { return(new LeafNode((byte)aReader.ReadBits(8))); } var n1 = GenerateTree(aReader); var n2 = GenerateTree(aReader); return(new TreeNode(n1, n2)); }
private void ReadLZWImage(BinaryReader aReader, GIFImageBlock res) { byte initialCodeSize = aReader.ReadByte(); var bitReader = new BitStreamReader(aReader); var output = new List <byte>(); var dict = new Dict(initialCodeSize); long startPos = aReader.BaseStream.Position; while (!bitReader.EndOfData) { int code = (int)bitReader.ReadBits(dict.bits); if (bitReader.EndOfData) { break; } if (code == dict.ClearCode) { dict.Clear(); } else if (code == dict.EODCode) { break; } else { int pre = dict.Expand(code, output); if (pre != -1 && dict.lastCode != -1) { dict.AddPair(dict.lastCode, (byte)pre); } dict.lastCode = code; } } if (!bitReader.EndOfData) { byte tmp = 1; while (tmp != 0 && aReader.BaseStream.Position < aReader.BaseStream.Length) { tmp = aReader.ReadByte(); } } res.packedSize = (int)(aReader.BaseStream.Position - startPos); res.data = output; GIFLoader.Log("Image Data:" + output.Count + " (packed: " + res.packedSize + ")"); GIFLoader.Log("X: " + res.xPos + " Y: " + res.yPos); GIFLoader.Log("W: " + res.width + " H: " + res.height); GIFLoader.Log("Winterlaced: " + res.IsInterlaced); GIFLoader.Log("---"); }
public static byte[] Decompress(BinaryReader data) { Contract.Requires(data != null); Contract.Ensures(Contract.Result <byte[]>() != null); var compType = data.ReadByte(); if (compType == 0) { throw new NotImplementedException("Compression type 0 is not currently supported."); } var primeData = _sPrime[compType]; Contract.Assume(primeData != null); var tail = BuildList(primeData); var head = BuildTree(tail); using (var outputStream = new MemoryStream()) { var bitStream = new BitStreamReader(data); int decoded; do { var node = Decode(bitStream, head); decoded = node.DecompressedValue; switch (decoded) { case byte.MaxValue: break; case byte.MaxValue + 1: var newValue = bitStream.ReadBits(8); outputStream.WriteByte((byte)newValue); Contract.Assume(tail != null); tail = InsertNode(tail, newValue); break; default: outputStream.WriteByte((byte)decoded); break; } } while (decoded != byte.MaxValue); return(outputStream.ToArray()); } }
private static HuffmanLinkedNode Decode(BitStreamReader input, HuffmanLinkedNode head) { Contract.Requires(input != null); Contract.Requires(head != null); Contract.Ensures(Contract.Result <HuffmanLinkedNode>() != null); while (head.Child0 != null) { var bit = input.ReadBits(1); if (bit == -1) { throw new InvalidDataException("Unexpected end of file."); } head = bit == 0 ? head.Child0 : head.Child1; } return(head); }
private static void ReadIndexedImage(BinaryReader aReader, BMPImage bmp) { int w = Mathf.Abs(bmp.info.width); int h = Mathf.Abs(bmp.info.height); int bitCount = bmp.info.nBitsPerPixel; int rowLength = ((bitCount * w + 31) / 32) * 4; int count = rowLength * h; int pad = rowLength - (w * bitCount + 7) / 8; Color32[] data = bmp.imageData = new Color32[w * h]; if (aReader.BaseStream.Position + count > aReader.BaseStream.Length) { Debug.LogError("Unexpected end of file. (Have " + (aReader.BaseStream.Position + count) + " bytes, expected " + aReader.BaseStream.Length + " bytes)"); return; } BitStreamReader bitReader = new BitStreamReader(aReader); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int v = (int)bitReader.ReadBits(bitCount); if (v >= bmp.palette.Count) { Debug.LogError("Indexed bitmap has indices greater than it's color palette"); return; } data[x + y * w] = bmp.palette[v]; } bitReader.Flush(); for (int i = 0; i < pad; i++) { aReader.ReadByte(); } } }
public static Byte ReadByte(this BitStreamReader reader) { return((Byte)reader.ReadBits(8)); }
public static Single ReadFloat32(this BitStreamReader reader) { return(BitConverterX.Int32ToFloat32(reader.ReadBits(32))); }
public static UInt32 ReadUInt32(this BitStreamReader reader) { return(BitConverterX.Int32ToUInt32(reader.ReadBits(32))); }
public static UInt16 ReadUInt16(this BitStreamReader reader) { return((UInt16)BitConverterX.Int32ToUInt32(reader.ReadBits(16))); }
public static Int32 ReadInt32(this BitStreamReader reader) { return(reader.ReadBits(32)); }
public static Int16 ReadInt16(this BitStreamReader reader) { return((Int16)reader.ReadBits(16)); }
public static Char ReadChar(this BitStreamReader reader) { return((Char)reader.ReadBits(16)); }
private static int DecodeLiteral(BitStreamReader bitStream, PkLibCompressionType compressionType) { Contract.Requires(bitStream != null); // Return values: // 0x000 to 0x0ff: One byte from compressed file. // 0x100 to 0x305: Copy previous block (0x100 = 1 byte). // -1: End of stream. switch (bitStream.ReadBits(1)) { case -1: return -1; case 1: // The next bits are positions in buffers. int pos = _sPosition2[bitStream.PeekByte()]; // Skip the bits we just used. var numBits = _sLenBits[pos]; Contract.Assume(numBits < BitStreamReader.MaxBitCount); if (bitStream.ReadBits(numBits) == -1) return -1; var nBits = _sExLenBits[pos]; if (nBits != 0) { Contract.Assume(nBits < BitStreamReader.MaxBitCount); var val2 = bitStream.ReadBits(nBits); if (val2 == -1 && (pos + val2 != 0x10e)) return -1; pos = _sLenBase[pos] + val2; } return pos + 0x100; // Return number of bytes to repeat. case 0: if (compressionType == PkLibCompressionType.Binary) return bitStream.ReadBits(sizeof(byte) * 8); // TODO: Implement ASCII mode. throw new NotImplementedException("ASCII mode is not yet implemented."); default: return 0; } }
private static int DecodeDistance(BitStreamReader bitStream, int length, int dictSizeBits) { Contract.Requires(bitStream != null); Contract.Requires(length >= 0); Contract.Requires(dictSizeBits >= 0); Contract.Requires(dictSizeBits < BitStreamReader.MaxBitCount); if (bitStream.EnsureBits(8) == false) return 0; var pos = (int)_sPosition1[bitStream.PeekByte()]; var skip = _sDistBits[pos]; // Number of bits to skip. // Skip the appropriate number of bits Contract.Assume(skip < BitStreamReader.MaxBitCount); if (bitStream.ReadBits(skip) == -1) return 0; if (length == 2) { if (bitStream.EnsureBits(2) == false) return 0; pos = (pos << 2) | bitStream.ReadBits(2); } else { if (bitStream.EnsureBits(dictSizeBits) == false) return 0; pos = ((pos << dictSizeBits)) | bitStream.ReadBits(dictSizeBits); } return pos + 1; }