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 "opj_includes.h"
47 /* This table contains the norms of the basis function of the reversible MCT. */
49 static const OPJ_FLOAT64 opj_mct_norms[3] = { 1.732, .8292, .8292 };
52 /* This table contains the norms of the basis function of the irreversible MCT. */
54 static const OPJ_FLOAT64 opj_mct_norms_real[3] = { 1.732, 1.805, 1.573 };
56 const OPJ_FLOAT64 * opj_mct_get_mct_norms ()
61 const OPJ_FLOAT64 * opj_mct_get_mct_norms_real ()
63 return opj_mct_norms_real;
67 /* Foward reversible MCT. */
70 OPJ_INT32* restrict c0,
71 OPJ_INT32* restrict c1,
72 OPJ_INT32* restrict c2,
76 for(i = 0; i < n; ++i) {
80 OPJ_INT32 y = (r + (g * 2) + b) >> 2;
90 /* Inverse reversible MCT. */
93 OPJ_INT32* restrict c0,
94 OPJ_INT32* restrict c1,
95 OPJ_INT32* restrict c2,
99 for (i = 0; i < n; ++i) {
103 OPJ_INT32 g = y - ((u + v) >> 2);
113 /* Get norm of basis function of reversible MCT. */
115 OPJ_FLOAT64 opj_mct_getnorm(OPJ_UINT32 compno) {
116 return opj_mct_norms[compno];
120 /* Foward irreversible MCT. */
122 void opj_mct_encode_real(
123 OPJ_INT32* restrict c0,
124 OPJ_INT32* restrict c1,
125 OPJ_INT32* restrict c2,
129 for(i = 0; i < n; ++i) {
133 OPJ_INT32 y = opj_int_fix_mul(r, 2449) + opj_int_fix_mul(g, 4809) + opj_int_fix_mul(b, 934);
134 OPJ_INT32 u = -opj_int_fix_mul(r, 1382) - opj_int_fix_mul(g, 2714) + opj_int_fix_mul(b, 4096);
135 OPJ_INT32 v = opj_int_fix_mul(r, 4096) - opj_int_fix_mul(g, 3430) - opj_int_fix_mul(b, 666);
143 /* Inverse irreversible MCT. */
145 void opj_mct_decode_real(
146 OPJ_FLOAT32* restrict c0,
147 OPJ_FLOAT32* restrict c1,
148 OPJ_FLOAT32* restrict c2,
153 __m128 vrv, vgu, vgv, vbu;
154 vrv = _mm_set1_ps(1.402f);
155 vgu = _mm_set1_ps(0.34413f);
156 vgv = _mm_set1_ps(0.71414f);
157 vbu = _mm_set1_ps(1.772f);
158 for (i = 0; i < (n >> 3); ++i) {
162 vy = _mm_load_ps(c0);
163 vu = _mm_load_ps(c1);
164 vv = _mm_load_ps(c2);
165 vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
166 vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
167 vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
168 _mm_store_ps(c0, vr);
169 _mm_store_ps(c1, vg);
170 _mm_store_ps(c2, vb);
175 vy = _mm_load_ps(c0);
176 vu = _mm_load_ps(c1);
177 vv = _mm_load_ps(c2);
178 vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
179 vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
180 vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
181 _mm_store_ps(c0, vr);
182 _mm_store_ps(c1, vg);
183 _mm_store_ps(c2, vb);
190 for(i = 0; i < n; ++i) {
191 OPJ_FLOAT32 y = c0[i];
192 OPJ_FLOAT32 u = c1[i];
193 OPJ_FLOAT32 v = c2[i];
194 OPJ_FLOAT32 r = y + (v * 1.402f);
195 OPJ_FLOAT32 g = y - (u * 0.34413f) - (v * (0.71414f));
196 OPJ_FLOAT32 b = y + (u * 1.772f);
204 /* Get norm of basis function of irreversible MCT. */
206 OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno) {
207 return opj_mct_norms_real[compno];
211 OPJ_BOOL opj_mct_encode_custom(
212 OPJ_BYTE * pCodingdata,
218 OPJ_FLOAT32 * lMct = (OPJ_FLOAT32 *) pCodingdata;
222 OPJ_UINT32 lNbMatCoeff = pNbComp * pNbComp;
223 OPJ_INT32 * lCurrentData = 00;
224 OPJ_INT32 * lCurrentMatrix = 00;
225 OPJ_INT32 ** lData = (OPJ_INT32 **) pData;
226 OPJ_UINT32 lMultiplicator = 1 << 13;
229 OPJ_ARG_NOT_USED(isSigned);
231 lCurrentData = (OPJ_INT32 *) opj_malloc((pNbComp + lNbMatCoeff) * sizeof(OPJ_INT32));
232 if (! lCurrentData) {
236 lCurrentMatrix = lCurrentData + pNbComp;
238 for (i =0;i<lNbMatCoeff;++i) {
239 lCurrentMatrix[i] = (OPJ_INT32) (*(lMct++) * (OPJ_FLOAT32)lMultiplicator);
242 for (i = 0; i < n; ++i) {
243 lMctPtr = lCurrentMatrix;
244 for (j=0;j<pNbComp;++j) {
245 lCurrentData[j] = (*(lData[j]));
248 for (j=0;j<pNbComp;++j) {
250 for (k=0;k<pNbComp;++k) {
251 *(lData[j]) += opj_int_fix_mul(*lMctPtr, lCurrentData[k]);
259 opj_free(lCurrentData);
264 OPJ_BOOL opj_mct_decode_custom(
265 OPJ_BYTE * pDecodingData,
276 OPJ_FLOAT32 * lCurrentData = 00;
277 OPJ_FLOAT32 * lCurrentResult = 00;
278 OPJ_FLOAT32 ** lData = (OPJ_FLOAT32 **) pData;
280 OPJ_ARG_NOT_USED(isSigned);
282 lCurrentData = (OPJ_FLOAT32 *) opj_malloc (2 * pNbComp * sizeof(OPJ_FLOAT32));
283 if (! lCurrentData) {
286 lCurrentResult = lCurrentData + pNbComp;
288 for (i = 0; i < n; ++i) {
289 lMct = (OPJ_FLOAT32 *) pDecodingData;
290 for (j=0;j<pNbComp;++j) {
291 lCurrentData[j] = (OPJ_FLOAT32) (*(lData[j]));
293 for (j=0;j<pNbComp;++j) {
294 lCurrentResult[j] = 0;
295 for (k=0;k<pNbComp;++k) {
296 lCurrentResult[j] += *(lMct++) * lCurrentData[k];
298 *(lData[j]++) = (OPJ_FLOAT32) (lCurrentResult[j]);
301 opj_free(lCurrentData);
305 void opj_calculate_norms( OPJ_FLOAT64 * pNorms,
307 OPJ_FLOAT32 * pMatrix)
309 OPJ_UINT32 i,j,lIndex;
310 OPJ_FLOAT32 lCurrentValue;
311 OPJ_FLOAT64 * lNorms = (OPJ_FLOAT64 *) pNorms;
312 OPJ_FLOAT32 * lMatrix = (OPJ_FLOAT32 *) pMatrix;
314 for (i=0;i<pNbComps;++i) {
318 for (j=0;j<pNbComps;++j) {
319 lCurrentValue = lMatrix[lIndex];
321 lNorms[i] += lCurrentValue * lCurrentValue;
323 lNorms[i] = sqrt(lNorms[i]);