1
0

idctref.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "idctref.h"
  2. #include <math.h>
  3. #ifndef PI
  4. # ifdef M_PI
  5. # define PI M_PI
  6. # else
  7. # define PI 3.14159265358979323846
  8. # endif
  9. #endif
  10. bool IDCTRef::initted = false;
  11. double IDCTRef::c[8][8]; /* cosine transform matrix for 8x1 IDCT */
  12. /* initialize DCT coefficient matrix */
  13. void IDCTRef::init()
  14. {
  15. if (!initted)
  16. {
  17. int freq, time;
  18. for (freq=0; freq < 8; freq++)
  19. {
  20. double scale = (freq == 0) ? sqrt(0.125) : 0.5;
  21. for (time=0; time<8; time++)
  22. c[freq][time] = scale*cos((PI/8.0)*freq*(time + 0.5));
  23. }
  24. initted=true;
  25. }
  26. }
  27. /* perform IDCT matrix multiply for 8x8 coefficient block */
  28. void IDCTRef::idct(short *block)
  29. {
  30. int i, j, k, v;
  31. double partial_product;
  32. double tmp[64];
  33. for (i=0; i<8; i++)
  34. for (j=0; j<8; j++)
  35. {
  36. partial_product = 0.0;
  37. for (k=0; k<8; k++)
  38. partial_product+= c[k][j]*block[8*i+k];
  39. tmp[8*i+j] = partial_product;
  40. }
  41. /* Transpose operation is integrated into address mapping by switching
  42. loop order of i and j */
  43. for (j=0; j<8; j++)
  44. for (i=0; i<8; i++)
  45. {
  46. partial_product = 0.0;
  47. for (k=0; k<8; k++)
  48. partial_product+= c[k][i]*tmp[8*k+j];
  49. v = (int)floor(partial_product+0.5);
  50. block[8*i+j] = (v<-256) ? -256 : ((v>255) ? 255 : v);
  51. }
  52. }