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.
7 // This is a Go translation of idct.c from
9 // http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_IEC_13818-4_2004_Conformance_Testing/Video/verifier/mpeg2decode_960109.tar.gz
11 // which carries the following notice:
13 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
16 * Disclaimer of Warranty
18 * These software programs are available to the user without any license fee or
19 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
20 * any and all warranties, whether express, implied, or statuary, including any
21 * implied warranties or merchantability or of fitness for a particular
22 * purpose. In no event shall the copyright-holder be liable for any
23 * incidental, punitive, or consequential damages of any kind whatsoever
24 * arising from the use of these programs.
26 * This disclaimer of warranty extends to the user of these programs and user's
27 * customers, employees, agents, transferees, successors, and assigns.
29 * The MPEG Software Simulation Group does not represent or warrant that the
30 * programs furnished hereunder are free of infringement of any third-party
33 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
34 * are subject to royalty fees to patent holders. Many of these patents are
35 * general enough such that they are unavoidable regardless of implementation
40 const blockSize
= 64 // A DCT block is 8x8.
42 type block
[blockSize
]int32
45 w1
= 2841 // 2048*sqrt(2)*cos(1*pi/16)
46 w2
= 2676 // 2048*sqrt(2)*cos(2*pi/16)
47 w3
= 2408 // 2048*sqrt(2)*cos(3*pi/16)
48 w5
= 1609 // 2048*sqrt(2)*cos(5*pi/16)
49 w6
= 1108 // 2048*sqrt(2)*cos(6*pi/16)
50 w7
= 565 // 2048*sqrt(2)*cos(7*pi/16)
59 r2
= 181 // 256/sqrt(2)
62 // idct performs a 2-D Inverse Discrete Cosine Transformation.
64 // The input coefficients should already have been multiplied by the
65 // appropriate quantization table. We use fixed-point computation, with the
66 // number of bits for the fractional component varying over the intermediate
69 // For more on the actual algorithm, see Z. Wang, "Fast algorithms for the
70 // discrete W transform and for the discrete Fourier transform", IEEE Trans. on
71 // ASSP, Vol. ASSP- 32, pp. 803-816, Aug. 1984.
72 func idct(src
*block
) {
73 // Horizontal 1-D IDCT.
74 for y
:= 0; y
< 8; y
++ {
76 // If all the AC components are zero, then the IDCT is trivial.
77 if src
[y8
+1] == 0 && src
[y8
+2] == 0 && src
[y8
+3] == 0 &&
78 src
[y8
+4] == 0 && src
[y8
+5] == 0 && src
[y8
+6] == 0 && src
[y8
+7] == 0 {
92 x0
:= (src
[y8
+0] << 11) + 128
125 x2
= (r2
*(x4
+x5
) + 128) >> 8
126 x4
= (r2
*(x4
-x5
) + 128) >> 8
129 src
[y8
+0] = (x7
+ x1
) >> 8
130 src
[y8
+1] = (x3
+ x2
) >> 8
131 src
[y8
+2] = (x0
+ x4
) >> 8
132 src
[y8
+3] = (x8
+ x6
) >> 8
133 src
[y8
+4] = (x8
- x6
) >> 8
134 src
[y8
+5] = (x0
- x4
) >> 8
135 src
[y8
+6] = (x3
- x2
) >> 8
136 src
[y8
+7] = (x7
- x1
) >> 8
139 // Vertical 1-D IDCT.
140 for x
:= 0; x
< 8; x
++ {
141 // Similar to the horizontal 1-D IDCT case, if all the AC components are zero, then the IDCT is trivial.
142 // However, after performing the horizontal 1-D IDCT, there are typically non-zero AC components, so
143 // we do not bother to check for the all-zero case.
146 y0
:= (src
[8*0+x
] << 8) + 8192
147 y1
:= src
[8*4+x
] << 8
157 y4
= (y8
+ w1mw7
*y4
) >> 3
158 y5
= (y8
- w1pw7
*y5
) >> 3
160 y6
= (y8
- w3mw5
*y6
) >> 3
161 y7
= (y8
- w3pw5
*y7
) >> 3
167 y2
= (y1
- w2pw6
*y2
) >> 3
168 y3
= (y1
+ w2mw6
*y3
) >> 3
179 y2
= (r2
*(y4
+y5
) + 128) >> 8
180 y4
= (r2
*(y4
-y5
) + 128) >> 8
183 src
[8*0+x
] = (y7
+ y1
) >> 14
184 src
[8*1+x
] = (y3
+ y2
) >> 14
185 src
[8*2+x
] = (y0
+ y4
) >> 14
186 src
[8*3+x
] = (y8
+ y6
) >> 14
187 src
[8*4+x
] = (y8
- y6
) >> 14
188 src
[8*5+x
] = (y0
- y4
) >> 14
189 src
[8*6+x
] = (y3
- y2
) >> 14
190 src
[8*7+x
] = (y7
- y1
) >> 14