summaryrefslogtreecommitdiff
path: root/vp8/encoder/quantize.c
blob: 78f7dd4c6b48f8e90758ea766c8afd08773b2bd1 (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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
/*
 *  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"

//#if CONFIG_SEGFEATURES
#include "vp8/common/seg_common.h"

#ifdef ENC_DEBUG
extern int enc_debug;
#endif

#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];                // 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 = 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;
#if CONFIG_T8X8

    vpx_memset(qcoeff_ptr, 0, 32);
    vpx_memset(dqcoeff_ptr, 0, 32);
#endif
    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 = eob + 1;
}

#endif

#ifdef EXACT_QUANT
void vp8_regular_quantize_b(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;
    unsigned char *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;

//#if CONFIG_SEGFEATURES
    for (i = 0; i < b->eob_max_offset; 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];                // 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 = eob + 1;
}

/* 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;
    unsigned char *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 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;
}

#else

void vp8_regular_quantize_b(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];

        //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;
}

#endif //EXACT_QUANT


void vp8_quantize_mby_c(MACROBLOCK *x)
{
    int i;
    int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
#if CONFIG_I8X8
        && x->e_mbd.mode_info_context->mbmi.mode != I8X8_PRED
#endif
        && 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
#if CONFIG_I8X8
        && x->e_mbd.mode_info_context->mbmi.mode != I8X8_PRED
#endif
        && 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]);
}

#if CONFIG_T8X8

#ifdef EXACT_FASTQUANT
void vp8_fast_quantize_b_2x2_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;
  short *quant_shift_ptr = b->quant_shift;
  short *qcoeff_ptr = d->qcoeff;
  short *dqcoeff_ptr = d->dqcoeff;
  short *dequant_ptr = d->dequant;
  //double q2nd = 4;


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

  eob = -1;

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

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

    if (x >= zbin)
    {
      //x += (round_ptr[rc]/q2nd);
      x += (round_ptr[rc]);
      //y  = ((int)((int)(x * quant_ptr[rc] * q2nd) >> 16) + x)
      //    >> quant_shift_ptr[rc];                // quantize (x)
      y  = ((int)((int)(x * quant_ptr[rc]) >> 16) + x)
          >> quant_shift_ptr[rc];                // 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 = eob + 1;
}

void vp8_fast_quantize_b_8x8_c(BLOCK *b, BLOCKD *d)// only ac and dc difference, no difference among ac
{
  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;
  short *quant_shift_ptr = b->quant_shift;
  short *qcoeff_ptr = d->qcoeff;
  short *dqcoeff_ptr = d->dqcoeff;
  short *dequant_ptr = d->dequant;
  //double q1st = 2;
  vpx_memset(qcoeff_ptr, 0, 64*sizeof(short));
  vpx_memset(dqcoeff_ptr, 0, 64*sizeof(short));

  eob = -1;

  for (i = 0; i < 64; i++)
  {
    rc   = vp8_default_zig_zag1d_8x8[i];
    z    = coeff_ptr[rc];
    //zbin = zbin_ptr[rc!=0]/q1st ;
    zbin = zbin_ptr[rc!=0] ;

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

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

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

#else

void vp8_fast_quantize_b_2x2_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;
  short *qcoeff_ptr = d->qcoeff;
  short *dqcoeff_ptr = d->dqcoeff;
  short *dequant_ptr = d->dequant;
  //double q2nd = 4;
  vpx_memset(qcoeff_ptr, 0, 32);
  vpx_memset(dqcoeff_ptr, 0, 32);

  eob = -1;

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

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

    if (x >= zbin)
    {
      //y  = ((int)((x + round_ptr[rc]/q2nd) * quant_ptr[rc] * q2nd)) >> 16; // quantize (x)
      y  = ((int)((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] / q2nd;        // dequantized value
      dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value
#if CONFIG_EXTEND_QRANGE
      dqcoeff_ptr[rc] = (dqcoeff_ptr[rc]+2)>>2;
#endif

      if (y)
      {
        eob = i;                                // last nonzero coeffs
      }
    }
  }
  d->eob = eob + 1;
  //if (d->eob > 4) printf("Flag Fast 2 (%d)\n", d->eob);
}

void vp8_fast_quantize_b_8x8_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;
  short *qcoeff_ptr = d->qcoeff;
  short *dqcoeff_ptr = d->dqcoeff;
  short *dequant_ptr = d->dequant;
  //double q1st = 2;
  vpx_memset(qcoeff_ptr, 0, 64*sizeof(short));
  vpx_memset(dqcoeff_ptr, 0, 64*sizeof(short));

  eob = -1;

  for (i = 0; i < 64; i++)
  {

    rc   = vp8_default_zig_zag1d_8x8[i];
    z    = coeff_ptr[rc];
    //zbin = zbin_ptr[rc!=0]/q1st ;
    zbin = zbin_ptr[rc!=0] ;

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

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

#if CONFIG_EXTEND_QRANGE
      dqcoeff_ptr[rc] = (dqcoeff_ptr[rc]+2)>>2;
#endif


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

#endif //EXACT_FASTQUANT

#ifdef EXACT_QUANT
void vp8_regular_quantize_b_2x2(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;
  unsigned char *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;
  //double q2nd = 4;
  vpx_memset(qcoeff_ptr, 0, 32);
  vpx_memset(dqcoeff_ptr, 0, 32);

  eob = -1;

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

    //zbin = (zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value)/q2nd;
    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]/q2nd);
      x += (round_ptr[rc]);
      y  = ((int)((int)(x * quant_ptr[rc]) >> 16) + x)
           >> quant_shift_ptr[rc];                // quantize (x)
      x  = (y ^ sz) - sz;                         // get the sign back
      qcoeff_ptr[rc]  = x;                         // write to destination
      //dqcoeff_ptr[rc] = x * dequant_ptr[rc]/q2nd;        // dequantized value
      dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value

#if CONFIG_EXTEND_QRANGE
      dqcoeff_ptr[rc] = (dqcoeff_ptr[rc]+2)>>2;
#endif


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

  d->eob = eob + 1;
}

void vp8_regular_quantize_b_8x8(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;
  unsigned char *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;
  //double q1st = 2;

  vpx_memset(qcoeff_ptr, 0, 64*sizeof(short));
  vpx_memset(dqcoeff_ptr, 0, 64*sizeof(short));

  eob = -1;

  for (i = 0; i < 64; i++)
  {

    rc   = vp8_default_zig_zag1d_8x8[i];
    z    = coeff_ptr[rc];

    //zbin = (zbin_ptr[rc!=0] + *zbin_boost_ptr + zbin_oq_value)/q1st;
    zbin = (zbin_ptr[rc!=0] + *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!=0]/q1st);
      //y  = ((int)(((int)(x * quant_ptr[rc!=0] * q1st) >> 16) + x))
      //    >> quant_shift_ptr[rc!=0];                // quantize (x)
      x += (round_ptr[rc!=0]);
      y  = ((int)(((int)(x * quant_ptr[rc!=0]) >> 16) + x))
          >> quant_shift_ptr[rc!=0];                // quantize (x)
      x  = (y ^ sz) - sz;                         // get the sign back
      qcoeff_ptr[rc]  = x;                         // write to destination
      //dqcoeff_ptr[rc] = x * dequant_ptr[rc!=0] / q1st;        // dequantized value
      dqcoeff_ptr[rc] = x * dequant_ptr[rc!=0];        // dequantized value
#if CONFIG_EXTEND_QRANGE
      dqcoeff_ptr[rc] = (dqcoeff_ptr[rc]+2)>>2;
#endif

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

  d->eob = eob + 1;
}

void vp8_strict_quantize_b_2x2(BLOCK *b, BLOCKD *d)
{
  int i;
  int rc;
  int eob;
  int x;
  int y;
  int z;
  int sz;
  short *coeff_ptr;
  short *quant_ptr;
  unsigned char *quant_shift_ptr;
  short *qcoeff_ptr;
  short *dqcoeff_ptr;
  short *dequant_ptr;
  //double q2nd = 4;
  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 < 4; 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];
    //z = z * q2nd;
    //dq = dequant_ptr[rc]/q2nd;
    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;
}

void vp8_strict_quantize_b_8x8(BLOCK *b, BLOCKD *d)
{
  int i;
  int rc;
  int eob;
  int x;
  int y;
  int z;
  int sz;
  short *coeff_ptr;
  short *quant_ptr;
  unsigned char *quant_shift_ptr;
  short *qcoeff_ptr;
  short *dqcoeff_ptr;
  short *dequant_ptr;
  //double q1st = 2;
  printf("call strict quantizer\n");
  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, 64*sizeof(short));
  vpx_memset(dqcoeff_ptr, 0, 64*sizeof(short));
  for (i = 0; i < 64; i++)
  {
    int dq;
    int round;

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

#else

void vp8_regular_quantize_b_2x2(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;
  //double q2nd = 4;
  vpx_memset(qcoeff_ptr, 0, 32);
  vpx_memset(dqcoeff_ptr, 0, 32);

  eob = -1;
  for (i = 0; i < 4; i++)
  {
    rc   = vp8_default_zig_zag1d[i];
    z    = coeff_ptr[rc];
    //zbin = (zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value)/q2nd;
    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]/q2nd) * quant_ptr[rc]*q2nd)) >> 16; // quantize (x)
      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]/q2nd;        // dequantized value
      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;
}

void vp8_regular_quantize_b_8x8(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;
  //double q1st = 2;
  vpx_memset(qcoeff_ptr, 0, 64*sizeof(short));
  vpx_memset(dqcoeff_ptr, 0, 64*sizeof(short));

  eob = -1;
  for (i = 0; i < 64; i++)
  {

    rc   = vp8_default_zig_zag1d_8x8[i];
    z    = coeff_ptr[rc];
    //zbin = (zbin_ptr[rc!=0] + *zbin_boost_ptr + zbin_oq_value)/q1st;
    zbin = (zbin_ptr[rc!=0] + *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!=0]/q1st) * quant_ptr[rc!=0] * q1st) >> 16;
      y  = ((x + round_ptr[rc!=0]) * quant_ptr[rc!=0]) >> 16;
      x  = (y ^ sz) - sz;                         // get the sign back
      qcoeff_ptr[rc]  = x;                         // write to destination
      //dqcoeff_ptr[rc] = x * dequant_ptr[rc!=0]/q1st;        // dequantized value
      dqcoeff_ptr[rc] = x * dequant_ptr[rc!=0];        // dequantized value

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

#endif  //EXACT_QUANT

void vp8_quantize_mby_8x8(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->e_mbd.block[i].eob = 0;
  }
  x->e_mbd.block[24].eob = 0;
  for (i = 0; i < 16; i+=4)
    x->quantize_b_8x8(&x->block[i], &x->e_mbd.block[i]);

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

}

void vp8_quantize_mb_8x8(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 < 25; i ++)
  {
    x->e_mbd.block[i].eob = 0;
  }
  for (i = 0; i < 24; i+=4)
    x->quantize_b_8x8(&x->block[i], &x->e_mbd.block[i]);

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

void vp8_quantize_mbuv_8x8(MACROBLOCK *x)
{
  int i;

  for(i = 16; i < 24; i ++)
  {
    x->e_mbd.block[i].eob = 0;
  }
  for (i = 16; i < 24; i+=4)
    x->quantize_b_8x8(&x->block[i], &x->e_mbd.block[i]);
}

#endif //CONFIG_T8X8

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


#define EXACT_QUANT
#ifdef EXACT_QUANT
static void invert_quant(int improved_quant, short *quant,
                               unsigned char *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;
    }
    else
    {
        *quant = (1 << 16) / d;
        *shift = 0;
    }
}


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};

    int qrounding_factor = 48;
#if CONFIG_EXTEND_QRANGE
    int qzbin_factor = (vp8_dc_quant(Q,0) < 148) ? 84 : 80;
#else
    int qzbin_factor = (vp8_dc_quant(Q,0) < 37) ? 84: 80;
#endif

    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_factor * quant_val) + 64) >> 7;
        cpi->Y1round[Q][0] = (qrounding_factor * 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_factor * quant_val) + 64) >> 7;
        cpi->Y2round[Q][0] = (qrounding_factor * 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_factor * quant_val) + 64) >> 7;;
        cpi->UVround[Q][0] = (qrounding_factor * 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_fast[Q][rc] = (1 << 16) / quant_val;
            invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + rc,
                         cpi->Y1quant_shift[Q] + rc, quant_val);
            cpi->Y1zbin[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7;
            cpi->Y1round[Q][rc] = (qrounding_factor * 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_fast[Q][rc] = (1 << 16) / quant_val;
            invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + rc,
                         cpi->Y2quant_shift[Q] + rc, quant_val);
            cpi->Y2zbin[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7;
            cpi->Y2round[Q][rc] = (qrounding_factor * 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_fast[Q][rc] = (1 << 16) / quant_val;
            invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + rc,
                         cpi->UVquant_shift[Q] + rc, quant_val);
            cpi->UVzbin[Q][rc] = ((qzbin_factor * quant_val) + 64) >> 7;
            cpi->UVround[Q][rc] = (qrounding_factor * quant_val) >> 7;
            cpi->common.UVdequant[Q][rc] = quant_val;
            cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * 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};
    int qrounding_factor = 48;
#if CONFIG_EXTEND_QRANGE
    int qzbin_factor = vp8_dc_quant(Q,0) < 148 ) ? 84: 80;
#else
    int qzbin_factor = vp8_dc_quant(Q,0) < 37 ) ? 84: 80;
#endif

    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 * quant_val) + 64) >> 7;
        cpi->Y1round[Q][0] = (qrounding_factor * 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 * quant_val) + 64) >> 7;
        cpi->Y2round[Q][0] = (qrounding_factor * 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 * quant_val) + 64) >> 7;;
        cpi->UVround[Q][0] = (qrounding_factor * 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 * quant_val) + 64) >> 7;
            cpi->Y1round[Q][rc] = (qrounding_factor * 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 * quant_val) + 64) >> 7;
            cpi->Y2round[Q][rc] = (qrounding_factors * 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 * quant_val) + 64) >> 7;
            cpi->UVround[Q][rc] = (qrounding_factors * quant_val) >> 7;
            cpi->common.UVdequant[Q][rc] = quant_val;
            cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
        }
    }
}
#endif


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

    // Select the baseline MB Q index allowing for any segment level change.
//#if CONFIG_SEGFEATURES
    if ( segfeature_active( xd, segment_id, SEG_LVL_ALT_Q ) )
    {
        // Abs Value
        if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
            QIndex = get_segdata( xd, segment_id, SEG_LVL_ALT_Q );

        // Delta Value
        else
        {
            QIndex = cpi->common.base_qindex +
                     get_segdata( xd, segment_id, SEG_LVL_ALT_Q );

            // Clamp to valid range
            QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;
        }
    }
    else
        QIndex = cpi->common.base_qindex;

    // Y
    zbin_extra = ( cpi->common.Y1dequant[QIndex][1] *
                   ( cpi->zbin_over_quant +
                     cpi->zbin_mode_boost +
                     x->act_zbin_adj ) ) >> 7;

    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->e_mbd.block[i].dequant = cpi->common.Y1dequant[QIndex];
        x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
        x->block[i].zbin_extra = (short)zbin_extra;
//#if CONFIG_SEGFEATURES
        // Segment max eob offset feature.
        if ( segfeature_active( xd, segment_id, SEG_LVL_EOB ) )
        {
            x->block[i].eob_max_offset =
                get_segdata( xd, segment_id, SEG_LVL_EOB );
        }
        else
            x->block[i].eob_max_offset = 16;
    }

    // UV
    zbin_extra = ( cpi->common.UVdequant[QIndex][1] *
                   ( cpi->zbin_over_quant +
                     cpi->zbin_mode_boost +
                     x->act_zbin_adj ) ) >> 7;

    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->e_mbd.block[i].dequant = cpi->common.UVdequant[QIndex];
        x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
        x->block[i].zbin_extra = (short)zbin_extra;
//#if CONFIG_SEGFEATURES
        // Segment max eob offset feature.
        if ( segfeature_active( xd, segment_id, SEG_LVL_EOB ) )
        {
            x->block[i].eob_max_offset =
                get_segdata( xd, segment_id, SEG_LVL_EOB );
        }
        else
            x->block[i].eob_max_offset = 16;
    }

    // Y2
    zbin_extra = ( cpi->common.Y2dequant[QIndex][1] *
                   ( (cpi->zbin_over_quant / 2) +
                     cpi->zbin_mode_boost +
                     x->act_zbin_adj ) ) >> 7;

    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->e_mbd.block[24].dequant = cpi->common.Y2dequant[QIndex];
    x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex];
    x->block[24].zbin_extra = (short)zbin_extra;

//#if CONFIG_SEGFEATURES
    // TBD perhaps not use for Y2
    // Segment max eob offset feature.
    if ( segfeature_active( xd, segment_id, SEG_LVL_EOB ) )
    {
        x->block[24].eob_max_offset =
            get_segdata( xd, segment_id, SEG_LVL_EOB );
    }
    else
        x->block[24].eob_max_offset = 16;

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


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

    // Y
    zbin_extra = ( cpi->common.Y1dequant[QIndex][1] *
                   ( cpi->zbin_over_quant +
                     cpi->zbin_mode_boost +
                     x->act_zbin_adj ) ) >> 7;
    for (i = 0; i < 16; i++)
    {
        x->block[i].zbin_extra = (short)zbin_extra;
    }

    // UV
    zbin_extra = ( cpi->common.UVdequant[QIndex][1] *
                   ( cpi->zbin_over_quant +
                     cpi->zbin_mode_boost +
                     x->act_zbin_adj ) ) >> 7;

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

    // Y2
    zbin_extra = ( cpi->common.Y2dequant[QIndex][1] *
                   ( (cpi->zbin_over_quant / 2) +
                     cpi->zbin_mode_boost +
                     x->act_zbin_adj ) ) >> 7;

    x->block[24].zbin_extra = (short)zbin_extra;
}


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

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


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;
    int i;
    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;

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