1 
2 /*!
3  *************************************************************************************
4  * \file quantChroma_2step.c
5  *
6  * \brief
7  *    Quantization process for a Chroma block
8  *
9  * \author
10  *    Main contributors (see contributors.h for copyright, address and affiliation details)
11  *    - Alexis Michael Tourapis                  <alexismt@ieee.org>
12  *
13  *************************************************************************************
14  */
15 
16 #include "contributors.h"
17 
18 #include <math.h>
19 
20 #include "global.h"
21 #include "q_matrix.h"
22 #include "quant4x4.h"
23 #include "quantChroma.h"
24 
25 
26 /*!
27  ************************************************************************
28  * \brief
29  *    Quantization process for All coefficients for a 2x2 DC block
30  *
31  * \par Input:
32  *
33  * \par Output:
34  *
35  ************************************************************************
36  */
quant_dc2x2_2step(Macroblock * currMB,int ** tblock,int qp,int * DCLevel,int * DCRun,LevelQuantParams * q_params_4x4,int ** fadjust,const byte (* pos_scan)[2])37 int quant_dc2x2_2step(Macroblock *currMB, int **tblock, int qp, int* DCLevel, int* DCRun,
38                        LevelQuantParams *q_params_4x4, int **fadjust, const byte (*pos_scan)[2])
39 {
40   QuantParameters *p_Quant = currMB->p_Vid->p_Quant;
41   Boolean is_cavlc = (Boolean) (currMB->p_Slice->symbol_mode == CAVLC);
42   int coeff_ctr;
43 
44   int *m7;
45   int scaled_coeff;
46 
47   int   level, run = 0;
48   int   nonzero = FALSE;
49   int   qp_per = p_Quant->qp_per_matrix[qp];
50   int   q_bits = Q_BITS + qp_per + 1;
51   //const byte *p_scan = &pos_scan[0][0];
52   int*  DCL = &DCLevel[0];
53   int*  DCR = &DCRun[0];
54 
55   m7 = *tblock;
56 
57   // Quantization
58   for (coeff_ctr=0; coeff_ctr < 4; coeff_ctr++)
59   {
60     // we need to update q_params_4x4->OffsetComp to a 4x1 array that would contain offset info for
61     // every 2x2 DC position
62     if (*m7)
63     {
64       scaled_coeff = iabs (*m7) * q_params_4x4->ScaleComp;
65       level = (scaled_coeff + (q_params_4x4->OffsetComp << 1) ) >> q_bits;
66 
67       if (level  != 0)
68       {
69         if (is_cavlc)
70           level = imin(level, CAVLC_LEVEL_LIMIT);
71 
72         level = isignab(level, *m7);
73 
74         *m7++ = ((level * q_params_4x4->InvScaleComp) << qp_per);
75 
76         *DCL++ = level;
77         *DCR++ = run;
78         run    = 0;
79         nonzero = TRUE;
80       }
81       else
82       {
83         run++;
84         *m7++ = 0;
85       }
86     }
87     else
88     {
89       run++;
90       m7++;
91     }
92   }
93 
94   *DCL = 0;
95 
96   return nonzero;
97 }
98 
99 /*!
100  ************************************************************************
101  * \brief
102  *    Quantization process for All coefficients for a 2x2 DC block
103  *
104  * \par Input:
105  *
106  * \par Output:
107  *
108  ************************************************************************
109  */
quant_dc4x2_2step(Macroblock * currMB,int ** tblock,int qp,int * DCLevel,int * DCRun,LevelQuantParams * q_params_4x4,int ** fadjust,const byte (* pos_scan)[2])110 int quant_dc4x2_2step(Macroblock *currMB, int **tblock, int qp, int* DCLevel, int* DCRun,
111                        LevelQuantParams *q_params_4x4, int **fadjust, const byte (*pos_scan)[2])
112 {
113   QuantParameters *p_Quant = currMB->p_Vid->p_Quant;
114   Boolean is_cavlc = (Boolean) (currMB->p_Slice->symbol_mode == CAVLC);
115   int i,j, coeff_ctr;
116 
117   int *m7;
118   int scaled_coeff;
119 
120   int   level, run = 0;
121   int   nonzero = FALSE;
122   int   qp_per = p_Quant->qp_per_matrix[qp];
123   int   q_bits = Q_BITS + qp_per + 1;
124   const byte *p_scan = &pos_scan[0][0];
125   int*  DCL = &DCLevel[0];
126   int*  DCR = &DCRun[0];
127 
128   // Quantization
129   for (coeff_ctr = 0; coeff_ctr < 8; coeff_ctr++)
130   {
131     j = *p_scan++;  // note that in this part, somehow coefficients were transposed from 2x4 to 4x2.
132     i = *p_scan++;
133 
134     m7 = &tblock[j][i];
135 
136     if (*m7 != 0)
137     {
138       scaled_coeff = iabs (*m7) * q_params_4x4->ScaleComp;
139       level = (scaled_coeff + (q_params_4x4->OffsetComp << 1) ) >> q_bits;
140 
141       if (level  != 0)
142       {
143         if (is_cavlc)
144           level = imin(level, CAVLC_LEVEL_LIMIT);
145         level = isignab(level, *m7);
146 
147         *m7 = ((level * q_params_4x4->InvScaleComp) << qp_per);
148 
149         *DCL++ = level;
150         *DCR++ = run;
151         // reset zero level counter
152         run    = 0;
153         nonzero = TRUE;
154       }
155       else
156       {
157         run++;
158         *m7 = 0;
159       }
160     }
161     else
162     {
163       run++;
164     }
165   }
166 
167   *DCL = 0;
168 
169   return nonzero;
170 }
171 
172 
173 
174