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