(commit by michael)
[ffmpeg.git] / libavcodec / simple_idct.c
1 /*
2     Copyright (C) 2001 Michael Niedermayer (michaelni@gmx.at)
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19 /*
20   based upon some outcommented c code from mpeg2dec (idct_mmx.c written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>)
21 */
22
23 #include <inttypes.h>
24
25 #include "simple_idct.h"
26
27 #if 0
28 #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
29 #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
30 #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
31 #define W4 2048 /* 2048*sqrt (2)*cos (4*pi/16) */
32 #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
33 #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
34 #define W7 565  /* 2048*sqrt (2)*cos (7*pi/16) */
35 #define ROW_SHIFT 8
36 #define COL_SHIFT 17
37 #else
38 #define W1  22725  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
39 #define W2  21407  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
40 #define W3  19266  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
41 #define W4  16384  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
42 #define W5  12873  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
43 #define W6  8867   //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
44 #define W7  4520   //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
45 #define ROW_SHIFT 11
46 #define COL_SHIFT 20 // 6
47 #endif
48
49 /* 8x8 Matrix used to do a trivial (slow) 8 point IDCT */
50 static int coeff[64]={
51         W4, W4, W4, W4, W4, W4, W4, W4,
52         W1, W3, W5, W7,-W7,-W5,-W3,-W1,
53         W2, W6,-W6,-W2,-W2,-W6, W6, W2,
54         W3,-W7,-W1,-W5, W5, W1, W7,-W3,
55         W4,-W4,-W4, W4, W4,-W4,-W4, W4,
56         W5,-W1, W7, W3,-W3,-W7, W1,-W5,
57         W6,-W2, W2,-W6,-W6, W2,-W2, W6,
58         W7,-W5, W3,-W1, W1,-W3, W5,-W7
59 };
60
61 static int inline idctRowCondZ (int16_t * row)
62 {
63         int a0, a1, a2, a3, b0, b1, b2, b3;
64
65         if( !( ((uint32_t*)row)[0]|((uint32_t*)row)[1] |((uint32_t*)row)[2] |((uint32_t*)row)[3])) {
66 /*              row[0] = row[1] = row[2] = row[3] = row[4] =
67                         row[5] = row[6] = row[7] = 0;*/
68                 return 0;
69         }
70
71         if(!( ((uint32_t*)row)[2] |((uint32_t*)row)[3] )){
72                 a0 = W4*row[0] + W2*row[2] + (1<<(ROW_SHIFT-1));
73                 a1 = W4*row[0] + W6*row[2] + (1<<(ROW_SHIFT-1));
74                 a2 = W4*row[0] - W6*row[2] + (1<<(ROW_SHIFT-1));
75                 a3 = W4*row[0] - W2*row[2] + (1<<(ROW_SHIFT-1));
76
77                 b0 = W1*row[1] + W3*row[3];
78                 b1 = W3*row[1] - W7*row[3];
79                 b2 = W5*row[1] - W1*row[3];
80                 b3 = W7*row[1] - W5*row[3];
81         }else{
82                 a0 = W4*row[0] + W2*row[2] + W4*row[4] + W6*row[6] + (1<<(ROW_SHIFT-1));
83                 a1 = W4*row[0] + W6*row[2] - W4*row[4] - W2*row[6] + (1<<(ROW_SHIFT-1));
84                 a2 = W4*row[0] - W6*row[2] - W4*row[4] + W2*row[6] + (1<<(ROW_SHIFT-1));
85                 a3 = W4*row[0] - W2*row[2] + W4*row[4] - W6*row[6] + (1<<(ROW_SHIFT-1));
86
87                 b0 = W1*row[1] + W3*row[3] + W5*row[5] + W7*row[7];
88                 b1 = W3*row[1] - W7*row[3] - W1*row[5] - W5*row[7];
89                 b2 = W5*row[1] - W1*row[3] + W7*row[5] + W3*row[7];
90                 b3 = W7*row[1] - W5*row[3] + W3*row[5] - W1*row[7];
91         }
92
93         row[0] = (a0 + b0) >> ROW_SHIFT;
94         row[1] = (a1 + b1) >> ROW_SHIFT;
95         row[2] = (a2 + b2) >> ROW_SHIFT;
96         row[3] = (a3 + b3) >> ROW_SHIFT;
97         row[4] = (a3 - b3) >> ROW_SHIFT;
98         row[5] = (a2 - b2) >> ROW_SHIFT;
99         row[6] = (a1 - b1) >> ROW_SHIFT;
100         row[7] = (a0 - b0) >> ROW_SHIFT;
101         
102         return 1;
103 }
104
105 static int inline idctRowCondDC (int16_t * row)
106 {
107         int a0, a1, a2, a3, b0, b1, b2, b3;
108
109         if( !( ((uint32_t*)row)[1] |((uint32_t*)row)[2] |((uint32_t*)row)[3]| row[1])) {
110 //              row[0] = row[1] = row[2] = row[3] = row[4] = row[5] = row[6] = row[7] = row[0]<<3;
111                 uint16_t temp= row[0]<<3;
112                 ((uint32_t*)row)[0]=((uint32_t*)row)[1]=
113                 ((uint32_t*)row)[2]=((uint32_t*)row)[3]= temp + (temp<<16);
114                 return 0;
115         }
116
117         if(!( ((uint32_t*)row)[2] |((uint32_t*)row)[3] )){
118                 a0 = W4*row[0] + W2*row[2] + (1<<(ROW_SHIFT-1));
119                 a1 = W4*row[0] + W6*row[2] + (1<<(ROW_SHIFT-1));
120                 a2 = W4*row[0] - W6*row[2] + (1<<(ROW_SHIFT-1));
121                 a3 = W4*row[0] - W2*row[2] + (1<<(ROW_SHIFT-1));
122
123                 b0 = W1*row[1] + W3*row[3];
124                 b1 = W3*row[1] - W7*row[3];
125                 b2 = W5*row[1] - W1*row[3];
126                 b3 = W7*row[1] - W5*row[3];
127         }else{
128                 a0 = W4*row[0] + W2*row[2] + W4*row[4] + W6*row[6] + (1<<(ROW_SHIFT-1));
129                 a1 = W4*row[0] + W6*row[2] - W4*row[4] - W2*row[6] + (1<<(ROW_SHIFT-1));
130                 a2 = W4*row[0] - W6*row[2] - W4*row[4] + W2*row[6] + (1<<(ROW_SHIFT-1));
131                 a3 = W4*row[0] - W2*row[2] + W4*row[4] - W6*row[6] + (1<<(ROW_SHIFT-1));
132
133                 b0 = W1*row[1] + W3*row[3] + W5*row[5] + W7*row[7];
134                 b1 = W3*row[1] - W7*row[3] - W1*row[5] - W5*row[7];
135                 b2 = W5*row[1] - W1*row[3] + W7*row[5] + W3*row[7];
136                 b3 = W7*row[1] - W5*row[3] + W3*row[5] - W1*row[7];
137         }
138
139         row[0] = (a0 + b0) >> ROW_SHIFT;
140         row[7] = (a0 - b0) >> ROW_SHIFT;
141         row[1] = (a1 + b1) >> ROW_SHIFT;
142         row[6] = (a1 - b1) >> ROW_SHIFT;
143         row[2] = (a2 + b2) >> ROW_SHIFT;
144         row[5] = (a2 - b2) >> ROW_SHIFT;
145         row[3] = (a3 + b3) >> ROW_SHIFT;
146         row[4] = (a3 - b3) >> ROW_SHIFT;
147         
148         return 1;
149 }
150
151 static void inline idctCol (int16_t * col)
152 {
153
154 /*
155         if( !(col[8*1] | col[8*2] |col[8*3] |col[8*4] |col[8*5] |col[8*6] | col[8*7])) {
156                 col[8*0] = col[8*1] = col[8*2] = col[8*3] = col[8*4] =
157                         col[8*5] = col[8*6] = col[8*7] = col[8*0]<<3;
158                 return;
159         }*/
160
161         int a0, a1, a2, a3, b0, b1, b2, b3;
162         col[0] += (1<<(COL_SHIFT-1))/W4;
163         a0 = W4*col[8*0] + W2*col[8*2] + W4*col[8*4] + W6*col[8*6];
164         a1 = W4*col[8*0] + W6*col[8*2] - W4*col[8*4] - W2*col[8*6];
165         a2 = W4*col[8*0] - W6*col[8*2] - W4*col[8*4] + W2*col[8*6];
166         a3 = W4*col[8*0] - W2*col[8*2] + W4*col[8*4] - W6*col[8*6];
167
168         b0 = W1*col[8*1] + W3*col[8*3] + W5*col[8*5] + W7*col[8*7];
169         b1 = W3*col[8*1] - W7*col[8*3] - W1*col[8*5] - W5*col[8*7];
170         b2 = W5*col[8*1] - W1*col[8*3] + W7*col[8*5] + W3*col[8*7];
171         b3 = W7*col[8*1] - W5*col[8*3] + W3*col[8*5] - W1*col[8*7];
172
173         col[8*0] = (a0 + b0) >> COL_SHIFT;
174         col[8*7] = (a0 - b0) >> COL_SHIFT;
175         col[8*1] = (a1 + b1) >> COL_SHIFT;
176         col[8*6] = (a1 - b1) >> COL_SHIFT;
177         col[8*2] = (a2 + b2) >> COL_SHIFT;
178         col[8*5] = (a2 - b2) >> COL_SHIFT;
179         col[8*3] = (a3 + b3) >> COL_SHIFT;
180         col[8*4] = (a3 - b3) >> COL_SHIFT;
181 }
182
183 static void inline idctSparseCol (int16_t * col)
184 {
185         int a0, a1, a2, a3, b0, b1, b2, b3;
186         col[0] += (1<<(COL_SHIFT-1))/W4;
187         a0 = W4*col[8*0];
188         a1 = W4*col[8*0];
189         a2 = W4*col[8*0];
190         a3 = W4*col[8*0];
191
192         if(col[8*2]){
193                 a0 +=  + W2*col[8*2];
194                 a1 +=  + W6*col[8*2];
195                 a2 +=  - W6*col[8*2];
196                 a3 +=  - W2*col[8*2];
197         }
198
199         if(col[8*4]){
200                 a0 += + W4*col[8*4];
201                 a1 += - W4*col[8*4];
202                 a2 += - W4*col[8*4];
203                 a3 += + W4*col[8*4];
204         }
205
206         if(col[8*6]){
207                 a0 += + W6*col[8*6];
208                 a1 += - W2*col[8*6];
209                 a2 += + W2*col[8*6];
210                 a3 += - W6*col[8*6];
211         }
212
213         if(col[8*1]){
214                 b0 = W1*col[8*1];
215                 b1 = W3*col[8*1];
216                 b2 = W5*col[8*1];
217                 b3 = W7*col[8*1];
218         }else{
219                 b0 = 
220                 b1 = 
221                 b2 = 
222                 b3 = 0;
223         }
224
225         if(col[8*3]){
226                 b0 += + W3*col[8*3];
227                 b1 += - W7*col[8*3];
228                 b2 += - W1*col[8*3];
229                 b3 += - W5*col[8*3];
230         }
231
232         if(col[8*5]){
233                 b0 += + W5*col[8*5];
234                 b1 += - W1*col[8*5];
235                 b2 += + W7*col[8*5];
236                 b3 += + W3*col[8*5];
237         }
238
239         if(col[8*7]){
240                 b0 += + W7*col[8*7];
241                 b1 += - W5*col[8*7];
242                 b2 += + W3*col[8*7];
243                 b3 += - W1*col[8*7];
244         }
245
246         if(!(b0|b1|b2|b3)){
247                 col[8*0] = (a0) >> COL_SHIFT;
248                 col[8*7] = (a0) >> COL_SHIFT;
249                 col[8*1] = (a1) >> COL_SHIFT;
250                 col[8*6] = (a1) >> COL_SHIFT;
251                 col[8*2] = (a2) >> COL_SHIFT;
252                 col[8*5] = (a2) >> COL_SHIFT;
253                 col[8*3] = (a3) >> COL_SHIFT;
254                 col[8*4] = (a3) >> COL_SHIFT;
255         }else{
256                 col[8*0] = (a0 + b0) >> COL_SHIFT;
257                 col[8*7] = (a0 - b0) >> COL_SHIFT;
258                 col[8*1] = (a1 + b1) >> COL_SHIFT;
259                 col[8*6] = (a1 - b1) >> COL_SHIFT;
260                 col[8*2] = (a2 + b2) >> COL_SHIFT;
261                 col[8*5] = (a2 - b2) >> COL_SHIFT;
262                 col[8*3] = (a3 + b3) >> COL_SHIFT;
263                 col[8*4] = (a3 - b3) >> COL_SHIFT;
264         }
265 }
266
267 static void inline idctSparse2Col (int16_t * col)
268 {
269         int a0, a1, a2, a3, b0, b1, b2, b3;
270         col[0] += (1<<(COL_SHIFT-1))/W4;
271         a0 = W4*col[8*0];
272         a1 = W4*col[8*0];
273         a2 = W4*col[8*0];
274         a3 = W4*col[8*0];
275
276         if(col[8*2]){
277                 a0 +=  + W2*col[8*2];
278                 a1 +=  + W6*col[8*2];
279                 a2 +=  - W6*col[8*2];
280                 a3 +=  - W2*col[8*2];
281         }
282
283         if(col[8*4]){
284                 a0 += + W4*col[8*4];
285                 a1 += - W4*col[8*4];
286                 a2 += - W4*col[8*4];
287                 a3 += + W4*col[8*4];
288         }
289
290         if(col[8*6]){
291                 a0 += + W6*col[8*6];
292                 a1 += - W2*col[8*6];
293                 a2 += + W2*col[8*6];
294                 a3 += - W6*col[8*6];
295         }
296
297         if(col[8*1] || 1){
298                 b0 = W1*col[8*1];
299                 b1 = W3*col[8*1];
300                 b2 = W5*col[8*1];
301                 b3 = W7*col[8*1];
302         }else{
303                 b0 = 
304                 b1 = 
305                 b2 = 
306                 b3 = 0;
307         }
308
309         if(col[8*3]){
310                 b0 += + W3*col[8*3];
311                 b1 += - W7*col[8*3];
312                 b2 += - W1*col[8*3];
313                 b3 += - W5*col[8*3];
314         }
315
316         if(col[8*5]){
317                 b0 += + W5*col[8*5];
318                 b1 += - W1*col[8*5];
319                 b2 += + W7*col[8*5];
320                 b3 += + W3*col[8*5];
321         }
322
323         if(col[8*7]){
324                 b0 += + W7*col[8*7];
325                 b1 += - W5*col[8*7];
326                 b2 += + W3*col[8*7];
327                 b3 += - W1*col[8*7];
328         }
329
330         col[8*0] = (a0 + b0) >> COL_SHIFT;
331         col[8*7] = (a0 - b0) >> COL_SHIFT;
332         col[8*1] = (a1 + b1) >> COL_SHIFT;
333         col[8*6] = (a1 - b1) >> COL_SHIFT;
334         col[8*2] = (a2 + b2) >> COL_SHIFT;
335         col[8*5] = (a2 - b2) >> COL_SHIFT;
336         col[8*3] = (a3 + b3) >> COL_SHIFT;
337         col[8*4] = (a3 - b3) >> COL_SHIFT;
338 }
339
340
341 void simple_idct (short *block)
342 {
343
344         int i;
345         
346 #if 0
347         int nonZero[8];
348         int buffer[64];
349         int nNonZero=0;
350         
351         idctRowCondDC(block);
352         
353         for(i=1; i<8; i++)
354         {
355                 nonZero[nNonZero]=i;
356                 nNonZero+= idctRowCondZ(block + i*8);
357         }
358         
359         if(nNonZero==0)
360         {
361                 for(i=0; i<8; i++)
362                 {
363                         block[i   ]=
364                         block[i+8 ]=
365                         block[i+16]=
366                         block[i+24]=
367                         block[i+32]=
368                         block[i+40]=
369                         block[i+48]=
370                         block[i+56]= (W4*block[i] + (1<<(COL_SHIFT-1))) >> COL_SHIFT;
371                 }       
372         }
373         else if(nNonZero==1)
374         {
375                 int index= nonZero[0]*8;
376                 for(i=0; i<8; i++)
377                 {
378                         int bias= W4*block[i] + (1<<(COL_SHIFT-1));
379                         int c= block[i + index];
380                         block[i   ]= (c*coeff[index  ] + bias) >> COL_SHIFT;
381                         block[i+8 ]= (c*coeff[index+1] + bias) >> COL_SHIFT;
382                         block[i+16]= (c*coeff[index+2] + bias) >> COL_SHIFT;
383                         block[i+24]= (c*coeff[index+3] + bias) >> COL_SHIFT;
384                         block[i+32]= (c*coeff[index+4] + bias) >> COL_SHIFT;
385                         block[i+40]= (c*coeff[index+5] + bias) >> COL_SHIFT;
386                         block[i+48]= (c*coeff[index+6] + bias) >> COL_SHIFT;
387                         block[i+56]= (c*coeff[index+7] + bias) >> COL_SHIFT;
388                 }       
389         }
390 /*      else if(nNonZero==2)
391         {
392                 int index1= nonZero[0]*8;
393                 int index2= nonZero[1]*8;
394                 for(i=0; i<8; i++)
395                 {
396                         int bias= W4*block[i] + (1<<(COL_SHIFT-1));
397                         int c1= block[i + index1];
398                         int c2= block[i + index2];
399                         block[i   ]= (c1*coeff[index1  ] + c2*coeff[index2  ] + bias) >> COL_SHIFT;
400                         block[i+8 ]= (c1*coeff[index1+1] + c2*coeff[index2+1] + bias) >> COL_SHIFT;
401                         block[i+16]= (c1*coeff[index1+2] + c2*coeff[index2+2] + bias) >> COL_SHIFT;
402                         block[i+24]= (c1*coeff[index1+3] + c2*coeff[index2+3] + bias) >> COL_SHIFT;
403                         block[i+32]= (c1*coeff[index1+4] + c2*coeff[index2+4] + bias) >> COL_SHIFT;
404                         block[i+40]= (c1*coeff[index1+5] + c2*coeff[index2+5] + bias) >> COL_SHIFT;
405                         block[i+48]= (c1*coeff[index1+6] + c2*coeff[index2+6] + bias) >> COL_SHIFT;
406                         block[i+56]= (c1*coeff[index1+7] + c2*coeff[index2+7] + bias) >> COL_SHIFT;
407                 }       
408         }*/
409         else
410         {
411                 for(i=0; i<8; i++)
412                         idctSparse2Col(block + i);
413         }
414 #else           
415         for(i=0; i<8; i++)
416                 idctRowCondDC(block + i*8);
417         
418         for(i=0; i<8; i++)
419                 idctSparseCol(block + i);
420 #endif
421 }