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