Merge cherrypicks of [2540686, 2540687, 2540384, 2540688, 2540689, 2540690, 2540385, 2540691, 2540386, 2540387, 2540732, 2540388, 2540389, 2540733, 2540390, 2540752, 2540753, 2540736, 2540737, 2540757, 2540738, 2540758, 2540739, 2540759, 2540740, 2540741, 2540742, 2540743, 2540744, 2540745] into mnc-mr2-release

Change-Id: Ib2251c0cdf1456538de975e9298d8ba237e375ac
diff --git a/decoder/ih264d_api.c b/decoder/ih264d_api.c
index 95fe80a..8262c8b 100644
--- a/decoder/ih264d_api.c
+++ b/decoder/ih264d_api.c
@@ -969,7 +969,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;
@@ -1886,7 +1885,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;
 
@@ -1894,6 +1892,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 de162c7..88ff0ca 100644
--- a/decoder/ih264d_dpb_mgr.c
+++ b/decoder/ih264d_dpb_mgr.c
@@ -1279,11 +1279,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 0758b77..e34a29d 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 ba32225..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;
@@ -496,7 +496,7 @@
     UWORD32 u2_crop_offset_y = 0;
     UWORD32 u2_crop_offset_uv = 0;
     WORD32 ret;
-
+    UWORD32 u4_num_reorder_frames;
     /* High profile related syntax element */
     WORD32 i4_i;
     /* G050 */
@@ -564,10 +564,23 @@
     /*--------------------------------------------------------------------*/
 
     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((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((ps_dec->i4_header_decoded & 1) && (ps_seq->u1_level_idc != u1_level_idc))
+    {
+        ps_dec->u1_res_changed = 1;
+        return IVD_RES_CHANGED;
+    }
 
     ps_seq->u1_profile_idc = u1_profile_idc;
     ps_seq->u1_level_idc = u1_level_idc;
@@ -735,6 +748,14 @@
     {
         return ERROR_NUM_REF;
     }
+
+    /* Compare with older num_ref_frames is header is already once */
+    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;
+    }
+
     ps_seq->u1_num_ref_frames = u4_temp;
     COPYTHECONTEXT("SPS: num_ref_frames",ps_seq->u1_num_ref_frames);
 
@@ -843,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)
         {
@@ -879,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;
@@ -905,12 +925,19 @@
             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((ps_dec->i4_header_decoded & 1) &&
+                    (1 == ps_seq->u1_vui_parameters_present_flag) &&
+                    (1 == ps_seq->s_vui.u1_bitstream_restriction_flag))
+    {
+        u4_num_reorder_frames =  ps_seq->s_vui.u4_num_reorder_frames;
+    }
+    else
+    {
+        u4_num_reorder_frames = -1;
+    }
     if(1 == ps_seq->u1_vui_parameters_present_flag)
     {
         ret = ih264d_parse_vui_parametres(&ps_seq->s_vui, ps_bitstrm);
@@ -918,6 +945,30 @@
             return ret;
     }
 
+    /* Compare older u4_num_reorder_frames with the new one if header is already 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) &&
+                    (ps_seq->s_vui.u4_num_reorder_frames != u4_num_reorder_frames))
+    {
+        ps_dec->u1_res_changed = 1;
+        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;
 
@@ -936,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;
 }
@@ -1112,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 9146292..c89b6cd 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);