ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvs/xvidcore/src/quant/quant_mpeg.c
Revision: 1.1.2.2
Committed: Thu Oct 9 18:50:22 2003 UTC (21 years ago) by edgomez
Content type: text/plain
Branch: dev-api-4
Changes since 1.1.2.1: +1 -5 lines
Log Message:
Updated assembly functions to match new API -- more to come when matrices will be part of the function parameters

File Contents

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