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

rfxencode_diff_rlgr3.c « src - github.com/neutrinolabs/librfxcodec.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b68db3247a2b1e89aea47d6ee8d3ef41946acb2 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
 * FreeRDP: A Remote Desktop Protocol client.
 * RemoteFX Codec Library - RLGR
 *
 * Copyright 2011 Vic Lee
 * Copyright 2016-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.
 */

/**
 * This implementation of RLGR refers to
 * [MS-RDPRFX] 3.1.8.1.7.3 RLGR1/RLGR3 Pseudocode
 */

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

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

#include "rfxcommon.h"

#define PIXELS_IN_TILE 4096

/* Constants used within the RLGR1/RLGR3 algorithm */
#define KPMAX   (80)  /* max value for kp or krp */
#define LSGR    (3)   /* shift count to convert kp to k */
#define UP_GR   (4)   /* increase in kp after a zero run in RL mode */
#define DN_GR   (6)   /* decrease in kp after a nonzero symbol in RL mode */
#define UQ_GR   (3)   /* increase in kp after nonzero symbol in GR mode */
#define DQ_GR   (3)   /* decrease in kp after zero symbol in GR mode */

#define GetNextInput do { \
    input = *coef; \
    coef++; \
    coef_size--; \
} while (0)

#define CheckWrite do { \
    while (bit_count >= 8) \
    { \
        bit_count -= 8; \
        *cdata = bits >> bit_count; \
        cdata++; \
    } \
} while (0)

/* output GR code for (mag - 1) */
#define CodeGR(_krp, _lmag) do { \
    int lkr = _krp >> LSGR; \
    /* unary part of GR code */ \
    int lvk = _lmag >> lkr; \
    int llvk = lvk; \
    while (llvk >= 8) \
    { \
        bits <<= 8; \
        bits |= 0xFF; \
        llvk -= 8; \
        *cdata = bits >> bit_count; \
        cdata++; \
    } \
    bits <<= llvk; \
    bits |= (1 << llvk) - 1; \
    bit_count += llvk; \
    bits <<= 1; \
    bit_count++; \
    CheckWrite; \
    /* remainder part of GR code, if needed */ \
    if (lkr) \
    { \
        bits <<= lkr; \
        bits |= _lmag & ((1 << lkr) - 1); \
        bit_count += lkr; \
    } \
    /* update _krp, only if it is not equal to 1 */ \
    if (lvk == 0) \
    { \
        _krp = MAX(0, _krp - 2); \
    } \
    else if (lvk > 1) \
    { \
        _krp = MIN(KPMAX, _krp + lvk); \
    } \
} while (0)

int
rfx_encode_diff_rlgr3(sint16 *coef, uint8 *cdata, int cdata_size)
{
    int k;
    int kp;
    int krp;

    int input;
    int numZeros;
    int runmax;
    int mag;
    int sign;
    int processed_size;
    int lmag;
    int coef_size;
    int y;

    int bit_count;
    unsigned int bits;
    uint8 *cdata_org;

    uint32 twoMs1;
    uint32 twoMs2;
    uint32 sum2Ms;
    uint32 nIdx;

    /* the last 64 bytes are diff */
    for (k = PIXELS_IN_TILE - 1; k > PIXELS_IN_TILE - 64; k--)
    {
        coef[k] -= coef[k - 1];
    }

    /* initialize the parameters */
    k = 1;
    kp = 1 << LSGR;
    krp = 1 << LSGR;

    bit_count = 0;
    bits = 0;
    cdata_org = cdata;

    /* process all the input coefficients */
    coef_size = PIXELS_IN_TILE;
    while (coef_size > 0)
    {
        if (k)
        {

            /* RUN-LENGTH MODE */

            /* collect the run of zeros in the input stream */
            numZeros = 0;

            GetNextInput;
            while (input == 0 && coef_size > 0)
            {
                numZeros++;
                GetNextInput;
            }

            /* emit output zeros */
            runmax = 1 << k;
            while (numZeros >= runmax)
            {

                bits <<= 1;
                bit_count++;

                CheckWrite;

                numZeros -= runmax;

                kp = MIN(KPMAX, kp + UP_GR);
                k = kp >> LSGR;

                runmax = 1 << k;
            }

            /* output a 1 to terminate runs */
            bits <<= 1;
            bits |= 1;
            bit_count++;

            /* output the remaining run length using k bits */
            bits <<= k;
            bits |= numZeros;
            bit_count += k;

            CheckWrite;

            /* encode the nonzero value using GR coding */
            if (input < 0)
            {
                mag = -input;
                sign = 1;
            }
            else
            {
                mag = input;
                sign = 0;
            }

            bits <<= 1;
            bits |= sign;
            bit_count++;

            lmag = mag ? mag - 1 : 0;

            CodeGR(krp, lmag); /* output GR code for (mag - 1) */
            CheckWrite;

            kp = MAX(0, kp - DN_GR);
            k = kp >> LSGR;

        }
        else
        {

            /* GOLOMB-RICE MODE */

            /* RLGR3 variant */

            /* convert the next two input values to (2*magnitude - sign) and */
            /* encode their sum using GR code */

            GetNextInput;

            y = input >> 15;
            twoMs1 = (((input ^ y) - y) << 1) + y;

            GetNextInput;

            y = input >> 15;
            twoMs2 = (((input ^ y) - y) << 1) + y;

            sum2Ms = twoMs1 + twoMs2;

            CodeGR(krp, sum2Ms);

            CheckWrite;

            /* encode binary representation of the first input (twoMs1). */
            if (sum2Ms != 0)
            {
                GBSR(sum2Ms, nIdx);
                nIdx++;
            }
            else
            {
                nIdx = 0;
            }

            bits <<= nIdx;
            bits |= twoMs1;
            bit_count += nIdx;

            CheckWrite;

            /* update k,kp for the two input values */
            if (twoMs1 != 0)
            {
                if (twoMs2 != 0)
                {
                    kp = MAX(0, kp - (2 * DQ_GR));
                    k = kp >> LSGR;
                }
            }
            else
            {
                if (twoMs2 == 0)
                {
                    kp = MIN(KPMAX, kp + (2 * UQ_GR));
                    k = kp >> LSGR;
                }
            }
        }
    }

    if (bit_count > 0)
    {
        bits <<= 8 - bit_count;
        *cdata = bits;
        cdata++;
        bit_count = 0;
    }

    processed_size = cdata - cdata_org;

    return processed_size;
}