[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.2 - (view) (download)

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

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