]> git.sesse.net Git - vlc/blob - include/vdec_idct.h
Je vous avais pr�venu. Le demoronifier a frapp�.
[vlc] / include / vdec_idct.h
1 /*****************************************************************************
2  * vdec_idct.h : types for the inverse discrete cosine transform
3  * (c)1999 VideoLAN
4  *****************************************************************************
5  *****************************************************************************
6  * Requires:
7  *  "config.h"
8  *  "common.h"
9  *  "vlc_thread.h"
10  *  "video_parser.h"
11  *****************************************************************************/
12
13 /*****************************************************************************
14  * Common declarations
15  *****************************************************************************/
16
17 #ifndef VDEC_DFT
18 typedef short dctelem_t;
19 #else
20 typedef int dctelem_t;
21 #endif
22
23 struct vdec_thread_s;
24
25 #define SPARSE_SCALE_FACTOR 8
26 #define DCTSIZE 8 /* 8*8 DCT */
27
28 /*****************************************************************************
29  *  Macros
30  *****************************************************************************/
31
32 /* We assume that right shift corresponds to signed division by 2 with
33  * rounding towards minus infinity.  This is correct for typical "arithmetic
34  * shift" instructions that shift in copies of the sign bit.  But some
35  * C compilers implement >> with an unsigned shift.  For these machines you
36  * must define RIGHT_SHIFT_IS_UNSIGNED.
37  * RIGHT_SHIFT provides a proper signed right shift of an s32 quantity.
38  * It is only applied with constant shift counts.  SHIFT_TEMPS must be
39  * included in the variables of any routine using RIGHT_SHIFT.
40  */
41
42 #ifdef RIGHT_SHIFT_IS_UNSIGNED
43 #define SHIFT_TEMPS s32 shift_temp;
44 #define RIGHT_SHIFT(x,shft)  \
45     ((shift_temp = (x)) < 0 ? \
46      (shift_temp >> (shft)) | ((~((s32) 0)) << (32-(shft))) : \
47      (shift_temp >> (shft)))
48 #else
49 #define SHIFT_TEMPS
50 #define RIGHT_SHIFT(x,shft) ((x) >> (shft))
51 #endif
52
53 /*
54  * A 2-D IDCT can be done by 1-D IDCT on each row followed by 1-D IDCT
55  * on each column.  Direct algorithms are also available, but they are
56  * much more complex and seem not to be any faster when reduced to code.
57  *
58  * The poop on this scaling stuff is as follows:
59  *
60  * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
61  * larger than the true IDCT outputs.  The final outputs are therefore
62  * a factor of N larger than desired; since N=8 this can be cured by
63  * a simple right shift at the end of the algorithm.  The advantage of
64  * this arrangement is that we save two multiplications per 1-D IDCT,
65  * because the y0 and y4 inputs need not be divided by sqrt(N).
66  *
67  * We have to do addition and subtraction of the integer inputs, which
68  * is no problem, and multiplication by fractional constants, which is
69  * a problem to do in integer arithmetic.  We multiply all the constants
70  * by CONST_SCALE and convert them to integer constants (thus retaining
71  * CONST_BITS bits of precision in the constants).  After doing a
72  * multiplication we have to divide the product by CONST_SCALE, with proper
73  * rounding, to produce the correct output.  This division can be done
74  * cheaply as a right shift of CONST_BITS bits.  We postpone shifting
75  * as long as possible so that partial sums can be added together with
76  * full fractional precision.
77  *
78  * The outputs of the first pass are scaled up by PASS1_BITS bits so that
79  * they are represented to better-than-integral precision.  These outputs
80  * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
81  * with the recommended scaling.  (To scale up 12-bit sample data further, an
82  * intermediate s32 array would be needed.)
83  *
84  * To avoid overflow of the 32-bit intermediate results in pass 2, we must
85  * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
86  * shows that the values given below are the most effective.
87  */
88
89 #define CONST_BITS 8       /* Jimmy chose this constant :) */
90
91 #ifdef EIGHT_BIT_SAMPLES
92 #define PASS1_BITS  2
93 #else
94 #define PASS1_BITS  1           /* lose a little precision to avoid overflow */
95 #endif
96
97 #define ONE     ((s32) 1)
98
99 #define CONST_SCALE (ONE << CONST_BITS)
100
101 /* Convert a positive real constant to an integer scaled by CONST_SCALE.
102  * IMPORTANT: if your compiler doesn't do this arithmetic at compile time,
103  * you will pay a significant penalty in run time.  In that case, figure
104  * the correct integer constant values and insert them by hand.
105  */
106
107 #define FIX(x)  ((s32) ((x) * CONST_SCALE + 0.5))
108
109 /* When adding two opposite-signed fixes, the 0.5 cancels */
110 #define FIX2(x) ((s32) ((x) * CONST_SCALE))
111
112 /* Descale and correctly round an s32 value that's scaled by N bits.
113  * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
114  * the fudge factor is correct for either sign of X.
115  */
116
117 #define DESCALE(x,n)  RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
118
119 /* Multiply an s32 variable by an s32 constant to yield an s32 result.
120  * For 8-bit samples with the recommended scaling, all the variable
121  * and constant values involved are no more than 16 bits wide, so a
122  * 16x16->32 bit multiply can be used instead of a full 32x32 multiply;
123  * this provides a useful speedup on many machines.
124  * There is no way to specify a 16x16->32 multiply in portable C, but
125  * some C compilers will do the right thing if you provide the correct
126  * combination of casts.
127  * NB: for 12-bit samples, a full 32-bit multiplication will be needed.
128  */
129
130 #ifdef EIGHT_BIT_SAMPLES
131 #ifdef SHORTxSHORT_32           /* may work if 'int' is 32 bits */
132 #define MULTIPLY(var,const)  (((INT16) (var)) * ((INT16) (const)))
133 #endif
134 #ifdef SHORTxLCONST_32          /* known to work with Microsoft C 6.0 */
135 #define MULTIPLY(var,const)  (((INT16) (var)) * ((s32) (const)))
136 #endif
137 #endif
138
139 #ifndef MULTIPLY                /* default definition */
140 #define MULTIPLY(var,const)  ((var) * (const))
141 #endif
142
143 /*****************************************************************************
144  * Function pointers
145  *****************************************************************************/
146 typedef void (*f_idct_t)( struct vdec_thread_s *, dctelem_t*, int );
147
148 /*****************************************************************************
149  * Prototypes
150  *****************************************************************************/
151 void vdec_InitIDCT (struct vdec_thread_s * p_vdec);
152 void vdec_SparseIDCT( struct vdec_thread_s *, dctelem_t*, int );
153 void vdec_IDCT( struct vdec_thread_s *, dctelem_t*, int );