Coverage Report

Created: 2024-05-20 06:11

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