/src/FreeRDP/libfreerdp/codec/rfx_dwt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * RemoteFX Codec Library - DWT |
4 | | * |
5 | | * Copyright 2011 Vic Lee |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/assert.h> |
21 | | #include <winpr/cast.h> |
22 | | |
23 | | #include <freerdp/config.h> |
24 | | |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | |
29 | | #include "rfx_dwt.h" |
30 | | |
31 | | static INLINE void rfx_dwt_2d_decode_block(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT idwt, |
32 | | size_t subband_width) |
33 | 0 | { |
34 | 0 | const size_t total_width = subband_width << 1; |
35 | | |
36 | | /* Inverse DWT in horizontal direction, results in 2 sub-bands in L, H order in tmp buffer idwt. |
37 | | */ |
38 | | /* The 4 sub-bands are stored in HL(0), LH(1), HH(2), LL(3) order. */ |
39 | | /* The lower part L uses LL(3) and HL(0). */ |
40 | | /* The higher part H uses LH(1) and HH(2). */ |
41 | |
|
42 | 0 | const INT16* ll = buffer + subband_width * subband_width * 3; |
43 | 0 | const INT16* hl = buffer; |
44 | 0 | INT16* l_dst = idwt; |
45 | |
|
46 | 0 | const INT16* lh = buffer + subband_width * subband_width; |
47 | 0 | const INT16* hh = buffer + subband_width * subband_width * 2; |
48 | 0 | INT16* h_dst = idwt + subband_width * subband_width * 2; |
49 | |
|
50 | 0 | for (size_t y = 0; y < subband_width; y++) |
51 | 0 | { |
52 | | /* Even coefficients */ |
53 | 0 | l_dst[0] = WINPR_ASSERTING_INT_CAST(int16_t, ll[0] - ((hl[0] + hl[0] + 1) >> 1)); |
54 | 0 | h_dst[0] = WINPR_ASSERTING_INT_CAST(int16_t, lh[0] - ((hh[0] + hh[0] + 1) >> 1)); |
55 | 0 | for (size_t n = 1; n < subband_width; n++) |
56 | 0 | { |
57 | 0 | const size_t x = n << 1; |
58 | 0 | l_dst[x] = WINPR_ASSERTING_INT_CAST(int16_t, ll[n] - ((hl[n - 1] + hl[n] + 1) >> 1)); |
59 | 0 | h_dst[x] = WINPR_ASSERTING_INT_CAST(int16_t, lh[n] - ((hh[n - 1] + hh[n] + 1) >> 1)); |
60 | 0 | } |
61 | | |
62 | | /* Odd coefficients */ |
63 | 0 | size_t n = 0; |
64 | 0 | for (; n < subband_width - 1; n++) |
65 | 0 | { |
66 | 0 | const size_t x = n << 1; |
67 | |
|
68 | 0 | const int ld = (hl[n] << 1) + ((l_dst[x] + l_dst[x + 2]) >> 1); |
69 | 0 | const int hd = (hh[n] << 1) + ((h_dst[x] + h_dst[x + 2]) >> 1); |
70 | |
|
71 | 0 | l_dst[x + 1] = WINPR_ASSERTING_INT_CAST(INT16, ld); |
72 | 0 | h_dst[x + 1] = WINPR_ASSERTING_INT_CAST(INT16, hd); |
73 | 0 | } |
74 | | |
75 | 0 | const size_t x = n << 1; |
76 | |
|
77 | 0 | const int ld = (hl[n] << 1) + (l_dst[x]); |
78 | 0 | const int hd = (hh[n] << 1) + (h_dst[x]); |
79 | 0 | l_dst[x + 1] = WINPR_ASSERTING_INT_CAST(INT16, ld); |
80 | 0 | h_dst[x + 1] = WINPR_ASSERTING_INT_CAST(INT16, hd); |
81 | | |
82 | 0 | ll += subband_width; |
83 | 0 | hl += subband_width; |
84 | 0 | l_dst += total_width; |
85 | |
|
86 | 0 | lh += subband_width; |
87 | 0 | hh += subband_width; |
88 | 0 | h_dst += total_width; |
89 | 0 | } |
90 | | |
91 | | /* Inverse DWT in vertical direction, results are stored in original buffer. */ |
92 | 0 | for (size_t x = 0; x < total_width; x++) |
93 | 0 | { |
94 | 0 | const INT16* l = idwt + x; |
95 | 0 | const INT16* h = idwt + x + subband_width * total_width; |
96 | 0 | INT16* dst = buffer + x; |
97 | |
|
98 | 0 | const int dd = *l - ((*h * 2 + 1) >> 1); |
99 | 0 | *dst = WINPR_ASSERTING_INT_CAST(INT16, dd); |
100 | | |
101 | 0 | for (size_t n = 1; n < subband_width; n++) |
102 | 0 | { |
103 | 0 | l += total_width; |
104 | 0 | h += total_width; |
105 | | |
106 | | /* Even coefficients */ |
107 | 0 | const int d2 = *l - ((*(h - total_width) + *h + 1) >> 1); |
108 | 0 | dst[2 * total_width] = WINPR_ASSERTING_INT_CAST(INT16, d2); |
109 | | |
110 | | /* Odd coefficients */ |
111 | 0 | const int d = (*(h - total_width) << 1) + ((*dst + dst[2 * total_width]) >> 1); |
112 | 0 | dst[total_width] = WINPR_ASSERTING_INT_CAST(INT16, d); |
113 | | |
114 | 0 | dst += 2 * total_width; |
115 | 0 | } |
116 | | |
117 | 0 | const int d = (*h << 1) + ((*dst * 2) >> 1); |
118 | 0 | dst[total_width] = WINPR_ASSERTING_INT_CAST(INT16, d); |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | | void rfx_dwt_2d_decode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer) |
123 | 0 | { |
124 | 0 | WINPR_ASSERT(buffer); |
125 | 0 | WINPR_ASSERT(dwt_buffer); |
126 | | |
127 | 0 | rfx_dwt_2d_decode_block(&buffer[3840], dwt_buffer, 8); |
128 | 0 | rfx_dwt_2d_decode_block(&buffer[3072], dwt_buffer, 16); |
129 | 0 | rfx_dwt_2d_decode_block(&buffer[0], dwt_buffer, 32); |
130 | 0 | } |
131 | | |
132 | | static void rfx_dwt_2d_encode_block(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt, |
133 | | UINT32 subband_width) |
134 | 0 | { |
135 | 0 | INT16* src = NULL; |
136 | 0 | INT16* l = NULL; |
137 | 0 | INT16* h = NULL; |
138 | 0 | INT16* l_src = NULL; |
139 | 0 | INT16* h_src = NULL; |
140 | 0 | INT16* hl = NULL; |
141 | 0 | INT16* lh = NULL; |
142 | 0 | INT16* hh = NULL; |
143 | 0 | INT16* ll = NULL; |
144 | |
|
145 | 0 | const UINT32 total_width = subband_width << 1; |
146 | | |
147 | | /* DWT in vertical direction, results in 2 sub-bands in L, H order in tmp buffer dwt. */ |
148 | 0 | for (UINT32 x = 0; x < total_width; x++) |
149 | 0 | { |
150 | 0 | for (UINT32 n = 0; n < subband_width; n++) |
151 | 0 | { |
152 | 0 | UINT32 y = n << 1; |
153 | 0 | l = dwt + 1ULL * n * total_width + x; |
154 | 0 | h = l + 1ULL * subband_width * total_width; |
155 | 0 | src = buffer + 1ULL * y * total_width + x; |
156 | | |
157 | | /* H */ |
158 | 0 | *h = WINPR_ASSERTING_INT_CAST( |
159 | 0 | int16_t, (src[total_width] - |
160 | 0 | ((src[0] + src[n < subband_width - 1 ? 2 * total_width : 0]) >> 1)) >> |
161 | 0 | 1); |
162 | | |
163 | | /* L */ |
164 | 0 | *l = WINPR_ASSERTING_INT_CAST(int16_t, |
165 | 0 | src[0] + (n == 0 ? *h : (*(h - total_width) + *h) >> 1)); |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | | /* DWT in horizontal direction, results in 4 sub-bands in HL(0), LH(1), HH(2), LL(3) order, |
170 | | * stored in original buffer. */ |
171 | | /* The lower part L generates LL(3) and HL(0). */ |
172 | | /* The higher part H generates LH(1) and HH(2). */ |
173 | | |
174 | 0 | ll = buffer + 3ULL * subband_width * subband_width; |
175 | 0 | hl = buffer; |
176 | 0 | l_src = dwt; |
177 | |
|
178 | 0 | lh = buffer + 1ULL * subband_width * subband_width; |
179 | 0 | hh = buffer + 2ULL * subband_width * subband_width; |
180 | 0 | h_src = dwt + 2ULL * subband_width * subband_width; |
181 | |
|
182 | 0 | for (size_t y = 0; y < subband_width; y++) |
183 | 0 | { |
184 | | /* L */ |
185 | 0 | for (UINT32 n = 0; n < subband_width; n++) |
186 | 0 | { |
187 | 0 | UINT32 x = n << 1; |
188 | | |
189 | | /* HL */ |
190 | 0 | hl[n] = WINPR_ASSERTING_INT_CAST( |
191 | 0 | int16_t, |
192 | 0 | (l_src[x + 1] - ((l_src[x] + l_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1); |
193 | | /* LL */ |
194 | 0 | ll[n] = WINPR_ASSERTING_INT_CAST( |
195 | 0 | int16_t, l_src[x] + (n == 0 ? hl[n] : (hl[n - 1] + hl[n]) >> 1)); |
196 | 0 | } |
197 | | |
198 | | /* H */ |
199 | 0 | for (UINT32 n = 0; n < subband_width; n++) |
200 | 0 | { |
201 | 0 | UINT32 x = n << 1; |
202 | | |
203 | | /* HH */ |
204 | 0 | hh[n] = WINPR_ASSERTING_INT_CAST( |
205 | 0 | int16_t, |
206 | 0 | (h_src[x + 1] - ((h_src[x] + h_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1); |
207 | | /* LH */ |
208 | 0 | lh[n] = WINPR_ASSERTING_INT_CAST( |
209 | 0 | int16_t, h_src[x] + (n == 0 ? hh[n] : (hh[n - 1] + hh[n]) >> 1)); |
210 | 0 | } |
211 | | |
212 | 0 | ll += subband_width; |
213 | 0 | hl += subband_width; |
214 | 0 | l_src += total_width; |
215 | |
|
216 | 0 | lh += subband_width; |
217 | 0 | hh += subband_width; |
218 | 0 | h_src += total_width; |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | | void rfx_dwt_2d_encode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer) |
223 | 0 | { |
224 | 0 | WINPR_ASSERT(buffer); |
225 | 0 | WINPR_ASSERT(dwt_buffer); |
226 | | |
227 | 0 | rfx_dwt_2d_encode_block(&buffer[0], dwt_buffer, 32); |
228 | 0 | rfx_dwt_2d_encode_block(&buffer[3072], dwt_buffer, 16); |
229 | 0 | rfx_dwt_2d_encode_block(&buffer[3840], dwt_buffer, 8); |
230 | 0 | } |