Coverage Report

Created: 2025-09-27 07:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openjpeg/src/lib/openjp2/mct.c
Line
Count
Source
1
/*
2
 * The copyright in this software is being made available under the 2-clauses
3
 * BSD License, included below. This software may be subject to other third
4
 * party and contributor rights, including patent rights, and no such rights
5
 * are granted under this license.
6
 *
7
 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8
 * Copyright (c) 2002-2014, Professor Benoit Macq
9
 * Copyright (c) 2001-2003, David Janssens
10
 * Copyright (c) 2002-2003, Yannick Verschueren
11
 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12
 * Copyright (c) 2003-2014, Antonin Descampe
13
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14
 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
15
 * Copyright (c) 2012, CS Systemes d'Information, France
16
 * All rights reserved.
17
 *
18
 * Redistribution and use in source and binary forms, with or without
19
 * modification, are permitted provided that the following conditions
20
 * are met:
21
 * 1. Redistributions of source code must retain the above copyright
22
 *    notice, this list of conditions and the following disclaimer.
23
 * 2. Redistributions in binary form must reproduce the above copyright
24
 *    notice, this list of conditions and the following disclaimer in the
25
 *    documentation and/or other materials provided with the distribution.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37
 * POSSIBILITY OF SUCH DAMAGE.
38
 */
39
40
#ifdef __SSE__
41
#include <xmmintrin.h>
42
#endif
43
#ifdef __SSE2__
44
#include <emmintrin.h>
45
#endif
46
#ifdef __SSE4_1__
47
#include <smmintrin.h>
48
#endif
49
50
#include "opj_includes.h"
51
52
/* <summary> */
53
/* This table contains the norms of the basis function of the reversible MCT. */
54
/* </summary> */
55
static const OPJ_FLOAT64 opj_mct_norms[3] = { 1.732, .8292, .8292 };
56
57
/* <summary> */
58
/* This table contains the norms of the basis function of the irreversible MCT. */
59
/* </summary> */
60
static const OPJ_FLOAT64 opj_mct_norms_real[3] = { 1.732, 1.805, 1.573 };
61
62
const OPJ_FLOAT64 * opj_mct_get_mct_norms()
63
0
{
64
0
    return opj_mct_norms;
65
0
}
66
67
const OPJ_FLOAT64 * opj_mct_get_mct_norms_real()
68
0
{
69
0
    return opj_mct_norms_real;
70
0
}
71
72
/* <summary> */
73
/* Forward reversible MCT. */
74
/* </summary> */
75
#ifdef __SSE2__
76
void opj_mct_encode(
77
    OPJ_INT32* OPJ_RESTRICT c0,
78
    OPJ_INT32* OPJ_RESTRICT c1,
79
    OPJ_INT32* OPJ_RESTRICT c2,
80
    OPJ_SIZE_T n)
81
0
{
82
0
    OPJ_SIZE_T i;
83
0
    const OPJ_SIZE_T len = n;
84
    /* buffer are aligned on 16 bytes */
85
0
    assert(((size_t)c0 & 0xf) == 0);
86
0
    assert(((size_t)c1 & 0xf) == 0);
87
0
    assert(((size_t)c2 & 0xf) == 0);
88
89
0
    for (i = 0; i < (len & ~3U); i += 4) {
90
0
        __m128i y, u, v;
91
0
        __m128i r = _mm_load_si128((const __m128i *) & (c0[i]));
92
0
        __m128i g = _mm_load_si128((const __m128i *) & (c1[i]));
93
0
        __m128i b = _mm_load_si128((const __m128i *) & (c2[i]));
94
0
        y = _mm_add_epi32(g, g);
95
0
        y = _mm_add_epi32(y, b);
96
0
        y = _mm_add_epi32(y, r);
97
0
        y = _mm_srai_epi32(y, 2);
98
0
        u = _mm_sub_epi32(b, g);
99
0
        v = _mm_sub_epi32(r, g);
100
0
        _mm_store_si128((__m128i *) & (c0[i]), y);
101
0
        _mm_store_si128((__m128i *) & (c1[i]), u);
102
0
        _mm_store_si128((__m128i *) & (c2[i]), v);
103
0
    }
104
105
0
    for (; i < len; ++i) {
106
0
        OPJ_INT32 r = c0[i];
107
0
        OPJ_INT32 g = c1[i];
108
0
        OPJ_INT32 b = c2[i];
109
0
        OPJ_INT32 y = (r + (g * 2) + b) >> 2;
110
0
        OPJ_INT32 u = b - g;
111
0
        OPJ_INT32 v = r - g;
112
0
        c0[i] = y;
113
0
        c1[i] = u;
114
0
        c2[i] = v;
115
0
    }
116
0
}
117
#else
118
void opj_mct_encode(
119
    OPJ_INT32* OPJ_RESTRICT c0,
120
    OPJ_INT32* OPJ_RESTRICT c1,
121
    OPJ_INT32* OPJ_RESTRICT c2,
122
    OPJ_SIZE_T n)
123
{
124
    OPJ_SIZE_T i;
125
    const OPJ_SIZE_T len = n;
126
127
    for (i = 0; i < len; ++i) {
128
        OPJ_INT32 r = c0[i];
129
        OPJ_INT32 g = c1[i];
130
        OPJ_INT32 b = c2[i];
131
        OPJ_INT32 y = (r + (g * 2) + b) >> 2;
132
        OPJ_INT32 u = b - g;
133
        OPJ_INT32 v = r - g;
134
        c0[i] = y;
135
        c1[i] = u;
136
        c2[i] = v;
137
    }
138
}
139
#endif
140
141
/* <summary> */
142
/* Inverse reversible MCT. */
143
/* </summary> */
144
#ifdef __SSE2__
145
void opj_mct_decode(
146
    OPJ_INT32* OPJ_RESTRICT c0,
147
    OPJ_INT32* OPJ_RESTRICT c1,
148
    OPJ_INT32* OPJ_RESTRICT c2,
149
    OPJ_SIZE_T n)
150
13.0k
{
151
13.0k
    OPJ_SIZE_T i;
152
13.0k
    const OPJ_SIZE_T len = n;
153
154
2.69G
    for (i = 0; i < (len & ~3U); i += 4) {
155
2.69G
        __m128i r, g, b;
156
2.69G
        __m128i y = _mm_load_si128((const __m128i *) & (c0[i]));
157
2.69G
        __m128i u = _mm_load_si128((const __m128i *) & (c1[i]));
158
2.69G
        __m128i v = _mm_load_si128((const __m128i *) & (c2[i]));
159
2.69G
        g = y;
160
2.69G
        g = _mm_sub_epi32(g, _mm_srai_epi32(_mm_add_epi32(u, v), 2));
161
2.69G
        r = _mm_add_epi32(v, g);
162
2.69G
        b = _mm_add_epi32(u, g);
163
2.69G
        _mm_store_si128((__m128i *) & (c0[i]), r);
164
2.69G
        _mm_store_si128((__m128i *) & (c1[i]), g);
165
2.69G
        _mm_store_si128((__m128i *) & (c2[i]), b);
166
2.69G
    }
167
21.5k
    for (; i < len; ++i) {
168
8.42k
        OPJ_INT32 y = c0[i];
169
8.42k
        OPJ_INT32 u = c1[i];
170
8.42k
        OPJ_INT32 v = c2[i];
171
8.42k
        OPJ_INT32 g = y - ((u + v) >> 2);
172
8.42k
        OPJ_INT32 r = v + g;
173
8.42k
        OPJ_INT32 b = u + g;
174
8.42k
        c0[i] = r;
175
8.42k
        c1[i] = g;
176
8.42k
        c2[i] = b;
177
8.42k
    }
178
13.0k
}
179
#else
180
void opj_mct_decode(
181
    OPJ_INT32* OPJ_RESTRICT c0,
182
    OPJ_INT32* OPJ_RESTRICT c1,
183
    OPJ_INT32* OPJ_RESTRICT c2,
184
    OPJ_SIZE_T n)
185
{
186
    OPJ_SIZE_T i;
187
    for (i = 0; i < n; ++i) {
188
        OPJ_INT32 y = c0[i];
189
        OPJ_INT32 u = c1[i];
190
        OPJ_INT32 v = c2[i];
191
        OPJ_INT32 g = y - ((u + v) >> 2);
192
        OPJ_INT32 r = v + g;
193
        OPJ_INT32 b = u + g;
194
        c0[i] = r;
195
        c1[i] = g;
196
        c2[i] = b;
197
    }
198
}
199
#endif
200
201
/* <summary> */
202
/* Get norm of basis function of reversible MCT. */
203
/* </summary> */
204
OPJ_FLOAT64 opj_mct_getnorm(OPJ_UINT32 compno)
205
0
{
206
0
    return opj_mct_norms[compno];
207
0
}
208
209
/* <summary> */
210
/* Forward irreversible MCT. */
211
/* </summary> */
212
void opj_mct_encode_real(
213
    OPJ_FLOAT32* OPJ_RESTRICT c0,
214
    OPJ_FLOAT32* OPJ_RESTRICT c1,
215
    OPJ_FLOAT32* OPJ_RESTRICT c2,
216
    OPJ_SIZE_T n)
217
0
{
218
0
    OPJ_SIZE_T i;
219
0
#ifdef __SSE__
220
0
    const __m128 YR = _mm_set1_ps(0.299f);
221
0
    const __m128 YG = _mm_set1_ps(0.587f);
222
0
    const __m128 YB = _mm_set1_ps(0.114f);
223
0
    const __m128 UR = _mm_set1_ps(-0.16875f);
224
0
    const __m128 UG = _mm_set1_ps(-0.331260f);
225
0
    const __m128 UB = _mm_set1_ps(0.5f);
226
0
    const __m128 VR = _mm_set1_ps(0.5f);
227
0
    const __m128 VG = _mm_set1_ps(-0.41869f);
228
0
    const __m128 VB = _mm_set1_ps(-0.08131f);
229
0
    for (i = 0; i < (n >> 3); i ++) {
230
0
        __m128 r, g, b, y, u, v;
231
232
0
        r = _mm_load_ps(c0);
233
0
        g = _mm_load_ps(c1);
234
0
        b = _mm_load_ps(c2);
235
0
        y = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, YR), _mm_mul_ps(g, YG)),
236
0
                       _mm_mul_ps(b, YB));
237
0
        u = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, UR), _mm_mul_ps(g, UG)),
238
0
                       _mm_mul_ps(b, UB));
239
0
        v = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, VR), _mm_mul_ps(g, VG)),
240
0
                       _mm_mul_ps(b, VB));
241
0
        _mm_store_ps(c0, y);
242
0
        _mm_store_ps(c1, u);
243
0
        _mm_store_ps(c2, v);
244
0
        c0 += 4;
245
0
        c1 += 4;
246
0
        c2 += 4;
247
248
0
        r = _mm_load_ps(c0);
249
0
        g = _mm_load_ps(c1);
250
0
        b = _mm_load_ps(c2);
251
0
        y = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, YR), _mm_mul_ps(g, YG)),
252
0
                       _mm_mul_ps(b, YB));
253
0
        u = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, UR), _mm_mul_ps(g, UG)),
254
0
                       _mm_mul_ps(b, UB));
255
0
        v = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, VR), _mm_mul_ps(g, VG)),
256
0
                       _mm_mul_ps(b, VB));
257
0
        _mm_store_ps(c0, y);
258
0
        _mm_store_ps(c1, u);
259
0
        _mm_store_ps(c2, v);
260
0
        c0 += 4;
261
0
        c1 += 4;
262
0
        c2 += 4;
263
0
    }
264
0
    n &= 7;
265
0
#endif
266
0
    for (i = 0; i < n; ++i) {
267
0
        OPJ_FLOAT32 r = c0[i];
268
0
        OPJ_FLOAT32 g = c1[i];
269
0
        OPJ_FLOAT32 b = c2[i];
270
0
        OPJ_FLOAT32 y = 0.299f * r + 0.587f * g + 0.114f * b;
271
0
        OPJ_FLOAT32 u = -0.16875f * r - 0.331260f * g + 0.5f * b;
272
0
        OPJ_FLOAT32 v = 0.5f * r - 0.41869f * g - 0.08131f * b;
273
0
        c0[i] = y;
274
0
        c1[i] = u;
275
0
        c2[i] = v;
276
0
    }
277
0
}
278
279
/* <summary> */
280
/* Inverse irreversible MCT. */
281
/* </summary> */
282
void opj_mct_decode_real(
283
    OPJ_FLOAT32* OPJ_RESTRICT c0,
284
    OPJ_FLOAT32* OPJ_RESTRICT c1,
285
    OPJ_FLOAT32* OPJ_RESTRICT c2,
286
    OPJ_SIZE_T n)
287
99.7k
{
288
99.7k
    OPJ_SIZE_T i;
289
99.7k
#ifdef __SSE__
290
99.7k
    __m128 vrv, vgu, vgv, vbu;
291
99.7k
    vrv = _mm_set1_ps(1.402f);
292
99.7k
    vgu = _mm_set1_ps(0.34413f);
293
99.7k
    vgv = _mm_set1_ps(0.71414f);
294
99.7k
    vbu = _mm_set1_ps(1.772f);
295
349M
    for (i = 0; i < (n >> 3); ++i) {
296
349M
        __m128 vy, vu, vv;
297
349M
        __m128 vr, vg, vb;
298
299
349M
        vy = _mm_load_ps(c0);
300
349M
        vu = _mm_load_ps(c1);
301
349M
        vv = _mm_load_ps(c2);
302
349M
        vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
303
349M
        vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
304
349M
        vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
305
349M
        _mm_store_ps(c0, vr);
306
349M
        _mm_store_ps(c1, vg);
307
349M
        _mm_store_ps(c2, vb);
308
349M
        c0 += 4;
309
349M
        c1 += 4;
310
349M
        c2 += 4;
311
312
349M
        vy = _mm_load_ps(c0);
313
349M
        vu = _mm_load_ps(c1);
314
349M
        vv = _mm_load_ps(c2);
315
349M
        vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
316
349M
        vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
317
349M
        vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
318
349M
        _mm_store_ps(c0, vr);
319
349M
        _mm_store_ps(c1, vg);
320
349M
        _mm_store_ps(c2, vb);
321
349M
        c0 += 4;
322
349M
        c1 += 4;
323
349M
        c2 += 4;
324
349M
    }
325
99.7k
    n &= 7;
326
99.7k
#endif
327
157k
    for (i = 0; i < n; ++i) {
328
58.1k
        OPJ_FLOAT32 y = c0[i];
329
58.1k
        OPJ_FLOAT32 u = c1[i];
330
58.1k
        OPJ_FLOAT32 v = c2[i];
331
58.1k
        OPJ_FLOAT32 r = y + (v * 1.402f);
332
58.1k
        OPJ_FLOAT32 g = y - (u * 0.34413f) - (v * (0.71414f));
333
58.1k
        OPJ_FLOAT32 b = y + (u * 1.772f);
334
58.1k
        c0[i] = r;
335
58.1k
        c1[i] = g;
336
58.1k
        c2[i] = b;
337
58.1k
    }
338
99.7k
}
339
340
/* <summary> */
341
/* Get norm of basis function of irreversible MCT. */
342
/* </summary> */
343
OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno)
344
0
{
345
0
    return opj_mct_norms_real[compno];
346
0
}
347
348
349
OPJ_BOOL opj_mct_encode_custom(
350
    OPJ_BYTE * pCodingdata,
351
    OPJ_SIZE_T n,
352
    OPJ_BYTE ** pData,
353
    OPJ_UINT32 pNbComp,
354
    OPJ_UINT32 isSigned)
355
0
{
356
0
    OPJ_FLOAT32 * lMct = (OPJ_FLOAT32 *) pCodingdata;
357
0
    OPJ_SIZE_T i;
358
0
    OPJ_UINT32 j;
359
0
    OPJ_UINT32 k;
360
0
    OPJ_UINT32 lNbMatCoeff = pNbComp * pNbComp;
361
0
    OPJ_INT32 * lCurrentData = 00;
362
0
    OPJ_INT32 * lCurrentMatrix = 00;
363
0
    OPJ_INT32 ** lData = (OPJ_INT32 **) pData;
364
0
    OPJ_UINT32 lMultiplicator = 1 << 13;
365
0
    OPJ_INT32 * lMctPtr;
366
367
0
    OPJ_ARG_NOT_USED(isSigned);
368
369
0
    lCurrentData = (OPJ_INT32 *) opj_malloc((pNbComp + lNbMatCoeff) * sizeof(
370
0
            OPJ_INT32));
371
0
    if (! lCurrentData) {
372
0
        return OPJ_FALSE;
373
0
    }
374
375
0
    lCurrentMatrix = lCurrentData + pNbComp;
376
377
0
    for (i = 0; i < lNbMatCoeff; ++i) {
378
0
        lCurrentMatrix[i] = (OPJ_INT32)(*(lMct++) * (OPJ_FLOAT32)lMultiplicator);
379
0
    }
380
381
0
    for (i = 0; i < n; ++i)  {
382
0
        lMctPtr = lCurrentMatrix;
383
0
        for (j = 0; j < pNbComp; ++j) {
384
0
            lCurrentData[j] = (*(lData[j]));
385
0
        }
386
387
0
        for (j = 0; j < pNbComp; ++j) {
388
0
            *(lData[j]) = 0;
389
0
            for (k = 0; k < pNbComp; ++k) {
390
0
                *(lData[j]) += opj_int_fix_mul(*lMctPtr, lCurrentData[k]);
391
0
                ++lMctPtr;
392
0
            }
393
394
0
            ++lData[j];
395
0
        }
396
0
    }
397
398
0
    opj_free(lCurrentData);
399
400
0
    return OPJ_TRUE;
401
0
}
402
403
OPJ_BOOL opj_mct_decode_custom(
404
    OPJ_BYTE * pDecodingData,
405
    OPJ_SIZE_T n,
406
    OPJ_BYTE ** pData,
407
    OPJ_UINT32 pNbComp,
408
    OPJ_UINT32 isSigned)
409
0
{
410
0
    OPJ_FLOAT32 * lMct;
411
0
    OPJ_SIZE_T i;
412
0
    OPJ_UINT32 j;
413
0
    OPJ_UINT32 k;
414
415
0
    OPJ_FLOAT32 * lCurrentData = 00;
416
0
    OPJ_FLOAT32 * lCurrentResult = 00;
417
0
    OPJ_FLOAT32 ** lData = (OPJ_FLOAT32 **) pData;
418
419
0
    OPJ_ARG_NOT_USED(isSigned);
420
421
0
    lCurrentData = (OPJ_FLOAT32 *) opj_malloc(2 * pNbComp * sizeof(OPJ_FLOAT32));
422
0
    if (! lCurrentData) {
423
0
        return OPJ_FALSE;
424
0
    }
425
0
    lCurrentResult = lCurrentData + pNbComp;
426
427
0
    for (i = 0; i < n; ++i) {
428
0
        lMct = (OPJ_FLOAT32 *) pDecodingData;
429
0
        for (j = 0; j < pNbComp; ++j) {
430
0
            lCurrentData[j] = (OPJ_FLOAT32)(*(lData[j]));
431
0
        }
432
0
        for (j = 0; j < pNbComp; ++j) {
433
0
            lCurrentResult[j] = 0;
434
0
            for (k = 0; k < pNbComp; ++k) {
435
0
                lCurrentResult[j] += *(lMct++) * lCurrentData[k];
436
0
            }
437
0
            *(lData[j]++) = (OPJ_FLOAT32)(lCurrentResult[j]);
438
0
        }
439
0
    }
440
0
    opj_free(lCurrentData);
441
0
    return OPJ_TRUE;
442
0
}
443
444
void opj_calculate_norms(OPJ_FLOAT64 * pNorms,
445
                         OPJ_UINT32 pNbComps,
446
                         OPJ_FLOAT32 * pMatrix)
447
0
{
448
0
    OPJ_UINT32 i, j, lIndex;
449
0
    OPJ_FLOAT32 lCurrentValue;
450
0
    OPJ_FLOAT64 * lNorms = (OPJ_FLOAT64 *) pNorms;
451
0
    OPJ_FLOAT32 * lMatrix = (OPJ_FLOAT32 *) pMatrix;
452
453
0
    for (i = 0; i < pNbComps; ++i) {
454
0
        lNorms[i] = 0;
455
0
        lIndex = i;
456
457
0
        for (j = 0; j < pNbComps; ++j) {
458
0
            lCurrentValue = lMatrix[lIndex];
459
0
            lIndex += pNbComps;
460
0
            lNorms[i] += (OPJ_FLOAT64) lCurrentValue * lCurrentValue;
461
0
        }
462
0
        lNorms[i] = sqrt(lNorms[i]);
463
0
    }
464
0
}