1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*************************************************************
Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
Group. If you use this software, you agree to the following: This
program package is purely experimental, and is licensed "as is".
Permission is granted to use, modify, and distribute this program
without charge for any purpose, provided this license/ disclaimer
notice appears in the copies. No warranty or maintenance is given,
either expressed or implied. In no event shall the author(s) be
liable to you or a third party for any special, incidental,
consequential, or other damages, arising out of the use or inability
to use the program for any purpose (or the loss of data), even if we
have been advised of such possibilities. Any public reference or
advertisement of this source code should refer to it as the Portable
Video Research Group (PVRG) code, and not by any author(s) (or
Stanford University) name.
*************************************************************/
/*
************************************************************
chendct.c
A simple DCT algorithm that seems to have fairly nice arithmetic
properties.
W. H. Chen, C. H. Smith and S. C. Fralick "A fast computational
algorithm for the discrete cosine transform," IEEE Trans. Commun.,
vol. COM-25, pp. 1004-1009, Sept 1977.
************************************************************
*/
/*LABEL chendct.c */
/*PUBLIC*/
extern void ChenDct();
extern void ChenIDct();
/*PRIVATE*/
/* Standard Macros */
#define NO_MULTIPLY
#ifdef NO_MULTIPLY
#define LS(r,s) ((r) << (s))
#define RS(r,s) ((r) >> (s)) /* Caution with rounding... */
#else
#define LS(r,s) ((r) * (1 << (s)))
#define RS(r,s) ((r) / (1 << (s))) /* Correct rounding */
#endif
#define MSCALE(expr) RS((expr),9)
/* Cos constants */
#define c1d4 362L
#define c1d8 473L
#define c3d8 196L
#define c1d16 502L
#define c3d16 426L
#define c5d16 284L
#define c7d16 100L
/*
VECTOR_DEFINITION makes the temporary variables vectors.
Useful for machines with small register spaces.
*/
#ifdef VECTOR_DEFINITION
#define a0 a[0]
#define a1 a[1]
#define a2 a[2]
#define a3 a[3]
#define b0 b[0]
#define b1 b[1]
#define b2 b[2]
#define b3 b[3]
#define c0 c[0]
#define c1 c[1]
#define c2 c[2]
#define c3 c[3]
#endif
/*START*/
/*BFUNC
ChenDCT() implements the Chen forward dct. Note that there are two
input vectors that represent x=input, and y=output, and must be
defined (and storage allocated) before this routine is called.
EFUNC*/
void ChenDct(x,y)
int *x;
int *y;
{
register int i;
register int *aptr,*bptr;
#ifdef VECTOR_DEFINITION
register int a[4];
register int b[4];
register int c[4];
#else
register int a0,a1,a2,a3;
register int b0,b1,b2,b3;
register int c0,c1,c2,c3;
#endif
/* Loop over columns */
for(i=0;i<8;i++)
{
aptr = x+i;
bptr = aptr+56;
a0 = LS((*aptr+*bptr),2);
c3 = LS((*aptr-*bptr),2);
aptr += 8;
bptr -= 8;
a1 = LS((*aptr+*bptr),2);
c2 = LS((*aptr-*bptr),2);
aptr += 8;
bptr -= 8;
a2 = LS((*aptr+*bptr),2);
c1 = LS((*aptr-*bptr),2);
aptr += 8;
bptr -= 8;
a3 = LS((*aptr+*bptr),2);
c0 = LS((*aptr-*bptr),2);
b0 = a0+a3;
b1 = a1+a2;
b2 = a1-a2;
b3 = a0-a3;
aptr = y+i;
*aptr = MSCALE(c1d4*(b0+b1));
aptr[32] = MSCALE(c1d4*(b0-b1));
aptr[16] = MSCALE((c3d8*b2)+(c1d8*b3));
aptr[48] = MSCALE((c3d8*b3)-(c1d8*b2));
b0 = MSCALE(c1d4*(c2-c1));
b1 = MSCALE(c1d4*(c2+c1));
a0 = c0+b0;
a1 = c0-b0;
a2 = c3-b1;
a3 = c3+b1;
aptr[8] = MSCALE((c7d16*a0)+(c1d16*a3));
aptr[24] = MSCALE((c3d16*a2)-(c5d16*a1));
aptr[40] = MSCALE((c3d16*a1)+(c5d16*a2));
aptr[56] = MSCALE((c7d16*a3)-(c1d16*a0));
}
for(i=0;i<8;i++)
{ /* Loop over rows */
aptr = y+LS(i,3);
bptr = aptr+7;
c3 = RS((*(aptr)-*(bptr)),1);
a0 = RS((*(aptr++)+*(bptr--)),1);
c2 = RS((*(aptr)-*(bptr)),1);
a1 = RS((*(aptr++)+*(bptr--)),1);
c1 = RS((*(aptr)-*(bptr)),1);
a2 = RS((*(aptr++)+*(bptr--)),1);
c0 = RS((*(aptr)-*(bptr)),1);
a3 = RS((*(aptr)+*(bptr)),1);
b0 = a0+a3;
b1 = a1+a2;
b2 = a1-a2;
b3 = a0-a3;
aptr = y+LS(i,3);
*aptr = MSCALE(c1d4*(b0+b1));
aptr[4] = MSCALE(c1d4*(b0-b1));
aptr[2] = MSCALE((c3d8*b2)+(c1d8*b3));
aptr[6] = MSCALE((c3d8*b3)-(c1d8*b2));
b0 = MSCALE(c1d4*(c2-c1));
b1 = MSCALE(c1d4*(c2+c1));
a0 = c0+b0;
a1 = c0-b0;
a2 = c3-b1;
a3 = c3+b1;
aptr[1] = MSCALE((c7d16*a0)+(c1d16*a3));
aptr[3] = MSCALE((c3d16*a2)-(c5d16*a1));
aptr[5] = MSCALE((c3d16*a1)+(c5d16*a2));
aptr[7] = MSCALE((c7d16*a3)-(c1d16*a0));
}
/* We have an additional factor of 8 in the Chen algorithm. */
for(i=0,aptr=y;i<64;i++,aptr++)
*aptr = (((*aptr<0) ? (*aptr-4) : (*aptr+4))/8);
}
/*BFUNC
ChenIDCT() implements the Chen inverse dct. Note that there are two
input vectors that represent x=input, and y=output, and must be
defined (and storage allocated) before this routine is called.
EFUNC*/
void ChenIDct(x,y)
int *x;
int *y;
{
register int i;
register int *aptr;
#ifdef VECTOR_DEFINITION
register int a[4];
register int b[4];
register int c[4];
#else
register int a0,a1,a2,a3;
register int b0,b1,b2,b3;
register int c0,c1,c2,c3;
#endif
/* Loop over columns */
for(i=0;i<8;i++)
{
aptr = x+i;
b0 = LS(*aptr,2);
aptr += 8;
a0 = LS(*aptr,2);
aptr += 8;
b2 = LS(*aptr,2);
aptr += 8;
a1 = LS(*aptr,2);
aptr += 8;
b1 = LS(*aptr,2);
aptr += 8;
a2 = LS(*aptr,2);
aptr += 8;
b3 = LS(*aptr,2);
aptr += 8;
a3 = LS(*aptr,2);
/* Split into even mode b0 = x0 b1 = x4 b2 = x2 b3 = x6.
And the odd terms a0 = x1 a1 = x3 a2 = x5 a3 = x7.
*/
c0 = MSCALE((c7d16*a0)-(c1d16*a3));
c1 = MSCALE((c3d16*a2)-(c5d16*a1));
c2 = MSCALE((c3d16*a1)+(c5d16*a2));
c3 = MSCALE((c1d16*a0)+(c7d16*a3));
/* First Butterfly on even terms.*/
a0 = MSCALE(c1d4*(b0+b1));
a1 = MSCALE(c1d4*(b0-b1));
a2 = MSCALE((c3d8*b2)-(c1d8*b3));
a3 = MSCALE((c1d8*b2)+(c3d8*b3));
b0 = a0+a3;
b1 = a1+a2;
b2 = a1-a2;
b3 = a0-a3;
/* Second Butterfly */
a0 = c0+c1;
a1 = c0-c1;
a2 = c3-c2;
a3 = c3+c2;
c0 = a0;
c1 = MSCALE(c1d4*(a2-a1));
c2 = MSCALE(c1d4*(a2+a1));
c3 = a3;
aptr = y+i;
*aptr = b0+c3;
aptr += 8;
*aptr = b1+c2;
aptr += 8;
*aptr = b2+c1;
aptr += 8;
*aptr = b3+c0;
aptr += 8;
*aptr = b3-c0;
aptr += 8;
*aptr = b2-c1;
aptr += 8;
*aptr = b1-c2;
aptr += 8;
*aptr = b0-c3;
}
/* Loop over rows */
for(i=0;i<8;i++)
{
aptr = y+LS(i,3);
b0 = *(aptr++);
a0 = *(aptr++);
b2 = *(aptr++);
a1 = *(aptr++);
b1 = *(aptr++);
a2 = *(aptr++);
b3 = *(aptr++);
a3 = *(aptr);
/*
Split into even mode b0 = x0 b1 = x4 b2 = x2 b3 = x6.
And the odd terms a0 = x1 a1 = x3 a2 = x5 a3 = x7.
*/
c0 = MSCALE((c7d16*a0)-(c1d16*a3));
c1 = MSCALE((c3d16*a2)-(c5d16*a1));
c2 = MSCALE((c3d16*a1)+(c5d16*a2));
c3 = MSCALE((c1d16*a0)+(c7d16*a3));
/* First Butterfly on even terms.*/
a0 = MSCALE(c1d4*(b0+b1));
a1 = MSCALE(c1d4*(b0-b1));
a2 = MSCALE((c3d8*b2)-(c1d8*b3));
a3 = MSCALE((c1d8*b2)+(c3d8*b3));
/* Calculate last set of b's */
b0 = a0+a3;
b1 = a1+a2;
b2 = a1-a2;
b3 = a0-a3;
/* Second Butterfly */
a0 = c0+c1;
a1 = c0-c1;
a2 = c3-c2;
a3 = c3+c2;
c0 = a0;
c1 = MSCALE(c1d4*(a2-a1));
c2 = MSCALE(c1d4*(a2+a1));
c3 = a3;
aptr = y+LS(i,3);
*(aptr++) = b0+c3;
*(aptr++) = b1+c2;
*(aptr++) = b2+c1;
*(aptr++) = b3+c0;
*(aptr++) = b3-c0;
*(aptr++) = b2-c1;
*(aptr++) = b1-c2;
*(aptr) = b0-c3;
}
/*
Retrieve correct accuracy. We have additional factor
of 16 that must be removed.
*/
for(i=0,aptr=y;i<64;i++,aptr++)
*aptr = (((*aptr<0) ? (*aptr-8) : (*aptr+8)) /16);
}
/*END*/
|