]> git.sesse.net Git - vlc/blob - plugins/idct/idctclassic.c
fa8b6c207c64fe2d03ecfd7e9448bbbb27e88100
[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.17 2001/11/28 15:08:05 massiot 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 #define MODULE_NAME idctclassic
25 #include "modules_inner.h"
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "config.h"
36 #include "common.h"
37 #include "intf_msg.h"
38 #include "threads.h"
39 #include "mtime.h"
40 #include "tests.h"
41
42 #include "idct.h"
43 #include "block_c.h"
44
45 #include "modules.h"
46 #include "modules_export.h"
47
48 /*****************************************************************************
49  * Local and extern prototypes.
50  *****************************************************************************/
51 static void idct_getfunctions( function_list_t * p_function_list );
52
53 /*****************************************************************************
54  * Build configuration tree.
55  *****************************************************************************/
56 MODULE_CONFIG_START
57 ADD_WINDOW( "Configuration for classic IDCT module" )
58     ADD_COMMENT( "Ha, ha -- nothing to configure yet" )
59 MODULE_CONFIG_STOP
60
61 MODULE_INIT_START
62     p_module->i_capabilities = MODULE_CAPABILITY_NULL
63                                 | MODULE_CAPABILITY_IDCT;
64     p_module->psz_longname = "classic IDCT module";
65 MODULE_INIT_STOP
66
67 MODULE_ACTIVATE_START
68     idct_getfunctions( &p_module->p_functions->idct );
69 MODULE_ACTIVATE_STOP
70
71 MODULE_DEACTIVATE_START
72 MODULE_DEACTIVATE_STOP
73
74 /* Following functions are local */
75
76 /*****************************************************************************
77  * idct_Probe: returns a preference score
78  *****************************************************************************/
79 static int idct_Probe( probedata_t *p_data )
80 {
81     if( TestMethod( IDCT_METHOD_VAR, "idctclassic" )
82          || TestMethod( IDCT_METHOD_VAR, "classic" ) )
83     {
84         return( 999 );
85     }
86
87     /* This plugin always works */
88     return( 100 );
89 }
90
91 /*****************************************************************************
92  * NormScan : Unused in this IDCT
93  *****************************************************************************/
94 static void NormScan( u8 ppi_scan[2][64] )
95 {
96 }
97
98 /*****************************************************************************
99  * IDCT : IDCT function for normal matrices
100  *****************************************************************************/
101 static __inline__ void IDCT( dctelem_t * p_block )
102 {
103     s32 tmp0, tmp1, tmp2, tmp3;
104     s32 tmp10, tmp11, tmp12, tmp13;
105     s32 z1, z2, z3, z4, z5;
106     dctelem_t * dataptr;
107     int rowctr;
108     SHIFT_TEMPS
109
110   /* Pass 1: process rows. */
111   /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
112   /* furthermore, we scale the results by 2**PASS1_BITS. */
113
114     dataptr = p_block;
115     for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--)
116     {
117     /* Due to quantization, we will usually find that many of the input
118      * coefficients are zero, especially the AC terms.  We can exploit this
119      * by short-circuiting the IDCT calculation for any row in which all
120      * the AC terms are zero.  In that case each output is equal to the
121      * DC coefficient (with scale factor as needed).
122      * With typical images and quantization tables, half or more of the
123      * row DCT calculations can be simplified this way.
124      */
125
126         if ((dataptr[1] | dataptr[2] | dataptr[3] | dataptr[4] |
127                 dataptr[5] | dataptr[6] | dataptr[7]) == 0)
128         {
129       /* AC terms all zero */
130             dctelem_t dcval = (dctelem_t) (dataptr[0] << PASS1_BITS);
131
132             dataptr[0] = dcval;
133             dataptr[1] = dcval;
134             dataptr[2] = dcval;
135             dataptr[3] = dcval;
136             dataptr[4] = dcval;
137             dataptr[5] = dcval;
138             dataptr[6] = dcval;
139             dataptr[7] = dcval;
140
141             dataptr += DCTSIZE; /* advance pointer to next row */
142             continue;
143         }
144
145     /* Even part: reverse the even part of the forward DCT. */
146     /* The rotator is sqrt(2)*c(-6). */
147
148         z2 = (s32) dataptr[2];
149         z3 = (s32) dataptr[6];
150
151         z1 = MULTIPLY(z2 + z3, FIX(0.541196100));
152         tmp2 = z1 + MULTIPLY(z3, - FIX(1.847759065));
153         tmp3 = z1 + MULTIPLY(z2, FIX(0.765366865));
154
155         tmp0 = ((s32) dataptr[0] + (s32) dataptr[4]) << CONST_BITS;
156         tmp1 = ((s32) dataptr[0] - (s32) dataptr[4]) << CONST_BITS;
157
158         tmp10 = tmp0 + tmp3;
159         tmp13 = tmp0 - tmp3;
160         tmp11 = tmp1 + tmp2;
161         tmp12 = tmp1 - tmp2;
162
163     /* Odd part per figure 8; the matrix is unitary and hence its
164      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
165      */
166
167         tmp0 = (s32) dataptr[7];
168         tmp1 = (s32) dataptr[5];
169         tmp2 = (s32) dataptr[3];
170         tmp3 = (s32) dataptr[1];
171
172         z1 = tmp0 + tmp3;
173         z2 = tmp1 + tmp2;
174         z3 = tmp0 + tmp2;
175         z4 = tmp1 + tmp3;
176         z5 = MULTIPLY(z3 + z4, FIX(1.175875602)); /* sqrt(2) * c3 */
177
178         tmp0 = MULTIPLY(tmp0, FIX(0.298631336)); /* sqrt(2) * (-c1+c3+c5-c7) */
179         tmp1 = MULTIPLY(tmp1, FIX(2.053119869)); /* sqrt(2) * ( c1+c3-c5+c7) */
180         tmp2 = MULTIPLY(tmp2, FIX(3.072711026)); /* sqrt(2) * ( c1+c3+c5-c7) */
181         tmp3 = MULTIPLY(tmp3, FIX(1.501321110)); /* sqrt(2) * ( c1+c3-c5-c7) */
182         z1 = MULTIPLY(z1, - FIX(0.899976223)); /* sqrt(2) * (c7-c3) */
183         z2 = MULTIPLY(z2, - FIX(2.562915447)); /* sqrt(2) * (-c1-c3) */
184         z3 = MULTIPLY(z3, - FIX(1.961570560)); /* sqrt(2) * (-c3-c5) */
185         z4 = MULTIPLY(z4, - FIX(0.390180644)); /* sqrt(2) * (c5-c3) */
186
187         z3 += z5;
188         z4 += z5;
189
190         tmp0 += z1 + z3;
191         tmp1 += z2 + z4;
192         tmp2 += z2 + z3;
193         tmp3 += z1 + z4;
194
195     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
196
197         dataptr[0] = (dctelem_t) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
198         dataptr[7] = (dctelem_t) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
199         dataptr[1] = (dctelem_t) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
200         dataptr[6] = (dctelem_t) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
201         dataptr[2] = (dctelem_t) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
202         dataptr[5] = (dctelem_t) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
203         dataptr[3] = (dctelem_t) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
204         dataptr[4] = (dctelem_t) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
205
206         dataptr += DCTSIZE;             /* advance pointer to next row */
207     }
208
209   /* Pass 2: process columns. */
210   /* Note that we must descale the results by a factor of 8 == 2**3, */
211   /* and also undo the PASS1_BITS scaling. */
212
213     dataptr = p_block;
214     for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--)
215     {
216     /* Columns of zeroes can be exploited in the same way as we did with rows.
217      * However, the row calculation has created many nonzero AC terms, so the
218      * simplification applies less often (typically 5% to 10% of the time).
219      * On machines with very fast multiplication, it's possible that the
220      * test takes more time than it's worth.  In that case this section
221      * may be commented out.
222      */
223
224 #ifndef NO_ZERO_COLUMN_TEST /* Adds a test but avoids calculus */
225         if ((dataptr[DCTSIZE*1] | dataptr[DCTSIZE*2] | dataptr[DCTSIZE*3] |
226             dataptr[DCTSIZE*4] | dataptr[DCTSIZE*5] | dataptr[DCTSIZE*6] |
227             dataptr[DCTSIZE*7]) == 0)
228         {
229       /* AC terms all zero */
230             dctelem_t dcval = (dctelem_t) DESCALE((s32) dataptr[0], PASS1_BITS+3);
231
232             dataptr[DCTSIZE*0] = dcval;
233             dataptr[DCTSIZE*1] = dcval;
234             dataptr[DCTSIZE*2] = dcval;
235             dataptr[DCTSIZE*3] = dcval;
236             dataptr[DCTSIZE*4] = dcval;
237             dataptr[DCTSIZE*5] = dcval;
238             dataptr[DCTSIZE*6] = dcval;
239             dataptr[DCTSIZE*7] = dcval;
240
241             dataptr++;          /* advance pointer to next column */
242             continue;
243         }
244 #endif
245
246     /* Even part: reverse the even part of the forward DCT. */
247     /* The rotator is sqrt(2)*c(-6). */
248
249         z2 = (s32) dataptr[DCTSIZE*2];
250         z3 = (s32) dataptr[DCTSIZE*6];
251
252         z1 = MULTIPLY(z2 + z3, FIX(0.541196100));
253         tmp2 = z1 + MULTIPLY(z3, - FIX(1.847759065));
254         tmp3 = z1 + MULTIPLY(z2, FIX(0.765366865));
255
256         tmp0 = ((s32) dataptr[DCTSIZE*0] + (s32) dataptr[DCTSIZE*4]) << CONST_BITS;
257         tmp1 = ((s32) dataptr[DCTSIZE*0] - (s32) dataptr[DCTSIZE*4]) << CONST_BITS;
258
259         tmp10 = tmp0 + tmp3;
260         tmp13 = tmp0 - tmp3;
261         tmp11 = tmp1 + tmp2;
262         tmp12 = tmp1 - tmp2;
263
264     /* Odd part per figure 8; the matrix is unitary and hence its
265      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
266      */
267
268         tmp0 = (s32) dataptr[DCTSIZE*7];
269         tmp1 = (s32) dataptr[DCTSIZE*5];
270         tmp2 = (s32) dataptr[DCTSIZE*3];
271         tmp3 = (s32) dataptr[DCTSIZE*1];
272
273         z1 = tmp0 + tmp3;
274         z2 = tmp1 + tmp2;
275         z3 = tmp0 + tmp2;
276         z4 = tmp1 + tmp3;
277         z5 = MULTIPLY(z3 + z4, FIX(1.175875602)); /* sqrt(2) * c3 */
278
279         tmp0 = MULTIPLY(tmp0, FIX(0.298631336)); /* sqrt(2) * (-c1+c3+c5-c7) */
280         tmp1 = MULTIPLY(tmp1, FIX(2.053119869)); /* sqrt(2) * ( c1+c3-c5+c7) */
281         tmp2 = MULTIPLY(tmp2, FIX(3.072711026)); /* sqrt(2) * ( c1+c3+c5-c7) */
282         tmp3 = MULTIPLY(tmp3, FIX(1.501321110)); /* sqrt(2) * ( c1+c3-c5-c7) */
283         z1 = MULTIPLY(z1, - FIX(0.899976223)); /* sqrt(2) * (c7-c3) */
284         z2 = MULTIPLY(z2, - FIX(2.562915447)); /* sqrt(2) * (-c1-c3) */
285         z3 = MULTIPLY(z3, - FIX(1.961570560)); /* sqrt(2) * (-c3-c5) */
286         z4 = MULTIPLY(z4, - FIX(0.390180644)); /* sqrt(2) * (c5-c3) */
287
288         z3 += z5;
289         z4 += z5;
290
291         tmp0 += z1 + z3;
292         tmp1 += z2 + z4;
293         tmp2 += z2 + z3;
294         tmp3 += z1 + z4;
295
296     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
297
298         dataptr[DCTSIZE*0] = (dctelem_t) DESCALE(tmp10 + tmp3,
299                                            CONST_BITS+PASS1_BITS+3);
300         dataptr[DCTSIZE*7] = (dctelem_t) DESCALE(tmp10 - tmp3,
301                                            CONST_BITS+PASS1_BITS+3);
302         dataptr[DCTSIZE*1] = (dctelem_t) DESCALE(tmp11 + tmp2,
303                                            CONST_BITS+PASS1_BITS+3);
304         dataptr[DCTSIZE*6] = (dctelem_t) DESCALE(tmp11 - tmp2,
305                                            CONST_BITS+PASS1_BITS+3);
306         dataptr[DCTSIZE*2] = (dctelem_t) DESCALE(tmp12 + tmp1,
307                                            CONST_BITS+PASS1_BITS+3);
308         dataptr[DCTSIZE*5] = (dctelem_t) DESCALE(tmp12 - tmp1,
309                                            CONST_BITS+PASS1_BITS+3);
310         dataptr[DCTSIZE*3] = (dctelem_t) DESCALE(tmp13 + tmp0,
311                                            CONST_BITS+PASS1_BITS+3);
312         dataptr[DCTSIZE*4] = (dctelem_t) DESCALE(tmp13 - tmp0,
313                                            CONST_BITS+PASS1_BITS+3);
314
315         dataptr++;                      /* advance pointer to next column */
316     }
317 }
318
319 #include "idct_sparse.h"
320 #include "idct_decl.h"
321