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

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

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

revision 3, Fri Mar 8 02:46:11 2002 UTC revision 499, Sat Sep 21 03:11:36 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
  *  
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *    History:  
30   *   *
31   *      26.01.2002    fixed  quant4_intra dcscalar signed/unsigned error   *  $Id: quant_mpeg4.c,v 1.5 2002-09-21 03:07:56 suxen_drol Exp $
  *  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>  
32   *   *
33   *************************************************************************/   ****************************************************************************/
   
34    
35  #include "quant_mpeg4.h"  #include "quant_mpeg4.h"
36    #include "quant_matrix.h"
37    
38    /*****************************************************************************
39     * Function pointers
40     ****************************************************************************/
41    
   
 // function pointers  
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 76  Line 74 
74      FIX(56),    FIX(58),    FIX(60),    FIX(62)      FIX(56),    FIX(58),    FIX(60),    FIX(62)
75  };  };
76    
77    /*****************************************************************************
78  static const int16_t default_intra_matrix[64] = {   * Functions
79       8,17,18,19,21,23,25,27,   ****************************************************************************/
     17,18,19,21,23,25,27,28,  
     20,21,22,23,24,26,28,30,  
     21,22,23,24,26,28,30,32,  
     22,23,24,26,28,30,32,35,  
     23,24,26,28,30,32,35,38,  
     25,26,28,30,32,35,38,41,  
     27,28,30,32,35,38,41,45  
 };  
   
 static const int16_t default_inter_matrix[64] = {  
     16,17,18,19,20,21,22,23,  
     17,18,19,20,21,22,23,24,  
     18,19,20,21,22,23,24,25,  
     19,20,21,22,23,24,26,27,  
     20,21,22,23,25,26,27,28,  
     21,22,23,24,26,27,28,30,  
     22,23,24,26,27,28,30,31,  
     23,24,25,27,28,30,31,33  
 };  
   
80    
81  /*    quantize intra-block  /*    quantize intra-block
82    
# Line 108  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];
97      uint32_t i;      uint32_t i;
98            int16_t *intra_matrix;
99    
100            intra_matrix = get_intra_matrix();
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              level = ((level<<4) + (default_intra_matrix[i]>>1)) / default_intra_matrix[i];  
108                            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              level = ((level<<4) + (default_intra_matrix[i]>>1)) / default_intra_matrix[i];  
114                            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 145  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;
137    
138            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              level = (level * default_intra_matrix[i] * quant) >> 3;  
153                            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              level = (level * default_intra_matrix[i] * quant) >> 3;  
159                            level = (level * intra_matrix[i] * quant) >> 3;
160              data[i] = (level <= 2047 ? level : 2047);              data[i] = (level <= 2047 ? level : 2047);
161          }          }
162      }      }
# Line 189  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;
181      uint32_t i;      uint32_t i;
182            int16_t *inter_matrix;
183    
184      for (i = 0; i < 64; i++)          inter_matrix = get_inter_matrix();
185      {  
186          if (data[i] < 0)          for (i = 0; i < 64; i++) {
187          {                  if (data[i] < 0) {
188              uint32_t level = -data[i];              uint32_t level = -data[i];
189              level = ((level<<4) + (default_inter_matrix[i]>>1)) / default_inter_matrix[i];  
190                            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              level = ((level<<4) + (default_inter_matrix[i]>>1)) / default_inter_matrix[i];  
197                            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 227  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;
221            int16_t *inter_matrix;
222    
223      for (i = 0; i < 64; i++)          inter_matrix = get_inter_matrix();
224      {  
225          if (coeff[i] == 0)          for (i = 0; i < 64; i++) {
226          {                  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              level = ((2 * level + 1) * default_inter_matrix[i] * quant) >> 4;  
231                            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              level = ((2 * level + 1) * default_inter_matrix[i] * quant) >> 4;  
237                            level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;
238              data[i] = (level <= 2047 ? level : 2047);              data[i] = (level <= 2047 ? level : 2047);
239          }          }
240    
# Line 256  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.3  
changed lines
  Added in v.499

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