Blame libfreerdp/codec/rfx_quantization.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * RemoteFX Codec Library - Quantization
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Vic Lee
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 <freerdp/primitives.h>
Packit 1fb8d4
Packit 1fb8d4
#include "rfx_quantization.h"
Packit 1fb8d4
Packit 1fb8d4
/*
Packit 1fb8d4
 * Band		Offset		Dimensions	Size
Packit 1fb8d4
 *
Packit 1fb8d4
 * HL1		0		32x32		1024
Packit 1fb8d4
 * LH1		1024		32x32		1024
Packit 1fb8d4
 * HH1		2048		32x32		1024
Packit 1fb8d4
 *
Packit 1fb8d4
 * HL2		3072		16x16		256
Packit 1fb8d4
 * LH2		3328		16x16		256
Packit 1fb8d4
 * HH2		3584		16x16		256
Packit 1fb8d4
 *
Packit 1fb8d4
 * HL3		3840		8x8		64
Packit 1fb8d4
 * LH3		3904		8x8		64
Packit 1fb8d4
 * HH3		3968		8x8		64
Packit 1fb8d4
 *
Packit 1fb8d4
 * LL3		4032		8x8		64
Packit 1fb8d4
 */
Packit 1fb8d4
Packit Service 5a9772
static void rfx_quantization_decode_block(const primitives_t* prims, INT16* buffer, int buffer_size,
Packit Service 5a9772
                                          UINT32 factor)
Packit 1fb8d4
{
Packit 1fb8d4
	if (factor == 0)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	prims->lShiftC_16s(buffer, factor, buffer, buffer_size);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rfx_quantization_decode(INT16* buffer, const UINT32* quantVals)
Packit 1fb8d4
{
Packit 1fb8d4
	const primitives_t* prims = primitives_get();
Packit 1fb8d4
Packit Service 5a9772
	rfx_quantization_decode_block(prims, &buffer[0], 1024, quantVals[8] - 1);    /* HL1 */
Packit 1fb8d4
	rfx_quantization_decode_block(prims, &buffer[1024], 1024, quantVals[7] - 1); /* LH1 */
Packit 1fb8d4
	rfx_quantization_decode_block(prims, &buffer[2048], 1024, quantVals[9] - 1); /* HH1 */
Packit Service 5a9772
	rfx_quantization_decode_block(prims, &buffer[3072], 256, quantVals[5] - 1);  /* HL2 */
Packit Service 5a9772
	rfx_quantization_decode_block(prims, &buffer[3328], 256, quantVals[4] - 1);  /* LH2 */
Packit Service 5a9772
	rfx_quantization_decode_block(prims, &buffer[3584], 256, quantVals[6] - 1);  /* HH2 */
Packit Service 5a9772
	rfx_quantization_decode_block(prims, &buffer[3840], 64, quantVals[2] - 1);   /* HL3 */
Packit Service 5a9772
	rfx_quantization_decode_block(prims, &buffer[3904], 64, quantVals[1] - 1);   /* LH3 */
Packit Service 5a9772
	rfx_quantization_decode_block(prims, &buffer[3968], 64, quantVals[3] - 1);   /* HH3 */
Packit Service 5a9772
	rfx_quantization_decode_block(prims, &buffer[4032], 64, quantVals[0] - 1);   /* LL3 */
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rfx_quantization_encode_block(INT16* buffer, int buffer_size, UINT32 factor)
Packit 1fb8d4
{
Packit 1fb8d4
	INT16* dst;
Packit 1fb8d4
	INT16 half;
Packit 1fb8d4
Packit 1fb8d4
	if (factor == 0)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	half = (1 << (factor - 1));
Packit 1fb8d4
	/* Could probably use prims->rShiftC_16s(dst+half, factor, dst, buffer_size); */
Packit 1fb8d4
	for (dst = buffer; buffer_size > 0; dst++, buffer_size--)
Packit 1fb8d4
	{
Packit 1fb8d4
		*dst = (*dst + half) >> factor;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rfx_quantization_encode(INT16* buffer, const UINT32* quantization_values)
Packit 1fb8d4
{
Packit Service 5a9772
	rfx_quantization_encode_block(buffer, 1024, quantization_values[8] - 6);        /* HL1 */
Packit 1fb8d4
	rfx_quantization_encode_block(buffer + 1024, 1024, quantization_values[7] - 6); /* LH1 */
Packit 1fb8d4
	rfx_quantization_encode_block(buffer + 2048, 1024, quantization_values[9] - 6); /* HH1 */
Packit Service 5a9772
	rfx_quantization_encode_block(buffer + 3072, 256, quantization_values[5] - 6);  /* HL2 */
Packit Service 5a9772
	rfx_quantization_encode_block(buffer + 3328, 256, quantization_values[4] - 6);  /* LH2 */
Packit Service 5a9772
	rfx_quantization_encode_block(buffer + 3584, 256, quantization_values[6] - 6);  /* HH2 */
Packit Service 5a9772
	rfx_quantization_encode_block(buffer + 3840, 64, quantization_values[2] - 6);   /* HL3 */
Packit Service 5a9772
	rfx_quantization_encode_block(buffer + 3904, 64, quantization_values[1] - 6);   /* LH3 */
Packit Service 5a9772
	rfx_quantization_encode_block(buffer + 3968, 64, quantization_values[3] - 6);   /* HH3 */
Packit Service 5a9772
	rfx_quantization_encode_block(buffer + 4032, 64, quantization_values[0] - 6);   /* LL3 */
Packit 1fb8d4
Packit 1fb8d4
	/* The coefficients are scaled by << 5 at RGB->YCbCr phase, so we round it back here */
Packit 1fb8d4
	rfx_quantization_encode_block(buffer, 4096, 5);
Packit 1fb8d4
}