Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2015 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ***************************************************************************** |
| 18 | * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore |
| 19 | */ |
| 20 | /*! |
| 21 | ************************************************************************** |
| 22 | * \file ih264d_parse_headers.c |
| 23 | * |
| 24 | * \brief |
| 25 | * Contains High level syntax[above slice] parsing routines |
| 26 | * |
| 27 | * \date |
| 28 | * 19/12/2002 |
| 29 | * |
| 30 | * \author AI |
| 31 | ************************************************************************** |
| 32 | */ |
| 33 | #include "ih264_typedefs.h" |
| 34 | #include "ih264_macros.h" |
| 35 | #include "ih264_platform_macros.h" |
| 36 | #include "ih264d_bitstrm.h" |
| 37 | #include "ih264d_structs.h" |
| 38 | #include "ih264d_parse_cavlc.h" |
| 39 | #include "ih264d_defs.h" |
| 40 | #include "ih264d_defs.h" |
| 41 | #include "ih264d_defs.h" |
| 42 | #include "ih264d_parse_slice.h" |
| 43 | #include "ih264d_tables.h" |
| 44 | #include "ih264d_utils.h" |
| 45 | #include "ih264d_nal.h" |
| 46 | #include "ih264d_deblocking.h" |
| 47 | |
| 48 | #include "ih264d_mem_request.h" |
| 49 | #include "ih264d_debug.h" |
| 50 | #include "ih264d_error_handler.h" |
| 51 | #include "ih264d_mb_utils.h" |
| 52 | #include "ih264d_sei.h" |
| 53 | #include "ih264d_vui.h" |
| 54 | #include "ih264d_thread_parse_decode.h" |
| 55 | #include "ih264d_thread_compute_bs.h" |
| 56 | #include "ih264d_quant_scaling.h" |
| 57 | #include "ih264d_defs.h" |
| 58 | #include "ivd.h" |
Marco Nelissen | 8ef4c3f | 2015-06-03 07:26:32 -0700 | [diff] [blame] | 59 | #include "ih264d.h" |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 60 | |
| 61 | /*****************************************************************************/ |
| 62 | /* */ |
| 63 | /* Function Name : ih264d_parse_slice_partition */ |
| 64 | /* */ |
| 65 | /* Description : This function is intended to parse and decode slice part */ |
| 66 | /* itions. Currently it's not implemented. Decoder will */ |
| 67 | /* print a message, skips this NAL and continues */ |
| 68 | /* Inputs : ps_dec Decoder parameters */ |
| 69 | /* ps_bitstrm Bitstream */ |
| 70 | /* Globals : None */ |
| 71 | /* Processing : This functionality needs to be implemented */ |
| 72 | /* Outputs : None */ |
| 73 | /* Returns : None */ |
| 74 | /* */ |
| 75 | /* Issues : Not implemented */ |
| 76 | /* */ |
| 77 | /* Revision History: */ |
| 78 | /* */ |
| 79 | /* DD MM YYYY Author(s) Changes (Describe the changes made) */ |
| 80 | /* 06 05 2002 NS Draft */ |
| 81 | /* */ |
| 82 | /*****************************************************************************/ |
| 83 | |
| 84 | WORD32 ih264d_parse_slice_partition(dec_struct_t * ps_dec, |
| 85 | dec_bit_stream_t * ps_bitstrm) |
| 86 | { |
| 87 | H264_DEC_DEBUG_PRINT("\nSlice partition not supported"); |
| 88 | UNUSED(ps_dec); |
| 89 | UNUSED(ps_bitstrm); |
| 90 | return (0); |
| 91 | } |
| 92 | |
| 93 | /*****************************************************************************/ |
| 94 | /* */ |
| 95 | /* Function Name : ih264d_parse_sei */ |
| 96 | /* */ |
| 97 | /* Description : This function is intended to parse and decode SEI */ |
| 98 | /* Currently it's not implemented. Decoder will print a */ |
| 99 | /* message, skips this NAL and continues */ |
| 100 | /* Inputs : ps_dec Decoder parameters */ |
| 101 | /* ps_bitstrm Bitstream */ |
| 102 | /* Globals : None */ |
| 103 | /* Processing : This functionality needs to be implemented */ |
| 104 | /* Outputs : None */ |
| 105 | /* Returns : None */ |
| 106 | /* */ |
| 107 | /* Issues : Not implemented */ |
| 108 | /* */ |
| 109 | /* Revision History: */ |
| 110 | /* */ |
| 111 | /* DD MM YYYY Author(s) Changes (Describe the changes made) */ |
| 112 | /* 06 05 2002 NS Draft */ |
| 113 | /* */ |
| 114 | /*****************************************************************************/ |
| 115 | WORD32 ih264d_parse_sei(dec_struct_t * ps_dec, dec_bit_stream_t * ps_bitstrm) |
| 116 | { |
| 117 | UNUSED(ps_dec); |
| 118 | UNUSED(ps_bitstrm); |
| 119 | return (0); |
| 120 | } |
| 121 | |
| 122 | /*****************************************************************************/ |
| 123 | /* */ |
| 124 | /* Function Name : ih264d_parse_filler_data */ |
| 125 | /* */ |
| 126 | /* Description : This function is intended to parse and decode filler */ |
| 127 | /* data NAL. Currently it's not implemented. Decoder will */ |
| 128 | /* print a message, skips this NAL and continues */ |
| 129 | /* Inputs : ps_dec Decoder parameters */ |
| 130 | /* ps_bitstrm Bitstream */ |
| 131 | /* Globals : None */ |
| 132 | /* Processing : This functionality needs to be implemented */ |
| 133 | /* Outputs : None */ |
| 134 | /* Returns : None */ |
| 135 | /* */ |
| 136 | /* Issues : Not implemented */ |
| 137 | /* */ |
| 138 | /* Revision History: */ |
| 139 | /* */ |
| 140 | /* DD MM YYYY Author(s) Changes (Describe the changes made) */ |
| 141 | /* 06 05 2002 NS Draft */ |
| 142 | /* */ |
| 143 | /*****************************************************************************/ |
| 144 | WORD32 ih264d_parse_filler_data(dec_struct_t * ps_dec, |
| 145 | dec_bit_stream_t * ps_bitstrm) |
| 146 | { |
| 147 | UNUSED(ps_dec); |
| 148 | UNUSED(ps_bitstrm); |
| 149 | return (0); |
| 150 | } |
| 151 | |
| 152 | /*****************************************************************************/ |
| 153 | /* */ |
| 154 | /* Function Name : ih264d_parse_end_of_stream */ |
| 155 | /* */ |
| 156 | /* Description : This function is intended to parse and decode end of */ |
| 157 | /* sequence. Currently it's not implemented. Decoder will */ |
| 158 | /* print a message, skips this NAL and continues */ |
| 159 | /* Inputs : ps_dec Decoder parameters */ |
| 160 | /* Globals : None */ |
| 161 | /* Processing : This functionality needs to be implemented */ |
| 162 | /* Outputs : None */ |
| 163 | /* Returns : None */ |
| 164 | /* */ |
| 165 | /* Issues : Not implemented */ |
| 166 | /* */ |
| 167 | /* Revision History: */ |
| 168 | /* */ |
| 169 | /* DD MM YYYY Author(s) Changes (Describe the changes made) */ |
| 170 | /* 06 05 2002 NS Draft */ |
| 171 | /* */ |
| 172 | /*****************************************************************************/ |
| 173 | void ih264d_parse_end_of_stream(dec_struct_t * ps_dec) |
| 174 | { |
| 175 | UNUSED(ps_dec); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | /*! |
| 180 | ************************************************************************** |
| 181 | * \if Function name : ih264d_parse_pps \endif |
| 182 | * |
| 183 | * \brief |
| 184 | * Decodes Picture Parameter set |
| 185 | * |
| 186 | * \return |
| 187 | * 0 on Success and Error code otherwise |
| 188 | ************************************************************************** |
| 189 | */ |
| 190 | WORD32 ih264d_parse_pps(dec_struct_t * ps_dec, dec_bit_stream_t * ps_bitstrm) |
| 191 | { |
| 192 | UWORD8 uc_temp; |
| 193 | dec_seq_params_t * ps_sps = NULL; |
| 194 | dec_pic_params_t * ps_pps = NULL; |
| 195 | UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer; |
| 196 | UWORD32 *pu4_bitstrm_ofst = &ps_dec->ps_bitstrm->u4_ofst; |
| 197 | |
| 198 | /* Variables used for error resilience checks */ |
| 199 | UWORD32 u4_temp; |
| 200 | WORD32 i_temp; |
| 201 | |
| 202 | /* For High profile related syntax elements */ |
| 203 | UWORD8 u1_more_data_flag; |
| 204 | WORD32 i4_i; |
| 205 | |
| 206 | /*--------------------------------------------------------------------*/ |
| 207 | /* Decode pic_parameter_set_id and find corresponding pic params */ |
| 208 | /*--------------------------------------------------------------------*/ |
| 209 | u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 210 | if(u4_temp & MASK_ERR_PIC_SET_ID) |
| 211 | return ERROR_INV_SPS_PPS_T; |
| 212 | ps_pps = ps_dec->pv_scratch_sps_pps; |
| 213 | *ps_pps = ps_dec->ps_pps[u4_temp]; |
| 214 | ps_pps->u1_pic_parameter_set_id = (WORD8)u4_temp; |
| 215 | COPYTHECONTEXT("PPS: pic_parameter_set_id",ps_pps->u1_pic_parameter_set_id); |
| 216 | |
| 217 | /************************************************/ |
| 218 | /* initilization of High profile syntax element */ |
| 219 | /************************************************/ |
| 220 | ps_pps->i4_transform_8x8_mode_flag = 0; |
| 221 | ps_pps->i4_pic_scaling_matrix_present_flag = 0; |
| 222 | |
| 223 | /*--------------------------------------------------------------------*/ |
| 224 | /* Decode seq_parameter_set_id and map it to a seq_parameter_set */ |
| 225 | /*--------------------------------------------------------------------*/ |
| 226 | u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 227 | if(u4_temp & MASK_ERR_SEQ_SET_ID) |
| 228 | return ERROR_INV_SPS_PPS_T; |
| 229 | COPYTHECONTEXT("PPS: seq_parameter_set_id",u4_temp); |
| 230 | ps_sps = &ps_dec->ps_sps[u4_temp]; |
| 231 | ps_pps->ps_sps = ps_sps; |
| 232 | |
| 233 | /*--------------------------------------------------------------------*/ |
| 234 | /* Decode entropy_coding_mode */ |
| 235 | /*--------------------------------------------------------------------*/ |
| 236 | ps_pps->u1_entropy_coding_mode = ih264d_get_bit_h264(ps_bitstrm); |
| 237 | COPYTHECONTEXT("PPS: entropy_coding_mode_flag",ps_pps->u1_entropy_coding_mode); |
| 238 | |
| 239 | ps_pps->u1_pic_order_present_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 240 | COPYTHECONTEXT("PPS: pic_order_present_flag",ps_pps->u1_pic_order_present_flag); |
| 241 | |
| 242 | /*--------------------------------------------------------------------*/ |
| 243 | /* Decode num_slice_groups_minus1 */ |
| 244 | /*--------------------------------------------------------------------*/ |
| 245 | u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf) + 1; |
| 246 | if(u4_temp != 1) |
| 247 | { |
| 248 | UWORD32 i4_error_code; |
| 249 | i4_error_code = ERROR_FEATURE_UNAVAIL; |
| 250 | return i4_error_code; |
| 251 | } |
| 252 | ps_pps->u1_num_slice_groups = u4_temp; |
| 253 | COPYTHECONTEXT("PPS: num_slice_groups_minus1",ps_pps->u1_num_slice_groups -1); |
| 254 | |
| 255 | /*--------------------------------------------------------------------*/ |
| 256 | /* Other parameter set values */ |
| 257 | /*--------------------------------------------------------------------*/ |
| 258 | u4_temp = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 259 | if(u4_temp > H264_MAX_REF_IDX) |
| 260 | return ERROR_REF_IDX; |
| 261 | ps_pps->u1_num_ref_idx_lx_active[0] = u4_temp; |
| 262 | COPYTHECONTEXT("PPS: num_ref_idx_l0_active_minus1", |
| 263 | ps_pps->u1_num_ref_idx_lx_active[0] - 1); |
| 264 | |
| 265 | u4_temp = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 266 | if(u4_temp > H264_MAX_REF_IDX) |
| 267 | return ERROR_REF_IDX; |
| 268 | ps_pps->u1_num_ref_idx_lx_active[1] = u4_temp; |
| 269 | COPYTHECONTEXT("PPS: num_ref_idx_l1_active_minus1", |
| 270 | ps_pps->u1_num_ref_idx_lx_active[1] - 1); |
| 271 | |
| 272 | ps_pps->u1_wted_pred_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 273 | COPYTHECONTEXT("PPS: weighted prediction u4_flag",ps_pps->u1_wted_pred_flag); |
| 274 | uc_temp = ih264d_get_bits_h264(ps_bitstrm, 2); |
| 275 | COPYTHECONTEXT("PPS: weighted_bipred_idc",uc_temp); |
| 276 | ps_pps->u1_wted_bipred_idc = uc_temp; |
| 277 | |
| 278 | if(ps_pps->u1_wted_bipred_idc > MAX_WEIGHT_BIPRED_IDC) |
| 279 | return ERROR_INV_SPS_PPS_T; |
| 280 | |
| 281 | i_temp = 26 + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 282 | |
| 283 | if((i_temp < 0) || (i_temp > 51)) |
| 284 | return ERROR_INV_RANGE_QP_T; |
| 285 | |
| 286 | ps_pps->u1_pic_init_qp = i_temp; |
| 287 | COPYTHECONTEXT("PPS: pic_init_qp_minus26",ps_pps->u1_pic_init_qp - 26); |
| 288 | |
| 289 | i_temp = 26 + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 290 | |
| 291 | if((i_temp < 0) || (i_temp > 51)) |
| 292 | return ERROR_INV_RANGE_QP_T; |
| 293 | |
| 294 | ps_pps->u1_pic_init_qs = i_temp; |
| 295 | COPYTHECONTEXT("PPS: pic_init_qs_minus26",ps_pps->u1_pic_init_qs - 26); |
| 296 | |
| 297 | i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 298 | if((i_temp < -12) || (i_temp > 12)) |
| 299 | return ERROR_INV_RANGE_QP_T; |
| 300 | ps_pps->i1_chroma_qp_index_offset = i_temp; |
| 301 | COPYTHECONTEXT("PPS: chroma_qp_index_offset",ps_pps->i1_chroma_qp_index_offset); |
| 302 | |
| 303 | /***************************************************************************/ |
| 304 | /* initialize second_chroma_qp_index_offset to i1_chroma_qp_index_offset if */ |
| 305 | /* second_chroma_qp_index_offset is not present in bit-ps_bitstrm */ |
| 306 | /***************************************************************************/ |
| 307 | ps_pps->i1_second_chroma_qp_index_offset = |
| 308 | ps_pps->i1_chroma_qp_index_offset; |
| 309 | |
| 310 | ps_pps->u1_deblocking_filter_parameters_present_flag = ih264d_get_bit_h264( |
| 311 | ps_bitstrm); |
| 312 | COPYTHECONTEXT("PPS: deblocking_filter_control_present_flag", |
| 313 | ps_pps->u1_deblocking_filter_parameters_present_flag); |
| 314 | ps_pps->u1_constrained_intra_pred_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 315 | COPYTHECONTEXT("PPS: constrained_intra_pred_flag", |
| 316 | ps_pps->u1_constrained_intra_pred_flag); |
| 317 | ps_pps->u1_redundant_pic_cnt_present_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 318 | COPYTHECONTEXT("PPS: redundant_pic_cnt_present_flag", |
| 319 | ps_pps->u1_redundant_pic_cnt_present_flag); |
| 320 | |
| 321 | /* High profile related syntax elements */ |
| 322 | u1_more_data_flag = MORE_RBSP_DATA(ps_bitstrm); |
| 323 | if(u1_more_data_flag && (ps_pps->ps_sps->u1_profile_idc == HIGH_PROFILE_IDC)) |
| 324 | { |
| 325 | /* read transform_8x8_mode_flag */ |
| 326 | ps_pps->i4_transform_8x8_mode_flag = (WORD32)ih264d_get_bit_h264( |
| 327 | ps_bitstrm); |
| 328 | |
| 329 | /* read pic_scaling_matrix_present_flag */ |
| 330 | ps_pps->i4_pic_scaling_matrix_present_flag = |
| 331 | (WORD32)ih264d_get_bit_h264(ps_bitstrm); |
| 332 | |
| 333 | if(ps_pps->i4_pic_scaling_matrix_present_flag) |
| 334 | { |
| 335 | /* read the scaling matrices */ |
Marco Nelissen | 8ef4c3f | 2015-06-03 07:26:32 -0700 | [diff] [blame] | 336 | for(i4_i = 0; i4_i < (6 + (ps_pps->i4_transform_8x8_mode_flag << 1)); i4_i++) |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 337 | { |
| 338 | ps_pps->u1_pic_scaling_list_present_flag[i4_i] = |
| 339 | ih264d_get_bit_h264(ps_bitstrm); |
| 340 | |
| 341 | if(ps_pps->u1_pic_scaling_list_present_flag[i4_i]) |
| 342 | { |
| 343 | if(i4_i < 6) |
| 344 | { |
| 345 | ih264d_scaling_list( |
| 346 | ps_pps->i2_pic_scalinglist4x4[i4_i], |
| 347 | 16, |
| 348 | &ps_pps->u1_pic_use_default_scaling_matrix_flag[i4_i], |
| 349 | ps_bitstrm); |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | ih264d_scaling_list( |
| 354 | ps_pps->i2_pic_scalinglist8x8[i4_i - 6], |
| 355 | 64, |
| 356 | &ps_pps->u1_pic_use_default_scaling_matrix_flag[i4_i], |
| 357 | ps_bitstrm); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* read second_chroma_qp_index_offset syntax element */ |
| 364 | ps_pps->i1_second_chroma_qp_index_offset = ih264d_sev( |
| 365 | pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 366 | |
| 367 | if((ps_pps->i1_second_chroma_qp_index_offset + 12) > 24) |
| 368 | return ERROR_INV_RANGE_QP_T; |
| 369 | } |
| 370 | |
Harish Mahendrakar | 2ee0c1b | 2015-09-18 15:15:50 +0530 | [diff] [blame] | 371 | /* In case bitstream read has exceeded the filled size, then |
| 372 | return an error */ |
| 373 | if(ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst + 8) |
| 374 | { |
| 375 | return ERROR_INV_SPS_PPS_T; |
| 376 | } |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 377 | ps_pps->u1_is_valid = TRUE; |
| 378 | ps_dec->ps_pps[ps_pps->u1_pic_parameter_set_id] = *ps_pps; |
| 379 | return OK; |
| 380 | } |
| 381 | |
| 382 | /*! |
| 383 | ************************************************************************** |
| 384 | * \if Function name : ih264d_parse_sps \endif |
| 385 | * |
| 386 | * \brief |
| 387 | * Decodes Sequence parameter set from the bitstream |
| 388 | * |
| 389 | * \return |
| 390 | * 0 on Success and Error code otherwise |
| 391 | ************************************************************************** |
| 392 | */ |
| 393 | UWORD32 ih264d_correct_level_idc(UWORD32 u4_level_idc, UWORD32 u4_total_mbs) |
| 394 | { |
| 395 | UWORD32 u4_max_mbs_allowed; |
| 396 | |
| 397 | switch(u4_level_idc) |
| 398 | { |
| 399 | case H264_LEVEL_1_0: |
| 400 | u4_max_mbs_allowed = MAX_MBS_LEVEL_10; |
| 401 | break; |
| 402 | case H264_LEVEL_1_1: |
| 403 | u4_max_mbs_allowed = MAX_MBS_LEVEL_11; |
| 404 | break; |
| 405 | case H264_LEVEL_1_2: |
| 406 | u4_max_mbs_allowed = MAX_MBS_LEVEL_12; |
| 407 | break; |
| 408 | case H264_LEVEL_1_3: |
| 409 | u4_max_mbs_allowed = MAX_MBS_LEVEL_13; |
| 410 | break; |
| 411 | case H264_LEVEL_2_0: |
| 412 | u4_max_mbs_allowed = MAX_MBS_LEVEL_20; |
| 413 | break; |
| 414 | case H264_LEVEL_2_1: |
| 415 | u4_max_mbs_allowed = MAX_MBS_LEVEL_21; |
| 416 | break; |
| 417 | case H264_LEVEL_2_2: |
| 418 | u4_max_mbs_allowed = MAX_MBS_LEVEL_22; |
| 419 | break; |
| 420 | case H264_LEVEL_3_0: |
| 421 | u4_max_mbs_allowed = MAX_MBS_LEVEL_30; |
| 422 | break; |
| 423 | case H264_LEVEL_3_1: |
| 424 | u4_max_mbs_allowed = MAX_MBS_LEVEL_31; |
| 425 | break; |
| 426 | case H264_LEVEL_3_2: |
| 427 | u4_max_mbs_allowed = MAX_MBS_LEVEL_32; |
| 428 | break; |
| 429 | case H264_LEVEL_4_0: |
| 430 | u4_max_mbs_allowed = MAX_MBS_LEVEL_40; |
| 431 | break; |
| 432 | case H264_LEVEL_4_1: |
| 433 | u4_max_mbs_allowed = MAX_MBS_LEVEL_41; |
| 434 | break; |
| 435 | case H264_LEVEL_4_2: |
| 436 | u4_max_mbs_allowed = MAX_MBS_LEVEL_42; |
| 437 | break; |
| 438 | case H264_LEVEL_5_0: |
| 439 | u4_max_mbs_allowed = MAX_MBS_LEVEL_50; |
| 440 | break; |
| 441 | case H264_LEVEL_5_1: |
| 442 | default: |
| 443 | u4_max_mbs_allowed = MAX_MBS_LEVEL_51; |
| 444 | break; |
| 445 | |
| 446 | } |
| 447 | |
| 448 | /*correct of the level is incorrect*/ |
| 449 | if(u4_total_mbs > u4_max_mbs_allowed) |
| 450 | { |
| 451 | if(u4_total_mbs > MAX_MBS_LEVEL_50) |
| 452 | u4_level_idc = H264_LEVEL_5_1; |
| 453 | else if(u4_total_mbs > MAX_MBS_LEVEL_42) |
| 454 | u4_level_idc = H264_LEVEL_5_0; |
| 455 | else if(u4_total_mbs > MAX_MBS_LEVEL_41) |
| 456 | u4_level_idc = H264_LEVEL_4_2; |
| 457 | else if(u4_total_mbs > MAX_MBS_LEVEL_40) |
| 458 | u4_level_idc = H264_LEVEL_4_1; |
| 459 | else if(u4_total_mbs > MAX_MBS_LEVEL_32) |
| 460 | u4_level_idc = H264_LEVEL_4_0; |
| 461 | else if(u4_total_mbs > MAX_MBS_LEVEL_31) |
| 462 | u4_level_idc = H264_LEVEL_3_2; |
| 463 | else if(u4_total_mbs > MAX_MBS_LEVEL_30) |
| 464 | u4_level_idc = H264_LEVEL_3_1; |
| 465 | else if(u4_total_mbs > MAX_MBS_LEVEL_21) |
| 466 | u4_level_idc = H264_LEVEL_3_0; |
| 467 | else if(u4_total_mbs > MAX_MBS_LEVEL_20) |
| 468 | u4_level_idc = H264_LEVEL_2_1; |
| 469 | else if(u4_total_mbs > MAX_MBS_LEVEL_10) |
| 470 | u4_level_idc = H264_LEVEL_2_0; |
| 471 | } |
| 472 | |
| 473 | return (u4_level_idc); |
| 474 | |
| 475 | } |
| 476 | WORD32 ih264d_parse_sps(dec_struct_t *ps_dec, dec_bit_stream_t *ps_bitstrm) |
| 477 | { |
| 478 | UWORD8 i; |
| 479 | dec_seq_params_t *ps_seq = NULL; |
| 480 | UWORD8 u1_profile_idc, u1_level_idc, u1_seq_parameter_set_id; |
| 481 | UWORD16 i2_max_frm_num; |
| 482 | UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer; |
| 483 | UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst; |
| 484 | UWORD8 u1_frm, uc_constraint_set0_flag, uc_constraint_set1_flag; |
| 485 | |
| 486 | UWORD32 u4_temp; |
| 487 | WORD32 pic_height_in_map_units_minus1 = 0; |
| 488 | UWORD32 u2_pic_wd = 0; |
| 489 | UWORD32 u2_pic_ht = 0; |
| 490 | UWORD32 u2_frm_wd_y = 0; |
| 491 | UWORD32 u2_frm_ht_y = 0; |
| 492 | UWORD32 u2_frm_wd_uv = 0; |
| 493 | UWORD32 u2_frm_ht_uv = 0; |
| 494 | UWORD32 u2_crop_offset_y = 0; |
| 495 | UWORD32 u2_crop_offset_uv = 0; |
| 496 | WORD32 ret; |
Wonsik Kim | 142221a | 2016-04-12 15:20:08 +0900 | [diff] [blame] | 497 | UWORD32 u4_num_reorder_frames; |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 498 | /* High profile related syntax element */ |
| 499 | WORD32 i4_i; |
| 500 | /* G050 */ |
| 501 | UWORD8 u1_frame_cropping_flag, u1_frame_cropping_rect_left_ofst, |
| 502 | u1_frame_cropping_rect_right_ofst, |
| 503 | u1_frame_cropping_rect_top_ofst, |
| 504 | u1_frame_cropping_rect_bottom_ofst; |
| 505 | /* G050 */ |
| 506 | /*--------------------------------------------------------------------*/ |
| 507 | /* Decode seq_parameter_set_id and profile and level values */ |
| 508 | /*--------------------------------------------------------------------*/ |
| 509 | SWITCHONTRACE; |
| 510 | u1_profile_idc = ih264d_get_bits_h264(ps_bitstrm, 8); |
| 511 | COPYTHECONTEXT("SPS: profile_idc",u1_profile_idc); |
| 512 | |
| 513 | /* G050 */ |
| 514 | uc_constraint_set0_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 515 | uc_constraint_set1_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 516 | ih264d_get_bit_h264(ps_bitstrm); |
| 517 | |
| 518 | /*****************************************************/ |
| 519 | /* Read 5 bits for uc_constraint_set3_flag (1 bit) */ |
| 520 | /* and reserved_zero_4bits (4 bits) - Sushant */ |
| 521 | /*****************************************************/ |
| 522 | ih264d_get_bits_h264(ps_bitstrm, 5); |
| 523 | /* G050 */ |
| 524 | |
| 525 | /* Check whether particular profile is suported or not */ |
| 526 | /* Check whether particular profile is suported or not */ |
| 527 | if((u1_profile_idc != MAIN_PROFILE_IDC) && |
| 528 | |
| 529 | (u1_profile_idc != BASE_PROFILE_IDC) && |
| 530 | |
| 531 | (u1_profile_idc != HIGH_PROFILE_IDC) |
| 532 | |
| 533 | ) |
| 534 | { |
| 535 | |
Harish Mahendrakar | d7eee55 | 2016-12-05 13:36:19 +0530 | [diff] [blame] | 536 | /* Apart from Baseline, main and high profile, |
| 537 | * only extended profile is supported provided |
| 538 | * uc_constraint_set0_flag or uc_constraint_set1_flag are set to 1 |
| 539 | */ |
| 540 | if((u1_profile_idc != EXTENDED_PROFILE_IDC) || |
| 541 | ((uc_constraint_set1_flag != 1) && (uc_constraint_set0_flag != 1))) |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 542 | { |
Harish Mahendrakar | d7eee55 | 2016-12-05 13:36:19 +0530 | [diff] [blame] | 543 | return (ERROR_FEATURE_UNAVAIL); |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | u1_level_idc = ih264d_get_bits_h264(ps_bitstrm, 8); |
| 548 | |
Marco Nelissen | 8ef4c3f | 2015-06-03 07:26:32 -0700 | [diff] [blame] | 549 | |
Marco Nelissen | 8ef4c3f | 2015-06-03 07:26:32 -0700 | [diff] [blame] | 550 | |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 551 | COPYTHECONTEXT("SPS: u4_level_idc",u1_level_idc); |
| 552 | |
| 553 | u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 554 | if(u4_temp & MASK_ERR_SEQ_SET_ID) |
| 555 | return ERROR_INV_SPS_PPS_T; |
| 556 | u1_seq_parameter_set_id = u4_temp; |
| 557 | COPYTHECONTEXT("SPS: seq_parameter_set_id", |
| 558 | u1_seq_parameter_set_id); |
| 559 | |
| 560 | /*--------------------------------------------------------------------*/ |
| 561 | /* Find an seq param entry in seqparam array of decStruct */ |
| 562 | /*--------------------------------------------------------------------*/ |
| 563 | |
| 564 | ps_seq = ps_dec->pv_scratch_sps_pps; |
| 565 | *ps_seq = ps_dec->ps_sps[u1_seq_parameter_set_id]; |
Harish Mahendrakar | 251b007 | 2015-08-04 09:55:15 +0530 | [diff] [blame] | 566 | |
| 567 | if(NULL == ps_dec->ps_cur_sps) |
| 568 | ps_dec->ps_cur_sps = ps_seq; |
| 569 | |
Hamsalekha S | 8454dbf | 2017-02-21 16:02:23 +0530 | [diff] [blame^] | 570 | if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_profile_idc != u1_profile_idc)) |
Wonsik Kim | 142221a | 2016-04-12 15:20:08 +0900 | [diff] [blame] | 571 | { |
| 572 | ps_dec->u1_res_changed = 1; |
| 573 | return IVD_RES_CHANGED; |
| 574 | } |
| 575 | |
Hamsalekha S | 8454dbf | 2017-02-21 16:02:23 +0530 | [diff] [blame^] | 576 | if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_level_idc != u1_level_idc)) |
Wonsik Kim | 142221a | 2016-04-12 15:20:08 +0900 | [diff] [blame] | 577 | { |
| 578 | ps_dec->u1_res_changed = 1; |
| 579 | return IVD_RES_CHANGED; |
| 580 | } |
| 581 | |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 582 | ps_seq->u1_profile_idc = u1_profile_idc; |
| 583 | ps_seq->u1_level_idc = u1_level_idc; |
| 584 | ps_seq->u1_seq_parameter_set_id = u1_seq_parameter_set_id; |
| 585 | |
| 586 | /*******************************************************************/ |
| 587 | /* Initializations for high profile - Sushant */ |
| 588 | /*******************************************************************/ |
| 589 | ps_seq->i4_chroma_format_idc = 1; |
| 590 | ps_seq->i4_bit_depth_luma_minus8 = 0; |
| 591 | ps_seq->i4_bit_depth_chroma_minus8 = 0; |
| 592 | ps_seq->i4_qpprime_y_zero_transform_bypass_flag = 0; |
| 593 | ps_seq->i4_seq_scaling_matrix_present_flag = 0; |
| 594 | if(u1_profile_idc == HIGH_PROFILE_IDC) |
| 595 | { |
| 596 | |
| 597 | /* reading chroma_format_idc */ |
| 598 | ps_seq->i4_chroma_format_idc = ih264d_uev(pu4_bitstrm_ofst, |
| 599 | pu4_bitstrm_buf); |
| 600 | |
| 601 | /* Monochrome is not supported */ |
| 602 | if(ps_seq->i4_chroma_format_idc != 1) |
| 603 | { |
| 604 | return ERROR_INV_SPS_PPS_T; |
| 605 | } |
| 606 | |
| 607 | /* reading bit_depth_luma_minus8 */ |
| 608 | ps_seq->i4_bit_depth_luma_minus8 = ih264d_uev(pu4_bitstrm_ofst, |
| 609 | pu4_bitstrm_buf); |
| 610 | |
| 611 | if(ps_seq->i4_bit_depth_luma_minus8 != 0) |
| 612 | { |
| 613 | return ERROR_INV_SPS_PPS_T; |
| 614 | } |
| 615 | |
| 616 | /* reading bit_depth_chroma_minus8 */ |
| 617 | ps_seq->i4_bit_depth_chroma_minus8 = ih264d_uev(pu4_bitstrm_ofst, |
| 618 | pu4_bitstrm_buf); |
| 619 | |
| 620 | if(ps_seq->i4_bit_depth_chroma_minus8 != 0) |
| 621 | { |
| 622 | return ERROR_INV_SPS_PPS_T; |
| 623 | } |
| 624 | |
| 625 | /* reading qpprime_y_zero_transform_bypass_flag */ |
| 626 | ps_seq->i4_qpprime_y_zero_transform_bypass_flag = |
| 627 | (WORD32)ih264d_get_bit_h264(ps_bitstrm); |
| 628 | |
| 629 | if(ps_seq->i4_qpprime_y_zero_transform_bypass_flag != 0) |
| 630 | { |
| 631 | return ERROR_INV_SPS_PPS_T; |
| 632 | } |
| 633 | |
| 634 | /* reading seq_scaling_matrix_present_flag */ |
| 635 | ps_seq->i4_seq_scaling_matrix_present_flag = |
| 636 | (WORD32)ih264d_get_bit_h264(ps_bitstrm); |
| 637 | |
| 638 | if(ps_seq->i4_seq_scaling_matrix_present_flag) |
| 639 | { |
| 640 | for(i4_i = 0; i4_i < 8; i4_i++) |
| 641 | { |
| 642 | ps_seq->u1_seq_scaling_list_present_flag[i4_i] = |
| 643 | ih264d_get_bit_h264(ps_bitstrm); |
| 644 | |
| 645 | /* initialize u1_use_default_scaling_matrix_flag[i4_i] to zero */ |
| 646 | /* before calling scaling list */ |
| 647 | ps_seq->u1_use_default_scaling_matrix_flag[i4_i] = 0; |
| 648 | |
| 649 | if(ps_seq->u1_seq_scaling_list_present_flag[i4_i]) |
| 650 | { |
| 651 | if(i4_i < 6) |
| 652 | { |
| 653 | ih264d_scaling_list( |
| 654 | ps_seq->i2_scalinglist4x4[i4_i], |
| 655 | 16, |
| 656 | &ps_seq->u1_use_default_scaling_matrix_flag[i4_i], |
| 657 | ps_bitstrm); |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | ih264d_scaling_list( |
| 662 | ps_seq->i2_scalinglist8x8[i4_i - 6], |
| 663 | 64, |
| 664 | &ps_seq->u1_use_default_scaling_matrix_flag[i4_i], |
| 665 | ps_bitstrm); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | /*--------------------------------------------------------------------*/ |
| 672 | /* Decode MaxFrameNum */ |
| 673 | /*--------------------------------------------------------------------*/ |
| 674 | u4_temp = 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 675 | if(u4_temp > MAX_BITS_IN_FRAME_NUM) |
| 676 | { |
| 677 | return ERROR_INV_SPS_PPS_T; |
| 678 | } |
| 679 | ps_seq->u1_bits_in_frm_num = u4_temp; |
| 680 | COPYTHECONTEXT("SPS: log2_max_frame_num_minus4", |
| 681 | (ps_seq->u1_bits_in_frm_num - 4)); |
| 682 | |
| 683 | i2_max_frm_num = (1 << (ps_seq->u1_bits_in_frm_num)); |
| 684 | ps_seq->u2_u4_max_pic_num_minus1 = i2_max_frm_num - 1; |
| 685 | /*--------------------------------------------------------------------*/ |
| 686 | /* Decode picture order count and related values */ |
| 687 | /*--------------------------------------------------------------------*/ |
| 688 | u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 689 | |
| 690 | if(u4_temp > MAX_PIC_ORDER_CNT_TYPE) |
| 691 | { |
| 692 | return ERROR_INV_POC_TYPE_T; |
| 693 | } |
| 694 | ps_seq->u1_pic_order_cnt_type = u4_temp; |
| 695 | COPYTHECONTEXT("SPS: pic_order_cnt_type",ps_seq->u1_pic_order_cnt_type); |
| 696 | |
| 697 | ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = 1; |
| 698 | if(ps_seq->u1_pic_order_cnt_type == 0) |
| 699 | { |
| 700 | u4_temp = 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 701 | if(u4_temp > MAX_BITS_IN_POC_LSB) |
| 702 | { |
| 703 | return ERROR_INV_SPS_PPS_T; |
| 704 | } |
| 705 | ps_seq->u1_log2_max_pic_order_cnt_lsb_minus = u4_temp; |
| 706 | ps_seq->i4_max_pic_order_cntLsb = (1 << u4_temp); |
| 707 | COPYTHECONTEXT("SPS: log2_max_pic_order_cnt_lsb_minus4",(u4_temp - 4)); |
| 708 | } |
| 709 | else if(ps_seq->u1_pic_order_cnt_type == 1) |
| 710 | { |
| 711 | ps_seq->u1_delta_pic_order_always_zero_flag = ih264d_get_bit_h264( |
| 712 | ps_bitstrm); |
| 713 | COPYTHECONTEXT("SPS: delta_pic_order_always_zero_flag", |
| 714 | ps_seq->u1_delta_pic_order_always_zero_flag); |
| 715 | |
| 716 | ps_seq->i4_ofst_for_non_ref_pic = ih264d_sev(pu4_bitstrm_ofst, |
| 717 | pu4_bitstrm_buf); |
| 718 | COPYTHECONTEXT("SPS: offset_for_non_ref_pic", |
| 719 | ps_seq->i4_ofst_for_non_ref_pic); |
| 720 | |
| 721 | ps_seq->i4_ofst_for_top_to_bottom_field = ih264d_sev( |
| 722 | pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 723 | COPYTHECONTEXT("SPS: offset_for_top_to_bottom_field", |
| 724 | ps_seq->i4_ofst_for_top_to_bottom_field); |
| 725 | |
| 726 | u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 727 | if(u4_temp > 255) |
| 728 | return ERROR_INV_SPS_PPS_T; |
| 729 | ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = u4_temp; |
| 730 | COPYTHECONTEXT("SPS: num_ref_frames_in_pic_order_cnt_cycle", |
| 731 | ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle); |
| 732 | |
| 733 | for(i = 0; i < ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle; i++) |
| 734 | { |
| 735 | ps_seq->i4_ofst_for_ref_frame[i] = ih264d_sev( |
| 736 | pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 737 | COPYTHECONTEXT("SPS: offset_for_ref_frame", |
| 738 | ps_seq->i4_ofst_for_ref_frame[i]); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 743 | |
| 744 | if((u4_temp > H264_MAX_REF_PICS)) |
| 745 | { |
| 746 | return ERROR_NUM_REF; |
| 747 | } |
Wonsik Kim | 142221a | 2016-04-12 15:20:08 +0900 | [diff] [blame] | 748 | |
| 749 | /* Compare with older num_ref_frames is header is already once */ |
Hamsalekha S | 8454dbf | 2017-02-21 16:02:23 +0530 | [diff] [blame^] | 750 | if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_num_ref_frames != u4_temp)) |
Wonsik Kim | 142221a | 2016-04-12 15:20:08 +0900 | [diff] [blame] | 751 | { |
| 752 | ps_dec->u1_res_changed = 1; |
| 753 | return IVD_RES_CHANGED; |
| 754 | } |
| 755 | |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 756 | ps_seq->u1_num_ref_frames = u4_temp; |
| 757 | COPYTHECONTEXT("SPS: num_ref_frames",ps_seq->u1_num_ref_frames); |
| 758 | |
| 759 | ps_seq->u1_gaps_in_frame_num_value_allowed_flag = ih264d_get_bit_h264( |
| 760 | ps_bitstrm); |
| 761 | COPYTHECONTEXT("SPS: gaps_in_frame_num_value_allowed_flag", |
| 762 | ps_seq->u1_gaps_in_frame_num_value_allowed_flag); |
| 763 | |
| 764 | /*--------------------------------------------------------------------*/ |
| 765 | /* Decode FrameWidth and FrameHeight and related values */ |
| 766 | /*--------------------------------------------------------------------*/ |
| 767 | ps_seq->u2_frm_wd_in_mbs = 1 |
| 768 | + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf); |
| 769 | COPYTHECONTEXT("SPS: pic_width_in_mbs_minus1", |
| 770 | ps_seq->u2_frm_wd_in_mbs - 1); |
| 771 | u2_pic_wd = (ps_seq->u2_frm_wd_in_mbs << 4); |
| 772 | |
| 773 | pic_height_in_map_units_minus1 = ih264d_uev(pu4_bitstrm_ofst, |
| 774 | pu4_bitstrm_buf); |
| 775 | ps_seq->u2_frm_ht_in_mbs = 1 + pic_height_in_map_units_minus1; |
| 776 | |
| 777 | u2_pic_ht = (ps_seq->u2_frm_ht_in_mbs << 4); |
| 778 | |
| 779 | /*--------------------------------------------------------------------*/ |
| 780 | /* Get the value of MaxMbAddress and Number of bits needed for it */ |
| 781 | /*--------------------------------------------------------------------*/ |
| 782 | ps_seq->u2_max_mb_addr = (ps_seq->u2_frm_wd_in_mbs |
| 783 | * ps_seq->u2_frm_ht_in_mbs) - 1; |
| 784 | |
| 785 | ps_seq->u2_total_num_of_mbs = ps_seq->u2_max_mb_addr + 1; |
| 786 | |
| 787 | ps_seq->u1_level_idc = ih264d_correct_level_idc( |
| 788 | u1_level_idc, ps_seq->u2_total_num_of_mbs); |
| 789 | |
| 790 | u1_frm = ih264d_get_bit_h264(ps_bitstrm); |
| 791 | ps_seq->u1_frame_mbs_only_flag = u1_frm; |
| 792 | |
| 793 | COPYTHECONTEXT("SPS: frame_mbs_only_flag", u1_frm); |
| 794 | |
| 795 | if(!u1_frm) |
| 796 | { |
| 797 | u2_pic_ht <<= 1; |
| 798 | ps_seq->u1_mb_aff_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 799 | COPYTHECONTEXT("SPS: mb_adaptive_frame_field_flag", |
| 800 | ps_seq->u1_mb_aff_flag); |
| 801 | |
| 802 | } |
| 803 | else |
| 804 | ps_seq->u1_mb_aff_flag = 0; |
| 805 | |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 806 | ps_seq->u1_direct_8x8_inference_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 807 | |
| 808 | COPYTHECONTEXT("SPS: direct_8x8_inference_flag", |
| 809 | ps_seq->u1_direct_8x8_inference_flag); |
| 810 | |
| 811 | /* G050 */ |
| 812 | u1_frame_cropping_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 813 | COPYTHECONTEXT("SPS: frame_cropping_flag",u1_frame_cropping_flag); |
| 814 | |
| 815 | if(u1_frame_cropping_flag) |
| 816 | { |
| 817 | u1_frame_cropping_rect_left_ofst = ih264d_uev(pu4_bitstrm_ofst, |
| 818 | pu4_bitstrm_buf); |
| 819 | COPYTHECONTEXT("SPS: frame_cropping_rect_left_offset", |
| 820 | u1_frame_cropping_rect_left_ofst); |
| 821 | u1_frame_cropping_rect_right_ofst = ih264d_uev(pu4_bitstrm_ofst, |
| 822 | pu4_bitstrm_buf); |
| 823 | COPYTHECONTEXT("SPS: frame_cropping_rect_right_offset", |
| 824 | u1_frame_cropping_rect_right_ofst); |
| 825 | u1_frame_cropping_rect_top_ofst = ih264d_uev(pu4_bitstrm_ofst, |
| 826 | pu4_bitstrm_buf); |
| 827 | COPYTHECONTEXT("SPS: frame_cropping_rect_top_offset", |
| 828 | u1_frame_cropping_rect_top_ofst); |
| 829 | u1_frame_cropping_rect_bottom_ofst = ih264d_uev(pu4_bitstrm_ofst, |
| 830 | pu4_bitstrm_buf); |
| 831 | COPYTHECONTEXT("SPS: frame_cropping_rect_bottom_offset", |
| 832 | u1_frame_cropping_rect_bottom_ofst); |
| 833 | } |
| 834 | /* G050 */ |
| 835 | |
| 836 | ps_seq->u1_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm); |
| 837 | COPYTHECONTEXT("SPS: vui_parameters_present_flag", |
| 838 | ps_seq->u1_vui_parameters_present_flag); |
| 839 | |
| 840 | u2_frm_wd_y = u2_pic_wd + (UWORD8)(PAD_LEN_Y_H << 1); |
| 841 | if(1 == ps_dec->u4_share_disp_buf) |
| 842 | { |
| 843 | if(ps_dec->u4_app_disp_width > u2_frm_wd_y) |
| 844 | u2_frm_wd_y = ps_dec->u4_app_disp_width; |
| 845 | } |
| 846 | |
| 847 | u2_frm_ht_y = u2_pic_ht + (UWORD8)(PAD_LEN_Y_V << 2); |
| 848 | u2_frm_wd_uv = u2_pic_wd + (UWORD8)(PAD_LEN_UV_H << 2); |
| 849 | u2_frm_wd_uv = MAX(u2_frm_wd_uv, u2_frm_wd_y); |
| 850 | |
| 851 | u2_frm_ht_uv = (u2_pic_ht >> 1) + (UWORD8)(PAD_LEN_UV_V << 2); |
| 852 | u2_frm_ht_uv = MAX(u2_frm_ht_uv, (u2_frm_ht_y >> 1)); |
| 853 | |
| 854 | |
| 855 | /* Calculate display picture width, height and start u4_ofst from YUV420 */ |
| 856 | /* pictute buffers as per cropping information parsed above */ |
| 857 | { |
| 858 | UWORD16 u2_rgt_ofst = 0; |
| 859 | UWORD16 u2_lft_ofst = 0; |
| 860 | UWORD16 u2_top_ofst = 0; |
| 861 | UWORD16 u2_btm_ofst = 0; |
| 862 | UWORD8 u1_frm_mbs_flag; |
| 863 | UWORD8 u1_vert_mult_factor; |
| 864 | WORD32 i4_cropped_ht, i4_cropped_wd; |
| 865 | |
| 866 | if(u1_frame_cropping_flag) |
| 867 | { |
| 868 | /* Calculate right and left u4_ofst for cropped picture */ |
| 869 | u2_rgt_ofst = u1_frame_cropping_rect_right_ofst << 1; |
| 870 | u2_lft_ofst = u1_frame_cropping_rect_left_ofst << 1; |
| 871 | |
| 872 | /* Know frame MBs only u4_flag */ |
| 873 | u1_frm_mbs_flag = (1 == ps_seq->u1_frame_mbs_only_flag); |
| 874 | |
| 875 | /* Simplify the vertical u4_ofst calculation from field/frame */ |
| 876 | u1_vert_mult_factor = (2 - u1_frm_mbs_flag); |
| 877 | |
| 878 | /* Calculate bottom and top u4_ofst for cropped picture */ |
| 879 | u2_btm_ofst = (u1_frame_cropping_rect_bottom_ofst |
| 880 | << u1_vert_mult_factor); |
| 881 | u2_top_ofst = (u1_frame_cropping_rect_top_ofst |
| 882 | << u1_vert_mult_factor); |
| 883 | } |
| 884 | |
| 885 | /* Calculate u4_ofst from start of YUV 420 picture buffer to start of*/ |
| 886 | /* cropped picture buffer */ |
| 887 | u2_crop_offset_y = (u2_frm_wd_y * u2_top_ofst) + (u2_lft_ofst); |
| 888 | u2_crop_offset_uv = (u2_frm_wd_uv * (u2_top_ofst >> 1)) |
| 889 | + (u2_lft_ofst >> 1) * YUV420SP_FACTOR; |
| 890 | /* Calculate the display picture width and height based on crop */ |
| 891 | /* information */ |
| 892 | i4_cropped_ht = u2_pic_ht - (u2_btm_ofst + u2_top_ofst); |
| 893 | i4_cropped_wd = u2_pic_wd - (u2_rgt_ofst + u2_lft_ofst); |
| 894 | |
| 895 | if((i4_cropped_ht < MB_SIZE) || (i4_cropped_wd < MB_SIZE)) |
| 896 | { |
| 897 | return ERROR_INV_SPS_PPS_T; |
| 898 | } |
| 899 | |
Hamsalekha S | 8454dbf | 2017-02-21 16:02:23 +0530 | [diff] [blame^] | 900 | if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_pic_wd != u2_pic_wd)) |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 901 | { |
| 902 | ps_dec->u1_res_changed = 1; |
| 903 | return IVD_RES_CHANGED; |
| 904 | } |
Hamsalekha S | 8454dbf | 2017-02-21 16:02:23 +0530 | [diff] [blame^] | 905 | if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_pic_ht != u2_pic_ht)) |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 906 | { |
| 907 | ps_dec->u1_res_changed = 1; |
| 908 | return IVD_RES_CHANGED; |
| 909 | } |
| 910 | |
Harish Mahendrakar | 5351d3a | 2015-10-06 09:03:25 +0530 | [diff] [blame] | 911 | /* Check for unsupported resolutions */ |
| 912 | if((u2_pic_wd > H264_MAX_FRAME_WIDTH) || (u2_pic_ht > H264_MAX_FRAME_HEIGHT)) |
| 913 | { |
| 914 | return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED; |
| 915 | } |
| 916 | |
Harish Mahendrakar | 69b5191 | 2017-01-17 13:13:41 +0530 | [diff] [blame] | 917 | /* If MBAff is enabled, decoder support is limited to streams with |
| 918 | * width less than half of H264_MAX_FRAME_WIDTH. |
| 919 | * In case of MBAff decoder processes two rows at a time |
| 920 | */ |
| 921 | if((u2_pic_wd << ps_seq->u1_mb_aff_flag) > H264_MAX_FRAME_WIDTH) |
| 922 | { |
| 923 | return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED; |
| 924 | } |
| 925 | |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 926 | ps_dec->u2_disp_height = i4_cropped_ht; |
| 927 | |
| 928 | ps_dec->u2_disp_width = i4_cropped_wd; |
| 929 | |
| 930 | } |
| 931 | |
Wonsik Kim | 142221a | 2016-04-12 15:20:08 +0900 | [diff] [blame] | 932 | /* Backup u4_num_reorder_frames if header is already decoded */ |
Hamsalekha S | 8454dbf | 2017-02-21 16:02:23 +0530 | [diff] [blame^] | 933 | if((ps_dec->i4_header_decoded & 1) && |
Wonsik Kim | 142221a | 2016-04-12 15:20:08 +0900 | [diff] [blame] | 934 | (1 == ps_seq->u1_vui_parameters_present_flag) && |
| 935 | (1 == ps_seq->s_vui.u1_bitstream_restriction_flag)) |
| 936 | { |
| 937 | u4_num_reorder_frames = ps_seq->s_vui.u4_num_reorder_frames; |
| 938 | } |
| 939 | else |
| 940 | { |
| 941 | u4_num_reorder_frames = -1; |
| 942 | } |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 943 | if(1 == ps_seq->u1_vui_parameters_present_flag) |
| 944 | { |
| 945 | ret = ih264d_parse_vui_parametres(&ps_seq->s_vui, ps_bitstrm); |
| 946 | if(ret != OK) |
| 947 | return ret; |
| 948 | } |
| 949 | |
Wonsik Kim | 142221a | 2016-04-12 15:20:08 +0900 | [diff] [blame] | 950 | /* Compare older u4_num_reorder_frames with the new one if header is already decoded */ |
Hamsalekha S | 8454dbf | 2017-02-21 16:02:23 +0530 | [diff] [blame^] | 951 | if((ps_dec->i4_header_decoded & 1) && |
Wonsik Kim | 142221a | 2016-04-12 15:20:08 +0900 | [diff] [blame] | 952 | (-1 != (WORD32)u4_num_reorder_frames) && |
| 953 | (1 == ps_seq->u1_vui_parameters_present_flag) && |
| 954 | (1 == ps_seq->s_vui.u1_bitstream_restriction_flag) && |
| 955 | (ps_seq->s_vui.u4_num_reorder_frames != u4_num_reorder_frames)) |
| 956 | { |
| 957 | ps_dec->u1_res_changed = 1; |
| 958 | return IVD_RES_CHANGED; |
| 959 | } |
| 960 | |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 961 | ps_dec->u2_pic_wd = u2_pic_wd; |
| 962 | ps_dec->u2_pic_ht = u2_pic_ht; |
| 963 | |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 964 | /* Determining the Width and Height of Frame from that of Picture */ |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 965 | ps_dec->u2_frm_wd_y = u2_frm_wd_y; |
| 966 | ps_dec->u2_frm_ht_y = u2_frm_ht_y; |
| 967 | |
| 968 | ps_dec->u2_frm_wd_uv = u2_frm_wd_uv; |
| 969 | ps_dec->u2_frm_ht_uv = u2_frm_ht_uv; |
| 970 | ps_dec->s_pad_mgr.u1_pad_len_y_v = (UWORD8)(PAD_LEN_Y_V << (1 - u1_frm)); |
| 971 | ps_dec->s_pad_mgr.u1_pad_len_cr_v = (UWORD8)(PAD_LEN_UV_V << (1 - u1_frm)); |
| 972 | |
| 973 | ps_dec->u2_frm_wd_in_mbs = ps_seq->u2_frm_wd_in_mbs; |
| 974 | ps_dec->u2_frm_ht_in_mbs = ps_seq->u2_frm_ht_in_mbs; |
| 975 | |
| 976 | ps_dec->u2_crop_offset_y = u2_crop_offset_y; |
| 977 | ps_dec->u2_crop_offset_uv = u2_crop_offset_uv; |
| 978 | |
Harish Mahendrakar | 2ee0c1b | 2015-09-18 15:15:50 +0530 | [diff] [blame] | 979 | /* In case bitstream read has exceeded the filled size, then |
| 980 | return an error */ |
| 981 | if(ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst) |
| 982 | { |
| 983 | return ERROR_INV_SPS_PPS_T; |
| 984 | } |
| 985 | ps_seq->u1_is_valid = TRUE; |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 986 | ps_dec->ps_sps[u1_seq_parameter_set_id] = *ps_seq; |
| 987 | |
| 988 | return OK; |
| 989 | } |
| 990 | |
| 991 | /*! |
| 992 | ************************************************************************** |
| 993 | * \if Function name : ih264d_parse_end_of_sequence \endif |
| 994 | * |
| 995 | * \brief |
| 996 | * Decodes End of Sequence. |
| 997 | * |
| 998 | * \param ps_bitstrm : Pointer to bit ps_bitstrm containing the NAL unit |
| 999 | * |
| 1000 | * \return |
| 1001 | * 0 on Success and error code otherwise |
| 1002 | ************************************************************************** |
| 1003 | */ |
| 1004 | WORD32 ih264d_parse_end_of_sequence(dec_struct_t * ps_dec) |
| 1005 | { |
| 1006 | WORD32 ret; |
| 1007 | |
| 1008 | ret = ih264d_end_of_pic_processing(ps_dec); |
| 1009 | return ret; |
| 1010 | } |
| 1011 | |
| 1012 | /*! |
| 1013 | ************************************************************************** |
| 1014 | * \if Function name : AcessUnitDelimiterRbsp \endif |
| 1015 | * |
| 1016 | * \brief |
| 1017 | * Decodes AcessUnitDelimiterRbsp. |
| 1018 | * |
| 1019 | * \param ps_bitstrm : Pointer to bit ps_bitstrm containing the NAL unit |
| 1020 | * |
| 1021 | * \return |
| 1022 | * 0 on Success and error code otherwise |
| 1023 | ************************************************************************** |
| 1024 | */ |
| 1025 | |
| 1026 | WORD32 ih264d_access_unit_delimiter_rbsp(dec_struct_t * ps_dec) |
| 1027 | { |
| 1028 | UWORD8 u1_primary_pic_type; |
| 1029 | u1_primary_pic_type = ih264d_get_bits_h264(ps_dec->ps_bitstrm, 3); |
| 1030 | switch(u1_primary_pic_type) |
| 1031 | { |
| 1032 | case I_PIC: |
| 1033 | case SI_PIC: |
| 1034 | case ISI_PIC: |
| 1035 | ps_dec->ps_dec_err_status->u1_pic_aud_i = PIC_TYPE_I; |
| 1036 | break; |
| 1037 | default: |
| 1038 | ps_dec->ps_dec_err_status->u1_pic_aud_i = PIC_TYPE_UNKNOWN; |
| 1039 | } |
| 1040 | return (0); |
| 1041 | } |
| 1042 | /*! |
| 1043 | ************************************************************************** |
| 1044 | * \if Function name : ih264d_parse_nal_unit \endif |
| 1045 | * |
| 1046 | * \brief |
| 1047 | * Decodes NAL unit |
| 1048 | * |
| 1049 | * \return |
| 1050 | * 0 on Success and error code otherwise |
| 1051 | ************************************************************************** |
| 1052 | */ |
| 1053 | |
| 1054 | WORD32 ih264d_parse_nal_unit(iv_obj_t *dec_hdl, |
| 1055 | ivd_video_decode_op_t *ps_dec_op, |
| 1056 | UWORD8 *pu1_buf, |
| 1057 | UWORD32 u4_length) |
| 1058 | { |
| 1059 | |
| 1060 | dec_bit_stream_t *ps_bitstrm; |
| 1061 | |
| 1062 | |
| 1063 | dec_struct_t *ps_dec = (dec_struct_t *)dec_hdl->pv_codec_handle; |
| 1064 | ivd_video_decode_ip_t *ps_dec_in = |
| 1065 | (ivd_video_decode_ip_t *)ps_dec->pv_dec_in; |
| 1066 | dec_slice_params_t * ps_cur_slice = ps_dec->ps_cur_slice; |
| 1067 | UWORD8 u1_first_byte, u1_nal_ref_idc; |
| 1068 | UWORD8 u1_nal_unit_type; |
| 1069 | WORD32 i_status = OK; |
| 1070 | ps_bitstrm = ps_dec->ps_bitstrm; |
| 1071 | |
| 1072 | if(pu1_buf) |
| 1073 | { |
| 1074 | if(u4_length) |
| 1075 | { |
| 1076 | ps_dec_op->u4_frame_decoded_flag = 0; |
| 1077 | ih264d_process_nal_unit(ps_dec->ps_bitstrm, pu1_buf, |
| 1078 | u4_length); |
| 1079 | |
| 1080 | SWITCHOFFTRACE; |
| 1081 | u1_first_byte = ih264d_get_bits_h264(ps_bitstrm, 8); |
| 1082 | |
| 1083 | if(NAL_FORBIDDEN_BIT(u1_first_byte)) |
| 1084 | { |
| 1085 | H264_DEC_DEBUG_PRINT("\nForbidden bit set in Nal Unit, Let's try\n"); |
| 1086 | } |
| 1087 | u1_nal_unit_type = NAL_UNIT_TYPE(u1_first_byte); |
Harish Mahendrakar | 5e4f64c | 2016-06-01 13:31:45 +0530 | [diff] [blame] | 1088 | // if any other nal unit other than slice nal is encountered in between a |
| 1089 | // frame break out of loop without consuming header |
Harish Mahendrakar | d1896de | 2016-12-20 12:16:31 +0530 | [diff] [blame] | 1090 | if ((ps_dec->u4_slice_start_code_found == 1) |
| 1091 | && (ps_dec->u1_pic_decode_done != 1) |
| 1092 | && (u1_nal_unit_type > IDR_SLICE_NAL)) |
Harish Mahendrakar | 5e4f64c | 2016-06-01 13:31:45 +0530 | [diff] [blame] | 1093 | { |
| 1094 | return ERROR_INCOMPLETE_FRAME; |
| 1095 | } |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 1096 | ps_dec->u1_nal_unit_type = u1_nal_unit_type; |
| 1097 | u1_nal_ref_idc = (UWORD8)(NAL_REF_IDC(u1_first_byte)); |
| 1098 | //Skip all NALUs if SPS and PPS are not decoded |
| 1099 | switch(u1_nal_unit_type) |
| 1100 | { |
| 1101 | case SLICE_DATA_PARTITION_A_NAL: |
| 1102 | case SLICE_DATA_PARTITION_B_NAL: |
| 1103 | case SLICE_DATA_PARTITION_C_NAL: |
| 1104 | if(!ps_dec->i4_decode_header) |
| 1105 | ih264d_parse_slice_partition(ps_dec, ps_bitstrm); |
| 1106 | |
| 1107 | break; |
| 1108 | |
| 1109 | case IDR_SLICE_NAL: |
| 1110 | case SLICE_NAL: |
| 1111 | |
| 1112 | /* ! */ |
| 1113 | DEBUG_THREADS_PRINTF("Decoding a slice NAL\n"); |
| 1114 | if(!ps_dec->i4_decode_header) |
| 1115 | { |
| 1116 | if(ps_dec->i4_header_decoded == 3) |
| 1117 | { |
| 1118 | /* ! */ |
| 1119 | ps_dec->u4_slice_start_code_found = 1; |
| 1120 | |
| 1121 | ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm); |
| 1122 | |
| 1123 | i_status = ih264d_parse_decode_slice( |
| 1124 | (UWORD8)(u1_nal_unit_type |
| 1125 | == IDR_SLICE_NAL), |
| 1126 | u1_nal_ref_idc, ps_dec); |
| 1127 | |
| 1128 | if(i_status != OK) |
Hamsalekha S | 9f0dcba | 2015-06-09 15:54:31 +0530 | [diff] [blame] | 1129 | { |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 1130 | return i_status; |
Hamsalekha S | 9f0dcba | 2015-06-09 15:54:31 +0530 | [diff] [blame] | 1131 | } |
Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 1132 | } |
| 1133 | else |
| 1134 | { |
| 1135 | H264_DEC_DEBUG_PRINT( |
| 1136 | "\nSlice NAL Supplied but no header has been supplied\n"); |
| 1137 | } |
| 1138 | } |
| 1139 | break; |
| 1140 | |
| 1141 | case SEI_NAL: |
| 1142 | if(!ps_dec->i4_decode_header) |
| 1143 | { |
| 1144 | ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm); |
| 1145 | i_status = ih264d_parse_sei_message(ps_dec, ps_bitstrm); |
| 1146 | if(i_status != OK) |
| 1147 | return i_status; |
| 1148 | ih264d_parse_sei(ps_dec, ps_bitstrm); |
| 1149 | } |
| 1150 | break; |
| 1151 | case SEQ_PARAM_NAL: |
| 1152 | /* ! */ |
| 1153 | ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm); |
| 1154 | i_status = ih264d_parse_sps(ps_dec, ps_bitstrm); |
| 1155 | if(i_status == ERROR_INV_SPS_PPS_T) |
| 1156 | return i_status; |
| 1157 | if(!i_status) |
| 1158 | ps_dec->i4_header_decoded |= 0x1; |
| 1159 | break; |
| 1160 | |
| 1161 | case PIC_PARAM_NAL: |
| 1162 | /* ! */ |
| 1163 | ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm); |
| 1164 | i_status = ih264d_parse_pps(ps_dec, ps_bitstrm); |
| 1165 | if(i_status == ERROR_INV_SPS_PPS_T) |
| 1166 | return i_status; |
| 1167 | if(!i_status) |
| 1168 | ps_dec->i4_header_decoded |= 0x2; |
| 1169 | break; |
| 1170 | case ACCESS_UNIT_DELIMITER_RBSP: |
| 1171 | if(!ps_dec->i4_decode_header) |
| 1172 | { |
| 1173 | ih264d_access_unit_delimiter_rbsp(ps_dec); |
| 1174 | } |
| 1175 | break; |
| 1176 | //Let us ignore the END_OF_SEQ_RBSP NAL and decode even after this NAL |
| 1177 | case END_OF_STREAM_RBSP: |
| 1178 | if(!ps_dec->i4_decode_header) |
| 1179 | { |
| 1180 | ih264d_parse_end_of_stream(ps_dec); |
| 1181 | } |
| 1182 | break; |
| 1183 | case FILLER_DATA_NAL: |
| 1184 | if(!ps_dec->i4_decode_header) |
| 1185 | { |
| 1186 | ih264d_parse_filler_data(ps_dec, ps_bitstrm); |
| 1187 | } |
| 1188 | break; |
| 1189 | default: |
| 1190 | H264_DEC_DEBUG_PRINT("\nUnknown NAL type %d\n", u1_nal_unit_type); |
| 1191 | break; |
| 1192 | } |
| 1193 | |
| 1194 | } |
| 1195 | |
| 1196 | } |
| 1197 | |
| 1198 | return i_status; |
| 1199 | |
| 1200 | } |
| 1201 | |