Parent Directory | Revision Log
Revision 1053 - (view) (download)
1 : | edgomez | 851 | /************************************************************************** |
2 : | Isibaar | 3 | * |
3 : | edgomez | 851 | * XVID MPEG-4 VIDEO CODEC |
4 : | * mpeg-4 quantization/dequantization | ||
5 : | Isibaar | 3 | * |
6 : | edgomez | 851 | * 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 | 3 | * |
15 : | edgomez | 851 | * 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 : | Isibaar | 3 | * |
20 : | edgomez | 851 | * 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 : | Isibaar | 3 | * |
25 : | edgomez | 851 | * 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 : | Isibaar | 3 | * |
29 : | edgomez | 851 | *************************************************************************/ |
30 : | |||
31 : | /************************************************************************** | ||
32 : | Isibaar | 3 | * |
33 : | edgomez | 851 | * History: |
34 : | Isibaar | 3 | * |
35 : | edgomez | 851 | * 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 | 653 | * |
41 : | edgomez | 851 | *************************************************************************/ |
42 : | Isibaar | 3 | |
43 : | edgomez | 851 | #include "../global.h" |
44 : | Isibaar | 3 | #include "quant_mpeg4.h" |
45 : | Isibaar | 4 | #include "quant_matrix.h" |
46 : | Isibaar | 3 | |
47 : | edgomez | 1053 | /* function pointers */ |
48 : | Isibaar | 3 | 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 | 1053 | /* |
59 : | * divide-by-multiply table | ||
60 : | * need 17 bit shift (16 causes slight errors when q > 19) | ||
61 : | */ | ||
62 : | Isibaar | 3 | |
63 : | #define SCALEBITS 17 | ||
64 : | #define FIX(X) ((1UL << SCALEBITS) / (X) + 1) | ||
65 : | |||
66 : | edgomez | 195 | 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 : | Isibaar | 3 | |
77 : | edgomez | 1053 | /* 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 : | edgomez | 442 | |
84 : | edgomez | 195 | 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 | 3 | { |
90 : | edgomez | 195 | const uint32_t quantd = ((VM18P * quant) + (VM18Q / 2)) / VM18Q; |
91 : | const uint32_t mult = multipliers[quant]; | ||
92 : | uint32_t i; | ||
93 : | Isibaar | 4 | int16_t *intra_matrix; |
94 : | Isibaar | 3 | |
95 : | Isibaar | 4 | intra_matrix = get_intra_matrix(); |
96 : | |||
97 : | edgomez | 851 | coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar); |
98 : | Isibaar | 3 | |
99 : | edgomez | 195 | 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 | 851 | coeff[i] = level; |
112 : | edgomez | 195 | } else { |
113 : | coeff[i] = 0; | ||
114 : | } | ||
115 : | } | ||
116 : | Isibaar | 3 | } |
117 : | |||
118 : | |||
119 : | |||
120 : | edgomez | 1053 | /* |
121 : | * dequantize intra-block & clamp to [-2048,2047] | ||
122 : | * | ||
123 : | * data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4; | ||
124 : | */ | ||
125 : | Isibaar | 3 | |
126 : | edgomez | 195 | 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 | 3 | { |
132 : | edgomez | 195 | uint32_t i; |
133 : | Isibaar | 4 | int16_t *intra_matrix; |
134 : | |||
135 : | intra_matrix = get_intra_matrix(); | ||
136 : | Isibaar | 3 | |
137 : | edgomez | 851 | data[0] = coeff[0] * dcscalar; |
138 : | edgomez | 195 | 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 | 1053 | } else /* if (coeff[i] > 0) */ |
153 : | edgomez | 195 | { |
154 : | uint32_t level = coeff[i]; | ||
155 : | |||
156 : | level = (level * intra_matrix[i] * quant) >> 3; | ||
157 : | edgomez | 851 | data[i] = (level <= 2047 ? level : 2047); |
158 : | edgomez | 195 | } |
159 : | } | ||
160 : | Isibaar | 3 | } |
161 : | |||
162 : | |||
163 : | |||
164 : | edgomez | 1053 | /* 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 | 851 | */ |
170 : | Isibaar | 3 | |
171 : | edgomez | 195 | uint32_t |
172 : | quant4_inter_c(int16_t * coeff, | ||
173 : | const int16_t * data, | ||
174 : | const uint32_t quant) | ||
175 : | Isibaar | 3 | { |
176 : | edgomez | 195 | const uint32_t mult = multipliers[quant]; |
177 : | uint32_t sum = 0; | ||
178 : | uint32_t i; | ||
179 : | Isibaar | 4 | int16_t *inter_matrix; |
180 : | |||
181 : | inter_matrix = get_inter_matrix(); | ||
182 : | edgomez | 195 | |
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 | 851 | coeff[i] = level; |
198 : | edgomez | 195 | } else { |
199 : | coeff[i] = 0; | ||
200 : | } | ||
201 : | } | ||
202 : | return sum; | ||
203 : | Isibaar | 3 | } |
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 | 195 | void |
212 : | dequant4_inter_c(int16_t * data, | ||
213 : | const int16_t * coeff, | ||
214 : | const uint32_t quant) | ||
215 : | Isibaar | 3 | { |
216 : | edgomez | 195 | uint32_t sum = 0; |
217 : | uint32_t i; | ||
218 : | Isibaar | 4 | int16_t *inter_matrix; |
219 : | |||
220 : | inter_matrix = get_inter_matrix(); | ||
221 : | Isibaar | 3 | |
222 : | edgomez | 195 | 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 : | Isibaar | 3 | |
228 : | edgomez | 195 | level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4; |
229 : | data[i] = (level <= 2048 ? -level : -2048); | ||
230 : | edgomez | 1053 | } else /* if (coeff[i] > 0) */ |
231 : | edgomez | 195 | { |
232 : | uint32_t level = coeff[i]; | ||
233 : | Isibaar | 3 | |
234 : | edgomez | 195 | level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4; |
235 : | edgomez | 851 | data[i] = (level <= 2047 ? level : 2047); |
236 : | edgomez | 195 | } |
237 : | |||
238 : | sum ^= data[i]; | ||
239 : | } | ||
240 : | |||
241 : | edgomez | 1053 | /* mismatch control */ |
242 : | edgomez | 195 | |
243 : | if ((sum & 1) == 0) { | ||
244 : | data[63] ^= 1; | ||
245 : | } | ||
246 : | Isibaar | 3 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |