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.
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.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
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.
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.
41 #include <xmmintrin.h>
44 #include <emmintrin.h>
47 #include <smmintrin.h>
50 #include "opj_includes.h"
53 /* This table contains the norms of the basis function of the reversible MCT. */
55 static const OPJ_FLOAT64 opj_mct_norms[3] = { 1.732, .8292, .8292 };
58 /* This table contains the norms of the basis function of the irreversible MCT. */
60 static const OPJ_FLOAT64 opj_mct_norms_real[3] = { 1.732, 1.805, 1.573 };
62 const OPJ_FLOAT64 * opj_mct_get_mct_norms ()
67 const OPJ_FLOAT64 * opj_mct_get_mct_norms_real ()
69 return opj_mct_norms_real;
73 /* Foward reversible MCT. */
77 OPJ_INT32* restrict c0,
78 OPJ_INT32* restrict c1,
79 OPJ_INT32* restrict c2,
83 const OPJ_SIZE_T len = n;
85 for(i = 0; i < (len & ~3U); i += 4) {
87 __m128i r = _mm_load_si128((const __m128i *)&(c0[i]));
88 __m128i g = _mm_load_si128((const __m128i *)&(c1[i]));
89 __m128i b = _mm_load_si128((const __m128i *)&(c2[i]));
90 y = _mm_add_epi32(g, g);
91 y = _mm_add_epi32(y, b);
92 y = _mm_add_epi32(y, r);
93 y = _mm_srai_epi32(y, 2);
94 u = _mm_sub_epi32(b, g);
95 v = _mm_sub_epi32(r, g);
96 _mm_store_si128((__m128i *)&(c0[i]), y);
97 _mm_store_si128((__m128i *)&(c1[i]), u);
98 _mm_store_si128((__m128i *)&(c2[i]), v);
101 for(; i < len; ++i) {
105 OPJ_INT32 y = (r + (g * 2) + b) >> 2;
115 OPJ_INT32* restrict c0,
116 OPJ_INT32* restrict c1,
117 OPJ_INT32* restrict c2,
121 const OPJ_SIZE_T len = n;
123 for(i = 0; i < len; ++i) {
127 OPJ_INT32 y = (r + (g * 2) + b) >> 2;
138 /* Inverse reversible MCT. */
142 OPJ_INT32* restrict c0,
143 OPJ_INT32* restrict c1,
144 OPJ_INT32* restrict c2,
148 const OPJ_SIZE_T len = n;
150 for(i = 0; i < (len & ~3U); i += 4) {
152 __m128i y = _mm_load_si128((const __m128i *)&(c0[i]));
153 __m128i u = _mm_load_si128((const __m128i *)&(c1[i]));
154 __m128i v = _mm_load_si128((const __m128i *)&(c2[i]));
156 g = _mm_sub_epi32(g, _mm_srai_epi32(_mm_add_epi32(u, v), 2));
157 r = _mm_add_epi32(v, g);
158 b = _mm_add_epi32(u, g);
159 _mm_store_si128((__m128i *)&(c0[i]), r);
160 _mm_store_si128((__m128i *)&(c1[i]), g);
161 _mm_store_si128((__m128i *)&(c2[i]), b);
163 for (; i < len; ++i) {
167 OPJ_INT32 g = y - ((u + v) >> 2);
177 OPJ_INT32* restrict c0,
178 OPJ_INT32* restrict c1,
179 OPJ_INT32* restrict c2,
183 for (i = 0; i < n; ++i) {
187 OPJ_INT32 g = y - ((u + v) >> 2);
198 /* Get norm of basis function of reversible MCT. */
200 OPJ_FLOAT64 opj_mct_getnorm(OPJ_UINT32 compno) {
201 return opj_mct_norms[compno];
205 /* Foward irreversible MCT. */
208 void opj_mct_encode_real(
209 OPJ_INT32* restrict c0,
210 OPJ_INT32* restrict c1,
211 OPJ_INT32* restrict c2,
215 const OPJ_SIZE_T len = n;
217 const __m128i ry = _mm_set1_epi32(2449);
218 const __m128i gy = _mm_set1_epi32(4809);
219 const __m128i by = _mm_set1_epi32(934);
220 const __m128i ru = _mm_set1_epi32(1382);
221 const __m128i gu = _mm_set1_epi32(2714);
222 /* const __m128i bu = _mm_set1_epi32(4096); */
223 /* const __m128i rv = _mm_set1_epi32(4096); */
224 const __m128i gv = _mm_set1_epi32(3430);
225 const __m128i bv = _mm_set1_epi32(666);
226 const __m128i mulround = _mm_shuffle_epi32(_mm_cvtsi32_si128(4096), _MM_SHUFFLE(1, 0, 1, 0));
228 for(i = 0; i < (len & ~3U); i += 4) {
231 __m128i r = _mm_load_si128((const __m128i *)&(c0[i]));
232 __m128i g = _mm_load_si128((const __m128i *)&(c1[i]));
233 __m128i b = _mm_load_si128((const __m128i *)&(c2[i]));
236 hi = _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 3, 1, 1));
237 lo = _mm_mul_epi32(lo, ry);
238 hi = _mm_mul_epi32(hi, ry);
239 lo = _mm_add_epi64(lo, mulround);
240 hi = _mm_add_epi64(hi, mulround);
241 lo = _mm_srli_epi64(lo, 13);
242 hi = _mm_slli_epi64(hi, 32-13);
243 y = _mm_blend_epi16(lo, hi, 0xCC);
246 hi = _mm_shuffle_epi32(g, _MM_SHUFFLE(3, 3, 1, 1));
247 lo = _mm_mul_epi32(lo, gy);
248 hi = _mm_mul_epi32(hi, gy);
249 lo = _mm_add_epi64(lo, mulround);
250 hi = _mm_add_epi64(hi, mulround);
251 lo = _mm_srli_epi64(lo, 13);
252 hi = _mm_slli_epi64(hi, 32-13);
253 y = _mm_add_epi32(y, _mm_blend_epi16(lo, hi, 0xCC));
256 hi = _mm_shuffle_epi32(b, _MM_SHUFFLE(3, 3, 1, 1));
257 lo = _mm_mul_epi32(lo, by);
258 hi = _mm_mul_epi32(hi, by);
259 lo = _mm_add_epi64(lo, mulround);
260 hi = _mm_add_epi64(hi, mulround);
261 lo = _mm_srli_epi64(lo, 13);
262 hi = _mm_slli_epi64(hi, 32-13);
263 y = _mm_add_epi32(y, _mm_blend_epi16(lo, hi, 0xCC));
264 _mm_store_si128((__m128i *)&(c0[i]), y);
267 hi = _mm_shuffle_epi32(b, _MM_SHUFFLE(3, 3, 1, 1));
268 lo = _mm_mul_epi32(lo, mulround);
269 hi = _mm_mul_epi32(hi, mulround);*/
270 lo = _mm_cvtepi32_epi64(_mm_shuffle_epi32(b, _MM_SHUFFLE(3, 2, 2, 0)));
271 hi = _mm_cvtepi32_epi64(_mm_shuffle_epi32(b, _MM_SHUFFLE(3, 2, 3, 1)));
272 lo = _mm_slli_epi64(lo, 12);
273 hi = _mm_slli_epi64(hi, 12);
274 lo = _mm_add_epi64(lo, mulround);
275 hi = _mm_add_epi64(hi, mulround);
276 lo = _mm_srli_epi64(lo, 13);
277 hi = _mm_slli_epi64(hi, 32-13);
278 u = _mm_blend_epi16(lo, hi, 0xCC);
281 hi = _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 3, 1, 1));
282 lo = _mm_mul_epi32(lo, ru);
283 hi = _mm_mul_epi32(hi, ru);
284 lo = _mm_add_epi64(lo, mulround);
285 hi = _mm_add_epi64(hi, mulround);
286 lo = _mm_srli_epi64(lo, 13);
287 hi = _mm_slli_epi64(hi, 32-13);
288 u = _mm_sub_epi32(u, _mm_blend_epi16(lo, hi, 0xCC));
291 hi = _mm_shuffle_epi32(g, _MM_SHUFFLE(3, 3, 1, 1));
292 lo = _mm_mul_epi32(lo, gu);
293 hi = _mm_mul_epi32(hi, gu);
294 lo = _mm_add_epi64(lo, mulround);
295 hi = _mm_add_epi64(hi, mulround);
296 lo = _mm_srli_epi64(lo, 13);
297 hi = _mm_slli_epi64(hi, 32-13);
298 u = _mm_sub_epi32(u, _mm_blend_epi16(lo, hi, 0xCC));
299 _mm_store_si128((__m128i *)&(c1[i]), u);
302 hi = _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 3, 1, 1));
303 lo = _mm_mul_epi32(lo, mulround);
304 hi = _mm_mul_epi32(hi, mulround);*/
305 lo = _mm_cvtepi32_epi64(_mm_shuffle_epi32(r, _MM_SHUFFLE(3, 2, 2, 0)));
306 hi = _mm_cvtepi32_epi64(_mm_shuffle_epi32(r, _MM_SHUFFLE(3, 2, 3, 1)));
307 lo = _mm_slli_epi64(lo, 12);
308 hi = _mm_slli_epi64(hi, 12);
309 lo = _mm_add_epi64(lo, mulround);
310 hi = _mm_add_epi64(hi, mulround);
311 lo = _mm_srli_epi64(lo, 13);
312 hi = _mm_slli_epi64(hi, 32-13);
313 v = _mm_blend_epi16(lo, hi, 0xCC);
316 hi = _mm_shuffle_epi32(g, _MM_SHUFFLE(3, 3, 1, 1));
317 lo = _mm_mul_epi32(lo, gv);
318 hi = _mm_mul_epi32(hi, gv);
319 lo = _mm_add_epi64(lo, mulround);
320 hi = _mm_add_epi64(hi, mulround);
321 lo = _mm_srli_epi64(lo, 13);
322 hi = _mm_slli_epi64(hi, 32-13);
323 v = _mm_sub_epi32(v, _mm_blend_epi16(lo, hi, 0xCC));
326 hi = _mm_shuffle_epi32(b, _MM_SHUFFLE(3, 3, 1, 1));
327 lo = _mm_mul_epi32(lo, bv);
328 hi = _mm_mul_epi32(hi, bv);
329 lo = _mm_add_epi64(lo, mulround);
330 hi = _mm_add_epi64(hi, mulround);
331 lo = _mm_srli_epi64(lo, 13);
332 hi = _mm_slli_epi64(hi, 32-13);
333 v = _mm_sub_epi32(v, _mm_blend_epi16(lo, hi, 0xCC));
334 _mm_store_si128((__m128i *)&(c2[i]), v);
336 for(; i < len; ++i) {
340 OPJ_INT32 y = opj_int_fix_mul(r, 2449) + opj_int_fix_mul(g, 4809) + opj_int_fix_mul(b, 934);
341 OPJ_INT32 u = -opj_int_fix_mul(r, 1382) - opj_int_fix_mul(g, 2714) + opj_int_fix_mul(b, 4096);
342 OPJ_INT32 v = opj_int_fix_mul(r, 4096) - opj_int_fix_mul(g, 3430) - opj_int_fix_mul(b, 666);
349 void opj_mct_encode_real(
350 OPJ_INT32* restrict c0,
351 OPJ_INT32* restrict c1,
352 OPJ_INT32* restrict c2,
356 for(i = 0; i < n; ++i) {
360 OPJ_INT32 y = opj_int_fix_mul(r, 2449) + opj_int_fix_mul(g, 4809) + opj_int_fix_mul(b, 934);
361 OPJ_INT32 u = -opj_int_fix_mul(r, 1382) - opj_int_fix_mul(g, 2714) + opj_int_fix_mul(b, 4096);
362 OPJ_INT32 v = opj_int_fix_mul(r, 4096) - opj_int_fix_mul(g, 3430) - opj_int_fix_mul(b, 666);
371 /* Inverse irreversible MCT. */
373 void opj_mct_decode_real(
374 OPJ_FLOAT32* restrict c0,
375 OPJ_FLOAT32* restrict c1,
376 OPJ_FLOAT32* restrict c2,
381 __m128 vrv, vgu, vgv, vbu;
382 vrv = _mm_set1_ps(1.402f);
383 vgu = _mm_set1_ps(0.34413f);
384 vgv = _mm_set1_ps(0.71414f);
385 vbu = _mm_set1_ps(1.772f);
386 for (i = 0; i < (n >> 3); ++i) {
390 vy = _mm_load_ps(c0);
391 vu = _mm_load_ps(c1);
392 vv = _mm_load_ps(c2);
393 vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
394 vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
395 vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
396 _mm_store_ps(c0, vr);
397 _mm_store_ps(c1, vg);
398 _mm_store_ps(c2, vb);
403 vy = _mm_load_ps(c0);
404 vu = _mm_load_ps(c1);
405 vv = _mm_load_ps(c2);
406 vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
407 vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
408 vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
409 _mm_store_ps(c0, vr);
410 _mm_store_ps(c1, vg);
411 _mm_store_ps(c2, vb);
418 for(i = 0; i < n; ++i) {
419 OPJ_FLOAT32 y = c0[i];
420 OPJ_FLOAT32 u = c1[i];
421 OPJ_FLOAT32 v = c2[i];
422 OPJ_FLOAT32 r = y + (v * 1.402f);
423 OPJ_FLOAT32 g = y - (u * 0.34413f) - (v * (0.71414f));
424 OPJ_FLOAT32 b = y + (u * 1.772f);
432 /* Get norm of basis function of irreversible MCT. */
434 OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno) {
435 return opj_mct_norms_real[compno];
439 OPJ_BOOL opj_mct_encode_custom(
440 OPJ_BYTE * pCodingdata,
446 OPJ_FLOAT32 * lMct = (OPJ_FLOAT32 *) pCodingdata;
450 OPJ_UINT32 lNbMatCoeff = pNbComp * pNbComp;
451 OPJ_INT32 * lCurrentData = 00;
452 OPJ_INT32 * lCurrentMatrix = 00;
453 OPJ_INT32 ** lData = (OPJ_INT32 **) pData;
454 OPJ_UINT32 lMultiplicator = 1 << 13;
457 OPJ_ARG_NOT_USED(isSigned);
459 lCurrentData = (OPJ_INT32 *) opj_malloc((pNbComp + lNbMatCoeff) * sizeof(OPJ_INT32));
460 if (! lCurrentData) {
464 lCurrentMatrix = lCurrentData + pNbComp;
466 for (i =0;i<lNbMatCoeff;++i) {
467 lCurrentMatrix[i] = (OPJ_INT32) (*(lMct++) * (OPJ_FLOAT32)lMultiplicator);
470 for (i = 0; i < n; ++i) {
471 lMctPtr = lCurrentMatrix;
472 for (j=0;j<pNbComp;++j) {
473 lCurrentData[j] = (*(lData[j]));
476 for (j=0;j<pNbComp;++j) {
478 for (k=0;k<pNbComp;++k) {
479 *(lData[j]) += opj_int_fix_mul(*lMctPtr, lCurrentData[k]);
487 opj_free(lCurrentData);
492 OPJ_BOOL opj_mct_decode_custom(
493 OPJ_BYTE * pDecodingData,
504 OPJ_FLOAT32 * lCurrentData = 00;
505 OPJ_FLOAT32 * lCurrentResult = 00;
506 OPJ_FLOAT32 ** lData = (OPJ_FLOAT32 **) pData;
508 OPJ_ARG_NOT_USED(isSigned);
510 lCurrentData = (OPJ_FLOAT32 *) opj_malloc (2 * pNbComp * sizeof(OPJ_FLOAT32));
511 if (! lCurrentData) {
514 lCurrentResult = lCurrentData + pNbComp;
516 for (i = 0; i < n; ++i) {
517 lMct = (OPJ_FLOAT32 *) pDecodingData;
518 for (j=0;j<pNbComp;++j) {
519 lCurrentData[j] = (OPJ_FLOAT32) (*(lData[j]));
521 for (j=0;j<pNbComp;++j) {
522 lCurrentResult[j] = 0;
523 for (k=0;k<pNbComp;++k) {
524 lCurrentResult[j] += *(lMct++) * lCurrentData[k];
526 *(lData[j]++) = (OPJ_FLOAT32) (lCurrentResult[j]);
529 opj_free(lCurrentData);
533 void opj_calculate_norms( OPJ_FLOAT64 * pNorms,
535 OPJ_FLOAT32 * pMatrix)
537 OPJ_UINT32 i,j,lIndex;
538 OPJ_FLOAT32 lCurrentValue;
539 OPJ_FLOAT64 * lNorms = (OPJ_FLOAT64 *) pNorms;
540 OPJ_FLOAT32 * lMatrix = (OPJ_FLOAT32 *) pMatrix;
542 for (i=0;i<pNbComps;++i) {
546 for (j=0;j<pNbComps;++j) {
547 lCurrentValue = lMatrix[lIndex];
549 lNorms[i] += lCurrentValue * lCurrentValue;
551 lNorms[i] = sqrt(lNorms[i]);