mct.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifdef __SSE__
  32. #include <xmmintrin.h>
  33. #endif
  34. #include "opj_includes.h"
  35. /* <summary> */
  36. /* This table contains the norms of the basis function of the reversible MCT. */
  37. /* </summary> */
  38. static const double mct_norms[3] = { 1.732, .8292, .8292 };
  39. /* <summary> */
  40. /* This table contains the norms of the basis function of the irreversible MCT. */
  41. /* </summary> */
  42. static const double mct_norms_real[3] = { 1.732, 1.805, 1.573 };
  43. /* <summary> */
  44. /* Foward reversible MCT. */
  45. /* </summary> */
  46. void mct_encode(
  47. int* restrict c0,
  48. int* restrict c1,
  49. int* restrict c2,
  50. int n)
  51. {
  52. int i;
  53. for(i = 0; i < n; ++i) {
  54. int r = c0[i];
  55. int g = c1[i];
  56. int b = c2[i];
  57. int y = (r + (g * 2) + b) >> 2;
  58. int u = b - g;
  59. int v = r - g;
  60. c0[i] = y;
  61. c1[i] = u;
  62. c2[i] = v;
  63. }
  64. }
  65. /* <summary> */
  66. /* Inverse reversible MCT. */
  67. /* </summary> */
  68. void mct_decode(
  69. int* restrict c0,
  70. int* restrict c1,
  71. int* restrict c2,
  72. int n)
  73. {
  74. int i;
  75. for (i = 0; i < n; ++i) {
  76. int y = c0[i];
  77. int u = c1[i];
  78. int v = c2[i];
  79. int g = y - ((u + v) >> 2);
  80. int r = v + g;
  81. int b = u + g;
  82. c0[i] = r;
  83. c1[i] = g;
  84. c2[i] = b;
  85. }
  86. }
  87. /* <summary> */
  88. /* Get norm of basis function of reversible MCT. */
  89. /* </summary> */
  90. double mct_getnorm(int compno) {
  91. return mct_norms[compno];
  92. }
  93. /* <summary> */
  94. /* Foward irreversible MCT. */
  95. /* </summary> */
  96. void mct_encode_real(
  97. int* restrict c0,
  98. int* restrict c1,
  99. int* restrict c2,
  100. int n)
  101. {
  102. int i;
  103. for(i = 0; i < n; ++i) {
  104. int r = c0[i];
  105. int g = c1[i];
  106. int b = c2[i];
  107. int y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934);
  108. int u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096);
  109. int v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666);
  110. c0[i] = y;
  111. c1[i] = u;
  112. c2[i] = v;
  113. }
  114. }
  115. /* <summary> */
  116. /* Inverse irreversible MCT. */
  117. /* </summary> */
  118. void mct_decode_real(
  119. float* restrict c0,
  120. float* restrict c1,
  121. float* restrict c2,
  122. int n)
  123. {
  124. int i;
  125. #ifdef __SSE__
  126. __m128 vrv, vgu, vgv, vbu;
  127. vrv = _mm_set1_ps(1.402f);
  128. vgu = _mm_set1_ps(0.34413f);
  129. vgv = _mm_set1_ps(0.71414f);
  130. vbu = _mm_set1_ps(1.772f);
  131. for (i = 0; i < (n >> 3); ++i) {
  132. __m128 vy, vu, vv;
  133. __m128 vr, vg, vb;
  134. vy = _mm_load_ps(c0);
  135. vu = _mm_load_ps(c1);
  136. vv = _mm_load_ps(c2);
  137. vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
  138. vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
  139. vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
  140. _mm_store_ps(c0, vr);
  141. _mm_store_ps(c1, vg);
  142. _mm_store_ps(c2, vb);
  143. c0 += 4;
  144. c1 += 4;
  145. c2 += 4;
  146. vy = _mm_load_ps(c0);
  147. vu = _mm_load_ps(c1);
  148. vv = _mm_load_ps(c2);
  149. vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
  150. vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
  151. vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
  152. _mm_store_ps(c0, vr);
  153. _mm_store_ps(c1, vg);
  154. _mm_store_ps(c2, vb);
  155. c0 += 4;
  156. c1 += 4;
  157. c2 += 4;
  158. }
  159. n &= 7;
  160. #endif
  161. for(i = 0; i < n; ++i) {
  162. float y = c0[i];
  163. float u = c1[i];
  164. float v = c2[i];
  165. float r = y + (v * 1.402f);
  166. float g = y - (u * 0.34413f) - (v * (0.71414f));
  167. float b = y + (u * 1.772f);
  168. c0[i] = r;
  169. c1[i] = g;
  170. c2[i] = b;
  171. }
  172. }
  173. /* <summary> */
  174. /* Get norm of basis function of irreversible MCT. */
  175. /* </summary> */
  176. double mct_getnorm_real(int compno) {
  177. return mct_norms_real[compno];
  178. }