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
00027
00028
00029
00030
00031 static unsigned int unary_bin_decode(DecodingEnvironmentPtr dep_dp,
00032 BiContextTypePtr ctx,
00033 int ctx_offset);
00034 static unsigned int unary_bin_max_decode(DecodingEnvironmentPtr dep_dp,
00035 BiContextTypePtr ctx,
00036 int ctx_offset,
00037 unsigned int max_symbol);
00038 static unsigned int unary_exp_golomb_level_decode( DecodingEnvironmentPtr dep_dp,
00039 BiContextTypePtr ctx);
00040 static unsigned int unary_exp_golomb_mv_decode(DecodingEnvironmentPtr dep_dp,
00041 BiContextTypePtr ctx,
00042 unsigned int max_bin);
00043
00044 void CheckAvailabilityOfNeighborsCABAC(Macroblock *currMB)
00045 {
00046 ImageParameters *p_Img = currMB->p_Img;
00047 PixelPos up, left;
00048 int *mb_size = p_Img->mb_size[IS_LUMA];
00049
00050 p_Img->getNeighbour(currMB, -1, 0, mb_size, &left);
00051 p_Img->getNeighbour(currMB, 0, -1, mb_size, &up);
00052
00053 if (up.available)
00054 currMB->mb_up = &p_Img->mb_data[up.mb_addr];
00055 else
00056 currMB->mb_up = NULL;
00057
00058 if (left.available)
00059 currMB->mb_left = &p_Img->mb_data[left.mb_addr];
00060 else
00061 currMB->mb_left = NULL;
00062 }
00063
00064 void cabac_new_slice(Slice *currSlice)
00065 {
00066 currSlice->last_dquant=0;
00067 }
00068
00077 MotionInfoContexts* create_contexts_MotionInfo(void)
00078 {
00079 MotionInfoContexts *deco_ctx;
00080
00081 deco_ctx = (MotionInfoContexts*) calloc(1, sizeof(MotionInfoContexts) );
00082 if( deco_ctx == NULL )
00083 no_mem_exit("create_contexts_MotionInfo: deco_ctx");
00084
00085 return deco_ctx;
00086 }
00087
00088
00096 TextureInfoContexts* create_contexts_TextureInfo(void)
00097 {
00098 TextureInfoContexts *deco_ctx;
00099
00100 deco_ctx = (TextureInfoContexts*) calloc(1, sizeof(TextureInfoContexts) );
00101 if( deco_ctx == NULL )
00102 no_mem_exit("create_contexts_TextureInfo: deco_ctx");
00103
00104 return deco_ctx;
00105 }
00106
00107
00115 void delete_contexts_MotionInfo(MotionInfoContexts *deco_ctx)
00116 {
00117 if( deco_ctx == NULL )
00118 return;
00119
00120 free( deco_ctx );
00121 }
00122
00123
00131 void delete_contexts_TextureInfo(TextureInfoContexts *deco_ctx)
00132 {
00133 if( deco_ctx == NULL )
00134 return;
00135
00136 free( deco_ctx );
00137 }
00138
00139 void readFieldModeInfo_CABAC(Macroblock *currMB,
00140 SyntaxElement *se,
00141 DecodingEnvironmentPtr dep_dp)
00142 {
00143 Slice *currSlice = currMB->p_Slice;
00144 ImageParameters *p_Img = currMB->p_Img;
00145 MotionInfoContexts *ctx = currSlice->mot_ctx;
00146 int a = currMB->mbAvailA ? p_Img->mb_data[currMB->mbAddrA].mb_field : 0;
00147 int b = currMB->mbAvailB ? p_Img->mb_data[currMB->mbAddrB].mb_field : 0;
00148 int act_ctx = a + b;
00149
00150 se->value1 = biari_decode_symbol (dep_dp, &ctx->mb_aff_contexts[act_ctx]);
00151
00152 #if TRACE
00153 fprintf(p_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
00154 fflush(p_Dec->p_trace);
00155 #endif
00156 }
00157
00158
00159 int check_next_mb_and_get_field_mode_CABAC( Slice *currSlice,
00160 SyntaxElement *se,
00161 DataPartition *act_dp)
00162 {
00163 ImageParameters *p_Img = currSlice->p_Img;
00164 BiContextTypePtr mb_type_ctx_copy[3];
00165 BiContextTypePtr mb_aff_ctx_copy;
00166 DecodingEnvironmentPtr dep_dp_copy;
00167
00168 int length;
00169 DecodingEnvironmentPtr dep_dp = &(act_dp->de_cabac);
00170
00171 int bframe = (currSlice->slice_type == B_SLICE);
00172 int skip = 0;
00173 int field = 0;
00174 int i;
00175
00176 Macroblock *currMB;
00177
00178
00179 ++p_Img->current_mb_nr;
00180
00181 currMB = &p_Img->mb_data[p_Img->current_mb_nr];
00182 currMB->p_Img = p_Img;
00183 currMB->p_Slice = currSlice;
00184 currMB->slice_nr = p_Img->current_slice_nr;
00185 currMB->mb_field = p_Img->mb_data[p_Img->current_mb_nr-1].mb_field;
00186 currMB->mbAddrX = p_Img->current_mb_nr;
00187
00188 CheckAvailabilityOfNeighbors(currMB);
00189 CheckAvailabilityOfNeighborsCABAC(currMB);
00190
00191
00192 dep_dp_copy = (DecodingEnvironmentPtr) calloc(1, sizeof(DecodingEnvironment) );
00193 for (i=0;i<3;++i)
00194 mb_type_ctx_copy[i] = (BiContextTypePtr) calloc(NUM_MB_TYPE_CTX, sizeof(BiContextType) );
00195 mb_aff_ctx_copy = (BiContextTypePtr) calloc(NUM_MB_AFF_CTX, sizeof(BiContextType) );
00196
00197
00198 memcpy(dep_dp_copy,dep_dp,sizeof(DecodingEnvironment));
00199 length = *(dep_dp_copy->Dcodestrm_len) = *(dep_dp->Dcodestrm_len);
00200 for (i=0;i<3;++i)
00201 memcpy(mb_type_ctx_copy[i], currSlice->mot_ctx->mb_type_contexts[i],NUM_MB_TYPE_CTX*sizeof(BiContextType) );
00202 memcpy(mb_aff_ctx_copy, currSlice->mot_ctx->mb_aff_contexts,NUM_MB_AFF_CTX*sizeof(BiContextType) );
00203
00204
00205 #if TRACE
00206 strncpy(se->tracestring, "mb_skip_flag (of following bottom MB)", TRACESTRING_SIZE);
00207 #endif
00208 currSlice->last_dquant = 0;
00209 readMB_skip_flagInfo_CABAC(currMB, se, dep_dp);
00210
00211 skip = (bframe)? (se->value1==0 && se->value2==0) : (se->value1==0);
00212 if (!skip)
00213 {
00214 #if TRACE
00215 strncpy(se->tracestring, "mb_field_decoding_flag (of following bottom MB)", TRACESTRING_SIZE);
00216 #endif
00217 readFieldModeInfo_CABAC( currMB, se,dep_dp);
00218 field = se->value1;
00219 p_Img->mb_data[p_Img->current_mb_nr-1].mb_field = field;
00220 }
00221
00222
00223 p_Img->current_mb_nr--;
00224
00225 memcpy(dep_dp,dep_dp_copy,sizeof(DecodingEnvironment));
00226 *(dep_dp->Dcodestrm_len) = length;
00227 for (i=0;i<3;++i)
00228 memcpy(currSlice->mot_ctx->mb_type_contexts[i],mb_type_ctx_copy[i], NUM_MB_TYPE_CTX*sizeof(BiContextType) );
00229 memcpy( currSlice->mot_ctx->mb_aff_contexts,mb_aff_ctx_copy,NUM_MB_AFF_CTX*sizeof(BiContextType) );
00230
00231 CheckAvailabilityOfNeighborsCABAC(currMB);
00232
00233
00234 free(dep_dp_copy);
00235 for (i=0;i<3;++i)
00236 free(mb_type_ctx_copy[i]);
00237 free(mb_aff_ctx_copy);
00238
00239 return skip;
00240 }
00241
00242
00243
00244
00252 void readMVD_CABAC( Macroblock *currMB,
00253 SyntaxElement *se,
00254 DecodingEnvironmentPtr dep_dp)
00255 {
00256 ImageParameters *p_Img = currMB->p_Img;
00257 Slice *currSlice = currMB->p_Slice;
00258 MotionInfoContexts *ctx = currSlice->mot_ctx;
00259 int i = currMB->subblock_x;
00260 int j = currMB->subblock_y;
00261 int a = 0, b = 0;
00262 int act_ctx;
00263 int act_sym;
00264 int mv_local_err;
00265 int list_idx = se->value2 & 0x01;
00266 int k = (se->value2 >> 1);
00267
00268 PixelPos block_a, block_b;
00269
00270 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_LUMA], &block_a);
00271 get4x4Neighbour(currMB, i, j - 1, p_Img->mb_size[IS_LUMA], &block_b);
00272
00273 if (block_b.available)
00274 {
00275 b = iabs(p_Img->mb_data[block_b.mb_addr].mvd[list_idx][block_b.y][block_b.x][k]);
00276 if (currSlice->MbaffFrameFlag && (k==1))
00277 {
00278 if ((currMB->mb_field==0) && (p_Img->mb_data[block_b.mb_addr].mb_field==1))
00279 b *= 2;
00280 else if ((currMB->mb_field==1) && (p_Img->mb_data[block_b.mb_addr].mb_field==0))
00281 b /= 2;
00282 }
00283 }
00284
00285 if (block_a.available)
00286 {
00287 a = iabs(p_Img->mb_data[block_a.mb_addr].mvd[list_idx][block_a.y][block_a.x][k]);
00288 if (currSlice->MbaffFrameFlag && (k==1))
00289 {
00290 if ((currMB->mb_field==0) && (p_Img->mb_data[block_a.mb_addr].mb_field==1))
00291 a *= 2;
00292 else if ((currMB->mb_field==1) && (p_Img->mb_data[block_a.mb_addr].mb_field==0))
00293 a /= 2;
00294 }
00295 }
00296
00297 if ((mv_local_err = a + b)<3)
00298 act_ctx = 5*k;
00299 else
00300 {
00301 if (mv_local_err > 32)
00302 act_ctx = 5*k+3;
00303 else
00304 act_ctx = 5*k+2;
00305 }
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_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
00326 fflush(p_Dec->p_trace);
00327 #endif
00328 }
00329
00330
00337 void readB8_typeInfo_CABAC (Macroblock *currMB,
00338 SyntaxElement *se,
00339 DecodingEnvironmentPtr dep_dp)
00340 {
00341 Slice *currSlice = currMB->p_Slice;
00342 int act_sym = 0;
00343 int bframe = (currSlice->slice_type == B_SLICE);
00344
00345 MotionInfoContexts *ctx = currSlice->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_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
00410 fflush(p_Dec->p_trace);
00411 #endif
00412 }
00413
00421 void readMB_skip_flagInfo_CABAC( Macroblock *currMB,
00422 SyntaxElement *se,
00423 DecodingEnvironmentPtr dep_dp)
00424 {
00425 Slice *currSlice = currMB->p_Slice;
00426 int bframe=(currSlice->slice_type == B_SLICE);
00427 MotionInfoContexts *ctx = currSlice->mot_ctx;
00428 int a = (currMB->mb_left != NULL) ? (currMB->mb_left->skip_flag == 0) : 0;
00429 int b = (currMB->mb_up != NULL) ? (currMB->mb_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_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
00454 fflush(p_Dec->p_trace);
00455 #endif
00456 if (!se->value1)
00457 {
00458 currSlice->last_dquant = 0;
00459 }
00460 }
00461
00470 void readMB_transform_size_flag_CABAC( Macroblock *currMB,
00471 SyntaxElement *se,
00472 DecodingEnvironmentPtr dep_dp)
00473 {
00474 Slice *currSlice = currMB->p_Slice;
00475 TextureInfoContexts*ctx = currSlice->tex_ctx;
00476
00477 int b = (currMB->mb_up == NULL) ? 0 : currMB->mb_up->luma_transform_size_8x8_flag;
00478 int a = (currMB->mb_left == NULL) ? 0 : currMB->mb_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_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
00487 fflush(p_Dec->p_trace);
00488 #endif
00489
00490 }
00491
00499 void readMB_typeInfo_CABAC(Macroblock *currMB,
00500 SyntaxElement *se,
00501 DecodingEnvironmentPtr dep_dp)
00502 {
00503 Slice *currSlice = currMB->p_Slice;
00504 MotionInfoContexts *ctx = currSlice->mot_ctx;
00505
00506 int a = 0, b = 0;
00507 int act_ctx;
00508 int act_sym;
00509 int bframe=(currSlice->slice_type == B_SLICE);
00510 int mode_sym;
00511 int curr_mb_type;
00512
00513
00514 if(currSlice->slice_type == I_SLICE)
00515 {
00516 if (currMB->mb_up != NULL)
00517 b = (((currMB->mb_up)->mb_type != I4MB && currMB->mb_up->mb_type != I8MB) ? 1 : 0 );
00518
00519 if (currMB->mb_left != NULL)
00520 a = (((currMB->mb_left)->mb_type != I4MB && currMB->mb_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(currSlice->slice_type == SI_SLICE)
00566 {
00567
00568 if (currMB->mb_up != NULL)
00569 b = (( (currMB->mb_up)->mb_type != SI4MB) ? 1 : 0 );
00570
00571 if (currMB->mb_left != NULL)
00572 a = (( (currMB->mb_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_up != NULL)
00585 b = (( (currMB->mb_up)->mb_type != I4MB) ? 1 : 0 );
00586
00587 if (currMB->mb_left != NULL)
00588 a = (( (currMB->mb_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_up != NULL)
00639 b = (( (currMB->mb_up)->mb_type != 0) ? 1 : 0 );
00640
00641 if (currMB->mb_left != NULL)
00642 a = (( (currMB->mb_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 || (((currSlice->slice_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_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
00754 fflush(p_Dec->p_trace);
00755 #endif
00756 }
00757
00765 void readIntraPredMode_CABAC( Macroblock *currMB,
00766 SyntaxElement *se,
00767 DecodingEnvironmentPtr dep_dp)
00768 {
00769 Slice *currSlice = currMB->p_Slice;
00770 TextureInfoContexts *ctx = currSlice->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_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
00789 fflush(p_Dec->p_trace);
00790 #endif
00791 }
00799 void readRefFrame_CABAC(Macroblock *currMB,
00800 SyntaxElement *se,
00801 DecodingEnvironmentPtr dep_dp)
00802 {
00803 Slice *currSlice = currMB->p_Slice;
00804 ImageParameters *p_Img = currMB->p_Img;
00805 StorablePicture *dec_picture = p_Img->dec_picture;
00806 MotionInfoContexts *ctx = currSlice->mot_ctx;
00807 Macroblock *neighborMB = NULL;
00808
00809 int addctx = 0;
00810 int a = 0, b = 0;
00811 int act_ctx;
00812 int act_sym;
00813 char** refframe_array = dec_picture->motion.ref_idx[se->value2];
00814
00815 PixelPos block_a, block_b;
00816
00817 get4x4Neighbour(currMB, currMB->subblock_x - 1, currMB->subblock_y , p_Img->mb_size[IS_LUMA], &block_a);
00818 get4x4Neighbour(currMB, currMB->subblock_x, currMB->subblock_y - 1, p_Img->mb_size[IS_LUMA], &block_b);
00819
00820 if (block_b.available)
00821 {
00822 int b8b=((block_b.x >> 1) & 0x01)+(block_b.y & 0x02);
00823 neighborMB = &p_Img->mb_data[block_b.mb_addr];
00824 if (!( (neighborMB->mb_type==IPCM) || IS_DIRECT(neighborMB) || (neighborMB->b8mode[b8b]==0 && neighborMB->b8pdir[b8b]==2)))
00825 {
00826 if (currSlice->MbaffFrameFlag && (currMB->mb_field == FALSE) && (neighborMB->mb_field == TRUE))
00827 b = (refframe_array[block_b.pos_y][block_b.pos_x] > 1 ? 2 : 0);
00828 else
00829 b = (refframe_array[block_b.pos_y][block_b.pos_x] > 0 ? 2 : 0);
00830 }
00831 }
00832
00833 if (block_a.available)
00834 {
00835 int b8a=((block_a.x >> 1) & 0x01)+(block_a.y & 0x02);
00836 neighborMB = &p_Img->mb_data[block_a.mb_addr];
00837 if (!((neighborMB->mb_type==IPCM) || IS_DIRECT(neighborMB) || (neighborMB->b8mode[b8a]==0 && neighborMB->b8pdir[b8a]==2)))
00838 {
00839 if (currSlice->MbaffFrameFlag && (currMB->mb_field == FALSE) && (neighborMB->mb_field == 1))
00840 a = (refframe_array[block_a.pos_y][block_a.pos_x] > 1 ? 1 : 0);
00841 else
00842 a = (refframe_array[block_a.pos_y][block_a.pos_x] > 0 ? 1 : 0);
00843 }
00844 }
00845
00846 act_ctx = a + b;
00847 se->context = act_ctx;
00848
00849 act_sym = biari_decode_symbol(dep_dp,ctx->ref_no_contexts[addctx] + act_ctx );
00850
00851 if (act_sym != 0)
00852 {
00853 act_ctx = 4;
00854 act_sym = unary_bin_decode(dep_dp,ctx->ref_no_contexts[addctx] + act_ctx,1);
00855 ++act_sym;
00856 }
00857 se->value1 = act_sym;
00858
00859 #if TRACE
00860 fprintf(p_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
00861
00862 fflush(p_Dec->p_trace);
00863 #endif
00864 }
00865
00866
00874 void readDquant_CABAC( Macroblock *currMB,
00875 SyntaxElement *se,
00876 DecodingEnvironmentPtr dep_dp)
00877 {
00878 Slice *currSlice = currMB->p_Slice;
00879 MotionInfoContexts *ctx = currSlice->mot_ctx;
00880 int *dquant = &se->value1;
00881 int act_ctx = ((currSlice->last_dquant != 0) ? 1 : 0);
00882 int act_sym = biari_decode_symbol(dep_dp,ctx->delta_qp_contexts + act_ctx );
00883
00884 if (act_sym != 0)
00885 {
00886 act_ctx = 2;
00887 act_sym = unary_bin_decode(dep_dp,ctx->delta_qp_contexts + act_ctx,1);
00888 ++act_sym;
00889 }
00890
00891 *dquant = (act_sym + 1) >> 1;
00892 if((act_sym & 0x01)==0)
00893 *dquant = -*dquant;
00894
00895 currSlice->last_dquant = *dquant;
00896
00897 #if TRACE
00898 fprintf(p_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
00899 fflush(p_Dec->p_trace);
00900 #endif
00901 }
00909 void readCBP_CABAC(Macroblock *currMB,
00910 SyntaxElement *se,
00911 DecodingEnvironmentPtr dep_dp)
00912 {
00913 ImageParameters *p_Img = currMB->p_Img;
00914 StorablePicture *dec_picture = p_Img->dec_picture;
00915 Slice *currSlice = currMB->p_Slice;
00916 TextureInfoContexts *ctx = currSlice->tex_ctx;
00917 Macroblock *neighborMB = NULL;
00918
00919 int mb_x, mb_y;
00920 int a = 0, b = 0;
00921 int curr_cbp_ctx;
00922 int cbp = 0;
00923 int cbp_bit;
00924 int mask;
00925 PixelPos block_a;
00926
00927
00928 for (mb_y=0; mb_y < 4; mb_y += 2)
00929 {
00930 if (mb_y == 0)
00931 {
00932 neighborMB = currMB->mb_up;
00933 b = 0;
00934 }
00935
00936 for (mb_x=0; mb_x < 4; mb_x += 2)
00937 {
00938 if (mb_y == 0)
00939 {
00940 if (neighborMB != NULL)
00941 {
00942 if(neighborMB->mb_type!=IPCM)
00943 b = (( (neighborMB->cbp & (1<<(2 + (mb_x>>1)))) == 0) ? 2 : 0);
00944 }
00945 }
00946 else
00947 b = ( ((cbp & (1<<(mb_x/2))) == 0) ? 2: 0);
00948
00949 if (mb_x == 0)
00950 {
00951 get4x4Neighbour(currMB, (mb_x<<2) - 1, (mb_y << 2), p_Img->mb_size[IS_LUMA], &block_a);
00952 if (block_a.available)
00953 {
00954 if(p_Img->mb_data[block_a.mb_addr].mb_type==IPCM)
00955 a = 0;
00956 else
00957 a = (( (p_Img->mb_data[block_a.mb_addr].cbp & (1<<(2*(block_a.y/2)+1))) == 0) ? 1 : 0);
00958 }
00959 else
00960 a=0;
00961 }
00962 else
00963 a = ( ((cbp & (1<<mb_y)) == 0) ? 1: 0);
00964
00965 curr_cbp_ctx = a + b;
00966 mask = (1 << (mb_y + (mb_x >> 1)));
00967 cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[0] + curr_cbp_ctx );
00968 if (cbp_bit)
00969 cbp += mask;
00970 }
00971 }
00972
00973 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
00974 {
00975
00976
00977 b = 0;
00978 neighborMB = currMB->mb_up;
00979 if (neighborMB != NULL)
00980 {
00981 if (neighborMB->mb_type==IPCM || (neighborMB->cbp > 15))
00982 b = 2;
00983 }
00984
00985 a = 0;
00986 neighborMB = currMB->mb_left;
00987 if (neighborMB != NULL)
00988 {
00989 if (neighborMB->mb_type==IPCM || (neighborMB->cbp > 15))
00990 a = 1;
00991 }
00992
00993 curr_cbp_ctx = a + b;
00994 cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[1] + curr_cbp_ctx );
00995
00996
00997 if (cbp_bit)
00998 {
00999 b = 0;
01000 neighborMB = currMB->mb_up;
01001 if (neighborMB != NULL)
01002 {
01003
01004 if ((neighborMB->mb_type == IPCM) || ((neighborMB->cbp >> 4) == 2))
01005 b = 2;
01006 }
01007
01008
01009 a = 0;
01010 neighborMB = currMB->mb_left;
01011 if (neighborMB != NULL)
01012 {
01013 if ((neighborMB->mb_type == IPCM) || ((neighborMB->cbp >> 4) == 2))
01014 a = 1;
01015 }
01016
01017 curr_cbp_ctx = a + b;
01018 cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[2] + curr_cbp_ctx );
01019 cbp += (cbp_bit == 1) ? 32 : 16;
01020 }
01021 }
01022
01023 se->value1 = cbp;
01024
01025 if (!cbp)
01026 {
01027 currSlice->last_dquant = 0;
01028 }
01029
01030 #if TRACE
01031 fprintf(p_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
01032 fflush(p_Dec->p_trace);
01033 #endif
01034 }
01035
01043 void readCIPredMode_CABAC(Macroblock *currMB,
01044 SyntaxElement *se,
01045 DecodingEnvironmentPtr dep_dp)
01046 {
01047 Slice *currSlice = currMB->p_Slice;
01048 TextureInfoContexts *ctx = currSlice->tex_ctx;
01049 int *act_sym = &se->value1;
01050
01051 Macroblock *MbUp = currMB->mb_up;
01052 Macroblock *MbLeft = currMB->mb_left;
01053
01054 int b = (MbUp != NULL) ? (((MbUp->c_ipred_mode != 0) && (MbUp->mb_type != IPCM)) ? 1 : 0) : 0;
01055 int a = (MbLeft != NULL) ? (((MbLeft->c_ipred_mode != 0) && (MbLeft->mb_type != IPCM)) ? 1 : 0) : 0;
01056 int act_ctx = a + b;
01057
01058 *act_sym = biari_decode_symbol(dep_dp, ctx->cipr_contexts + act_ctx );
01059
01060 if (*act_sym != 0)
01061 *act_sym = unary_bin_max_decode(dep_dp, ctx->cipr_contexts + 3, 0, 1) + 1;
01062
01063 #if TRACE
01064 fprintf(p_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
01065 fflush(p_Dec->p_trace);
01066 #endif
01067
01068 }
01069
01070 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};
01071 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};
01072 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};
01073 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};
01074 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};
01075 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};
01076 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};
01077 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};
01078
01079
01080
01087 int read_and_store_CBP_block_bit_444 (Macroblock *currMB,
01088 DecodingEnvironmentPtr dep_dp,
01089 int type)
01090 {
01091 Slice *currSlice = currMB->p_Slice;
01092 ImageParameters *p_Img = currMB->p_Img;
01093 StorablePicture *dec_picture = p_Img->dec_picture;
01094 TextureInfoContexts *tex_ctx = currSlice->tex_ctx;
01095
01096 int y_ac = (type==LUMA_16AC || type==LUMA_8x8 || type==LUMA_8x4 || type==LUMA_4x8 || type==LUMA_4x4
01097 || type==CB_16AC || type==CB_8x8 || type==CB_8x4 || type==CB_4x8 || type==CB_4x4
01098 || type==CR_16AC || type==CR_8x8 || type==CR_8x4 || type==CR_4x8 || type==CR_4x4);
01099 int y_dc = (type==LUMA_16DC || type==CB_16DC || type==CR_16DC);
01100 int u_ac = (type==CHROMA_AC && !currMB->is_v_block);
01101 int v_ac = (type==CHROMA_AC && currMB->is_v_block);
01102 int chroma_dc = (type==CHROMA_DC || type==CHROMA_DC_2x4 || type==CHROMA_DC_4x4);
01103 int u_dc = (chroma_dc && !currMB->is_v_block);
01104 int v_dc = (chroma_dc && currMB->is_v_block);
01105 int j = (y_ac || u_ac || v_ac ? currMB->subblock_y : 0);
01106 int i = (y_ac || u_ac || v_ac ? currMB->subblock_x : 0);
01107 int bit = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 35);
01108 int default_bit = (currMB->is_intra_block ? 1 : 0);
01109 int upper_bit = default_bit;
01110 int left_bit = default_bit;
01111 int cbp_bit = 1;
01112 int ctx;
01113 int bit_pos_a = 0;
01114 int bit_pos_b = 0;
01115
01116 PixelPos block_a, block_b;
01117 if (y_ac)
01118 {
01119 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_LUMA], &block_a);
01120 get4x4Neighbour(currMB, i , j - 1, p_Img->mb_size[IS_LUMA], &block_b);
01121 if (block_a.available)
01122 bit_pos_a = 4*block_a.y + block_a.x;
01123 if (block_b.available)
01124 bit_pos_b = 4*block_b.y + block_b.x;
01125 }
01126 else if (y_dc)
01127 {
01128 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_LUMA], &block_a);
01129 get4x4Neighbour(currMB, i , j - 1, p_Img->mb_size[IS_LUMA], &block_b);
01130 }
01131 else if (u_ac||v_ac)
01132 {
01133 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_CHROMA], &block_a);
01134 get4x4Neighbour(currMB, i , j - 1, p_Img->mb_size[IS_CHROMA], &block_b);
01135 if (block_a.available)
01136 bit_pos_a = 4*block_a.y + block_a.x;
01137 if (block_b.available)
01138 bit_pos_b = 4*block_b.y + block_b.x;
01139 }
01140 else
01141 {
01142 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_CHROMA], &block_a);
01143 get4x4Neighbour(currMB, i , j - 1, p_Img->mb_size[IS_CHROMA], &block_b);
01144 }
01145
01146 if (dec_picture->chroma_format_idc!=YUV444)
01147 {
01148 if (type!=LUMA_8x8)
01149 {
01150
01151 if (block_b.available)
01152 {
01153 if(p_Img->mb_data[block_b.mb_addr].mb_type==IPCM)
01154 upper_bit=1;
01155 else
01156 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[0], bit + bit_pos_b);
01157 }
01158
01159 if (block_a.available)
01160 {
01161 if(p_Img->mb_data[block_a.mb_addr].mb_type==IPCM)
01162 left_bit=1;
01163 else
01164 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[0],bit + bit_pos_a);
01165 }
01166
01167
01168 ctx = 2 * upper_bit + left_bit;
01169
01170 cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
01171 }
01172 }
01173 else if( IS_INDEPENDENT(p_Img) )
01174 {
01175 if (type!=LUMA_8x8)
01176 {
01177
01178 if (block_b.available)
01179 {
01180 if(p_Img->mb_data[block_b.mb_addr].mb_type==IPCM)
01181 upper_bit = 1;
01182 else
01183 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[0],bit+bit_pos_b);
01184 }
01185
01186
01187 if (block_a.available)
01188 {
01189 if(p_Img->mb_data[block_a.mb_addr].mb_type==IPCM)
01190 left_bit = 1;
01191 else
01192 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[0],bit+bit_pos_a);
01193 }
01194
01195
01196 ctx = 2 * upper_bit + left_bit;
01197
01198 cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
01199 }
01200 }
01201 else {
01202 if (block_b.available)
01203 {
01204 if(p_Img->mb_data[block_b.mb_addr].mb_type==IPCM)
01205 upper_bit=1;
01206 else
01207 {
01208 if(type==LUMA_8x8)
01209 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits_8x8[0], bit + bit_pos_b);
01210 else if (type==CB_8x8)
01211 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits_8x8[1], bit + bit_pos_b);
01212 else if (type==CR_8x8)
01213 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits_8x8[2], bit + bit_pos_b);
01214 else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
01215 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[1],bit+bit_pos_b);
01216 else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
01217 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[2],bit+bit_pos_b);
01218 else
01219 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[0],bit+bit_pos_b);
01220 }
01221 }
01222
01223
01224 if (block_a.available)
01225 {
01226 if(p_Img->mb_data[block_a.mb_addr].mb_type==IPCM)
01227 left_bit=1;
01228 else
01229 {
01230 if(type==LUMA_8x8)
01231 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits_8x8[0],bit+bit_pos_a);
01232 else if (type==CB_8x8)
01233 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits_8x8[1],bit+bit_pos_a);
01234 else if (type==CR_8x8)
01235 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits_8x8[2],bit+bit_pos_a);
01236 else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
01237 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[1],bit+bit_pos_a);
01238 else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
01239 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[2],bit+bit_pos_a);
01240 else
01241 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[0],bit+bit_pos_a);
01242 }
01243 }
01244
01245 ctx = 2 * upper_bit + left_bit;
01246
01247 cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
01248 }
01249
01250
01251 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));
01252
01253 if (cbp_bit)
01254 {
01255 if (type==LUMA_8x8)
01256 {
01257 currMB->cbp_bits[0] |= ((int64) 0x33 << bit );
01258
01259 if (dec_picture->chroma_format_idc==YUV444)
01260 {
01261 currMB->cbp_bits_8x8[0] |= ((int64) 0x33 << bit );
01262 }
01263 }
01264 else if (type==CB_8x8)
01265 {
01266 currMB->cbp_bits_8x8[1] |= ((int64) 0x33 << bit );
01267 currMB->cbp_bits[1] |= ((int64) 0x33 << bit );
01268 }
01269 else if (type==CR_8x8)
01270 {
01271 currMB->cbp_bits_8x8[2] |= ((int64) 0x33 << bit );
01272 currMB->cbp_bits[2] |= ((int64) 0x33 << bit );
01273 }
01274 else if (type==LUMA_8x4)
01275 {
01276 currMB->cbp_bits[0] |= ((int64) 0x03 << bit );
01277 }
01278 else if (type==CB_8x4)
01279 {
01280 currMB->cbp_bits[1] |= ((int64) 0x03 << bit );
01281 }
01282 else if (type==CR_8x4)
01283 {
01284 currMB->cbp_bits[2] |= ((int64) 0x03 << bit );
01285 }
01286 else if (type==LUMA_4x8)
01287 {
01288 currMB->cbp_bits[0] |= ((int64) 0x11<< bit );
01289 }
01290 else if (type==CB_4x8)
01291 {
01292 currMB->cbp_bits[1] |= ((int64)0x11<< bit );
01293 }
01294 else if (type==CR_4x8)
01295 {
01296 currMB->cbp_bits[2] |= ((int64)0x11<< bit );
01297 }
01298 else if ((type==CB_4x4)||(type==CB_16AC)||(type==CB_16DC))
01299 {
01300 currMB->cbp_bits[1] |= ((int64)0x01<<bit);
01301 }
01302 else if ((type==CR_4x4)||(type==CR_16AC)||(type==CR_16DC))
01303 {
01304 currMB->cbp_bits[2] |= ((int64)0x01<<bit);
01305 }
01306 else
01307 {
01308 currMB->cbp_bits[0] |= ((int64)0x01<<bit);
01309 }
01310 }
01311 return cbp_bit;
01312 }
01313
01314
01321 int read_and_store_CBP_block_bit_normal (Macroblock *currMB,
01322 DecodingEnvironmentPtr dep_dp,
01323 int type)
01324 {
01325 Slice *currSlice = currMB->p_Slice;
01326 ImageParameters *p_Img = currMB->p_Img;
01327 TextureInfoContexts *tex_ctx = currSlice->tex_ctx;
01328
01329 int y_ac = (type==LUMA_16AC || type==LUMA_8x8 || type==LUMA_8x4 || type==LUMA_4x8 || type==LUMA_4x4);
01330 int y_dc = (type==LUMA_16DC);
01331 int u_ac = (type==CHROMA_AC && !currMB->is_v_block);
01332 int v_ac = (type==CHROMA_AC && currMB->is_v_block);
01333 int chroma_dc = (type==CHROMA_DC || type==CHROMA_DC_2x4 || type==CHROMA_DC_4x4);
01334 int u_dc = (chroma_dc && !currMB->is_v_block);
01335 int v_dc = (chroma_dc && currMB->is_v_block);
01336 int j = (y_ac || u_ac || v_ac ? currMB->subblock_y : 0);
01337 int i = (y_ac || u_ac || v_ac ? currMB->subblock_x : 0);
01338 int bit = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 35);
01339 int default_bit = (currMB->is_intra_block ? 1 : 0);
01340 int upper_bit = default_bit;
01341 int left_bit = default_bit;
01342 int cbp_bit = 1;
01343 int ctx;
01344 int bit_pos_a = 0;
01345 int bit_pos_b = 0;
01346
01347 PixelPos block_a, block_b;
01348
01349 if (y_ac)
01350 {
01351 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_LUMA], &block_a);
01352 get4x4Neighbour(currMB, i , j - 1, p_Img->mb_size[IS_LUMA], &block_b);
01353 if (block_a.available)
01354 bit_pos_a = 4*block_a.y + block_a.x;
01355 if (block_b.available)
01356 bit_pos_b = 4*block_b.y + block_b.x;
01357 }
01358 else if (y_dc)
01359 {
01360 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_LUMA], &block_a);
01361 get4x4Neighbour(currMB, i , j - 1, p_Img->mb_size[IS_LUMA], &block_b);
01362 }
01363 else if (u_ac||v_ac)
01364 {
01365 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_CHROMA], &block_a);
01366 get4x4Neighbour(currMB, i , j - 1, p_Img->mb_size[IS_CHROMA], &block_b);
01367 if (block_a.available)
01368 bit_pos_a = 4*block_a.y + block_a.x;
01369 if (block_b.available)
01370 bit_pos_b = 4*block_b.y + block_b.x;
01371 }
01372 else
01373 {
01374 get4x4Neighbour(currMB, i - 1, j , p_Img->mb_size[IS_CHROMA], &block_a);
01375 get4x4Neighbour(currMB, i , j - 1, p_Img->mb_size[IS_CHROMA], &block_b);
01376 }
01377
01378 if (type!=LUMA_8x8)
01379 {
01380
01381 if (block_b.available)
01382 {
01383 if(p_Img->mb_data[block_b.mb_addr].mb_type==IPCM)
01384 upper_bit=1;
01385 else
01386 upper_bit = get_bit(p_Img->mb_data[block_b.mb_addr].cbp_bits[0], bit + bit_pos_b);
01387 }
01388
01389 if (block_a.available)
01390 {
01391 if(p_Img->mb_data[block_a.mb_addr].mb_type==IPCM)
01392 left_bit=1;
01393 else
01394 left_bit = get_bit(p_Img->mb_data[block_a.mb_addr].cbp_bits[0],bit + bit_pos_a);
01395 }
01396
01397 ctx = 2 * upper_bit + left_bit;
01398
01399 cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
01400 }
01401
01402
01403 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));
01404
01405 if (cbp_bit)
01406 {
01407 if (type==LUMA_8x8)
01408 {
01409 currMB->cbp_bits[0] |= ((int64) 0x33 << bit );
01410 }
01411 else if (type==LUMA_8x4)
01412 {
01413 currMB->cbp_bits[0] |= ((int64) 0x03 << bit );
01414 }
01415 else if (type==LUMA_4x8)
01416 {
01417 currMB->cbp_bits[0] |= ((int64) 0x11<< bit );
01418 }
01419 else
01420 {
01421 currMB->cbp_bits[0] |= ((int64)0x01<<bit);
01422 }
01423 }
01424 return cbp_bit;
01425 }
01426
01427
01428
01429
01430
01431
01432
01433
01434 static const byte pos2ctx_map8x8 [] = { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
01435 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9, 10, 9, 8, 7,
01436 7, 6, 11, 12, 13, 11, 6, 7, 8, 9, 14, 10, 9, 8, 6, 11,
01437 12, 13, 11, 6, 9, 14, 10, 9, 11, 12, 13, 11 ,14, 10, 12, 14};
01438 static const byte pos2ctx_map8x4 [] = { 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 9, 8, 6, 7, 8,
01439 9, 10, 11, 9, 8, 6, 12, 8, 9, 10, 11, 9, 13, 13, 14, 14};
01440 static const byte pos2ctx_map4x4 [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14};
01441 static const byte pos2ctx_map2x4c[] = { 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
01442 static const byte pos2ctx_map4x4c[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2};
01443 static const byte* pos2ctx_map [] = {pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8, pos2ctx_map8x4,
01444 pos2ctx_map8x4, pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
01445 pos2ctx_map2x4c, pos2ctx_map4x4c,
01446 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8,pos2ctx_map8x4,
01447 pos2ctx_map8x4, pos2ctx_map4x4,
01448 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8,pos2ctx_map8x4,
01449 pos2ctx_map8x4,pos2ctx_map4x4};
01450
01451
01452 static const byte pos2ctx_map8x8i[] = { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
01453 6, 9, 10, 10, 8, 11, 12, 11, 9, 9, 10, 10, 8, 11, 12, 11,
01454 9, 9, 10, 10, 8, 11, 12, 11, 9, 9, 10, 10, 8, 13, 13, 9,
01455 9, 10, 10, 8, 13, 13, 9, 9, 10, 10, 14, 14, 14, 14, 14, 14};
01456 static const byte pos2ctx_map8x4i[] = { 0, 1, 2, 3, 4, 5, 6, 3, 4, 5, 6, 3, 4, 7, 6, 8,
01457 9, 7, 6, 8, 9, 10, 11, 12, 12, 10, 11, 13, 13, 14, 14, 14};
01458 static const byte pos2ctx_map4x8i[] = { 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 6, 2, 7, 7, 8,
01459 8, 8, 5, 6, 9, 10, 10, 11, 11, 11, 12, 13, 13, 14, 14, 14};
01460 static const byte* pos2ctx_map_int[] = {pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
01461 pos2ctx_map4x8i,pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
01462 pos2ctx_map2x4c, pos2ctx_map4x4c,
01463 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
01464 pos2ctx_map8x4i,pos2ctx_map4x4,
01465 pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
01466 pos2ctx_map8x4i,pos2ctx_map4x4};
01467
01468
01469 static const byte pos2ctx_last8x8 [] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
01470 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
01471 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
01472 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8};
01473 static const byte pos2ctx_last8x4 [] = { 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
01474 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8};
01475
01476 static const byte pos2ctx_last4x4 [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
01477 static const byte pos2ctx_last2x4c[] = { 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
01478 static const byte pos2ctx_last4x4c[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2};
01479 static const byte* pos2ctx_last [] = {pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8, pos2ctx_last8x4,
01480 pos2ctx_last8x4, pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last4x4,
01481 pos2ctx_last2x4c, pos2ctx_last4x4c,
01482 pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8,pos2ctx_last8x4,
01483 pos2ctx_last8x4, pos2ctx_last4x4,
01484 pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8,pos2ctx_last8x4,
01485 pos2ctx_last8x4, pos2ctx_last4x4};
01486
01487
01488
01495 int read_significance_map (Macroblock *currMB,
01496 DecodingEnvironmentPtr dep_dp,
01497 int type,
01498 int coeff[])
01499 {
01500 int i, sig;
01501 int coeff_ctr = 0;
01502 int i0 = 0;
01503 int i1 = maxpos[type];
01504 ImageParameters *p_Img = currMB->p_Img;
01505
01506 int fld = ( p_Img->structure!=FRAME || currMB->mb_field );
01507 const byte **pos2ctx_Map = (fld) ? pos2ctx_map_int : pos2ctx_map;
01508 Slice *currSlice = currMB->p_Slice;
01509 TextureInfoContexts *tex_ctx = currSlice->tex_ctx;
01510
01511 BiContextTypePtr map_ctx = tex_ctx->map_contexts[fld][type2ctx_map [type]];
01512 BiContextTypePtr last_ctx = tex_ctx->last_contexts[fld][type2ctx_last[type]];
01513
01514 if (!c1isdc[type])
01515 {
01516 ++i0;
01517 ++i1;
01518 coeff--;
01519 }
01520
01521 for (i=i0; i < i1; ++i)
01522 {
01523
01524 sig = biari_decode_symbol (dep_dp, map_ctx + pos2ctx_Map [type][i]);
01525
01526 if (sig)
01527 {
01528 coeff[i] = 1;
01529 ++coeff_ctr;
01530
01531 if (biari_decode_symbol (dep_dp, last_ctx + pos2ctx_last[type][i]))
01532 {
01533 memset(&coeff[i + 1], 0, (i1 - i) * sizeof(int));
01534 i = i1;
01535 }
01536 }
01537 else
01538 {
01539 coeff[i] = 0;
01540 }
01541 }
01542
01543 if (i < i1 + 1)
01544 {
01545 coeff[i] = 1;
01546 ++coeff_ctr;
01547 }
01548
01549 return coeff_ctr;
01550 }
01551
01552
01553
01560 void read_significant_coefficients (DecodingEnvironmentPtr dep_dp,
01561 TextureInfoContexts *tex_ctx,
01562 int type,
01563 int coeff[])
01564 {
01565 int i, ctx;
01566 int c1 = 1;
01567 int c2 = 0;
01568 BiContextType *one_contexts = tex_ctx->one_contexts[type2ctx_one[type]];
01569 BiContextType *abs_contexts = tex_ctx->abs_contexts[type2ctx_abs[type]];
01570
01571 for (i=maxpos[type]; i>=0; i--)
01572 {
01573 if (coeff[i]!=0)
01574 {
01575 ctx = imin (c1,4);
01576 coeff[i] += biari_decode_symbol (dep_dp, one_contexts + ctx);
01577 if (coeff[i]==2)
01578 {
01579 ctx = imin (c2++, max_c2[type]);
01580 coeff[i] += unary_exp_golomb_level_decode (dep_dp, abs_contexts + ctx);
01581 c1=0;
01582 }
01583 else if (c1)
01584 {
01585 ++c1;
01586 }
01587 if (biari_decode_symbol_eq_prob(dep_dp))
01588 {
01589 coeff[i] *= -1;
01590 }
01591 }
01592 }
01593 }
01594
01595
01602 void readRunLevel_CABAC (Macroblock *currMB,
01603 SyntaxElement *se,
01604 DecodingEnvironmentPtr dep_dp)
01605 {
01606 Slice *currSlice = currMB->p_Slice;
01607 ImageParameters *p_Img = currMB->p_Img;
01608 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
01609
01610
01611 if (active_sps->chroma_format_idc == YUV444)
01612 currMB->read_and_store_CBP_block_bit = read_and_store_CBP_block_bit_444;
01613 else
01614 currMB->read_and_store_CBP_block_bit = read_and_store_CBP_block_bit_normal;
01615
01616
01617
01618 if (currSlice->coeff_ctr < 0)
01619 {
01620
01621 if ((currSlice->coeff_ctr = currMB->read_and_store_CBP_block_bit (currMB, dep_dp, se->context) )!=0)
01622 {
01623
01624 currSlice->coeff_ctr = read_significance_map (currMB, dep_dp, se->context, currSlice->coeff);
01625
01626
01627 read_significant_coefficients (dep_dp, currSlice->tex_ctx, se->context, currSlice->coeff);
01628 }
01629 }
01630
01631
01632 if (currSlice->coeff_ctr)
01633 {
01634
01635 for (se->value2=0; currSlice->coeff[currSlice->pos]==0; ++currSlice->pos, ++se->value2);
01636 se->value1 = currSlice->coeff[currSlice->pos++];
01637 }
01638 else
01639 {
01640
01641 se->value1 = se->value2 = 0;
01642 }
01643
01644 if (currSlice->coeff_ctr-- == 0) currSlice->pos=0;
01645
01646 #if TRACE
01647 fprintf(p_Dec->p_trace, "@%-6d %-53s %3d %3d\n",symbolCount++, se->tracestring, se->value1,se->value2);
01648 fflush(p_Dec->p_trace);
01649 #endif
01650 }
01651
01658 int readSyntaxElement_CABAC(SyntaxElement *se, ImageParameters *p_Img, DataPartition *this_dataPart)
01659 {
01660 Macroblock *currMB = &p_Img->mb_data[p_Img->current_mb_nr];
01661 DecodingEnvironmentPtr dep_dp = &(this_dataPart->de_cabac);
01662 int curr_len = arideco_bits_read(dep_dp);
01663
01664
01665 se->reading(currMB, se, dep_dp);
01666
01667 se->len = (arideco_bits_read(dep_dp) - curr_len);
01668
01669 #if (TRACE==2)
01670 fprintf(p_Dec->p_trace, "curr_len: %d\n",curr_len);
01671 fprintf(p_Dec->p_trace, "se_len: %d\n",se->len);
01672 #endif
01673
01674 return (se->len);
01675 }
01676
01677
01686 static unsigned int unary_bin_max_decode(DecodingEnvironmentPtr dep_dp,
01687 BiContextTypePtr ctx,
01688 int ctx_offset,
01689 unsigned int max_symbol)
01690 {
01691 unsigned int symbol = biari_decode_symbol(dep_dp, ctx );
01692
01693 if (symbol==0 || (max_symbol == 0))
01694 return symbol;
01695 else
01696 {
01697 unsigned int l;
01698 ctx += ctx_offset;
01699 symbol = 0;
01700 do
01701 {
01702 l = biari_decode_symbol(dep_dp, ctx);
01703 ++symbol;
01704 }
01705 while( (l != 0) && (symbol < max_symbol) );
01706
01707 if ((l != 0) && (symbol == max_symbol))
01708 ++symbol;
01709 return symbol;
01710 }
01711 }
01712
01713
01721 static unsigned int unary_bin_decode(DecodingEnvironmentPtr dep_dp,
01722 BiContextTypePtr ctx,
01723 int ctx_offset)
01724 {
01725 unsigned int symbol = biari_decode_symbol(dep_dp, ctx );
01726
01727 if (symbol == 0)
01728 return 0;
01729 else
01730 {
01731 unsigned int l;
01732 ctx += ctx_offset;;
01733 symbol = 0;
01734 do
01735 {
01736 l=biari_decode_symbol(dep_dp, ctx);
01737 ++symbol;
01738 }
01739 while( l != 0 );
01740 return symbol;
01741 }
01742 }
01743
01744
01756 int cabac_startcode_follows(Slice *currSlice, int eos_bit)
01757 {
01758 unsigned int bit;
01759
01760 if( eos_bit )
01761 {
01762 const byte *partMap = assignSE2partition[currSlice->dp_mode];
01763 DataPartition *dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
01764 DecodingEnvironmentPtr dep_dp = &(dP->de_cabac);
01765
01766 bit = biari_decode_final (dep_dp);
01767
01768 #if TRACE
01769 fprintf(p_Dec->p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, "end_of_slice_flag", bit);
01770 fflush(p_Dec->p_trace);
01771 #endif
01772 }
01773 else
01774 {
01775 bit = 0;
01776 }
01777
01778 return (bit == 1 ? 1 : 0);
01779 }
01780
01788 unsigned int exp_golomb_decode_eq_prob( DecodingEnvironmentPtr dep_dp,
01789 int k)
01790 {
01791 unsigned int l;
01792 int symbol = 0;
01793 int binary_symbol = 0;
01794
01795 do
01796 {
01797 l = biari_decode_symbol_eq_prob(dep_dp);
01798 if (l == 1)
01799 {
01800 symbol += (1<<k);
01801 ++k;
01802 }
01803 }
01804 while (l!=0);
01805
01806 while (k--)
01807 if (biari_decode_symbol_eq_prob(dep_dp)==1)
01808 binary_symbol |= (1<<k);
01809
01810 return (unsigned int) (symbol + binary_symbol);
01811 }
01812
01813
01820 static unsigned int unary_exp_golomb_level_decode( DecodingEnvironmentPtr dep_dp,
01821 BiContextTypePtr ctx)
01822 {
01823 unsigned int symbol = biari_decode_symbol(dep_dp, ctx );
01824
01825 if (symbol==0)
01826 return 0;
01827 else
01828 {
01829 unsigned int l, k = 1;
01830 unsigned int exp_start = 13;
01831
01832 symbol=0;
01833
01834 do
01835 {
01836 l=biari_decode_symbol(dep_dp, ctx);
01837 ++symbol;
01838 ++k;
01839 }
01840 while((l != 0) && (k != exp_start));
01841 if (l!=0)
01842 symbol += exp_golomb_decode_eq_prob(dep_dp,0)+1;
01843 return symbol;
01844 }
01845 }
01846
01847
01848
01849
01856 static unsigned int unary_exp_golomb_mv_decode(DecodingEnvironmentPtr dep_dp,
01857 BiContextTypePtr ctx,
01858 unsigned int max_bin)
01859 {
01860 unsigned int symbol = biari_decode_symbol(dep_dp, ctx );
01861
01862 if (symbol == 0)
01863 return 0;
01864 else
01865 {
01866 unsigned int exp_start = 8;
01867 unsigned int l,k = 1;
01868 unsigned int bin = 1;
01869
01870 symbol=0;
01871
01872 ++ctx;
01873 do
01874 {
01875 l=biari_decode_symbol(dep_dp, ctx);
01876 if ((++bin)==2) ctx++;
01877 if (bin==max_bin)
01878 ++ctx;
01879 ++symbol;
01880 ++k;
01881 }
01882 while((l!=0) && (k!=exp_start));
01883 if (l!=0)
01884 symbol += exp_golomb_decode_eq_prob(dep_dp,3) + 1;
01885 return symbol;
01886 }
01887 }
01888
01889
01896 void readIPCM_CABAC(Slice *currSlice, struct datapartition *dP)
01897 {
01898 ImageParameters *p_Img = currSlice->p_Img;
01899 StorablePicture *dec_picture = p_Img->dec_picture;
01900 Bitstream* currStream = dP->bitstream;
01901 DecodingEnvironmentPtr dep = &(dP->de_cabac);
01902 byte *buf = currStream->streamBuffer;
01903 int BitstreamLengthInBits = (dP->bitstream->bitstream_length << 3) + 7;
01904
01905 int val = 0;
01906
01907 int bits_read = 0;
01908 int bitoffset, bitdepth;
01909 int uv, i, j;
01910
01911 while (dep->DbitsLeft >= 8)
01912 {
01913 dep->Dvalue >>= 8;
01914 dep->DbitsLeft -= 8;
01915 (*dep->Dcodestrm_len)--;
01916 }
01917
01918 bitoffset = (*dep->Dcodestrm_len) << 3;
01919
01920
01921 bitdepth = p_Img->bitdepth_luma;
01922 for(i=0;i<MB_BLOCK_SIZE;++i)
01923 {
01924 for(j=0;j<MB_BLOCK_SIZE;++j)
01925 {
01926 bits_read += GetBits(buf, bitoffset, &val, BitstreamLengthInBits, bitdepth);
01927 #if TRACE
01928 tracebits2("pcm_byte luma", bitdepth, val);
01929 #endif
01930 currSlice->cof[0][i][j] = val;
01931
01932 bitoffset += bitdepth;
01933 }
01934 }
01935
01936
01937 bitdepth = p_Img->bitdepth_chroma;
01938 if ((dec_picture->chroma_format_idc != YUV400) && !IS_INDEPENDENT(p_Img))
01939 {
01940 for (uv=1; uv<3; ++uv)
01941 {
01942 for(i=0;i<p_Img->mb_cr_size_y;++i)
01943 {
01944 for(j=0;j<p_Img->mb_cr_size_x;++j)
01945 {
01946 bits_read += GetBits(buf, bitoffset, &val, BitstreamLengthInBits, bitdepth);
01947 #if TRACE
01948 tracebits2("pcm_byte chroma", bitdepth, val);
01949 #endif
01950 currSlice->cof[uv][i][j] = val;
01951
01952 bitoffset += bitdepth;
01953 }
01954 }
01955 }
01956 }
01957
01958 (*dep->Dcodestrm_len) += ( bits_read >> 3);
01959 if (bits_read & 7)
01960 {
01961 ++(*dep->Dcodestrm_len);
01962 }
01963 }
01964