00001
00012 #include "global.h"
00013 #include "elements.h"
00014 #include "defines.h"
00015 #include "fmo.h"
00016 #include "vlc.h"
00017 #include "mbuffer.h"
00018 #include "header.h"
00019
00020 #include "ctx_tables.h"
00021
00022 extern StorablePicture *dec_picture;
00023
00024 #if TRACE
00025 #define SYMTRACESTRING(s) strncpy(sym.tracestring,s,TRACESTRING_SIZE)
00026 #else
00027 #define SYMTRACESTRING(s) // do nothing
00028 #endif
00029
00030 extern int UsedBits;
00031
00032 static void ref_pic_list_reordering(Slice *currSlice);
00033 static void pred_weight_table(Slice *currSlice);
00034
00035
00042 unsigned CeilLog2( unsigned uiVal)
00043 {
00044 unsigned uiTmp = uiVal-1;
00045 unsigned uiRet = 0;
00046
00047 while( uiTmp != 0 )
00048 {
00049 uiTmp >>= 1;
00050 uiRet++;
00051 }
00052 return uiRet;
00053 }
00054
00055 unsigned CeilLog2_sf( unsigned uiVal)
00056 {
00057 unsigned uiTmp = uiVal-1;
00058 unsigned uiRet = 0;
00059
00060 while( uiTmp > 0 )
00061 {
00062 uiTmp >>= 1;
00063 uiRet++;
00064 }
00065 return uiRet;
00066 }
00067
00076 int FirstPartOfSliceHeader(Slice *currSlice)
00077 {
00078 byte dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
00079 DataPartition *partition = &(currSlice->partArr[dP_nr]);
00080 Bitstream *currStream = partition->bitstream;
00081 int tmp;
00082
00083 UsedBits= partition->bitstream->frame_bitoffset;
00084
00085
00086 currSlice->start_mb_nr = ue_v ("SH: first_mb_in_slice", currStream);
00087
00088 tmp = ue_v ("SH: slice_type", currStream);
00089
00090 if (tmp > 4) tmp -= 5;
00091
00092 img->type = currSlice->picture_type = (SliceType) tmp;
00093
00094 currSlice->pic_parameter_set_id = ue_v ("SH: pic_parameter_set_id", currStream);
00095
00096 if( img->separate_colour_plane_flag )
00097 img->colour_plane_id = u_v (2, "SH: colour_plane_id", currStream);
00098 else
00099 img->colour_plane_id = PLANE_Y;
00100
00101 return UsedBits;
00102 }
00103
00112 int RestOfSliceHeader(Slice *currSlice)
00113 {
00114 byte dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
00115 DataPartition *partition = &(currSlice->partArr[dP_nr]);
00116 Bitstream *currStream = partition->bitstream;
00117
00118 int val, len;
00119
00120 img->frame_num = u_v (active_sps->log2_max_frame_num_minus4 + 4, "SH: frame_num", currStream);
00121
00122
00123 if (img->idr_flag)
00124 {
00125 img->pre_frame_num = img->frame_num;
00126
00127 img->last_ref_pic_poc = 0;
00128 assert(img->frame_num == 0);
00129 }
00130
00131 if (active_sps->frame_mbs_only_flag)
00132 {
00133 img->structure = FRAME;
00134 img->field_pic_flag=0;
00135 }
00136 else
00137 {
00138
00139 img->field_pic_flag = u_1("SH: field_pic_flag", currStream);
00140 if (img->field_pic_flag)
00141 {
00142
00143 img->bottom_field_flag = u_1("SH: bottom_field_flag", currStream);
00144 img->structure = img->bottom_field_flag ? BOTTOM_FIELD : TOP_FIELD;
00145 }
00146 else
00147 {
00148 img->structure = FRAME;
00149 img->bottom_field_flag = FALSE;
00150 }
00151 }
00152
00153 currSlice->structure = (PictureStructure) img->structure;
00154
00155 img->MbaffFrameFlag=(active_sps->mb_adaptive_frame_field_flag && (img->field_pic_flag==0));
00156
00157 if (img->structure == FRAME )
00158 assert (img->field_pic_flag == 0);
00159 if (img->structure == TOP_FIELD )
00160 assert (img->field_pic_flag == 1 && (img->bottom_field_flag == FALSE));
00161 if (img->structure == BOTTOM_FIELD)
00162 assert (img->field_pic_flag == 1 && (img->bottom_field_flag == TRUE ));
00163
00164 if (img->idr_flag)
00165 {
00166 img->idr_pic_id = ue_v("SH: idr_pic_id", currStream);
00167 }
00168
00169 if (active_sps->pic_order_cnt_type == 0)
00170 {
00171 img->pic_order_cnt_lsb = u_v(active_sps->log2_max_pic_order_cnt_lsb_minus4 + 4, "SH: pic_order_cnt_lsb", currStream);
00172 if( active_pps->pic_order_present_flag == 1 && !img->field_pic_flag )
00173 img->delta_pic_order_cnt_bottom = se_v("SH: delta_pic_order_cnt_bottom", currStream);
00174 else
00175 img->delta_pic_order_cnt_bottom = 0;
00176 }
00177 if( active_sps->pic_order_cnt_type == 1 && !active_sps->delta_pic_order_always_zero_flag )
00178 {
00179 img->delta_pic_order_cnt[ 0 ] = se_v("SH: delta_pic_order_cnt[0]", currStream);
00180 if( active_pps->pic_order_present_flag == 1 && !img->field_pic_flag )
00181 img->delta_pic_order_cnt[ 1 ] = se_v("SH: delta_pic_order_cnt[1]", currStream);
00182 }else
00183 {
00184 if (active_sps->pic_order_cnt_type == 1)
00185 {
00186 img->delta_pic_order_cnt[ 0 ] = 0;
00187 img->delta_pic_order_cnt[ 1 ] = 0;
00188 }
00189 }
00190
00192 if (active_pps->redundant_pic_cnt_present_flag)
00193 {
00194 img->redundant_pic_cnt = ue_v ("SH: redundant_pic_cnt", currStream);
00195 }
00196
00197 if(img->type==B_SLICE)
00198 {
00199 img->direct_spatial_mv_pred_flag = u_1 ("SH: direct_spatial_mv_pred_flag", currStream);
00200 }
00201
00202 img->num_ref_idx_l0_active = active_pps->num_ref_idx_l0_active_minus1 + 1;
00203 img->num_ref_idx_l1_active = active_pps->num_ref_idx_l1_active_minus1 + 1;
00204
00205 if(img->type==P_SLICE || img->type == SP_SLICE || img->type==B_SLICE)
00206 {
00207 val = u_1 ("SH: num_ref_idx_override_flag", currStream);
00208 if (val)
00209 {
00210 img->num_ref_idx_l0_active = 1 + ue_v ("SH: num_ref_idx_l0_active_minus1", currStream);
00211
00212 if(img->type==B_SLICE)
00213 {
00214 img->num_ref_idx_l1_active = 1 + ue_v ("SH: num_ref_idx_l1_active_minus1", currStream);
00215 }
00216 }
00217 }
00218 if (img->type!=B_SLICE)
00219 {
00220 img->num_ref_idx_l1_active = 0;
00221 }
00222
00223 ref_pic_list_reordering(currSlice);
00224
00225 img->apply_weights = ((active_pps->weighted_pred_flag && (currSlice->picture_type == P_SLICE || currSlice->picture_type == SP_SLICE) )
00226 || ((active_pps->weighted_bipred_idc > 0 ) && (currSlice->picture_type == B_SLICE)));
00227
00228 if ((active_pps->weighted_pred_flag&&(img->type==P_SLICE|| img->type == SP_SLICE))||
00229 (active_pps->weighted_bipred_idc==1 && (img->type==B_SLICE)))
00230 {
00231 pred_weight_table(currSlice);
00232 }
00233
00234 if (img->nal_reference_idc)
00235 dec_ref_pic_marking(currStream);
00236
00237 if (active_pps->entropy_coding_mode_flag && img->type!=I_SLICE && img->type!=SI_SLICE)
00238 {
00239 img->model_number = ue_v("SH: cabac_init_idc", currStream);
00240 }
00241 else
00242 {
00243 img->model_number = 0;
00244 }
00245
00246 currSlice->slice_qp_delta = val = se_v("SH: slice_qp_delta", currStream);
00247 currSlice->qp = img->qp = 26 + active_pps->pic_init_qp_minus26 + val;
00248
00249 if ((img->qp < -img->bitdepth_luma_qp_scale) || (img->qp > 51))
00250 error ("slice_qp_delta makes slice_qp_y out of range", 500);
00251
00252 if(img->type==SP_SLICE || img->type == SI_SLICE)
00253 {
00254 if(img->type==SP_SLICE)
00255 {
00256 img->sp_switch = u_1 ("SH: sp_for_switch_flag", currStream);
00257 }
00258 currSlice->slice_qs_delta = val = se_v("SH: slice_qs_delta", currStream);
00259 currSlice->qs = img->qpsp = 26 + active_pps->pic_init_qs_minus26 + val;
00260 if ((img->qpsp < 0) || (img->qpsp > 51))
00261 error ("slice_qs_delta makes slice_qs_y out of range", 500);
00262 }
00263
00264 if ( !HI_INTRA_ONLY_PROFILE || (HI_INTRA_ONLY_PROFILE && (params->intra_profile_deblocking == 1) ))
00265
00266 {
00267 if (active_pps->deblocking_filter_control_present_flag)
00268 {
00269 currSlice->DFDisableIdc = ue_v ("SH: disable_deblocking_filter_idc", currStream);
00270
00271 if (currSlice->DFDisableIdc!=1)
00272 {
00273 currSlice->DFAlphaC0Offset = 2 * se_v("SH: slice_alpha_c0_offset_div2", currStream);
00274 currSlice->DFBetaOffset = 2 * se_v("SH: slice_beta_offset_div2", currStream);
00275 }
00276 else
00277 {
00278 currSlice->DFAlphaC0Offset = currSlice->DFBetaOffset = 0;
00279 }
00280 }
00281 else
00282 {
00283 currSlice->DFDisableIdc = currSlice->DFAlphaC0Offset = currSlice->DFBetaOffset = 0;
00284 }
00285 }
00286 else
00287 {
00288
00289 if (active_pps->deblocking_filter_control_present_flag)
00290 {
00291 currSlice->DFDisableIdc = ue_v ("SH: disable_deblocking_filter_idc", currStream);
00292
00293 if (currSlice->DFDisableIdc!=1)
00294 {
00295 currSlice->DFAlphaC0Offset = 2 * se_v("SH: slice_alpha_c0_offset_div2", currStream);
00296 currSlice->DFBetaOffset = 2 * se_v("SH: slice_beta_offset_div2", currStream);
00297 }
00298 }
00299
00300 currSlice->DFDisableIdc =1;
00301 currSlice->DFAlphaC0Offset = currSlice->DFBetaOffset = 0;
00302 }
00303
00304
00305 if (active_pps->num_slice_groups_minus1>0 && active_pps->slice_group_map_type>=3 &&
00306 active_pps->slice_group_map_type<=5)
00307 {
00308 len = (active_sps->pic_height_in_map_units_minus1+1)*(active_sps->pic_width_in_mbs_minus1+1)/
00309 (active_pps->slice_group_change_rate_minus1+1);
00310 if (((active_sps->pic_height_in_map_units_minus1+1)*(active_sps->pic_width_in_mbs_minus1+1))%
00311 (active_pps->slice_group_change_rate_minus1+1))
00312 len +=1;
00313
00314 len = CeilLog2(len+1);
00315
00316 img->slice_group_change_cycle = u_v (len, "SH: slice_group_change_cycle", currStream);
00317 }
00318 img->PicHeightInMbs = img->FrameHeightInMbs / ( 1 + img->field_pic_flag );
00319 img->PicSizeInMbs = img->PicWidthInMbs * img->PicHeightInMbs;
00320 img->FrameSizeInMbs = img->PicWidthInMbs * img->FrameHeightInMbs;
00321
00322 return UsedBits;
00323 }
00324
00325
00332 static void ref_pic_list_reordering(Slice *currSlice)
00333 {
00334 byte dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
00335 DataPartition *partition = &(currSlice->partArr[dP_nr]);
00336 Bitstream *currStream = partition->bitstream;
00337 int i, val;
00338
00339 alloc_ref_pic_list_reordering_buffer(currSlice);
00340
00341 if (img->type!=I_SLICE && img->type!=SI_SLICE)
00342 {
00343 val = currSlice->ref_pic_list_reordering_flag_l0 = u_1 ("SH: ref_pic_list_reordering_flag_l0", currStream);
00344
00345 if (val)
00346 {
00347 i=0;
00348 do
00349 {
00350 val = currSlice->reordering_of_pic_nums_idc_l0[i] = ue_v("SH: reordering_of_pic_nums_idc_l0", currStream);
00351 if (val==0 || val==1)
00352 {
00353 currSlice->abs_diff_pic_num_minus1_l0[i] = ue_v("SH: abs_diff_pic_num_minus1_l0", currStream);
00354 }
00355 else
00356 {
00357 if (val==2)
00358 {
00359 currSlice->long_term_pic_idx_l0[i] = ue_v("SH: long_term_pic_idx_l0", currStream);
00360 }
00361 }
00362 i++;
00363
00364 } while (val != 3);
00365 }
00366 }
00367
00368 if (img->type==B_SLICE)
00369 {
00370 val = currSlice->ref_pic_list_reordering_flag_l1 = u_1 ("SH: ref_pic_list_reordering_flag_l1", currStream);
00371
00372 if (val)
00373 {
00374 i=0;
00375 do
00376 {
00377 val = currSlice->reordering_of_pic_nums_idc_l1[i] = ue_v("SH: reordering_of_pic_nums_idc_l1", currStream);
00378 if (val==0 || val==1)
00379 {
00380 currSlice->abs_diff_pic_num_minus1_l1[i] = ue_v("SH: abs_diff_pic_num_minus1_l1", currStream);
00381 }
00382 else
00383 {
00384 if (val==2)
00385 {
00386 currSlice->long_term_pic_idx_l1[i] = ue_v("SH: long_term_pic_idx_l1", currStream);
00387 }
00388 }
00389 i++;
00390
00391 } while (val != 3);
00392 }
00393 }
00394
00395
00396 if(img->redundant_pic_cnt && (img->type != I_SLICE) )
00397 {
00398 redundant_slice_ref_idx = currSlice->abs_diff_pic_num_minus1_l0[0] + 1;
00399 }
00400 }
00401
00402
00403 static void reset_wp_params(ImageParameters *img)
00404 {
00405 int i,comp;
00406 int log_weight_denom;
00407
00408 for (i=0; i<MAX_REFERENCE_PICTURES; i++)
00409 {
00410 for (comp=0; comp<3; comp++)
00411 {
00412 log_weight_denom = (comp == 0) ? img->luma_log2_weight_denom : img->chroma_log2_weight_denom;
00413 img->wp_weight[0][i][comp] = 1 << log_weight_denom;
00414 img->wp_weight[1][i][comp] = 1 << log_weight_denom;
00415 }
00416 }
00417 }
00418
00425 static void pred_weight_table(Slice *currSlice)
00426 {
00427 byte dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
00428 DataPartition *partition = &(currSlice->partArr[dP_nr]);
00429 Bitstream *currStream = partition->bitstream;
00430 int luma_weight_flag_l0, luma_weight_flag_l1, chroma_weight_flag_l0, chroma_weight_flag_l1;
00431 int i,j;
00432
00433 img->luma_log2_weight_denom = ue_v ("SH: luma_log2_weight_denom", currStream);
00434 img->wp_round_luma = img->luma_log2_weight_denom ? 1<<(img->luma_log2_weight_denom - 1): 0;
00435
00436 if ( 0 != active_sps->chroma_format_idc)
00437 {
00438 img->chroma_log2_weight_denom = ue_v ("SH: chroma_log2_weight_denom", currStream);
00439 img->wp_round_chroma = img->chroma_log2_weight_denom ? 1<<(img->chroma_log2_weight_denom - 1): 0;
00440 }
00441
00442 reset_wp_params(img);
00443
00444 for (i=0; i<img->num_ref_idx_l0_active; i++)
00445 {
00446 luma_weight_flag_l0 = u_1("SH: luma_weight_flag_l0", currStream);
00447
00448 if (luma_weight_flag_l0)
00449 {
00450 img->wp_weight[0][i][0] = se_v ("SH: luma_weight_l0", currStream);
00451 img->wp_offset[0][i][0] = se_v ("SH: luma_offset_l0", currStream);
00452 img->wp_offset[0][i][0] = img->wp_offset[0][i][0]<<(img->bitdepth_luma-8);
00453 }
00454 else
00455 {
00456 img->wp_weight[0][i][0] = 1<<img->luma_log2_weight_denom;
00457 img->wp_offset[0][i][0] = 0;
00458 }
00459
00460 if (active_sps->chroma_format_idc != 0)
00461 {
00462 chroma_weight_flag_l0 = u_1 ("SH: chroma_weight_flag_l0", currStream);
00463
00464 for (j=1; j<3; j++)
00465 {
00466 if (chroma_weight_flag_l0)
00467 {
00468 img->wp_weight[0][i][j] = se_v("SH: chroma_weight_l0", currStream);
00469 img->wp_offset[0][i][j] = se_v("SH: chroma_offset_l0", currStream);
00470 img->wp_offset[0][i][j] = img->wp_offset[0][i][j]<<(img->bitdepth_chroma-8);
00471 }
00472 else
00473 {
00474 img->wp_weight[0][i][j] = 1<<img->chroma_log2_weight_denom;
00475 img->wp_offset[0][i][j] = 0;
00476 }
00477 }
00478 }
00479 }
00480 if ((img->type == B_SLICE) && active_pps->weighted_bipred_idc == 1)
00481 {
00482 for (i=0; i<img->num_ref_idx_l1_active; i++)
00483 {
00484 luma_weight_flag_l1 = u_1("SH: luma_weight_flag_l1", currStream);
00485
00486 if (luma_weight_flag_l1)
00487 {
00488 img->wp_weight[1][i][0] = se_v ("SH: luma_weight_l1", currStream);
00489 img->wp_offset[1][i][0] = se_v ("SH: luma_offset_l1", currStream);
00490 img->wp_offset[1][i][0] = img->wp_offset[1][i][0]<<(img->bitdepth_luma-8);
00491 }
00492 else
00493 {
00494 img->wp_weight[1][i][0] = 1<<img->luma_log2_weight_denom;
00495 img->wp_offset[1][i][0] = 0;
00496 }
00497
00498 if (active_sps->chroma_format_idc != 0)
00499 {
00500 chroma_weight_flag_l1 = u_1 ("SH: chroma_weight_flag_l1", currStream);
00501
00502 for (j=1; j<3; j++)
00503 {
00504 if (chroma_weight_flag_l1)
00505 {
00506 img->wp_weight[1][i][j] = se_v("SH: chroma_weight_l1", currStream);
00507 img->wp_offset[1][i][j] = se_v("SH: chroma_offset_l1", currStream);
00508 img->wp_offset[1][i][j] = img->wp_offset[1][i][j]<<(img->bitdepth_chroma-8);
00509 }
00510 else
00511 {
00512 img->wp_weight[1][i][j] = 1<<img->chroma_log2_weight_denom;
00513 img->wp_offset[1][i][j] = 0;
00514 }
00515 }
00516 }
00517 }
00518 }
00519 }
00520
00521
00528 void dec_ref_pic_marking(Bitstream *currStream)
00529 {
00530 int val;
00531
00532 DecRefPicMarking_t *tmp_drpm,*tmp_drpm2;
00533
00534
00535 while (img->dec_ref_pic_marking_buffer)
00536 {
00537 tmp_drpm=img->dec_ref_pic_marking_buffer;
00538
00539 img->dec_ref_pic_marking_buffer=tmp_drpm->Next;
00540 free (tmp_drpm);
00541 }
00542
00543 if (img->idr_flag)
00544 {
00545 img->no_output_of_prior_pics_flag = u_1("SH: no_output_of_prior_pics_flag", currStream);
00546 img->long_term_reference_flag = u_1("SH: long_term_reference_flag", currStream);
00547 }
00548 else
00549 {
00550 img->adaptive_ref_pic_buffering_flag = u_1("SH: adaptive_ref_pic_buffering_flag", currStream);
00551 if (img->adaptive_ref_pic_buffering_flag)
00552 {
00553
00554 do
00555 {
00556 tmp_drpm=(DecRefPicMarking_t*)calloc (1,sizeof (DecRefPicMarking_t));
00557 tmp_drpm->Next=NULL;
00558
00559 val = tmp_drpm->memory_management_control_operation = ue_v("SH: memory_management_control_operation", currStream);
00560
00561 if ((val==1)||(val==3))
00562 {
00563 tmp_drpm->difference_of_pic_nums_minus1 = ue_v("SH: difference_of_pic_nums_minus1", currStream);
00564 }
00565 if (val==2)
00566 {
00567 tmp_drpm->long_term_pic_num = ue_v("SH: long_term_pic_num", currStream);
00568 }
00569
00570 if ((val==3)||(val==6))
00571 {
00572 tmp_drpm->long_term_frame_idx = ue_v("SH: long_term_frame_idx", currStream);
00573 }
00574 if (val==4)
00575 {
00576 tmp_drpm->max_long_term_frame_idx_plus1 = ue_v("SH: max_long_term_pic_idx_plus1", currStream);
00577 }
00578
00579
00580 if (img->dec_ref_pic_marking_buffer==NULL)
00581 {
00582 img->dec_ref_pic_marking_buffer=tmp_drpm;
00583 }
00584 else
00585 {
00586 tmp_drpm2=img->dec_ref_pic_marking_buffer;
00587 while (tmp_drpm2->Next!=NULL) tmp_drpm2=tmp_drpm2->Next;
00588 tmp_drpm2->Next=tmp_drpm;
00589 }
00590
00591 }
00592 while (val != 0);
00593 }
00594 }
00595 }
00596
00608 void decode_poc(ImageParameters *img)
00609 {
00610 int i;
00611
00612 unsigned int MaxPicOrderCntLsb = (1<<(active_sps->log2_max_pic_order_cnt_lsb_minus4+4));
00613
00614 switch ( active_sps->pic_order_cnt_type )
00615 {
00616 case 0:
00617
00618 if(img->idr_flag)
00619 {
00620 img->PrevPicOrderCntMsb = 0;
00621 img->PrevPicOrderCntLsb = 0;
00622 }
00623 else
00624 {
00625 if (img->last_has_mmco_5)
00626 {
00627 if (img->last_pic_bottom_field)
00628 {
00629 img->PrevPicOrderCntMsb = 0;
00630 img->PrevPicOrderCntLsb = 0;
00631 }
00632 else
00633 {
00634 img->PrevPicOrderCntMsb = 0;
00635 img->PrevPicOrderCntLsb = img->toppoc;
00636 }
00637 }
00638 }
00639
00640 if( img->pic_order_cnt_lsb < img->PrevPicOrderCntLsb &&
00641 ( img->PrevPicOrderCntLsb - img->pic_order_cnt_lsb ) >= ( MaxPicOrderCntLsb / 2 ) )
00642 img->PicOrderCntMsb = img->PrevPicOrderCntMsb + MaxPicOrderCntLsb;
00643 else if ( img->pic_order_cnt_lsb > img->PrevPicOrderCntLsb &&
00644 ( img->pic_order_cnt_lsb - img->PrevPicOrderCntLsb ) > ( MaxPicOrderCntLsb / 2 ) )
00645 img->PicOrderCntMsb = img->PrevPicOrderCntMsb - MaxPicOrderCntLsb;
00646 else
00647 img->PicOrderCntMsb = img->PrevPicOrderCntMsb;
00648
00649
00650
00651 if(img->field_pic_flag==0)
00652 {
00653 img->toppoc = img->PicOrderCntMsb + img->pic_order_cnt_lsb;
00654 img->bottompoc = img->toppoc + img->delta_pic_order_cnt_bottom;
00655 img->ThisPOC = img->framepoc = (img->toppoc < img->bottompoc)? img->toppoc : img->bottompoc;
00656 }
00657 else if (img->bottom_field_flag == FALSE)
00658 {
00659 img->ThisPOC= img->toppoc = img->PicOrderCntMsb + img->pic_order_cnt_lsb;
00660 }
00661 else
00662 {
00663 img->ThisPOC= img->bottompoc = img->PicOrderCntMsb + img->pic_order_cnt_lsb;
00664 }
00665 img->framepoc=img->ThisPOC;
00666
00667 if ( img->frame_num!=img->PreviousFrameNum)
00668 img->PreviousFrameNum=img->frame_num;
00669
00670 if(img->nal_reference_idc)
00671 {
00672 img->PrevPicOrderCntLsb = img->pic_order_cnt_lsb;
00673 img->PrevPicOrderCntMsb = img->PicOrderCntMsb;
00674 }
00675
00676 break;
00677
00678 case 1:
00679
00680 if(img->idr_flag)
00681 {
00682 img->FrameNumOffset=0;
00683 img->delta_pic_order_cnt[0]=0;
00684 if(img->frame_num)
00685 error("frame_num not equal to zero in IDR picture", -1020);
00686 }
00687 else
00688 {
00689 if (img->last_has_mmco_5)
00690 {
00691 img->PreviousFrameNumOffset = 0;
00692 img->PreviousFrameNum = 0;
00693 }
00694 if (img->frame_num<img->PreviousFrameNum)
00695 {
00696 img->FrameNumOffset = img->PreviousFrameNumOffset + img->MaxFrameNum;
00697 }
00698 else
00699 {
00700 img->FrameNumOffset = img->PreviousFrameNumOffset;
00701 }
00702 }
00703
00704
00705 if(active_sps->num_ref_frames_in_pic_order_cnt_cycle)
00706 img->AbsFrameNum = img->FrameNumOffset+img->frame_num;
00707 else
00708 img->AbsFrameNum=0;
00709 if( (!img->nal_reference_idc) && img->AbsFrameNum > 0)
00710 img->AbsFrameNum--;
00711
00712
00713 img->ExpectedDeltaPerPicOrderCntCycle=0;
00714
00715 if(active_sps->num_ref_frames_in_pic_order_cnt_cycle)
00716 for(i=0;i<(int) active_sps->num_ref_frames_in_pic_order_cnt_cycle;i++)
00717 img->ExpectedDeltaPerPicOrderCntCycle += active_sps->offset_for_ref_frame[i];
00718
00719 if(img->AbsFrameNum)
00720 {
00721 img->PicOrderCntCycleCnt = (img->AbsFrameNum-1)/active_sps->num_ref_frames_in_pic_order_cnt_cycle;
00722 img->FrameNumInPicOrderCntCycle = (img->AbsFrameNum-1)%active_sps->num_ref_frames_in_pic_order_cnt_cycle;
00723 img->ExpectedPicOrderCnt = img->PicOrderCntCycleCnt*img->ExpectedDeltaPerPicOrderCntCycle;
00724 for(i=0;i<=(int)img->FrameNumInPicOrderCntCycle;i++)
00725 img->ExpectedPicOrderCnt += active_sps->offset_for_ref_frame[i];
00726 }
00727 else
00728 img->ExpectedPicOrderCnt=0;
00729
00730 if(!img->nal_reference_idc)
00731 img->ExpectedPicOrderCnt += active_sps->offset_for_non_ref_pic;
00732
00733 if(img->field_pic_flag==0)
00734 {
00735 img->toppoc = img->ExpectedPicOrderCnt + img->delta_pic_order_cnt[0];
00736 img->bottompoc = img->toppoc + active_sps->offset_for_top_to_bottom_field + img->delta_pic_order_cnt[1];
00737 img->ThisPOC = img->framepoc = (img->toppoc < img->bottompoc)? img->toppoc : img->bottompoc;
00738 }
00739 else if (img->bottom_field_flag == FALSE)
00740 {
00741 img->ThisPOC = img->toppoc = img->ExpectedPicOrderCnt + img->delta_pic_order_cnt[0];
00742 }
00743 else
00744 {
00745 img->ThisPOC = img->bottompoc = img->ExpectedPicOrderCnt + active_sps->offset_for_top_to_bottom_field + img->delta_pic_order_cnt[0];
00746 }
00747 img->framepoc=img->ThisPOC;
00748
00749 img->PreviousFrameNum=img->frame_num;
00750 img->PreviousFrameNumOffset=img->FrameNumOffset;
00751
00752 break;
00753
00754
00755 case 2:
00756 if(img->idr_flag)
00757 {
00758 img->FrameNumOffset=0;
00759 img->ThisPOC = img->framepoc = img->toppoc = img->bottompoc = 0;
00760 if(img->frame_num)
00761 error("frame_num not equal to zero in IDR picture", -1020);
00762 }
00763 else
00764 {
00765 if (img->last_has_mmco_5)
00766 {
00767 img->PreviousFrameNum = 0;
00768 img->PreviousFrameNumOffset = 0;
00769 }
00770 if (img->frame_num<img->PreviousFrameNum)
00771 img->FrameNumOffset = img->PreviousFrameNumOffset + img->MaxFrameNum;
00772 else
00773 img->FrameNumOffset = img->PreviousFrameNumOffset;
00774
00775
00776 img->AbsFrameNum = img->FrameNumOffset+img->frame_num;
00777 if(!img->nal_reference_idc)
00778 img->ThisPOC = (2*img->AbsFrameNum - 1);
00779 else
00780 img->ThisPOC = (2*img->AbsFrameNum);
00781
00782 if (img->field_pic_flag==0)
00783 img->toppoc = img->bottompoc = img->framepoc = img->ThisPOC;
00784 else if (img->bottom_field_flag == FALSE)
00785 img->toppoc = img->framepoc = img->ThisPOC;
00786 else img->bottompoc = img->framepoc = img->ThisPOC;
00787 }
00788
00789 img->PreviousFrameNum=img->frame_num;
00790 img->PreviousFrameNumOffset=img->FrameNumOffset;
00791 break;
00792
00793
00794 default:
00795
00796 assert( 1==0 );
00797 break;
00798 }
00799 }
00800
00809 int dumppoc(ImageParameters *img)
00810 {
00811 printf ("\nPOC locals...\n");
00812 printf ("toppoc %d\n", (int) img->toppoc);
00813 printf ("bottompoc %d\n", (int) img->bottompoc);
00814 printf ("frame_num %d\n", (int) img->frame_num);
00815 printf ("field_pic_flag %d\n", (int) img->field_pic_flag);
00816 printf ("bottom_field_flag %d\n", (int) img->bottom_field_flag);
00817 printf ("POC SPS\n");
00818 printf ("log2_max_frame_num_minus4 %d\n", (int) active_sps->log2_max_frame_num_minus4);
00819 printf ("log2_max_pic_order_cnt_lsb_minus4 %d\n", (int) active_sps->log2_max_pic_order_cnt_lsb_minus4);
00820 printf ("pic_order_cnt_type %d\n", (int) active_sps->pic_order_cnt_type);
00821 printf ("num_ref_frames_in_pic_order_cnt_cycle %d\n", (int) active_sps->num_ref_frames_in_pic_order_cnt_cycle);
00822 printf ("delta_pic_order_always_zero_flag %d\n", (int) active_sps->delta_pic_order_always_zero_flag);
00823 printf ("offset_for_non_ref_pic %d\n", (int) active_sps->offset_for_non_ref_pic);
00824 printf ("offset_for_top_to_bottom_field %d\n", (int) active_sps->offset_for_top_to_bottom_field);
00825 printf ("offset_for_ref_frame[0] %d\n", (int) active_sps->offset_for_ref_frame[0]);
00826 printf ("offset_for_ref_frame[1] %d\n", (int) active_sps->offset_for_ref_frame[1]);
00827 printf ("POC in SLice Header\n");
00828 printf ("pic_order_present_flag %d\n", (int) active_pps->pic_order_present_flag);
00829 printf ("delta_pic_order_cnt[0] %d\n", (int) img->delta_pic_order_cnt[0]);
00830 printf ("delta_pic_order_cnt[1] %d\n", (int) img->delta_pic_order_cnt[1]);
00831 printf ("delta_pic_order_cnt[2] %d\n", (int) img->delta_pic_order_cnt[2]);
00832 printf ("idr_flag %d\n", (int) img->idr_flag);
00833 printf ("MaxFrameNum %d\n", (int) img->MaxFrameNum);
00834
00835 return 0;
00836 }
00837
00845 int picture_order(ImageParameters *img)
00846 {
00847 if (img->field_pic_flag==0)
00848 return img->framepoc;
00849 else if (img->bottom_field_flag == FALSE)
00850 return img->toppoc;
00851 else
00852 return img->bottompoc;
00853 }
00854