Add packaging files for Tizen
[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_c(BLOCK *b, BLOCKD *d)
107 {
108     int i, rc, eob;
109     int zbin;
110     int x, y, z, sz;
111     short *zbin_boost_ptr  = b->zrun_zbin_boost;
112     short *coeff_ptr       = b->coeff;
113     short *zbin_ptr        = b->zbin;
114     short *round_ptr       = b->round;
115     short *quant_ptr       = b->quant;
116     unsigned char *quant_shift_ptr = b->quant_shift;
117     short *qcoeff_ptr      = d->qcoeff;
118     short *dqcoeff_ptr     = d->dqcoeff;
119     short *dequant_ptr     = d->dequant;
120     short zbin_oq_value    = b->zbin_extra;
121
122     vpx_memset(qcoeff_ptr, 0, 32);
123     vpx_memset(dqcoeff_ptr, 0, 32);
124
125     eob = -1;
126
127     for (i = 0; i < 16; i++)
128     {
129         rc   = vp8_default_zig_zag1d[i];
130         z    = coeff_ptr[rc];
131
132         zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
133
134         zbin_boost_ptr ++;
135         sz = (z >> 31);                                 // sign of z
136         x  = (z ^ sz) - sz;                             // x = abs(z)
137
138         if (x >= zbin)
139         {
140             x += round_ptr[rc];
141             y  = (((x * quant_ptr[rc]) >> 16) + x)
142                  >> quant_shift_ptr[rc];                // quantize (x)
143             x  = (y ^ sz) - sz;                         // get the sign back
144             qcoeff_ptr[rc]  = x;                        // write to destination
145             dqcoeff_ptr[rc] = x * dequant_ptr[rc];      // dequantized value
146
147             if (y)
148             {
149                 eob = i;                                // last nonzero coeffs
150                 zbin_boost_ptr = b->zrun_zbin_boost;    // reset zero runlength
151             }
152         }
153     }
154
155     *d->eob = (char)(eob + 1);
156 }
157
158 /* Perform regular quantization, with unbiased rounding and no zero bin. */
159 void vp8_strict_quantize_b_c(BLOCK *b, BLOCKD *d)
160 {
161     int i;
162     int rc;
163     int eob;
164     int x;
165     int y;
166     int z;
167     int sz;
168     short *coeff_ptr;
169     short *quant_ptr;
170     unsigned char *quant_shift_ptr;
171     short *qcoeff_ptr;
172     short *dqcoeff_ptr;
173     short *dequant_ptr;
174
175     coeff_ptr       = b->coeff;
176     quant_ptr       = b->quant;
177     quant_shift_ptr = b->quant_shift;
178     qcoeff_ptr      = d->qcoeff;
179     dqcoeff_ptr     = d->dqcoeff;
180     dequant_ptr     = d->dequant;
181     eob = - 1;
182     vpx_memset(qcoeff_ptr, 0, 32);
183     vpx_memset(dqcoeff_ptr, 0, 32);
184     for (i = 0; i < 16; i++)
185     {
186         int dq;
187         int round;
188
189         /*TODO: These arrays should be stored in zig-zag order.*/
190         rc = vp8_default_zig_zag1d[i];
191         z = coeff_ptr[rc];
192         dq = dequant_ptr[rc];
193         round = dq >> 1;
194         /* Sign of z. */
195         sz = -(z < 0);
196         x = (z + sz) ^ sz;
197         x += round;
198         if (x >= dq)
199         {
200             /* Quantize x. */
201             y  = (((x * quant_ptr[rc]) >> 16) + x) >> quant_shift_ptr[rc];
202             /* Put the sign back. */
203             x = (y + sz) ^ sz;
204             /* Save the coefficient and its dequantized value. */
205             qcoeff_ptr[rc] = x;
206             dqcoeff_ptr[rc] = x * dq;
207             /* Remember the last non-zero coefficient. */
208             if (y)
209                 eob = i;
210         }
211     }
212
213     *d->eob = (char)(eob + 1);
214 }
215
216 #else
217
218 void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d)
219 {
220     int i, rc, eob;
221     int zbin;
222     int x, y, z, sz;
223     short *zbin_boost_ptr = b->zrun_zbin_boost;
224     short *coeff_ptr      = b->coeff;
225     short *zbin_ptr       = b->zbin;
226     short *round_ptr      = b->round;
227     short *quant_ptr      = b->quant;
228     short *qcoeff_ptr     = d->qcoeff;
229     short *dqcoeff_ptr    = d->dqcoeff;
230     short *dequant_ptr    = d->dequant;
231     short zbin_oq_value   = b->zbin_extra;
232
233     vpx_memset(qcoeff_ptr, 0, 32);
234     vpx_memset(dqcoeff_ptr, 0, 32);
235
236     eob = -1;
237
238     for (i = 0; i < 16; i++)
239     {
240         rc   = vp8_default_zig_zag1d[i];
241         z    = coeff_ptr[rc];
242
243         //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,
440                           44, 44};
441
442     for (Q = 0; Q < QINDEX_RANGE; Q++)
443     {
444         // dc values
445         quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
446         cpi->Y1quant_fast[Q][0] = (1 << 16) / quant_val;
447         invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 0,
448                      cpi->Y1quant_shift[Q] + 0, quant_val);
449         cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
450         cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
451         cpi->common.Y1dequant[Q][0] = quant_val;
452         cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
453
454         quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
455         cpi->Y2quant_fast[Q][0] = (1 << 16) / quant_val;
456         invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 0,
457                      cpi->Y2quant_shift[Q] + 0, quant_val);
458         cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
459         cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
460         cpi->common.Y2dequant[Q][0] = quant_val;
461         cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
462
463         quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
464         cpi->UVquant_fast[Q][0] = (1 << 16) / quant_val;
465         invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 0,
466                      cpi->UVquant_shift[Q] + 0, quant_val);
467         cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
468         cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
469         cpi->common.UVdequant[Q][0] = quant_val;
470         cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
471
472         // all the ac values = ;
473         quant_val = vp8_ac_yquant(Q);
474         cpi->Y1quant_fast[Q][1] = (1 << 16) / quant_val;
475         invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 1,
476                      cpi->Y1quant_shift[Q] + 1, quant_val);
477         cpi->Y1zbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
478         cpi->Y1round[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
479         cpi->common.Y1dequant[Q][1] = quant_val;
480         cpi->zrun_zbin_boost_y1[Q][1] = (quant_val * zbin_boost[1]) >> 7;
481
482         quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
483         cpi->Y2quant_fast[Q][1] = (1 << 16) / quant_val;
484         invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 1,
485                      cpi->Y2quant_shift[Q] + 1, quant_val);
486         cpi->Y2zbin[Q][1] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
487         cpi->Y2round[Q][1] = (qrounding_factors_y2[Q] * quant_val) >> 7;
488         cpi->common.Y2dequant[Q][1] = quant_val;
489         cpi->zrun_zbin_boost_y2[Q][1] = (quant_val * zbin_boost[1]) >> 7;
490
491         quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
492         cpi->UVquant_fast[Q][1] = (1 << 16) / quant_val;
493         invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 1,
494                      cpi->UVquant_shift[Q] + 1, quant_val);
495         cpi->UVzbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
496         cpi->UVround[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
497         cpi->common.UVdequant[Q][1] = quant_val;
498         cpi->zrun_zbin_boost_uv[Q][1] = (quant_val * zbin_boost[1]) >> 7;
499
500         for (i = 2; i < 16; i++)
501         {
502             cpi->Y1quant_fast[Q][i] = cpi->Y1quant_fast[Q][1];
503             cpi->Y1quant[Q][i] = cpi->Y1quant[Q][1];
504             cpi->Y1quant_shift[Q][i] = cpi->Y1quant_shift[Q][1];
505             cpi->Y1zbin[Q][i] = cpi->Y1zbin[Q][1];
506             cpi->Y1round[Q][i] = cpi->Y1round[Q][1];
507             cpi->zrun_zbin_boost_y1[Q][i] = (cpi->common.Y1dequant[Q][1] *
508                                              zbin_boost[i]) >> 7;
509
510             cpi->Y2quant_fast[Q][i] = cpi->Y2quant_fast[Q][1];
511             cpi->Y2quant[Q][i] = cpi->Y2quant[Q][1];
512             cpi->Y2quant_shift[Q][i] = cpi->Y2quant_shift[Q][1];
513             cpi->Y2zbin[Q][i] = cpi->Y2zbin[Q][1];
514             cpi->Y2round[Q][i] = cpi->Y2round[Q][1];
515             cpi->zrun_zbin_boost_y2[Q][i] = (cpi->common.Y2dequant[Q][1] *
516                                              zbin_boost[i]) >> 7;
517
518             cpi->UVquant_fast[Q][i] = cpi->UVquant_fast[Q][1];
519             cpi->UVquant[Q][i] = cpi->UVquant[Q][1];
520             cpi->UVquant_shift[Q][i] = cpi->UVquant_shift[Q][1];
521             cpi->UVzbin[Q][i] = cpi->UVzbin[Q][1];
522             cpi->UVround[Q][i] = cpi->UVround[Q][1];
523             cpi->zrun_zbin_boost_uv[Q][i] = (cpi->common.UVdequant[Q][1] *
524                                              zbin_boost[i]) >> 7;
525         }
526     }
527 }
528 #else
529 void vp8cx_init_quantizer(VP8_COMP *cpi)
530 {
531     int i;
532     int quant_val;
533     int Q;
534
535     int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44};
536
537     for (Q = 0; Q < QINDEX_RANGE; Q++)
538     {
539         // dc values
540         quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
541         cpi->Y1quant[Q][0] = (1 << 16) / quant_val;
542         cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
543         cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
544         cpi->common.Y1dequant[Q][0] = quant_val;
545         cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
546
547         quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
548         cpi->Y2quant[Q][0] = (1 << 16) / quant_val;
549         cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
550         cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
551         cpi->common.Y2dequant[Q][0] = quant_val;
552         cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
553
554         quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
555         cpi->UVquant[Q][0] = (1 << 16) / quant_val;
556         cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
557         cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
558         cpi->common.UVdequant[Q][0] = quant_val;
559         cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
560
561         // all the ac values = ;
562         for (i = 1; i < 16; i++)
563         {
564             int rc = vp8_default_zig_zag1d[i];
565
566             quant_val = vp8_ac_yquant(Q);
567             cpi->Y1quant[Q][rc] = (1 << 16) / quant_val;
568             cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
569             cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
570             cpi->common.Y1dequant[Q][rc] = quant_val;
571             cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7;
572
573             quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
574             cpi->Y2quant[Q][rc] = (1 << 16) / quant_val;
575             cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
576             cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7;
577             cpi->common.Y2dequant[Q][rc] = quant_val;
578             cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7;
579
580             quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
581             cpi->UVquant[Q][rc] = (1 << 16) / quant_val;
582             cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
583             cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
584             cpi->common.UVdequant[Q][rc] = quant_val;
585             cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
586         }
587     }
588 }
589 #endif
590
591 #define ZBIN_EXTRA_Y \
592     (( cpi->common.Y1dequant[QIndex][1] *  \
593     ( cpi->zbin_over_quant +  \
594       cpi->zbin_mode_boost +  \
595       x->act_zbin_adj ) ) >> 7)
596
597 #define ZBIN_EXTRA_UV \
598     (( cpi->common.UVdequant[QIndex][1] *  \
599     ( cpi->zbin_over_quant +  \
600       cpi->zbin_mode_boost +  \
601       x->act_zbin_adj ) ) >> 7)
602
603 #define ZBIN_EXTRA_Y2 \
604     (( cpi->common.Y2dequant[QIndex][1] *  \
605     ( (cpi->zbin_over_quant / 2) +  \
606        cpi->zbin_mode_boost +  \
607        x->act_zbin_adj ) ) >> 7)
608
609 void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x, int ok_to_skip)
610 {
611     int i;
612     int QIndex;
613     MACROBLOCKD *xd = &x->e_mbd;
614     int zbin_extra;
615
616     // Select the baseline MB Q index.
617     if (xd->segmentation_enabled)
618     {
619         // Abs Value
620         if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
621
622             QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
623         // Delta Value
624         else
625         {
626             QIndex = cpi->common.base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
627             QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;    // Clamp to valid range
628         }
629     }
630     else
631         QIndex = cpi->common.base_qindex;
632
633     /* This initialization should be called at least once. Use ok_to_skip to
634      * decide if it is ok to skip.
635      * Before encoding a frame, this function is always called with ok_to_skip
636      * =0, which means no skiping of calculations. The "last" values are
637      * initialized at that time.
638      */
639     if (!ok_to_skip || QIndex != x->q_index)
640     {
641
642         xd->dequant_y1_dc[0] = 1;
643         xd->dequant_y1[0] = cpi->common.Y1dequant[QIndex][0];
644         xd->dequant_y2[0] = cpi->common.Y2dequant[QIndex][0];
645         xd->dequant_uv[0] = cpi->common.UVdequant[QIndex][0];
646
647         for (i = 1; i < 16; i++)
648         {
649             xd->dequant_y1_dc[i] =
650             xd->dequant_y1[i] = cpi->common.Y1dequant[QIndex][1];
651             xd->dequant_y2[i] = cpi->common.Y2dequant[QIndex][1];
652             xd->dequant_uv[i] = cpi->common.UVdequant[QIndex][1];
653         }
654 #if 1
655         /*TODO:  Remove dequant from BLOCKD.  This is a temporary solution until
656          * the quantizer code uses a passed in pointer to the dequant constants.
657          * This will also require modifications to the x86 and neon assembly.
658          * */
659         for (i = 0; i < 16; i++)
660             x->e_mbd.block[i].dequant = xd->dequant_y1; //cpi->common.Y1dequant[QIndex];
661         for (i = 16; i < 24; i++)
662             x->e_mbd.block[i].dequant = xd->dequant_uv; //cpi->common.UVdequant[QIndex];
663         x->e_mbd.block[24].dequant = xd->dequant_y2; //cpi->common.Y2dequant[QIndex];
664 #endif
665
666         // Y
667         zbin_extra = ZBIN_EXTRA_Y;
668
669         for (i = 0; i < 16; i++)
670         {
671             x->block[i].quant = cpi->Y1quant[QIndex];
672             x->block[i].quant_fast = cpi->Y1quant_fast[QIndex];
673             x->block[i].quant_shift = cpi->Y1quant_shift[QIndex];
674             x->block[i].zbin = cpi->Y1zbin[QIndex];
675             x->block[i].round = cpi->Y1round[QIndex];
676             x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
677             x->block[i].zbin_extra = (short)zbin_extra;
678         }
679
680         // UV
681         zbin_extra = ZBIN_EXTRA_UV;
682
683         for (i = 16; i < 24; i++)
684         {
685             x->block[i].quant = cpi->UVquant[QIndex];
686             x->block[i].quant_fast = cpi->UVquant_fast[QIndex];
687             x->block[i].quant_shift = cpi->UVquant_shift[QIndex];
688             x->block[i].zbin = cpi->UVzbin[QIndex];
689             x->block[i].round = cpi->UVround[QIndex];
690             x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
691             x->block[i].zbin_extra = (short)zbin_extra;
692         }
693
694         // Y2
695         zbin_extra = ZBIN_EXTRA_Y2;
696
697         x->block[24].quant_fast = cpi->Y2quant_fast[QIndex];
698         x->block[24].quant = cpi->Y2quant[QIndex];
699         x->block[24].quant_shift = cpi->Y2quant_shift[QIndex];
700         x->block[24].zbin = cpi->Y2zbin[QIndex];
701         x->block[24].round = cpi->Y2round[QIndex];
702         x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex];
703         x->block[24].zbin_extra = (short)zbin_extra;
704
705         /* save this macroblock QIndex for vp8_update_zbin_extra() */
706         x->q_index = QIndex;
707
708         cpi->last_zbin_over_quant = cpi->zbin_over_quant;
709         cpi->last_zbin_mode_boost = cpi->zbin_mode_boost;
710         x->last_act_zbin_adj = x->act_zbin_adj;
711
712
713
714     }
715     else if(cpi->last_zbin_over_quant != cpi->zbin_over_quant
716             || cpi->last_zbin_mode_boost != cpi->zbin_mode_boost
717             || x->last_act_zbin_adj != x->act_zbin_adj)
718     {
719         // Y
720         zbin_extra = ZBIN_EXTRA_Y;
721
722         for (i = 0; i < 16; i++)
723             x->block[i].zbin_extra = (short)zbin_extra;
724
725         // UV
726         zbin_extra = ZBIN_EXTRA_UV;
727
728         for (i = 16; i < 24; i++)
729             x->block[i].zbin_extra = (short)zbin_extra;
730
731         // Y2
732         zbin_extra = ZBIN_EXTRA_Y2;
733         x->block[24].zbin_extra = (short)zbin_extra;
734
735         cpi->last_zbin_over_quant = cpi->zbin_over_quant;
736         cpi->last_zbin_mode_boost = cpi->zbin_mode_boost;
737         x->last_act_zbin_adj = x->act_zbin_adj;
738     }
739 }
740
741 void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x)
742 {
743     int i;
744     int QIndex = x->q_index;
745     int zbin_extra;
746
747     // Y
748     zbin_extra = ZBIN_EXTRA_Y;
749
750     for (i = 0; i < 16; i++)
751         x->block[i].zbin_extra = (short)zbin_extra;
752
753     // UV
754     zbin_extra = ZBIN_EXTRA_UV;
755
756     for (i = 16; i < 24; i++)
757         x->block[i].zbin_extra = (short)zbin_extra;
758
759     // Y2
760     zbin_extra = ZBIN_EXTRA_Y2;
761     x->block[24].zbin_extra = (short)zbin_extra;
762 }
763 #undef ZBIN_EXTRA_Y
764 #undef ZBIN_EXTRA_UV
765 #undef ZBIN_EXTRA_Y2
766
767 void vp8cx_frame_init_quantizer(VP8_COMP *cpi)
768 {
769     // Clear Zbin mode boost for default case
770     cpi->zbin_mode_boost = 0;
771
772     // MB level quantizer setup
773     vp8cx_mb_init_quantizer(cpi, &cpi->mb, 0);
774 }
775
776
777 void vp8_set_quantizer(struct VP8_COMP *cpi, int Q)
778 {
779     VP8_COMMON *cm = &cpi->common;
780     MACROBLOCKD *mbd = &cpi->mb.e_mbd;
781     int update = 0;
782     int new_delta_q;
783     cm->base_qindex = Q;
784
785     /* if any of the delta_q values are changing update flag has to be set */
786     /* currently only y2dc_delta_q may change */
787
788     cm->y1dc_delta_q = 0;
789     cm->y2ac_delta_q = 0;
790     cm->uvdc_delta_q = 0;
791     cm->uvac_delta_q = 0;
792
793     if (Q < 4)
794     {
795         new_delta_q = 4-Q;
796     }
797     else
798         new_delta_q = 0;
799
800     update |= cm->y2dc_delta_q != new_delta_q;
801     cm->y2dc_delta_q = new_delta_q;
802
803
804     // Set Segment specific quatizers
805     mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
806     mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
807     mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
808     mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
809
810     /* quantizer has to be reinitialized for any delta_q changes */
811     if(update)
812         vp8cx_init_quantizer(cpi);
813
814 }