[cvs] / xvidcore / src / quant / quant_mpeg4.c Repository:
ViewVC logotype

Diff of /xvidcore/src/quant/quant_mpeg4.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2, Fri Mar 8 19:16:41 2002 UTC revision 1.5, Sat Sep 21 03:07:56 2002 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *    XVID MPEG-4 VIDEO CODEC   *    XVID MPEG-4 VIDEO CODEC
4   *    mpeg-4 quantization/dequantization   *  - Mpeg4 quantization/dequantization functions -
5     *
6     *  Copyright(C) 2002 Peter Ross <pross@xvid.org>
7   *   *
8   *    This program is an implementation of a part of one or more MPEG-4   *    This program is an implementation of a part of one or more MPEG-4
9   *    Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *    Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
# Line 24  Line 26 
26   *   *
27   *    You should have received a copy of the GNU General Public License   *    You should have received a copy of the GNU General Public License
28   *    along with this program; if not, write to the Free Software   *    along with this program; if not, write to the Free Software
29   *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  *  
  *************************************************************************/  
   
 /**************************************************************************  
30   *   *
31   *    History:   *  $Id$
32   *   *
33   *      26.01.2002    fixed  quant4_intra dcscalar signed/unsigned error   ****************************************************************************/
  *  20.01.2002    increased accuracy of >> divide  
  *  26.12.2001    divide-by-multiplication optimization  
  *  22.12.2001    [-127,127] clamping removed; minor tweaks  
  *      19.11.2001    inital version <pross@cs.rmit.edu.au>  
  *  
  *************************************************************************/  
   
34    
35  #include "quant_mpeg4.h"  #include "quant_mpeg4.h"
36  #include "quant_matrix.h"  #include "quant_matrix.h"
37    
38  // function pointers  /*****************************************************************************
39     * Function pointers
40     ****************************************************************************/
41    
42  quant_intraFuncPtr quant4_intra;  quant_intraFuncPtr quant4_intra;
43  quant_intraFuncPtr dequant4_intra;  quant_intraFuncPtr dequant4_intra;
44  dequant_interFuncPtr dequant4_inter;  dequant_interFuncPtr dequant4_inter;
45  quant_interFuncPtr quant4_inter;  quant_interFuncPtr quant4_inter;
46    
47    
48    /*****************************************************************************
49     * Local data
50     ****************************************************************************/
51    
52  #define DIV_DIV(A,B)    ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) )  #define DIV_DIV(A,B)    ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) )
53  #define SIGN(A)  ((A)>0?1:-1)  #define SIGN(A)  ((A)>0?1:-1)
54  #define VM18P    3  #define VM18P    3
55  #define VM18Q    4  #define VM18Q    4
56    
57    
58  // divide-by-multiply table  /*
59  // need 17 bit shift (16 causes slight errors when q > 19)   * divide-by-multiply table
60     * need 17 bit shift (16 causes slight errors when q > 19)
61     */
62    
63  #define SCALEBITS    17  #define SCALEBITS    17
64  #define FIX(X)        ((1UL << SCALEBITS) / (X) + 1)  #define FIX(X)        ((1UL << SCALEBITS) / (X) + 1)
65    
66  static const uint32_t multipliers[32] =  static const uint32_t multipliers[32] = {
 {  
67      0,          FIX(2),     FIX(4),     FIX(6),      0,          FIX(2),     FIX(4),     FIX(6),
68      FIX(8),     FIX(10),    FIX(12),    FIX(14),      FIX(8),     FIX(10),    FIX(12),    FIX(14),
69      FIX(16),    FIX(18),    FIX(20),    FIX(22),      FIX(16),    FIX(18),    FIX(20),    FIX(22),
# Line 75  Line 74 
74      FIX(56),    FIX(58),    FIX(60),    FIX(62)      FIX(56),    FIX(58),    FIX(60),    FIX(62)
75  };  };
76    
77    /*****************************************************************************
78     * Functions
79     ****************************************************************************/
80    
81  /*    quantize intra-block  /*    quantize intra-block
82    
83      // const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q);      // const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q);
# Line 83  Line 86 
86      // coeff[i] = (level + quantd) / quant2;      // coeff[i] = (level + quantd) / quant2;
87  */  */
88    
89  void quant4_intra_c(int16_t * coeff, const int16_t * data, const uint32_t quant, const uint32_t dcscalar)  void
90    quant4_intra_c(int16_t * coeff,
91                               const int16_t * data,
92                               const uint32_t quant,
93                               const uint32_t dcscalar)
94  {  {
95      const uint32_t quantd = ((VM18P*quant) + (VM18Q/2)) / VM18Q;      const uint32_t quantd = ((VM18P*quant) + (VM18Q/2)) / VM18Q;
96      const uint32_t mult = multipliers[quant];      const uint32_t mult = multipliers[quant];
# Line 94  Line 101 
101    
102      coeff[0] = DIV_DIV(data[0], (int32_t)dcscalar);      coeff[0] = DIV_DIV(data[0], (int32_t)dcscalar);
103    
104      for (i = 1; i < 64; i++)          for (i = 1; i < 64; i++) {
105      {                  if (data[i] < 0) {
         if (data[i] < 0)  
         {  
106              uint32_t level = -data[i];              uint32_t level = -data[i];
107    
108              level = ((level<<4) + (intra_matrix[i]>>1)) / intra_matrix[i];              level = ((level<<4) + (intra_matrix[i]>>1)) / intra_matrix[i];
109              level = ((level + quantd) * mult) >> 17;              level = ((level + quantd) * mult) >> 17;
110              coeff[i] = -(int16_t)level;              coeff[i] = -(int16_t)level;
111          }                  } else if (data[i] > 0) {
         else if (data[i] > 0)  
         {  
112              uint32_t level = data[i];              uint32_t level = data[i];
113    
114              level = ((level<<4) + (intra_matrix[i]>>1)) / intra_matrix[i];              level = ((level<<4) + (intra_matrix[i]>>1)) / intra_matrix[i];
115              level = ((level + quantd) * mult) >> 17;              level = ((level + quantd) * mult) >> 17;
116              coeff[i] = level;              coeff[i] = level;
117          }                  } else {
         else  
         {  
118              coeff[i] = 0;              coeff[i] = 0;
119          }          }
120      }      }
# Line 123  Line 126 
126      // data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4;      // data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4;
127  */  */
128    
129  void dequant4_intra_c(int16_t *data, const int16_t *coeff, const uint32_t quant, const uint32_t dcscalar)  void
130    dequant4_intra_c(int16_t * data,
131                                     const int16_t * coeff,
132                                     const uint32_t quant,
133                                     const uint32_t dcscalar)
134  {  {
135      uint32_t i;      uint32_t i;
136          int16_t *intra_matrix;          int16_t *intra_matrix;
# Line 131  Line 138 
138          intra_matrix = get_intra_matrix();          intra_matrix = get_intra_matrix();
139    
140      data[0] = coeff[0]  * dcscalar;      data[0] = coeff[0]  * dcscalar;
141      if (data[0] < -2048)          if (data[0] < -2048) {
     {  
142          data[0] = -2048;          data[0] = -2048;
143      }          } else if (data[0] > 2047) {
     else if (data[0] > 2047)  
     {  
144          data[0] = 2047;          data[0] = 2047;
145      }      }
146    
147      for (i = 1; i < 64; i++)          for (i = 1; i < 64; i++) {
148      {                  if (coeff[i] == 0) {
         if (coeff[i] == 0)  
         {  
149              data[i] = 0;              data[i] = 0;
150          }                  } else if (coeff[i] < 0) {
         else if (coeff[i] < 0)  
         {  
151              uint32_t level = -coeff[i];              uint32_t level = -coeff[i];
152    
153              level = (level * intra_matrix[i] * quant) >> 3;              level = (level * intra_matrix[i] * quant) >> 3;
154              data[i] = (level <= 2048 ? -(int16_t)level : -2048);              data[i] = (level <= 2048 ? -(int16_t)level : -2048);
155          }                  } else                                  // if (coeff[i] > 0)
         else // if (coeff[i] > 0)  
156          {          {
157              uint32_t level = coeff[i];              uint32_t level = coeff[i];
158    
159              level = (level * intra_matrix[i] * quant) >> 3;              level = (level * intra_matrix[i] * quant) >> 3;
160              data[i] = (level <= 2047 ? level : 2047);              data[i] = (level <= 2047 ? level : 2047);
161          }          }
# Line 170  Line 171 
171      // sum += abs(level);      // sum += abs(level);
172  */  */
173    
174  uint32_t quant4_inter_c(int16_t * coeff, const int16_t * data, const uint32_t quant)  uint32_t
175    quant4_inter_c(int16_t * coeff,
176                               const int16_t * data,
177                               const uint32_t quant)
178  {  {
179      const uint32_t mult = multipliers[quant];      const uint32_t mult = multipliers[quant];
180      uint32_t sum = 0;      uint32_t sum = 0;
# Line 179  Line 183 
183    
184          inter_matrix = get_inter_matrix();          inter_matrix = get_inter_matrix();
185    
186      for (i = 0; i < 64; i++)          for (i = 0; i < 64; i++) {
187      {                  if (data[i] < 0) {
         if (data[i] < 0)  
         {  
188              uint32_t level = -data[i];              uint32_t level = -data[i];
189    
190              level = ((level<<4) + (inter_matrix[i]>>1)) / inter_matrix[i];              level = ((level<<4) + (inter_matrix[i]>>1)) / inter_matrix[i];
191              level = (level * mult) >> 17;              level = (level * mult) >> 17;
192              sum += level;              sum += level;
193              coeff[i] = -(int16_t)level;              coeff[i] = -(int16_t)level;
194          }                  } else if (data[i] > 0) {
         else if (data[i] > 0)  
         {  
195              uint32_t level = data[i];              uint32_t level = data[i];
196    
197              level = ((level<<4) + (inter_matrix[i]>>1)) / inter_matrix[i];              level = ((level<<4) + (inter_matrix[i]>>1)) / inter_matrix[i];
198              level = (level * mult) >> 17;              level = (level * mult) >> 17;
199              sum += level;              sum += level;
200              coeff[i] = level;              coeff[i] = level;
201          }                  } else {
         else  
         {  
202              coeff[i] = 0;              coeff[i] = 0;
203          }          }
204      }      }
# Line 211  Line 211 
211    data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16    data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16
212  */  */
213    
214  void dequant4_inter_c(int16_t *data, const int16_t *coeff, const uint32_t quant)  void
215    dequant4_inter_c(int16_t * data,
216                                     const int16_t * coeff,
217                                     const uint32_t quant)
218  {  {
219      uint32_t sum = 0;      uint32_t sum = 0;
220      uint32_t i;      uint32_t i;
# Line 219  Line 222 
222    
223          inter_matrix = get_inter_matrix();          inter_matrix = get_inter_matrix();
224    
225      for (i = 0; i < 64; i++)          for (i = 0; i < 64; i++) {
226      {                  if (coeff[i] == 0) {
         if (coeff[i] == 0)  
         {  
227              data[i] = 0;              data[i] = 0;
228          }                  } else if (coeff[i] < 0) {
         else if (coeff[i] < 0)  
         {  
229              int32_t level = -coeff[i];              int32_t level = -coeff[i];
230    
231              level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;              level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;
232              data[i] = (level <= 2048 ? -level : -2048);              data[i] = (level <= 2048 ? -level : -2048);
233          }                  } else                                  // if (coeff[i] > 0)
         else // if (coeff[i] > 0)  
234          {          {
235              uint32_t level = coeff[i];              uint32_t level = coeff[i];
236    
237              level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;              level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;
238              data[i] = (level <= 2047 ? level : 2047);              data[i] = (level <= 2047 ? level : 2047);
239          }          }
# Line 243  Line 243 
243    
244      // mismatch control      // mismatch control
245    
246      if ((sum & 1) == 0)          if ((sum & 1) == 0) {
     {  
247          data[63] ^= 1;          data[63] ^= 1;
248      }      }
249  }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.5

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4