Example #1
0
		public virtual void Build (BitStream bs)
		{
			Read (bs);
		}
Example #2
0
		void ReadFrame (BitStream bs)
		{
			if ((frameFlags [curFrame] & 0x01) == 0x01) {
				ReadPaletteChunk (bs);
				//				DumpPalette ();
			}

			for (int i = 1; i < 8; i ++)
				if ((frameFlags [curFrame] & (1<<i)) == (1<<i))
					ReadAudioChunk (bs, i-1);

			if (bs.Position == bs.Length)
				return;

			ReadVideoChunk(bs);
		}
Example #3
0
			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");
			}
Example #4
0
		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);
		}
Example #5
0
		void ReadAudioChunk (BitStream bs, int channel)
		{
			uint frameAudioLength = 0;
			uint audioDecompressedLength = 0;

			bool compressed = ((audioFlags[channel] & (int)ARFlags.Compressed) == (int)ARFlags.Compressed);
			bool is16 = ((audioFlags[channel] & (int)ARFlags.Is16Bit) == (int)ARFlags.Is16Bit);
			bool stereo = ((audioFlags[channel] & (int)ARFlags.Stereo) == (int)ARFlags.Stereo);

			bs.AssertAtByteBoundary ();

			bs.DebugFirst ();

			Console.WriteLine ("reading audio chunk length");
			frameAudioLength = bs.ReadDWord ();
			Console.WriteLine ("audio chunk length = {0}", frameAudioLength);

			if (compressed) {
				audioDecompressedLength = bs.ReadDWord ();
				Console.WriteLine ("decompressed audio length = {0}", audioDecompressedLength);


				byte[] streamBuf = new byte[frameAudioLength];
				bs.Read (streamBuf, 0, streamBuf.Length);

				audioOut[channel] = new byte[audioDecompressedLength];

				if (is16)
					if (stereo)
						Decompress16Stereo (new BitStream (streamBuf), new MemoryStream (audioOut[channel]));
					else
						Decompress16Mono (new BitStream (streamBuf), new MemoryStream (audioOut[channel]));
				else
					if (stereo)
						Decompress8Stereo (new BitStream (streamBuf), new MemoryStream (audioOut[channel]));
					else
						Decompress8Mono (new BitStream (streamBuf), new MemoryStream (audioOut[channel]));
			}
			else {
				audioOut[channel] = new byte[frameAudioLength];
				bs.Read (audioOut[channel], 0, (int)frameAudioLength);
			}
		}
Example #6
0
		public uint Decode (BitStream bs)
		{
			iter.Reset ();
			while (!iter.IsLeaf)
				iter.ProcessBit (bs);

			return iter.Value;
		}
Example #7
0
		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);
		}
Example #8
0
		void ReadVideoChunk (BitStream bs)
		{
			int current_block_x = 0, current_block_y = 0;
			int block_rez_x = (int)PaddedWidth / 4;
			int block_rez_y = (int)PaddedHeight / 4;

			Console.WriteLine ("reading video chunk");
			bs.DebugFirst ();

			while (true) {
				ushort type_descriptor = 0;
				if (type_Tree != null)
					type_descriptor = (ushort)type_Tree.Decode (bs);
				BlockType type = (BlockType)(type_descriptor & 0x3);
				uint chain_length = block_chain_size_table [(type_descriptor & 0xfc) >> 2];

				Console.WriteLine ("rendering {0} blocks of type {1}", chain_length, type);

				for (int i = 0; i < chain_length; i ++) {
					switch (type) {
					case BlockType.Mono:
						ushort pixel = (ushort) mclr_Tree.Decode (bs);
						byte color1 = (byte)((pixel >> 8) & 0xff);
						byte color0 = (byte)(pixel & 0xff);
						ushort map = (ushort) mmap_Tree.Decode (bs);
						int mask = 1;
						int bx, by;
						bx = current_block_x * 4;
						by = current_block_y * 4;
						for (int bi = 0; bi < 16;) {
							if ((map & mask) == 0)
								frameData[by * PaddedWidth + bx] = color0;
							else
								frameData[by * PaddedWidth + bx] = color1;
							mask <<= 1;
							bx ++;
							bi++;
							if ((bi % 4) == 0) {
								bx = current_block_x * 4;
								by ++;
							}
						}
						break;
					case BlockType.Full:
						break;
					case BlockType.Void:
						break;
					case BlockType.Solid:
						break;
					}
					current_block_x ++;
					if (current_block_x == block_rez_x) {
						current_block_x = 0;
						current_block_y ++;
						if (current_block_y == block_rez_y)
							return;
					}
				}
			}
		}
Example #9
0
		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);
		}
Example #10
0
		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);
		}
Example #11
0
		void ReadPaletteChunk (BitStream bs)
		{
			byte[] new_palette = new byte[256 * 3];

			bs.AssertAtByteBoundary ();

			bs.ZeroReadCount ();

			int paletteLength = bs.ReadByte();
			Console.WriteLine ("palette chunk is {0} bytes long", paletteLength * 4);

			//			byte[] palBuf = new byte[paletteLength * 4 - 1];
			//			bs.Read (palBuf, 0, palBuf.Length);
			//			MemoryStream palStream = new MemoryStream (palBuf, false);

			BitStream palStream = bs;
			int new_index = 0, index = 0;
			while (new_index < 256) {
				byte p = (byte)palStream.ReadByte();
				if ((p & 0x80) == 0x80) {
					int count = (p & 0x7f) + 1;
					//					Console.WriteLine ("copy1 {0} entries, from {1} to {2}", count, index, new_index);
					Array.Copy (palette, index * 3, new_palette, new_index * 3, count * 3);
					index += count;
					new_index += count;
				}
				else if ((p & 0x40) == 0x40) {
					int count = (p & 0x3f) + 1;
					int tmp_index = palStream.ReadByte();
					//					Console.WriteLine ("copy2 {0} entries, from {1} to {2}", count, tmp_index, new_index);
					Array.Copy (palette, tmp_index * 3, new_palette, new_index * 3, count * 3);
					new_index += count;
				}
				else if ((p & 0xc0) == 0x00) {
					int b = p & 0x3f;
					int g = palStream.ReadByte() & 0x3f;
					int r = palStream.ReadByte() & 0x3f;
					new_palette [new_index * 3] = palmap[r];
					new_palette [new_index * 3 + 1] = palmap[g];
					new_palette [new_index * 3 + 2] = palmap[b];
					//					Console.WriteLine ("Assign palette[{0}] = {1},{2},{3}", new_index,
					//							   palmap[r], palmap[g], palmap[b]);
					new_index ++;
				}
			}

			palette = new_palette;
			//			Console.WriteLine ("read {0} bytes toward filling the palette", palBuf.Length + 1);
		}
Example #12
0
		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]);
		}
Example #13
0
		protected override void ReadValue (BitStream bs, HuffmanNode node)
		{
			uint value = DecodeValue (bs);

			Console.WriteLine ("Read Value {0:x}", value);

			if (value == markers[0]) {
				Console.WriteLine ("found marker1");
				shortest[0] = node;
				node.value = -1;
			}
			else if (value == markers[1]) {
				Console.WriteLine ("found marker2");
				shortest[1] = node;
				node.value = -1;
			}
			else if (value == markers[2]) {
				Console.WriteLine ("found marker3");
				shortest[2] = node;
				node.value = -1;
			}
			else 
				node.value = value;
		}
Example #14
0
		uint DecodeValue (BitStream bs)
		{
			uint lowValue, highValue;

			lowValue = 0;
			if (lowTree != null) {
				lowValue = lowTree.Decode (bs);
			}

			highValue = 0;
			if (highTree != null) {
				highValue = highTree.Decode (bs);
			}

			return (lowValue | (highValue << 8));
		}
Example #15
0
		protected virtual void ReadValue (BitStream bs, HuffmanNode node)
		{
			node.value = (uint)bs.ReadByte();
		}
Example #16
0
		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);
		}
Example #17
0
		protected void Read (BitStream bs)
		{
			root = new HuffmanNode ();
			ReadNode (bs, Root);
		}
Example #18
0
		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);
			}
		}