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 - DWT
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include "rfx_dwt.h"
|
|
|
|
|
2024-05-30 00:48:22 +03:00
|
|
|
static INLINE void rfx_dwt_2d_decode_block(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT idwt,
|
|
|
|
size_t subband_width)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
INT16* dst = NULL;
|
|
|
|
INT16* l = NULL;
|
|
|
|
INT16* h = NULL;
|
|
|
|
INT16* l_dst = NULL;
|
|
|
|
INT16* h_dst = NULL;
|
|
|
|
INT16* hl = NULL;
|
|
|
|
INT16* lh = NULL;
|
|
|
|
INT16* hh = NULL;
|
|
|
|
INT16* ll = NULL;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2023-10-11 14:56:23 +03:00
|
|
|
const size_t total_width = subband_width << 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/* Inverse DWT in horizontal direction, results in 2 sub-bands in L, H order in tmp buffer idwt.
|
|
|
|
*/
|
2011-08-10 11:13:39 +04:00
|
|
|
/* The 4 sub-bands are stored in HL(0), LH(1), HH(2), LL(3) order. */
|
|
|
|
/* The lower part L uses LL(3) and HL(0). */
|
|
|
|
/* The higher part H uses LH(1) and HH(2). */
|
|
|
|
|
|
|
|
ll = buffer + subband_width * subband_width * 3;
|
|
|
|
hl = buffer;
|
|
|
|
l_dst = idwt;
|
|
|
|
|
|
|
|
lh = buffer + subband_width * subband_width;
|
|
|
|
hh = buffer + subband_width * subband_width * 2;
|
|
|
|
h_dst = idwt + subband_width * subband_width * 2;
|
|
|
|
|
2023-10-11 14:56:23 +03:00
|
|
|
for (size_t y = 0; y < subband_width; y++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
|
|
|
/* Even coefficients */
|
|
|
|
l_dst[0] = ll[0] - ((hl[0] + hl[0] + 1) >> 1);
|
|
|
|
h_dst[0] = lh[0] - ((hh[0] + hh[0] + 1) >> 1);
|
2023-10-11 14:56:23 +03:00
|
|
|
for (size_t n = 1; n < subband_width; n++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2023-10-11 14:56:23 +03:00
|
|
|
const size_t x = n << 1;
|
2019-11-06 17:24:51 +03:00
|
|
|
l_dst[x] = ll[n] - ((hl[n - 1] + hl[n] + 1) >> 1);
|
|
|
|
h_dst[x] = lh[n] - ((hh[n - 1] + hh[n] + 1) >> 1);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Odd coefficients */
|
2023-10-11 14:56:23 +03:00
|
|
|
size_t n = 0;
|
2024-01-30 12:25:38 +03:00
|
|
|
for (; n < subband_width - 1; n++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2023-10-11 14:56:23 +03:00
|
|
|
const size_t x = n << 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
l_dst[x + 1] = (hl[n] << 1) + ((l_dst[x] + l_dst[x + 2]) >> 1);
|
|
|
|
h_dst[x + 1] = (hh[n] << 1) + ((h_dst[x] + h_dst[x + 2]) >> 1);
|
|
|
|
}
|
2023-10-11 14:56:23 +03:00
|
|
|
|
|
|
|
const size_t x = n << 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
l_dst[x + 1] = (hl[n] << 1) + (l_dst[x]);
|
2019-11-06 17:24:51 +03:00
|
|
|
h_dst[x + 1] = (hh[n] << 1) + (h_dst[x]);
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
ll += subband_width;
|
|
|
|
hl += subband_width;
|
|
|
|
l_dst += total_width;
|
|
|
|
|
|
|
|
lh += subband_width;
|
|
|
|
hh += subband_width;
|
|
|
|
h_dst += total_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inverse DWT in vertical direction, results are stored in original buffer. */
|
2023-10-11 14:56:23 +03:00
|
|
|
for (size_t x = 0; x < total_width; x++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2020-05-06 12:36:44 +03:00
|
|
|
l = idwt + x;
|
|
|
|
h = idwt + x + subband_width * total_width;
|
|
|
|
dst = buffer + x;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2020-05-06 12:36:44 +03:00
|
|
|
*dst = *l - ((*h * 2 + 1) >> 1);
|
|
|
|
|
2023-10-11 14:56:23 +03:00
|
|
|
for (size_t n = 1; n < subband_width; n++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2020-05-06 12:36:44 +03:00
|
|
|
l += total_width;
|
|
|
|
h += total_width;
|
|
|
|
|
|
|
|
/* Even coefficients */
|
|
|
|
dst[2 * total_width] = *l - ((*(h - total_width) + *h + 1) >> 1);
|
|
|
|
|
|
|
|
/* Odd coefficients */
|
|
|
|
dst[total_width] = (*(h - total_width) << 1) + ((*dst + dst[2 * total_width]) >> 1);
|
|
|
|
|
|
|
|
dst += 2 * total_width;
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
2020-05-06 12:36:44 +03:00
|
|
|
|
|
|
|
dst[total_width] = (*h << 1) + ((*dst * 2) >> 1);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 00:48:22 +03:00
|
|
|
void rfx_dwt_2d_decode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2023-08-23 15:45:35 +03:00
|
|
|
WINPR_ASSERT(buffer);
|
|
|
|
WINPR_ASSERT(dwt_buffer);
|
|
|
|
|
2014-08-19 22:48:09 +04:00
|
|
|
rfx_dwt_2d_decode_block(&buffer[3840], dwt_buffer, 8);
|
|
|
|
rfx_dwt_2d_decode_block(&buffer[3072], dwt_buffer, 16);
|
|
|
|
rfx_dwt_2d_decode_block(&buffer[0], dwt_buffer, 32);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|
|
|
|
|
2024-05-30 00:48:22 +03:00
|
|
|
static void rfx_dwt_2d_encode_block(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt,
|
|
|
|
UINT32 subband_width)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
INT16* src = NULL;
|
|
|
|
INT16* l = NULL;
|
|
|
|
INT16* h = NULL;
|
|
|
|
INT16* l_src = NULL;
|
|
|
|
INT16* h_src = NULL;
|
|
|
|
INT16* hl = NULL;
|
|
|
|
INT16* lh = NULL;
|
|
|
|
INT16* hh = NULL;
|
|
|
|
INT16* ll = NULL;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
2023-08-23 15:45:35 +03:00
|
|
|
const UINT32 total_width = subband_width << 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
/* DWT in vertical direction, results in 2 sub-bands in L, H order in tmp buffer dwt. */
|
2023-08-23 15:45:35 +03:00
|
|
|
for (UINT32 x = 0; x < total_width; x++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2023-08-23 15:45:35 +03:00
|
|
|
for (UINT32 n = 0; n < subband_width; n++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2023-08-23 15:45:35 +03:00
|
|
|
UINT32 y = n << 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
l = dwt + n * total_width + x;
|
|
|
|
h = l + subband_width * total_width;
|
|
|
|
src = buffer + y * total_width + x;
|
|
|
|
|
|
|
|
/* H */
|
2019-11-06 17:24:51 +03:00
|
|
|
*h = (src[total_width] -
|
|
|
|
((src[0] + src[n < subband_width - 1 ? 2 * total_width : 0]) >> 1)) >>
|
|
|
|
1;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
/* L */
|
|
|
|
*l = src[0] + (n == 0 ? *h : (*(h - total_width) + *h) >> 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/* DWT in horizontal direction, results in 4 sub-bands in HL(0), LH(1), HH(2), LL(3) order,
|
|
|
|
* stored in original buffer. */
|
2011-08-10 11:13:39 +04:00
|
|
|
/* The lower part L generates LL(3) and HL(0). */
|
|
|
|
/* The higher part H generates LH(1) and HH(2). */
|
|
|
|
|
|
|
|
ll = buffer + subband_width * subband_width * 3;
|
|
|
|
hl = buffer;
|
|
|
|
l_src = dwt;
|
|
|
|
|
|
|
|
lh = buffer + subband_width * subband_width;
|
|
|
|
hh = buffer + subband_width * subband_width * 2;
|
|
|
|
h_src = dwt + subband_width * subband_width * 2;
|
|
|
|
|
2023-08-23 15:45:35 +03:00
|
|
|
for (UINT32 y = 0; y < subband_width; y++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
|
|
|
/* L */
|
2023-08-23 15:45:35 +03:00
|
|
|
for (UINT32 n = 0; n < subband_width; n++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2023-08-23 15:45:35 +03:00
|
|
|
UINT32 x = n << 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
/* HL */
|
2019-11-06 17:24:51 +03:00
|
|
|
hl[n] =
|
|
|
|
(l_src[x + 1] - ((l_src[x] + l_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
/* LL */
|
|
|
|
ll[n] = l_src[x] + (n == 0 ? hl[n] : (hl[n - 1] + hl[n]) >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* H */
|
2023-08-23 15:45:35 +03:00
|
|
|
for (UINT32 n = 0; n < subband_width; n++)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2023-08-23 15:45:35 +03:00
|
|
|
UINT32 x = n << 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
|
|
|
|
/* HH */
|
2019-11-06 17:24:51 +03:00
|
|
|
hh[n] =
|
|
|
|
(h_src[x + 1] - ((h_src[x] + h_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1;
|
2011-08-10 11:13:39 +04:00
|
|
|
/* LH */
|
|
|
|
lh[n] = h_src[x] + (n == 0 ? hh[n] : (hh[n - 1] + hh[n]) >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ll += subband_width;
|
|
|
|
hl += subband_width;
|
|
|
|
l_src += total_width;
|
|
|
|
|
|
|
|
lh += subband_width;
|
|
|
|
hh += subband_width;
|
|
|
|
h_src += total_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 00:48:22 +03:00
|
|
|
void rfx_dwt_2d_encode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer)
|
2011-08-10 11:13:39 +04:00
|
|
|
{
|
2023-08-23 15:45:35 +03:00
|
|
|
WINPR_ASSERT(buffer);
|
|
|
|
WINPR_ASSERT(dwt_buffer);
|
|
|
|
|
2014-08-19 22:48:09 +04:00
|
|
|
rfx_dwt_2d_encode_block(&buffer[0], dwt_buffer, 32);
|
|
|
|
rfx_dwt_2d_encode_block(&buffer[3072], dwt_buffer, 16);
|
|
|
|
rfx_dwt_2d_encode_block(&buffer[3840], dwt_buffer, 8);
|
2011-08-10 11:13:39 +04:00
|
|
|
}
|