[cvs] / xvidcore / src / quant / quant_h263.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/quant/quant_h263.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7.2.3 - (view) (download)

1 : edgomez 1.7.2.2 /*****************************************************************************
2 : Isibaar 1.1 *
3 : edgomez 1.7.2.2 * XVID MPEG-4 VIDEO CODEC
4 :     * - MPEG4 Quantization H263 implementation -
5 : Isibaar 1.1 *
6 : edgomez 1.7.2.2 * Copyright(C) 2001-2003 Peter Ross <pross@xvid.org>
7 : Isibaar 1.1 *
8 : edgomez 1.7.2.2 * 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 :     *
13 :     * 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 :     *
18 :     * 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 :     *
22 :     * $Id$
23 :     *
24 :     ****************************************************************************/
25 : Isibaar 1.1
26 : edgomez 1.7 #include "../global.h"
27 : edgomez 1.7.2.2 #include "quant.h"
28 : Isibaar 1.1
29 : edgomez 1.7.2.2 /*****************************************************************************
30 :     * Global function pointers
31 :     ****************************************************************************/
32 :    
33 :     /* 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 : Isibaar 1.1
48 :     #define SCALEBITS 16
49 :     #define FIX(X) ((1L << SCALEBITS) / (X) + 1)
50 :    
51 : edgomez 1.7.2.2 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 1.2 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 1.1
63 : edgomez 1.7.2.2 /*****************************************************************************
64 :     * Function definitions
65 :     ****************************************************************************/
66 : Isibaar 1.1
67 :     /* quantize intra-block
68 : edgomez 1.7.2.2 */
69 : Isibaar 1.1
70 : edgomez 1.7.2.2 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 1.1 {
76 :     const uint32_t mult = multipliers[quant];
77 : edgomez 1.7 const uint16_t quant_m_2 = quant << 1;
78 : edgomez 1.7.2.2 int i;
79 : Isibaar 1.1
80 : edgomez 1.7 coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar);
81 : Isibaar 1.1
82 :     for (i = 1; i < 64; i++) {
83 :     int16_t acLevel = data[i];
84 :    
85 :     if (acLevel < 0) {
86 :     acLevel = -acLevel;
87 :     if (acLevel < quant_m_2) {
88 :     coeff[i] = 0;
89 :     continue;
90 :     }
91 : edgomez 1.7 acLevel = (acLevel * mult) >> SCALEBITS;
92 : Isibaar 1.1 coeff[i] = -acLevel;
93 :     } else {
94 :     if (acLevel < quant_m_2) {
95 :     coeff[i] = 0;
96 :     continue;
97 :     }
98 : edgomez 1.7 acLevel = (acLevel * mult) >> SCALEBITS;
99 : Isibaar 1.1 coeff[i] = acLevel;
100 :     }
101 :     }
102 : edgomez 1.7.2.2
103 : edgomez 1.7.2.3 return(0);
104 : Isibaar 1.1 }
105 :    
106 :    
107 :     /* quantize inter-block
108 : edgomez 1.7.2.2 */
109 : Isibaar 1.1
110 : edgomez 1.2 uint32_t
111 : edgomez 1.7.2.2 quant_h263_inter_c(int16_t * coeff,
112 :     const int16_t * data,
113 :     const uint32_t quant)
114 : Isibaar 1.1 {
115 :     const uint32_t mult = multipliers[quant];
116 : edgomez 1.7 const uint16_t quant_m_2 = quant << 1;
117 :     const uint16_t quant_d_2 = quant >> 1;
118 : edgomez 1.7.2.2 uint32_t sum = 0;
119 : Isibaar 1.1 uint32_t i;
120 :    
121 :     for (i = 0; i < 64; i++) {
122 :     int16_t acLevel = data[i];
123 : edgomez 1.2
124 : Isibaar 1.1 if (acLevel < 0) {
125 :     acLevel = (-acLevel) - quant_d_2;
126 :     if (acLevel < quant_m_2) {
127 :     coeff[i] = 0;
128 :     continue;
129 :     }
130 :    
131 : edgomez 1.7 acLevel = (acLevel * mult) >> SCALEBITS;
132 : edgomez 1.7.2.1 sum += acLevel; /* sum += |acLevel| */
133 : Isibaar 1.1 coeff[i] = -acLevel;
134 :     } else {
135 :     acLevel -= quant_d_2;
136 :     if (acLevel < quant_m_2) {
137 :     coeff[i] = 0;
138 :     continue;
139 :     }
140 : edgomez 1.7 acLevel = (acLevel * mult) >> SCALEBITS;
141 : Isibaar 1.1 sum += acLevel;
142 :     coeff[i] = acLevel;
143 :     }
144 :     }
145 :    
146 : edgomez 1.7.2.2 return(sum);
147 : Isibaar 1.1 }
148 :    
149 :    
150 :     /* dequantize intra-block & clamp to [-2048,2047]
151 : edgomez 1.7.2.2 */
152 : Isibaar 1.1
153 : edgomez 1.7.2.2 uint32_t
154 :     dequant_h263_intra_c(int16_t * data,
155 :     const int16_t * coeff,
156 :     const uint32_t quant,
157 :     const uint32_t dcscalar)
158 : Isibaar 1.1 {
159 :     const int32_t quant_m_2 = quant << 1;
160 :     const int32_t quant_add = (quant & 1 ? quant : quant - 1);
161 : edgomez 1.7.2.2 int i;
162 : Isibaar 1.1
163 : edgomez 1.7 data[0] = coeff[0] * dcscalar;
164 : edgomez 1.2 if (data[0] < -2048) {
165 : Isibaar 1.1 data[0] = -2048;
166 : edgomez 1.2 } else if (data[0] > 2047) {
167 : Isibaar 1.1 data[0] = 2047;
168 :     }
169 :    
170 :     for (i = 1; i < 64; i++) {
171 :     int32_t acLevel = coeff[i];
172 : edgomez 1.2
173 :     if (acLevel == 0) {
174 : Isibaar 1.1 data[i] = 0;
175 : edgomez 1.2 } else if (acLevel < 0) {
176 : Isibaar 1.1 acLevel = quant_m_2 * -acLevel + quant_add;
177 :     data[i] = (acLevel <= 2048 ? -acLevel : -2048);
178 : edgomez 1.7.2.2 } else {
179 : Isibaar 1.1 acLevel = quant_m_2 * acLevel + quant_add;
180 :     data[i] = (acLevel <= 2047 ? acLevel : 2047);
181 :     }
182 :     }
183 : edgomez 1.7.2.2
184 :     return(0);
185 : Isibaar 1.1 }
186 :    
187 :    
188 :    
189 :     /* dequantize inter-block & clamp to [-2048,2047]
190 : edgomez 1.7.2.2 */
191 : Isibaar 1.1
192 : edgomez 1.7.2.2 uint32_t
193 :     dequant_h263_inter_c(int16_t * data,
194 : edgomez 1.2 const int16_t * coeff,
195 :     const uint32_t quant)
196 : Isibaar 1.1 {
197 : edgomez 1.7 const uint16_t quant_m_2 = quant << 1;
198 :     const uint16_t quant_add = (quant & 1 ? quant : quant - 1);
199 : edgomez 1.7.2.2 int i;
200 : Isibaar 1.1
201 :     for (i = 0; i < 64; i++) {
202 :     int16_t acLevel = coeff[i];
203 : edgomez 1.2
204 :     if (acLevel == 0) {
205 : Isibaar 1.1 data[i] = 0;
206 : edgomez 1.2 } else if (acLevel < 0) {
207 : Isibaar 1.1 acLevel = acLevel * quant_m_2 - quant_add;
208 :     data[i] = (acLevel >= -2048 ? acLevel : -2048);
209 : edgomez 1.7.2.2 } else {
210 : Isibaar 1.1 acLevel = acLevel * quant_m_2 + quant_add;
211 :     data[i] = (acLevel <= 2047 ? acLevel : 2047);
212 :     }
213 :     }
214 : edgomez 1.7.2.2
215 :     return(0);
216 : Isibaar 1.1 }

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