Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rfx_dwt.c « libfreerdp-rfx - github.com/FreeRDP/FreeRDP-old.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 97b8ec635635ee301a2de444e93444a754e5c221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
   FreeRDP: A Remote Desktop Protocol client.
   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.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "rfx_dwt.h"

void
rfx_dwt_2d_decode_block(sint16 * buffer, sint16 * idwt, int subband_width)
{
	sint16 * dst, * l, * h;
	sint16 * l_dst, * h_dst;
	sint16 * hl, * lh, * hh, * ll;
	int total_width;
	int x, y;
	int n;

	total_width = subband_width << 1;

	/* Inverse DWT in horizontal direction, results in 2 sub-bands in L, H order in tmp buffer idwt. */
	/* 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;

	for (y = 0; y < subband_width; y++)
	{
		/* Even coefficients */
		l_dst[0] = ll[0] - ((hl[0] + hl[0] + 1) >> 1);
		h_dst[0] = lh[0] - ((hh[0] + hh[0] + 1) >> 1);
		for (n = 1; n < subband_width; n++)
		{
			x = n << 1;
			l_dst[x] = ll[n] - ((hl[n-1] + hl[n] + 1) >> 1);
			h_dst[x] = lh[n] - ((hh[n-1] + hh[n] + 1) >> 1);
		}

		/* Odd coefficients */
		for (n = 0; n < subband_width-1; n++)
		{
			x = n << 1;
			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);
		}
		x = n << 1;
		l_dst[x + 1] = (hl[n] << 1) + (l_dst[x]);
		h_dst[x + 1] = (hh[n] << 1) + (h_dst[x]);		

		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. */
	for (x = 0; x < total_width; x++)
	{
		/* Even coefficients */
		for (n = 0; n < subband_width; n++)
		{
			y = n << 1;
			dst = buffer + y * total_width + x;
			l = idwt + n * total_width + x;
			h = l + subband_width * total_width;
			dst[0] = *l - (((n > 0 ? *(h - total_width) : *h) + (*h) + 1) >> 1);
		}

		/* Odd coefficients */
		for (n = 0; n < subband_width; n++)
		{
			y = n << 1;
			dst = buffer + y * total_width + x;
			l = idwt + n * total_width + x;
			h = l + subband_width * total_width;
			dst[total_width] = (*h << 1) + ((dst[0] + dst[n < subband_width - 1 ? 2 * total_width : 0]) >> 1);
		}
	}
}

void
rfx_dwt_2d_decode(sint16 * buffer, sint16 * dwt_buffer_8, sint16 * dwt_buffer_16, sint16 * dwt_buffer_32)
{
	rfx_dwt_2d_decode_block(buffer + 3840, dwt_buffer_8, 8);
	rfx_dwt_2d_decode_block(buffer + 3072, dwt_buffer_16, 16);
	rfx_dwt_2d_decode_block(buffer, dwt_buffer_32, 32);
}

void
rfx_dwt_2d_encode_block(sint16 * buffer, sint16 * dwt, int subband_width)
{
	sint16 * src, * l, * h;
	sint16 * l_src, * h_src;
	sint16 * hl, * lh, * hh, * ll;
	int total_width;
	int x, y;
	int n;

	total_width = subband_width << 1;

	/* DWT in vertical direction, results in 2 sub-bands in L, H order in tmp buffer dwt. */
	for (x = 0; x < total_width; x++)
	{
		for (n = 0; n < subband_width; n++)
		{
			y = n << 1;
			l = dwt + n * total_width + x;
			h = l + subband_width * total_width;
			src = buffer + y * total_width + x;

			/* H */
			*h = (src[total_width] - ((src[0] + src[n < subband_width - 1 ? 2 * total_width : 0]) >> 1)) >> 1;

			/* L */
			*l = src[0] + (n == 0 ? *h : (*(h - total_width) + *h) >> 1);
		}
	}

	/* DWT in horizontal direction, results in 4 sub-bands in HL(0), LH(1), HH(2), LL(3) order, stored in original buffer. */
	/* 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;

	for (y = 0; y < subband_width; y++)
	{
		/* L */
		for (n = 0; n < subband_width; n++)
		{
			x = n << 1;

			/* HL */
			hl[n] = (l_src[x + 1] - ((l_src[x] + l_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1;
			/* LL */
			ll[n] = l_src[x] + (n == 0 ? hl[n] : (hl[n - 1] + hl[n]) >> 1);
		}

		/* H */
		for (n = 0; n < subband_width; n++)
		{
			x = n << 1;

			/* HH */
			hh[n] = (h_src[x + 1] - ((h_src[x] + h_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1;
			/* 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;
	}
}

void
rfx_dwt_2d_encode(sint16 * buffer, sint16 * dwt_buffer_8, sint16 * dwt_buffer_16, sint16 * dwt_buffer_32)
{
	rfx_dwt_2d_encode_block(buffer, dwt_buffer_32, 32);
	rfx_dwt_2d_encode_block(buffer + 3072, dwt_buffer_16, 16);
	rfx_dwt_2d_encode_block(buffer + 3840, dwt_buffer_8, 8);
}