Blame libfreerdp/codec/rfx.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * RemoteFX Codec Library
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Vic Lee
Packit 1fb8d4
 * Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2015 Norbert Federa <norbert.federa@thincast.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <assert.h>
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <stdlib.h>
Packit 1fb8d4
#include <string.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/tchar.h>
Packit 1fb8d4
#include <winpr/sysinfo.h>
Packit 1fb8d4
#include <winpr/registry.h>
Packit 1fb8d4
#include <winpr/tchar.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/log.h>
Packit 1fb8d4
#include <freerdp/codec/rfx.h>
Packit 1fb8d4
#include <freerdp/constants.h>
Packit 1fb8d4
#include <freerdp/primitives.h>
Packit 1fb8d4
#include <freerdp/codec/region.h>
Packit 1fb8d4
#include <freerdp/build-config.h>
Packit 1fb8d4
#include <freerdp/codec/region.h>
Packit 1fb8d4
Packit 1fb8d4
#include "rfx_constants.h"
Packit 1fb8d4
#include "rfx_types.h"
Packit 1fb8d4
#include "rfx_decode.h"
Packit 1fb8d4
#include "rfx_encode.h"
Packit 1fb8d4
#include "rfx_quantization.h"
Packit 1fb8d4
#include "rfx_dwt.h"
Packit 1fb8d4
#include "rfx_rlgr.h"
Packit 1fb8d4
Packit 1fb8d4
#include "rfx_sse2.h"
Packit 1fb8d4
#include "rfx_neon.h"
Packit 1fb8d4
Packit 1fb8d4
#define TAG FREERDP_TAG("codec")
Packit 1fb8d4
Packit 1fb8d4
#ifndef RFX_INIT_SIMD
Packit Service 5a9772
#define RFX_INIT_SIMD(_rfx_context) \
Packit Service 5a9772
	do                              \
Packit Service 5a9772
	{                               \
Packit Service 5a9772
	} while (0)
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit Service 5a9772
#define RFX_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\RemoteFX"
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * The quantization values control the compression rate and quality. The value
Packit 1fb8d4
 * range is between 6 and 15. The higher value, the higher compression rate
Packit 1fb8d4
 * and lower quality.
Packit 1fb8d4
 *
Packit 1fb8d4
 * This is the default values being use by the MS RDP server, and we will also
Packit 1fb8d4
 * use it as our default values for the encoder. It can be overrided by setting
Packit 1fb8d4
 * the context->num_quants and context->quants member.
Packit 1fb8d4
 *
Packit 1fb8d4
 * The order of the values are:
Packit 1fb8d4
 * LL3, LH3, HL3, HH3, LH2, HL2, HH2, LH1, HL1, HH1
Packit 1fb8d4
 */
Packit Service 5a9772
static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 };
Packit 1fb8d4
Packit 1fb8d4
static void rfx_profiler_create(RFX_CONTEXT* context)
Packit 1fb8d4
{
Packit 1fb8d4
	PROFILER_CREATE(context->priv->prof_rfx_decode_rgb, "rfx_decode_rgb")
Packit Service 5a9772
	PROFILER_CREATE(context->priv->prof_rfx_decode_component, "rfx_decode_component")
Packit 1fb8d4
	PROFILER_CREATE(context->priv->prof_rfx_rlgr_decode, "rfx_rlgr_decode")
Packit Service 5a9772
	PROFILER_CREATE(context->priv->prof_rfx_differential_decode, "rfx_differential_decode")
Packit Service 5a9772
	PROFILER_CREATE(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode")
Packit 1fb8d4
	PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode")
Packit 1fb8d4
	PROFILER_CREATE(context->priv->prof_rfx_ycbcr_to_rgb, "prims->yCbCrToRGB")
Packit 1fb8d4
	PROFILER_CREATE(context->priv->prof_rfx_encode_rgb, "rfx_encode_rgb")
Packit Service 5a9772
	PROFILER_CREATE(context->priv->prof_rfx_encode_component, "rfx_encode_component")
Packit 1fb8d4
	PROFILER_CREATE(context->priv->prof_rfx_rlgr_encode, "rfx_rlgr_encode")
Packit Service 5a9772
	PROFILER_CREATE(context->priv->prof_rfx_differential_encode, "rfx_differential_encode")
Packit Service 5a9772
	PROFILER_CREATE(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode")
Packit 1fb8d4
	PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode")
Packit 1fb8d4
	PROFILER_CREATE(context->priv->prof_rfx_rgb_to_ycbcr, "prims->RGBToYCbCr")
Packit Service 5a9772
	PROFILER_CREATE(context->priv->prof_rfx_encode_format_rgb, "rfx_encode_format_rgb")
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_profiler_free(RFX_CONTEXT* context)
Packit 1fb8d4
{
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_decode_rgb)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_decode_component)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_rlgr_decode)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_differential_decode)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_quantization_decode)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_dwt_2d_decode)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_ycbcr_to_rgb)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_encode_rgb)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_encode_component)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_rlgr_encode)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_differential_encode)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_quantization_encode)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_dwt_2d_encode)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_rgb_to_ycbcr)
Packit 1fb8d4
	PROFILER_FREE(context->priv->prof_rfx_encode_format_rgb)
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_profiler_print(RFX_CONTEXT* context)
Packit 1fb8d4
{
Packit 1fb8d4
	PROFILER_PRINT_HEADER
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_decode_rgb)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_decode_component)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_rlgr_decode)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_differential_decode)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_quantization_decode)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_decode)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_ycbcr_to_rgb)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_encode_rgb)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_encode_component)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_rlgr_encode)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_differential_encode)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_quantization_encode)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_encode)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_rgb_to_ycbcr)
Packit 1fb8d4
	PROFILER_PRINT(context->priv->prof_rfx_encode_format_rgb)
Packit 1fb8d4
	PROFILER_PRINT_FOOTER
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_tile_init(void* obj)
Packit 1fb8d4
{
Packit 1fb8d4
	RFX_TILE* tile = (RFX_TILE*)obj;
Packit 1fb8d4
	if (tile)
Packit 1fb8d4
	{
Packit 1fb8d4
		tile->x = 0;
Packit 1fb8d4
		tile->y = 0;
Packit 1fb8d4
		tile->YLen = 0;
Packit 1fb8d4
		tile->YData = NULL;
Packit 1fb8d4
		tile->CbLen = 0;
Packit 1fb8d4
		tile->CbData = NULL;
Packit 1fb8d4
		tile->CrLen = 0;
Packit 1fb8d4
		tile->CrData = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void* rfx_decoder_tile_new(void* val)
Packit 1fb8d4
{
Packit 1fb8d4
	RFX_TILE* tile = NULL;
Packit 1fb8d4
	WINPR_UNUSED(val);
Packit 1fb8d4
Packit Service 5a9772
	if (!(tile = (RFX_TILE*)calloc(1, sizeof(RFX_TILE))))
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit Service 5a9772
	if (!(tile->data = (BYTE*)_aligned_malloc(4 * 64 * 64, 16)))
Packit 1fb8d4
	{
Packit 1fb8d4
		free(tile);
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	tile->allocated = TRUE;
Packit 1fb8d4
	return tile;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_decoder_tile_free(void* obj)
Packit 1fb8d4
{
Packit 1fb8d4
	RFX_TILE* tile = (RFX_TILE*)obj;
Packit 1fb8d4
Packit 1fb8d4
	if (tile)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (tile->allocated)
Packit 1fb8d4
			_aligned_free(tile->data);
Packit 1fb8d4
Packit 1fb8d4
		free(tile);
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void* rfx_encoder_tile_new(void* val)
Packit 1fb8d4
{
Packit 1fb8d4
	WINPR_UNUSED(val);
Packit 1fb8d4
	return calloc(1, sizeof(RFX_TILE));
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_encoder_tile_free(void* obj)
Packit 1fb8d4
{
Packit 1fb8d4
	free(obj);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
RFX_CONTEXT* rfx_context_new(BOOL encoder)
Packit 1fb8d4
{
Packit 1fb8d4
	HKEY hKey;
Packit 1fb8d4
	LONG status;
Packit 1fb8d4
	DWORD dwType;
Packit 1fb8d4
	DWORD dwSize;
Packit 1fb8d4
	DWORD dwValue;
Packit 1fb8d4
	SYSTEM_INFO sysinfo;
Packit 1fb8d4
	RFX_CONTEXT* context;
Packit 1fb8d4
	wObject* pool;
Packit 1fb8d4
	RFX_CONTEXT_PRIV* priv;
Packit 1fb8d4
	context = (RFX_CONTEXT*)calloc(1, sizeof(RFX_CONTEXT));
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	context->encoder = encoder;
Packit Service 5a9772
	context->currentMessage.freeArray = TRUE;
Packit 1fb8d4
	context->priv = priv = (RFX_CONTEXT_PRIV*)calloc(1, sizeof(RFX_CONTEXT_PRIV));
Packit 1fb8d4
Packit 1fb8d4
	if (!priv)
Packit 1fb8d4
		goto error_priv;
Packit 1fb8d4
Packit 1fb8d4
	priv->log = WLog_Get("com.freerdp.codec.rfx");
Packit 1fb8d4
	WLog_OpenAppender(priv->log);
Packit 1fb8d4
#ifdef WITH_DEBUG_RFX
Packit 1fb8d4
	WLog_SetLogLevel(priv->log, WLOG_DEBUG);
Packit 1fb8d4
#endif
Packit 1fb8d4
	priv->TilePool = ObjectPool_New(TRUE);
Packit 1fb8d4
Packit 1fb8d4
	if (!priv->TilePool)
Packit 1fb8d4
		goto error_tilePool;
Packit 1fb8d4
Packit 1fb8d4
	pool = ObjectPool_Object(priv->TilePool);
Packit 1fb8d4
	pool->fnObjectInit = rfx_tile_init;
Packit 1fb8d4
Packit 1fb8d4
	if (context->encoder)
Packit 1fb8d4
	{
Packit 1fb8d4
		pool->fnObjectNew = rfx_encoder_tile_new;
Packit 1fb8d4
		pool->fnObjectFree = rfx_encoder_tile_free;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		pool->fnObjectNew = rfx_decoder_tile_new;
Packit 1fb8d4
		pool->fnObjectFree = rfx_decoder_tile_free;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/*
Packit 1fb8d4
	 * align buffers to 16 byte boundary (needed for SSE/NEON instructions)
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * y_r_buffer, cb_g_buffer, cr_b_buffer: 64 * 64 * sizeof(INT16) = 8192 (0x2000)
Packit 1fb8d4
	 * dwt_buffer: 32 * 32 * 2 * 2 * sizeof(INT16) = 8192, maximum sub-band width is 32
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * Additionally we add 32 bytes (16 in front and 16 at the back of the buffer)
Packit 1fb8d4
	 * in order to allow optimized functions (SEE, NEON) to read from positions
Packit 1fb8d4
	 * that are actually in front/beyond the buffer. Offset calculations are
Packit 1fb8d4
	 * performed at the BufferPool_Take function calls in rfx_encode/decode.c.
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * We then multiply by 3 to use a single, partioned buffer for all 3 channels.
Packit 1fb8d4
	 */
Packit 1fb8d4
	priv->BufferPool = BufferPool_New(TRUE, (8192 + 32) * 3, 16);
Packit 1fb8d4
Packit 1fb8d4
	if (!priv->BufferPool)
Packit 1fb8d4
		goto error_BufferPool;
Packit 1fb8d4
Packit 1fb8d4
#ifdef _WIN32
Packit 1fb8d4
	{
Packit 1fb8d4
		BOOL isVistaOrLater;
Packit 1fb8d4
		OSVERSIONINFOA verinfo;
Packit 1fb8d4
		ZeroMemory(&verinfo, sizeof(OSVERSIONINFOA));
Packit 1fb8d4
		verinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
Packit 1fb8d4
		GetVersionExA(&verinfo);
Packit Service 5a9772
		isVistaOrLater =
Packit Service 5a9772
		    ((verinfo.dwMajorVersion >= 6) && (verinfo.dwMinorVersion >= 0)) ? TRUE : FALSE;
Packit 1fb8d4
		priv->UseThreads = isVistaOrLater;
Packit 1fb8d4
	}
Packit 1fb8d4
#else
Packit 1fb8d4
	priv->UseThreads = TRUE;
Packit 1fb8d4
#endif
Packit 1fb8d4
	GetNativeSystemInfo(&sysinfo);
Packit 1fb8d4
	priv->MinThreadCount = sysinfo.dwNumberOfProcessors;
Packit 1fb8d4
	priv->MaxThreadCount = 0;
Packit Service 5a9772
	status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
Packit 1fb8d4
Packit 1fb8d4
	if (status == ERROR_SUCCESS)
Packit 1fb8d4
	{
Packit 1fb8d4
		dwSize = sizeof(dwValue);
Packit 1fb8d4
Packit Service 5a9772
		if (RegQueryValueEx(hKey, _T("UseThreads"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
Packit Service 5a9772
		    ERROR_SUCCESS)
Packit 1fb8d4
			priv->UseThreads = dwValue ? 1 : 0;
Packit 1fb8d4
Packit Service 5a9772
		if (RegQueryValueEx(hKey, _T("MinThreadCount"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
Packit Service 5a9772
		    ERROR_SUCCESS)
Packit 1fb8d4
			priv->MinThreadCount = dwValue;
Packit 1fb8d4
Packit Service 5a9772
		if (RegQueryValueEx(hKey, _T("MaxThreadCount"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
Packit Service 5a9772
		    ERROR_SUCCESS)
Packit 1fb8d4
			priv->MaxThreadCount = dwValue;
Packit 1fb8d4
Packit 1fb8d4
		RegCloseKey(hKey);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (priv->UseThreads)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Call primitives_get here in order to avoid race conditions when using primitives_get */
Packit 1fb8d4
		/* from multiple threads. This call will initialize all function pointers correctly     */
Packit 1fb8d4
		/* before any decoding threads are started */
Packit 1fb8d4
		primitives_get();
Packit 1fb8d4
		priv->ThreadPool = CreateThreadpool(NULL);
Packit 1fb8d4
Packit 1fb8d4
		if (!priv->ThreadPool)
Packit 1fb8d4
			goto error_threadPool;
Packit 1fb8d4
Packit 1fb8d4
		InitializeThreadpoolEnvironment(&priv->ThreadPoolEnv);
Packit 1fb8d4
		SetThreadpoolCallbackPool(&priv->ThreadPoolEnv, priv->ThreadPool);
Packit 1fb8d4
Packit 1fb8d4
		if (priv->MinThreadCount)
Packit 1fb8d4
			if (!SetThreadpoolThreadMinimum(priv->ThreadPool, priv->MinThreadCount))
Packit 1fb8d4
				goto error_threadPool_minimum;
Packit 1fb8d4
Packit 1fb8d4
		if (priv->MaxThreadCount)
Packit 1fb8d4
			SetThreadpoolThreadMaximum(priv->ThreadPool, priv->MaxThreadCount);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* initialize the default pixel format */
Packit 1fb8d4
	rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32);
Packit 1fb8d4
	/* create profilers for default decoding routines */
Packit 1fb8d4
	rfx_profiler_create(context);
Packit 1fb8d4
	/* set up default routines */
Packit 1fb8d4
	context->quantization_decode = rfx_quantization_decode;
Packit 1fb8d4
	context->quantization_encode = rfx_quantization_encode;
Packit 1fb8d4
	context->dwt_2d_decode = rfx_dwt_2d_decode;
Packit 1fb8d4
	context->dwt_2d_encode = rfx_dwt_2d_encode;
Packit 1fb8d4
	context->rlgr_decode = rfx_rlgr_decode;
Packit 1fb8d4
	context->rlgr_encode = rfx_rlgr_encode;
Packit 1fb8d4
	RFX_INIT_SIMD(context);
Packit 1fb8d4
	context->state = RFX_STATE_SEND_HEADERS;
Packit Service 5a9772
	context->expectedDataBlockType = WBT_FRAME_BEGIN;
Packit 1fb8d4
	return context;
Packit 1fb8d4
error_threadPool_minimum:
Packit 1fb8d4
	CloseThreadpool(priv->ThreadPool);
Packit 1fb8d4
error_threadPool:
Packit 1fb8d4
	BufferPool_Free(priv->BufferPool);
Packit 1fb8d4
error_BufferPool:
Packit 1fb8d4
	ObjectPool_Free(priv->TilePool);
Packit 1fb8d4
error_tilePool:
Packit 1fb8d4
	free(priv);
Packit 1fb8d4
error_priv:
Packit 1fb8d4
	free(context);
Packit 1fb8d4
	return NULL;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rfx_context_free(RFX_CONTEXT* context)
Packit 1fb8d4
{
Packit 1fb8d4
	RFX_CONTEXT_PRIV* priv;
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	assert(NULL != context);
Packit 1fb8d4
	assert(NULL != context->priv);
Packit 1fb8d4
	assert(NULL != context->priv->TilePool);
Packit 1fb8d4
	assert(NULL != context->priv->BufferPool);
Packit 1fb8d4
	priv = context->priv;
Packit Service 5a9772
	rfx_message_free(context, &context->currentMessage);
Packit 1fb8d4
	free(context->quants);
Packit 1fb8d4
	ObjectPool_Free(priv->TilePool);
Packit 1fb8d4
	rfx_profiler_print(context);
Packit 1fb8d4
	rfx_profiler_free(context);
Packit 1fb8d4
Packit 1fb8d4
	if (priv->UseThreads)
Packit 1fb8d4
	{
Packit 1fb8d4
		CloseThreadpool(context->priv->ThreadPool);
Packit 1fb8d4
		DestroyThreadpoolEnvironment(&context->priv->ThreadPoolEnv);
Packit 1fb8d4
		free(priv->workObjects);
Packit 1fb8d4
		free(priv->tileWorkParams);
Packit 1fb8d4
#ifdef WITH_PROFILER
Packit 1fb8d4
		WLog_VRB(TAG,
Packit 1fb8d4
		         "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!");
Packit 1fb8d4
#endif
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	BufferPool_Free(context->priv->BufferPool);
Packit 1fb8d4
	free(context->priv);
Packit 1fb8d4
	free(context);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* message, UINT32 index)
Packit 1fb8d4
{
Packit 1fb8d4
	return message->tiles[index];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* message, UINT32 index)
Packit 1fb8d4
{
Packit 1fb8d4
	return &message->rects[index];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rfx_context_set_pixel_format(RFX_CONTEXT* context, UINT32 pixel_format)
Packit 1fb8d4
{
Packit 1fb8d4
	context->pixel_format = pixel_format;
Packit 1fb8d4
	context->bits_per_pixel = GetBitsPerPixel(pixel_format);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rfx_context_reset(RFX_CONTEXT* context, UINT32 width, UINT32 height)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	context->width = width;
Packit 1fb8d4
	context->height = height;
Packit 1fb8d4
	context->state = RFX_STATE_SEND_HEADERS;
Packit Service 5a9772
	context->expectedDataBlockType = WBT_FRAME_BEGIN;
Packit 1fb8d4
	context->frameIdx = 0;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rfx_process_message_sync(RFX_CONTEXT* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 magic;
Packit 1fb8d4
	context->decodedHeaderBlocks &= ~_RFX_DECODED_SYNC;
Packit 1fb8d4
Packit 1fb8d4
	/* RFX_SYNC */
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 6)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "RfxSync packet too small");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, magic); /* magic (4 bytes), 0xCACCACCA */
Packit 1fb8d4
	if (magic != WF_MAGIC)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "invalid magic number 0x%08" PRIX32 "", magic);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT16(s, context->version); /* version (2 bytes), WF_VERSION_1_0 (0x0100) */
Packit 1fb8d4
	if (context->version != WF_VERSION_1_0)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "invalid version number 0x%08" PRIX32 "", context->version);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	WLog_Print(context->priv->log, WLOG_DEBUG, "version 0x%08" PRIX32 "", context->version);
Packit 1fb8d4
	context->decodedHeaderBlocks |= _RFX_DECODED_SYNC;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rfx_process_message_codec_versions(RFX_CONTEXT* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE numCodecs;
Packit 1fb8d4
	context->decodedHeaderBlocks &= ~_RFX_DECODED_VERSIONS;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "%s: packet too small for reading codec versions", __FUNCTION__);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT8(s, numCodecs);         /* numCodecs (1 byte), must be set to 0x01 */
Packit Service 5a9772
	Stream_Read_UINT8(s, context->codec_id); /* codecId (1 byte), must be set to 0x01 */
Packit Service 5a9772
	Stream_Read_UINT16(
Packit Service 5a9772
	    s, context->codec_version); /* version (2 bytes), must be set to WF_VERSION_1_0 (0x0100)  */
Packit 1fb8d4
Packit 1fb8d4
	if (numCodecs != 1)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: numCodes is 0x%02" PRIX8 " (must be 0x01)", __FUNCTION__, numCodecs);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (context->codec_id != 0x01)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: invalid codec id (0x%02" PRIX32 ")", __FUNCTION__, context->codec_id);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (context->codec_version != WF_VERSION_1_0)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: invalid codec version (0x%08" PRIX32 ")", __FUNCTION__,
Packit 1fb8d4
		         context->codec_version);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	WLog_Print(context->priv->log, WLOG_DEBUG, "id %" PRIu32 " version 0x%" PRIX32 ".",
Packit 1fb8d4
	           context->codec_id, context->codec_version);
Packit 1fb8d4
	context->decodedHeaderBlocks |= _RFX_DECODED_VERSIONS;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rfx_process_message_channels(RFX_CONTEXT* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE channelId;
Packit 1fb8d4
	BYTE numChannels;
Packit 1fb8d4
	context->decodedHeaderBlocks &= ~_RFX_DECODED_CHANNELS;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "RfxMessageChannels packet too small");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT8(s, numChannels); /* numChannels (1 byte), must bet set to 0x01 */
Packit 1fb8d4
Packit 1fb8d4
	/* In RDVH sessions, numChannels will represent the number of virtual monitors
Packit 1fb8d4
	 * configured and does not always be set to 0x01 as [MS-RDPRFX] said.
Packit 1fb8d4
	 */
Packit 1fb8d4
	if (numChannels < 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "no channels announced");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < (size_t)(numChannels * 5))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "RfxMessageChannels packet too small for numChannels=%" PRIu8 "",
Packit 1fb8d4
		         numChannels);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* RFX_CHANNELT */
Packit 1fb8d4
	Stream_Read_UINT8(s, channelId); /* channelId (1 byte), must be set to 0x00 */
Packit 1fb8d4
Packit 1fb8d4
	if (channelId != 0x00)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "channelId:0x%02" PRIX8 ", expected:0x00", channelId);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT16(s, context->width);  /* width (2 bytes) */
Packit 1fb8d4
	Stream_Read_UINT16(s, context->height); /* height (2 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (!context->width || !context->height)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: invalid channel with/height: %" PRIu16 "x%" PRIu16 "", __FUNCTION__,
Packit 1fb8d4
		         context->width, context->height);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Now, only the first monitor can be used, therefore the other channels will be ignored. */
Packit 1fb8d4
	Stream_Seek(s, 5 * (numChannels - 1));
Packit Service 5a9772
	WLog_Print(context->priv->log, WLOG_DEBUG,
Packit Service 5a9772
	           "numChannels %" PRIu8 " id %" PRIu8 ", %" PRIu16 "x%" PRIu16 ".", numChannels,
Packit Service 5a9772
	           channelId, context->width, context->height);
Packit 1fb8d4
	context->decodedHeaderBlocks |= _RFX_DECODED_CHANNELS;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rfx_process_message_context(RFX_CONTEXT* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE ctxId;
Packit 1fb8d4
	UINT16 tileSize;
Packit 1fb8d4
	UINT16 properties;
Packit 1fb8d4
	context->decodedHeaderBlocks &= ~_RFX_DECODED_CONTEXT;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 5)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "RfxMessageContext packet too small");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT8(s, ctxId);     /* ctxId (1 byte), must be set to 0x00 */
Packit Service 5a9772
	Stream_Read_UINT16(s, tileSize); /* tileSize (2 bytes), must be set to CT_TILE_64x64 (0x0040) */
Packit 1fb8d4
	Stream_Read_UINT16(s, properties); /* properties (2 bytes) */
Packit 1fb8d4
	WLog_Print(context->priv->log, WLOG_DEBUG,
Packit Service 5a9772
	           "ctxId %" PRIu8 " tileSize %" PRIu16 " properties 0x%04" PRIX16 ".", ctxId, tileSize,
Packit Service 5a9772
	           properties);
Packit 1fb8d4
	context->properties = properties;
Packit 1fb8d4
	context->flags = (properties & 0x0007);
Packit 1fb8d4
Packit 1fb8d4
	if (context->flags == CODEC_MODE)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in image mode.");
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in video mode.");
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	switch ((properties & 0x1E00) >> 9)
Packit 1fb8d4
	{
Packit 1fb8d4
		case CLW_ENTROPY_RLGR1:
Packit 1fb8d4
			context->mode = RLGR1;
Packit 1fb8d4
			WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR1.");
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CLW_ENTROPY_RLGR3:
Packit 1fb8d4
			context->mode = RLGR3;
Packit 1fb8d4
			WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR3.");
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			WLog_ERR(TAG, "unknown RLGR algorithm.");
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	context->decodedHeaderBlocks |= _RFX_DECODED_CONTEXT;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL rfx_process_message_frame_begin(RFX_CONTEXT* context, RFX_MESSAGE* message, wStream* s,
Packit Service 5a9772
                                            UINT16* pExpectedBlockType)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 frameIdx;
Packit 1fb8d4
	UINT16 numRegions;
Packit 1fb8d4
Packit 1fb8d4
	if (*pExpectedBlockType != WBT_FRAME_BEGIN)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: message unexpected wants WBT_FRAME_BEGIN", __FUNCTION__);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	*pExpectedBlockType = WBT_REGION;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 6)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "RfxMessageFrameBegin packet too small");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT32(
Packit Service 5a9772
	    s, frameIdx); /* frameIdx (4 bytes), if codec is in video mode, must be ignored */
Packit 1fb8d4
	Stream_Read_UINT16(s, numRegions); /* numRegions (2 bytes) */
Packit 1fb8d4
	WLog_Print(context->priv->log, WLOG_DEBUG,
Packit Service 5a9772
	           "RFX_FRAME_BEGIN: frameIdx: %" PRIu32 " numRegions: %" PRIu16 "", frameIdx,
Packit Service 5a9772
	           numRegions);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL rfx_process_message_frame_end(RFX_CONTEXT* context, RFX_MESSAGE* message, wStream* s,
Packit Service 5a9772
                                          UINT16* pExpectedBlockType)
Packit 1fb8d4
{
Packit 1fb8d4
	if (*pExpectedBlockType != WBT_FRAME_END)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: message unexpected, wants WBT_FRAME_END", __FUNCTION__);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	*pExpectedBlockType = WBT_FRAME_BEGIN;
Packit 1fb8d4
	WLog_Print(context->priv->log, WLOG_DEBUG, "RFX_FRAME_END");
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL rfx_process_message_region(RFX_CONTEXT* context, RFX_MESSAGE* message, wStream* s,
Packit Service 5a9772
                                       UINT16* pExpectedBlockType)
Packit 1fb8d4
{
Packit 1fb8d4
	int i;
Packit 1fb8d4
	UINT16 regionType;
Packit 1fb8d4
	UINT16 numTileSets;
Packit Service 5a9772
	RFX_RECT* tmpRects;
Packit 1fb8d4
Packit 1fb8d4
	if (*pExpectedBlockType != WBT_REGION)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: message unexpected wants WBT_REGION", __FUNCTION__);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	*pExpectedBlockType = WBT_EXTENSION;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 3)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "%s: packet too small (regionFlags/numRects)", __FUNCTION__);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Seek_UINT8(s);                     /* regionFlags (1 byte) */
Packit 1fb8d4
	Stream_Read_UINT16(s, message->numRects); /* numRects (2 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (message->numRects < 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		/*
Packit 1fb8d4
		   If numRects is zero the decoder must generate a rectangle with
Packit 1fb8d4
		   coordinates (0, 0, width, height).
Packit 1fb8d4
		   See [MS-RDPRFX] (revision >= 17.0) 2.2.2.3.3 TS_RFX_REGION
Packit 1fb8d4
		   https://msdn.microsoft.com/en-us/library/ff635233.aspx
Packit 1fb8d4
		*/
Packit Service 5a9772
		tmpRects = realloc(message->rects, sizeof(RFX_RECT));
Packit Service 5a9772
		if (!tmpRects)
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		message->numRects = 1;
Packit Service 5a9772
		message->rects = tmpRects;
Packit 1fb8d4
		message->rects->x = 0;
Packit 1fb8d4
		message->rects->y = 0;
Packit 1fb8d4
		message->rects->width = context->width;
Packit 1fb8d4
		message->rects->height = context->height;
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < (size_t)(8 * message->numRects))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: packet too small for num_rects=%" PRIu16 "", __FUNCTION__,
Packit 1fb8d4
		         message->numRects);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	tmpRects = realloc(message->rects, message->numRects * sizeof(RFX_RECT));
Packit Service 5a9772
	if (!tmpRects)
Packit 1fb8d4
		return FALSE;
Packit Service 5a9772
	message->rects = tmpRects;
Packit 1fb8d4
Packit 1fb8d4
	/* rects */
Packit 1fb8d4
	for (i = 0; i < message->numRects; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		RFX_RECT* rect = rfx_message_get_rect(message, i);
Packit 1fb8d4
		/* RFX_RECT */
Packit Service 5a9772
		Stream_Read_UINT16(s, rect->x);      /* x (2 bytes) */
Packit Service 5a9772
		Stream_Read_UINT16(s, rect->y);      /* y (2 bytes) */
Packit Service 5a9772
		Stream_Read_UINT16(s, rect->width);  /* width (2 bytes) */
Packit 1fb8d4
		Stream_Read_UINT16(s, rect->height); /* height (2 bytes) */
Packit 1fb8d4
		WLog_Print(context->priv->log, WLOG_DEBUG,
Packit Service 5a9772
		           "rect %d (x,y=%" PRIu16 ",%" PRIu16 " w,h=%" PRIu16 " %" PRIu16 ").", i, rect->x,
Packit Service 5a9772
		           rect->y, rect->width, rect->height);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "%s: packet too small (regionType/numTileSets)", __FUNCTION__);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT16(s, regionType);  /*regionType (2 bytes): MUST be set to CBT_REGION (0xCAC1)*/
Packit Service 5a9772
	Stream_Read_UINT16(s, numTileSets); /*numTilesets (2 bytes): MUST be set to 0x0001.*/
Packit 1fb8d4
Packit 1fb8d4
	if (regionType != CBT_REGION)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: invalid region type 0x%04" PRIX16 "", __FUNCTION__, regionType);
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (numTileSets != 0x0001)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: invalid number of tilesets (%" PRIu16 ")", __FUNCTION__, numTileSets);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
struct _RFX_TILE_PROCESS_WORK_PARAM
Packit 1fb8d4
{
Packit 1fb8d4
	RFX_TILE* tile;
Packit 1fb8d4
	RFX_CONTEXT* context;
Packit 1fb8d4
};
Packit 1fb8d4
typedef struct _RFX_TILE_PROCESS_WORK_PARAM RFX_TILE_PROCESS_WORK_PARAM;
Packit 1fb8d4
Packit Service 5a9772
static void CALLBACK rfx_process_message_tile_work_callback(PTP_CALLBACK_INSTANCE instance,
Packit Service 5a9772
                                                            void* context, PTP_WORK work)
Packit 1fb8d4
{
Packit Service 5a9772
	RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context;
Packit 1fb8d4
	rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL rfx_process_message_tileset(RFX_CONTEXT* context, RFX_MESSAGE* message, wStream* s,
Packit Service 5a9772
                                        UINT16* pExpectedBlockType)
Packit 1fb8d4
{
Packit 1fb8d4
	BOOL rc;
Packit 1fb8d4
	int i, close_cnt;
Packit 1fb8d4
	BYTE quant;
Packit 1fb8d4
	RFX_TILE* tile;
Packit Service 5a9772
	RFX_TILE** tmpTiles;
Packit 1fb8d4
	UINT32* quants;
Packit Service 5a9772
	UINT16 subtype, numTiles;
Packit 1fb8d4
	UINT32 blockLen;
Packit 1fb8d4
	UINT32 blockType;
Packit 1fb8d4
	UINT32 tilesDataSize;
Packit 1fb8d4
	PTP_WORK* work_objects = NULL;
Packit 1fb8d4
	RFX_TILE_PROCESS_WORK_PARAM* params = NULL;
Packit 1fb8d4
	void* pmem;
Packit 1fb8d4
Packit Service 5a9772
	if (*pExpectedBlockType != WBT_EXTENSION)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: message unexpected wants a tileset", __FUNCTION__);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	*pExpectedBlockType = WBT_FRAME_END;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 14)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "RfxMessageTileSet packet too small");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT16(s, subtype); /* subtype (2 bytes) must be set to CBT_TILESET (0xCAC2) */
Packit 1fb8d4
	if (subtype != CBT_TILESET)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "invalid subtype, expected CBT_TILESET.");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Seek_UINT16(s);                   /* idx (2 bytes), must be set to 0x0000 */
Packit Service 5a9772
	Stream_Seek_UINT16(s);                   /* properties (2 bytes) */
Packit 1fb8d4
	Stream_Read_UINT8(s, context->numQuant); /* numQuant (1 byte) */
Packit Service 5a9772
	Stream_Seek_UINT8(s);                    /* tileSize (1 byte), must be set to 0x40 */
Packit 1fb8d4
Packit 1fb8d4
	if (context->numQuant < 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "no quantization value.");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT16(s, numTiles); /* numTiles (2 bytes) */
Packit Service 5a9772
	if (numTiles < 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Windows Server 2012 (not R2) can send empty tile sets */
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, tilesDataSize); /* tilesDataSize (4 bytes) */
Packit 1fb8d4
Packit Service 5a9772
	if (!(pmem = realloc((void*)context->quants, context->numQuant * 10 * sizeof(UINT32))))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	quants = context->quants = (UINT32*)pmem;
Packit 1fb8d4
Packit 1fb8d4
	/* quantVals */
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < (size_t)(context->numQuant * 5))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "RfxMessageTileSet packet too small for num_quants=%" PRIu8 "",
Packit 1fb8d4
		         context->numQuant);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < context->numQuant; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* RFX_CODEC_QUANT */
Packit 1fb8d4
		Stream_Read_UINT8(s, quant);
Packit 1fb8d4
		*quants++ = (quant & 0x0F);
Packit 1fb8d4
		*quants++ = (quant >> 4);
Packit 1fb8d4
		Stream_Read_UINT8(s, quant);
Packit 1fb8d4
		*quants++ = (quant & 0x0F);
Packit 1fb8d4
		*quants++ = (quant >> 4);
Packit 1fb8d4
		Stream_Read_UINT8(s, quant);
Packit 1fb8d4
		*quants++ = (quant & 0x0F);
Packit 1fb8d4
		*quants++ = (quant >> 4);
Packit 1fb8d4
		Stream_Read_UINT8(s, quant);
Packit 1fb8d4
		*quants++ = (quant & 0x0F);
Packit 1fb8d4
		*quants++ = (quant >> 4);
Packit 1fb8d4
		Stream_Read_UINT8(s, quant);
Packit 1fb8d4
		*quants++ = (quant & 0x0F);
Packit 1fb8d4
		*quants++ = (quant >> 4);
Packit 1fb8d4
		WLog_Print(context->priv->log, WLOG_DEBUG,
Packit Service 5a9772
		           "quant %d (%" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32
Packit Service 5a9772
		           " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 ").",
Packit 1fb8d4
		           i, context->quants[i * 10], context->quants[i * 10 + 1],
Packit 1fb8d4
		           context->quants[i * 10 + 2], context->quants[i * 10 + 3],
Packit 1fb8d4
		           context->quants[i * 10 + 4], context->quants[i * 10 + 5],
Packit 1fb8d4
		           context->quants[i * 10 + 6], context->quants[i * 10 + 7],
Packit 1fb8d4
		           context->quants[i * 10 + 8], context->quants[i * 10 + 9]);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	for (i = 0; i < message->numTiles; i++)
Packit 1fb8d4
	{
Packit Service 5a9772
		ObjectPool_Return(context->priv->TilePool, message->tiles[i]);
Packit Service 5a9772
		message->tiles[i] = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	tmpTiles = (RFX_TILE**)realloc(message->tiles, numTiles * sizeof(RFX_TILE*));
Packit Service 5a9772
	if (!tmpTiles)
Packit Service 5a9772
		return FALSE;
Packit Service 5a9772
Packit Service 5a9772
	message->tiles = tmpTiles;
Packit Service 5a9772
	message->numTiles = numTiles;
Packit Service 5a9772
Packit 1fb8d4
	if (context->priv->UseThreads)
Packit 1fb8d4
	{
Packit Service 5a9772
		work_objects = (PTP_WORK*)calloc(message->numTiles, sizeof(PTP_WORK));
Packit Service 5a9772
		params = (RFX_TILE_PROCESS_WORK_PARAM*)calloc(message->numTiles,
Packit Service 5a9772
		                                              sizeof(RFX_TILE_PROCESS_WORK_PARAM));
Packit 1fb8d4
Packit 1fb8d4
		if (!work_objects)
Packit 1fb8d4
		{
Packit 1fb8d4
			free(params);
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (!params)
Packit 1fb8d4
		{
Packit 1fb8d4
			free(work_objects);
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* tiles */
Packit 1fb8d4
	close_cnt = 0;
Packit 1fb8d4
	rc = TRUE;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < message->numTiles; i++)
Packit 1fb8d4
	{
Packit Service 5a9772
		wStream sub;
Packit Service 5a9772
		if (!(tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool)))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "RfxMessageTileSet failed to get tile from object pool");
Packit 1fb8d4
			rc = FALSE;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		message->tiles[i] = tile;
Packit 1fb8d4
Packit 1fb8d4
		/* RFX_TILE */
Packit 1fb8d4
		if (Stream_GetRemainingLength(s) < 6)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "RfxMessageTileSet packet too small to read tile %d/%" PRIu16 "", i,
Packit 1fb8d4
			         message->numTiles);
Packit 1fb8d4
			rc = FALSE;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		Stream_StaticInit(&sub, Stream_Pointer(s), Stream_GetRemainingLength(s));
Packit Service 5a9772
		Stream_Read_UINT16(&sub,
Packit 1fb8d4
		                   blockType); /* blockType (2 bytes), must be set to CBT_TILE (0xCAC3) */
Packit Service 5a9772
		Stream_Read_UINT32(&sub, blockLen); /* blockLen (4 bytes) */
Packit 1fb8d4
Packit Service 5a9772
		if (!Stream_SafeSeek(s, blockLen))
Packit Service 5a9772
		{
Packit Service 5a9772
			rc = FALSE;
Packit Service 5a9772
			break;
Packit Service 5a9772
		}
Packit Service 5a9772
		if ((blockLen < 6 + 13) || (Stream_GetRemainingLength(&sub) < blockLen - 6))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG,
Packit Service 5a9772
			         "RfxMessageTileSet not enough bytes to read tile %d/%" PRIu16
Packit Service 5a9772
			         " with blocklen=%" PRIu32 "",
Packit 1fb8d4
			         i, message->numTiles, blockLen);
Packit 1fb8d4
			rc = FALSE;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (blockType != CBT_TILE)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "unknown block type 0x%" PRIX32 ", expected CBT_TILE (0xCAC3).",
Packit 1fb8d4
			         blockType);
Packit 1fb8d4
			rc = FALSE;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		Stream_Read_UINT8(&sub, tile->quantIdxY);  /* quantIdxY (1 byte) */
Packit Service 5a9772
		Stream_Read_UINT8(&sub, tile->quantIdxCb); /* quantIdxCb (1 byte) */
Packit Service 5a9772
		Stream_Read_UINT8(&sub, tile->quantIdxCr); /* quantIdxCr (1 byte) */
Packit Service 5a9772
		Stream_Read_UINT16(&sub, tile->xIdx);      /* xIdx (2 bytes) */
Packit Service 5a9772
		Stream_Read_UINT16(&sub, tile->yIdx);      /* yIdx (2 bytes) */
Packit Service 5a9772
		Stream_Read_UINT16(&sub, tile->YLen);      /* YLen (2 bytes) */
Packit Service 5a9772
		Stream_Read_UINT16(&sub, tile->CbLen);     /* CbLen (2 bytes) */
Packit Service 5a9772
		Stream_Read_UINT16(&sub, tile->CrLen);     /* CrLen (2 bytes) */
Packit Service 5a9772
		Stream_GetPointer(&sub, tile->YData);
Packit Service 5a9772
		if (!Stream_SafeSeek(&sub, tile->YLen))
Packit Service 5a9772
		{
Packit Service 5a9772
			rc = FALSE;
Packit Service 5a9772
			break;
Packit Service 5a9772
		}
Packit Service 5a9772
		Stream_GetPointer(&sub, tile->CbData);
Packit Service 5a9772
		if (!Stream_SafeSeek(&sub, tile->CbLen))
Packit Service 5a9772
		{
Packit Service 5a9772
			rc = FALSE;
Packit Service 5a9772
			break;
Packit Service 5a9772
		}
Packit Service 5a9772
		Stream_GetPointer(&sub, tile->CrData);
Packit Service 5a9772
		if (!Stream_SafeSeek(&sub, tile->CrLen))
Packit Service 5a9772
		{
Packit Service 5a9772
			rc = FALSE;
Packit Service 5a9772
			break;
Packit Service 5a9772
		}
Packit 1fb8d4
		tile->x = tile->xIdx * 64;
Packit 1fb8d4
		tile->y = tile->yIdx * 64;
Packit 1fb8d4
Packit 1fb8d4
		if (context->priv->UseThreads)
Packit 1fb8d4
		{
Packit Service 5a9772
			if (!params)
Packit Service 5a9772
			{
Packit Service 5a9772
				rc = FALSE;
Packit Service 5a9772
				break;
Packit Service 5a9772
			}
Packit Service 5a9772
Packit 1fb8d4
			params[i].context = context;
Packit 1fb8d4
			params[i].tile = message->tiles[i];
Packit 1fb8d4
Packit Service 5a9772
			if (!(work_objects[i] =
Packit Service 5a9772
			          CreateThreadpoolWork(rfx_process_message_tile_work_callback,
Packit Service 5a9772
			                               (void*)&params[i], &context->priv->ThreadPoolEnv)))
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "CreateThreadpoolWork failed.");
Packit 1fb8d4
				rc = FALSE;
Packit 1fb8d4
				break;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			SubmitThreadpoolWork(work_objects[i]);
Packit 1fb8d4
			close_cnt = i + 1;
Packit 1fb8d4
		}
Packit 1fb8d4
		else
Packit 1fb8d4
		{
Packit 1fb8d4
			rfx_decode_rgb(context, tile, tile->data, 64 * 4);
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (context->priv->UseThreads)
Packit 1fb8d4
	{
Packit 1fb8d4
		for (i = 0; i < close_cnt; i++)
Packit 1fb8d4
		{
Packit 1fb8d4
			WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE);
Packit 1fb8d4
			CloseThreadpoolWork(work_objects[i]);
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	free(work_objects);
Packit 1fb8d4
	free(params);
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < message->numTiles; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!(tile = message->tiles[i]))
Packit 1fb8d4
			continue;
Packit 1fb8d4
Packit 1fb8d4
		tile->YLen = tile->CbLen = tile->CrLen = 0;
Packit 1fb8d4
		tile->YData = tile->CbData = tile->CrData = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
BOOL rfx_process_message(RFX_CONTEXT* context, const BYTE* data, UINT32 length, UINT32 left,
Packit Service 5a9772
                         UINT32 top, BYTE* dst, UINT32 dstFormat, UINT32 dstStride,
Packit Service 5a9772
                         UINT32 dstHeight, REGION16* invalidRegion)
Packit 1fb8d4
{
Packit 1fb8d4
	REGION16 updateRegion;
Packit 1fb8d4
	UINT32 blockLen;
Packit 1fb8d4
	UINT32 blockType;
Packit Service 5a9772
	wStream inStream, *s = &inStream;
Packit 1fb8d4
	BOOL ok = TRUE;
Packit Service 5a9772
	RFX_MESSAGE* message;
Packit 1fb8d4
Packit 1fb8d4
	if (!context || !data || !length)
Packit Service 5a9772
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	message = &context->currentMessage;
Packit 1fb8d4
Packit Service 5a9772
	Stream_StaticInit(s, (BYTE*)data, length);
Packit 1fb8d4
Packit 1fb8d4
	message->freeRects = TRUE;
Packit 1fb8d4
Packit 1fb8d4
	while (ok && Stream_GetRemainingLength(s) > 6)
Packit 1fb8d4
	{
Packit Service 5a9772
		wStream subStream;
Packit Service 5a9772
		size_t extraBlockLen = 0;
Packit Service 5a9772
Packit 1fb8d4
		/* RFX_BLOCKT */
Packit 1fb8d4
		Stream_Read_UINT16(s, blockType); /* blockType (2 bytes) */
Packit Service 5a9772
		Stream_Read_UINT32(s, blockLen);  /* blockLen (4 bytes) */
Packit Service 5a9772
		WLog_Print(context->priv->log, WLOG_DEBUG, "blockType 0x%" PRIX32 " blockLen %" PRIu32 "",
Packit 1fb8d4
		           blockType, blockLen);
Packit 1fb8d4
Packit Service 5a9772
		if (blockLen < 6)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "blockLen too small(%" PRIu32 ")", blockLen);
Packit Service 5a9772
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (Stream_GetRemainingLength(s) < blockLen - 6)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "%s: packet too small for blocklen=%" PRIu32 "", __FUNCTION__, blockLen);
Packit Service 5a9772
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != _RFX_DECODED_HEADERS)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "%s: incomplete header blocks processing", __FUNCTION__);
Packit Service 5a9772
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION)
Packit 1fb8d4
		{
Packit 1fb8d4
			/* RFX_CODEC_CHANNELT */
Packit 1fb8d4
			UINT8 codecId;
Packit 1fb8d4
			UINT8 channelId;
Packit 1fb8d4
Packit 1fb8d4
			if (Stream_GetRemainingLength(s) < 2)
Packit Service 5a9772
				return FALSE;
Packit 1fb8d4
Packit Service 5a9772
			extraBlockLen = 2;
Packit Service 5a9772
			Stream_Read_UINT8(s, codecId);   /* codecId (1 byte) must be set to 0x01 */
Packit Service 5a9772
			Stream_Read_UINT8(s, channelId); /* channelId (1 byte) 0xFF or 0x00, see below */
Packit 1fb8d4
Packit 1fb8d4
			if (codecId != 0x01)
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "%s: invalid codecId 0x%02" PRIX8 "", __FUNCTION__, codecId);
Packit Service 5a9772
				return FALSE;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			if (blockType == WBT_CONTEXT)
Packit 1fb8d4
			{
Packit 1fb8d4
				/* If the blockType is set to WBT_CONTEXT, then channelId MUST be set to 0xFF.*/
Packit 1fb8d4
				if (channelId != 0xFF)
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG,
Packit Service 5a9772
					         "%s: invalid channelId 0x%02" PRIX8 " for blockType 0x%08" PRIX32 "",
Packit Service 5a9772
					         __FUNCTION__, channelId, blockType);
Packit Service 5a9772
					return FALSE;
Packit 1fb8d4
				}
Packit 1fb8d4
			}
Packit 1fb8d4
			else
Packit 1fb8d4
			{
Packit 1fb8d4
				/* For all other values of blockType, channelId MUST be set to 0x00. */
Packit 1fb8d4
				if (channelId != 0x00)
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG, "%s: invalid channelId 0x%02" PRIX8 " for blockType WBT_CONTEXT",
Packit 1fb8d4
					         __FUNCTION__, channelId);
Packit Service 5a9772
					return FALSE;
Packit 1fb8d4
				}
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		Stream_StaticInit(&subStream, Stream_Pointer(s), blockLen - (6 + extraBlockLen));
Packit Service 5a9772
		Stream_Seek(s, blockLen - (6 + extraBlockLen));
Packit Service 5a9772
Packit 1fb8d4
		switch (blockType)
Packit 1fb8d4
		{
Packit 1fb8d4
			/* Header messages:
Packit 1fb8d4
			 * The stream MUST start with the header messages and any of these headers can appear
Packit 1fb8d4
			 * in the stream at a later stage. The header messages can be repeated.
Packit 1fb8d4
			 */
Packit 1fb8d4
			case WBT_SYNC:
Packit Service 5a9772
				ok = rfx_process_message_sync(context, &subStream);
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case WBT_CONTEXT:
Packit Service 5a9772
				ok = rfx_process_message_context(context, &subStream);
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case WBT_CODEC_VERSIONS:
Packit Service 5a9772
				ok = rfx_process_message_codec_versions(context, &subStream);
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case WBT_CHANNELS:
Packit Service 5a9772
				ok = rfx_process_message_channels(context, &subStream);
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit Service 5a9772
				/* Data messages:
Packit Service 5a9772
				 * The data associated with each encoded frame or image is always bracketed by the
Packit Service 5a9772
				 * TS_RFX_FRAME_BEGIN (section 2.2.2.3.1) and TS_RFX_FRAME_END (section 2.2.2.3.2)
Packit Service 5a9772
				 * messages. There MUST only be one TS_RFX_REGION (section 2.2.2.3.3) message per
Packit Service 5a9772
				 * frame and one TS_RFX_TILESET (section 2.2.2.3.4) message per TS_RFX_REGION.
Packit Service 5a9772
				 */
Packit 1fb8d4
Packit 1fb8d4
			case WBT_FRAME_BEGIN:
Packit Service 5a9772
				ok = rfx_process_message_frame_begin(context, message, &subStream,
Packit Service 5a9772
				                                     &context->expectedDataBlockType);
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case WBT_REGION:
Packit Service 5a9772
				ok = rfx_process_message_region(context, message, &subStream,
Packit Service 5a9772
				                                &context->expectedDataBlockType);
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case WBT_EXTENSION:
Packit Service 5a9772
				ok = rfx_process_message_tileset(context, message, &subStream,
Packit Service 5a9772
				                                 &context->expectedDataBlockType);
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case WBT_FRAME_END:
Packit Service 5a9772
				ok = rfx_process_message_frame_end(context, message, &subStream,
Packit Service 5a9772
				                                   &context->expectedDataBlockType);
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit Service 5a9772
				WLog_ERR(TAG, "%s: unknown blockType 0x%" PRIX32 "", __FUNCTION__, blockType);
Packit Service 5a9772
				return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (ok)
Packit 1fb8d4
	{
Packit 1fb8d4
		UINT32 i, j;
Packit 1fb8d4
		UINT32 nbUpdateRects;
Packit 1fb8d4
		REGION16 clippingRects;
Packit 1fb8d4
		const RECTANGLE_16* updateRects;
Packit 1fb8d4
		const DWORD formatSize = GetBytesPerPixel(context->pixel_format);
Packit 1fb8d4
		const UINT32 dstWidth = dstStride / GetBytesPerPixel(dstFormat);
Packit 1fb8d4
		region16_init(&clippingRects);
Packit 1fb8d4
Packit 1fb8d4
		for (i = 0; i < message->numRects; i++)
Packit 1fb8d4
		{
Packit 1fb8d4
			RECTANGLE_16 clippingRect;
Packit 1fb8d4
			const RFX_RECT* rect = &(message->rects[i]);
Packit 1fb8d4
			clippingRect.left = MIN(left + rect->x, dstWidth);
Packit 1fb8d4
			clippingRect.top = MIN(top + rect->y, dstHeight);
Packit 1fb8d4
			clippingRect.right = MIN(clippingRect.left + rect->width, dstWidth);
Packit 1fb8d4
			clippingRect.bottom = MIN(clippingRect.top + rect->height, dstHeight);
Packit 1fb8d4
			region16_union_rect(&clippingRects, &clippingRects, &clippingRect);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		for (i = 0; i < message->numTiles; i++)
Packit 1fb8d4
		{
Packit 1fb8d4
			RECTANGLE_16 updateRect;
Packit 1fb8d4
			const RFX_TILE* tile = rfx_message_get_tile(message, i);
Packit 1fb8d4
			updateRect.left = left + tile->x;
Packit 1fb8d4
			updateRect.top = top + tile->y;
Packit 1fb8d4
			updateRect.right = updateRect.left + 64;
Packit 1fb8d4
			updateRect.bottom = updateRect.top + 64;
Packit 1fb8d4
			region16_init(&updateRegion);
Packit 1fb8d4
			region16_intersect_rect(&updateRegion, &clippingRects, &updateRect);
Packit 1fb8d4
			updateRects = region16_rects(&updateRegion, &nbUpdateRects);
Packit 1fb8d4
Packit 1fb8d4
			for (j = 0; j < nbUpdateRects; j++)
Packit 1fb8d4
			{
Packit 1fb8d4
				const UINT32 stride = 64 * formatSize;
Packit 1fb8d4
				const UINT32 nXDst = updateRects[j].left;
Packit 1fb8d4
				const UINT32 nYDst = updateRects[j].top;
Packit 1fb8d4
				const UINT32 nXSrc = nXDst - updateRect.left;
Packit 1fb8d4
				const UINT32 nYSrc = nYDst - updateRect.top;
Packit 1fb8d4
				const UINT32 nWidth = updateRects[j].right - updateRects[j].left;
Packit 1fb8d4
				const UINT32 nHeight = updateRects[j].bottom - updateRects[j].top;
Packit 1fb8d4
Packit Service 5a9772
				if (!freerdp_image_copy(dst, dstFormat, dstStride, nXDst, nYDst, nWidth, nHeight,
Packit Service 5a9772
				                        tile->data, context->pixel_format, stride, nXSrc, nYSrc,
Packit Service 5a9772
				                        NULL, FREERDP_FLIP_NONE))
Packit 1fb8d4
				{
Packit 1fb8d4
					region16_uninit(&updateRegion);
Packit Service 5a9772
					return FALSE;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				if (invalidRegion)
Packit 1fb8d4
					region16_union_rect(invalidRegion, invalidRegion, &updateRects[j]);
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			region16_uninit(&updateRegion);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		region16_uninit(&clippingRects);
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
UINT16 rfx_message_get_tile_count(RFX_MESSAGE* message)
Packit 1fb8d4
{
Packit 1fb8d4
	return message->numTiles;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
UINT16 rfx_message_get_rect_count(RFX_MESSAGE* message)
Packit 1fb8d4
{
Packit 1fb8d4
	return message->numRects;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rfx_message_free(RFX_CONTEXT* context, RFX_MESSAGE* message)
Packit 1fb8d4
{
Packit 1fb8d4
	int i;
Packit 1fb8d4
	RFX_TILE* tile;
Packit 1fb8d4
Packit 1fb8d4
	if (message)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((message->rects) && (message->freeRects))
Packit 1fb8d4
		{
Packit 1fb8d4
			free(message->rects);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (message->tiles)
Packit 1fb8d4
		{
Packit 1fb8d4
			for (i = 0; i < message->numTiles; i++)
Packit 1fb8d4
			{
Packit 1fb8d4
				if (!(tile = message->tiles[i]))
Packit 1fb8d4
					continue;
Packit 1fb8d4
Packit 1fb8d4
				if (tile->YCbCrData)
Packit 1fb8d4
				{
Packit 1fb8d4
					BufferPool_Return(context->priv->BufferPool, tile->YCbCrData);
Packit 1fb8d4
					tile->YCbCrData = NULL;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit Service 5a9772
				ObjectPool_Return(context->priv->TilePool, (void*)tile);
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			free(message->tiles);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (!message->freeArray)
Packit 1fb8d4
			free(message);
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_update_context_properties(RFX_CONTEXT* context)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT16 properties;
Packit 1fb8d4
	/* properties in tilesets: note that this has different format from the one in TS_RFX_CONTEXT */
Packit Service 5a9772
	properties = 1;                          /* lt */
Packit Service 5a9772
	properties |= (context->flags << 1);     /* flags */
Packit Service 5a9772
	properties |= (COL_CONV_ICT << 4);       /* cct */
Packit 1fb8d4
	properties |= (CLW_XFORM_DWT_53_A << 6); /* xft */
Packit Service 5a9772
	properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10); /* et */
Packit Service 5a9772
	properties |= (SCALAR_QUANTIZATION << 14);                                              /* qt */
Packit 1fb8d4
	context->properties = properties;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_write_message_sync(RFX_CONTEXT* context, wStream* s)
Packit 1fb8d4
{
Packit Service 5a9772
	Stream_Write_UINT16(s, WBT_SYNC);       /* BlockT.blockType (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, 12);             /* BlockT.blockLen (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, WF_MAGIC);       /* magic (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT16(s, WF_VERSION_1_0); /* version (2 bytes) */
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_write_message_codec_versions(RFX_CONTEXT* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	Stream_Write_UINT16(s, WBT_CODEC_VERSIONS); /* BlockT.blockType (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, 10);                 /* BlockT.blockLen (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 1);                   /* numCodecs (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 1);                   /* codecs.codecId (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT16(s, WF_VERSION_1_0);     /* codecs.version (2 bytes) */
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_write_message_channels(RFX_CONTEXT* context, wStream* s)
Packit 1fb8d4
{
Packit Service 5a9772
	Stream_Write_UINT16(s, WBT_CHANNELS);    /* BlockT.blockType (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, 12);              /* BlockT.blockLen (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 1);                /* numChannels (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0);                /* Channel.channelId (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT16(s, context->width);  /* Channel.width (2 bytes) */
Packit 1fb8d4
	Stream_Write_UINT16(s, context->height); /* Channel.height (2 bytes) */
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_write_message_context(RFX_CONTEXT* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT16 properties;
Packit Service 5a9772
	Stream_Write_UINT16(s, WBT_CONTEXT);   /* CodecChannelT.blockType (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, 13);            /* CodecChannelT.blockLen (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 1);              /* CodecChannelT.codecId (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0xFF);           /* CodecChannelT.channelId (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0);              /* ctxId (1 byte) */
Packit 1fb8d4
	Stream_Write_UINT16(s, CT_TILE_64x64); /* tileSize (2 bytes) */
Packit 1fb8d4
	/* properties */
Packit Service 5a9772
	properties = context->flags;             /* flags */
Packit Service 5a9772
	properties |= (COL_CONV_ICT << 3);       /* cct */
Packit 1fb8d4
	properties |= (CLW_XFORM_DWT_53_A << 5); /* xft */
Packit Service 5a9772
	properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9); /* et */
Packit Service 5a9772
	properties |= (SCALAR_QUANTIZATION << 13);                                             /* qt */
Packit 1fb8d4
	Stream_Write_UINT16(s, properties); /* properties (2 bytes) */
Packit 1fb8d4
	rfx_update_context_properties(context);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rfx_compose_message_header(RFX_CONTEXT* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	rfx_write_message_sync(context, s);
Packit 1fb8d4
	rfx_write_message_context(context, s);
Packit 1fb8d4
	rfx_write_message_codec_versions(context, s);
Packit 1fb8d4
	rfx_write_message_channels(context, s);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static int rfx_tile_length(RFX_TILE* tile)
Packit 1fb8d4
{
Packit 1fb8d4
	return 19 + tile->YLen + tile->CbLen + tile->CrLen;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rfx_write_tile(RFX_CONTEXT* context, wStream* s, RFX_TILE* tile)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 blockLen;
Packit 1fb8d4
	blockLen = rfx_tile_length(tile);
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, blockLen))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	Stream_Write_UINT16(s, CBT_TILE);           /* BlockT.blockType (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, blockLen);           /* BlockT.blockLen (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT8(s, tile->quantIdxY);     /* quantIdxY (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, tile->quantIdxCb);    /* quantIdxCb (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, tile->quantIdxCr);    /* quantIdxCr (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT16(s, tile->xIdx);         /* xIdx (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, tile->yIdx);         /* yIdx (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, tile->YLen);         /* YLen (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, tile->CbLen);        /* CbLen (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, tile->CrLen);        /* CrLen (2 bytes) */
Packit Service 5a9772
	Stream_Write(s, tile->YData, tile->YLen);   /* YData */
Packit 1fb8d4
	Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */
Packit 1fb8d4
	Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
struct _RFX_TILE_COMPOSE_WORK_PARAM
Packit 1fb8d4
{
Packit 1fb8d4
	RFX_TILE* tile;
Packit 1fb8d4
	RFX_CONTEXT* context;
Packit 1fb8d4
};
Packit 1fb8d4
Packit Service 5a9772
static void CALLBACK rfx_compose_message_tile_work_callback(PTP_CALLBACK_INSTANCE instance,
Packit Service 5a9772
                                                            void* context, PTP_WORK work)
Packit 1fb8d4
{
Packit Service 5a9772
	RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context;
Packit 1fb8d4
	rfx_encode_rgb(param->context, param->tile);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL computeRegion(const RFX_RECT* rects, int numRects, REGION16* region, int width,
Packit Service 5a9772
                          int height)
Packit 1fb8d4
{
Packit 1fb8d4
	int i;
Packit 1fb8d4
	const RFX_RECT* rect = rects;
Packit 1fb8d4
	const RECTANGLE_16 mainRect = { 0, 0, width, height };
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < numRects; i++, rect++)
Packit 1fb8d4
	{
Packit 1fb8d4
		RECTANGLE_16 rect16;
Packit 1fb8d4
		rect16.left = rect->x;
Packit 1fb8d4
		rect16.top = rect->y;
Packit 1fb8d4
		rect16.right = rect->x + rect->width;
Packit 1fb8d4
		rect16.bottom = rect->y + rect->height;
Packit 1fb8d4
Packit 1fb8d4
		if (!region16_union_rect(region, region, &rect16))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return region16_intersect_rect(region, region, &mainRect);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#define TILE_NO(v) ((v) / 64)
Packit 1fb8d4
Packit Service 5a9772
static BOOL setupWorkers(RFX_CONTEXT* context, int nbTiles)
Packit 1fb8d4
{
Packit 1fb8d4
	RFX_CONTEXT_PRIV* priv = context->priv;
Packit 1fb8d4
	void* pmem;
Packit 1fb8d4
Packit 1fb8d4
	if (!context->priv->UseThreads)
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
Packit Service 5a9772
	if (!(pmem = realloc((void*)priv->workObjects, sizeof(PTP_WORK) * nbTiles)))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	priv->workObjects = (PTP_WORK*)pmem;
Packit 1fb8d4
Packit Service 5a9772
	if (!(pmem =
Packit Service 5a9772
	          realloc((void*)priv->tileWorkParams, sizeof(RFX_TILE_COMPOSE_WORK_PARAM) * nbTiles)))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* context, const RFX_RECT* rects, int numRects,
Packit Service 5a9772
                                BYTE* data, int w, int h, int s)
Packit 1fb8d4
{
Packit Service 5a9772
	const UINT32 width = (UINT32)w;
Packit Service 5a9772
	const UINT32 height = (UINT32)h;
Packit Service 5a9772
	const UINT32 scanline = (UINT32)s;
Packit 1fb8d4
	UINT32 i, maxNbTiles, maxTilesX, maxTilesY;
Packit 1fb8d4
	UINT32 xIdx, yIdx, regionNbRects;
Packit 1fb8d4
	UINT32 gridRelX, gridRelY, ax, ay, bytesPerPixel;
Packit 1fb8d4
	RFX_TILE* tile;
Packit 1fb8d4
	RFX_RECT* rfxRect;
Packit 1fb8d4
	RFX_MESSAGE* message = NULL;
Packit 1fb8d4
	PTP_WORK* workObject = NULL;
Packit 1fb8d4
	RFX_TILE_COMPOSE_WORK_PARAM* workParam = NULL;
Packit 1fb8d4
	BOOL success = FALSE;
Packit 1fb8d4
	REGION16 rectsRegion, tilesRegion;
Packit 1fb8d4
	RECTANGLE_16 currentTileRect;
Packit 1fb8d4
	const RECTANGLE_16* regionRect;
Packit 1fb8d4
	const RECTANGLE_16* extents;
Packit 1fb8d4
	assert(data);
Packit 1fb8d4
	assert(rects);
Packit 1fb8d4
	assert(numRects > 0);
Packit Service 5a9772
	assert(w > 0);
Packit Service 5a9772
	assert(h > 0);
Packit Service 5a9772
	assert(s > 0);
Packit 1fb8d4
Packit 1fb8d4
	if (!(message = (RFX_MESSAGE*)calloc(1, sizeof(RFX_MESSAGE))))
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	region16_init(&tilesRegion);
Packit 1fb8d4
	region16_init(&rectsRegion);
Packit 1fb8d4
Packit 1fb8d4
	if (context->state == RFX_STATE_SEND_HEADERS)
Packit 1fb8d4
		rfx_update_context_properties(context);
Packit 1fb8d4
Packit 1fb8d4
	message->frameIdx = context->frameIdx++;
Packit 1fb8d4
Packit 1fb8d4
	if (!context->numQuant)
Packit 1fb8d4
	{
Packit Service 5a9772
		if (!(context->quants = (UINT32*)malloc(sizeof(rfx_default_quantization_values))))
Packit 1fb8d4
			goto skip_encoding_loop;
Packit 1fb8d4
Packit 1fb8d4
		CopyMemory(context->quants, &rfx_default_quantization_values,
Packit 1fb8d4
		           sizeof(rfx_default_quantization_values));
Packit 1fb8d4
		context->numQuant = 1;
Packit 1fb8d4
		context->quantIdxY = 0;
Packit 1fb8d4
		context->quantIdxCb = 0;
Packit 1fb8d4
		context->quantIdxCr = 0;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	message->numQuant = context->numQuant;
Packit 1fb8d4
	message->quantVals = context->quants;
Packit 1fb8d4
	bytesPerPixel = (context->bits_per_pixel / 8);
Packit 1fb8d4
Packit 1fb8d4
	if (!computeRegion(rects, numRects, &rectsRegion, width, height))
Packit 1fb8d4
		goto skip_encoding_loop;
Packit 1fb8d4
Packit 1fb8d4
	extents = region16_extents(&rectsRegion);
Packit 1fb8d4
	assert(extents->right - extents->left > 0);
Packit 1fb8d4
	assert(extents->bottom - extents->top > 0);
Packit 1fb8d4
	maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left);
Packit 1fb8d4
	maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top);
Packit 1fb8d4
	maxNbTiles = maxTilesX * maxTilesY;
Packit 1fb8d4
Packit 1fb8d4
	if (!(message->tiles = calloc(maxNbTiles, sizeof(RFX_TILE*))))
Packit 1fb8d4
		goto skip_encoding_loop;
Packit 1fb8d4
Packit 1fb8d4
	if (!setupWorkers(context, maxNbTiles))
Packit 1fb8d4
		goto skip_encoding_loop;
Packit 1fb8d4
Packit 1fb8d4
	if (context->priv->UseThreads)
Packit 1fb8d4
	{
Packit 1fb8d4
		workObject = context->priv->workObjects;
Packit 1fb8d4
		workParam = context->priv->tileWorkParams;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	regionRect = region16_rects(&rectsRegion, &regionNbRects);
Packit 1fb8d4
Packit 1fb8d4
	if (!(message->rects = calloc(regionNbRects, sizeof(RFX_RECT))))
Packit 1fb8d4
		goto skip_encoding_loop;
Packit 1fb8d4
Packit 1fb8d4
	message->numRects = regionNbRects;
Packit 1fb8d4
Packit Service 5a9772
	for (i = 0, rfxRect = message->rects; i < regionNbRects; i++, regionRect++, rfxRect++)
Packit 1fb8d4
	{
Packit Service 5a9772
		UINT32 startTileX = regionRect->left / 64;
Packit Service 5a9772
		UINT32 endTileX = (regionRect->right - 1) / 64;
Packit Service 5a9772
		UINT32 startTileY = regionRect->top / 64;
Packit Service 5a9772
		UINT32 endTileY = (regionRect->bottom - 1) / 64;
Packit 1fb8d4
		rfxRect->x = regionRect->left;
Packit 1fb8d4
		rfxRect->y = regionRect->top;
Packit 1fb8d4
		rfxRect->width = (regionRect->right - regionRect->left);
Packit 1fb8d4
		rfxRect->height = (regionRect->bottom - regionRect->top);
Packit 1fb8d4
Packit 1fb8d4
		for (yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY;
Packit 1fb8d4
		     yIdx++, gridRelY += 64)
Packit 1fb8d4
		{
Packit Service 5a9772
			UINT32 tileHeight = 64;
Packit 1fb8d4
Packit 1fb8d4
			if ((yIdx == endTileY) && (gridRelY + 64 > height))
Packit 1fb8d4
				tileHeight = height - gridRelY;
Packit 1fb8d4
Packit 1fb8d4
			currentTileRect.top = gridRelY;
Packit 1fb8d4
			currentTileRect.bottom = gridRelY + tileHeight;
Packit 1fb8d4
Packit 1fb8d4
			for (xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX;
Packit 1fb8d4
			     xIdx++, gridRelX += 64)
Packit 1fb8d4
			{
Packit 1fb8d4
				int tileWidth = 64;
Packit 1fb8d4
Packit 1fb8d4
				if ((xIdx == endTileX) && (gridRelX + 64 > width))
Packit 1fb8d4
					tileWidth = width - gridRelX;
Packit 1fb8d4
Packit 1fb8d4
				currentTileRect.left = gridRelX;
Packit 1fb8d4
				currentTileRect.right = gridRelX + tileWidth;
Packit 1fb8d4
Packit 1fb8d4
				/* checks if this tile is already treated */
Packit 1fb8d4
				if (region16_intersects_rect(&tilesRegion, &currentTileRect))
Packit 1fb8d4
					continue;
Packit 1fb8d4
Packit Service 5a9772
				if (!(tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool)))
Packit 1fb8d4
					goto skip_encoding_loop;
Packit 1fb8d4
Packit 1fb8d4
				tile->xIdx = xIdx;
Packit 1fb8d4
				tile->yIdx = yIdx;
Packit 1fb8d4
				tile->x = gridRelX;
Packit 1fb8d4
				tile->y = gridRelY;
Packit 1fb8d4
				tile->scanline = scanline;
Packit 1fb8d4
				tile->width = tileWidth;
Packit 1fb8d4
				tile->height = tileHeight;
Packit 1fb8d4
				ax = gridRelX;
Packit 1fb8d4
				ay = gridRelY;
Packit 1fb8d4
Packit 1fb8d4
				if (tile->data && tile->allocated)
Packit 1fb8d4
				{
Packit 1fb8d4
					free(tile->data);
Packit 1fb8d4
					tile->allocated = FALSE;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				tile->data = &data[(ay * scanline) + (ax * bytesPerPixel)];
Packit 1fb8d4
				tile->quantIdxY = context->quantIdxY;
Packit 1fb8d4
				tile->quantIdxCb = context->quantIdxCb;
Packit 1fb8d4
				tile->quantIdxCr = context->quantIdxCr;
Packit 1fb8d4
				tile->YLen = tile->CbLen = tile->CrLen = 0;
Packit 1fb8d4
Packit 1fb8d4
				if (!(tile->YCbCrData = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
Packit 1fb8d4
					goto skip_encoding_loop;
Packit 1fb8d4
Packit Service 5a9772
				tile->YData = (BYTE*)&(tile->YCbCrData[((8192 + 32) * 0) + 16]);
Packit Service 5a9772
				tile->CbData = (BYTE*)&(tile->YCbCrData[((8192 + 32) * 1) + 16]);
Packit Service 5a9772
				tile->CrData = (BYTE*)&(tile->YCbCrData[((8192 + 32) * 2) + 16]);
Packit 1fb8d4
				message->tiles[message->numTiles] = tile;
Packit 1fb8d4
				message->numTiles++;
Packit 1fb8d4
Packit 1fb8d4
				if (context->priv->UseThreads)
Packit 1fb8d4
				{
Packit 1fb8d4
					workParam->context = context;
Packit 1fb8d4
					workParam->tile = tile;
Packit 1fb8d4
Packit Service 5a9772
					if (!(*workObject = CreateThreadpoolWork(rfx_compose_message_tile_work_callback,
Packit Service 5a9772
					                                         (void*)workParam,
Packit Service 5a9772
					                                         &context->priv->ThreadPoolEnv)))
Packit 1fb8d4
					{
Packit 1fb8d4
						goto skip_encoding_loop;
Packit 1fb8d4
					}
Packit 1fb8d4
Packit 1fb8d4
					SubmitThreadpoolWork(*workObject);
Packit 1fb8d4
					workObject++;
Packit 1fb8d4
					workParam++;
Packit 1fb8d4
				}
Packit 1fb8d4
				else
Packit 1fb8d4
				{
Packit 1fb8d4
					rfx_encode_rgb(context, tile);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				if (!region16_union_rect(&tilesRegion, &tilesRegion, &currentTileRect))
Packit 1fb8d4
					goto skip_encoding_loop;
Packit 1fb8d4
			} /* xIdx */
Packit Service 5a9772
		}     /* yIdx */
Packit Service 5a9772
	}         /* rects */
Packit 1fb8d4
Packit 1fb8d4
	success = TRUE;
Packit 1fb8d4
skip_encoding_loop:
Packit 1fb8d4
Packit 1fb8d4
	if (success && message->numTiles != maxNbTiles)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (message->numTiles > 0)
Packit 1fb8d4
		{
Packit Service 5a9772
			void* pmem = realloc((void*)message->tiles, sizeof(RFX_TILE*) * message->numTiles);
Packit 1fb8d4
Packit 1fb8d4
			if (pmem)
Packit Service 5a9772
				message->tiles = (RFX_TILE**)pmem;
Packit 1fb8d4
			else
Packit 1fb8d4
				success = FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
		else
Packit 1fb8d4
			success = FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* when using threads ensure all computations are done */
Packit 1fb8d4
	if (success)
Packit 1fb8d4
	{
Packit 1fb8d4
		message->tilesDataSize = 0;
Packit 1fb8d4
		workObject = context->priv->workObjects;
Packit 1fb8d4
Packit 1fb8d4
		for (i = 0; i < message->numTiles; i++)
Packit 1fb8d4
		{
Packit 1fb8d4
			tile = message->tiles[i];
Packit 1fb8d4
Packit 1fb8d4
			if (context->priv->UseThreads)
Packit 1fb8d4
			{
Packit 1fb8d4
				if (*workObject)
Packit 1fb8d4
				{
Packit 1fb8d4
					WaitForThreadpoolWorkCallbacks(*workObject, FALSE);
Packit 1fb8d4
					CloseThreadpoolWork(*workObject);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				workObject++;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			message->tilesDataSize += rfx_tile_length(tile);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		region16_uninit(&tilesRegion);
Packit 1fb8d4
		region16_uninit(&rectsRegion);
Packit 1fb8d4
Packit 1fb8d4
		return message;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	WLog_ERR(TAG, "%s: failed", __FUNCTION__);
Packit 1fb8d4
	message->freeRects = TRUE;
Packit 1fb8d4
	rfx_message_free(context, message);
Packit 1fb8d4
	return NULL;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* context, RFX_MESSAGE* message, int* numMessages,
Packit Service 5a9772
                                      int maxDataSize)
Packit 1fb8d4
{
Packit 1fb8d4
	int i, j;
Packit 1fb8d4
	UINT32 tileDataSize;
Packit 1fb8d4
	RFX_MESSAGE* messages;
Packit 1fb8d4
	maxDataSize -= 1024; /* reserve enough space for headers */
Packit 1fb8d4
	*numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4;
Packit 1fb8d4
Packit Service 5a9772
	if (!(messages = (RFX_MESSAGE*)calloc((*numMessages), sizeof(RFX_MESSAGE))))
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	j = 0;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < message->numTiles; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		tileDataSize = rfx_tile_length(message->tiles[i]);
Packit 1fb8d4
Packit Service 5a9772
		if ((messages[j].tilesDataSize + tileDataSize) > ((UINT32)maxDataSize))
Packit 1fb8d4
			j++;
Packit 1fb8d4
Packit 1fb8d4
		if (!messages[j].numTiles)
Packit 1fb8d4
		{
Packit 1fb8d4
			messages[j].frameIdx = message->frameIdx + j;
Packit 1fb8d4
			messages[j].numQuant = message->numQuant;
Packit 1fb8d4
			messages[j].quantVals = message->quantVals;
Packit 1fb8d4
			messages[j].numRects = message->numRects;
Packit 1fb8d4
			messages[j].rects = message->rects;
Packit 1fb8d4
			messages[j].freeRects = FALSE;
Packit 1fb8d4
			messages[j].freeArray = TRUE;
Packit 1fb8d4
Packit Service 5a9772
			if (!(messages[j].tiles = (RFX_TILE**)calloc(message->numTiles, sizeof(RFX_TILE*))))
Packit 1fb8d4
				goto free_messages;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		messages[j].tilesDataSize += tileDataSize;
Packit 1fb8d4
		messages[j].tiles[messages[j].numTiles++] = message->tiles[i];
Packit 1fb8d4
		message->tiles[i] = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	*numMessages = j + 1;
Packit 1fb8d4
	context->frameIdx += j;
Packit 1fb8d4
	message->numTiles = 0;
Packit 1fb8d4
	return messages;
Packit 1fb8d4
free_messages:
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < j; i++)
Packit 1fb8d4
		free(messages[i].tiles);
Packit 1fb8d4
Packit 1fb8d4
	free(messages);
Packit 1fb8d4
	return NULL;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
RFX_MESSAGE* rfx_encode_messages(RFX_CONTEXT* context, const RFX_RECT* rects, int numRects,
Packit 1fb8d4
                                 BYTE* data, int width, int height, int scanline, int* numMessages,
Packit 1fb8d4
                                 int maxDataSize)
Packit 1fb8d4
{
Packit 1fb8d4
	RFX_MESSAGE* message;
Packit 1fb8d4
	RFX_MESSAGE* messageList;
Packit 1fb8d4
Packit Service 5a9772
	if (!(message = rfx_encode_message(context, rects, numRects, data, width, height, scanline)))
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit Service 5a9772
	if (!(messageList = rfx_split_message(context, message, numMessages, maxDataSize)))
Packit 1fb8d4
	{
Packit 1fb8d4
		message->freeRects = TRUE;
Packit 1fb8d4
		rfx_message_free(context, message);
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rfx_message_free(context, message);
Packit 1fb8d4
	return messageList;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL rfx_write_message_tileset(RFX_CONTEXT* context, wStream* s, RFX_MESSAGE* message)
Packit 1fb8d4
{
Packit 1fb8d4
	int i;
Packit 1fb8d4
	RFX_TILE* tile;
Packit 1fb8d4
	UINT32 blockLen;
Packit 1fb8d4
	UINT32* quantVals;
Packit 1fb8d4
	blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize;
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, blockLen))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	Stream_Write_UINT16(s, WBT_EXTENSION);          /* CodecChannelT.blockType (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, blockLen);               /* set CodecChannelT.blockLen (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 1);                       /* CodecChannelT.codecId (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0);                       /* CodecChannelT.channelId (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT16(s, CBT_TILESET);            /* subtype (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, 0);                      /* idx (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, context->properties);    /* properties (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT8(s, message->numQuant);       /* numQuant (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0x40);                    /* tileSize (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT16(s, message->numTiles);      /* numTiles (2 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, message->tilesDataSize); /* tilesDataSize (4 bytes) */
Packit 1fb8d4
	quantVals = message->quantVals;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < message->numQuant * 5; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		Stream_Write_UINT8(s, quantVals[0] + (quantVals[1] << 4));
Packit 1fb8d4
		quantVals += 2;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < message->numTiles; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!(tile = message->tiles[i]))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		if (!rfx_write_tile(context, s, tile))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
#ifdef WITH_DEBUG_RFX
Packit 1fb8d4
	WLog_Print(context->priv->log, WLOG_DEBUG,
Packit Service 5a9772
	           "numQuant: %" PRIu16 " numTiles: %" PRIu16 " tilesDataSize: %" PRIu32 "",
Packit 1fb8d4
	           message->numQuant, message->numTiles, message->tilesDataSize);
Packit 1fb8d4
#endif
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL rfx_write_message_frame_begin(RFX_CONTEXT* context, wStream* s, RFX_MESSAGE* message)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, 14))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	Stream_Write_UINT16(s, WBT_FRAME_BEGIN);   /* CodecChannelT.blockType */
Packit Service 5a9772
	Stream_Write_UINT32(s, 14);                /* CodecChannelT.blockLen */
Packit Service 5a9772
	Stream_Write_UINT8(s, 1);                  /* CodecChannelT.codecId */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0);                  /* CodecChannelT.channelId */
Packit 1fb8d4
	Stream_Write_UINT32(s, message->frameIdx); /* frameIdx */
Packit Service 5a9772
	Stream_Write_UINT16(s, 1);                 /* numRegions */
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL rfx_write_message_region(RFX_CONTEXT* context, wStream* s, RFX_MESSAGE* message)
Packit 1fb8d4
{
Packit 1fb8d4
	int i;
Packit 1fb8d4
	UINT32 blockLen;
Packit 1fb8d4
	blockLen = 15 + (message->numRects * 8);
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, blockLen))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	Stream_Write_UINT16(s, WBT_REGION);        /* CodecChannelT.blockType (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, blockLen);          /* set CodecChannelT.blockLen (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 1);                  /* CodecChannelT.codecId (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0);                  /* CodecChannelT.channelId (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 1);                  /* regionFlags (1 byte) */
Packit 1fb8d4
	Stream_Write_UINT16(s, message->numRects); /* numRects (2 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < message->numRects; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		const RFX_RECT* rect = rfx_message_get_rect(message, i);
Packit 1fb8d4
		/* Clipping rectangles are relative to destLeft, destTop */
Packit Service 5a9772
		Stream_Write_UINT16(s, rect->x);      /* x (2 bytes) */
Packit Service 5a9772
		Stream_Write_UINT16(s, rect->y);      /* y (2 bytes) */
Packit Service 5a9772
		Stream_Write_UINT16(s, rect->width);  /* width (2 bytes) */
Packit 1fb8d4
		Stream_Write_UINT16(s, rect->height); /* height (2 bytes) */
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT16(s, CBT_REGION); /* regionType (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, 1);          /* numTilesets (2 bytes) */
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL rfx_write_message_frame_end(RFX_CONTEXT* context, wStream* s, RFX_MESSAGE* message)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, 8))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT16(s, WBT_FRAME_END); /* CodecChannelT.blockType */
Packit Service 5a9772
	Stream_Write_UINT32(s, 8);             /* CodecChannelT.blockLen */
Packit Service 5a9772
	Stream_Write_UINT8(s, 1);              /* CodecChannelT.codecId */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0);              /* CodecChannelT.channelId */
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rfx_write_message(RFX_CONTEXT* context, wStream* s, RFX_MESSAGE* message)
Packit 1fb8d4
{
Packit 1fb8d4
	if (context->state == RFX_STATE_SEND_HEADERS)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!rfx_compose_message_header(context, s))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		context->state = RFX_STATE_SEND_FRAME_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!rfx_write_message_frame_begin(context, s, message) ||
Packit 1fb8d4
	    !rfx_write_message_region(context, s, message) ||
Packit 1fb8d4
	    !rfx_write_message_tileset(context, s, message) ||
Packit 1fb8d4
	    !rfx_write_message_frame_end(context, s, message))
Packit 1fb8d4
	{
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
BOOL rfx_compose_message(RFX_CONTEXT* context, wStream* s, const RFX_RECT* rects, int numRects,
Packit Service 5a9772
                         BYTE* data, int width, int height, int scanline)
Packit 1fb8d4
{
Packit 1fb8d4
	RFX_MESSAGE* message;
Packit 1fb8d4
	BOOL ret = TRUE;
Packit 1fb8d4
Packit Service 5a9772
	if (!(message = rfx_encode_message(context, rects, numRects, data, width, height, scanline)))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	ret = rfx_write_message(context, s, message);
Packit 1fb8d4
	message->freeRects = TRUE;
Packit 1fb8d4
	rfx_message_free(context, message);
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}