mask mousewheel segv bug, mask opengl sw fallback read to ram, fix tiff config withou...
[goodguy/cinelerra.git] / cinelerra-5.1 / libzmpeg3 / video / idct.C
1 #include "../libzmpeg3.h"
2
3 /**********************************************************/
4 /* inverse two dimensional DCT, Chen-Wang algorithm       */
5 /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984)             */
6 /* 32-bit integer arithmetic (8 bit coefficients)         */
7 /* 11 mults, 29 adds per DCT                              */
8 /*                                      sE, 18.8.91       */
9 /**********************************************************/
10 /* coefficients extended to 12 bit for IEEE1180-1990      */
11 /* compliance                           sE,  2.1.94       */
12 /**********************************************************/
13
14 /* this code assumes >> to be a two's-complement arithmetic */
15 /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2               */
16
17 #define W1 2841 /* 2048*sqrt(2)*cos(1*pi/16) */
18 #define W2 2676 /* 2048*sqrt(2)*cos(2*pi/16) */
19 #define W3 2408 /* 2048*sqrt(2)*cos(3*pi/16) */
20 #define W5 1609 /* 2048*sqrt(2)*cos(5*pi/16) */
21 #define W6 1108 /* 2048*sqrt(2)*cos(6*pi/16) */
22 #define W7 565  /* 2048*sqrt(2)*cos(7*pi/16) */
23
24 /* row (horizontal) IDCT
25  *
26  *           7                       pi         1
27  * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
28  *          l=0                      8          2
29  *
30  * where: c[0]    = 128
31  *        c[1..7] = 128*sqrt(2)
32  */
33
34 static int idct_row(short *blk)
35 {
36   int x0, x1, x2, x3, x4, x5, x6, x7, x8;
37
38   /* shortcut */
39   if( !((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
40         (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])) ) {
41     blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
42     return 0;
43   }
44
45   x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */
46
47   x8 = W7*(x4+x5);         /* first stage */
48   x4 = x8 + (W1-W7)*x4;
49   x5 = x8 - (W1+W7)*x5;
50   x8 = W3*(x6+x7);
51   x6 = x8 - (W3-W5)*x6;
52   x7 = x8 - (W3+W5)*x7;
53
54   x8 = x0 + x1;            /* second stage */
55   x0 -= x1;
56   x1 = W6*(x3+x2);
57   x2 = x1 - (W2+W6)*x2;
58   x3 = x1 + (W2-W6)*x3;
59   x1 = x4 + x6;
60   x4 -= x6;
61   x6 = x5 + x7;
62   x5 -= x7;
63
64   x7 = x8 + x3;            /* third stage */
65   x8 -= x3;
66   x3 = x0 + x2;
67   x0 -= x2;
68   x2 = (181*(x4+x5)+128)>>8;
69   x4 = (181*(x4-x5)+128)>>8;
70
71   blk[0] = (x7+x1)>>8;     /* fourth stage */
72   blk[1] = (x3+x2)>>8;
73   blk[2] = (x0+x4)>>8;
74   blk[3] = (x8+x6)>>8;
75   blk[4] = (x8-x6)>>8;
76   blk[5] = (x0-x4)>>8;
77   blk[6] = (x3-x2)>>8;
78   blk[7] = (x7-x1)>>8;
79   return 0;
80 }
81
82 /* column (vertical) IDCT
83  *
84  *             7                         pi         1
85  * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
86  *            l=0                        8          2
87  *
88  * where: c[0]    = 1/1024
89  *        c[1..7] = (1/1024)*sqrt(2)
90  */
91
92 static int idct_col(short *blk)
93 {
94   int x0, x1, x2, x3, x4, x5, x6, x7, x8;
95
96   /* shortcut */
97   if( !((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
98         (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) |
99         (x7 = blk[8 * 3])) ) {
100     blk[8*0] = blk[8*1] = blk[8*2] = blk[8*3] =
101     blk[8*4] = blk[8*5] = blk[8*6] = blk[8*7] = (blk[8*0]+32)>>6;
102     return 0;
103   }
104
105   x0 = (blk[8*0]<<8) + 8192;
106
107   x8 = W7*(x4+x5) + 4;     /* first stage */
108   x4 = (x8+(W1-W7)*x4)>>3;
109   x5 = (x8-(W1+W7)*x5)>>3;
110   x8 = W3*(x6+x7) + 4;
111   x6 = (x8-(W3-W5)*x6)>>3;
112   x7 = (x8-(W3+W5)*x7)>>3;
113
114   x8 = x0 + x1;            /* second stage */
115   x0 -= x1;
116   x1 = W6*(x3+x2) + 4;
117   x2 = (x1-(W2+W6)*x2)>>3;
118   x3 = (x1+(W2-W6)*x3)>>3;
119   x1 = x4 + x6;
120   x4 -= x6;
121   x6 = x5 + x7;
122   x5 -= x7;
123
124   x7 = x8 + x3;            /* third stage */
125   x8 -= x3;
126   x3 = x0 + x2;
127   x0 -= x2;
128   x2 = (181 * (x4+x5) + 128) >> 8;
129   x4 = (181 * (x4-x5) + 128) >> 8;
130
131   blk[8 * 0] = (x7+x1) >> 14; /* fourth stage */
132   blk[8 * 1] = (x3+x2) >> 14;
133   blk[8 * 2] = (x0+x4) >> 14;
134   blk[8 * 3] = (x8+x6) >> 14;
135   blk[8 * 4] = (x8-x6) >> 14;
136   blk[8 * 5] = (x0-x4) >> 14;
137   blk[8 * 6] = (x3-x2) >> 14;
138   blk[8 * 7] = (x7-x1) >> 14;
139   return 0;
140 }
141
142
143 /* two dimensional inverse discrete cosine transform */
144 void zslice_decoder_t::
145 idct_conversion(short* block)
146 {
147   int i;
148   for( i=0; i<8; ++i )
149     idct_row(block + 8*i);
150   for( i=0; i<8; ++i )
151     idct_col(block + i);
152 }
153