summaryrefslogtreecommitdiffstats
path: root/libvpx/vp8/encoder/quantize.c
blob: fda997ff655c8b2338d2a71ba4ad326f0c987d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */


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

#include "onyx_int.h"
#include "quantize.h"
#include "vp8/common/quant_common.h"

#define EXACT_QUANT

#ifdef EXACT_FASTQUANT
void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
{
    int i, rc, eob;
    int zbin;
    int x, y, z, sz;
    short *coeff_ptr       = b->coeff;
    short *zbin_ptr        = b->zbin;
    short *round_ptr       = b->round;
    short *quant_ptr       = b->quant_fast;
    unsigned char *quant_shift_ptr = b->quant_shift;
    short *qcoeff_ptr      = d->qcoeff;
    short *dqcoeff_ptr     = d->dqcoeff;
    short *dequant_ptr     = d->dequant;

    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)
        {
            x += round_ptr[rc];
            y  = ((((x * quant_ptr[rc]) >> 16) + x)
                 * quant_shift_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 */
            }
        }
    }
    *d->eob = (char)(eob + 1);
}

#else

void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
{
    int i, rc, eob;
    int x, y, z, sz;
    short *coeff_ptr   = b->coeff;
    short *round_ptr   = b->round;
    short *quant_ptr   = b->quant_fast;
    short *qcoeff_ptr  = d->qcoeff;
    short *dqcoeff_ptr = d->dqcoeff;
    short *dequant_ptr = d->dequant;

    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) */

        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 */
        }
    }
    *d->eob = (char)(eob + 1);
}

#endif

#ifdef EXACT_QUANT
void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d)
{
    int i, rc, eob;
    int zbin;
    int x, y, z, sz;
    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;

    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] + *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)
        {
            x += round_ptr[rc];
            y  = ((((x * quant_ptr[rc]) >> 16) + x)
                 * quant_shift_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; /* reset zero runlength */
            }
        }
    }

    *d->eob = (char)(eob + 1);
}

/* Perform regular quantization, with unbiased rounding and no zero bin. */
void vp8_strict_quantize_b_c(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;

    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;
    eob = - 1;
    vpx_memset(qcoeff_ptr, 0, 32);
    vpx_memset(dqcoeff_ptr, 0, 32);
    for (i = 0; i < 16; i++)
    {
        int dq;
        int rounding;

        /*TODO: These arrays should be stored in zig-zag order.*/
        rc = vp8_default_zig_zag1d[i];
        z = coeff_ptr[rc];
        dq = dequant_ptr[rc];
        rounding = dq >> 1;
        /* Sign of z. */
        sz = -(z < 0);
        x = (z + sz) ^ sz;
        x += rounding;
        if (x >= dq)
        {
            /* Quantize x. */
            y  = ((((x * quant_ptr[rc]) >> 16) + x) * quant_shift_ptr[rc]) >> 16;
            /* 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 = (char)(eob + 1);
}

#else

void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d)
{
    int i, rc, eob;
    int zbin;
    int x, y, z, sz;
    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;

    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] + *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 zrl */
            }
        }
    }

    *d->eob = (char)(eob + 1);
}

#endif

void vp8_quantize_mby_c(MACROBLOCK *x)
{
    int i;
    int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);

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

    if(has_2nd_order)
        x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
}

void vp8_quantize_mb_c(MACROBLOCK *x)
{
    int i;
    int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);

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


void vp8_quantize_mbuv_c(MACROBLOCK *x)
{
    int i;

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

/* quantize_b_pair function pointer in MACROBLOCK structure is set to one of
 * these two C functions if corresponding optimized routine is not available.
 * NEON optimized version implements currently the fast quantization for pair
 * of blocks. */
void vp8_regular_quantize_b_pair(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
{
    vp8_regular_quantize_b(b1, d1);
    vp8_regular_quantize_b(b2, d2);
}

void vp8_fast_quantize_b_pair_c(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
{
    vp8_fast_quantize_b_c(b1, d1);
    vp8_fast_quantize_b_c(b2, d2);
}


static const int qrounding_factors[129] =
{
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48
};


static const int qzbin_factors[129] =
{
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80
};


static const int qrounding_factors_y2[129] =
{
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48, 48, 48, 48, 48, 48, 48, 48,
    48
};


static const int qzbin_factors_y2[129] =
{
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    84, 84, 84, 84, 84, 84, 84, 84,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80, 80, 80, 80, 80, 80, 80, 80,
    80
};


#define EXACT_QUANT
#ifdef EXACT_QUANT
static void invert_quant(int improved_quant, short *quant,
                         short *shift, short d)
{
    if(improved_quant)
    {
        unsigned t;
        int l;
        t = d;
        for(l = 0; t > 1; l++)
            t>>=1;
        t = 1 + (1<<(16+l))/d;
        *quant = (short)(t - (1<<16));
        *shift = l;
        /* use multiplication and constant shift by 16 */
        *shift = 1 << (16 - *shift);
    }
    else
    {
        *quant = (1 << 16) / d;
        *shift = 0;
        /* use multiplication and constant shift by 16 */
        *shift = 1 << (16 - *shift);
    }
}


void vp8cx_init_quantizer(VP8_COMP *cpi)
{
    int i;
    int quant_val;
    int Q;

    int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44,
                          44, 44};

    for (Q = 0; Q < QINDEX_RANGE; Q++)
    {
        /* dc values */
        quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
        cpi->Y1quant_fast[Q][0] = (1 << 16) / quant_val;
        invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 0,
                     cpi->Y1quant_shift[Q] + 0, quant_val);
        cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
        cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
        cpi->common.Y1dequant[Q][0] = quant_val;
        cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;

        quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
        cpi->Y2quant_fast[Q][0] = (1 << 16) / quant_val;
        invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 0,
                     cpi->Y2quant_shift[Q] + 0, quant_val);
        cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
        cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
        cpi->common.Y2dequant[Q][0] = quant_val;
        cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;

        quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
        cpi->UVquant_fast[Q][0] = (1 << 16) / quant_val;
        invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 0,
                     cpi->UVquant_shift[Q] + 0, quant_val);
        cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
        cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
        cpi->common.UVdequant[Q][0] = quant_val;
        cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;

        /* all the ac values = ; */
        quant_val = vp8_ac_yquant(Q);
        cpi->Y1quant_fast[Q][1] = (1 << 16) / quant_val;
        invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 1,
                     cpi->Y1quant_shift[Q] + 1, quant_val);
        cpi->Y1zbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
        cpi->Y1round[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
        cpi->common.Y1dequant[Q][1] = quant_val;
        cpi->zrun_zbin_boost_y1[Q][1] = (quant_val * zbin_boost[1]) >> 7;

        quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
        cpi->Y2quant_fast[Q][1] = (1 << 16) / quant_val;
        invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 1,
                     cpi->Y2quant_shift[Q] + 1, quant_val);
        cpi->Y2zbin[Q][1] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
        cpi->Y2round[Q][1] = (qrounding_factors_y2[Q] * quant_val) >> 7;
        cpi->common.Y2dequant[Q][1] = quant_val;
        cpi->zrun_zbin_boost_y2[Q][1] = (quant_val * zbin_boost[1]) >> 7;

        quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
        cpi->UVquant_fast[Q][1] = (1 << 16) / quant_val;
        invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 1,
                     cpi->UVquant_shift[Q] + 1, quant_val);
        cpi->UVzbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
        cpi->UVround[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
        cpi->common.UVdequant[Q][1] = quant_val;
        cpi->zrun_zbin_boost_uv[Q][1] = (quant_val * zbin_boost[1]) >> 7;

        for (i = 2; i < 16; i++)
        {
            cpi->Y1quant_fast[Q][i] = cpi->Y1quant_fast[Q][1];
            cpi->Y1quant[Q][i] = cpi->Y1quant[Q][1];
            cpi->Y1quant_shift[Q][i] = cpi->Y1quant_shift[Q][1];
            cpi->Y1zbin[Q][i] = cpi->Y1zbin[Q][1];
            cpi->Y1round[Q][i] = cpi->Y1round[Q][1];
            cpi->zrun_zbin_boost_y1[Q][i] = (cpi->common.Y1dequant[Q][1] *
                                             zbin_boost[i]) >> 7;

            cpi->Y2quant_fast[Q][i] = cpi->Y2quant_fast[Q][1];
            cpi->Y2quant[Q][i] = cpi->Y2quant[Q][1];
            cpi->Y2quant_shift[Q][i] = cpi->Y2quant_shift[Q][1];
            cpi->Y2zbin[Q][i] = cpi->Y2zbin[Q][1];
            cpi->Y2round[Q][i] = cpi->Y2round[Q][1];
            cpi->zrun_zbin_boost_y2[Q][i] = (cpi->common.Y2dequant[Q][1] *
                                             zbin_boost[i]) >> 7;

            cpi->UVquant_fast[Q][i] = cpi->UVquant_fast[Q][1];
            cpi->UVquant[Q][i] = cpi->UVquant[Q][1];
            cpi->UVquant_shift[Q][i] = cpi->UVquant_shift[Q][1];
            cpi->UVzbin[Q][i] = cpi->UVzbin[Q][1];
            cpi->UVround[Q][i] = cpi->UVround[Q][1];
            cpi->zrun_zbin_boost_uv[Q][i] = (cpi->common.UVdequant[Q][1] *
                                             zbin_boost[i]) >> 7;
        }
    }
}
#else
void vp8cx_init_quantizer(VP8_COMP *cpi)
{
    int i;
    int quant_val;
    int Q;

    int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44};

    for (Q = 0; Q < QINDEX_RANGE; Q++)
    {
        /* dc values */
        quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
        cpi->Y1quant[Q][0] = (1 << 16) / quant_val;
        cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
        cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
        cpi->common.Y1dequant[Q][0] = quant_val;
        cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;

        quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
        cpi->Y2quant[Q][0] = (1 << 16) / quant_val;
        cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
        cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
        cpi->common.Y2dequant[Q][0] = quant_val;
        cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;

        quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
        cpi->UVquant[Q][0] = (1 << 16) / quant_val;
        cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
        cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
        cpi->common.UVdequant[Q][0] = quant_val;
        cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;

        /* all the ac values = ; */
        for (i = 1; i < 16; i++)
        {
            int rc = vp8_default_zig_zag1d[i];

            quant_val = vp8_ac_yquant(Q);
            cpi->Y1quant[Q][rc] = (1 << 16) / quant_val;
            cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
            cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
            cpi->common.Y1dequant[Q][rc] = quant_val;
            cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7;

            quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
            cpi->Y2quant[Q][rc] = (1 << 16) / quant_val;
            cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
            cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7;
            cpi->common.Y2dequant[Q][rc] = quant_val;
            cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7;

            quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
            cpi->UVquant[Q][rc] = (1 << 16) / quant_val;
            cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
            cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
            cpi->common.UVdequant[Q][rc] = quant_val;
            cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
        }
    }
}
#endif

#define ZBIN_EXTRA_Y \
    (( cpi->common.Y1dequant[QIndex][1] *  \
    ( x->zbin_over_quant +  \
      x->zbin_mode_boost +  \
      x->act_zbin_adj ) ) >> 7)

#define ZBIN_EXTRA_UV \
    (( cpi->common.UVdequant[QIndex][1] *  \
    ( x->zbin_over_quant +  \
      x->zbin_mode_boost +  \
      x->act_zbin_adj ) ) >> 7)

#define ZBIN_EXTRA_Y2 \
    (( cpi->common.Y2dequant[QIndex][1] *  \
    ( (x->zbin_over_quant / 2) +  \
       x->zbin_mode_boost +  \
       x->act_zbin_adj ) ) >> 7)

void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x, int ok_to_skip)
{
    int i;
    int QIndex;
    MACROBLOCKD *xd = &x->e_mbd;
    int zbin_extra;

    /* Select the baseline MB Q index. */
    if (xd->segmentation_enabled)
    {
        /* Abs Value */
        if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
            QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
        /* Delta Value */
        else
        {
            QIndex = cpi->common.base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
            /* Clamp to valid range */
            QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;
        }
    }
    else
        QIndex = cpi->common.base_qindex;

    /* This initialization should be called at least once. Use ok_to_skip to
     * decide if it is ok to skip.
     * Before encoding a frame, this function is always called with ok_to_skip
     * =0, which means no skiping of calculations. The "last" values are
     * initialized at that time.
     */
    if (!ok_to_skip || QIndex != x->q_index)
    {

        xd->dequant_y1_dc[0] = 1;
        xd->dequant_y1[0] = cpi->common.Y1dequant[QIndex][0];
        xd->dequant_y2[0] = cpi->common.Y2dequant[QIndex][0];
        xd->dequant_uv[0] = cpi->common.UVdequant[QIndex][0];

        for (i = 1; i < 16; i++)
        {
            xd->dequant_y1_dc[i] =
            xd->dequant_y1[i] = cpi->common.Y1dequant[QIndex][1];
            xd->dequant_y2[i] = cpi->common.Y2dequant[QIndex][1];
            xd->dequant_uv[i] = cpi->common.UVdequant[QIndex][1];
        }
#if 1
        /*TODO:  Remove dequant from BLOCKD.  This is a temporary solution until
         * the quantizer code uses a passed in pointer to the dequant constants.
         * This will also require modifications to the x86 and neon assembly.
         * */
        for (i = 0; i < 16; i++)
            x->e_mbd.block[i].dequant = xd->dequant_y1;
        for (i = 16; i < 24; i++)
            x->e_mbd.block[i].dequant = xd->dequant_uv;
        x->e_mbd.block[24].dequant = xd->dequant_y2;
#endif

        /* Y */
        zbin_extra = ZBIN_EXTRA_Y;

        for (i = 0; i < 16; i++)
        {
            x->block[i].quant = cpi->Y1quant[QIndex];
            x->block[i].quant_fast = cpi->Y1quant_fast[QIndex];
            x->block[i].quant_shift = cpi->Y1quant_shift[QIndex];
            x->block[i].zbin = cpi->Y1zbin[QIndex];
            x->block[i].round = cpi->Y1round[QIndex];
            x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
            x->block[i].zbin_extra = (short)zbin_extra;
        }

        /* UV */
        zbin_extra = ZBIN_EXTRA_UV;

        for (i = 16; i < 24; i++)
        {
            x->block[i].quant = cpi->UVquant[QIndex];
            x->block[i].quant_fast = cpi->UVquant_fast[QIndex];
            x->block[i].quant_shift = cpi->UVquant_shift[QIndex];
            x->block[i].zbin = cpi->UVzbin[QIndex];
            x->block[i].round = cpi->UVround[QIndex];
            x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
            x->block[i].zbin_extra = (short)zbin_extra;
        }

        /* Y2 */
        zbin_extra = ZBIN_EXTRA_Y2;

        x->block[24].quant_fast = cpi->Y2quant_fast[QIndex];
        x->block[24].quant = cpi->Y2quant[QIndex];
        x->block[24].quant_shift = cpi->Y2quant_shift[QIndex];
        x->block[24].zbin = cpi->Y2zbin[QIndex];
        x->block[24].round = cpi->Y2round[QIndex];
        x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex];
        x->block[24].zbin_extra = (short)zbin_extra;

        /* save this macroblock QIndex for vp8_update_zbin_extra() */
        x->q_index = QIndex;

        x->last_zbin_over_quant = x->zbin_over_quant;
        x->last_zbin_mode_boost = x->zbin_mode_boost;
        x->last_act_zbin_adj = x->act_zbin_adj;



    }
    else if(x->last_zbin_over_quant != x->zbin_over_quant
            || x->last_zbin_mode_boost != x->zbin_mode_boost
            || x->last_act_zbin_adj != x->act_zbin_adj)
    {
        /* Y */
        zbin_extra = ZBIN_EXTRA_Y;

        for (i = 0; i < 16; i++)
            x->block[i].zbin_extra = (short)zbin_extra;

        /* UV */
        zbin_extra = ZBIN_EXTRA_UV;

        for (i = 16; i < 24; i++)
            x->block[i].zbin_extra = (short)zbin_extra;

        /* Y2 */
        zbin_extra = ZBIN_EXTRA_Y2;
        x->block[24].zbin_extra = (short)zbin_extra;

        x->last_zbin_over_quant = x->zbin_over_quant;
        x->last_zbin_mode_boost = x->zbin_mode_boost;
        x->last_act_zbin_adj = x->act_zbin_adj;
    }
}

void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x)
{
    int i;
    int QIndex = x->q_index;
    int zbin_extra;

    /* Y */
    zbin_extra = ZBIN_EXTRA_Y;

    for (i = 0; i < 16; i++)
        x->block[i].zbin_extra = (short)zbin_extra;

    /* UV */
    zbin_extra = ZBIN_EXTRA_UV;

    for (i = 16; i < 24; i++)
        x->block[i].zbin_extra = (short)zbin_extra;

    /* Y2 */
    zbin_extra = ZBIN_EXTRA_Y2;
    x->block[24].zbin_extra = (short)zbin_extra;
}
#undef ZBIN_EXTRA_Y
#undef ZBIN_EXTRA_UV
#undef ZBIN_EXTRA_Y2

void vp8cx_frame_init_quantizer(VP8_COMP *cpi)
{
    /* Clear Zbin mode boost for default case */
    cpi->mb.zbin_mode_boost = 0;

    /* MB level quantizer setup */
    vp8cx_mb_init_quantizer(cpi, &cpi->mb, 0);
}


void vp8_set_quantizer(struct VP8_COMP *cpi, int Q)
{
    VP8_COMMON *cm = &cpi->common;
    MACROBLOCKD *mbd = &cpi->mb.e_mbd;
    int update = 0;
    int new_delta_q;
    cm->base_qindex = Q;

    /* if any of the delta_q values are changing update flag has to be set */
    /* currently only y2dc_delta_q may change */

    cm->y1dc_delta_q = 0;
    cm->y2ac_delta_q = 0;
    cm->uvdc_delta_q = 0;
    cm->uvac_delta_q = 0;

    if (Q < 4)
    {
        new_delta_q = 4-Q;
    }
    else
        new_delta_q = 0;

    update |= cm->y2dc_delta_q != new_delta_q;
    cm->y2dc_delta_q = new_delta_q;


    /* Set Segment specific quatizers */
    mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
    mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
    mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
    mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];

    /* quantizer has to be reinitialized for any delta_q changes */
    if(update)
        vp8cx_init_quantizer(cpi);

}