Coverage Report

Created: 2024-09-08 06:18

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