1
/* quantize.c, quantization / inverse quantization */
3
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6
* Disclaimer of Warranty
8
* These software programs are available to the user without any license fee or
9
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10
* any and all warranties, whether express, implied, or statuary, including any
11
* implied warranties or merchantability or of fitness for a particular
12
* purpose. In no event shall the copyright-holder be liable for any
13
* incidental, punitive, or consequential damages of any kind whatsoever
14
* arising from the use of these programs.
16
* This disclaimer of warranty extends to the user of these programs and user's
17
* customers, employees, agents, transferees, successors, and assigns.
19
* The MPEG Software Simulation Group does not represent or warrant that the
20
* programs furnished hereunder are free of infringement of any third-party
23
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24
* are subject to royalty fees to patent holders. Many of these patents are
25
* general enough such that they are unavoidable regardless of implementation
31
#include "mpeg2enc_config.h"
32
#include "mpeg2enc_global.h"
34
static void iquant1_intra _ANSI_ARGS_((short *src, short *dst,
35
int dc_prec, unsigned char *quant_mat, int mquant));
36
static void iquant1_non_intra _ANSI_ARGS_((short *src, short *dst,
37
unsigned char *quant_mat, int mquant));
39
/* Test Model 5 quantization
41
* this quantizer has a bias of 1/8 stepsize towards zero
42
* (except for the DC coefficient)
44
int MPEG2_quant_intra(src,dst,dc_prec,quant_mat,mquant,mpeg2_struct)
47
unsigned char *quant_mat;
49
struct MPEG2_structure *mpeg2_struct;
55
d = 8>>dc_prec; /* intra_dc_mult */
56
dst[0] = (x>=0) ? (x+(d>>1))/d : -((-x+(d>>1))/d); /* round(x/d) */
62
y = (32*(x>=0 ? x : -x) + (d>>1))/d; /* round(32*x/quant_mat) */
64
y = (y+d)/(2*mquant); /* (y+0.75*mquant) / (2*mquant) */
66
/* clip to syntax limits */
69
if (mpeg2_struct->mpeg1)
75
dst[i] = (x>=0) ? y : -y;
78
/* this quantizer is virtually identical to the above */
81
d = mquant*quant_mat[i];
82
y = (16*x + ((3*d)>>3)) / d;
83
dst[i] = (src[i]<0) ? -y : y;
90
int MPEG2_quant_non_intra(src,dst,quant_mat,mquant,mpeg2_struct)
92
unsigned char *quant_mat;
94
struct MPEG2_structure *mpeg2_struct;
106
y = (32*(x>=0 ? x : -x) + (d>>1))/d; /* round(32*x/quant_mat) */
109
/* clip to syntax limits */
112
if (mpeg2_struct->mpeg1)
118
if ((dst[i] = (x>=0 ? y : -y)) != 0)
125
/* MPEG-2 inverse quantization */
126
void MPEG2_iquant_intra(src,dst,dc_prec,quant_mat,mquant,mpeg2_struct)
129
unsigned char *quant_mat;
131
struct MPEG2_structure *mpeg2_struct;
135
if (mpeg2_struct->mpeg1)
136
iquant1_intra(src,dst,dc_prec,quant_mat,mquant);
139
sum = dst[0] = src[0] << (3-dc_prec);
142
val = (int)(src[i]*quant_mat[i]*mquant)/16;
143
sum+= dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
146
/* mismatch control */
152
void MPEG2_iquant_non_intra(src,dst,quant_mat,mquant,mpeg2_struct)
154
unsigned char *quant_mat;
156
struct MPEG2_structure *mpeg2_struct;
160
if (mpeg2_struct->mpeg1)
161
iquant1_non_intra(src,dst,quant_mat,mquant);
169
val = (int)((2*val+(val>0 ? 1 : -1))*quant_mat[i]*mquant)/32;
170
sum+= dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
173
/* mismatch control */
179
/* MPEG-1 inverse quantization */
180
static void iquant1_intra(src,dst,dc_prec,quant_mat,mquant)
183
unsigned char *quant_mat;
188
dst[0] = src[0] << (3-dc_prec);
191
val = (int)(src[i]*quant_mat[i]*mquant)/16;
193
/* mismatch control */
194
if ((val&1)==0 && val!=0)
195
val+= (val>0) ? -1 : 1;
198
dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
202
static void iquant1_non_intra(src,dst,quant_mat,mquant)
204
unsigned char *quant_mat;
214
val = (int)((2*val+(val>0 ? 1 : -1))*quant_mat[i]*mquant)/32;
216
/* mismatch control */
217
if ((val&1)==0 && val!=0)
218
val+= (val>0) ? -1 : 1;
222
dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);