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