quantize.c 8.39 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9
10
11
12
13
14
15
16
17
18
 */


#include <math.h>
#include "vpx_mem/vpx_mem.h"

#include "quantize.h"
#include "entropy.h"
#include "predictdc.h"

19
20
//#define EXACT_QUANT
#ifdef EXACT_QUANT
John Koleszar's avatar
John Koleszar committed
21
22
23
24
25
void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
{
    int i, rc, eob;
    int zbin;
    int x, y, z, sz;
26
27
28
29
30
31
32
33
    short *coeff_ptr       = b->coeff;
    short *zbin_ptr        = b->zbin;
    short *round_ptr       = b->round;
    short *quant_ptr       = b->quant;
    short *quant_shift_ptr = b->quant_shift;
    short *qcoeff_ptr      = d->qcoeff;
    short *dqcoeff_ptr     = d->dqcoeff;
    short *dequant_ptr     = d->dequant;
John Koleszar's avatar
John Koleszar committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    vpx_memset(qcoeff_ptr, 0, 32);
    vpx_memset(dqcoeff_ptr, 0, 32);

    eob = -1;

    for (i = 0; i < 16; i++)
    {
        rc   = vp8_default_zig_zag1d[i];
        z    = coeff_ptr[rc];
        zbin = zbin_ptr[rc] ;

        sz = (z >> 31);                                 // sign of z
        x  = (z ^ sz) - sz;                             // x = abs(z)

        if (x >= zbin)
        {
51
52
53
            x += round_ptr[rc];
            y  = (((x * quant_ptr[rc]) >> 16) + x)
                 >> quant_shift_ptr[rc];                // quantize (x)
John Koleszar's avatar
John Koleszar committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
            x  = (y ^ sz) - sz;                         // get the sign back
            qcoeff_ptr[rc] = x;                          // write to destination
            dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value

            if (y)
            {
                eob = i;                                // last nonzero coeffs
            }
        }
    }
    d->eob = eob + 1;
}

void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
{
    int i, rc, eob;
    int zbin;
    int x, y, z, sz;
72
73
74
75
76
77
78
79
80
81
    short *zbin_boost_ptr  = b->zrun_zbin_boost;
    short *coeff_ptr       = b->coeff;
    short *zbin_ptr        = b->zbin;
    short *round_ptr       = b->round;
    short *quant_ptr       = b->quant;
    short *quant_shift_ptr = b->quant_shift;
    short *qcoeff_ptr      = d->qcoeff;
    short *dqcoeff_ptr     = d->dqcoeff;
    short *dequant_ptr     = d->dequant;
    short zbin_oq_value    = b->zbin_extra;
John Koleszar's avatar
John Koleszar committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

    vpx_memset(qcoeff_ptr, 0, 32);
    vpx_memset(dqcoeff_ptr, 0, 32);

    eob = -1;

    for (i = 0; i < 16; i++)
    {
        rc   = vp8_default_zig_zag1d[i];
        z    = coeff_ptr[rc];

        //if ( i == 0 )
        //    zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value/2;
        //else
        zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;

        zbin_boost_ptr ++;
        sz = (z >> 31);                                 // sign of z
        x  = (z ^ sz) - sz;                             // x = abs(z)

        if (x >= zbin)
        {
104
105
106
            x += round_ptr[rc];
            y  = (((x * quant_ptr[rc]) >> 16) + x)
                 >> quant_shift_ptr[rc];                // quantize (x)
John Koleszar's avatar
John Koleszar committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
            x  = (y ^ sz) - sz;                         // get the sign back
            qcoeff_ptr[rc]  = x;                         // write to destination
            dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value

            if (y)
            {
                eob = i;                                // last nonzero coeffs
                zbin_boost_ptr = &b->zrun_zbin_boost[0];    // reset zero runlength
            }
        }
    }

    d->eob = eob + 1;
}
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

/* Perform regular quantization, with unbiased rounding and no zero bin. */
void vp8_strict_quantize_b(BLOCK *b, BLOCKD *d)
{
    int i;
    int rc;
    int eob;
    int x;
    int y;
    int z;
    int sz;
    short *coeff_ptr;
    short *quant_ptr;
    short *quant_shift_ptr;
    short *qcoeff_ptr;
    short *dqcoeff_ptr;
    short *dequant_ptr;

139
140
141
142
143
144
    coeff_ptr       = b->coeff;
    quant_ptr       = b->quant;
    quant_shift_ptr = b->quant_shift;
    qcoeff_ptr      = d->qcoeff;
    dqcoeff_ptr     = d->dqcoeff;
    dequant_ptr     = d->dequant;
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    eob = - 1;
    vpx_memset(qcoeff_ptr, 0, 32);
    vpx_memset(dqcoeff_ptr, 0, 32);
    for (i = 0; i < 16; i++)
    {
        int dq;
        int round;

        /*TODO: These arrays should be stored in zig-zag order.*/
        rc = vp8_default_zig_zag1d[i];
        z = coeff_ptr[rc];
        dq = dequant_ptr[rc];
        round = dq >> 1;
        /* Sign of z. */
        sz = -(z < 0);
        x = (z + sz) ^ sz;
        x += round;
        if (x >= dq)
        {
            /* Quantize x. */
            y  = (((x * quant_ptr[rc]) >> 16) + x) >> quant_shift_ptr[rc];
            /* Put the sign back. */
            x = (y + sz) ^ sz;
            /* Save the coefficient and its dequantized value. */
            qcoeff_ptr[rc] = x;
            dqcoeff_ptr[rc] = x * dq;
            /* Remember the last non-zero coefficient. */
            if (y)
                eob = i;
        }
    }

    d->eob = eob + 1;
}

180
#else
John Koleszar's avatar
John Koleszar committed
181
182
183
184
185
void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
{
    int i, rc, eob;
    int zbin;
    int x, y, z, sz;
186
187
188
189
    short *coeff_ptr   = b->coeff;
    short *round_ptr   = b->round;
    short *quant_ptr   = b->quant;
    short *qcoeff_ptr  = d->qcoeff;
John Koleszar's avatar
John Koleszar committed
190
    short *dqcoeff_ptr = d->dqcoeff;
191
    short *dequant_ptr = d->dequant;
John Koleszar's avatar
John Koleszar committed
192
193
194
195
196
197
198
199
200
201

    eob = -1;
    for (i = 0; i < 16; i++)
    {
        rc   = vp8_default_zig_zag1d[i];
        z    = coeff_ptr[rc];

        sz = (z >> 31);                                 // sign of z
        x  = (z ^ sz) - sz;                             // x = abs(z)

Yaowu Xu's avatar
Yaowu Xu committed
202
203
204
205
        y  = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
        x  = (y ^ sz) - sz;                         // get the sign back
        qcoeff_ptr[rc] = x;                          // write to destination
        dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value
John Koleszar's avatar
John Koleszar committed
206

Yaowu Xu's avatar
Yaowu Xu committed
207
208
209
        if (y)
        {
            eob = i;                                // last nonzero coeffs
John Koleszar's avatar
John Koleszar committed
210
211
212
213
214
215
216
217
218
219
        }
    }
    d->eob = eob + 1;
}

void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
{
    int i, rc, eob;
    int zbin;
    int x, y, z, sz;
220
221
222
223
224
225
226
227
228
    short *zbin_boost_ptr = b->zrun_zbin_boost;
    short *coeff_ptr      = b->coeff;
    short *zbin_ptr       = b->zbin;
    short *round_ptr      = b->round;
    short *quant_ptr      = b->quant;
    short *qcoeff_ptr     = d->qcoeff;
    short *dqcoeff_ptr    = d->dqcoeff;
    short *dequant_ptr    = d->dequant;
    short zbin_oq_value   = b->zbin_extra;
John Koleszar's avatar
John Koleszar committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

    vpx_memset(qcoeff_ptr, 0, 32);
    vpx_memset(dqcoeff_ptr, 0, 32);

    eob = -1;

    for (i = 0; i < 16; i++)
    {
        rc   = vp8_default_zig_zag1d[i];
        z    = coeff_ptr[rc];

        //if ( i == 0 )
        //    zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value/2;
        //else
        zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;

        zbin_boost_ptr ++;
        sz = (z >> 31);                                 // sign of z
        x  = (z ^ sz) - sz;                             // x = abs(z)

        if (x >= zbin)
        {
            y  = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
            x  = (y ^ sz) - sz;                         // get the sign back
            qcoeff_ptr[rc]  = x;                         // write to destination
            dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value

            if (y)
            {
                eob = i;                                // last nonzero coeffs
                zbin_boost_ptr = &b->zrun_zbin_boost[0];    // reset zero runlength
            }
        }
    }

    d->eob = eob + 1;
}

267
#endif
268

John Koleszar's avatar
John Koleszar committed
269
270
271
void vp8_quantize_mby(MACROBLOCK *x)
{
    int i;
272
273
    int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
John Koleszar's avatar
John Koleszar committed
274

275
276
    for (i = 0; i < 16; i++)
        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
John Koleszar's avatar
John Koleszar committed
277

278
    if(has_2nd_order)
John Koleszar's avatar
John Koleszar committed
279
280
281
282
283
284
        x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
}

void vp8_quantize_mb(MACROBLOCK *x)
{
    int i;
285
286
    int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
John Koleszar's avatar
John Koleszar committed
287

288
289
    for (i = 0; i < 24+has_2nd_order; i++)
        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
John Koleszar's avatar
John Koleszar committed
290
291
292
293
294
295
296
297
298
299
}


void vp8_quantize_mbuv(MACROBLOCK *x)
{
    int i;

    for (i = 16; i < 24; i++)
        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
}