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