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