00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "contributors.h"
00011
00012 #include <math.h>
00013 #include <float.h>
00014
00015 #include "global.h"
00016 #include "cabac.h"
00017 #include "image.h"
00018 #include "fmo.h"
00019 #include "macroblock.h"
00020 #include "mb_access.h"
00021 #include "rdoq.h"
00022
00023 static const int entropyBits[128]=
00024 {
00025 895, 943, 994, 1048, 1105, 1165, 1228, 1294,
00026 1364, 1439, 1517, 1599, 1686, 1778, 1875, 1978,
00027 2086, 2200, 2321, 2448, 2583, 2725, 2876, 3034,
00028 3202, 3380, 3568, 3767, 3977, 4199, 4435, 4684,
00029 4948, 5228, 5525, 5840, 6173, 6527, 6903, 7303,
00030 7727, 8178, 8658, 9169, 9714, 10294, 10914, 11575,
00031 12282, 13038, 13849, 14717, 15650, 16653, 17734, 18899,
00032 20159, 21523, 23005, 24617, 26378, 28306, 30426, 32768,
00033 32768, 35232, 37696, 40159, 42623, 45087, 47551, 50015,
00034 52479, 54942, 57406, 59870, 62334, 64798, 67262, 69725,
00035 72189, 74653, 77117, 79581, 82044, 84508, 86972, 89436,
00036 91900, 94363, 96827, 99291, 101755, 104219, 106683, 109146,
00037 111610, 114074, 116538, 119002, 121465, 123929, 126393, 128857,
00038 131321, 133785, 136248, 138712, 141176, 143640, 146104, 148568,
00039 151031, 153495, 155959, 158423, 160887, 163351, 165814, 168278,
00040 170742, 173207, 175669, 178134, 180598, 183061, 185525, 187989
00041 };
00042
00043
00044 static int biari_no_bits(signed short symbol, BiContextTypePtr bi_ct )
00045 {
00046 int ctx_state, estBits;
00047
00048 symbol = (short) (symbol != 0);
00049
00050 ctx_state = (symbol == bi_ct->MPS) ? 64 + bi_ct->state : 63 - bi_ct->state;
00051 estBits=entropyBits[127 - ctx_state];
00052
00053 return(estBits);
00054 }
00055
00056 static int biari_state(signed short symbol, BiContextTypePtr bi_ct )
00057 {
00058 int ctx_state;
00059
00060 symbol = (short) (symbol != 0);
00061 ctx_state = (symbol == bi_ct->MPS) ? 64 + bi_ct->state : 63 - bi_ct->state;
00062
00063 return(ctx_state);
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 static void est_CBP_block_bit (Macroblock* currMB, int type)
00073 {
00074 Slice *currSlice = currMB->p_slice;
00075 estBitsCabacStruct *cabacEstBits = &currSlice->estBitsCabac[type];
00076 int ctx;
00077
00078 for (ctx=0; ctx<=3; ctx++)
00079 {
00080 cabacEstBits->blockCbpBits[ctx][0]=biari_no_bits(0, currSlice->tex_ctx->bcbp_contexts[type2ctx_bcbp[type]]+ctx);
00081
00082 cabacEstBits->blockCbpBits[ctx][1]=biari_no_bits(1, currSlice->tex_ctx->bcbp_contexts[type2ctx_bcbp[type]]+ctx);
00083 }
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 static void est_significance_map(Macroblock* currMB, int type)
00093 {
00094 Slice *currSlice = currMB->p_slice;
00095 int k;
00096 int k1 = maxpos[type]-1;
00097 #if ENABLE_FIELD_CTX
00098 int fld = ( currSlice->structure!=FRAME || currMB->mb_field );
00099 #else
00100 int fld = 0;
00101 #endif
00102 BiContextTypePtr map_ctx = currSlice->tex_ctx->map_contexts [fld][type2ctx_map [type]];
00103 BiContextTypePtr last_ctx = currSlice->tex_ctx->last_contexts[fld][type2ctx_last[type]];
00104 const byte *pos2ctxmap = fld ? pos2ctx_map_int [type] : pos2ctx_map[type];
00105 const byte *pos2ctxlast = pos2ctx_last[type];
00106
00107 estBitsCabacStruct *cabacEstBits = &currSlice->estBitsCabac[type];
00108
00109 for (k = 0; k < k1; k++)
00110 {
00111 cabacEstBits->significantBits[pos2ctxmap[k]][0] = biari_no_bits (0, map_ctx + pos2ctxmap[k]);
00112
00113 cabacEstBits->significantBits[pos2ctxmap[k]][1] = biari_no_bits (1, map_ctx + pos2ctxmap[k]);
00114
00115 cabacEstBits->lastBits[pos2ctxlast[k]][0] = biari_no_bits(0, last_ctx+pos2ctxlast[k]);
00116
00117 cabacEstBits->lastBits[pos2ctxlast[k]][1] = biari_no_bits(1, last_ctx+pos2ctxlast[k]);
00118 }
00119
00120 cabacEstBits->significantBits[pos2ctxmap[k1]][0]=0;
00121 cabacEstBits->significantBits[pos2ctxmap[k1]][1]=0;
00122 cabacEstBits->lastBits[pos2ctxlast[k1]][0]=0;
00123 cabacEstBits->lastBits[pos2ctxlast[k1]][1]=0;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 static void est_significant_coefficients (Macroblock* currMB, int type)
00133 {
00134 Slice *currSlice = currMB->p_slice;
00135 int ctx;
00136 int maxCtx = imin(4, max_c2[type]);
00137 estBitsCabacStruct *cabacEstBits = &currSlice->estBitsCabac[type];
00138
00139 for (ctx=0; ctx<=4; ctx++){
00140 cabacEstBits->greaterOneBits[0][ctx][0]=
00141 biari_no_bits (0, currSlice->tex_ctx->one_contexts[type2ctx_one[type]] + ctx);
00142
00143 cabacEstBits->greaterOneBits[0][ctx][1]=
00144 biari_no_bits (1, currSlice->tex_ctx->one_contexts[type2ctx_one[type]] + ctx);
00145 }
00146
00147 for (ctx=0; ctx<=maxCtx; ctx++){
00148 cabacEstBits->greaterOneBits[1][ctx][0]=
00149 biari_no_bits(0, currSlice->tex_ctx->abs_contexts[type2ctx_abs[type]] + ctx);
00150
00151 cabacEstBits->greaterOneState[ctx]=biari_state(0, currSlice->tex_ctx->abs_contexts[type2ctx_abs[type]] + ctx);
00152
00153 cabacEstBits->greaterOneBits[1][ctx][1]=
00154 biari_no_bits(1, currSlice->tex_ctx->abs_contexts[type2ctx_abs[type]] + ctx);
00155 }
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 static int est_unary_exp_golomb_level_encode(Macroblock *currMB, unsigned int symbol, int ctx, int type)
00165 {
00166 Slice *currSlice = currMB->p_slice;
00167 estBitsCabacStruct *cabacEstBits = &currSlice->estBitsCabac[type];
00168 unsigned int l = symbol, k = 1;
00169 unsigned int exp_start = 13;
00170 int estBits;
00171
00172 if (symbol==0)
00173 {
00174 estBits = cabacEstBits->greaterOneBits[1][ctx][0];
00175 return (estBits);
00176 }
00177 else
00178 {
00179 estBits = cabacEstBits->greaterOneBits[1][ctx][1];
00180
00181 while (((--l)>0) && (++k <= exp_start))
00182 {
00183 estBits += cabacEstBits->greaterOneBits[1][ctx][1];
00184 }
00185
00186 if (symbol < exp_start)
00187 {
00188 estBits += cabacEstBits->greaterOneBits[1][ctx][0];
00189 }
00190 else
00191 {
00192 estBits+=est_exp_golomb_encode_eq_prob(symbol - exp_start);
00193 }
00194 }
00195 return(estBits);
00196 }
00197
00198 void precalculate_unary_exp_golomb_level(ImageParameters *p_Img)
00199 {
00200 int state, ctx_state0, ctx_state1, estBits0, estBits1, symbol;
00201
00202 for (state=0; state<=63; state++)
00203 {
00204
00205 ctx_state0 = 64 + state;
00206 estBits0 = entropyBits[127-ctx_state0];
00207 ctx_state1 = 63 - state;
00208 estBits1 = entropyBits[127-ctx_state1];
00209
00210 for (symbol = 0; symbol < MAX_PREC_COEFF; symbol++)
00211 {
00212 p_Img->precalcUnaryLevelTab[ctx_state0][symbol] = est_unary_exp_golomb_level_bits(symbol, estBits0, estBits1);
00213
00214
00215 p_Img->precalcUnaryLevelTab[ctx_state1][symbol] =est_unary_exp_golomb_level_bits(symbol, estBits1, estBits0);
00216 }
00217 }
00218 }
00219
00220 int est_unary_exp_golomb_level_bits(unsigned int symbol, int bits0, int bits1)
00221 {
00222 unsigned int l,k;
00223 unsigned int exp_start = 13;
00224 int estBits;
00225
00226 if (symbol == 0)
00227 {
00228 return (bits0);
00229 }
00230 else
00231 {
00232 estBits = bits1;
00233 l = symbol;
00234 k = 1;
00235 while (((--l)>0) && (++k <= exp_start))
00236 {
00237 estBits += bits1;
00238 }
00239 if (symbol < exp_start)
00240 {
00241 estBits += bits0;
00242 }
00243 else
00244 {
00245 estBits += est_exp_golomb_encode_eq_prob(symbol - exp_start);
00246 }
00247 }
00248 return(estBits);
00249 }
00250
00251
00252
00253
00254
00255
00256
00257 int est_exp_golomb_encode_eq_prob(unsigned int symbol)
00258 {
00259 int k = 0, estBits = 0;
00260
00261 for(;;)
00262 {
00263 if (symbol >= (unsigned int)(1<<k))
00264 {
00265 estBits++;
00266 symbol -= (1<<k);
00267 k++;
00268 }
00269 else
00270 {
00271 estBits += (k + 1);
00272 break;
00273 }
00274 }
00275 return(estBits);
00276 }
00277
00278
00279
00280
00281
00282
00283
00284 void estRunLevel_CABAC (Macroblock *currMB, int context)
00285 {
00286 est_CBP_block_bit (currMB, context);
00287
00288 est_significance_map (currMB, context);
00289
00290 est_significant_coefficients (currMB, context);
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300 int est_write_and_store_CBP_block_bit(Macroblock* currMB, int type)
00301 {
00302 Slice *currSlice = currMB->p_slice;
00303 ImageParameters *p_Img = currMB->p_Img;
00304 estBitsCabacStruct *cabacEstBits = &currSlice->estBitsCabac[type];
00305
00306 int y_ac = (type==LUMA_16AC || type==LUMA_8x8 || type==LUMA_8x4 || type==LUMA_4x8 || type==LUMA_4x4
00307 || type==CB_16AC || type==CB_8x8 || type==CB_8x4 || type==CB_4x8 || type==CB_4x4
00308 || type==CR_16AC || type==CR_8x8 || type==CR_8x4 || type==CR_4x8 || type==CR_4x4);
00309 int y_dc = (type==LUMA_16DC || type==CB_16DC || type==CR_16DC);
00310 int u_ac = (type==CHROMA_AC && !p_Img->is_v_block);
00311 int v_ac = (type==CHROMA_AC && p_Img->is_v_block);
00312 int chroma_dc = (type==CHROMA_DC || type==CHROMA_DC_2x4 || type==CHROMA_DC_4x4);
00313 int u_dc = (chroma_dc && !p_Img->is_v_block);
00314 int v_dc = (chroma_dc && p_Img->is_v_block);
00315 int j = (y_ac || u_ac || v_ac ? currMB->subblock_y : 0);
00316 int i = (y_ac || u_ac || v_ac ? currMB->subblock_x : 0);
00317 int bit;
00318 int default_bit = (IS_INTRA(currMB) ? 1 : 0);
00319 int upper_bit = default_bit;
00320 int left_bit = default_bit;
00321 int ctx, estBits = 0;
00322
00323 int bit_pos_a = 0;
00324 int bit_pos_b = 0;
00325
00326 PixelPos block_a, block_b;
00327
00328 if (y_ac || y_dc)
00329 {
00330 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_LUMA], &block_a);
00331 get4x4Neighbour(currMB, i, j -1, p_Img->mb_size[IS_LUMA], &block_b);
00332 if (y_ac)
00333 {
00334 if (block_a.available)
00335 bit_pos_a = 4*block_a.y + block_a.x;
00336 if (block_b.available)
00337 bit_pos_b = 4*block_b.y + block_b.x;
00338 }
00339 }
00340 else
00341 {
00342 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_CHROMA], &block_a);
00343 get4x4Neighbour(currMB, i, j - 1, p_Img->mb_size[IS_CHROMA], &block_b);
00344 if (u_ac||v_ac)
00345 {
00346 if (block_a.available)
00347 bit_pos_a = (block_a.y << 2) + block_a.x;
00348 if (block_b.available)
00349 bit_pos_b = (block_b.y << 2) + block_b.x;
00350 }
00351 }
00352
00353 bit = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 35);
00354
00355 if (p_Img->enc_picture->chroma_format_idc!=YUV444 || IS_INDEPENDENT(currMB->p_Inp))
00356 {
00357 if (type!=LUMA_8x8)
00358 {
00359 if (block_b.available)
00360 {
00361 if(p_Img->mb_data[block_b.mb_addr].mb_type==IPCM)
00362 upper_bit = 1;
00363 else
00364 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[0], bit + bit_pos_b);
00365 }
00366
00367 if (block_a.available)
00368 {
00369 if(p_Img->mb_data[block_a.mb_addr].mb_type==IPCM)
00370 left_bit = 1;
00371 else
00372 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[0], bit + bit_pos_a);
00373 }
00374
00375 ctx = 2*upper_bit+left_bit;
00376
00377 estBits = cabacEstBits->blockCbpBits[ctx][0] - cabacEstBits->blockCbpBits[ctx][1];
00378 }
00379 }
00380 else
00381 {
00382 if (block_b.available)
00383 {
00384 if(p_Img->mb_data[block_b.mb_addr].mb_type == IPCM)
00385 upper_bit=1;
00386 else
00387 {
00388 if(type==LUMA_8x8)
00389 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits_8x8[0], bit + bit_pos_b);
00390 else if (type==CB_8x8)
00391 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits_8x8[1], bit + bit_pos_b);
00392 else if (type==CR_8x8)
00393 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits_8x8[2], bit + bit_pos_b);
00394 else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
00395 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[1], bit + bit_pos_b);
00396 else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
00397 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[2], bit + bit_pos_b);
00398 else
00399 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[0], bit + bit_pos_b);
00400 }
00401 }
00402
00403 if (block_a.available)
00404 {
00405 if(p_Img->mb_data[block_a.mb_addr].mb_type==IPCM)
00406 left_bit = 1;
00407 else
00408 {
00409 if(type==LUMA_8x8)
00410 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits_8x8[0],bit + bit_pos_a);
00411 else if (type==CB_8x8)
00412 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits_8x8[1],bit + bit_pos_a);
00413 else if (type==CR_8x8)
00414 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits_8x8[2],bit + bit_pos_a);
00415 else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
00416 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[1], bit + bit_pos_a);
00417 else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
00418 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[2], bit + bit_pos_a);
00419 else
00420 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[0], bit + bit_pos_a);
00421 }
00422 }
00423
00424 ctx = 2*upper_bit+left_bit;
00425
00426
00427 estBits = cabacEstBits->blockCbpBits[ctx][0] - cabacEstBits->blockCbpBits[ctx][1];
00428 }
00429
00430 return(estBits);
00431 }
00432
00433
00434
00435
00436
00437
00438 void est_writeRunLevel_CABAC(Macroblock *currMB, levelDataStruct levelData[], int levelTabMin[], int type, double lambda, int kInit, int kStop,
00439 int noCoeff, int estCBP)
00440 {
00441 ImageParameters *p_Img = currMB->p_Img;
00442 Slice *currSlice = currMB->p_slice;
00443 estBitsCabacStruct *cabacEstBits = &currSlice->estBitsCabac[type];
00444 int k, i;
00445 int estBits;
00446 double lagr, lagrMin=0, lagrTabMin, lagrTab;
00447 int c1 = 1, c2 = 0, c1Tab[3], c2Tab[3];
00448 int iBest, levelTab[64];
00449 int ctx, greater_one, last, maxK = maxpos[type];
00450 double lagrAcc, lagrLastMin=0, lagrLast;
00451 int kBest=0, kStart, first;
00452 levelDataStruct *dataLevel;
00453
00454 for (k = 0; k <= maxK; k++)
00455 {
00456 levelTabMin[k] = 0;
00457 }
00458
00459 if (noCoeff > 0)
00460 {
00461 if (noCoeff > 1)
00462 {
00463 kStart = kInit; kBest = 0; first = 1;
00464 lagrAcc = 0;
00465 for (k = kStart; k <= kStop; k++)
00466 {
00467 lagrAcc += levelData[k].errLevel[0];
00468 }
00469
00470 if (levelData[kStart].noLevels > 2)
00471 {
00472 lagrAcc -= levelData[kStart].errLevel[0];
00473 lagrLastMin=lambda * (cabacEstBits->lastBits[pos2ctx_last[type][kStart]][1] - cabacEstBits->lastBits[pos2ctx_last[type][kStart]][0]) + lagrAcc;
00474
00475 kBest = kStart;
00476 kStart++;
00477 first = 0;
00478 }
00479
00480 for (k = kStart; k <= kStop; k++)
00481 {
00482 dataLevel = &levelData[k];
00483 lagrMin = dataLevel->errLevel[0] + lambda * cabacEstBits->significantBits[pos2ctx_map[type][k]][0];
00484 lagrAcc -= dataLevel->errLevel[0];
00485
00486 if (dataLevel->noLevels > 1)
00487 {
00488 estBits = SIGN_BITS + cabacEstBits->significantBits[pos2ctx_map[type][k]][1]+
00489 cabacEstBits->greaterOneBits[0][4][0];
00490
00491 lagrLast = dataLevel->errLevel[1] + lambda * (estBits + cabacEstBits->lastBits[pos2ctx_last[type][k]][1]) + lagrAcc;
00492 lagr = dataLevel->errLevel[1] + lambda * (estBits + cabacEstBits->lastBits[pos2ctx_last[type][k]][0]);
00493
00494 lagrMin = (lagr < lagrMin) ? lagr : lagrMin;
00495
00496 if (lagrLast < lagrLastMin || first==1)
00497 {
00498 kBest = k;
00499 first = 0;
00500 lagrLastMin = lagrLast;
00501 }
00502 }
00503 lagrAcc += lagrMin;
00504 }
00505 kStart = kBest;
00506 }
00507 else
00508 {
00509 kStart = kStop;
00510 }
00511
00512 lagrTabMin = 0;
00513 for (k = 0; k <= kStart; k++)
00514 {
00515 lagrTabMin += levelData[k].errLevel[0];
00516 }
00517
00518 lagrTab=0;
00519
00520
00521
00522 lagrTabMin += (lambda*estCBP);
00523 iBest = 0; first = 1;
00524 for (k = kStart; k >= 0; k--)
00525 {
00526 dataLevel = &levelData[k];
00527 last = (k == kStart);
00528
00529 if (!last)
00530 {
00531 lagrMin = dataLevel->errLevel[0] + lambda * cabacEstBits->significantBits[pos2ctx_map[type][k]][0];
00532 iBest = 0;
00533 first = 0;
00534 }
00535
00536 for (i = 1; i < dataLevel->noLevels; i++)
00537 {
00538 estBits = SIGN_BITS + cabacEstBits->significantBits[pos2ctx_map[type][k]][1];
00539 estBits += cabacEstBits->lastBits[pos2ctx_last[type][k]][last];
00540
00541
00542 greater_one = (dataLevel->level[i] > 1);
00543
00544 c1Tab[i] = c1;
00545 c2Tab[i] = c2;
00546
00547 ctx = imin(c1Tab[i], 4);
00548 estBits += cabacEstBits->greaterOneBits[0][ctx][greater_one];
00549
00550
00551 if (greater_one)
00552 {
00553 ctx = imin(c2Tab[i], max_c2[type]);
00554 if ( (dataLevel->level[i] - 2) < MAX_PREC_COEFF)
00555 {
00556 estBits += p_Img->precalcUnaryLevelTab[cabacEstBits->greaterOneState[ctx]][dataLevel->level[i] - 2];
00557 }
00558 else
00559 {
00560 estBits += est_unary_exp_golomb_level_encode(currMB, dataLevel->level[i] - 2, ctx, type);
00561 }
00562
00563 c1Tab[i] = 0;
00564 c2Tab[i]++;
00565 }
00566 else if (c1Tab[i])
00567 {
00568 c1Tab[i]++;
00569 }
00570
00571 lagr = dataLevel->errLevel[i] + lambda*estBits;
00572 if (lagr<lagrMin || first==1)
00573 {
00574 iBest = i;
00575 lagrMin=lagr;
00576 first = 0;
00577 }
00578 }
00579
00580 if (iBest>0)
00581 {
00582 c1 = c1Tab[iBest];
00583 c2 = c2Tab[iBest];
00584 }
00585
00586 levelTab[k] = dataLevel->level[iBest];
00587 lagrTab += lagrMin;
00588 }
00589
00590
00591 if (lagrTab < lagrTabMin)
00592 {
00593 memcpy(levelTabMin, levelTab, (kStart + 1) * sizeof(int));
00594 }
00595 }
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605 int init_trellis_data_4x4_CABAC(Macroblock *currMB, int **tblock, int block_x, int qp_per, int qp_rem,
00606 LevelQuantParams **q_params_4x4, const byte *p_scan,
00607 levelDataStruct *dataLevel, int* kStart, int* kStop, int type)
00608 {
00609 Slice *currSlice = currMB->p_slice;
00610 int noCoeff = 0;
00611 int i, j, coeff_ctr;
00612 int *m7;
00613 int end_coeff_ctr = ( ( type == LUMA_4x4 ) ? 16 : 15 );
00614 int q_bits = Q_BITS + qp_per;
00615 int q_offset = ( 1 << (q_bits - 1) );
00616 int level, lowerInt, k;
00617 double err, estErr;
00618
00619
00620 for (coeff_ctr = 0; coeff_ctr < end_coeff_ctr; coeff_ctr++)
00621 {
00622 i = *p_scan++;
00623 j = *p_scan++;
00624
00625 m7 = &tblock[j][block_x + i];
00626
00627 if (*m7 == 0)
00628 {
00629 dataLevel->levelDouble = 0;
00630 dataLevel->level[0] = 0;
00631 dataLevel->noLevels = 1;
00632 err = 0.0;
00633 dataLevel->errLevel[0] = 0.0;
00634 }
00635 else
00636 {
00637 estErr = ((double) estErr4x4[qp_rem][j][i]) / currSlice->norm_factor_4x4;
00638
00639 dataLevel->levelDouble = iabs(*m7 * q_params_4x4[j][i].ScaleComp);
00640 level = (dataLevel->levelDouble >> q_bits);
00641
00642 lowerInt = (((int)dataLevel->levelDouble - (level << q_bits)) < q_offset )? 1 : 0;
00643
00644 dataLevel->level[0] = 0;
00645 if (level == 0 && lowerInt == 1)
00646 {
00647 dataLevel->noLevels = 1;
00648 }
00649 else if (level == 0 && lowerInt == 0)
00650 {
00651 dataLevel->level[1] = 1;
00652 dataLevel->noLevels = 2;
00653 *kStop = coeff_ctr;
00654 noCoeff++;
00655 }
00656 else if (level > 0 && lowerInt == 1)
00657 {
00658 dataLevel->level[1] = level;
00659 dataLevel->noLevels = 2;
00660 *kStop = coeff_ctr;
00661 noCoeff++;
00662 }
00663 else
00664 {
00665 dataLevel->level[1] = level;
00666 dataLevel->level[2] = level + 1;
00667 dataLevel->noLevels = 3;
00668 *kStop = coeff_ctr;
00669 *kStart = coeff_ctr;
00670 noCoeff++;
00671 }
00672
00673 for (k = 0; k < dataLevel->noLevels; k++)
00674 {
00675 err = (double)(dataLevel->level[k] << q_bits) - (double)dataLevel->levelDouble;
00676 dataLevel->errLevel[k] = (err * err * estErr);
00677 }
00678 }
00679
00680 dataLevel++;
00681 }
00682 return (noCoeff);
00683 }
00684
00685
00686
00687
00688
00689
00690
00691 int init_trellis_data_8x8_CABAC(Macroblock *currMB, int **tblock, int block_x, int qp_per, int qp_rem, LevelQuantParams **q_params_8x8, const byte *p_scan,
00692 levelDataStruct *dataLevel, int* kStart, int* kStop)
00693 {
00694 Slice *currSlice = currMB->p_slice;
00695 int noCoeff = 0;
00696 int *m7;
00697 int i, j, coeff_ctr, end_coeff_ctr = 64;
00698 int q_bits = Q_BITS_8 + qp_per;
00699 int q_offset = ( 1 << (q_bits - 1) );
00700 double err, estErr;
00701 int level, lowerInt, k;
00702
00703 for (coeff_ctr = 0; coeff_ctr < end_coeff_ctr; coeff_ctr++)
00704 {
00705 i = *p_scan++;
00706 j = *p_scan++;
00707
00708 m7 = &tblock[j][block_x + i];
00709
00710 if (*m7 == 0)
00711 {
00712 dataLevel->levelDouble = 0;
00713 dataLevel->level[0] = 0;
00714 dataLevel->noLevels = 1;
00715 err = 0.0;
00716 dataLevel->errLevel[0] = 0.0;
00717 }
00718 else
00719 {
00720 estErr = (double) estErr8x8[qp_rem][j][i] / currSlice->norm_factor_8x8;
00721
00722 dataLevel->levelDouble = iabs(*m7 * q_params_8x8[j][i].ScaleComp);
00723 level = (dataLevel->levelDouble >> q_bits);
00724
00725 lowerInt = (((int)dataLevel->levelDouble - (level << q_bits)) < q_offset )? 1 : 0;
00726
00727 dataLevel->level[0] = 0;
00728 if (level == 0 && lowerInt == 1)
00729 {
00730 dataLevel->noLevels = 1;
00731 }
00732 else if (level == 0 && lowerInt == 0)
00733 {
00734 dataLevel->level[1] = 1;
00735 dataLevel->noLevels = 2;
00736 *kStop = coeff_ctr;
00737 noCoeff++;
00738 }
00739 else if (level > 0 && lowerInt == 1)
00740 {
00741 dataLevel->level[1] = level;
00742 dataLevel->noLevels = 2;
00743 *kStop = coeff_ctr;
00744 noCoeff++;
00745 }
00746 else
00747 {
00748 dataLevel->level[1] = level;
00749 dataLevel->level[2] = level + 1;
00750 dataLevel->noLevels = 3;
00751 *kStop = coeff_ctr;
00752 *kStart = coeff_ctr;
00753 noCoeff++;
00754 }
00755
00756 for (k = 0; k < dataLevel->noLevels; k++)
00757 {
00758 err = (double)(dataLevel->level[k] << q_bits) - (double)dataLevel->levelDouble;
00759 dataLevel->errLevel[k] = err * err * estErr;
00760 }
00761 }
00762
00763 dataLevel++;
00764 }
00765 return (noCoeff);
00766 }
00767
00768
00769
00770
00771
00772
00773
00774 int init_trellis_data_DC_CABAC(Macroblock *currMB, int **tblock, int qp_per, int qp_rem,
00775 LevelQuantParams *q_params_4x4, const byte *p_scan,
00776 levelDataStruct *dataLevel, int* kStart, int* kStop)
00777 {
00778 Slice *currSlice = currMB->p_slice;
00779 int noCoeff = 0;
00780 int i, j, coeff_ctr, end_coeff_ctr = 16;
00781 int q_bits = Q_BITS + qp_per + 1;
00782 int q_offset = ( 1 << (q_bits - 1) );
00783 int level, lowerInt, k;
00784 int *m7;
00785 double err, estErr = (double) estErr4x4[qp_rem][0][0] / currSlice->norm_factor_4x4;
00786
00787 for (coeff_ctr = 0; coeff_ctr < end_coeff_ctr; coeff_ctr++)
00788 {
00789 i = *p_scan++;
00790 j = *p_scan++;
00791 m7 = &tblock[j][i];
00792
00793 if (*m7 == 0)
00794 {
00795 dataLevel->levelDouble = 0;
00796 dataLevel->level[0] = 0;
00797 dataLevel->noLevels = 1;
00798 err = 0.0;
00799 dataLevel->errLevel[0] = 0.0;
00800 }
00801 else
00802 {
00803 dataLevel->levelDouble = iabs(*m7 * q_params_4x4->ScaleComp);
00804 level = (dataLevel->levelDouble >> q_bits);
00805
00806 lowerInt=( ((int)dataLevel->levelDouble - (level<<q_bits)) < q_offset )? 1 : 0;
00807
00808 dataLevel->level[0] = 0;
00809 if (level == 0 && lowerInt == 1)
00810 {
00811 dataLevel->noLevels = 1;
00812 }
00813 else if (level == 0 && lowerInt == 0)
00814 {
00815 dataLevel->level[1] = 1;
00816 dataLevel->noLevels = 2;
00817 *kStop = coeff_ctr;
00818 noCoeff++;
00819 }
00820 else if (level > 0 && lowerInt == 1)
00821 {
00822 dataLevel->level[1] = level;
00823 dataLevel->noLevels = 2;
00824 *kStop = coeff_ctr;
00825 noCoeff++;
00826 }
00827 else
00828 {
00829 dataLevel->level[1] = level;
00830 dataLevel->level[2] = level + 1;
00831 dataLevel->noLevels = 3;
00832 *kStop = coeff_ctr;
00833 *kStart = coeff_ctr;
00834 noCoeff++;
00835 }
00836
00837 for (k = 0; k < dataLevel->noLevels; k++)
00838 {
00839 err = (double)(dataLevel->level[k] << q_bits) - (double)dataLevel->levelDouble;
00840 dataLevel->errLevel[k] = (err * err * estErr);
00841 }
00842 }
00843 dataLevel++;
00844 }
00845 return (noCoeff);
00846 }
00847
00848
00849