idct.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package jpeg
  5. // This is a Go translation of idct.c from
  6. //
  7. // http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_IEC_13818-4_2004_Conformance_Testing/Video/verifier/mpeg2decode_960109.tar.gz
  8. //
  9. // which carries the following notice:
  10. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  11. /*
  12. * Disclaimer of Warranty
  13. *
  14. * These software programs are available to the user without any license fee or
  15. * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
  16. * any and all warranties, whether express, implied, or statuary, including any
  17. * implied warranties or merchantability or of fitness for a particular
  18. * purpose. In no event shall the copyright-holder be liable for any
  19. * incidental, punitive, or consequential damages of any kind whatsoever
  20. * arising from the use of these programs.
  21. *
  22. * This disclaimer of warranty extends to the user of these programs and user's
  23. * customers, employees, agents, transferees, successors, and assigns.
  24. *
  25. * The MPEG Software Simulation Group does not represent or warrant that the
  26. * programs furnished hereunder are free of infringement of any third-party
  27. * patents.
  28. *
  29. * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  30. * are subject to royalty fees to patent holders. Many of these patents are
  31. * general enough such that they are unavoidable regardless of implementation
  32. * design.
  33. *
  34. */
  35. const blockSize = 64 // A DCT block is 8x8.
  36. type block [blockSize]int32
  37. const (
  38. w1 = 2841 // 2048*sqrt(2)*cos(1*pi/16)
  39. w2 = 2676 // 2048*sqrt(2)*cos(2*pi/16)
  40. w3 = 2408 // 2048*sqrt(2)*cos(3*pi/16)
  41. w5 = 1609 // 2048*sqrt(2)*cos(5*pi/16)
  42. w6 = 1108 // 2048*sqrt(2)*cos(6*pi/16)
  43. w7 = 565 // 2048*sqrt(2)*cos(7*pi/16)
  44. w1pw7 = w1 + w7
  45. w1mw7 = w1 - w7
  46. w2pw6 = w2 + w6
  47. w2mw6 = w2 - w6
  48. w3pw5 = w3 + w5
  49. w3mw5 = w3 - w5
  50. r2 = 181 // 256/sqrt(2)
  51. )
  52. // idct performs a 2-D Inverse Discrete Cosine Transformation.
  53. //
  54. // The input coefficients should already have been multiplied by the
  55. // appropriate quantization table. We use fixed-point computation, with the
  56. // number of bits for the fractional component varying over the intermediate
  57. // stages.
  58. //
  59. // For more on the actual algorithm, see Z. Wang, "Fast algorithms for the
  60. // discrete W transform and for the discrete Fourier transform", IEEE Trans. on
  61. // ASSP, Vol. ASSP- 32, pp. 803-816, Aug. 1984.
  62. func idct(src *block) {
  63. // Horizontal 1-D IDCT.
  64. for y := 0; y < 8; y++ {
  65. y8 := y * 8
  66. // If all the AC components are zero, then the IDCT is trivial.
  67. if src[y8+1] == 0 && src[y8+2] == 0 && src[y8+3] == 0 &&
  68. src[y8+4] == 0 && src[y8+5] == 0 && src[y8+6] == 0 && src[y8+7] == 0 {
  69. dc := src[y8+0] << 3
  70. src[y8+0] = dc
  71. src[y8+1] = dc
  72. src[y8+2] = dc
  73. src[y8+3] = dc
  74. src[y8+4] = dc
  75. src[y8+5] = dc
  76. src[y8+6] = dc
  77. src[y8+7] = dc
  78. continue
  79. }
  80. // Prescale.
  81. x0 := (src[y8+0] << 11) + 128
  82. x1 := src[y8+4] << 11
  83. x2 := src[y8+6]
  84. x3 := src[y8+2]
  85. x4 := src[y8+1]
  86. x5 := src[y8+7]
  87. x6 := src[y8+5]
  88. x7 := src[y8+3]
  89. // Stage 1.
  90. x8 := w7 * (x4 + x5)
  91. x4 = x8 + w1mw7*x4
  92. x5 = x8 - w1pw7*x5
  93. x8 = w3 * (x6 + x7)
  94. x6 = x8 - w3mw5*x6
  95. x7 = x8 - w3pw5*x7
  96. // Stage 2.
  97. x8 = x0 + x1
  98. x0 -= x1
  99. x1 = w6 * (x3 + x2)
  100. x2 = x1 - w2pw6*x2
  101. x3 = x1 + w2mw6*x3
  102. x1 = x4 + x6
  103. x4 -= x6
  104. x6 = x5 + x7
  105. x5 -= x7
  106. // Stage 3.
  107. x7 = x8 + x3
  108. x8 -= x3
  109. x3 = x0 + x2
  110. x0 -= x2
  111. x2 = (r2*(x4+x5) + 128) >> 8
  112. x4 = (r2*(x4-x5) + 128) >> 8
  113. // Stage 4.
  114. src[y8+0] = (x7 + x1) >> 8
  115. src[y8+1] = (x3 + x2) >> 8
  116. src[y8+2] = (x0 + x4) >> 8
  117. src[y8+3] = (x8 + x6) >> 8
  118. src[y8+4] = (x8 - x6) >> 8
  119. src[y8+5] = (x0 - x4) >> 8
  120. src[y8+6] = (x3 - x2) >> 8
  121. src[y8+7] = (x7 - x1) >> 8
  122. }
  123. // Vertical 1-D IDCT.
  124. for x := 0; x < 8; x++ {
  125. // Similar to the horizontal 1-D IDCT case, if all the AC components are zero, then the IDCT is trivial.
  126. // However, after performing the horizontal 1-D IDCT, there are typically non-zero AC components, so
  127. // we do not bother to check for the all-zero case.
  128. // Prescale.
  129. y0 := (src[8*0+x] << 8) + 8192
  130. y1 := src[8*4+x] << 8
  131. y2 := src[8*6+x]
  132. y3 := src[8*2+x]
  133. y4 := src[8*1+x]
  134. y5 := src[8*7+x]
  135. y6 := src[8*5+x]
  136. y7 := src[8*3+x]
  137. // Stage 1.
  138. y8 := w7*(y4+y5) + 4
  139. y4 = (y8 + w1mw7*y4) >> 3
  140. y5 = (y8 - w1pw7*y5) >> 3
  141. y8 = w3*(y6+y7) + 4
  142. y6 = (y8 - w3mw5*y6) >> 3
  143. y7 = (y8 - w3pw5*y7) >> 3
  144. // Stage 2.
  145. y8 = y0 + y1
  146. y0 -= y1
  147. y1 = w6*(y3+y2) + 4
  148. y2 = (y1 - w2pw6*y2) >> 3
  149. y3 = (y1 + w2mw6*y3) >> 3
  150. y1 = y4 + y6
  151. y4 -= y6
  152. y6 = y5 + y7
  153. y5 -= y7
  154. // Stage 3.
  155. y7 = y8 + y3
  156. y8 -= y3
  157. y3 = y0 + y2
  158. y0 -= y2
  159. y2 = (r2*(y4+y5) + 128) >> 8
  160. y4 = (r2*(y4-y5) + 128) >> 8
  161. // Stage 4.
  162. src[8*0+x] = (y7 + y1) >> 14
  163. src[8*1+x] = (y3 + y2) >> 14
  164. src[8*2+x] = (y0 + y4) >> 14
  165. src[8*3+x] = (y8 + y6) >> 14
  166. src[8*4+x] = (y8 - y6) >> 14
  167. src[8*5+x] = (y0 - y4) >> 14
  168. src[8*6+x] = (y3 - y2) >> 14
  169. src[8*7+x] = (y7 - y1) >> 14
  170. }
  171. }