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

1 : edgomez 1174 /*****************************************************************************
2 : Isibaar 3 *
3 : edgomez 1174 * XVID MPEG-4 VIDEO CODEC
4 :     * - MPEG4 Quantization H263 implementation -
5 : Isibaar 3 *
6 : edgomez 1174 * Copyright(C) 2001-2003 Peter Ross <pross@xvid.org>
7 : Isibaar 3 *
8 : edgomez 1174 * This program is free software ; you can redistribute it and/or modify
9 :     * it under the terms of the GNU General Public License as published by
10 :     * the Free Software Foundation ; either version 2 of the License, or
11 :     * (at your option) any later version.
12 : Isibaar 3 *
13 : edgomez 1174 * This program is distributed in the hope that it will be useful,
14 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
15 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     * GNU General Public License for more details.
17 : Isibaar 3 *
18 : edgomez 1174 * You should have received a copy of the GNU General Public License
19 :     * along with this program ; if not, write to the Free Software
20 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 : Isibaar 3 *
22 : edgomez 1174 * $Id: quant_h263.c,v 1.7.2.2 2003-10-07 13:02:35 edgomez Exp $
23 : Isibaar 3 *
24 : edgomez 1174 ****************************************************************************/
25 : Isibaar 3
26 : edgomez 851 #include "../global.h"
27 : edgomez 1174 #include "quant.h"
28 : Isibaar 3
29 : edgomez 1174 /*****************************************************************************
30 :     * Global function pointers
31 :     ****************************************************************************/
32 : Isibaar 3
33 : edgomez 1174 /* Quant */
34 :     quant_intraFuncPtr quant_h263_intra;
35 :     quant_interFuncPtr quant_h263_inter;
36 :    
37 :     /* DeQuant */
38 :     quant_intraFuncPtr dequant_h263_intra;
39 :     quant_interFuncPtr dequant_h263_inter;
40 :    
41 :     /*****************************************************************************
42 :     * Local data
43 :     ****************************************************************************/
44 :    
45 :     /* divide-by-multiply table
46 :     * a 16 bit shiting is enough in this case */
47 :    
48 : Isibaar 3 #define SCALEBITS 16
49 :     #define FIX(X) ((1L << SCALEBITS) / (X) + 1)
50 :    
51 : edgomez 1174 static const uint32_t multipliers[32] =
52 :     {
53 :     0, FIX(2), FIX(4), FIX(6),
54 :     FIX(8), FIX(10), FIX(12), FIX(14),
55 : edgomez 195 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 : edgomez 1174 /*****************************************************************************
64 :     * Function definitions
65 :     ****************************************************************************/
66 : Isibaar 3
67 :     /* quantize intra-block
68 : edgomez 1174 */
69 : Isibaar 3
70 : edgomez 1174 uint32_t
71 :     quant_h263_intra_c(int16_t * coeff,
72 :     const int16_t * data,
73 :     const uint32_t quant,
74 :     const uint32_t dcscalar)
75 : Isibaar 3 {
76 :     const uint32_t mult = multipliers[quant];
77 : edgomez 851 const uint16_t quant_m_2 = quant << 1;
78 : edgomez 1174 uint32_t sum = 0;
79 :     int i;
80 : Isibaar 3
81 : edgomez 851 coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar);
82 : edgomez 1174 sum += coeff[0];
83 : Isibaar 3
84 :     for (i = 1; i < 64; i++) {
85 :     int16_t acLevel = data[i];
86 :    
87 :     if (acLevel < 0) {
88 :     acLevel = -acLevel;
89 :     if (acLevel < quant_m_2) {
90 :     coeff[i] = 0;
91 :     continue;
92 :     }
93 : edgomez 851 acLevel = (acLevel * mult) >> SCALEBITS;
94 : edgomez 1174 sum += acLevel;
95 : Isibaar 3 coeff[i] = -acLevel;
96 :     } else {
97 :     if (acLevel < quant_m_2) {
98 :     coeff[i] = 0;
99 :     continue;
100 :     }
101 : edgomez 851 acLevel = (acLevel * mult) >> SCALEBITS;
102 : edgomez 1174 sum += acLevel;
103 : Isibaar 3 coeff[i] = acLevel;
104 :     }
105 :     }
106 : edgomez 1174
107 :     return(sum);
108 : Isibaar 3 }
109 :    
110 :    
111 :     /* quantize inter-block
112 : edgomez 1174 */
113 : Isibaar 3
114 : edgomez 195 uint32_t
115 : edgomez 1174 quant_h263_inter_c(int16_t * coeff,
116 :     const int16_t * data,
117 :     const uint32_t quant)
118 : Isibaar 3 {
119 :     const uint32_t mult = multipliers[quant];
120 : edgomez 851 const uint16_t quant_m_2 = quant << 1;
121 :     const uint16_t quant_d_2 = quant >> 1;
122 : edgomez 1174 uint32_t sum = 0;
123 : Isibaar 3 uint32_t i;
124 :    
125 :     for (i = 0; i < 64; i++) {
126 :     int16_t acLevel = data[i];
127 : edgomez 195
128 : Isibaar 3 if (acLevel < 0) {
129 :     acLevel = (-acLevel) - quant_d_2;
130 :     if (acLevel < quant_m_2) {
131 :     coeff[i] = 0;
132 :     continue;
133 :     }
134 :    
135 : edgomez 851 acLevel = (acLevel * mult) >> SCALEBITS;
136 : edgomez 1053 sum += acLevel; /* sum += |acLevel| */
137 : Isibaar 3 coeff[i] = -acLevel;
138 :     } else {
139 :     acLevel -= quant_d_2;
140 :     if (acLevel < quant_m_2) {
141 :     coeff[i] = 0;
142 :     continue;
143 :     }
144 : edgomez 851 acLevel = (acLevel * mult) >> SCALEBITS;
145 : Isibaar 3 sum += acLevel;
146 :     coeff[i] = acLevel;
147 :     }
148 :     }
149 :    
150 : edgomez 1174 return(sum);
151 : Isibaar 3 }
152 :    
153 :    
154 :     /* dequantize intra-block & clamp to [-2048,2047]
155 : edgomez 1174 */
156 : Isibaar 3
157 : edgomez 1174 uint32_t
158 :     dequant_h263_intra_c(int16_t * data,
159 :     const int16_t * coeff,
160 :     const uint32_t quant,
161 :     const uint32_t dcscalar)
162 : Isibaar 3 {
163 :     const int32_t quant_m_2 = quant << 1;
164 :     const int32_t quant_add = (quant & 1 ? quant : quant - 1);
165 : edgomez 1174 int i;
166 : Isibaar 3
167 : edgomez 851 data[0] = coeff[0] * dcscalar;
168 : edgomez 195 if (data[0] < -2048) {
169 : Isibaar 3 data[0] = -2048;
170 : edgomez 195 } else if (data[0] > 2047) {
171 : Isibaar 3 data[0] = 2047;
172 :     }
173 :    
174 :     for (i = 1; i < 64; i++) {
175 :     int32_t acLevel = coeff[i];
176 : edgomez 195
177 :     if (acLevel == 0) {
178 : Isibaar 3 data[i] = 0;
179 : edgomez 195 } else if (acLevel < 0) {
180 : Isibaar 3 acLevel = quant_m_2 * -acLevel + quant_add;
181 :     data[i] = (acLevel <= 2048 ? -acLevel : -2048);
182 : edgomez 1174 } else {
183 : Isibaar 3 acLevel = quant_m_2 * acLevel + quant_add;
184 :     data[i] = (acLevel <= 2047 ? acLevel : 2047);
185 :     }
186 :     }
187 : edgomez 1174
188 :     return(0);
189 : Isibaar 3 }
190 :    
191 :    
192 :    
193 :     /* dequantize inter-block & clamp to [-2048,2047]
194 : edgomez 1174 */
195 : Isibaar 3
196 : edgomez 1174 uint32_t
197 :     dequant_h263_inter_c(int16_t * data,
198 : edgomez 195 const int16_t * coeff,
199 :     const uint32_t quant)
200 : Isibaar 3 {
201 : edgomez 851 const uint16_t quant_m_2 = quant << 1;
202 :     const uint16_t quant_add = (quant & 1 ? quant : quant - 1);
203 : edgomez 1174 int i;
204 : Isibaar 3
205 :     for (i = 0; i < 64; i++) {
206 :     int16_t acLevel = coeff[i];
207 : edgomez 195
208 :     if (acLevel == 0) {
209 : Isibaar 3 data[i] = 0;
210 : edgomez 195 } else if (acLevel < 0) {
211 : Isibaar 3 acLevel = acLevel * quant_m_2 - quant_add;
212 :     data[i] = (acLevel >= -2048 ? acLevel : -2048);
213 : edgomez 1174 } else {
214 : Isibaar 3 acLevel = acLevel * quant_m_2 + quant_add;
215 :     data[i] = (acLevel <= 2047 ? acLevel : 2047);
216 :     }
217 :     }
218 : edgomez 1174
219 :     return(0);
220 : Isibaar 3 }

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