You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.9KB

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