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.
5 // This is a Go translation of idct.c from
7 // http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_IEC_13818-4_2004_Conformance_Testing/Video/verifier/mpeg2decode_960109.tar.gz
9 // which carries the following notice:
11 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
14 * Disclaimer of Warranty
16 * These software programs are available to the user without any license fee or
17 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
18 * any and all warranties, whether express, implied, or statuary, including any
19 * implied warranties or merchantability or of fitness for a particular
20 * purpose. In no event shall the copyright-holder be liable for any
21 * incidental, punitive, or consequential damages of any kind whatsoever
22 * arising from the use of these programs.
24 * This disclaimer of warranty extends to the user of these programs and user's
25 * customers, employees, agents, transferees, successors, and assigns.
27 * The MPEG Software Simulation Group does not represent or warrant that the
28 * programs furnished hereunder are free of infringement of any third-party
31 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
32 * are subject to royalty fees to patent holders. Many of these patents are
33 * general enough such that they are unavoidable regardless of implementation
41 w1 = 2841 // 2048*sqrt(2)*cos(1*pi/16)
42 w2 = 2676 // 2048*sqrt(2)*cos(2*pi/16)
43 w3 = 2408 // 2048*sqrt(2)*cos(3*pi/16)
44 w5 = 1609 // 2048*sqrt(2)*cos(5*pi/16)
45 w6 = 1108 // 2048*sqrt(2)*cos(6*pi/16)
46 w7 = 565 // 2048*sqrt(2)*cos(7*pi/16)
55 r2 = 181 // 256/sqrt(2)
58 // 2-D Inverse Discrete Cosine Transformation, followed by a +128 level shift.
60 // The input coefficients should already have been multiplied by the appropriate quantization table.
61 // We use fixed-point computation, with the number of bits for the fractional component varying over the
62 // intermediate stages. The final values are expected to range within [0, 255], after a +128 level shift.
64 // For more on the actual algorithm, see Z. Wang, "Fast algorithms for the discrete W transform and
65 // for the discrete Fourier transform", IEEE Trans. on ASSP, Vol. ASSP- 32, pp. 803-816, Aug. 1984.
67 // Horizontal 1-D IDCT.
68 for y := 0; y < 8; y++ {
69 // If all the AC components are zero, then the IDCT is trivial.
70 if b[y*8+1] == 0 && b[y*8+2] == 0 && b[y*8+3] == 0 &&
71 b[y*8+4] == 0 && b[y*8+5] == 0 && b[y*8+6] == 0 && b[y*8+7] == 0 {
85 x0 := (b[y*8+0] << 11) + 128
118 x2 = (r2*(x4+x5) + 128) >> 8
119 x4 = (r2*(x4-x5) + 128) >> 8
122 b[8*y+0] = (x7 + x1) >> 8
123 b[8*y+1] = (x3 + x2) >> 8
124 b[8*y+2] = (x0 + x4) >> 8
125 b[8*y+3] = (x8 + x6) >> 8
126 b[8*y+4] = (x8 - x6) >> 8
127 b[8*y+5] = (x0 - x4) >> 8
128 b[8*y+6] = (x3 - x2) >> 8
129 b[8*y+7] = (x7 - x1) >> 8
132 // Vertical 1-D IDCT.
133 for x := 0; x < 8; x++ {
134 // Similar to the horizontal 1-D IDCT case, if all the AC components are zero, then the IDCT is trivial.
135 // However, after performing the horizontal 1-D IDCT, there are typically non-zero AC components, so
136 // we do not bother to check for the all-zero case.
139 y0 := (b[8*0+x] << 8) + 8192
150 y4 = (y8 + w1mw7*y4) >> 3
151 y5 = (y8 - w1pw7*y5) >> 3
153 y6 = (y8 - w3mw5*y6) >> 3
154 y7 = (y8 - w3pw5*y7) >> 3
160 y2 = (y1 - w2pw6*y2) >> 3
161 y3 = (y1 + w2mw6*y3) >> 3
172 y2 = (r2*(y4+y5) + 128) >> 8
173 y4 = (r2*(y4-y5) + 128) >> 8
176 b[8*0+x] = (y7 + y1) >> 14
177 b[8*1+x] = (y3 + y2) >> 14
178 b[8*2+x] = (y0 + y4) >> 14
179 b[8*3+x] = (y8 + y6) >> 14
180 b[8*4+x] = (y8 - y6) >> 14
181 b[8*5+x] = (y0 - y4) >> 14
182 b[8*6+x] = (y3 - y2) >> 14
183 b[8*7+x] = (y7 - y1) >> 14