Blame libfreerdp/codec/rfx_encode.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * RemoteFX Codec Library - Encode
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Vic Lee
Packit 1fb8d4
 * Copyright 2011 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 <stdio.h>
Packit 1fb8d4
#include <stdlib.h>
Packit 1fb8d4
#include <string.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/collections.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/primitives.h>
Packit 1fb8d4
Packit 1fb8d4
#include "rfx_types.h"
Packit 1fb8d4
#include "rfx_rlgr.h"
Packit 1fb8d4
#include "rfx_differential.h"
Packit 1fb8d4
#include "rfx_quantization.h"
Packit 1fb8d4
#include "rfx_dwt.h"
Packit 1fb8d4
Packit 1fb8d4
#include "rfx_encode.h"
Packit 1fb8d4
Packit 1fb8d4
#define MINMAX(_v,_l,_h) ((_v) < (_l) ? (_l) : ((_v) > (_h) ? (_h) : (_v)))
Packit 1fb8d4
Packit 1fb8d4
static void rfx_encode_format_rgb(const BYTE* rgb_data, int width, int height,
Packit 1fb8d4
                                  int rowstride,
Packit 1fb8d4
                                  UINT32 pixel_format, const BYTE* palette, INT16* r_buf, INT16* g_buf,
Packit 1fb8d4
                                  INT16* b_buf)
Packit 1fb8d4
{
Packit 1fb8d4
	int x, y;
Packit 1fb8d4
	int x_exceed;
Packit 1fb8d4
	int y_exceed;
Packit 1fb8d4
	const BYTE* src;
Packit 1fb8d4
	INT16 r, g, b;
Packit 1fb8d4
	INT16* r_last, *g_last, *b_last;
Packit 1fb8d4
	x_exceed = 64 - width;
Packit 1fb8d4
	y_exceed = 64 - height;
Packit 1fb8d4
Packit 1fb8d4
	for (y = 0; y < height; y++)
Packit 1fb8d4
	{
Packit 1fb8d4
		src = rgb_data + y * rowstride;
Packit 1fb8d4
Packit 1fb8d4
		switch (pixel_format)
Packit 1fb8d4
		{
Packit 1fb8d4
			case PIXEL_FORMAT_BGRX32:
Packit 1fb8d4
			case PIXEL_FORMAT_BGRA32:
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					*b_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*g_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*r_buf++ = (INT16)(*src++);
Packit 1fb8d4
					src++;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PIXEL_FORMAT_XBGR32:
Packit 1fb8d4
			case PIXEL_FORMAT_ABGR32:
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					src++;
Packit 1fb8d4
					*b_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*g_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*r_buf++ = (INT16)(*src++);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PIXEL_FORMAT_RGBX32:
Packit 1fb8d4
			case PIXEL_FORMAT_RGBA32:
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					*r_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*g_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*b_buf++ = (INT16)(*src++);
Packit 1fb8d4
					src++;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PIXEL_FORMAT_XRGB32:
Packit 1fb8d4
			case PIXEL_FORMAT_ARGB32:
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					src++;
Packit 1fb8d4
					*r_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*g_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*b_buf++ = (INT16)(*src++);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PIXEL_FORMAT_BGR24:
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					*b_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*g_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*r_buf++ = (INT16)(*src++);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PIXEL_FORMAT_RGB24:
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					*r_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*g_buf++ = (INT16)(*src++);
Packit 1fb8d4
					*b_buf++ = (INT16)(*src++);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PIXEL_FORMAT_BGR16:
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					*b_buf++ = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
Packit 1fb8d4
					*g_buf++ = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
Packit 1fb8d4
					*r_buf++ = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
Packit 1fb8d4
					src += 2;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PIXEL_FORMAT_RGB16:
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					*r_buf++ = (INT16)(((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
Packit 1fb8d4
					*g_buf++ = (INT16)((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
Packit 1fb8d4
					*b_buf++ = (INT16)((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
Packit 1fb8d4
					src += 2;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PIXEL_FORMAT_RGB8:
Packit 1fb8d4
				if (!palette)
Packit 1fb8d4
					break;
Packit 1fb8d4
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					int shift;
Packit 1fb8d4
					BYTE idx;
Packit 1fb8d4
					shift = (7 - (x % 8));
Packit 1fb8d4
					idx = ((*src) >> shift) & 1;
Packit 1fb8d4
					idx |= (((*(src + 1)) >> shift) & 1) << 1;
Packit 1fb8d4
					idx |= (((*(src + 2)) >> shift) & 1) << 2;
Packit 1fb8d4
					idx |= (((*(src + 3)) >> shift) & 1) << 3;
Packit 1fb8d4
					idx *= 3;
Packit 1fb8d4
					*r_buf++ = (INT16) palette[idx];
Packit 1fb8d4
					*g_buf++ = (INT16) palette[idx + 1];
Packit 1fb8d4
					*b_buf++ = (INT16) palette[idx + 2];
Packit 1fb8d4
Packit 1fb8d4
					if (shift == 0)
Packit 1fb8d4
						src += 4;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PIXEL_FORMAT_A4:
Packit 1fb8d4
				if (!palette)
Packit 1fb8d4
					break;
Packit 1fb8d4
Packit 1fb8d4
				for (x = 0; x < width; x++)
Packit 1fb8d4
				{
Packit 1fb8d4
					int idx = (*src) * 3;
Packit 1fb8d4
					*r_buf++ = (INT16) palette[idx];
Packit 1fb8d4
					*g_buf++ = (INT16) palette[idx + 1];
Packit 1fb8d4
					*b_buf++ = (INT16) palette[idx + 2];
Packit 1fb8d4
					src++;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit 1fb8d4
				break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		/* Fill the horizontal region outside of 64x64 tile size with the right-most pixel for best quality */
Packit 1fb8d4
		if (x_exceed > 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			r = *(r_buf - 1);
Packit 1fb8d4
			g = *(g_buf - 1);
Packit 1fb8d4
			b = *(b_buf - 1);
Packit 1fb8d4
Packit 1fb8d4
			for (x = 0; x < x_exceed; x++)
Packit 1fb8d4
			{
Packit 1fb8d4
				*r_buf++ = r;
Packit 1fb8d4
				*g_buf++ = g;
Packit 1fb8d4
				*b_buf++ = b;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Fill the vertical region outside of 64x64 tile size with the last line. */
Packit 1fb8d4
	if (y_exceed > 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		r_last = r_buf - 64;
Packit 1fb8d4
		g_last = g_buf - 64;
Packit 1fb8d4
		b_last = b_buf - 64;
Packit 1fb8d4
Packit 1fb8d4
		while (y_exceed > 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			CopyMemory(r_buf, r_last, 64 * sizeof(INT16));
Packit 1fb8d4
			CopyMemory(g_buf, g_last, 64 * sizeof(INT16));
Packit 1fb8d4
			CopyMemory(b_buf, b_last, 64 * sizeof(INT16));
Packit 1fb8d4
			r_buf += 64;
Packit 1fb8d4
			g_buf += 64;
Packit 1fb8d4
			b_buf += 64;
Packit 1fb8d4
			y_exceed--;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* rfx_encode_rgb_to_ycbcr code now resides in the primitives library. */
Packit 1fb8d4
Packit 1fb8d4
static void rfx_encode_component(RFX_CONTEXT* context,
Packit 1fb8d4
                                 const UINT32* quantization_values,
Packit 1fb8d4
                                 INT16* data, BYTE* buffer, int buffer_size, int* size)
Packit 1fb8d4
{
Packit 1fb8d4
	INT16* dwt_buffer;
Packit 1fb8d4
	dwt_buffer = BufferPool_Take(context->priv->BufferPool, -1); /* dwt_buffer */
Packit 1fb8d4
	PROFILER_ENTER(context->priv->prof_rfx_encode_component)
Packit 1fb8d4
	PROFILER_ENTER(context->priv->prof_rfx_dwt_2d_encode)
Packit 1fb8d4
	context->dwt_2d_encode(data, dwt_buffer);
Packit 1fb8d4
	PROFILER_EXIT(context->priv->prof_rfx_dwt_2d_encode)
Packit 1fb8d4
	PROFILER_ENTER(context->priv->prof_rfx_quantization_encode)
Packit 1fb8d4
	context->quantization_encode(data, quantization_values);
Packit 1fb8d4
	PROFILER_EXIT(context->priv->prof_rfx_quantization_encode)
Packit 1fb8d4
	PROFILER_ENTER(context->priv->prof_rfx_differential_encode)
Packit 1fb8d4
	rfx_differential_encode(data + 4032, 64);
Packit 1fb8d4
	PROFILER_EXIT(context->priv->prof_rfx_differential_encode)
Packit 1fb8d4
	PROFILER_ENTER(context->priv->prof_rfx_rlgr_encode)
Packit 1fb8d4
	*size = context->rlgr_encode(context->mode, data, 4096, buffer, buffer_size);
Packit 1fb8d4
	PROFILER_EXIT(context->priv->prof_rfx_rlgr_encode)
Packit 1fb8d4
	PROFILER_EXIT(context->priv->prof_rfx_encode_component)
Packit 1fb8d4
	BufferPool_Return(context->priv->BufferPool, dwt_buffer);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rfx_encode_rgb(RFX_CONTEXT* context, RFX_TILE* tile)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE* pBuffer;
Packit 1fb8d4
	INT16* pSrcDst[3];
Packit 1fb8d4
	int YLen, CbLen, CrLen;
Packit 1fb8d4
	UINT32* YQuant, *CbQuant, *CrQuant;
Packit 1fb8d4
	primitives_t* prims = primitives_get();
Packit 1fb8d4
	static const prim_size_t roi_64x64 = { 64, 64 };
Packit 1fb8d4
Packit 1fb8d4
	if (!(pBuffer = (BYTE*) BufferPool_Take(context->priv->BufferPool, -1)))
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	YLen = CbLen = CrLen = 0;
Packit 1fb8d4
	YQuant = context->quants + (tile->quantIdxY * 10);
Packit 1fb8d4
	CbQuant = context->quants + (tile->quantIdxCb * 10);
Packit 1fb8d4
	CrQuant = context->quants + (tile->quantIdxCr * 10);
Packit 1fb8d4
	pSrcDst[0] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 0) +
Packit 1fb8d4
	                                       16])); /* y_r_buffer */
Packit 1fb8d4
	pSrcDst[1] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 1) +
Packit 1fb8d4
	                                       16])); /* cb_g_buffer */
Packit 1fb8d4
	pSrcDst[2] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 2) +
Packit 1fb8d4
	                                       16])); /* cr_b_buffer */
Packit 1fb8d4
	PROFILER_ENTER(context->priv->prof_rfx_encode_rgb)
Packit 1fb8d4
	PROFILER_ENTER(context->priv->prof_rfx_encode_format_rgb)
Packit 1fb8d4
	rfx_encode_format_rgb(tile->data, tile->width, tile->height, tile->scanline,
Packit 1fb8d4
	                      context->pixel_format, context->palette, pSrcDst[0], pSrcDst[1], pSrcDst[2]);
Packit 1fb8d4
	PROFILER_EXIT(context->priv->prof_rfx_encode_format_rgb)
Packit 1fb8d4
	PROFILER_ENTER(context->priv->prof_rfx_rgb_to_ycbcr)
Packit 1fb8d4
	prims->RGBToYCbCr_16s16s_P3P3((const INT16**) pSrcDst, 64 * sizeof(INT16),
Packit 1fb8d4
	                              pSrcDst, 64 * sizeof(INT16), &roi_64x64);
Packit 1fb8d4
	PROFILER_EXIT(context->priv->prof_rfx_rgb_to_ycbcr)
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * We need to clear the buffers as the RLGR encoder expects it to be initialized to zero.
Packit 1fb8d4
	 * This allows simplifying and improving the performance of the encoding process.
Packit 1fb8d4
	 */
Packit 1fb8d4
	ZeroMemory(tile->YData, 4096);
Packit 1fb8d4
	ZeroMemory(tile->CbData, 4096);
Packit 1fb8d4
	ZeroMemory(tile->CrData, 4096);
Packit 1fb8d4
	rfx_encode_component(context, YQuant, pSrcDst[0], tile->YData, 4096, &YLen);
Packit 1fb8d4
	rfx_encode_component(context, CbQuant, pSrcDst[1], tile->CbData, 4096, &CbLen);
Packit 1fb8d4
	rfx_encode_component(context, CrQuant, pSrcDst[2], tile->CrData, 4096, &CrLen);
Packit 1fb8d4
	tile->YLen = (UINT16) YLen;
Packit 1fb8d4
	tile->CbLen = (UINT16) CbLen;
Packit 1fb8d4
	tile->CrLen = (UINT16) CrLen;
Packit 1fb8d4
	PROFILER_EXIT(context->priv->prof_rfx_encode_rgb)
Packit 1fb8d4
	BufferPool_Return(context->priv->BufferPool, pBuffer);
Packit 1fb8d4
}