faandct.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Floating point AAN DCT
  3. * this implementation is based upon the IJG integer AAN DCT (see jfdctfst.c)
  4. *
  5. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  6. * Copyright (c) 2003 Roman Shaposhnik
  7. *
  8. * Permission to use, copy, modify, and/or distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. /**
  21. * @file libavcodec/faandct.c
  22. * @brief
  23. * Floating point AAN DCT
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "dsputil.h"
  27. #include "faandct.h"
  28. #define FLOAT float
  29. #ifdef FAAN_POSTSCALE
  30. # define SCALE(x) postscale[x]
  31. #else
  32. # define SCALE(x) 1
  33. #endif
  34. //numbers generated by simple c code (not as accurate as they could be)
  35. /*
  36. for(i=0; i<8; i++){
  37. printf("#define B%d %1.20llf\n", i, (long double)1.0/(cosl(i*acosl(-1.0)/(long double)16.0)*sqrtl(2)));
  38. }
  39. */
  40. #define B0 1.00000000000000000000
  41. #define B1 0.72095982200694791383 // (cos(pi*1/16)sqrt(2))^-1
  42. #define B2 0.76536686473017954350 // (cos(pi*2/16)sqrt(2))^-1
  43. #define B3 0.85043009476725644878 // (cos(pi*3/16)sqrt(2))^-1
  44. #define B4 1.00000000000000000000 // (cos(pi*4/16)sqrt(2))^-1
  45. #define B5 1.27275858057283393842 // (cos(pi*5/16)sqrt(2))^-1
  46. #define B6 1.84775906502257351242 // (cos(pi*6/16)sqrt(2))^-1
  47. #define B7 3.62450978541155137218 // (cos(pi*7/16)sqrt(2))^-1
  48. #define A1 0.70710678118654752438 // cos(pi*4/16)
  49. #define A2 0.54119610014619698435 // cos(pi*6/16)sqrt(2)
  50. #define A5 0.38268343236508977170 // cos(pi*6/16)
  51. #define A4 1.30656296487637652774 // cos(pi*2/16)sqrt(2)
  52. static const FLOAT postscale[64]={
  53. B0*B0, B0*B1, B0*B2, B0*B3, B0*B4, B0*B5, B0*B6, B0*B7,
  54. B1*B0, B1*B1, B1*B2, B1*B3, B1*B4, B1*B5, B1*B6, B1*B7,
  55. B2*B0, B2*B1, B2*B2, B2*B3, B2*B4, B2*B5, B2*B6, B2*B7,
  56. B3*B0, B3*B1, B3*B2, B3*B3, B3*B4, B3*B5, B3*B6, B3*B7,
  57. B4*B0, B4*B1, B4*B2, B4*B3, B4*B4, B4*B5, B4*B6, B4*B7,
  58. B5*B0, B5*B1, B5*B2, B5*B3, B5*B4, B5*B5, B5*B6, B5*B7,
  59. B6*B0, B6*B1, B6*B2, B6*B3, B6*B4, B6*B5, B6*B6, B6*B7,
  60. B7*B0, B7*B1, B7*B2, B7*B3, B7*B4, B7*B5, B7*B6, B7*B7,
  61. };
  62. static av_always_inline void row_fdct(FLOAT temp[64], DCTELEM * data)
  63. {
  64. FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  65. FLOAT tmp10, tmp11, tmp12, tmp13;
  66. FLOAT z2, z4, z11, z13;
  67. FLOAT av_unused z5;
  68. int i;
  69. for (i=0; i<8*8; i+=8) {
  70. tmp0= data[0 + i] + data[7 + i];
  71. tmp7= data[0 + i] - data[7 + i];
  72. tmp1= data[1 + i] + data[6 + i];
  73. tmp6= data[1 + i] - data[6 + i];
  74. tmp2= data[2 + i] + data[5 + i];
  75. tmp5= data[2 + i] - data[5 + i];
  76. tmp3= data[3 + i] + data[4 + i];
  77. tmp4= data[3 + i] - data[4 + i];
  78. tmp10= tmp0 + tmp3;
  79. tmp13= tmp0 - tmp3;
  80. tmp11= tmp1 + tmp2;
  81. tmp12= tmp1 - tmp2;
  82. temp[0 + i]= tmp10 + tmp11;
  83. temp[4 + i]= tmp10 - tmp11;
  84. tmp12 += tmp13;
  85. tmp12 *= A1;
  86. temp[2 + i]= tmp13 + tmp12;
  87. temp[6 + i]= tmp13 - tmp12;
  88. tmp4 += tmp5;
  89. tmp5 += tmp6;
  90. tmp6 += tmp7;
  91. #if 0
  92. z5= (tmp4 - tmp6) * A5;
  93. z2= tmp4*A2 + z5;
  94. z4= tmp6*A4 + z5;
  95. #else
  96. z2= tmp4*(A2+A5) - tmp6*A5;
  97. z4= tmp6*(A4-A5) + tmp4*A5;
  98. #endif
  99. tmp5*=A1;
  100. z11= tmp7 + tmp5;
  101. z13= tmp7 - tmp5;
  102. temp[5 + i]= z13 + z2;
  103. temp[3 + i]= z13 - z2;
  104. temp[1 + i]= z11 + z4;
  105. temp[7 + i]= z11 - z4;
  106. }
  107. }
  108. void ff_faandct(DCTELEM * data)
  109. {
  110. FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  111. FLOAT tmp10, tmp11, tmp12, tmp13;
  112. FLOAT z2, z4, z11, z13;
  113. FLOAT av_unused z5;
  114. FLOAT temp[64];
  115. int i;
  116. emms_c();
  117. row_fdct(temp, data);
  118. for (i=0; i<8; i++) {
  119. tmp0= temp[8*0 + i] + temp[8*7 + i];
  120. tmp7= temp[8*0 + i] - temp[8*7 + i];
  121. tmp1= temp[8*1 + i] + temp[8*6 + i];
  122. tmp6= temp[8*1 + i] - temp[8*6 + i];
  123. tmp2= temp[8*2 + i] + temp[8*5 + i];
  124. tmp5= temp[8*2 + i] - temp[8*5 + i];
  125. tmp3= temp[8*3 + i] + temp[8*4 + i];
  126. tmp4= temp[8*3 + i] - temp[8*4 + i];
  127. tmp10= tmp0 + tmp3;
  128. tmp13= tmp0 - tmp3;
  129. tmp11= tmp1 + tmp2;
  130. tmp12= tmp1 - tmp2;
  131. data[8*0 + i]= lrintf(SCALE(8*0 + i) * (tmp10 + tmp11));
  132. data[8*4 + i]= lrintf(SCALE(8*4 + i) * (tmp10 - tmp11));
  133. tmp12 += tmp13;
  134. tmp12 *= A1;
  135. data[8*2 + i]= lrintf(SCALE(8*2 + i) * (tmp13 + tmp12));
  136. data[8*6 + i]= lrintf(SCALE(8*6 + i) * (tmp13 - tmp12));
  137. tmp4 += tmp5;
  138. tmp5 += tmp6;
  139. tmp6 += tmp7;
  140. #if 0
  141. z5= (tmp4 - tmp6) * A5;
  142. z2= tmp4*A2 + z5;
  143. z4= tmp6*A4 + z5;
  144. #else
  145. z2= tmp4*(A2+A5) - tmp6*A5;
  146. z4= tmp6*(A4-A5) + tmp4*A5;
  147. #endif
  148. tmp5*=A1;
  149. z11= tmp7 + tmp5;
  150. z13= tmp7 - tmp5;
  151. data[8*5 + i]= lrintf(SCALE(8*5 + i) * (z13 + z2));
  152. data[8*3 + i]= lrintf(SCALE(8*3 + i) * (z13 - z2));
  153. data[8*1 + i]= lrintf(SCALE(8*1 + i) * (z11 + z4));
  154. data[8*7 + i]= lrintf(SCALE(8*7 + i) * (z11 - z4));
  155. }
  156. }
  157. void ff_faandct248(DCTELEM * data)
  158. {
  159. FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  160. FLOAT tmp10, tmp11, tmp12, tmp13;
  161. FLOAT temp[64];
  162. int i;
  163. emms_c();
  164. row_fdct(temp, data);
  165. for (i=0; i<8; i++) {
  166. tmp0 = temp[8*0 + i] + temp[8*1 + i];
  167. tmp1 = temp[8*2 + i] + temp[8*3 + i];
  168. tmp2 = temp[8*4 + i] + temp[8*5 + i];
  169. tmp3 = temp[8*6 + i] + temp[8*7 + i];
  170. tmp4 = temp[8*0 + i] - temp[8*1 + i];
  171. tmp5 = temp[8*2 + i] - temp[8*3 + i];
  172. tmp6 = temp[8*4 + i] - temp[8*5 + i];
  173. tmp7 = temp[8*6 + i] - temp[8*7 + i];
  174. tmp10 = tmp0 + tmp3;
  175. tmp11 = tmp1 + tmp2;
  176. tmp12 = tmp1 - tmp2;
  177. tmp13 = tmp0 - tmp3;
  178. data[8*0 + i] = lrintf(SCALE(8*0 + i) * (tmp10 + tmp11));
  179. data[8*4 + i] = lrintf(SCALE(8*4 + i) * (tmp10 - tmp11));
  180. tmp12 += tmp13;
  181. tmp12 *= A1;
  182. data[8*2 + i] = lrintf(SCALE(8*2 + i) * (tmp13 + tmp12));
  183. data[8*6 + i] = lrintf(SCALE(8*6 + i) * (tmp13 - tmp12));
  184. tmp10 = tmp4 + tmp7;
  185. tmp11 = tmp5 + tmp6;
  186. tmp12 = tmp5 - tmp6;
  187. tmp13 = tmp4 - tmp7;
  188. data[8*1 + i] = lrintf(SCALE(8*0 + i) * (tmp10 + tmp11));
  189. data[8*5 + i] = lrintf(SCALE(8*4 + i) * (tmp10 - tmp11));
  190. tmp12 += tmp13;
  191. tmp12 *= A1;
  192. data[8*3 + i] = lrintf(SCALE(8*2 + i) * (tmp13 + tmp12));
  193. data[8*7 + i] = lrintf(SCALE(8*6 + i) * (tmp13 - tmp12));
  194. }
  195. }