Merge "Detect toolchain based on gcc -dumpmachine"
[profile/ivi/libvpx.git] / vp8 / encoder / quantize.c
1 /*
2  *  Copyright (c) 2010 The VP8 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 "quantize.h"
16 #include "entropy.h"
17 #include "predictdc.h"
18
19 void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
20 {
21     int i, rc, eob;
22     int zbin;
23     int x, y, z, sz;
24     short *coeff_ptr  = &b->coeff[0];
25     short *zbin_ptr   = &b->zbin[0][0];
26     short *round_ptr  = &b->round[0][0];
27     short *quant_ptr  = &b->quant[0][0];
28     short *qcoeff_ptr = d->qcoeff;
29     short *dqcoeff_ptr = d->dqcoeff;
30     short *dequant_ptr = &d->dequant[0][0];
31
32     vpx_memset(qcoeff_ptr, 0, 32);
33     vpx_memset(dqcoeff_ptr, 0, 32);
34
35     eob = -1;
36
37     for (i = 0; i < 16; i++)
38     {
39         rc   = vp8_default_zig_zag1d[i];
40         z    = coeff_ptr[rc];
41         zbin = zbin_ptr[rc] ;
42
43         sz = (z >> 31);                                 // sign of z
44         x  = (z ^ sz) - sz;                             // x = abs(z)
45
46         if (x >= zbin)
47         {
48             y  = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
49             x  = (y ^ sz) - sz;                         // get the sign back
50             qcoeff_ptr[rc] = x;                          // write to destination
51             dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value
52
53             if (y)
54             {
55                 eob = i;                                // last nonzero coeffs
56             }
57         }
58     }
59     d->eob = eob + 1;
60 }
61
62 void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
63 {
64     int i, rc, eob;
65     int zbin;
66     int x, y, z, sz;
67     short *zbin_boost_ptr = &b->zrun_zbin_boost[0];
68     short *coeff_ptr  = &b->coeff[0];
69     short *zbin_ptr   = &b->zbin[0][0];
70     short *round_ptr  = &b->round[0][0];
71     short *quant_ptr  = &b->quant[0][0];
72     short *qcoeff_ptr = d->qcoeff;
73     short *dqcoeff_ptr = d->dqcoeff;
74     short *dequant_ptr = &d->dequant[0][0];
75     short zbin_oq_value = b->zbin_extra;
76
77     vpx_memset(qcoeff_ptr, 0, 32);
78     vpx_memset(dqcoeff_ptr, 0, 32);
79
80     eob = -1;
81
82     for (i = 0; i < 16; i++)
83     {
84         rc   = vp8_default_zig_zag1d[i];
85         z    = coeff_ptr[rc];
86
87         //if ( i == 0 )
88         //    zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value/2;
89         //else
90         zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
91
92         zbin_boost_ptr ++;
93         sz = (z >> 31);                                 // sign of z
94         x  = (z ^ sz) - sz;                             // x = abs(z)
95
96         if (x >= zbin)
97         {
98             y  = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
99             x  = (y ^ sz) - sz;                         // get the sign back
100             qcoeff_ptr[rc]  = x;                         // write to destination
101             dqcoeff_ptr[rc] = x * dequant_ptr[rc];        // dequantized value
102
103             if (y)
104             {
105                 eob = i;                                // last nonzero coeffs
106                 zbin_boost_ptr = &b->zrun_zbin_boost[0];    // reset zero runlength
107             }
108         }
109     }
110
111     d->eob = eob + 1;
112 }
113
114 void vp8_quantize_mby(MACROBLOCK *x)
115 {
116     int i;
117     int has_2nd_order = (x->e_mbd.mbmi.mode != B_PRED
118         && x->e_mbd.mbmi.mode != SPLITMV);
119
120     for (i = 0; i < 16; i++)
121     {
122         x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
123         x->e_mbd.mbmi.mb_skip_coeff &=
124             (x->e_mbd.block[i].eob <= has_2nd_order);
125     }
126
127     if(has_2nd_order)
128     {
129         x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
130         x->e_mbd.mbmi.mb_skip_coeff &= (!x->e_mbd.block[24].eob);
131     }
132 }
133
134 void vp8_quantize_mb(MACROBLOCK *x)
135 {
136     int i;
137     int has_2nd_order=(x->e_mbd.mbmi.mode != B_PRED
138         && x->e_mbd.mbmi.mode != SPLITMV);
139
140     x->e_mbd.mbmi.mb_skip_coeff = 1;
141     for (i = 0; i < 24+has_2nd_order; i++)
142     {
143         x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
144         x->e_mbd.mbmi.mb_skip_coeff &=
145             (x->e_mbd.block[i].eob <= (has_2nd_order && i<16));
146     }
147 }
148
149
150 void vp8_quantize_mbuv(MACROBLOCK *x)
151 {
152     int i;
153
154     for (i = 16; i < 24; i++)
155     {
156         x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
157         x->e_mbd.mbmi.mb_skip_coeff &= (!x->e_mbd.block[i].eob);
158     }
159 }