[svn] / branches / dev-api-4 / xvidcore / src / quant / quant_h263.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/quant/quant_h263.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 886 - (view) (download)

1 : Isibaar 3 /**************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 : edgomez 851 * 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.12.2001 dequant_inter bug fix
36 :     * 22.12.2001 clamp dequant output to [-2048,2047]
37 :     * 19.11.2001 quant_inter now returns sum of abs. coefficient values
38 :     * 02.11.2001 added const to function args <pross@cs.rmit.edu.au>
39 :     * 28.10.2001 total rewrite <pross@cs.rmit.edu.au>
40 : Isibaar 3 *
41 :     *************************************************************************/
42 :    
43 : edgomez 851 #include "../global.h"
44 : Isibaar 3 #include "quant_h263.h"
45 :    
46 :     /* mutliply+shift division table
47 :     */
48 :    
49 :     #define SCALEBITS 16
50 :     #define FIX(X) ((1L << SCALEBITS) / (X) + 1)
51 :    
52 : edgomez 195 static const uint32_t multipliers[32] = {
53 :     0, FIX(2), FIX(4), FIX(6),
54 :     FIX(8), FIX(10), FIX(12), FIX(14),
55 :     FIX(16), FIX(18), FIX(20), FIX(22),
56 :     FIX(24), FIX(26), FIX(28), FIX(30),
57 :     FIX(32), FIX(34), FIX(36), FIX(38),
58 :     FIX(40), FIX(42), FIX(44), FIX(46),
59 :     FIX(48), FIX(50), FIX(52), FIX(54),
60 :     FIX(56), FIX(58), FIX(60), FIX(62)
61 :     };
62 : Isibaar 3
63 :    
64 :    
65 : edgomez 851 // function pointers
66 : Isibaar 3 quanth263_intraFuncPtr quant_intra;
67 :     quanth263_intraFuncPtr dequant_intra;
68 :    
69 :     quanth263_interFuncPtr quant_inter;
70 :     dequanth263_interFuncPtr dequant_inter;
71 :    
72 :    
73 :    
74 :     /* quantize intra-block
75 :     */
76 :    
77 :    
78 : edgomez 195 void
79 :     quant_intra_c(int16_t * coeff,
80 :     const int16_t * data,
81 :     const uint32_t quant,
82 :     const uint32_t dcscalar)
83 : Isibaar 3 {
84 :     const uint32_t mult = multipliers[quant];
85 : edgomez 851 const uint16_t quant_m_2 = quant << 1;
86 : edgomez 195 uint32_t i;
87 : Isibaar 3
88 : edgomez 851 coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar);
89 : Isibaar 3
90 :     for (i = 1; i < 64; i++) {
91 :     int16_t acLevel = data[i];
92 :    
93 :     if (acLevel < 0) {
94 :     acLevel = -acLevel;
95 :     if (acLevel < quant_m_2) {
96 :     coeff[i] = 0;
97 :     continue;
98 :     }
99 : edgomez 851 acLevel = (acLevel * mult) >> SCALEBITS;
100 : Isibaar 3 coeff[i] = -acLevel;
101 :     } else {
102 :     if (acLevel < quant_m_2) {
103 :     coeff[i] = 0;
104 :     continue;
105 :     }
106 : edgomez 851 acLevel = (acLevel * mult) >> SCALEBITS;
107 : Isibaar 3 coeff[i] = acLevel;
108 :     }
109 :     }
110 :     }
111 :    
112 :    
113 :     /* quantize inter-block
114 :     */
115 :    
116 : edgomez 195 uint32_t
117 :     quant_inter_c(int16_t * coeff,
118 :     const int16_t * data,
119 :     const uint32_t quant)
120 : Isibaar 3 {
121 :     const uint32_t mult = multipliers[quant];
122 : edgomez 851 const uint16_t quant_m_2 = quant << 1;
123 :     const uint16_t quant_d_2 = quant >> 1;
124 : Isibaar 3 int sum = 0;
125 :     uint32_t i;
126 :    
127 :     for (i = 0; i < 64; i++) {
128 :     int16_t acLevel = data[i];
129 : edgomez 195
130 : Isibaar 3 if (acLevel < 0) {
131 :     acLevel = (-acLevel) - quant_d_2;
132 :     if (acLevel < quant_m_2) {
133 :     coeff[i] = 0;
134 :     continue;
135 :     }
136 :    
137 : edgomez 851 acLevel = (acLevel * mult) >> SCALEBITS;
138 :     sum += acLevel; // sum += |acLevel|
139 : Isibaar 3 coeff[i] = -acLevel;
140 :     } else {
141 :     acLevel -= quant_d_2;
142 :     if (acLevel < quant_m_2) {
143 :     coeff[i] = 0;
144 :     continue;
145 :     }
146 : edgomez 851 acLevel = (acLevel * mult) >> SCALEBITS;
147 : Isibaar 3 sum += acLevel;
148 :     coeff[i] = acLevel;
149 :     }
150 :     }
151 :    
152 :     return sum;
153 :     }
154 :    
155 :    
156 :     /* dequantize intra-block & clamp to [-2048,2047]
157 :     */
158 :    
159 : edgomez 195 void
160 :     dequant_intra_c(int16_t * data,
161 :     const int16_t * coeff,
162 :     const uint32_t quant,
163 :     const uint32_t dcscalar)
164 : Isibaar 3 {
165 :     const int32_t quant_m_2 = quant << 1;
166 :     const int32_t quant_add = (quant & 1 ? quant : quant - 1);
167 : edgomez 195 uint32_t i;
168 : Isibaar 3
169 : edgomez 851 data[0] = coeff[0] * dcscalar;
170 : edgomez 195 if (data[0] < -2048) {
171 : Isibaar 3 data[0] = -2048;
172 : edgomez 195 } else if (data[0] > 2047) {
173 : Isibaar 3 data[0] = 2047;
174 :     }
175 :    
176 :    
177 :     for (i = 1; i < 64; i++) {
178 :     int32_t acLevel = coeff[i];
179 : edgomez 195
180 :     if (acLevel == 0) {
181 : Isibaar 3 data[i] = 0;
182 : edgomez 195 } else if (acLevel < 0) {
183 : Isibaar 3 acLevel = quant_m_2 * -acLevel + quant_add;
184 :     data[i] = (acLevel <= 2048 ? -acLevel : -2048);
185 : edgomez 851 } else // if (acLevel > 0) {
186 : edgomez 195 {
187 : Isibaar 3 acLevel = quant_m_2 * acLevel + quant_add;
188 :     data[i] = (acLevel <= 2047 ? acLevel : 2047);
189 :     }
190 :     }
191 :     }
192 :    
193 :    
194 :    
195 :     /* dequantize inter-block & clamp to [-2048,2047]
196 :     */
197 :    
198 : edgomez 195 void
199 :     dequant_inter_c(int16_t * data,
200 :     const int16_t * coeff,
201 :     const uint32_t quant)
202 : Isibaar 3 {
203 : edgomez 851 const uint16_t quant_m_2 = quant << 1;
204 :     const uint16_t quant_add = (quant & 1 ? quant : quant - 1);
205 : Isibaar 3 uint32_t i;
206 :    
207 :     for (i = 0; i < 64; i++) {
208 :     int16_t acLevel = coeff[i];
209 : edgomez 195
210 :     if (acLevel == 0) {
211 : Isibaar 3 data[i] = 0;
212 : edgomez 195 } else if (acLevel < 0) {
213 : Isibaar 3 acLevel = acLevel * quant_m_2 - quant_add;
214 :     data[i] = (acLevel >= -2048 ? acLevel : -2048);
215 : edgomez 851 } else // if (acLevel > 0)
216 : edgomez 195 {
217 : Isibaar 3 acLevel = acLevel * quant_m_2 + quant_add;
218 :     data[i] = (acLevel <= 2047 ? acLevel : 2047);
219 :     }
220 :     }
221 :     }

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