00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "contributors.h"
00019
00020 #include <math.h>
00021
00022 #include "global.h"
00023
00024 #include "image.h"
00025 #include "mb_access.h"
00026 #include "vlc.h"
00027 #include "transform.h"
00028 #include "mc_prediction.h"
00029 #include "q_offsets.h"
00030 #include "q_matrix.h"
00031 #include "quant4x4.h"
00032 #include "rdoq.h"
00033
00034
00035
00036
00037
00038
00039
00040
00041 int quant_4x4_trellis(Macroblock *currMB, int **tblock, struct quant_methods *q_method)
00042 {
00043 int block_x = q_method->block_x;
00044
00045 int* ACLevel = q_method->ACLevel;
00046 int* ACRun = q_method->ACRun;
00047 Slice *currSlice = currMB->p_slice;
00048 QuantParameters *p_Quant = currMB->p_Img->p_Quant;
00049 int qp = q_method->qp;
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
00055 Boolean is_cavlc = (currSlice->symbol_mode == CAVLC);
00056 int i,j, coeff_ctr;
00057
00058 int *m7;
00059
00060 int level, run = 0;
00061 int nonzero = FALSE;
00062 int qp_per = p_Quant->qp_per_matrix[qp];
00063 const byte *p_scan = &pos_scan[0][0];
00064 int* ACL = &ACLevel[0];
00065 int* ACR = &ACRun[0];
00066
00067 int levelTrellis[16];
00068
00069 currSlice->rdoq_4x4(currMB, tblock, q_method, levelTrellis);
00070
00071
00072 for (coeff_ctr = 0; coeff_ctr < 16; coeff_ctr++)
00073 {
00074 i = *p_scan++;
00075 j = *p_scan++;
00076
00077 m7 = &tblock[j][block_x + i];
00078
00079 if (*m7 != 0)
00080 {
00081
00082
00083
00084
00085 level = levelTrellis[coeff_ctr];
00086
00087 if (level != 0)
00088 {
00089 if (is_cavlc)
00090 level = imin(level, CAVLC_LEVEL_LIMIT);
00091
00092 *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
00093
00094 level = isignab(level, *m7);
00095 *m7 = rshift_rnd_sf(((level * q_params_4x4[j][i].InvScaleComp) << qp_per), 4);
00096 *ACL++ = level;
00097 *ACR++ = run;
00098
00099 run = 0;
00100 nonzero = TRUE;
00101 }
00102 else
00103 {
00104 run++;
00105 *m7 = 0;
00106 }
00107 }
00108 else
00109 {
00110 run++;
00111 }
00112 }
00113
00114 *ACL = 0;
00115
00116 return nonzero;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 void rdoq_4x4_CAVLC(Macroblock *currMB, int **tblock, struct quant_methods *q_method, int levelTrellis[])
00128 {
00129 ImageParameters *p_Img = currMB->p_Img;
00130 int block_x = q_method->block_x;
00131 int block_y = q_method->block_y;
00132 LevelQuantParams **q_params_4x4 = q_method->q_params;
00133 const byte (*pos_scan)[2] = q_method->pos_scan;
00134 const byte *p_scan = &pos_scan[0][0];
00135 int qp = q_method->qp;
00136 QuantParameters *p_Quant = currMB->p_Img->p_Quant;
00137 int qp_per = p_Quant->qp_per_matrix[qp];
00138 int qp_rem = p_Quant->qp_rem_matrix[qp];
00139
00140 levelDataStruct levelData[16];
00141 double lambda_md = 0.0;
00142
00143 int type = LUMA_4x4;
00144 int pos_x = block_x >> BLOCK_SHIFT;
00145 int pos_y = block_y >> BLOCK_SHIFT;
00146 int b8 = 2*(pos_y >> 1) + (pos_x >> 1);
00147 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
00148
00149 if ((p_Img->type==B_SLICE) && p_Img->nal_reference_idc)
00150 {
00151 lambda_md = p_Img->lambda_md[5][p_Img->masterQP];
00152 }
00153 else
00154 {
00155 lambda_md = p_Img->lambda_md[p_Img->type][p_Img->masterQP];
00156 }
00157
00158 init_trellis_data_4x4_CAVLC(currMB, tblock, block_x, qp_per, qp_rem, q_params_4x4, p_scan, &levelData[0], type);
00159 est_RunLevel_CAVLC(currMB, levelData, levelTrellis, LUMA, b8, b4, 16, lambda_md);
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169 void rdoq_4x4_CABAC(Macroblock *currMB, int **tblock, struct quant_methods *q_method, int levelTrellis[])
00170 {
00171 ImageParameters *p_Img = currMB->p_Img;
00172 int block_x = q_method->block_x;
00173 LevelQuantParams **q_params_4x4 = q_method->q_params;
00174 const byte (*pos_scan)[2] = q_method->pos_scan;
00175 const byte *p_scan = &pos_scan[0][0];
00176 int qp = q_method->qp;
00177 QuantParameters *p_Quant = currMB->p_Img->p_Quant;
00178 int qp_per = p_Quant->qp_per_matrix[qp];
00179 int qp_rem = p_Quant->qp_rem_matrix[qp];
00180
00181 levelDataStruct levelData[16];
00182 double lambda_md = 0.0;
00183 int kStart=0, kStop=0, noCoeff = 0, estBits;
00184
00185 int type = LUMA_4x4;
00186
00187 if ((p_Img->type==B_SLICE) && p_Img->nal_reference_idc)
00188 {
00189 lambda_md = p_Img->lambda_md[5][p_Img->masterQP];
00190 }
00191 else
00192 {
00193 lambda_md = p_Img->lambda_md[p_Img->type][p_Img->masterQP];
00194 }
00195
00196 noCoeff = init_trellis_data_4x4_CABAC(currMB, tblock, block_x, qp_per, qp_rem, q_params_4x4, p_scan, &levelData[0], &kStart, &kStop, type);
00197 estBits = est_write_and_store_CBP_block_bit(currMB, LUMA_4x4);
00198 est_writeRunLevel_CABAC(currMB, levelData, levelTrellis, LUMA_4x4, lambda_md, kStart, kStop, noCoeff, estBits);
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208 int quant_ac4x4_trellis(Macroblock *currMB, int **tblock, struct quant_methods *q_method, int type)
00209 {
00210 int block_x = q_method->block_x;
00211
00212 int* ACLevel = q_method->ACLevel;
00213 int* ACRun = q_method->ACRun;
00214 int qp = q_method->qp;
00215 LevelQuantParams **q_params_4x4 = q_method->q_params;
00216 const byte (*pos_scan)[2] = q_method->pos_scan;
00217 const byte *c_cost = q_method->c_cost;
00218 int *coeff_cost = q_method->coeff_cost;
00219
00220 Slice *currSlice = currMB->p_slice;
00221 QuantParameters *p_Quant = currMB->p_Img->p_Quant;
00222 Boolean is_cavlc = (currSlice->symbol_mode == CAVLC);
00223 int i,j, coeff_ctr;
00224
00225 int *m7;
00226 int level, run = 0;
00227 int nonzero = FALSE;
00228 int qp_per = p_Quant->qp_per_matrix[qp];
00229 const byte *p_scan = &pos_scan[1][0];
00230 int* ACL = &ACLevel[0];
00231 int* ACR = &ACRun[0];
00232
00233 int levelTrellis[16];
00234
00235 currSlice->rdoq_ac4x4(currMB, tblock, q_method, levelTrellis, type);
00236
00237
00238 for (coeff_ctr = 1; coeff_ctr < 16; coeff_ctr++)
00239 {
00240 i = *p_scan++;
00241 j = *p_scan++;
00242
00243 m7 = &tblock[j][block_x + i];
00244 if (*m7 != 0)
00245 {
00246
00247
00248
00249
00250 level=levelTrellis[coeff_ctr - 1];
00251
00252 if (level != 0)
00253 {
00254 if (is_cavlc)
00255 level = imin(level, CAVLC_LEVEL_LIMIT);
00256
00257 *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
00258
00259 level = isignab(level, *m7);
00260 *m7 = rshift_rnd_sf(((level * q_params_4x4[j][i].InvScaleComp) << qp_per), 4);
00261
00262
00263
00264 *ACL++ = level;
00265 *ACR++ = run;
00266
00267 run = 0;
00268 nonzero = TRUE;
00269 }
00270 else
00271 {
00272 run++;
00273 *m7 = 0;
00274 }
00275 }
00276 else
00277 {
00278 run++;
00279 }
00280 }
00281
00282 *ACL = 0;
00283
00284 return nonzero;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 void rdoq_ac4x4_CAVLC(Macroblock *currMB, int **tblock, struct quant_methods *q_method, int levelTrellis[], int type)
00296 {
00297 ImageParameters *p_Img = currMB->p_Img;
00298 int block_x = q_method->block_x;
00299 int block_y = q_method->block_y;
00300 LevelQuantParams **q_params_4x4 = q_method->q_params;
00301 const byte (*pos_scan)[2] = q_method->pos_scan;
00302 int qp = q_method->qp;
00303 QuantParameters *p_Quant = p_Img->p_Quant;
00304 int qp_per = p_Quant->qp_per_matrix[qp];
00305 int qp_rem = p_Quant->qp_rem_matrix[qp];
00306
00307 const byte *p_scan = &pos_scan[1][0];
00308 levelDataStruct levelData[16];
00309 double lambda_md = 0.0;
00310
00311 int pos_x = block_x >> BLOCK_SHIFT;
00312 int pos_y = block_y >> BLOCK_SHIFT;
00313 int b8 = 2*(pos_y >> 1) + (pos_x >> 1);
00314 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
00315 int block_type = ( (type == CHROMA_AC) ? CHROMA_AC : LUMA_INTRA16x16AC);
00316
00317 if ((p_Img->type==B_SLICE) && p_Img->nal_reference_idc)
00318 {
00319 lambda_md = p_Img->lambda_md[5][p_Img->masterQP];
00320 }
00321 else
00322 {
00323 lambda_md = p_Img->lambda_md[p_Img->type][p_Img->masterQP];
00324 }
00325
00326 init_trellis_data_4x4_CAVLC(currMB, tblock, block_x, qp_per, qp_rem, q_params_4x4, p_scan, &levelData[0], type);
00327 est_RunLevel_CAVLC(currMB, levelData, levelTrellis, block_type, b8, b4, 15, lambda_md);
00328 }
00329
00330
00331
00332
00333
00334
00335
00336
00337 void rdoq_ac4x4_CABAC(Macroblock *currMB, int **tblock, struct quant_methods *q_method, int levelTrellis[], int type)
00338 {
00339 ImageParameters *p_Img = currMB->p_Img;
00340 int block_x = q_method->block_x;
00341 LevelQuantParams **q_params_4x4 = q_method->q_params;
00342 const byte (*pos_scan)[2] = q_method->pos_scan;
00343 int qp = q_method->qp;
00344 QuantParameters *p_Quant = p_Img->p_Quant;
00345 int qp_per = p_Quant->qp_per_matrix[qp];
00346 int qp_rem = p_Quant->qp_rem_matrix[qp];
00347
00348 const byte *p_scan = &pos_scan[1][0];
00349 levelDataStruct levelData[16];
00350 double lambda_md = 0.0;
00351 int kStart = 0, kStop = 0, noCoeff = 0, estBits;
00352
00353 if ((p_Img->type==B_SLICE) && p_Img->nal_reference_idc)
00354 {
00355 lambda_md = p_Img->lambda_md[5][p_Img->masterQP];
00356 }
00357 else
00358 {
00359 lambda_md = p_Img->lambda_md[p_Img->type][p_Img->masterQP];
00360 }
00361
00362 noCoeff = init_trellis_data_4x4_CABAC(currMB, tblock, block_x, qp_per, qp_rem, q_params_4x4, p_scan, &levelData[0], &kStart, &kStop, type);
00363 estBits = est_write_and_store_CBP_block_bit(currMB, type);
00364 est_writeRunLevel_CABAC(currMB, levelData, levelTrellis, type, lambda_md, kStart, kStop, noCoeff, estBits);
00365 }
00366
00367
00368
00369
00370
00371
00372
00373
00374 int quant_dc4x4_trellis(Macroblock *currMB, int **tblock, int qp, int* DCLevel, int* DCRun,
00375 LevelQuantParams *q_params_4x4, const byte (*pos_scan)[2])
00376 {
00377 Slice *currSlice = currMB->p_slice;
00378 QuantParameters *p_Quant = currMB->p_Img->p_Quant;
00379 Boolean is_cavlc = (currSlice->symbol_mode == CAVLC);
00380 int i,j, coeff_ctr;
00381
00382 int *m7;
00383
00384 int level, run = 0;
00385 int nonzero = FALSE;
00386 int qp_per = p_Quant->qp_per_matrix[qp];
00387 int qp_rem = p_Quant->qp_rem_matrix[qp];
00388 const byte *p_scan = &pos_scan[0][0];
00389 int* DCL = &DCLevel[0];
00390 int* DCR = &DCRun[0];
00391
00392 int levelTrellis[16];
00393
00394 currSlice->rdoq_dc(currMB, tblock, qp_per, qp_rem, q_params_4x4, pos_scan, levelTrellis, LUMA_16DC);
00395
00396
00397 for (coeff_ctr = 0; coeff_ctr < 16; coeff_ctr++)
00398 {
00399 i = *p_scan++;
00400 j = *p_scan++;
00401
00402 m7 = &tblock[j][i];
00403
00404 if (*m7 != 0)
00405 {
00406 level = levelTrellis[coeff_ctr];
00407
00408 if (level != 0)
00409 {
00410 if (is_cavlc)
00411 level = imin(level, CAVLC_LEVEL_LIMIT);
00412
00413 level = isignab(level, *m7);
00414 *m7 = level;
00415 *DCL++ = level;
00416 *DCR++ = run;
00417
00418 run = 0;
00419 nonzero = TRUE;
00420 }
00421 else
00422 {
00423 run++;
00424 *m7 = 0;
00425 }
00426 }
00427 else
00428 {
00429 run++;
00430 }
00431 }
00432
00433 *DCL = 0;
00434
00435 return nonzero;
00436 }
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 void rdoq_dc_CAVLC(Macroblock *currMB, int **tblock, int qp_per, int qp_rem, LevelQuantParams *q_params_4x4,
00447 const byte (*pos_scan)[2], int levelTrellis[], int type)
00448 {
00449 ImageParameters *p_Img = currMB->p_Img;
00450 const byte *p_scan = &pos_scan[0][0];
00451 levelDataStruct levelData[16];
00452 double lambda_md = 0.0;
00453
00454 if ((p_Img->type==B_SLICE) && p_Img->nal_reference_idc)
00455 {
00456 lambda_md = p_Img->lambda_md[5][p_Img->masterQP];
00457 }
00458 else
00459 {
00460 lambda_md = p_Img->lambda_md[p_Img->type][p_Img->masterQP];
00461 }
00462
00463 init_trellis_data_DC_CAVLC(currMB, tblock, qp_per, qp_rem, q_params_4x4, p_scan, &levelData[0]);
00464 est_RunLevel_CAVLC(currMB, levelData, levelTrellis, LUMA_INTRA16x16DC, 0, 0, 16, lambda_md);
00465 }
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 void rdoq_dc_CABAC(Macroblock *currMB, int **tblock, int qp_per, int qp_rem, LevelQuantParams *q_params_4x4, const byte (*pos_scan)[2], int levelTrellis[], int type)
00477 {
00478 ImageParameters *p_Img = currMB->p_Img;
00479 const byte *p_scan = &pos_scan[0][0];
00480 levelDataStruct levelData[16];
00481 double lambda_md = 0.0;
00482 int kStart=0, kStop=0, noCoeff = 0, estBits;
00483
00484 if ((p_Img->type==B_SLICE) && p_Img->nal_reference_idc)
00485 {
00486 lambda_md = p_Img->lambda_md[5][p_Img->masterQP];
00487 }
00488 else
00489 {
00490 lambda_md = p_Img->lambda_md[p_Img->type][p_Img->masterQP];
00491 }
00492
00493 noCoeff = init_trellis_data_DC_CABAC(currMB, tblock, qp_per, qp_rem, q_params_4x4, p_scan, &levelData[0], &kStart, &kStop);
00494 estBits = est_write_and_store_CBP_block_bit(currMB, type);
00495 est_writeRunLevel_CABAC(currMB, levelData, levelTrellis, type, lambda_md, kStart, kStop, noCoeff, estBits);
00496 }
00497