]> git.sesse.net Git - vlc/blob - plugins/idct/idctclassic.c
* Fixed the BeOS compile typo.
[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.10 2001/05/30 17:03:12 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_STOP
65
66 MODULE_INIT_START
67     p_module->i_capabilities = MODULE_CAPABILITY_NULL
68                                 | MODULE_CAPABILITY_IDCT;
69     p_module->psz_longname = "classic IDCT module";
70 MODULE_INIT_STOP
71
72 MODULE_ACTIVATE_START
73     idct_getfunctions( &p_module->p_functions->idct );
74 MODULE_ACTIVATE_STOP
75
76 MODULE_DEACTIVATE_START
77 MODULE_DEACTIVATE_STOP
78
79 /* Following functions are local */
80
81 /*****************************************************************************
82  * Functions exported as capabilities. They are declared as static so that
83  * we don't pollute the namespace too much.
84  *****************************************************************************/
85 static void idct_getfunctions( function_list_t * p_function_list )
86 {
87     p_function_list->pf_probe = idct_Probe;
88 #define F p_function_list->functions.idct
89     F.pf_idct_init = _M( vdec_InitIDCT );
90     F.pf_sparse_idct = _M( vdec_SparseIDCT );
91     F.pf_idct = _M( vdec_IDCT );
92     F.pf_norm_scan = vdec_NormScan;
93     F.pf_decode_init = _M( vdec_InitDecode );
94     F.pf_decode_mb_c = _M( vdec_DecodeMacroblockC );
95     F.pf_decode_mb_bw = _M( vdec_DecodeMacroblockBW );
96 #undef F
97 }
98
99 /*****************************************************************************
100  * idct_Probe: returns a preference score
101  *****************************************************************************/
102 static int idct_Probe( probedata_t *p_data )
103 {
104     if( TestMethod( IDCT_METHOD_VAR, "idctclassic" ) )
105     {
106         return( 999 );
107     }
108
109     /* This plugin always works */
110     return( 100 );
111 }
112
113 /*****************************************************************************
114  * vdec_NormScan : Unused in this IDCT
115  *****************************************************************************/
116 static void vdec_NormScan( u8 ppi_scan[2][64] )
117 {
118 }
119
120 /*****************************************************************************
121  * vdec_IDCT : IDCT function for normal matrices
122  *****************************************************************************/
123 void _M( vdec_IDCT )( vdec_thread_t * p_vdec, dctelem_t * p_block,
124                 int i_idontcare )
125 {
126     /* dct classique: pour tester la meilleure entre la classique et la */
127     /* no classique */
128     s32 tmp0, tmp1, tmp2, tmp3;
129     s32 tmp10, tmp11, tmp12, tmp13;
130     s32 z1, z2, z3, z4, z5;
131     dctelem_t * dataptr;
132     int rowctr;
133     SHIFT_TEMPS
134
135   /* Pass 1: process rows. */
136   /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
137   /* furthermore, we scale the results by 2**PASS1_BITS. */
138
139     dataptr = p_block;
140     for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--)
141     {
142     /* Due to quantization, we will usually find that many of the input
143      * coefficients are zero, especially the AC terms.  We can exploit this
144      * by short-circuiting the IDCT calculation for any row in which all
145      * the AC terms are zero.  In that case each output is equal to the
146      * DC coefficient (with scale factor as needed).
147      * With typical images and quantization tables, half or more of the
148      * row DCT calculations can be simplified this way.
149      */
150
151         if ((dataptr[1] | dataptr[2] | dataptr[3] | dataptr[4] |
152                 dataptr[5] | dataptr[6] | dataptr[7]) == 0)
153         {
154       /* AC terms all zero */
155             dctelem_t dcval = (dctelem_t) (dataptr[0] << PASS1_BITS);
156
157             dataptr[0] = dcval;
158             dataptr[1] = dcval;
159             dataptr[2] = dcval;
160             dataptr[3] = dcval;
161             dataptr[4] = dcval;
162             dataptr[5] = dcval;
163             dataptr[6] = dcval;
164             dataptr[7] = dcval;
165
166             dataptr += DCTSIZE; /* advance pointer to next row */
167             continue;
168         }
169
170     /* Even part: reverse the even part of the forward DCT. */
171     /* The rotator is sqrt(2)*c(-6). */
172
173         z2 = (s32) dataptr[2];
174         z3 = (s32) dataptr[6];
175
176         z1 = MULTIPLY(z2 + z3, FIX(0.541196100));
177         tmp2 = z1 + MULTIPLY(z3, - FIX(1.847759065));
178         tmp3 = z1 + MULTIPLY(z2, FIX(0.765366865));
179
180         tmp0 = ((s32) dataptr[0] + (s32) dataptr[4]) << CONST_BITS;
181         tmp1 = ((s32) dataptr[0] - (s32) dataptr[4]) << CONST_BITS;
182
183         tmp10 = tmp0 + tmp3;
184         tmp13 = tmp0 - tmp3;
185         tmp11 = tmp1 + tmp2;
186         tmp12 = tmp1 - tmp2;
187
188     /* Odd part per figure 8; the matrix is unitary and hence its
189      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
190      */
191
192         tmp0 = (s32) dataptr[7];
193         tmp1 = (s32) dataptr[5];
194         tmp2 = (s32) dataptr[3];
195         tmp3 = (s32) dataptr[1];
196
197         z1 = tmp0 + tmp3;
198         z2 = tmp1 + tmp2;
199         z3 = tmp0 + tmp2;
200         z4 = tmp1 + tmp3;
201         z5 = MULTIPLY(z3 + z4, FIX(1.175875602)); /* sqrt(2) * c3 */
202
203         tmp0 = MULTIPLY(tmp0, FIX(0.298631336)); /* sqrt(2) * (-c1+c3+c5-c7) */
204         tmp1 = MULTIPLY(tmp1, FIX(2.053119869)); /* sqrt(2) * ( c1+c3-c5+c7) */
205         tmp2 = MULTIPLY(tmp2, FIX(3.072711026)); /* sqrt(2) * ( c1+c3+c5-c7) */
206         tmp3 = MULTIPLY(tmp3, FIX(1.501321110)); /* sqrt(2) * ( c1+c3-c5-c7) */
207         z1 = MULTIPLY(z1, - FIX(0.899976223)); /* sqrt(2) * (c7-c3) */
208         z2 = MULTIPLY(z2, - FIX(2.562915447)); /* sqrt(2) * (-c1-c3) */
209         z3 = MULTIPLY(z3, - FIX(1.961570560)); /* sqrt(2) * (-c3-c5) */
210         z4 = MULTIPLY(z4, - FIX(0.390180644)); /* sqrt(2) * (c5-c3) */
211
212         z3 += z5;
213         z4 += z5;
214
215         tmp0 += z1 + z3;
216         tmp1 += z2 + z4;
217         tmp2 += z2 + z3;
218         tmp3 += z1 + z4;
219
220     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
221
222         dataptr[0] = (dctelem_t) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
223         dataptr[7] = (dctelem_t) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
224         dataptr[1] = (dctelem_t) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
225         dataptr[6] = (dctelem_t) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
226         dataptr[2] = (dctelem_t) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
227         dataptr[5] = (dctelem_t) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
228         dataptr[3] = (dctelem_t) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
229         dataptr[4] = (dctelem_t) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
230
231         dataptr += DCTSIZE;             /* advance pointer to next row */
232     }
233
234   /* Pass 2: process columns. */
235   /* Note that we must descale the results by a factor of 8 == 2**3, */
236   /* and also undo the PASS1_BITS scaling. */
237
238     dataptr = p_block;
239     for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--)
240     {
241     /* Columns of zeroes can be exploited in the same way as we did with rows.
242      * However, the row calculation has created many nonzero AC terms, so the
243      * simplification applies less often (typically 5% to 10% of the time).
244      * On machines with very fast multiplication, it's possible that the
245      * test takes more time than it's worth.  In that case this section
246      * may be commented out.
247      */
248
249 #ifndef NO_ZERO_COLUMN_TEST /*ajoute un test mais evite des calculs */
250         if ((dataptr[DCTSIZE*1] | dataptr[DCTSIZE*2] | dataptr[DCTSIZE*3] |
251             dataptr[DCTSIZE*4] | dataptr[DCTSIZE*5] | dataptr[DCTSIZE*6] |
252             dataptr[DCTSIZE*7]) == 0)
253         {
254       /* AC terms all zero */
255             dctelem_t dcval = (dctelem_t) DESCALE((s32) dataptr[0], PASS1_BITS+3);
256
257             dataptr[DCTSIZE*0] = dcval;
258             dataptr[DCTSIZE*1] = dcval;
259             dataptr[DCTSIZE*2] = dcval;
260             dataptr[DCTSIZE*3] = dcval;
261             dataptr[DCTSIZE*4] = dcval;
262             dataptr[DCTSIZE*5] = dcval;
263             dataptr[DCTSIZE*6] = dcval;
264             dataptr[DCTSIZE*7] = dcval;
265
266             dataptr++;          /* advance pointer to next column */
267             continue;
268         }
269 #endif
270
271     /* Even part: reverse the even part of the forward DCT. */
272     /* The rotator is sqrt(2)*c(-6). */
273
274         z2 = (s32) dataptr[DCTSIZE*2];
275         z3 = (s32) dataptr[DCTSIZE*6];
276
277         z1 = MULTIPLY(z2 + z3, FIX(0.541196100));
278         tmp2 = z1 + MULTIPLY(z3, - FIX(1.847759065));
279         tmp3 = z1 + MULTIPLY(z2, FIX(0.765366865));
280
281         tmp0 = ((s32) dataptr[DCTSIZE*0] + (s32) dataptr[DCTSIZE*4]) << CONST_BITS;
282         tmp1 = ((s32) dataptr[DCTSIZE*0] - (s32) dataptr[DCTSIZE*4]) << CONST_BITS;
283
284         tmp10 = tmp0 + tmp3;
285         tmp13 = tmp0 - tmp3;
286         tmp11 = tmp1 + tmp2;
287         tmp12 = tmp1 - tmp2;
288
289     /* Odd part per figure 8; the matrix is unitary and hence its
290      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
291      */
292
293         tmp0 = (s32) dataptr[DCTSIZE*7];
294         tmp1 = (s32) dataptr[DCTSIZE*5];
295         tmp2 = (s32) dataptr[DCTSIZE*3];
296         tmp3 = (s32) dataptr[DCTSIZE*1];
297
298         z1 = tmp0 + tmp3;
299         z2 = tmp1 + tmp2;
300         z3 = tmp0 + tmp2;
301         z4 = tmp1 + tmp3;
302         z5 = MULTIPLY(z3 + z4, FIX(1.175875602)); /* sqrt(2) * c3 */
303
304         tmp0 = MULTIPLY(tmp0, FIX(0.298631336)); /* sqrt(2) * (-c1+c3+c5-c7) */
305         tmp1 = MULTIPLY(tmp1, FIX(2.053119869)); /* sqrt(2) * ( c1+c3-c5+c7) */
306         tmp2 = MULTIPLY(tmp2, FIX(3.072711026)); /* sqrt(2) * ( c1+c3+c5-c7) */
307         tmp3 = MULTIPLY(tmp3, FIX(1.501321110)); /* sqrt(2) * ( c1+c3-c5-c7) */
308         z1 = MULTIPLY(z1, - FIX(0.899976223)); /* sqrt(2) * (c7-c3) */
309         z2 = MULTIPLY(z2, - FIX(2.562915447)); /* sqrt(2) * (-c1-c3) */
310         z3 = MULTIPLY(z3, - FIX(1.961570560)); /* sqrt(2) * (-c3-c5) */
311         z4 = MULTIPLY(z4, - FIX(0.390180644)); /* sqrt(2) * (c5-c3) */
312
313         z3 += z5;
314         z4 += z5;
315
316         tmp0 += z1 + z3;
317         tmp1 += z2 + z4;
318         tmp2 += z2 + z3;
319         tmp3 += z1 + z4;
320
321     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
322
323         dataptr[DCTSIZE*0] = (dctelem_t) DESCALE(tmp10 + tmp3,
324                                            CONST_BITS+PASS1_BITS+3);
325         dataptr[DCTSIZE*7] = (dctelem_t) DESCALE(tmp10 - tmp3,
326                                            CONST_BITS+PASS1_BITS+3);
327         dataptr[DCTSIZE*1] = (dctelem_t) DESCALE(tmp11 + tmp2,
328                                            CONST_BITS+PASS1_BITS+3);
329         dataptr[DCTSIZE*6] = (dctelem_t) DESCALE(tmp11 - tmp2,
330                                            CONST_BITS+PASS1_BITS+3);
331         dataptr[DCTSIZE*2] = (dctelem_t) DESCALE(tmp12 + tmp1,
332                                            CONST_BITS+PASS1_BITS+3);
333         dataptr[DCTSIZE*5] = (dctelem_t) DESCALE(tmp12 - tmp1,
334                                            CONST_BITS+PASS1_BITS+3);
335         dataptr[DCTSIZE*3] = (dctelem_t) DESCALE(tmp13 + tmp0,
336                                            CONST_BITS+PASS1_BITS+3);
337         dataptr[DCTSIZE*4] = (dctelem_t) DESCALE(tmp13 - tmp0,
338                                            CONST_BITS+PASS1_BITS+3);
339
340         dataptr++;                      /* advance pointer to next column */
341     }
342 }
343