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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9.2.1 - (view) (download)

1 : edgomez 1.9 /**************************************************************************
2 : Isibaar 1.1 *
3 : edgomez 1.9 * XVID MPEG-4 VIDEO CODEC
4 :     * mpeg-4 quantization/dequantization
5 : Isibaar 1.1 *
6 : edgomez 1.9 * 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 : Isibaar 1.1 *
15 : edgomez 1.9 * 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 : edgomez 1.6 *
20 : edgomez 1.9 * 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 : edgomez 1.4 *
25 : edgomez 1.9 * 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 : edgomez 1.4 *
29 : edgomez 1.9 *************************************************************************/
30 :    
31 :     /**************************************************************************
32 : edgomez 1.6 *
33 : edgomez 1.9 * History:
34 : edgomez 1.6 *
35 : edgomez 1.9 * 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 : edgomez 1.6 *
41 : edgomez 1.9 *************************************************************************/
42 : Isibaar 1.1
43 : edgomez 1.9 #include "../global.h"
44 : Isibaar 1.1 #include "quant_mpeg4.h"
45 : Isibaar 1.2 #include "quant_matrix.h"
46 : Isibaar 1.1
47 : edgomez 1.9.2.1 /* function pointers */
48 : Isibaar 1.1 quant_intraFuncPtr quant4_intra;
49 :     quant_intraFuncPtr dequant4_intra;
50 :     dequant_interFuncPtr dequant4_inter;
51 :     quant_interFuncPtr quant4_inter;
52 :    
53 :    
54 :     #define VM18P 3
55 :     #define VM18Q 4
56 :    
57 :    
58 : edgomez 1.9.2.1 /*
59 :     * divide-by-multiply table
60 :     * need 17 bit shift (16 causes slight errors when q > 19)
61 :     */
62 : Isibaar 1.1
63 :     #define SCALEBITS 17
64 :     #define FIX(X) ((1UL << SCALEBITS) / (X) + 1)
65 :    
66 : edgomez 1.3 static const uint32_t multipliers[32] = {
67 :     0, FIX(2), FIX(4), FIX(6),
68 :     FIX(8), FIX(10), FIX(12), FIX(14),
69 :     FIX(16), FIX(18), FIX(20), FIX(22),
70 :     FIX(24), FIX(26), FIX(28), FIX(30),
71 :     FIX(32), FIX(34), FIX(36), FIX(38),
72 :     FIX(40), FIX(42), FIX(44), FIX(46),
73 :     FIX(48), FIX(50), FIX(52), FIX(54),
74 :     FIX(56), FIX(58), FIX(60), FIX(62)
75 :     };
76 : edgomez 1.4
77 : edgomez 1.9.2.1 /* quantize intra-block
78 :     *
79 :     * const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q);
80 :     * level = DIV_DIV(16 * data[i], default_intra_matrix[i]);
81 :     * coeff[i] = (level + quantd) / quant2;
82 :     */
83 : Isibaar 1.1
84 : edgomez 1.3 void
85 :     quant4_intra_c(int16_t * coeff,
86 :     const int16_t * data,
87 :     const uint32_t quant,
88 :     const uint32_t dcscalar)
89 : Isibaar 1.1 {
90 : edgomez 1.3 const uint32_t quantd = ((VM18P * quant) + (VM18Q / 2)) / VM18Q;
91 :     const uint32_t mult = multipliers[quant];
92 :     uint32_t i;
93 : Isibaar 1.2 int16_t *intra_matrix;
94 :    
95 :     intra_matrix = get_intra_matrix();
96 : Isibaar 1.1
97 : edgomez 1.9 coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar);
98 : Isibaar 1.1
99 : edgomez 1.3 for (i = 1; i < 64; i++) {
100 :     if (data[i] < 0) {
101 :     uint32_t level = -data[i];
102 :    
103 :     level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i];
104 :     level = ((level + quantd) * mult) >> 17;
105 :     coeff[i] = -(int16_t) level;
106 :     } else if (data[i] > 0) {
107 :     uint32_t level = data[i];
108 :    
109 :     level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i];
110 :     level = ((level + quantd) * mult) >> 17;
111 : edgomez 1.9 coeff[i] = level;
112 : edgomez 1.3 } else {
113 :     coeff[i] = 0;
114 :     }
115 :     }
116 : Isibaar 1.1 }
117 :    
118 :    
119 :    
120 : edgomez 1.9.2.1 /*
121 :     * dequantize intra-block & clamp to [-2048,2047]
122 :     *
123 :     * data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4;
124 :     */
125 : Isibaar 1.1
126 : edgomez 1.3 void
127 :     dequant4_intra_c(int16_t * data,
128 :     const int16_t * coeff,
129 :     const uint32_t quant,
130 :     const uint32_t dcscalar)
131 : Isibaar 1.1 {
132 : edgomez 1.3 uint32_t i;
133 : Isibaar 1.2 int16_t *intra_matrix;
134 :    
135 :     intra_matrix = get_intra_matrix();
136 : edgomez 1.3
137 : edgomez 1.9 data[0] = coeff[0] * dcscalar;
138 : edgomez 1.3 if (data[0] < -2048) {
139 :     data[0] = -2048;
140 :     } else if (data[0] > 2047) {
141 :     data[0] = 2047;
142 :     }
143 :    
144 :     for (i = 1; i < 64; i++) {
145 :     if (coeff[i] == 0) {
146 :     data[i] = 0;
147 :     } else if (coeff[i] < 0) {
148 :     uint32_t level = -coeff[i];
149 :    
150 :     level = (level * intra_matrix[i] * quant) >> 3;
151 :     data[i] = (level <= 2048 ? -(int16_t) level : -2048);
152 : edgomez 1.9.2.1 } else /* if (coeff[i] > 0) */
153 : edgomez 1.3 {
154 :     uint32_t level = coeff[i];
155 :    
156 :     level = (level * intra_matrix[i] * quant) >> 3;
157 : edgomez 1.9 data[i] = (level <= 2047 ? level : 2047);
158 : edgomez 1.3 }
159 :     }
160 : Isibaar 1.1 }
161 :    
162 :    
163 :    
164 : edgomez 1.9.2.1 /* quantize inter-block
165 :     *
166 :     * level = DIV_DIV(16 * data[i], default_intra_matrix[i]);
167 :     * coeff[i] = (level + quantd) / quant2;
168 :     * sum += abs(level);
169 : edgomez 1.9 */
170 : Isibaar 1.1
171 : edgomez 1.3 uint32_t
172 :     quant4_inter_c(int16_t * coeff,
173 :     const int16_t * data,
174 :     const uint32_t quant)
175 : Isibaar 1.1 {
176 : edgomez 1.3 const uint32_t mult = multipliers[quant];
177 :     uint32_t sum = 0;
178 :     uint32_t i;
179 : Isibaar 1.2 int16_t *inter_matrix;
180 :    
181 :     inter_matrix = get_inter_matrix();
182 : edgomez 1.3
183 :     for (i = 0; i < 64; i++) {
184 :     if (data[i] < 0) {
185 :     uint32_t level = -data[i];
186 :    
187 :     level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
188 :     level = (level * mult) >> 17;
189 :     sum += level;
190 :     coeff[i] = -(int16_t) level;
191 :     } else if (data[i] > 0) {
192 :     uint32_t level = data[i];
193 :    
194 :     level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
195 :     level = (level * mult) >> 17;
196 :     sum += level;
197 : edgomez 1.9 coeff[i] = level;
198 : edgomez 1.3 } else {
199 :     coeff[i] = 0;
200 :     }
201 :     }
202 :     return sum;
203 : Isibaar 1.1 }
204 :    
205 :    
206 :    
207 :     /* dequantize inter-block & clamp to [-2048,2047]
208 :     data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16
209 :     */
210 :    
211 : edgomez 1.3 void
212 :     dequant4_inter_c(int16_t * data,
213 :     const int16_t * coeff,
214 :     const uint32_t quant)
215 : Isibaar 1.1 {
216 : edgomez 1.3 uint32_t sum = 0;
217 :     uint32_t i;
218 : Isibaar 1.2 int16_t *inter_matrix;
219 :    
220 :     inter_matrix = get_inter_matrix();
221 : edgomez 1.3
222 :     for (i = 0; i < 64; i++) {
223 :     if (coeff[i] == 0) {
224 :     data[i] = 0;
225 :     } else if (coeff[i] < 0) {
226 :     int32_t level = -coeff[i];
227 :    
228 :     level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;
229 :     data[i] = (level <= 2048 ? -level : -2048);
230 : edgomez 1.9.2.1 } else /* if (coeff[i] > 0) */
231 : edgomez 1.3 {
232 :     uint32_t level = coeff[i];
233 :    
234 :     level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;
235 : edgomez 1.9 data[i] = (level <= 2047 ? level : 2047);
236 : edgomez 1.3 }
237 :    
238 :     sum ^= data[i];
239 :     }
240 :    
241 : edgomez 1.9.2.1 /* mismatch control */
242 : edgomez 1.3
243 :     if ((sum & 1) == 0) {
244 :     data[63] ^= 1;
245 :     }
246 : Isibaar 1.1 }

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