Coverage Report

Created: 2023-09-25 06:56

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