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