PageRenderTime 77ms CodeModel.GetById 22ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 0ms

/media/libvpx/vp8/encoder/quantize.c

http://github.com/zpao/v8monkey
C | 734 lines | 579 code | 112 blank | 43 comment | 40 complexity | 38b045f868dd87f25dbb74cfd0f23ffd MD5 | raw file
  1/*
  2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3 *
  4 *  Use of this source code is governed by a BSD-style license
  5 *  that can be found in the LICENSE file in the root of the source
  6 *  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.
  9 */
 10
 11
 12#include <math.h>
 13#include "vpx_mem/vpx_mem.h"
 14
 15#include "onyx_int.h"
 16#include "quantize.h"
 17#include "vp8/common/quant_common.h"
 18
 19#define EXACT_QUANT
 20
 21#ifdef EXACT_FASTQUANT
 22void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
 23{
 24    int i, rc, eob;
 25    int zbin;
 26    int x, y, z, sz;
 27    short *coeff_ptr       = b->coeff;
 28    short *zbin_ptr        = b->zbin;
 29    short *round_ptr       = b->round;
 30    short *quant_ptr       = b->quant_fast;
 31    unsigned char *quant_shift_ptr = b->quant_shift;
 32    short *qcoeff_ptr      = d->qcoeff;
 33    short *dqcoeff_ptr     = d->dqcoeff;
 34    short *dequant_ptr     = d->dequant;
 35
 36    vpx_memset(qcoeff_ptr, 0, 32);
 37    vpx_memset(dqcoeff_ptr, 0, 32);
 38
 39    eob = -1;
 40
 41    for (i = 0; i < 16; i++)
 42    {
 43        rc   = vp8_default_zig_zag1d[i];
 44        z    = coeff_ptr[rc];
 45        zbin = zbin_ptr[rc] ;
 46
 47        sz = (z >> 31);                                 // sign of z
 48        x  = (z ^ sz) - sz;                             // x = abs(z)
 49
 50        if (x >= zbin)
 51        {
 52            x += round_ptr[rc];
 53            y  = (((x * quant_ptr[rc]) >> 16) + x)
 54                 >> quant_shift_ptr[rc];                // quantize (x)
 55            x  = (y ^ sz) - sz;                         // get the sign back
 56            qcoeff_ptr[rc] = x;                          // write to destination
 57            dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value
 58
 59            if (y)
 60            {
 61                eob = i;                                // last nonzero coeffs
 62            }
 63        }
 64    }
 65    d->eob = eob + 1;
 66}
 67
 68#else
 69
 70void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
 71{
 72    int i, rc, eob;
 73    int x, y, z, sz;
 74    short *coeff_ptr   = b->coeff;
 75    short *round_ptr   = b->round;
 76    short *quant_ptr   = b->quant_fast;
 77    short *qcoeff_ptr  = d->qcoeff;
 78    short *dqcoeff_ptr = d->dqcoeff;
 79    short *dequant_ptr = d->dequant;
 80
 81    eob = -1;
 82    for (i = 0; i < 16; i++)
 83    {
 84        rc   = vp8_default_zig_zag1d[i];
 85        z    = coeff_ptr[rc];
 86
 87        sz = (z >> 31);                                 // sign of z
 88        x  = (z ^ sz) - sz;                             // x = abs(z)
 89
 90        y  = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
 91        x  = (y ^ sz) - sz;                         // get the sign back
 92        qcoeff_ptr[rc] = x;                          // write to destination
 93        dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value
 94
 95        if (y)
 96        {
 97            eob = i;                                // last nonzero coeffs
 98        }
 99    }
100    d->eob = eob + 1;
101}
102
103#endif
104
105#ifdef EXACT_QUANT
106void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
107{
108    int i, rc, eob;
109    int zbin;
110    int x, y, z, sz;
111    short *zbin_boost_ptr  = b->zrun_zbin_boost;
112    short *coeff_ptr       = b->coeff;
113    short *zbin_ptr        = b->zbin;
114    short *round_ptr       = b->round;
115    short *quant_ptr       = b->quant;
116    unsigned char *quant_shift_ptr = b->quant_shift;
117    short *qcoeff_ptr      = d->qcoeff;
118    short *dqcoeff_ptr     = d->dqcoeff;
119    short *dequant_ptr     = d->dequant;
120    short zbin_oq_value    = b->zbin_extra;
121
122    vpx_memset(qcoeff_ptr, 0, 32);
123    vpx_memset(dqcoeff_ptr, 0, 32);
124
125    eob = -1;
126
127    for (i = 0; i < 16; i++)
128    {
129        rc   = vp8_default_zig_zag1d[i];
130        z    = coeff_ptr[rc];
131
132        zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
133
134        zbin_boost_ptr ++;
135        sz = (z >> 31);                                 // sign of z
136        x  = (z ^ sz) - sz;                             // x = abs(z)
137
138        if (x >= zbin)
139        {
140            x += round_ptr[rc];
141            y  = (((x * quant_ptr[rc]) >> 16) + x)
142                 >> quant_shift_ptr[rc];                // quantize (x)
143            x  = (y ^ sz) - sz;                         // get the sign back
144            qcoeff_ptr[rc]  = x;                        // write to destination
145            dqcoeff_ptr[rc] = x * dequant_ptr[rc];      // dequantized value
146
147            if (y)
148            {
149                eob = i;                                // last nonzero coeffs
150                zbin_boost_ptr = b->zrun_zbin_boost;    // reset zero runlength
151            }
152        }
153    }
154
155    d->eob = eob + 1;
156}
157
158/* Perform regular quantization, with unbiased rounding and no zero bin. */
159void vp8_strict_quantize_b(BLOCK *b, BLOCKD *d)
160{
161    int i;
162    int rc;
163    int eob;
164    int x;
165    int y;
166    int z;
167    int sz;
168    short *coeff_ptr;
169    short *quant_ptr;
170    unsigned char *quant_shift_ptr;
171    short *qcoeff_ptr;
172    short *dqcoeff_ptr;
173    short *dequant_ptr;
174
175    coeff_ptr       = b->coeff;
176    quant_ptr       = b->quant;
177    quant_shift_ptr = b->quant_shift;
178    qcoeff_ptr      = d->qcoeff;
179    dqcoeff_ptr     = d->dqcoeff;
180    dequant_ptr     = d->dequant;
181    eob = - 1;
182    vpx_memset(qcoeff_ptr, 0, 32);
183    vpx_memset(dqcoeff_ptr, 0, 32);
184    for (i = 0; i < 16; i++)
185    {
186        int dq;
187        int round;
188
189        /*TODO: These arrays should be stored in zig-zag order.*/
190        rc = vp8_default_zig_zag1d[i];
191        z = coeff_ptr[rc];
192        dq = dequant_ptr[rc];
193        round = dq >> 1;
194        /* Sign of z. */
195        sz = -(z < 0);
196        x = (z + sz) ^ sz;
197        x += round;
198        if (x >= dq)
199        {
200            /* Quantize x. */
201            y  = (((x * quant_ptr[rc]) >> 16) + x) >> quant_shift_ptr[rc];
202            /* Put the sign back. */
203            x = (y + sz) ^ sz;
204            /* Save the coefficient and its dequantized value. */
205            qcoeff_ptr[rc] = x;
206            dqcoeff_ptr[rc] = x * dq;
207            /* Remember the last non-zero coefficient. */
208            if (y)
209                eob = i;
210        }
211    }
212
213    d->eob = eob + 1;
214}
215
216#else
217
218void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
219{
220    int i, rc, eob;
221    int zbin;
222    int x, y, z, sz;
223    short *zbin_boost_ptr = b->zrun_zbin_boost;
224    short *coeff_ptr      = b->coeff;
225    short *zbin_ptr       = b->zbin;
226    short *round_ptr      = b->round;
227    short *quant_ptr      = b->quant;
228    short *qcoeff_ptr     = d->qcoeff;
229    short *dqcoeff_ptr    = d->dqcoeff;
230    short *dequant_ptr    = d->dequant;
231    short zbin_oq_value   = b->zbin_extra;
232
233    vpx_memset(qcoeff_ptr, 0, 32);
234    vpx_memset(dqcoeff_ptr, 0, 32);
235
236    eob = -1;
237
238    for (i = 0; i < 16; i++)
239    {
240        rc   = vp8_default_zig_zag1d[i];
241        z    = coeff_ptr[rc];
242
243        //if ( i == 0 )
244        //    zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value/2;
245        //else
246        zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
247
248        zbin_boost_ptr ++;
249        sz = (z >> 31);                                 // sign of z
250        x  = (z ^ sz) - sz;                             // x = abs(z)
251
252        if (x >= zbin)
253        {
254            y  = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
255            x  = (y ^ sz) - sz;                         // get the sign back
256            qcoeff_ptr[rc]  = x;                         // write to destination
257            dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value
258
259            if (y)
260            {
261                eob = i;                                // last nonzero coeffs
262                zbin_boost_ptr = &b->zrun_zbin_boost[0];    // reset zero runlength
263            }
264        }
265    }
266
267    d->eob = eob + 1;
268}
269
270#endif
271
272void vp8_quantize_mby_c(MACROBLOCK *x)
273{
274    int i;
275    int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
276        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
277
278    for (i = 0; i < 16; i++)
279        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
280
281    if(has_2nd_order)
282        x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
283}
284
285void vp8_quantize_mb_c(MACROBLOCK *x)
286{
287    int i;
288    int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
289        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
290
291    for (i = 0; i < 24+has_2nd_order; i++)
292        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
293}
294
295
296void vp8_quantize_mbuv_c(MACROBLOCK *x)
297{
298    int i;
299
300    for (i = 16; i < 24; i++)
301        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
302}
303
304/* quantize_b_pair function pointer in MACROBLOCK structure is set to one of
305 * these two C functions if corresponding optimized routine is not available.
306 * NEON optimized version implements currently the fast quantization for pair
307 * of blocks. */
308void vp8_regular_quantize_b_pair(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
309{
310    vp8_regular_quantize_b(b1, d1);
311    vp8_regular_quantize_b(b2, d2);
312}
313
314void vp8_fast_quantize_b_pair_c(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
315{
316    vp8_fast_quantize_b_c(b1, d1);
317    vp8_fast_quantize_b_c(b2, d2);
318}
319
320
321static const int qrounding_factors[129] =
322{
323    48, 48, 48, 48, 48, 48, 48, 48,
324    48, 48, 48, 48, 48, 48, 48, 48,
325    48, 48, 48, 48, 48, 48, 48, 48,
326    48, 48, 48, 48, 48, 48, 48, 48,
327    48, 48, 48, 48, 48, 48, 48, 48,
328    48, 48, 48, 48, 48, 48, 48, 48,
329    48, 48, 48, 48, 48, 48, 48, 48,
330    48, 48, 48, 48, 48, 48, 48, 48,
331    48, 48, 48, 48, 48, 48, 48, 48,
332    48, 48, 48, 48, 48, 48, 48, 48,
333    48, 48, 48, 48, 48, 48, 48, 48,
334    48, 48, 48, 48, 48, 48, 48, 48,
335    48, 48, 48, 48, 48, 48, 48, 48,
336    48, 48, 48, 48, 48, 48, 48, 48,
337    48, 48, 48, 48, 48, 48, 48, 48,
338    48, 48, 48, 48, 48, 48, 48, 48,
339    48
340};
341
342
343static const int qzbin_factors[129] =
344{
345    84, 84, 84, 84, 84, 84, 84, 84,
346    84, 84, 84, 84, 84, 84, 84, 84,
347    84, 84, 84, 84, 84, 84, 84, 84,
348    84, 84, 84, 84, 84, 84, 84, 84,
349    84, 84, 84, 84, 84, 84, 84, 84,
350    84, 84, 84, 84, 84, 84, 84, 84,
351    80, 80, 80, 80, 80, 80, 80, 80,
352    80, 80, 80, 80, 80, 80, 80, 80,
353    80, 80, 80, 80, 80, 80, 80, 80,
354    80, 80, 80, 80, 80, 80, 80, 80,
355    80, 80, 80, 80, 80, 80, 80, 80,
356    80, 80, 80, 80, 80, 80, 80, 80,
357    80, 80, 80, 80, 80, 80, 80, 80,
358    80, 80, 80, 80, 80, 80, 80, 80,
359    80, 80, 80, 80, 80, 80, 80, 80,
360    80, 80, 80, 80, 80, 80, 80, 80,
361    80
362};
363
364
365static const int qrounding_factors_y2[129] =
366{
367    48, 48, 48, 48, 48, 48, 48, 48,
368    48, 48, 48, 48, 48, 48, 48, 48,
369    48, 48, 48, 48, 48, 48, 48, 48,
370    48, 48, 48, 48, 48, 48, 48, 48,
371    48, 48, 48, 48, 48, 48, 48, 48,
372    48, 48, 48, 48, 48, 48, 48, 48,
373    48, 48, 48, 48, 48, 48, 48, 48,
374    48, 48, 48, 48, 48, 48, 48, 48,
375    48, 48, 48, 48, 48, 48, 48, 48,
376    48, 48, 48, 48, 48, 48, 48, 48,
377    48, 48, 48, 48, 48, 48, 48, 48,
378    48, 48, 48, 48, 48, 48, 48, 48,
379    48, 48, 48, 48, 48, 48, 48, 48,
380    48, 48, 48, 48, 48, 48, 48, 48,
381    48, 48, 48, 48, 48, 48, 48, 48,
382    48, 48, 48, 48, 48, 48, 48, 48,
383    48
384};
385
386
387static const int qzbin_factors_y2[129] =
388{
389    84, 84, 84, 84, 84, 84, 84, 84,
390    84, 84, 84, 84, 84, 84, 84, 84,
391    84, 84, 84, 84, 84, 84, 84, 84,
392    84, 84, 84, 84, 84, 84, 84, 84,
393    84, 84, 84, 84, 84, 84, 84, 84,
394    84, 84, 84, 84, 84, 84, 84, 84,
395    80, 80, 80, 80, 80, 80, 80, 80,
396    80, 80, 80, 80, 80, 80, 80, 80,
397    80, 80, 80, 80, 80, 80, 80, 80,
398    80, 80, 80, 80, 80, 80, 80, 80,
399    80, 80, 80, 80, 80, 80, 80, 80,
400    80, 80, 80, 80, 80, 80, 80, 80,
401    80, 80, 80, 80, 80, 80, 80, 80,
402    80, 80, 80, 80, 80, 80, 80, 80,
403    80, 80, 80, 80, 80, 80, 80, 80,
404    80, 80, 80, 80, 80, 80, 80, 80,
405    80
406};
407
408
409#define EXACT_QUANT
410#ifdef EXACT_QUANT
411static void invert_quant(int improved_quant, short *quant,
412                               unsigned char *shift, short d)
413{
414    if(improved_quant)
415    {
416        unsigned t;
417        int l;
418        t = d;
419        for(l = 0; t > 1; l++)
420            t>>=1;
421        t = 1 + (1<<(16+l))/d;
422        *quant = (short)(t - (1<<16));
423        *shift = l;
424    }
425    else
426    {
427        *quant = (1 << 16) / d;
428        *shift = 0;
429    }
430}
431
432
433void vp8cx_init_quantizer(VP8_COMP *cpi)
434{
435    int i;
436    int quant_val;
437    int Q;
438
439    int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44};
440
441    for (Q = 0; Q < QINDEX_RANGE; Q++)
442    {
443        // dc values
444        quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
445        cpi->Y1quant_fast[Q][0] = (1 << 16) / quant_val;
446        invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 0,
447                     cpi->Y1quant_shift[Q] + 0, quant_val);
448        cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
449        cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
450        cpi->common.Y1dequant[Q][0] = quant_val;
451        cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
452
453        quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
454        cpi->Y2quant_fast[Q][0] = (1 << 16) / quant_val;
455        invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 0,
456                     cpi->Y2quant_shift[Q] + 0, quant_val);
457        cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
458        cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
459        cpi->common.Y2dequant[Q][0] = quant_val;
460        cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
461
462        quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
463        cpi->UVquant_fast[Q][0] = (1 << 16) / quant_val;
464        invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 0,
465                     cpi->UVquant_shift[Q] + 0, quant_val);
466        cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
467        cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
468        cpi->common.UVdequant[Q][0] = quant_val;
469        cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
470
471        // all the ac values = ;
472        for (i = 1; i < 16; i++)
473        {
474            int rc = vp8_default_zig_zag1d[i];
475
476            quant_val = vp8_ac_yquant(Q);
477            cpi->Y1quant_fast[Q][rc] = (1 << 16) / quant_val;
478            invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + rc,
479                         cpi->Y1quant_shift[Q] + rc, quant_val);
480            cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
481            cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
482            cpi->common.Y1dequant[Q][rc] = quant_val;
483            cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7;
484
485            quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
486            cpi->Y2quant_fast[Q][rc] = (1 << 16) / quant_val;
487            invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + rc,
488                         cpi->Y2quant_shift[Q] + rc, quant_val);
489            cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
490            cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7;
491            cpi->common.Y2dequant[Q][rc] = quant_val;
492            cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7;
493
494            quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
495            cpi->UVquant_fast[Q][rc] = (1 << 16) / quant_val;
496            invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + rc,
497                         cpi->UVquant_shift[Q] + rc, quant_val);
498            cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
499            cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
500            cpi->common.UVdequant[Q][rc] = quant_val;
501            cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
502        }
503    }
504}
505#else
506void vp8cx_init_quantizer(VP8_COMP *cpi)
507{
508    int i;
509    int quant_val;
510    int Q;
511
512    int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44};
513
514    for (Q = 0; Q < QINDEX_RANGE; Q++)
515    {
516        // dc values
517        quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
518        cpi->Y1quant[Q][0] = (1 << 16) / quant_val;
519        cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
520        cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
521        cpi->common.Y1dequant[Q][0] = quant_val;
522        cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
523
524        quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
525        cpi->Y2quant[Q][0] = (1 << 16) / quant_val;
526        cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
527        cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
528        cpi->common.Y2dequant[Q][0] = quant_val;
529        cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
530
531        quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
532        cpi->UVquant[Q][0] = (1 << 16) / quant_val;
533        cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
534        cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
535        cpi->common.UVdequant[Q][0] = quant_val;
536        cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
537
538        // all the ac values = ;
539        for (i = 1; i < 16; i++)
540        {
541            int rc = vp8_default_zig_zag1d[i];
542
543            quant_val = vp8_ac_yquant(Q);
544            cpi->Y1quant[Q][rc] = (1 << 16) / quant_val;
545            cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
546            cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
547            cpi->common.Y1dequant[Q][rc] = quant_val;
548            cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7;
549
550            quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
551            cpi->Y2quant[Q][rc] = (1 << 16) / quant_val;
552            cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
553            cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7;
554            cpi->common.Y2dequant[Q][rc] = quant_val;
555            cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7;
556
557            quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
558            cpi->UVquant[Q][rc] = (1 << 16) / quant_val;
559            cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
560            cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
561            cpi->common.UVdequant[Q][rc] = quant_val;
562            cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
563        }
564    }
565}
566#endif
567
568
569void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x)
570{
571    int i;
572    int QIndex;
573    MACROBLOCKD *xd = &x->e_mbd;
574    int zbin_extra;
575
576    // Select the baseline MB Q index.
577    if (xd->segmentation_enabled)
578    {
579        // Abs Value
580        if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
581
582            QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
583        // Delta Value
584        else
585        {
586            QIndex = cpi->common.base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
587            QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;    // Clamp to valid range
588        }
589    }
590    else
591        QIndex = cpi->common.base_qindex;
592
593    // Y
594    zbin_extra = ( cpi->common.Y1dequant[QIndex][1] *
595                   ( cpi->zbin_over_quant +
596                     cpi->zbin_mode_boost +
597                     x->act_zbin_adj ) ) >> 7;
598
599    for (i = 0; i < 16; i++)
600    {
601        x->block[i].quant = cpi->Y1quant[QIndex];
602        x->block[i].quant_fast = cpi->Y1quant_fast[QIndex];
603        x->block[i].quant_shift = cpi->Y1quant_shift[QIndex];
604        x->block[i].zbin = cpi->Y1zbin[QIndex];
605        x->block[i].round = cpi->Y1round[QIndex];
606        x->e_mbd.block[i].dequant = cpi->common.Y1dequant[QIndex];
607        x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
608        x->block[i].zbin_extra = (short)zbin_extra;
609    }
610
611    // UV
612    zbin_extra = ( cpi->common.UVdequant[QIndex][1] *
613                   ( cpi->zbin_over_quant +
614                     cpi->zbin_mode_boost +
615                     x->act_zbin_adj ) ) >> 7;
616
617    for (i = 16; i < 24; i++)
618    {
619        x->block[i].quant = cpi->UVquant[QIndex];
620        x->block[i].quant_fast = cpi->UVquant_fast[QIndex];
621        x->block[i].quant_shift = cpi->UVquant_shift[QIndex];
622        x->block[i].zbin = cpi->UVzbin[QIndex];
623        x->block[i].round = cpi->UVround[QIndex];
624        x->e_mbd.block[i].dequant = cpi->common.UVdequant[QIndex];
625        x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
626        x->block[i].zbin_extra = (short)zbin_extra;
627    }
628
629    // Y2
630    zbin_extra = ( cpi->common.Y2dequant[QIndex][1] *
631                   ( (cpi->zbin_over_quant / 2) +
632                     cpi->zbin_mode_boost +
633                     x->act_zbin_adj ) ) >> 7;
634
635    x->block[24].quant_fast = cpi->Y2quant_fast[QIndex];
636    x->block[24].quant = cpi->Y2quant[QIndex];
637    x->block[24].quant_shift = cpi->Y2quant_shift[QIndex];
638    x->block[24].zbin = cpi->Y2zbin[QIndex];
639    x->block[24].round = cpi->Y2round[QIndex];
640    x->e_mbd.block[24].dequant = cpi->common.Y2dequant[QIndex];
641    x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex];
642    x->block[24].zbin_extra = (short)zbin_extra;
643
644    /* save this macroblock QIndex for vp8_update_zbin_extra() */
645    x->q_index = QIndex;
646}
647
648
649void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x)
650{
651    int i;
652    int QIndex = x->q_index;
653    int zbin_extra;
654
655    // Y
656    zbin_extra = ( cpi->common.Y1dequant[QIndex][1] *
657                   ( cpi->zbin_over_quant +
658                     cpi->zbin_mode_boost +
659                     x->act_zbin_adj ) ) >> 7;
660    for (i = 0; i < 16; i++)
661    {
662        x->block[i].zbin_extra = (short)zbin_extra;
663    }
664
665    // UV
666    zbin_extra = ( cpi->common.UVdequant[QIndex][1] *
667                   ( cpi->zbin_over_quant +
668                     cpi->zbin_mode_boost +
669                     x->act_zbin_adj ) ) >> 7;
670
671    for (i = 16; i < 24; i++)
672    {
673        x->block[i].zbin_extra = (short)zbin_extra;
674    }
675
676    // Y2
677    zbin_extra = ( cpi->common.Y2dequant[QIndex][1] *
678                   ( (cpi->zbin_over_quant / 2) +
679                     cpi->zbin_mode_boost +
680                     x->act_zbin_adj ) ) >> 7;
681
682    x->block[24].zbin_extra = (short)zbin_extra;
683}
684
685
686void vp8cx_frame_init_quantizer(VP8_COMP *cpi)
687{
688    // Clear Zbin mode boost for default case
689    cpi->zbin_mode_boost = 0;
690
691    // MB level quantizer setup
692    vp8cx_mb_init_quantizer(cpi, &cpi->mb);
693}
694
695
696void vp8_set_quantizer(struct VP8_COMP *cpi, int Q)
697{
698    VP8_COMMON *cm = &cpi->common;
699    MACROBLOCKD *mbd = &cpi->mb.e_mbd;
700    int update = 0;
701    int new_delta_q;
702    cm->base_qindex = Q;
703
704    /* if any of the delta_q values are changing update flag has to be set */
705    /* currently only y2dc_delta_q may change */
706
707    cm->y1dc_delta_q = 0;
708    cm->y2ac_delta_q = 0;
709    cm->uvdc_delta_q = 0;
710    cm->uvac_delta_q = 0;
711
712    if (Q < 4)
713    {
714        new_delta_q = 4-Q;
715    }
716    else
717        new_delta_q = 0;
718
719    update |= cm->y2dc_delta_q != new_delta_q;
720    cm->y2dc_delta_q = new_delta_q;
721
722
723    // Set Segment specific quatizers
724    mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
725    mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
726    mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
727    mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
728
729    /* quantizer has to be reinitialized for any delta_q changes */
730    if(update)
731        vp8cx_init_quantizer(cpi);
732
733}
734