3 DCT implementations and test routines.
5 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21 int zigzagtable[64] = {
22 0, 1, 5, 6, 14, 15, 27, 28,
23 2, 4, 7, 13, 16, 26, 29, 42,
24 3, 8, 12, 17, 25, 30, 41, 43,
25 9, 11, 18, 24, 31, 40, 44, 53,
26 10, 19, 23, 32, 39, 45, 52, 54,
27 20, 22, 33, 38, 46, 51, 55, 60,
28 21, 34, 37, 47, 50, 56, 59, 61,
29 35, 36, 48, 49, 57, 58, 62, 63};
31 static double table[8][8] =
33 {0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548},
34 {0.980785280403230,0.831469612302545,0.555570233019602,0.195090322016128,-0.195090322016128,-0.555570233019602,-0.831469612302545,-0.980785280403230},
35 {0.923879532511287,0.382683432365090,-0.382683432365090,-0.923879532511287,-0.923879532511287,-0.382683432365090,0.382683432365090,0.923879532511287},
36 {0.831469612302545,-0.195090322016128,-0.980785280403230,-0.555570233019602,0.555570233019602,0.980785280403230,0.195090322016129,-0.831469612302545},
37 {0.707106781186548,-0.707106781186547,-0.707106781186548,0.707106781186547,0.707106781186548,-0.707106781186547,-0.707106781186547,0.707106781186547},
38 {0.555570233019602,-0.980785280403230,0.195090322016128,0.831469612302545,-0.831469612302545,-0.195090322016128,0.980785280403231,-0.555570233019602},
39 {0.382683432365090,-0.923879532511287,0.923879532511287,-0.382683432365090,-0.382683432365091,0.923879532511287,-0.923879532511286,0.382683432365090},
40 {0.195090322016128,-0.555570233019602,0.831469612302545,-0.980785280403231,0.980785280403230,-0.831469612302545,0.555570233019602,-0.195090322016129}
54 c+=table[u][x]*src[v*8+x];
64 c+=table[v][y]*tmp[y*8+u];
66 src[v*8+u] = (int)(c*0.25+0.5);
80 c+=table[u][x]*src[y*8+u];
90 c+=table[v][y]*tmp[v*8+x];
92 src[y*8+x] = (int)(c*0.25+0.5);
96 static double c[8] = {1.0,
97 0.980785280403230, // cos(Pi*1/16), sin(Pi*7/16)
98 0.923879532511287, // cos(Pi*2/16), sin(Pi*6/16)
99 0.831469612302545, // cos(Pi*3/16), sin(Pi*5/16)
100 0.707106781186548, // cos(Pi*4/16), sin(Pi*4/16), 1/sqrt(2)
101 0.555570233019602, // cos(Pi*5/16), sin(Pi*3/16)
102 0.382683432365090, // cos(Pi*6/16), sin(Pi*2/16)
103 0.195090322016128 // cos(Pi*7/16), sin(Pi*1/16)
107 static int ccquant = -1;
109 void preparequant(int quant)
113 cc[0] = c[0]/(quant*2*4);
114 cc[1] = c[1]/(quant*2*4);
115 cc[2] = c[2]/(quant*2*4);
116 cc[3] = c[3]/(quant*2*4);
117 cc[4] = c[4]/(quant*2*4);
118 cc[5] = c[5]/(quant*2*4);
119 cc[6] = c[6]/(quant*2*4);
120 cc[7] = c[7]/(quant*2*4);
124 inline static void innerdct(const double*a,double*b, const double*c)
131 //{ 1, 3, 5, 7, -7, -5, -3, -1},
132 //{ 3, -7, -1, -5, 5, 1, 7, -3},
133 //{ 5, -1, 7, 3, -3, -7, 1, -5},
134 //{ 7, -5, 3, -1, 1, -3, 5, -7}
135 double b0,b1,b2,b3,b4,b5;
145 b[2*8] = (b2-b5)*c[2] + (b3-b4)*c[6];
146 b[6*8] = (b2-b5)*c[6] + (b4-b3)*c[2];
153 b[1*8] = b0*c[1] + b1*c[3] + b2*c[5] + b3*c[7];
154 b[3*8] = b0*c[3] - b1*c[7] - b2*c[1] - b3*c[5];
155 b[5*8] = b0*c[5] - b1*c[1] + b2*c[7] + b3*c[3];
156 b[7*8] = b0*c[7] - b1*c[5] + b2*c[3] - b3*c[1];
159 void dct2(int*src, int*dest)
161 double tmp[64], tmp2[64];
170 double* a=&tmp2[v*8];
181 int v = (int)(tmp2[t]);
182 dest[zigzagtable[t]] = v;
192 tmp[zigzagtable[t]] = src[t];
194 memcpy(src, tmp, sizeof(int)*64);