1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - quantization/dequantization - |
* - MPEG4 Quantization H263 implementation - |
5 |
* |
* |
6 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
7 |
* |
* |
8 |
* XviD is free software; you can redistribute it and/or modify it |
* This program is free software ; you can redistribute it and/or modify |
9 |
* 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 |
10 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
* (at your option) any later version. |
12 |
* |
* |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
* Under section 8 of the GNU General Public License, the copyright |
* $Id: quant_h263.c,v 1.7.2.2 2003-10-07 13:02:35 edgomez Exp $ |
|
* holders of XVID explicitly forbid distribution in the following |
|
|
* countries: |
|
|
* |
|
|
* - Japan |
|
|
* - United States of America |
|
|
* |
|
|
* Linking XviD statically or dynamically with other modules is making a |
|
|
* combined work based on XviD. Thus, the terms and conditions of the |
|
|
* GNU General Public License cover the whole combination. |
|
|
* |
|
|
* As a special exception, the copyright holders of XviD give you |
|
|
* permission to link XviD with independent modules that communicate with |
|
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
|
|
* license terms of these independent modules, and to copy and distribute |
|
|
* the resulting combined work under terms of your choice, provided that |
|
|
* every copy of the combined work is accompanied by a complete copy of |
|
|
* the source code of XviD (the version of XviD used to produce the |
|
|
* combined work), being distributed under the terms of the GNU General |
|
|
* Public License plus this exception. An independent module is a module |
|
|
* which is not derived from or based on XviD. |
|
|
* |
|
|
* Note that people who make modified versions of XviD are not obligated |
|
|
* to grant this special exception for their modified versions; it is |
|
|
* their choice whether to do so. The GNU General Public License gives |
|
|
* permission to release a modified version without this exception; this |
|
|
* exception also makes it possible to release a modified version which |
|
|
* carries forward this exception. |
|
23 |
* |
* |
24 |
* $Id: quant_h263.c,v 1.6 2002-12-15 01:21:12 edgomez Exp $ |
****************************************************************************/ |
|
* |
|
|
*************************************************************************/ |
|
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 = (uint16_t)(quant << 1); |
const uint16_t quant_m_2 = quant << 1; |
78 |
uint32_t i; |
uint32_t sum = 0; |
79 |
|
int i; |
80 |
|
|
81 |
coeff[0] = (int16_t)(DIV_DIV(data[0], (int32_t) dcscalar)); |
coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar); |
82 |
|
sum += coeff[0]; |
83 |
|
|
84 |
for (i = 1; i < 64; i++) { |
for (i = 1; i < 64; i++) { |
85 |
int16_t acLevel = data[i]; |
int16_t acLevel = data[i]; |
90 |
coeff[i] = 0; |
coeff[i] = 0; |
91 |
continue; |
continue; |
92 |
} |
} |
93 |
acLevel = (int16_t)(((uint32_t)acLevel * mult) >> SCALEBITS); |
acLevel = (acLevel * mult) >> SCALEBITS; |
94 |
|
sum += acLevel; |
95 |
coeff[i] = -acLevel; |
coeff[i] = -acLevel; |
96 |
} else { |
} else { |
97 |
if (acLevel < quant_m_2) { |
if (acLevel < quant_m_2) { |
98 |
coeff[i] = 0; |
coeff[i] = 0; |
99 |
continue; |
continue; |
100 |
} |
} |
101 |
acLevel = (int16_t)(((uint32_t)acLevel * mult) >> SCALEBITS); |
acLevel = (acLevel * mult) >> SCALEBITS; |
102 |
|
sum += acLevel; |
103 |
coeff[i] = acLevel; |
coeff[i] = acLevel; |
104 |
} |
} |
105 |
} |
} |
106 |
|
|
107 |
|
return(sum); |
108 |
} |
} |
109 |
|
|
110 |
|
|
112 |
*/ |
*/ |
113 |
|
|
114 |
uint32_t |
uint32_t |
115 |
quant_inter_c(int16_t * coeff, |
quant_h263_inter_c(int16_t * coeff, |
116 |
const int16_t * data, |
const int16_t * data, |
117 |
const uint32_t quant) |
const uint32_t quant) |
118 |
{ |
{ |
119 |
const uint32_t mult = multipliers[quant]; |
const uint32_t mult = multipliers[quant]; |
120 |
const uint16_t quant_m_2 = (uint16_t)(quant << 1); |
const uint16_t quant_m_2 = quant << 1; |
121 |
const uint16_t quant_d_2 = (uint16_t)(quant >> 1); |
const uint16_t quant_d_2 = quant >> 1; |
122 |
int sum = 0; |
uint32_t sum = 0; |
123 |
uint32_t i; |
uint32_t i; |
124 |
|
|
125 |
for (i = 0; i < 64; i++) { |
for (i = 0; i < 64; i++) { |
132 |
continue; |
continue; |
133 |
} |
} |
134 |
|
|
135 |
acLevel = (int16_t)(((uint32_t)acLevel * mult) >> SCALEBITS); |
acLevel = (acLevel * mult) >> SCALEBITS; |
136 |
sum += acLevel; /* sum += |acLevel| */ |
sum += acLevel; /* sum += |acLevel| */ |
137 |
coeff[i] = -acLevel; |
coeff[i] = -acLevel; |
138 |
} else { |
} else { |
141 |
coeff[i] = 0; |
coeff[i] = 0; |
142 |
continue; |
continue; |
143 |
} |
} |
144 |
acLevel = (int16_t)(((uint32_t)acLevel * mult) >> SCALEBITS); |
acLevel = (acLevel * mult) >> SCALEBITS; |
145 |
sum += acLevel; |
sum += acLevel; |
146 |
coeff[i] = acLevel; |
coeff[i] = acLevel; |
147 |
} |
} |
148 |
} |
} |
149 |
|
|
150 |
return sum; |
return(sum); |
151 |
} |
} |
152 |
|
|
153 |
|
|
154 |
/* dequantize intra-block & clamp to [-2048,2047] |
/* dequantize intra-block & clamp to [-2048,2047] |
155 |
*/ |
*/ |
156 |
|
|
157 |
void |
uint32_t |
158 |
dequant_intra_c(int16_t * data, |
dequant_h263_intra_c(int16_t * data, |
159 |
const int16_t * coeff, |
const int16_t * coeff, |
160 |
const uint32_t quant, |
const uint32_t quant, |
161 |
const uint32_t dcscalar) |
const uint32_t dcscalar) |
162 |
{ |
{ |
163 |
const int32_t quant_m_2 = quant << 1; |
const int32_t quant_m_2 = quant << 1; |
164 |
const int32_t quant_add = (quant & 1 ? quant : quant - 1); |
const int32_t quant_add = (quant & 1 ? quant : quant - 1); |
165 |
uint32_t i; |
int i; |
166 |
|
|
167 |
data[0] = (int16_t)(coeff[0] * dcscalar); |
data[0] = coeff[0] * dcscalar; |
168 |
if (data[0] < -2048) { |
if (data[0] < -2048) { |
169 |
data[0] = -2048; |
data[0] = -2048; |
170 |
} else if (data[0] > 2047) { |
} else if (data[0] > 2047) { |
171 |
data[0] = 2047; |
data[0] = 2047; |
172 |
} |
} |
173 |
|
|
|
|
|
174 |
for (i = 1; i < 64; i++) { |
for (i = 1; i < 64; i++) { |
175 |
int32_t acLevel = coeff[i]; |
int32_t acLevel = coeff[i]; |
176 |
|
|
179 |
} else if (acLevel < 0) { |
} else if (acLevel < 0) { |
180 |
acLevel = quant_m_2 * -acLevel + quant_add; |
acLevel = quant_m_2 * -acLevel + quant_add; |
181 |
data[i] = (acLevel <= 2048 ? -acLevel : -2048); |
data[i] = (acLevel <= 2048 ? -acLevel : -2048); |
182 |
} else /* if (acLevel > 0) { */ |
} else { |
|
{ |
|
183 |
acLevel = quant_m_2 * acLevel + quant_add; |
acLevel = quant_m_2 * acLevel + quant_add; |
184 |
data[i] = (acLevel <= 2047 ? acLevel : 2047); |
data[i] = (acLevel <= 2047 ? acLevel : 2047); |
185 |
} |
} |
186 |
} |
} |
187 |
|
|
188 |
|
return(0); |
189 |
} |
} |
190 |
|
|
191 |
|
|
193 |
/* dequantize inter-block & clamp to [-2048,2047] |
/* dequantize inter-block & clamp to [-2048,2047] |
194 |
*/ |
*/ |
195 |
|
|
196 |
void |
uint32_t |
197 |
dequant_inter_c(int16_t * data, |
dequant_h263_inter_c(int16_t * data, |
198 |
const int16_t * coeff, |
const int16_t * coeff, |
199 |
const uint32_t quant) |
const uint32_t quant) |
200 |
{ |
{ |
201 |
const uint16_t quant_m_2 = (uint16_t)(quant << 1); |
const uint16_t quant_m_2 = quant << 1; |
202 |
const uint16_t quant_add = (uint16_t)(quant & 1 ? quant : quant - 1); |
const uint16_t quant_add = (quant & 1 ? quant : quant - 1); |
203 |
uint32_t i; |
int i; |
204 |
|
|
205 |
for (i = 0; i < 64; i++) { |
for (i = 0; i < 64; i++) { |
206 |
int16_t acLevel = coeff[i]; |
int16_t acLevel = coeff[i]; |
210 |
} else if (acLevel < 0) { |
} else if (acLevel < 0) { |
211 |
acLevel = acLevel * quant_m_2 - quant_add; |
acLevel = acLevel * quant_m_2 - quant_add; |
212 |
data[i] = (acLevel >= -2048 ? acLevel : -2048); |
data[i] = (acLevel >= -2048 ? acLevel : -2048); |
213 |
} else /* if (acLevel > 0) */ |
} else { |
|
{ |
|
214 |
acLevel = acLevel * quant_m_2 + quant_add; |
acLevel = acLevel * quant_m_2 + quant_add; |
215 |
data[i] = (acLevel <= 2047 ? acLevel : 2047); |
data[i] = (acLevel <= 2047 ? acLevel : 2047); |
216 |
} |
} |
217 |
} |
} |
218 |
|
|
219 |
|
return(0); |
220 |
} |
} |