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