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

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

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

revision 1.1, Tue Oct 7 13:02:35 2003 UTC revision 1.2, Mon Mar 22 22:36:24 2004 UTC
# Line 0  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  - MPEG4 Quantization related header  -
5     *
6     *  Copyright(C) 2001-2003 Peter Ross <pross@xvid.org>
7     *
8     *  This program is free software ; you can redistribute it and/or modify
9     *  it under the terms of the GNU General Public License as published by
10     *  the Free Software Foundation ; either version 2 of the License, or
11     *  (at your option) any later version.
12     *
13     *  This program is distributed in the hope that it will be useful,
14     *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
15     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     *  GNU General Public License for more details.
17     *
18     *  You should have received a copy of the GNU General Public License
19     *  along with this program ; if not, write to the Free Software
20     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21     *
22     * $Id$
23     *
24     ****************************************************************************/
25    
26    #include "../global.h"
27    #include "quant.h"
28    #include "quant_matrix.h"
29    
30    /*****************************************************************************
31     * Global function pointers
32     ****************************************************************************/
33    
34    /* Quant */
35    quant_intraFuncPtr quant_mpeg_intra;
36    quant_interFuncPtr quant_mpeg_inter;
37    
38    /* DeQuant */
39    quant_intraFuncPtr dequant_mpeg_intra;
40    quant_interFuncPtr dequant_mpeg_inter;
41    
42    /*****************************************************************************
43     * Local data
44     ****************************************************************************/
45    
46    #define VM18P 3
47    #define VM18Q 4
48    
49    /* divide-by-multiply table
50     * needs 17 bit shift (16 causes slight errors when q > 19) */
51    
52    #define SCALEBITS 17
53    #define FIX(X)    ((1UL << SCALEBITS) / (X) + 1)
54    
55    static const uint32_t multipliers[32] =
56    {
57            0,       FIX(2),  FIX(4),  FIX(6),
58            FIX(8),  FIX(10), FIX(12), FIX(14),
59            FIX(16), FIX(18), FIX(20), FIX(22),
60            FIX(24), FIX(26), FIX(28), FIX(30),
61            FIX(32), FIX(34), FIX(36), FIX(38),
62            FIX(40), FIX(42), FIX(44), FIX(46),
63            FIX(48), FIX(50), FIX(52), FIX(54),
64            FIX(56), FIX(58), FIX(60), FIX(62)
65    };
66    
67    /*****************************************************************************
68     * Function definitions
69     ****************************************************************************/
70    
71    /* quantize intra-block
72     *
73     * const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q);
74     * level = DIV_DIV(16 * data[i], default_intra_matrix[i]);
75     * coeff[i] = (level + quantd) / quant2;
76     */
77    
78    uint32_t
79    quant_mpeg_intra_c(int16_t * coeff,
80                                       const int16_t * data,
81                                       const uint32_t quant,
82                                       const uint32_t dcscalar,
83                                       const uint16_t * mpeg_quant_matrices)
84    {
85            const uint32_t quantd = ((VM18P * quant) + (VM18Q / 2)) / VM18Q;
86            const uint32_t mult = multipliers[quant];
87            const uint16_t *intra_matrix = get_intra_matrix(mpeg_quant_matrices);
88            int i;
89    
90            coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar);
91    
92            for (i = 1; i < 64; i++) {
93                    if (data[i] < 0) {
94                            uint32_t level = -data[i];
95    
96                            level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i];
97                            level = ((level + quantd) * mult) >> SCALEBITS;
98                            coeff[i] = -(int16_t) level;
99                    } else if (data[i] > 0) {
100                            uint32_t level = data[i];
101    
102                            level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i];
103                            level = ((level + quantd) * mult) >> SCALEBITS;
104                            coeff[i] = level;
105                    } else {
106                            coeff[i] = 0;
107                    }
108            }
109    
110            return(0);
111    }
112    
113    /* quantize inter-block
114     *
115     * level = DIV_DIV(16 * data[i], default_intra_matrix[i]);
116     * coeff[i] = (level + quantd) / quant2;
117     * sum += abs(level);
118     */
119    
120    uint32_t
121    quant_mpeg_inter_c(int16_t * coeff,
122                                       const int16_t * data,
123                                       const uint32_t quant,
124                                       const uint16_t * mpeg_quant_matrices)
125    {
126            const uint32_t mult = multipliers[quant];
127            const uint16_t *inter_matrix = get_inter_matrix(mpeg_quant_matrices);
128            uint32_t sum = 0;
129            int i;
130    
131            for (i = 0; i < 64; i++) {
132                    if (data[i] < 0) {
133                            uint32_t level = -data[i];
134    
135                            level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
136                            level = (level * mult) >> 17;
137                            sum += level;
138                            coeff[i] = -(int16_t) level;
139                    } else if (data[i] > 0) {
140                            uint32_t level = data[i];
141    
142                            level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
143                            level = (level * mult) >> 17;
144                            sum += level;
145                            coeff[i] = level;
146                    } else {
147                            coeff[i] = 0;
148                    }
149            }
150    
151            return(sum);
152    }
153    
154    /* dequantize intra-block & clamp to [-2048,2047]
155     *
156     * data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4;
157     */
158    
159    uint32_t
160    dequant_mpeg_intra_c(int16_t * data,
161                                             const int16_t * coeff,
162                                             const uint32_t quant,
163                                             const uint32_t dcscalar,
164                                             const uint16_t * mpeg_quant_matrices)
165    {
166            const uint16_t *intra_matrix = get_intra_matrix(mpeg_quant_matrices);
167            int i;
168    
169            data[0] = coeff[0] * dcscalar;
170            if (data[0] < -2048) {
171                    data[0] = -2048;
172            } else if (data[0] > 2047) {
173                    data[0] = 2047;
174            }
175    
176            for (i = 1; i < 64; i++) {
177                    if (coeff[i] == 0) {
178                            data[i] = 0;
179                    } else if (coeff[i] < 0) {
180                            uint32_t level = -coeff[i];
181    
182                            level = (level * intra_matrix[i] * quant) >> 3;
183                            data[i] = (level <= 2048 ? -(int16_t) level : -2048);
184                    } else {
185                            uint32_t level = coeff[i];
186    
187                            level = (level * intra_matrix[i] * quant) >> 3;
188                            data[i] = (level <= 2047 ? level : 2047);
189                    }
190            }
191    
192            return(0);
193    }
194    
195    
196    /* dequantize inter-block & clamp to [-2048,2047]
197     * data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16
198     */
199    
200    uint32_t
201    dequant_mpeg_inter_c(int16_t * data,
202                                             const int16_t * coeff,
203                                             const uint32_t quant,
204                                             const uint16_t * mpeg_quant_matrices)
205    {
206            uint32_t sum = 0;
207            const uint16_t *inter_matrix = get_inter_matrix(mpeg_quant_matrices);
208            int i;
209    
210            for (i = 0; i < 64; i++) {
211                    if (coeff[i] == 0) {
212                            data[i] = 0;
213                    } else if (coeff[i] < 0) {
214                            int32_t level = -coeff[i];
215    
216                            level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;
217                            data[i] = (level <= 2048 ? -level : -2048);
218                    } else {
219                            uint32_t level = coeff[i];
220    
221                            level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4;
222                            data[i] = (level <= 2047 ? level : 2047);
223                    }
224    
225                    sum ^= data[i];
226            }
227    
228            /*      mismatch control */
229            if ((sum & 1) == 0) {
230                    data[63] ^= 1;
231            }
232    
233            return(0);
234    }

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

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