00001
00015 #include "global.h"
00016 #include "image.h"
00017 #include "parsetcommon.h"
00018 #include "parset.h"
00019 #include "nalu.h"
00020 #include "memalloc.h"
00021 #include "fmo.h"
00022 #include "cabac.h"
00023 #include "vlc.h"
00024 #include "mbuffer.h"
00025 #include "erc_api.h"
00026
00027 #if TRACE
00028 #define SYMTRACESTRING(s) strncpy(sym->tracestring,s,TRACESTRING_SIZE)
00029 #else
00030 #define SYMTRACESTRING(s) // do nothing
00031 #endif
00032
00033 const byte ZZ_SCAN[16] =
00034 { 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
00035 };
00036
00037 const byte ZZ_SCAN8[64] =
00038 { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
00039 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
00040 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
00041 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
00042 };
00043
00044 extern int UsedBits;
00045 extern ColocatedParams *Co_located;
00046 extern ColocatedParams *Co_located_JV[MAX_PLANE];
00047
00048 extern int quant_intra_default[16];
00049 extern int quant_inter_default[16];
00050 extern int quant8_intra_default[64];
00051 extern int quant8_inter_default[64];
00052
00053 seq_parameter_set_rbsp_t SeqParSet[MAXSPS];
00054 pic_parameter_set_rbsp_t PicParSet[MAXPPS];
00055
00056 extern StorablePicture* dec_picture;
00057
00058 extern void init_frext(ImageParameters *img);
00059
00060
00061 void Scaling_List(int *scalingList, int sizeOfScalingList, Boolean *UseDefaultScalingMatrix, Bitstream *s)
00062 {
00063 int j, scanj;
00064 int delta_scale, lastScale, nextScale;
00065
00066 lastScale = 8;
00067 nextScale = 8;
00068
00069 for(j=0; j<sizeOfScalingList; j++)
00070 {
00071 scanj = (sizeOfScalingList==16) ? ZZ_SCAN[j]:ZZ_SCAN8[j];
00072
00073 if(nextScale!=0)
00074 {
00075 delta_scale = se_v ( " : delta_sl " , s);
00076 nextScale = (lastScale + delta_scale + 256) % 256;
00077 *UseDefaultScalingMatrix = (Boolean) (scanj==0 && nextScale==0);
00078 }
00079
00080 scalingList[scanj] = (nextScale==0) ? lastScale:nextScale;
00081 lastScale = scalingList[scanj];
00082 }
00083 }
00084
00085
00086 int InterpretSPS (DataPartition *p, seq_parameter_set_rbsp_t *sps)
00087 {
00088 unsigned i;
00089 unsigned n_ScalingList;
00090 int reserved_zero;
00091 Bitstream *s = p->bitstream;
00092
00093 assert (p != NULL);
00094 assert (p->bitstream != NULL);
00095 assert (p->bitstream->streamBuffer != 0);
00096 assert (sps != NULL);
00097
00098 UsedBits = 0;
00099
00100 sps->profile_idc = u_v (8, "SPS: profile_idc" , s);
00101
00102 if ((sps->profile_idc!=BASELINE ) &&
00103 (sps->profile_idc!=MAIN ) &&
00104 (sps->profile_idc!=EXTENDED ) &&
00105 (sps->profile_idc!=FREXT_HP ) &&
00106 (sps->profile_idc!=FREXT_Hi10P ) &&
00107 (sps->profile_idc!=FREXT_Hi422 ) &&
00108 (sps->profile_idc!=FREXT_Hi444 ) &&
00109 (sps->profile_idc!=FREXT_CAVLC444 ))
00110 {
00111 return UsedBits;
00112 }
00113
00114 sps->constrained_set0_flag = u_1 ( "SPS: constrained_set0_flag" , s);
00115 sps->constrained_set1_flag = u_1 ( "SPS: constrained_set1_flag" , s);
00116 sps->constrained_set2_flag = u_1 ( "SPS: constrained_set2_flag" , s);
00117 sps->constrained_set3_flag = u_1 ( "SPS: constrained_set3_flag" , s);
00118 reserved_zero = u_v (4, "SPS: reserved_zero_4bits" , s);
00119 assert (reserved_zero==0);
00120
00121 sps->level_idc = u_v (8, "SPS: level_idc" , s);
00122
00123 sps->seq_parameter_set_id = ue_v ("SPS: seq_parameter_set_id" , s);
00124
00125
00126 sps->chroma_format_idc = 1;
00127 sps->bit_depth_luma_minus8 = 0;
00128 sps->bit_depth_chroma_minus8 = 0;
00129 img->lossless_qpprime_flag = 0;
00130 sps->separate_colour_plane_flag = 0;
00131
00132 if((sps->profile_idc==FREXT_HP ) ||
00133 (sps->profile_idc==FREXT_Hi10P) ||
00134 (sps->profile_idc==FREXT_Hi422) ||
00135 (sps->profile_idc==FREXT_Hi444) ||
00136 (sps->profile_idc==FREXT_CAVLC444))
00137 {
00138 sps->chroma_format_idc = ue_v ("SPS: chroma_format_idc" , s);
00139
00140 if(sps->chroma_format_idc == YUV444)
00141 {
00142 sps->separate_colour_plane_flag = u_1 ("SPS: separate_colour_plane_flag" , s);
00143 }
00144
00145 sps->bit_depth_luma_minus8 = ue_v ("SPS: bit_depth_luma_minus8" , s);
00146 sps->bit_depth_chroma_minus8 = ue_v ("SPS: bit_depth_chroma_minus8" , s);
00147 img->lossless_qpprime_flag = u_1 ("SPS: lossless_qpprime_y_zero_flag" , s);
00148
00149 sps->seq_scaling_matrix_present_flag = u_1 ( "SPS: seq_scaling_matrix_present_flag" , s);
00150
00151 if(sps->seq_scaling_matrix_present_flag)
00152 {
00153 n_ScalingList = (sps->chroma_format_idc != YUV444) ? 8 : 12;
00154 for(i=0; i<n_ScalingList; i++)
00155 {
00156 sps->seq_scaling_list_present_flag[i] = u_1 ( "SPS: seq_scaling_list_present_flag" , s);
00157 if(sps->seq_scaling_list_present_flag[i])
00158 {
00159 if(i<6)
00160 Scaling_List(sps->ScalingList4x4[i], 16, &sps->UseDefaultScalingMatrix4x4Flag[i], s);
00161 else
00162 Scaling_List(sps->ScalingList8x8[i-6], 64, &sps->UseDefaultScalingMatrix8x8Flag[i-6], s);
00163 }
00164 }
00165 }
00166 }
00167
00168 sps->log2_max_frame_num_minus4 = ue_v ("SPS: log2_max_frame_num_minus4" , s);
00169 sps->pic_order_cnt_type = ue_v ("SPS: pic_order_cnt_type" , s);
00170
00171 if (sps->pic_order_cnt_type == 0)
00172 sps->log2_max_pic_order_cnt_lsb_minus4 = ue_v ("SPS: log2_max_pic_order_cnt_lsb_minus4" , s);
00173 else if (sps->pic_order_cnt_type == 1)
00174 {
00175 sps->delta_pic_order_always_zero_flag = u_1 ("SPS: delta_pic_order_always_zero_flag" , s);
00176 sps->offset_for_non_ref_pic = se_v ("SPS: offset_for_non_ref_pic" , s);
00177 sps->offset_for_top_to_bottom_field = se_v ("SPS: offset_for_top_to_bottom_field" , s);
00178 sps->num_ref_frames_in_pic_order_cnt_cycle = ue_v ("SPS: num_ref_frames_in_pic_order_cnt_cycle" , s);
00179 for(i=0; i<sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
00180 sps->offset_for_ref_frame[i] = se_v ("SPS: offset_for_ref_frame[i]" , s);
00181 }
00182 sps->num_ref_frames = ue_v ("SPS: num_ref_frames" , s);
00183 sps->gaps_in_frame_num_value_allowed_flag = u_1 ("SPS: gaps_in_frame_num_value_allowed_flag" , s);
00184 sps->pic_width_in_mbs_minus1 = ue_v ("SPS: pic_width_in_mbs_minus1" , s);
00185 sps->pic_height_in_map_units_minus1 = ue_v ("SPS: pic_height_in_map_units_minus1" , s);
00186 sps->frame_mbs_only_flag = u_1 ("SPS: frame_mbs_only_flag" , s);
00187 if (!sps->frame_mbs_only_flag)
00188 {
00189 sps->mb_adaptive_frame_field_flag = u_1 ("SPS: mb_adaptive_frame_field_flag" , s);
00190 }
00191 sps->direct_8x8_inference_flag = u_1 ("SPS: direct_8x8_inference_flag" , s);
00192 sps->frame_cropping_flag = u_1 ("SPS: frame_cropping_flag" , s);
00193
00194 if (sps->frame_cropping_flag)
00195 {
00196 sps->frame_cropping_rect_left_offset = ue_v ("SPS: frame_cropping_rect_left_offset" , s);
00197 sps->frame_cropping_rect_right_offset = ue_v ("SPS: frame_cropping_rect_right_offset" , s);
00198 sps->frame_cropping_rect_top_offset = ue_v ("SPS: frame_cropping_rect_top_offset" , s);
00199 sps->frame_cropping_rect_bottom_offset = ue_v ("SPS: frame_cropping_rect_bottom_offset" , s);
00200 }
00201 sps->vui_parameters_present_flag = (Boolean) u_1 ("SPS: vui_parameters_present_flag" , s);
00202
00203 InitVUI(sps);
00204 ReadVUI(p, sps);
00205
00206 sps->Valid = TRUE;
00207
00208 return UsedBits;
00209 }
00210
00211
00212 void InitVUI(seq_parameter_set_rbsp_t *sps)
00213 {
00214 sps->vui_seq_parameters.matrix_coefficients = 2;
00215 }
00216
00217
00218 int ReadVUI(DataPartition *p, seq_parameter_set_rbsp_t *sps)
00219 {
00220 Bitstream *s = p->bitstream;
00221 if (sps->vui_parameters_present_flag)
00222 {
00223 sps->vui_seq_parameters.aspect_ratio_info_present_flag = u_1 ("VUI: aspect_ratio_info_present_flag" , s);
00224 if (sps->vui_seq_parameters.aspect_ratio_info_present_flag)
00225 {
00226 sps->vui_seq_parameters.aspect_ratio_idc = u_v ( 8, "VUI: aspect_ratio_idc" , s);
00227 if (255==sps->vui_seq_parameters.aspect_ratio_idc)
00228 {
00229 sps->vui_seq_parameters.sar_width = u_v (16, "VUI: sar_width" , s);
00230 sps->vui_seq_parameters.sar_height = u_v (16, "VUI: sar_height" , s);
00231 }
00232 }
00233
00234 sps->vui_seq_parameters.overscan_info_present_flag = u_1 ("VUI: overscan_info_present_flag" , s);
00235 if (sps->vui_seq_parameters.overscan_info_present_flag)
00236 {
00237 sps->vui_seq_parameters.overscan_appropriate_flag = u_1 ("VUI: overscan_appropriate_flag" , s);
00238 }
00239
00240 sps->vui_seq_parameters.video_signal_type_present_flag = u_1 ("VUI: video_signal_type_present_flag" , s);
00241 if (sps->vui_seq_parameters.video_signal_type_present_flag)
00242 {
00243 sps->vui_seq_parameters.video_format = u_v ( 3,"VUI: video_format" , s);
00244 sps->vui_seq_parameters.video_full_range_flag = u_1 ( "VUI: video_full_range_flag" , s);
00245 sps->vui_seq_parameters.colour_description_present_flag = u_1 ( "VUI: color_description_present_flag" , s);
00246 if(sps->vui_seq_parameters.colour_description_present_flag)
00247 {
00248 sps->vui_seq_parameters.colour_primaries = u_v ( 8,"VUI: colour_primaries" , s);
00249 sps->vui_seq_parameters.transfer_characteristics = u_v ( 8,"VUI: transfer_characteristics" , s);
00250 sps->vui_seq_parameters.matrix_coefficients = u_v ( 8,"VUI: matrix_coefficients" , s);
00251 }
00252 }
00253 sps->vui_seq_parameters.chroma_location_info_present_flag = u_1 ( "VUI: chroma_loc_info_present_flag" , s);
00254 if(sps->vui_seq_parameters.chroma_location_info_present_flag)
00255 {
00256 sps->vui_seq_parameters.chroma_sample_loc_type_top_field = ue_v ( "VUI: chroma_sample_loc_type_top_field" , s);
00257 sps->vui_seq_parameters.chroma_sample_loc_type_bottom_field = ue_v ( "VUI: chroma_sample_loc_type_bottom_field" , s);
00258 }
00259 sps->vui_seq_parameters.timing_info_present_flag = u_1 ("VUI: timing_info_present_flag" , s);
00260 if (sps->vui_seq_parameters.timing_info_present_flag)
00261 {
00262 sps->vui_seq_parameters.num_units_in_tick = u_v (32,"VUI: num_units_in_tick" , s);
00263 sps->vui_seq_parameters.time_scale = u_v (32,"VUI: time_scale" , s);
00264 sps->vui_seq_parameters.fixed_frame_rate_flag = u_1 ( "VUI: fixed_frame_rate_flag" , s);
00265 }
00266 sps->vui_seq_parameters.nal_hrd_parameters_present_flag = u_1 ("VUI: nal_hrd_parameters_present_flag" , s);
00267 if (sps->vui_seq_parameters.nal_hrd_parameters_present_flag)
00268 {
00269 ReadHRDParameters(p, &(sps->vui_seq_parameters.nal_hrd_parameters));
00270 }
00271 sps->vui_seq_parameters.vcl_hrd_parameters_present_flag = u_1 ("VUI: vcl_hrd_parameters_present_flag" , s);
00272 if (sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
00273 {
00274 ReadHRDParameters(p, &(sps->vui_seq_parameters.vcl_hrd_parameters));
00275 }
00276 if (sps->vui_seq_parameters.nal_hrd_parameters_present_flag || sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
00277 {
00278 sps->vui_seq_parameters.low_delay_hrd_flag = u_1 ("VUI: low_delay_hrd_flag" , s);
00279 }
00280 sps->vui_seq_parameters.pic_struct_present_flag = u_1 ("VUI: pic_struct_present_flag " , s);
00281 sps->vui_seq_parameters.bitstream_restriction_flag = u_1 ("VUI: bitstream_restriction_flag" , s);
00282 if (sps->vui_seq_parameters.bitstream_restriction_flag)
00283 {
00284 sps->vui_seq_parameters.motion_vectors_over_pic_boundaries_flag = u_1 ("VUI: motion_vectors_over_pic_boundaries_flag", s);
00285 sps->vui_seq_parameters.max_bytes_per_pic_denom = ue_v ("VUI: max_bytes_per_pic_denom" , s);
00286 sps->vui_seq_parameters.max_bits_per_mb_denom = ue_v ("VUI: max_bits_per_mb_denom" , s);
00287 sps->vui_seq_parameters.log2_max_mv_length_horizontal = ue_v ("VUI: log2_max_mv_length_horizontal" , s);
00288 sps->vui_seq_parameters.log2_max_mv_length_vertical = ue_v ("VUI: log2_max_mv_length_vertical" , s);
00289 sps->vui_seq_parameters.num_reorder_frames = ue_v ("VUI: num_reorder_frames" , s);
00290 sps->vui_seq_parameters.max_dec_frame_buffering = ue_v ("VUI: max_dec_frame_buffering" , s);
00291 }
00292 }
00293
00294 return 0;
00295 }
00296
00297
00298 int ReadHRDParameters(DataPartition *p, hrd_parameters_t *hrd)
00299 {
00300 Bitstream *s = p->bitstream;
00301 unsigned int SchedSelIdx;
00302
00303 hrd->cpb_cnt_minus1 = ue_v ( "VUI: cpb_cnt_minus1" , s);
00304 hrd->bit_rate_scale = u_v ( 4,"VUI: bit_rate_scale" , s);
00305 hrd->cpb_size_scale = u_v ( 4,"VUI: cpb_size_scale" , s);
00306
00307 for( SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++ )
00308 {
00309 hrd->bit_rate_value_minus1[ SchedSelIdx ] = ue_v ( "VUI: bit_rate_value_minus1" , s);
00310 hrd->cpb_size_value_minus1[ SchedSelIdx ] = ue_v ( "VUI: cpb_size_value_minus1" , s);
00311 hrd->cbr_flag[ SchedSelIdx ] = u_1 ( "VUI: cbr_flag" , s);
00312 }
00313
00314 hrd->initial_cpb_removal_delay_length_minus1 = u_v ( 5,"VUI: initial_cpb_removal_delay_length_minus1" , s);
00315 hrd->cpb_removal_delay_length_minus1 = u_v ( 5,"VUI: cpb_removal_delay_length_minus1" , s);
00316 hrd->dpb_output_delay_length_minus1 = u_v ( 5,"VUI: dpb_output_delay_length_minus1" , s);
00317 hrd->time_offset_length = u_v ( 5,"VUI: time_offset_length" , s);
00318
00319 return 0;
00320 }
00321
00322
00323 int InterpretPPS (DataPartition *p, pic_parameter_set_rbsp_t *pps)
00324 {
00325 unsigned i;
00326 unsigned n_ScalingList;
00327 int chroma_format_idc;
00328 int NumberBitsPerSliceGroupId;
00329 Bitstream *s = p->bitstream;
00330
00331 assert (p != NULL);
00332 assert (p->bitstream != NULL);
00333 assert (p->bitstream->streamBuffer != 0);
00334 assert (pps != NULL);
00335
00336 UsedBits = 0;
00337
00338 pps->pic_parameter_set_id = ue_v ("PPS: pic_parameter_set_id" , s);
00339 pps->seq_parameter_set_id = ue_v ("PPS: seq_parameter_set_id" , s);
00340 pps->entropy_coding_mode_flag = u_1 ("PPS: entropy_coding_mode_flag" , s);
00341
00347 pps->pic_order_present_flag = u_1 ("PPS: pic_order_present_flag" , s);
00348
00349 pps->num_slice_groups_minus1 = ue_v ("PPS: num_slice_groups_minus1" , s);
00350
00351
00352 if (pps->num_slice_groups_minus1 > 0)
00353 {
00354 pps->slice_group_map_type = ue_v ("PPS: slice_group_map_type" , s);
00355 if (pps->slice_group_map_type == 0)
00356 {
00357 for (i=0; i<=pps->num_slice_groups_minus1; i++)
00358 pps->run_length_minus1 [i] = ue_v ("PPS: run_length_minus1 [i]" , s);
00359 }
00360 else if (pps->slice_group_map_type == 2)
00361 {
00362 for (i=0; i<pps->num_slice_groups_minus1; i++)
00363 {
00365 pps->top_left [i] = ue_v ("PPS: top_left [i]" , s);
00366 pps->bottom_right [i] = ue_v ("PPS: bottom_right [i]" , s);
00367 }
00368 }
00369 else if (pps->slice_group_map_type == 3 ||
00370 pps->slice_group_map_type == 4 ||
00371 pps->slice_group_map_type == 5)
00372 {
00373 pps->slice_group_change_direction_flag = u_1 ("PPS: slice_group_change_direction_flag" , s);
00374 pps->slice_group_change_rate_minus1 = ue_v ("PPS: slice_group_change_rate_minus1" , s);
00375 }
00376 else if (pps->slice_group_map_type == 6)
00377 {
00378 if (pps->num_slice_groups_minus1+1 >4)
00379 NumberBitsPerSliceGroupId = 3;
00380 else if (pps->num_slice_groups_minus1+1 > 2)
00381 NumberBitsPerSliceGroupId = 2;
00382 else
00383 NumberBitsPerSliceGroupId = 1;
00384 pps->num_slice_group_map_units_minus1 = ue_v ("PPS: num_slice_group_map_units_minus1" , s);
00385 if ((pps->slice_group_id = calloc (pps->num_slice_group_map_units_minus1+1, 1)) == NULL)
00386 no_mem_exit ("InterpretPPS: slice_group_id");
00387 for (i=0; i<=pps->num_slice_group_map_units_minus1; i++)
00388 pps->slice_group_id[i] = u_v (NumberBitsPerSliceGroupId, "slice_group_id[i]", s);
00389 }
00390 }
00391
00392
00393
00394 pps->num_ref_idx_l0_active_minus1 = ue_v ("PPS: num_ref_idx_l0_active_minus1" , s);
00395 pps->num_ref_idx_l1_active_minus1 = ue_v ("PPS: num_ref_idx_l1_active_minus1" , s);
00396 pps->weighted_pred_flag = u_1 ("PPS: weighted_pred_flag" , s);
00397 pps->weighted_bipred_idc = u_v ( 2, "PPS: weighted_bipred_idc" , s);
00398 pps->pic_init_qp_minus26 = se_v ("PPS: pic_init_qp_minus26" , s);
00399 pps->pic_init_qs_minus26 = se_v ("PPS: pic_init_qs_minus26" , s);
00400
00401 pps->chroma_qp_index_offset = se_v ("PPS: chroma_qp_index_offset" , s);
00402
00403 pps->deblocking_filter_control_present_flag = u_1 ("PPS: deblocking_filter_control_present_flag" , s);
00404 pps->constrained_intra_pred_flag = u_1 ("PPS: constrained_intra_pred_flag" , s);
00405 pps->redundant_pic_cnt_present_flag = u_1 ("PPS: redundant_pic_cnt_present_flag" , s);
00406
00407 if(more_rbsp_data(s->streamBuffer, s->frame_bitoffset,s->bitstream_length))
00408 {
00409
00410 pps->transform_8x8_mode_flag = u_1 ("PPS: transform_8x8_mode_flag" , s);
00411 pps->pic_scaling_matrix_present_flag = u_1 ("PPS: pic_scaling_matrix_present_flag" , s);
00412
00413 if(pps->pic_scaling_matrix_present_flag)
00414 {
00415 chroma_format_idc = SeqParSet[pps->seq_parameter_set_id].chroma_format_idc;
00416 n_ScalingList = 6 + ((chroma_format_idc != YUV444) ? 2 : 6) * pps->transform_8x8_mode_flag;
00417 for(i=0; i<n_ScalingList; i++)
00418 {
00419 pps->pic_scaling_list_present_flag[i]= u_1 ("PPS: pic_scaling_list_present_flag" , s);
00420
00421 if(pps->pic_scaling_list_present_flag[i])
00422 {
00423 if(i<6)
00424 Scaling_List(pps->ScalingList4x4[i], 16, &pps->UseDefaultScalingMatrix4x4Flag[i], s);
00425 else
00426 Scaling_List(pps->ScalingList8x8[i-6], 64, &pps->UseDefaultScalingMatrix8x8Flag[i-6], s);
00427 }
00428 }
00429 }
00430 pps->second_chroma_qp_index_offset = se_v ("PPS: second_chroma_qp_index_offset" , s);
00431 }
00432 else
00433 {
00434 pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
00435 }
00436
00437 pps->Valid = TRUE;
00438 return UsedBits;
00439 }
00440
00441
00442 void PPSConsistencyCheck (pic_parameter_set_rbsp_t *pps)
00443 {
00444 printf ("Consistency checking a picture parset, to be implemented\n");
00445
00446 }
00447
00448 void SPSConsistencyCheck (seq_parameter_set_rbsp_t *sps)
00449 {
00450 printf ("Consistency checking a sequence parset, to be implemented\n");
00451 }
00452
00453 void MakePPSavailable (int id, pic_parameter_set_rbsp_t *pps)
00454 {
00455 assert (pps->Valid == TRUE);
00456
00457 if (PicParSet[id].Valid == TRUE && PicParSet[id].slice_group_id != NULL)
00458 free (PicParSet[id].slice_group_id);
00459
00460 memcpy (&PicParSet[id], pps, sizeof (pic_parameter_set_rbsp_t));
00461
00462
00463
00464 PicParSet[id].slice_group_id = pps->slice_group_id;
00465 pps->slice_group_id = NULL;
00466 }
00467
00468 void CleanUpPPS()
00469 {
00470 int i;
00471
00472 for (i=0; i<MAXPPS; i++)
00473 {
00474 if (PicParSet[i].Valid == TRUE && PicParSet[i].slice_group_id != NULL)
00475 free (PicParSet[i].slice_group_id);
00476
00477 PicParSet[i].Valid = FALSE;
00478 }
00479 }
00480
00481
00482 void MakeSPSavailable (int id, seq_parameter_set_rbsp_t *sps)
00483 {
00484 assert (sps->Valid == TRUE);
00485 memcpy (&SeqParSet[id], sps, sizeof (seq_parameter_set_rbsp_t));
00486 }
00487
00488
00489 void ProcessSPS (NALU_t *nalu)
00490 {
00491 DataPartition *dp = AllocPartition(1);
00492 seq_parameter_set_rbsp_t *sps = AllocSPS();
00493
00494 memcpy (dp->bitstream->streamBuffer, &nalu->buf[1], nalu->len-1);
00495 dp->bitstream->code_len = dp->bitstream->bitstream_length = RBSPtoSODB (dp->bitstream->streamBuffer, nalu->len-1);
00496 dp->bitstream->ei_flag = 0;
00497 dp->bitstream->read_len = dp->bitstream->frame_bitoffset = 0;
00498 InterpretSPS (dp, sps);
00499
00500 if (sps->Valid)
00501 {
00502 if (active_sps)
00503 {
00504 if (sps->seq_parameter_set_id == active_sps->seq_parameter_set_id)
00505 {
00506 if (!sps_is_equal(sps, active_sps))
00507 {
00508 if (dec_picture)
00509 {
00510
00511 exit_picture(&dec_picture);
00512 }
00513 active_sps=NULL;
00514 }
00515 }
00516 }
00517
00518 MakeSPSavailable (sps->seq_parameter_set_id, sps);
00519 img->profile_idc = sps->profile_idc;
00520 img->separate_colour_plane_flag = sps->separate_colour_plane_flag;
00521 if( img->separate_colour_plane_flag )
00522 {
00523 img->ChromaArrayType = 0;
00524 }
00525 else
00526 {
00527 img->ChromaArrayType = sps->chroma_format_idc;
00528 }
00529 }
00530
00531 FreePartition (dp, 1);
00532 FreeSPS (sps);
00533 }
00534
00535
00536 void ProcessPPS (NALU_t *nalu)
00537 {
00538 DataPartition *dp;
00539 pic_parameter_set_rbsp_t *pps;
00540
00541 dp = AllocPartition(1);
00542 pps = AllocPPS();
00543 memcpy (dp->bitstream->streamBuffer, &nalu->buf[1], nalu->len-1);
00544 dp->bitstream->code_len = dp->bitstream->bitstream_length = RBSPtoSODB (dp->bitstream->streamBuffer, nalu->len-1);
00545 dp->bitstream->ei_flag = 0;
00546 dp->bitstream->read_len = dp->bitstream->frame_bitoffset = 0;
00547 InterpretPPS (dp, pps);
00548
00549 if (active_pps)
00550 {
00551 if (pps->pic_parameter_set_id == active_pps->pic_parameter_set_id)
00552 {
00553 if (!pps_is_equal(pps, active_pps))
00554 {
00555 if (dec_picture)
00556 {
00557
00558 exit_picture(&dec_picture);
00559 }
00560 active_pps = NULL;
00561 }
00562 }
00563 }
00564 MakePPSavailable (pps->pic_parameter_set_id, pps);
00565 FreePartition (dp, 1);
00566 FreePPS (pps);
00567 }
00568
00569 void activate_sps (seq_parameter_set_rbsp_t *sps)
00570 {
00571 int nplane;
00572 if (active_sps != sps)
00573 {
00574 if (dec_picture)
00575 {
00576
00577 exit_picture(&dec_picture);
00578 }
00579 active_sps = sps;
00580
00581 img->bitdepth_chroma = 0;
00582 img->width_cr = 0;
00583 img->height_cr = 0;
00584
00585
00586 if (active_sps->level_idc <= 10)
00587 {
00588 img->max_vmv_r = 64 * 4;
00589 }
00590 else if (active_sps->level_idc <= 20)
00591 {
00592 img->max_vmv_r = 128 * 4;
00593 }
00594 else if (active_sps->level_idc <= 30)
00595 {
00596 img->max_vmv_r = 256 * 4;
00597 }
00598 else
00599 {
00600 img->max_vmv_r = 512 * 4;
00601 }
00602
00603
00604 img->bitdepth_luma = sps->bit_depth_luma_minus8 + 8;
00605 img->bitdepth_scale[0] = 1 << sps->bit_depth_luma_minus8;
00606 if (sps->chroma_format_idc != YUV400)
00607 {
00608 img->bitdepth_chroma = sps->bit_depth_chroma_minus8 + 8;
00609 img->bitdepth_scale[1] = 1 << sps->bit_depth_chroma_minus8;
00610 }
00611
00612 img->MaxFrameNum = 1<<(sps->log2_max_frame_num_minus4+4);
00613 img->PicWidthInMbs = (sps->pic_width_in_mbs_minus1 +1);
00614 img->PicHeightInMapUnits = (sps->pic_height_in_map_units_minus1 +1);
00615 img->FrameHeightInMbs = ( 2 - sps->frame_mbs_only_flag ) * img->PicHeightInMapUnits;
00616 img->FrameSizeInMbs = img->PicWidthInMbs * img->FrameHeightInMbs;
00617
00618 img->yuv_format=sps->chroma_format_idc;
00619
00620 img->width = img->PicWidthInMbs * MB_BLOCK_SIZE;
00621 img->height = img->FrameHeightInMbs * MB_BLOCK_SIZE;
00622
00623 if (sps->chroma_format_idc == YUV420)
00624 {
00625 img->width_cr = img->width >>1;
00626 img->height_cr = img->height >>1;
00627 }
00628 else if (sps->chroma_format_idc == YUV422)
00629 {
00630 img->width_cr = img->width >>1;
00631 img->height_cr = img->height;
00632 }
00633 else if (sps->chroma_format_idc == YUV444)
00634 {
00635
00636 img->width_cr = img->width;
00637 img->height_cr = img->height;
00638 }
00639
00640 img->width_cr_m1 = img->width_cr - 1;
00641
00642 init_frext(img);
00643 init_global_buffers();
00644
00645 if (!img->no_output_of_prior_pics_flag)
00646 {
00647 flush_dpb();
00648 }
00649 init_dpb();
00650
00651 if( IS_INDEPENDENT(img) )
00652 {
00653 for( nplane=0; nplane<MAX_PLANE; nplane++ )
00654 {
00655 if( NULL != Co_located_JV[nplane] )
00656 {
00657 free_colocated(Co_located_JV[nplane]);
00658 }
00659 Co_located_JV[nplane] = alloc_colocated (img->width, img->height,sps->mb_adaptive_frame_field_flag);
00660 }
00661 }
00662 else
00663 {
00664 if (NULL!=Co_located)
00665 {
00666 free_colocated(Co_located);
00667 }
00668 Co_located = alloc_colocated (img->width, img->height,sps->mb_adaptive_frame_field_flag);
00669 }
00670
00671 ercInit(img->width, img->height, 1);
00672 }
00673 }
00674
00675 void activate_pps(pic_parameter_set_rbsp_t *pps)
00676 {
00677 if (active_pps != pps)
00678 {
00679 if (dec_picture)
00680 {
00681
00682 exit_picture(&dec_picture);
00683 }
00684
00685 active_pps = pps;
00686
00687
00688 img->Transform8x8Mode = pps->transform_8x8_mode_flag;
00689
00690 }
00691 }
00692
00693 void UseParameterSet (int PicParsetId)
00694 {
00695 seq_parameter_set_rbsp_t *sps = &SeqParSet[PicParSet[PicParsetId].seq_parameter_set_id];
00696 pic_parameter_set_rbsp_t *pps = &PicParSet[PicParsetId];
00697 int i;
00698
00699
00700 if (PicParSet[PicParsetId].Valid != TRUE)
00701 printf ("Trying to use an invalid (uninitialized) Picture Parameter Set with ID %d, expect the unexpected...\n", PicParsetId);
00702 if (SeqParSet[PicParSet[PicParsetId].seq_parameter_set_id].Valid != TRUE)
00703 printf ("PicParset %d references an invalid (uninitialized) Sequence Parameter Set with ID %d, expect the unexpected...\n", PicParsetId, (int) PicParSet[PicParsetId].seq_parameter_set_id);
00704
00705 sps = &SeqParSet[PicParSet[PicParsetId].seq_parameter_set_id];
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716 if ((int) sps->pic_order_cnt_type < 0 || sps->pic_order_cnt_type > 2)
00717 {
00718 printf ("invalid sps->pic_order_cnt_type = %d\n", (int) sps->pic_order_cnt_type);
00719 error ("pic_order_cnt_type != 1", -1000);
00720 }
00721
00722 if (sps->pic_order_cnt_type == 1)
00723 {
00724 if(sps->num_ref_frames_in_pic_order_cnt_cycle >= MAXnum_ref_frames_in_pic_order_cnt_cycle)
00725 {
00726 error("num_ref_frames_in_pic_order_cnt_cycle too large",-1011);
00727 }
00728 }
00729
00730 activate_sps(sps);
00731 activate_pps(pps);
00732
00733
00734
00735 if (pps->entropy_coding_mode_flag == UVLC)
00736 {
00737 nal_startcode_follows = uvlc_startcode_follows;
00738 for (i=0; i<3; i++)
00739 {
00740 img->currentSlice->partArr[i].readSyntaxElement = readSyntaxElement_UVLC;
00741 }
00742 }
00743 else
00744 {
00745 nal_startcode_follows = cabac_startcode_follows;
00746 for (i=0; i<3; i++)
00747 {
00748 img->currentSlice->partArr[i].readSyntaxElement = readSyntaxElement_CABAC;
00749 }
00750 }
00751 }
00752