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