[svn] / trunk / xvidcore / src / quant / quant_mpeg4.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/quant/quant_mpeg4.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (view) (download)

1 : Isibaar 3 /**************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * mpeg-4 quantization/dequantization
5 :     *
6 :     * This program is an implementation of a part of one or more MPEG-4
7 :     * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
8 :     * to use this software module in hardware or software products are
9 :     * advised that its use may infringe existing patents or copyrights, and
10 :     * any such use would be at such party's own risk. The original
11 :     * developer of this software module and his/her company, and subsequent
12 :     * editors and their companies, will have no liability for use of this
13 :     * software or modifications or derivatives thereof.
14 :     *
15 :     * This program is free software; you can redistribute it and/or modify
16 :     * it under the terms of the GNU General Public License as published by
17 :     * the Free Software Foundation; either version 2 of the License, or
18 :     * (at your option) any later version.
19 :     *
20 :     * This program is distributed in the hope that it will be useful,
21 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 :     * GNU General Public License for more details.
24 :     *
25 :     * You should have received a copy of the GNU General Public License
26 :     * along with this program; if not, write to the Free Software
27 :     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 :     *
29 :     *************************************************************************/
30 :    
31 :     /**************************************************************************
32 :     *
33 :     * History:
34 :     *
35 :     * 26.01.2002 fixed quant4_intra dcscalar signed/unsigned error
36 :     * 20.01.2002 increased accuracy of >> divide
37 :     * 26.12.2001 divide-by-multiplication optimization
38 :     * 22.12.2001 [-127,127] clamping removed; minor tweaks
39 :     * 19.11.2001 inital version <pross@cs.rmit.edu.au>
40 :     *
41 :     *************************************************************************/
42 :    
43 :    
44 :     #include "quant_mpeg4.h"
45 :    
46 :    
47 :    
48 :     // function pointers
49 :     quant_intraFuncPtr quant4_intra;
50 :     quant_intraFuncPtr dequant4_intra;
51 :     dequant_interFuncPtr dequant4_inter;
52 :     quant_interFuncPtr quant4_inter;
53 :    
54 :    
55 :     #define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) )
56 :     #define SIGN(A) ((A)>0?1:-1)
57 :     #define VM18P 3
58 :     #define VM18Q 4
59 :    
60 :    
61 :     // divide-by-multiply table
62 :     // need 17 bit shift (16 causes slight errors when q > 19)
63 :    
64 :     #define SCALEBITS 17
65 :     #define FIX(X) ((1UL << SCALEBITS) / (X) + 1)
66 :    
67 :     static const uint32_t multipliers[32] =
68 :     {
69 :     0, FIX(2), FIX(4), FIX(6),
70 :     FIX(8), FIX(10), FIX(12), FIX(14),
71 :     FIX(16), FIX(18), FIX(20), FIX(22),
72 :     FIX(24), FIX(26), FIX(28), FIX(30),
73 :     FIX(32), FIX(34), FIX(36), FIX(38),
74 :     FIX(40), FIX(42), FIX(44), FIX(46),
75 :     FIX(48), FIX(50), FIX(52), FIX(54),
76 :     FIX(56), FIX(58), FIX(60), FIX(62)
77 :     };
78 :    
79 :    
80 :     static const int16_t default_intra_matrix[64] = {
81 :     8,17,18,19,21,23,25,27,
82 :     17,18,19,21,23,25,27,28,
83 :     20,21,22,23,24,26,28,30,
84 :     21,22,23,24,26,28,30,32,
85 :     22,23,24,26,28,30,32,35,
86 :     23,24,26,28,30,32,35,38,
87 :     25,26,28,30,32,35,38,41,
88 :     27,28,30,32,35,38,41,45
89 :     };
90 :    
91 :     static const int16_t default_inter_matrix[64] = {
92 :     16,17,18,19,20,21,22,23,
93 :     17,18,19,20,21,22,23,24,
94 :     18,19,20,21,22,23,24,25,
95 :     19,20,21,22,23,24,26,27,
96 :     20,21,22,23,25,26,27,28,
97 :     21,22,23,24,26,27,28,30,
98 :     22,23,24,26,27,28,30,31,
99 :     23,24,25,27,28,30,31,33
100 :     };
101 :    
102 :    
103 :     /* quantize intra-block
104 :    
105 :     // const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q);
106 :     //
107 :     // level = DIV_DIV(16 * data[i], default_intra_matrix[i]);
108 :     // coeff[i] = (level + quantd) / quant2;
109 :     */
110 :    
111 :     void quant4_intra_c(int16_t * coeff, const int16_t * data, const uint32_t quant, const uint32_t dcscalar)
112 :     {
113 :     const uint32_t quantd = ((VM18P*quant) + (VM18Q/2)) / VM18Q;
114 :     const uint32_t mult = multipliers[quant];
115 :     uint32_t i;
116 :    
117 :     coeff[0] = DIV_DIV(data[0], (int32_t)dcscalar);
118 :    
119 :     for (i = 1; i < 64; i++)
120 :     {
121 :     if (data[i] < 0)
122 :     {
123 :     uint32_t level = -data[i];
124 :     level = ((level<<4) + (default_intra_matrix[i]>>1)) / default_intra_matrix[i];
125 :     level = ((level + quantd) * mult) >> 17;
126 :     coeff[i] = -(int16_t)level;
127 :     }
128 :     else if (data[i] > 0)
129 :     {
130 :     uint32_t level = data[i];
131 :     level = ((level<<4) + (default_intra_matrix[i]>>1)) / default_intra_matrix[i];
132 :     level = ((level + quantd) * mult) >> 17;
133 :     coeff[i] = level;
134 :     }
135 :     else
136 :     {
137 :     coeff[i] = 0;
138 :     }
139 :     }
140 :     }
141 :    
142 :    
143 :    
144 :     /* dequantize intra-block & clamp to [-2048,2047]
145 :     // data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4;
146 :     */
147 :    
148 :     void dequant4_intra_c(int16_t *data, const int16_t *coeff, const uint32_t quant, const uint32_t dcscalar)
149 :     {
150 :     uint32_t i;
151 :    
152 :     data[0] = coeff[0] * dcscalar;
153 :     if (data[0] < -2048)
154 :     {
155 :     data[0] = -2048;
156 :     }
157 :     else if (data[0] > 2047)
158 :     {
159 :     data[0] = 2047;
160 :     }
161 :    
162 :     for (i = 1; i < 64; i++)
163 :     {
164 :     if (coeff[i] == 0)
165 :     {
166 :     data[i] = 0;
167 :     }
168 :     else if (coeff[i] < 0)
169 :     {
170 :     uint32_t level = -coeff[i];
171 :     level = (level * default_intra_matrix[i] * quant) >> 3;
172 :     data[i] = (level <= 2048 ? -(int16_t)level : -2048);
173 :     }
174 :     else // if (coeff[i] > 0)
175 :     {
176 :     uint32_t level = coeff[i];
177 :     level = (level * default_intra_matrix[i] * quant) >> 3;
178 :     data[i] = (level <= 2047 ? level : 2047);
179 :     }
180 :     }
181 :     }
182 :    
183 :    
184 :    
185 :     /* quantize inter-block
186 :    
187 :     // level = DIV_DIV(16 * data[i], default_intra_matrix[i]);
188 :     // coeff[i] = (level + quantd) / quant2;
189 :     // sum += abs(level);
190 :     */
191 :    
192 :     uint32_t quant4_inter_c(int16_t * coeff, const int16_t * data, const uint32_t quant)
193 :     {
194 :     const uint32_t mult = multipliers[quant];
195 :     uint32_t sum = 0;
196 :     uint32_t i;
197 :    
198 :     for (i = 0; i < 64; i++)
199 :     {
200 :     if (data[i] < 0)
201 :     {
202 :     uint32_t level = -data[i];
203 :     level = ((level<<4) + (default_inter_matrix[i]>>1)) / default_inter_matrix[i];
204 :     level = (level * mult) >> 17;
205 :     sum += level;
206 :     coeff[i] = -(int16_t)level;
207 :     }
208 :     else if (data[i] > 0)
209 :     {
210 :     uint32_t level = data[i];
211 :     level = ((level<<4) + (default_inter_matrix[i]>>1)) / default_inter_matrix[i];
212 :     level = (level * mult) >> 17;
213 :     sum += level;
214 :     coeff[i] = level;
215 :     }
216 :     else
217 :     {
218 :     coeff[i] = 0;
219 :     }
220 :     }
221 :     return sum;
222 :     }
223 :    
224 :    
225 :    
226 :     /* dequantize inter-block & clamp to [-2048,2047]
227 :     data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16
228 :     */
229 :    
230 :     void dequant4_inter_c(int16_t *data, const int16_t *coeff, const uint32_t quant)
231 :     {
232 :     uint32_t sum = 0;
233 :     uint32_t i;
234 :    
235 :     for (i = 0; i < 64; i++)
236 :     {
237 :     if (coeff[i] == 0)
238 :     {
239 :     data[i] = 0;
240 :     }
241 :     else if (coeff[i] < 0)
242 :     {
243 :     int32_t level = -coeff[i];
244 :     level = ((2 * level + 1) * default_inter_matrix[i] * quant) >> 4;
245 :     data[i] = (level <= 2048 ? -level : -2048);
246 :     }
247 :     else // if (coeff[i] > 0)
248 :     {
249 :     uint32_t level = coeff[i];
250 :     level = ((2 * level + 1) * default_inter_matrix[i] * quant) >> 4;
251 :     data[i] = (level <= 2047 ? level : 2047);
252 :     }
253 :    
254 :     sum ^= data[i];
255 :     }
256 :    
257 :     // mismatch control
258 :    
259 :     if ((sum & 1) == 0)
260 :     {
261 :     data[63] ^= 1;
262 :     }
263 :     }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4