FreeRDP
rfx_dwt.c
1 
20 #include <freerdp/config.h>
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "rfx_dwt.h"
27 
28 static INLINE void rfx_dwt_2d_decode_block(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT idwt,
29  size_t subband_width)
30 {
31  const size_t total_width = subband_width << 1;
32 
33  /* Inverse DWT in horizontal direction, results in 2 sub-bands in L, H order in tmp buffer idwt.
34  */
35  /* The 4 sub-bands are stored in HL(0), LH(1), HH(2), LL(3) order. */
36  /* The lower part L uses LL(3) and HL(0). */
37  /* The higher part H uses LH(1) and HH(2). */
38 
39  const INT16* ll = buffer + subband_width * subband_width * 3;
40  const INT16* hl = buffer;
41  INT16* l_dst = idwt;
42 
43  const INT16* lh = buffer + subband_width * subband_width;
44  const INT16* hh = buffer + subband_width * subband_width * 2;
45  INT16* h_dst = idwt + subband_width * subband_width * 2;
46 
47  for (size_t y = 0; y < subband_width; y++)
48  {
49  /* Even coefficients */
50  l_dst[0] = ll[0] - ((hl[0] + hl[0] + 1) >> 1);
51  h_dst[0] = lh[0] - ((hh[0] + hh[0] + 1) >> 1);
52  for (size_t n = 1; n < subband_width; n++)
53  {
54  const size_t x = n << 1;
55  l_dst[x] = ll[n] - ((hl[n - 1] + hl[n] + 1) >> 1);
56  h_dst[x] = lh[n] - ((hh[n - 1] + hh[n] + 1) >> 1);
57  }
58 
59  /* Odd coefficients */
60  size_t n = 0;
61  for (; n < subband_width - 1; n++)
62  {
63  const size_t x = n << 1;
64  l_dst[x + 1] = (hl[n] << 1) + ((l_dst[x] + l_dst[x + 2]) >> 1);
65  h_dst[x + 1] = (hh[n] << 1) + ((h_dst[x] + h_dst[x + 2]) >> 1);
66  }
67 
68  const size_t x = n << 1;
69  l_dst[x + 1] = (hl[n] << 1) + (l_dst[x]);
70  h_dst[x + 1] = (hh[n] << 1) + (h_dst[x]);
71 
72  ll += subband_width;
73  hl += subband_width;
74  l_dst += total_width;
75 
76  lh += subband_width;
77  hh += subband_width;
78  h_dst += total_width;
79  }
80 
81  /* Inverse DWT in vertical direction, results are stored in original buffer. */
82  for (size_t x = 0; x < total_width; x++)
83  {
84  const INT16* l = idwt + x;
85  const INT16* h = idwt + x + subband_width * total_width;
86  INT16* dst = buffer + x;
87 
88  *dst = *l - ((*h * 2 + 1) >> 1);
89 
90  for (size_t n = 1; n < subband_width; n++)
91  {
92  l += total_width;
93  h += total_width;
94 
95  /* Even coefficients */
96  dst[2 * total_width] = *l - ((*(h - total_width) + *h + 1) >> 1);
97 
98  /* Odd coefficients */
99  dst[total_width] = (*(h - total_width) << 1) + ((*dst + dst[2 * total_width]) >> 1);
100 
101  dst += 2 * total_width;
102  }
103 
104  dst[total_width] = (*h << 1) + ((*dst * 2) >> 1);
105  }
106 }
107 
108 void rfx_dwt_2d_decode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer)
109 {
110  WINPR_ASSERT(buffer);
111  WINPR_ASSERT(dwt_buffer);
112 
113  rfx_dwt_2d_decode_block(&buffer[3840], dwt_buffer, 8);
114  rfx_dwt_2d_decode_block(&buffer[3072], dwt_buffer, 16);
115  rfx_dwt_2d_decode_block(&buffer[0], dwt_buffer, 32);
116 }
117 
118 static void rfx_dwt_2d_encode_block(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt,
119  UINT32 subband_width)
120 {
121  INT16* src = NULL;
122  INT16* l = NULL;
123  INT16* h = NULL;
124  INT16* l_src = NULL;
125  INT16* h_src = NULL;
126  INT16* hl = NULL;
127  INT16* lh = NULL;
128  INT16* hh = NULL;
129  INT16* ll = NULL;
130 
131  const UINT32 total_width = subband_width << 1;
132 
133  /* DWT in vertical direction, results in 2 sub-bands in L, H order in tmp buffer dwt. */
134  for (UINT32 x = 0; x < total_width; x++)
135  {
136  for (UINT32 n = 0; n < subband_width; n++)
137  {
138  UINT32 y = n << 1;
139  l = dwt + 1ULL * n * total_width + x;
140  h = l + 1ULL * subband_width * total_width;
141  src = buffer + 1ULL * y * total_width + x;
142 
143  /* H */
144  *h = (src[total_width] -
145  ((src[0] + src[n < subband_width - 1 ? 2 * total_width : 0]) >> 1)) >>
146  1;
147 
148  /* L */
149  *l = src[0] + (n == 0 ? *h : (*(h - total_width) + *h) >> 1);
150  }
151  }
152 
153  /* DWT in horizontal direction, results in 4 sub-bands in HL(0), LH(1), HH(2), LL(3) order,
154  * stored in original buffer. */
155  /* The lower part L generates LL(3) and HL(0). */
156  /* The higher part H generates LH(1) and HH(2). */
157 
158  ll = buffer + 3ULL * subband_width * subband_width;
159  hl = buffer;
160  l_src = dwt;
161 
162  lh = buffer + 1ULL * subband_width * subband_width;
163  hh = buffer + 2ULL * subband_width * subband_width;
164  h_src = dwt + 2ULL * subband_width * subband_width;
165 
166  for (size_t y = 0; y < subband_width; y++)
167  {
168  /* L */
169  for (UINT32 n = 0; n < subband_width; n++)
170  {
171  UINT32 x = n << 1;
172 
173  /* HL */
174  hl[n] =
175  (l_src[x + 1] - ((l_src[x] + l_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1;
176  /* LL */
177  ll[n] = l_src[x] + (n == 0 ? hl[n] : (hl[n - 1] + hl[n]) >> 1);
178  }
179 
180  /* H */
181  for (UINT32 n = 0; n < subband_width; n++)
182  {
183  UINT32 x = n << 1;
184 
185  /* HH */
186  hh[n] =
187  (h_src[x + 1] - ((h_src[x] + h_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1;
188  /* LH */
189  lh[n] = h_src[x] + (n == 0 ? hh[n] : (hh[n - 1] + hh[n]) >> 1);
190  }
191 
192  ll += subband_width;
193  hl += subband_width;
194  l_src += total_width;
195 
196  lh += subband_width;
197  hh += subband_width;
198  h_src += total_width;
199  }
200 }
201 
202 void rfx_dwt_2d_encode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer)
203 {
204  WINPR_ASSERT(buffer);
205  WINPR_ASSERT(dwt_buffer);
206 
207  rfx_dwt_2d_encode_block(&buffer[0], dwt_buffer, 32);
208  rfx_dwt_2d_encode_block(&buffer[3072], dwt_buffer, 16);
209  rfx_dwt_2d_encode_block(&buffer[3840], dwt_buffer, 8);
210 }