view src/main/java/com/glavsoft/rfb/encoding/decoder/ZRLEDecoder.java @ 539:0d11471e0b5b

add comment ZRLEDecoder
author e165729 <e165729@ie.u-ryukyu.ac.jp>
date Thu, 05 Sep 2019 19:05:02 +0900
parents 6620e04f994c
children b86d445685c7
line wrap: on
line source

// Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
// All rights reserved.
//
//-------------------------------------------------------------------------
// This file is part of the TightVNC software.  Please visit our Web site:
//
//                       http://www.tightvnc.com/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//-------------------------------------------------------------------------
//

package com.glavsoft.rfb.encoding.decoder;

import com.glavsoft.drawing.Renderer;
import com.glavsoft.exceptions.TransportException;
import com.glavsoft.rfb.encoding.EncodingType;
import com.glavsoft.transport.Reader;
import jp.ac.u_ryukyu.treevnc.CheckDelay;
import jp.ac.u_ryukyu.treevnc.TreeRFBProto;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.zip.Deflater;

public class ZRLEDecoder extends ZlibDecoder {
	private static final int MAX_TILE_SIZE = 64;
	private int[] decodedBitmap;
	private int[] palette;

	class TileLoop {
		private final boolean blocking;
		private final int half;
		private int deflate_size = 55507;  // 圧縮サイズ
		private ByteBuffer c1;            // パケット?
		private int width; // phase2 length
		private FramebufferUpdateRectangle c1rect;
		private int c1headerPos;
		private int prevLineOffset;
		private int prevC1Offset;
		private int prevoffset;
		private Deflater deflater;     // 圧縮クラス
		private int rectPos;
		private int ztileInLine;
		private int hwidth;
		private int hc1width;
		private int hoffset;
		private int hc1offset;
		private int discard;

		/**
		 * Multicast framebufferUpdate to children.
		 * read FrameBuffferUpdate. If it is ZLE, make it ZLEE which is self contained compressed packet.
		 * put the packet to the multicastqueue. Then normal rendering engine read the same stream using is.reset().
		 *
		 * framebufferUpdateを子nodeに送信する際に、ZLEを辞書をつけてZLEEのパケットに変換する
		 * multicast queueに入れる。普通のレンダリングではis.reset()を使って同じストリームを読み込む
		 *
		 * Haeder
		 *    messageID  ( FrameBuffer Update
		 *    1 byte padding
		 *    2 byte numberofrectangle
		 *      2 - U16 - x-position
		 *      2 - U16 - y-position
		 *      2 - U16 - width
		 *      2 - U16 - height
		 *      4 - S32 - encoding-type
		 *      4 byte datalengths
		 *      datalengths databyte
		 *
		 * @throws TransportException
		 * @throws UnsupportedEncodingException
		 */

		// コンストラクタ
		public TileLoop(int offset) {
			prevoffset = prevLineOffset = prevC1Offset = offset;   // オフセット記憶群に代入
			if (offset < deflate_size+spanGap) {
				// packet size fit in broadcast send it all at once
				// ブロードキャストに適合するパケットサイズで一斉に送信
				// 十分に小さいのでブロッキングせずに送信
				blocking = false;
			} else
				blocking = true;
			discard = 0; half = 0;
		}

		// ?: ブロッキングしたものをc1に渡している?
		private void zrleeBlocking(TreeRFBProto rfb, ByteBuffer header, FramebufferUpdateRectangle rect, byte bytes[]) {
			// dump32(inputs);
			deflater = rfb.deflater;
			newMulticastPacket(rfb, rect);
			c1.put(header.get(0));
			c1rect = new FramebufferUpdateRectangle(rect.x, rect.y, 0, 0);
			if (!blocking) {
				deflater.setInput(bytes,0,prevoffset);    // bytesを圧縮データとして0からprevoffsetまでをdeflater内部にセットする
				deflater.deflate(c1);						// setInputで入力したデータを圧縮してc1に入れる
				flushMuticast(rfb);
			}
			return;
		}

		// ?: c1の値を設定している実部分? c1(=パケット)にheaderを設定してるっぽい? 要: TreeRFBProto読み
		private void newMulticastPacket(TreeRFBProto rfb, FramebufferUpdateRectangle rect) {
			c1 = rfb.multicastqueue.allocate(deflate_size);  // ByteBufferのallocateと同等
			if (rfb.addSerialNum)
				c1.putLong(rfb.counter++);   // 現在のByteBufferにlongの値を書き込み
			if (rfb.checkDelay)
				CheckDelay.checkDelay(c1, rect.x, rect.y, rect.width, rect.height, System.currentTimeMillis(), EncodingType.CHECK_DELAY);
			c1headerPos = c1.position();
			c1.put((byte) 0);
			c1.put((byte) 0);
			c1.putShort((short) 0);
			c1.position(c1.position()+16);
			c1.putInt(0);     // should be data length
			width = 0;
			rectPos = 4;
			ztileInLine = 0;
		}

		// oh... フィールド変数です...
		int spanGap = 128;
		/**
		 *
		 * Series of tiles compose at most three rectangles. SYNC_FLUSH is necessary on
		 * rectangle boundaries.
		 *
		 * tileは最大3つの長方形に分けられる
		 * をSYNC_FLUSH(パケットへ書き込み)する際は長方形の区切りの部分(境界部分)である必要がある
		 *
		 *                +----+
		 *                |  | |   phase 0
		 *     +---------------+
		 *     |       |       |   phase 1
		 *     +----+----------+
		 *     |  | |              phase 2
		 *     +----+
		 *
		 * Broadcast packet have to less than 64kbytes
		 *     A tile 64x64x3 11288byte, a packet can contain 5 raw tiles, when these are
		 *     compressed 10 to 100 tiles can be stored. It is impossible to predict the
		 *     compression rate. To check the compressed capacity, Deflate.needsInputs() can
		 *     be used. If needsInputs() is false on SYNC_FLUSH, smaller input is necessary.
		 *
		 *     We'll try 512 tiles before SYNC_FLUSH in a phase, if it fails try flush former 256 tiles.
		 *     If it will failed again, flush the previous line and do flush 512 tiles in new Packet.
		 *     If it failed again try former 256 tiles flushed, if this failes again dicard the former half.
		 *     The last case cannot happen but former 256 tiles have to be flushed, because the next 256 lines
		 *     may failed again and restart the from this point.
		 *     The next packet start with later 256 tiles filled and unflushed.
		 *
		 * ブロードキャストパケットは64KB以内である必要がある
		 * タイルは64(縦)x64(横)x3(RGB)= 11288Byteであり、圧縮しない場合だと5tile分をパケットに含めることができる
		 * tileを圧縮すると10~100タイルを含めることができる。しかし、どのくらい圧縮できるかは予想不可能
		 * 圧縮容量はDeflate.needsInputs()で確認することができる
		 * SYNC_FLUSHした時にneedsInputs()がfalseだった場合、書き込みを少なくする必要がある→パケットの容量不足、書き込み量を減らす
		 *
		 * あるphaseでSYNC_FLUSHをする前に512tileの書き込みを試す
		 * 失敗した場合半分の256tileで試す
		 * 更に失敗した場合は前の行までで書き込みを行い、新しいパケットで512tileを試す
		 * 更に更に失敗した場合は前半分の256タイルで書き込みを試す
		 * それでも失敗した場合は前半分の256タイルを破棄する??
		 * 最後の場合は起こりえないが、次の256タイル(後半部256タイル)が失敗して一つ前の段階(前半部256タイル)から再開する可能性があるため
		 * 前半部を書き込む必要がある
		 * 次のパケットは、後の256タイルがいっぱいになってフラッシュされた状態で始まります。(次のパケットは後半部の256タイルが書き込まれていない状態で始まる?)
		 *
		 * @param rfb
		 * @param last
		 * @param rect
		 * @param bytes
		 * @param offset
		 * @param tileW
		 * @param tileH
		 */

		int MAX_ZTILE = 512;   // 一つのパケットで圧縮を試す、最大のタイル数

		// フェーズ分けして、ブロッキングを行なっている部分
		public void multicastPut(TreeRFBProto rfb, boolean last, FramebufferUpdateRectangle rect, byte[] bytes, int offset, int tileW, int tileH) {
			if (!blocking) return;
			int span = offset - prevoffset;    // ?: 現在の位置調整?
			deflater.setInput(bytes,prevoffset,span);
			prevoffset = offset;
			c1rect.width  += tileW; width += tileW;
			if (c1rect.x > rect.x) {  // phase 0
				if (c1rect.x+c1rect.width < rect.x+rect.width) {
					compressAndCheckFlush(rfb, rect, bytes, offset,false, last);
				} else {
					c1rect.width = rect.x+rect.width-c1rect.x ;
					c1rect.height += tileH;
					compressAndCheckFlush(rfb,rect,bytes,offset,true, last);
				}
			} else if (!last && c1.remaining() > spanGap) { // phase 1
				if (width >= rect.width) {
					c1rect.width = rect.width;
					width = 0;
					c1rect.height += tileH;
					prevLineOffset = offset;
					prevC1Offset = c1.position();
					compressAndCheckFlush(rfb,rect,bytes,offset,true, last);
				} else {
					compressAndCheckFlush(rfb,rect,bytes,offset,false, last);
				}
			} else { // phase2
				//  rewind to the last line finish phase 1
				int savew = width;
				c1.position(prevC1Offset);
				flushRectangle(rect);
				if (savew>0) {
					// recompress overrun and flush phase 2
					c1rect.width = savew;
					c1rect.height = tileH;
					compressAndCheckFlush(rfb,rect,bytes,offset,true, last);
				}
			}
		}

		private void compressAndCheckFlush(TreeRFBProto rfb, FramebufferUpdateRectangle rect, byte[] bytes, int offset, boolean flush, boolean last) {
			ztileInLine++;
			if (!flush && ztileInLine < MAX_ZTILE) {
				if (ztileInLine == MAX_ZTILE/2) {
					hwidth = width; hc1width = c1rect.width; hoffset = offset; hc1offset = c1.position();
				}
				deflater.deflate(c1, Deflater.NO_FLUSH);
			} else {
				deflater.deflate(c1, Deflater.SYNC_FLUSH);
				if (!deflater.needsInput()) {
					// too large, try half line
					width = hwidth;
					c1rect.width = hc1width;
					c1.position(hc1offset);
					deflater.setInput(bytes, prevLineOffset, hoffset - prevC1Offset);
					deflater.deflate(c1, Deflater.SYNC_FLUSH);
					int from, len;
					if (!deflater.needsInput()) {
						// flush previous line and start new packet
						c1.position(prevC1Offset);
						unputrectangle();
						flushMuticast(rfb);
						newMulticastPacket(rfb, rect);
						nextRectangle(rect);
						// we already reached MIX_ZTILE do half of them, do compress right now
						from = prevC1Offset;
						len = offset - prevLineOffset;
						deflater.setInput(bytes, from, len);
						deflater.deflate(c1, Deflater.SYNC_FLUSH);
						if (deflater.needsInput()) {
							flushRectangle(rect);  // we are the flushed last line
							return;
						}
						// half size should always succeed
						from = prevC1Offset;
						len = hoffset - prevLineOffset;
						deflater.setInput(bytes, from, len);
						deflater.deflate(c1, Deflater.SYNC_FLUSH);
						if (!deflater.needsInput()) { /* fatal error discard this line */
							discard++;
							if (!last) {
								newMulticastPacket(rfb, rect);
								nextRectangle(rect);
							}
							return;
						} else
							flushRectangle(rect);  // normal case
						// later half is remain continue
					} else {
						flushMuticast(rfb);
						newMulticastPacket(rfb,rect);
						nextRectangle(rect);
					}
					// do later half in new Packet
					from = hoffset ; len = offset - hoffset;
					deflater.setInput(bytes, from, len);
					deflater.deflate(c1, Deflater.NO_FLUSH);
					hwidth = width;
					hoffset = offset;
					hc1offset = c1.position();
					ztileInLine = MAX_ZTILE/2;
				}
			}
		}

		/**
		 * fix rectangle header
		 * create next rectangle header
		 * update position paramater
		 * send muticast pacate if nessesally
		 */
		private void flushRectangle(FramebufferUpdateRectangle rect) {
			c1.putShort(rectPos + 0,  (short)c1rect.x);
			c1.putShort(rectPos + 2,  (short)c1rect.y);
			c1.putShort(rectPos + 4,  (short)c1rect.width);
			c1.putShort(rectPos + 6, (short)c1rect.height);
			c1.putInt(rectPos + 8,EncodingType.ZRLEE.getId());
			c1.putInt(rectPos + 12, c1.position()-rectPos-12); // data length
			rectPos = c1.position();
			c1.putShort(2,(short)(c1.getShort(2)+1));    // increment rectangle count
			nextRectangle(rect);
			ztileInLine = 0;
		}

		private void unputrectangle() {
			c1.putShort(2,(short)(c1.getShort(2)-1));    // last rectangle is canceled
		}

		private void nextRectangle(FramebufferUpdateRectangle rect) {
			if (c1rect.x+c1rect.width < rect.x+rect.width) {
				c1rect.x = c1rect.width;   // next rectangle is phase 1
			} else {
				c1rect.x = rect.x;
				c1rect.y += c1rect.height;
			}
			width = 0;
			c1rect.width = 0;
			c1rect.height = 0;
		}

		private void flushMuticast(TreeRFBProto rfb) {
			c1.flip();
			//System.out.println("multicastPut: " + c1rect + " length: " + (c1.remaining()-c1headerPos-header.limit()));
			try {
				deflater.reset();

				LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
				bufs.add(c1);
				if (rfb.isTreeManager() && rfb.connectionPresenter.isUseMulticast()) {
					for (ByteBuffer buf : bufs)
						rfb.getViewer().getRfbBroadcastListener().multicastUpdateRectangle(buf);
				} else {
					rfb.multicastqueue.waitput(bufs);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void decode(Reader reader, Renderer renderer,
					   FramebufferUpdateRectangle rect) throws TransportException {
		int zippedLength = (int) reader.readUInt32();
		if (0 == zippedLength) return;
		int length = rect.width * rect.height * renderer.getBytesPerPixel();
		byte[] bytes = unzip(reader, zippedLength, length, rect.getEncodingType());
		decode1(renderer, null, rect, bytes, zippedLength, null);
	}


	public void multicastDecode(Reader reader, Renderer renderer,
								FramebufferUpdateRectangle rect, TreeRFBProto rfb) throws TransportException {
		ByteBuffer header = ByteBuffer.allocate(16);
		reader.read(header.array());
		int zippedLength = (int) reader.readUInt32();
		if (0 == zippedLength) return;
		int length = rect.width * rect.height * renderer.getBytesPerPixel();
		byte[] bytes = unzip(reader, zippedLength, length, rect.getEncodingType());
		decode1(renderer, header, rect, bytes, zippedLength, rfb);
	}

	public void decode1(Renderer renderer, ByteBuffer header, FramebufferUpdateRectangle rect, byte[] bytes, int zippedLength, TreeRFBProto rfbProto) throws TransportException {
		int offset = zippedLength;
		int maxX = rect.x + rect.width;
		int maxY = rect.y + rect.height;

		TileLoop tileloop = new TileLoop(zippedLength);
		//System.out.println("decode1: "+rect);
		if (null == palette) {
			palette = new int [128];
		}
		if (null == decodedBitmap) {
			decodedBitmap = new int[MAX_TILE_SIZE * MAX_TILE_SIZE];
		}

		if (rfbProto.multicastBlocking)  {
			tileloop.zrleeBlocking(rfbProto, header, rect,bytes);
		}
		for (int tileY = rect.y; tileY < maxY; tileY += MAX_TILE_SIZE) {
			int tileHeight = Math.min(maxY - tileY, MAX_TILE_SIZE);

			for (int tileX = rect.x; tileX < maxX; tileX += MAX_TILE_SIZE) {
				int tileWidth = Math.min(maxX - tileX, MAX_TILE_SIZE);
				int subencoding = bytes[offset++] & 0x0ff;
				if(subencoding!=0)
					System.out.println("----------------"+subencoding);
				// 128 -plain RLE, 130-255 - Palette RLE
				boolean isRle = (subencoding & 128) != 0;
				// 2 to 16 for raw packed palette data, 130 to 255 for Palette RLE (subencoding - 128)
				int paletteSize = subencoding & 127;
				offset += readPalette(bytes, offset, renderer, paletteSize);
				if (1 == subencoding) { // A solid tile consisting of a single colour
					renderer.fillRect(palette[0], tileX, tileY, tileWidth, tileHeight);
				} else if (isRle) {
					if (0 == paletteSize) { // subencoding == 128 (or paletteSize == 0) - Plain RLE
						offset += decodePlainRle(bytes, offset, renderer, tileX, tileY, tileWidth, tileHeight);
					} else {
						offset += decodePaletteRle(bytes, offset, renderer, tileX, tileY, tileWidth, tileHeight);
					}
				} else {
					if (0 == paletteSize) { // subencoding == 0 (or paletteSize == 0) - raw CPIXEL data
						offset += decodeRaw(bytes, offset, renderer, tileX, tileY, tileWidth, tileHeight);
//						System.out.println("offset:"+offset);
					} else {
						offset += decodePacked(bytes, offset, renderer, paletteSize, tileX, tileY, tileWidth, tileHeight);
					}
				}
				if (rfbProto != null && rfbProto.multicastBlocking) tileloop.multicastPut(rfbProto, false, rect, bytes,  offset, tileWidth, tileHeight);
			}
		}
		if (rfbProto != null && rfbProto.multicastBlocking) tileloop.multicastPut(rfbProto, true, rect, bytes,  offset, 0, 0);
	}

	private int decodePlainRle(byte[] bytes, int offset, Renderer renderer,
							   int tileX, int tileY, int tileWidth, int tileHeight) {
		int bytesPerCPixel = renderer.getBytesPerCPixel();
		int decodedOffset = 0;
		int decodedEnd = tileWidth * tileHeight;
		int index = offset;
		while (decodedOffset < decodedEnd) {
			int color = renderer.getCompactPixelColor(bytes, index);
			index += bytesPerCPixel;
			int rlength = 1;
			do {
				rlength += bytes[index] & 0x0ff;
			} while ((bytes[index++] & 0x0ff) == 255);
			assert rlength <= decodedEnd - decodedOffset;
			renderer.fillColorBitmapWithColor(decodedBitmap, decodedOffset, rlength, color);
			decodedOffset += rlength;
		}
		renderer.drawColoredBitmap(decodedBitmap, tileX, tileY, tileWidth, tileHeight);
		return index - offset;
	}

	private int decodePaletteRle(byte[] bytes, int offset, Renderer renderer,
								 int tileX, int tileY, int tileWidth, int tileHeight) {
		int decodedOffset = 0;
		int decodedEnd = tileWidth * tileHeight;
		int index = offset;
		while (decodedOffset < decodedEnd) {
			int colorIndex = bytes[index++];
			int color = palette[colorIndex & 127];
			int rlength = 1;
			if ((colorIndex & 128) != 0) {
				do {
					rlength += bytes[index] & 0x0ff;
				} while (bytes[index++] == (byte) 255);
			}
			assert rlength <= decodedEnd - decodedOffset;
			renderer.fillColorBitmapWithColor(decodedBitmap, decodedOffset, rlength, color);
			decodedOffset += rlength;
		}
		renderer.drawColoredBitmap(decodedBitmap, tileX, tileY, tileWidth, tileHeight);
		return index - offset;
	}

	private int decodePacked(byte[] bytes, int offset, Renderer renderer,
							 int paletteSize, int tileX, int tileY, int tileWidth, int tileHeight) {
		int bitsPerPalletedPixel = paletteSize > 16 ? 8 : paletteSize > 4 ? 4
				: paletteSize > 2 ? 2 : 1;
		int packedOffset = offset;
		int decodedOffset = 0;
		for (int i = 0; i < tileHeight; ++i) {
			int decodedRowEnd = decodedOffset + tileWidth;
			int byteProcessed = 0;
			int bitsRemain = 0;

			while (decodedOffset < decodedRowEnd) {
				if (bitsRemain == 0) {
					byteProcessed = bytes[packedOffset++];
					bitsRemain = 8;
				}
				bitsRemain -= bitsPerPalletedPixel;
				int index = byteProcessed >> bitsRemain & (1 << bitsPerPalletedPixel) - 1 & 127;
				int color = palette[index];
				renderer.fillColorBitmapWithColor(decodedBitmap, decodedOffset, 1, color);
				++decodedOffset;
			}
		}
		renderer.drawColoredBitmap(decodedBitmap, tileX, tileY, tileWidth, tileHeight);
		return packedOffset - offset;
	}

	private int decodeRaw(byte[] bytes, int offset, Renderer renderer,
						  int tileX, int tileY, int tileWidth, int tileHeight) throws TransportException {
		return renderer.drawCompactBytes(bytes, offset, tileX, tileY, tileWidth, tileHeight);
	}

	private int readPalette(byte[] bytes, int offset, Renderer renderer, int paletteSize) {
		final int bytesPerCPixel = renderer.getBytesPerCPixel();
		for (int i=0; i<paletteSize; ++i) {
			palette[i] = renderer.getCompactPixelColor(bytes, offset + i* bytesPerCPixel);
		}
		return paletteSize * bytesPerCPixel;
	}



}