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