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