view src/main/java/com/glavsoft/rfb/encoding/decoder/ZRLEDecoder.java @ 614:cab01ab88422

fix blocking
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 22 Feb 2020 01:54:33 +0900
parents 994a710100fd
children e7d48fb40262
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 static final int RECT_HEADER_SIZE = 16;
	private int[] decodedBitmap;
	private int[] palette;
	private boolean WifiMulticast;

	class TileLoop {
		private final boolean blocking;
		final int MARGIN = 25000;
		final int deflate_size = 62000-MARGIN;
		private ByteBuffer c1;
		private int width; // phase2 length
		private FramebufferUpdateRectangle c0rect,c1rect,rect;
		private int prevLineOffset;
		private int prevC1Offset;
		private int prevC1LineOffset;
		private int prevoffset;
		private Deflater deflater;
		private int flushOffset;

		/**
		 * 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().
		 * <p>
		 * 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(TreeRFBProto rfb, int offset) {
			prevoffset = prevLineOffset = flushOffset = offset;
			prevC1Offset = 0;
			if (rfb == null || offset < deflate_size + MARGIN) {
				// packet size fit in broadcast send it all at once
				blocking = false;
			} else
				blocking = true;
		}

		private void zrleeBlocking(TreeRFBProto rfb, ByteBuffer header, FramebufferUpdateRectangle rect, byte bytes[]) {
			// dump32(inputs);
			deflater = rfb.deflater;
			this.rect = rect;
			c0rect = null;
			c1rect = new FramebufferUpdateRectangle(rect.x, rect.y, 0, 0);
			newMulticastPacket(rfb);
			//c1.put(header.get(0));
			if (!blocking) {
				deflater.setInput(bytes, 0,(int) prevoffset);
				deflater.deflate(c1);
				flushMuticast(rfb, bytes);
			}
			System.out.println("blocking "+rect);
			return;
		}

		private void newMulticastPacket(TreeRFBProto rfb) {
			c1 = rfb.multicastqueue.allocate(deflate_size + MARGIN);
			c1.limit(deflate_size);
			if (rfb.addSerialNum)
				c1.putLong(rfb.counter++);
			if (rfb.checkDelay)
				CheckDelay.checkDelay(c1, rect.x, rect.y, rect.width, rect.height, System.currentTimeMillis(), EncodingType.CHECK_DELAY);
			c1.put((byte) 0);
			c1.put((byte) 0);
			c1.putShort((short) 0);
			c1.position(c1.position() + 12);
			c1.putInt(0);     // should be data length
			prevC1Offset = c1.position();
			prevC1LineOffset = prevC1Offset;
			c1rect.width = c1rect.height = 0;
			c0rect = null;
		}

		/**
		 * Series of tiles compose at most three rectangles. SYNC_FLUSH is necessary on
		 * rectangle boundaries.
		 *
		 *               +----+
		 *               |    |   phase 0
		 *    +---------------+
		 *    |               |   phase 1
		 *    +----+----------+
		 *    |    |              phase 2
		 *    +----+
		 *
		 * Broadcast packet have to be less than 64kbytes
		 * A tile 64x64x4 166384byte
		 *
		 * we cannot rewind zlib stream, zlib stream have to be flushed at the end of rectangles
		 * if we leave 16kbytes space margin, zlib stream can be flushed safely
		 *
		 * keep completed previous rectangle in c0rect, which is possibly none.
		 * at the last or packet full, flush c0rect first, then flush c1rect if it exists
		 *  @param rfb
		 * @param last
		 * @param bytes
		 * @param offset
		 * @param tileW
		 * @param tileH
		 * @param tileX
		 * @param tileY
		 */

		public void multicastPut(TreeRFBProto rfb, boolean last, byte[] bytes, int offset, int tileW, int tileH, int tileX, int tileY) throws TransportException {
			if (!blocking) return;
			int span = offset - prevoffset;
			deflater.setInput(bytes, prevoffset, span);
			width += tileW;
			c1rect.width += tileW;
			do {
				deflater.deflate(c1, Deflater.SYNC_FLUSH);
				if (!deflater.needsInput()) {
					// packet full
					flushDeflator(true," full ");
					int bytesRead = (int)deflater.getBytesRead();
					prevoffset = flushOffset+bytesRead;
					assert(prevoffset==offset);
					// System.out.println("od prevOffset = "+prevoffset+" span = "+(prevoffset-flushOffset)+ " " + c1rect);
					if (c0rect!=null) { // finish pahse 1
						flushRectangle(c0rect,prevC1LineOffset);
						makeHeaderSpace();
						c0rect = null;
					}
					flushRectangle(c1rect,c1.position()); // phase 2
					flushMuticast(rfb, bytes);
					if (last) {
						return;
					} else {
						newMulticastPacket(rfb);
						if (width<rect.width) { // phase 0
							c1rect.x = rect.x + width;
						} else {
							c1rect.x = 0; // phase 1
							c1rect.y += tileH;
						}
						return;
					}
				}
			} while (! deflater.needsInput());
			prevoffset = offset;
			if (last) {
				// System.out.println("last prevOffset = "+prevoffset+" span = "+(prevoffset-flushOffset)+ " " + c1rect);
				flushDeflator(false," last");
				if (c0rect!=null) {
					flushRectangle(c0rect,prevC1LineOffset);
					makeHeaderSpace();
					c0rect = null;
				}
				flushRectangle(c1rect,c1.position());
				flushMuticast(rfb, bytes);
				return;
			}
			if (c1rect.height==0) c1rect.height = tileH;
			if (c1rect.x > rect.x) {  // phase 0
				assert(c0rect==null);
				if (c1rect.x + c1rect.width >= rect.x + rect.width) {  // end of phase 0
					boolean end = flushDeflator(false," end of phase 0 ");
					width = 0;
					flushRectangle(c1rect,c1.position());
					c1rect = new FramebufferUpdateRectangle(rect.x,c1rect.y+tileH,0,0);
					if (end || c1rect.y + tileY >= rect.y+rect.height) {
						flushMuticast(rfb,bytes);
						return;
					}
					c1.position(c1.position()+ RECT_HEADER_SIZE); // header space
					prevC1Offset = c1.position();
				}
			} else {  // phase 1
				if (width >= rect.width) { // next line
					boolean end = flushDeflator(false, " phoase 1 next line ");
					width = 0;
					prevC1LineOffset = c1.position();
					if (c0rect!=null) { // extend phase 1
						c0rect.height += tileH;
					} else { // first phase 1 case
						c0rect = c1rect;
					}
					c1rect = new FramebufferUpdateRectangle(rect.x, c0rect.y+c0rect.height, 0, 0);
					if (end || c1rect.y + tileY >= rect.y+rect.height) {
						c0rect = null; // next will be first phase 1 case
						flushMuticast(rfb,bytes);
						return;
					}
					prevLineOffset = offset;
				}
			}
		}

		public void makeHeaderSpace() {
			// previous rectangle is finished, make next header space and copy already compressed part
			System.arraycopy(c1.array(),prevC1LineOffset,c1.array(),prevC1LineOffset+ RECT_HEADER_SIZE,c1.position()-prevC1LineOffset);
			c1.position(c1.position()+ RECT_HEADER_SIZE);
			prevC1Offset = prevC1LineOffset+ RECT_HEADER_SIZE;
		}

		/**
		 *
		 * @param extend   use MARGIN
		 * @param what     reason of flush (debug purpose)
		 * @return         packet full
		 * @throws TransportException
		 */
		private boolean flushDeflator(boolean extend,String what) throws TransportException {
			// System.out.println("flusing "+what+c1);
			if (extend) c1.limit(c1.capacity()- RECT_HEADER_SIZE);
			deflater.deflate(c1, Deflater.FULL_FLUSH);
			if (c1.remaining()==0) {
				if (!extend) {
					c1.limit(c1.capacity()- RECT_HEADER_SIZE);
					deflater.deflate(c1, Deflater.FULL_FLUSH);
				}
				if (c1.remaining() == 0) {
					ByteBuffer tmp = ByteBuffer.allocate(65536);
					deflater.deflate(tmp,Deflater.FULL_FLUSH);
					throw new TransportException("Multicast packet overrun "+(c1.limit()+tmp.position())+" bytes required", null);
				}
				return true;
			}
			return false;
		}

		/**
		 * fix rectangle header
		 * create next rectangle header
		 * update position paramater
		 * send muticast pacate if nessesally
		 */
		private void flushRectangle(FramebufferUpdateRectangle rect,int pos) {
			if (rect.width==0) return;
			System.out.println("sending broadcast" + rect);
			if (rect.y >= this.rect.y+this.rect.height)
				System.out.println("over y broadcast" + this.rect);
			c1.putShort(prevC1Offset - 16, (short) rect.x);
			c1.putShort(prevC1Offset - 14, (short) rect.y);
			c1.putShort(prevC1Offset - 12, (short) rect.width);
			c1.putShort(prevC1Offset - 10, (short) rect.height);
			c1.putInt(prevC1Offset - 8, EncodingType.ZRLEE.getId());
			c1.putInt(prevC1Offset  - 4, pos - prevC1Offset ); // data length
			c1.putShort(2, (short) (c1.getShort(2) + 1));    // increment rectangle count
		}

		private void flushMuticast(TreeRFBProto rfb, byte[] bytes) {
			c1.flip();
			//System.out.println("multicastPut: " + c1rect + " length: " + (c1.remaining()-c1headerPos-header.limit()));
			deflater.reset();
			LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
			bufs.add(c1);
			// rfb.getContext().checkFrameBufferRectanble(c1, bytes, flushOffset, prevoffset);
			flushOffset = prevoffset;
			if (WifiMulticast) {
				for (ByteBuffer buf : bufs)
					rfb.getViewer().getRfbBroadcastListener().multicastUpdateRectangle(buf);
			} else if (rfb.multicastBlocking) {
				rfb.multicastqueue.put(bufs);  // debug purpose
			}
		}
	}

	@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()+1024*32;
		ByteBuffer buf = unzip(reader, zippedLength, length, rect.getEncodingType());
		decode1(renderer, null, rect, buf, zippedLength, null);
	}


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

	public String decoding = "";

	public void decode1(Renderer renderer, ByteBuffer header, FramebufferUpdateRectangle rect, ByteBuffer buf, int zippedLength, TreeRFBProto rfbProto) throws TransportException {
		int offset = zippedLength;
		int maxX = rect.x + rect.width;
		int maxY = rect.y + rect.height;
		byte[] bytes = buf.array();
		WifiMulticast = rfbProto !=null && (rfbProto.multicastBlocking ||(rfbProto.isTreeManager()&&rfbProto.getViewer().getUseMulticast()));

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

		if (WifiMulticast) {
			tileloop.zrleeBlocking(rfbProto, header, rect, bytes);
		}
		try {
			// System.out.print(decoding);
			for (int tileY = rect.y; tileY < maxY; tileY += MAX_TILE_SIZE) {
				int tileHeight = Math.min(maxY - tileY, MAX_TILE_SIZE);
				if (tileloop.blocking) {
					tileloop.c1rect.height += tileHeight;
				}
				for (int tileX = rect.x; tileX < maxX; tileX += MAX_TILE_SIZE) {
					// System.out.print(" "+Integer.toHexString(0xff&bytes[offset])+(offset<bytes.length-1?Integer.toHexString(0xff&bytes[offset+1]):""));
					int tileWidth = Math.min(maxX - tileX, MAX_TILE_SIZE);
					int subencoding = bytes[offset++] & 0x0ff;
					if (subencoding != 0) {
						// System.out.println("----------------" + subencoding);
						throw new TransportException("Bad subencoding",null);
					}
					// 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 (WifiMulticast)
						tileloop.multicastPut(rfbProto, false, bytes, offset, tileWidth, tileHeight, tileX, tileY);
				}
			}
			if (WifiMulticast)
				tileloop.multicastPut(rfbProto, true, bytes, offset, 0, 0, 0, 0);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}



	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;
	}



}