Merge "Decoder: Fixed allocation size of pred info buffer" into mnc-dr-dev
diff --git a/decoder/ih264d_api.c b/decoder/ih264d_api.c
index 15e62bc..95fe80a 100644
--- a/decoder/ih264d_api.c
+++ b/decoder/ih264d_api.c
@@ -1376,9 +1376,8 @@
     pu1_buf += size / 2;
     ps_dec->ps_dpb_mgr->ps_init_dpb[1][0] = (struct pic_buffer_t *)pu1_buf;
 
-    size = (sizeof(UWORD32) * 3
-                        * (MAX_FRAMES * MAX_FRAMES))
-                        << 3;
+    size = (sizeof(UWORD32) * 2 * 3
+                        * ((MAX_FRAMES << 1) * (MAX_FRAMES << 1)) * 2);
     pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
     RETURN_IF((NULL == pv_buf), IV_FAIL);
     ps_dec->pu4_mbaff_wt_mat = pv_buf;
@@ -2277,6 +2276,10 @@
                 ps_dec->u1_top_bottom_decoded |= TOP_FIELD_ONLY;
             }
         }
+        else
+        {
+                ps_dec->u1_top_bottom_decoded = TOP_FIELD_ONLY | BOT_FIELD_ONLY;
+        }
 
         /* if new frame in not found (if we are still getting slices from previous frame)
          * ih264d_deblock_display is not called. Such frames will not be added to reference /display
diff --git a/decoder/ih264d_parse_headers.c b/decoder/ih264d_parse_headers.c
index bc4ace4..068ee5b 100644
--- a/decoder/ih264d_parse_headers.c
+++ b/decoder/ih264d_parse_headers.c
@@ -361,11 +361,13 @@
         }
 
         /* read second_chroma_qp_index_offset syntax element */
-        ps_pps->i1_second_chroma_qp_index_offset = ih264d_sev(
+        i_temp = ih264d_sev(
                         pu4_bitstrm_ofst, pu4_bitstrm_buf);
 
-        if((ps_pps->i1_second_chroma_qp_index_offset + 12) > 24)
+        if((i_temp < -12) || (i_temp > 12))
             return ERROR_INV_RANGE_QP_T;
+
+        ps_pps->i1_second_chroma_qp_index_offset = i_temp;
     }
 
     /* In case bitstream read has exceeded the filled size, then
@@ -494,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 */
@@ -567,6 +569,18 @@
     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;
     ps_seq->u1_seq_parameter_set_id = u1_seq_parameter_set_id;
@@ -733,6 +747,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);
 
@@ -877,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;
@@ -909,6 +931,17 @@
 
     }
 
+    /* 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);
@@ -916,6 +949,17 @@
             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;
+    }
+
     ps_dec->u2_pic_wd = u2_pic_wd;
     ps_dec->u2_pic_ht = u2_pic_ht;
 
diff --git a/decoder/ih264d_parse_islice.c b/decoder/ih264d_parse_islice.c
index 0312060..504b775 100644
--- a/decoder/ih264d_parse_islice.c
+++ b/decoder/ih264d_parse_islice.c
@@ -866,6 +866,8 @@
             ps_cur_deblk_mb->u1_mb_qp = ps_dec->u1_qp;
         }
 
+        uc_more_data_flag = MORE_RBSP_DATA(ps_bitstrm);
+
         if(u1_mbaff)
         {
             ih264d_update_mbaff_left_nnz(ps_dec, ps_cur_mb_info);
@@ -879,7 +881,7 @@
         /**************************************************************/
 
         i2_cur_mb_addr++;
-        uc_more_data_flag = MORE_RBSP_DATA(ps_bitstrm);
+
 
         /* Store the colocated information */
         {
@@ -1087,8 +1089,7 @@
             {
                 ih264d_update_mbaff_left_nnz(ps_dec, ps_cur_mb_info);
             }
-            /* Next macroblock information */
-            i2_cur_mb_addr++;
+
 
             if(ps_cur_mb_info->u1_topmb && u1_mbaff)
                 uc_more_data_flag = 1;
@@ -1099,6 +1100,16 @@
                 uc_more_data_flag = !uc_more_data_flag;
                 COPYTHECONTEXT("Decode Sliceterm",!uc_more_data_flag);
             }
+
+            if(u1_mbaff)
+            {
+                if(!uc_more_data_flag && (0 == (i2_cur_mb_addr & 1)))
+                {
+                    return ERROR_EOB_FLUSHBITS_T;
+                }
+            }
+            /* Next macroblock information */
+            i2_cur_mb_addr++;
             /* Store the colocated information */
             {
 
diff --git a/decoder/ih264d_parse_pslice.c b/decoder/ih264d_parse_pslice.c
index a78ea97..97ea27c 100644
--- a/decoder/ih264d_parse_pslice.c
+++ b/decoder/ih264d_parse_pslice.c
@@ -1006,8 +1006,7 @@
         {
             ih264d_update_mbaff_left_nnz(ps_dec, ps_cur_mb_info);
         }
-        /* Next macroblock information */
-        i2_cur_mb_addr++;
+
 
         if(ps_cur_mb_info->u1_topmb && u1_mbaff)
             uc_more_data_flag = 1;
@@ -1019,6 +1018,15 @@
             COPYTHECONTEXT("Decode Sliceterm",!uc_more_data_flag);
         }
 
+        if(u1_mbaff)
+        {
+            if(!uc_more_data_flag && (0 == (i2_cur_mb_addr & 1)))
+            {
+                return ERROR_EOB_FLUSHBITS_T;
+            }
+        }
+        /* Next macroblock information */
+        i2_cur_mb_addr++;
         u1_num_mbs++;
         u1_num_mbsNby2++;
         ps_parse_mb_data++;
diff --git a/decoder/ih264d_parse_slice.c b/decoder/ih264d_parse_slice.c
index f755970..7e0815f 100644
--- a/decoder/ih264d_parse_slice.c
+++ b/decoder/ih264d_parse_slice.c
@@ -1191,10 +1191,6 @@
     u1_nal_unit_type = SLICE_NAL;
     if(u1_is_idr_slice)
     {
-        if(0 == u1_field_pic_flag)
-        {
-            ps_dec->u1_top_bottom_decoded = TOP_FIELD_ONLY | BOT_FIELD_ONLY;
-        }
         u1_nal_unit_type = IDR_SLICE_NAL;
         u4_idr_pic_id = ih264d_uev(pu4_bitstrm_ofst,
                                    pu4_bitstrm_buf);
diff --git a/decoder/ih264d_utils.c b/decoder/ih264d_utils.c
index 96bf1a3..4f6deca 100644
--- a/decoder/ih264d_utils.c
+++ b/decoder/ih264d_utils.c
@@ -1873,6 +1873,7 @@
     size = sizeof(parse_pmbarams_t) * (ps_dec->u1_recon_mb_grp);
     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
     RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
     ps_dec->ps_parse_mb_data = pv_buf;
 
     size = sizeof(parse_part_params_t)