faanidct.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Floating point AAN IDCT
  3. * Copyright (c) 2008 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "faanidct.h"
  22. /* To allow switching to double. */
  23. #define FLOAT float
  24. #define B0 1.0000000000000000000000
  25. #define B1 1.3870398453221474618216 // cos(pi*1/16)sqrt(2)
  26. #define B2 1.3065629648763765278566 // cos(pi*2/16)sqrt(2)
  27. #define B3 1.1758756024193587169745 // cos(pi*3/16)sqrt(2)
  28. #define B4 1.0000000000000000000000 // cos(pi*4/16)sqrt(2)
  29. #define B5 0.7856949583871021812779 // cos(pi*5/16)sqrt(2)
  30. #define B6 0.5411961001461969843997 // cos(pi*6/16)sqrt(2)
  31. #define B7 0.2758993792829430123360 // cos(pi*7/16)sqrt(2)
  32. #define A4 0.70710678118654752438 // cos(pi*4/16)
  33. #define A2 0.92387953251128675613 // cos(pi*2/16)
  34. static const FLOAT prescale[64]={
  35. B0*B0/8, B0*B1/8, B0*B2/8, B0*B3/8, B0*B4/8, B0*B5/8, B0*B6/8, B0*B7/8,
  36. B1*B0/8, B1*B1/8, B1*B2/8, B1*B3/8, B1*B4/8, B1*B5/8, B1*B6/8, B1*B7/8,
  37. B2*B0/8, B2*B1/8, B2*B2/8, B2*B3/8, B2*B4/8, B2*B5/8, B2*B6/8, B2*B7/8,
  38. B3*B0/8, B3*B1/8, B3*B2/8, B3*B3/8, B3*B4/8, B3*B5/8, B3*B6/8, B3*B7/8,
  39. B4*B0/8, B4*B1/8, B4*B2/8, B4*B3/8, B4*B4/8, B4*B5/8, B4*B6/8, B4*B7/8,
  40. B5*B0/8, B5*B1/8, B5*B2/8, B5*B3/8, B5*B4/8, B5*B5/8, B5*B6/8, B5*B7/8,
  41. B6*B0/8, B6*B1/8, B6*B2/8, B6*B3/8, B6*B4/8, B6*B5/8, B6*B6/8, B6*B7/8,
  42. B7*B0/8, B7*B1/8, B7*B2/8, B7*B3/8, B7*B4/8, B7*B5/8, B7*B6/8, B7*B7/8,
  43. };
  44. static inline void p8idct(DCTELEM data[64], FLOAT temp[64], uint8_t *dest, int stride, int x, int y, int type){
  45. int i;
  46. FLOAT av_unused tmp0;
  47. FLOAT s04, d04, s17, d17, s26, d26, s53, d53;
  48. FLOAT os07, os16, os25, os34;
  49. FLOAT od07, od16, od25, od34;
  50. for(i=0; i<y*8; i+=y){
  51. s17= temp[1*x + i] + temp[7*x + i];
  52. d17= temp[1*x + i] - temp[7*x + i];
  53. s53= temp[5*x + i] + temp[3*x + i];
  54. d53= temp[5*x + i] - temp[3*x + i];
  55. od07= s17 + s53;
  56. od25= (s17 - s53)*(2*A4);
  57. #if 0 //these 2 are equivalent
  58. tmp0= (d17 + d53)*(2*A2);
  59. od34= d17*( 2*B6) - tmp0;
  60. od16= d53*(-2*B2) + tmp0;
  61. #else
  62. od34= d17*(2*(B6-A2)) - d53*(2*A2);
  63. od16= d53*(2*(A2-B2)) + d17*(2*A2);
  64. #endif
  65. od16 -= od07;
  66. od25 -= od16;
  67. od34 += od25;
  68. s26 = temp[2*x + i] + temp[6*x + i];
  69. d26 = temp[2*x + i] - temp[6*x + i];
  70. d26*= 2*A4;
  71. d26-= s26;
  72. s04= temp[0*x + i] + temp[4*x + i];
  73. d04= temp[0*x + i] - temp[4*x + i];
  74. os07= s04 + s26;
  75. os34= s04 - s26;
  76. os16= d04 + d26;
  77. os25= d04 - d26;
  78. if(type==0){
  79. temp[0*x + i]= os07 + od07;
  80. temp[7*x + i]= os07 - od07;
  81. temp[1*x + i]= os16 + od16;
  82. temp[6*x + i]= os16 - od16;
  83. temp[2*x + i]= os25 + od25;
  84. temp[5*x + i]= os25 - od25;
  85. temp[3*x + i]= os34 - od34;
  86. temp[4*x + i]= os34 + od34;
  87. }else if(type==1){
  88. data[0*x + i]= lrintf(os07 + od07);
  89. data[7*x + i]= lrintf(os07 - od07);
  90. data[1*x + i]= lrintf(os16 + od16);
  91. data[6*x + i]= lrintf(os16 - od16);
  92. data[2*x + i]= lrintf(os25 + od25);
  93. data[5*x + i]= lrintf(os25 - od25);
  94. data[3*x + i]= lrintf(os34 - od34);
  95. data[4*x + i]= lrintf(os34 + od34);
  96. }else if(type==2){
  97. dest[0*stride + i]= av_clip_uint8(((int)dest[0*stride + i]) + lrintf(os07 + od07));
  98. dest[7*stride + i]= av_clip_uint8(((int)dest[7*stride + i]) + lrintf(os07 - od07));
  99. dest[1*stride + i]= av_clip_uint8(((int)dest[1*stride + i]) + lrintf(os16 + od16));
  100. dest[6*stride + i]= av_clip_uint8(((int)dest[6*stride + i]) + lrintf(os16 - od16));
  101. dest[2*stride + i]= av_clip_uint8(((int)dest[2*stride + i]) + lrintf(os25 + od25));
  102. dest[5*stride + i]= av_clip_uint8(((int)dest[5*stride + i]) + lrintf(os25 - od25));
  103. dest[3*stride + i]= av_clip_uint8(((int)dest[3*stride + i]) + lrintf(os34 - od34));
  104. dest[4*stride + i]= av_clip_uint8(((int)dest[4*stride + i]) + lrintf(os34 + od34));
  105. }else{
  106. dest[0*stride + i]= av_clip_uint8(lrintf(os07 + od07));
  107. dest[7*stride + i]= av_clip_uint8(lrintf(os07 - od07));
  108. dest[1*stride + i]= av_clip_uint8(lrintf(os16 + od16));
  109. dest[6*stride + i]= av_clip_uint8(lrintf(os16 - od16));
  110. dest[2*stride + i]= av_clip_uint8(lrintf(os25 + od25));
  111. dest[5*stride + i]= av_clip_uint8(lrintf(os25 - od25));
  112. dest[3*stride + i]= av_clip_uint8(lrintf(os34 - od34));
  113. dest[4*stride + i]= av_clip_uint8(lrintf(os34 + od34));
  114. }
  115. }
  116. }
  117. void ff_faanidct(DCTELEM block[64]){
  118. FLOAT temp[64];
  119. int i;
  120. emms_c();
  121. for(i=0; i<64; i++)
  122. temp[i] = block[i] * prescale[i];
  123. p8idct(block, temp, NULL, 0, 1, 8, 0);
  124. p8idct(block, temp, NULL, 0, 8, 1, 1);
  125. }
  126. void ff_faanidct_add(uint8_t *dest, int line_size, DCTELEM block[64]){
  127. FLOAT temp[64];
  128. int i;
  129. emms_c();
  130. for(i=0; i<64; i++)
  131. temp[i] = block[i] * prescale[i];
  132. p8idct(block, temp, NULL, 0, 1, 8, 0);
  133. p8idct(NULL , temp, dest, line_size, 8, 1, 2);
  134. }
  135. void ff_faanidct_put(uint8_t *dest, int line_size, DCTELEM block[64]){
  136. FLOAT temp[64];
  137. int i;
  138. emms_c();
  139. for(i=0; i<64; i++)
  140. temp[i] = block[i] * prescale[i];
  141. p8idct(block, temp, NULL, 0, 1, 8, 0);
  142. p8idct(NULL , temp, dest, line_size, 8, 1, 3);
  143. }