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 /* Forward reversible MCT. */
77 OPJ_INT32* restrict c0,
78 OPJ_INT32* restrict c1,
79 OPJ_INT32* restrict c2,
83 const OPJ_SIZE_T len = n;
84 /* buffer are aligned on 16 bytes */
85 assert( ((size_t)c0 & 0xf) == 0 );
86 assert( ((size_t)c1 & 0xf) == 0 );
87 assert( ((size_t)c2 & 0xf) == 0 );
89 for(i = 0; i < (len & ~3U); i += 4) {
91 __m128i r = _mm_load_si128((const __m128i *)&(c0[i]));
92 __m128i g = _mm_load_si128((const __m128i *)&(c1[i]));
93 __m128i b = _mm_load_si128((const __m128i *)&(c2[i]));
94 y = _mm_add_epi32(g, g);
95 y = _mm_add_epi32(y, b);
96 y = _mm_add_epi32(y, r);
97 y = _mm_srai_epi32(y, 2);
98 u = _mm_sub_epi32(b, g);
99 v = _mm_sub_epi32(r, g);
100 _mm_store_si128((__m128i *)&(c0[i]), y);
101 _mm_store_si128((__m128i *)&(c1[i]), u);
102 _mm_store_si128((__m128i *)&(c2[i]), v);
105 for(; i < len; ++i) {
109 OPJ_INT32 y = (r + (g * 2) + b) >> 2;
119 OPJ_INT32* restrict c0,
120 OPJ_INT32* restrict c1,
121 OPJ_INT32* restrict c2,
125 const OPJ_SIZE_T len = n;
127 for(i = 0; i < len; ++i) {
131 OPJ_INT32 y = (r + (g * 2) + b) >> 2;
142 /* Inverse reversible MCT. */
146 OPJ_INT32* restrict c0,
147 OPJ_INT32* restrict c1,
148 OPJ_INT32* restrict c2,
152 const OPJ_SIZE_T len = n;
154 for(i = 0; i < (len & ~3U); i += 4) {
156 __m128i y = _mm_load_si128((const __m128i *)&(c0[i]));
157 __m128i u = _mm_load_si128((const __m128i *)&(c1[i]));
158 __m128i v = _mm_load_si128((const __m128i *)&(c2[i]));
160 g = _mm_sub_epi32(g, _mm_srai_epi32(_mm_add_epi32(u, v), 2));
161 r = _mm_add_epi32(v, g);
162 b = _mm_add_epi32(u, g);
163 _mm_store_si128((__m128i *)&(c0[i]), r);
164 _mm_store_si128((__m128i *)&(c1[i]), g);
165 _mm_store_si128((__m128i *)&(c2[i]), b);
167 for (; i < len; ++i) {
171 OPJ_INT32 g = y - ((u + v) >> 2);
181 OPJ_INT32* restrict c0,
182 OPJ_INT32* restrict c1,
183 OPJ_INT32* restrict c2,
187 for (i = 0; i < n; ++i) {
191 OPJ_INT32 g = y - ((u + v) >> 2);
202 /* Get norm of basis function of reversible MCT. */
204 OPJ_FLOAT64 opj_mct_getnorm(OPJ_UINT32 compno) {
205 return opj_mct_norms[compno];
209 /* Forward irreversible MCT. */
212 void opj_mct_encode_real(
213 OPJ_INT32* restrict c0,
214 OPJ_INT32* restrict c1,
215 OPJ_INT32* restrict c2,
219 const OPJ_SIZE_T len = n;
221 const __m128i ry = _mm_set1_epi32(2449);
222 const __m128i gy = _mm_set1_epi32(4809);
223 const __m128i by = _mm_set1_epi32(934);
224 const __m128i ru = _mm_set1_epi32(1382);
225 const __m128i gu = _mm_set1_epi32(2714);
226 /* const __m128i bu = _mm_set1_epi32(4096); */
227 /* const __m128i rv = _mm_set1_epi32(4096); */
228 const __m128i gv = _mm_set1_epi32(3430);
229 const __m128i bv = _mm_set1_epi32(666);
230 const __m128i mulround = _mm_shuffle_epi32(_mm_cvtsi32_si128(4096), _MM_SHUFFLE(1, 0, 1, 0));
232 for(i = 0; i < (len & ~3U); i += 4) {
235 __m128i r = _mm_load_si128((const __m128i *)&(c0[i]));
236 __m128i g = _mm_load_si128((const __m128i *)&(c1[i]));
237 __m128i b = _mm_load_si128((const __m128i *)&(c2[i]));
240 hi = _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 3, 1, 1));
241 lo = _mm_mul_epi32(lo, ry);
242 hi = _mm_mul_epi32(hi, ry);
243 lo = _mm_add_epi64(lo, mulround);
244 hi = _mm_add_epi64(hi, mulround);
245 lo = _mm_srli_epi64(lo, 13);
246 hi = _mm_slli_epi64(hi, 32-13);
247 y = _mm_blend_epi16(lo, hi, 0xCC);
250 hi = _mm_shuffle_epi32(g, _MM_SHUFFLE(3, 3, 1, 1));
251 lo = _mm_mul_epi32(lo, gy);
252 hi = _mm_mul_epi32(hi, gy);
253 lo = _mm_add_epi64(lo, mulround);
254 hi = _mm_add_epi64(hi, mulround);
255 lo = _mm_srli_epi64(lo, 13);
256 hi = _mm_slli_epi64(hi, 32-13);
257 y = _mm_add_epi32(y, _mm_blend_epi16(lo, hi, 0xCC));
260 hi = _mm_shuffle_epi32(b, _MM_SHUFFLE(3, 3, 1, 1));
261 lo = _mm_mul_epi32(lo, by);
262 hi = _mm_mul_epi32(hi, by);
263 lo = _mm_add_epi64(lo, mulround);
264 hi = _mm_add_epi64(hi, mulround);
265 lo = _mm_srli_epi64(lo, 13);
266 hi = _mm_slli_epi64(hi, 32-13);
267 y = _mm_add_epi32(y, _mm_blend_epi16(lo, hi, 0xCC));
268 _mm_store_si128((__m128i *)&(c0[i]), y);
271 hi = _mm_shuffle_epi32(b, _MM_SHUFFLE(3, 3, 1, 1));
272 lo = _mm_mul_epi32(lo, mulround);
273 hi = _mm_mul_epi32(hi, mulround);*/
274 lo = _mm_cvtepi32_epi64(_mm_shuffle_epi32(b, _MM_SHUFFLE(3, 2, 2, 0)));
275 hi = _mm_cvtepi32_epi64(_mm_shuffle_epi32(b, _MM_SHUFFLE(3, 2, 3, 1)));
276 lo = _mm_slli_epi64(lo, 12);
277 hi = _mm_slli_epi64(hi, 12);
278 lo = _mm_add_epi64(lo, mulround);
279 hi = _mm_add_epi64(hi, mulround);
280 lo = _mm_srli_epi64(lo, 13);
281 hi = _mm_slli_epi64(hi, 32-13);
282 u = _mm_blend_epi16(lo, hi, 0xCC);
285 hi = _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 3, 1, 1));
286 lo = _mm_mul_epi32(lo, ru);
287 hi = _mm_mul_epi32(hi, ru);
288 lo = _mm_add_epi64(lo, mulround);
289 hi = _mm_add_epi64(hi, mulround);
290 lo = _mm_srli_epi64(lo, 13);
291 hi = _mm_slli_epi64(hi, 32-13);
292 u = _mm_sub_epi32(u, _mm_blend_epi16(lo, hi, 0xCC));
295 hi = _mm_shuffle_epi32(g, _MM_SHUFFLE(3, 3, 1, 1));
296 lo = _mm_mul_epi32(lo, gu);
297 hi = _mm_mul_epi32(hi, gu);
298 lo = _mm_add_epi64(lo, mulround);
299 hi = _mm_add_epi64(hi, mulround);
300 lo = _mm_srli_epi64(lo, 13);
301 hi = _mm_slli_epi64(hi, 32-13);
302 u = _mm_sub_epi32(u, _mm_blend_epi16(lo, hi, 0xCC));
303 _mm_store_si128((__m128i *)&(c1[i]), u);
306 hi = _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 3, 1, 1));
307 lo = _mm_mul_epi32(lo, mulround);
308 hi = _mm_mul_epi32(hi, mulround);*/
309 lo = _mm_cvtepi32_epi64(_mm_shuffle_epi32(r, _MM_SHUFFLE(3, 2, 2, 0)));
310 hi = _mm_cvtepi32_epi64(_mm_shuffle_epi32(r, _MM_SHUFFLE(3, 2, 3, 1)));
311 lo = _mm_slli_epi64(lo, 12);
312 hi = _mm_slli_epi64(hi, 12);
313 lo = _mm_add_epi64(lo, mulround);
314 hi = _mm_add_epi64(hi, mulround);
315 lo = _mm_srli_epi64(lo, 13);
316 hi = _mm_slli_epi64(hi, 32-13);
317 v = _mm_blend_epi16(lo, hi, 0xCC);
320 hi = _mm_shuffle_epi32(g, _MM_SHUFFLE(3, 3, 1, 1));
321 lo = _mm_mul_epi32(lo, gv);
322 hi = _mm_mul_epi32(hi, gv);
323 lo = _mm_add_epi64(lo, mulround);
324 hi = _mm_add_epi64(hi, mulround);
325 lo = _mm_srli_epi64(lo, 13);
326 hi = _mm_slli_epi64(hi, 32-13);
327 v = _mm_sub_epi32(v, _mm_blend_epi16(lo, hi, 0xCC));
330 hi = _mm_shuffle_epi32(b, _MM_SHUFFLE(3, 3, 1, 1));
331 lo = _mm_mul_epi32(lo, bv);
332 hi = _mm_mul_epi32(hi, bv);
333 lo = _mm_add_epi64(lo, mulround);
334 hi = _mm_add_epi64(hi, mulround);
335 lo = _mm_srli_epi64(lo, 13);
336 hi = _mm_slli_epi64(hi, 32-13);
337 v = _mm_sub_epi32(v, _mm_blend_epi16(lo, hi, 0xCC));
338 _mm_store_si128((__m128i *)&(c2[i]), v);
340 for(; i < len; ++i) {
344 OPJ_INT32 y = opj_int_fix_mul(r, 2449) + opj_int_fix_mul(g, 4809) + opj_int_fix_mul(b, 934);
345 OPJ_INT32 u = -opj_int_fix_mul(r, 1382) - opj_int_fix_mul(g, 2714) + opj_int_fix_mul(b, 4096);
346 OPJ_INT32 v = opj_int_fix_mul(r, 4096) - opj_int_fix_mul(g, 3430) - opj_int_fix_mul(b, 666);
353 void opj_mct_encode_real(
354 OPJ_INT32* restrict c0,
355 OPJ_INT32* restrict c1,
356 OPJ_INT32* restrict c2,
360 for(i = 0; i < n; ++i) {
364 OPJ_INT32 y = opj_int_fix_mul(r, 2449) + opj_int_fix_mul(g, 4809) + opj_int_fix_mul(b, 934);
365 OPJ_INT32 u = -opj_int_fix_mul(r, 1382) - opj_int_fix_mul(g, 2714) + opj_int_fix_mul(b, 4096);
366 OPJ_INT32 v = opj_int_fix_mul(r, 4096) - opj_int_fix_mul(g, 3430) - opj_int_fix_mul(b, 666);
375 /* Inverse irreversible MCT. */
377 void opj_mct_decode_real(
378 OPJ_FLOAT32* restrict c0,
379 OPJ_FLOAT32* restrict c1,
380 OPJ_FLOAT32* restrict c2,
385 __m128 vrv, vgu, vgv, vbu;
386 vrv = _mm_set1_ps(1.402f);
387 vgu = _mm_set1_ps(0.34413f);
388 vgv = _mm_set1_ps(0.71414f);
389 vbu = _mm_set1_ps(1.772f);
390 for (i = 0; i < (n >> 3); ++i) {
394 vy = _mm_load_ps(c0);
395 vu = _mm_load_ps(c1);
396 vv = _mm_load_ps(c2);
397 vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
398 vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
399 vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
400 _mm_store_ps(c0, vr);
401 _mm_store_ps(c1, vg);
402 _mm_store_ps(c2, vb);
407 vy = _mm_load_ps(c0);
408 vu = _mm_load_ps(c1);
409 vv = _mm_load_ps(c2);
410 vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
411 vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
412 vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
413 _mm_store_ps(c0, vr);
414 _mm_store_ps(c1, vg);
415 _mm_store_ps(c2, vb);
422 for(i = 0; i < n; ++i) {
423 OPJ_FLOAT32 y = c0[i];
424 OPJ_FLOAT32 u = c1[i];
425 OPJ_FLOAT32 v = c2[i];
426 OPJ_FLOAT32 r = y + (v * 1.402f);
427 OPJ_FLOAT32 g = y - (u * 0.34413f) - (v * (0.71414f));
428 OPJ_FLOAT32 b = y + (u * 1.772f);
436 /* Get norm of basis function of irreversible MCT. */
438 OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno) {
439 return opj_mct_norms_real[compno];
443 OPJ_BOOL opj_mct_encode_custom(
444 OPJ_BYTE * pCodingdata,
450 OPJ_FLOAT32 * lMct = (OPJ_FLOAT32 *) pCodingdata;
454 OPJ_UINT32 lNbMatCoeff = pNbComp * pNbComp;
455 OPJ_INT32 * lCurrentData = 00;
456 OPJ_INT32 * lCurrentMatrix = 00;
457 OPJ_INT32 ** lData = (OPJ_INT32 **) pData;
458 OPJ_UINT32 lMultiplicator = 1 << 13;
461 OPJ_ARG_NOT_USED(isSigned);
463 lCurrentData = (OPJ_INT32 *) opj_malloc((pNbComp + lNbMatCoeff) * sizeof(OPJ_INT32));
464 if (! lCurrentData) {
468 lCurrentMatrix = lCurrentData + pNbComp;
470 for (i =0;i<lNbMatCoeff;++i) {
471 lCurrentMatrix[i] = (OPJ_INT32) (*(lMct++) * (OPJ_FLOAT32)lMultiplicator);
474 for (i = 0; i < n; ++i) {
475 lMctPtr = lCurrentMatrix;
476 for (j=0;j<pNbComp;++j) {
477 lCurrentData[j] = (*(lData[j]));
480 for (j=0;j<pNbComp;++j) {
482 for (k=0;k<pNbComp;++k) {
483 *(lData[j]) += opj_int_fix_mul(*lMctPtr, lCurrentData[k]);
491 opj_free(lCurrentData);
496 OPJ_BOOL opj_mct_decode_custom(
497 OPJ_BYTE * pDecodingData,
508 OPJ_FLOAT32 * lCurrentData = 00;
509 OPJ_FLOAT32 * lCurrentResult = 00;
510 OPJ_FLOAT32 ** lData = (OPJ_FLOAT32 **) pData;
512 OPJ_ARG_NOT_USED(isSigned);
514 lCurrentData = (OPJ_FLOAT32 *) opj_malloc (2 * pNbComp * sizeof(OPJ_FLOAT32));
515 if (! lCurrentData) {
518 lCurrentResult = lCurrentData + pNbComp;
520 for (i = 0; i < n; ++i) {
521 lMct = (OPJ_FLOAT32 *) pDecodingData;
522 for (j=0;j<pNbComp;++j) {
523 lCurrentData[j] = (OPJ_FLOAT32) (*(lData[j]));
525 for (j=0;j<pNbComp;++j) {
526 lCurrentResult[j] = 0;
527 for (k=0;k<pNbComp;++k) {
528 lCurrentResult[j] += *(lMct++) * lCurrentData[k];
530 *(lData[j]++) = (OPJ_FLOAT32) (lCurrentResult[j]);
533 opj_free(lCurrentData);
537 void opj_calculate_norms( OPJ_FLOAT64 * pNorms,
539 OPJ_FLOAT32 * pMatrix)
541 OPJ_UINT32 i,j,lIndex;
542 OPJ_FLOAT32 lCurrentValue;
543 OPJ_FLOAT64 * lNorms = (OPJ_FLOAT64 *) pNorms;
544 OPJ_FLOAT32 * lMatrix = (OPJ_FLOAT32 *) pMatrix;
546 for (i=0;i<pNbComps;++i) {
550 for (j=0;j<pNbComps;++j) {
551 lCurrentValue = lMatrix[lIndex];
553 lNorms[i] += lCurrentValue * lCurrentValue;
555 lNorms[i] = sqrt(lNorms[i]);