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 |
* $Id: quant_mpeg.c,v 1.2 2004-03-22 22:36:24 edgomez Exp $ |
* $Id: quant_mpeg.c,v 1.4 2008-11-26 01:04:34 Isibaar Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
43 |
* Local data |
* Local data |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
|
#define VM18P 3 |
|
|
#define VM18Q 4 |
|
|
|
|
46 |
/* divide-by-multiply table |
/* divide-by-multiply table |
47 |
* needs 17 bit shift (16 causes slight errors when q > 19) */ |
* needs 17 bit shift (16 causes slight errors when q > 19) */ |
48 |
|
|
|
#define SCALEBITS 17 |
|
49 |
#define FIX(X) ((1UL << SCALEBITS) / (X) + 1) |
#define FIX(X) ((1UL << SCALEBITS) / (X) + 1) |
50 |
|
|
51 |
static const uint32_t multipliers[32] = |
static const uint32_t multipliers[32] = |
65 |
****************************************************************************/ |
****************************************************************************/ |
66 |
|
|
67 |
/* quantize intra-block |
/* quantize intra-block |
|
* |
|
|
* const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q); |
|
|
* level = DIV_DIV(16 * data[i], default_intra_matrix[i]); |
|
|
* coeff[i] = (level + quantd) / quant2; |
|
68 |
*/ |
*/ |
69 |
|
|
70 |
uint32_t |
uint32_t |
74 |
const uint32_t dcscalar, |
const uint32_t dcscalar, |
75 |
const uint16_t * mpeg_quant_matrices) |
const uint16_t * mpeg_quant_matrices) |
76 |
{ |
{ |
77 |
const uint32_t quantd = ((VM18P * quant) + (VM18Q / 2)) / VM18Q; |
const uint16_t * intra_matrix_rec = mpeg_quant_matrices + 1*64; |
|
const uint32_t mult = multipliers[quant]; |
|
|
const uint16_t *intra_matrix = get_intra_matrix(mpeg_quant_matrices); |
|
78 |
int i; |
int i; |
79 |
|
int rounding = 1<<(SCALEBITS-1-3); |
80 |
|
|
81 |
coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar); |
coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar); |
82 |
|
|
83 |
for (i = 1; i < 64; i++) { |
for (i = 1; i < 64; i++) { |
84 |
if (data[i] < 0) { |
int32_t level = data[i]; |
85 |
uint32_t level = -data[i]; |
level *= intra_matrix_rec[i]; |
86 |
|
level = (level + rounding)>>(SCALEBITS-3); |
|
level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i]; |
|
|
level = ((level + quantd) * mult) >> SCALEBITS; |
|
|
coeff[i] = -(int16_t) level; |
|
|
} else if (data[i] > 0) { |
|
|
uint32_t level = data[i]; |
|
|
|
|
|
level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i]; |
|
|
level = ((level + quantd) * mult) >> SCALEBITS; |
|
87 |
coeff[i] = level; |
coeff[i] = level; |
|
} else { |
|
|
coeff[i] = 0; |
|
|
} |
|
88 |
} |
} |
89 |
|
|
90 |
return(0); |
return(0); |