2011-08-10 11:13:39 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-10 11:13:39 +04:00
|
|
|
* RemoteFX Codec Library - Encode
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
2011-12-29 20:41:48 +04:00
|
|
|
* Copyright 2011 Norbert Federa <nfedera@thinstuff.com>
|
2011-08-10 11:13:39 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
#include "rfx_types.h"
|
|
|
|
#include "rfx_rlgr.h"
|
|
|
|
#include "rfx_differential.h"
|
|
|
|
#include "rfx_quantization.h"
|
|
|
|
#include "rfx_dwt.h"
|
|
|
|
|
|
|
|
#include "rfx_encode.h"
|
|
|
|
|
|
|
|
#define MINMAX(_v,_l,_h) ((_v) < (_l) ? (_l) : ((_v) > (_h) ? (_h) : (_v)))
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
static void rfx_encode_format_rgb(const BYTE* rgb_data, int width, int height, int rowstride,
|
|
|
|
RDP_PIXEL_FORMAT pixel_format, const BYTE* palette, INT16* r_buf, INT16* g_buf, INT16* b_buf)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
int x_exceed;
|
|
|
|
int y_exceed;
|
2012-10-09 11:01:37 +04:00
|
|
|
const BYTE* src;
|
|
|
|
INT16 r, g, b;
|
|
|
|
INT16 *r_last, *g_last, *b_last;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
x_exceed = 64 - width;
|
|
|
|
y_exceed = 64 - height;
|
2012-02-05 03:18:46 +04:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
src = rgb_data + y * rowstride;
|
|
|
|
|
|
|
|
switch (pixel_format)
|
|
|
|
{
|
2012-03-13 15:02:19 +04:00
|
|
|
case RDP_PIXEL_FORMAT_B8G8R8A8:
|
2011-08-10 11:13:39 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
*b_buf++ = (INT16) (*src++);
|
|
|
|
*g_buf++ = (INT16) (*src++);
|
|
|
|
*r_buf++ = (INT16) (*src++);
|
2011-08-10 11:13:39 +04:00
|
|
|
src++;
|
|
|
|
}
|
|
|
|
break;
|
2012-03-13 15:02:19 +04:00
|
|
|
case RDP_PIXEL_FORMAT_R8G8B8A8:
|
2011-08-10 11:13:39 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
*r_buf++ = (INT16) (*src++);
|
|
|
|
*g_buf++ = (INT16) (*src++);
|
|
|
|
*b_buf++ = (INT16) (*src++);
|
2011-08-10 11:13:39 +04:00
|
|
|
src++;
|
|
|
|
}
|
|
|
|
break;
|
2012-03-13 15:02:19 +04:00
|
|
|
case RDP_PIXEL_FORMAT_B8G8R8:
|
2011-08-10 11:13:39 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
*b_buf++ = (INT16) (*src++);
|
|
|
|
*g_buf++ = (INT16) (*src++);
|
|
|
|
*r_buf++ = (INT16) (*src++);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
break;
|
2012-03-13 15:02:19 +04:00
|
|
|
case RDP_PIXEL_FORMAT_R8G8B8:
|
2011-08-10 11:13:39 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
*r_buf++ = (INT16) (*src++);
|
|
|
|
*g_buf++ = (INT16) (*src++);
|
|
|
|
*b_buf++ = (INT16) (*src++);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
break;
|
2012-03-13 15:02:19 +04:00
|
|
|
case RDP_PIXEL_FORMAT_B5G6R5_LE:
|
2011-09-08 02:53:07 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
*b_buf++ = (INT16) (((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
|
|
|
|
*g_buf++ = (INT16) ((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
|
|
|
|
*r_buf++ = (INT16) ((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
|
2011-09-08 02:53:07 +04:00
|
|
|
src += 2;
|
|
|
|
}
|
|
|
|
break;
|
2012-03-13 15:02:19 +04:00
|
|
|
case RDP_PIXEL_FORMAT_R5G6B5_LE:
|
2011-09-08 02:53:07 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
*r_buf++ = (INT16) (((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
|
|
|
|
*g_buf++ = (INT16) ((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
|
|
|
|
*b_buf++ = (INT16) ((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
|
2011-09-08 02:53:07 +04:00
|
|
|
src += 2;
|
|
|
|
}
|
|
|
|
break;
|
2012-03-13 15:02:19 +04:00
|
|
|
case RDP_PIXEL_FORMAT_P4_PLANER:
|
2011-09-14 21:48:08 +04:00
|
|
|
if (!palette)
|
|
|
|
break;
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
int shift;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE idx;
|
2011-09-14 21:48:08 +04:00
|
|
|
|
|
|
|
shift = (7 - (x % 8));
|
|
|
|
idx = ((*src) >> shift) & 1;
|
|
|
|
idx |= (((*(src + 1)) >> shift) & 1) << 1;
|
|
|
|
idx |= (((*(src + 2)) >> shift) & 1) << 2;
|
|
|
|
idx |= (((*(src + 3)) >> shift) & 1) << 3;
|
|
|
|
idx *= 3;
|
2012-10-09 11:01:37 +04:00
|
|
|
*r_buf++ = (INT16) palette[idx];
|
|
|
|
*g_buf++ = (INT16) palette[idx + 1];
|
|
|
|
*b_buf++ = (INT16) palette[idx + 2];
|
2011-09-14 21:48:08 +04:00
|
|
|
if (shift == 0)
|
|
|
|
src += 4;
|
|
|
|
}
|
|
|
|
break;
|
2012-03-13 15:02:19 +04:00
|
|
|
case RDP_PIXEL_FORMAT_P8:
|
2011-09-14 21:48:08 +04:00
|
|
|
if (!palette)
|
|
|
|
break;
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
int idx = (*src) * 3;
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
*r_buf++ = (INT16) palette[idx];
|
|
|
|
*g_buf++ = (INT16) palette[idx + 1];
|
|
|
|
*b_buf++ = (INT16) palette[idx + 2];
|
2011-09-14 21:48:08 +04:00
|
|
|
src++;
|
|
|
|
}
|
|
|
|
break;
|
2011-08-10 11:13:39 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-08-26 11:01:57 +04:00
|
|
|
/* Fill the horizontal region outside of 64x64 tile size with the right-most pixel for best quality */
|
2011-08-10 11:13:39 +04:00
|
|
|
if (x_exceed > 0)
|
|
|
|
{
|
2011-08-26 11:01:57 +04:00
|
|
|
r = *(r_buf - 1);
|
|
|
|
g = *(g_buf - 1);
|
|
|
|
b = *(b_buf - 1);
|
2012-02-05 03:18:46 +04:00
|
|
|
|
2011-08-26 11:01:57 +04:00
|
|
|
for (x = 0; x < x_exceed; x++)
|
|
|
|
{
|
|
|
|
*r_buf++ = r;
|
|
|
|
*g_buf++ = g;
|
|
|
|
*b_buf++ = b;
|
|
|
|
}
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 11:01:57 +04:00
|
|
|
/* Fill the vertical region outside of 64x64 tile size with the last line. */
|
2011-08-10 11:13:39 +04:00
|
|
|
if (y_exceed > 0)
|
|
|
|
{
|
2011-08-26 11:01:57 +04:00
|
|
|
r_last = r_buf - 64;
|
|
|
|
g_last = g_buf - 64;
|
|
|
|
b_last = b_buf - 64;
|
2012-02-05 03:18:46 +04:00
|
|
|
|
2011-08-26 11:01:57 +04:00
|
|
|
while (y_exceed > 0)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
memcpy(r_buf, r_last, 64 * sizeof(INT16));
|
|
|
|
memcpy(g_buf, g_last, 64 * sizeof(INT16));
|
|
|
|
memcpy(b_buf, b_last, 64 * sizeof(INT16));
|
2011-08-26 11:01:57 +04:00
|
|
|
r_buf += 64;
|
|
|
|
g_buf += 64;
|
|
|
|
b_buf += 64;
|
|
|
|
y_exceed--;
|
|
|
|
}
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
void rfx_encode_rgb_to_ycbcr(INT16* y_r_buf, INT16* cb_g_buf, INT16* cr_b_buf)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
/* INT32 is used intentionally because we calculate with shifted factors! */
|
2011-08-10 11:13:39 +04:00
|
|
|
int i;
|
2012-10-09 11:26:39 +04:00
|
|
|
INT32 r, g, b;
|
|
|
|
INT32 y, cb, cr;
|
2011-09-03 17:21:52 +04:00
|
|
|
|
|
|
|
/**
|
2012-02-05 03:18:46 +04:00
|
|
|
* The encoded YCbCr coefficients are represented as 11.5 fixed-point numbers:
|
2011-09-03 17:21:52 +04:00
|
|
|
*
|
|
|
|
* 1 sign bit + 10 integer bits + 5 fractional bits
|
|
|
|
*
|
|
|
|
* However only 7 integer bits will be actually used since the value range is [-128.0, 127.0].
|
2012-10-09 11:01:37 +04:00
|
|
|
* In other words, the encoded coefficients is scaled by << 5 when interpreted as INT16.
|
2011-09-03 22:34:49 +04:00
|
|
|
* It will be scaled down to original during the quantization phase.
|
2011-09-03 17:21:52 +04:00
|
|
|
*/
|
2011-08-10 11:13:39 +04:00
|
|
|
for (i = 0; i < 4096; i++)
|
|
|
|
{
|
|
|
|
r = y_r_buf[i];
|
|
|
|
g = cb_g_buf[i];
|
|
|
|
b = cr_b_buf[i];
|
2011-12-29 20:41:48 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We scale the factors by << 15 into 32-bit integers in order to avoid slower
|
|
|
|
* floating point multiplications. Since the terms need to be scaled by << 5 we
|
|
|
|
* simply scale the final sum by >> 10
|
|
|
|
*
|
|
|
|
* Y: 0.299000 << 15 = 9798, 0.587000 << 15 = 19235, 0.114000 << 15 = 3735
|
|
|
|
* Cb: 0.168935 << 15 = 5535, 0.331665 << 15 = 10868, 0.500590 << 15 = 16403
|
|
|
|
* Cr: 0.499813 << 15 = 16377, 0.418531 << 15 = 13714, 0.081282 << 15 = 2663
|
|
|
|
*/
|
|
|
|
|
2012-02-05 03:18:46 +04:00
|
|
|
y = (r * 9798 + g * 19235 + b * 3735) >> 10;
|
|
|
|
cb = (r * -5535 + g * -10868 + b * 16403) >> 10;
|
|
|
|
cr = (r * 16377 + g * -13714 + b * -2663) >> 10;
|
2011-12-29 20:41:48 +04:00
|
|
|
|
2011-09-03 22:34:49 +04:00
|
|
|
y_r_buf[i] = MINMAX(y - 4096, -4096, 4095);
|
|
|
|
cb_g_buf[i] = MINMAX(cb, -4096, 4095);
|
|
|
|
cr_b_buf[i] = MINMAX(cr, -4096, 4095);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static void rfx_encode_component(RFX_CONTEXT* context, const UINT32* quantization_values,
|
2012-10-09 11:01:37 +04:00
|
|
|
INT16* data, BYTE* buffer, int buffer_size, int* size)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_encode_component);
|
|
|
|
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_dwt_2d_encode);
|
|
|
|
context->dwt_2d_encode(data, context->priv->dwt_buffer);
|
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_dwt_2d_encode);
|
|
|
|
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_quantization_encode);
|
|
|
|
context->quantization_encode(data, quantization_values);
|
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_quantization_encode);
|
|
|
|
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_differential_encode);
|
|
|
|
rfx_differential_encode(data + 4032, 64);
|
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_differential_encode);
|
|
|
|
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_rlgr_encode);
|
|
|
|
*size = rfx_rlgr_encode(context->mode, data, 4096, buffer, buffer_size);
|
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_rlgr_encode);
|
|
|
|
|
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_encode_component);
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
void rfx_encode_rgb(RFX_CONTEXT* context, const BYTE* rgb_data, int width, int height, int rowstride,
|
2012-10-09 11:26:39 +04:00
|
|
|
const UINT32* y_quants, const UINT32* cb_quants, const UINT32* cr_quants,
|
2011-08-10 11:13:39 +04:00
|
|
|
STREAM* data_out, int* y_size, int* cb_size, int* cr_size)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
INT16* y_r_buffer = context->priv->y_r_buffer;
|
|
|
|
INT16* cb_g_buffer = context->priv->cb_g_buffer;
|
|
|
|
INT16* cr_b_buffer = context->priv->cr_b_buffer;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_encode_rgb);
|
|
|
|
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_encode_format_rgb);
|
|
|
|
rfx_encode_format_rgb(rgb_data, width, height, rowstride,
|
2011-09-14 21:48:08 +04:00
|
|
|
context->pixel_format, context->palette, y_r_buffer, cb_g_buffer, cr_b_buffer);
|
2011-08-10 11:13:39 +04:00
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_encode_format_rgb);
|
|
|
|
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_encode_rgb_to_ycbcr);
|
|
|
|
context->encode_rgb_to_ycbcr(context->priv->y_r_buffer, context->priv->cb_g_buffer, context->priv->cr_b_buffer);
|
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_encode_rgb_to_ycbcr);
|
|
|
|
|
|
|
|
/* Ensure the buffer is reasonably large enough */
|
|
|
|
stream_check_size(data_out, 4096);
|
|
|
|
rfx_encode_component(context, y_quants, context->priv->y_r_buffer,
|
|
|
|
stream_get_tail(data_out), stream_get_left(data_out), y_size);
|
|
|
|
stream_seek(data_out, *y_size);
|
|
|
|
|
|
|
|
stream_check_size(data_out, 4096);
|
|
|
|
rfx_encode_component(context, cb_quants, context->priv->cb_g_buffer,
|
|
|
|
stream_get_tail(data_out), stream_get_left(data_out), cb_size);
|
|
|
|
stream_seek(data_out, *cb_size);
|
|
|
|
|
|
|
|
stream_check_size(data_out, 4096);
|
|
|
|
rfx_encode_component(context, cr_quants, context->priv->cr_b_buffer,
|
|
|
|
stream_get_tail(data_out), stream_get_left(data_out), cr_size);
|
|
|
|
stream_seek(data_out, *cr_size);
|
|
|
|
|
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_encode_rgb);
|
|
|
|
}
|