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