823d359abfa39710f3f1e4309fbdefe19b4c96e3
[swftools.git] / lib / h.263 / dct.c
1 /* dct.c
2
3    DCT implementations and test routines.
4    
5    Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
6
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.
11
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.
16
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 */
20
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};
30
31 static double table[8][8] =
32 {
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}
41 };
42
43 void dct(int*src)
44 {
45     double tmp[64];
46     int x,y,u,v,t;
47
48     for(v=0;v<8;v++)
49     for(u=0;u<8;u++)
50     {
51         double c = 0;
52         for(x=0;x<8;x++)
53         {
54             c+=table[u][x]*src[v*8+x];
55         }
56         tmp[v*8+u] = c;
57     }
58     for(u=0;u<8;u++)
59     for(v=0;v<8;v++)
60     {
61         double c = 0;
62         for(y=0;y<8;y++)
63         {
64             c+=table[v][y]*tmp[y*8+u];
65         }
66         src[v*8+u] = (int)(c*0.25+0.5);
67     }
68 }
69
70 void idct(int*src)
71 {
72     double tmp[64];
73     int x,y,u,v;
74     for(y=0;y<8;y++)
75     for(x=0;x<8;x++)
76     {
77         double c = 0;
78         for(u=0;u<8;u++)
79         {
80             c+=table[u][x]*src[y*8+u];
81         }
82         tmp[y*8+x] = c;
83     }
84     for(y=0;y<8;y++)
85     for(x=0;x<8;x++)
86     {
87         double c = 0;
88         for(v=0;v<8;v++)
89         {
90             c+=table[v][y]*tmp[v*8+x];
91         }
92         src[y*8+x] = (int)(c*0.25+0.5);
93     }
94 }
95
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)
104 };
105
106 static double cc[8];
107 static int ccquant = -1;
108
109 void preparequant(int quant)
110 {
111     if(ccquant == quant)
112         return;
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);
121     ccquant = quant;
122 }
123
124 inline static void innerdct(const double*a,double*b, const double*c)
125 {
126     // c1*c7*2 = c6
127     // c2*c6*2 = c4
128     // c3*c5*2 = c2
129     // c4*c4*2 = 1
130
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;
136     b2 = (a[0]+a[7]);
137     b3 = (a[1]+a[6]);
138     b4 = (a[2]+a[5]);
139     b5 = (a[3]+a[4]);
140
141     b0 = (b2+b5)*c[4];
142     b1 = (b3+b4)*c[4];
143     b[0*8] = b0 + b1;
144     b[4*8] = b0 - b1;
145     b[2*8] = (b2-b5)*c[2] + (b3-b4)*c[6];
146     b[6*8] = (b2-b5)*c[6] + (b4-b3)*c[2];
147
148     b0 = (a[0]-a[7]);
149     b1 = (a[1]-a[6]);
150     b2 = (a[2]-a[5]);
151     b3 = (a[3]-a[4]);
152
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];
157 }
158
159 void dct2(int*src, int*dest)
160 {
161     double tmp[64], tmp2[64];
162     double*p;
163     int u,x,v,t;
164
165     for(t=0;t<64;t++)
166         tmp2[t] = src[t];
167
168     for(v=0;v<8;v++)
169     {
170         double* a=&tmp2[v*8];
171         double* b=&tmp[v];
172         innerdct(a,b,c);
173     }
174     for(v=0;v<8;v++)
175     {
176         double* a=&tmp[v*8];
177         double* b=&tmp2[v];
178         innerdct(a,b,cc);
179     }
180     for(t=0;t<64;t++) {
181         int v = (int)(tmp2[t]);
182         dest[zigzagtable[t]] = v;
183     }
184 }
185
186
187 void zigzag(int*src)
188 {
189     int tmp[64];
190     int t;
191     for(t=0;t<64;t++) {
192         tmp[zigzagtable[t]] = src[t];
193     }
194     memcpy(src, tmp, sizeof(int)*64);
195 }
196
197