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

1 : edgomez 1.4 /*****************************************************************************
2 : Isibaar 1.1 *
3 : edgomez 1.4 * XVID MPEG-4 VIDEO CODEC
4 :     * - Mpeg4 quantization/dequantization functions -
5 : Isibaar 1.1 *
6 : suxen_drol 1.5 * Copyright(C) 2002 Peter Ross <pross@xvid.org>
7 : Isibaar 1.1 *
8 : edgomez 1.4 * This program is an implementation of a part of one or more MPEG-4
9 :     * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
10 :     * to use this software module in hardware or software products are
11 :     * advised that its use may infringe existing patents or copyrights, and
12 :     * any such use would be at such party's own risk. The original
13 :     * developer of this software module and his/her company, and subsequent
14 :     * editors and their companies, will have no liability for use of this
15 :     * software or modifications or derivatives thereof.
16 :     *
17 :     * This program is free software ; you can redistribute it and/or modify
18 :     * it under the terms of the GNU General Public License as published by
19 :     * the Free Software Foundation ; either version 2 of the License, or
20 :     * (at your option) any later version.
21 :     *
22 :     * This program is distributed in the hope that it will be useful,
23 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
24 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 :     * GNU General Public License for more details.
26 :     *
27 :     * You should have received a copy of the GNU General Public License
28 :     * along with this program ; if not, write to the Free Software
29 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 :     *
31 : suxen_drol 1.5 * $Id: quant_mpeg4.c,v 1.4 2002/09/07 11:21:10 edgomez Exp $
32 : edgomez 1.4 *
33 :     ****************************************************************************/
34 : Isibaar 1.1
35 :     #include "quant_mpeg4.h"
36 : Isibaar 1.2 #include "quant_matrix.h"
37 : Isibaar 1.1
38 : edgomez 1.4 /*****************************************************************************
39 :     * Function pointers
40 :     ****************************************************************************/
41 :    
42 : Isibaar 1.1 quant_intraFuncPtr quant4_intra;
43 :     quant_intraFuncPtr dequant4_intra;
44 :     dequant_interFuncPtr dequant4_inter;
45 :     quant_interFuncPtr quant4_inter;
46 :    
47 :    
48 : edgomez 1.4 /*****************************************************************************
49 :     * Local data
50 :     ****************************************************************************/
51 :    
52 : Isibaar 1.1 #define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) )
53 :     #define SIGN(A) ((A)>0?1:-1)
54 :     #define VM18P 3
55 :     #define VM18Q 4
56 :    
57 :    
58 : edgomez 1.4 /*
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 :     /*****************************************************************************
78 :     * Functions
79 :     ****************************************************************************/
80 : Isibaar 1.1
81 :     /* quantize intra-block
82 :    
83 :     // const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q);
84 :     //
85 :     // level = DIV_DIV(16 * data[i], default_intra_matrix[i]);
86 :     // coeff[i] = (level + quantd) / quant2;
87 :     */
88 :    
89 : edgomez 1.3 void
90 :     quant4_intra_c(int16_t * coeff,
91 :     const int16_t * data,
92 :     const uint32_t quant,
93 :     const uint32_t dcscalar)
94 : Isibaar 1.1 {
95 : edgomez 1.3 const uint32_t quantd = ((VM18P * quant) + (VM18Q / 2)) / VM18Q;
96 :     const uint32_t mult = multipliers[quant];
97 :     uint32_t i;
98 : Isibaar 1.2 int16_t *intra_matrix;
99 :    
100 :     intra_matrix = get_intra_matrix();
101 : Isibaar 1.1
102 : edgomez 1.3 coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar);
103 : Isibaar 1.1
104 : edgomez 1.3 for (i = 1; i < 64; i++) {
105 :     if (data[i] < 0) {
106 :     uint32_t level = -data[i];
107 :    
108 :     level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i];
109 :     level = ((level + quantd) * mult) >> 17;
110 :     coeff[i] = -(int16_t) level;
111 :     } else if (data[i] > 0) {
112 :     uint32_t level = data[i];
113 :    
114 :     level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i];
115 :     level = ((level + quantd) * mult) >> 17;
116 :     coeff[i] = level;
117 :     } else {
118 :     coeff[i] = 0;
119 :     }
120 :     }
121 : Isibaar 1.1 }
122 :    
123 :    
124 :    
125 :     /* dequantize intra-block & clamp to [-2048,2047]
126 :     // data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4;
127 :     */
128 :    
129 : edgomez 1.3 void
130 :     dequant4_intra_c(int16_t * data,
131 :     const int16_t * coeff,
132 :     const uint32_t quant,
133 :     const uint32_t dcscalar)
134 : Isibaar 1.1 {
135 : edgomez 1.3 uint32_t i;
136 : Isibaar 1.2 int16_t *intra_matrix;
137 :    
138 :     intra_matrix = get_intra_matrix();
139 : edgomez 1.3
140 :     data[0] = coeff[0] * dcscalar;
141 :     if (data[0] < -2048) {
142 :     data[0] = -2048;
143 :     } else if (data[0] > 2047) {
144 :     data[0] = 2047;
145 :     }
146 :    
147 :     for (i = 1; i < 64; i++) {
148 :     if (coeff[i] == 0) {
149 :     data[i] = 0;
150 :     } else if (coeff[i] < 0) {
151 :     uint32_t level = -coeff[i];
152 :    
153 :     level = (level * intra_matrix[i] * quant) >> 3;
154 :     data[i] = (level <= 2048 ? -(int16_t) level : -2048);
155 :     } else // if (coeff[i] > 0)
156 :     {
157 :     uint32_t level = coeff[i];
158 :    
159 :     level = (level * intra_matrix[i] * quant) >> 3;
160 :     data[i] = (level <= 2047 ? level : 2047);
161 :     }
162 :     }
163 : Isibaar 1.1 }
164 :    
165 :    
166 :    
167 :     /* quantize inter-block
168 :    
169 :     // level = DIV_DIV(16 * data[i], default_intra_matrix[i]);
170 :     // coeff[i] = (level + quantd) / quant2;
171 :     // sum += abs(level);
172 :     */
173 :    
174 : edgomez 1.3 uint32_t
175 :     quant4_inter_c(int16_t * coeff,
176 :     const int16_t * data,
177 :     const uint32_t quant)
178 : Isibaar 1.1 {
179 : edgomez 1.3 const uint32_t mult = multipliers[quant];
180 :     uint32_t sum = 0;
181 :     uint32_t i;
182 : Isibaar 1.2 int16_t *inter_matrix;
183 :    
184 :     inter_matrix = get_inter_matrix();
185 : edgomez 1.3
186 :     for (i = 0; i < 64; i++) {
187 :     if (data[i] < 0) {
188 :     uint32_t level = -data[i];
189 :    
190 :     level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
191 :     level = (level * mult) >> 17;
192 :     sum += level;
193 :     coeff[i] = -(int16_t) level;
194 :     } else if (data[i] > 0) {
195 :     uint32_t level = data[i];
196 :    
197 :     level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
198 :     level = (level * mult) >> 17;
199 :     sum += level;
200 :     coeff[i] = level;
201 :     } else {
202 :     coeff[i] = 0;
203 :     }
204 :     }
205 :     return sum;
206 : Isibaar 1.1 }
207 :    
208 :    
209 :    
210 :     /* dequantize inter-block & clamp to [-2048,2047]
211 :     data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16
212 :     */
213 :    
214 : edgomez 1.3 void
215 :     dequant4_inter_c(int16_t * data,
216 :     const int16_t * coeff,
217 :     const uint32_t quant)
218 : Isibaar 1.1 {
219 : edgomez 1.3 uint32_t sum = 0;
220 :     uint32_t i;
221 : Isibaar 1.2 int16_t *inter_matrix;
222 :    
223 :     inter_matrix = get_inter_matrix();
224 : edgomez 1.3
225 :     for (i = 0; i < 64; i++) {
226 :     if (coeff[i] == 0) {
227 :     data[i] = 0;
228 :     } else if (coeff[i] < 0) {
229 :     int32_t level = -coeff[i];
230 :    
231 :     level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;
232 :     data[i] = (level <= 2048 ? -level : -2048);
233 :     } else // if (coeff[i] > 0)
234 :     {
235 :     uint32_t level = coeff[i];
236 :    
237 :     level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;
238 :     data[i] = (level <= 2047 ? level : 2047);
239 :     }
240 :    
241 :     sum ^= data[i];
242 :     }
243 :    
244 :     // mismatch control
245 :    
246 :     if ((sum & 1) == 0) {
247 :     data[63] ^= 1;
248 :     }
249 : Isibaar 1.1 }

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