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
|
2013-12-04 14:37:57 +04:00
|
|
|
* Copyright 2011 Norbert Federa <norbert.federa@thincast.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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
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
|
|
|
|
2013-01-23 06:24:04 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
2013-01-19 02:32:58 +04:00
|
|
|
#include <freerdp/primitives.h>
|
|
|
|
|
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"
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define MINMAX(_v, _l, _h) ((_v) < (_l) ? (_l) : ((_v) > (_h) ? (_h) : (_v)))
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static void rfx_encode_format_rgb(const BYTE* rgb_data, int width, int height, int rowstride,
|
|
|
|
UINT32 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;
|
2023-07-31 20:07:08 +03:00
|
|
|
const UINT32* src_32;
|
|
|
|
const UINT16* src_16;
|
2012-10-09 11:01:37 +04:00
|
|
|
INT16 r, g, b;
|
2019-11-06 17:24:51 +03:00
|
|
|
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;
|
2023-07-31 20:07:08 +03:00
|
|
|
src_32 = (UINT32*)src;
|
|
|
|
src_16 = (UINT16*)src;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
switch (pixel_format)
|
|
|
|
{
|
2016-09-20 10:50:44 +03:00
|
|
|
case PIXEL_FORMAT_BGRX32:
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_BGRA32:
|
2011-08-10 11:13:39 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2023-07-31 20:07:08 +03:00
|
|
|
*b_buf++ = (INT16)(*src_32 & 0xFF);
|
|
|
|
*g_buf++ = (INT16)((*src_32 >> 8) & 0xFF);
|
|
|
|
*r_buf++ = (INT16)((*src_32 >> 16) & 0xFF);
|
|
|
|
src_32++;
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2016-09-20 10:50:44 +03:00
|
|
|
case PIXEL_FORMAT_XBGR32:
|
|
|
|
case PIXEL_FORMAT_ABGR32:
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2023-07-31 20:07:08 +03:00
|
|
|
*b_buf++ = (INT16)((*src_32 >> 8) & 0xFF);
|
|
|
|
*g_buf++ = (INT16)((*src_32 >> 16) & 0xFF);
|
|
|
|
*r_buf++ = (INT16)((*src_32 >> 24) & 0xFF);
|
|
|
|
src_32++;
|
2016-09-20 10:50:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PIXEL_FORMAT_RGBX32:
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_RGBA32:
|
2011-08-10 11:13:39 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2023-07-31 20:07:08 +03:00
|
|
|
*r_buf++ = (INT16)(*src_32 & 0xFF);
|
|
|
|
*g_buf++ = (INT16)((*src_32 >> 8) & 0xFF);
|
|
|
|
*b_buf++ = (INT16)((*src_32 >> 16) & 0xFF);
|
|
|
|
src_32++;
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2016-09-20 10:50:44 +03:00
|
|
|
case PIXEL_FORMAT_XRGB32:
|
|
|
|
case PIXEL_FORMAT_ARGB32:
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2023-07-31 20:07:08 +03:00
|
|
|
*r_buf++ = (INT16)((*src_32 >> 8) & 0xFF);
|
|
|
|
*g_buf++ = (INT16)((*src_32 >> 16) & 0xFF);
|
|
|
|
*b_buf++ = (INT16)((*src_32 >> 24) & 0xFF);
|
|
|
|
src_32++;
|
2016-09-20 10:50:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
case PIXEL_FORMAT_BGR24:
|
2011-08-10 11:13:39 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2023-07-31 20:07:08 +03:00
|
|
|
#ifdef __LITTLE_ENDIAN__
|
2016-04-05 18:07:45 +03:00
|
|
|
*b_buf++ = (INT16)(*src++);
|
|
|
|
*g_buf++ = (INT16)(*src++);
|
|
|
|
*r_buf++ = (INT16)(*src++);
|
2023-07-31 20:07:08 +03:00
|
|
|
#else
|
|
|
|
*r_buf++ = (INT16)(*src++);
|
|
|
|
*g_buf++ = (INT16)(*src++);
|
|
|
|
*b_buf++ = (INT16)(*src++);
|
|
|
|
#endif
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
|
|
|
case PIXEL_FORMAT_RGB24:
|
2011-08-10 11:13:39 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2023-07-31 20:07:08 +03:00
|
|
|
#ifdef __LITTLE_ENDIAN__
|
2016-04-05 18:07:45 +03:00
|
|
|
*r_buf++ = (INT16)(*src++);
|
|
|
|
*g_buf++ = (INT16)(*src++);
|
|
|
|
*b_buf++ = (INT16)(*src++);
|
2023-07-31 20:07:08 +03:00
|
|
|
#else
|
|
|
|
*b_buf++ = (INT16)(*src++);
|
|
|
|
*g_buf++ = (INT16)(*src++);
|
|
|
|
*r_buf++ = (INT16)(*src++);
|
|
|
|
#endif
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
|
|
|
case PIXEL_FORMAT_BGR16:
|
2011-09-08 02:53:07 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2023-07-31 20:07:08 +03:00
|
|
|
*b_buf++ = (INT16)((*src_16) & 0x1F);
|
|
|
|
*g_buf++ = (INT16)((*src_16 >> 5) & 0x3F);
|
|
|
|
*r_buf++ = (INT16)((*src_16 >> 11) & 0x1F);
|
|
|
|
src_16++;
|
2011-09-08 02:53:07 +04:00
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-09-08 02:53:07 +04:00
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
|
|
|
case PIXEL_FORMAT_RGB16:
|
2011-09-08 02:53:07 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2023-07-31 20:07:08 +03:00
|
|
|
*r_buf++ = (INT16)((*src_16 & 0x1F));
|
|
|
|
*g_buf++ = (INT16)((*src_16 >> 5) & 0x3F);
|
|
|
|
*b_buf++ = (INT16)((*src_16 >> 11) & 0x1F);
|
|
|
|
src_16++;
|
2011-09-08 02:53:07 +04:00
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-09-08 02:53:07 +04:00
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
|
|
|
case PIXEL_FORMAT_RGB8:
|
2011-09-14 21:48:08 +04:00
|
|
|
if (!palette)
|
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-09-14 21:48:08 +04:00
|
|
|
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));
|
2023-07-31 20:07:08 +03:00
|
|
|
idx = (BYTE)(((*src_32 & 0xFF) >> shift) & 1);
|
|
|
|
idx |= (BYTE)(((((*src_32 >> 8) & 0xFF) >> shift) & 1) << 1);
|
|
|
|
idx |= (BYTE)(((((*src_32 >> 16) & 0xFF) >> shift) & 1) << 2);
|
|
|
|
idx |= (BYTE)(((((*src_32 >> 24) & 0xFF) >> shift) & 1) << 3);
|
2011-09-14 21:48:08 +04:00
|
|
|
idx *= 3;
|
2019-11-06 17:24:51 +03:00
|
|
|
*r_buf++ = (INT16)palette[idx];
|
|
|
|
*g_buf++ = (INT16)palette[idx + 1];
|
|
|
|
*b_buf++ = (INT16)palette[idx + 2];
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-09-14 21:48:08 +04:00
|
|
|
if (shift == 0)
|
2023-07-31 20:07:08 +03:00
|
|
|
src_32++;
|
2011-09-14 21:48:08 +04:00
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-09-14 21:48:08 +04:00
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
|
|
|
case PIXEL_FORMAT_A4:
|
2011-09-14 21:48:08 +04:00
|
|
|
if (!palette)
|
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-09-14 21:48:08 +04:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
int idx = (*src) * 3;
|
2019-11-06 17:24:51 +03: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++;
|
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-09-14 21:48:08 +04:00
|
|
|
break;
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2011-08-10 11:13:39 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-04-05 18:07:45 +03:00
|
|
|
|
2019-11-06 17:24:51 +03: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)
|
|
|
|
{
|
2013-08-12 21:13:27 +04:00
|
|
|
CopyMemory(r_buf, r_last, 64 * sizeof(INT16));
|
|
|
|
CopyMemory(g_buf, g_last, 64 * sizeof(INT16));
|
|
|
|
CopyMemory(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-19 02:32:58 +04:00
|
|
|
/* rfx_encode_rgb_to_ycbcr code now resides in the primitives library. */
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static void rfx_encode_component(RFX_CONTEXT* context, const UINT32* quantization_values,
|
2016-04-05 18:07:45 +03:00
|
|
|
INT16* data, BYTE* buffer, int buffer_size, int* size)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2013-01-23 06:24:04 +04:00
|
|
|
INT16* dwt_buffer;
|
|
|
|
dwt_buffer = BufferPool_Take(context->priv->BufferPool, -1); /* dwt_buffer */
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_encode_component)
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_dwt_2d_encode)
|
2016-04-05 18:07:45 +03:00
|
|
|
context->dwt_2d_encode(data, dwt_buffer);
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_dwt_2d_encode)
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_quantization_encode)
|
2016-04-05 18:07:45 +03:00
|
|
|
context->quantization_encode(data, quantization_values);
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_quantization_encode)
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_differential_encode)
|
2016-04-05 18:07:45 +03:00
|
|
|
rfx_differential_encode(data + 4032, 64);
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_differential_encode)
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_rlgr_encode)
|
2016-11-23 16:22:42 +03:00
|
|
|
*size = context->rlgr_encode(context->mode, data, 4096, buffer, buffer_size);
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_rlgr_encode)
|
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_encode_component)
|
2013-01-23 06:24:04 +04:00
|
|
|
BufferPool_Return(context->priv->BufferPool, dwt_buffer);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
|
2013-08-13 22:13:05 +04:00
|
|
|
void rfx_encode_rgb(RFX_CONTEXT* context, RFX_TILE* tile)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2022-04-27 22:02:18 +03:00
|
|
|
union
|
|
|
|
{
|
|
|
|
const INT16** cpv;
|
|
|
|
INT16** pv;
|
|
|
|
} cnv;
|
2013-08-20 02:39:19 +04:00
|
|
|
BYTE* pBuffer;
|
2013-01-19 02:32:58 +04:00
|
|
|
INT16* pSrcDst[3];
|
2013-08-13 19:09:54 +04:00
|
|
|
int YLen, CbLen, CrLen;
|
2019-11-06 17:24:51 +03:00
|
|
|
UINT32 *YQuant, *CbQuant, *CrQuant;
|
2013-01-23 06:24:04 +04:00
|
|
|
primitives_t* prims = primitives_get();
|
2013-01-19 02:32:58 +04:00
|
|
|
static const prim_size_t roi_64x64 = { 64, 64 };
|
2013-01-23 06:24:04 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(pBuffer = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
|
2015-03-23 19:25:23 +03:00
|
|
|
return;
|
|
|
|
|
2013-08-13 19:09:54 +04:00
|
|
|
YLen = CbLen = CrLen = 0;
|
2013-08-13 21:45:28 +04:00
|
|
|
YQuant = context->quants + (tile->quantIdxY * 10);
|
|
|
|
CbQuant = context->quants + (tile->quantIdxCb * 10);
|
|
|
|
CrQuant = context->quants + (tile->quantIdxCr * 10);
|
2019-11-06 17:24:51 +03:00
|
|
|
pSrcDst[0] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 0) + 16])); /* y_r_buffer */
|
|
|
|
pSrcDst[1] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 1) + 16])); /* cb_g_buffer */
|
|
|
|
pSrcDst[2] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 2) + 16])); /* cr_b_buffer */
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_encode_rgb)
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_encode_format_rgb)
|
2016-04-05 18:07:45 +03:00
|
|
|
rfx_encode_format_rgb(tile->data, tile->width, tile->height, tile->scanline,
|
2019-11-06 17:24:51 +03:00
|
|
|
context->pixel_format, context->palette, pSrcDst[0], pSrcDst[1],
|
|
|
|
pSrcDst[2]);
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_encode_format_rgb)
|
|
|
|
PROFILER_ENTER(context->priv->prof_rfx_rgb_to_ycbcr)
|
2022-04-27 22:02:18 +03:00
|
|
|
|
|
|
|
cnv.pv = pSrcDst;
|
|
|
|
prims->RGBToYCbCr_16s16s_P3P3(cnv.cpv, 64 * sizeof(INT16), pSrcDst, 64 * sizeof(INT16),
|
|
|
|
&roi_64x64);
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_rgb_to_ycbcr)
|
2013-08-13 21:45:28 +04:00
|
|
|
/**
|
|
|
|
* We need to clear the buffers as the RLGR encoder expects it to be initialized to zero.
|
|
|
|
* This allows simplifying and improving the performance of the encoding process.
|
|
|
|
*/
|
|
|
|
ZeroMemory(tile->YData, 4096);
|
|
|
|
ZeroMemory(tile->CbData, 4096);
|
|
|
|
ZeroMemory(tile->CrData, 4096);
|
|
|
|
rfx_encode_component(context, YQuant, pSrcDst[0], tile->YData, 4096, &YLen);
|
|
|
|
rfx_encode_component(context, CbQuant, pSrcDst[1], tile->CbData, 4096, &CbLen);
|
|
|
|
rfx_encode_component(context, CrQuant, pSrcDst[2], tile->CrData, 4096, &CrLen);
|
2019-11-06 17:24:51 +03:00
|
|
|
tile->YLen = (UINT16)YLen;
|
|
|
|
tile->CbLen = (UINT16)CbLen;
|
|
|
|
tile->CrLen = (UINT16)CrLen;
|
2018-02-15 12:19:15 +03:00
|
|
|
PROFILER_EXIT(context->priv->prof_rfx_encode_rgb)
|
2013-08-20 02:39:19 +04:00
|
|
|
BufferPool_Return(context->priv->BufferPool, pBuffer);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|