Merge cherrypicks of [2435782, 2435615, 2435577, 2435653, 2435616, 2435708, 2435617, 2435558, 2435784, 2435709, 2435631, 2435559, 2435560, 2435618, 2435801, 2435674, 2435710, 2435746, 2435579, 2435747, 2435711, 2435785, 2435786, 2435787, 2435713, 2435804, 2435822, 2435842, 2435753, 2435965, 2436024, 2435885] into nyc-mr1-security-e-release

Change-Id: I2e3446ec566e2f41fbc04e05bbf28944c38913e7
diff --git a/decoder/ih264d_api.c b/decoder/ih264d_api.c
index 6940788..2cde456 100644
--- a/decoder/ih264d_api.c
+++ b/decoder/ih264d_api.c
@@ -1002,7 +1002,6 @@
     ps_dec->ps_cur_sps = NULL;
     ps_dec->u1_init_dec_flag = 0;
     ps_dec->u1_first_slice_in_stream = 1;
-    ps_dec->u1_first_pb_nal_in_pic = 1;
     ps_dec->u1_last_pic_not_decoded = 0;
     ps_dec->u4_app_disp_width = 0;
     ps_dec->i4_header_decoded = 0;
@@ -1919,7 +1918,6 @@
     ps_dec->cur_dec_mb_num = 0;
     ps_dec->cur_recon_mb_num = 0;
     ps_dec->u4_first_slice_in_pic = 1;
-    ps_dec->u1_first_pb_nal_in_pic = 1;
     ps_dec->u1_slice_header_done = 0;
     ps_dec->u1_dangling_field = 0;
 
@@ -1927,6 +1925,7 @@
     ps_dec->u4_bs_deblk_thread_created = 0;
     ps_dec->u4_cur_bs_mb_num = 0;
     ps_dec->u4_start_recon_deblk  = 0;
+    ps_dec->u4_sps_cnt_in_process = 0;
 
     DEBUG_THREADS_PRINTF(" Starting process call\n");
 
diff --git a/decoder/ih264d_dpb_mgr.c b/decoder/ih264d_dpb_mgr.c
index e02cc90..0a61ffd 100644
--- a/decoder/ih264d_dpb_mgr.c
+++ b/decoder/ih264d_dpb_mgr.c
@@ -1289,11 +1289,22 @@
                                           u4_cur_pic_num);
                     if(ret != OK)
                         return ret;
-                    ret = ih264d_delete_st_node_or_make_lt(ps_dpb_mgr,
-                                                     u4_cur_pic_num, u4_lt_idx,
-                                                     u1_fld_pic_flag);
-                    if(ret != OK)
-                        return ret;
+
+                    if(ps_dpb_mgr->u1_num_st_ref_bufs > 0)
+
+                    {
+                        ret = ih264d_delete_st_node_or_make_lt(ps_dpb_mgr,
+                                                               u4_cur_pic_num,
+                                                               u4_lt_idx,
+                                                               u1_fld_pic_flag);
+                        if(ret != OK)
+                            return ret;
+                    }
+                    else
+                    {
+                        return ERROR_DBP_MANAGER_T;
+                    }
+
                     u1_marked_lt = 1;
                     break;
                 }
diff --git a/decoder/ih264d_parse_bslice.c b/decoder/ih264d_parse_bslice.c
index 85b0d0f..772964a 100644
--- a/decoder/ih264d_parse_bslice.c
+++ b/decoder/ih264d_parse_bslice.c
@@ -1405,22 +1405,9 @@
         ps_slice->u1_num_ref_idx_lx_active[0] = u4_temp;
         ps_slice->u1_num_ref_idx_lx_active[1] = ui_temp1;
     }
-    /* Initialize the Reference list once in Picture if the slice type    */
-    /* of first slice is between 5 to 9 defined in table 7.3 of standard  */
-    /* If picture contains both P & B slices then Initialize the Reference*/
-    /* List only when it switches from P to B and B to P                     */
 
-    {
-        UWORD8 init_idx_flg = (ps_dec->u1_pr_sl_type
-                        != ps_dec->ps_cur_slice->u1_slice_type);
-        if(ps_dec->u1_first_pb_nal_in_pic
-                        || (init_idx_flg & !ps_dec->u1_sl_typ_5_9)
-                        || ps_dec->u1_num_ref_idx_lx_active_prev
-                                        != ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0])
-            ih264d_init_ref_idx_lx_b(ps_dec);
-        if(ps_dec->u1_first_pb_nal_in_pic & ps_dec->u1_sl_typ_5_9)
-            ps_dec->u1_first_pb_nal_in_pic = 0;
-    }
+
+    ih264d_init_ref_idx_lx_b(ps_dec);
     /* Store the value for future slices in the same picture */
     ps_dec->u1_num_ref_idx_lx_active_prev =
                     ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0];
diff --git a/decoder/ih264d_parse_headers.c b/decoder/ih264d_parse_headers.c
index 3807a14..d8c37a6 100644
--- a/decoder/ih264d_parse_headers.c
+++ b/decoder/ih264d_parse_headers.c
@@ -484,7 +484,7 @@
     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
     UWORD8 u1_frm, uc_constraint_set0_flag, uc_constraint_set1_flag;
-
+    WORD32 i4_cropped_ht, i4_cropped_wd;
     UWORD32 u4_temp;
     WORD32 pic_height_in_map_units_minus1 = 0;
     UWORD32 u2_pic_wd = 0;
@@ -564,18 +564,19 @@
     /*--------------------------------------------------------------------*/
 
     ps_seq = ps_dec->pv_scratch_sps_pps;
-    *ps_seq = ps_dec->ps_sps[u1_seq_parameter_set_id];
+    if(ps_dec->i4_header_decoded & 1)
+    {
+        *ps_seq = *ps_dec->ps_cur_sps;
+    }
 
-    if(NULL == ps_dec->ps_cur_sps)
-        ps_dec->ps_cur_sps = ps_seq;
 
-    if((3 == ps_dec->i4_header_decoded) && (ps_seq->u1_profile_idc != u1_profile_idc))
+    if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_profile_idc != u1_profile_idc))
     {
         ps_dec->u1_res_changed = 1;
         return IVD_RES_CHANGED;
     }
 
-    if((3 == ps_dec->i4_header_decoded) && (ps_seq->u1_level_idc != u1_level_idc))
+    if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_level_idc != u1_level_idc))
     {
         ps_dec->u1_res_changed = 1;
         return IVD_RES_CHANGED;
@@ -749,7 +750,7 @@
     }
 
     /* Compare with older num_ref_frames is header is already once */
-    if((3 == ps_dec->i4_header_decoded) && (ps_seq->u1_num_ref_frames != u4_temp))
+    if((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_num_ref_frames != u4_temp))
     {
         ps_dec->u1_res_changed = 1;
         return IVD_RES_CHANGED;
@@ -863,7 +864,6 @@
         UWORD16 u2_btm_ofst = 0;
         UWORD8 u1_frm_mbs_flag;
         UWORD8 u1_vert_mult_factor;
-        WORD32 i4_cropped_ht, i4_cropped_wd;
 
         if(u1_frame_cropping_flag)
         {
@@ -899,12 +899,12 @@
             return ERROR_INV_SPS_PPS_T;
         }
 
-        if((3 == ps_dec->i4_header_decoded) && (ps_dec->u2_pic_wd != u2_pic_wd))
+        if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_pic_wd != u2_pic_wd))
         {
             ps_dec->u1_res_changed = 1;
             return IVD_RES_CHANGED;
         }
-        if((3 == ps_dec->i4_header_decoded) && (ps_dec->u2_pic_ht != u2_pic_ht))
+        if((ps_dec->i4_header_decoded & 1) && (ps_dec->u2_pic_ht != u2_pic_ht))
         {
             ps_dec->u1_res_changed = 1;
             return IVD_RES_CHANGED;
@@ -925,14 +925,10 @@
             return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
         }
 
-        ps_dec->u2_disp_height = i4_cropped_ht;
-
-        ps_dec->u2_disp_width = i4_cropped_wd;
-
     }
 
     /* Backup u4_num_reorder_frames if header is already decoded */
-    if((3 == ps_dec->i4_header_decoded) &&
+    if((ps_dec->i4_header_decoded & 1) &&
                     (1 == ps_seq->u1_vui_parameters_present_flag) &&
                     (1 == ps_seq->s_vui.u1_bitstream_restriction_flag))
     {
@@ -950,7 +946,7 @@
     }
 
     /* Compare older u4_num_reorder_frames with the new one if header is already decoded */
-    if((3 == ps_dec->i4_header_decoded) &&
+    if((ps_dec->i4_header_decoded & 1) &&
                     (-1 != (WORD32)u4_num_reorder_frames) &&
                     (1 == ps_seq->u1_vui_parameters_present_flag) &&
                     (1 == ps_seq->s_vui.u1_bitstream_restriction_flag) &&
@@ -960,6 +956,19 @@
         return IVD_RES_CHANGED;
     }
 
+    /* In case bitstream read has exceeded the filled size, then
+     return an error */
+    if (ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst)
+    {
+        return ERROR_INV_SPS_PPS_T;
+    }
+
+    /*--------------------------------------------------------------------*/
+    /* All initializations to ps_dec are beyond this point                */
+    /*--------------------------------------------------------------------*/
+    ps_dec->u2_disp_height = i4_cropped_ht;
+    ps_dec->u2_disp_width = i4_cropped_wd;
+
     ps_dec->u2_pic_wd = u2_pic_wd;
     ps_dec->u2_pic_ht = u2_pic_ht;
 
@@ -978,14 +987,9 @@
     ps_dec->u2_crop_offset_y = u2_crop_offset_y;
     ps_dec->u2_crop_offset_uv = u2_crop_offset_uv;
 
-    /* In case bitstream read has exceeded the filled size, then
-       return an error */
-    if(ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst)
-    {
-        return ERROR_INV_SPS_PPS_T;
-    }
     ps_seq->u1_is_valid = TRUE;
     ps_dec->ps_sps[u1_seq_parameter_set_id] = *ps_seq;
+    ps_dec->ps_cur_sps = &ps_dec->ps_sps[u1_seq_parameter_set_id];
 
     return OK;
 }
@@ -1154,6 +1158,16 @@
                     /* ! */
                     ih264d_rbsp_to_sodb(ps_dec->ps_bitstrm);
                     i_status = ih264d_parse_sps(ps_dec, ps_bitstrm);
+                    ps_dec->u4_sps_cnt_in_process++;
+                    /*If a resolution change happens within a process call, due to multiple sps
+                     * we will not support it.
+                     */
+                    if((ps_dec->u4_sps_cnt_in_process > 1 ) &&
+                                    (i_status == IVD_RES_CHANGED))
+                    {
+                        i_status = ERROR_INV_SPS_PPS_T;
+                        ps_dec->u1_res_changed = 0;
+                    }
                     if(i_status == ERROR_INV_SPS_PPS_T)
                         return i_status;
                     if(!i_status)
diff --git a/decoder/ih264d_parse_pslice.c b/decoder/ih264d_parse_pslice.c
index 97ea27c..bcfbe05 100644
--- a/decoder/ih264d_parse_pslice.c
+++ b/decoder/ih264d_parse_pslice.c
@@ -1977,23 +1977,7 @@
         UWORD8 uc_refIdxReFlagL0 = ih264d_get_bit_h264(ps_bitstrm);
         COPYTHECONTEXT("SH: ref_pic_list_reordering_flag_l0",uc_refIdxReFlagL0);
 
-        /* Initialize the Reference list once in Picture if the slice type    */
-        /* of first slice is between 5 to 9 defined in table 7.3 of standard  */
-        /* If picture contains both P & B slices then Initialize the Reference*/
-        /* List only when it switches from P to B and B to P                     */
-        {
-            UWORD8 init_idx_flg = (ps_dec->u1_pr_sl_type
-                            != ps_dec->ps_cur_slice->u1_slice_type);
-            if(ps_dec->u1_first_pb_nal_in_pic
-                            || (init_idx_flg & !ps_dec->u1_sl_typ_5_9)
-                            || ps_dec->u1_num_ref_idx_lx_active_prev
-                                            != ps_cur_slice->u1_num_ref_idx_lx_active[0])
-            {
-                ih264d_init_ref_idx_lx_p(ps_dec);
-            }
-            if(ps_dec->u1_first_pb_nal_in_pic & ps_dec->u1_sl_typ_5_9)
-                ps_dec->u1_first_pb_nal_in_pic = 0;
-        }
+        ih264d_init_ref_idx_lx_p(ps_dec);
         /* Store the value for future slices in the same picture */
         ps_dec->u1_num_ref_idx_lx_active_prev =
                         ps_cur_slice->u1_num_ref_idx_lx_active[0];
diff --git a/decoder/ih264d_parse_slice.c b/decoder/ih264d_parse_slice.c
index 7e0815f..fad2dff 100644
--- a/decoder/ih264d_parse_slice.c
+++ b/decoder/ih264d_parse_slice.c
@@ -284,10 +284,8 @@
 
     {
         dec_err_status_t * ps_err = ps_dec->ps_dec_err_status;
-        if(ps_dec->u1_sl_typ_5_9
-                        && ((ps_cur_slice->u1_slice_type == I_SLICE)
-                                        || (ps_cur_slice->u1_slice_type
-                                                        == SI_SLICE)))
+        if((ps_cur_slice->u1_slice_type == I_SLICE)
+                        || (ps_cur_slice->u1_slice_type == SI_SLICE))
             ps_err->u1_cur_pic_type = PIC_TYPE_I;
         else
             ps_err->u1_cur_pic_type = PIC_TYPE_UNKNOWN;
@@ -1034,14 +1032,12 @@
 
     u1_slice_type = u4_temp;
     COPYTHECONTEXT("SH: slice_type",(u1_slice_type));
-    ps_dec->u1_sl_typ_5_9 = 0;
     /* Find Out the Slice Type is 5 to 9 or not then Set the Flag   */
     /* u1_sl_typ_5_9 = 1 .Which tells that all the slices in the Pic*/
     /* will be of same type of current                            */
     if(u1_slice_type > 4)
     {
         u1_slice_type -= 5;
-        ps_dec->u1_sl_typ_5_9 = 1;
     }
 
     {
diff --git a/decoder/ih264d_structs.h b/decoder/ih264d_structs.h
index 4260393..c83c34e 100644
--- a/decoder/ih264d_structs.h
+++ b/decoder/ih264d_structs.h
@@ -897,11 +897,9 @@
 
     /* slice Header Simplification */
     UWORD8 u1_pr_sl_type;
-    UWORD8 u1_sl_typ_5_9;
     WORD32 i4_frametype;
     UWORD32 u4_app_disp_width;
     WORD32 i4_error_code;
-    UWORD8 u1_first_pb_nal_in_pic;
     UWORD32 u4_bitoffset;
 
     /* Variables added to handle field pics */
@@ -1249,6 +1247,7 @@
     UWORD32 u4_cur_bs_mb_num;
     UWORD32 u4_bs_cur_slice_num_mbs;
     UWORD32 u4_cur_deblk_mb_num;
+    UWORD32 u4_sps_cnt_in_process;
     volatile UWORD16 u2_cur_slice_num_bs;
 
     UWORD32 u4_deblk_mb_x;
diff --git a/decoder/ih264d_utils.c b/decoder/ih264d_utils.c
index c64ee8a..4f6deca 100644
--- a/decoder/ih264d_utils.c
+++ b/decoder/ih264d_utils.c
@@ -1999,10 +1999,7 @@
 
     /* Allocate memory for packed pred info */
     num_entries = u4_total_mbs;
-    if(1 == ps_dec->ps_cur_sps->u1_num_ref_frames)
-        num_entries *= 16;
-    else
-        num_entries *= 16 * 2;
+    num_entries *= 16 * 2;
 
     size = sizeof(pred_info_pkd_t) * num_entries;
     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);