/AMVmuxer/ffmpeg/libavcodec/fdctref.c

http://amv-codec-tools.googlecode.com/ · C · 157 lines · 82 code · 25 blank · 50 comment · 12 complexity · 7f25046d3dc638318ad6685ed056f290 MD5 · raw file

  1. /**
  2. * @file fdctref.c
  3. * forward discrete cosine transform, double precision.
  4. */
  5. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  6. /*
  7. * Disclaimer of Warranty
  8. *
  9. * These software programs are available to the user without any license fee or
  10. * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
  11. * any and all warranties, whether express, implied, or statuary, including any
  12. * implied warranties or merchantability or of fitness for a particular
  13. * purpose. In no event shall the copyright-holder be liable for any
  14. * incidental, punitive, or consequential damages of any kind whatsoever
  15. * arising from the use of these programs.
  16. *
  17. * This disclaimer of warranty extends to the user of these programs and user's
  18. * customers, employees, agents, transferees, successors, and assigns.
  19. *
  20. * The MPEG Software Simulation Group does not represent or warrant that the
  21. * programs furnished hereunder are free of infringement of any third-party
  22. * patents.
  23. *
  24. * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  25. * are subject to royalty fees to patent holders. Many of these patents are
  26. * general enough such that they are unavoidable regardless of implementation
  27. * design.
  28. */
  29. #include <math.h>
  30. #ifndef PI
  31. # ifdef M_PI
  32. # define PI M_PI
  33. # else
  34. # define PI 3.14159265358979323846
  35. # endif
  36. #endif
  37. /* global declarations */
  38. void init_fdct (void);
  39. void fdct (short *block);
  40. /* private data */
  41. static double c[8][8]; /* transform coefficients */
  42. void init_fdct()
  43. {
  44. int i, j;
  45. double s;
  46. for (i=0; i<8; i++)
  47. {
  48. s = (i==0) ? sqrt(0.125) : 0.5;
  49. for (j=0; j<8; j++)
  50. c[i][j] = s * cos((PI/8.0)*i*(j+0.5));
  51. }
  52. }
  53. void fdct(block)
  54. short *block;
  55. {
  56. register int i, j;
  57. double s;
  58. double tmp[64];
  59. for(i = 0; i < 8; i++)
  60. for(j = 0; j < 8; j++)
  61. {
  62. s = 0.0;
  63. /*
  64. * for(k = 0; k < 8; k++)
  65. * s += c[j][k] * block[8 * i + k];
  66. */
  67. s += c[j][0] * block[8 * i + 0];
  68. s += c[j][1] * block[8 * i + 1];
  69. s += c[j][2] * block[8 * i + 2];
  70. s += c[j][3] * block[8 * i + 3];
  71. s += c[j][4] * block[8 * i + 4];
  72. s += c[j][5] * block[8 * i + 5];
  73. s += c[j][6] * block[8 * i + 6];
  74. s += c[j][7] * block[8 * i + 7];
  75. tmp[8 * i + j] = s;
  76. }
  77. for(j = 0; j < 8; j++)
  78. for(i = 0; i < 8; i++)
  79. {
  80. s = 0.0;
  81. /*
  82. * for(k = 0; k < 8; k++)
  83. * s += c[i][k] * tmp[8 * k + j];
  84. */
  85. s += c[i][0] * tmp[8 * 0 + j];
  86. s += c[i][1] * tmp[8 * 1 + j];
  87. s += c[i][2] * tmp[8 * 2 + j];
  88. s += c[i][3] * tmp[8 * 3 + j];
  89. s += c[i][4] * tmp[8 * 4 + j];
  90. s += c[i][5] * tmp[8 * 5 + j];
  91. s += c[i][6] * tmp[8 * 6 + j];
  92. s += c[i][7] * tmp[8 * 7 + j];
  93. s*=8.0;
  94. block[8 * i + j] = (short)floor(s + 0.499999);
  95. /*
  96. * reason for adding 0.499999 instead of 0.5:
  97. * s is quite often x.5 (at least for i and/or j = 0 or 4)
  98. * and setting the rounding threshold exactly to 0.5 leads to an
  99. * extremely high arithmetic implementation dependency of the result;
  100. * s being between x.5 and x.500001 (which is now incorrectly rounded
  101. * downwards instead of upwards) is assumed to occur less often
  102. * (if at all)
  103. */
  104. }
  105. }
  106. /* perform IDCT matrix multiply for 8x8 coefficient block */
  107. void idct(block)
  108. short *block;
  109. {
  110. int i, j, k, v;
  111. double partial_product;
  112. double tmp[64];
  113. for (i=0; i<8; i++)
  114. for (j=0; j<8; j++)
  115. {
  116. partial_product = 0.0;
  117. for (k=0; k<8; k++)
  118. partial_product+= c[k][j]*block[8*i+k];
  119. tmp[8*i+j] = partial_product;
  120. }
  121. /* Transpose operation is integrated into address mapping by switching
  122. loop order of i and j */
  123. for (j=0; j<8; j++)
  124. for (i=0; i<8; i++)
  125. {
  126. partial_product = 0.0;
  127. for (k=0; k<8; k++)
  128. partial_product+= c[k][i]*tmp[8*k+j];
  129. v = (int) floor(partial_product+0.5);
  130. block[8*i+j] = v;
  131. }
  132. }