00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "global.h"
00015
00016 #include "cabac.h"
00017 #include "biariencode.h"
00018 #include "image.h"
00019 #include "mb_access.h"
00020
00021 #if TRACE
00022 #define CABAC_TRACE if (dp->bitstream->trace_enabled) trace2out_cabac (se)
00023 #else
00024 #define CABAC_TRACE
00025 #endif
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 static const byte pos2ctx_map8x8 [] = {
00037 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
00038 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9, 10, 9, 8, 7,
00039 7, 6, 11, 12, 13, 11, 6, 7, 8, 9, 14, 10, 9, 8, 6, 11,
00040 12, 13, 11, 6, 9, 14, 10, 9, 11, 12, 13, 11 ,14, 10, 12, 14
00041 };
00042
00043 static const byte pos2ctx_map8x4 [] = {
00044 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 9, 8, 6, 7, 8,
00045 9, 10, 11, 9, 8, 6, 12, 8, 9, 10, 11, 9, 13, 13, 14, 14
00046 };
00047
00048 static const byte pos2ctx_map4x4 [] = {
00049 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14
00050 };
00051
00052 static const byte pos2ctx_map2x4c[] = {
00053 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
00054 };
00055
00056 static const byte pos2ctx_map4x4c[] = {
00057 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2
00058 };
00059
00060 const byte* pos2ctx_map [] = {
00061 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8, pos2ctx_map8x4,
00062 pos2ctx_map8x4, pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
00063 pos2ctx_map2x4c, pos2ctx_map4x4c,
00064 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8,pos2ctx_map8x4,
00065 pos2ctx_map8x4, pos2ctx_map4x4,
00066 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8,pos2ctx_map8x4,
00067 pos2ctx_map8x4,pos2ctx_map4x4
00068 };
00069
00070
00071
00072 static const byte pos2ctx_map8x8i[] = {
00073 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
00074 6, 9, 10, 10, 8, 11, 12, 11, 9, 9, 10, 10, 8, 11, 12, 11,
00075 9, 9, 10, 10, 8, 11, 12, 11, 9, 9, 10, 10, 8, 13, 13, 9,
00076 9, 10, 10, 8, 13, 13, 9, 9, 10, 10, 14, 14, 14, 14, 14, 14
00077 };
00078
00079 static const byte pos2ctx_map8x4i[] = {
00080 0, 1, 2, 3, 4, 5, 6, 3, 4, 5, 6, 3, 4, 7, 6, 8,
00081 9, 7, 6, 8, 9, 10, 11, 12, 12, 10, 11, 13, 13, 14, 14, 14
00082 };
00083
00084 static const byte pos2ctx_map4x8i[] = {
00085 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 6, 2, 7, 7, 8,
00086 8, 8, 5, 6, 9, 10, 10, 11, 11, 11, 12, 13, 13, 14, 14, 14
00087 };
00088
00089 const byte* pos2ctx_map_int[] = {
00090 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
00091 pos2ctx_map4x8i,pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
00092 pos2ctx_map2x4c, pos2ctx_map4x4c,
00093
00094 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
00095 pos2ctx_map8x4i,pos2ctx_map4x4,
00096 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
00097 pos2ctx_map8x4i,pos2ctx_map4x4
00098 };
00099
00100
00101
00102 static const byte pos2ctx_last8x8 [] = {
00103 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00104 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00105 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
00106 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8
00107 };
00108
00109 static const byte pos2ctx_last8x4 [] = {
00110 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
00111 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8
00112 };
00113
00114 static const byte pos2ctx_last4x4 [] = {
00115 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
00116 };
00117
00118 static const byte pos2ctx_last2x4c[] = {
00119 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
00120 };
00121
00122 static const byte pos2ctx_last4x4c[] = {
00123 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2
00124 };
00125
00126 const byte* pos2ctx_last [] = {
00127 pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8, pos2ctx_last8x4,
00128 pos2ctx_last8x4, pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last4x4,
00129 pos2ctx_last2x4c, pos2ctx_last4x4c,
00130 pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8,pos2ctx_last8x4,
00131 pos2ctx_last8x4, pos2ctx_last4x4,
00132 pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8,pos2ctx_last8x4,
00133 pos2ctx_last8x4, pos2ctx_last4x4
00134 };
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 static void exp_golomb_encode_eq_prob( EncodingEnvironmentPtr eep_dp,
00149 unsigned int symbol,
00150 int k)
00151 {
00152 for(;;)
00153 {
00154 if (symbol >= (unsigned int)(1<<k))
00155 {
00156 biari_encode_symbol_eq_prob(eep_dp, 1);
00157 symbol = symbol - (1<<k);
00158 k++;
00159 }
00160 else
00161 {
00162 biari_encode_symbol_eq_prob(eep_dp, 0);
00163 while (k--)
00164 biari_encode_symbol_eq_prob(eep_dp, (signed short)((symbol>>k)&1));
00165 break;
00166 }
00167 }
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 static void unary_bin_encode(EncodingEnvironmentPtr eep_dp,
00179 unsigned int symbol,
00180 BiContextTypePtr ctx,
00181 int ctx_offset)
00182 {
00183 if (symbol==0)
00184 {
00185 biari_encode_symbol(eep_dp, 0, ctx );
00186 return;
00187 }
00188 else
00189 {
00190 biari_encode_symbol(eep_dp, 1, ctx );
00191 ctx += ctx_offset;
00192 while ((--symbol) > 0)
00193 biari_encode_symbol(eep_dp, 1, ctx);
00194 biari_encode_symbol(eep_dp, 0, ctx);
00195 }
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 static void unary_bin_max_encode(EncodingEnvironmentPtr eep_dp,
00208 unsigned int symbol,
00209 BiContextTypePtr ctx,
00210 int ctx_offset,
00211 unsigned int max_symbol)
00212 {
00213 if (symbol==0)
00214 {
00215 biari_encode_symbol(eep_dp, 0, ctx );
00216 return;
00217 }
00218 else
00219 {
00220 unsigned int l = symbol;
00221 biari_encode_symbol(eep_dp, 1, ctx );
00222
00223 ctx += ctx_offset;
00224 while ((--l)>0)
00225 biari_encode_symbol(eep_dp, 1, ctx);
00226 if (symbol < max_symbol)
00227 biari_encode_symbol(eep_dp, 0, ctx);
00228 }
00229 }
00230
00231
00232
00233
00234
00235
00236
00237 static void unary_exp_golomb_level_encode( EncodingEnvironmentPtr eep_dp,
00238 unsigned int symbol,
00239 BiContextTypePtr ctx)
00240 {
00241 if (symbol==0)
00242 {
00243 biari_encode_symbol(eep_dp, 0, ctx );
00244 return;
00245 }
00246 else
00247 {
00248 unsigned int l=symbol;
00249 unsigned int k = 1;
00250
00251 biari_encode_symbol(eep_dp, 1, ctx );
00252 while (((--l)>0) && (++k <= 13))
00253 biari_encode_symbol(eep_dp, 1, ctx);
00254 if (symbol < 13)
00255 biari_encode_symbol(eep_dp, 0, ctx);
00256 else
00257 exp_golomb_encode_eq_prob(eep_dp,symbol - 13, 0);
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268 static void unary_exp_golomb_mv_encode(EncodingEnvironmentPtr eep_dp,
00269 unsigned int symbol,
00270 BiContextTypePtr ctx,
00271 unsigned int max_bin)
00272 {
00273 if (symbol==0)
00274 {
00275 biari_encode_symbol(eep_dp, 0, ctx );
00276 return;
00277 }
00278 else
00279 {
00280 unsigned int bin = 1;
00281 unsigned int l = symbol, k = 1;
00282 biari_encode_symbol(eep_dp, 1, ctx++ );
00283
00284 while (((--l)>0) && (++k <= 8))
00285 {
00286 biari_encode_symbol(eep_dp, 1, ctx );
00287 if ((++bin) == 2)
00288 ++ctx;
00289 if (bin == max_bin)
00290 ++ctx;
00291 }
00292 if (symbol < 8)
00293 biari_encode_symbol(eep_dp, 0, ctx);
00294 else
00295 exp_golomb_encode_eq_prob(eep_dp, symbol - 8, 3);
00296 }
00297 }
00298
00299
00300
00301
00302
00303
00304
00305
00306 void CheckAvailabilityOfNeighborsCABAC(Macroblock *currMB)
00307 {
00308 ImageParameters *p_Img = currMB->p_Img;
00309 PixelPos up, left;
00310 int *mb_size = p_Img->mb_size[IS_LUMA];
00311
00312 p_Img->getNeighbour(currMB, -1, 0, mb_size, &left);
00313 p_Img->getNeighbour(currMB, 0, -1, mb_size, &up);
00314
00315 if (up.available)
00316 currMB->mb_up = &p_Img->mb_data[up.mb_addr];
00317 else
00318 currMB->mb_up = NULL;
00319
00320 if (left.available)
00321 currMB->mb_left = &p_Img->mb_data[left.mb_addr];
00322 else
00323 currMB->mb_left = NULL;
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333 MotionInfoContexts* create_contexts_MotionInfo(void)
00334 {
00335 MotionInfoContexts* enco_ctx;
00336
00337 enco_ctx = (MotionInfoContexts*) calloc(1, sizeof(MotionInfoContexts) );
00338 if( enco_ctx == NULL )
00339 no_mem_exit("create_contexts_MotionInfo: enco_ctx");
00340
00341 return enco_ctx;
00342 }
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 TextureInfoContexts* create_contexts_TextureInfo(void)
00353 {
00354 TextureInfoContexts* enco_ctx = (TextureInfoContexts*) calloc(1, sizeof(TextureInfoContexts) );
00355 if( enco_ctx == NULL )
00356 no_mem_exit("create_contexts_TextureInfo: enco_ctx");
00357
00358 return enco_ctx;
00359 }
00360
00361
00362
00363
00364
00365
00366
00367
00368 void delete_contexts_MotionInfo(MotionInfoContexts *enco_ctx)
00369 {
00370 if( enco_ctx == NULL )
00371 return;
00372
00373 free( enco_ctx );
00374 }
00375
00376
00377
00378
00379
00380
00381
00382
00383 void delete_contexts_TextureInfo(TextureInfoContexts *enco_ctx)
00384 {
00385 if( enco_ctx == NULL )
00386 return;
00387
00388 free( enco_ctx );
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 void writeFieldModeInfo_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
00400 {
00401 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00402 int curr_len = arienco_bits_written(eep_dp);
00403 ImageParameters *p_Img = currMB->p_Img;
00404 Macroblock *mb_data = p_Img->mb_data;
00405
00406 int a = currMB->mbAvailA ? mb_data[currMB->mbAddrA].mb_field : 0;
00407 int b = currMB->mbAvailB ? mb_data[currMB->mbAddrB].mb_field : 0;
00408
00409 int act_ctx = a + b;
00410
00411 #if ENABLE_FIELD_CTX
00412 TextureInfoContexts *ctx = dp->p_Slice->tex_ctx;
00413
00414 biari_encode_symbol(eep_dp, (signed short) (se->value1 != 0), &ctx->mb_aff_contexts[act_ctx]);
00415 #endif
00416
00417 se->context = act_ctx;
00418
00419 dp->bitstream->write_flag = 1;
00420 se->len = (arienco_bits_written(eep_dp) - curr_len);
00421 CABAC_TRACE;
00422 }
00423
00424
00425
00426
00427
00428
00429
00430 void writeMB_Pskip_flagInfo_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
00431 {
00432 Slice *currSlice = currMB->p_slice;
00433 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00434 int curr_len = arienco_bits_written(eep_dp);
00435 MotionInfoContexts *ctx = currSlice->mot_ctx;
00436 int curr_mb_type = se->value1;
00437 int a = (currMB->mb_left != NULL && (currMB->mb_left->skip_flag == 0)) ? 1 : 0;
00438 int b = (currMB->mb_up != NULL && (currMB->mb_up ->skip_flag == 0)) ? 1 : 0;
00439 int act_ctx = a + b;
00440
00441 if (curr_mb_type==0)
00442 biari_encode_symbol(eep_dp, 1,&ctx->mb_type_contexts[1][act_ctx]);
00443 else
00444 biari_encode_symbol(eep_dp, 0,&ctx->mb_type_contexts[1][act_ctx]);
00445
00446 currMB->skip_flag = (curr_mb_type==0) ? 1 : 0;
00447
00448 se->context = act_ctx;
00449 se->value1 = 1 - currMB->skip_flag;
00450
00451 dp->bitstream->write_flag = 1;
00452 se->len = (arienco_bits_written(eep_dp) - curr_len);
00453 CABAC_TRACE;
00454 }
00455
00456
00457
00458
00459
00460
00461
00462 void writeMB_Bskip_flagInfo_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
00463 {
00464 Slice *currSlice = currMB->p_slice;
00465 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00466 int curr_len = arienco_bits_written(eep_dp);
00467 MotionInfoContexts *ctx = currSlice->mot_ctx;
00468 int a = (currMB->mb_left != NULL && (currMB->mb_left->skip_flag == 0)) ? 1 : 0;
00469 int b = (currMB->mb_up != NULL && (currMB->mb_up ->skip_flag == 0)) ? 1 : 0;
00470 int act_ctx = a + b;
00471
00472 act_ctx += 7;
00473
00474 if (se->value1==0 && se->value2==0)
00475 biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][act_ctx]);
00476 else
00477 biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][act_ctx]);
00478
00479 currMB->skip_flag = (se->value1==0 && se->value2==0) ? 1 : 0;
00480
00481 se->context = act_ctx;
00482 se->value1 = 1 - currMB->skip_flag;
00483
00484 dp->bitstream->write_flag = 1;
00485 se->len = (arienco_bits_written(eep_dp) - curr_len);
00486 CABAC_TRACE;
00487 }
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 void writeMB_transform_size_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
00498 {
00499 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00500 TextureInfoContexts *ctx = (dp->p_Slice)->tex_ctx;
00501
00502 int curr_len = arienco_bits_written(eep_dp);
00503 int act_sym = currMB->luma_transform_size_8x8_flag;
00504
00505
00506 int b = (currMB->mb_up == NULL) ? 0 : currMB->mb_up->luma_transform_size_8x8_flag;
00507 int a = (currMB->mb_left == NULL) ? 0 :currMB->mb_left->luma_transform_size_8x8_flag;
00508 int act_ctx = a + b;
00509
00510 se->context = act_ctx;
00511 biari_encode_symbol(eep_dp, (signed short) (act_sym != 0), ctx->transform_size_contexts + act_ctx );
00512
00513 dp->bitstream->write_flag = 1;
00514 se->len = (arienco_bits_written(eep_dp) - curr_len);
00515 CABAC_TRACE;
00516 }
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526 void writeMB_P_typeInfo_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
00527 {
00528 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00529 int curr_len = arienco_bits_written(eep_dp);
00530
00531 BiContextType *mb_type_contexts = dp->p_Slice->mot_ctx->mb_type_contexts[1];
00532
00533 int act_ctx = 0;
00534 int mode_sym = 0;
00535 int act_sym = se->value1;
00536 int mode16x16 = 7;
00537
00538 if (act_sym >= mode16x16)
00539 {
00540 mode_sym = act_sym - mode16x16;
00541 act_sym = mode16x16;
00542 }
00543
00544 switch (act_sym)
00545 {
00546 case 0:
00547 break;
00548 case 1:
00549 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[4]);
00550 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[5]);
00551 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[6]);
00552 break;
00553 case 2:
00554 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[4]);
00555 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[5]);
00556 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[7]);
00557 break;
00558 case 3:
00559 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[4]);
00560 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[5]);
00561 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[7]);
00562 break;
00563 case 4:
00564 case 5:
00565 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[4]);
00566 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[5]);
00567 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[6]);
00568 break;
00569 case 6:
00570 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[4]);
00571 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[7]);
00572 break;
00573 case 7:
00574 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[4]);
00575 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[7]);
00576 break;
00577 default:
00578 printf ("Unsupported MB-MODE in writeMB_I_typeInfo_CABAC!\n");
00579 exit (1);
00580 }
00581
00582 if(act_sym == mode16x16)
00583 {
00584 if( mode_sym == 24 )
00585 {
00586 biari_encode_symbol_final(eep_dp, 1 );
00587 dp->bitstream->write_flag = 1;
00588 se->len = (arienco_bits_written(eep_dp) - curr_len);
00589 CABAC_TRACE;
00590 return;
00591 }
00592 biari_encode_symbol_final(eep_dp, 0 );
00593
00594 act_ctx = 8;
00595 act_sym = mode_sym/12;
00596 biari_encode_symbol(eep_dp, (signed short) act_sym, mb_type_contexts + act_ctx );
00597 mode_sym = mode_sym % 12;
00598
00599 act_sym = mode_sym >> 2;
00600 act_ctx = 9;
00601 if (act_sym==0)
00602 {
00603 biari_encode_symbol(eep_dp, 0, mb_type_contexts + act_ctx );
00604 }
00605 else
00606 {
00607 biari_encode_symbol(eep_dp, 1, mb_type_contexts + act_ctx );
00608 biari_encode_symbol(eep_dp, (signed short) (act_sym!=1), mb_type_contexts + act_ctx );
00609 }
00610
00611 mode_sym = mode_sym & 0x03;
00612 act_ctx = 10;
00613 act_sym = mode_sym >> 1;
00614 biari_encode_symbol(eep_dp, (signed short) act_sym, mb_type_contexts + act_ctx );
00615 act_sym = (mode_sym & 0x01);
00616 biari_encode_symbol(eep_dp, (signed short) act_sym, mb_type_contexts + act_ctx );
00617 }
00618
00619 dp->bitstream->write_flag = 1;
00620 se->len = (arienco_bits_written(eep_dp) - curr_len);
00621 CABAC_TRACE;
00622 }
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632 void writeMB_B_typeInfo_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
00633 {
00634 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00635 int curr_len = arienco_bits_written(eep_dp);
00636
00637 signed short csym;
00638 int act_sym = se->value1;
00639
00640 int mode_sym = 0;
00641 int mode16x16 = 24;
00642
00643 MotionInfoContexts *ctx = dp->p_Slice->mot_ctx;
00644 BiContextType *mb_type_contexts = ctx->mb_type_contexts[2];
00645
00646 int a = (currMB->mb_left == NULL) ? 0 : ((currMB->mb_left->mb_type != 0) ? 1 : 0 );
00647 int b = (currMB->mb_up == NULL) ? 0 : ((currMB->mb_up->mb_type != 0) ? 1 : 0 );
00648 int act_ctx = a + b;
00649 se->context = act_ctx;
00650
00651 if (act_sym >= mode16x16)
00652 {
00653 mode_sym = act_sym - mode16x16;
00654 act_sym = mode16x16;
00655 }
00656
00657 if (act_sym==0)
00658 {
00659 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[act_ctx]);
00660 }
00661 else if (act_sym<=2)
00662 {
00663 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[act_ctx]);
00664 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[4]);
00665 csym = (short) (act_sym-1 != 0);
00666 biari_encode_symbol (eep_dp, csym, &mb_type_contexts[6]);
00667 }
00668 else if (act_sym<=10)
00669 {
00670 int temp_sym = act_sym - 3;
00671 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[act_ctx]);
00672 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[4]);
00673 biari_encode_symbol (eep_dp, 0, &mb_type_contexts[5]);
00674 csym = (short) ((temp_sym >> 2)&0x01) != 0;
00675 biari_encode_symbol (eep_dp, csym, &mb_type_contexts[6]);
00676 csym = (short) ((temp_sym >> 1)&0x01) != 0;
00677 biari_encode_symbol (eep_dp, csym, &mb_type_contexts[6]);
00678 csym = (short) (temp_sym & 0x01) != 0;
00679 biari_encode_symbol (eep_dp, csym, &mb_type_contexts[6]);
00680 }
00681 else if (act_sym==11 || act_sym==22)
00682 {
00683 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[act_ctx]);
00684 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[4]);
00685 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[5]);
00686 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[6]);
00687 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[6]);
00688 csym = (short) (act_sym != 11);
00689 biari_encode_symbol (eep_dp, csym, &mb_type_contexts[6]);
00690 }
00691 else
00692 {
00693 int temp_sym = (act_sym > 22) ? act_sym - 13 : act_sym - 12;
00694 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[act_ctx]);
00695 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[4]);
00696 biari_encode_symbol (eep_dp, 1, &mb_type_contexts[5]);
00697 csym = (short) ((temp_sym >> 3)&0x01) != 0;
00698 biari_encode_symbol (eep_dp, csym, &mb_type_contexts[6]);
00699 csym = (short) ((temp_sym >> 2)&0x01) != 0;
00700 biari_encode_symbol (eep_dp, csym, &mb_type_contexts[6]);
00701 csym = (short) ((temp_sym >> 1)&0x01) != 0;
00702 biari_encode_symbol (eep_dp, csym, &mb_type_contexts[6]);
00703 csym = (short) (temp_sym & 0x01) != 0;
00704 biari_encode_symbol (eep_dp, csym, &mb_type_contexts[6]);
00705 }
00706
00707
00708 if(act_sym == mode16x16)
00709 {
00710 mb_type_contexts = ctx->mb_type_contexts[1];
00711 if( mode_sym == 24 )
00712 {
00713 biari_encode_symbol_final(eep_dp, 1 );
00714 dp->bitstream->write_flag = 1;
00715 se->len = (arienco_bits_written(eep_dp) - curr_len);
00716 CABAC_TRACE;
00717 return;
00718 }
00719 biari_encode_symbol_final(eep_dp, 0 );
00720
00721 act_ctx = 8;
00722 act_sym = mode_sym/12;
00723 biari_encode_symbol(eep_dp, (signed short) act_sym, mb_type_contexts + act_ctx );
00724 mode_sym = mode_sym % 12;
00725
00726 act_sym = mode_sym >> 2;
00727 act_ctx = 9;
00728 if (act_sym==0)
00729 {
00730 biari_encode_symbol(eep_dp, 0, mb_type_contexts + act_ctx );
00731 }
00732 else
00733 {
00734 biari_encode_symbol(eep_dp, 1, mb_type_contexts + act_ctx );
00735 biari_encode_symbol(eep_dp, (signed short) (act_sym!=1), mb_type_contexts + act_ctx );
00736 }
00737
00738 mode_sym = mode_sym & 0x03;
00739 act_ctx = 10;
00740 act_sym = mode_sym >> 1;
00741 biari_encode_symbol(eep_dp, (signed short) act_sym, mb_type_contexts + act_ctx );
00742 act_sym = (mode_sym & 0x01);
00743 biari_encode_symbol(eep_dp, (signed short) act_sym, mb_type_contexts + act_ctx );
00744 }
00745
00746 dp->bitstream->write_flag = 1;
00747 se->len = (arienco_bits_written(eep_dp) - curr_len);
00748 CABAC_TRACE;
00749 }
00750
00751
00752
00753
00754
00755
00756
00757
00758 void writeMB_I_typeInfo_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
00759 {
00760 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00761 int curr_len = arienco_bits_written(eep_dp);
00762 int a, b;
00763 int act_ctx = 0;
00764 int act_sym;
00765 Slice *currSlice = dp->p_Slice;
00766 int mode_sym = 0;
00767
00768 MotionInfoContexts *ctx = currSlice->mot_ctx;
00769
00770 if (currMB->mb_up == NULL)
00771 b = 0;
00772 else
00773 b = ((currMB->mb_up->mb_type != I4MB && currMB->mb_up->mb_type != I8MB) ? 1 : 0 );
00774
00775 if (currMB->mb_left == NULL)
00776 a = 0;
00777 else
00778 a = ((currMB->mb_left->mb_type != I4MB && currMB->mb_left->mb_type != I8MB) ? 1 : 0 );
00779
00780 act_ctx = a + b;
00781 act_sym = se->value1;
00782 se->context = act_ctx;
00783
00784 if (act_sym==0)
00785 {
00786 biari_encode_symbol(eep_dp, 0, ctx->mb_type_contexts[0] + act_ctx );
00787 }
00788 else if( act_sym == 25 )
00789 {
00790 biari_encode_symbol(eep_dp, 1, ctx->mb_type_contexts[0] + act_ctx );
00791 biari_encode_symbol_final(eep_dp, 1);
00792 }
00793 else
00794 {
00795 biari_encode_symbol(eep_dp, 1, ctx->mb_type_contexts[0] + act_ctx );
00796
00797 biari_encode_symbol_final(eep_dp, 0);
00798
00799 mode_sym = act_sym-1;
00800 act_ctx = 4;
00801 act_sym = mode_sym/12;
00802 biari_encode_symbol(eep_dp, (signed short) act_sym, ctx->mb_type_contexts[0] + act_ctx );
00803 mode_sym = mode_sym % 12;
00804 act_sym = mode_sym / 4;
00805 act_ctx = 5;
00806 if (act_sym==0)
00807 {
00808 biari_encode_symbol(eep_dp, 0, ctx->mb_type_contexts[0] + act_ctx );
00809 }
00810 else
00811 {
00812 biari_encode_symbol(eep_dp, 1, ctx->mb_type_contexts[0] + act_ctx );
00813 act_ctx=6;
00814 biari_encode_symbol(eep_dp, (signed short) (act_sym!=1), ctx->mb_type_contexts[0] + act_ctx );
00815 }
00816 mode_sym = mode_sym & 0x03;
00817 act_sym = mode_sym >> 1;
00818 act_ctx = 7;
00819 biari_encode_symbol(eep_dp, (signed short) act_sym, ctx->mb_type_contexts[0] + act_ctx );
00820 act_ctx = 8;
00821 act_sym = mode_sym & 0x01;
00822 biari_encode_symbol(eep_dp, (signed short) act_sym, ctx->mb_type_contexts[0] + act_ctx );
00823 }
00824
00825 dp->bitstream->write_flag = 1;
00826 se->len = (arienco_bits_written(eep_dp) - curr_len);
00827 CABAC_TRACE;
00828 }
00829
00830
00831
00832
00833
00834
00835
00836
00837 void writeB8_B_typeInfo_CABAC(SyntaxElement *se, DataPartition *dp)
00838 {
00839 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00840 BiContextType *b8_type_contexts = dp->p_Slice->mot_ctx->b8_type_contexts[1];
00841 int curr_len = arienco_bits_written(eep_dp);
00842 int act_sym = se->value1;
00843 signed short csym;
00844
00845 if (act_sym==0)
00846 {
00847 biari_encode_symbol (eep_dp, 0, &b8_type_contexts[0]);
00848 dp->bitstream->write_flag = 1;
00849 se->len = (arienco_bits_written(eep_dp) - curr_len);
00850 CABAC_TRACE;
00851 return;
00852 }
00853 else
00854 {
00855 biari_encode_symbol (eep_dp, 1, &b8_type_contexts[0]);
00856 act_sym--;
00857 }
00858
00859 if (act_sym < 2)
00860 {
00861 biari_encode_symbol (eep_dp, 0, &b8_type_contexts[1]);
00862 biari_encode_symbol (eep_dp, (signed short) (act_sym!=0), &b8_type_contexts[3]);
00863 }
00864 else if (act_sym < 6)
00865 {
00866 biari_encode_symbol (eep_dp, 1, &b8_type_contexts[1]);
00867 biari_encode_symbol (eep_dp, 0, &b8_type_contexts[2]);
00868 csym = (short) (((act_sym - 2) >> 1) & 0x01) != 0;
00869 biari_encode_symbol (eep_dp, csym, &b8_type_contexts[3]);
00870 csym = (short) ((act_sym - 2) & 0x01) != 0;
00871 biari_encode_symbol (eep_dp, csym, &b8_type_contexts[3]);
00872 }
00873 else
00874 {
00875 biari_encode_symbol (eep_dp, 1, &b8_type_contexts[1]);
00876 biari_encode_symbol (eep_dp, 1, &b8_type_contexts[2]);
00877 csym = (short) (((act_sym - 6)>> 2) & 0x01);
00878 if (csym)
00879 {
00880 biari_encode_symbol (eep_dp, 1, &b8_type_contexts[3]);
00881 csym = (short) ((act_sym - 6) & 0x01) != 0;
00882 biari_encode_symbol (eep_dp, csym, &b8_type_contexts[3]);
00883 }
00884 else
00885 {
00886 biari_encode_symbol (eep_dp, 0, &b8_type_contexts[3]);
00887 csym = (short) (((act_sym - 6) >> 1) & 0x01) != 0;
00888 biari_encode_symbol (eep_dp, csym, &b8_type_contexts[3]);
00889 csym = (short) ((act_sym - 6) & 0x01) != 0;
00890 biari_encode_symbol (eep_dp, csym, &b8_type_contexts[3]);
00891 }
00892 }
00893
00894 dp->bitstream->write_flag = 1;
00895 se->len = (arienco_bits_written(eep_dp) - curr_len);
00896 CABAC_TRACE;
00897 }
00898
00899
00900
00901
00902
00903
00904
00905
00906 void writeB8_typeInfo_CABAC(SyntaxElement *se, DataPartition *dp)
00907 {
00908 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00909 BiContextType (*b8_type_contexts) = dp->p_Slice->mot_ctx->b8_type_contexts[0];
00910 int curr_len = arienco_bits_written(eep_dp);
00911
00912 switch (se->value1)
00913 {
00914 case 0:
00915 biari_encode_symbol (eep_dp, 1, &b8_type_contexts[1]);
00916 break;
00917 case 1:
00918 biari_encode_symbol (eep_dp, 0, &b8_type_contexts[1]);
00919 biari_encode_symbol (eep_dp, 0, &b8_type_contexts[3]);
00920 break;
00921 case 2:
00922 biari_encode_symbol (eep_dp, 0, &b8_type_contexts[1]);
00923 biari_encode_symbol (eep_dp, 1, &b8_type_contexts[3]);
00924 biari_encode_symbol (eep_dp, 1, &b8_type_contexts[4]);
00925 break;
00926 case 3:
00927 biari_encode_symbol (eep_dp, 0, &b8_type_contexts[1]);
00928 biari_encode_symbol (eep_dp, 1, &b8_type_contexts[3]);
00929 biari_encode_symbol (eep_dp, 0, &b8_type_contexts[4]);
00930 break;
00931 }
00932
00933 dp->bitstream->write_flag = 1;
00934 se->len = (arienco_bits_written(eep_dp) - curr_len);
00935 CABAC_TRACE;
00936 }
00937
00938
00939
00940
00941
00942
00943
00944
00945 void writeIntraPredMode_CABAC(SyntaxElement *se, DataPartition *dp)
00946 {
00947 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00948 int curr_len = arienco_bits_written(eep_dp);
00949 BiContextType *ipr_contexts = (dp->p_Slice)->tex_ctx->ipr_contexts;
00950
00951
00952 if (se->value1 == -1)
00953 biari_encode_symbol(eep_dp, 1, ipr_contexts);
00954 else
00955 {
00956 biari_encode_symbol(eep_dp, 0, ipr_contexts);
00957
00958
00959 biari_encode_symbol(eep_dp,(signed short)( se->value1 & 0x1 ), ipr_contexts+1);
00960 biari_encode_symbol(eep_dp,(signed short)((se->value1 & 0x2)>>1), ipr_contexts+1);
00961 biari_encode_symbol(eep_dp,(signed short)((se->value1 & 0x4)>>2), ipr_contexts+1);
00962 }
00963
00964 dp->bitstream->write_flag = 1;
00965 se->len = (arienco_bits_written(eep_dp) - curr_len);
00966 CABAC_TRACE;
00967 }
00968
00969
00970
00971
00972
00973
00974
00975
00976 void writeRefPic_P_CABAC(SyntaxElement *se, DataPartition *dp)
00977 {
00978 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
00979 int curr_len = arienco_bits_written(eep_dp);
00980 Slice *currSlice = dp->p_Slice;
00981 ImageParameters *p_Img = dp->p_Img;
00982 PicMotionParams *motion = &p_Img->enc_picture->motion;
00983 MotionInfoContexts *ctx = currSlice->mot_ctx;
00984 Macroblock *currMB = &p_Img->mb_data[p_Img->current_mb_nr];
00985 int addctx = 0;
00986
00987 int a = 0, b = 0;
00988 int act_ctx;
00989 int act_sym;
00990 char** refframe_array = motion->ref_idx[se->value2];
00991
00992 PixelPos block_a, block_b;
00993 int *mb_size = p_Img->mb_size[IS_LUMA];
00994
00995 get4x4Neighbour(currMB, currMB->subblock_x - 1, currMB->subblock_y , mb_size, &block_a);
00996 get4x4Neighbour(currMB, currMB->subblock_x, currMB->subblock_y - 1, mb_size, &block_b);
00997
00998 if (block_b.available)
00999 {
01000 if (p_Img->MbaffFrameFlag && (currMB->mb_field == FALSE) && (p_Img->mb_data[block_b.mb_addr].mb_field == TRUE))
01001 b = (refframe_array[block_b.pos_y][block_b.pos_x] > 1 ? 2 : 0);
01002 else
01003 b = (refframe_array[block_b.pos_y][block_b.pos_x] > 0 ? 2 : 0);
01004 }
01005
01006 if (block_a.available)
01007 {
01008 if (p_Img->MbaffFrameFlag && (currMB->mb_field == FALSE) && (p_Img->mb_data[block_a.mb_addr].mb_field == TRUE))
01009 a = (refframe_array[block_a.pos_y][block_a.pos_x] > 1 ? 1 : 0);
01010 else
01011 a = (refframe_array[block_a.pos_y][block_a.pos_x] > 0 ? 1 : 0);
01012 }
01013
01014 act_ctx = a + b;
01015 se->context = act_ctx;
01016 act_sym = se->value1;
01017
01018 if (act_sym==0)
01019 {
01020 biari_encode_symbol(eep_dp, 0, ctx->ref_no_contexts[addctx] + act_ctx );
01021 }
01022 else
01023 {
01024 biari_encode_symbol(eep_dp, 1, ctx->ref_no_contexts[addctx] + act_ctx);
01025 act_sym--;
01026 act_ctx=4;
01027 unary_bin_encode(eep_dp, act_sym, ctx->ref_no_contexts[addctx] + act_ctx, 1);
01028 }
01029
01030 dp->bitstream->write_flag = 1;
01031 se->len = (arienco_bits_written(eep_dp) - curr_len);
01032 CABAC_TRACE;
01033 }
01034
01035
01036
01037
01038
01039
01040
01041
01042 void writeRefPic_B_CABAC(SyntaxElement *se, DataPartition *dp)
01043 {
01044 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
01045 int curr_len = arienco_bits_written(eep_dp);
01046 Slice *currSlice = dp->p_Slice;
01047 ImageParameters *p_Img = dp->p_Img;
01048 PicMotionParams *motion = &p_Img->enc_picture->motion;
01049 MotionInfoContexts *ctx = currSlice->mot_ctx;
01050 Macroblock *currMB = &p_Img->mb_data[p_Img->current_mb_nr];
01051 int addctx = 0;
01052
01053 int a = 0, b = 0;
01054 int act_ctx;
01055 int act_sym;
01056 char** refframe_array = motion->ref_idx[se->value2];
01057
01058 int b8a, b8b;
01059
01060 PixelPos block_a, block_b;
01061 int *mb_size = p_Img->mb_size[IS_LUMA];
01062
01063 get4x4Neighbour(currMB, currMB->subblock_x - 1, currMB->subblock_y , mb_size, &block_a);
01064 get4x4Neighbour(currMB, currMB->subblock_x, currMB->subblock_y - 1, mb_size, &block_b);
01065
01066 if (block_b.available)
01067 {
01068 b8b=((block_b.x >> 1) & 0x01)+2*((block_b.y >> 1) & 0x01);
01069
01070 if (((p_Img->mb_data[block_b.mb_addr].mb_type == 0) && !p_Img->giRDOpt_B8OnlyFlag) || (p_Img->mb_data[block_b.mb_addr].b8x8[b8b].mode==0))
01071 b=0;
01072 else
01073 {
01074 if (p_Img->MbaffFrameFlag && (currMB->mb_field == FALSE) && (p_Img->mb_data[block_b.mb_addr].mb_field == TRUE))
01075 b = (refframe_array[block_b.pos_y][block_b.pos_x] > 1 ? 1 : 0);
01076 else
01077 b = (refframe_array[block_b.pos_y][block_b.pos_x] > 0 ? 1 : 0);
01078 }
01079 }
01080
01081 if (block_a.available)
01082 {
01083 b8a=((block_a.x >> 1) & 0x01)+2*((block_a.y >> 1) & 0x01);
01084 if (((p_Img->mb_data[block_a.mb_addr].mb_type == 0) && !p_Img->giRDOpt_B8OnlyFlag) || (p_Img->mb_data[block_a.mb_addr].b8x8[b8a].mode ==0))
01085 a=0;
01086 else
01087 {
01088 if (p_Img->MbaffFrameFlag && (currMB->mb_field == FALSE) && (p_Img->mb_data[block_a.mb_addr].mb_field == TRUE))
01089 a = (refframe_array[block_a.pos_y][block_a.pos_x] > 1 ? 1 : 0);
01090 else
01091 a = (refframe_array[block_a.pos_y][block_a.pos_x] > 0 ? 1 : 0);
01092 }
01093 }
01094
01095 act_ctx = a + 2*b;
01096 se->context = act_ctx;
01097 act_sym = se->value1;
01098
01099 if (act_sym==0)
01100 {
01101 biari_encode_symbol(eep_dp, 0, ctx->ref_no_contexts[addctx] + act_ctx );
01102 }
01103 else
01104 {
01105 biari_encode_symbol(eep_dp, 1, ctx->ref_no_contexts[addctx] + act_ctx);
01106 act_sym--;
01107 act_ctx=4;
01108 unary_bin_encode(eep_dp, act_sym, ctx->ref_no_contexts[addctx] + act_ctx, 1);
01109 }
01110
01111 dp->bitstream->write_flag = 1;
01112 se->len = (arienco_bits_written(eep_dp) - curr_len);
01113 CABAC_TRACE;
01114 }
01115
01116
01117
01118
01119
01120
01121
01122
01123 void writeDquant_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
01124 {
01125 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
01126 int curr_len = arienco_bits_written(eep_dp);
01127
01128 TextureInfoContexts *ctx = dp->p_Slice->tex_ctx;
01129
01130 int dquant = se->value1;
01131 int sign = (dquant <= 0) ? 0 : -1;
01132 int act_sym = (iabs(dquant) << 1) + sign;
01133 int act_ctx = ((currMB->prev_dqp != 0) ? 1 : 0);
01134
01135 if (act_sym == 0)
01136 {
01137 biari_encode_symbol(eep_dp, 0, ctx->delta_qp_contexts + act_ctx );
01138 }
01139 else
01140 {
01141 biari_encode_symbol(eep_dp, 1, ctx->delta_qp_contexts + act_ctx);
01142 act_ctx=2;
01143 act_sym--;
01144 unary_bin_encode(eep_dp, act_sym,ctx->delta_qp_contexts+act_ctx,1);
01145 }
01146
01147 dp->bitstream->write_flag = 1;
01148 se->len = (arienco_bits_written(eep_dp) - curr_len);
01149 CABAC_TRACE;
01150 }
01151
01152
01153
01154
01155
01156
01157
01158
01159 void writeMVD_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
01160 {
01161 ImageParameters *p_Img = currMB->p_Img;
01162 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
01163
01164 int curr_len = arienco_bits_written(eep_dp);
01165 MotionInfoContexts *ctx = dp->p_Slice->mot_ctx;
01166
01167 int i = currMB->subblock_x;
01168 int j = currMB->subblock_y;
01169 int a, b;
01170 int act_ctx;
01171 int act_sym;
01172 int mv_pred_res;
01173 int mv_local_err;
01174 int mv_sign;
01175 int list_idx = se->value2 & 0x01;
01176 int k = (se->value2>>1);
01177
01178 PixelPos block_a, block_b;
01179
01180 int *mb_size = p_Img->mb_size[IS_LUMA];
01181
01182 get4x4Neighbour(currMB, i - 1, j , mb_size, &block_a);
01183 get4x4Neighbour(currMB, i , j - 1, mb_size, &block_b);
01184
01185 if (block_b.available)
01186 {
01187 b = iabs(p_Img->mb_data[block_b.mb_addr].mvd[list_idx][block_b.y][block_b.x][k]);
01188 if (p_Img->MbaffFrameFlag && (k==1))
01189 {
01190 if ((currMB->mb_field==0) && (p_Img->mb_data[block_b.mb_addr].mb_field==1))
01191 b *= 2;
01192 else if ((currMB->mb_field==1) && (p_Img->mb_data[block_b.mb_addr].mb_field==0))
01193 b /= 2;
01194 }
01195 }
01196 else
01197 b=0;
01198
01199 if (block_a.available)
01200 {
01201 a = iabs(p_Img->mb_data[block_a.mb_addr].mvd[list_idx][block_a.y][block_a.x][k]);
01202 if (p_Img->MbaffFrameFlag && (k==1))
01203 {
01204 if ((currMB->mb_field==0) && (p_Img->mb_data[block_a.mb_addr].mb_field==1))
01205 a *= 2;
01206 else if ((currMB->mb_field==1) && (p_Img->mb_data[block_a.mb_addr].mb_field==0))
01207 a /= 2;
01208 }
01209 }
01210 else
01211 a = 0;
01212
01213 if ((mv_local_err = a + b)<3)
01214 act_ctx = 5 * k;
01215 else
01216 {
01217 if (mv_local_err>32)
01218 act_ctx = 5 * k + 3;
01219 else
01220 act_ctx = 5 * k + 2;
01221 }
01222
01223 mv_pred_res = se->value1;
01224 se->context = act_ctx;
01225
01226 act_sym = iabs(mv_pred_res);
01227
01228 if (act_sym == 0)
01229 biari_encode_symbol(eep_dp, 0, &ctx->mv_res_contexts[0][act_ctx] );
01230 else
01231 {
01232 biari_encode_symbol(eep_dp, 1, &ctx->mv_res_contexts[0][act_ctx] );
01233 act_sym--;
01234 act_ctx=5*k;
01235 unary_exp_golomb_mv_encode(eep_dp,act_sym,ctx->mv_res_contexts[1]+act_ctx,3);
01236 mv_sign = (mv_pred_res<0) ? 1: 0;
01237 biari_encode_symbol_eq_prob(eep_dp, (signed short) mv_sign);
01238 }
01239
01240 dp->bitstream->write_flag = 1;
01241 se->len = (arienco_bits_written(eep_dp) - curr_len);
01242 CABAC_TRACE;
01243 }
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253 void writeCIPredMode_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
01254 {
01255 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
01256 int curr_len = arienco_bits_written(eep_dp);
01257 TextureInfoContexts *ctx = dp->p_Slice->tex_ctx;
01258
01259 int act_sym = se->value1;
01260
01261 Macroblock *MbUp = currMB->mb_up;
01262 Macroblock *MbLeft = currMB->mb_left;
01263
01264 int b = (MbUp != NULL) ? (((MbUp->c_ipred_mode != 0) && (MbUp->mb_type != IPCM)) ? 1 : 0) : 0;
01265 int a = (MbLeft != NULL) ? (((MbLeft->c_ipred_mode != 0) && (MbLeft->mb_type != IPCM)) ? 1 : 0) : 0;
01266
01267 int act_ctx = a + b;
01268
01269 if (act_sym==0)
01270 biari_encode_symbol(eep_dp, 0, ctx->cipr_contexts + act_ctx );
01271 else
01272 {
01273 biari_encode_symbol(eep_dp, 1, ctx->cipr_contexts + act_ctx );
01274 unary_bin_max_encode(eep_dp,(unsigned int) (act_sym-1),ctx->cipr_contexts + 3,0,2);
01275 }
01276
01277 dp->bitstream->write_flag = 1;
01278 se->len = (arienco_bits_written(eep_dp) - curr_len);
01279 CABAC_TRACE;
01280 }
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290 void writeCBP_BIT_CABAC (Macroblock* currMB, int b8, int bit, int cbp, EncodingEnvironmentPtr eep_dp, TextureInfoContexts *ctx)
01291 {
01292 ImageParameters *p_Img = currMB->p_Img;
01293 PixelPos block_a;
01294 int a = 0, b = 0;
01295
01296 int mb_x=(b8 & 0x01)<<1;
01297 int mb_y=(b8 >> 1)<<1;
01298
01299 if (mb_y == 0)
01300 {
01301 if (!((currMB->mb_up == NULL) || ((currMB->mb_up)->mb_type==IPCM)))
01302 {
01303 b = (( ((currMB->mb_up)->cbp & (1<<(2+(mb_x>>1)))) == 0) ? 1 : 0);
01304 }
01305
01306 }
01307 else
01308 b = ( ((cbp & (1<<(mb_x >> 1))) == 0) ? 1: 0);
01309
01310 if (mb_x == 0)
01311 {
01312 get4x4Neighbour(currMB, (mb_x << 2) - 1, (mb_y << 2), p_Img->mb_size[IS_LUMA], &block_a);
01313
01314 if (block_a.available && (!(p_Img->mb_data[block_a.mb_addr].mb_type==IPCM)))
01315 {
01316 a = (( (p_Img->mb_data[block_a.mb_addr].cbp & (1<<(2*(block_a.y>>1)+1))) == 0) ? 1 : 0);
01317 }
01318 }
01319 else
01320 a = ( ((cbp & (1<<mb_y)) == 0) ? 1: 0);
01321
01322
01323 biari_encode_symbol (eep_dp, (signed short) bit, ctx->cbp_contexts[0] + a+2*b);
01324 }
01325
01326
01327
01328
01329
01330
01331
01332
01333 void writeCBP_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
01334 {
01335 Slice *currSlice = currMB->p_slice;
01336 ImageParameters *p_Img = currSlice->p_Img;
01337 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
01338 int curr_len = arienco_bits_written(eep_dp);
01339 TextureInfoContexts *ctx = currSlice->tex_ctx;
01340
01341 int a0 = 0, a1 = 0, b0 = 0, b1 = 0;
01342 int curr_cbp_ctx, curr_cbp_idx;
01343 int cbp = se->value1;
01344 int cbp_bit;
01345 int b8;
01346
01347 for (b8=0; b8<4; ++b8)
01348 {
01349 curr_cbp_idx = (currMB->b8x8[b8].mode == IBLOCK ? 0 : 1);
01350 writeCBP_BIT_CABAC (currMB, b8, cbp&(1<<b8), cbp, eep_dp, ctx);
01351 }
01352
01353 if ((p_Img->yuv_format != YUV400) && (p_Img->yuv_format != YUV444) )
01354 {
01355
01356 if (currMB->mb_up != NULL)
01357 {
01358 if((currMB->mb_up)->mb_type == IPCM || ((currMB->mb_up)->cbp > 15))
01359 b0 = 2;
01360 }
01361
01362 if (currMB->mb_left != NULL)
01363 {
01364 if((currMB->mb_left)->mb_type==IPCM || ((currMB->mb_left)->cbp > 15))
01365 a0 = 1;
01366 }
01367
01368 curr_cbp_ctx = a0 + b0;
01369 cbp_bit = (cbp > 15 ) ? 1 : 0;
01370 biari_encode_symbol(eep_dp, (signed short) cbp_bit, ctx->cbp_contexts[1] + curr_cbp_ctx );
01371
01372 if (cbp > 15)
01373 {
01374 if (currMB->mb_up != NULL)
01375 {
01376 if((currMB->mb_up)->mb_type == IPCM ||
01377 (((currMB->mb_up)->cbp > 15) && ( ((currMB->mb_up)->cbp >> 4) == 2)))
01378 b1 = 2;
01379 }
01380
01381 if (currMB->mb_left != NULL)
01382 {
01383 if((currMB->mb_left)->mb_type==IPCM ||
01384 (((currMB->mb_left)->cbp > 15) && ( ((currMB->mb_left)->cbp >> 4) == 2)))
01385 a1 = 1;
01386 }
01387
01388 curr_cbp_ctx = a1 + b1;
01389 cbp_bit = ((cbp>>4) == 2) ? 1 : 0;
01390 biari_encode_symbol(eep_dp, (signed short) cbp_bit, ctx->cbp_contexts[2] + curr_cbp_ctx );
01391 }
01392 }
01393
01394 dp->bitstream->write_flag = 1;
01395 se->len = (arienco_bits_written(eep_dp) - curr_len);
01396 CABAC_TRACE;
01397 }
01398
01399
01400
01401
01402
01403
01404
01405 void write_and_store_CBP_block_bit_444 (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int cbp_bit, TextureInfoContexts* tex_ctx)
01406 {
01407 ImageParameters *p_Img = currMB->p_Img;
01408 Macroblock *mb_data = p_Img->mb_data;
01409
01410 int y_ac = (type==LUMA_16AC || type==LUMA_8x8 || type==LUMA_8x4 || type==LUMA_4x8 || type==LUMA_4x4
01411 || type==CB_16AC || type==CB_8x8 || type==CB_8x4 || type==CB_4x8 || type==CB_4x4
01412 || type==CR_16AC || type==CR_8x8 || type==CR_8x4 || type==CR_4x8 || type==CR_4x4);
01413 int y_dc = (type==LUMA_16DC || type==CB_16DC || type==CR_16DC);
01414 int u_ac = (type==CHROMA_AC && !p_Img->is_v_block);
01415 int v_ac = (type==CHROMA_AC && p_Img->is_v_block);
01416 int chroma_dc = (type==CHROMA_DC || type==CHROMA_DC_2x4 || type==CHROMA_DC_4x4);
01417 int u_dc = (chroma_dc && !p_Img->is_v_block);
01418 int v_dc = (chroma_dc && p_Img->is_v_block);
01419 int j = (y_ac || u_ac || v_ac ? currMB->subblock_y : 0);
01420 int i = (y_ac || u_ac || v_ac ? currMB->subblock_x : 0);
01421 int bit = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 23);
01422 int default_bit = (currMB->is_intra_block ? 1 : 0);
01423 int upper_bit = default_bit;
01424 int left_bit = default_bit;
01425 int ctx;
01426
01427 int bit_pos_a = 0;
01428 int bit_pos_b = 0;
01429
01430 PixelPos block_a, block_b;
01431
01432 if (y_ac || y_dc)
01433 {
01434 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_LUMA], &block_a);
01435 get4x4Neighbour(currMB, i, j -1, p_Img->mb_size[IS_LUMA], &block_b);
01436 if (y_ac)
01437 {
01438 if (block_a.available)
01439 bit_pos_a = (block_a.y << 2) + block_a.x;
01440 if (block_b.available)
01441 bit_pos_b = (block_b.y << 2) + block_b.x;
01442 }
01443 }
01444 else
01445 {
01446 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_CHROMA], &block_a);
01447 get4x4Neighbour(currMB, i, j - 1, p_Img->mb_size[IS_CHROMA], &block_b);
01448 if (u_ac||v_ac)
01449 {
01450 if (block_a.available)
01451 bit_pos_a = (block_a.y << 2) + block_a.x;
01452 if (block_b.available)
01453 bit_pos_b = (block_b.y << 2) + block_b.x;
01454 }
01455 }
01456
01457 bit = (y_dc ? 0 : y_ac ? 1 + j + (i >> 2) : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 + j + (i >> 2): 35 + j + (i >> 2));
01458
01459 if (cbp_bit)
01460 {
01461 if (type==LUMA_8x8)
01462 {
01463 currMB->cbp_bits[0] |= ((int64) 0x33 << bit);
01464
01465 if (p_Img->enc_picture->chroma_format_idc == YUV444)
01466 {
01467 currMB->cbp_bits_8x8[0] |= ((int64) 0x33 << bit);
01468 }
01469 }
01470 else if (type==CB_8x8)
01471 {
01472 currMB->cbp_bits_8x8[1] |= ((int64) 0x33 << bit);
01473 currMB->cbp_bits[1] |= ((int64) 0x33 << bit);
01474 }
01475 else if (type==CR_8x8)
01476 {
01477 currMB->cbp_bits_8x8[2] |= ((int64) 0x33 << bit);
01478 currMB->cbp_bits[2] |= ((int64) 0x33 << bit);
01479 }
01480 else if (type==LUMA_8x4)
01481 {
01482 currMB->cbp_bits[0] |= ((int64) 0x03 << bit);
01483 }
01484 else if (type==LUMA_4x8)
01485 {
01486 currMB->cbp_bits[0] |= ((int64) 0x11 << bit);
01487 }
01488 else if (type==CB_8x4)
01489 {
01490 currMB->cbp_bits[1] |= ((int64) 0x03 << bit);
01491 }
01492 else if (type==CR_8x4)
01493 {
01494 currMB->cbp_bits[2] |= ((int64) 0x03 << bit);
01495 }
01496 else if (type==CB_4x8)
01497 {
01498 currMB->cbp_bits[1] |= ((int64) 0x11 << bit);
01499 }
01500 else if (type==CR_4x8)
01501 {
01502 currMB->cbp_bits[2] |= ((int64) 0x11 << bit);
01503 }
01504 else if ((type==CB_4x4)||(type==CB_16AC)||(type==CB_16DC))
01505 {
01506 currMB->cbp_bits[1] |= ((int64) 0x01 << bit);
01507 }
01508 else if ((type == CR_4x4)||(type == CR_16AC)||(type == CR_16DC))
01509 {
01510 currMB->cbp_bits[2] |= ((int64) 0x01 << bit);
01511 }
01512 else
01513 {
01514 currMB->cbp_bits[0] |= ((int64) 0x01 << bit);
01515 }
01516 }
01517
01518 bit = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 35);
01519
01520 if (p_Img->enc_picture->chroma_format_idc!=YUV444)
01521 {
01522 if (type!=LUMA_8x8)
01523 {
01524 if (block_b.available)
01525 {
01526 if(mb_data[block_b.mb_addr].mb_type==IPCM)
01527 upper_bit = 1;
01528 else
01529 upper_bit = get_bit(mb_data[block_b.mb_addr].cbp_bits[0],bit+bit_pos_b);
01530 }
01531
01532
01533 if (block_a.available)
01534 {
01535 if(mb_data[block_a.mb_addr].mb_type == IPCM)
01536 left_bit = 1;
01537 else
01538 left_bit = get_bit(mb_data[block_a.mb_addr].cbp_bits[0], bit + bit_pos_a);
01539 }
01540
01541 ctx = (upper_bit << 1) + left_bit;
01542
01543
01544 biari_encode_symbol (eep_dp, (short)cbp_bit, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
01545 }
01546 }
01547 else if( IS_INDEPENDENT(currMB->p_Inp) )
01548 {
01549 if (type!=LUMA_8x8)
01550 {
01551 if (block_b.available)
01552 {
01553 if(mb_data[block_b.mb_addr].mb_type==IPCM)
01554 upper_bit = 1;
01555 else
01556 upper_bit = get_bit(mb_data[block_b.mb_addr].cbp_bits[0],bit+bit_pos_b);
01557 }
01558
01559
01560 if (block_a.available)
01561 {
01562 if(mb_data[block_a.mb_addr].mb_type == IPCM)
01563 left_bit = 1;
01564 else
01565 left_bit = get_bit(mb_data[block_a.mb_addr].cbp_bits[0], bit + bit_pos_a);
01566 }
01567
01568 ctx = (upper_bit << 1) + left_bit;
01569
01570
01571 biari_encode_symbol (eep_dp, (short)cbp_bit, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
01572 }
01573 }
01574 else
01575 {
01576 if (block_b.available)
01577 {
01578 if(mb_data[block_b.mb_addr].mb_type == IPCM)
01579 upper_bit=1;
01580 else
01581 {
01582 if(type==LUMA_8x8)
01583 upper_bit = get_bit(mb_data[block_b.mb_addr].cbp_bits_8x8[0], bit + bit_pos_b);
01584 else if (type==CB_8x8)
01585 upper_bit = get_bit(mb_data[block_b.mb_addr].cbp_bits_8x8[1], bit + bit_pos_b);
01586 else if (type==CR_8x8)
01587 upper_bit = get_bit(mb_data[block_b.mb_addr].cbp_bits_8x8[2], bit + bit_pos_b);
01588 else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
01589 upper_bit = get_bit(mb_data[block_b.mb_addr].cbp_bits[1], bit + bit_pos_b);
01590 else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
01591 upper_bit = get_bit(mb_data[block_b.mb_addr].cbp_bits[2], bit + bit_pos_b);
01592 else
01593 upper_bit = get_bit(mb_data[block_b.mb_addr].cbp_bits[0], bit + bit_pos_b);
01594 }
01595 }
01596
01597 if (block_a.available)
01598 {
01599 if(mb_data[block_a.mb_addr].mb_type==IPCM)
01600 left_bit = 1;
01601 else
01602 {
01603 if(type==LUMA_8x8)
01604 left_bit = get_bit(mb_data[block_a.mb_addr].cbp_bits_8x8[0],bit + bit_pos_a);
01605 else if (type==CB_8x8)
01606 left_bit = get_bit(mb_data[block_a.mb_addr].cbp_bits_8x8[1],bit + bit_pos_a);
01607 else if (type==CR_8x8)
01608 left_bit = get_bit(mb_data[block_a.mb_addr].cbp_bits_8x8[2],bit + bit_pos_a);
01609 else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
01610 left_bit = get_bit(mb_data[block_a.mb_addr].cbp_bits[1],bit + bit_pos_a);
01611 else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
01612 left_bit = get_bit(mb_data[block_a.mb_addr].cbp_bits[2],bit + bit_pos_a);
01613 else
01614 left_bit = get_bit(mb_data[block_a.mb_addr].cbp_bits[0],bit + bit_pos_a);
01615 }
01616 }
01617
01618 ctx = 2*upper_bit+left_bit;
01619
01620
01621 biari_encode_symbol (eep_dp, (short)cbp_bit, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]]+ctx);
01622 }
01623 }
01624
01625
01626
01627
01628
01629
01630
01631
01632 void write_and_store_CBP_block_bit (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int cbp_bit, TextureInfoContexts* tex_ctx)
01633 {
01634 ImageParameters *p_Img = currMB->p_Img;
01635 Macroblock *mb_data = p_Img->mb_data;
01636 int y_ac = (type==LUMA_16AC || type==LUMA_8x8 || type==LUMA_8x4 || type==LUMA_4x8 || type==LUMA_4x4);
01637 int y_dc = (type==LUMA_16DC);
01638 int u_ac = (type==CHROMA_AC && !p_Img->is_v_block);
01639 int v_ac = (type==CHROMA_AC && p_Img->is_v_block);
01640 int chroma_dc = (type==CHROMA_DC || type==CHROMA_DC_2x4 || type==CHROMA_DC_4x4);
01641 int u_dc = (chroma_dc && !p_Img->is_v_block);
01642 int v_dc = (chroma_dc && p_Img->is_v_block);
01643 int j = (y_ac || u_ac || v_ac ? currMB->subblock_y : 0);
01644 int i = (y_ac || u_ac || v_ac ? currMB->subblock_x : 0);
01645 int bit = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 23);
01646 int default_bit = (currMB->is_intra_block ? 1 : 0);
01647 int upper_bit = default_bit;
01648 int left_bit = default_bit;
01649 int ctx;
01650
01651 int bit_pos_a = 0;
01652 int bit_pos_b = 0;
01653
01654 PixelPos block_a, block_b;
01655
01656 if (y_ac || y_dc)
01657 {
01658 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_LUMA], &block_a);
01659 get4x4Neighbour(currMB, i, j -1, p_Img->mb_size[IS_LUMA], &block_b);
01660 if (y_ac)
01661 {
01662 if (block_a.available)
01663 bit_pos_a = 4*block_a.y + block_a.x;
01664 if (block_b.available)
01665 bit_pos_b = 4*block_b.y + block_b.x;
01666 }
01667 }
01668 else
01669 {
01670 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_CHROMA], &block_a);
01671 get4x4Neighbour(currMB, i, j - 1, p_Img->mb_size[IS_CHROMA], &block_b);
01672 if (u_ac||v_ac)
01673 {
01674 if (block_a.available)
01675 bit_pos_a = (block_a.y << 2) + block_a.x;
01676 if (block_b.available)
01677 bit_pos_b = (block_b.y << 2) + block_b.x;
01678 }
01679 }
01680
01681 bit = (y_dc ? 0 : y_ac ? 1 + j + (i >> 2): u_dc ? 17 : v_dc ? 18 : u_ac ? 19 + j + (i >> 2): 35 + j + (i >> 2));
01682
01683 if (cbp_bit)
01684 {
01685 if (type==LUMA_8x8)
01686 {
01687 currMB->cbp_bits[0] |= ((int64) 0x33 << bit);
01688 }
01689 else if (type==LUMA_8x4)
01690 {
01691 currMB->cbp_bits[0] |= ((int64) 0x03 << bit);
01692 }
01693 else if (type==LUMA_4x8)
01694 {
01695 currMB->cbp_bits[0] |= ((int64) 0x11 << bit);
01696 }
01697 else
01698 {
01699 currMB->cbp_bits[0] |= ((int64) 0x01 << bit);
01700 }
01701 }
01702
01703 bit = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 35);
01704
01705 if (type!=LUMA_8x8)
01706 {
01707 if (block_b.available)
01708 {
01709 if(mb_data[block_b.mb_addr].mb_type==IPCM)
01710 upper_bit = 1;
01711 else
01712 upper_bit = get_bit(mb_data[block_b.mb_addr].cbp_bits[0],bit+bit_pos_b);
01713 }
01714
01715
01716 if (block_a.available)
01717 {
01718 if(mb_data[block_a.mb_addr].mb_type == IPCM)
01719 left_bit = 1;
01720 else
01721 left_bit = get_bit(mb_data[block_a.mb_addr].cbp_bits[0], bit + bit_pos_a);
01722 }
01723
01724 ctx = (upper_bit << 1) + left_bit;
01725
01726
01727 biari_encode_symbol (eep_dp, (short)cbp_bit, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
01728 }
01729 }
01730
01731
01732
01733
01734
01735
01736
01737 void write_significance_map (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int coeff[], int coeff_ctr, TextureInfoContexts* tex_ctx)
01738 {
01739 int k;
01740 uint16 sig, last;
01741 int k0 = 0;
01742 int k1 = maxpos[type];
01743
01744 #if ENABLE_FIELD_CTX
01745 ImageParameters *p_Img = currMB->p_Img;
01746 int fld = ( p_Img->structure!=FRAME || currMB->mb_field );
01747 #else
01748 int fld = 0;
01749 #endif
01750 BiContextTypePtr map_ctx = tex_ctx->map_contexts [fld][type2ctx_map [type]];
01751 BiContextTypePtr last_ctx = tex_ctx->last_contexts[fld][type2ctx_last[type]];
01752 const byte *pos2ctxmap = fld ? pos2ctx_map_int [type] : pos2ctx_map [type];
01753 const byte *pos2ctxlast = pos2ctx_last[type];
01754
01755 if (!c1isdc[type])
01756 {
01757 ++k0;
01758 ++k1;
01759 --coeff;
01760 }
01761
01762 for (k=k0; k<k1; ++k)
01763 {
01764 sig = (uint16) (coeff[k] != 0);
01765 biari_encode_symbol (eep_dp, sig, map_ctx + pos2ctxmap[k]);
01766 if (sig)
01767 {
01768 last = (uint16) (--coeff_ctr == 0);
01769
01770 biari_encode_symbol(eep_dp, last, last_ctx + pos2ctxlast[k]);
01771 if (last)
01772 return;
01773 }
01774 }
01775 }
01776
01777
01778
01779
01780
01781
01782
01783
01784 void write_significant_coefficients (EncodingEnvironmentPtr eep_dp, int type, int coeff[], TextureInfoContexts* tex_ctx)
01785 {
01786 BiContextType *one_contexts = tex_ctx->one_contexts[type2ctx_one[type]];
01787 BiContextType *abs_contexts = tex_ctx->abs_contexts[type2ctx_abs[type]];
01788 int absLevel;
01789 int ctx;
01790 short sign;
01791 short greater_one;
01792 int c1 = 1;
01793 int c2 = 0;
01794 int i;
01795
01796 for (i=maxpos[type]; i>=0; i--)
01797 {
01798 if (coeff[i]!=0)
01799 {
01800 if (coeff[i] > 0)
01801 {
01802 absLevel = coeff[i];
01803 sign = 0;
01804 }
01805 else
01806 {
01807 absLevel = -coeff[i];
01808 sign = 1;
01809 }
01810
01811 greater_one = (short) (absLevel > 1);
01812
01813
01814 ctx = imin(c1, 4);
01815 biari_encode_symbol (eep_dp, greater_one, one_contexts + ctx);
01816
01817 if (greater_one)
01818 {
01819 ctx = imin(c2++, max_c2[type]);
01820 unary_exp_golomb_level_encode(eep_dp, absLevel - 2, abs_contexts + ctx);
01821 c1 = 0;
01822 }
01823 else if (c1)
01824 {
01825 c1++;
01826 }
01827 biari_encode_symbol_eq_prob (eep_dp, sign);
01828 }
01829 }
01830 }
01831
01832
01833
01834
01835
01836
01837
01838
01839
01840 void writeRunLevel_CABAC (Macroblock* currMB, SyntaxElement *se, DataPartition *dp)
01841 {
01842 Slice *currSlice = currMB->p_slice;
01843 EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
01844 int curr_len = arienco_bits_written(eep_dp);
01845
01846
01847 if (se->value1 != 0)
01848 {
01849 currSlice->pos += se->value2;
01850 currSlice->coeff[currSlice->pos++] = se->value1;
01851 ++currSlice->coeff_ctr;
01852 }
01853 else
01854 {
01855 TextureInfoContexts *tex_ctx = currSlice->tex_ctx;
01856
01857 if (currSlice->coeff_ctr > 0)
01858 {
01859 currSlice->write_and_store_CBP_block_bit (currMB, eep_dp, se->context, 1, tex_ctx);
01860
01861 write_significance_map (currMB, eep_dp, se->context, currSlice->coeff, currSlice->coeff_ctr, tex_ctx);
01862
01863 write_significant_coefficients (eep_dp, se->context, currSlice->coeff, tex_ctx);
01864 }
01865 else
01866 currSlice->write_and_store_CBP_block_bit (currMB, eep_dp, se->context, 0, tex_ctx);
01867
01868
01869 currSlice->pos = currSlice->coeff_ctr = 0;
01870 memset(currSlice->coeff, 0 , 64 * sizeof(int));
01871 }
01872
01873 dp->bitstream->write_flag = 1;
01874 se->len = (arienco_bits_written(eep_dp) - curr_len);
01875 CABAC_TRACE;
01876 }
01877
01878