00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "contributors.h"
00018
00019 #include <math.h>
00020
00021 #include "global.h"
00022
00023 #include "image.h"
00024 #include "mb_access.h"
00025 #include "vlc.h"
00026 #include "transform.h"
00027 #include "mc_prediction.h"
00028 #include "q_offsets.h"
00029 #include "q_matrix.h"
00030 #include "quant4x4.h"
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 int quant_4x4_around(Macroblock *currMB, int **tblock, struct quant_methods *q_method)
00041 {
00042 ImageParameters *p_Img = currMB->p_Img;
00043 QuantParameters *p_Quant = p_Img->p_Quant;
00044 int AdaptRndWeight = p_Img->AdaptRndWeight;
00045
00046 int block_x = q_method->block_x;
00047 int qp = q_method->qp;
00048 int* ACLevel = q_method->ACLevel;
00049 int* ACRun = q_method->ACRun;
00050 LevelQuantParams **q_params_4x4 = q_method->q_params;
00051 const byte (*pos_scan)[2] = q_method->pos_scan;
00052 const byte *c_cost = q_method->c_cost;
00053 int *coeff_cost = q_method->coeff_cost;
00054 Boolean is_cavlc = (currMB->p_slice->symbol_mode == CAVLC);
00055 LevelQuantParams *q_params = NULL;
00056 int **fadjust4x4 = q_method->fadjust;
00057
00058 int i,j, coeff_ctr;
00059
00060 int *m7;
00061 int scaled_coeff;
00062
00063 int level, run = 0;
00064 int nonzero = FALSE;
00065 int qp_per = p_Quant->qp_per_matrix[qp];
00066 int q_bits = Q_BITS + qp_per;
00067 const byte *p_scan = &pos_scan[0][0];
00068 int* ACL = &ACLevel[0];
00069 int* ACR = &ACRun[0];
00070 int* padjust4x4;
00071
00072
00073 for (coeff_ctr = 0; coeff_ctr < 16; ++coeff_ctr)
00074 {
00075 i = *p_scan++;
00076 j = *p_scan++;
00077
00078 padjust4x4 = &fadjust4x4[j][block_x + i];
00079 m7 = &tblock[j][block_x + i];
00080
00081 if (*m7 != 0)
00082 {
00083 q_params = &q_params_4x4[j][i];
00084 scaled_coeff = iabs (*m7) * q_params->ScaleComp;
00085 level = (scaled_coeff + q_params->OffsetComp) >> q_bits;
00086
00087 if (level != 0)
00088 {
00089 if (is_cavlc)
00090 level = imin(level, CAVLC_LEVEL_LIMIT);
00091
00092 *padjust4x4 = rshift_rnd_sf((AdaptRndWeight * (scaled_coeff - (level << q_bits))), q_bits + 1);
00093
00094 *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
00095
00096 level = isignab(level, *m7);
00097 *m7 = rshift_rnd_sf(((level * q_params->InvScaleComp) << qp_per), 4);
00098
00099
00100
00101 *ACL++ = level;
00102 *ACR++ = run;
00103
00104 run = 0;
00105 nonzero = TRUE;
00106 }
00107 else
00108 {
00109 *padjust4x4 = 0;
00110 *m7 = 0;
00111 ++run;
00112 }
00113 }
00114 else
00115 {
00116 *padjust4x4 = 0;
00117 ++run;
00118 }
00119 }
00120
00121 *ACL = 0;
00122
00123 return nonzero;
00124 }
00125
00126 int quant_ac4x4_around(Macroblock *currMB, int **tblock, struct quant_methods *q_method, int type)
00127 {
00128 int block_x = q_method->block_x;
00129
00130 int qp = q_method->qp;
00131 int* ACLevel = q_method->ACLevel;
00132 int* ACRun = q_method->ACRun;
00133 LevelQuantParams **q_params_4x4 = q_method->q_params;
00134 int **fadjust4x4 = q_method->fadjust;
00135 const byte (*pos_scan)[2] = q_method->pos_scan;
00136 const byte *c_cost = q_method->c_cost;
00137 int *coeff_cost = q_method->coeff_cost;
00138
00139 Boolean is_cavlc = (currMB->p_slice->symbol_mode == CAVLC);
00140 ImageParameters *p_Img = currMB->p_Img;
00141 QuantParameters *p_Quant = p_Img->p_Quant;
00142 int AdaptRndWeight = p_Img->AdaptRndWeight;
00143 LevelQuantParams *q_params = NULL;
00144
00145 int i,j, coeff_ctr;
00146
00147 int *m7;
00148 int scaled_coeff;
00149
00150 int level, run = 0;
00151 int nonzero = FALSE;
00152 int qp_per = p_Quant->qp_per_matrix[qp];
00153 int q_bits = Q_BITS + qp_per;
00154 const byte *p_scan = &pos_scan[1][0];
00155 int* ACL = &ACLevel[0];
00156 int* ACR = &ACRun[0];
00157 int* padjust4x4;
00158
00159
00160 for (coeff_ctr = 1; coeff_ctr < 16; ++coeff_ctr)
00161 {
00162 i = *p_scan++;
00163 j = *p_scan++;
00164
00165 padjust4x4 = &fadjust4x4[j][block_x + i];
00166 m7 = &tblock[j][block_x + i];
00167 if (*m7 != 0)
00168 {
00169 q_params = &q_params_4x4[j][i];
00170 scaled_coeff = iabs (*m7) * q_params->ScaleComp;
00171 level = (scaled_coeff + q_params->OffsetComp) >> q_bits;
00172
00173 if (level != 0)
00174 {
00175 if (is_cavlc)
00176 level = imin(level, CAVLC_LEVEL_LIMIT);
00177
00178 *padjust4x4 = rshift_rnd_sf((AdaptRndWeight * (scaled_coeff - (level << q_bits))), (q_bits + 1));
00179
00180 *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
00181
00182 level = isignab(level, *m7);
00183 *m7 = rshift_rnd_sf(((level * q_params->InvScaleComp) << qp_per), 4);
00184
00185
00186
00187 *ACL++ = level;
00188 *ACR++ = run;
00189
00190 run = 0;
00191 nonzero = TRUE;
00192 }
00193 else
00194 {
00195 *padjust4x4 = 0;
00196 *m7 = 0;
00197 ++run;
00198 }
00199 }
00200 else
00201 {
00202 *padjust4x4 = 0;
00203 ++run;
00204 }
00205 }
00206
00207 *ACL = 0;
00208
00209 return nonzero;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219 int quant_dc4x4_around(Macroblock *currMB, int **tblock, int qp, int* DCLevel, int* DCRun,
00220 LevelQuantParams *q_params_4x4, const byte (*pos_scan)[2])
00221 {
00222 QuantParameters *p_Quant = currMB->p_Img->p_Quant;
00223 Boolean is_cavlc = (currMB->p_slice->symbol_mode == CAVLC);
00224 int i,j, coeff_ctr;
00225
00226 int *m7;
00227 int scaled_coeff;
00228
00229 int level, run = 0;
00230 int nonzero = FALSE;
00231 int qp_per = p_Quant->qp_per_matrix[qp];
00232 int q_bits = Q_BITS + qp_per + 1;
00233 const byte *p_scan = &pos_scan[0][0];
00234 int* DCL = &DCLevel[0];
00235 int* DCR = &DCRun[0];
00236
00237
00238 for (coeff_ctr = 0; coeff_ctr < 16; ++coeff_ctr)
00239 {
00240 i = *p_scan++;
00241 j = *p_scan++;
00242
00243 m7 = &tblock[j][i];
00244
00245 if (*m7 != 0)
00246 {
00247 scaled_coeff = iabs (*m7) * q_params_4x4->ScaleComp;
00248 level = (scaled_coeff + (q_params_4x4->OffsetComp << 1) ) >> q_bits;
00249
00250 if (level != 0)
00251 {
00252 if (is_cavlc)
00253 level = imin(level, CAVLC_LEVEL_LIMIT);
00254
00255 level = isignab(level, *m7);
00256 *m7 = level;
00257 *DCL++ = level;
00258 *DCR++ = run;
00259
00260 run = 0;
00261 nonzero = TRUE;
00262 }
00263 else
00264 {
00265 ++run;
00266 *m7 = 0;
00267 }
00268 }
00269 else
00270 {
00271 ++run;
00272 }
00273 }
00274
00275 *DCL = 0;
00276
00277 return nonzero;
00278 }