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