void Decompress8Mono(BitStream bs, Stream outputStream) { int flag; flag = bs.ReadBit(); /* DataPresent */ Console.WriteLine(flag == 1 ? "data present" : "data not present"); flag = bs.ReadBit(); /* Is16Bits */ Console.WriteLine(flag == 1 ? "16 bits" : "8 bits"); flag = bs.ReadBit(); /* IsStereo */ Console.WriteLine(flag == 1 ? "stereo" : "mono"); HuffmanTree t = new HuffmanTree(); byte _base; t.Build(bs); _base = (byte)bs.ReadByte(); OutputSample(outputStream, _base); while (bs.ReadCount < bs.Length) { /* read the huffman encoded deltas */ int d = (int)t.Decode(bs); _base = (byte)(_base + d); OutputSample(outputStream, _base); } Console.WriteLine("outputted {0} bytes in samples for this frame.", outputStream.Position); }
void Decompress16Stereo(BitStream bs, Stream outputStream) { int flag; flag = bs.ReadBit(); /* DataPresent */ Console.WriteLine(flag == 1 ? "data present" : "data not present"); flag = bs.ReadBit(); /* IsStereo */ Console.WriteLine(flag == 1 ? "stereo" : "mono"); flag = bs.ReadBit(); /* Is16Bits */ Console.WriteLine(flag == 1 ? "16 bits" : "8 bits"); HuffmanTree ll = new HuffmanTree(); HuffmanTree lh = new HuffmanTree(); HuffmanTree rl = new HuffmanTree(); HuffmanTree rh = new HuffmanTree(); ll.Build(bs); lh.Build(bs); rl.Build(bs); rh.Build(bs); byte[] tmp = new byte[2]; short rightbase; short leftbase; tmp[1] = (byte)bs.ReadByte(); tmp[0] = (byte)bs.ReadByte(); rightbase = BitConverter.ToInt16(tmp, 0); tmp[1] = (byte)bs.ReadByte(); tmp[0] = (byte)bs.ReadByte(); leftbase = BitConverter.ToInt16(tmp, 0); Console.WriteLine("initial bases = {0}, {1}", leftbase, rightbase); OutputSample(outputStream, leftbase, rightbase); while (bs.ReadCount < bs.Length) { /* read the huffman encoded deltas */ tmp[0] = (byte)(ll == null ? 0 : ll.Decode(bs)); tmp[1] = (byte)(lh == null ? 0 : lh.Decode(bs)); short leftdelta = BitConverter.ToInt16(tmp, 0); tmp[0] = (byte)(rl == null ? 0 : rl.Decode(bs)); tmp[1] = (byte)(rh == null ? 0 : rh.Decode(bs)); short rightdelta = BitConverter.ToInt16(tmp, 0); rightbase += rightdelta; leftbase += leftdelta; OutputSample(outputStream, leftbase, rightbase); } Console.WriteLine("outputted {0} bytes in samples for this frame.", outputStream.Position); }
public void ProcessBit(BitStream bs) { currentNode = bs.ReadBit() == 0 ? currentNode.branch_0 : currentNode.branch_1; if (currentNode == null) { throw new Exception("can't advance to child nodes from a leaf node"); } }
void Decompress16Mono(BitStream bs, Stream outputStream) { int flag; flag = bs.ReadBit(); /* DataPresent */ Console.WriteLine(flag == 1 ? "data present" : "data not present"); flag = bs.ReadBit(); /* Is16Bits */ Console.WriteLine(flag == 1 ? "16 bits" : "8 bits"); flag = bs.ReadBit(); /* IsStereo */ Console.WriteLine(flag == 1 ? "stereo" : "mono"); HuffmanTree h = new HuffmanTree(); HuffmanTree l = new HuffmanTree(); byte[] _base = new byte[2]; l.Build(bs); h.Build(bs); _base[1] = (byte)bs.ReadByte(); _base[0] = (byte)bs.ReadByte(); OutputSample(outputStream, _base); while (bs.ReadCount < bs.Length) { /* read the huffman encoded deltas */ int ld = (int)l.Decode(bs); int hd = (int)h.Decode(bs); if (ld + _base[0] > 255) { _base[1] += 1; _base[0] = (byte)(ld + _base[0] - 255); } else { _base[0] = (byte)(hd + _base[0]); } _base[1] = (byte)(hd + _base[1]); OutputSample(outputStream, _base); } Console.WriteLine("outputted {0} bytes in samples for this frame.", outputStream.Position); }
public override void Build(BitStream bs) { bs.ZeroReadCount(); #if false int tag = bs.ReadBit(); if (tag == 1) { #endif lowTree = new HuffmanTree(); Console.WriteLine("building low tree"); lowTree.Build(bs); Console.WriteLine("low tree ends at {0:x}", bs.ReadCount); #if false } tag = bs.ReadBit(); if (tag == 1) { #endif highTree = new HuffmanTree(); Console.WriteLine("building high tree"); highTree.Build(bs); Console.WriteLine("high tree ends at {0:x}", bs.ReadCount); #if false } #endif markers[0] = bs.ReadWord(); markers[1] = bs.ReadWord(); markers[2] = bs.ReadWord(); Console.WriteLine("markers = {0:x}, {1:x}, {2:x}", markers[0], markers[1], markers[2]); Read(bs); Console.WriteLine("shortest1 = {0}", shortest[0]); Console.WriteLine("shortest2 = {0}", shortest[1]); Console.WriteLine("shortest3 = {0}", shortest[2]); }
void Decompress8Stereo(BitStream bs, Stream outputStream) { int flag; flag = bs.ReadBit(); /* DataPresent */ Console.WriteLine(flag == 1 ? "data present" : "data not present"); flag = bs.ReadBit(); /* Is16Bits */ Console.WriteLine(flag == 1 ? "16 bits" : "8 bits"); flag = bs.ReadBit(); /* IsStereo */ Console.WriteLine(flag == 1 ? "stereo" : "mono"); HuffmanTree r = new HuffmanTree(); HuffmanTree l = new HuffmanTree(); byte leftbase; byte rightbase; l.Build(bs); r.Build(bs); rightbase = (byte)bs.ReadByte(); leftbase = (byte)bs.ReadByte(); OutputSample(outputStream, leftbase, rightbase); while (bs.ReadCount < bs.Length) { /* read the huffman encoded deltas */ int rd = (int)r.Decode(bs); int ld = (int)l.Decode(bs); rightbase = (byte)(rightbase + rd); leftbase = (byte)(leftbase + ld); OutputSample(outputStream, leftbase, rightbase); } Console.WriteLine("outputted {0} bytes in samples for this frame.", outputStream.Position); }
protected void ReadNode(BitStream bs, HuffmanNode node) { int flag = bs.ReadBit(); if (flag == 0) { /* it's a leaf */ ReadValue(bs, node); } else { /* it's a node */ node.branch_0 = new HuffmanNode(); ReadNode(bs, node.branch_0); node.branch_1 = new HuffmanNode(); ReadNode(bs, node.branch_1); } }
public void ProcessBit (BitStream bs) { currentNode = bs.ReadBit() == 0 ? currentNode.branch_0 : currentNode.branch_1; if (currentNode == null) throw new Exception ("can't advance to child nodes from a leaf node"); }
void Decompress8Mono (BitStream bs, Stream outputStream) { int flag; flag = bs.ReadBit(); /* DataPresent */ Console.WriteLine (flag == 1 ? "data present" : "data not present"); flag = bs.ReadBit(); /* Is16Bits */ Console.WriteLine (flag == 1 ? "16 bits" : "8 bits"); flag = bs.ReadBit(); /* IsStereo */ Console.WriteLine (flag == 1 ? "stereo" : "mono"); HuffmanTree t = new HuffmanTree(); byte _base; t.Build (bs); _base = (byte)bs.ReadByte(); OutputSample (outputStream, _base); while (bs.ReadCount < bs.Length) { /* read the huffman encoded deltas */ int d = (int)t.Decode (bs); _base = (byte)(_base + d); OutputSample (outputStream, _base); } Console.WriteLine ("outputted {0} bytes in samples for this frame.", outputStream.Position); }
void Decompress8Stereo (BitStream bs, Stream outputStream) { int flag; flag = bs.ReadBit(); /* DataPresent */ Console.WriteLine (flag == 1 ? "data present" : "data not present"); flag = bs.ReadBit(); /* Is16Bits */ Console.WriteLine (flag == 1 ? "16 bits" : "8 bits"); flag = bs.ReadBit(); /* IsStereo */ Console.WriteLine (flag == 1 ? "stereo" : "mono"); HuffmanTree r = new HuffmanTree(); HuffmanTree l = new HuffmanTree(); byte leftbase; byte rightbase; l.Build (bs); r.Build (bs); rightbase = (byte)bs.ReadByte(); leftbase = (byte)bs.ReadByte(); OutputSample (outputStream, leftbase, rightbase); while (bs.ReadCount < bs.Length) { /* read the huffman encoded deltas */ int rd = (int)r.Decode (bs); int ld = (int)l.Decode (bs); rightbase = (byte)(rightbase + rd); leftbase = (byte)(leftbase + ld); OutputSample (outputStream, leftbase, rightbase); } Console.WriteLine ("outputted {0} bytes in samples for this frame.", outputStream.Position); }
void Decompress16Mono (BitStream bs, Stream outputStream) { int flag; flag = bs.ReadBit(); /* DataPresent */ Console.WriteLine (flag == 1 ? "data present" : "data not present"); flag = bs.ReadBit(); /* Is16Bits */ Console.WriteLine (flag == 1 ? "16 bits" : "8 bits"); flag = bs.ReadBit(); /* IsStereo */ Console.WriteLine (flag == 1 ? "stereo" : "mono"); HuffmanTree h = new HuffmanTree(); HuffmanTree l = new HuffmanTree(); byte[] _base = new byte[2]; l.Build (bs); h.Build (bs); _base[1] = (byte)bs.ReadByte(); _base[0] = (byte)bs.ReadByte(); OutputSample (outputStream, _base); while (bs.ReadCount < bs.Length) { /* read the huffman encoded deltas */ int ld = (int)l.Decode (bs); int hd = (int)h.Decode (bs); if (ld + _base[0] > 255) { _base[1] += 1; _base[0] = (byte)(ld + _base[0] - 255); } else _base[0] = (byte)(hd + _base[0]); _base[1] = (byte)(hd + _base[1]); OutputSample (outputStream, _base); } Console.WriteLine ("outputted {0} bytes in samples for this frame.", outputStream.Position); }
void Decompress16Stereo (BitStream bs, Stream outputStream) { int flag; flag = bs.ReadBit(); /* DataPresent */ Console.WriteLine (flag == 1 ? "data present" : "data not present"); flag = bs.ReadBit(); /* IsStereo */ Console.WriteLine (flag == 1 ? "stereo" : "mono"); flag = bs.ReadBit(); /* Is16Bits */ Console.WriteLine (flag == 1 ? "16 bits" : "8 bits"); HuffmanTree ll = new HuffmanTree(); HuffmanTree lh = new HuffmanTree(); HuffmanTree rl = new HuffmanTree(); HuffmanTree rh = new HuffmanTree(); ll.Build (bs); lh.Build (bs); rl.Build (bs); rh.Build (bs); byte[] tmp = new byte[2]; short rightbase; short leftbase; tmp[1] = (byte)bs.ReadByte(); tmp[0] = (byte)bs.ReadByte(); rightbase = BitConverter.ToInt16 (tmp, 0); tmp[1] = (byte)bs.ReadByte(); tmp[0] = (byte)bs.ReadByte(); leftbase = BitConverter.ToInt16 (tmp, 0); Console.WriteLine ("initial bases = {0}, {1}", leftbase, rightbase); OutputSample (outputStream, leftbase, rightbase); while (bs.ReadCount < bs.Length) { /* read the huffman encoded deltas */ tmp[0] = (byte)(ll == null ? 0 : ll.Decode (bs)); tmp[1] = (byte)(lh == null ? 0 : lh.Decode (bs)); short leftdelta = BitConverter.ToInt16 (tmp, 0); tmp[0] = (byte)(rl == null ? 0 : rl.Decode (bs)); tmp[1] = (byte)(rh == null ? 0 : rh.Decode (bs)); short rightdelta = BitConverter.ToInt16 (tmp, 0); rightbase += rightdelta; leftbase += leftdelta; OutputSample (outputStream, leftbase, rightbase); } Console.WriteLine ("outputted {0} bytes in samples for this frame.", outputStream.Position); }
void ReadTrees () { Console.WriteLine ("Building huffman trees (from position {0:x}, length {1:x}):", stream.Position, treesSize); byte[] tree_buf = new byte[treesSize]; stream.Read (tree_buf, 0, (int)treesSize); BitStream bs = new BitStream (tree_buf); int tag; BitStream.debug = true; #if false tag = bs.ReadBit (); if (tag == 1) { #endif Console.WriteLine (" + MMap"); mmap_Tree = new Dynamic16HuffmanTree (); mmap_Tree.Build (bs); Console.WriteLine (" + After MMap tree, read {0} bytes", bs.ReadCount); #if false } #endif BitStream.debug = false; bs.ZeroReadCount (); #if false tag = bs.ReadBit (); if (tag == 1) { #endif Console.WriteLine (" + MClr"); mclr_Tree = new Dynamic16HuffmanTree (); mclr_Tree.Build (bs); Console.WriteLine (" + After MClr tree, read {0} bytes", bs.ReadCount); #if false } bs.ZeroReadCount (); tag = bs.ReadBit (); if (tag == 1) { #endif Console.WriteLine (" + Full"); full_Tree = new Dynamic16HuffmanTree (); full_Tree.Build (bs); Console.WriteLine (" + After Full tree, read {0} bytes", bs.ReadCount); #if false } bs.ZeroReadCount (); tag = bs.ReadBit (); if (tag == 1) { #endif Console.WriteLine (" + Type"); type_Tree = new Dynamic16HuffmanTree (); type_Tree.Build (bs); Console.WriteLine (" + After Type tree, read {0} bytes", bs.ReadCount); #if false } #endif Console.WriteLine ("done (read {0} out of {1} bytes).", bs.ReadCount, treesSize); }
public override void Build (BitStream bs) { bs.ZeroReadCount (); #if false int tag = bs.ReadBit(); if (tag == 1) { #endif lowTree = new HuffmanTree (); Console.WriteLine ("building low tree"); lowTree.Build (bs); Console.WriteLine ("low tree ends at {0:x}", bs.ReadCount); #if false } tag = bs.ReadBit (); if (tag == 1) { #endif highTree = new HuffmanTree (); Console.WriteLine ("building high tree"); highTree.Build (bs); Console.WriteLine ("high tree ends at {0:x}", bs.ReadCount); #if false } #endif markers[0] = bs.ReadWord (); markers[1] = bs.ReadWord (); markers[2] = bs.ReadWord (); Console.WriteLine ("markers = {0:x}, {1:x}, {2:x}", markers[0], markers[1], markers[2]); Read (bs); Console.WriteLine ("shortest1 = {0}", shortest[0]); Console.WriteLine ("shortest2 = {0}", shortest[1]); Console.WriteLine ("shortest3 = {0}", shortest[2]); }
protected void ReadNode (BitStream bs, HuffmanNode node) { int flag = bs.ReadBit(); if (flag == 0) { /* it's a leaf */ ReadValue (bs, node); } else { /* it's a node */ node.branch_0 = new HuffmanNode (); ReadNode (bs, node.branch_0); node.branch_1 = new HuffmanNode (); ReadNode (bs, node.branch_1); } }
void ReadTrees() { Console.WriteLine("Building huffman trees (from position {0:x}, length {1:x}):", stream.Position, treesSize); byte[] tree_buf = new byte[treesSize]; stream.Read(tree_buf, 0, (int)treesSize); BitStream bs = new BitStream(tree_buf); int tag; BitStream.debug = true; #if false tag = bs.ReadBit(); if (tag == 1) { #endif Console.WriteLine(" + MMap"); mmap_Tree = new Dynamic16HuffmanTree(); mmap_Tree.Build(bs); Console.WriteLine(" + After MMap tree, read {0} bytes", bs.ReadCount); #if false } #endif BitStream.debug = false; bs.ZeroReadCount(); #if false tag = bs.ReadBit(); if (tag == 1) { #endif Console.WriteLine(" + MClr"); mclr_Tree = new Dynamic16HuffmanTree(); mclr_Tree.Build(bs); Console.WriteLine(" + After MClr tree, read {0} bytes", bs.ReadCount); #if false } bs.ZeroReadCount(); tag = bs.ReadBit(); if (tag == 1) { #endif Console.WriteLine(" + Full"); full_Tree = new Dynamic16HuffmanTree(); full_Tree.Build(bs); Console.WriteLine(" + After Full tree, read {0} bytes", bs.ReadCount); #if false } bs.ZeroReadCount(); tag = bs.ReadBit(); if (tag == 1) { #endif Console.WriteLine(" + Type"); type_Tree = new Dynamic16HuffmanTree(); type_Tree.Build(bs); Console.WriteLine(" + After Type tree, read {0} bytes", bs.ReadCount); #if false } #endif Console.WriteLine("done (read {0} out of {1} bytes).", bs.ReadCount, treesSize); }