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