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* OPJ_RESTRICT c0,
78 OPJ_INT32* OPJ_RESTRICT c1,
79 OPJ_INT32* OPJ_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* OPJ_RESTRICT c0,
120 OPJ_INT32* OPJ_RESTRICT c1,
121 OPJ_INT32* OPJ_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* OPJ_RESTRICT c0,
147 OPJ_INT32* OPJ_RESTRICT c1,
148 OPJ_INT32* OPJ_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* OPJ_RESTRICT c0,
182 OPJ_INT32* OPJ_RESTRICT c1,
183 OPJ_INT32* OPJ_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)
206 return opj_mct_norms[compno];
210 /* Forward irreversible MCT. */
212 void opj_mct_encode_real(
213 OPJ_FLOAT32* OPJ_RESTRICT c0,
214 OPJ_FLOAT32* OPJ_RESTRICT c1,
215 OPJ_FLOAT32* OPJ_RESTRICT c2,
220 const __m128 YR = _mm_set1_ps(0.299f);
221 const __m128 YG = _mm_set1_ps(0.587f);
222 const __m128 YB = _mm_set1_ps(0.114f);
223 const __m128 UR = _mm_set1_ps(-0.16875f);
224 const __m128 UG = _mm_set1_ps(-0.331260f);
225 const __m128 UB = _mm_set1_ps(0.5f);
226 const __m128 VR = _mm_set1_ps(0.5f);
227 const __m128 VG = _mm_set1_ps(-0.41869f);
228 const __m128 VB = _mm_set1_ps(-0.08131f);
229 for (i = 0; i < (n >> 3); i ++) {
230 __m128 r, g, b, y, u, v;
235 y = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, YR), _mm_mul_ps(g, YG)),
237 u = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, UR), _mm_mul_ps(g, UG)),
239 v = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, VR), _mm_mul_ps(g, VG)),
251 y = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, YR), _mm_mul_ps(g, YG)),
253 u = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, UR), _mm_mul_ps(g, UG)),
255 v = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, VR), _mm_mul_ps(g, VG)),
266 for (i = 0; i < n; ++i) {
267 OPJ_FLOAT32 r = c0[i];
268 OPJ_FLOAT32 g = c1[i];
269 OPJ_FLOAT32 b = c2[i];
270 OPJ_FLOAT32 y = 0.299f * r + 0.587f * g + 0.114f * b;
271 OPJ_FLOAT32 u = -0.16875f * r - 0.331260f * g + 0.5f * b;
272 OPJ_FLOAT32 v = 0.5f * r - 0.41869f * g - 0.08131f * b;
280 /* Inverse irreversible MCT. */
282 void opj_mct_decode_real(
283 OPJ_FLOAT32* OPJ_RESTRICT c0,
284 OPJ_FLOAT32* OPJ_RESTRICT c1,
285 OPJ_FLOAT32* OPJ_RESTRICT c2,
290 __m128 vrv, vgu, vgv, vbu;
291 vrv = _mm_set1_ps(1.402f);
292 vgu = _mm_set1_ps(0.34413f);
293 vgv = _mm_set1_ps(0.71414f);
294 vbu = _mm_set1_ps(1.772f);
295 for (i = 0; i < (n >> 3); ++i) {
299 vy = _mm_load_ps(c0);
300 vu = _mm_load_ps(c1);
301 vv = _mm_load_ps(c2);
302 vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
303 vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
304 vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
305 _mm_store_ps(c0, vr);
306 _mm_store_ps(c1, vg);
307 _mm_store_ps(c2, vb);
312 vy = _mm_load_ps(c0);
313 vu = _mm_load_ps(c1);
314 vv = _mm_load_ps(c2);
315 vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
316 vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
317 vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
318 _mm_store_ps(c0, vr);
319 _mm_store_ps(c1, vg);
320 _mm_store_ps(c2, vb);
327 for (i = 0; i < n; ++i) {
328 OPJ_FLOAT32 y = c0[i];
329 OPJ_FLOAT32 u = c1[i];
330 OPJ_FLOAT32 v = c2[i];
331 OPJ_FLOAT32 r = y + (v * 1.402f);
332 OPJ_FLOAT32 g = y - (u * 0.34413f) - (v * (0.71414f));
333 OPJ_FLOAT32 b = y + (u * 1.772f);
341 /* Get norm of basis function of irreversible MCT. */
343 OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno)
345 return opj_mct_norms_real[compno];
349 OPJ_BOOL opj_mct_encode_custom(
350 OPJ_BYTE * pCodingdata,
356 OPJ_FLOAT32 * lMct = (OPJ_FLOAT32 *) pCodingdata;
360 OPJ_UINT32 lNbMatCoeff = pNbComp * pNbComp;
361 OPJ_INT32 * lCurrentData = 00;
362 OPJ_INT32 * lCurrentMatrix = 00;
363 OPJ_INT32 ** lData = (OPJ_INT32 **) pData;
364 OPJ_UINT32 lMultiplicator = 1 << 13;
367 OPJ_ARG_NOT_USED(isSigned);
369 lCurrentData = (OPJ_INT32 *) opj_malloc((pNbComp + lNbMatCoeff) * sizeof(
371 if (! lCurrentData) {
375 lCurrentMatrix = lCurrentData + pNbComp;
377 for (i = 0; i < lNbMatCoeff; ++i) {
378 lCurrentMatrix[i] = (OPJ_INT32)(*(lMct++) * (OPJ_FLOAT32)lMultiplicator);
381 for (i = 0; i < n; ++i) {
382 lMctPtr = lCurrentMatrix;
383 for (j = 0; j < pNbComp; ++j) {
384 lCurrentData[j] = (*(lData[j]));
387 for (j = 0; j < pNbComp; ++j) {
389 for (k = 0; k < pNbComp; ++k) {
390 *(lData[j]) += opj_int_fix_mul(*lMctPtr, lCurrentData[k]);
398 opj_free(lCurrentData);
403 OPJ_BOOL opj_mct_decode_custom(
404 OPJ_BYTE * pDecodingData,
415 OPJ_FLOAT32 * lCurrentData = 00;
416 OPJ_FLOAT32 * lCurrentResult = 00;
417 OPJ_FLOAT32 ** lData = (OPJ_FLOAT32 **) pData;
419 OPJ_ARG_NOT_USED(isSigned);
421 lCurrentData = (OPJ_FLOAT32 *) opj_malloc(2 * pNbComp * sizeof(OPJ_FLOAT32));
422 if (! lCurrentData) {
425 lCurrentResult = lCurrentData + pNbComp;
427 for (i = 0; i < n; ++i) {
428 lMct = (OPJ_FLOAT32 *) pDecodingData;
429 for (j = 0; j < pNbComp; ++j) {
430 lCurrentData[j] = (OPJ_FLOAT32)(*(lData[j]));
432 for (j = 0; j < pNbComp; ++j) {
433 lCurrentResult[j] = 0;
434 for (k = 0; k < pNbComp; ++k) {
435 lCurrentResult[j] += *(lMct++) * lCurrentData[k];
437 *(lData[j]++) = (OPJ_FLOAT32)(lCurrentResult[j]);
440 opj_free(lCurrentData);
444 void opj_calculate_norms(OPJ_FLOAT64 * pNorms,
446 OPJ_FLOAT32 * pMatrix)
448 OPJ_UINT32 i, j, lIndex;
449 OPJ_FLOAT32 lCurrentValue;
450 OPJ_FLOAT64 * lNorms = (OPJ_FLOAT64 *) pNorms;
451 OPJ_FLOAT32 * lMatrix = (OPJ_FLOAT32 *) pMatrix;
453 for (i = 0; i < pNbComps; ++i) {
457 for (j = 0; j < pNbComps; ++j) {
458 lCurrentValue = lMatrix[lIndex];
460 lNorms[i] += (OPJ_FLOAT64) lCurrentValue * lCurrentValue;
462 lNorms[i] = sqrt(lNorms[i]);