1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* quantization/dequantization |
* - MPEG4 Quantization H263 implementation - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
|
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
|
|
* to use this software module in hardware or software products are |
|
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
7 |
* |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
* 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 |
* it under the terms of the GNU General Public License as published by |
17 |
* |
* |
18 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
*************************************************************************/ |
* $Id: quant_h263.c,v 1.7.2.3 2003-10-09 18:50:22 edgomez Exp $ |
|
|
|
|
/************************************************************************** |
|
|
* |
|
|
* History: |
|
23 |
* |
* |
24 |
* 26.12.2001 dequant_inter bug fix |
****************************************************************************/ |
|
* 22.12.2001 clamp dequant output to [-2048,2047] |
|
|
* 19.11.2001 quant_inter now returns sum of abs. coefficient values |
|
|
* 02.11.2001 added const to function args <pross@cs.rmit.edu.au> |
|
|
* 28.10.2001 total rewrite <pross@cs.rmit.edu.au> |
|
|
* |
|
|
*************************************************************************/ |
|
25 |
|
|
26 |
#include "quant_h263.h" |
#include "../global.h" |
27 |
|
#include "quant.h" |
28 |
|
|
29 |
/* mutliply+shift division table |
/***************************************************************************** |
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 |
|
|
48 |
#define SCALEBITS 16 |
#define SCALEBITS 16 |
49 |
#define FIX(X) ((1L << SCALEBITS) / (X) + 1) |
#define FIX(X) ((1L << SCALEBITS) / (X) + 1) |
50 |
|
|
51 |
static const uint32_t multipliers[32] = { |
static const uint32_t multipliers[32] = |
52 |
|
{ |
53 |
0, FIX(2), FIX(4), FIX(6), |
0, FIX(2), FIX(4), FIX(6), |
54 |
FIX(8), FIX(10), FIX(12), FIX(14), |
FIX(8), FIX(10), FIX(12), FIX(14), |
55 |
FIX(16), FIX(18), FIX(20), FIX(22), |
FIX(16), FIX(18), FIX(20), FIX(22), |
60 |
FIX(56), FIX(58), FIX(60), FIX(62) |
FIX(56), FIX(58), FIX(60), FIX(62) |
61 |
}; |
}; |
62 |
|
|
63 |
|
/***************************************************************************** |
64 |
|
* Function definitions |
65 |
#define DIV_DIV(a, b) ((a)>0) ? ((a)+((b)>>1))/(b) : ((a)-((b)>>1))/(b) |
****************************************************************************/ |
|
|
|
|
// function pointers |
|
|
quanth263_intraFuncPtr quant_intra; |
|
|
quanth263_intraFuncPtr dequant_intra; |
|
|
|
|
|
quanth263_interFuncPtr quant_inter; |
|
|
dequanth263_interFuncPtr dequant_inter; |
|
|
|
|
|
|
|
66 |
|
|
67 |
/* quantize intra-block |
/* quantize intra-block |
68 |
*/ |
*/ |
69 |
|
|
70 |
|
uint32_t |
71 |
void |
quant_h263_intra_c(int16_t * coeff, |
|
quant_intra_c(int16_t * coeff, |
|
72 |
const int16_t * data, |
const int16_t * data, |
73 |
const uint32_t quant, |
const uint32_t quant, |
74 |
const uint32_t dcscalar) |
const uint32_t dcscalar) |
75 |
{ |
{ |
76 |
const uint32_t mult = multipliers[quant]; |
const uint32_t mult = multipliers[quant]; |
77 |
const uint16_t quant_m_2 = quant << 1; |
const uint16_t quant_m_2 = quant << 1; |
78 |
uint32_t i; |
int i; |
79 |
|
|
80 |
coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar); |
coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar); |
81 |
|
|
99 |
coeff[i] = acLevel; |
coeff[i] = acLevel; |
100 |
} |
} |
101 |
} |
} |
102 |
|
|
103 |
|
return(0); |
104 |
} |
} |
105 |
|
|
106 |
|
|
108 |
*/ |
*/ |
109 |
|
|
110 |
uint32_t |
uint32_t |
111 |
quant_inter_c(int16_t * coeff, |
quant_h263_inter_c(int16_t * coeff, |
112 |
const int16_t * data, |
const int16_t * data, |
113 |
const uint32_t quant) |
const uint32_t quant) |
114 |
{ |
{ |
115 |
const uint32_t mult = multipliers[quant]; |
const uint32_t mult = multipliers[quant]; |
116 |
const uint16_t quant_m_2 = quant << 1; |
const uint16_t quant_m_2 = quant << 1; |
117 |
const uint16_t quant_d_2 = quant >> 1; |
const uint16_t quant_d_2 = quant >> 1; |
118 |
int sum = 0; |
uint32_t sum = 0; |
119 |
uint32_t i; |
uint32_t i; |
120 |
|
|
121 |
for (i = 0; i < 64; i++) { |
for (i = 0; i < 64; i++) { |
129 |
} |
} |
130 |
|
|
131 |
acLevel = (acLevel * mult) >> SCALEBITS; |
acLevel = (acLevel * mult) >> SCALEBITS; |
132 |
sum += acLevel; // sum += |acLevel| |
sum += acLevel; /* sum += |acLevel| */ |
133 |
coeff[i] = -acLevel; |
coeff[i] = -acLevel; |
134 |
} else { |
} else { |
135 |
acLevel -= quant_d_2; |
acLevel -= quant_d_2; |
143 |
} |
} |
144 |
} |
} |
145 |
|
|
146 |
return sum; |
return(sum); |
147 |
} |
} |
148 |
|
|
149 |
|
|
150 |
/* dequantize intra-block & clamp to [-2048,2047] |
/* dequantize intra-block & clamp to [-2048,2047] |
151 |
*/ |
*/ |
152 |
|
|
153 |
void |
uint32_t |
154 |
dequant_intra_c(int16_t * data, |
dequant_h263_intra_c(int16_t * data, |
155 |
const int16_t * coeff, |
const int16_t * coeff, |
156 |
const uint32_t quant, |
const uint32_t quant, |
157 |
const uint32_t dcscalar) |
const uint32_t dcscalar) |
158 |
{ |
{ |
159 |
const int32_t quant_m_2 = quant << 1; |
const int32_t quant_m_2 = quant << 1; |
160 |
const int32_t quant_add = (quant & 1 ? quant : quant - 1); |
const int32_t quant_add = (quant & 1 ? quant : quant - 1); |
161 |
uint32_t i; |
int i; |
162 |
|
|
163 |
data[0] = coeff[0] * dcscalar; |
data[0] = coeff[0] * dcscalar; |
164 |
if (data[0] < -2048) { |
if (data[0] < -2048) { |
167 |
data[0] = 2047; |
data[0] = 2047; |
168 |
} |
} |
169 |
|
|
|
|
|
170 |
for (i = 1; i < 64; i++) { |
for (i = 1; i < 64; i++) { |
171 |
int32_t acLevel = coeff[i]; |
int32_t acLevel = coeff[i]; |
172 |
|
|
175 |
} else if (acLevel < 0) { |
} else if (acLevel < 0) { |
176 |
acLevel = quant_m_2 * -acLevel + quant_add; |
acLevel = quant_m_2 * -acLevel + quant_add; |
177 |
data[i] = (acLevel <= 2048 ? -acLevel : -2048); |
data[i] = (acLevel <= 2048 ? -acLevel : -2048); |
178 |
} else // if (acLevel > 0) { |
} else { |
|
{ |
|
179 |
acLevel = quant_m_2 * acLevel + quant_add; |
acLevel = quant_m_2 * acLevel + quant_add; |
180 |
data[i] = (acLevel <= 2047 ? acLevel : 2047); |
data[i] = (acLevel <= 2047 ? acLevel : 2047); |
181 |
} |
} |
182 |
} |
} |
183 |
|
|
184 |
|
return(0); |
185 |
} |
} |
186 |
|
|
187 |
|
|
189 |
/* dequantize inter-block & clamp to [-2048,2047] |
/* dequantize inter-block & clamp to [-2048,2047] |
190 |
*/ |
*/ |
191 |
|
|
192 |
void |
uint32_t |
193 |
dequant_inter_c(int16_t * data, |
dequant_h263_inter_c(int16_t * data, |
194 |
const int16_t * coeff, |
const int16_t * coeff, |
195 |
const uint32_t quant) |
const uint32_t quant) |
196 |
{ |
{ |
197 |
const uint16_t quant_m_2 = quant << 1; |
const uint16_t quant_m_2 = quant << 1; |
198 |
const uint16_t quant_add = (quant & 1 ? quant : quant - 1); |
const uint16_t quant_add = (quant & 1 ? quant : quant - 1); |
199 |
uint32_t i; |
int i; |
200 |
|
|
201 |
for (i = 0; i < 64; i++) { |
for (i = 0; i < 64; i++) { |
202 |
int16_t acLevel = coeff[i]; |
int16_t acLevel = coeff[i]; |
206 |
} else if (acLevel < 0) { |
} else if (acLevel < 0) { |
207 |
acLevel = acLevel * quant_m_2 - quant_add; |
acLevel = acLevel * quant_m_2 - quant_add; |
208 |
data[i] = (acLevel >= -2048 ? acLevel : -2048); |
data[i] = (acLevel >= -2048 ? acLevel : -2048); |
209 |
} else // if (acLevel > 0) |
} else { |
|
{ |
|
210 |
acLevel = acLevel * quant_m_2 + quant_add; |
acLevel = acLevel * quant_m_2 + quant_add; |
211 |
data[i] = (acLevel <= 2047 ? acLevel : 2047); |
data[i] = (acLevel <= 2047 ? acLevel : 2047); |
212 |
} |
} |
213 |
} |
} |
214 |
|
|
215 |
|
return(0); |
216 |
} |
} |