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