]> git.sesse.net Git - vlc/blob - plugins/idct/idctclassic.c
b1e0b8ec3523be42b43d9dd8b01acc16465b0128
[vlc] / plugins / idct / idctclassic.c
1 /*****************************************************************************
2  * idctclassic.c : Classic IDCT module
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: idctclassic.c,v 1.24 2002/05/18 17:47:46 sam Exp $
6  *
7  * Authors: GaĆ«l Hendryckx <jimmy@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <videolan/vlc.h>
31
32 #include "idct.h"
33 #include "block_c.h"
34
35 /*****************************************************************************
36  * Local and extern prototypes.
37  *****************************************************************************/
38 static void idct_getfunctions( function_list_t * p_function_list );
39
40 /*****************************************************************************
41  * Build configuration tree.
42  *****************************************************************************/
43 MODULE_CONFIG_START
44 MODULE_CONFIG_STOP
45
46 MODULE_INIT_START
47     SET_DESCRIPTION( _("classic IDCT module") )
48     ADD_CAPABILITY( IDCT, 100 )
49     ADD_SHORTCUT( "classic" )
50     ADD_SHORTCUT( "idctclassic" )
51 MODULE_INIT_STOP
52
53 MODULE_ACTIVATE_START
54     idct_getfunctions( &p_module->p_functions->idct );
55 MODULE_ACTIVATE_STOP
56
57 MODULE_DEACTIVATE_START
58 MODULE_DEACTIVATE_STOP
59
60 /* Following functions are local */
61
62 /*****************************************************************************
63  * NormScan : Unused in this IDCT
64  *****************************************************************************/
65 static void NormScan( u8 ppi_scan[2][64] )
66 {
67 }
68
69 /*****************************************************************************
70  * IDCT : IDCT function for normal matrices
71  *****************************************************************************/
72 static inline void IDCT( dctelem_t * p_block )
73 {
74     s32 tmp0, tmp1, tmp2, tmp3;
75     s32 tmp10, tmp11, tmp12, tmp13;
76     s32 z1, z2, z3, z4, z5;
77     dctelem_t * dataptr;
78     int rowctr;
79     SHIFT_TEMPS
80
81   /* Pass 1: process rows. */
82   /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
83   /* furthermore, we scale the results by 2**PASS1_BITS. */
84
85     dataptr = p_block;
86     for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--)
87     {
88     /* Due to quantization, we will usually find that many of the input
89      * coefficients are zero, especially the AC terms.  We can exploit this
90      * by short-circuiting the IDCT calculation for any row in which all
91      * the AC terms are zero.  In that case each output is equal to the
92      * DC coefficient (with scale factor as needed).
93      * With typical images and quantization tables, half or more of the
94      * row DCT calculations can be simplified this way.
95      */
96
97         if ((dataptr[1] | dataptr[2] | dataptr[3] | dataptr[4] |
98                 dataptr[5] | dataptr[6] | dataptr[7]) == 0)
99         {
100       /* AC terms all zero */
101             dctelem_t dcval = (dctelem_t) (dataptr[0] << PASS1_BITS);
102
103             dataptr[0] = dcval;
104             dataptr[1] = dcval;
105             dataptr[2] = dcval;
106             dataptr[3] = dcval;
107             dataptr[4] = dcval;
108             dataptr[5] = dcval;
109             dataptr[6] = dcval;
110             dataptr[7] = dcval;
111
112             dataptr += DCTSIZE; /* advance pointer to next row */
113             continue;
114         }
115
116     /* Even part: reverse the even part of the forward DCT. */
117     /* The rotator is sqrt(2)*c(-6). */
118
119         z2 = (s32) dataptr[2];
120         z3 = (s32) dataptr[6];
121
122         z1 = MULTIPLY(z2 + z3, FIX(0.541196100));
123         tmp2 = z1 + MULTIPLY(z3, - FIX(1.847759065));
124         tmp3 = z1 + MULTIPLY(z2, FIX(0.765366865));
125
126         tmp0 = ((s32) dataptr[0] + (s32) dataptr[4]) << CONST_BITS;
127         tmp1 = ((s32) dataptr[0] - (s32) dataptr[4]) << CONST_BITS;
128
129         tmp10 = tmp0 + tmp3;
130         tmp13 = tmp0 - tmp3;
131         tmp11 = tmp1 + tmp2;
132         tmp12 = tmp1 - tmp2;
133
134     /* Odd part per figure 8; the matrix is unitary and hence its
135      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
136      */
137
138         tmp0 = (s32) dataptr[7];
139         tmp1 = (s32) dataptr[5];
140         tmp2 = (s32) dataptr[3];
141         tmp3 = (s32) dataptr[1];
142
143         z1 = tmp0 + tmp3;
144         z2 = tmp1 + tmp2;
145         z3 = tmp0 + tmp2;
146         z4 = tmp1 + tmp3;
147         z5 = MULTIPLY(z3 + z4, FIX(1.175875602)); /* sqrt(2) * c3 */
148
149         tmp0 = MULTIPLY(tmp0, FIX(0.298631336)); /* sqrt(2) * (-c1+c3+c5-c7) */
150         tmp1 = MULTIPLY(tmp1, FIX(2.053119869)); /* sqrt(2) * ( c1+c3-c5+c7) */
151         tmp2 = MULTIPLY(tmp2, FIX(3.072711026)); /* sqrt(2) * ( c1+c3+c5-c7) */
152         tmp3 = MULTIPLY(tmp3, FIX(1.501321110)); /* sqrt(2) * ( c1+c3-c5-c7) */
153         z1 = MULTIPLY(z1, - FIX(0.899976223)); /* sqrt(2) * (c7-c3) */
154         z2 = MULTIPLY(z2, - FIX(2.562915447)); /* sqrt(2) * (-c1-c3) */
155         z3 = MULTIPLY(z3, - FIX(1.961570560)); /* sqrt(2) * (-c3-c5) */
156         z4 = MULTIPLY(z4, - FIX(0.390180644)); /* sqrt(2) * (c5-c3) */
157
158         z3 += z5;
159         z4 += z5;
160
161         tmp0 += z1 + z3;
162         tmp1 += z2 + z4;
163         tmp2 += z2 + z3;
164         tmp3 += z1 + z4;
165
166     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
167
168         dataptr[0] = (dctelem_t) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
169         dataptr[7] = (dctelem_t) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
170         dataptr[1] = (dctelem_t) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
171         dataptr[6] = (dctelem_t) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
172         dataptr[2] = (dctelem_t) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
173         dataptr[5] = (dctelem_t) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
174         dataptr[3] = (dctelem_t) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
175         dataptr[4] = (dctelem_t) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
176
177         dataptr += DCTSIZE;             /* advance pointer to next row */
178     }
179
180   /* Pass 2: process columns. */
181   /* Note that we must descale the results by a factor of 8 == 2**3, */
182   /* and also undo the PASS1_BITS scaling. */
183
184     dataptr = p_block;
185     for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--)
186     {
187     /* Columns of zeroes can be exploited in the same way as we did with rows.
188      * However, the row calculation has created many nonzero AC terms, so the
189      * simplification applies less often (typically 5% to 10% of the time).
190      * On machines with very fast multiplication, it's possible that the
191      * test takes more time than it's worth.  In that case this section
192      * may be commented out.
193      */
194
195 #ifndef NO_ZERO_COLUMN_TEST /* Adds a test but avoids calculus */
196         if ((dataptr[DCTSIZE*1] | dataptr[DCTSIZE*2] | dataptr[DCTSIZE*3] |
197             dataptr[DCTSIZE*4] | dataptr[DCTSIZE*5] | dataptr[DCTSIZE*6] |
198             dataptr[DCTSIZE*7]) == 0)
199         {
200       /* AC terms all zero */
201             dctelem_t dcval = (dctelem_t) DESCALE((s32) dataptr[0], PASS1_BITS+3);
202
203             dataptr[DCTSIZE*0] = dcval;
204             dataptr[DCTSIZE*1] = dcval;
205             dataptr[DCTSIZE*2] = dcval;
206             dataptr[DCTSIZE*3] = dcval;
207             dataptr[DCTSIZE*4] = dcval;
208             dataptr[DCTSIZE*5] = dcval;
209             dataptr[DCTSIZE*6] = dcval;
210             dataptr[DCTSIZE*7] = dcval;
211
212             dataptr++;          /* advance pointer to next column */
213             continue;
214         }
215 #endif
216
217     /* Even part: reverse the even part of the forward DCT. */
218     /* The rotator is sqrt(2)*c(-6). */
219
220         z2 = (s32) dataptr[DCTSIZE*2];
221         z3 = (s32) dataptr[DCTSIZE*6];
222
223         z1 = MULTIPLY(z2 + z3, FIX(0.541196100));
224         tmp2 = z1 + MULTIPLY(z3, - FIX(1.847759065));
225         tmp3 = z1 + MULTIPLY(z2, FIX(0.765366865));
226
227         tmp0 = ((s32) dataptr[DCTSIZE*0] + (s32) dataptr[DCTSIZE*4]) << CONST_BITS;
228         tmp1 = ((s32) dataptr[DCTSIZE*0] - (s32) dataptr[DCTSIZE*4]) << CONST_BITS;
229
230         tmp10 = tmp0 + tmp3;
231         tmp13 = tmp0 - tmp3;
232         tmp11 = tmp1 + tmp2;
233         tmp12 = tmp1 - tmp2;
234
235     /* Odd part per figure 8; the matrix is unitary and hence its
236      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
237      */
238
239         tmp0 = (s32) dataptr[DCTSIZE*7];
240         tmp1 = (s32) dataptr[DCTSIZE*5];
241         tmp2 = (s32) dataptr[DCTSIZE*3];
242         tmp3 = (s32) dataptr[DCTSIZE*1];
243
244         z1 = tmp0 + tmp3;
245         z2 = tmp1 + tmp2;
246         z3 = tmp0 + tmp2;
247         z4 = tmp1 + tmp3;
248         z5 = MULTIPLY(z3 + z4, FIX(1.175875602)); /* sqrt(2) * c3 */
249
250         tmp0 = MULTIPLY(tmp0, FIX(0.298631336)); /* sqrt(2) * (-c1+c3+c5-c7) */
251         tmp1 = MULTIPLY(tmp1, FIX(2.053119869)); /* sqrt(2) * ( c1+c3-c5+c7) */
252         tmp2 = MULTIPLY(tmp2, FIX(3.072711026)); /* sqrt(2) * ( c1+c3+c5-c7) */
253         tmp3 = MULTIPLY(tmp3, FIX(1.501321110)); /* sqrt(2) * ( c1+c3-c5-c7) */
254         z1 = MULTIPLY(z1, - FIX(0.899976223)); /* sqrt(2) * (c7-c3) */
255         z2 = MULTIPLY(z2, - FIX(2.562915447)); /* sqrt(2) * (-c1-c3) */
256         z3 = MULTIPLY(z3, - FIX(1.961570560)); /* sqrt(2) * (-c3-c5) */
257         z4 = MULTIPLY(z4, - FIX(0.390180644)); /* sqrt(2) * (c5-c3) */
258
259         z3 += z5;
260         z4 += z5;
261
262         tmp0 += z1 + z3;
263         tmp1 += z2 + z4;
264         tmp2 += z2 + z3;
265         tmp3 += z1 + z4;
266
267     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
268
269         dataptr[DCTSIZE*0] = (dctelem_t) DESCALE(tmp10 + tmp3,
270                                            CONST_BITS+PASS1_BITS+3);
271         dataptr[DCTSIZE*7] = (dctelem_t) DESCALE(tmp10 - tmp3,
272                                            CONST_BITS+PASS1_BITS+3);
273         dataptr[DCTSIZE*1] = (dctelem_t) DESCALE(tmp11 + tmp2,
274                                            CONST_BITS+PASS1_BITS+3);
275         dataptr[DCTSIZE*6] = (dctelem_t) DESCALE(tmp11 - tmp2,
276                                            CONST_BITS+PASS1_BITS+3);
277         dataptr[DCTSIZE*2] = (dctelem_t) DESCALE(tmp12 + tmp1,
278                                            CONST_BITS+PASS1_BITS+3);
279         dataptr[DCTSIZE*5] = (dctelem_t) DESCALE(tmp12 - tmp1,
280                                            CONST_BITS+PASS1_BITS+3);
281         dataptr[DCTSIZE*3] = (dctelem_t) DESCALE(tmp13 + tmp0,
282                                            CONST_BITS+PASS1_BITS+3);
283         dataptr[DCTSIZE*4] = (dctelem_t) DESCALE(tmp13 - tmp0,
284                                            CONST_BITS+PASS1_BITS+3);
285
286         dataptr++;                      /* advance pointer to next column */
287     }
288 }
289
290 static inline void RestoreCPUState( )
291 {
292     ;
293 }
294
295 #include "idct_sparse.h"
296 #include "idct_decl.h"
297