• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright (C) 2022 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
23  *  isvcd_parse_headers.c
24  *
25  * @brief
26  *  Contains High level syntax[above slice] parsing routines
27  *
28  * @author
29  *  Kishore
30  *
31  * @par List of Functions:
32  *  - isvcd_set_default_seq_svc_ext()
33  *  - isvcd_parse_subset_sps()
34  *  - isvcd_dec_ref_base_pic_marking()
35  *  - isvcd_parse_nal_unit()
36  *  - isvcd_parse_sps()
37  *  - isvcd_parse_pps()
38  *
39  * @remarks
40  *  None
41  *
42  *******************************************************************************
43  */
44 
45 #include <string.h>
46 #include <assert.h>
47 
48 #include "ih264_typedefs.h"
49 #include "ih264_defs.h"
50 #include "ih264_macros.h"
51 #include "ih264_platform_macros.h"
52 #include "ih264d_bitstrm.h"
53 #include "isvcd_structs.h"
54 #include "ih264d_parse_cavlc.h"
55 #include "ih264d_defs.h"
56 #include "ih264d_parse_slice.h"
57 #include "ih264d_tables.h"
58 #include "ih264d_utils.h"
59 #include "ih264d_nal.h"
60 #include "ih264d_deblocking.h"
61 #include "ih264d_mem_request.h"
62 #include "ih264d_debug.h"
63 #include "ih264_debug.h"
64 #include "ih264d_error_handler.h"
65 #include "ih264d_mb_utils.h"
66 #include "ih264d_sei.h"
67 #include "ih264d_vui.h"
68 #include "ih264d_thread_parse_decode.h"
69 #include "ih264d_thread_compute_bs.h"
70 #include "ih264d_quant_scaling.h"
71 #include "ih264d_defs.h"
72 #include "ivd.h"
73 #include "ih264d_parse_islice.h"
74 #include "isvcd_parse_slice.h"
75 #include "ih264d_process_bslice.h"
76 #include "ih264d_process_pslice.h"
77 #include "isvcd_vui.h"
78 
79 WORD32 ih264d_access_unit_delimiter_rbsp(dec_struct_t *ps_dec);
80 void ih264d_get_pre_sei_params(dec_struct_t *ps_dec, UWORD8 u1_nal_unit_type);
81 UWORD32 ih264d_correct_level_idc(UWORD32 u4_level_idc, UWORD32 u4_total_mbs);
82 WORD32 ih264d_parse_filler_data(dec_struct_t *ps_dec, dec_bit_stream_t *ps_bitstrm);
83 void ih264d_parse_end_of_stream(dec_struct_t *ps_dec);
84 WORD32 ih264d_parse_slice_partition(dec_struct_t *ps_dec, dec_bit_stream_t *ps_bitstrm);
85 /*!
86 **************************************************************************
87 * \if Function name : isvcd_set_default_seq_svc_ext \en
88 dif
89 *
90 * \brief
91 *    Sets the default values for the svc params in the SVC bitstream
92 *
93 * \return
94 **************************************************************************
95 */
isvcd_set_default_seq_svc_ext(dec_subset_seq_params_t * ps_seq_svc_ext)96 void isvcd_set_default_seq_svc_ext(dec_subset_seq_params_t *ps_seq_svc_ext)
97 {
98     ps_seq_svc_ext->u1_inter_layer_deblocking_filter_control_present_flag = 0;
99     ps_seq_svc_ext->u1_extended_spatial_scalability_idc = 0;
100     ps_seq_svc_ext->u1_chroma_phase_x_plus1_flag = 1;
101     ps_seq_svc_ext->u1_chroma_phase_y_plus1 = 1;
102     ps_seq_svc_ext->u1_seq_ref_layer_chroma_phase_x_plus1_flag =
103         ps_seq_svc_ext->u1_chroma_phase_x_plus1_flag;
104     ps_seq_svc_ext->u1_seq_ref_layer_chroma_phase_y_plus1 = ps_seq_svc_ext->u1_chroma_phase_y_plus1;
105     ps_seq_svc_ext->i4_seq_scaled_ref_layer_left_offset = 0;
106     ps_seq_svc_ext->i4_seq_scaled_ref_layer_top_offset = 0;
107     ps_seq_svc_ext->i4_seq_scaled_ref_layer_right_offset = 0;
108     ps_seq_svc_ext->i4_seq_scaled_ref_layer_bottom_offset = 0;
109     ps_seq_svc_ext->u1_seq_tcoeff_level_prediction_flag =
110         ps_seq_svc_ext->u1_adaptive_tcoeff_level_prediction_flag = 0;
111     ps_seq_svc_ext->u1_slice_header_restriction_flag = 0;
112     ps_seq_svc_ext->u1_svc_vui_parameters_present_flag = 0;
113 }
114 /*!
115 **************************************************************************
116 * \if Function name : isvcd_parse_subset_sps \en
117 dif
118 *
119 * \brief
120 *    Decodes Sequence parameter set from the SVC bitstream
121 *
122 * \return
123 *    0 on Success and Error code otherwise
124 **************************************************************************
125 */
isvcd_parse_subset_sps(svc_dec_lyr_struct_t * ps_svc_lyr_dec,dec_bit_stream_t * ps_bitstrm)126 WORD32 isvcd_parse_subset_sps(svc_dec_lyr_struct_t *ps_svc_lyr_dec, dec_bit_stream_t *ps_bitstrm)
127 {
128     dec_struct_t *ps_dec = &ps_svc_lyr_dec->s_dec;
129     UWORD8 i;
130     dec_seq_params_t *ps_seq = NULL;
131     dec_svc_seq_params_t *ps_subset_seq = NULL;
132     dec_subset_seq_params_t *ps_seq_svc_ext;
133     UWORD8 u1_profile_idc, u1_level_idc, u1_seq_parameter_set_id, u1_mb_aff_flag = 0;
134     UWORD16 i2_max_frm_num;
135     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
136     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
137     UWORD8 u1_frm, uc_constraint_set0_flag, uc_constraint_set1_flag, uc_constraint_set2_flag;
138     WORD32 i4_cropped_ht, i4_cropped_wd;
139     UWORD32 u4_temp;
140     UWORD64 u8_temp;
141     UWORD32 u4_pic_height_in_map_units, u4_pic_width_in_mbs;
142     UWORD32 u2_pic_wd = 0;
143     UWORD32 u2_pic_ht = 0;
144     UWORD32 u2_frm_wd_y = 0;
145     UWORD32 u2_frm_ht_y = 0;
146     UWORD32 u2_frm_wd_uv = 0;
147     UWORD32 u2_frm_ht_uv = 0;
148     UWORD32 u2_crop_offset_y = 0;
149     UWORD32 u2_crop_offset_uv = 0;
150     WORD32 ret;
151     /* High profile related syntax element */
152     WORD32 i4_i;
153     /* G050 */
154     UWORD8 u1_frame_cropping_flag,
155         u1_frame_cropping_rect_left_ofst = 0, u1_frame_cropping_rect_right_ofst = 0,
156         u1_frame_cropping_rect_top_ofst = 0, u1_frame_cropping_rect_bottom_ofst = 0;
157     /* G050 */
158     /*--------------------------------------------------------------------*/
159     /* Decode seq_parameter_set_id and profile and level values           */
160     /*--------------------------------------------------------------------*/
161     SWITCHONTRACE;
162     u1_profile_idc = ih264d_get_bits_h264(ps_bitstrm, 8);
163     COPYTHECONTEXT("SPS: profile_idc", u1_profile_idc);
164 
165     /* G050 */
166     uc_constraint_set0_flag = ih264d_get_bit_h264(ps_bitstrm);
167     uc_constraint_set1_flag = ih264d_get_bit_h264(ps_bitstrm);
168     uc_constraint_set2_flag = ih264d_get_bit_h264(ps_bitstrm);
169     UNUSED(uc_constraint_set1_flag);
170     UNUSED(uc_constraint_set2_flag);
171 
172     /*****************************************************/
173     /* Read 5 bits for uc_constraint_set3_flag (1 bit)   */
174     /* and reserved_zero_4bits (4 bits) - Sushant        */
175     /*****************************************************/
176     ih264d_get_bits_h264(ps_bitstrm, 5);
177     /* G050 */
178     u1_level_idc = (UWORD8) ih264d_get_bits_h264(ps_bitstrm, 8);
179     COPYTHECONTEXT("SPS: u4_level_idc", u1_level_idc);
180 
181     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
182     if(u4_temp & MASK_ERR_SEQ_SET_ID) return ERROR_INV_SPS_PPS_T;
183     u1_seq_parameter_set_id = u4_temp;
184     COPYTHECONTEXT("SPS: seq_parameter_set_id", u1_seq_parameter_set_id);
185 
186     if(u1_seq_parameter_set_id >= MAX_NUM_SEQ_PARAMS) return ERROR_INV_SPS_PPS_T;
187 
188     /*--------------------------------------------------------------------*/
189     /* Find an seq param entry in seqparam array of decStruct             */
190     /*--------------------------------------------------------------------*/
191     ps_subset_seq = ps_svc_lyr_dec->pv_scratch_subset_sps;
192     memset(ps_subset_seq, 0, sizeof(dec_svc_seq_params_t));
193     ps_seq = ps_dec->pv_scratch_sps_pps;
194     memset(ps_seq, 0, sizeof(dec_seq_params_t));
195 
196     if(ps_dec->i4_header_decoded & 1)
197     {
198         if(NULL != ps_dec->ps_cur_sps)
199             *ps_seq = *ps_dec->ps_cur_sps;
200         else
201             return ERROR_INV_SPS_PPS_T;
202     }
203 
204     ps_seq->u1_profile_idc = u1_profile_idc;
205     ps_seq->u1_level_idc = u1_level_idc;
206     ps_seq->u1_seq_parameter_set_id = u1_seq_parameter_set_id;
207 
208     /* subset_seq_sps_will be stored from location 32 : MAX_NUM_SEQ_PARAMS*/
209     u1_seq_parameter_set_id += MAX_NUM_SEQ_PARAMS;
210     ps_subset_seq->ps_seq = &ps_dec->ps_sps[u1_seq_parameter_set_id];
211 
212     if((ps_dec->i4_header_decoded & 1) &&
213        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
214        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_profile_idc != u1_profile_idc))
215     {
216         ps_dec->u1_res_changed = 1;
217         return IVD_RES_CHANGED;
218     }
219 
220     if((ps_dec->i4_header_decoded & 1) &&
221        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
222        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_level_idc != u1_level_idc))
223     {
224         ps_dec->u1_res_changed = 1;
225         return IVD_RES_CHANGED;
226     }
227     /*******************************************************************/
228     /* Initializations for high profile - Sushant                      */
229     /*******************************************************************/
230     ps_seq->i4_chroma_format_idc = 1;
231     ps_seq->i4_bit_depth_luma_minus8 = 0;
232     ps_seq->i4_bit_depth_chroma_minus8 = 0;
233     ps_seq->i4_qpprime_y_zero_transform_bypass_flag = 0;
234     ps_seq->i4_seq_scaling_matrix_present_flag = 0;
235     if(u1_profile_idc == HIGH_PROFILE_IDC || u1_profile_idc == SCALABLE_BASELINE_PROFILE_IDC ||
236        u1_profile_idc == SCALABLE_HIGH_PROFILE_IDC)
237     {
238         /* reading chroma_format_idc   */
239         ps_seq->i4_chroma_format_idc = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
240 
241         /* Monochrome is not supported */
242         if(ps_seq->i4_chroma_format_idc != 1)
243         {
244             return ERROR_FEATURE_UNAVAIL;
245         }
246 
247         /* reading bit_depth_luma_minus8   */
248         ps_seq->i4_bit_depth_luma_minus8 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
249 
250         if(ps_seq->i4_bit_depth_luma_minus8 != 0)
251         {
252             return ERROR_FEATURE_UNAVAIL;
253         }
254 
255         /* reading bit_depth_chroma_minus8   */
256         ps_seq->i4_bit_depth_chroma_minus8 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
257 
258         if(ps_seq->i4_bit_depth_chroma_minus8 != 0)
259         {
260             return ERROR_FEATURE_UNAVAIL;
261         }
262 
263         /* reading qpprime_y_zero_transform_bypass_flag   */
264         ps_seq->i4_qpprime_y_zero_transform_bypass_flag = (WORD32) ih264d_get_bit_h264(ps_bitstrm);
265 
266         if(ps_seq->i4_qpprime_y_zero_transform_bypass_flag != 0)
267         {
268             return ERROR_INV_SPS_PPS_T;
269         }
270 
271         /* reading seq_scaling_matrix_present_flag   */
272         ps_seq->i4_seq_scaling_matrix_present_flag = (WORD32) ih264d_get_bit_h264(ps_bitstrm);
273 
274         if(ps_seq->i4_seq_scaling_matrix_present_flag)
275         {
276             for(i4_i = 0; i4_i < 8; i4_i++)
277             {
278                 ps_seq->u1_seq_scaling_list_present_flag[i4_i] = ih264d_get_bit_h264(ps_bitstrm);
279 
280                 /* initialize u1_use_default_scaling_matrix_flag[i4_i] to zero */
281                 /* before calling scaling list                             */
282                 ps_seq->u1_use_default_scaling_matrix_flag[i4_i] = 0;
283 
284                 if(ps_seq->u1_seq_scaling_list_present_flag[i4_i])
285                 {
286                     if(i4_i < 6)
287                     {
288                         ret = ih264d_scaling_list(ps_seq->i2_scalinglist4x4[i4_i], 16,
289                                                   &ps_seq->u1_use_default_scaling_matrix_flag[i4_i],
290                                                   ps_bitstrm);
291                     }
292                     else
293                     {
294                         ret = ih264d_scaling_list(ps_seq->i2_scalinglist8x8[i4_i - 6], 64,
295                                                   &ps_seq->u1_use_default_scaling_matrix_flag[i4_i],
296                                                   ps_bitstrm);
297                     }
298                     if(ret != OK)
299                     {
300                         return ret;
301                     }
302                 }
303             }
304         }
305     }
306     /*--------------------------------------------------------------------*/
307     /* Decode MaxFrameNum                                                 */
308     /*--------------------------------------------------------------------*/
309     u8_temp = (UWORD64) 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
310     if(u8_temp > MAX_BITS_IN_FRAME_NUM)
311     {
312         return ERROR_INV_SPS_PPS_T;
313     }
314     ps_seq->u1_bits_in_frm_num = (UWORD8) u8_temp;
315     COPYTHECONTEXT("SPS: log2_max_frame_num_minus4", (ps_seq->u1_bits_in_frm_num - 4));
316 
317     i2_max_frm_num = (1 << (ps_seq->u1_bits_in_frm_num));
318     ps_seq->u2_u4_max_pic_num_minus1 = i2_max_frm_num - 1;
319     /*--------------------------------------------------------------------*/
320     /* Decode picture order count and related values                      */
321     /*--------------------------------------------------------------------*/
322     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
323 
324     if(u4_temp > MAX_PIC_ORDER_CNT_TYPE)
325     {
326         return ERROR_INV_POC_TYPE_T;
327     }
328     ps_seq->u1_pic_order_cnt_type = u4_temp;
329     COPYTHECONTEXT("SPS: pic_order_cnt_type", ps_seq->u1_pic_order_cnt_type);
330 
331     ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = 1;
332     if(ps_seq->u1_pic_order_cnt_type == 0)
333     {
334         u8_temp = (UWORD64) 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
335         if(u8_temp > MAX_BITS_IN_POC_LSB)
336         {
337             return ERROR_INV_SPS_PPS_T;
338         }
339         ps_seq->u1_log2_max_pic_order_cnt_lsb_minus = (UWORD8) u8_temp;
340         ps_seq->i4_max_pic_order_cntLsb = (1 << u8_temp);
341         COPYTHECONTEXT("SPS: log2_max_pic_order_cnt_lsb_minus4", (u8_temp - 4));
342     }
343     else if(ps_seq->u1_pic_order_cnt_type == 1)
344     {
345         ps_seq->u1_delta_pic_order_always_zero_flag = ih264d_get_bit_h264(ps_bitstrm);
346         COPYTHECONTEXT("SPS: delta_pic_order_always_zero_flag",
347                        ps_seq->u1_delta_pic_order_always_zero_flag);
348 
349         ps_seq->i4_ofst_for_non_ref_pic = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
350         COPYTHECONTEXT("SPS: offset_for_non_ref_pic", ps_seq->i4_ofst_for_non_ref_pic);
351 
352         ps_seq->i4_ofst_for_top_to_bottom_field = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
353         COPYTHECONTEXT("SPS: offset_for_top_to_bottom_field",
354                        ps_seq->i4_ofst_for_top_to_bottom_field);
355 
356         u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
357         if(u4_temp > 255) return ERROR_INV_SPS_PPS_T;
358         ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = u4_temp;
359         COPYTHECONTEXT("SPS: num_ref_frames_in_pic_order_cnt_cycle",
360                        ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle);
361 
362         for(i = 0; i < ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
363         {
364             ps_seq->i4_ofst_for_ref_frame[i] = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
365             COPYTHECONTEXT("SPS: offset_for_ref_frame", ps_seq->i4_ofst_for_ref_frame[i]);
366         }
367     }
368 
369     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
370 
371     if((u4_temp > H264_MAX_REF_PICS))
372     {
373         return ERROR_NUM_REF;
374     }
375 
376     /* Compare with older num_ref_frames is header is already once */
377     if((ps_dec->i4_header_decoded & 1) &&
378        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
379        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_num_ref_frames != u4_temp))
380     {
381         ps_dec->u1_res_changed = 1;
382         return IVD_RES_CHANGED;
383     }
384     ps_seq->u1_num_ref_frames = u4_temp;
385     COPYTHECONTEXT("SPS: num_ref_frames", ps_seq->u1_num_ref_frames);
386 
387     ps_seq->u1_gaps_in_frame_num_value_allowed_flag = ih264d_get_bit_h264(ps_bitstrm);
388     COPYTHECONTEXT("SPS: gaps_in_frame_num_value_allowed_flag",
389                    ps_seq->u1_gaps_in_frame_num_value_allowed_flag);
390     /* SVC_DEC_REVIEW */
391     ps_seq->u1_gaps_in_frame_num_value_allowed_flag = 0;
392 
393     /*--------------------------------------------------------------------*/
394     /* Decode FrameWidth and FrameHeight and related values               */
395     /*--------------------------------------------------------------------*/
396     u8_temp = (UWORD64) 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
397     /* Check  for unsupported resolutions*/
398     if(u8_temp > (H264_MAX_FRAME_WIDTH >> 4))
399     {
400         return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
401     }
402     u4_pic_width_in_mbs = (UWORD32) u8_temp;
403     COPYTHECONTEXT("SPS: pic_width_in_mbs_minus1", u4_pic_width_in_mbs - 1);
404 
405     u8_temp = (UWORD64) 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
406     if(u8_temp > (H264_MAX_FRAME_HEIGHT >> 4))
407     {
408         return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
409     }
410     u4_pic_height_in_map_units = (UWORD32) u8_temp;
411 
412     ps_seq->u2_frm_wd_in_mbs = u4_pic_width_in_mbs;
413     ps_seq->u2_frm_ht_in_mbs = u4_pic_height_in_map_units;
414 
415     u2_pic_wd = (u4_pic_width_in_mbs << 4);
416     u2_pic_ht = (u4_pic_height_in_map_units << 4);
417     /*--------------------------------------------------------------------*/
418     /* Get the value of MaxMbAddress and Number of bits needed for it     */
419     /*--------------------------------------------------------------------*/
420     ps_seq->u2_max_mb_addr = (ps_seq->u2_frm_wd_in_mbs * ps_seq->u2_frm_ht_in_mbs) - 1;
421 
422     ps_seq->u2_total_num_of_mbs = ps_seq->u2_max_mb_addr + 1;
423 
424     ps_seq->u1_level_idc = ih264d_correct_level_idc(u1_level_idc, ps_seq->u2_total_num_of_mbs);
425 
426     u1_frm = ih264d_get_bit_h264(ps_bitstrm);
427 
428     if((ps_dec->i4_header_decoded & 1) &&
429        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
430        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_frame_mbs_only_flag != u1_frm))
431     {
432         ps_dec->u1_res_changed = 1;
433         return IVD_RES_CHANGED;
434     }
435     ps_seq->u1_frame_mbs_only_flag = u1_frm;
436 
437     COPYTHECONTEXT("SPS: frame_mbs_only_flag", u1_frm);
438 
439     if(!u1_frm) u1_mb_aff_flag = ih264d_get_bit_h264(ps_bitstrm);
440     if((ps_dec->i4_header_decoded & 1) &&
441        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
442        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_mb_aff_flag != u1_mb_aff_flag))
443     {
444         ps_dec->u1_res_changed = 1;
445         return IVD_RES_CHANGED;
446     }
447     if(!u1_frm)
448     {
449         u2_pic_ht <<= 1;
450         ps_seq->u1_mb_aff_flag = u1_mb_aff_flag;
451         COPYTHECONTEXT("SPS: mb_adaptive_frame_field_flag", ps_seq->u1_mb_aff_flag);
452     }
453     else
454         ps_seq->u1_mb_aff_flag = 0;
455 
456     ps_seq->u1_direct_8x8_inference_flag = ih264d_get_bit_h264(ps_bitstrm);
457 
458     COPYTHECONTEXT("SPS: direct_8x8_inference_flag", ps_seq->u1_direct_8x8_inference_flag);
459 
460     /* G050 */
461     u1_frame_cropping_flag = ih264d_get_bit_h264(ps_bitstrm);
462     COPYTHECONTEXT("SPS: frame_cropping_flag", u1_frame_cropping_flag);
463 
464     if(u1_frame_cropping_flag)
465     {
466         u1_frame_cropping_rect_left_ofst = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
467         COPYTHECONTEXT("SPS: frame_cropping_rect_left_offset", u1_frame_cropping_rect_left_ofst);
468         u1_frame_cropping_rect_right_ofst = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
469         COPYTHECONTEXT("SPS: frame_cropping_rect_right_offset", u1_frame_cropping_rect_right_ofst);
470         u1_frame_cropping_rect_top_ofst = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
471         COPYTHECONTEXT("SPS: frame_cropping_rect_top_offset", u1_frame_cropping_rect_top_ofst);
472         u1_frame_cropping_rect_bottom_ofst = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
473         COPYTHECONTEXT("SPS: frame_cropping_rect_bottom_offset",
474                        u1_frame_cropping_rect_bottom_ofst);
475     }
476     /* G050 */
477     ps_seq->u1_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
478     COPYTHECONTEXT("SPS: vui_parameters_present_flag", ps_seq->u1_vui_parameters_present_flag);
479 
480     u2_frm_wd_y = u2_pic_wd + (UWORD8) (PAD_LEN_Y_H << 1);
481     if(1 == ps_dec->u4_share_disp_buf)
482     {
483         if(ps_dec->u4_app_disp_width > u2_frm_wd_y) u2_frm_wd_y = ps_dec->u4_app_disp_width;
484     }
485 
486     u2_frm_ht_y = u2_pic_ht + (UWORD8) (PAD_LEN_Y_V << 2);
487     u2_frm_wd_uv = u2_pic_wd + (UWORD8) (PAD_LEN_UV_H << 2);
488     u2_frm_wd_uv = MAX(u2_frm_wd_uv, u2_frm_wd_y);
489 
490     u2_frm_ht_uv = (u2_pic_ht >> 1) + (UWORD8) (PAD_LEN_UV_V << 2);
491     u2_frm_ht_uv = MAX(u2_frm_ht_uv, (u2_frm_ht_y >> 1));
492 
493     /* Calculate display picture width, height and start u4_ofst from YUV420 */
494     /* pictute buffers as per cropping information parsed above             */
495     {
496         UWORD16 u2_rgt_ofst = 0;
497         UWORD16 u2_lft_ofst = 0;
498         UWORD16 u2_top_ofst = 0;
499         UWORD16 u2_btm_ofst = 0;
500         UWORD8 u1_frm_mbs_flag;
501         UWORD8 u1_vert_mult_factor;
502 
503         if(u1_frame_cropping_flag)
504         {
505             /* Calculate right and left u4_ofst for cropped picture           */
506             u2_rgt_ofst = u1_frame_cropping_rect_right_ofst << 1;
507             u2_lft_ofst = u1_frame_cropping_rect_left_ofst << 1;
508 
509             /* Know frame MBs only u4_flag                                      */
510             u1_frm_mbs_flag = (1 == ps_seq->u1_frame_mbs_only_flag);
511 
512             /* Simplify the vertical u4_ofst calculation from field/frame     */
513             u1_vert_mult_factor = (2 - u1_frm_mbs_flag);
514 
515             /* Calculate bottom and top u4_ofst for cropped  picture          */
516             u2_btm_ofst = (u1_frame_cropping_rect_bottom_ofst << u1_vert_mult_factor);
517             u2_top_ofst = (u1_frame_cropping_rect_top_ofst << u1_vert_mult_factor);
518         }
519 
520         /* Calculate u4_ofst from start of YUV 420 picture buffer to start of*/
521         /* cropped picture buffer                                           */
522         u2_crop_offset_y = (u2_frm_wd_y * u2_top_ofst) + (u2_lft_ofst);
523         u2_crop_offset_uv =
524             (u2_frm_wd_uv * (u2_top_ofst >> 1)) + (u2_lft_ofst >> 1) * YUV420SP_FACTOR;
525         /* Calculate the display picture width and height based on crop      */
526         /* information                                                       */
527         i4_cropped_ht = (WORD32) u2_pic_ht - (WORD32) (u2_btm_ofst + u2_top_ofst);
528         i4_cropped_wd = (WORD32) u2_pic_wd - (WORD32) (u2_rgt_ofst + u2_lft_ofst);
529 
530         if((i4_cropped_ht < MB_SIZE) || (i4_cropped_wd < MB_SIZE))
531         {
532             return ERROR_INV_SPS_PPS_T;
533         }
534 
535         if((ps_dec->i4_header_decoded & 1) &&
536            (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
537            (ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].u2_pic_wd != u2_pic_wd))
538         {
539             ps_dec->u1_res_changed = 1;
540             return IVD_RES_CHANGED;
541         }
542 
543         if((ps_dec->i4_header_decoded & 1) &&
544            (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
545            (ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].u2_disp_width != i4_cropped_wd))
546         {
547             ps_dec->u1_res_changed = 1;
548             return IVD_RES_CHANGED;
549         }
550 
551         if((ps_dec->i4_header_decoded & 1) &&
552            (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
553            (ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].u2_pic_ht != u2_pic_ht))
554         {
555             ps_dec->u1_res_changed = 1;
556             return IVD_RES_CHANGED;
557         }
558 
559         if((ps_dec->i4_header_decoded & 1) &&
560            (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
561            (ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].u2_disp_height != i4_cropped_ht))
562         {
563             ps_dec->u1_res_changed = 1;
564             return IVD_RES_CHANGED;
565         }
566         /* Check again for unsupported resolutions with updated values*/
567         if((u2_pic_wd > SVCD_MAX_FRAME_WIDTH) || (u2_pic_ht > SVCD_MAX_FRAME_HEIGHT) ||
568            (u2_pic_wd < SVCD_MIN_FRAME_WIDTH) || (u2_pic_ht < SVCD_MIN_FRAME_HEIGHT) ||
569            (u2_pic_wd * (UWORD32) u2_pic_ht > SVCD_MAX_FRAME_SIZE))
570         {
571             return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
572         }
573 
574         /* If MBAff is enabled, decoder support is limited to streams with
575          * width less than half of H264_MAX_FRAME_WIDTH.
576          * In case of MBAff decoder processes two rows at a time
577          */
578         if((u2_pic_wd << ps_seq->u1_mb_aff_flag) > H264_MAX_FRAME_WIDTH)
579         {
580             return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
581         }
582     }
583 
584     if(1 == ps_seq->u1_vui_parameters_present_flag)
585     {
586         ret = ih264d_parse_vui_parametres(&ps_seq->s_vui, ps_bitstrm);
587         if(ret != OK) return ret;
588     }
589     ps_seq_svc_ext = &ps_subset_seq->s_sps_svc_ext;
590 
591     isvcd_set_default_seq_svc_ext(ps_seq_svc_ext);
592 
593     if(SCALABLE_BASELINE_PROFILE_IDC == ps_seq->u1_profile_idc ||
594        SCALABLE_HIGH_PROFILE_IDC == ps_seq->u1_profile_idc)
595     {
596         SWITCHONTRACE;
597         ps_seq_svc_ext->u1_inter_layer_deblocking_filter_control_present_flag =
598             ih264d_get_bit_h264(ps_bitstrm);
599         COPYTHECONTEXT("SPS_EXt: u1_inter_layer_deblocking_filter_control_present_flag",
600                        ps_seq_svc_ext->u1_inter_layer_deblocking_filter_control_present_flag);
601 
602         ps_seq_svc_ext->u1_extended_spatial_scalability_idc = ih264d_get_bits_h264(ps_bitstrm, 2);
603         COPYTHECONTEXT("SPS_EXt: u1_extended_spatial_scalability_idc",
604                        ps_seq_svc_ext->u1_extended_spatial_scalability_idc);
605 
606         /* u1_extended_spatial_scalability_idc value 0, 1 and 2 are supported */
607         if(ps_seq_svc_ext->u1_extended_spatial_scalability_idc > 2)
608         {
609             return ERROR_SVC_INV_SUBSET_SPS;
610         }
611 
612         /* ChromaArrayType = i4_chroma_format_idc  if  separate_colour_plane_flag =
613          * 0 for all chroma format except 4:4:4 */
614         if(1 == ps_seq->i4_chroma_format_idc || 2 == ps_seq->i4_chroma_format_idc)
615         {
616             ps_seq_svc_ext->u1_chroma_phase_x_plus1_flag = ih264d_get_bit_h264(ps_bitstrm);
617             COPYTHECONTEXT("SPS_EXt: u1_chroma_phase_x_plus1_flag",
618                            ps_seq_svc_ext->u1_chroma_phase_x_plus1_flag);
619         }
620 
621         if(1 == ps_seq->i4_chroma_format_idc)
622         {
623             ps_seq_svc_ext->u1_chroma_phase_y_plus1 = ih264d_get_bits_h264(ps_bitstrm, 2);
624             COPYTHECONTEXT("SPS_EXt: u1_chroma_phase_y_plus1",
625                            ps_seq_svc_ext->u1_chroma_phase_y_plus1);
626 
627             if(ps_seq_svc_ext->u1_chroma_phase_y_plus1 >= 3)
628             {
629                 return ERROR_SVC_INV_SUBSET_SPS;
630             }
631         }
632 
633         /* inferred values not covered in isvcd_set_default_seq_svc_ext*/
634         ps_seq_svc_ext->u1_seq_ref_layer_chroma_phase_x_plus1_flag =
635             ps_seq_svc_ext->u1_chroma_phase_x_plus1_flag;
636         ps_seq_svc_ext->u1_seq_ref_layer_chroma_phase_y_plus1 =
637             ps_seq_svc_ext->u1_chroma_phase_y_plus1;
638 
639         if(1 == ps_seq_svc_ext->u1_extended_spatial_scalability_idc)
640         {
641             if(ps_seq->i4_chroma_format_idc > 0)
642             {
643                 ps_seq_svc_ext->u1_seq_ref_layer_chroma_phase_x_plus1_flag =
644                     ih264d_get_bit_h264(ps_bitstrm);
645                 COPYTHECONTEXT("SPS_EXt: u1_seq_ref_layer_chroma_phase_x_plus1_flag",
646                                ps_seq_svc_ext->u1_seq_ref_layer_chroma_phase_x_plus1_flag);
647 
648                 ps_seq_svc_ext->u1_seq_ref_layer_chroma_phase_y_plus1 =
649                     ih264d_get_bits_h264(ps_bitstrm, 2);
650                 COPYTHECONTEXT("SPS_EXt: u1_seq_ref_layer_chroma_phase_y_plus1",
651                                ps_seq_svc_ext->u1_seq_ref_layer_chroma_phase_y_plus1);
652 
653                 if(ps_seq_svc_ext->u1_seq_ref_layer_chroma_phase_y_plus1 >= 3)
654                 {
655                     return ERROR_SVC_INV_SUBSET_SPS;
656                 }
657             }
658 
659             ps_seq_svc_ext->i4_seq_scaled_ref_layer_left_offset =
660                 ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
661             COPYTHECONTEXT("SPS_EXt: i4_seq_scaled_ref_layer_left_offset",
662                            ps_seq_svc_ext->i4_seq_scaled_ref_layer_left_offset);
663 
664             if(ps_seq_svc_ext->i4_seq_scaled_ref_layer_left_offset != 0)
665             {
666                 return ERROR_SVC_INV_SUBSET_SPS;
667             }
668 
669             if(ps_seq_svc_ext->i4_seq_scaled_ref_layer_left_offset >= MAX_SCLD_REF_LAYER_OFFSET ||
670                ps_seq_svc_ext->i4_seq_scaled_ref_layer_left_offset < MIN_SCLD_REF_LAYER_OFFSET)
671             {
672                 return ERROR_SVC_INV_SUBSET_SPS;
673             }
674 
675             ps_seq_svc_ext->i4_seq_scaled_ref_layer_top_offset =
676                 ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
677             COPYTHECONTEXT("SPS_EXt: i4_seq_scaled_ref_layer_top_offset",
678                            ps_seq_svc_ext->i4_seq_scaled_ref_layer_top_offset);
679 
680             if(ps_seq_svc_ext->i4_seq_scaled_ref_layer_top_offset != 0)
681             {
682                 return ERROR_SVC_INV_SUBSET_SPS;
683             }
684 
685             if(ps_seq_svc_ext->i4_seq_scaled_ref_layer_top_offset >= MAX_SCLD_REF_LAYER_OFFSET ||
686                ps_seq_svc_ext->i4_seq_scaled_ref_layer_top_offset < MIN_SCLD_REF_LAYER_OFFSET)
687             {
688                 return ERROR_SVC_INV_SUBSET_SPS;
689             }
690 
691             ps_seq_svc_ext->i4_seq_scaled_ref_layer_right_offset =
692                 ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
693             COPYTHECONTEXT("SPS_EXt: i4_seq_scaled_ref_layer_right_offset",
694                            ps_seq_svc_ext->i4_seq_scaled_ref_layer_right_offset);
695 
696             if(ps_seq_svc_ext->i4_seq_scaled_ref_layer_right_offset >= MAX_SCLD_REF_LAYER_OFFSET ||
697                ps_seq_svc_ext->i4_seq_scaled_ref_layer_right_offset < MIN_SCLD_REF_LAYER_OFFSET)
698             {
699                 return ERROR_SVC_INV_SUBSET_SPS;
700             }
701 
702             ps_seq_svc_ext->i4_seq_scaled_ref_layer_bottom_offset =
703                 ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
704             COPYTHECONTEXT("SPS_EXt: i4_seq_scaled_ref_layer_bottom_offset",
705                            ps_seq_svc_ext->i4_seq_scaled_ref_layer_bottom_offset);
706 
707             if(ps_seq_svc_ext->i4_seq_scaled_ref_layer_bottom_offset >= MAX_SCLD_REF_LAYER_OFFSET ||
708                ps_seq_svc_ext->i4_seq_scaled_ref_layer_bottom_offset < MIN_SCLD_REF_LAYER_OFFSET)
709             {
710                 return ERROR_INV_SLICE_HDR_T;
711             }
712         }
713 
714         ps_seq_svc_ext->u1_seq_tcoeff_level_prediction_flag = ih264d_get_bit_h264(ps_bitstrm);
715         COPYTHECONTEXT("SPS_EXt: u1_seq_tcoeff_level_prediction_flag",
716                        ps_seq_svc_ext->u1_seq_tcoeff_level_prediction_flag);
717 
718         if(1 == ps_seq_svc_ext->u1_seq_tcoeff_level_prediction_flag)
719         {
720             ps_seq_svc_ext->u1_adaptive_tcoeff_level_prediction_flag =
721                 ih264d_get_bit_h264(ps_bitstrm);
722             COPYTHECONTEXT("SPS_EXt: u1_adaptive_tcoeff_level_prediction_flag",
723                            ps_seq_svc_ext->u1_adaptive_tcoeff_level_prediction_flag);
724         }
725 
726         ps_seq_svc_ext->u1_slice_header_restriction_flag = ih264d_get_bit_h264(ps_bitstrm);
727         COPYTHECONTEXT("SPS_EXt: u1_slice_header_restriction_flag",
728                        ps_seq_svc_ext->u1_slice_header_restriction_flag);
729 
730         ps_seq_svc_ext->u1_svc_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
731         COPYTHECONTEXT("SPS_EXt: u1_svc_vui_parameters_present_flag",
732                        ps_seq_svc_ext->u1_svc_vui_parameters_present_flag);
733 
734         if(1 == ps_seq_svc_ext->u1_svc_vui_parameters_present_flag)
735         {
736             if(NULL ==
737                ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].s_sps_svc_ext.ps_svc_vui_ext)
738             {
739                 void *pv_buf;
740                 UWORD32 size;
741                 /* Memory allocation only if VUI is enabled in a particular subset SPS*/
742                 size = sizeof(svc_vui_ext_t);
743                 pv_buf = ps_dec->pf_aligned_alloc(ps_dec->pv_mem_ctxt, 128, size);
744                 RETURN_IF((NULL == pv_buf), IV_FAIL);
745                 memset(pv_buf, 0, size);
746                 ps_seq_svc_ext->ps_svc_vui_ext = pv_buf;
747                 ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id]
748                     .s_sps_svc_ext.ps_svc_vui_ext = pv_buf;
749             }
750             else
751             {
752                 ps_seq_svc_ext->ps_svc_vui_ext =
753                     ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id]
754                         .s_sps_svc_ext.ps_svc_vui_ext;
755             }
756             ret = isvcd_parse_vui_ext_parametres(ps_seq_svc_ext->ps_svc_vui_ext, ps_bitstrm);
757             if(ret != OK) return ret;
758         }
759     }
760     /* Add conditions for SCALABLE BASELINE PROFILE */
761     if(SCALABLE_BASELINE_PROFILE_IDC == ps_seq->u1_profile_idc ||
762        ((SCALABLE_HIGH_PROFILE_IDC == ps_seq->u1_profile_idc) && (1 == uc_constraint_set0_flag)))
763     {
764         if(ps_seq->i4_chroma_format_idc != 1)
765         {
766             return ERROR_FEATURE_UNAVAIL;
767         }
768 
769         if(ps_seq->i4_bit_depth_luma_minus8 != 0)
770         {
771             return ERROR_FEATURE_UNAVAIL;
772         }
773 
774         if(ps_seq->i4_bit_depth_chroma_minus8 != 0)
775         {
776             return ERROR_FEATURE_UNAVAIL;
777         }
778 
779         if(ps_seq->i4_qpprime_y_zero_transform_bypass_flag != 0)
780         {
781             return ERROR_FEATURE_UNAVAIL;
782         }
783 
784         if(ps_seq->u1_frame_mbs_only_flag != 1)
785         {
786             return ERROR_FEATURE_UNAVAIL;
787         }
788 
789         if((0 != ps_seq_svc_ext->i4_seq_scaled_ref_layer_left_offset % 16) &&
790            (0 != ps_seq_svc_ext->i4_seq_scaled_ref_layer_top_offset % 16))
791         {
792             return ERROR_FEATURE_UNAVAIL;
793         }
794     }
795     /* Compare older num_reorder_frames with the new one if header is already
796      * decoded */
797     if((ps_dec->i4_header_decoded & 1) &&
798        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
799        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_vui_parameters_present_flag) &&
800        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].s_vui.u1_bitstream_restriction_flag))
801     {
802         ps_dec->u1_res_changed = 1;
803         return IVD_RES_CHANGED;
804     }
805     /* In case bitstream read has exceeded the filled size, then return an error */
806     if(EXCEED_OFFSET(ps_bitstrm))
807     {
808         return ERROR_INV_SPS_PPS_T;
809     }
810 
811     /*--------------------------------------------------------------------*/
812     /* All initializations to ps_dec are beyond this point                */
813     /*--------------------------------------------------------------------*/
814     {
815         WORD32 reorder_depth = ih264d_get_dpb_size(ps_seq);
816         if((1 == ps_seq->u1_vui_parameters_present_flag) &&
817            (1 == ps_seq->s_vui.u1_bitstream_restriction_flag))
818         {
819             reorder_depth = ps_seq->s_vui.u4_num_reorder_frames + 1;
820         }
821 
822         if(reorder_depth > H264_MAX_REF_PICS)
823         {
824             return ERROR_INV_SPS_PPS_T;
825         }
826 
827         if(ps_seq->u1_frame_mbs_only_flag != 1) reorder_depth *= 2;
828         ps_subset_seq->i4_reorder_depth = reorder_depth + DISPLAY_LATENCY;
829     }
830     ps_subset_seq->u2_disp_height = i4_cropped_ht;
831     ps_subset_seq->u2_disp_width = i4_cropped_wd;
832     ps_subset_seq->u2_pic_wd = u2_pic_wd;
833     ps_subset_seq->u2_pic_ht = u2_pic_ht;
834 
835     /* Assuming 8k is the maximum resolution svc dec supports*/
836     if(u2_frm_wd_y > H264_MAX_FRAME_WIDTH) return (NOT_OK);
837     if(u2_frm_ht_y > H264_MAX_FRAME_HEIGHT) return (NOT_OK);
838     if(u2_frm_wd_uv > H264_MAX_FRAME_WIDTH) return (NOT_OK);
839     if(u2_frm_ht_uv > H264_MAX_FRAME_HEIGHT) return (NOT_OK);
840 
841     /* Determining the Width and Height of Frame from that of Picture */
842     ps_subset_seq->u2_frm_wd_y = u2_frm_wd_y;
843     ps_subset_seq->u2_frm_ht_y = u2_frm_ht_y;
844     ps_subset_seq->u2_frm_wd_uv = u2_frm_wd_uv;
845     ps_subset_seq->u2_frm_ht_uv = u2_frm_ht_uv;
846 
847     ps_subset_seq->u1_pad_len_y_v = (UWORD8) (PAD_LEN_Y_V << (1 - u1_frm));
848     ps_subset_seq->u1_pad_len_cr_v = (UWORD8) (PAD_LEN_UV_V << (1 - u1_frm));
849 
850     ps_subset_seq->u2_crop_offset_y = u2_crop_offset_y;
851     ps_subset_seq->u2_crop_offset_uv = u2_crop_offset_uv;
852 
853     if(((ps_dec->u2_pic_wd * ps_dec->u2_pic_ht) <
854         (ps_subset_seq->u2_pic_wd * ps_subset_seq->u2_pic_ht)) ||
855        (ps_dec->i4_reorder_depth < ps_subset_seq->i4_reorder_depth))
856     {
857         ps_dec->i4_reorder_depth = ps_subset_seq->i4_reorder_depth;
858 
859         ps_dec->u2_disp_height = ps_subset_seq->u2_disp_height;
860         ps_dec->u2_disp_width = ps_subset_seq->u2_disp_width;
861 
862         ps_dec->u2_pic_wd = ps_subset_seq->u2_pic_wd;
863         ps_dec->u2_pic_ht = ps_subset_seq->u2_pic_ht;
864         ps_dec->u4_total_mbs = ps_seq->u2_total_num_of_mbs << (1 - ps_seq->u1_frame_mbs_only_flag);
865 
866         /* Determining the Width and Height of Frame from that of Picture */
867         ps_dec->u2_frm_wd_y = ps_subset_seq->u2_frm_wd_y;
868         ps_dec->u2_frm_ht_y = ps_subset_seq->u2_frm_ht_y;
869         ps_dec->u2_frm_wd_uv = ps_subset_seq->u2_frm_wd_uv;
870         ps_dec->u2_frm_ht_uv = ps_subset_seq->u2_frm_ht_uv;
871 
872         ps_dec->s_pad_mgr.u1_pad_len_y_v = ps_subset_seq->u1_pad_len_y_v;
873         ps_dec->s_pad_mgr.u1_pad_len_cr_v = ps_subset_seq->u1_pad_len_cr_v;
874 
875         ps_dec->u2_frm_wd_in_mbs = ps_seq->u2_frm_wd_in_mbs;
876         ps_dec->u2_frm_ht_in_mbs = ps_seq->u2_frm_ht_in_mbs;
877 
878         ps_dec->u2_crop_offset_y = ps_subset_seq->u2_crop_offset_y;
879         ps_dec->u2_crop_offset_uv = ps_subset_seq->u2_crop_offset_uv;
880     }
881 
882     ps_seq->u1_is_valid = TRUE;
883     ps_dec->ps_sps[u1_seq_parameter_set_id] = *ps_seq;
884     if(NULL != ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].s_sps_svc_ext.ps_svc_vui_ext)
885     {
886         ps_seq_svc_ext->ps_svc_vui_ext =
887             ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].s_sps_svc_ext.ps_svc_vui_ext;
888     }
889     ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id] = *ps_subset_seq;
890     ps_dec->ps_cur_sps = &ps_dec->ps_sps[u1_seq_parameter_set_id];
891     ps_svc_lyr_dec->ps_cur_subset_sps = &ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id];
892 
893     return OK;
894 }
895 /*!
896  **************************************************************************
897  * \if Function name : isvcd_dec_ref_base_pic_marking \endif
898  *
899  * \brief
900  *    Decodes reference base pic marking params
901  *
902  * \return
903  *    0 on Success and error code otherwise
904  **************************************************************************
905  */
906 
isvcd_dec_ref_base_pic_marking(dec_ref_base_pic_marking_params_t * ps_ref_base_pic_marking_svc_ext,dec_bit_stream_t * ps_bitstrm)907 WORD32 isvcd_dec_ref_base_pic_marking(
908     dec_ref_base_pic_marking_params_t *ps_ref_base_pic_marking_svc_ext,
909     dec_bit_stream_t *ps_bitstrm)
910 {
911     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
912     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
913 
914     SWITCHONTRACE;
915 
916     ps_ref_base_pic_marking_svc_ext->u1_adaptive_ref_base_pic_marking_mode_flag =
917         ih264d_get_bit_h264(ps_bitstrm);
918     COPYTHECONTEXT(
919         "Dec ref base pic marking params : "
920         "u1_adaptive_ref_base_pic_marking_mode_flag",
921         ps_ref_base_pic_marking_svc_ext->u1_adaptive_ref_base_pic_marking_mode_flag);
922 
923     if(1 == ps_ref_base_pic_marking_svc_ext->u1_adaptive_ref_base_pic_marking_mode_flag)
924     {
925         do
926         {
927             ps_ref_base_pic_marking_svc_ext->u4_memory_management_base_control_operation =
928                 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
929             COPYTHECONTEXT(
930                 "Dec ref base pic marking params : "
931                 "u4_memory_management_base_control_operation",
932                 ps_ref_base_pic_marking_svc_ext->u4_memory_management_base_control_operation);
933 
934             if(1 == ps_ref_base_pic_marking_svc_ext->u4_memory_management_base_control_operation)
935             {
936                 ps_ref_base_pic_marking_svc_ext->u4_difference_of_base_pic_nums_minus1 =
937                     ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
938                 COPYTHECONTEXT(
939                     "Dec ref base pic marking params : "
940                     "u4_difference_of_base_pic_nums_minus1",
941                     ps_ref_base_pic_marking_svc_ext->u4_difference_of_base_pic_nums_minus1);
942             }
943 
944             if(2 == ps_ref_base_pic_marking_svc_ext->u4_memory_management_base_control_operation)
945             {
946                 ps_ref_base_pic_marking_svc_ext->u4_long_term_base_pic_num =
947                     ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
948                 COPYTHECONTEXT("Dec ref base pic marking params : u4_long_term_base_pic_num",
949                                ps_ref_base_pic_marking_svc_ext->u4_long_term_base_pic_num);
950             }
951 
952         } while(0 != ps_ref_base_pic_marking_svc_ext->u4_memory_management_base_control_operation);
953     }
954     SWITCHOFFTRACE;
955 
956     return OK;
957 }
958 
959 /*!
960  **************************************************************************
961  * \if Function name : isvcd_parse_nal_unit \endif
962  *
963  * \brief
964  *    Decodes NAL unit
965  *
966  * \return
967  *    0 on Success and error code otherwise
968  **************************************************************************
969  */
970 
isvcd_parse_nal_unit(svc_dec_lyr_struct_t * dec_svc_hdl,UWORD8 u1_nal_ref_idc)971 WORD32 isvcd_parse_nal_unit(svc_dec_lyr_struct_t *dec_svc_hdl, UWORD8 u1_nal_ref_idc)
972 {
973     dec_bit_stream_t *ps_bitstrm;
974 
975     dec_struct_t *ps_dec;
976     svc_dec_lyr_struct_t *ps_svc_lyr_dec;
977     UWORD8 u1_nal_unit_type;
978     WORD32 i_status = OK;
979 
980     ps_svc_lyr_dec = (svc_dec_lyr_struct_t *) dec_svc_hdl;
981     ps_dec = &ps_svc_lyr_dec->s_dec;
982 
983     {
984         SWITCHOFFTRACE;
985         u1_nal_unit_type = ps_dec->u1_nal_unit_type;
986 
987         ps_bitstrm = ps_dec->ps_bitstrm;
988 
989         // Skip all NALUs if SPS and PPS are not decoded
990         switch(u1_nal_unit_type)
991         {
992             case SLICE_DATA_PARTITION_A_NAL:
993             case SLICE_DATA_PARTITION_B_NAL:
994             case SLICE_DATA_PARTITION_C_NAL:
995                 if(!ps_dec->i4_decode_header) ih264d_parse_slice_partition(ps_dec, ps_bitstrm);
996                 break;
997 
998             case IDR_SLICE_NAL:
999             case SLICE_NAL:
1000 
1001                 if(ps_svc_lyr_dec->u1_base_res_flag != 1)
1002                 {
1003                     return NOT_OK;
1004                 }
1005                 if(!ps_dec->i4_decode_header)
1006                 {
1007                     if(ps_dec->i4_header_decoded == 3)
1008                     {
1009                         /* ! */
1010                         DEBUG_THREADS_PRINTF("Decoding  a slice NAL\n");
1011                         {
1012                             ih264d_get_pre_sei_params(ps_dec, u1_nal_unit_type);
1013                             /* ! */
1014                             ps_dec->u4_slice_start_code_found = 1;
1015 
1016                             i_status = isvcd_parse_decode_slice(
1017                                 (UWORD8) (u1_nal_unit_type == IDR_SLICE_NAL), u1_nal_ref_idc,
1018                                 ps_svc_lyr_dec);
1019 
1020                             if(i_status != OK)
1021                             {
1022                                 return i_status;
1023                             }
1024                         }
1025                     }
1026                 }
1027                 break;
1028 
1029             case SEI_NAL:
1030             case PREFIX_UNIT_NAL:
1031             case SEQ_PARAM_NAL:
1032             case PIC_PARAM_NAL:
1033             case SUBSET_SPS_NAL:
1034                 H264_DEC_DEBUG_PRINT("\nUnknown NAL type %d\n", u1_nal_unit_type);
1035                 break;
1036 
1037             case ACCESS_UNIT_DELIMITER_RBSP:
1038                 if(!ps_dec->i4_decode_header)
1039                 {
1040                     ih264d_access_unit_delimiter_rbsp(ps_dec);
1041                 }
1042                 break;
1043                 // ignore the END_OF_SEQ_RBSP NAL and decode even after this NAL
1044             case END_OF_STREAM_RBSP:
1045                 if(!ps_dec->i4_decode_header)
1046                 {
1047                     ih264d_parse_end_of_stream(ps_dec);
1048                 }
1049                 break;
1050             case FILLER_DATA_NAL:
1051                 if(!ps_dec->i4_decode_header)
1052                 {
1053                     ih264d_parse_filler_data(ps_dec, ps_bitstrm);
1054                 }
1055                 break;
1056             case CODED_SLICE_EXTENSION_NAL:
1057 
1058                 if(ps_svc_lyr_dec->u1_base_res_flag == 1)
1059                 {
1060                     return NOT_OK;
1061                 }
1062                 if(!ps_dec->i4_decode_header)
1063                 {
1064                     if(ps_dec->i4_header_decoded == 3)
1065                     {
1066                         /* ! */
1067                         DEBUG_THREADS_PRINTF("Decoding  an SVC slice NAL\n");
1068                         {
1069                             {
1070                                 ih264d_get_pre_sei_params(ps_dec, u1_nal_unit_type);
1071                                 /* ! */
1072                                 ps_dec->u4_slice_start_code_found = 1;
1073 
1074                                 i_status = isvcd_parse_decode_slice_ext_nal(
1075                                     (UWORD8) (ps_svc_lyr_dec->ps_nal_svc_ext->u1_idr_flag),
1076                                     u1_nal_ref_idc, ps_svc_lyr_dec);
1077 
1078                                 if(i_status != OK)
1079                                 {
1080                                     return i_status;
1081                                 }
1082                             }
1083                         }
1084                     }
1085                 }
1086                 break;
1087 
1088             default:
1089                 H264_DEC_DEBUG_PRINT("\nUnknown NAL type %d\n", u1_nal_unit_type);
1090                 break;
1091         }
1092     }
1093     return i_status;
1094 }
1095 
1096 /*!
1097 **************************************************************************
1098 * \if Function name : isvcd_parse_sps \endif
1099 *
1100 * \brief
1101 *    Decodes Picture Parameter set
1102 *
1103 * \return
1104 *    0 on Success and Error code otherwise
1105 **************************************************************************
1106 */
isvcd_parse_sps(svc_dec_lyr_struct_t * ps_svc_lyr_dec,dec_bit_stream_t * ps_bitstrm)1107 WORD32 isvcd_parse_sps(svc_dec_lyr_struct_t *ps_svc_lyr_dec, dec_bit_stream_t *ps_bitstrm)
1108 {
1109     dec_struct_t *ps_dec = &ps_svc_lyr_dec->s_dec;
1110     UWORD8 i;
1111     dec_seq_params_t *ps_seq = NULL;
1112     dec_svc_seq_params_t *ps_subset_seq = NULL;
1113     UWORD8 u1_profile_idc, u1_level_idc, u1_seq_parameter_set_id, u1_mb_aff_flag = 0;
1114     UWORD16 i2_max_frm_num;
1115     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
1116     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
1117     UWORD8 u1_frm, uc_constraint_set0_flag, uc_constraint_set1_flag, uc_constraint_set2_flag;
1118     WORD32 i4_cropped_ht, i4_cropped_wd;
1119     UWORD32 u4_temp;
1120     UWORD64 u8_temp;
1121     UWORD32 u4_pic_height_in_map_units, u4_pic_width_in_mbs;
1122     UWORD32 u2_pic_wd = 0;
1123     UWORD32 u2_pic_ht = 0;
1124     UWORD32 u2_frm_wd_y = 0;
1125     UWORD32 u2_frm_ht_y = 0;
1126     UWORD32 u2_frm_wd_uv = 0;
1127     UWORD32 u2_frm_ht_uv = 0;
1128     UWORD32 u2_crop_offset_y = 0;
1129     UWORD32 u2_crop_offset_uv = 0;
1130     WORD32 ret;
1131     WORD32 num_reorder_frames;
1132     /* High profile related syntax element */
1133     WORD32 i4_i;
1134     /* G050 */
1135     UWORD8 u1_frame_cropping_flag,
1136         u1_frame_cropping_rect_left_ofst = 0, u1_frame_cropping_rect_right_ofst = 0,
1137         u1_frame_cropping_rect_top_ofst = 0, u1_frame_cropping_rect_bottom_ofst = 0;
1138     /* G050 */
1139     /*--------------------------------------------------------------------*/
1140     /* Decode seq_parameter_set_id and profile and level values           */
1141     /*--------------------------------------------------------------------*/
1142     SWITCHONTRACE;
1143     u1_profile_idc = ih264d_get_bits_h264(ps_bitstrm, 8);
1144     COPYTHECONTEXT("SPS: profile_idc", u1_profile_idc);
1145 
1146     /* G050 */
1147     uc_constraint_set0_flag = ih264d_get_bit_h264(ps_bitstrm);
1148     uc_constraint_set1_flag = ih264d_get_bit_h264(ps_bitstrm);
1149     uc_constraint_set2_flag = ih264d_get_bit_h264(ps_bitstrm);
1150     UNUSED(uc_constraint_set2_flag);
1151     /*****************************************************/
1152     /* Read 5 bits for uc_constraint_set3_flag (1 bit)   */
1153     /* and reserved_zero_4bits (4 bits) - Sushant        */
1154     /*****************************************************/
1155     ih264d_get_bits_h264(ps_bitstrm, 5);
1156     /* G050 */
1157     /* Check whether particular profile is suported or not */
1158     /* Check whether particular profile is suported or not */
1159     if((u1_profile_idc != MAIN_PROFILE_IDC) && (u1_profile_idc != BASE_PROFILE_IDC) &&
1160        (u1_profile_idc != HIGH_PROFILE_IDC))
1161     {
1162         /* Apart from Baseline, main and high profile,
1163          * only extended profile is supported provided
1164          * uc_constraint_set0_flag or uc_constraint_set1_flag are set to 1
1165          */
1166         if((u1_profile_idc != EXTENDED_PROFILE_IDC) ||
1167            ((uc_constraint_set1_flag != 1) && (uc_constraint_set0_flag != 1)))
1168         {
1169             return (ERROR_FEATURE_UNAVAIL);
1170         }
1171     }
1172 
1173     u1_level_idc = ih264d_get_bits_h264(ps_bitstrm, 8);
1174     COPYTHECONTEXT("SPS: u4_level_idc", u1_level_idc);
1175 
1176     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1177     if(u4_temp & MASK_ERR_SEQ_SET_ID) return ERROR_INV_SPS_PPS_T;
1178     u1_seq_parameter_set_id = u4_temp;
1179     COPYTHECONTEXT("SPS: seq_parameter_set_id", u1_seq_parameter_set_id);
1180 
1181     /*--------------------------------------------------------------------*/
1182     /* Find an seq param entry in seqparam array of decStruct             */
1183     /*--------------------------------------------------------------------*/
1184     ps_subset_seq = ps_svc_lyr_dec->pv_scratch_subset_sps;
1185     memset(ps_subset_seq, 0, sizeof(dec_svc_seq_params_t));
1186     ps_seq = ps_dec->pv_scratch_sps_pps;
1187     memset(ps_seq, 0, sizeof(dec_seq_params_t));
1188 
1189     if(ps_dec->i4_header_decoded & 1)
1190     {
1191         if(ps_dec->ps_cur_sps != NULL)
1192             *ps_seq = *ps_dec->ps_cur_sps;
1193         else
1194             return ERROR_INV_SPS_PPS_T;
1195     }
1196 
1197     if((ps_dec->i4_header_decoded & 1) &&
1198        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
1199        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_profile_idc != u1_profile_idc))
1200     {
1201         ps_dec->u1_res_changed = 1;
1202         return IVD_RES_CHANGED;
1203     }
1204 
1205     if((ps_dec->i4_header_decoded & 1) &&
1206        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
1207        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_level_idc != u1_level_idc))
1208     {
1209         ps_dec->u1_res_changed = 1;
1210         return IVD_RES_CHANGED;
1211     }
1212 
1213     ps_seq->u1_profile_idc = u1_profile_idc;
1214     ps_seq->u1_level_idc = u1_level_idc;
1215     ps_seq->u1_seq_parameter_set_id = u1_seq_parameter_set_id;
1216     ps_subset_seq->ps_seq = &ps_dec->ps_sps[u1_seq_parameter_set_id];
1217 
1218     /*******************************************************************/
1219     /* Initializations for high profile - Sushant                      */
1220     /*******************************************************************/
1221     ps_seq->i4_chroma_format_idc = 1;
1222     ps_seq->i4_bit_depth_luma_minus8 = 0;
1223     ps_seq->i4_bit_depth_chroma_minus8 = 0;
1224     ps_seq->i4_qpprime_y_zero_transform_bypass_flag = 0;
1225     ps_seq->i4_seq_scaling_matrix_present_flag = 0;
1226     if(u1_profile_idc == HIGH_PROFILE_IDC || u1_profile_idc == SCALABLE_BASELINE_PROFILE_IDC ||
1227        u1_profile_idc == SCALABLE_HIGH_PROFILE_IDC)
1228     {
1229         /* reading chroma_format_idc   */
1230         ps_seq->i4_chroma_format_idc = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1231 
1232         /* Monochrome is not supported */
1233         if(ps_seq->i4_chroma_format_idc != 1)
1234         {
1235             return ERROR_FEATURE_UNAVAIL;
1236         }
1237 
1238         /* reading bit_depth_luma_minus8   */
1239         ps_seq->i4_bit_depth_luma_minus8 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1240 
1241         if(ps_seq->i4_bit_depth_luma_minus8 != 0)
1242         {
1243             return ERROR_FEATURE_UNAVAIL;
1244         }
1245 
1246         /* reading bit_depth_chroma_minus8   */
1247         ps_seq->i4_bit_depth_chroma_minus8 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1248 
1249         if(ps_seq->i4_bit_depth_chroma_minus8 != 0)
1250         {
1251             return ERROR_FEATURE_UNAVAIL;
1252         }
1253 
1254         /* reading qpprime_y_zero_transform_bypass_flag   */
1255         ps_seq->i4_qpprime_y_zero_transform_bypass_flag = (WORD32) ih264d_get_bit_h264(ps_bitstrm);
1256 
1257         if(ps_seq->i4_qpprime_y_zero_transform_bypass_flag != 0)
1258         {
1259             return ERROR_INV_SPS_PPS_T;
1260         }
1261 
1262         /* reading seq_scaling_matrix_present_flag   */
1263         ps_seq->i4_seq_scaling_matrix_present_flag = (WORD32) ih264d_get_bit_h264(ps_bitstrm);
1264 
1265         if(ps_seq->i4_seq_scaling_matrix_present_flag)
1266         {
1267             for(i4_i = 0; i4_i < 8; i4_i++)
1268             {
1269                 ps_seq->u1_seq_scaling_list_present_flag[i4_i] = ih264d_get_bit_h264(ps_bitstrm);
1270 
1271                 /* initialize u1_use_default_scaling_matrix_flag[i4_i] to zero */
1272                 /* before calling scaling list                             */
1273                 ps_seq->u1_use_default_scaling_matrix_flag[i4_i] = 0;
1274 
1275                 if(ps_seq->u1_seq_scaling_list_present_flag[i4_i])
1276                 {
1277                     if(i4_i < 6)
1278                     {
1279                         ret = ih264d_scaling_list(ps_seq->i2_scalinglist4x4[i4_i], 16,
1280                                                   &ps_seq->u1_use_default_scaling_matrix_flag[i4_i],
1281                                                   ps_bitstrm);
1282                     }
1283                     else
1284                     {
1285                         ret = ih264d_scaling_list(ps_seq->i2_scalinglist8x8[i4_i - 6], 64,
1286                                                   &ps_seq->u1_use_default_scaling_matrix_flag[i4_i],
1287                                                   ps_bitstrm);
1288                     }
1289                     if(ret != OK)
1290                     {
1291                         return ret;
1292                     }
1293                 }
1294             }
1295         }
1296     }
1297     /*--------------------------------------------------------------------*/
1298     /* Decode MaxFrameNum                                                 */
1299     /*--------------------------------------------------------------------*/
1300     u8_temp = (UWORD64) 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1301     if(u8_temp > MAX_BITS_IN_FRAME_NUM)
1302     {
1303         return ERROR_INV_SPS_PPS_T;
1304     }
1305     ps_seq->u1_bits_in_frm_num = (UWORD8) u8_temp;
1306     COPYTHECONTEXT("SPS: log2_max_frame_num_minus4", (ps_seq->u1_bits_in_frm_num - 4));
1307 
1308     i2_max_frm_num = (1 << (ps_seq->u1_bits_in_frm_num));
1309     ps_seq->u2_u4_max_pic_num_minus1 = i2_max_frm_num - 1;
1310     /*--------------------------------------------------------------------*/
1311     /* Decode picture order count and related values                      */
1312     /*--------------------------------------------------------------------*/
1313     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1314     if(u4_temp > MAX_PIC_ORDER_CNT_TYPE)
1315     {
1316         return ERROR_INV_POC_TYPE_T;
1317     }
1318     ps_seq->u1_pic_order_cnt_type = u4_temp;
1319     COPYTHECONTEXT("SPS: pic_order_cnt_type", ps_seq->u1_pic_order_cnt_type);
1320 
1321     ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = 1;
1322     if(ps_seq->u1_pic_order_cnt_type == 0)
1323     {
1324         u8_temp = (UWORD64) 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1325         if(u8_temp > MAX_BITS_IN_POC_LSB)
1326         {
1327             return ERROR_INV_SPS_PPS_T;
1328         }
1329         ps_seq->u1_log2_max_pic_order_cnt_lsb_minus = (UWORD8) u8_temp;
1330         ps_seq->i4_max_pic_order_cntLsb = (1 << u8_temp);
1331         COPYTHECONTEXT("SPS: log2_max_pic_order_cnt_lsb_minus4", (u8_temp - 4));
1332     }
1333     else if(ps_seq->u1_pic_order_cnt_type == 1)
1334     {
1335         ps_seq->u1_delta_pic_order_always_zero_flag = ih264d_get_bit_h264(ps_bitstrm);
1336         COPYTHECONTEXT("SPS: delta_pic_order_always_zero_flag",
1337                        ps_seq->u1_delta_pic_order_always_zero_flag);
1338 
1339         ps_seq->i4_ofst_for_non_ref_pic = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1340         COPYTHECONTEXT("SPS: offset_for_non_ref_pic", ps_seq->i4_ofst_for_non_ref_pic);
1341 
1342         ps_seq->i4_ofst_for_top_to_bottom_field = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1343         COPYTHECONTEXT("SPS: offset_for_top_to_bottom_field",
1344                        ps_seq->i4_ofst_for_top_to_bottom_field);
1345 
1346         u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1347         if(u4_temp > 255) return ERROR_INV_SPS_PPS_T;
1348         ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle = u4_temp;
1349         COPYTHECONTEXT("SPS: num_ref_frames_in_pic_order_cnt_cycle",
1350                        ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle);
1351 
1352         for(i = 0; i < ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
1353         {
1354             ps_seq->i4_ofst_for_ref_frame[i] = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1355             COPYTHECONTEXT("SPS: offset_for_ref_frame", ps_seq->i4_ofst_for_ref_frame[i]);
1356         }
1357     }
1358 
1359     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1360 
1361     if((u4_temp > H264_MAX_REF_PICS))
1362     {
1363         return ERROR_NUM_REF;
1364     }
1365 
1366     /* Compare with older num_ref_frames is header is already once */
1367     if((ps_dec->i4_header_decoded & 1) &&
1368        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
1369        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_num_ref_frames != u4_temp))
1370     {
1371         ps_dec->u1_res_changed = 1;
1372         return IVD_RES_CHANGED;
1373     }
1374 
1375     ps_seq->u1_num_ref_frames = u4_temp;
1376     COPYTHECONTEXT("SPS: num_ref_frames", ps_seq->u1_num_ref_frames);
1377 
1378     ps_seq->u1_gaps_in_frame_num_value_allowed_flag = ih264d_get_bit_h264(ps_bitstrm);
1379     COPYTHECONTEXT("SPS: gaps_in_frame_num_value_allowed_flag",
1380                    ps_seq->u1_gaps_in_frame_num_value_allowed_flag);
1381 
1382     ps_seq->u1_gaps_in_frame_num_value_allowed_flag = 0;
1383 
1384     /*--------------------------------------------------------------------*/
1385     /* Decode FrameWidth and FrameHeight and related values               */
1386     /*--------------------------------------------------------------------*/
1387     u8_temp = (UWORD64) 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1388     /* Check  for unsupported resolutions*/
1389     if(u8_temp > (H264_MAX_FRAME_WIDTH >> 4))
1390     {
1391         return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1392     }
1393     u4_pic_width_in_mbs = (UWORD32) u8_temp;
1394     COPYTHECONTEXT("SPS: pic_width_in_mbs_minus1", u4_pic_width_in_mbs - 1);
1395 
1396     u8_temp = (UWORD64) 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1397     if(u8_temp > (H264_MAX_FRAME_HEIGHT >> 4))
1398     {
1399         return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1400     }
1401     u4_pic_height_in_map_units = (UWORD32) u8_temp;
1402 
1403     ps_seq->u2_frm_wd_in_mbs = u4_pic_width_in_mbs;
1404     ps_seq->u2_frm_ht_in_mbs = u4_pic_height_in_map_units;
1405     u2_pic_wd = (u4_pic_width_in_mbs << 4);
1406     u2_pic_ht = (u4_pic_height_in_map_units << 4);
1407     /*--------------------------------------------------------------------*/
1408     /* Get the value of MaxMbAddress and Number of bits needed for it     */
1409     /*--------------------------------------------------------------------*/
1410     ps_seq->u2_max_mb_addr = (ps_seq->u2_frm_wd_in_mbs * ps_seq->u2_frm_ht_in_mbs) - 1;
1411     ps_seq->u2_total_num_of_mbs = ps_seq->u2_max_mb_addr + 1;
1412     ps_seq->u1_level_idc = ih264d_correct_level_idc(u1_level_idc, ps_seq->u2_total_num_of_mbs);
1413 
1414     u1_frm = ih264d_get_bit_h264(ps_bitstrm);
1415     if((ps_dec->i4_header_decoded & 1) &&
1416        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
1417        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_frame_mbs_only_flag != u1_frm))
1418     {
1419         ps_dec->u1_res_changed = 1;
1420         return IVD_RES_CHANGED;
1421     }
1422     ps_seq->u1_frame_mbs_only_flag = u1_frm;
1423     COPYTHECONTEXT("SPS: frame_mbs_only_flag", u1_frm);
1424 
1425     if(!u1_frm) u1_mb_aff_flag = ih264d_get_bit_h264(ps_bitstrm);
1426 
1427     if((ps_dec->i4_header_decoded & 1) &&
1428        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
1429        (ps_dec->ps_sps[u1_seq_parameter_set_id].u1_mb_aff_flag != u1_mb_aff_flag))
1430     {
1431         ps_dec->u1_res_changed = 1;
1432         return IVD_RES_CHANGED;
1433     }
1434 
1435     if(!u1_frm)
1436     {
1437         u2_pic_ht <<= 1;
1438         ps_seq->u1_mb_aff_flag = u1_mb_aff_flag;
1439         COPYTHECONTEXT("SPS: mb_adaptive_frame_field_flag", ps_seq->u1_mb_aff_flag);
1440     }
1441     else
1442         ps_seq->u1_mb_aff_flag = 0;
1443 
1444     ps_seq->u1_direct_8x8_inference_flag = ih264d_get_bit_h264(ps_bitstrm);
1445     COPYTHECONTEXT("SPS: direct_8x8_inference_flag", ps_seq->u1_direct_8x8_inference_flag);
1446 
1447     /* G050 */
1448     u1_frame_cropping_flag = ih264d_get_bit_h264(ps_bitstrm);
1449     COPYTHECONTEXT("SPS: frame_cropping_flag", u1_frame_cropping_flag);
1450 
1451     if(u1_frame_cropping_flag)
1452     {
1453         u1_frame_cropping_rect_left_ofst = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1454         COPYTHECONTEXT("SPS: frame_cropping_rect_left_offset", u1_frame_cropping_rect_left_ofst);
1455         u1_frame_cropping_rect_right_ofst = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1456         COPYTHECONTEXT("SPS: frame_cropping_rect_right_offset", u1_frame_cropping_rect_right_ofst);
1457         u1_frame_cropping_rect_top_ofst = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1458         COPYTHECONTEXT("SPS: frame_cropping_rect_top_offset", u1_frame_cropping_rect_top_ofst);
1459         u1_frame_cropping_rect_bottom_ofst = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1460         COPYTHECONTEXT("SPS: frame_cropping_rect_bottom_offset",
1461                        u1_frame_cropping_rect_bottom_ofst);
1462     }
1463     /* G050 */
1464     ps_seq->u1_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
1465     COPYTHECONTEXT("SPS: vui_parameters_present_flag", ps_seq->u1_vui_parameters_present_flag);
1466 
1467     u2_frm_wd_y = u2_pic_wd + (UWORD8) (PAD_LEN_Y_H << 1);
1468 
1469     if(1 == ps_dec->u4_share_disp_buf)
1470     {
1471         if(ps_dec->u4_app_disp_width > u2_frm_wd_y) u2_frm_wd_y = ps_dec->u4_app_disp_width;
1472     }
1473 
1474     u2_frm_ht_y = u2_pic_ht + (UWORD8) (PAD_LEN_Y_V << 2);
1475     u2_frm_wd_uv = u2_pic_wd + (UWORD8) (PAD_LEN_UV_H << 2);
1476     u2_frm_wd_uv = MAX(u2_frm_wd_uv, u2_frm_wd_y);
1477     u2_frm_ht_uv = (u2_pic_ht >> 1) + (UWORD8) (PAD_LEN_UV_V << 2);
1478     u2_frm_ht_uv = MAX(u2_frm_ht_uv, (u2_frm_ht_y >> 1));
1479 
1480     /* Calculate display picture width, height and start u4_ofst from YUV420 */
1481     /* pictute buffers as per cropping information parsed above             */
1482     {
1483         UWORD16 u2_rgt_ofst = 0;
1484         UWORD16 u2_lft_ofst = 0;
1485         UWORD16 u2_top_ofst = 0;
1486         UWORD16 u2_btm_ofst = 0;
1487         UWORD8 u1_frm_mbs_flag;
1488         UWORD8 u1_vert_mult_factor;
1489 
1490         if(u1_frame_cropping_flag)
1491         {
1492             /* Calculate right and left u4_ofst for cropped picture           */
1493             u2_rgt_ofst = u1_frame_cropping_rect_right_ofst << 1;
1494             u2_lft_ofst = u1_frame_cropping_rect_left_ofst << 1;
1495 
1496             /* Know frame MBs only u4_flag                                      */
1497             u1_frm_mbs_flag = (1 == ps_seq->u1_frame_mbs_only_flag);
1498 
1499             /* Simplify the vertical u4_ofst calculation from field/frame     */
1500             u1_vert_mult_factor = (2 - u1_frm_mbs_flag);
1501 
1502             /* Calculate bottom and top u4_ofst for cropped  picture          */
1503             u2_btm_ofst = (u1_frame_cropping_rect_bottom_ofst << u1_vert_mult_factor);
1504             u2_top_ofst = (u1_frame_cropping_rect_top_ofst << u1_vert_mult_factor);
1505         }
1506 
1507         /* Calculate u4_ofst from start of YUV 420 picture buffer to start of*/
1508         /* cropped picture buffer                                           */
1509         u2_crop_offset_y = (u2_frm_wd_y * u2_top_ofst) + (u2_lft_ofst);
1510         u2_crop_offset_uv =
1511             (u2_frm_wd_uv * (u2_top_ofst >> 1)) + (u2_lft_ofst >> 1) * YUV420SP_FACTOR;
1512         /* Calculate the display picture width and height based on crop      */
1513         /* information                                                       */
1514         i4_cropped_ht = (WORD32) u2_pic_ht - (WORD32) (u2_btm_ofst + u2_top_ofst);
1515         i4_cropped_wd = (WORD32) u2_pic_wd - (WORD32) (u2_rgt_ofst + u2_lft_ofst);
1516 
1517         if((i4_cropped_ht < MB_SIZE) || (i4_cropped_wd < MB_SIZE))
1518         {
1519             return ERROR_INV_SPS_PPS_T;
1520         }
1521 
1522         if((ps_dec->i4_header_decoded & 1) &&
1523            (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
1524            (ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].u2_pic_wd != u2_pic_wd))
1525         {
1526             ps_dec->u1_res_changed = 1;
1527             return IVD_RES_CHANGED;
1528         }
1529 
1530         if((ps_dec->i4_header_decoded & 1) &&
1531            (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
1532            (ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].u2_disp_width != i4_cropped_wd))
1533         {
1534             ps_dec->u1_res_changed = 1;
1535             return IVD_RES_CHANGED;
1536         }
1537 
1538         if((ps_dec->i4_header_decoded & 1) &&
1539            (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
1540            (ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].u2_pic_ht != u2_pic_ht))
1541         {
1542             ps_dec->u1_res_changed = 1;
1543             return IVD_RES_CHANGED;
1544         }
1545 
1546         if((ps_dec->i4_header_decoded & 1) &&
1547            (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) &&
1548            (ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id].u2_disp_height != i4_cropped_ht))
1549         {
1550             ps_dec->u1_res_changed = 1;
1551             return IVD_RES_CHANGED;
1552         }
1553         /* Check again for unsupported resolutions with updated values*/
1554         if((u2_pic_wd > SVCD_MAX_FRAME_WIDTH) || (u2_pic_ht > SVCD_MAX_FRAME_HEIGHT) ||
1555            (u2_pic_wd < SVCD_MIN_FRAME_WIDTH) || (u2_pic_ht < SVCD_MIN_FRAME_HEIGHT) ||
1556            (u2_pic_wd * (UWORD32) u2_pic_ht > SVCD_MAX_FRAME_SIZE))
1557         {
1558             return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1559         }
1560 
1561         /* If MBAff is enabled, decoder support is limited to streams with
1562          * width less than half of H264_MAX_FRAME_WIDTH.
1563          * In case of MBAff decoder processes two rows at a time
1564          */
1565         if((u2_pic_wd << ps_seq->u1_mb_aff_flag) > H264_MAX_FRAME_WIDTH)
1566         {
1567             return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1568         }
1569     }
1570 
1571     /* Backup num_reorder_frames if header is already decoded */
1572     if((ps_dec->i4_header_decoded & 1) && (1 == ps_seq->u1_vui_parameters_present_flag) &&
1573        (1 == ps_seq->s_vui.u1_bitstream_restriction_flag))
1574     {
1575         num_reorder_frames = (WORD32) ps_seq->s_vui.u4_num_reorder_frames;
1576     }
1577     else
1578     {
1579         num_reorder_frames = -1;
1580     }
1581     if(1 == ps_seq->u1_vui_parameters_present_flag)
1582     {
1583         ret = ih264d_parse_vui_parametres(&ps_seq->s_vui, ps_bitstrm);
1584         if(ret != OK) return ret;
1585     }
1586 
1587     /* Compare older num_reorder_frames with the new one if header is already
1588      * decoded */
1589     if((ps_dec->i4_header_decoded & 1) &&
1590        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_is_valid) && (-1 != num_reorder_frames) &&
1591        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].u1_vui_parameters_present_flag) &&
1592        (1 == ps_dec->ps_sps[u1_seq_parameter_set_id].s_vui.u1_bitstream_restriction_flag) &&
1593        ((WORD32) ps_dec->ps_sps[u1_seq_parameter_set_id].s_vui.u4_num_reorder_frames !=
1594         num_reorder_frames))
1595     {
1596         ps_dec->u1_res_changed = 1;
1597         return IVD_RES_CHANGED;
1598     }
1599 
1600     /* In case bitstream read has exceeded the filled size, then return an error */
1601     if(EXCEED_OFFSET(ps_bitstrm))
1602     {
1603         return ERROR_INV_SPS_PPS_T;
1604     }
1605 
1606     /*--------------------------------------------------------------------*/
1607     /* All initializations to ps_dec are beyond this point                */
1608     /*--------------------------------------------------------------------*/
1609     {
1610         WORD32 reorder_depth = ih264d_get_dpb_size(ps_seq);
1611         if((1 == ps_seq->u1_vui_parameters_present_flag) &&
1612            (1 == ps_seq->s_vui.u1_bitstream_restriction_flag))
1613         {
1614             reorder_depth = ps_seq->s_vui.u4_num_reorder_frames + 1;
1615         }
1616 
1617         if(reorder_depth > H264_MAX_REF_PICS)
1618         {
1619             return ERROR_INV_SPS_PPS_T;
1620         }
1621 
1622         if(ps_seq->u1_frame_mbs_only_flag != 1) reorder_depth *= 2;
1623         ps_subset_seq->i4_reorder_depth = reorder_depth + DISPLAY_LATENCY;
1624     }
1625     ps_subset_seq->u2_disp_height = i4_cropped_ht;
1626     ps_subset_seq->u2_disp_width = i4_cropped_wd;
1627     ps_subset_seq->u2_pic_wd = u2_pic_wd;
1628     ps_subset_seq->u2_pic_ht = u2_pic_ht;
1629 
1630     /* Determining the Width and Height of Frame from that of Picture */
1631     ps_subset_seq->u2_frm_wd_y = u2_frm_wd_y;
1632     ps_subset_seq->u2_frm_ht_y = u2_frm_ht_y;
1633     ps_subset_seq->u2_frm_wd_uv = u2_frm_wd_uv;
1634     ps_subset_seq->u2_frm_ht_uv = u2_frm_ht_uv;
1635 
1636     ps_subset_seq->u1_pad_len_y_v = (UWORD8) (PAD_LEN_Y_V << (1 - u1_frm));
1637     ps_subset_seq->u1_pad_len_cr_v = (UWORD8) (PAD_LEN_UV_V << (1 - u1_frm));
1638 
1639     ps_subset_seq->u2_crop_offset_y = u2_crop_offset_y;
1640     ps_subset_seq->u2_crop_offset_uv = u2_crop_offset_uv;
1641 
1642     if(((ps_dec->u2_pic_wd * ps_dec->u2_pic_ht) <
1643         (ps_subset_seq->u2_pic_wd * ps_subset_seq->u2_pic_ht)) ||
1644        (ps_dec->i4_reorder_depth < ps_subset_seq->i4_reorder_depth))
1645     {
1646         ps_dec->i4_reorder_depth = ps_subset_seq->i4_reorder_depth;
1647 
1648         ps_dec->u2_disp_height = ps_subset_seq->u2_disp_height;
1649         ps_dec->u2_disp_width = ps_subset_seq->u2_disp_width;
1650         ps_dec->u2_pic_wd = ps_subset_seq->u2_pic_wd;
1651         ps_dec->u2_pic_ht = ps_subset_seq->u2_pic_ht;
1652         ps_dec->u4_total_mbs = ps_seq->u2_total_num_of_mbs << (1 - ps_seq->u1_frame_mbs_only_flag);
1653 
1654         /* Determining the Width and Height of Frame from that of Picture */
1655         ps_dec->u2_frm_wd_y = ps_subset_seq->u2_frm_wd_y;
1656         ps_dec->u2_frm_ht_y = ps_subset_seq->u2_frm_ht_y;
1657         ps_dec->u2_frm_wd_uv = ps_subset_seq->u2_frm_wd_uv;
1658         ps_dec->u2_frm_ht_uv = ps_subset_seq->u2_frm_ht_uv;
1659 
1660         ps_dec->s_pad_mgr.u1_pad_len_y_v = ps_subset_seq->u1_pad_len_y_v;
1661         ps_dec->s_pad_mgr.u1_pad_len_cr_v = ps_subset_seq->u1_pad_len_cr_v;
1662 
1663         ps_dec->u2_frm_wd_in_mbs = ps_seq->u2_frm_wd_in_mbs;
1664         ps_dec->u2_frm_ht_in_mbs = ps_seq->u2_frm_ht_in_mbs;
1665         ps_dec->u2_crop_offset_y = ps_subset_seq->u2_crop_offset_y;
1666         ps_dec->u2_crop_offset_uv = ps_subset_seq->u2_crop_offset_uv;
1667     }
1668 
1669     ps_seq->u1_is_valid = TRUE;
1670     ps_dec->ps_sps[u1_seq_parameter_set_id] = *ps_seq;
1671     ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id] = *ps_subset_seq;
1672     ps_dec->ps_cur_sps = &ps_dec->ps_sps[u1_seq_parameter_set_id];
1673     ps_svc_lyr_dec->ps_cur_subset_sps = &ps_svc_lyr_dec->ps_subset_sps[u1_seq_parameter_set_id];
1674 
1675     return OK;
1676 }
1677 
1678 /*!
1679 **************************************************************************
1680 * \if Function name : isvcd_parse_pps \endif
1681 *
1682 * \brief
1683 *    Decodes Picture Parameter set
1684 *
1685 * \return
1686 *    0 on Success and Error code otherwise
1687 **************************************************************************
1688 */
isvcd_parse_pps(svc_dec_lyr_struct_t * ps_svc_lyr_dec,dec_bit_stream_t * ps_bitstrm)1689 WORD32 isvcd_parse_pps(svc_dec_lyr_struct_t *ps_svc_lyr_dec, dec_bit_stream_t *ps_bitstrm)
1690 {
1691     dec_struct_t *ps_dec = &ps_svc_lyr_dec->s_dec;
1692     UWORD8 uc_temp;
1693     dec_seq_params_t *ps_sps = NULL;
1694     dec_pic_params_t *ps_pps = NULL;
1695     UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
1696     UWORD32 *pu4_bitstrm_ofst = &ps_dec->ps_bitstrm->u4_ofst;
1697 
1698     /* Variables used for error resilience checks */
1699     UWORD64 u8_temp;
1700     UWORD32 u4_temp;
1701     WORD32 i_temp;
1702 
1703     /* For High profile related syntax elements */
1704     UWORD8 u1_more_data_flag;
1705     WORD32 i4_i;
1706 
1707     /*--------------------------------------------------------------------*/
1708     /* Decode pic_parameter_set_id and find corresponding pic params      */
1709     /*--------------------------------------------------------------------*/
1710     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1711     if(u4_temp & MASK_ERR_PIC_SET_ID) return ERROR_INV_SPS_PPS_T;
1712     ps_pps = ps_dec->pv_scratch_sps_pps;
1713     *ps_pps = ps_dec->ps_pps[u4_temp];
1714     ps_pps->u1_pic_parameter_set_id = (UWORD8) u4_temp;
1715     COPYTHECONTEXT("PPS: pic_parameter_set_id", ps_pps->u1_pic_parameter_set_id);
1716 
1717     /************************************************/
1718     /* initilization of High profile syntax element */
1719     /************************************************/
1720     ps_pps->i4_transform_8x8_mode_flag = 0;
1721     ps_pps->i4_pic_scaling_matrix_present_flag = 0;
1722 
1723     /*--------------------------------------------------------------------*/
1724     /* Decode seq_parameter_set_id and map it to a seq_parameter_set      */
1725     /*--------------------------------------------------------------------*/
1726     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1727     if(u4_temp & MASK_ERR_SEQ_SET_ID) return ERROR_INV_SPS_PPS_T;
1728     COPYTHECONTEXT("PPS: seq_parameter_set_id", u4_temp);
1729     ps_sps = &ps_dec->ps_sps[u4_temp];
1730     ps_pps->ps_sps = ps_sps;
1731 
1732     /*--------------------------------------------------------------------*/
1733     /* Decode entropy_coding_mode                                         */
1734     /*--------------------------------------------------------------------*/
1735     ps_pps->u1_entropy_coding_mode = ih264d_get_bit_h264(ps_bitstrm);
1736     COPYTHECONTEXT("PPS: entropy_coding_mode_flag", ps_pps->u1_entropy_coding_mode);
1737 
1738     ps_pps->u1_pic_order_present_flag = ih264d_get_bit_h264(ps_bitstrm);
1739     COPYTHECONTEXT("PPS: pic_order_present_flag", ps_pps->u1_pic_order_present_flag);
1740 
1741     /*--------------------------------------------------------------------*/
1742     /* Decode num_slice_groups_minus1                                     */
1743     /*--------------------------------------------------------------------*/
1744     u8_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf) + (UWORD64) 1;
1745     if(u8_temp != 1)
1746     {
1747         return ERROR_FEATURE_UNAVAIL;
1748     }
1749     ps_pps->u1_num_slice_groups = (UWORD8) u8_temp;
1750     COPYTHECONTEXT("PPS: num_slice_groups_minus1", ps_pps->u1_num_slice_groups - 1);
1751 
1752     /*--------------------------------------------------------------------*/
1753     /* Other parameter set values                                         */
1754     /*--------------------------------------------------------------------*/
1755     u8_temp = (UWORD64) 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1756     if(u8_temp >= H264_MAX_REF_IDX) return ERROR_REF_IDX;
1757     ps_pps->u1_num_ref_idx_lx_active[0] = (UWORD8) u8_temp;
1758     COPYTHECONTEXT("PPS: num_ref_idx_l0_active_minus1", ps_pps->u1_num_ref_idx_lx_active[0] - 1);
1759 
1760     u8_temp = (UWORD64) 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1761     if(u8_temp >= H264_MAX_REF_IDX) return ERROR_REF_IDX;
1762     ps_pps->u1_num_ref_idx_lx_active[1] = (UWORD8) u8_temp;
1763     COPYTHECONTEXT("PPS: num_ref_idx_l1_active_minus1", ps_pps->u1_num_ref_idx_lx_active[1] - 1);
1764 
1765     ps_pps->u1_wted_pred_flag = ih264d_get_bit_h264(ps_bitstrm);
1766     COPYTHECONTEXT("PPS: weighted prediction u4_flag", ps_pps->u1_wted_pred_flag);
1767     uc_temp = (UWORD8) ih264d_get_bits_h264(ps_bitstrm, 2);
1768     COPYTHECONTEXT("PPS: weighted_bipred_idc", uc_temp);
1769     ps_pps->u1_wted_bipred_idc = uc_temp;
1770 
1771     if(ps_pps->u1_wted_bipred_idc > MAX_WEIGHT_BIPRED_IDC) return ERROR_INV_SPS_PPS_T;
1772 
1773     {
1774         WORD64 i8_temp = (WORD64) 26 + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1775 
1776         if((i8_temp < MIN_H264_QP) || (i8_temp > MAX_H264_QP)) return ERROR_INV_RANGE_QP_T;
1777 
1778         ps_pps->u1_pic_init_qp = (UWORD8) i8_temp;
1779         COPYTHECONTEXT("PPS: pic_init_qp_minus26", ps_pps->u1_pic_init_qp - 26);
1780 
1781         i8_temp = (WORD64) 26 + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1782         if((i8_temp < MIN_H264_QP) || (i8_temp > MAX_H264_QP)) return ERROR_INV_RANGE_QP_T;
1783 
1784         ps_pps->u1_pic_init_qs = (UWORD8) i8_temp;
1785         COPYTHECONTEXT("PPS: pic_init_qs_minus26", ps_pps->u1_pic_init_qs - 26);
1786     }
1787 
1788     i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1789     if((i_temp < -12) || (i_temp > 12)) return ERROR_INV_RANGE_QP_T;
1790     ps_pps->i1_chroma_qp_index_offset = i_temp;
1791     COPYTHECONTEXT("PPS: chroma_qp_index_offset", ps_pps->i1_chroma_qp_index_offset);
1792 
1793     /***************************************************************************/
1794     /* initialize second_chroma_qp_index_offset to i1_chroma_qp_index_offset if */
1795     /* second_chroma_qp_index_offset is not present in bit-ps_bitstrm */
1796     /***************************************************************************/
1797     ps_pps->i1_second_chroma_qp_index_offset = ps_pps->i1_chroma_qp_index_offset;
1798 
1799     ps_pps->u1_deblocking_filter_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
1800     COPYTHECONTEXT("PPS: deblocking_filter_control_present_flag",
1801                    ps_pps->u1_deblocking_filter_parameters_present_flag);
1802     ps_pps->u1_constrained_intra_pred_flag = ih264d_get_bit_h264(ps_bitstrm);
1803     COPYTHECONTEXT("PPS: constrained_intra_pred_flag", ps_pps->u1_constrained_intra_pred_flag);
1804     ps_pps->u1_redundant_pic_cnt_present_flag = ih264d_get_bit_h264(ps_bitstrm);
1805     COPYTHECONTEXT("PPS: redundant_pic_cnt_present_flag",
1806                    ps_pps->u1_redundant_pic_cnt_present_flag);
1807 
1808     /* High profile related syntax elements */
1809     u1_more_data_flag = MORE_RBSP_DATA(ps_bitstrm);
1810 
1811     if(u1_more_data_flag)
1812     {
1813         /* read transform_8x8_mode_flag  */
1814         ps_pps->i4_transform_8x8_mode_flag = (WORD32) ih264d_get_bit_h264(ps_bitstrm);
1815 
1816         /* read pic_scaling_matrix_present_flag */
1817         ps_pps->i4_pic_scaling_matrix_present_flag = (WORD32) ih264d_get_bit_h264(ps_bitstrm);
1818 
1819         if(ps_pps->i4_pic_scaling_matrix_present_flag)
1820         {
1821             /* read the scaling matrices */
1822             for(i4_i = 0; i4_i < (6 + (ps_pps->i4_transform_8x8_mode_flag << 1)); i4_i++)
1823             {
1824                 ps_pps->u1_pic_scaling_list_present_flag[i4_i] = ih264d_get_bit_h264(ps_bitstrm);
1825 
1826                 if(ps_pps->u1_pic_scaling_list_present_flag[i4_i])
1827                 {
1828                     WORD32 ret;
1829                     if(i4_i < 6)
1830                     {
1831                         ret = ih264d_scaling_list(
1832                             ps_pps->i2_pic_scalinglist4x4[i4_i], 16,
1833                             &ps_pps->u1_pic_use_default_scaling_matrix_flag[i4_i], ps_bitstrm);
1834                     }
1835                     else
1836                     {
1837                         ret = ih264d_scaling_list(
1838                             ps_pps->i2_pic_scalinglist8x8[i4_i - 6], 64,
1839                             &ps_pps->u1_pic_use_default_scaling_matrix_flag[i4_i], ps_bitstrm);
1840                     }
1841 
1842                     if(ret != OK)
1843                     {
1844                         return ret;
1845                     }
1846                 }
1847             }
1848         }
1849 
1850         /* read second_chroma_qp_index_offset syntax element */
1851         i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1852         if((i_temp < -12) || (i_temp > 12)) return ERROR_INV_RANGE_QP_T;
1853 
1854         ps_pps->i1_second_chroma_qp_index_offset = i_temp;
1855     }
1856 
1857     if(SCALABLE_BASELINE_PROFILE_IDC == ps_sps->u1_profile_idc)
1858 
1859     {
1860         if(ps_pps->u1_num_slice_groups > 7)
1861         {
1862             return ERROR_INV_SPS_PPS_T;
1863         }
1864     }
1865 
1866     /* In case bitstream read has exceeded the filled size, then return an error */
1867     if(EXCEED_OFFSET(ps_bitstrm))
1868     {
1869         return ERROR_INV_SPS_PPS_T;
1870     }
1871     ps_pps->u1_is_valid = TRUE;
1872     ps_dec->ps_pps[ps_pps->u1_pic_parameter_set_id] = *ps_pps;
1873     return OK;
1874 }
1875