Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rfxencode_dwt.c « src - github.com/neutrinolabs/librfxcodec.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5d820443fa32a55b6219a7f3862368f1f6c3a1a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
 * FreeRDP: A Remote Desktop Protocol client.
 * RemoteFX Codec Library - DWT
 *
 * Copyright 2011 Vic Lee
 * Copyright 2014-2017 Jay Sorg <jay.sorg@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "rfxcommon.h"

/******************************************************************************/
static int
rfx_dwt_2d_encode_horz(sint16 *buffer, sint16 *dwt, int subband_width)
{
    sint16 *l_src, *h_src;
    sint16 *hl, *lh, *hh, *ll;
    int x, y;
    int n;

    /* DWT in horizontal direction, results in 4 sub-bands in
     * HL(0), LH(1), HH(2), LL(3) order, stored in original buffer. */
    /* The lower part L generates LL(3) and HL(0). */
    /* The higher part H generates LH(1) and HH(2). */

    ll = buffer + subband_width * subband_width * 3;
    hl = buffer;
    l_src = dwt;

    lh = buffer + subband_width * subband_width;
    hh = buffer + subband_width * subband_width * 2;
    h_src = dwt + subband_width * subband_width * 2;

    for (y = 0; y < subband_width; y++)
    {

        /* pre */
        hl[0] = (l_src[1] - ((l_src[0] + l_src[2]) >> 1)) >> 1;
        ll[0] = l_src[0] + hl[0];

        /* loop */
        for (n = 1; n < subband_width - 1; n++)
        {
            x = n << 1;
            hl[n] = (l_src[x + 1] - ((l_src[x] + l_src[x + 2]) >> 1)) >> 1;
            ll[n] = l_src[x] + ((hl[n - 1] + hl[n]) >> 1);
        }

        /* post */
        n = subband_width - 1;
        x = n << 1;
        hl[n] = (l_src[x + 1] - ((l_src[x] + l_src[x]) >> 1)) >> 1;
        ll[n] = l_src[x] + ((hl[n - 1] + hl[n]) >> 1);

        /* pre */
        hh[0] = (h_src[1] - ((h_src[0] + h_src[2]) >> 1)) >> 1;
        lh[0] = h_src[0] + hh[0];

        /* loop */
        for (n = 1; n < subband_width - 1; n++)
        {
            x = n << 1;
            hh[n] = (h_src[x + 1] - ((h_src[x] + h_src[x + 2]) >> 1)) >> 1;
            lh[n] = h_src[x] + ((hh[n - 1] + hh[n]) >> 1);
        }

        /* post */
        n = subband_width - 1;
        x = n << 1;
        hh[n] = (h_src[x + 1] - ((h_src[x] + h_src[x]) >> 1)) >> 1;
        lh[n] = h_src[x] + ((hh[n - 1] + hh[n]) >> 1);

        ll += subband_width;
        hl += subband_width;
        l_src += subband_width << 1;

        lh += subband_width;
        hh += subband_width;
        h_src += subband_width << 1;
    }
    return 0;
}

/******************************************************************************/
static int
rfx_dwt_2d_encode_block(sint16 *buffer, sint16 *dwt, int subband_width)
{
    sint16 *src, *l, *h;
    int total_width;
    int x, y;
    int n;

    total_width = subband_width << 1;

    /* DWT in vertical direction, results in 2 sub-bands in L, H order in
     * tmp buffer dwt. */
    for (x = 0; x < total_width; x++)
    {

        /* pre */
        l = dwt + x;
        h = l + subband_width * total_width;
        src = buffer + x;
        *h = (src[total_width] - ((src[0] + src[2 * total_width]) >> 1)) >> 1;
        *l = src[0] + (*h);

        /* loop */
        for (n = 1; n < subband_width - 1; n++)
        {
            y = n << 1;
            l = dwt + n * total_width + x;
            h = l + subband_width * total_width;
            src = buffer + y * total_width + x;
            *h = (src[total_width] - ((src[0] + src[2 * total_width]) >> 1)) >> 1;
            *l = src[0] + ((*(h - total_width) + *h) >> 1);
        }

        /* post */
        n = subband_width - 1;
        y = n << 1;
        l = dwt + n * total_width + x;
        h = l + subband_width * total_width;
        src = buffer + y * total_width + x;
        *h = (src[total_width] - ((src[0] + src[0]) >> 1)) >> 1;
        *l = src[0] + ((*(h - total_width) + *h) >> 1);

    }

    return rfx_dwt_2d_encode_horz(buffer, dwt, subband_width);
}

/******************************************************************************/
static int
rfx_dwt_2d_encode_block8(const uint8 *in_buffer,
                         sint16 *buffer, sint16 *dwt, int subband_width)
{
    const uint8 *src;
    sint16 *l, *h;
    sint16 s1, s2, s3;
    int total_width;
    int x, y;
    int n;

    total_width = subband_width << 1;

    /* DWT in vertical direction, results in 2 sub-bands in L, H order in
     * tmp buffer dwt. */
    for (x = 0; x < total_width; x++)
    {

        /* pre */
        l = dwt + x;
        h = l + subband_width * total_width;
        src = in_buffer + x;
        s1 = (src[total_width] - 128) << DWT_FACTOR;
        s2 = (src[0] - 128) << DWT_FACTOR;
        s3 = (src[2 * total_width] - 128) << DWT_FACTOR;
        *h = (s1 - ((s2 + s3) >> 1)) >> 1;
        s1 = (src[0] - 128) << DWT_FACTOR;
        *l = s1 + *h;

        /* loop */
        for (n = 1; n < subband_width - 1; n++)
        {
            y = n << 1;
            l = dwt + n * total_width + x;
            h = l + subband_width * total_width;
            src = in_buffer + y * total_width + x;
            s1 = (src[total_width] - 128) << DWT_FACTOR;
            s2 = (src[0] - 128) << DWT_FACTOR;
            s3 = (src[2 * total_width] - 128) << DWT_FACTOR;
            *h = (s1 - ((s2 + s3) >> 1)) >> 1;
            s1 = (src[0] - 128) << DWT_FACTOR;
            *l = s1 + ((*(h - total_width) + *h) >> 1);
        }

        /* post */
        n = subband_width - 1;
        y = n << 1;
        l = dwt + n * total_width + x;
        h = l + subband_width * total_width;
        src = in_buffer + y * total_width + x;
        s1 = (src[total_width] - 128) << DWT_FACTOR;
        s2 = (src[0] - 128) << DWT_FACTOR;
        s3 = (src[0] - 128) << DWT_FACTOR;
        *h = (s1 - ((s2 + s3) >> 1)) >> 1;
        s1 = (src[0] - 128) << DWT_FACTOR;
        *l = s1 + ((*(h - total_width) + *h) >> 1);

    }

    return rfx_dwt_2d_encode_horz(buffer, dwt, subband_width);
}

/******************************************************************************/
int
rfx_dwt_2d_encode(const uint8 *in_buffer, sint16 *buffer, sint16 *dwt_buffer)
{
    rfx_dwt_2d_encode_block8(in_buffer, buffer, dwt_buffer, 32);
    rfx_dwt_2d_encode_block(buffer + 3072, dwt_buffer, 16);
    rfx_dwt_2d_encode_block(buffer + 3840, dwt_buffer, 8);
    return 0;
}