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
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 quant_intra(src,dst,dc_prec,quant_mat,mquant)
47 unsigned char *quant_mat;
54 d = 8>>dc_prec; /* intra_dc_mult */
55 dst[0] = (x>=0) ? (x+(d>>1))/d : -((-x+(d>>1))/d); /* round(x/d) */
61 y = (32*(x>=0 ? x : -x) + (d>>1))/d; /* round(32*x/quant_mat) */
63 y = (y+d)/(2*mquant); /* (y+0.75*mquant) / (2*mquant) */
65 /* clip to syntax limits */
74 dst[i] = (x>=0) ? y : -y;
77 /* this quantizer is virtually identical to the above */
80 d = mquant*quant_mat[i];
81 y = (16*x + ((3*d)>>3)) / d;
82 dst[i] = (src[i]<0) ? -y : y;
89 int quant_non_intra(src,dst,quant_mat,mquant)
91 unsigned char *quant_mat;
104 y = (32*(x>=0 ? x : -x) + (d>>1))/d; /* round(32*x/quant_mat) */
107 /* clip to syntax limits */
116 if ((dst[i] = (x>=0 ? y : -y)) != 0)
123 /* MPEG-2 inverse quantization */
124 void iquant_intra(src,dst,dc_prec,quant_mat,mquant)
127 unsigned char *quant_mat;
133 iquant1_intra(src,dst,dc_prec,quant_mat,mquant);
136 sum = dst[0] = src[0] << (3-dc_prec);
139 val = (int)(src[i]*quant_mat[i]*mquant)/16;
140 sum+= dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
143 /* mismatch control */
149 void iquant_non_intra(src,dst,quant_mat,mquant)
151 unsigned char *quant_mat;
157 iquant1_non_intra(src,dst,quant_mat,mquant);
165 val = (int)((2*val+(val>0 ? 1 : -1))*quant_mat[i]*mquant)/32;
166 sum+= dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
169 /* mismatch control */
175 /* MPEG-1 inverse quantization */
176 static void iquant1_intra(src,dst,dc_prec,quant_mat,mquant)
179 unsigned char *quant_mat;
184 dst[0] = src[0] << (3-dc_prec);
187 val = (int)(src[i]*quant_mat[i]*mquant)/16;
189 /* mismatch control */
190 if ((val&1)==0 && val!=0)
191 val+= (val>0) ? -1 : 1;
194 dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);
198 static void iquant1_non_intra(src,dst,quant_mat,mquant)
200 unsigned char *quant_mat;
210 val = (int)((2*val+(val>0 ? 1 : -1))*quant_mat[i]*mquant)/32;
212 /* mismatch control */
213 if ((val&1)==0 && val!=0)
214 val+= (val>0) ? -1 : 1;
218 dst[i] = (val>2047) ? 2047 : ((val<-2048) ? -2048 : val);