00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <math.h>
00017
00018 #include "global.h"
00019
00020 #include "elements.h"
00021 #include "header.h"
00022 #include "rtp.h"
00023 #include "mbuffer.h"
00024 #include "vlc.h"
00025 #include "parset.h"
00026
00027
00028 #if TRACE
00029 #define SYMTRACESTRING(s) strncpy(sym.tracestring,s,TRACESTRING_SIZE)
00030 #else
00031 #define SYMTRACESTRING(s) // do nothing
00032 #endif
00033
00034 const int * assignSE2partition[2] ;
00035 const int assignSE2partition_NoDP[SE_MAX_ELEMENTS] =
00036 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00037 const int assignSE2partition_DP[SE_MAX_ELEMENTS] =
00038
00039 { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0 } ;
00040
00041 static int ref_pic_list_reordering(Slice *currSlice, Bitstream *bitstream);
00042 static int pred_weight_table (Slice *currSlice, Bitstream *bitstream);
00043 static int get_picture_type (Slice *currSlice);
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 int SliceHeader(Slice* currSlice)
00054 {
00055 ImageParameters *p_Img = currSlice->p_Img;
00056
00057 seq_parameter_set_rbsp_t *active_sps = currSlice->active_sps;
00058 pic_parameter_set_rbsp_t *active_pps = currSlice->active_pps;
00059
00060 int dP_nr = assignSE2partition[currSlice->partition_mode][SE_HEADER];
00061 Bitstream *bitstream = currSlice->partArr[dP_nr].bitstream;
00062 int len = 0;
00063 unsigned int field_pic_flag = 0;
00064 byte bottom_field_flag = 0;
00065
00066 int num_bits_slice_group_change_cycle;
00067 float numtmp;
00068
00069 if (currSlice->MbaffFrameFlag)
00070 len = ue_v("SH: first_mb_in_slice", p_Img->current_mb_nr >> 1, bitstream);
00071 else
00072 len = ue_v("SH: first_mb_in_slice", p_Img->current_mb_nr, bitstream);
00073
00074 len += ue_v("SH: slice_type", get_picture_type (currSlice), bitstream);
00075
00076 len += ue_v("SH: pic_parameter_set_id" , active_pps->pic_parameter_set_id ,bitstream);
00077
00078 if( active_sps->separate_colour_plane_flag == 1 )
00079 len += u_v( 2, "SH: colour_plane_id", p_Img->colour_plane_id, bitstream );
00080
00081 len += u_v (p_Img->log2_max_frame_num_minus4 + 4,"SH: frame_num", p_Img->frame_num, bitstream);
00082
00083 if (!currSlice->active_sps->frame_mbs_only_flag)
00084 {
00085
00086 field_pic_flag = (currSlice->structure == TOP_FIELD || currSlice->structure ==BOTTOM_FIELD)?1:0;
00087 assert( field_pic_flag == p_Img->fld_flag );
00088 len += u_1("SH: field_pic_flag", field_pic_flag, bitstream);
00089
00090 if (field_pic_flag)
00091 {
00092
00093 bottom_field_flag = (byte) (currSlice->structure == BOTTOM_FIELD);
00094 len += u_1("SH: bottom_field_flag" , bottom_field_flag ,bitstream);
00095 }
00096 }
00097
00098 if (currSlice->idr_flag)
00099 {
00100
00101 len += ue_v ("SH: idr_pic_id", (p_Img->number & 0x01), bitstream);
00102 }
00103
00104 if (p_Img->pic_order_cnt_type == 0)
00105 {
00106 if (active_sps->frame_mbs_only_flag)
00107 {
00108 p_Img->pic_order_cnt_lsb = (p_Img->toppoc & ~((((unsigned int)(-1)) << (p_Img->log2_max_pic_order_cnt_lsb_minus4+4))) );
00109 }
00110 else
00111 {
00112 if (!field_pic_flag || currSlice->structure == TOP_FIELD)
00113 p_Img->pic_order_cnt_lsb = (p_Img->toppoc & ~((((unsigned int)(-1)) << (p_Img->log2_max_pic_order_cnt_lsb_minus4+4))) );
00114 else if ( currSlice->structure == BOTTOM_FIELD )
00115 p_Img->pic_order_cnt_lsb = (p_Img->bottompoc & ~((((unsigned int)(-1)) << (p_Img->log2_max_pic_order_cnt_lsb_minus4+4))) );
00116 }
00117
00118 len += u_v (p_Img->log2_max_pic_order_cnt_lsb_minus4+4, "SH: pic_order_cnt_lsb", p_Img->pic_order_cnt_lsb, bitstream);
00119
00120 if (p_Img->bottom_field_pic_order_in_frame_present_flag && !field_pic_flag)
00121 {
00122 len += se_v ("SH: delta_pic_order_cnt_bottom", p_Img->delta_pic_order_cnt_bottom, bitstream);
00123 }
00124 }
00125 if (p_Img->pic_order_cnt_type == 1 && !p_Img->delta_pic_order_always_zero_flag)
00126 {
00127 len += se_v ("SH: delta_pic_order_cnt[0]", p_Img->delta_pic_order_cnt[0], bitstream);
00128
00129 if (p_Img->bottom_field_pic_order_in_frame_present_flag && !field_pic_flag)
00130 {
00131 len += se_v ("SH: delta_pic_order_cnt[1]", p_Img->delta_pic_order_cnt[1], bitstream);
00132 }
00133 }
00134
00135 if (active_pps->redundant_pic_cnt_present_flag)
00136 {
00137 len += ue_v ("SH: redundant_pic_cnt", p_Img->redundant_pic_cnt, bitstream);
00138 }
00139
00140
00141 if (currSlice->slice_type == B_SLICE)
00142 {
00143 len += u_1 ("SH: direct_spatial_mv_pred_flag", p_Img->direct_spatial_mv_pred_flag, bitstream);
00144 }
00145
00146 if ((currSlice->slice_type == P_SLICE) || (currSlice->slice_type == B_SLICE) || (currSlice->slice_type==SP_SLICE))
00147 {
00148 int override_flag;
00149 if ((currSlice->slice_type == P_SLICE) || (currSlice->slice_type == SP_SLICE))
00150 {
00151 override_flag = (currSlice->num_ref_idx_active[LIST_0] != (active_pps->num_ref_idx_l0_active_minus1 +1)) ? 1 : 0;
00152 }
00153 else
00154 {
00155 override_flag = ((currSlice->num_ref_idx_active[LIST_0] != (active_pps->num_ref_idx_l0_active_minus1 +1))
00156 || (currSlice->num_ref_idx_active[LIST_1] != (active_pps->num_ref_idx_l1_active_minus1 +1))) ? 1 : 0;
00157 }
00158
00159 len += u_1 ("SH: num_ref_idx_active_override_flag", override_flag, bitstream);
00160
00161 if (override_flag)
00162 {
00163 len += ue_v ("SH: num_ref_idx_l0_active_minus1", currSlice->num_ref_idx_active[LIST_0]-1, bitstream);
00164 if (currSlice->slice_type == B_SLICE)
00165 {
00166 len += ue_v ("SH: num_ref_idx_l1_active_minus1", currSlice->num_ref_idx_active[LIST_1]-1, bitstream);
00167 }
00168 }
00169
00170 }
00171 len += ref_pic_list_reordering(currSlice, bitstream);
00172
00173 if (((currSlice->slice_type == P_SLICE || currSlice->slice_type == SP_SLICE) && active_pps->weighted_pred_flag) ||
00174 ((currSlice->slice_type == B_SLICE) && currSlice->weighted_prediction == 1))
00175 {
00176 len += pred_weight_table(currSlice, bitstream);
00177 }
00178
00179 if (p_Img->nal_reference_idc)
00180 {
00181 p_Img->adaptive_ref_pic_buffering_flag = (p_Img->dec_ref_pic_marking_buffer != NULL);
00182 len += dec_ref_pic_marking(bitstream,
00183 p_Img->dec_ref_pic_marking_buffer,
00184 currSlice->idr_flag,
00185 p_Img->no_output_of_prior_pics_flag,
00186 p_Img->long_term_reference_flag );
00187 }
00188
00189 if(currSlice->symbol_mode==CABAC && currSlice->slice_type!=I_SLICE )
00190 {
00191 len += ue_v("SH: cabac_init_idc", currSlice->model_number, bitstream);
00192 }
00193
00194 len += se_v("SH: slice_qp_delta", (currSlice->qp - 26 - active_pps->pic_init_qp_minus26), bitstream);
00195
00196 if (currSlice->slice_type == SP_SLICE )
00197 {
00198 if (currSlice->slice_type == SP_SLICE)
00199 {
00200 len += u_1 ("SH: sp_for_switch_flag", (currSlice->si_frame_indicator || currSlice->sp2_frame_indicator), bitstream);
00201 }
00202 len += se_v ("SH: slice_qs_delta", (p_Img->qpsp - 26), bitstream );
00203 }
00204
00205 if (active_pps->deblocking_filter_control_present_flag)
00206 {
00207 len += ue_v("SH: disable_deblocking_filter_idc",currSlice->DFDisableIdc, bitstream);
00208
00209 if (currSlice->DFDisableIdc!=1)
00210 {
00211 len += se_v ("SH: slice_alpha_c0_offset_div2", currSlice->DFAlphaC0Offset / 2, bitstream);
00212 len += se_v ("SH: slice_beta_offset_div2 ", currSlice->DFBetaOffset / 2, bitstream);
00213 }
00214 }
00215
00216 if ( active_pps->num_slice_groups_minus1 > 0 &&
00217 active_pps->slice_group_map_type >= 3 && active_pps->slice_group_map_type <= 5)
00218 {
00219 numtmp=p_Img->PicHeightInMapUnits*p_Img->PicWidthInMbs/(float)(active_pps->slice_group_change_rate_minus1+1)+1;
00220 num_bits_slice_group_change_cycle = (int)ceil(log(numtmp)/log(2));
00221
00222
00223 len += u_v (num_bits_slice_group_change_cycle, "SH: slice_group_change_cycle", p_Img->slice_group_change_cycle, bitstream);
00224 }
00225
00226
00227
00228
00229 if(currSlice->partition_mode && !currSlice->idr_flag)
00230 {
00231 len += ue_v("DPA: slice_id", currSlice->slice_nr, bitstream);
00232 }
00233
00234 return len;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 static int ref_pic_list_reordering(Slice *currSlice, Bitstream *bitstream)
00248 {
00249
00250 int i, len=0;
00251 if ((currSlice->slice_type != I_SLICE) )
00252 {
00253 len += u_1 ("SH: ref_pic_list_reordering_flag_l0", currSlice->ref_pic_list_reordering_flag[LIST_0], bitstream);
00254 if (currSlice->ref_pic_list_reordering_flag[LIST_0])
00255 {
00256 i=-1;
00257 do
00258 {
00259 i++;
00260 len += ue_v ("SH: reordering_of_pic_nums_idc", currSlice->reordering_of_pic_nums_idc[LIST_0][i], bitstream);
00261 if (currSlice->reordering_of_pic_nums_idc[LIST_0][i]==0 ||
00262 currSlice->reordering_of_pic_nums_idc[LIST_0][i]==1)
00263 {
00264 len += ue_v ("SH: abs_diff_pic_num_minus1_l0", currSlice->abs_diff_pic_num_minus1[LIST_0][i], bitstream);
00265 }
00266 else
00267 {
00268 if (currSlice->reordering_of_pic_nums_idc[LIST_0][i]==2)
00269 {
00270 len += ue_v ("SH: long_term_pic_idx_l0", currSlice->long_term_pic_idx[LIST_0][i], bitstream);
00271 }
00272 }
00273
00274 } while (currSlice->reordering_of_pic_nums_idc[LIST_0][i] != 3);
00275 }
00276 }
00277
00278 if (currSlice->slice_type == B_SLICE)
00279 {
00280 len += u_1 ("SH: ref_pic_list_reordering_flag_l1", currSlice->ref_pic_list_reordering_flag[LIST_1], bitstream);
00281 if (currSlice->ref_pic_list_reordering_flag[LIST_1])
00282 {
00283 i=-1;
00284 do
00285 {
00286 i++;
00287 len += ue_v ("SH: remapping_of_pic_num_idc", currSlice->reordering_of_pic_nums_idc[LIST_1][i], bitstream);
00288 if (currSlice->reordering_of_pic_nums_idc[LIST_1][i]==0 ||
00289 currSlice->reordering_of_pic_nums_idc[LIST_1][i]==1)
00290 {
00291 len += ue_v ("SH: abs_diff_pic_num_minus1_l1", currSlice->abs_diff_pic_num_minus1[LIST_1][i], bitstream);
00292 }
00293 else
00294 {
00295 if (currSlice->reordering_of_pic_nums_idc[LIST_1][i]==2)
00296 {
00297 len += ue_v ("SH: long_term_pic_idx_l1", currSlice->long_term_pic_idx[LIST_1][i], bitstream);
00298 }
00299 }
00300 } while (currSlice->reordering_of_pic_nums_idc[LIST_1][i] != 3);
00301 }
00302 }
00303
00304 return len;
00305 }
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 int dec_ref_pic_marking(Bitstream *bitstream, DecRefPicMarking_t *p_drpm, int idr_flag, int no_output_of_prior_pics_flag, int long_term_reference_flag )
00318 {
00319 DecRefPicMarking_t *tmp_drpm;
00320
00321 int val, len=0;
00322 int adaptive_ref_pic_buffering_flag;
00323
00324 if (idr_flag)
00325 {
00326 len += u_1("SH: no_output_of_prior_pics_flag", no_output_of_prior_pics_flag, bitstream);
00327 len += u_1("SH: long_term_reference_flag", long_term_reference_flag, bitstream);
00328 }
00329 else
00330 {
00331 adaptive_ref_pic_buffering_flag = (p_drpm != NULL);
00332
00333 len += u_1("SH: adaptive_ref_pic_buffering_flag", adaptive_ref_pic_buffering_flag, bitstream);
00334
00335 if (adaptive_ref_pic_buffering_flag)
00336 {
00337 tmp_drpm = p_drpm;
00338
00339 do
00340 {
00341 if (tmp_drpm==NULL) error ("Error encoding MMCO commands", 500);
00342
00343 val = tmp_drpm->memory_management_control_operation;
00344 len += ue_v("SH: memory_management_control_operation", val, bitstream);
00345
00346 if ((val==1)||(val==3))
00347 {
00348 len += 1 + ue_v("SH: difference_of_pic_nums_minus1", tmp_drpm->difference_of_pic_nums_minus1, bitstream);
00349 }
00350 if (val==2)
00351 {
00352 len+= ue_v("SH: long_term_pic_num", tmp_drpm->long_term_pic_num, bitstream);
00353 }
00354 if ((val==3)||(val==6))
00355 {
00356 len+= ue_v("SH: long_term_frame_idx", tmp_drpm->long_term_frame_idx, bitstream);
00357 }
00358 if (val==4)
00359 {
00360 len += ue_v("SH: max_long_term_pic_idx_plus1", tmp_drpm->max_long_term_frame_idx_plus1, bitstream);
00361 }
00362
00363 tmp_drpm=tmp_drpm->Next;
00364
00365 } while (val != 0);
00366
00367 }
00368 }
00369 return len;
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 static int pred_weight_table(Slice *currSlice, Bitstream *bitstream)
00383 {
00384 int len = 0;
00385 int i,j;
00386
00387 len += ue_v("SH: luma_log_weight_denom", currSlice->luma_log_weight_denom, bitstream);
00388
00389 if ( 0 != currSlice->active_sps->chroma_format_idc)
00390 {
00391 len += ue_v("SH: chroma_log_weight_denom", currSlice->chroma_log_weight_denom, bitstream);
00392 }
00393
00394 for (i=0; i< currSlice->num_ref_idx_active[LIST_0]; i++)
00395 {
00396 if ( (currSlice->wp_weight[0][i][0] != 1<<currSlice->luma_log_weight_denom) || (currSlice->wp_offset[0][i][0] != 0) )
00397 {
00398 len += u_1 ("SH: luma_weight_flag_l0", 1, bitstream);
00399 len += se_v ("SH: luma_weight_l0", currSlice->wp_weight[0][i][0], bitstream);
00400 len += se_v ("SH: luma_offset_l0", (currSlice->wp_offset[0][i][0]>>(currSlice->bitdepth_luma - 8)), bitstream);
00401 }
00402 else
00403 {
00404 len += u_1 ("SH: luma_weight_flag_l0", 0, bitstream);
00405 }
00406
00407 if (currSlice->active_sps->chroma_format_idc!=0)
00408 {
00409 if ( (currSlice->wp_weight[0][i][1] != 1<<currSlice->chroma_log_weight_denom) || (currSlice->wp_offset[0][i][1] != 0) ||
00410 (currSlice->wp_weight[0][i][2] != 1<<currSlice->chroma_log_weight_denom) || (currSlice->wp_offset[0][i][2] != 0) )
00411 {
00412 len += u_1 ("chroma_weight_flag_l0", 1, bitstream);
00413 for (j=1; j<3; j++)
00414 {
00415 len += se_v ("chroma_weight_l0", currSlice->wp_weight[0][i][j] ,bitstream);
00416 len += se_v ("chroma_offset_l0", (currSlice->wp_offset[0][i][j]>>(currSlice->bitdepth_chroma-8)) ,bitstream);
00417 }
00418 }
00419 else
00420 {
00421 len += u_1 ("chroma_weight_flag_l0", 0, bitstream);
00422 }
00423 }
00424 }
00425
00426 if (currSlice->slice_type == B_SLICE)
00427 {
00428 for (i=0; i< currSlice->num_ref_idx_active[LIST_1]; i++)
00429 {
00430 if ( (currSlice->wp_weight[1][i][0] != 1<<currSlice->luma_log_weight_denom) || (currSlice->wp_offset[1][i][0] != 0) )
00431 {
00432 len += u_1 ("SH: luma_weight_flag_l1", 1, bitstream);
00433 len += se_v ("SH: luma_weight_l1", currSlice->wp_weight[1][i][0], bitstream);
00434 len += se_v ("SH: luma_offset_l1", (currSlice->wp_offset[1][i][0]>>(currSlice->bitdepth_luma-8)), bitstream);
00435 }
00436 else
00437 {
00438 len += u_1 ("SH: luma_weight_flag_l1", 0, bitstream);
00439 }
00440
00441 if (currSlice->active_sps->chroma_format_idc!=0)
00442 {
00443 if ( (currSlice->wp_weight[1][i][1] != 1<<currSlice->chroma_log_weight_denom) || (currSlice->wp_offset[1][i][1] != 0) ||
00444 (currSlice->wp_weight[1][i][2] != 1<<currSlice->chroma_log_weight_denom) || (currSlice->wp_offset[1][i][2] != 0) )
00445 {
00446 len += u_1 ("chroma_weight_flag_l1", 1, bitstream);
00447 for (j=1; j<3; j++)
00448 {
00449 len += se_v ("chroma_weight_l1", currSlice->wp_weight[1][i][j] ,bitstream);
00450 len += se_v ("chroma_offset_l1", (currSlice->wp_offset[1][i][j]>>(currSlice->bitdepth_chroma-8)) ,bitstream);
00451 }
00452 }
00453 else
00454 {
00455 len += u_1 ("chroma_weight_flag_l1", 0, bitstream);
00456 }
00457 }
00458 }
00459 }
00460 return len;
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 static int get_picture_type(Slice *currSlice)
00474 {
00475
00476
00477 int same_slicetype_for_whole_frame = 5;
00478
00479 switch (currSlice->slice_type)
00480 {
00481 case I_SLICE:
00482 return 2 + same_slicetype_for_whole_frame;
00483 break;
00484 case P_SLICE:
00485 return 0 + same_slicetype_for_whole_frame;
00486 break;
00487 case B_SLICE:
00488 return 1 + same_slicetype_for_whole_frame;
00489 break;
00490 case SP_SLICE:
00491 return 3 + same_slicetype_for_whole_frame;
00492 break;
00493 default:
00494 error("Picture Type not supported!",1);
00495 break;
00496 }
00497
00498 return 0;
00499 }
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530 int Partition_BC_Header(Slice *currSlice, int PartNo)
00531 {
00532 DataPartition *partition = &((currSlice)->partArr[PartNo]);
00533 SyntaxElement sym;
00534
00535 assert (PartNo > 0 && PartNo < currSlice->max_part_nr);
00536
00537 sym.type = SE_HEADER;
00538 sym.value2 = 0;
00539
00540 SYMTRACESTRING("RTP-PH: Slice ID");
00541 sym.value1 = currSlice->slice_nr;
00542 writeSE_UVLC (&sym, partition);
00543
00544 return sym.len;
00545 }
00546