]> git.sesse.net Git - vlc/blob - plugins/idct/idctclassic.c
1c6928a2c010a53f174c0e1155893d2f42aa3c23
[vlc] / plugins / idct / idctclassic.c
1 /*****************************************************************************
2  * idctclassic.c : Classic IDCT module
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: idctclassic.c,v 1.4 2001/01/16 05:04:25 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 #define MODULE_NAME idctclassic
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
31 #include <stdlib.h>
32
33 #include "config.h"
34 #include "common.h"
35 #include "threads.h"
36 #include "mtime.h"
37 #include "tests.h"
38
39 #include "video.h"
40 #include "video_output.h"
41
42 #include "video_decoder.h"
43
44 #include "modules.h"
45 #include "modules_inner.h"
46
47 #include "idct.h"
48
49 /*****************************************************************************
50  * Local and extern prototypes.
51  *****************************************************************************/
52 static void idct_getfunctions( function_list_t * p_function_list );
53
54 static int  idct_Probe      ( probedata_t *p_data );
55 static void vdec_InitIDCT   ( vdec_thread_t * p_vdec);
56        void vdec_SparseIDCT ( vdec_thread_t * p_vdec, dctelem_t * p_block,
57                               int i_sparse_pos);
58 static void vdec_IDCT       ( vdec_thread_t * p_vdec, dctelem_t * p_block,
59                               int i_idontcare );
60
61
62 /*****************************************************************************
63  * Build configuration tree.
64  *****************************************************************************/
65 MODULE_CONFIG_START
66 ADD_WINDOW( "Configuration for classic IDCT module" )
67     ADD_COMMENT( "Ha, ha -- nothing to configure yet" )
68 MODULE_CONFIG_END
69
70 /*****************************************************************************
71  * InitModule: get the module structure and configuration.
72  *****************************************************************************
73  * We have to fill psz_name, psz_longname and psz_version. These variables
74  * will be strdup()ed later by the main application because the module can
75  * be unloaded later to save memory, and we want to be able to access this
76  * data even after the module has been unloaded.
77  *****************************************************************************/
78 int InitModule( module_t * p_module )
79 {
80     p_module->psz_name = MODULE_STRING;
81     p_module->psz_longname = "classic C IDCT module";
82     p_module->psz_version = VERSION;
83
84     p_module->i_capabilities = MODULE_CAPABILITY_NULL
85                                 | MODULE_CAPABILITY_IDCT;
86
87     return( 0 );
88 }
89
90 /*****************************************************************************
91  * ActivateModule: set the module to an usable state.
92  *****************************************************************************
93  * This function fills the capability functions and the configuration
94  * structure. Once ActivateModule() has been called, the i_usage can
95  * be set to 0 and calls to NeedModule() be made to increment it. To unload
96  * the module, one has to wait until i_usage == 0 and call DeactivateModule().
97  *****************************************************************************/
98 int ActivateModule( module_t * p_module )
99 {
100     p_module->p_functions = malloc( sizeof( module_functions_t ) );
101     if( p_module->p_functions == NULL )
102     {
103         return( -1 );
104     }
105
106     idct_getfunctions( &p_module->p_functions->idct );
107
108     p_module->p_config = p_config;
109
110     return( 0 );
111 }
112
113 /*****************************************************************************
114  * DeactivateModule: make sure the module can be unloaded.
115  *****************************************************************************
116  * This function must only be called when i_usage == 0. If it successfully
117  * returns, i_usage can be set to -1 and the module unloaded. Be careful to
118  * lock usage_lock during the whole process.
119  *****************************************************************************/
120 int DeactivateModule( module_t * p_module )
121 {
122     free( p_module->p_functions );
123
124     return( 0 );
125 }
126
127 /* Following functions are local */
128
129 /*****************************************************************************
130  * Functions exported as capabilities. They are declared as static so that
131  * we don't pollute the namespace too much.
132  *****************************************************************************/
133 static void idct_getfunctions( function_list_t * p_function_list )
134 {
135     p_function_list->pf_probe = idct_Probe;
136     p_function_list->functions.idct.pf_init = vdec_InitIDCT;
137     p_function_list->functions.idct.pf_sparse_idct = vdec_SparseIDCT;
138     p_function_list->functions.idct.pf_idct = vdec_IDCT;
139 }
140
141 /*****************************************************************************
142  * idct_Probe: returns a preference score
143  *****************************************************************************/
144 static int idct_Probe( probedata_t *p_data )
145 {
146     if( TestMethod( IDCT_METHOD_VAR, "idctclassic" ) )
147     {
148         return( 999 );
149     }
150
151     /* This plugin always works */
152     return( 100 );
153 }
154
155 /*****************************************************************************
156  * vdec_InitIDCT : initialize datas for vdec_SparseIDCT
157  *****************************************************************************/
158 static void vdec_InitIDCT (vdec_thread_t * p_vdec)
159 {
160     int i;
161
162     dctelem_t * p_pre = p_vdec->p_pre_idct;
163     memset( p_pre, 0, 64*64*sizeof(dctelem_t) );
164
165     for( i=0 ; i < 64 ; i++ )
166     {
167         p_pre[i*64+i] = 1 << SPARSE_SCALE_FACTOR;
168         vdec_IDCT( p_vdec, &p_pre[i*64], 0) ;
169     }
170     return;
171 }
172
173 /*****************************************************************************
174  * vdec_IDCT : IDCT function for normal matrices
175  *****************************************************************************/
176 static void vdec_IDCT( vdec_thread_t * p_vdec, dctelem_t * p_block,
177                        int i_idontcare )
178 {
179     /* dct classique: pour tester la meilleure entre la classique et la */
180     /* no classique */
181     s32 tmp0, tmp1, tmp2, tmp3;
182     s32 tmp10, tmp11, tmp12, tmp13;
183     s32 z1, z2, z3, z4, z5;
184     dctelem_t * dataptr;
185     int rowctr;
186     SHIFT_TEMPS
187
188   /* Pass 1: process rows. */
189   /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
190   /* furthermore, we scale the results by 2**PASS1_BITS. */
191
192     dataptr = p_block;
193     for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--)
194     {
195     /* Due to quantization, we will usually find that many of the input
196      * coefficients are zero, especially the AC terms.  We can exploit this
197      * by short-circuiting the IDCT calculation for any row in which all
198      * the AC terms are zero.  In that case each output is equal to the
199      * DC coefficient (with scale factor as needed).
200      * With typical images and quantization tables, half or more of the
201      * row DCT calculations can be simplified this way.
202      */
203
204         if ((dataptr[1] | dataptr[2] | dataptr[3] | dataptr[4] |
205                 dataptr[5] | dataptr[6] | dataptr[7]) == 0)
206         {
207       /* AC terms all zero */
208             dctelem_t dcval = (dctelem_t) (dataptr[0] << PASS1_BITS);
209
210             dataptr[0] = dcval;
211             dataptr[1] = dcval;
212             dataptr[2] = dcval;
213             dataptr[3] = dcval;
214             dataptr[4] = dcval;
215             dataptr[5] = dcval;
216             dataptr[6] = dcval;
217             dataptr[7] = dcval;
218
219             dataptr += DCTSIZE; /* advance pointer to next row */
220             continue;
221         }
222
223     /* Even part: reverse the even part of the forward DCT. */
224     /* The rotator is sqrt(2)*c(-6). */
225
226         z2 = (s32) dataptr[2];
227         z3 = (s32) dataptr[6];
228
229         z1 = MULTIPLY(z2 + z3, FIX(0.541196100));
230         tmp2 = z1 + MULTIPLY(z3, - FIX(1.847759065));
231         tmp3 = z1 + MULTIPLY(z2, FIX(0.765366865));
232
233         tmp0 = ((s32) dataptr[0] + (s32) dataptr[4]) << CONST_BITS;
234         tmp1 = ((s32) dataptr[0] - (s32) dataptr[4]) << CONST_BITS;
235
236         tmp10 = tmp0 + tmp3;
237         tmp13 = tmp0 - tmp3;
238         tmp11 = tmp1 + tmp2;
239         tmp12 = tmp1 - tmp2;
240
241     /* Odd part per figure 8; the matrix is unitary and hence its
242      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
243      */
244
245         tmp0 = (s32) dataptr[7];
246         tmp1 = (s32) dataptr[5];
247         tmp2 = (s32) dataptr[3];
248         tmp3 = (s32) dataptr[1];
249
250         z1 = tmp0 + tmp3;
251         z2 = tmp1 + tmp2;
252         z3 = tmp0 + tmp2;
253         z4 = tmp1 + tmp3;
254         z5 = MULTIPLY(z3 + z4, FIX(1.175875602)); /* sqrt(2) * c3 */
255
256         tmp0 = MULTIPLY(tmp0, FIX(0.298631336)); /* sqrt(2) * (-c1+c3+c5-c7) */
257         tmp1 = MULTIPLY(tmp1, FIX(2.053119869)); /* sqrt(2) * ( c1+c3-c5+c7) */
258         tmp2 = MULTIPLY(tmp2, FIX(3.072711026)); /* sqrt(2) * ( c1+c3+c5-c7) */
259         tmp3 = MULTIPLY(tmp3, FIX(1.501321110)); /* sqrt(2) * ( c1+c3-c5-c7) */
260         z1 = MULTIPLY(z1, - FIX(0.899976223)); /* sqrt(2) * (c7-c3) */
261         z2 = MULTIPLY(z2, - FIX(2.562915447)); /* sqrt(2) * (-c1-c3) */
262         z3 = MULTIPLY(z3, - FIX(1.961570560)); /* sqrt(2) * (-c3-c5) */
263         z4 = MULTIPLY(z4, - FIX(0.390180644)); /* sqrt(2) * (c5-c3) */
264
265         z3 += z5;
266         z4 += z5;
267
268         tmp0 += z1 + z3;
269         tmp1 += z2 + z4;
270         tmp2 += z2 + z3;
271         tmp3 += z1 + z4;
272
273     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
274
275         dataptr[0] = (dctelem_t) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
276         dataptr[7] = (dctelem_t) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
277         dataptr[1] = (dctelem_t) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
278         dataptr[6] = (dctelem_t) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
279         dataptr[2] = (dctelem_t) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
280         dataptr[5] = (dctelem_t) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
281         dataptr[3] = (dctelem_t) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
282         dataptr[4] = (dctelem_t) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
283
284         dataptr += DCTSIZE;             /* advance pointer to next row */
285     }
286
287   /* Pass 2: process columns. */
288   /* Note that we must descale the results by a factor of 8 == 2**3, */
289   /* and also undo the PASS1_BITS scaling. */
290
291     dataptr = p_block;
292     for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--)
293     {
294     /* Columns of zeroes can be exploited in the same way as we did with rows.
295      * However, the row calculation has created many nonzero AC terms, so the
296      * simplification applies less often (typically 5% to 10% of the time).
297      * On machines with very fast multiplication, it's possible that the
298      * test takes more time than it's worth.  In that case this section
299      * may be commented out.
300      */
301
302 #ifndef NO_ZERO_COLUMN_TEST /*ajoute un test mais evite des calculs */
303         if ((dataptr[DCTSIZE*1] | dataptr[DCTSIZE*2] | dataptr[DCTSIZE*3] |
304             dataptr[DCTSIZE*4] | dataptr[DCTSIZE*5] | dataptr[DCTSIZE*6] |
305             dataptr[DCTSIZE*7]) == 0)
306         {
307       /* AC terms all zero */
308             dctelem_t dcval = (dctelem_t) DESCALE((s32) dataptr[0], PASS1_BITS+3);
309
310             dataptr[DCTSIZE*0] = dcval;
311             dataptr[DCTSIZE*1] = dcval;
312             dataptr[DCTSIZE*2] = dcval;
313             dataptr[DCTSIZE*3] = dcval;
314             dataptr[DCTSIZE*4] = dcval;
315             dataptr[DCTSIZE*5] = dcval;
316             dataptr[DCTSIZE*6] = dcval;
317             dataptr[DCTSIZE*7] = dcval;
318
319             dataptr++;          /* advance pointer to next column */
320             continue;
321         }
322 #endif
323
324     /* Even part: reverse the even part of the forward DCT. */
325     /* The rotator is sqrt(2)*c(-6). */
326
327         z2 = (s32) dataptr[DCTSIZE*2];
328         z3 = (s32) dataptr[DCTSIZE*6];
329
330         z1 = MULTIPLY(z2 + z3, FIX(0.541196100));
331         tmp2 = z1 + MULTIPLY(z3, - FIX(1.847759065));
332         tmp3 = z1 + MULTIPLY(z2, FIX(0.765366865));
333
334         tmp0 = ((s32) dataptr[DCTSIZE*0] + (s32) dataptr[DCTSIZE*4]) << CONST_BITS;
335         tmp1 = ((s32) dataptr[DCTSIZE*0] - (s32) dataptr[DCTSIZE*4]) << CONST_BITS;
336
337         tmp10 = tmp0 + tmp3;
338         tmp13 = tmp0 - tmp3;
339         tmp11 = tmp1 + tmp2;
340         tmp12 = tmp1 - tmp2;
341
342     /* Odd part per figure 8; the matrix is unitary and hence its
343      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
344      */
345
346         tmp0 = (s32) dataptr[DCTSIZE*7];
347         tmp1 = (s32) dataptr[DCTSIZE*5];
348         tmp2 = (s32) dataptr[DCTSIZE*3];
349         tmp3 = (s32) dataptr[DCTSIZE*1];
350
351         z1 = tmp0 + tmp3;
352         z2 = tmp1 + tmp2;
353         z3 = tmp0 + tmp2;
354         z4 = tmp1 + tmp3;
355         z5 = MULTIPLY(z3 + z4, FIX(1.175875602)); /* sqrt(2) * c3 */
356
357         tmp0 = MULTIPLY(tmp0, FIX(0.298631336)); /* sqrt(2) * (-c1+c3+c5-c7) */
358         tmp1 = MULTIPLY(tmp1, FIX(2.053119869)); /* sqrt(2) * ( c1+c3-c5+c7) */
359         tmp2 = MULTIPLY(tmp2, FIX(3.072711026)); /* sqrt(2) * ( c1+c3+c5-c7) */
360         tmp3 = MULTIPLY(tmp3, FIX(1.501321110)); /* sqrt(2) * ( c1+c3-c5-c7) */
361         z1 = MULTIPLY(z1, - FIX(0.899976223)); /* sqrt(2) * (c7-c3) */
362         z2 = MULTIPLY(z2, - FIX(2.562915447)); /* sqrt(2) * (-c1-c3) */
363         z3 = MULTIPLY(z3, - FIX(1.961570560)); /* sqrt(2) * (-c3-c5) */
364         z4 = MULTIPLY(z4, - FIX(0.390180644)); /* sqrt(2) * (c5-c3) */
365
366         z3 += z5;
367         z4 += z5;
368
369         tmp0 += z1 + z3;
370         tmp1 += z2 + z4;
371         tmp2 += z2 + z3;
372         tmp3 += z1 + z4;
373
374     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
375
376         dataptr[DCTSIZE*0] = (dctelem_t) DESCALE(tmp10 + tmp3,
377                                            CONST_BITS+PASS1_BITS+3);
378         dataptr[DCTSIZE*7] = (dctelem_t) DESCALE(tmp10 - tmp3,
379                                            CONST_BITS+PASS1_BITS+3);
380         dataptr[DCTSIZE*1] = (dctelem_t) DESCALE(tmp11 + tmp2,
381                                            CONST_BITS+PASS1_BITS+3);
382         dataptr[DCTSIZE*6] = (dctelem_t) DESCALE(tmp11 - tmp2,
383                                            CONST_BITS+PASS1_BITS+3);
384         dataptr[DCTSIZE*2] = (dctelem_t) DESCALE(tmp12 + tmp1,
385                                            CONST_BITS+PASS1_BITS+3);
386         dataptr[DCTSIZE*5] = (dctelem_t) DESCALE(tmp12 - tmp1,
387                                            CONST_BITS+PASS1_BITS+3);
388         dataptr[DCTSIZE*3] = (dctelem_t) DESCALE(tmp13 + tmp0,
389                                            CONST_BITS+PASS1_BITS+3);
390         dataptr[DCTSIZE*4] = (dctelem_t) DESCALE(tmp13 - tmp0,
391                                            CONST_BITS+PASS1_BITS+3);
392
393         dataptr++;                      /* advance pointer to next column */
394     }
395 }
396