Parent Directory
|
Revision Log
Revision 1.3 - (view) (download)
1 : | Isibaar | 1.1 | /************************************************************************** |
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 : | Isibaar | 1.2 | #include "quant_matrix.h" |
46 : | Isibaar | 1.1 | |
47 : | // function pointers | ||
48 : | quant_intraFuncPtr quant4_intra; | ||
49 : | quant_intraFuncPtr dequant4_intra; | ||
50 : | dequant_interFuncPtr dequant4_inter; | ||
51 : | quant_interFuncPtr quant4_inter; | ||
52 : | |||
53 : | |||
54 : | #define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) | ||
55 : | #define SIGN(A) ((A)>0?1:-1) | ||
56 : | #define VM18P 3 | ||
57 : | #define VM18Q 4 | ||
58 : | |||
59 : | |||
60 : | // divide-by-multiply table | ||
61 : | // need 17 bit shift (16 causes slight errors when q > 19) | ||
62 : | |||
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 : | Isibaar | 1.1 | |
77 : | /* quantize intra-block | ||
78 : | |||
79 : | // const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q); | ||
80 : | // | ||
81 : | // level = DIV_DIV(16 * data[i], default_intra_matrix[i]); | ||
82 : | // coeff[i] = (level + quantd) / quant2; | ||
83 : | */ | ||
84 : | |||
85 : | edgomez | 1.3 | void |
86 : | quant4_intra_c(int16_t * coeff, | ||
87 : | const int16_t * data, | ||
88 : | const uint32_t quant, | ||
89 : | const uint32_t dcscalar) | ||
90 : | Isibaar | 1.1 | { |
91 : | edgomez | 1.3 | const uint32_t quantd = ((VM18P * quant) + (VM18Q / 2)) / VM18Q; |
92 : | const uint32_t mult = multipliers[quant]; | ||
93 : | uint32_t i; | ||
94 : | Isibaar | 1.2 | int16_t *intra_matrix; |
95 : | |||
96 : | intra_matrix = get_intra_matrix(); | ||
97 : | Isibaar | 1.1 | |
98 : | edgomez | 1.3 | coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar); |
99 : | Isibaar | 1.1 | |
100 : | edgomez | 1.3 | for (i = 1; i < 64; i++) { |
101 : | if (data[i] < 0) { | ||
102 : | uint32_t level = -data[i]; | ||
103 : | |||
104 : | level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i]; | ||
105 : | level = ((level + quantd) * mult) >> 17; | ||
106 : | coeff[i] = -(int16_t) level; | ||
107 : | } else if (data[i] > 0) { | ||
108 : | uint32_t level = data[i]; | ||
109 : | |||
110 : | level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i]; | ||
111 : | level = ((level + quantd) * mult) >> 17; | ||
112 : | coeff[i] = level; | ||
113 : | } else { | ||
114 : | coeff[i] = 0; | ||
115 : | } | ||
116 : | } | ||
117 : | Isibaar | 1.1 | } |
118 : | |||
119 : | |||
120 : | |||
121 : | /* dequantize intra-block & clamp to [-2048,2047] | ||
122 : | // data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4; | ||
123 : | */ | ||
124 : | |||
125 : | edgomez | 1.3 | void |
126 : | dequant4_intra_c(int16_t * data, | ||
127 : | const int16_t * coeff, | ||
128 : | const uint32_t quant, | ||
129 : | const uint32_t dcscalar) | ||
130 : | Isibaar | 1.1 | { |
131 : | edgomez | 1.3 | uint32_t i; |
132 : | Isibaar | 1.2 | int16_t *intra_matrix; |
133 : | |||
134 : | intra_matrix = get_intra_matrix(); | ||
135 : | edgomez | 1.3 | |
136 : | data[0] = coeff[0] * dcscalar; | ||
137 : | if (data[0] < -2048) { | ||
138 : | data[0] = -2048; | ||
139 : | } else if (data[0] > 2047) { | ||
140 : | data[0] = 2047; | ||
141 : | } | ||
142 : | |||
143 : | for (i = 1; i < 64; i++) { | ||
144 : | if (coeff[i] == 0) { | ||
145 : | data[i] = 0; | ||
146 : | } else if (coeff[i] < 0) { | ||
147 : | uint32_t level = -coeff[i]; | ||
148 : | |||
149 : | level = (level * intra_matrix[i] * quant) >> 3; | ||
150 : | data[i] = (level <= 2048 ? -(int16_t) level : -2048); | ||
151 : | } else // if (coeff[i] > 0) | ||
152 : | { | ||
153 : | uint32_t level = coeff[i]; | ||
154 : | |||
155 : | level = (level * intra_matrix[i] * quant) >> 3; | ||
156 : | data[i] = (level <= 2047 ? level : 2047); | ||
157 : | } | ||
158 : | } | ||
159 : | Isibaar | 1.1 | } |
160 : | |||
161 : | |||
162 : | |||
163 : | /* quantize inter-block | ||
164 : | |||
165 : | // level = DIV_DIV(16 * data[i], default_intra_matrix[i]); | ||
166 : | // coeff[i] = (level + quantd) / quant2; | ||
167 : | // sum += abs(level); | ||
168 : | */ | ||
169 : | |||
170 : | edgomez | 1.3 | uint32_t |
171 : | quant4_inter_c(int16_t * coeff, | ||
172 : | const int16_t * data, | ||
173 : | const uint32_t quant) | ||
174 : | Isibaar | 1.1 | { |
175 : | edgomez | 1.3 | const uint32_t mult = multipliers[quant]; |
176 : | uint32_t sum = 0; | ||
177 : | uint32_t i; | ||
178 : | Isibaar | 1.2 | int16_t *inter_matrix; |
179 : | |||
180 : | inter_matrix = get_inter_matrix(); | ||
181 : | edgomez | 1.3 | |
182 : | for (i = 0; i < 64; i++) { | ||
183 : | if (data[i] < 0) { | ||
184 : | uint32_t level = -data[i]; | ||
185 : | |||
186 : | level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i]; | ||
187 : | level = (level * mult) >> 17; | ||
188 : | sum += level; | ||
189 : | coeff[i] = -(int16_t) level; | ||
190 : | } else if (data[i] > 0) { | ||
191 : | uint32_t level = data[i]; | ||
192 : | |||
193 : | level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i]; | ||
194 : | level = (level * mult) >> 17; | ||
195 : | sum += level; | ||
196 : | coeff[i] = level; | ||
197 : | } else { | ||
198 : | coeff[i] = 0; | ||
199 : | } | ||
200 : | } | ||
201 : | return sum; | ||
202 : | Isibaar | 1.1 | } |
203 : | |||
204 : | |||
205 : | |||
206 : | /* dequantize inter-block & clamp to [-2048,2047] | ||
207 : | data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16 | ||
208 : | */ | ||
209 : | |||
210 : | edgomez | 1.3 | void |
211 : | dequant4_inter_c(int16_t * data, | ||
212 : | const int16_t * coeff, | ||
213 : | const uint32_t quant) | ||
214 : | Isibaar | 1.1 | { |
215 : | edgomez | 1.3 | uint32_t sum = 0; |
216 : | uint32_t i; | ||
217 : | Isibaar | 1.2 | int16_t *inter_matrix; |
218 : | |||
219 : | inter_matrix = get_inter_matrix(); | ||
220 : | edgomez | 1.3 | |
221 : | for (i = 0; i < 64; i++) { | ||
222 : | if (coeff[i] == 0) { | ||
223 : | data[i] = 0; | ||
224 : | } else if (coeff[i] < 0) { | ||
225 : | int32_t level = -coeff[i]; | ||
226 : | |||
227 : | level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4; | ||
228 : | data[i] = (level <= 2048 ? -level : -2048); | ||
229 : | } else // if (coeff[i] > 0) | ||
230 : | { | ||
231 : | uint32_t level = coeff[i]; | ||
232 : | |||
233 : | level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4; | ||
234 : | data[i] = (level <= 2047 ? level : 2047); | ||
235 : | } | ||
236 : | |||
237 : | sum ^= data[i]; | ||
238 : | } | ||
239 : | |||
240 : | // mismatch control | ||
241 : | |||
242 : | if ((sum & 1) == 0) { | ||
243 : | data[63] ^= 1; | ||
244 : | } | ||
245 : | Isibaar | 1.1 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |