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