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 = (char)(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 = (char)(eob + 1);
101}
102
103#endif
104
105#ifdef EXACT_QUANT
106void vp8_regular_quantize_b_c(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 = (char)(eob + 1);
156}
157
158/* Perform regular quantization, with unbiased rounding and no zero bin. */
159void vp8_strict_quantize_b_c(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 = (char)(eob + 1);
214}
215
216#else
217
218void vp8_regular_quantize_b_c(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        zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
244
245        zbin_boost_ptr ++;
246        sz = (z >> 31);                              /* sign of z */
247        x  = (z ^ sz) - sz;                          /* x = abs(z) */
248
249        if (x >= zbin)
250        {
251            y  = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; /* quantize (x) */
252            x  = (y ^ sz) - sz;                      /* get the sign back */
253            qcoeff_ptr[rc]  = x;                     /* write to destination */
254            dqcoeff_ptr[rc] = x * dequant_ptr[rc];   /* dequantized value */
255
256            if (y)
257            {
258                eob = i;                             /* last nonzero coeffs */
259                zbin_boost_ptr = &b->zrun_zbin_boost[0]; /* reset zrl */
260            }
261        }
262    }
263
264    *d->eob = (char)(eob + 1);
265}
266
267#endif
268
269void vp8_quantize_mby_c(MACROBLOCK *x)
270{
271    int i;
272    int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
273        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
274
275    for (i = 0; i < 16; i++)
276        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
277
278    if(has_2nd_order)
279        x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
280}
281
282void vp8_quantize_mb_c(MACROBLOCK *x)
283{
284    int i;
285    int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
286        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
287
288    for (i = 0; i < 24+has_2nd_order; i++)
289        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
290}
291
292
293void vp8_quantize_mbuv_c(MACROBLOCK *x)
294{
295    int i;
296
297    for (i = 16; i < 24; i++)
298        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
299}
300
301/* quantize_b_pair function pointer in MACROBLOCK structure is set to one of
302 * these two C functions if corresponding optimized routine is not available.
303 * NEON optimized version implements currently the fast quantization for pair
304 * of blocks. */
305void vp8_regular_quantize_b_pair(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
306{
307    vp8_regular_quantize_b(b1, d1);
308    vp8_regular_quantize_b(b2, d2);
309}
310
311void vp8_fast_quantize_b_pair_c(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
312{
313    vp8_fast_quantize_b_c(b1, d1);
314    vp8_fast_quantize_b_c(b2, d2);
315}
316
317
318static const int qrounding_factors[129] =
319{
320    48, 48, 48, 48, 48, 48, 48, 48,
321    48, 48, 48, 48, 48, 48, 48, 48,
322    48, 48, 48, 48, 48, 48, 48, 48,
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
337};
338
339
340static const int qzbin_factors[129] =
341{
342    84, 84, 84, 84, 84, 84, 84, 84,
343    84, 84, 84, 84, 84, 84, 84, 84,
344    84, 84, 84, 84, 84, 84, 84, 84,
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    80, 80, 80, 80, 80, 80, 80, 80,
349    80, 80, 80, 80, 80, 80, 80, 80,
350    80, 80, 80, 80, 80, 80, 80, 80,
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
359};
360
361
362static const int qrounding_factors_y2[129] =
363{
364    48, 48, 48, 48, 48, 48, 48, 48,
365    48, 48, 48, 48, 48, 48, 48, 48,
366    48, 48, 48, 48, 48, 48, 48, 48,
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
381};
382
383
384static const int qzbin_factors_y2[129] =
385{
386    84, 84, 84, 84, 84, 84, 84, 84,
387    84, 84, 84, 84, 84, 84, 84, 84,
388    84, 84, 84, 84, 84, 84, 84, 84,
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    80, 80, 80, 80, 80, 80, 80, 80,
393    80, 80, 80, 80, 80, 80, 80, 80,
394    80, 80, 80, 80, 80, 80, 80, 80,
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
403};
404
405
406#define EXACT_QUANT
407#ifdef EXACT_QUANT
408static void invert_quant(int improved_quant, short *quant,
409                               unsigned char *shift, short d)
410{
411    if(improved_quant)
412    {
413        unsigned t;
414        int l;
415        t = d;
416        for(l = 0; t > 1; l++)
417            t>>=1;
418        t = 1 + (1<<(16+l))/d;
419        *quant = (short)(t - (1<<16));
420        *shift = l;
421    }
422    else
423    {
424        *quant = (1 << 16) / d;
425        *shift = 0;
426    }
427}
428
429
430void vp8cx_init_quantizer(VP8_COMP *cpi)
431{
432    int i;
433    int quant_val;
434    int Q;
435
436    int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44,
437                          44, 44};
438
439    for (Q = 0; Q < QINDEX_RANGE; Q++)
440    {
441        /* dc values */
442        quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
443        cpi->Y1quant_fast[Q][0] = (1 << 16) / quant_val;
444        invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 0,
445                     cpi->Y1quant_shift[Q] + 0, quant_val);
446        cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
447        cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
448        cpi->common.Y1dequant[Q][0] = quant_val;
449        cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
450
451        quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
452        cpi->Y2quant_fast[Q][0] = (1 << 16) / quant_val;
453        invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 0,
454                     cpi->Y2quant_shift[Q] + 0, quant_val);
455        cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
456        cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
457        cpi->common.Y2dequant[Q][0] = quant_val;
458        cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
459
460        quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
461        cpi->UVquant_fast[Q][0] = (1 << 16) / quant_val;
462        invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 0,
463                     cpi->UVquant_shift[Q] + 0, quant_val);
464        cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
465        cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
466        cpi->common.UVdequant[Q][0] = quant_val;
467        cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
468
469        /* all the ac values = ; */
470        quant_val = vp8_ac_yquant(Q);
471        cpi->Y1quant_fast[Q][1] = (1 << 16) / quant_val;
472        invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 1,
473                     cpi->Y1quant_shift[Q] + 1, quant_val);
474        cpi->Y1zbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
475        cpi->Y1round[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
476        cpi->common.Y1dequant[Q][1] = quant_val;
477        cpi->zrun_zbin_boost_y1[Q][1] = (quant_val * zbin_boost[1]) >> 7;
478
479        quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
480        cpi->Y2quant_fast[Q][1] = (1 << 16) / quant_val;
481        invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 1,
482                     cpi->Y2quant_shift[Q] + 1, quant_val);
483        cpi->Y2zbin[Q][1] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
484        cpi->Y2round[Q][1] = (qrounding_factors_y2[Q] * quant_val) >> 7;
485        cpi->common.Y2dequant[Q][1] = quant_val;
486        cpi->zrun_zbin_boost_y2[Q][1] = (quant_val * zbin_boost[1]) >> 7;
487
488        quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
489        cpi->UVquant_fast[Q][1] = (1 << 16) / quant_val;
490        invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 1,
491                     cpi->UVquant_shift[Q] + 1, quant_val);
492        cpi->UVzbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
493        cpi->UVround[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
494        cpi->common.UVdequant[Q][1] = quant_val;
495        cpi->zrun_zbin_boost_uv[Q][1] = (quant_val * zbin_boost[1]) >> 7;
496
497        for (i = 2; i < 16; i++)
498        {
499            cpi->Y1quant_fast[Q][i] = cpi->Y1quant_fast[Q][1];
500            cpi->Y1quant[Q][i] = cpi->Y1quant[Q][1];
501            cpi->Y1quant_shift[Q][i] = cpi->Y1quant_shift[Q][1];
502            cpi->Y1zbin[Q][i] = cpi->Y1zbin[Q][1];
503            cpi->Y1round[Q][i] = cpi->Y1round[Q][1];
504            cpi->zrun_zbin_boost_y1[Q][i] = (cpi->common.Y1dequant[Q][1] *
505                                             zbin_boost[i]) >> 7;
506
507            cpi->Y2quant_fast[Q][i] = cpi->Y2quant_fast[Q][1];
508            cpi->Y2quant[Q][i] = cpi->Y2quant[Q][1];
509            cpi->Y2quant_shift[Q][i] = cpi->Y2quant_shift[Q][1];
510            cpi->Y2zbin[Q][i] = cpi->Y2zbin[Q][1];
511            cpi->Y2round[Q][i] = cpi->Y2round[Q][1];
512            cpi->zrun_zbin_boost_y2[Q][i] = (cpi->common.Y2dequant[Q][1] *
513                                             zbin_boost[i]) >> 7;
514
515            cpi->UVquant_fast[Q][i] = cpi->UVquant_fast[Q][1];
516            cpi->UVquant[Q][i] = cpi->UVquant[Q][1];
517            cpi->UVquant_shift[Q][i] = cpi->UVquant_shift[Q][1];
518            cpi->UVzbin[Q][i] = cpi->UVzbin[Q][1];
519            cpi->UVround[Q][i] = cpi->UVround[Q][1];
520            cpi->zrun_zbin_boost_uv[Q][i] = (cpi->common.UVdequant[Q][1] *
521                                             zbin_boost[i]) >> 7;
522        }
523    }
524}
525#else
526void vp8cx_init_quantizer(VP8_COMP *cpi)
527{
528    int i;
529    int quant_val;
530    int Q;
531
532    int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44};
533
534    for (Q = 0; Q < QINDEX_RANGE; Q++)
535    {
536        /* dc values */
537        quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
538        cpi->Y1quant[Q][0] = (1 << 16) / quant_val;
539        cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
540        cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
541        cpi->common.Y1dequant[Q][0] = quant_val;
542        cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
543
544        quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
545        cpi->Y2quant[Q][0] = (1 << 16) / quant_val;
546        cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
547        cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
548        cpi->common.Y2dequant[Q][0] = quant_val;
549        cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
550
551        quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
552        cpi->UVquant[Q][0] = (1 << 16) / quant_val;
553        cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
554        cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
555        cpi->common.UVdequant[Q][0] = quant_val;
556        cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
557
558        /* all the ac values = ; */
559        for (i = 1; i < 16; i++)
560        {
561            int rc = vp8_default_zig_zag1d[i];
562
563            quant_val = vp8_ac_yquant(Q);
564            cpi->Y1quant[Q][rc] = (1 << 16) / quant_val;
565            cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
566            cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
567            cpi->common.Y1dequant[Q][rc] = quant_val;
568            cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7;
569
570            quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
571            cpi->Y2quant[Q][rc] = (1 << 16) / quant_val;
572            cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
573            cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7;
574            cpi->common.Y2dequant[Q][rc] = quant_val;
575            cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7;
576
577            quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
578            cpi->UVquant[Q][rc] = (1 << 16) / quant_val;
579            cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
580            cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
581            cpi->common.UVdequant[Q][rc] = quant_val;
582            cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
583        }
584    }
585}
586#endif
587
588#define ZBIN_EXTRA_Y \
589    (( cpi->common.Y1dequant[QIndex][1] *  \
590    ( cpi->zbin_over_quant +  \
591      cpi->zbin_mode_boost +  \
592      x->act_zbin_adj ) ) >> 7)
593
594#define ZBIN_EXTRA_UV \
595    (( cpi->common.UVdequant[QIndex][1] *  \
596    ( cpi->zbin_over_quant +  \
597      cpi->zbin_mode_boost +  \
598      x->act_zbin_adj ) ) >> 7)
599
600#define ZBIN_EXTRA_Y2 \
601    (( cpi->common.Y2dequant[QIndex][1] *  \
602    ( (cpi->zbin_over_quant / 2) +  \
603       cpi->zbin_mode_boost +  \
604       x->act_zbin_adj ) ) >> 7)
605
606void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x, int ok_to_skip)
607{
608    int i;
609    int QIndex;
610    MACROBLOCKD *xd = &x->e_mbd;
611    int zbin_extra;
612
613    /* Select the baseline MB Q index. */
614    if (xd->segmentation_enabled)
615    {
616        /* Abs Value */
617        if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
618            QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
619        /* Delta Value */
620        else
621        {
622            QIndex = cpi->common.base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
623            /* Clamp to valid range */
624            QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;
625        }
626    }
627    else
628        QIndex = cpi->common.base_qindex;
629
630    /* This initialization should be called at least once. Use ok_to_skip to
631     * decide if it is ok to skip.
632     * Before encoding a frame, this function is always called with ok_to_skip
633     * =0, which means no skiping of calculations. The "last" values are
634     * initialized at that time.
635     */
636    if (!ok_to_skip || QIndex != x->q_index)
637    {
638
639        xd->dequant_y1_dc[0] = 1;
640        xd->dequant_y1[0] = cpi->common.Y1dequant[QIndex][0];
641        xd->dequant_y2[0] = cpi->common.Y2dequant[QIndex][0];
642        xd->dequant_uv[0] = cpi->common.UVdequant[QIndex][0];
643
644        for (i = 1; i < 16; i++)
645        {
646            xd->dequant_y1_dc[i] =
647            xd->dequant_y1[i] = cpi->common.Y1dequant[QIndex][1];
648            xd->dequant_y2[i] = cpi->common.Y2dequant[QIndex][1];
649            xd->dequant_uv[i] = cpi->common.UVdequant[QIndex][1];
650        }
651#if 1
652        /*TODO:  Remove dequant from BLOCKD.  This is a temporary solution until
653         * the quantizer code uses a passed in pointer to the dequant constants.
654         * This will also require modifications to the x86 and neon assembly.
655         * */
656        for (i = 0; i < 16; i++)
657            x->e_mbd.block[i].dequant = xd->dequant_y1;
658        for (i = 16; i < 24; i++)
659            x->e_mbd.block[i].dequant = xd->dequant_uv;
660        x->e_mbd.block[24].dequant = xd->dequant_y2;
661#endif
662
663        /* Y */
664        zbin_extra = ZBIN_EXTRA_Y;
665
666        for (i = 0; i < 16; i++)
667        {
668            x->block[i].quant = cpi->Y1quant[QIndex];
669            x->block[i].quant_fast = cpi->Y1quant_fast[QIndex];
670            x->block[i].quant_shift = cpi->Y1quant_shift[QIndex];
671            x->block[i].zbin = cpi->Y1zbin[QIndex];
672            x->block[i].round = cpi->Y1round[QIndex];
673            x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
674            x->block[i].zbin_extra = (short)zbin_extra;
675        }
676
677        /* UV */
678        zbin_extra = ZBIN_EXTRA_UV;
679
680        for (i = 16; i < 24; i++)
681        {
682            x->block[i].quant = cpi->UVquant[QIndex];
683            x->block[i].quant_fast = cpi->UVquant_fast[QIndex];
684            x->block[i].quant_shift = cpi->UVquant_shift[QIndex];
685            x->block[i].zbin = cpi->UVzbin[QIndex];
686            x->block[i].round = cpi->UVround[QIndex];
687            x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
688            x->block[i].zbin_extra = (short)zbin_extra;
689        }
690
691        /* Y2 */
692        zbin_extra = ZBIN_EXTRA_Y2;
693
694        x->block[24].quant_fast = cpi->Y2quant_fast[QIndex];
695        x->block[24].quant = cpi->Y2quant[QIndex];
696        x->block[24].quant_shift = cpi->Y2quant_shift[QIndex];
697        x->block[24].zbin = cpi->Y2zbin[QIndex];
698        x->block[24].round = cpi->Y2round[QIndex];
699        x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex];
700        x->block[24].zbin_extra = (short)zbin_extra;
701
702        /* save this macroblock QIndex for vp8_update_zbin_extra() */
703        x->q_index = QIndex;
704
705        cpi->last_zbin_over_quant = cpi->zbin_over_quant;
706        cpi->last_zbin_mode_boost = cpi->zbin_mode_boost;
707        x->last_act_zbin_adj = x->act_zbin_adj;
708
709
710
711    }
712    else if(cpi->last_zbin_over_quant != cpi->zbin_over_quant
713            || cpi->last_zbin_mode_boost != cpi->zbin_mode_boost
714            || x->last_act_zbin_adj != x->act_zbin_adj)
715    {
716        /* Y */
717        zbin_extra = ZBIN_EXTRA_Y;
718
719        for (i = 0; i < 16; i++)
720            x->block[i].zbin_extra = (short)zbin_extra;
721
722        /* UV */
723        zbin_extra = ZBIN_EXTRA_UV;
724
725        for (i = 16; i < 24; i++)
726            x->block[i].zbin_extra = (short)zbin_extra;
727
728        /* Y2 */
729        zbin_extra = ZBIN_EXTRA_Y2;
730        x->block[24].zbin_extra = (short)zbin_extra;
731
732        cpi->last_zbin_over_quant = cpi->zbin_over_quant;
733        cpi->last_zbin_mode_boost = cpi->zbin_mode_boost;
734        x->last_act_zbin_adj = x->act_zbin_adj;
735    }
736}
737
738void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x)
739{
740    int i;
741    int QIndex = x->q_index;
742    int zbin_extra;
743
744    /* Y */
745    zbin_extra = ZBIN_EXTRA_Y;
746
747    for (i = 0; i < 16; i++)
748        x->block[i].zbin_extra = (short)zbin_extra;
749
750    /* UV */
751    zbin_extra = ZBIN_EXTRA_UV;
752
753    for (i = 16; i < 24; i++)
754        x->block[i].zbin_extra = (short)zbin_extra;
755
756    /* Y2 */
757    zbin_extra = ZBIN_EXTRA_Y2;
758    x->block[24].zbin_extra = (short)zbin_extra;
759}
760#undef ZBIN_EXTRA_Y
761#undef ZBIN_EXTRA_UV
762#undef ZBIN_EXTRA_Y2
763
764void vp8cx_frame_init_quantizer(VP8_COMP *cpi)
765{
766    /* Clear Zbin mode boost for default case */
767    cpi->zbin_mode_boost = 0;
768
769    /* MB level quantizer setup */
770    vp8cx_mb_init_quantizer(cpi, &cpi->mb, 0);
771}
772
773
774void vp8_set_quantizer(struct VP8_COMP *cpi, int Q)
775{
776    VP8_COMMON *cm = &cpi->common;
777    MACROBLOCKD *mbd = &cpi->mb.e_mbd;
778    int update = 0;
779    int new_delta_q;
780    cm->base_qindex = Q;
781
782    /* if any of the delta_q values are changing update flag has to be set */
783    /* currently only y2dc_delta_q may change */
784
785    cm->y1dc_delta_q = 0;
786    cm->y2ac_delta_q = 0;
787    cm->uvdc_delta_q = 0;
788    cm->uvac_delta_q = 0;
789
790    if (Q < 4)
791    {
792        new_delta_q = 4-Q;
793    }
794    else
795        new_delta_q = 0;
796
797    update |= cm->y2dc_delta_q != new_delta_q;
798    cm->y2dc_delta_q = new_delta_q;
799
800
801    /* Set Segment specific quatizers */
802    mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
803    mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
804    mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
805    mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
806
807    /* quantizer has to be reinitialized for any delta_q changes */
808    if(update)
809        vp8cx_init_quantizer(cpi);
810
811}
812