[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.1.2.1, Tue Oct 7 13:02:35 2003 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    {
84            const uint32_t quantd = ((VM18P * quant) + (VM18Q / 2)) / VM18Q;
85            const uint32_t mult = multipliers[quant];
86            const int16_t *intra_matrix = get_intra_matrix();
87            uint32_t sum = 0;
88            int i;
89    
90            coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar);
91            sum += coeff[0];
92    
93            for (i = 1; i < 64; i++) {
94                    if (data[i] < 0) {
95                            uint32_t level = -data[i];
96    
97                            level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i];
98                            level = ((level + quantd) * mult) >> SCALEBITS;
99                            sum += level;
100                            coeff[i] = -(int16_t) level;
101                    } else if (data[i] > 0) {
102                            uint32_t level = data[i];
103    
104                            level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i];
105                            level = ((level + quantd) * mult) >> SCALEBITS;
106                            sum += level;
107                            coeff[i] = level;
108                    } else {
109                            coeff[i] = 0;
110                    }
111            }
112    
113            return(sum);
114    }
115    
116    /* quantize inter-block
117     *
118     * level = DIV_DIV(16 * data[i], default_intra_matrix[i]);
119     * coeff[i] = (level + quantd) / quant2;
120     * sum += abs(level);
121     */
122    
123    uint32_t
124    quant_mpeg_inter_c(int16_t * coeff,
125                                       const int16_t * data,
126                                       const uint32_t quant)
127    {
128            const uint32_t mult = multipliers[quant];
129            const int16_t *inter_matrix = get_inter_matrix();
130            uint32_t sum = 0;
131            int i;
132    
133            for (i = 0; i < 64; i++) {
134                    if (data[i] < 0) {
135                            uint32_t level = -data[i];
136    
137                            level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
138                            level = (level * mult) >> 17;
139                            sum += level;
140                            coeff[i] = -(int16_t) level;
141                    } else if (data[i] > 0) {
142                            uint32_t level = data[i];
143    
144                            level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
145                            level = (level * mult) >> 17;
146                            sum += level;
147                            coeff[i] = level;
148                    } else {
149                            coeff[i] = 0;
150                    }
151            }
152    
153            return(sum);
154    }
155    
156    /* dequantize intra-block & clamp to [-2048,2047]
157     *
158     * data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4;
159     */
160    
161    uint32_t
162    dequant_mpeg_intra_c(int16_t * data,
163                                             const int16_t * coeff,
164                                             const uint32_t quant,
165                                             const uint32_t dcscalar)
166    {
167            const int16_t *intra_matrix = get_intra_matrix();
168            int i;
169    
170            data[0] = coeff[0] * dcscalar;
171            if (data[0] < -2048) {
172                    data[0] = -2048;
173            } else if (data[0] > 2047) {
174                    data[0] = 2047;
175            }
176    
177            for (i = 1; i < 64; i++) {
178                    if (coeff[i] == 0) {
179                            data[i] = 0;
180                    } else if (coeff[i] < 0) {
181                            uint32_t level = -coeff[i];
182    
183                            level = (level * intra_matrix[i] * quant) >> 3;
184                            data[i] = (level <= 2048 ? -(int16_t) level : -2048);
185                    } else {
186                            uint32_t level = coeff[i];
187    
188                            level = (level * intra_matrix[i] * quant) >> 3;
189                            data[i] = (level <= 2047 ? level : 2047);
190                    }
191            }
192    
193            return(0);
194    }
195    
196    
197    /* dequantize inter-block & clamp to [-2048,2047]
198     * data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16
199     */
200    
201    uint32_t
202    dequant_mpeg_inter_c(int16_t * data,
203                                             const int16_t * coeff,
204                                             const uint32_t quant)
205    {
206            uint32_t sum = 0;
207            const int16_t *inter_matrix = get_inter_matrix();
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.1.2.1

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