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
00034 extern void init_frext(ImageParameters *p_Img);
00035
00036
00037 void Scaling_List(int *scalingList, int sizeOfScalingList, Boolean *UseDefaultScalingMatrix, Bitstream *s)
00038 {
00039 int j, scanj;
00040 int delta_scale, lastScale, nextScale;
00041
00042 lastScale = 8;
00043 nextScale = 8;
00044
00045 for(j=0; j<sizeOfScalingList; j++)
00046 {
00047 scanj = (sizeOfScalingList==16) ? ZZ_SCAN[j]:ZZ_SCAN8[j];
00048
00049 if(nextScale!=0)
00050 {
00051 delta_scale = se_v ( " : delta_sl " , s);
00052 nextScale = (lastScale + delta_scale + 256) % 256;
00053 *UseDefaultScalingMatrix = (Boolean) (scanj==0 && nextScale==0);
00054 }
00055
00056 scalingList[scanj] = (nextScale==0) ? lastScale:nextScale;
00057 lastScale = scalingList[scanj];
00058 }
00059 }
00060
00061
00062 int InterpretSPS (ImageParameters *p_Img, DataPartition *p, seq_parameter_set_rbsp_t *sps)
00063 {
00064 unsigned i;
00065 unsigned n_ScalingList;
00066 int reserved_zero;
00067 Bitstream *s = p->bitstream;
00068
00069 assert (p != NULL);
00070 assert (p->bitstream != NULL);
00071 assert (p->bitstream->streamBuffer != 0);
00072 assert (sps != NULL);
00073
00074 p_Dec->UsedBits = 0;
00075
00076 sps->profile_idc = u_v (8, "SPS: profile_idc" , s);
00077
00078 if ((sps->profile_idc!=BASELINE ) &&
00079 (sps->profile_idc!=MAIN ) &&
00080 (sps->profile_idc!=EXTENDED ) &&
00081 (sps->profile_idc!=FREXT_HP ) &&
00082 (sps->profile_idc!=FREXT_Hi10P ) &&
00083 (sps->profile_idc!=FREXT_Hi422 ) &&
00084 (sps->profile_idc!=FREXT_Hi444 ) &&
00085 (sps->profile_idc!=FREXT_CAVLC444 ))
00086 {
00087 printf("Invalid Profile IDC (%d) encountered. \n", sps->profile_idc);
00088 return p_Dec->UsedBits;
00089 }
00090
00091 sps->constrained_set0_flag = u_1 ( "SPS: constrained_set0_flag" , s);
00092 sps->constrained_set1_flag = u_1 ( "SPS: constrained_set1_flag" , s);
00093 sps->constrained_set2_flag = u_1 ( "SPS: constrained_set2_flag" , s);
00094 sps->constrained_set3_flag = u_1 ( "SPS: constrained_set3_flag" , s);
00095 reserved_zero = u_v (4, "SPS: reserved_zero_4bits" , s);
00096 assert (reserved_zero==0);
00097
00098 sps->level_idc = u_v (8, "SPS: level_idc" , s);
00099
00100 sps->seq_parameter_set_id = ue_v ("SPS: seq_parameter_set_id" , s);
00101
00102
00103 sps->chroma_format_idc = 1;
00104 sps->bit_depth_luma_minus8 = 0;
00105 sps->bit_depth_chroma_minus8 = 0;
00106 p_Img->lossless_qpprime_flag = 0;
00107 sps->separate_colour_plane_flag = 0;
00108
00109 if((sps->profile_idc==FREXT_HP ) ||
00110 (sps->profile_idc==FREXT_Hi10P) ||
00111 (sps->profile_idc==FREXT_Hi422) ||
00112 (sps->profile_idc==FREXT_Hi444) ||
00113 (sps->profile_idc==FREXT_CAVLC444))
00114 {
00115 sps->chroma_format_idc = ue_v ("SPS: chroma_format_idc" , s);
00116
00117 if(sps->chroma_format_idc == YUV444)
00118 {
00119 sps->separate_colour_plane_flag = u_1 ("SPS: separate_colour_plane_flag" , s);
00120 }
00121
00122 sps->bit_depth_luma_minus8 = ue_v ("SPS: bit_depth_luma_minus8" , s);
00123 sps->bit_depth_chroma_minus8 = ue_v ("SPS: bit_depth_chroma_minus8" , s);
00124 p_Img->lossless_qpprime_flag = u_1 ("SPS: lossless_qpprime_y_zero_flag" , s);
00125
00126 sps->seq_scaling_matrix_present_flag = u_1 ( "SPS: seq_scaling_matrix_present_flag" , s);
00127
00128 if(sps->seq_scaling_matrix_present_flag)
00129 {
00130 n_ScalingList = (sps->chroma_format_idc != YUV444) ? 8 : 12;
00131 for(i=0; i<n_ScalingList; i++)
00132 {
00133 sps->seq_scaling_list_present_flag[i] = u_1 ( "SPS: seq_scaling_list_present_flag" , s);
00134 if(sps->seq_scaling_list_present_flag[i])
00135 {
00136 if(i<6)
00137 Scaling_List(sps->ScalingList4x4[i], 16, &sps->UseDefaultScalingMatrix4x4Flag[i], s);
00138 else
00139 Scaling_List(sps->ScalingList8x8[i-6], 64, &sps->UseDefaultScalingMatrix8x8Flag[i-6], s);
00140 }
00141 }
00142 }
00143 }
00144
00145 sps->log2_max_frame_num_minus4 = ue_v ("SPS: log2_max_frame_num_minus4" , s);
00146 sps->pic_order_cnt_type = ue_v ("SPS: pic_order_cnt_type" , s);
00147
00148 if (sps->pic_order_cnt_type == 0)
00149 sps->log2_max_pic_order_cnt_lsb_minus4 = ue_v ("SPS: log2_max_pic_order_cnt_lsb_minus4" , s);
00150 else if (sps->pic_order_cnt_type == 1)
00151 {
00152 sps->delta_pic_order_always_zero_flag = u_1 ("SPS: delta_pic_order_always_zero_flag" , s);
00153 sps->offset_for_non_ref_pic = se_v ("SPS: offset_for_non_ref_pic" , s);
00154 sps->offset_for_top_to_bottom_field = se_v ("SPS: offset_for_top_to_bottom_field" , s);
00155 sps->num_ref_frames_in_pic_order_cnt_cycle = ue_v ("SPS: num_ref_frames_in_pic_order_cnt_cycle" , s);
00156 for(i=0; i<sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
00157 sps->offset_for_ref_frame[i] = se_v ("SPS: offset_for_ref_frame[i]" , s);
00158 }
00159 sps->num_ref_frames = ue_v ("SPS: num_ref_frames" , s);
00160 sps->gaps_in_frame_num_value_allowed_flag = u_1 ("SPS: gaps_in_frame_num_value_allowed_flag" , s);
00161 sps->pic_width_in_mbs_minus1 = ue_v ("SPS: pic_width_in_mbs_minus1" , s);
00162 sps->pic_height_in_map_units_minus1 = ue_v ("SPS: pic_height_in_map_units_minus1" , s);
00163 sps->frame_mbs_only_flag = u_1 ("SPS: frame_mbs_only_flag" , s);
00164 if (!sps->frame_mbs_only_flag)
00165 {
00166 sps->mb_adaptive_frame_field_flag = u_1 ("SPS: mb_adaptive_frame_field_flag" , s);
00167 }
00168 sps->direct_8x8_inference_flag = u_1 ("SPS: direct_8x8_inference_flag" , s);
00169 sps->frame_cropping_flag = u_1 ("SPS: frame_cropping_flag" , s);
00170
00171 if (sps->frame_cropping_flag)
00172 {
00173 sps->frame_cropping_rect_left_offset = ue_v ("SPS: frame_cropping_rect_left_offset" , s);
00174 sps->frame_cropping_rect_right_offset = ue_v ("SPS: frame_cropping_rect_right_offset" , s);
00175 sps->frame_cropping_rect_top_offset = ue_v ("SPS: frame_cropping_rect_top_offset" , s);
00176 sps->frame_cropping_rect_bottom_offset = ue_v ("SPS: frame_cropping_rect_bottom_offset" , s);
00177 }
00178 sps->vui_parameters_present_flag = (Boolean) u_1 ("SPS: vui_parameters_present_flag" , s);
00179
00180 InitVUI(sps);
00181 ReadVUI(p, sps);
00182
00183 sps->Valid = TRUE;
00184
00185 return p_Dec->UsedBits;
00186 }
00187
00188
00189 void InitVUI(seq_parameter_set_rbsp_t *sps)
00190 {
00191 sps->vui_seq_parameters.matrix_coefficients = 2;
00192 }
00193
00194
00195 int ReadVUI(DataPartition *p, seq_parameter_set_rbsp_t *sps)
00196 {
00197 Bitstream *s = p->bitstream;
00198 if (sps->vui_parameters_present_flag)
00199 {
00200 sps->vui_seq_parameters.aspect_ratio_info_present_flag = u_1 ("VUI: aspect_ratio_info_present_flag" , s);
00201 if (sps->vui_seq_parameters.aspect_ratio_info_present_flag)
00202 {
00203 sps->vui_seq_parameters.aspect_ratio_idc = u_v ( 8, "VUI: aspect_ratio_idc" , s);
00204 if (255==sps->vui_seq_parameters.aspect_ratio_idc)
00205 {
00206 sps->vui_seq_parameters.sar_width = u_v (16, "VUI: sar_width" , s);
00207 sps->vui_seq_parameters.sar_height = u_v (16, "VUI: sar_height" , s);
00208 }
00209 }
00210
00211 sps->vui_seq_parameters.overscan_info_present_flag = u_1 ("VUI: overscan_info_present_flag" , s);
00212 if (sps->vui_seq_parameters.overscan_info_present_flag)
00213 {
00214 sps->vui_seq_parameters.overscan_appropriate_flag = u_1 ("VUI: overscan_appropriate_flag" , s);
00215 }
00216
00217 sps->vui_seq_parameters.video_signal_type_present_flag = u_1 ("VUI: video_signal_type_present_flag" , s);
00218 if (sps->vui_seq_parameters.video_signal_type_present_flag)
00219 {
00220 sps->vui_seq_parameters.video_format = u_v ( 3,"VUI: video_format" , s);
00221 sps->vui_seq_parameters.video_full_range_flag = u_1 ( "VUI: video_full_range_flag" , s);
00222 sps->vui_seq_parameters.colour_description_present_flag = u_1 ( "VUI: color_description_present_flag" , s);
00223 if(sps->vui_seq_parameters.colour_description_present_flag)
00224 {
00225 sps->vui_seq_parameters.colour_primaries = u_v ( 8,"VUI: colour_primaries" , s);
00226 sps->vui_seq_parameters.transfer_characteristics = u_v ( 8,"VUI: transfer_characteristics" , s);
00227 sps->vui_seq_parameters.matrix_coefficients = u_v ( 8,"VUI: matrix_coefficients" , s);
00228 }
00229 }
00230 sps->vui_seq_parameters.chroma_location_info_present_flag = u_1 ( "VUI: chroma_loc_info_present_flag" , s);
00231 if(sps->vui_seq_parameters.chroma_location_info_present_flag)
00232 {
00233 sps->vui_seq_parameters.chroma_sample_loc_type_top_field = ue_v ( "VUI: chroma_sample_loc_type_top_field" , s);
00234 sps->vui_seq_parameters.chroma_sample_loc_type_bottom_field = ue_v ( "VUI: chroma_sample_loc_type_bottom_field" , s);
00235 }
00236 sps->vui_seq_parameters.timing_info_present_flag = u_1 ("VUI: timing_info_present_flag" , s);
00237 if (sps->vui_seq_parameters.timing_info_present_flag)
00238 {
00239 sps->vui_seq_parameters.num_units_in_tick = u_v (32,"VUI: num_units_in_tick" , s);
00240 sps->vui_seq_parameters.time_scale = u_v (32,"VUI: time_scale" , s);
00241 sps->vui_seq_parameters.fixed_frame_rate_flag = u_1 ( "VUI: fixed_frame_rate_flag" , s);
00242 }
00243 sps->vui_seq_parameters.nal_hrd_parameters_present_flag = u_1 ("VUI: nal_hrd_parameters_present_flag" , s);
00244 if (sps->vui_seq_parameters.nal_hrd_parameters_present_flag)
00245 {
00246 ReadHRDParameters(p, &(sps->vui_seq_parameters.nal_hrd_parameters));
00247 }
00248 sps->vui_seq_parameters.vcl_hrd_parameters_present_flag = u_1 ("VUI: vcl_hrd_parameters_present_flag" , s);
00249 if (sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
00250 {
00251 ReadHRDParameters(p, &(sps->vui_seq_parameters.vcl_hrd_parameters));
00252 }
00253 if (sps->vui_seq_parameters.nal_hrd_parameters_present_flag || sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
00254 {
00255 sps->vui_seq_parameters.low_delay_hrd_flag = u_1 ("VUI: low_delay_hrd_flag" , s);
00256 }
00257 sps->vui_seq_parameters.pic_struct_present_flag = u_1 ("VUI: pic_struct_present_flag " , s);
00258 sps->vui_seq_parameters.bitstream_restriction_flag = u_1 ("VUI: bitstream_restriction_flag" , s);
00259 if (sps->vui_seq_parameters.bitstream_restriction_flag)
00260 {
00261 sps->vui_seq_parameters.motion_vectors_over_pic_boundaries_flag = u_1 ("VUI: motion_vectors_over_pic_boundaries_flag", s);
00262 sps->vui_seq_parameters.max_bytes_per_pic_denom = ue_v ("VUI: max_bytes_per_pic_denom" , s);
00263 sps->vui_seq_parameters.max_bits_per_mb_denom = ue_v ("VUI: max_bits_per_mb_denom" , s);
00264 sps->vui_seq_parameters.log2_max_mv_length_horizontal = ue_v ("VUI: log2_max_mv_length_horizontal" , s);
00265 sps->vui_seq_parameters.log2_max_mv_length_vertical = ue_v ("VUI: log2_max_mv_length_vertical" , s);
00266 sps->vui_seq_parameters.num_reorder_frames = ue_v ("VUI: num_reorder_frames" , s);
00267 sps->vui_seq_parameters.max_dec_frame_buffering = ue_v ("VUI: max_dec_frame_buffering" , s);
00268 }
00269 }
00270
00271 return 0;
00272 }
00273
00274
00275 int ReadHRDParameters(DataPartition *p, hrd_parameters_t *hrd)
00276 {
00277 Bitstream *s = p->bitstream;
00278 unsigned int SchedSelIdx;
00279
00280 hrd->cpb_cnt_minus1 = ue_v ( "VUI: cpb_cnt_minus1" , s);
00281 hrd->bit_rate_scale = u_v ( 4,"VUI: bit_rate_scale" , s);
00282 hrd->cpb_size_scale = u_v ( 4,"VUI: cpb_size_scale" , s);
00283
00284 for( SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++ )
00285 {
00286 hrd->bit_rate_value_minus1[ SchedSelIdx ] = ue_v ( "VUI: bit_rate_value_minus1" , s);
00287 hrd->cpb_size_value_minus1[ SchedSelIdx ] = ue_v ( "VUI: cpb_size_value_minus1" , s);
00288 hrd->cbr_flag[ SchedSelIdx ] = u_1 ( "VUI: cbr_flag" , s);
00289 }
00290
00291 hrd->initial_cpb_removal_delay_length_minus1 = u_v ( 5,"VUI: initial_cpb_removal_delay_length_minus1" , s);
00292 hrd->cpb_removal_delay_length_minus1 = u_v ( 5,"VUI: cpb_removal_delay_length_minus1" , s);
00293 hrd->dpb_output_delay_length_minus1 = u_v ( 5,"VUI: dpb_output_delay_length_minus1" , s);
00294 hrd->time_offset_length = u_v ( 5,"VUI: time_offset_length" , s);
00295
00296 return 0;
00297 }
00298
00299
00300 int InterpretPPS (ImageParameters *p_Img, DataPartition *p, pic_parameter_set_rbsp_t *pps)
00301 {
00302 unsigned i;
00303 unsigned n_ScalingList;
00304 int chroma_format_idc;
00305 int NumberBitsPerSliceGroupId;
00306 Bitstream *s = p->bitstream;
00307
00308 assert (p != NULL);
00309 assert (p->bitstream != NULL);
00310 assert (p->bitstream->streamBuffer != 0);
00311 assert (pps != NULL);
00312
00313 p_Dec->UsedBits = 0;
00314
00315 pps->pic_parameter_set_id = ue_v ("PPS: pic_parameter_set_id" , s);
00316 pps->seq_parameter_set_id = ue_v ("PPS: seq_parameter_set_id" , s);
00317 pps->entropy_coding_mode_flag = u_1 ("PPS: entropy_coding_mode_flag" , s);
00318
00324 pps->bottom_field_pic_order_in_frame_present_flag = u_1 ("PPS: bottom_field_pic_order_in_frame_present_flag" , s);
00325
00326 pps->num_slice_groups_minus1 = ue_v ("PPS: num_slice_groups_minus1" , s);
00327
00328
00329 if (pps->num_slice_groups_minus1 > 0)
00330 {
00331 pps->slice_group_map_type = ue_v ("PPS: slice_group_map_type" , s);
00332 if (pps->slice_group_map_type == 0)
00333 {
00334 for (i=0; i<=pps->num_slice_groups_minus1; i++)
00335 pps->run_length_minus1 [i] = ue_v ("PPS: run_length_minus1 [i]" , s);
00336 }
00337 else if (pps->slice_group_map_type == 2)
00338 {
00339 for (i=0; i<pps->num_slice_groups_minus1; i++)
00340 {
00342 pps->top_left [i] = ue_v ("PPS: top_left [i]" , s);
00343 pps->bottom_right [i] = ue_v ("PPS: bottom_right [i]" , s);
00344 }
00345 }
00346 else if (pps->slice_group_map_type == 3 ||
00347 pps->slice_group_map_type == 4 ||
00348 pps->slice_group_map_type == 5)
00349 {
00350 pps->slice_group_change_direction_flag = u_1 ("PPS: slice_group_change_direction_flag" , s);
00351 pps->slice_group_change_rate_minus1 = ue_v ("PPS: slice_group_change_rate_minus1" , s);
00352 }
00353 else if (pps->slice_group_map_type == 6)
00354 {
00355 if (pps->num_slice_groups_minus1+1 >4)
00356 NumberBitsPerSliceGroupId = 3;
00357 else if (pps->num_slice_groups_minus1+1 > 2)
00358 NumberBitsPerSliceGroupId = 2;
00359 else
00360 NumberBitsPerSliceGroupId = 1;
00361 pps->pic_size_in_map_units_minus1 = ue_v ("PPS: pic_size_in_map_units_minus1" , s);
00362 if ((pps->slice_group_id = calloc (pps->pic_size_in_map_units_minus1+1, 1)) == NULL)
00363 no_mem_exit ("InterpretPPS: slice_group_id");
00364 for (i=0; i<=pps->pic_size_in_map_units_minus1; i++)
00365 pps->slice_group_id[i] = (byte) u_v (NumberBitsPerSliceGroupId, "slice_group_id[i]", s);
00366 }
00367 }
00368
00369
00370
00371 pps->num_ref_idx_l0_active_minus1 = ue_v ("PPS: num_ref_idx_l0_active_minus1" , s);
00372 pps->num_ref_idx_l1_active_minus1 = ue_v ("PPS: num_ref_idx_l1_active_minus1" , s);
00373 pps->weighted_pred_flag = u_1 ("PPS: weighted_pred_flag" , s);
00374 pps->weighted_bipred_idc = u_v ( 2, "PPS: weighted_bipred_idc" , s);
00375 pps->pic_init_qp_minus26 = se_v ("PPS: pic_init_qp_minus26" , s);
00376 pps->pic_init_qs_minus26 = se_v ("PPS: pic_init_qs_minus26" , s);
00377
00378 pps->chroma_qp_index_offset = se_v ("PPS: chroma_qp_index_offset" , s);
00379
00380 pps->deblocking_filter_control_present_flag = u_1 ("PPS: deblocking_filter_control_present_flag" , s);
00381 pps->constrained_intra_pred_flag = u_1 ("PPS: constrained_intra_pred_flag" , s);
00382 pps->redundant_pic_cnt_present_flag = u_1 ("PPS: redundant_pic_cnt_present_flag" , s);
00383
00384 if(more_rbsp_data(s->streamBuffer, s->frame_bitoffset,s->bitstream_length))
00385 {
00386
00387 pps->transform_8x8_mode_flag = u_1 ("PPS: transform_8x8_mode_flag" , s);
00388 pps->pic_scaling_matrix_present_flag = u_1 ("PPS: pic_scaling_matrix_present_flag" , s);
00389
00390 if(pps->pic_scaling_matrix_present_flag)
00391 {
00392 chroma_format_idc = p_Img->SeqParSet[pps->seq_parameter_set_id].chroma_format_idc;
00393 n_ScalingList = 6 + ((chroma_format_idc != YUV444) ? 2 : 6) * pps->transform_8x8_mode_flag;
00394 for(i=0; i<n_ScalingList; i++)
00395 {
00396 pps->pic_scaling_list_present_flag[i]= u_1 ("PPS: pic_scaling_list_present_flag" , s);
00397
00398 if(pps->pic_scaling_list_present_flag[i])
00399 {
00400 if(i<6)
00401 Scaling_List(pps->ScalingList4x4[i], 16, &pps->UseDefaultScalingMatrix4x4Flag[i], s);
00402 else
00403 Scaling_List(pps->ScalingList8x8[i-6], 64, &pps->UseDefaultScalingMatrix8x8Flag[i-6], s);
00404 }
00405 }
00406 }
00407 pps->second_chroma_qp_index_offset = se_v ("PPS: second_chroma_qp_index_offset" , s);
00408 }
00409 else
00410 {
00411 pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
00412 }
00413
00414 pps->Valid = TRUE;
00415 return p_Dec->UsedBits;
00416 }
00417
00418
00419 void PPSConsistencyCheck (pic_parameter_set_rbsp_t *pps)
00420 {
00421 printf ("Consistency checking a picture parset, to be implemented\n");
00422
00423 }
00424
00425 void SPSConsistencyCheck (seq_parameter_set_rbsp_t *sps)
00426 {
00427 printf ("Consistency checking a sequence parset, to be implemented\n");
00428 }
00429
00430 void MakePPSavailable (ImageParameters *p_Img, int id, pic_parameter_set_rbsp_t *pps)
00431 {
00432 assert (pps->Valid == TRUE);
00433
00434 if (p_Img->PicParSet[id].Valid == TRUE && p_Img->PicParSet[id].slice_group_id != NULL)
00435 free (p_Img->PicParSet[id].slice_group_id);
00436
00437 memcpy (&p_Img->PicParSet[id], pps, sizeof (pic_parameter_set_rbsp_t));
00438
00439
00440
00441 p_Img->PicParSet[id].slice_group_id = pps->slice_group_id;
00442 pps->slice_group_id = NULL;
00443 }
00444
00445 void CleanUpPPS(ImageParameters *p_Img)
00446 {
00447 int i;
00448
00449 for (i=0; i<MAXPPS; i++)
00450 {
00451 if (p_Img->PicParSet[i].Valid == TRUE && p_Img->PicParSet[i].slice_group_id != NULL)
00452 free (p_Img->PicParSet[i].slice_group_id);
00453
00454 p_Img->PicParSet[i].Valid = FALSE;
00455 }
00456 }
00457
00458
00459 void MakeSPSavailable (ImageParameters *p_Img, int id, seq_parameter_set_rbsp_t *sps)
00460 {
00461 assert (sps->Valid == TRUE);
00462 memcpy (&p_Img->SeqParSet[id], sps, sizeof (seq_parameter_set_rbsp_t));
00463 }
00464
00465
00466 void ProcessSPS (ImageParameters *p_Img, NALU_t *nalu)
00467 {
00468 DataPartition *dp = AllocPartition(1);
00469 seq_parameter_set_rbsp_t *sps = AllocSPS();
00470
00471 memcpy (dp->bitstream->streamBuffer, &nalu->buf[1], nalu->len-1);
00472 dp->bitstream->code_len = dp->bitstream->bitstream_length = RBSPtoSODB (dp->bitstream->streamBuffer, nalu->len-1);
00473 dp->bitstream->ei_flag = 0;
00474 dp->bitstream->read_len = dp->bitstream->frame_bitoffset = 0;
00475 InterpretSPS (p_Img, dp, sps);
00476
00477 if (sps->Valid)
00478 {
00479 if (p_Img->active_sps)
00480 {
00481 if (sps->seq_parameter_set_id == p_Img->active_sps->seq_parameter_set_id)
00482 {
00483 if (!sps_is_equal(sps, p_Img->active_sps))
00484 {
00485 if (p_Img->dec_picture)
00486 {
00487
00488 exit_picture(p_Img, &p_Img->dec_picture);
00489 }
00490 p_Img->active_sps=NULL;
00491 }
00492 }
00493 }
00494
00495 MakeSPSavailable (p_Img, sps->seq_parameter_set_id, sps);
00496 p_Img->profile_idc = sps->profile_idc;
00497 p_Img->separate_colour_plane_flag = sps->separate_colour_plane_flag;
00498 if( p_Img->separate_colour_plane_flag )
00499 {
00500 p_Img->ChromaArrayType = 0;
00501 }
00502 else
00503 {
00504 p_Img->ChromaArrayType = sps->chroma_format_idc;
00505 }
00506 }
00507
00508 FreePartition (dp, 1);
00509 FreeSPS (sps);
00510 }
00511
00512
00513 void ProcessPPS (ImageParameters *p_Img, NALU_t *nalu)
00514 {
00515 DataPartition *dp = AllocPartition(1);
00516 pic_parameter_set_rbsp_t *pps = AllocPPS();
00517
00518 memcpy (dp->bitstream->streamBuffer, &nalu->buf[1], nalu->len-1);
00519 dp->bitstream->code_len = dp->bitstream->bitstream_length = RBSPtoSODB (dp->bitstream->streamBuffer, nalu->len-1);
00520 dp->bitstream->ei_flag = 0;
00521 dp->bitstream->read_len = dp->bitstream->frame_bitoffset = 0;
00522 InterpretPPS (p_Img, dp, pps);
00523
00524 if (p_Img->active_pps)
00525 {
00526 if (pps->pic_parameter_set_id == p_Img->active_pps->pic_parameter_set_id)
00527 {
00528 if (!pps_is_equal(pps, p_Img->active_pps))
00529 {
00530 if (p_Img->dec_picture)
00531 {
00532
00533 exit_picture(p_Img, &p_Img->dec_picture);
00534 }
00535 p_Img->active_pps = NULL;
00536 }
00537 }
00538 }
00539 MakePPSavailable (p_Img, pps->pic_parameter_set_id, pps);
00540 FreePartition (dp, 1);
00541 FreePPS (pps);
00542 }
00543
00551 static void updateMaxValue(FrameFormat *format)
00552 {
00553 format->max_value[0] = (1 << format->bit_depth[0]) - 1;
00554 format->max_value_sq[0] = format->max_value[0] * format->max_value[0];
00555 format->max_value[1] = (1 << format->bit_depth[1]) - 1;
00556 format->max_value_sq[1] = format->max_value[1] * format->max_value[1];
00557 format->max_value[2] = (1 << format->bit_depth[2]) - 1;
00558 format->max_value_sq[2] = format->max_value[2] * format->max_value[2];
00559 }
00560
00568 void reset_format_info(seq_parameter_set_rbsp_t *sps, ImageParameters *p_Img, FrameFormat *source, FrameFormat *output)
00569 {
00570 InputParameters *p_Inp = p_Img->p_Inp;
00571 static const int SubWidthC [4]= { 1, 2, 2, 1};
00572 static const int SubHeightC [4]= { 1, 2, 1, 1};
00573
00574 int crop_left, crop_right;
00575 int crop_top, crop_bottom;
00576
00577
00578 if (sps->frame_cropping_flag)
00579 {
00580 crop_left = SubWidthC [sps->chroma_format_idc] * sps->frame_cropping_rect_left_offset;
00581 crop_right = SubWidthC [sps->chroma_format_idc] * sps->frame_cropping_rect_right_offset;
00582 crop_top = SubHeightC[sps->chroma_format_idc] * ( 2 - sps->frame_mbs_only_flag ) * sps->frame_cropping_rect_top_offset;
00583 crop_bottom = SubHeightC[sps->chroma_format_idc] * ( 2 - sps->frame_mbs_only_flag ) * sps->frame_cropping_rect_bottom_offset;
00584 }
00585 else
00586 {
00587 crop_left = crop_right = crop_top = crop_bottom = 0;
00588 }
00589
00590 source->width = p_Img->width - crop_left - crop_right;
00591 source->height = p_Img->height - crop_top - crop_bottom;
00592
00593
00594 if (sps->frame_cropping_flag)
00595 {
00596 crop_left = sps->frame_cropping_rect_left_offset;
00597 crop_right = sps->frame_cropping_rect_right_offset;
00598 crop_top = ( 2 - sps->frame_mbs_only_flag ) * sps->frame_cropping_rect_top_offset;
00599 crop_bottom = ( 2 - sps->frame_mbs_only_flag ) * sps->frame_cropping_rect_bottom_offset;
00600 }
00601 else
00602 {
00603 crop_left = crop_right = crop_top = crop_bottom = 0;
00604 }
00605
00606 if ((sps->chroma_format_idc==YUV400) && p_Inp->write_uv)
00607 {
00608 source->width_cr = (source->width >> 1);
00609 source->height_cr = (source->height >> 1);
00610 }
00611 else
00612 {
00613 source->width_cr = p_Img->width_cr - crop_left - crop_right;
00614 source->height_cr = p_Img->height_cr - crop_top - crop_bottom;
00615 }
00616
00617 output->width = p_Img->width;
00618 output->height = p_Img->height;
00619 output->width_cr = p_Img->width_cr;
00620 output->height_cr = p_Img->height_cr;
00621
00622 source->size_cmp[0] = source->width * source->height;
00623 source->size_cmp[1] = source->width_cr * source->height_cr;
00624 source->size_cmp[2] = source->size_cmp[1];
00625 source->size = source->size_cmp[0] + source->size_cmp[1] + source->size_cmp[2];
00626 source->mb_width = source->width / MB_BLOCK_SIZE;
00627 source->mb_height = source->height / MB_BLOCK_SIZE;
00628 source->pic_unit_size_on_disk = (imax(source->bit_depth[0], source->bit_depth[1]) > 8) ? 16 : 8;
00629 source->pic_unit_size_shift3 = source->pic_unit_size_on_disk >> 3;
00630
00631
00632 output->size_cmp[0] = output->width * output->height;
00633 output->size_cmp[1] = output->width_cr * output->height_cr;
00634 output->size_cmp[2] = output->size_cmp[1];
00635 output->size = output->size_cmp[0] + output->size_cmp[1] + output->size_cmp[2];
00636 output->mb_width = output->width / MB_BLOCK_SIZE;
00637 output->mb_height = output->height / MB_BLOCK_SIZE;
00638
00639
00640 output->bit_depth[0] = source->bit_depth[0] = p_Img->bitdepth_luma;
00641 output->bit_depth[1] = source->bit_depth[1] = p_Img->bitdepth_chroma;
00642 output->bit_depth[2] = source->bit_depth[2] = p_Img->bitdepth_chroma;
00643 output->pic_unit_size_on_disk = (imax(output->bit_depth[0], output->bit_depth[1]) > 8) ? 16 : 8;
00644 output->pic_unit_size_shift3 = output->pic_unit_size_on_disk >> 3;
00645
00646 output->frame_rate = source->frame_rate;
00647 output->color_model = source->color_model;
00648 output->yuv_format = source->yuv_format = (ColorFormat) sps->chroma_format_idc;
00649
00650 output->auto_crop_bottom = crop_bottom;
00651 output->auto_crop_right = crop_right;
00652 output->auto_crop_bottom_cr = (crop_bottom * p_Img->mb_cr_size_y) / MB_BLOCK_SIZE;
00653 output->auto_crop_right_cr = (crop_right * p_Img->mb_cr_size_x) / MB_BLOCK_SIZE;
00654
00655 source->auto_crop_bottom = output->auto_crop_bottom;
00656 source->auto_crop_right = output->auto_crop_right;
00657 source->auto_crop_bottom_cr = output->auto_crop_bottom_cr;
00658 source->auto_crop_right_cr = output->auto_crop_right_cr;
00659
00660 updateMaxValue(source);
00661 updateMaxValue(output);
00662 }
00663
00671 void activate_sps (ImageParameters *p_Img, seq_parameter_set_rbsp_t *sps)
00672 {
00673 InputParameters *p_Inp = p_Img->p_Inp;
00674
00675 if (p_Img->active_sps != sps)
00676 {
00677 if (p_Img->dec_picture)
00678 {
00679
00680 exit_picture(p_Img, &p_Img->dec_picture);
00681 }
00682 p_Img->active_sps = sps;
00683
00684 p_Img->bitdepth_chroma = 0;
00685 p_Img->width_cr = 0;
00686 p_Img->height_cr = 0;
00687
00688
00689 if (p_Img->active_sps->level_idc <= 10)
00690 {
00691 p_Img->max_vmv_r = 64 * 4;
00692 }
00693 else if (p_Img->active_sps->level_idc <= 20)
00694 {
00695 p_Img->max_vmv_r = 128 * 4;
00696 }
00697 else if (p_Img->active_sps->level_idc <= 30)
00698 {
00699 p_Img->max_vmv_r = 256 * 4;
00700 }
00701 else
00702 {
00703 p_Img->max_vmv_r = 512 * 4;
00704 }
00705
00706
00707 p_Img->bitdepth_luma = sps->bit_depth_luma_minus8 + 8;
00708 p_Img->bitdepth_scale[0] = 1 << sps->bit_depth_luma_minus8;
00709 if (sps->chroma_format_idc != YUV400)
00710 {
00711 p_Img->bitdepth_chroma = sps->bit_depth_chroma_minus8 + 8;
00712 p_Img->bitdepth_scale[1] = 1 << sps->bit_depth_chroma_minus8;
00713 }
00714
00715 p_Img->MaxFrameNum = 1<<(sps->log2_max_frame_num_minus4+4);
00716 p_Img->PicWidthInMbs = (sps->pic_width_in_mbs_minus1 +1);
00717 p_Img->PicHeightInMapUnits = (sps->pic_height_in_map_units_minus1 +1);
00718 p_Img->FrameHeightInMbs = ( 2 - sps->frame_mbs_only_flag ) * p_Img->PicHeightInMapUnits;
00719 p_Img->FrameSizeInMbs = p_Img->PicWidthInMbs * p_Img->FrameHeightInMbs;
00720
00721 p_Img->yuv_format=sps->chroma_format_idc;
00722
00723 p_Img->width = p_Img->PicWidthInMbs * MB_BLOCK_SIZE;
00724 p_Img->height = p_Img->FrameHeightInMbs * MB_BLOCK_SIZE;
00725
00726 if (sps->chroma_format_idc == YUV420)
00727 {
00728 p_Img->width_cr = (p_Img->width >> 1);
00729 p_Img->height_cr = (p_Img->height >> 1);
00730 }
00731 else if (sps->chroma_format_idc == YUV422)
00732 {
00733 p_Img->width_cr = (p_Img->width >> 1);
00734 p_Img->height_cr = p_Img->height;
00735 }
00736 else if (sps->chroma_format_idc == YUV444)
00737 {
00738
00739 p_Img->width_cr = p_Img->width;
00740 p_Img->height_cr = p_Img->height;
00741 }
00742
00743 init_frext(p_Img);
00744 init_global_buffers(p_Img);
00745
00746 if (!p_Img->no_output_of_prior_pics_flag)
00747 {
00748 flush_dpb(p_Img);
00749 }
00750 init_dpb(p_Img);
00751
00752 ercInit(p_Img, p_Img->width, p_Img->height, 1);
00753 }
00754
00755 reset_format_info(sps, p_Img, &p_Inp->source, &p_Inp->output);
00756
00757 }
00758
00759 void activate_pps(ImageParameters *p_Img, pic_parameter_set_rbsp_t *pps)
00760 {
00761 if (p_Img->active_pps != pps)
00762 {
00763 if (p_Img->dec_picture)
00764 {
00765
00766 exit_picture(p_Img, &p_Img->dec_picture);
00767 }
00768
00769 p_Img->active_pps = pps;
00770
00771
00772 p_Img->Transform8x8Mode = pps->transform_8x8_mode_flag;
00773
00774 }
00775 }
00776
00777 void UseParameterSet (Slice *currSlice, int PicParsetId)
00778 {
00779 ImageParameters *p_Img = currSlice->p_Img;
00780 seq_parameter_set_rbsp_t *sps = &p_Img->SeqParSet[p_Img->PicParSet[PicParsetId].seq_parameter_set_id];
00781 pic_parameter_set_rbsp_t *pps = &p_Img->PicParSet[PicParsetId];
00782 int i;
00783
00784 if (p_Img->PicParSet[PicParsetId].Valid != TRUE)
00785 printf ("Trying to use an invalid (uninitialized) Picture Parameter Set with ID %d, expect the unexpected...\n", PicParsetId);
00786 if (p_Img->SeqParSet[p_Img->PicParSet[PicParsetId].seq_parameter_set_id].Valid != TRUE)
00787 printf ("PicParset %d references an invalid (uninitialized) Sequence Parameter Set with ID %d, expect the unexpected...\n", PicParsetId, (int) p_Img->PicParSet[PicParsetId].seq_parameter_set_id);
00788
00789 sps = &p_Img->SeqParSet[p_Img->PicParSet[PicParsetId].seq_parameter_set_id];
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800 if ((int) sps->pic_order_cnt_type < 0 || sps->pic_order_cnt_type > 2)
00801 {
00802 printf ("invalid sps->pic_order_cnt_type = %d\n", (int) sps->pic_order_cnt_type);
00803 error ("pic_order_cnt_type != 1", -1000);
00804 }
00805
00806 if (sps->pic_order_cnt_type == 1)
00807 {
00808 if(sps->num_ref_frames_in_pic_order_cnt_cycle >= MAXnum_ref_frames_in_pic_order_cnt_cycle)
00809 {
00810 error("num_ref_frames_in_pic_order_cnt_cycle too large",-1011);
00811 }
00812 }
00813
00814 activate_sps(p_Img, sps);
00815 activate_pps(p_Img, pps);
00816
00817
00818 if (pps->entropy_coding_mode_flag == CAVLC)
00819 {
00820 currSlice->nal_startcode_follows = uvlc_startcode_follows;
00821 for (i=0; i<3; i++)
00822 {
00823 currSlice->partArr[i].readSyntaxElement = readSyntaxElement_UVLC;
00824 }
00825 }
00826 else
00827 {
00828 currSlice->nal_startcode_follows = cabac_startcode_follows;
00829 for (i=0; i<3; i++)
00830 {
00831 currSlice->partArr[i].readSyntaxElement = readSyntaxElement_CABAC;
00832 }
00833 }
00834 }
00835