resolve merge conflicts of 7899c8cffa to mnc-dev.
am: b3a53cebdb

* commit 'b3a53cebdb51e50fe7ddd670b306271268f19d18':
  Aligned ps_pic_pu to 4 bytes
diff --git a/common/arm/ihevc_intra_pred_filters_neon_intr.c b/common/arm/ihevc_intra_pred_filters_neon_intr.c
index 0e89de3..2564ffa 100644
--- a/common/arm/ihevc_intra_pred_filters_neon_intr.c
+++ b/common/arm/ihevc_intra_pred_filters_neon_intr.c
@@ -466,7 +466,7 @@
     WORD32 four_nt = 4 * nt;
 
     WORD32 src_4nt;
-
+    WORD32 src_0nt;
     /* Naming has been made as per the functionlity it has, For eg. pu1_src_tmp_1 is denoting pu1_src + 1   */
     /* src_val_1 to load value from pointer pu1_src_tmp_1, add_res has the result of adding 2 values        */
     UWORD8 *pu1_src_tmp_0 = pu1_src;
@@ -522,6 +522,7 @@
         }
 
         src_4nt = pu1_src[4 * nt];
+        src_0nt = pu1_src[0];
         /* Strong filtering of reference samples */
         if(1 == bi_linear_int_flag)
         {
@@ -616,7 +617,7 @@
             pu1_dst_tmp_0 += 8;
         }
         pu1_dst[4 * nt] = src_4nt;
-
+        pu1_dst[0] = src_0nt;
     }
 
 }
diff --git a/common/arm/ihevc_intra_ref_substitution_a9q.c b/common/arm/ihevc_intra_ref_substitution_a9q.c
index 3270786..d090d84 100644
--- a/common/arm/ihevc_intra_ref_substitution_a9q.c
+++ b/common/arm/ihevc_intra_ref_substitution_a9q.c
@@ -571,14 +571,27 @@
                 ihevc_memset_mul_8_a9q(&pu1_dst[two_nt - 1 - (nt - 1)], 0, nt);
             }
 
-            if(nbr_flags & 0xC)
+            /* Bottom - left availability is checked for every 8x8 TU position and set accordingly */
             {
-                for(i = nt; i < two_nt; i++)
+                if(nbr_flags & 0x8)
+                {
+                    for(i = nt; i < (nt + 8); i++)
                     pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
-            }
-            else
-            {
-                ihevc_memset_mul_8_a9q(&pu1_dst[two_nt - 1 - (two_nt - 1)], 0, nt);
+                }
+                else
+                {
+                    ihevc_memset_mul_8_a9q(&pu1_dst[nt - 8], 0, 8);
+                }
+
+                if(nbr_flags & 0x4)
+                {
+                    for(i = (nt + 8); i < two_nt; i++)
+                        pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
+                }
+                else
+                {
+                    ihevc_memset_mul_8_a9q(&pu1_dst[0], 0, 8);
+                }
             }
 
 
@@ -682,16 +695,48 @@
                 ihevc_memset_mul_8_a9q(&pu1_dst[two_nt - 1 - (nt - 1)], 0, nt);
             }
 
-            if(nbr_flags & 0xF)
+            /* Bottom - left availability is checked for every 8x8 TU position and set accordingly */
             {
-                for(i = nt; i < two_nt; i++)
+                if(nbr_flags & 0x8)
+                {
+                    for(i = nt; i < (nt + 8); i++)
                     pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
-            }
-            else
-            {
-                ihevc_memset_mul_8_a9q(&pu1_dst[two_nt - 1 - (two_nt - 1)], 0, nt);
-            }
+                }
+                else
+                {
+                    ihevc_memset_mul_8_a9q(&pu1_dst[24], 0, 8);
+                }
 
+                if(nbr_flags & 0x4)
+                {
+                    for(i = (nt + 8); i < (nt + 16); i++)
+                        pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
+                }
+                else
+                {
+                    ihevc_memset_mul_8_a9q(&pu1_dst[16], 0, 8);
+                }
+
+                if(nbr_flags & 0x2)
+                {
+                    for(i = (nt + 16); i < (nt + 24); i++)
+                        pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
+                }
+                else
+                {
+                    ihevc_memset_mul_8_a9q(&pu1_dst[8], 0, 8);
+                }
+
+                if(nbr_flags & 0x1)
+                {
+                    for(i = (nt + 24); i < (two_nt); i++)
+                        pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
+                }
+                else
+                {
+                    ihevc_memset_mul_8_a9q(&pu1_dst[0], 0, 8);
+                }
+            }
 
             if(nbr_flags & 0xF00)
             {
diff --git a/common/arm/ihevc_platform_macros.h b/common/arm/ihevc_platform_macros.h
index fc08ba0..6d48cd9 100644
--- a/common/arm/ihevc_platform_macros.h
+++ b/common/arm/ihevc_platform_macros.h
@@ -116,6 +116,8 @@
 
 #define INLINE inline
 
+#define POPCNT_U32(x)       __builtin_popcount(x)
+
 static INLINE UWORD32 CLZ(UWORD32 u4_word)
 {
     if(u4_word)
@@ -124,6 +126,11 @@
         return 32;
 }
 
+static INLINE UWORD32 CLZNZ(UWORD32 u4_word)
+{
+   return (__builtin_clz(u4_word));
+}
+
 static INLINE UWORD32 CTZ(UWORD32 u4_word)
 {
     if(0 == u4_word)
diff --git a/common/ihevc_buf_mgr.c b/common/ihevc_buf_mgr.c
index b6e4f2a..93340ff 100644
--- a/common/ihevc_buf_mgr.c
+++ b/common/ihevc_buf_mgr.c
@@ -43,10 +43,12 @@
 *******************************************************************************
 */
 #include <stdlib.h>
+#include <assert.h>
 #include "ihevc_typedefs.h"
 #include "ihevc_macros.h"
 #include "ihevc_func_selector.h"
 #include "ihevc_buf_mgr.h"
+#include "ihevc_debug.h"
 
 
 /**
@@ -165,6 +167,8 @@
     pv_ret_ptr = NULL;
     for(id = 0; id < (WORD32)ps_buf_mgr->u4_max_buf_cnt; id++)
     {
+        ASSERT(ps_buf_mgr->au4_status[id] != 2);
+
         /* Check if the buffer is non-null and status is zero */
         if((ps_buf_mgr->au4_status[id] == 0) && (ps_buf_mgr->apv_ptr[id]))
         {
@@ -206,6 +210,8 @@
 
     for(id = 0; id < ps_buf_mgr->u4_max_buf_cnt; id++)
     {
+        ASSERT(ps_buf_mgr->au4_status[id] != 2);
+
         if((ps_buf_mgr->au4_status[id] == 0) &&
            (ps_buf_mgr->apv_ptr[id]))
         {
@@ -256,6 +262,7 @@
     }
 
     ps_buf_mgr->au4_status[buf_id] &= ~mask;
+    ASSERT(ps_buf_mgr->au4_status[buf_id] != 2);
 
     /* If both the REF and DISP are zero, DEC is set to zero */
     if(ps_buf_mgr->au4_status[buf_id] == 1)
@@ -312,6 +319,7 @@
     }
 
     ps_buf_mgr->au4_status[buf_id] |= mask;
+    ASSERT(ps_buf_mgr->au4_status[buf_id] != 2);
     return 0;
 }
 
diff --git a/common/ihevc_chroma_itrans_recon.h b/common/ihevc_chroma_itrans_recon.h
index c20cebf..7d4958b 100644
--- a/common/ihevc_chroma_itrans_recon.h
+++ b/common/ihevc_chroma_itrans_recon.h
@@ -102,6 +102,11 @@
 ihevc_chroma_itrans_recon_16x16_ft ihevc_chroma_itrans_recon_16x16;
 ihevc_hbd_chroma_itrans_recon_16x16_ft ihevc_hbd_chroma_itrans_recon_16x16;
 
+/* A9 Q Function Declarations */
+ihevc_chroma_itrans_recon_4x4_ft ihevc_chroma_itrans_recon_4x4_a9q;
+ihevc_chroma_itrans_recon_8x8_ft ihevc_chroma_itrans_recon_8x8_a9q;
+ihevc_chroma_itrans_recon_16x16_ft ihevc_chroma_itrans_recon_16x16_a9q;
+
 ihevc_hbd_chroma_itrans_recon_4x4_ft ihevc_hbd_chroma_itrans_recon_4x4_sse42;
 ihevc_hbd_chroma_itrans_recon_8x8_ft ihevc_hbd_chroma_itrans_recon_8x8_sse42;
 ihevc_hbd_chroma_itrans_recon_16x16_ft ihevc_hbd_chroma_itrans_recon_16x16_sse42;
diff --git a/common/ihevc_defs.h b/common/ihevc_defs.h
index 7f58121..a2b7eda 100644
--- a/common/ihevc_defs.h
+++ b/common/ihevc_defs.h
@@ -210,6 +210,16 @@
     VID_FMT_UNSPECIFIED
 };
 
+typedef enum {
+    USER_DATA_BAR_DATA       = 0,
+    USER_DATA_CC_DATA        = 1,
+    USER_DATA_AFD_DATA       = 2,
+
+    // do not add anything below
+    USER_DATA_MAX
+} USER_DATA_SEI_TYPE_T;
+
+
 #define BIT_DEPTH           8
 #define BIT_DEPTH_LUMA      BIT_DEPTH
 #define BIT_DEPTH_CHROMA    BIT_DEPTH
@@ -246,10 +256,10 @@
 /* Tile restrictions                                                         */
 /*****************************************************************************/
 /* Minimum tile width in Main Profile */
-#define MIN_TILE_WD  MIN_CTB_SIZE
+#define MIN_TILE_WD  MAX_CTB_SIZE
 
 /* Minimum tile height in Main Profile */
-#define MIN_TILE_HT  MIN_CTB_SIZE
+#define MIN_TILE_HT  MAX_CTB_SIZE
 
 /*****************************************************************************/
 /* SPS restrictions                                                          */
@@ -455,5 +465,9 @@
 #define INTRA_PRED_CHROMA_IDX_NONE  7
 
 
+#define MAX_NUM_CLOCK_TS    3
+#define MAX_USERDATA_PAYLOAD 256
+
+#define MAX_CPB_CNT 32
 
 #endif /*__IHEVC_DEFS_H_*/
diff --git a/common/ihevc_func_types.h b/common/ihevc_func_types.h
index f5a2c44..74de30e 100644
--- a/common/ihevc_func_types.h
+++ b/common/ihevc_func_types.h
@@ -45,7 +45,7 @@
 #define    CXAINTR      10
 
 /* Neon intrinsics */
-#define    NEONINTR     0
+#define    NEONINTR     11
 
 /* X86 intrinsics */
 #define    X86INTR      12
diff --git a/common/ihevc_intra_pred_filters.c b/common/ihevc_intra_pred_filters.c
index ea81d00..abb219a 100644
--- a/common/ihevc_intra_pred_filters.c
+++ b/common/ihevc_intra_pred_filters.c
@@ -374,16 +374,28 @@
                 ihevc_memset_mul_8(&pu1_dst[two_nt - 1 - (nt - 1)], 0, nt);
             }
 
-            if(nbr_flags & 0xC)
+            /* Bottom - left availability is checked for every 8x8 TU position and set accordingly */
             {
-                for(i = nt; i < two_nt; i++)
+                if(nbr_flags & 0x8)
+                {
+                    for(i = nt; i < (nt + 8); i++)
                     pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
-            }
-            else
-            {
-                ihevc_memset_mul_8(&pu1_dst[two_nt - 1 - (two_nt - 1)], 0, nt);
-            }
+                }
+                else
+                {
+                    ihevc_memset_mul_8(&pu1_dst[nt - 8], 0, 8);
+                }
 
+                if(nbr_flags & 0x4)
+                {
+                    for(i = (nt + 8); i < two_nt; i++)
+                        pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
+                }
+                else
+                {
+                    ihevc_memset_mul_8(&pu1_dst[0], 0, 8);
+                }
+            }
 
             if(nbr_flags & 0x300)
             {
@@ -485,14 +497,47 @@
                 ihevc_memset_mul_8(&pu1_dst[two_nt - 1 - (nt - 1)], 0, nt);
             }
 
-            if(nbr_flags & 0xF)
+            /* Bottom - left availability is checked for every 8x8 TU position and set accordingly */
             {
-                for(i = nt; i < two_nt; i++)
+                if(nbr_flags & 0x8)
+                {
+                    for(i = nt; i < (nt + 8); i++)
                     pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
-            }
-            else
-            {
-                ihevc_memset_mul_8(&pu1_dst[two_nt - 1 - (two_nt - 1)], 0, nt);
+                }
+                else
+                {
+                    ihevc_memset_mul_8(&pu1_dst[24], 0, 8);
+                }
+
+                if(nbr_flags & 0x4)
+                {
+                    for(i = (nt + 8); i < (nt + 16); i++)
+                        pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
+                }
+                else
+                {
+                    ihevc_memset_mul_8(&pu1_dst[16], 0, 8);
+                }
+
+                if(nbr_flags & 0x2)
+                {
+                    for(i = (nt + 16); i < (nt + 24); i++)
+                        pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
+                }
+                else
+                {
+                    ihevc_memset_mul_8(&pu1_dst[8], 0, 8);
+                }
+
+                if(nbr_flags & 0x1)
+                {
+                    for(i = (nt + 24); i < (two_nt); i++)
+                        pu1_dst[two_nt - 1 - i] = pu1_left[i * src_strd];
+                }
+                else
+                {
+                    ihevc_memset_mul_8(&pu1_dst[0], 0, 8);
+                }
             }
 
 
diff --git a/common/ihevc_structs.h b/common/ihevc_structs.h
index 26c1a39..93d2ad4 100644
--- a/common/ihevc_structs.h
+++ b/common/ihevc_structs.h
@@ -38,6 +38,870 @@
 #ifndef _IHEVC_STRUCTS_H_
 #define _IHEVC_STRUCTS_H_
 
+
+/**
+ * Buffering Period SEI parameters Info
+ */
+typedef struct
+{
+    /**
+     * specifies SPS Id active for the coded picture assosiated
+     * with the bp message.
+     */
+    UWORD8  u1_bp_seq_parameter_set_id;
+
+    /**
+     * Derived from Hrd parameters
+     */
+    UWORD8  u1_sub_pic_cpb_params_present_flag;
+
+    /**
+     * specifies the presence of the initial_alt_cpb_removal_delay[ i ]
+     * and initial_alt_cpb_removal_offset[ i ] syntax elements
+     */
+    UWORD8  u1_rap_cpb_params_present_flag;
+
+    /**
+     * cbp removal delay used in buffering period SEI
+     */
+    UWORD32 u4_cpb_delay_offset;
+
+    /**
+     * dbp removal delay used in buffering period SEI
+     */
+    UWORD32 u4_dpb_delay_offset;
+
+    /**
+     * concatanation flag
+     */
+    UWORD8 u1_concatenation_flag;
+
+    /**
+     * delata cbp removal delay
+     */
+    UWORD32 u4_au_cpb_removal_delay_delta_minus1;
+
+    /**
+     * specify the default initial CPB removal delays, respectively,
+     * for the CPB when the NAL HRD parameters are in use
+     */
+    UWORD32 au4_nal_initial_cpb_removal_delay[MAX_CPB_CNT];
+
+    /**
+     * specify the alternate initial CPB removal delays, respectively,
+     * for the CPB when the NAL HRD parameters are in use
+     */
+    UWORD32 au4_nal_initial_alt_cpb_removal_delay[MAX_CPB_CNT];
+
+    /**
+     * specify the initial CPB removal delay offset, respectively,
+     * for the CPB when the NAL HRD parameters are in use
+     */
+    UWORD32 au4_nal_initial_cpb_removal_delay_offset[MAX_CPB_CNT];
+
+    /**
+     * specify the alternate initial CPB removal delays offsets, respectively,
+     * for the CPB when the NAL HRD parameters are in use
+     */
+    UWORD32 au4_nal_initial_alt_cpb_removal_delay_offset[MAX_CPB_CNT];
+
+    /**
+     * specify the default initial CPB removal delays, respectively,
+     * for the CPB when the VCL HRD parameters are in use
+     */
+    UWORD32 au4_vcl_initial_cpb_removal_delay[MAX_CPB_CNT];
+
+    /**
+     * specify the initial alt CPB removal delays , respectively,
+     * for the CPB when the VCL HRD parameters are in use
+     */
+    UWORD32 au4_vcl_initial_alt_cpb_removal_delay[MAX_CPB_CNT];
+
+    /**
+     * specify the initial CPB removal delay offset, respectively,
+     * for the CPB when the VCL HRD parameters are in use
+     */
+    UWORD32 au4_vcl_initial_cpb_removal_delay_offset[MAX_CPB_CNT];
+
+    /**
+     * specify the alternate initial CPB removal delays offsets, respectively,
+     * for the CPB when the VCL HRD parameters are in use
+     */
+    UWORD32 au4_vcl_initial_alt_cpb_removal_delay_offset[MAX_CPB_CNT];
+
+    /**
+     * Inital CPB removal delay
+     */
+    UWORD32 u4_initial_cpb_removal_delay_length;
+
+    /**
+     * CPB cnt for corr. sublayer
+     */
+    UWORD32 u4_cpb_cnt;
+
+
+    /**
+     * VBV buffer size used in buffering period SEI
+     */
+    UWORD32 u4_buffer_size_sei;
+
+    /**
+     * Encoder buffer fullness  used in buffering period SEI
+     */
+    UWORD32 u4_dbf_sei;
+
+    /**
+     * target bitrate used in buffering period SEI
+     */
+    UWORD32 u4_target_bit_rate_sei;
+
+}buf_period_sei_params_t;
+
+
+/**
+ * Picture Timing SEI parameters Info
+ */
+typedef struct
+{
+    /**
+     * derived from vui parameters
+     */
+    UWORD8 u1_frame_field_info_present_flag;
+
+    /**
+     * indicates whether a picture should be displayed as a
+     * frame or as one or more fields
+     */
+    UWORD32 u4_pic_struct;
+
+    UWORD32 u4_source_scan_type;
+
+    /**
+     * if 1, indicates if the current pic is a duplicte pic in output order
+     */
+    UWORD8 u1_duplicate_flag;
+
+    /**
+     * specifies the number clock ticks between the nominal CPB removal time
+     * au associated with the pt SEI message and
+     * the preceding au in decoding order that contained a bp SEI message
+     */
+    UWORD32 u4_au_cpb_removal_delay_minus1;
+
+    /**
+     * compute the DPB output time of the picture
+     */
+    UWORD32 u4_pic_dpb_output_delay;
+
+    UWORD32 u4_pic_dpb_output_du_delay;
+
+    /**
+     * specifies the number of decoding units in the access unit
+     * the picture timing SEI message is associated with
+     */
+    UWORD32 u4_num_decoding_units_minus1;
+
+    /**
+     * if 1 specifies that the du_common_cpb_removal_delay_increment_minus1 is present
+     */
+    UWORD8 u1_du_common_cpb_removal_delay_flag;
+
+    /**
+     * specifies the duration, in units of clock sub-ticks,
+     * between the nominal CPB removal times of any two consecutive decoding units
+     * in decoding order in the access unit associated with the pt_SEI message
+     */
+    UWORD32 u4_du_common_cpb_removal_delay_increment_minus1; //same as u4_du_cpb_removal_delay_increment_minus1
+
+    /**
+     * specifies the number of NAL units in the decoding unit of the access unit
+     * the picture timing SEI message is associated with.
+     * range from 0 to (pic size in ctby - 1)
+     */
+    UWORD32 au4_num_nalus_in_du_minus1[4320 / MIN_CTB_SIZE];
+
+    /**
+     * specifies the duration, in units of clock sub-ticks,
+     * between the nominal CPB removal times of the ( i + 1 )-th decoding unit and the i-th decoding unit,
+     * in decoding order, in the access unit associated with the pt_SEI message
+     */
+    UWORD32 au4_du_cpb_removal_delay_increment_minus1[4320 / MIN_CTB_SIZE];
+
+}pic_timing_sei_params_t;
+
+/**
+ * Structure to hold Recovery point SEI parameters Info
+ */
+typedef struct
+{
+    /**
+     * specifies the recovery point of output pictures in output order
+     */
+    WORD32 i4_recovery_poc_cnt;
+
+    UWORD8 u1_exact_match_flag;
+
+    /**
+     * indicates the presence or absence of a broken link in the NAL unit
+     * stream at the location of the recovery point SEI message
+     */
+
+    UWORD8 u1_broken_link_flag;
+
+}recovery_point_sei_params_t;
+
+/**
+ * Structure to hold Mastering Display Colour Volume SEI
+ */
+typedef struct
+{
+    /**
+     * Array to store the display_primaries_x values
+     */
+    UWORD16 au2_display_primaries_x[3];
+
+    /**
+     * Array to store the display_primaries_y values
+     */
+    UWORD16 au2_display_primaries_y[3];
+
+    /**
+     * Variable to store the white point x value
+     */
+    UWORD16 u2_white_point_x;
+
+    /**
+     * Variable to store the white point y value
+     */
+    UWORD16 u2_white_point_y;
+
+    /**
+     * Variable to store the max display mastering luminance value
+     */
+    UWORD32 u4_max_display_mastering_luminance;
+
+    /**
+     * Variable to store the min display mastering luminance value
+     */
+    UWORD32 u4_min_display_mastering_luminance;
+
+}mastering_dis_col_vol_sei_params_t;
+
+/**
+ * Structure to hold active parameter parameter set SEI parameters Info
+ */
+typedef struct
+{
+    /*
+    * active vps id
+    */
+
+    UWORD8 u1_active_video_parameter_set_id;
+
+    /*
+     * default set to zero.
+     */
+    UWORD8 u1_self_contained_cvs_flag;
+
+    UWORD8 u1_no_parameter_set_update_flag;
+
+    UWORD8 u1_num_sps_ids_minus1;
+
+    /*
+     * active sps id
+     */
+    UWORD8 au1_active_seq_parameter_set_id[15];
+
+    UWORD32 au4_layer_sps_idx[64];
+
+}active_parameter_set_sei_param_t;
+
+/**
+ * Structure to hold SEI Hash values
+ */
+typedef struct
+{
+    /*
+     * SEI Hash values for each color component
+     */
+    UWORD8 au1_sei_hash[3][16];
+
+}hash_sei_param_t;
+
+/**
+ * Structure to hold user data registered SEI param Info
+ */
+typedef struct
+{
+    /**
+     * Contains country code by Annex A of Recommendation ITU-T T.35
+     */
+    UWORD8 u1_itu_t_t35_country_code;
+
+    /**
+     * Contains country code by Annex B of Recommendation ITU-T T.35
+     */
+    UWORD8 u1_itu_t_t35_country_code_extension_byte;
+
+    /**
+     * Contains data registered as specified in Recommendation ITU-T T.35
+     */
+    UWORD8 u1_itu_t_t35_payload_byte[MAX_USERDATA_PAYLOAD];
+
+    /**
+     * Valid payload size present in this buffer
+     */
+    WORD32 i4_valid_payload_size;
+
+    /**
+     * Total payload size incase payloadSize > IHEVCD_MAX_USERDATA_PAYLOAD
+     */
+    WORD32 i4_payload_size;
+}user_data_registered_itu_t_t35_t;
+
+/**
+ * Structure to hold time code SEI param info
+ */
+typedef struct
+{
+    /**
+     * Number of sets of clock timestamp syntax elements present for the current picture
+     */
+    UWORD8 u1_num_clock_ts;
+
+    /**
+     * Indicates presenc of associated set of clock timestamps
+     */
+    UWORD8 au1_clock_timestamp_flag[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Used in calculating clockTimestamp[i]
+     */
+    UWORD8 au1_units_field_based_flag[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the method of dropping values of the n_frames[i] syntax element
+     */
+    UWORD8 au1_counting_type[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies that the n_frames[i] syntax element is followed by seconds_value[i],
+     * minutes_value[i] and hours_value[i]
+     */
+    UWORD8 au1_full_timestamp_flag[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Indicates the discontinuity in clockTimestamp
+     */
+    UWORD8 au1_discontinuity_flag[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the skipping of one or more values of n_frames[i]
+     */
+    UWORD8 au1_cnt_dropped_flag[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the value of nFrames used to compute clockTimestamp[i]
+     */
+    UWORD16 au2_n_frames[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the presence of seconds_value[i] and minutes_flag[i]
+     */
+    UWORD8 au1_seconds_flag[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the presence of minutes_value[i] and hours_flag[i]
+     */
+    UWORD8 au1_minutes_flag[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the presence of hours_value[i]
+     */
+    UWORD8 au1_hours_flag[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the value of sS used to compute clockTimestamp[i]
+     */
+    UWORD8 au1_seconds_value[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the value of mM used to compute clockTimestamp[i]
+     */
+    UWORD8 au1_minutes_value[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the value of hH used to compute clockTimestamp[i]
+     */
+    UWORD8 au1_hours_value[MAX_NUM_CLOCK_TS];
+
+    /**
+     * Specifies the length in bits of the time_offset_value[i]
+     */
+    UWORD8 au1_time_offset_length[MAX_NUM_CLOCK_TS];
+
+    /**
+     * pecifies the value of tOffset used to compute clockTimestamp[i]
+     */
+    UWORD8 au1_time_offset_value[MAX_NUM_CLOCK_TS];
+
+}time_code_t;
+
+
+/**
+ * Structure to hold SEI parameters Info
+ */
+typedef struct
+{
+
+    WORD8 i1_sei_parameters_present_flag;
+
+    WORD8 i1_aud_present_flag;
+
+    WORD8 i1_buf_period_params_present_flag;
+
+    WORD8 i1_pic_timing_params_present_flag;
+
+    WORD8 i1_recovery_point_params_present_flag;
+
+    WORD8 i1_active_parameter_set;
+
+    WORD8 i4_sei_mastering_disp_colour_vol_params_present_flags;
+
+    /* Enable/Disable SEI Hash on the Decoded picture & Hash type */
+    /* < 3 : Checksum, 2 : CRC, 1 : MD5, 0 : disable >            */
+    /* Other values are not supported                             */
+    WORD8 i1_decoded_pic_hash_sei_flag;
+
+    /* number of user data e.g. CC data, BAR data, AFD data etc */
+    WORD32 i4_sei_user_data_cnt;
+
+    WORD8 i1_user_data_registered_present_flag;
+
+    WORD8 i1_time_code_present_flag;
+
+    buf_period_sei_params_t  s_buf_period_sei_params;
+
+    pic_timing_sei_params_t  s_pic_timing_sei_params;
+
+    recovery_point_sei_params_t s_recovery_point_params;
+
+    active_parameter_set_sei_param_t s_active_parameter_set_sei_params;
+
+    hash_sei_param_t    s_hash_sei_params;
+
+    mastering_dis_col_vol_sei_params_t s_mastering_dis_col_vol_sei_params;
+
+    user_data_registered_itu_t_t35_t as_user_data_registered_itu_t_t35[USER_DATA_MAX];
+
+    time_code_t s_time_code;
+} sei_params_t;
+
+
+/**
+ * Sub-layer HRD parameters Info
+ */
+typedef struct
+{
+    /**
+     * (together with bit_rate_scale) specifies the
+     * maximum input bit rate for the i-th CPB
+     */
+    UWORD32 au4_bit_rate_value_minus1[MAX_CPB_CNT];
+    /**
+     * together with cpb_size_scale to specify the
+     * CPB size when the CPB operates at the access unit level.
+     */
+    UWORD32 au4_cpb_size_value_minus1[MAX_CPB_CNT];
+
+    /**
+     * together with cpb_size_du_scale to specify the CPB size
+     * when the CPB operates at sub-picture level
+     */
+    UWORD32 au4_cpb_size_du_value_minus1[MAX_CPB_CNT];
+
+    /**
+     * specifies the maximum input bit rate for the i-th CPB when the CPB
+     * operates at the sub-picture level. bit_rate_du_value_minus1[ i ]
+     * shall be in the range of 0 to 2^32 - 2
+     */
+    UWORD32 au4_bit_rate_du_value_minus1[MAX_CPB_CNT];
+
+    /**
+     * if 1, specifies that the HSS operates in a constant bit rate (CBR) mode
+     * if 0, specifies that the HSS operates in a intermittent bit rate (CBR) mode
+     */
+    UWORD8  au1_cbr_flag[32];
+
+}sub_lyr_hrd_params_t;
+
+/**
+ * HRD parameters Info
+ */
+typedef struct
+{
+    /**
+     * Indicates the presence of the
+     * num_units_in_ticks, time_scale flag
+     */
+    UWORD8 u1_timing_info_present_flag;
+
+    /**
+     * Number of units that
+     * correspond to one increment of the
+     * clock. Indicates the  resolution
+     */
+    UWORD32 u4_num_units_in_tick;
+
+    /**
+     * The number of time units that pass in one second
+     */
+    UWORD32 u4_time_scale;
+
+    /**
+     * Nal- hrd parameters flag
+     */
+    UWORD8 u1_nal_hrd_parameters_present_flag;
+
+    /**
+     * VCL- hrd parameters flag
+     */
+    UWORD8 u1_vcl_hrd_parameters_present_flag;
+
+    /**
+     * Indicates the presence of NAL-HRD params or VCL_HRD params
+     * in the bitstream
+     */
+    UWORD8 u1_cpbdpb_delays_present_flag;
+
+    /**
+     * specifies that sub-picture level CPB removal delay parameters are
+     * present in picture timing SEI messages
+     */
+    UWORD8 u1_sub_pic_cpb_params_present_flag;
+
+    /**
+     * specify the clock sub-tick
+     * (the minimum interval of time that can be represented in the coded data when sub_pic_cpb_params_present_flag is equal to 1)
+     */
+    UWORD8 u1_tick_divisor_minus2;
+
+    /**
+     * specifies the length, in bits for the du cpb delay syntax in pt_sei
+     */
+    UWORD8 u1_du_cpb_removal_delay_increment_length_minus1;
+
+    /**
+     * Indicates presence of sub_pic_cpb_params in pic timing sei
+     */
+    UWORD8 u1_sub_pic_cpb_params_in_pic_timing_sei_flag;
+
+    /**
+     * specifies the length, in bits, of the pic_dpb_output_du_delay syntax
+     * element in the picture timing SEI message and the
+     * pic_spt_dpb_output_du_delay syntax element in the decoding unit
+     * information SEI message
+     */
+    UWORD8 u1_dpb_output_delay_du_length_minus1;
+
+    /**
+     * (together with bit_rate_value_minus1) specifies the
+     * maximum input bit rate of the i-th CPB
+     */
+    UWORD32 u4_bit_rate_scale;
+
+    /**
+     * (together with cpb_size_du_value_minus1) specfies
+     * CPB size of the i-th CPB when the CPB operates
+     * at the access unit level
+     */
+    UWORD32 u4_cpb_size_scale;
+
+    /**
+     * (together with cpb_size_du_value_minus1) specfies
+     * CPB size of the i-th CPB when the CPB operates
+     * at the sub-picture level
+     */
+    UWORD32 u4_cpb_size_du_scale;
+
+
+    /**
+     * specifies the length, in bits for initial cpb delay (nal/vcl)sysntax in bp sei
+     */
+    UWORD8  u1_initial_cpb_removal_delay_length_minus1;
+
+    /**
+     * specifies the length, in bits for the au cpb delay syntax in pt_sei
+     */
+    UWORD8  u1_au_cpb_removal_delay_length_minus1;
+
+    /**
+     * specifies the length, in bits, of the pic_dpb_output_delay syntax element in the pt SEI message
+     */
+    UWORD8  u1_dpb_output_delay_length_minus1;
+
+    /**
+     * if 1, , for the highest temporal sub-layers, the temporal distance between the HRD output times
+     * of consecutive pictures in output order is constrained refer to Table E-6
+     */
+    UWORD8 au1_fixed_pic_rate_general_flag[6];
+
+    UWORD8 au1_fixed_pic_rate_within_cvs_flag[6];
+
+    /**
+     * if 1, , for the highest temporal sub-layers, the temporal distance (in clock ticks) between the
+     * element units that specify HRD output times of consecutive pictures in output order is constrained
+     * refer to Table E-6
+     */
+    UWORD8 au1_elemental_duration_in_tc_minus1[6];
+
+    /**
+     * specifies the HRD operational mode
+     */
+    UWORD8 au1_low_delay_hrd_flag[6];
+
+    /**
+     * 1 specifies the number of alternative CPB specifications in the
+     * bitstream of the cvs when HighestTid is equal to i
+     */
+    UWORD8 au1_cpb_cnt_minus1[6];
+
+
+    /**
+     * VUI level Sub-layer HRD parameters
+     */
+    sub_lyr_hrd_params_t as_sub_layer_hrd_params[6];
+
+}hrd_params_t;
+
+/**
+ * Structure to hold VUI parameters Info
+ */
+typedef struct
+{
+    /**
+     * indicates the presence of aspect_ratio
+     */
+    UWORD8 u1_aspect_ratio_info_present_flag;
+
+    /**
+     * specifies the aspect ratio of the luma samples
+     */
+    UWORD8 u1_aspect_ratio_idc;
+
+    /**
+     *  width of the luma samples. user dependent
+     */
+    UWORD16 u2_sar_width;
+
+    /**
+     *  hieght of the luma samples. user dependent
+     */
+    UWORD16 u2_sar_height;
+
+    /**
+     * if 1, specifies that the overscan_appropriate_flag is present
+     * if 0, the preferred display method for the video signal is unspecified
+     */
+    UWORD8 u1_overscan_info_present_flag;
+
+    /**
+     * if 1,indicates that the cropped decoded pictures output
+     * are suitable for display using overscan
+     */
+    UWORD8 u1_overscan_appropriate_flag;
+
+    /**
+     * if 1 specifies that video_format, video_full_range_flag and
+     * colour_description_present_flag are present
+     */
+    UWORD8 u1_video_signal_type_present_flag;
+
+    /**
+     *
+     */
+    UWORD8 u1_video_format;
+
+    /**
+     * indicates the black level and range of the luma and chroma signals
+     */
+    UWORD8 u1_video_full_range_flag;
+
+    /**
+     * if 1,to 1 specifies that colour_primaries, transfer_characteristics
+     * and matrix_coefficients are present
+     */
+    UWORD8 u1_colour_description_present_flag;
+
+    /**
+     * indicates the chromaticity coordinates of the source primaries
+     */
+    UWORD8 u1_colour_primaries;
+
+    /**
+     * indicates the opto-electronic transfer characteristic of the source picture
+     */
+    UWORD8 u1_transfer_characteristics;
+
+    /**
+     * the matrix coefficients used in deriving luma and chroma signals
+     * from the green, blue, and red primaries
+     */
+    UWORD8 u1_matrix_coefficients;
+
+    /**
+     * if 1, specifies that chroma_sample_loc_type_top_field and
+     * chroma_sample_loc_type_bottom_field are present
+     */
+    UWORD8 u1_chroma_loc_info_present_flag;
+
+    /**
+     * location of chroma samples
+     */
+    UWORD8 u1_chroma_sample_loc_type_top_field;
+
+    UWORD8 u1_chroma_sample_loc_type_bottom_field;
+
+    /**
+     * if 1, indicates that the value of all decoded chroma samples is
+     * equal to 1 << ( BitDepthC - 1 )
+     */
+    UWORD8 u1_neutral_chroma_indication_flag;
+
+    /**
+     * 1 indicates that the coded video sequence conveys pictures that represent fields
+     * 0 indicates the pictures that represents field
+     */
+    UWORD8 u1_field_seq_flag;
+
+    /**
+     * specifies that picture timing SEI messages are present for every picture
+     */
+    UWORD8 u1_frame_field_info_present_flag;
+
+    /**
+     * 1 indicates that the default display window parameters follow next in the VUI
+     */
+    UWORD8 u1_default_display_window_flag;
+
+    /**
+     * specify the samples of the pictures in the coded video sequence
+     * that are within the default display window,
+     * in terms of a rectangular region specified in picture coordinates for display
+     */
+    UWORD32 u4_def_disp_win_left_offset;
+
+    UWORD32 u4_def_disp_win_right_offset;
+
+    UWORD32 u4_def_disp_win_top_offset;
+
+    UWORD32 u4_def_disp_win_bottom_offset;
+
+    /**
+     * to 1 specifies that the syntax structure hrd_parameters is present in the vui_parameters syntax structue
+     */
+    UWORD8 u1_vui_hrd_parameters_present_flag;
+
+    /**
+     * VUI level HRD parameters
+     */
+    hrd_params_t s_vui_hrd_parameters;
+
+    /**
+     * Indicates the presence of the
+     * num_units_in_ticks, time_scale flag
+     */
+    UWORD8 u1_vui_timing_info_present_flag;
+
+    /**
+     * Number of units that
+     * correspond to one increment of the
+     * clock. Indicates the  resolution
+     */
+    UWORD32 u4_vui_num_units_in_tick;
+
+    /**
+     * The number of time units that pass in one second
+     */
+    UWORD32 u4_vui_time_scale;
+    /**
+     * if 1, indicates that the POC for each picture in the coded video sequence (cvs) (not the first picture), in decoding order,
+     * is proportional to the output time of the picture relative to that of the first picture in the cvs
+     */
+    UWORD8 u1_poc_proportional_to_timing_flag;
+
+    /**
+     * num_ticks_poc_diff_one_minus1 plus 1 specifies the number of clock ticks
+     * corresponding to a difference of poc values equal to 1
+     */
+    UWORD8 u1_num_ticks_poc_diff_one_minus1;
+
+    /**
+     * 1, specifies that the following cvs bitstream restriction parameters are present
+     */
+    UWORD8 u1_bitstream_restriction_flag;
+
+    /**
+     *  if 1, indicates that each pps that is active in the cvs has
+     *  the same value of the tile syntax elements
+     */
+    UWORD8 u1_tiles_fixed_structure_flag;
+
+    /**
+     * if 0, indicates that no pel outside the pic boundaries and
+     * no sub-pels derived using pels outside the pic boundaries is used for inter prediction
+     */
+    UWORD8 u1_motion_vectors_over_pic_boundaries_flag;
+
+    /**
+     * if 1, indicates
+     * all P/B slices belonging to the same pic have an identical refpic list0,
+     * all B slices that belong to the same picture have an identical refpic list1.
+     */
+    UWORD8 u1_restricted_ref_pic_lists_flag;
+
+    /**
+     * min_spatial_segmentation_idc, when not equal to 0, establishes a bound on the maximum possible size of distinct
+     * coded spatial segmentation regions in the pictures of the CVS. When min_spatial_segmentation_idc is not present, it is
+     * inferred to be equal to 0. The value of min_spatial_segmentation_idc shall be in the range of 0 to 4095, inclusive.
+     *
+     * can be used by a decoder to calculate the maximum number of luma samples to be processed by one processing thread
+     *
+     * If tiles=0 and entropy_sync=0 then
+     *     no slice shall exceed ( 4 * PicSizeInSamplesY ) / minSpatialSegmentationTimes4 luma samples
+     *
+     * If tiles=1 and entropy_sync=0 then
+     *     no tile shall exceed ( 4 * PicSizeInSamplesY ) / minSpatialSegmentationTimes4 luma samples
+     *
+     * If tiles=0 and entropy_sync=1 then
+     *     ( 2 * pic_height_in_luma_samples + pic_width_in_luma_samples ) * CtbSizeY
+     *             <= ( 4 * PicSizeInSamplesY ) / minSpatialSegmentationTimes4
+     */
+    UWORD32 u4_min_spatial_segmentation_idc;
+    /**
+     * Indicates a number of bytes not exceeded by the sum of the sizes of the VCL NAL units
+     * associated with any coded picture
+     */
+    UWORD8 u1_max_bytes_per_pic_denom;
+
+    /**
+     * Indicates an upper bound for the number of bits of coding_unit() data
+     */
+    UWORD8 u1_max_bits_per_mincu_denom;
+
+    /**
+     * Indicate the maximum absolute value of a decoded horizontal MV component
+     * in quarter-pel luma units
+     */
+    UWORD8 u1_log2_max_mv_length_horizontal;
+
+    /**
+     * Indicate the maximum absolute value of a decoded vertical MV component
+     * in quarter-pel luma units
+     */
+    UWORD8 u1_log2_max_mv_length_vertical;
+}vui_t;
+
 /**
  * Picture buffer
  */
@@ -59,6 +923,16 @@
      */
     UWORD8 u1_buf_id;
 
+
+    // See IV_FLD_TYPE_T for all field types
+    UWORD32 e4_fld_type;
+
+    sei_params_t s_sei_params;
+
+    WORD32 i4_vui_present;
+
+    vui_t s_vui;
+
 }pic_buf_t;
 
 
@@ -386,7 +1260,7 @@
     UWORD32     b2_pred_mode    : 2;
 
 
-/**
+    /**
      *  Merge flag for each partition - 0 or 1
      */
     UWORD32     b1_merge_flag   : 1;
@@ -424,7 +1298,6 @@
      */
     UWORD32      b2_part_idx     : 2;
 
-
 }pu_t;
 
 /**
@@ -442,7 +1315,6 @@
      */
     UWORD32     b4_pos_y            : 4;
 
-
     /*************************************************************************/
     /* Luma TU size (width or height) = 1 << (b3_size + 2)                   */
     /*   i.e. 0 : 4, 1 : 8, 2: 16, 3: 32, 4: 64                              */
@@ -460,7 +1332,6 @@
     /* Cr info. For the first three TUs in 8x8 (for 4x4 luma) this will      */
     /* be zero. For all the other cases this will be 1                       */
     /*************************************************************************/
-
     /**
      * 4x4 Luma TUs only the fourth one contains cb,cr
      * TODO: Check if this is really needed, cb_cbf and cr_cbf should be enough
@@ -909,7 +1780,6 @@
 }tu_sblk_coeff_data_t;
 
 
-
 /*************************************************************************/
 /* The following describes how each of the CU cases are handled          */
 /*************************************************************************/
@@ -1310,7 +2180,6 @@
      */
     WORD8 ai1_bit_rate_info_present_flag[VPS_MAX_SUB_LAYERS];
 
-
     /**
      * pic_rate_info_present_flag[i]
      */
@@ -1320,428 +2189,23 @@
      * avg_bit_rate[i]
      */
     UWORD16 au2_avg_bit_rate[VPS_MAX_SUB_LAYERS];
+
     /**
      * max_bit_rate[i]
      */
     UWORD16 au2_max_bit_rate[VPS_MAX_SUB_LAYERS];
+
     /**
      * constant_pic_rate_idc[i]
      */
     WORD8 ai1_constant_pic_rate_idc[VPS_MAX_SUB_LAYERS];
+
     /**
      * avg_pic_rate[i]
      */
     UWORD16 au2_avg_pic_rate[VPS_MAX_SUB_LAYERS];
 }vps_t;
 
-/**
- * Sub-layer HRD parameters Info
- */
-typedef struct
-{
-    /**
-    *  (together with bit_rate_scale) specifies the
-    *  maximum input bit rate for the i-th CPB
-    */
-    UWORD32 au4_bit_rate_value_minus1[32];
-    /**
-    *  together with cpb_size_scale to specify the
-    *  CPB size when the CPB operates at the access unit level.
-    */
-    UWORD32 au4_cpb_size_value_minus1[32];
-
-    /**
-    * together with cpb_size_du_scale to specify the CPB size
-    * when the CPB operates at sub-picture level
-    */
-    UWORD32 au4_cpb_size_du_value_minus1[32];
-
-    /**
-    * specifies the maximum input bit rate for the i-th CPB when the CPB
-    * operates at the sub-picture level. bit_rate_du_value_minus1[ i ]
-    * shall be in the range of 0 to 2^32 - 2
-    */
-    UWORD32 au4_bit_rate_du_value_minus1[32];
-
-    /**
-    * if 1, specifies that the HSS operates in a constant bit rate (CBR) mode
-    * if 0, specifies that the HSS operates in a intermittent bit rate (CBR) mode
-    */
-    UWORD8  au1_cbr_flag[32];
-
-}sub_lyr_hrd_params_t;
-
-/**
- * HRD parameters Info
- */
-typedef struct
-{
-
-    /**
-    *   Indicates the presence of the
-    *   num_units_in_ticks, time_scale flag
-    */
-    UWORD8 u1_timing_info_present_flag;
-
-    /**
-    *   Number of units that
-    *   correspond to one increment of the
-    *   clock. Indicates the  resolution
-    */
-    UWORD32 u4_num_units_in_tick;
-
-    /**
-    *   The number of time units that pass in one second
-    */
-    UWORD32 u4_time_scale;
-
-    /**
-    * Nal- hrd parameters flag
-    */
-    UWORD8 u1_nal_hrd_parameters_present_flag;
-
-    /**
-    * VCL- hrd parameters flag
-    */
-    UWORD8 u1_vcl_hrd_parameters_present_flag;
-
-    /**
-    * Indicates the presence of NAL-HRD params or VCL_HRD params
-    * in the bitstream
-    */
-    UWORD8 u1_cpbdpb_delays_present_flag;
-
-    /**
-    * specifies that sub-picture level CPB removal delay parameters are
-    * present in picture timing SEI messages
-    */
-    UWORD8 u1_sub_pic_cpb_params_present_flag;
-
-    /**
-    * specify the clock sub-tick
-    * (the minimum interval of time that can be represented in the coded data when sub_pic_cpb_params_present_flag is equal to 1)
-    */
-    UWORD8 u1_tick_divisor_minus2;
-
-    /**
-    * specifies the length, in bits for the du cpb delay syntax in pt_sei
-    */
-    UWORD8 u1_du_cpb_removal_delay_increment_length_minus1;
-
-    /**
-    * Indicates presence of sub_pic_cpb_params in pic timing sei
-    */
-    UWORD8 u1_sub_pic_cpb_params_in_pic_timing_sei_flag;
-
-    /**
-    * specifies the length, in bits, of the pic_dpb_output_du_delay syntax
-    * element in the picture timing SEI message and the
-    * pic_spt_dpb_output_du_delay syntax element in the decoding unit
-    * information SEI message
-     */
-    UWORD8 u1_dpb_output_delay_du_length_minus1;
-
-    /**
-    * (together with bit_rate_value_minus1) specifies the
-    * maximum input bit rate of the i-th CPB
-    */
-    UWORD32 u4_bit_rate_scale;
-
-    /**
-    * (together with cpb_size_du_value_minus1) specfies
-    * CPB size of the i-th CPB when the CPB operates
-    * at the access unit level
-    */
-    UWORD32 u4_cpb_size_scale;
-
-    /**
-    * (together with cpb_size_du_value_minus1) specfies
-    * CPB size of the i-th CPB when the CPB operates
-    * at the sub-picture level
-    */
-    UWORD32 u4_cpb_size_du_scale;
-
-
-    /**
-    * specifies the length, in bits for initial cpb delay (nal/vcl)sysntax in bp sei
-    */
-    UWORD8  u1_initial_cpb_removal_delay_length_minus1;
-
-    /**
-    * specifies the length, in bits for the au cpb delay syntax in pt_sei
-    */
-    UWORD8  u1_au_cpb_removal_delay_length_minus1;
-
-    /**
-    * specifies the length, in bits, of the pic_dpb_output_delay syntax element in the pt SEI message
-    */
-    UWORD8  u1_dpb_output_delay_length_minus1;
-
-    /**
-    * if 1, , for the highest temporal sub-layers, the temporal distance between the HRD output times
-    *  of consecutive pictures in output order is constrained refer to Table E-6
-    */
-    UWORD8 au1_fixed_pic_rate_general_flag[6];
-
-    UWORD8 au1_fixed_pic_rate_within_cvs_flag[6];
-
-    /**
-    * if 1, , for the highest temporal sub-layers, the temporal distance (in clock ticks) between the
-    * element units that specify HRD output times of consecutive pictures in output order is constrained
-    * refer to Table E-6
-    */
-    UWORD8 au1_elemental_duration_in_tc_minus1[6];
-
-    /**
-    * specifies the HRD operational mode
-    */
-    UWORD8 au1_low_delay_hrd_flag[6];
-
-    /**
-    * 1 specifies the number of alternative CPB specifications in the
-    * bitstream of the cvs when HighestTid is equal to i
-    */
-    UWORD8 au1_cpb_cnt_minus1[6];
-
-
-    /**
-    *  VUI level Sub-layer HRD parameters
-    */
-    sub_lyr_hrd_params_t as_sub_layer_hrd_params[6];
-
-}hrd_params_t;
-
-
-/**
- * Structure to hold VUI parameters Info
- */
-typedef struct
-{
-    /**
-    *  indicates the presence of aspect_ratio
-    */
-    UWORD8 u1_aspect_ratio_info_present_flag;
-
-    /**
-    *  specifies the aspect ratio of the luma samples
-    */
-    UWORD8 u1_aspect_ratio_idc;
-
-    /**
-    *  width of the luma samples. user dependent
-    */
-    UWORD16 u2_sar_width;
-
-    /**
-    *  hieght of the luma samples. user dependent
-    */
-    UWORD16 u2_sar_height;
-
-    /**
-    * if 1, specifies that the overscan_appropriate_flag is present
-    * if 0, the preferred display method for the video signal is unspecified
-    */
-    UWORD8 u1_overscan_info_present_flag;
-
-    /**
-    * if 1,indicates that the cropped decoded pictures output
-    * are suitable for display using overscan
-    */
-    UWORD8 u1_overscan_appropriate_flag;
-
-    /**
-    * if 1 specifies that video_format, video_full_range_flag and
-    * colour_description_present_flag are present
-    */
-    UWORD8 u1_video_signal_type_present_flag;
-
-    /**
-    *
-    */
-    UWORD8 u1_video_format;
-
-    /**
-    * indicates the black level and range of the luma and chroma signals
-    */
-    UWORD8 u1_video_full_range_flag;
-
-    /**
-    * if 1,to 1 specifies that colour_primaries, transfer_characteristics
-    * and matrix_coefficients are present
-    */
-    UWORD8 u1_colour_description_present_flag;
-
-    /**
-    * indicates the chromaticity coordinates of the source primaries
-    */
-    UWORD8 u1_colour_primaries;
-
-    /**
-    * indicates the opto-electronic transfer characteristic of the source picture
-    */
-    UWORD8 u1_transfer_characteristics;
-
-    /**
-    * the matrix coefficients used in deriving luma and chroma signals
-    * from the green, blue, and red primaries
-    */
-    UWORD8 u1_matrix_coefficients;
-
-    /**
-    * if 1, specifies that chroma_sample_loc_type_top_field and
-    * chroma_sample_loc_type_bottom_field are present
-    */
-    UWORD8 u1_chroma_loc_info_present_flag;
-
-    /**
-    * location of chroma samples
-    */
-    UWORD8 u1_chroma_sample_loc_type_top_field;
-
-    UWORD8 u1_chroma_sample_loc_type_bottom_field;
-
-    /**
-    * if 1, indicates that the value of all decoded chroma samples is
-    * equal to 1 << ( BitDepthC - 1 )
-    */
-    UWORD8 u1_neutral_chroma_indication_flag;
-
-    /**
-    *  1 indicates that the coded video sequence conveys pictures that represent fields
-    *  0 indicates the pictures that represents field
-    */
-    UWORD8 u1_field_seq_flag;
-
-    /**
-    * specifies that picture timing SEI messages are present for every picture
-    */
-    UWORD8 u1_frame_field_info_present_flag;
-
-    /**
-    * 1 indicates that the default display window parameters follow next in the VUI
-    */
-    UWORD8 u1_default_display_window_flag;
-
-    /**
-    * specify the samples of the pictures in the coded video sequence
-    * that are within the default display window,
-    * in terms of a rectangular region specified in picture coordinates for display
-    */
-    UWORD32 u4_def_disp_win_left_offset;
-
-    UWORD32 u4_def_disp_win_right_offset;
-
-    UWORD32 u4_def_disp_win_top_offset;
-
-    UWORD32 u4_def_disp_win_bottom_offset;
-
-    /**
-    *  to 1 specifies that the syntax structure hrd_parameters is present in the vui_parameters syntax structue
-    */
-    UWORD8 u1_vui_hrd_parameters_present_flag;
-
-    /**
-    *  VUI level HRD parameters
-    */
-    hrd_params_t s_vui_hrd_parameters;
-
-    /**
-    *   Indicates the presence of the
-    *   num_units_in_ticks, time_scale flag
-    */
-    UWORD8 u1_vui_timing_info_present_flag;
-
-    /**
-    *   Number of units that
-    *   correspond to one increment of the
-    *   clock. Indicates the  resolution
-    */
-    UWORD32 u4_vui_num_units_in_tick;
-
-    /**
-    *   The number of time units that pass in one second
-    */
-    UWORD32 u4_vui_time_scale;
-    /**
-    * if 1, indicates that the POC for each picture in the coded video sequence (cvs) (not the first picture), in decoding order,
-    * is proportional to the output time of the picture relative to that of the first picture in the cvs
-    */
-    UWORD8 u1_poc_proportional_to_timing_flag;
-
-    /**
-    * num_ticks_poc_diff_one_minus1 plus 1 specifies the number of clock ticks
-    * corresponding to a difference of poc values equal to 1
-    */
-    UWORD8 u1_num_ticks_poc_diff_one_minus1;
-
-    /**
-    * 1, specifies that the following cvs bitstream restriction parameters are present
-    */
-    UWORD8 u1_bitstream_restriction_flag;
-
-    /**
-    *  if 1, indicates that each pps that is active in the cvs has
-    *  the same value of the tile syntax elements
-    */
-    UWORD8 u1_tiles_fixed_structure_flag;
-
-    /**
-    * if 0, indicates that no pel outside the pic boundaries and
-    * no sub-pels derived using pels outside the pic boundaries is used for inter prediction
-    */
-    UWORD8 u1_motion_vectors_over_pic_boundaries_flag;
-
-    /**
-    * if 1, indicates
-    * all P/B slices belonging to the same pic have an identical refpic list0,
-    * all B slices that belong to the same picture have an identical refpic list1.
-    */
-    UWORD8 u1_restricted_ref_pic_lists_flag;
-
-    /**
-    *   min_spatial_segmentation_idc, when not equal to 0, establishes a bound on the maximum possible size of distinct
-    *   coded spatial segmentation regions in the pictures of the CVS. When min_spatial_segmentation_idc is not present, it is
-    *   inferred to be equal to 0. The value of min_spatial_segmentation_idc shall be in the range of 0 to 4095, inclusive.
-    *
-    *   can be used by a decoder to calculate the maximum number of luma samples to be processed by one processing thread
-    *
-    *   If tiles=0 and entropy_sync=0 then
-    *       no slice shall exceed ( 4 * PicSizeInSamplesY ) / minSpatialSegmentationTimes4 luma samples
-    *
-    *   If tiles=1 and entropy_sync=0 then
-    *       no tile shall exceed ( 4 * PicSizeInSamplesY ) / minSpatialSegmentationTimes4 luma samples
-    *
-    *   If tiles=0 and entropy_sync=1 then
-    *       ( 2 * pic_height_in_luma_samples + pic_width_in_luma_samples ) * CtbSizeY
-    *               <= ( 4 * PicSizeInSamplesY ) / minSpatialSegmentationTimes4
-    */
-    UWORD32 u4_min_spatial_segmentation_idc;
-    /**
-    * Indicates a number of bytes not exceeded by the sum of the sizes of the VCL NAL units
-    * associated with any coded picture
-    */
-    UWORD8 u1_max_bytes_per_pic_denom;
-
-    /**
-    *  Indicates an upper bound for the number of bits of coding_unit() data
-    */
-    UWORD8 u1_max_bits_per_mincu_denom;
-
-    /**
-    * Indicate the maximum absolute value of a decoded horizontal MV component
-    * in quarter-pel luma units
-    */
-    UWORD8 u1_log2_max_mv_length_horizontal;
-
-    /**
-    * Indicate the maximum absolute value of a decoded vertical MV component
-    * in quarter-pel luma units
-    */
-    UWORD8 u1_log2_max_mv_length_vertical;
-
-
-}vui_t;
-
 
 /**
  * Structure to hold SPS info
@@ -2288,283 +2752,6 @@
 }pps_t;
 
 
-
-/**
- * Buffering Period SEI parameters Info
- */
-typedef struct
-{
-    /**
-    * specifies SPS Id active for the coded picture assosiated
-    * with the bp message.
-    */
-    UWORD8  u1_sps_id;
-
-    /**
-    * Derived from Hrd parameters
-    */
-    UWORD8  u1_sub_pic_cpb_params_present_flag;
-
-    /**
-    * specifies the presence of the initial_alt_cpb_removal_delay[ i ]
-    * and initial_alt_cpb_removal_offset[ i ] syntax elements
-    */
-    UWORD8  u1_rap_cpb_params_present_flag;
-
-    /**
-    * cbp removal delay used in buffering period SEI
-    */
-    UWORD32 cpb_delay_offset;
-
-    /**
-    * dbp removal delay used in buffering period SEI
-    */
-    UWORD32 dpb_delay_offset;
-
-    /**
-    * concatanation flag
-    */
-    UWORD8 concatenation_flag;
-
-    /**
-    * delata cbp removal delay
-    */
-    UWORD32 au_cpb_removal_delay_delta_minus1;
-
-    /**
-    * specify the default initial CPB removal delays, respectively,
-    * for the CPB when the NAL HRD parameters are in use
-    */
-    UWORD32 au4_nal_initial_cpb_removal_delay[32];
-
-    /**
-    * specify the alternate initial CPB removal delays, respectively,
-    * for the CPB when the NAL HRD parameters are in use
-    */
-    UWORD32 au4_nal_initial_alt_cpb_removal_delay[32];
-
-    /**
-    * specify the initial CPB removal delay offset, respectively,
-    * for the CPB when the NAL HRD parameters are in use
-    */
-    UWORD32 au4_nal_initial_cpb_removal_delay_offset[32];
-
-    /**
-    * specify the alternate initial CPB removal delays offsets, respectively,
-    * for the CPB when the NAL HRD parameters are in use
-    */
-    UWORD32 au4_nal_initial_alt_cpb_removal_delay_offset[32];
-
-    /**
-    * specify the default initial CPB removal delays, respectively,
-    * for the CPB when the VCL HRD parameters are in use
-    */
-    UWORD32 au4_vcl_initial_cpb_removal_delay[32];
-
-    /**
-    * specify the initial alt CPB removal delays , respectively,
-    * for the CPB when the VCL HRD parameters are in use
-    */
-    UWORD32 au4_vcl_initial_alt_cpb_removal_delay[32];
-
-    /**
-    * specify the initial CPB removal delay offset, respectively,
-    * for the CPB when the VCL HRD parameters are in use
-    */
-    UWORD32 au4_vcl_initial_cpb_removal_delay_offset[32];
-
-    /**
-    * specify the alternate initial CPB removal delays offsets, respectively,
-    * for the CPB when the VCL HRD parameters are in use
-    */
-    UWORD32 au4_vcl_initial_alt_cpb_removal_delay_offset[32];
-
-    /**
-    *  Inital CPB removal delay
-    */
-    UWORD32 u4_initial_cpb_removal_delay_length;
-
-    /**
-    *  CPB cnt for corr. sublayer
-    */
-    UWORD32 u4_cpb_cnt;
-
-
-    /**
-    * VBV buffer size used in buffering period SEI
-    */
-    UWORD32 u4_buffer_size_sei;
-
-    /**
-    * Encoder buffer fullness  used in buffering period SEI
-    */
-    UWORD32 u4_dbf_sei;
-
-    /**
-    * target bitrate used in buffering period SEI
-    */
-    UWORD32 u4_target_bit_rate_sei;
-
-
-
-
-}buf_period_sei_params_t;
-
-
-/**
- * Picture Timing SEI parameters Info
- */
-typedef struct
-{
-    /**
-    * derived from vui parameters
-    */
-    UWORD8 u1_frame_field_info_present_flag;
-
-    /**
-    * indicates whether a picture should be displayed as a
-    * frame or as one or more fields
-    */
-    UWORD32 u4_pic_struct;
-
-    UWORD8  u1_num_clk_ticks;
-
-    /**
-    * indicates whether a scan-type of the pic should be interpreted
-    * as progressive or interlaced
-    */
-    UWORD8 u1_progressive_source_idc;
-
-    /**
-    * if 1, indicates if the current pic is a duplicte pic in output order
-    */
-    UWORD8 u1_duplicate_flag;
-
-    /**
-    * specifies the number clock ticks between the nominal CPB removal time
-    * au associated with the pt SEI message and
-    * the preceding au in decoding order that contained a bp SEI message
-    */
-    UWORD32 u4_au_cpb_removal_delay_minus1;
-
-    /**
-    * compute the DPB output time of the picture
-    */
-    UWORD32 u4_pic_dpb_output_delay;
-
-    UWORD32 u4_pic_dpb_output_du_delay;
-
-    /**
-    * specifies the number of decoding units in the access unit
-    * the picture timing SEI message is associated with
-    */
-    UWORD32 u4_num_decoding_units_minus1;
-
-    /**
-    * if 1 specifies that the du_common_cpb_removal_delay_increment_minus1 is present
-    */
-    UWORD32 u4_du_common_cpb_removal_delay_flag;
-
-    /**
-    * specifies the duration, in units of clock sub-ticks,
-    * between the nominal CPB removal times of any two consecutive decoding units
-    * in decoding order in the access unit associated with the pt_SEI message
-    */
-    UWORD32 u4_du_common_cpb_removal_delay_increment_minus1; //same as u4_du_cpb_removal_delay_increment_minus1
-
-    /**
-    * specifies the number of NAL units in the decoding unit of the access unit
-    * the picture timing SEI message is associated with.
-    * range from 0 to (pic size in ctby - 1)
-    */
-    UWORD32 u4_num_nalus_in_du_minus1;
-
-    /**
-    * specifies the duration, in units of clock sub-ticks,
-    * between the nominal CPB removal times of the ( i + 1 )-th decoding unit and the i-th decoding unit,
-    * in decoding order, in the access unit associated with the pt_SEI message
-    */
-    UWORD32 u4_du_cpb_removal_delay_increment_minus1;
-
-
-}pic_timing_sei_params_t;
-
-/**
- * Structure to hold Recovery point SEI parameters Info
- */
-typedef struct
-{
-    /**
-    * specifies the recovery point of output pictures in output order
-    */
-    WORD32 i4_recovery_poc_cnt;
-
-    UWORD8 u1_exact_match_flag;
-
-    /**
-    * indicates the presence or absence of a broken link in the NAL unit
-    * stream at the location of the recovery point SEI message
-    */
-
-    UWORD8 u1_broken_link_flag;
-
-}recovery_point_sei_params_t;
-/**
- * Structure to hold active parameter parameter set SEI parameters Info
- */
-typedef struct
-{
-    /*
-    * active vps id
-    */
-
-    UWORD8 u1_active_video_parameter_set_id;
-
-    /*
-    * default set to zero.
-    */
-    UWORD8 u1_self_contained_cvs_flag;
-
-    UWORD8 u1_no_parameter_set_update_flag;
-
-    UWORD8 u1_num_sps_ids_minus1;
-
-    /*
-    * active sps id
-    */
-    UWORD8 au1_active_seq_parameter_set_id[15];
-
-}active_parameter_set_sei_param_t;
-
-/**
- * Structure to hold SEI parameters Info
- */
-typedef struct
-{
-
-    WORD8 i1_sei_parameters_present_flag;
-
-    WORD8 i1_aud_present_flag;
-
-    WORD8 i1_buf_period_params_present_flag;
-
-    WORD8 i1_pic_timing_params_present_flag;
-
-    WORD8 i1_recovery_point_params_present_flag;
-
-    buf_period_sei_params_t  s_buf_period_sei_params;
-
-    pic_timing_sei_params_t  s_pic_timing_sei_params;
-
-    recovery_point_sei_params_t s_recovery_point_params;
-
-    active_parameter_set_sei_param_t s_active_parameter_set_sei_params;
-
-
-} sei_params_t;
-
-
-
 /**
  * Structure to hold slice header info
  */
diff --git a/common/ihevc_trans_tables.c b/common/ihevc_trans_tables.c
index 139699a..7c93662 100644
--- a/common/ihevc_trans_tables.c
+++ b/common/ihevc_trans_tables.c
@@ -93,6 +93,12 @@
     40, 45, 51, 57, 64, 72
 };
 
+/*Multiplied by 16 for flat scale coeff */
+const WORD32 g_ihevc_iquant_scales_flat_scale[6] =
+{
+    640, 720, 816, 912, 1024, 1152
+};
+
 const WORD16 g_ihevc_iquant_intr_scales[6][8] =
 {
     { 40, 40, 40, 40, 40, 40, 40, 40 },
@@ -250,6 +256,25 @@
     {  9, -25,  43, -57,  70, -80,  87, -90,  90, -87,  80, -70,  57, -43,  25,  -9 }
 };
 
+const WORD32 g_ai4_ihevc_trans_16[16][8] =
+{
+    { 64,  64,  64,  64,  64,  64,  64,  64 },
+    { 90,  87,  80,  70,  57,  43,  25,   9 },
+    { 89,  75,  50,  18, -18, -50, -75, -89 },
+    { 87,  57,   9, -43, -80, -90, -70, -25 },
+    { 83,  36, -36, -83, -83, -36,  36,  83 },
+    { 80,   9, -70, -87, -25,  57,  90,  43 },
+    { 75, -18, -89, -50,  50,  89,  18, -75 },
+    { 70, -43, -87,   9,  90,  25, -80, -57 },
+    { 64, -64, -64,  64,  64, -64, -64,  64 },
+    { 57, -80, -25,  90,  -9, -87,  43,  70 },
+    { 50, -89,  18,  75, -75, -18,  89, -50 },
+    { 43, -90,  57,  25, -87,  70,   9, -80 },
+    { 36, -83,  83, -36, -36,  83, -83,  36 },
+    { 25, -70,  90, -80,  43,   9, -57,  87 },
+    { 18, -50,  75, -89,  89, -75,  50, -18 },
+    {  9, -25,  43, -57,  70, -80,  87, -90}};
+
 const WORD16 g_ai2_ihevc_trans_16_transpose[1][16] =
 {
     { 64, 90, 89, 87, 83, 80, 75, 70, 64, 57, 50, 43, 36, 25, 18, 9 }
@@ -330,7 +355,41 @@
     {  4, -13,  22, -31,  38, -46,  54, -61,  67, -73,  78, -82,  85, -88,  90, -90,  90, -90,  88, -85,  82, -78,  73, -67,  61, -54,  46, -38,  31, -22,  13,  -4 }
 };
 
-
+const WORD32 g_ai4_ihevc_trans_32[32][16] =
+{
+    { 64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64},
+    { 90,  90,  88,  85,  82,  78,  73,  67,  61,  54,  46,  38,  31,  22,  13,   4},
+    { 90,  87,  80,  70,  57,  43,  25,   9,  -9, -25, -43, -57, -70, -80, -87, -90},
+    { 90,  82,  67,  46,  22,  -4, -31, -54, -73, -85, -90, -88, -78, -61, -38, -13},
+    { 89,  75,  50,  18, -18, -50, -75, -89, -89, -75, -50, -18,  18,  50,  75,  89},
+    { 88,  67,  31, -13, -54, -82, -90, -78, -46,  -4,  38,  73,  90,  85,  61,  22},
+    { 87,  57,   9, -43, -80, -90, -70, -25,  25,  70,  90,  80,  43,  -9, -57, -87},
+    { 85,  46, -13, -67, -90, -73, -22,  38,  82,  88,  54,  -4, -61, -90, -78, -31},
+    { 83,  36, -36, -83, -83, -36,  36,  83,  83,  36, -36, -83, -83, -36,  36,  83},
+    { 82,  22, -54, -90, -61,  13,  78,  85,  31, -46, -90, -67,   4,  73,  88,  38},
+    { 80,   9, -70, -87, -25,  57,  90,  43, -43, -90, -57,  25,  87,  70,  -9, -80},
+    { 78,  -4, -82, -73,  13,  85,  67, -22, -88, -61,  31,  90,  54, -38, -90, -46},
+    { 75, -18, -89, -50,  50,  89,  18, -75, -75,  18,  89,  50, -50, -89, -18,  75},
+    { 73, -31, -90, -22,  78,  67, -38, -90, -13,  82,  61, -46, -88,  -4,  85,  54},
+    { 70, -43, -87,   9,  90,  25, -80, -57,  57,  80, -25, -90,  -9,  87,  43, -70},
+    { 67, -54, -78,  38,  85, -22, -90,   4,  90,  13, -88, -31,  82,  46, -73, -61},
+    { 64, -64, -64,  64,  64, -64, -64,  64,  64, -64, -64,  64,  64, -64, -64,  64},
+    { 61, -73, -46,  82,  31, -88, -13,  90,  -4, -90,  22,  85, -38, -78,  54,  67},
+    { 57, -80, -25,  90,  -9, -87,  43,  70, -70, -43,  87,   9, -90,  25,  80, -57},
+    { 54, -85,  -4,  88, -46, -61,  82,  13, -90,  38,  67, -78, -22,  90, -31, -73},
+    { 50, -89,  18,  75, -75, -18,  89, -50, -50,  89, -18, -75,  75,  18, -89,  50},
+    { 46, -90,  38,  54, -90,  31,  61, -88,  22,  67, -85,  13,  73, -82,   4,  78},
+    { 43, -90,  57,  25, -87,  70,   9, -80,  80,  -9, -70,  87, -25, -57,  90, -43},
+    { 38, -88,  73,  -4, -67,  90, -46, -31,  85, -78,  13,  61, -90,  54,  22, -82},
+    { 36, -83,  83, -36, -36,  83, -83,  36,  36, -83,  83, -36, -36,  83, -83,  36},
+    { 31, -78,  90, -61,   4,  54, -88,  82, -38, -22,  73, -90,  67, -13, -46,  85},
+    { 25, -70,  90, -80,  43,   9, -57,  87, -87,  57,  -9, -43,  80, -90,  70, -25},
+    { 22, -61,  85, -90,  73, -38,  -4,  46, -78,  90, -82,  54, -13, -31,  67, -88},
+    { 18, -50,  75, -89,  89, -75,  50, -18, -18,  50, -75,  89, -89,  75, -50,  18},
+    { 13, -38,  61, -78,  88, -90,  85, -73,  54, -31,   4,  22, -46,  67, -82,  90},
+    {  9, -25,  43, -57,  70, -80,  87, -90,  90, -87,  80, -70,  57, -43,  25,  -9},
+    {  4, -13,  22, -31,  38, -46,  54, -61,  67, -73,  78, -82,  85, -88,  90, -90}
+};
 
 const WORD32 g_ai2_ihevc_trans_32_intr_16[15][4] =
 { /* 4*32 = 128 bit */
diff --git a/common/ihevc_trans_tables.h b/common/ihevc_trans_tables.h
index 7295967..581cd92 100644
--- a/common/ihevc_trans_tables.h
+++ b/common/ihevc_trans_tables.h
@@ -41,6 +41,8 @@
 
 extern const WORD16 g_ihevc_iquant_intr_scales[6][8];
 
+extern const WORD32 g_ihevc_iquant_scales_flat_scale[6];
+
 extern const WORD32 g_ihevc_quant_scales[6];
 
 extern const WORD16 g_ai2_ihevc_trans_dst_4[4][4];
@@ -52,10 +54,11 @@
 extern const WORD16 g_ai2_ihevc_trans_8[8][8];
 
 extern const WORD16 g_ai2_ihevc_trans_16[16][16];
+extern const WORD32 g_ai4_ihevc_trans_16[16][8];
 extern const WORD16 g_ai2_ihevc_trans_16_transpose[1][16];
 extern const WORD16 g_ai2_ihevc_trans_32_transpose[1][32];
 extern const WORD16 g_ai2_ihevc_trans_32[32][32];
-
+extern const WORD32 g_ai4_ihevc_trans_32[32][16];
 
 extern const WORD32 g_ai4_ihevc_trans_dst_intr_4[3][4];
 
diff --git a/common/ihevc_typedefs.h b/common/ihevc_typedefs.h
index 47a7a2f..7a82b7f 100644
--- a/common/ihevc_typedefs.h
+++ b/common/ihevc_typedefs.h
@@ -48,6 +48,7 @@
 
 typedef double          DOUBLE;
 
+typedef char           STRWORD8;
 
 
 
diff --git a/common/ithread.c b/common/ithread.c
index e3d5e00..0b55206 100644
--- a/common/ithread.c
+++ b/common/ithread.c
@@ -38,7 +38,6 @@
 #include "ithread.h"
 #include <sys/types.h>
 
-#ifndef X86_MSVC
 //#define PTHREAD_AFFINITY
 //#define SYSCALL_AFFINITY
 
@@ -52,266 +51,6 @@
 #include <semaphore.h>
 #include <unistd.h>
 
-
-#endif
-
-
-
-
-#ifdef X86_MSVC
-
-#include <windows.h>
-#define SEM_MAX_COUNT       100
-#define SEM_INCREMENT_COUNT 1
-
-UWORD32 ithread_get_handle_size(void)
-{
-    return (sizeof(HANDLE));
-}
-
-UWORD32 ithread_get_mutex_lock_size(void)
-{
-    return (sizeof(HANDLE));
-}
-
-WORD32 ithread_create(void *thread_handle, void *attribute, void *strt, void *argument)
-{
-    HANDLE *ppv_thread_handle;
-    HANDLE thread_handle_value;
-
-    if(0 == thread_handle)
-        return -1;
-
-    ppv_thread_handle = (HANDLE *)thread_handle;
-    thread_handle_value = (void *)CreateThread
-                    (NULL,                              /* Attributes      */
-                     1024 * 128,                        /* Stack size      */
-                     (LPTHREAD_START_ROUTINE)strt,      /* Thread function */
-                     argument,                          /* Parameters      */
-                     0,                                 /* Creation flags  */
-                     NULL);                             /* Thread ID       */
-    *ppv_thread_handle = (HANDLE)thread_handle_value;
-
-    return 0;
-}
-
-WORD32 ithread_join(void *thread_handle, void **val_ptr)
-{
-    HANDLE *ppv_thread_handle;
-    HANDLE thread_handle_value;
-
-    if(0 == thread_handle)
-        return -1;
-
-    ppv_thread_handle = (HANDLE *)thread_handle;
-    thread_handle_value = *ppv_thread_handle;
-
-    if(WAIT_OBJECT_0 == WaitForSingleObject(thread_handle_value, INFINITE))
-    {
-        CloseHandle(thread_handle_value);
-    }
-
-    return 0;
-}
-
-void ithread_exit(void *thread_handle)
-{
-    HANDLE *ppv_thread_handle;
-    HANDLE thread_handle_value;
-    DWORD thread_exit_code;
-
-    if(0 == thread_handle)
-        return;
-
-    ppv_thread_handle = (HANDLE *)thread_handle;
-    thread_handle_value = *ppv_thread_handle;
-    /* Get exit code for thread. If the return value is 0, means thread is busy */
-    if(0 != GetExitCodeThread(thread_handle_value, &thread_exit_code))
-    {
-        TerminateThread(thread_handle_value, thread_exit_code);
-    }
-
-    return;
-}
-
-WORD32 ithread_get_mutex_struct_size(void)
-{
-    return (sizeof(HANDLE));
-}
-
-WORD32 ithread_mutex_init(void *mutex)
-{
-    HANDLE *ppv_mutex_handle;
-    HANDLE mutex_handle_value;
-
-    if(0 == mutex)
-        return -1;
-
-    ppv_mutex_handle = (HANDLE *)mutex;
-    mutex_handle_value = CreateSemaphore(NULL, 1, 1, NULL);
-    *ppv_mutex_handle = mutex_handle_value;
-    return 0;
-}
-
-WORD32 ithread_mutex_destroy(void *mutex)
-{
-    HANDLE *ppv_mutex_handle;
-    HANDLE mutex_handle_value;
-
-    if(0 == mutex)
-        return -1;
-
-    ppv_mutex_handle = (HANDLE *)mutex;
-    mutex_handle_value = *ppv_mutex_handle;
-    CloseHandle(mutex_handle_value);
-    return 0;
-}
-
-WORD32 ithread_mutex_lock(void *mutex)
-{
-    HANDLE *ppv_mutex_handle;
-    HANDLE mutex_handle_value;
-    DWORD  result = 0;
-
-    if(0 == mutex)
-        return -1;
-
-    ppv_mutex_handle = (HANDLE *)mutex;
-    mutex_handle_value = *ppv_mutex_handle;
-    result = WaitForSingleObject(mutex_handle_value, INFINITE);
-
-    if(WAIT_OBJECT_0 == result)
-        return 0;
-
-    return 1;
-
-}
-
-WORD32 ithread_mutex_unlock(void *mutex)
-{
-    HANDLE *ppv_mutex_handle;
-    HANDLE mutex_handle_value;
-    DWORD  result = 0;
-
-    if(0 == mutex)
-        return -1;
-
-    ppv_mutex_handle = (HANDLE *)mutex;
-    mutex_handle_value = *ppv_mutex_handle;
-    result = ReleaseSemaphore(mutex_handle_value, 1, NULL);
-
-    if(0 == result)
-        return -1;
-
-    return 0;
-}
-
-void ithread_yield(void) { }
-
-void ithread_usleep(UWORD32 u4_time_us)
-{
-    UWORD32 u4_time_ms = u4_time_us / 1000;
-    Sleep(u4_time_ms);
-}
-
-void ithread_msleep(UWORD32 u4_time_ms)
-{
-    Sleep(u4_time_ms);
-}
-
-
-void ithread_sleep(UWORD32 u4_time)
-{
-    UWORD32 u4_time_ms = u4_time * 1000;
-    Sleep(u4_time_ms);
-}
-
-UWORD32 ithread_get_sem_struct_size(void)
-{
-    return (sizeof(HANDLE));
-}
-
-WORD32 ithread_sem_init(void *sem, WORD32 pshared, UWORD32 value)
-{
-    HANDLE *sem_handle = (HANDLE *)sem;
-    HANDLE sem_handle_value;
-
-    if(0 == sem)
-        return -1;
-
-    sem_handle_value = CreateSemaphore(NULL,  /* Security Attribute*/
-                                       value,  /* Initial count     */
-                                       SEM_MAX_COUNT, /* Max value         */
-                                       NULL);        /* Name, not used    */
-    *sem_handle = sem_handle_value;
-    return 0;
-}
-
-WORD32 ithread_sem_post(void *sem)
-{
-    HANDLE *sem_handle = (HANDLE *)sem;
-    HANDLE sem_handle_value;
-
-    if(0 == sem)
-        return -1;
-
-    sem_handle_value = *sem_handle;
-
-    /* Post on Semaphore by releasing the lock on mutex */
-    if(ReleaseSemaphore(sem_handle_value, SEM_INCREMENT_COUNT, NULL))
-        return 0;
-
-    return -1;
-}
-
-WORD32 ithread_sem_wait(void *sem)
-{
-    DWORD          result = 0;
-    HANDLE *sem_handle = (HANDLE *)sem;
-    HANDLE sem_handle_value;
-
-    if(0 == sem)
-        return -1;
-
-    sem_handle_value = *sem_handle;
-
-    /* Wait on Semaphore object infinitly */
-    result = WaitForSingleObject(sem_handle_value, INFINITE);
-
-    /* If lock on semaphore is acquired, return SUCCESS */
-    if(WAIT_OBJECT_0 == result)
-        return 0;
-
-    /* If call timeouts, return FAILURE */
-    if(WAIT_TIMEOUT == result)
-        return -1;
-
-    return 0;
-}
-
-WORD32 ithread_sem_destroy(void *sem)
-{
-    HANDLE *sem_handle = (HANDLE *)sem;
-    HANDLE sem_handle_value;
-
-    if(0 == sem)
-        return -1;
-
-    sem_handle_value = *sem_handle;
-
-    if(FALSE == CloseHandle(sem_handle_value))
-    {
-        return -1;
-    }
-    return 0;
-}
-
-WORD32 ithread_set_affinity(WORD32 core_id)
-{
-    return 1;
-}
-
-#else
 UWORD32 ithread_get_handle_size(void)
 {
     return sizeof(pthread_t);
@@ -322,7 +61,6 @@
     return sizeof(pthread_mutex_t);
 }
 
-
 WORD32 ithread_create(void *thread_handle, void *attribute, void *strt, void *argument)
 {
     return pthread_create((pthread_t *)thread_handle, attribute, (void * (*)(void *))strt, argument);
@@ -388,7 +126,6 @@
     return (sizeof(sem_t));
 }
 
-
 WORD32 ithread_sem_init(void *sem, WORD32 pshared, UWORD32 value)
 {
     return sem_init((sem_t *)sem, pshared, value);
@@ -399,19 +136,16 @@
     return sem_post((sem_t *)sem);
 }
 
-
 WORD32 ithread_sem_wait(void *sem)
 {
     return sem_wait((sem_t *)sem);
 }
 
-
 WORD32 ithread_sem_destroy(void *sem)
 {
     return sem_destroy((sem_t *)sem);
 }
 
-
 WORD32 ithread_set_affinity(WORD32 core_id)
 {
 
@@ -446,6 +180,4 @@
 #endif
 
     return core_id;
-
 }
-#endif
diff --git a/common/iv.h b/common/iv.h
index a60cf47..842bb6c 100644
--- a/common/iv.h
+++ b/common/iv.h
@@ -63,20 +63,6 @@
     IV_FAIL                                     = 0x1,
 }IV_API_CALL_STATUS_T;
 
-/* IV_MEM_TYPE_T: This Enumeration defines the type of memory (Internal/Ext */
-/* -ernal) along with the cacheable/non-cacheable attributes                */
-
-typedef enum {
-    IV_NA_MEM_TYPE                              = 0x7FFFFFFF,
-    IV_INTERNAL_CACHEABLE_PERSISTENT_MEM        = 0x1,
-    IV_INTERNAL_CACHEABLE_SCRATCH_MEM           = 0x2,
-    IV_EXTERNAL_CACHEABLE_PERSISTENT_MEM        = 0x3,
-    IV_EXTERNAL_CACHEABLE_SCRATCH_MEM           = 0x4,
-    IV_INTERNAL_NONCACHEABLE_PERSISTENT_MEM     = 0x5,
-    IV_INTERNAL_NONCACHEABLE_SCRATCH_MEM        = 0x6,
-    IV_EXTERNAL_NONCACHEABLE_PERSISTENT_MEM     = 0x7,
-    IV_EXTERNAL_NONCACHEABLE_SCRATCH_MEM        = 0x8
-}IV_MEM_TYPE_T;
 
 /* IV_COLOR_FORMAT_T: This enumeration lists all the color formats which    */
 /* finds usage in video/image codecs                                        */
@@ -148,10 +134,6 @@
 /* IV_API_COMMAND_TYPE_T:API command type                                   */
 typedef enum {
     IV_CMD_NA                           = 0x7FFFFFFF,
-    IV_CMD_GET_NUM_MEM_REC              = 0x0,
-    IV_CMD_FILL_NUM_MEM_REC             = 0x1,
-    IV_CMD_RETRIEVE_MEMREC              = 0x2,
-    IV_CMD_INIT                         = 0x3,
     IV_CMD_DUMMY_ELEMENT                = 0x4,
 }IV_API_COMMAND_TYPE_T;
 
@@ -178,35 +160,6 @@
     void                                        *pv_codec_handle;
 }iv_obj_t;
 
-/* iv_mem_rec_t: This structure defines the memory record holder which will  */
-/* be used by the codec to communicate its memory requirements to the        */
-/* application through appropriate API functions                             */
-
-typedef struct {
-    /**
-     * u4_size of the structure
-     */
-    UWORD32                                     u4_size;
-
-    /**
-     * Pointer to the memory allocated by the application
-     */
-    void                                        *pv_base;
-
-    /**
-     * u4_size of the memory to be allocated
-     */
-    UWORD32                                     u4_mem_size;
-
-    /**
-     * Alignment of the memory pointer
-     */
-    UWORD32                                     u4_mem_alignment;
-    /**
-     * Nature of the memory to be allocated
-     */
-    IV_MEM_TYPE_T                               e_mem_type;
-}iv_mem_rec_t;
 
 /* IV_YUV_BUF_T: This structure defines attributes for the yuv buffer        */
 
@@ -277,141 +230,6 @@
     UWORD32                                     u4_v_strd;
 }iv_yuv_buf_t;
 
-/*****************************************************************************/
-/*  Get Number of Memory Records                                             */
-/*****************************************************************************/
-
-/* IV_API_COMMAND_TYPE_T::e_cmd = IV_CMD_GET_NUM_MEM_REC                     */
-
-
-typedef struct {
-    /**
-     * u4_size of the structure
-     */
-    UWORD32                                     u4_size;
-
-    /**
-     * cmd
-     */
-    IV_API_COMMAND_TYPE_T                       e_cmd;
-}iv_num_mem_rec_ip_t;
-
-
-typedef struct {
-    /**
-     * u4_size of the structure
-     */
-    UWORD32                                     u4_size;
-
-    /**
-     * error code
-     */
-    UWORD32                                     u4_error_code;
-
-    /**
-     * num_mem_rec
-     */
-    UWORD32                                     u4_num_mem_rec;
-}iv_num_mem_rec_op_t;
-
-
-/*****************************************************************************/
-/*  Fill Memory Records                                                      */
-/*****************************************************************************/
-
-/* IV_API_COMMAND_TYPE_T::e_cmd = IV_CMD_FILL_NUM_MEM_REC                    */
-
-
-typedef struct {
-    /**
-     * u4_size of the structure
-     */
-    UWORD32                                     u4_size;
-
-    /**
-     * cmd
-     */
-    IV_API_COMMAND_TYPE_T                       e_cmd;
-
-    /**
-     * pointer to array of memrecords structures should be filled by codec
-    with details of memory resource requirements
-     */
-    iv_mem_rec_t                                *pv_mem_rec_location;
-
-    /**
-     * maximum width for which codec should request memory requirements
-     */
-    UWORD32                                     u4_max_frm_wd;
-
-    /**
-     * maximum height for which codec should request memory requirements
-     */
-    UWORD32                                     u4_max_frm_ht;
-}iv_fill_mem_rec_ip_t;
-
-
-typedef struct {
-    /**
-     * u4_size of the structure
-     */
-    UWORD32                                     u4_size;
-
-    /**
-     * error_code
-     */
-    UWORD32                                     u4_error_code;
-
-    /**
-     * no of memory record structures which are filled by codec
-     */
-    UWORD32                                     u4_num_mem_rec_filled;
-}iv_fill_mem_rec_op_t;
-
-
-/*****************************************************************************/
-/*  Retrieve Memory Records                                                  */
-/*****************************************************************************/
-
-/* IV_API_COMMAND_TYPE_T::e_cmd = IV_CMD_RETRIEVE_MEMREC                     */
-
-
-
-typedef struct {
-    /**
-     * u4_size of the structure
-     */
-    UWORD32                                     u4_size;
-
-    /**
-     * cmd
-     */
-    IV_API_COMMAND_TYPE_T                       e_cmd;
-
-    /**
-     * array of structures where codec should fill with all resources(memory) with it
-     */
-    iv_mem_rec_t                                *pv_mem_rec_location;
-}iv_retrieve_mem_rec_ip_t;
-
-
-typedef struct {
-    /**
-     * u4_size of the structure
-     */
-    UWORD32                                     u4_size;
-
-    /**
-     * error_code
-     */
-    UWORD32                                     u4_error_code;
-
-    /**
-     * no of memory records filled by codec
-     */
-    UWORD32                                     u4_num_mem_rec_filled;
-}iv_retrieve_mem_rec_op_t;
-
 
 
 #endif /* _IV_H */
diff --git a/common/ivd.h b/common/ivd.h
index 812da18..fe39a78 100644
--- a/common/ivd.h
+++ b/common/ivd.h
@@ -126,7 +126,9 @@
 /* IVD_API_COMMAND_TYPE_T:API command type                                   */
 typedef enum {
     IVD_CMD_VIDEO_NA                          = 0x7FFFFFFF,
-    IVD_CMD_VIDEO_CTL                         = IV_CMD_DUMMY_ELEMENT + 1,
+    IVD_CMD_CREATE                            = IV_CMD_DUMMY_ELEMENT + 1,
+    IVD_CMD_DELETE,
+    IVD_CMD_VIDEO_CTL,
     IVD_CMD_VIDEO_DECODE,
     IVD_CMD_GET_DISPLAY_FRAME,
     IVD_CMD_REL_DISPLAY_FRAME,
@@ -226,7 +228,8 @@
     IVD_DEC_REF_BUF_NULL                        = 0x28,
     IVD_DEC_FRM_SKIPPED                         = 0x29,
     IVD_RES_CHANGED                             = 0x2a,
-    IVD_DUMMY_ELEMENT_FOR_CODEC_EXTENSIONS      = 0x300,
+    IVD_MEM_ALLOC_FAILED                        = 0x2b,
+    IVD_DUMMY_ELEMENT_FOR_CODEC_EXTENSIONS      = 0xD0,
 }IVD_ERROR_CODES_T;
 
 
@@ -255,10 +258,10 @@
 }ivd_out_bufdesc_t;
 
 /*****************************************************************************/
-/*   Initialize decoder                                                      */
+/*   Create decoder                                                          */
 /*****************************************************************************/
 
-/* IVD_API_COMMAND_TYPE_T::e_cmd = IVD_CMD_INIT                              */
+/* IVD_API_COMMAND_TYPE_T::e_cmd = IVD_CMD_CREATE                            */
 
 
 typedef struct {
@@ -273,26 +276,36 @@
     IVD_API_COMMAND_TYPE_T                  e_cmd;
 
     /**
-     *no memrecords which are allocated on request of codec through fill mem records
-     */
-    UWORD32                                 u4_num_mem_rec;
-    /**
-     * maximum height for which codec should be initialized
-     */
-    UWORD32                                 u4_frm_max_wd;
-    /**
-     * maximum width for which codec should be initialized
-     */
-    UWORD32                                 u4_frm_max_ht;
-    /**
      * format in which codec has to give out frame data for display
      */
     IV_COLOR_FORMAT_T                       e_output_format;
+
     /**
-     * pointer to memrecord array, which contains allocated resources
+     * Flag to indicate shared display buffer mode
      */
-    iv_mem_rec_t                            *pv_mem_rec_location;
-}ivd_init_ip_t;
+    UWORD32                                 u4_share_disp_buf;
+
+    /**
+     * Pointer to a function for aligned allocation.
+     */
+    void    *(*pf_aligned_alloc)(void *pv_mem_ctxt, WORD32 alignment, WORD32 size);
+
+    /**
+     * Pointer to a function for aligned free.
+     */
+    void   (*pf_aligned_free)(void *pv_mem_ctxt, void *pv_buf);
+
+    /**
+     * Pointer to memory context that is needed during alloc/free for custom
+     * memory managers. This will be passed as first argument to pf_aligned_alloc and
+     * pf_aligned_free.
+     * If application is using standard memory functions like
+     * malloc/aligned_malloc/memalign/free/aligned_free,
+     * then this is not needed and can be set to NULL
+     */
+    void    *pv_mem_ctxt;
+
+}ivd_create_ip_t;
 
 
 typedef struct {
@@ -305,8 +318,49 @@
      * u4_error_code
      */
     UWORD32                                 u4_error_code;
-}ivd_init_op_t;
 
+    /**
+     * Codec Handle
+     */
+    void                                    *pv_handle;
+
+}ivd_create_op_t;
+
+
+/*****************************************************************************/
+/*  Delete decoder                                                           */
+/*****************************************************************************/
+
+/* IVD_API_COMMAND_TYPE_T::e_cmd = IVD_CMD_DELETE                              */
+
+
+
+typedef struct {
+    /**
+     * u4_size of the structure
+     */
+    UWORD32                                     u4_size;
+
+    /**
+     * cmd
+     */
+    IVD_API_COMMAND_TYPE_T                       e_cmd;
+
+}ivd_delete_ip_t;
+
+
+typedef struct {
+    /**
+     * u4_size of the structure
+     */
+    UWORD32                                     u4_size;
+
+    /**
+     * error_code
+     */
+    UWORD32                                     u4_error_code;
+
+}ivd_delete_op_t;
 
 /*****************************************************************************/
 /*   Video Decode                                                            */
diff --git a/common/mips/ihevc_platform_macros.h b/common/mips/ihevc_platform_macros.h
index e614aea..b028624 100644
--- a/common/mips/ihevc_platform_macros.h
+++ b/common/mips/ihevc_platform_macros.h
@@ -58,10 +58,17 @@
 #define SHR_NEG(val,shift)  ((shift>0)?(val>>shift):(val<<(-shift)))
 #define SHL_NEG(val,shift)  ((shift<0)?(val>>(-shift)):(val<<shift))
 
-static inline UWORD32 CLZ(UWORD32 x)
+static inline UWORD32 CLZ(UWORD32 u4_word)
 {
-    asm("clz %0, %1" : "=r"(x) : "r"(x));
-    return x;
+    if(u4_word)
+        return (__builtin_clz(u4_word));
+    else
+        return 32;
+}
+
+static inline UWORD32 CLZNZ(UWORD32 u4_word)
+{
+   return (__builtin_clz(u4_word));
 }
 
 static inline UWORD32 CTZ(UWORD32 u4_word)
@@ -76,6 +83,8 @@
     }
 }
 
+#define POPCNT_U32(x)       __builtin_popcount(x)
+
 #define NOP(nop_cnt)    {UWORD32 nop_i; for (nop_i = 0; nop_i < nop_cnt; nop_i++);}
 
 #define INLINE
diff --git a/common/x86/ihevc_platform_macros.h b/common/x86/ihevc_platform_macros.h
index 22c58d2..1d5dc74 100644
--- a/common/x86/ihevc_platform_macros.h
+++ b/common/x86/ihevc_platform_macros.h
@@ -68,6 +68,8 @@
 
 #define NOP(nop_cnt)    {UWORD32 nop_i; for (nop_i = 0; nop_i < nop_cnt; nop_i++);}
 
+#define POPCNT_U32(x)       __builtin_popcount(x)
+
 #define PLD(a)
 #define INLINE inline
 
@@ -78,7 +80,10 @@
     else
         return 32;
 }
-
+static INLINE UWORD32 CLZNZ(UWORD32 u4_word)
+{
+   return (__builtin_clz(u4_word));
+}
 static INLINE UWORD32 CTZ(UWORD32 u4_word)
 {
     if(0 == u4_word)
diff --git a/decoder/ihevcd_api.c b/decoder/ihevcd_api.c
index ea0b8b5..55d965c 100644
--- a/decoder/ihevcd_api.c
+++ b/decoder/ihevcd_api.c
@@ -32,8 +32,8 @@
 * - ihevcd_set_default_params()
 * - ihevcd_init()
 * - ihevcd_get_num_rec()
-* - ihevcd_fill_num_mem_rec()
-* - ihevcd_init_mem_rec()
+* - ihevcd_allocate_static_bufs()
+* - ihevcd_create()
 * - ihevcd_retrieve_memrec()
 * - ihevcd_set_display_frame()
 * - ihevcd_set_flush_mode()
@@ -92,12 +92,16 @@
 #include "ihevcd_job_queue.h"
 #include "ihevcd_statistics.h"
 
+
+#define ALIGNED_FREE(ps_codec, y) \
+if(y) {ps_codec->pf_aligned_free(ps_codec->pv_mem_ctxt, ((void *)y)); (y) = NULL;}
+
 /*****************************************************************************/
 /* Function Prototypes                                                       */
 /*****************************************************************************/
 IV_API_CALL_STATUS_T ihevcd_get_version(CHAR *pc_version_string,
                                         UWORD32 u4_version_buffer_size);
-
+WORD32 ihevcd_free_dynamic_bufs(codec_t *ps_codec);
 
 
 /**
@@ -133,7 +137,7 @@
     IVD_API_COMMAND_TYPE_T e_cmd;
     UWORD32 *pu4_api_ip;
     UWORD32 *pu4_api_op;
-    WORD32 i, j;
+    WORD32 i;
 
     if(NULL == pv_api_op)
         return (IV_FAIL);
@@ -149,31 +153,14 @@
     /* error checks on handle */
     switch((WORD32)e_cmd)
     {
-        case IV_CMD_GET_NUM_MEM_REC:
-        case IV_CMD_FILL_NUM_MEM_REC:
+        case IVD_CMD_CREATE:
             break;
-        case IV_CMD_INIT:
-            if(ps_handle == NULL)
-            {
-                *(pu4_api_op + 1) |= 1 << IVD_UNSUPPORTEDPARAM;
-                *(pu4_api_op + 1) |= IVD_HANDLE_NULL;
-                return IV_FAIL;
-            }
 
-            if(ps_handle->u4_size != sizeof(iv_obj_t))
-            {
-                *(pu4_api_op + 1) |= 1 << IVD_UNSUPPORTEDPARAM;
-                *(pu4_api_op + 1) |= IVD_HANDLE_STRUCT_SIZE_INCORRECT;
-                DEBUG("Sizes do not match. Expected: %d, Got: %d",
-                                sizeof(iv_obj_t), ps_handle->u4_size);
-                return IV_FAIL;
-            }
-            break;
         case IVD_CMD_REL_DISPLAY_FRAME:
         case IVD_CMD_SET_DISPLAY_FRAME:
         case IVD_CMD_GET_DISPLAY_FRAME:
         case IVD_CMD_VIDEO_DECODE:
-        case IV_CMD_RETRIEVE_MEMREC:
+        case IVD_CMD_DELETE:
         case IVD_CMD_VIDEO_CTL:
             if(ps_handle == NULL)
             {
@@ -205,540 +192,57 @@
 
     switch((WORD32)e_cmd)
     {
-        case IV_CMD_GET_NUM_MEM_REC:
+        case IVD_CMD_CREATE:
         {
-            ihevcd_cxa_num_mem_rec_ip_t *ps_ip =
-                            (ihevcd_cxa_num_mem_rec_ip_t *)pv_api_ip;
-            ihevcd_cxa_num_mem_rec_op_t *ps_op =
-                            (ihevcd_cxa_num_mem_rec_op_t *)pv_api_op;
-            ps_op->s_ivd_num_mem_rec_op_t.u4_error_code = 0;
+            ihevcd_cxa_create_ip_t *ps_ip = (ihevcd_cxa_create_ip_t *)pv_api_ip;
+            ihevcd_cxa_create_op_t *ps_op = (ihevcd_cxa_create_op_t *)pv_api_op;
 
-            if(ps_ip->s_ivd_num_mem_rec_ip_t.u4_size
-                            != sizeof(ihevcd_cxa_num_mem_rec_ip_t))
+
+            ps_op->s_ivd_create_op_t.u4_error_code = 0;
+
+            if((ps_ip->s_ivd_create_ip_t.u4_size > sizeof(ihevcd_cxa_create_ip_t))
+                            || (ps_ip->s_ivd_create_ip_t.u4_size
+                                            < sizeof(ivd_create_ip_t)))
             {
-                ps_op->s_ivd_num_mem_rec_op_t.u4_error_code |= 1
+                ps_op->s_ivd_create_op_t.u4_error_code |= 1
                                 << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_num_mem_rec_op_t.u4_error_code |=
+                ps_op->s_ivd_create_op_t.u4_error_code |=
                                 IVD_IP_API_STRUCT_SIZE_INCORRECT;
+
                 return (IV_FAIL);
             }
 
-            if(ps_op->s_ivd_num_mem_rec_op_t.u4_size
-                            != sizeof(ihevcd_cxa_num_mem_rec_op_t))
+            if((ps_op->s_ivd_create_op_t.u4_size != sizeof(ihevcd_cxa_create_op_t))
+                            && (ps_op->s_ivd_create_op_t.u4_size
+                                            != sizeof(ivd_create_op_t)))
             {
-                ps_op->s_ivd_num_mem_rec_op_t.u4_error_code |= 1
+                ps_op->s_ivd_create_op_t.u4_error_code |= 1
                                 << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_num_mem_rec_op_t.u4_error_code |=
+                ps_op->s_ivd_create_op_t.u4_error_code |=
                                 IVD_OP_API_STRUCT_SIZE_INCORRECT;
-                return (IV_FAIL);
-            }
-        }
-            break;
-        case IV_CMD_FILL_NUM_MEM_REC:
-        {
-            ihevcd_cxa_fill_mem_rec_ip_t *ps_ip =
-                            (ihevcd_cxa_fill_mem_rec_ip_t *)pv_api_ip;
-            ihevcd_cxa_fill_mem_rec_op_t *ps_op =
-                            (ihevcd_cxa_fill_mem_rec_op_t *)pv_api_op;
-            iv_mem_rec_t *ps_mem_rec;
-            WORD32 max_wd = ps_ip->s_ivd_fill_mem_rec_ip_t.u4_max_frm_wd;
-            WORD32 max_ht = ps_ip->s_ivd_fill_mem_rec_ip_t.u4_max_frm_ht;
 
-            max_wd = ALIGN64(max_wd);
-            max_ht = ALIGN64(max_ht);
-
-            ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code = 0;
-
-            if((ps_ip->s_ivd_fill_mem_rec_ip_t.u4_size
-                            > sizeof(ihevcd_cxa_fill_mem_rec_ip_t))
-                            || (ps_ip->s_ivd_fill_mem_rec_ip_t.u4_size
-                                            < sizeof(iv_fill_mem_rec_ip_t)))
-            {
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                                IVD_IP_API_STRUCT_SIZE_INCORRECT;
                 return (IV_FAIL);
             }
 
-            if((ps_op->s_ivd_fill_mem_rec_op_t.u4_size
-                            != sizeof(ihevcd_cxa_fill_mem_rec_op_t))
-                            && (ps_op->s_ivd_fill_mem_rec_op_t.u4_size
-                                            != sizeof(iv_fill_mem_rec_op_t)))
-            {
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                                IVD_OP_API_STRUCT_SIZE_INCORRECT;
-                return (IV_FAIL);
-            }
 
-            if(max_wd < MIN_WD)
-            {
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                                IVD_REQUESTED_WIDTH_NOT_SUPPPORTED;
-                return (IV_FAIL);
-            }
-
-            if(max_wd > MAX_WD)
-            {
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                                IVD_REQUESTED_WIDTH_NOT_SUPPPORTED;
-                return (IV_FAIL);
-            }
-
-            if(max_ht < MIN_HT)
-            {
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                                IVD_REQUESTED_HEIGHT_NOT_SUPPPORTED;
-                return (IV_FAIL);
-            }
-
-            if((max_ht * max_wd) > (MAX_HT * MAX_WD))
-
-            {
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                                IVD_REQUESTED_HEIGHT_NOT_SUPPPORTED;
-                return (IV_FAIL);
-            }
-
-            if(NULL == ps_ip->s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location)
-            {
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                                IVD_NUM_REC_NOT_SUFFICIENT;
-                return (IV_FAIL);
-            }
-
-            /* check memrecords sizes are correct */
-            ps_mem_rec = ps_ip->s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location;
-            for(i = 0; i < MEM_REC_CNT; i++)
-            {
-                if(ps_mem_rec[i].u4_size != sizeof(iv_mem_rec_t))
-                {
-                    ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |= 1
-                                    << IVD_UNSUPPORTEDPARAM;
-                    ps_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                                    IVD_MEM_REC_STRUCT_SIZE_INCORRECT;
-                    return IV_FAIL;
-                }
-            }
-        }
-            break;
-
-        case IV_CMD_INIT:
-        {
-            ihevcd_cxa_init_ip_t *ps_ip = (ihevcd_cxa_init_ip_t *)pv_api_ip;
-            ihevcd_cxa_init_op_t *ps_op = (ihevcd_cxa_init_op_t *)pv_api_op;
-            iv_mem_rec_t *ps_mem_rec;
-            WORD32 max_wd = ps_ip->s_ivd_init_ip_t.u4_frm_max_wd;
-            WORD32 max_ht = ps_ip->s_ivd_init_ip_t.u4_frm_max_ht;
-
-            max_wd = ALIGN64(max_wd);
-            max_ht = ALIGN64(max_ht);
-
-            ps_op->s_ivd_init_op_t.u4_error_code = 0;
-
-            if((ps_ip->s_ivd_init_ip_t.u4_size > sizeof(ihevcd_cxa_init_ip_t))
-                            || (ps_ip->s_ivd_init_ip_t.u4_size
-                                            < sizeof(ivd_init_ip_t)))
-            {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
-                                IVD_IP_API_STRUCT_SIZE_INCORRECT;
-                DEBUG("\n");
-                return (IV_FAIL);
-            }
-
-            if((ps_op->s_ivd_init_op_t.u4_size != sizeof(ihevcd_cxa_init_op_t))
-                            && (ps_op->s_ivd_init_op_t.u4_size
-                                            != sizeof(ivd_init_op_t)))
-            {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
-                                IVD_OP_API_STRUCT_SIZE_INCORRECT;
-                DEBUG("\n");
-                return (IV_FAIL);
-            }
-
-            if(ps_ip->s_ivd_init_ip_t.u4_num_mem_rec != MEM_REC_CNT)
-            {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
-                                IVD_INIT_DEC_NOT_SUFFICIENT;
-                DEBUG("\n");
-                return (IV_FAIL);
-            }
-
-            if(max_wd < MIN_WD)
-            {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
-                                IVD_INIT_DEC_WIDTH_NOT_SUPPPORTED;
-                DEBUG("\n");
-                return (IV_FAIL);
-            }
-
-            if(max_wd > MAX_WD)
-            {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
-                                IVD_INIT_DEC_WIDTH_NOT_SUPPPORTED;
-                DEBUG("\n");
-                return (IV_FAIL);
-            }
-
-            if(max_ht < MIN_HT)
-            {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
-                                IVD_INIT_DEC_HEIGHT_NOT_SUPPPORTED;
-                DEBUG("\n");
-                return (IV_FAIL);
-            }
-
-            if((max_ht * max_wd) > (MAX_HT * MAX_WD))
-
-            {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
-                                IVD_INIT_DEC_HEIGHT_NOT_SUPPPORTED;
-                DEBUG("\n");
-                return (IV_FAIL);
-            }
-
-            if(NULL == ps_ip->s_ivd_init_ip_t.pv_mem_rec_location)
-            {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
-                                IVD_NUM_REC_NOT_SUFFICIENT;
-                DEBUG("\n");
-                return (IV_FAIL);
-            }
-
-            if((ps_ip->s_ivd_init_ip_t.e_output_format != IV_YUV_420P)
-                            && (ps_ip->s_ivd_init_ip_t.e_output_format
+            if((ps_ip->s_ivd_create_ip_t.e_output_format != IV_YUV_420P)
+                            && (ps_ip->s_ivd_create_ip_t.e_output_format
                                             != IV_YUV_422ILE)
-                            && (ps_ip->s_ivd_init_ip_t.e_output_format
+                            && (ps_ip->s_ivd_create_ip_t.e_output_format
                                             != IV_RGB_565)
-                            && (ps_ip->s_ivd_init_ip_t.e_output_format
-                                            != IV_RGBA_8888)
-                            && (ps_ip->s_ivd_init_ip_t.e_output_format
+                            && (ps_ip->s_ivd_create_ip_t.e_output_format
                                             != IV_YUV_420SP_UV)
-                            && (ps_ip->s_ivd_init_ip_t.e_output_format
+                            && (ps_ip->s_ivd_create_ip_t.e_output_format
                                             != IV_YUV_420SP_VU))
             {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
+                ps_op->s_ivd_create_op_t.u4_error_code |= 1
                                 << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
+                ps_op->s_ivd_create_op_t.u4_error_code |=
                                 IVD_INIT_DEC_COL_FMT_NOT_SUPPORTED;
-                DEBUG("\n");
+
                 return (IV_FAIL);
             }
 
-            /* verify number of mem records */
-            if(ps_ip->s_ivd_init_ip_t.u4_num_mem_rec < MEM_REC_CNT)
-            {
-                ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_init_op_t.u4_error_code |=
-                                IVD_INIT_DEC_MEM_REC_NOT_SUFFICIENT;
-                DEBUG("\n");
-                return IV_FAIL;
-            }
-
-            ps_mem_rec = ps_ip->s_ivd_init_ip_t.pv_mem_rec_location;
-            /* check memrecords sizes are correct */
-            for(i = 0; i < (WORD32)ps_ip->s_ivd_init_ip_t.u4_num_mem_rec; i++)
-            {
-                if(ps_mem_rec[i].u4_size != sizeof(iv_mem_rec_t))
-                {
-                    ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                    << IVD_UNSUPPORTEDPARAM;
-                    ps_op->s_ivd_init_op_t.u4_error_code |=
-                                    IVD_MEM_REC_STRUCT_SIZE_INCORRECT;
-                    DEBUG("i: %d\n", i);
-                    return IV_FAIL;
-                }
-                /* check memrecords pointers are not NULL */
-
-                if(ps_mem_rec[i].pv_base == NULL)
-                {
-
-                    ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                    << IVD_UNSUPPORTEDPARAM;
-                    ps_op->s_ivd_init_op_t.u4_error_code |=
-                                    IVD_INIT_DEC_MEM_REC_BASE_NULL;
-                    DEBUG("i: %d\n", i);
-                    return IV_FAIL;
-
-                }
-
-            }
-
-            /* verify memtabs for overlapping regions */
-            {
-                void *start[MEM_REC_CNT];
-                void *end[MEM_REC_CNT];
-
-                start[0] = (ps_mem_rec[0].pv_base);
-                end[0] = (UWORD8 *)(ps_mem_rec[0].pv_base)
-                                + ps_mem_rec[0].u4_mem_size - 1;
-                for(i = 1; i < MEM_REC_CNT; i++)
-                {
-                    /* This array is populated to check memtab overlapp */
-                    start[i] = (ps_mem_rec[i].pv_base);
-                    end[i] = (UWORD8 *)(ps_mem_rec[i].pv_base)
-                                    + ps_mem_rec[i].u4_mem_size - 1;
-
-                    for(j = 0; j < i; j++)
-                    {
-                        if((start[i] >= start[j]) && (start[i] <= end[j]))
-                        {
-                            ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                            << IVD_UNSUPPORTEDPARAM;
-                            ps_op->s_ivd_init_op_t.u4_error_code |=
-                                            IVD_INIT_DEC_MEM_REC_OVERLAP_ERR;
-                            DEBUG("i: %d, j: %d\n", i, j);
-                            return IV_FAIL;
-                        }
-
-                        if((end[i] >= start[j]) && (end[i] <= end[j]))
-                        {
-                            ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                            << IVD_UNSUPPORTEDPARAM;
-                            ps_op->s_ivd_init_op_t.u4_error_code |=
-                                            IVD_INIT_DEC_MEM_REC_OVERLAP_ERR;
-                            DEBUG("i: %d, j: %d\n", i, j);
-                            return IV_FAIL;
-                        }
-
-                        if((start[i] < start[j]) && (end[i] > end[j]))
-                        {
-                            ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                            << IVD_UNSUPPORTEDPARAM;
-                            ps_op->s_ivd_init_op_t.u4_error_code |=
-                                            IVD_INIT_DEC_MEM_REC_OVERLAP_ERR;
-                            DEBUG("i: %d, j: %d\n", i, j);
-                            return IV_FAIL;
-                        }
-                    }
-
-                }
-            }
-
-            {
-                iv_mem_rec_t mem_rec_ittiam_api[MEM_REC_CNT];
-                ihevcd_cxa_fill_mem_rec_ip_t s_fill_mem_rec_ip;
-                ihevcd_cxa_fill_mem_rec_op_t s_fill_mem_rec_op;
-                IV_API_CALL_STATUS_T e_status;
-
-                WORD32 i;
-                s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.e_cmd =
-                                IV_CMD_FILL_NUM_MEM_REC;
-                s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location =
-                                mem_rec_ittiam_api;
-                s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_wd =
-                                max_wd;
-                s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_ht =
-                                max_ht;
-
-                if(ps_ip->s_ivd_init_ip_t.u4_size
-                                > offsetof(ihevcd_cxa_init_ip_t, i4_level))
-                {
-                    s_fill_mem_rec_ip.i4_level = ps_ip->i4_level;
-                }
-                else
-                {
-                    s_fill_mem_rec_ip.i4_level = IHEVC_LEVEL_31;
-                }
-
-                if(ps_ip->s_ivd_init_ip_t.u4_size
-                                > offsetof(ihevcd_cxa_init_ip_t,
-                                           u4_num_ref_frames))
-                {
-                    s_fill_mem_rec_ip.u4_num_ref_frames =
-                                    ps_ip->u4_num_ref_frames;
-                }
-                else
-                {
-                    s_fill_mem_rec_ip.u4_num_ref_frames = (MAX_REF_CNT + 1);
-                }
-
-                if(ps_ip->s_ivd_init_ip_t.u4_size
-                                > offsetof(ihevcd_cxa_init_ip_t,
-                                           u4_num_reorder_frames))
-                {
-                    s_fill_mem_rec_ip.u4_num_reorder_frames =
-                                    ps_ip->u4_num_reorder_frames;
-                }
-                else
-                {
-                    s_fill_mem_rec_ip.u4_num_reorder_frames = (MAX_REF_CNT + 1);
-                }
-
-                if(ps_ip->s_ivd_init_ip_t.u4_size
-                                > offsetof(ihevcd_cxa_init_ip_t,
-                                           u4_num_extra_disp_buf))
-                {
-                    s_fill_mem_rec_ip.u4_num_extra_disp_buf =
-                                    ps_ip->u4_num_extra_disp_buf;
-                }
-                else
-                {
-                    s_fill_mem_rec_ip.u4_num_extra_disp_buf = 0;
-                }
-
-                if(ps_ip->s_ivd_init_ip_t.u4_size
-                                > offsetof(ihevcd_cxa_init_ip_t,
-                                           u4_share_disp_buf))
-                {
-#ifndef LOGO_EN
-                    s_fill_mem_rec_ip.u4_share_disp_buf =
-                                    ps_ip->u4_share_disp_buf;
-#else
-                    s_fill_mem_rec_ip.u4_share_disp_buf = 0;
-#endif
-                }
-                else
-                {
-                    s_fill_mem_rec_ip.u4_share_disp_buf = 0;
-                }
-
-                s_fill_mem_rec_ip.e_output_format =
-                                ps_ip->s_ivd_init_ip_t.e_output_format;
-
-                if((s_fill_mem_rec_ip.e_output_format != IV_YUV_420P)
-                                && (s_fill_mem_rec_ip.e_output_format
-                                                != IV_YUV_420SP_UV)
-                                && (s_fill_mem_rec_ip.e_output_format
-                                                != IV_YUV_420SP_VU))
-                {
-                    s_fill_mem_rec_ip.u4_share_disp_buf = 0;
-                }
-
-                s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.u4_size =
-                                sizeof(ihevcd_cxa_fill_mem_rec_ip_t);
-                s_fill_mem_rec_op.s_ivd_fill_mem_rec_op_t.u4_size =
-                                sizeof(ihevcd_cxa_fill_mem_rec_op_t);
-
-                for(i = 0; i < MEM_REC_CNT; i++)
-                    mem_rec_ittiam_api[i].u4_size = sizeof(iv_mem_rec_t);
-
-                e_status = ihevcd_cxa_api_function(NULL,
-                                                   (void *)&s_fill_mem_rec_ip,
-                                                   (void *)&s_fill_mem_rec_op);
-                if(IV_FAIL == e_status)
-                {
-                    ps_op->s_ivd_init_op_t.u4_error_code =
-                                    s_fill_mem_rec_op.s_ivd_fill_mem_rec_op_t.u4_error_code;
-                    DEBUG("Fail\n");
-                    return (IV_FAIL);
-                }
-
-                for(i = 0; i < MEM_REC_CNT; i++)
-                {
-#ifdef ARMRVDS
-                    if((UWORD32)(ps_mem_rec[i].pv_base) & (mem_rec_ittiam_api[i].u4_mem_alignment - 1))
-                    {
-                        ps_op->s_ivd_init_op_t.u4_error_code |= 1 << IVD_UNSUPPORTEDPARAM;
-                        ps_op->s_ivd_init_op_t.u4_error_code |= IVD_INIT_DEC_MEM_REC_ALIGNMENT_ERR;
-                        DEBUG("Fail\n");
-                        return IV_FAIL;
-                    }
-#endif
-
-                    if(ps_mem_rec[i].u4_mem_size
-                                    < mem_rec_ittiam_api[i].u4_mem_size)
-                    {
-                        ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                        << IVD_UNSUPPORTEDPARAM;
-                        ps_op->s_ivd_init_op_t.u4_error_code |=
-                                        IVD_INIT_DEC_MEM_REC_INSUFFICIENT_SIZE;
-                        DEBUG("i: %d \n", i);
-                        return IV_FAIL;
-                    }
-                    if(ps_mem_rec[i].u4_mem_alignment
-                                    != mem_rec_ittiam_api[i].u4_mem_alignment)
-                    {
-                        ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                        << IVD_UNSUPPORTEDPARAM;
-                        ps_op->s_ivd_init_op_t.u4_error_code |=
-                                        IVD_INIT_DEC_MEM_REC_ALIGNMENT_ERR;
-                        DEBUG("i: %d \n", i);
-                        return IV_FAIL;
-                    }
-                    if(ps_mem_rec[i].e_mem_type
-                                    != mem_rec_ittiam_api[i].e_mem_type)
-                    {
-                        UWORD32 check = IV_SUCCESS;
-                        UWORD32 diff = mem_rec_ittiam_api[i].e_mem_type
-                                        - ps_mem_rec[i].e_mem_type;
-
-                        if((ps_mem_rec[i].e_mem_type
-                                        <= IV_EXTERNAL_CACHEABLE_SCRATCH_MEM)
-                                        && (mem_rec_ittiam_api[i].e_mem_type
-                                                        >= IV_INTERNAL_NONCACHEABLE_PERSISTENT_MEM))
-                        {
-                            check = IV_FAIL;
-                        }
-                        if(3 != (mem_rec_ittiam_api[i].e_mem_type % 4))
-                        {
-                            /*
-                             * It is not IV_EXTERNAL_NONCACHEABLE_PERSISTENT_MEM or IV_EXTERNAL_CACHEABLE_PERSISTENT_MEM
-                             */
-                            if((diff < 1) || (diff > 3))
-                            {
-                                // Difference between 1 and 3 is okay for all cases other than the two filtered
-                                // with the MOD condition above
-                                check = IV_FAIL;
-                            }
-                        }
-                        else
-                        {
-                            if(diff == 1)
-                            {
-                                /*
-                                 * This particular case is when codec asked for External Persistent, but got
-                                 * Internal Scratch.
-                                 */
-                                check = IV_FAIL;
-                            }
-                            if((diff != 2) && (diff != 3))
-                            {
-                                check = IV_FAIL;
-                            }
-                        }
-                        if(check == IV_FAIL)
-                        {
-                            ps_op->s_ivd_init_op_t.u4_error_code |= 1
-                                            << IVD_UNSUPPORTEDPARAM;
-                            ps_op->s_ivd_init_op_t.u4_error_code |=
-                                            IVD_INIT_DEC_MEM_REC_INCORRECT_TYPE;
-                            DEBUG("i: %d \n", i);
-                            return IV_FAIL;
-                        }
-                    }
-                }
-            }
-
         }
             break;
 
@@ -939,49 +443,35 @@
         }
             break;
 
-        case IV_CMD_RETRIEVE_MEMREC:
+        case IVD_CMD_DELETE:
         {
-            ihevcd_cxa_retrieve_mem_rec_ip_t *ps_ip =
-                            (ihevcd_cxa_retrieve_mem_rec_ip_t *)pv_api_ip;
-            ihevcd_cxa_retrieve_mem_rec_op_t *ps_op =
-                            (ihevcd_cxa_retrieve_mem_rec_op_t *)pv_api_op;
-            iv_mem_rec_t *ps_mem_rec;
+            ihevcd_cxa_delete_ip_t *ps_ip =
+                            (ihevcd_cxa_delete_ip_t *)pv_api_ip;
+            ihevcd_cxa_delete_op_t *ps_op =
+                            (ihevcd_cxa_delete_op_t *)pv_api_op;
 
-            ps_op->s_ivd_retrieve_mem_rec_op_t.u4_error_code = 0;
+            ps_op->s_ivd_delete_op_t.u4_error_code = 0;
 
-            if(ps_ip->s_ivd_retrieve_mem_rec_ip_t.u4_size
-                            != sizeof(ihevcd_cxa_retrieve_mem_rec_ip_t))
+            if(ps_ip->s_ivd_delete_ip_t.u4_size
+                            != sizeof(ihevcd_cxa_delete_ip_t))
             {
-                ps_op->s_ivd_retrieve_mem_rec_op_t.u4_error_code |= 1
+                ps_op->s_ivd_delete_op_t.u4_error_code |= 1
                                 << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_retrieve_mem_rec_op_t.u4_error_code |=
+                ps_op->s_ivd_delete_op_t.u4_error_code |=
                                 IVD_IP_API_STRUCT_SIZE_INCORRECT;
                 return (IV_FAIL);
             }
 
-            if(ps_op->s_ivd_retrieve_mem_rec_op_t.u4_size
-                            != sizeof(ihevcd_cxa_retrieve_mem_rec_op_t))
+            if(ps_op->s_ivd_delete_op_t.u4_size
+                            != sizeof(ihevcd_cxa_delete_op_t))
             {
-                ps_op->s_ivd_retrieve_mem_rec_op_t.u4_error_code |= 1
+                ps_op->s_ivd_delete_op_t.u4_error_code |= 1
                                 << IVD_UNSUPPORTEDPARAM;
-                ps_op->s_ivd_retrieve_mem_rec_op_t.u4_error_code |=
+                ps_op->s_ivd_delete_op_t.u4_error_code |=
                                 IVD_OP_API_STRUCT_SIZE_INCORRECT;
                 return (IV_FAIL);
             }
 
-            ps_mem_rec = ps_ip->s_ivd_retrieve_mem_rec_ip_t.pv_mem_rec_location;
-            /* check memrecords sizes are correct */
-            for(i = 0; i < MEM_REC_CNT; i++)
-            {
-                if(ps_mem_rec[i].u4_size != sizeof(iv_mem_rec_t))
-                {
-                    ps_op->s_ivd_retrieve_mem_rec_op_t.u4_error_code |= 1
-                                    << IVD_UNSUPPORTEDPARAM;
-                    ps_op->s_ivd_retrieve_mem_rec_op_t.u4_error_code |=
-                                    IVD_MEM_REC_STRUCT_SIZE_INCORRECT;
-                    return IV_FAIL;
-                }
-            }
         }
             break;
 
@@ -1480,7 +970,7 @@
 *******************************************************************************
 *
 * @brief
-*  Initialize the context. This will be called by  init_mem_rec and during
+*  Initialize the context. This will be called by  create and during
 * reset
 *
 * @par Description:
@@ -1501,12 +991,15 @@
     WORD32 status = IV_SUCCESS;
     WORD32 i;
 
+    /* Free any dynamic buffers that are allocated */
+    ihevcd_free_dynamic_bufs(ps_codec);
 
+    ps_codec->u4_allocate_dynamic_done = 0;
     ps_codec->i4_num_disp_bufs = 1;
     ps_codec->i4_flush_mode = 0;
 
-    ps_codec->i4_ht = ps_codec->i4_disp_ht = ps_codec->i4_max_ht;
-    ps_codec->i4_wd = ps_codec->i4_disp_wd = ps_codec->i4_max_wd;
+    ps_codec->i4_ht = ps_codec->i4_disp_ht = 0;
+    ps_codec->i4_wd = ps_codec->i4_disp_wd = 0;
     ps_codec->i4_strd = 0;
     ps_codec->i4_disp_strd = 0;
     ps_codec->i4_num_cores = 1;
@@ -1572,23 +1065,6 @@
     }
 
     ihevcd_set_default_params(ps_codec);
-    ps_codec->pv_proc_jobq = ihevcd_jobq_init(ps_codec->pv_proc_jobq_buf, ps_codec->i4_proc_jobq_buf_size);
-    RETURN_IF((ps_codec->pv_proc_jobq == NULL), IV_FAIL);
-
-    /* Update the jobq context to all the threads */
-    ps_codec->s_parse.pv_proc_jobq = ps_codec->pv_proc_jobq;
-    for(i = 0; i < MAX_PROCESS_THREADS; i++)
-    {
-        ps_codec->as_process[i].pv_proc_jobq = ps_codec->pv_proc_jobq;
-        ps_codec->as_process[i].i4_id = i;
-        ps_codec->as_process[i].ps_codec = ps_codec;
-
-        /* Set the following to zero assuming it is a single core solution
-         * When threads are launched these will be set appropriately
-         */
-        ps_codec->as_process[i].i4_check_parse_status = 0;
-        ps_codec->as_process[i].i4_check_proc_status = 0;
-    }
     /* Initialize MV Bank buffer manager */
     ihevc_buf_mgr_init((buf_mgr_t *)ps_codec->pv_mv_buf_mgr);
 
@@ -1623,11 +1099,10 @@
 *******************************************************************************
 *
 * @brief
-*  Gets number of memory records required by the codec
+*  Allocate static memory for the codec
 *
 * @par Description:
-*  Gets codec mem record requirements and adds concealment  modules
-* requirements
+*  Allocates static memory for the codec
 *
 * @param[in] pv_api_ip
 *  Pointer to input argument structure
@@ -1642,1104 +1117,177 @@
 *
 *******************************************************************************
 */
-WORD32 ihevcd_get_num_rec(void *pv_api_ip, void *pv_api_op)
+WORD32 ihevcd_allocate_static_bufs(iv_obj_t **pps_codec_obj,
+                                   ihevcd_cxa_create_ip_t *ps_create_ip,
+                                   ihevcd_cxa_create_op_t *ps_create_op)
 {
-
-    iv_num_mem_rec_op_t *ps_mem_q_op;
-
-    UNUSED(pv_api_ip);
-    ps_mem_q_op = (iv_num_mem_rec_op_t *)pv_api_op;
-    ps_mem_q_op->u4_num_mem_rec = MEM_REC_CNT;
-    DEBUG("Get num mem records without concealment %d\n",
-                    ps_mem_q_op->u4_num_mem_rec);
-#ifdef APPLY_CONCEALMENT
-    {
-        IV_API_CALL_STATUS_T status;
-        icncl_num_mem_rec_ip_t cncl_mem_ip;
-        icncl_num_mem_rec_op_t cncl_mem_op;
-
-        cncl_mem_ip.s_ivd_num_rec_ip_t.e_cmd = IV_CMD_GET_NUM_MEM_REC;
-        cncl_mem_ip.s_ivd_num_rec_ip_t.u4_size = sizeof(icncl_num_mem_rec_ip_t);
-
-        status = icncl_api_function(NULL, (void *)&cncl_mem_ip, (void *)&cncl_mem_op);
-
-        if(status == IV_SUCCESS)
-        {
-            /* Add the concealment library's memory requirements */
-            ps_mem_q_op->u4_num_mem_rec += cncl_mem_op.s_ivd_num_mem_rec_op_t.u4_num_mem_rec;
-            DEBUG("Get num mem records %d\n", ps_mem_q_op->u4_num_mem_rec);
-            return status; /* Nothing else to do, return */
-        }
-        else
-        {
-            /*
-             * Something went wrong with the concealment library call.
-             */
-            DEBUG("ERROR: Get num mem records %d\n", ps_mem_q_op->u4_num_mem_rec);
-            return status;
-        }
-
-    }
-#endif //APPLY_CONCEALMENT
-
-
-    return IV_SUCCESS;
-
-}
-
-/**
-*******************************************************************************
-*
-* @brief
-*  Fills memory requirements of the codec
-*
-* @par Description:
-*  Gets codec mem record requirements and adds concealment  modules
-* requirements
-*
-* @param[in] pv_api_ip
-*  Pointer to input argument structure
-*
-* @param[out] pv_api_op
-*  Pointer to output argument structure
-*
-* @returns  Status
-*
-* @remarks
-*
-*
-*******************************************************************************
-*/
-WORD32 ihevcd_fill_num_mem_rec(void *pv_api_ip, void *pv_api_op)
-{
-
-    ihevcd_cxa_fill_mem_rec_ip_t *ps_mem_q_ip;
-    ihevcd_cxa_fill_mem_rec_op_t *ps_mem_q_op;
-    WORD32 level;
-    WORD32 num_reorder_frames;
-    WORD32 num_ref_frames;
-    WORD32 num_extra_disp_bufs;
-    WORD32 max_dpb_size;
-
-    iv_mem_rec_t *ps_mem_rec;
-    iv_mem_rec_t *ps_mem_rec_base;
-    WORD32 no_of_mem_rec_filled;
-    WORD32 chroma_format, share_disp_buf;
-    WORD32 max_ctb_cnt;
-    WORD32 max_wd_luma, max_wd_chroma;
-    WORD32 max_ht_luma, max_ht_chroma;
-    WORD32 max_tile_cols, max_tile_rows;
-    WORD32 max_ctb_rows, max_ctb_cols;
-    WORD32 max_num_cu_cols;
+    WORD32 size;
+    void *pv_buf;
+    UWORD8 *pu1_buf;
     WORD32 i;
-    WORD32 max_num_4x4_cols;
+    codec_t *ps_codec;
     IV_API_CALL_STATUS_T status = IV_SUCCESS;
-    no_of_mem_rec_filled = 0;
-
-    //TODO: Remove as and when the following are used
-    UNUSED(num_extra_disp_bufs);
-    UNUSED(no_of_mem_rec_filled);
-    UNUSED(max_wd_chroma);
-    UNUSED(max_ht_chroma);
-
-    ps_mem_q_ip = (ihevcd_cxa_fill_mem_rec_ip_t *)pv_api_ip;
-    ps_mem_q_op = (ihevcd_cxa_fill_mem_rec_op_t *)pv_api_op;
-
-    if(ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_fill_mem_rec_ip_t, i4_level))
-    {
-        level = ps_mem_q_ip->i4_level;
-        /* Spec requires level should be multiplied by 30
-         * API has values where level is multiplied by 10. This keeps it consistent with H264
-         * Because of the above differences, level is multiplied by 3 here.
-         */
-        level *= 3;
-    }
-    else
-    {
-        level = MAX_LEVEL;
-    }
-
-    if(ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_fill_mem_rec_ip_t,
-                               u4_num_reorder_frames))
-    {
-        num_reorder_frames = ps_mem_q_ip->u4_num_reorder_frames;
-    }
-    else
-    {
-        num_reorder_frames = MAX_REF_CNT;
-    }
-
-    if(ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_fill_mem_rec_ip_t, u4_num_ref_frames))
-    {
-        num_ref_frames = ps_mem_q_ip->u4_num_ref_frames;
-    }
-    else
-    {
-        num_ref_frames = MAX_REF_CNT;
-    }
-
-    if(ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_fill_mem_rec_ip_t,
-                               u4_num_extra_disp_buf))
-    {
-        num_extra_disp_bufs = ps_mem_q_ip->u4_num_extra_disp_buf;
-    }
-    else
-    {
-        num_extra_disp_bufs = 0;
-    }
-
-    if(ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_fill_mem_rec_ip_t, u4_share_disp_buf))
-    {
-#ifndef LOGO_EN
-        share_disp_buf = ps_mem_q_ip->u4_share_disp_buf;
-#else
-        share_disp_buf = 0;
-#endif
-    }
-    else
-    {
-        share_disp_buf = 0;
-    }
-
-    if(ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_fill_mem_rec_ip_t, e_output_format))
-    {
-        chroma_format = ps_mem_q_ip->e_output_format;
-    }
-    else
-    {
-        chroma_format = -1;
-    }
-
-    /* Shared disp buffer mode is supported only for 420SP formats */
-    if((chroma_format != IV_YUV_420P) &&
-       (chroma_format != IV_YUV_420SP_UV) &&
-       (chroma_format != IV_YUV_420SP_VU))
-    {
-        share_disp_buf = 0;
-    }
-
-    {
-
-        max_ht_luma = ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.u4_max_frm_ht;
-        max_wd_luma = ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.u4_max_frm_wd;
-
-        max_ht_luma = ALIGN64(max_ht_luma);
-        max_wd_luma = ALIGN64(max_wd_luma);
-
-
-
-        max_tile_cols = (max_wd_luma + MIN_TILE_WD - 1) / MIN_TILE_WD;
-        max_tile_rows = (max_ht_luma + MIN_TILE_HT - 1) / MIN_TILE_HT;
-        max_ctb_rows  = max_ht_luma / MIN_CTB_SIZE;
-        max_ctb_cols  = max_wd_luma / MIN_CTB_SIZE;
-        max_ctb_cnt   = max_ctb_rows * max_ctb_cols;
-        max_num_cu_cols = max_wd_luma / MIN_CU_SIZE;
-        max_num_4x4_cols = max_wd_luma / 4;
-    }
-    /*
-     * If level is lesser than 31 and the resolution required is higher,
-     * then make the level at least 31.
-     */
-    /*    if (num_mbs > MAX_NUM_MBS_3_0 && level < MAX_LEVEL)
-     {
-     level           = MAX_LEVEL;
-     }
-     */
-    if((level < MIN_LEVEL) || (level > MAX_LEVEL))
-    {
-        ps_mem_q_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                        IHEVCD_LEVEL_UNSUPPORTED;
-        level = MAX_LEVEL;
-    }
-    if(num_ref_frames > MAX_REF_CNT)
-    {
-        ps_mem_q_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                        IHEVCD_NUM_REF_UNSUPPORTED;
-        num_ref_frames = MAX_REF_CNT;
-    }
-
-    if(num_reorder_frames > MAX_REF_CNT)
-    {
-        ps_mem_q_op->s_ivd_fill_mem_rec_op_t.u4_error_code |=
-                        IHEVCD_NUM_REORDER_UNSUPPORTED;
-        num_reorder_frames = MAX_REF_CNT;
-    }
-
-    max_dpb_size = ihevcd_get_dpb_size(level, max_wd_luma * max_ht_luma);
-    ps_mem_rec_base = ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location;
-
-    /* Set all memory reconds as persistent and alignment as 128
-     * by default
-     */
-    ps_mem_rec = ps_mem_rec_base;
-    for(i = 0; i < MEM_REC_CNT; i++)
-    {
-        ps_mem_rec->u4_mem_alignment = 128;
-        ps_mem_rec->e_mem_type = IV_EXTERNAL_CACHEABLE_PERSISTENT_MEM;
-        ps_mem_rec++;
-    }
+    void *(*pf_aligned_alloc)(void *pv_mem_ctxt, WORD32 alignment, WORD32 size);
+    void (*pf_aligned_free)(void *pv_mem_ctxt, void *pv_buf);
+    void *pv_mem_ctxt;
 
     /* Request memory for HEVCD object */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_IV_OBJ];
-    ps_mem_rec->u4_mem_size = sizeof(iv_obj_t);
+    ps_create_op->s_ivd_create_op_t.pv_handle = NULL;
 
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_IV_OBJ,
-                    ps_mem_rec->u4_mem_size);
+    pf_aligned_alloc = ps_create_ip->s_ivd_create_ip_t.pf_aligned_alloc;
+    pf_aligned_free = ps_create_ip->s_ivd_create_ip_t.pf_aligned_free;
+    pv_mem_ctxt  = ps_create_ip->s_ivd_create_ip_t.pv_mem_ctxt;
 
-    /* Request memory for HEVC Codec context */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_CODEC];
-    ps_mem_rec->u4_mem_size = sizeof(codec_t);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_CODEC,
-                    ps_mem_rec->u4_mem_size);
 
-    /* Request memory for buffer which holds bitstream after emulation prevention */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_BITSBUF];
-    ps_mem_rec->u4_mem_size = MAX((max_wd_luma * max_ht_luma), MIN_BITSBUF_SIZE);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_BITSBUF,
-                    ps_mem_rec->u4_mem_size);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, sizeof(iv_obj_t));
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    *pps_codec_obj = (iv_obj_t *)pv_buf;
+    ps_create_op->s_ivd_create_op_t.pv_handle = *pps_codec_obj;
 
-    /* Request memory for buffer which holds TU structures and coeff data for
-     * a set of CTBs in the current picture */
-    /*TODO Currently the buffer is allocated at a frame level. Reduce this to
-     * allocate for s set of CTBs and add appropriate synchronization logic to
-     * ensure that this is data is not overwritten before consumption
-     */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_TU_DATA];
-    ps_mem_rec->u4_mem_size = ihevcd_get_tu_data_size(max_wd_luma * max_ht_luma);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_TU_DATA,
-                    ps_mem_rec->u4_mem_size);
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_MVBANK];
-
-    ps_mem_rec->u4_mem_size = sizeof(buf_mgr_t);
-
-    /* Size for holding mv_buf_t for each MV Bank */
-    /* Note this allocation is done for BUF_MGR_MAX_CNT instead of
-     * max_dpb_size or MAX_DPB_SIZE for following reasons
-     * max_dpb_size will be based on max_wd and max_ht
-     * For higher max_wd and max_ht this number will be smaller than MAX_DPB_SIZE
-     * But during actual initialization number of buffers allocated can be more
-     *
-     * One extra MV Bank is needed to hold current pics MV bank.
-     * Since this is only a structure allocation and not actual buffer allocation,
-     * it is allocated for (MAX_DPB_SIZE + 1) entries
-     */
-    ps_mem_rec->u4_mem_size += (MAX_DPB_SIZE + 1) * sizeof(mv_buf_t);
-
-    {
-        /* Allocate for pu_map, pu_t and pic_pu_idx for each MV bank */
-        /* Note: Number of luma samples is not max_wd * max_ht here, instead it is
-         * set to maximum number of luma samples allowed at the given level.
-         * This is done to ensure that any stream with width and height lesser
-         * than max_wd and max_ht is supported. Number of buffers required can be greater
-         * for lower width and heights at a given level and this increased number of buffers
-         * might require more memory than what max_wd and max_ht buffer would have required
-         * Also note one extra buffer is allocted to store current pictures MV bank
-         * In case of asynchronous parsing and processing, number of buffers should increase here
-         * based on when parsing and processing threads are synchronized
-         */
-        WORD32 lvl_idx = ihevcd_get_lvl_idx(level);
-        WORD32 max_luma_samples = gai4_ihevc_max_luma_pic_size[lvl_idx];
-        ps_mem_rec->u4_mem_size += (max_dpb_size + 1) *
-                        ihevcd_get_pic_mv_bank_size(max_luma_samples);
-        DEBUG("\nMemory record Id %d = %d \n", MEM_REC_MVBANK,
-                        ps_mem_rec->u4_mem_size);
-    }
-    // TODO GPU : Have to creat ping-pong view for VPS,SPS,PPS.
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_VPS];
-    ps_mem_rec->u4_mem_size = MAX_VPS_CNT * sizeof(vps_t);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_VPS,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SPS];
-    ps_mem_rec->u4_mem_size = MAX_SPS_CNT * sizeof(sps_t);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_SPS,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PPS];
-    ps_mem_rec->u4_mem_size = MAX_PPS_CNT * sizeof(pps_t);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_PPS,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SLICE_HDR];
-    ps_mem_rec->u4_mem_size = MAX_SLICE_HDR_CNT * sizeof(slice_header_t);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_SLICE_HDR,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_TILE];
-    {
-        WORD32 tile_size;
-
-        tile_size  = max_tile_cols * max_tile_rows;
-        tile_size  *= sizeof(tile_t);
-
-
-        ps_mem_rec->u4_mem_size = MAX_PPS_CNT * tile_size;
-    }
-
-
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_TILE,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_ENTRY_OFST];
-    {
-        WORD32 num_entry_points;
-
-        /* One entry point per tile */
-        num_entry_points  = max_tile_cols * max_tile_rows;
-
-        /* One entry point per row of CTBs */
-        /*********************************************************************/
-        /* Only tiles or entropy sync is enabled at a time in main           */
-        /* profile, but since memory required does not increase too much,    */
-        /* this allocation is done to handle both cases                      */
-        /*********************************************************************/
-        num_entry_points  += max_ctb_rows;
-
-
-        ps_mem_rec->u4_mem_size = sizeof(WORD32) * num_entry_points;
-    }
-
-
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_ENTRY_OFST,
-                    ps_mem_rec->u4_mem_size);
-
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SCALING_MAT];
-    {
-        WORD32 scaling_mat_size;
-
-        SCALING_MAT_SIZE(scaling_mat_size)
-        ps_mem_rec->u4_mem_size = (MAX_SPS_CNT + MAX_PPS_CNT) * scaling_mat_size * sizeof(WORD16);
-    }
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_SCALING_MAT,
-                    ps_mem_rec->u4_mem_size);
-
-    /* Holds one row skip_flag at 8x8 level used during parsing */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PARSE_SKIP_FLAG];
-
-    /* 1 bit per 8x8 */
-    ps_mem_rec->u4_mem_size = max_num_cu_cols / 8;
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_PARSE_SKIP_FLAG,
-                  ps_mem_rec->u4_mem_size);
-
-    /* Holds one row skip_flag at 8x8 level used during parsing */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PARSE_CT_DEPTH];
-
-    /* 2 bits per 8x8 */
-    ps_mem_rec->u4_mem_size = max_num_cu_cols / 4;
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_PARSE_CT_DEPTH,
-                  ps_mem_rec->u4_mem_size);
-
-    /* Holds one row skip_flag at 8x8 level used during parsing */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PARSE_INTRA_PRED_MODE];
-
-    /* 8 bits per 4x4 */
-    /* 16 bytes each for top and left 64 pixels and 16 bytes for default mode */
-    ps_mem_rec->u4_mem_size = 3 * 16 * sizeof(UWORD8);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_PARSE_INTRA_PRED_MODE,
-                  ps_mem_rec->u4_mem_size);
-
-    /* Holds one intra mode at 8x8 level for entire picture */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_INTRA_FLAG];
-
-    /* 1 bit per 8x8 */
-    ps_mem_rec->u4_mem_size = (max_wd_luma / MIN_CU_SIZE) * (max_ht_luma / MIN_CU_SIZE) / 8;
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_INTRA_FLAG,
-                  ps_mem_rec->u4_mem_size);
-
-    /* Holds one transquant bypass flag at 8x8 level for entire picture */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_TRANSQUANT_BYPASS_FLAG];
-
-    /* 1 bit per 8x8 */
-    /* Extra row and column are allocated for easy processing of top and left blocks while loop filtering */
-    ps_mem_rec->u4_mem_size = ((max_wd_luma + 64) / MIN_CU_SIZE) * ((max_ht_luma + 64) / MIN_CU_SIZE) / 8;
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_TRANSQUANT_BYPASS_FLAG,
-                  ps_mem_rec->u4_mem_size);
-
-    /* Request memory to hold thread handles for each processing thread */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_THREAD_HANDLE];
-    ps_mem_rec->u4_mem_size = MAX_PROCESS_THREADS * ithread_get_handle_size();
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_THREAD_HANDLE,
-                    ps_mem_rec->u4_mem_size);
-
-
-    {
-        WORD32 job_queue_size;
-        WORD32 num_jobs;
-        ps_mem_rec = &ps_mem_rec_base[MEM_REC_PROC_JOBQ];
-
-
-        /* One job per row of CTBs */
-        num_jobs  = max_ctb_rows;
-
-        /* One each tile a row of CTBs, num_jobs has to incremented */
-        num_jobs  *= max_tile_cols;
-
-        /* One format convert/frame copy job per row of CTBs for non-shared mode*/
-        num_jobs  += max_ctb_rows;
-
-
-        job_queue_size = ihevcd_jobq_ctxt_size();
-        job_queue_size += num_jobs * sizeof(proc_job_t);
-        ps_mem_rec->u4_mem_size = job_queue_size;
-        DEBUG("\nMemory record Id %d = %d \n", MEM_REC_PROC_JOBQ,
-                        ps_mem_rec->u4_mem_size);
-    }
-
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PARSE_MAP];
-    ps_mem_rec->u4_mem_size = max_ctb_cnt;
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_PARSE_MAP,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PROC_MAP];
-    ps_mem_rec->u4_mem_size = max_ctb_cnt;
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_PROC_MAP,
-                    ps_mem_rec->u4_mem_size);
-
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_DISP_MGR];
-
-    /* size for holding display manager context */
-    ps_mem_rec->u4_mem_size = sizeof(buf_mgr_t);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_DISP_MGR,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_DPB_MGR];
-
-    /* size for holding dpb manager context */
-    ps_mem_rec->u4_mem_size = sizeof(dpb_mgr_t);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_DPB_MGR,
-                    ps_mem_rec->u4_mem_size);
-
-    /** Holds top and left neighbor's pu idx into picture level pu array */
-    /* Only one top row is enough but left has to be replicated for each process context */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PIC_PU_IDX_NEIGHBOR];
-
-    ps_mem_rec->u4_mem_size = (max_num_4x4_cols  /* left */ + MAX_PROCESS_THREADS * (MAX_CTB_SIZE / 4)/* top */ + 1/* top right */) * sizeof(WORD32);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_PIC_PU_IDX_NEIGHBOR,
-                    ps_mem_rec->u4_mem_size);
-
-
-
-    /* TO hold scratch buffers needed for each process context */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PROC_SCRATCH];
-    {
-        WORD32 size = 0;
-        WORD32 inter_pred_tmp_buf_size;
-        WORD32 ntaps_luma;
-        WORD32 pu_map_size;
-        WORD32 sao_size = 0;
-        ntaps_luma = 8;
-
-        /* Max inter pred size (number of bytes) */
-        inter_pred_tmp_buf_size = sizeof(WORD16) * (MAX_CTB_SIZE + ntaps_luma) * MAX_CTB_SIZE;
-        inter_pred_tmp_buf_size = ALIGN64(inter_pred_tmp_buf_size);
-
-
-        /* To hold pu_index w.r.t. frame level pu_t array for a CTB at 4x4 level*/
-        /* 16 x 16 4x4 in a CTB of size 64 x 64 and two extra needed for holding
-         * neighbors
-         */
-        pu_map_size = sizeof(WORD32) * (18 * 18);
-
-        pu_map_size = ALIGN64(pu_map_size);
-        size += pu_map_size;
-
-        /* To hold inter pred temporary buffers */
-        size += 2 * inter_pred_tmp_buf_size;
-
-
-        /* Allocate for each process context */
-        size *= MAX_PROCESS_THREADS;
-
-
-        /* To hold SAO left buffer for luma */
-        sao_size += sizeof(UWORD8) * (MAX(max_ht_luma, max_wd_luma));
-
-        /* To hold SAO left buffer for chroma */
-        sao_size += sizeof(UWORD8) * (MAX(max_ht_luma, max_wd_luma));
-
-        /* To hold SAO top buffer for luma */
-        sao_size += sizeof(UWORD8) * max_wd_luma;
-
-        /* To hold SAO top buffer for chroma */
-        sao_size += sizeof(UWORD8) * max_wd_luma;
-
-        /* To hold SAO top left luma pixel value for last output ctb in a row*/
-        sao_size += sizeof(UWORD8) * max_ctb_rows;
-
-        /* To hold SAO top left chroma pixel value last output ctb in a row*/
-        sao_size += sizeof(UWORD8) * max_ctb_rows * 2;
-
-        /* To hold SAO top left pixel luma for current ctb - column array*/
-        sao_size += sizeof(UWORD8) * max_ctb_rows;
-
-        /* To hold SAO top left pixel chroma for current ctb-column array*/
-        sao_size += sizeof(UWORD8) * max_ctb_rows * 2;
-
-        /* To hold SAO top right pixel luma pixel value last output ctb in a row*/
-        sao_size += sizeof(UWORD8) * max_ctb_cols;
-
-        /* To hold SAO top right pixel chroma pixel value last output ctb in a row*/
-        sao_size += sizeof(UWORD8) * max_ctb_cols * 2;
-
-        /*To hold SAO botton bottom left pixels for luma*/
-        sao_size += sizeof(UWORD8) * max_ctb_rows;
-
-        /*To hold SAO botton bottom left pixels for luma*/
-        sao_size += sizeof(UWORD8) * max_ctb_rows * 2;
-        sao_size = ALIGN64(sao_size);
-        size += sao_size;
-        ps_mem_rec->u4_mem_size = size;
-    }
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_PROC_SCRATCH,
-                    ps_mem_rec->u4_mem_size);
-
-    /* TO hold scratch buffers needed for each SAO context */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SAO_SCRATCH];
-    {
-        WORD32 size = 0;
-
-        size = 4 * MAX_CTB_SIZE * MAX_CTB_SIZE;
-
-        /* 2 temporary buffers*/
-        size *= 2;
-
-        size *= MAX_PROCESS_THREADS;
-
-        ps_mem_rec->u4_mem_size = size;
-    }
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_SAO_SCRATCH,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_BS_QP];
-    {
-        WORD32 size = 0;
-        WORD32 vert_bs_size, horz_bs_size;
-        WORD32 qp_const_flag_size;
-        WORD32 qp_size, num_8x8;
-
-        /* Max Number of vertical edges */
-        vert_bs_size = max_wd_luma / 8 + 2 * MAX_CTB_SIZE / 8;
-
-        /* Max Number of horizontal edges - extra MAX_CTB_SIZE / 8 to handle the last 4 rows separately(shifted CTB processing) */
-        vert_bs_size *= (max_ht_luma + MAX_CTB_SIZE) / MIN_TU_SIZE;
-
-        /* Number of bytes */
-        vert_bs_size /= 8;
-
-        /* Two bits per edge */
-        vert_bs_size *= 2;
-
-        /* Max Number of horizontal edges */
-        horz_bs_size = max_ht_luma / 8 + MAX_CTB_SIZE / 8;
-
-        /* Max Number of vertical edges - extra MAX_CTB_SIZE / 8 to handle the last 4 columns separately(shifted CTB processing) */
-        horz_bs_size *= (max_wd_luma + MAX_CTB_SIZE) / MIN_TU_SIZE;
-
-        /* Number of bytes */
-        horz_bs_size /= 8;
-
-        /* Two bits per edge */
-        horz_bs_size *= 2;
-
-        /* Max CTBs in a row */
-        qp_const_flag_size = max_wd_luma / MIN_CTB_SIZE + 1 /* The last ctb row deblk is done in last ctb + 1 row.*/;
-
-        /* Max CTBs in a column */
-        qp_const_flag_size *= max_ht_luma / MIN_CTB_SIZE;
-
-        /* Number of bytes */
-        qp_const_flag_size = (qp_const_flag_size + 7) >> 3;
-
-        /* QP changes at CU level - So store at 8x8 level */
-        num_8x8 = (max_ht_luma * max_wd_luma) / (MIN_CU_SIZE * MIN_CU_SIZE);
-        qp_size = num_8x8;
-
-        /* To hold vertical boundary strength */
-        size += vert_bs_size;
-
-        /* To hold horizontal boundary strength */
-        size += horz_bs_size;
-
-        /* To hold QP */
-        size += qp_size;
-
-        /* To hold QP const in CTB flags */
-        size += qp_const_flag_size;
-
-        ps_mem_rec->u4_mem_size = size;
-    }
-
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_BS_QP,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_TILE_IDX];
-    {
-        WORD32 size = 0;
-        /* Max CTBs in a row */
-        size  = max_wd_luma / MIN_CTB_SIZE + 2 /* Top row and bottom row extra. This ensures accessing left,top in first row
-                                                  and right in last row will not result in invalid access*/;
-        /* Max CTBs in a column */
-        size *= max_ht_luma / MIN_CTB_SIZE;
-
-        size *= sizeof(UWORD16);
-        ps_mem_rec->u4_mem_size = size;
-    }
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_TILE_IDX,
-                    ps_mem_rec->u4_mem_size);
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SAO];
-    {
-        UWORD32 size;
-
-        /* 4 bytes per color component per CTB */
-        size = 3 * 4;
-
-        /* MAX number of CTBs in a row */
-        size *= max_wd_luma / MIN_CTB_SIZE;
-
-        /* MAX number of CTBs in a column */
-        size *= max_ht_luma / MIN_CTB_SIZE;
-        ps_mem_rec->u4_mem_size = size;
-    }
-
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_SAO,
-                    ps_mem_rec->u4_mem_size);
-
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_REF_PIC];
-
-    /* size for holding buffer manager context */
-    ps_mem_rec->u4_mem_size = sizeof(buf_mgr_t);
-
-    /* Size for holding pic_buf_t for each reference picture */
-    /* Note this allocation is done for BUF_MGR_MAX_CNT instead of
-     * max_dpb_size or MAX_DPB_SIZE for following reasons
-     * max_dpb_size will be based on max_wd and max_ht
-     * For higher max_wd and max_ht this number will be smaller than MAX_DPB_SIZE
-     * But during actual initialization number of buffers allocated can be more
-     *
-     * Also to handle display depth application can allocate more than what
-     * codec asks for in case of non-shared mode
-     * Since this is only a structure allocation and not actual buffer allocation,
-     * it is allocated for BUF_MGR_MAX_CNT entries
-     */
-    ps_mem_rec->u4_mem_size += BUF_MGR_MAX_CNT * sizeof(pic_buf_t);
-
-    /* In case of non-shared mode allocate for reference picture buffers */
-    /* In case of shared and 420p output, allocate for chroma samples */
-    if((0 == share_disp_buf) || (chroma_format == IV_YUV_420P))
-    {
-        UWORD32 init_num_bufs;
-        UWORD32 init_extra_bufs;
-        WORD32 chroma_only;
-
-        chroma_only = 0;
-        init_extra_bufs = 0;
-        init_num_bufs = num_reorder_frames + num_ref_frames + 1;
-
-        /* In case of shared display buffers and chroma format 420P
-         * Allocate for chroma in reference buffers, luma buffer will be display buffer
-         */
-
-        if((1 == share_disp_buf) && (chroma_format == IV_YUV_420P))
-        {
-            chroma_only = 1;
-            init_extra_bufs = num_extra_disp_bufs;
-        }
-
-        /* Note: Number of luma samples is not max_wd * max_ht here, instead it is
-         * set to maximum number of luma samples allowed at the given level.
-         * This is done to ensure that any stream with width and height lesser
-         * than max_wd and max_ht is supported. Number of buffers required can be greater
-         * for lower width and heights at a given level and this increased number of buffers
-         * might require more memory than what max_wd and max_ht buffer would have required
-         * Number of buffers is doubled in order to return one frame at a time instead of sending
-         * multiple outputs during dpb full case.
-         * Also note one extra buffer is allocted to store current picture
-         * In case of asynchronous parsing and processing, number of buffers should increase here
-         * based on when parsing and processing threads are synchronized
-         */
-        ps_mem_rec->u4_mem_size +=
-                        ihevcd_get_total_pic_buf_size(max_wd_luma * max_ht_luma, level,  PAD_WD,  PAD_HT,
-                                                      init_num_bufs, init_extra_bufs, chroma_only);
-    }
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_REF_PIC,
-                    ps_mem_rec->u4_mem_size);
-
-    /* Request memory to hold mem records to be returned during retrieve call */
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_BACKUP];
-    ps_mem_rec->u4_mem_size = MEM_REC_CNT * sizeof(iv_mem_rec_t);
-    DEBUG("\nMemory record Id %d = %d \n", MEM_REC_BACKUP,
-                    ps_mem_rec->u4_mem_size);
-
-    /* Each memtab size is aligned to next multiple of 128 bytes */
-    /* This is to ensure all the memtabs start at different cache lines */
-    ps_mem_rec = ps_mem_rec_base;
-    for(i = 0; i < MEM_REC_CNT; i++)
-    {
-        ps_mem_rec->u4_mem_size = ALIGN128(ps_mem_rec->u4_mem_size);
-        ps_mem_rec++;
-    }
-    ps_mem_q_op->s_ivd_fill_mem_rec_op_t.u4_num_mem_rec_filled = MEM_REC_CNT;
-#ifdef APPLY_CONCEALMENT
-    {
-        IV_API_CALL_STATUS_T status;
-        icncl_fill_mem_rec_ip_t cncl_fill_ip;
-        icncl_fill_mem_rec_op_t cncl_fill_op;
-        UWORD8 mem_loc = MEM_REC_CNT;
-
-        cncl_fill_ip.s_ivd_fill_mem_rec_ip_t.e_cmd = IV_CMD_FILL_NUM_MEM_REC;
-        cncl_fill_ip.s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location = &(memTab[mem_loc]);
-        cncl_fill_ip.s_ivd_fill_mem_rec_ip_t.u4_size = ps_mem_q_ip->s_ivd_fill_mem_rec_ip_t.u4_size;
-        cncl_fill_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_wd = max_wd_luma;
-        cncl_fill_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_ht = max_ht_luma;
-
-        status = icncl_api_function(NULL, (void *)&cncl_fill_ip, (void *)&cncl_fill_op);
-
-        if(IV_SUCCESS == status)
-        {
-            icncl_num_mem_rec_ip_t cncl_mem_ip;
-            icncl_num_mem_rec_op_t cncl_mem_op;
-
-            cncl_mem_ip.s_ivd_num_rec_ip_t.e_cmd = IV_CMD_GET_NUM_MEM_REC;
-            cncl_mem_ip.s_ivd_num_rec_ip_t.u4_size = sizeof(icncl_num_mem_rec_ip_t);
-
-            status = icncl_api_function(NULL, (void *)&cncl_mem_ip, (void *)&cncl_mem_op);
-            if(IV_SUCCESS == status)
-            {
-                ps_mem_q_op->s_ivd_fill_mem_rec_op_t.u4_num_mem_rec_filled += cncl_mem_op.s_ivd_num_mem_rec_op_t.u4_num_mem_rec;
-            }
-        }
-
-        return status;
-
-    }
-#endif //APPLY_CONCEALMENT
-    DEBUG("Num mem recs in fill call : %d\n",
-                    ps_mem_q_op->s_ivd_fill_mem_rec_op_t.u4_num_mem_rec_filled);
-
-
-    return (status);
-}
-
-
-/**
-*******************************************************************************
-*
-* @brief
-*  Initializes from mem records passed to the codec
-*
-* @par Description:
-*  Initializes pointers based on mem records passed
-*
-* @param[in] ps_codec_obj
-*  Pointer to codec object at API level
-*
-* @param[in] pv_api_ip
-*  Pointer to input argument structure
-*
-* @param[out] pv_api_op
-*  Pointer to output argument structure
-*
-* @returns  Status
-*
-* @remarks
-*
-*
-*******************************************************************************
-*/
-WORD32 ihevcd_init_mem_rec(iv_obj_t *ps_codec_obj,
-                           void *pv_api_ip,
-                           void *pv_api_op)
-{
-
-    ihevcd_cxa_init_ip_t *dec_init_ip;
-    ihevcd_cxa_init_op_t *dec_init_op;
-    WORD32 i;
-    iv_mem_rec_t *ps_mem_rec, *ps_mem_rec_base;
-    WORD32 status = IV_SUCCESS;
-    codec_t *ps_codec;
-    WORD32 max_tile_cols, max_tile_rows;
-
-    dec_init_ip = (ihevcd_cxa_init_ip_t *)pv_api_ip;
-    dec_init_op = (ihevcd_cxa_init_op_t *)pv_api_op;
-
-    ps_mem_rec_base = dec_init_ip->s_ivd_init_ip_t.pv_mem_rec_location;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_CODEC];
-    ps_codec_obj->pv_codec_handle = ps_mem_rec->pv_base;
-
-    ps_codec = (codec_t *)(ps_codec_obj->pv_codec_handle);
+    (*pps_codec_obj)->pv_codec_handle = NULL;
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, sizeof(codec_t));
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    (*pps_codec_obj)->pv_codec_handle = (codec_t *)pv_buf;
+    ps_codec = (codec_t *)pv_buf;
 
-    /* Note this memset can not be done in init() call, since init will called
-    during reset as well. And calling this during reset will mean all pointers
-    need to reinitialized*/
     memset(ps_codec, 0, sizeof(codec_t));
 
-    if(dec_init_ip->s_ivd_init_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_init_ip_t, i4_level))
-    {
-        ps_codec->i4_init_level = dec_init_ip->i4_level;
-
-        ps_codec->i4_init_level *= 3;
-    }
-    else
-    {
-        ps_codec->i4_init_level = MAX_LEVEL;
-    }
-
-    if(dec_init_ip->s_ivd_init_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_init_ip_t, u4_num_ref_frames))
-    {
-        ps_codec->i4_init_num_ref = dec_init_ip->u4_num_ref_frames;
-    }
-    else
-    {
-        ps_codec->i4_init_num_ref = MAX_REF_CNT;
-    }
-
-    if(dec_init_ip->s_ivd_init_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_init_ip_t, u4_num_reorder_frames))
-    {
-        ps_codec->i4_init_num_reorder = dec_init_ip->u4_num_reorder_frames;
-    }
-    else
-    {
-        ps_codec->i4_init_num_reorder = MAX_REF_CNT;
-    }
-
-    if(dec_init_ip->s_ivd_init_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_init_ip_t, u4_num_extra_disp_buf))
-    {
-        ps_codec->i4_init_num_extra_disp_buf =
-                        dec_init_ip->u4_num_extra_disp_buf;
-    }
-    else
-    {
-        ps_codec->i4_init_num_extra_disp_buf = 0;
-    }
-
-    if(dec_init_ip->s_ivd_init_ip_t.u4_size
-                    > offsetof(ihevcd_cxa_init_ip_t, u4_share_disp_buf))
-    {
 #ifndef LOGO_EN
-        ps_codec->i4_share_disp_buf = dec_init_ip->u4_share_disp_buf;
+    ps_codec->i4_share_disp_buf = ps_create_ip->s_ivd_create_ip_t.u4_share_disp_buf;
 #else
-        ps_codec->i4_share_disp_buf = 0;
+    ps_codec->i4_share_disp_buf = 0;
 #endif
-    }
-    else
-    {
-        ps_codec->i4_share_disp_buf = 0;
-    }
+
     /* Shared display mode is supported only for 420SP and 420P formats */
-    if((dec_init_ip->s_ivd_init_ip_t.e_output_format != IV_YUV_420P) &&
-       (dec_init_ip->s_ivd_init_ip_t.e_output_format != IV_YUV_420SP_UV) &&
-       (dec_init_ip->s_ivd_init_ip_t.e_output_format != IV_YUV_420SP_VU))
+    if((ps_create_ip->s_ivd_create_ip_t.e_output_format != IV_YUV_420P) &&
+       (ps_create_ip->s_ivd_create_ip_t.e_output_format != IV_YUV_420SP_UV) &&
+       (ps_create_ip->s_ivd_create_ip_t.e_output_format != IV_YUV_420SP_VU))
     {
         ps_codec->i4_share_disp_buf = 0;
     }
 
-    if((ps_codec->i4_init_level < MIN_LEVEL)
-                    || (ps_codec->i4_init_level > MAX_LEVEL))
-    {
-        dec_init_op->s_ivd_init_op_t.u4_error_code |= IHEVCD_LEVEL_UNSUPPORTED;
-        return (IV_FAIL);
-    }
+    ps_codec->e_chroma_fmt = ps_create_ip->s_ivd_create_ip_t.e_output_format;
 
-    if(ps_codec->i4_init_num_ref > MAX_REF_CNT)
-    {
-        dec_init_op->s_ivd_init_op_t.u4_error_code |=
-                        IHEVCD_NUM_REF_UNSUPPORTED;
-        ps_codec->i4_init_num_ref = MAX_REF_CNT;
-    }
+    ps_codec->pf_aligned_alloc = pf_aligned_alloc;
+    ps_codec->pf_aligned_free = pf_aligned_free;
+    ps_codec->pv_mem_ctxt = pv_mem_ctxt;
 
-    if(ps_codec->i4_init_num_reorder > MAX_REF_CNT)
-    {
-        dec_init_op->s_ivd_init_op_t.u4_error_code |=
-                        IHEVCD_NUM_REORDER_UNSUPPORTED;
-        ps_codec->i4_init_num_reorder = MAX_REF_CNT;
-    }
+    /* Request memory to hold thread handles for each processing thread */
+    size = MAX_PROCESS_THREADS * ithread_get_handle_size();
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
 
-    if(ps_codec->i4_init_num_extra_disp_buf > MAX_REF_CNT)
-    {
-        dec_init_op->s_ivd_init_op_t.u4_error_code |=
-                        IHEVCD_NUM_EXTRA_DISP_UNSUPPORTED;
-        ps_codec->i4_init_num_extra_disp_buf = 0;
-    }
-
-    ps_codec->e_chroma_fmt = dec_init_ip->s_ivd_init_ip_t.e_output_format;
-
-    ps_codec->i4_max_wd = dec_init_ip->s_ivd_init_ip_t.u4_frm_max_wd;
-    ps_codec->i4_max_ht = dec_init_ip->s_ivd_init_ip_t.u4_frm_max_ht;
-
-    ps_codec->i4_max_wd = ALIGN64(ps_codec->i4_max_wd);
-    ps_codec->i4_max_ht = ALIGN64(ps_codec->i4_max_ht);
-
-    ps_codec->i4_new_max_wd = ps_codec->i4_max_wd;
-    ps_codec->i4_new_max_ht = ps_codec->i4_max_ht;
-
-    max_tile_cols = (ps_codec->i4_max_wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
-    max_tile_rows = (ps_codec->i4_max_ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_BACKUP];
-    ps_codec->ps_mem_rec_backup = (iv_mem_rec_t *)ps_mem_rec->pv_base;
-
-    memcpy(ps_codec->ps_mem_rec_backup, ps_mem_rec_base,
-           MEM_REC_CNT * sizeof(iv_mem_rec_t));
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_BITSBUF];
-    ps_codec->pu1_bitsbuf = (UWORD8 *)ps_mem_rec->pv_base;
-    ps_codec->u4_bitsbuf_size = ps_mem_rec->u4_mem_size;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_TU_DATA];
-    ps_codec->pv_tu_data = ps_mem_rec->pv_base;
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_MVBANK];
-    ps_codec->pv_mv_buf_mgr = ps_mem_rec->pv_base;
-    ps_codec->pv_mv_bank_buf_base = (UWORD8 *)ps_codec->pv_mv_buf_mgr + sizeof(buf_mgr_t);
-
-    ps_codec->i4_total_mv_bank_size = ps_mem_rec->u4_mem_size - sizeof(buf_mgr_t);
-
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_VPS];
-    ps_codec->ps_vps_base = (vps_t *)ps_mem_rec->pv_base;
-    ps_codec->s_parse.ps_vps_base = ps_codec->ps_vps_base;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SPS];
-    ps_codec->ps_sps_base = (sps_t *)ps_mem_rec->pv_base;
-    ps_codec->s_parse.ps_sps_base = ps_codec->ps_sps_base;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PPS];
-    ps_codec->ps_pps_base = (pps_t *)ps_mem_rec->pv_base;
-    ps_codec->s_parse.ps_pps_base = ps_codec->ps_pps_base;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SLICE_HDR];
-    ps_codec->ps_slice_hdr_base = (slice_header_t *)ps_mem_rec->pv_base;
-    ps_codec->s_parse.ps_slice_hdr_base = ps_codec->ps_slice_hdr_base;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_TILE];
-    ps_codec->ps_tile = (tile_t *)ps_mem_rec->pv_base;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_ENTRY_OFST];
-    ps_codec->pi4_entry_ofst = (WORD32 *)ps_mem_rec->pv_base;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SCALING_MAT];
-    ps_codec->pi2_scaling_mat = (WORD16 *)ps_mem_rec->pv_base;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PARSE_SKIP_FLAG];
-    ps_codec->s_parse.pu4_skip_cu_top = (UWORD32 *)ps_mem_rec->pv_base;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PARSE_CT_DEPTH];
-    ps_codec->s_parse.pu4_ct_depth_top = (UWORD32 *)ps_mem_rec->pv_base;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PARSE_INTRA_PRED_MODE];
-    ps_codec->s_parse.pu1_luma_intra_pred_mode_left =
-                    (UWORD8 *)ps_mem_rec->pv_base;
-    ps_codec->s_parse.pu1_luma_intra_pred_mode_top  =
-                    (UWORD8 *)ps_mem_rec->pv_base + 16;
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_INTRA_FLAG];
-
-    memset(ps_mem_rec->pv_base, 0, (ps_codec->i4_max_wd / MIN_CU_SIZE) * (ps_codec->i4_max_ht / MIN_CU_SIZE) / 8);
-
-    ps_codec->pu1_pic_intra_flag = (UWORD8 *)ps_mem_rec->pv_base;
-    ps_codec->s_parse.pu1_pic_intra_flag = ps_codec->pu1_pic_intra_flag;
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_TRANSQUANT_BYPASS_FLAG];
-
-    {
-        WORD32 loop_filter_size = ((ps_codec->i4_max_wd  + 64) / MIN_CU_SIZE) * ((ps_codec->i4_max_ht + 64) / MIN_CU_SIZE) / 8;
-        WORD32 loop_filter_strd = (ps_codec->i4_max_wd + 63) >> 6;
-
-        memset(ps_mem_rec->pv_base, 0, loop_filter_size);
-
-        /* The offset is added for easy processing of top and left blocks while loop filtering */
-        ps_codec->pu1_pic_no_loop_filter_flag = (UWORD8 *)ps_mem_rec->pv_base + loop_filter_strd + 1;
-        ps_codec->s_parse.pu1_pic_no_loop_filter_flag = ps_codec->pu1_pic_no_loop_filter_flag;
-        ps_codec->s_parse.s_deblk_ctxt.pu1_pic_no_loop_filter_flag = ps_codec->pu1_pic_no_loop_filter_flag;
-        ps_codec->s_parse.s_sao_ctxt.pu1_pic_no_loop_filter_flag = ps_codec->pu1_pic_no_loop_filter_flag;
-    }
-
-    /* Initialize pointers in PPS structures */
-    {
-        sps_t *ps_sps = ps_codec->ps_sps_base;
-        pps_t *ps_pps = ps_codec->ps_pps_base;
-        tile_t *ps_tile =  ps_codec->ps_tile;
-        WORD16 *pi2_scaling_mat =  ps_codec->pi2_scaling_mat;
-        WORD32 scaling_mat_size;
-
-        SCALING_MAT_SIZE(scaling_mat_size);
-
-        for(i = 0; i < MAX_SPS_CNT; i++)
-        {
-            ps_sps->pi2_scaling_mat  = pi2_scaling_mat;
-            pi2_scaling_mat += scaling_mat_size;
-            ps_sps++;
-        }
-
-        for(i = 0; i < MAX_PPS_CNT; i++)
-        {
-            ps_pps->ps_tile = ps_tile;
-            ps_tile += (max_tile_cols * max_tile_rows);
-
-            ps_pps->pi2_scaling_mat  = pi2_scaling_mat;
-            pi2_scaling_mat += scaling_mat_size;
-            ps_pps++;
-        }
-
-    }
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_THREAD_HANDLE];
     for(i = 0; i < MAX_PROCESS_THREADS; i++)
     {
         WORD32 handle_size = ithread_get_handle_size();
         ps_codec->apv_process_thread_handle[i] =
-                        (UWORD8 *)ps_mem_rec->pv_base + (i * handle_size);
+                        (UWORD8 *)pv_buf + (i * handle_size);
     }
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PROC_JOBQ];
-    ps_codec->pv_proc_jobq_buf = ps_mem_rec->pv_base;
-    ps_codec->i4_proc_jobq_buf_size = ps_mem_rec->u4_mem_size;
+    /* Request memory for static bitstream buffer which holds bitstream after emulation prevention */
+    size = MIN_BITSBUF_SIZE;
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->pu1_bitsbuf_static = pv_buf;
+    ps_codec->u4_bitsbuf_size_static = size;
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PARSE_MAP];
-    ps_codec->pu1_parse_map = (UWORD8 *)ps_mem_rec->pv_base;
+    /* size for holding display manager context */
+    size = sizeof(buf_mgr_t);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->pv_disp_buf_mgr = pv_buf;
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PROC_MAP];
-    ps_codec->pu1_proc_map = (UWORD8 *)ps_mem_rec->pv_base;
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_DISP_MGR];
-    ps_codec->pv_disp_buf_mgr = ps_mem_rec->pv_base;
+    /* size for holding dpb manager context */
+    size = sizeof(dpb_mgr_t);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->pv_dpb_mgr = pv_buf;
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_DPB_MGR];
-    ps_codec->pv_dpb_mgr = ps_mem_rec->pv_base;
+    /* size for holding buffer manager context */
+    size = sizeof(buf_mgr_t);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->pv_pic_buf_mgr = pv_buf;
+
+    /* size for holding mv buffer manager context */
+    size = sizeof(buf_mgr_t);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->pv_mv_buf_mgr = pv_buf;
+
+    size = MAX_VPS_CNT * sizeof(vps_t);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->ps_vps_base = pv_buf;
+    ps_codec->s_parse.ps_vps_base = ps_codec->ps_vps_base;
+
+    size = MAX_SPS_CNT * sizeof(sps_t);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->ps_sps_base = pv_buf;
+    ps_codec->s_parse.ps_sps_base = ps_codec->ps_sps_base;
+
+    size = MAX_PPS_CNT * sizeof(pps_t);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->ps_pps_base = pv_buf;
+    ps_codec->s_parse.ps_pps_base = ps_codec->ps_pps_base;
+
+    size = MAX_SLICE_HDR_CNT * sizeof(slice_header_t);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->ps_slice_hdr_base = (slice_header_t *)pv_buf;
+    ps_codec->s_parse.ps_slice_hdr_base = ps_codec->ps_slice_hdr_base;
 
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PIC_PU_IDX_NEIGHBOR];
+    SCALING_MAT_SIZE(size)
+    size = (MAX_SPS_CNT + MAX_PPS_CNT) * size * sizeof(WORD16);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->pi2_scaling_mat = (WORD16 *)pv_buf;
+
+
+    /* Size for holding pic_buf_t for each reference picture
+     * Since this is only a structure allocation and not actual buffer allocation,
+     * it is allocated for BUF_MGR_MAX_CNT entries
+     */
+    size = BUF_MGR_MAX_CNT * sizeof(pic_buf_t);
+    pv_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->pv_pic_buf_base = (UWORD8 *)pv_buf;
+
+    /* TO hold scratch buffers needed for each SAO context */
+    size = 4 * MAX_CTB_SIZE * MAX_CTB_SIZE;
+
+    /* 2 temporary buffers*/
+    size *= 2;
+    size *= MAX_PROCESS_THREADS;
+
+    pu1_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pu1_buf), IV_FAIL);
 
     for(i = 0; i < MAX_PROCESS_THREADS; i++)
     {
-        UWORD32 *pu4_buf = (UWORD32 *)ps_mem_rec->pv_base;
-        ps_codec->as_process[i].pu4_pic_pu_idx_left = pu4_buf + i * (MAX_CTB_SIZE / 4);
-        memset(ps_codec->as_process[i].pu4_pic_pu_idx_left, 0, sizeof(UWORD32) * MAX_CTB_SIZE / 4);
-        ps_codec->as_process[i].pu4_pic_pu_idx_top = pu4_buf + MAX_PROCESS_THREADS * (MAX_CTB_SIZE / 4);
+        ps_codec->as_process[i].s_sao_ctxt.pu1_tmp_buf_luma = (UWORD8 *)pu1_buf;
+        pu1_buf += 4 * MAX_CTB_SIZE * MAX_CTB_SIZE * sizeof(UWORD8);
+
+        ps_codec->as_process[i].s_sao_ctxt.pu1_tmp_buf_chroma = (UWORD8 *)pu1_buf;
+        pu1_buf += 4 * MAX_CTB_SIZE * MAX_CTB_SIZE * sizeof(UWORD8);
     }
-    memset(ps_codec->as_process[0].pu4_pic_pu_idx_top, 0, sizeof(UWORD32) * (ps_codec->i4_max_wd / 4 + 1));
 
+    /* Allocate intra pred modes buffer */
+    /* 8 bits per 4x4 */
+    /* 16 bytes each for top and left 64 pixels and 16 bytes for default mode */
+    size =  3 * 16 * sizeof(UWORD8);
+    pu1_buf = pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pu1_buf), IV_FAIL);
+    ps_codec->s_parse.pu1_luma_intra_pred_mode_left = pu1_buf;
+    ps_codec->s_parse.pu1_luma_intra_pred_mode_top  = pu1_buf + 16;
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_PROC_SCRATCH];
     {
-        UWORD8 *pu1_buf = (UWORD8 *)ps_mem_rec->pv_base;
-        WORD32 pic_pu_idx_map_size;
-
         WORD32 inter_pred_tmp_buf_size, ntaps_luma;
+        WORD32 pic_pu_idx_map_size;
 
         /* Max inter pred size */
         ntaps_luma = 8;
@@ -2750,6 +1298,15 @@
         /* To hold pu_index w.r.t. frame level pu_t array for a CTB */
         pic_pu_idx_map_size = sizeof(WORD32) * (18 * 18);
         pic_pu_idx_map_size = ALIGN64(pic_pu_idx_map_size);
+
+        size =  inter_pred_tmp_buf_size * 2;
+        size += pic_pu_idx_map_size;
+        size *= MAX_PROCESS_THREADS;
+
+        pu1_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+        RETURN_IF((NULL == pu1_buf), IV_FAIL);
+
+
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].pi2_inter_pred_tmp_buf1 = (WORD16 *)pu1_buf;
@@ -2775,52 +1332,370 @@
             ps_codec->as_process[i].i4_inter_pred_tmp_buf_strd = MAX_CTB_SIZE;
 
         }
+    }
+    /* Initialize pointers in PPS structures */
+    {
+        sps_t *ps_sps = ps_codec->ps_sps_base;
+        pps_t *ps_pps = ps_codec->ps_pps_base;
+        WORD16 *pi2_scaling_mat =  ps_codec->pi2_scaling_mat;
+        WORD32 scaling_mat_size;
+
+        SCALING_MAT_SIZE(scaling_mat_size);
+
+        for(i = 0; i < MAX_SPS_CNT; i++)
+        {
+            ps_sps->pi2_scaling_mat  = pi2_scaling_mat;
+            pi2_scaling_mat += scaling_mat_size;
+            ps_sps++;
+        }
+
+        for(i = 0; i < MAX_PPS_CNT; i++)
+        {
+            ps_pps->pi2_scaling_mat  = pi2_scaling_mat;
+            pi2_scaling_mat += scaling_mat_size;
+            ps_pps++;
+        }
+    }
+
+    return (status);
+}
+
+/**
+*******************************************************************************
+*
+* @brief
+*  Free static memory for the codec
+*
+* @par Description:
+*  Free static memory for the codec
+*
+* @param[in] ps_codec
+*  Pointer to codec context
+*
+* @returns  Status
+*
+* @remarks
+*
+*
+*******************************************************************************
+*/
+WORD32 ihevcd_free_static_bufs(iv_obj_t *ps_codec_obj)
+{
+    codec_t *ps_codec;
+
+    void (*pf_aligned_free)(void *pv_mem_ctxt, void *pv_buf);
+    void *pv_mem_ctxt;
+
+    ps_codec = (codec_t *)ps_codec_obj->pv_codec_handle;
+    pf_aligned_free = ps_codec->pf_aligned_free;
+    pv_mem_ctxt = ps_codec->pv_mem_ctxt;
+
+
+    ALIGNED_FREE(ps_codec, ps_codec->apv_process_thread_handle[0]);
+    ALIGNED_FREE(ps_codec, ps_codec->pu1_bitsbuf_static);
+
+    ALIGNED_FREE(ps_codec, ps_codec->pv_disp_buf_mgr);
+    ALIGNED_FREE(ps_codec, ps_codec->pv_dpb_mgr);
+    ALIGNED_FREE(ps_codec, ps_codec->pv_pic_buf_mgr);
+    ALIGNED_FREE(ps_codec, ps_codec->pv_mv_buf_mgr);
+    ALIGNED_FREE(ps_codec, ps_codec->ps_vps_base);
+    ALIGNED_FREE(ps_codec, ps_codec->ps_sps_base);
+    ALIGNED_FREE(ps_codec, ps_codec->ps_pps_base);
+    ALIGNED_FREE(ps_codec, ps_codec->ps_slice_hdr_base);
+    ALIGNED_FREE(ps_codec, ps_codec->pi2_scaling_mat);
+    ALIGNED_FREE(ps_codec, ps_codec->pv_pic_buf_base);
+    ALIGNED_FREE(ps_codec, ps_codec->s_parse.pu1_luma_intra_pred_mode_left);
+    ALIGNED_FREE(ps_codec, ps_codec->as_process[0].s_sao_ctxt.pu1_tmp_buf_luma);
+    ALIGNED_FREE(ps_codec, ps_codec->as_process[0].pi2_inter_pred_tmp_buf1);
+    ALIGNED_FREE(ps_codec, ps_codec_obj->pv_codec_handle);
+
+    if(ps_codec_obj)
+    {
+        pf_aligned_free(pv_mem_ctxt, ps_codec_obj);
+    }
+
+    return IV_SUCCESS;
+
+}
+
+
+/**
+*******************************************************************************
+*
+* @brief
+*  Allocate dynamic memory for the codec
+*
+* @par Description:
+*  Allocates dynamic memory for the codec
+*
+* @param[in] ps_codec
+*  Pointer to codec context
+*
+* @returns  Status
+*
+* @remarks
+*
+*
+*******************************************************************************
+*/
+WORD32 ihevcd_allocate_dynamic_bufs(codec_t *ps_codec)
+{
+    WORD32 max_tile_cols, max_tile_rows;
+    WORD32 max_ctb_rows, max_ctb_cols;
+    WORD32 max_num_cu_cols;
+    WORD32 max_num_cu_rows;
+    WORD32 max_num_4x4_cols;
+    WORD32 max_ctb_cnt;
+    WORD32 wd;
+    WORD32 ht;
+    WORD32 i;
+    WORD32 max_dpb_size;
+    void *pv_mem_ctxt = ps_codec->pv_mem_ctxt;
+    void *pv_buf;
+    UWORD8 *pu1_buf;
+    WORD32 size;
+
+    wd = ALIGN64(ps_codec->i4_wd);
+    ht = ALIGN64(ps_codec->i4_ht);
+
+    max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
+    max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
+    max_ctb_rows  = ht / MIN_CTB_SIZE;
+    max_ctb_cols  = wd / MIN_CTB_SIZE;
+    max_ctb_cnt   = max_ctb_rows * max_ctb_cols;
+    max_num_cu_cols = wd / MIN_CU_SIZE;
+    max_num_cu_rows = ht / MIN_CU_SIZE;
+    max_num_4x4_cols = wd / 4;
+
+    /* Allocate tile structures */
+    size = max_tile_cols * max_tile_rows;
+    size *= sizeof(tile_t);
+    size *= MAX_PPS_CNT;
+
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+    ps_codec->ps_tile = (tile_t *)pv_buf;
+
+
+    /* Allocate memory to hold entry point offsets */
+    /* One entry point per tile */
+    size = max_tile_cols * max_tile_rows;
+
+    /* One entry point per row of CTBs */
+    /*********************************************************************/
+    /* Only tiles or entropy sync is enabled at a time in main           */
+    /* profile, but since memory required does not increase too much,    */
+    /* this allocation is done to handle both cases                      */
+    /*********************************************************************/
+    size  += max_ctb_rows;
+    size *= sizeof(WORD32);
+
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+    ps_codec->pi4_entry_ofst = (WORD32 *)pv_buf;
+
+    /* Allocate parse skip flag buffer */
+    /* 1 bit per 8x8 */
+    size = max_num_cu_cols / 8;
+    size = ALIGN4(size);
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+    ps_codec->s_parse.pu4_skip_cu_top = (UWORD32 *)pv_buf;
+
+    /* Allocate parse coding tree depth buffer */
+    /* 2 bits per 8x8 */
+    size =  max_num_cu_cols / 4;
+    size = ALIGN4(size);
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+    ps_codec->s_parse.pu4_ct_depth_top = (UWORD32 *)pv_buf;
+
+    /* Allocate intra flag buffer */
+    /* 1 bit per 8x8 */
+    size =  max_num_cu_cols * max_num_cu_rows / 8;
+    size = ALIGN4(size);
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+    ps_codec->pu1_pic_intra_flag = (UWORD8 *)pv_buf;
+    ps_codec->s_parse.pu1_pic_intra_flag = ps_codec->pu1_pic_intra_flag;
+
+    /* Allocate transquant bypass flag buffer */
+    /* 1 bit per 8x8 */
+    /* Extra row and column are allocated for easy processing of top and left blocks while loop filtering */
+    size =  ((max_num_cu_cols + 8) * (max_num_cu_rows + 8)) / 8;
+    size = ALIGN4(size);
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 1, size);
+    {
+        WORD32 loop_filter_strd = (wd + 63) >> 6;
+        ps_codec->pu1_pic_no_loop_filter_flag_base = pv_buf;
+        /* The offset is added for easy processing of top and left blocks while loop filtering */
+        ps_codec->pu1_pic_no_loop_filter_flag = (UWORD8 *)pv_buf + loop_filter_strd + 1;
+        ps_codec->s_parse.pu1_pic_no_loop_filter_flag = ps_codec->pu1_pic_no_loop_filter_flag;
+        ps_codec->s_parse.s_deblk_ctxt.pu1_pic_no_loop_filter_flag = ps_codec->pu1_pic_no_loop_filter_flag;
+        ps_codec->s_parse.s_sao_ctxt.pu1_pic_no_loop_filter_flag = ps_codec->pu1_pic_no_loop_filter_flag;
+    }
+
+    /* Initialize pointers in PPS structures */
+    {
+        pps_t *ps_pps = ps_codec->ps_pps_base;
+        tile_t *ps_tile =  ps_codec->ps_tile;
+
+        for(i = 0; i < MAX_PPS_CNT; i++)
+        {
+            ps_pps->ps_tile = ps_tile;
+            ps_tile += (max_tile_cols * max_tile_rows);
+            ps_pps++;
+        }
+
+    }
+
+    /* Allocate memory for job queue */
+
+    /* One job per row of CTBs */
+    size  = max_ctb_rows;
+
+    /* One each tile a row of CTBs, num_jobs has to incremented */
+    size  *= max_tile_cols;
+
+    /* One format convert/frame copy job per row of CTBs for non-shared mode*/
+    size  += max_ctb_rows;
+
+    size *= sizeof(proc_job_t);
+
+    size += ihevcd_jobq_ctxt_size();
+    size = ALIGN4(size);
+
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->pv_proc_jobq_buf = pv_buf;
+    ps_codec->i4_proc_jobq_buf_size = size;
+
+    size =  max_ctb_cnt;
+    size = ALIGN4(size);
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+    ps_codec->pu1_parse_map = (UWORD8 *)pv_buf;
+
+    size =  max_ctb_cnt;
+    size = ALIGN4(size);
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+    ps_codec->pu1_proc_map = (UWORD8 *)pv_buf;
+
+    /** Holds top and left neighbor's pu idx into picture level pu array */
+    /* Only one top row is enough but left has to be replicated for each process context */
+    size =  (max_num_4x4_cols  /* left */ + MAX_PROCESS_THREADS * (MAX_CTB_SIZE / 4)/* top */ + 1/* top right */) * sizeof(WORD32);
+    size = ALIGN4(size);
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+
+    for(i = 0; i < MAX_PROCESS_THREADS; i++)
+    {
+        UWORD32 *pu4_buf = (UWORD32 *)pv_buf;
+        ps_codec->as_process[i].pu4_pic_pu_idx_left = pu4_buf + i * (MAX_CTB_SIZE / 4);
+        memset(ps_codec->as_process[i].pu4_pic_pu_idx_left, 0, sizeof(UWORD32) * MAX_CTB_SIZE / 4);
+        ps_codec->as_process[i].pu4_pic_pu_idx_top = pu4_buf + MAX_PROCESS_THREADS * (MAX_CTB_SIZE / 4);
+    }
+    memset(ps_codec->as_process[0].pu4_pic_pu_idx_top, 0, sizeof(UWORD32) * (wd / 4 + 1));
+
+    {
+        /* To hold SAO left buffer for luma */
+        size  = sizeof(UWORD8) * (MAX(ht, wd));
+
+        /* To hold SAO left buffer for chroma */
+        size += sizeof(UWORD8) * (MAX(ht, wd));
+
+        /* To hold SAO top buffer for luma */
+        size += sizeof(UWORD8) * wd;
+
+        /* To hold SAO top buffer for chroma */
+        size += sizeof(UWORD8) * wd;
+
+        /* To hold SAO top left luma pixel value for last output ctb in a row*/
+        size += sizeof(UWORD8) * max_ctb_rows;
+
+        /* To hold SAO top left chroma pixel value last output ctb in a row*/
+        size += sizeof(UWORD8) * max_ctb_rows * 2;
+
+        /* To hold SAO top left pixel luma for current ctb - column array*/
+        size += sizeof(UWORD8) * max_ctb_rows;
+
+        /* To hold SAO top left pixel chroma for current ctb-column array*/
+        size += sizeof(UWORD8) * max_ctb_rows * 2;
+
+        /* To hold SAO top right pixel luma pixel value last output ctb in a row*/
+        size += sizeof(UWORD8) * max_ctb_cols;
+
+        /* To hold SAO top right pixel chroma pixel value last output ctb in a row*/
+        size += sizeof(UWORD8) * max_ctb_cols * 2;
+
+        /*To hold SAO botton bottom left pixels for luma*/
+        size += sizeof(UWORD8) * max_ctb_rows;
+
+        /*To hold SAO botton bottom left pixels for luma*/
+        size += sizeof(UWORD8) * max_ctb_rows * 2;
+        size = ALIGN64(size);
+
+        pu1_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+        RETURN_IF((NULL == pu1_buf), IV_FAIL);
+        memset(pu1_buf, 0, size);
+
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].s_sao_ctxt.pu1_sao_src_left_luma = (UWORD8 *)pu1_buf;
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_left_luma = (UWORD8 *)pu1_buf;
-        pu1_buf += MAX(ps_codec->i4_max_ht, ps_codec->i4_max_wd);
+        pu1_buf += MAX(ht, wd);
 
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].s_sao_ctxt.pu1_sao_src_left_chroma = (UWORD8 *)pu1_buf;
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_left_chroma = (UWORD8 *)pu1_buf;
-        pu1_buf += MAX(ps_codec->i4_max_ht, ps_codec->i4_max_wd);
+        pu1_buf += MAX(ht, wd);
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].s_sao_ctxt.pu1_sao_src_top_luma = (UWORD8 *)pu1_buf;
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_top_luma = (UWORD8 *)pu1_buf;
-        pu1_buf += ps_codec->i4_max_wd;
+        pu1_buf += wd;
 
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].s_sao_ctxt.pu1_sao_src_top_chroma = (UWORD8 *)pu1_buf;
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_top_chroma = (UWORD8 *)pu1_buf;
-        pu1_buf += ps_codec->i4_max_wd;
+        pu1_buf += wd;
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].s_sao_ctxt.pu1_sao_src_luma_top_left_ctb = (UWORD8 *)pu1_buf;
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_luma_top_left_ctb = (UWORD8 *)pu1_buf;
-        pu1_buf += ps_codec->i4_max_ht / MIN_CTB_SIZE;
+        pu1_buf += ht / MIN_CTB_SIZE;
 
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].s_sao_ctxt.pu1_sao_src_chroma_top_left_ctb = (UWORD8 *)pu1_buf;
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_chroma_top_left_ctb = (UWORD8 *)pu1_buf;
-        pu1_buf += (ps_codec->i4_max_ht / MIN_CTB_SIZE) * 2;
+        pu1_buf += (ht / MIN_CTB_SIZE) * 2;
 
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].s_sao_ctxt.pu1_sao_src_top_left_luma_curr_ctb = (UWORD8 *)pu1_buf;
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_top_left_luma_curr_ctb = (UWORD8 *)pu1_buf;
-        pu1_buf += ps_codec->i4_max_ht / MIN_CTB_SIZE;
+        pu1_buf += ht / MIN_CTB_SIZE;
 
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
@@ -2828,21 +1703,21 @@
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_top_left_chroma_curr_ctb = (UWORD8 *)pu1_buf;
 
-        pu1_buf += (ps_codec->i4_max_ht / MIN_CTB_SIZE) * 2;
+        pu1_buf += (ht / MIN_CTB_SIZE) * 2;
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].s_sao_ctxt.pu1_sao_src_top_left_luma_top_right = (UWORD8 *)pu1_buf;
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_top_left_luma_top_right = (UWORD8 *)pu1_buf;
 
-        pu1_buf += ps_codec->i4_max_wd / MIN_CTB_SIZE;
+        pu1_buf += wd / MIN_CTB_SIZE;
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
             ps_codec->as_process[i].s_sao_ctxt.pu1_sao_src_top_left_chroma_top_right = (UWORD8 *)pu1_buf;
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_top_left_chroma_top_right = (UWORD8 *)pu1_buf;
 
-        pu1_buf += (ps_codec->i4_max_wd / MIN_CTB_SIZE) * 2;
+        pu1_buf += (wd / MIN_CTB_SIZE) * 2;
 
         /*Per CTB, Store 1 value for luma , 2 values for chroma*/
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
@@ -2851,7 +1726,7 @@
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_top_left_luma_bot_left = (UWORD8 *)pu1_buf;
 
-        pu1_buf += (ps_codec->i4_max_ht / MIN_CTB_SIZE);
+        pu1_buf += (ht / MIN_CTB_SIZE);
 
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
@@ -2859,35 +1734,22 @@
         }
         ps_codec->s_parse.s_sao_ctxt.pu1_sao_src_top_left_chroma_bot_left = (UWORD8 *)pu1_buf;
 
-        pu1_buf += (ps_codec->i4_max_ht / MIN_CTB_SIZE) * 2;
+        pu1_buf += (ht / MIN_CTB_SIZE) * 2;
     }
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SAO_SCRATCH];
-    {
-        UWORD8 *pu1_buf = (UWORD8 *)ps_mem_rec->pv_base;
-        for(i = 0; i < MAX_PROCESS_THREADS; i++)
-        {
-            ps_codec->as_process[i].s_sao_ctxt.pu1_tmp_buf_luma = (UWORD8 *)pu1_buf;
-            pu1_buf += 4 * MAX_CTB_SIZE * MAX_CTB_SIZE * sizeof(UWORD8);
 
-            ps_codec->as_process[i].s_sao_ctxt.pu1_tmp_buf_chroma = (UWORD8 *)pu1_buf;
-            pu1_buf += 4 * MAX_CTB_SIZE * MAX_CTB_SIZE * sizeof(UWORD8);
-        }
-    }
-
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_BS_QP];
     {
-        UWORD8 *pu1_buf = (UWORD8 *)ps_mem_rec->pv_base;
+        UWORD8 *pu1_buf = (UWORD8 *)pv_buf;
         WORD32 vert_bs_size, horz_bs_size;
         WORD32 qp_const_flag_size;
         WORD32 qp_size;
         WORD32 num_8x8;
 
         /* Max Number of vertical edges */
-        vert_bs_size = ps_codec->i4_max_wd / 8 + 2 * MAX_CTB_SIZE / 8;
+        vert_bs_size = wd / 8 + 2 * MAX_CTB_SIZE / 8;
 
         /* Max Number of horizontal edges - extra MAX_CTB_SIZE / 8 to handle the last 4 rows separately(shifted CTB processing) */
-        vert_bs_size *= (ps_codec->i4_max_ht + MAX_CTB_SIZE) / MIN_TU_SIZE;
+        vert_bs_size *= (ht + MAX_CTB_SIZE) / MIN_TU_SIZE;
 
         /* Number of bytes */
         vert_bs_size /= 8;
@@ -2896,10 +1758,10 @@
         vert_bs_size *= 2;
 
         /* Max Number of horizontal edges */
-        horz_bs_size = ps_codec->i4_max_ht / 8 + MAX_CTB_SIZE / 8;
+        horz_bs_size = ht / 8 + MAX_CTB_SIZE / 8;
 
         /* Max Number of vertical edges - extra MAX_CTB_SIZE / 8 to handle the last 4 columns separately(shifted CTB processing) */
-        horz_bs_size *= (ps_codec->i4_max_wd + MAX_CTB_SIZE) / MIN_TU_SIZE;
+        horz_bs_size *= (wd + MAX_CTB_SIZE) / MIN_TU_SIZE;
 
         /* Number of bytes */
         horz_bs_size /= 8;
@@ -2908,18 +1770,34 @@
         horz_bs_size *= 2;
 
         /* Max CTBs in a row */
-        qp_const_flag_size = ps_codec->i4_max_wd / MIN_CTB_SIZE + 1 /* The last ctb row deblk is done in last ctb + 1 row.*/;
+        qp_const_flag_size = wd / MIN_CTB_SIZE + 1 /* The last ctb row deblk is done in last ctb + 1 row.*/;
 
         /* Max CTBs in a column */
-        qp_const_flag_size *= ps_codec->i4_max_ht / MIN_CTB_SIZE;
+        qp_const_flag_size *= ht / MIN_CTB_SIZE;
 
         /* Number of bytes */
         qp_const_flag_size /= 8;
 
         /* QP changes at CU level - So store at 8x8 level */
-        num_8x8 = (ps_codec->i4_max_ht * ps_codec->i4_max_wd) / (MIN_CU_SIZE * MIN_CU_SIZE);
+        num_8x8 = (ht * wd) / (MIN_CU_SIZE * MIN_CU_SIZE);
         qp_size = num_8x8;
-        memset(pu1_buf, 0, vert_bs_size + horz_bs_size + qp_size + qp_const_flag_size);
+
+        /* To hold vertical boundary strength */
+        size += vert_bs_size;
+
+        /* To hold horizontal boundary strength */
+        size += horz_bs_size;
+
+        /* To hold QP */
+        size += qp_size;
+
+        /* To hold QP const in CTB flags */
+        size += qp_const_flag_size;
+
+        pu1_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+        RETURN_IF((NULL == pu1_buf), IV_FAIL);
+
+        memset(pu1_buf, 0, size);
 
         for(i = 0; i < MAX_PROCESS_THREADS; i++)
         {
@@ -2959,73 +1837,194 @@
 
     }
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_TILE_IDX];
-    {
-        UWORD8 *pu1_buf = (UWORD8 *)ps_mem_rec->pv_base;
+    /* Max CTBs in a row */
+    size  = wd / MIN_CTB_SIZE + 2 /* Top row and bottom row extra. This ensures accessing left,top in first row
+                                              and right in last row will not result in invalid access*/;
+    /* Max CTBs in a column */
+    size *= ht / MIN_CTB_SIZE;
 
-        for(i = 0; i < MAX_PROCESS_THREADS; i++)
-        {
-            ps_codec->as_process[i].pu1_tile_idx = (UWORD16 *)pu1_buf + ps_codec->i4_max_wd / MIN_CTB_SIZE /* Offset 1 row */;
-        }
+    size *= sizeof(UWORD16);
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+
+    ps_codec->pu1_tile_idx_base = pv_buf;
+    for(i = 0; i < MAX_PROCESS_THREADS; i++)
+    {
+        ps_codec->as_process[i].pu1_tile_idx = (UWORD16 *)pv_buf + wd / MIN_CTB_SIZE /* Offset 1 row */;
     }
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_SAO];
-    ps_codec->s_parse.ps_pic_sao = (sao_t *)ps_mem_rec->pv_base;
-    ps_codec->s_parse.s_sao_ctxt.ps_pic_sao = (sao_t *)ps_mem_rec->pv_base;
+    /* 4 bytes per color component per CTB */
+    size = 3 * 4;
+
+    /* MAX number of CTBs in a row */
+    size *= wd / MIN_CTB_SIZE;
+
+    /* MAX number of CTBs in a column */
+    size *= ht / MIN_CTB_SIZE;
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    memset(pv_buf, 0, size);
+
+    ps_codec->s_parse.ps_pic_sao = (sao_t *)pv_buf;
+    ps_codec->s_parse.s_sao_ctxt.ps_pic_sao = (sao_t *)pv_buf;
     for(i = 0; i < MAX_PROCESS_THREADS; i++)
     {
         ps_codec->as_process[i].s_sao_ctxt.ps_pic_sao = ps_codec->s_parse.ps_pic_sao;
     }
 
-    ps_mem_rec = &ps_mem_rec_base[MEM_REC_REF_PIC];
-    ps_codec->pv_pic_buf_mgr = ps_mem_rec->pv_base;
-    ps_codec->pv_pic_buf_base = (UWORD8 *)ps_codec->pv_pic_buf_mgr + sizeof(buf_mgr_t);
-    ps_codec->i4_total_pic_buf_size = ps_mem_rec->u4_mem_size - sizeof(buf_mgr_t);
-    ps_codec->pu1_cur_chroma_ref_buf = (UWORD8 *)ps_codec->pv_pic_buf_base + BUF_MGR_MAX_CNT * sizeof(pic_buf_t);
-    ps_codec->i4_remaining_pic_buf_size = ps_codec->i4_total_pic_buf_size - BUF_MGR_MAX_CNT * sizeof(pic_buf_t);
-
-
-
-
-#ifdef APPLY_CONCEALMENT
+    /* Only if width * height * 3 / 2 is greater than MIN_BITSBUF_SIZE,
+    then allocate dynamic bistream buffer */
+    ps_codec->pu1_bitsbuf_dynamic = NULL;
+    size = wd * ht;
+    if(size > MIN_BITSBUF_SIZE)
     {
+        pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+        RETURN_IF((NULL == pv_buf), IV_FAIL);
+        ps_codec->pu1_bitsbuf_dynamic = pv_buf;
+        ps_codec->u4_bitsbuf_size_dynamic = size;
+    }
 
-        UWORD32 mem_loc;
+    size = ihevcd_get_tu_data_size(wd * ht);
+    pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+    RETURN_IF((NULL == pv_buf), IV_FAIL);
+    ps_codec->pv_tu_data = pv_buf;
 
-        icncl_init_ip_t cncl_init_ip;
-        icncl_init_op_t cncl_init_op;
-        iv_mem_rec_t *ps_mem_rec;
-        DecStruct *ps_codec;
+    {
+        sps_t *ps_sps = (ps_codec->s_parse.ps_sps_base + ps_codec->i4_sps_id);
 
-        ps_mem_rec = dec_init_ip->s_ivd_init_ip_t.pv_mem_rec_location;
-        mem_loc = MEM_REC_CNT;
 
-        ps_codec->ps_conceal = (iv_obj_t *)ps_mem_rec[mem_loc].pv_base;
-        ps_codec->i4_first_frame_done = 0;
+        /* Allocate for pu_map, pu_t and pic_pu_idx for each MV bank */
+        /* Note: Number of luma samples is not max_wd * max_ht here, instead it is
+         * set to maximum number of luma samples allowed at the given level.
+         * This is done to ensure that any stream with width and height lesser
+         * than max_wd and max_ht is supported. Number of buffers required can be greater
+         * for lower width and heights at a given level and this increased number of buffers
+         * might require more memory than what max_wd and max_ht buffer would have required
+         * Also note one extra buffer is allocted to store current pictures MV bank
+         * In case of asynchronous parsing and processing, number of buffers should increase here
+         * based on when parsing and processing threads are synchronized
+         */
+        max_dpb_size = ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1];
+        /* Size for holding mv_buf_t for each MV Bank
+         * One extra MV Bank is needed to hold current pics MV bank.
+         */
+        size = (max_dpb_size + 1) * sizeof(mv_buf_t);
 
-        cncl_init_ip.u4_size = sizeof(icncl_init_ip_t);
-        cncl_init_ip.pv_mem_rec_location = &(ps_mem_rec[mem_loc]);
-        cncl_init_ip.e_cmd = IV_CMD_INIT;
+        size += (max_dpb_size + 1) *
+                        ihevcd_get_pic_mv_bank_size(wd * ht);
 
-        status = icncl_api_function(ps_codec->ps_conceal, (void *)&cncl_init_ip, (void *)&cncl_init_op);
+        pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+        RETURN_IF((NULL == pv_buf), IV_FAIL);
+
+        ps_codec->pv_mv_bank_buf_base = pv_buf;
+        ps_codec->i4_total_mv_bank_size = size;
 
     }
-#endif //APPLY_CONCEALMENT
 
-    status = ihevcd_init(ps_codec);
+    /* In case of non-shared mode allocate for reference picture buffers */
+    /* In case of shared and 420p output, allocate for chroma samples */
+    if(0 == ps_codec->i4_share_disp_buf)
+    {
+        /* Number of buffers is doubled in order to return one frame at a time instead of sending
+         * multiple outputs during dpb full case.
+         * Also note one extra buffer is allocted to store current picture
+         * In case of asynchronous parsing and processing, number of buffers should increase here
+         * based on when parsing and processing threads are synchronized
+         */
+        size = ihevcd_get_total_pic_buf_size(ps_codec, wd, ht);
+        pv_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+        RETURN_IF((NULL == pv_buf), IV_FAIL);
 
-    TRACE_INIT(NULL);
-    STATS_INIT();
-    return status;
+
+        ps_codec->i4_total_pic_buf_size = size;
+        ps_codec->pu1_ref_pic_buf_base = (UWORD8 *)pv_buf;
+    }
+
+    ps_codec->pv_proc_jobq = ihevcd_jobq_init(ps_codec->pv_proc_jobq_buf, ps_codec->i4_proc_jobq_buf_size);
+    RETURN_IF((ps_codec->pv_proc_jobq == NULL), IV_FAIL);
+
+    /* Update the jobq context to all the threads */
+    ps_codec->s_parse.pv_proc_jobq = ps_codec->pv_proc_jobq;
+    for(i = 0; i < MAX_PROCESS_THREADS; i++)
+    {
+        ps_codec->as_process[i].pv_proc_jobq = ps_codec->pv_proc_jobq;
+        ps_codec->as_process[i].i4_id = i;
+        ps_codec->as_process[i].ps_codec = ps_codec;
+
+        /* Set the following to zero assuming it is a single core solution
+         * When threads are launched these will be set appropriately
+         */
+        ps_codec->as_process[i].i4_check_parse_status = 0;
+        ps_codec->as_process[i].i4_check_proc_status = 0;
+    }
+
+    ps_codec->u4_allocate_dynamic_done = 1;
+
+    return IV_SUCCESS;
 }
+
 /**
 *******************************************************************************
 *
 * @brief
-*  Retrieves mem records passed to the codec
+*  Free dynamic memory for the codec
 *
 * @par Description:
-*  Retrieves memrecs passed earlier
+*  Free dynamic memory for the codec
+*
+* @param[in] ps_codec
+*  Pointer to codec context
+*
+* @returns  Status
+*
+* @remarks
+*
+*
+*******************************************************************************
+*/
+WORD32 ihevcd_free_dynamic_bufs(codec_t *ps_codec)
+{
+
+    if(ps_codec->pv_proc_jobq)
+    {
+        ihevcd_jobq_deinit(ps_codec->pv_proc_jobq);
+        ps_codec->pv_proc_jobq = NULL;
+    }
+
+    ALIGNED_FREE(ps_codec, ps_codec->ps_tile);
+    ALIGNED_FREE(ps_codec, ps_codec->pi4_entry_ofst);
+    ALIGNED_FREE(ps_codec, ps_codec->s_parse.pu4_skip_cu_top);
+    ALIGNED_FREE(ps_codec, ps_codec->s_parse.pu4_ct_depth_top);
+    ALIGNED_FREE(ps_codec, ps_codec->pu1_pic_intra_flag);
+    ALIGNED_FREE(ps_codec, ps_codec->pu1_pic_no_loop_filter_flag_base);
+    ALIGNED_FREE(ps_codec, ps_codec->pv_proc_jobq_buf);
+    ALIGNED_FREE(ps_codec, ps_codec->pu1_parse_map);
+    ALIGNED_FREE(ps_codec, ps_codec->pu1_proc_map);
+    ALIGNED_FREE(ps_codec, ps_codec->as_process[0].pu4_pic_pu_idx_left);
+    ALIGNED_FREE(ps_codec, ps_codec->as_process[0].s_sao_ctxt.pu1_sao_src_left_luma);
+    ALIGNED_FREE(ps_codec, ps_codec->as_process[0].s_bs_ctxt.pu4_pic_vert_bs);
+    ALIGNED_FREE(ps_codec, ps_codec->pu1_tile_idx_base);
+    ALIGNED_FREE(ps_codec, ps_codec->s_parse.ps_pic_sao);
+    ALIGNED_FREE(ps_codec, ps_codec->pu1_bitsbuf_dynamic);
+    ALIGNED_FREE(ps_codec, ps_codec->pv_tu_data);
+    ALIGNED_FREE(ps_codec, ps_codec->pv_mv_bank_buf_base);
+    ALIGNED_FREE(ps_codec, ps_codec->pu1_ref_pic_buf_base);
+    ALIGNED_FREE(ps_codec, ps_codec->pu1_cur_chroma_ref_buf);
+
+    ps_codec->u4_allocate_dynamic_done = 0;
+    return IV_SUCCESS;
+}
+
+
+/**
+*******************************************************************************
+*
+* @brief
+*  Initializes from mem records passed to the codec
+*
+* @par Description:
+*  Initializes pointers based on mem records passed
 *
 * @param[in] ps_codec_obj
 *  Pointer to codec object at API level
@@ -3043,74 +2042,82 @@
 *
 *******************************************************************************
 */
-WORD32 ihevcd_retrieve_memrec(iv_obj_t *ps_codec_obj,
-                              void *pv_api_ip,
-                              void *pv_api_op)
+WORD32 ihevcd_create(iv_obj_t *ps_codec_obj,
+                           void *pv_api_ip,
+                           void *pv_api_op)
 {
 
-    iv_retrieve_mem_rec_ip_t *dec_clr_ip;
-    iv_retrieve_mem_rec_op_t *dec_clr_op;
-    codec_t *ps_codec;
-    dec_clr_ip = (iv_retrieve_mem_rec_ip_t *)pv_api_ip;
-    dec_clr_op = (iv_retrieve_mem_rec_op_t *)pv_api_op;
-    ps_codec = (codec_t *)(ps_codec_obj->pv_codec_handle);
+    ihevcd_cxa_create_op_t *ps_create_op;
 
-    if(ps_codec->i4_init_done != 1)
+    WORD32 ret;
+    codec_t *ps_codec;
+    ps_create_op = (ihevcd_cxa_create_op_t *)pv_api_op;
+
+    ps_create_op->s_ivd_create_op_t.u4_error_code = 0;
+
+    ret = ihevcd_allocate_static_bufs(&ps_codec_obj, pv_api_ip, pv_api_op);
+
+    /* If allocation of some buffer fails, then free buffers allocated till then */
+    if((IV_FAIL == ret) && (NULL != ps_codec_obj))
     {
-        dec_clr_op->u4_error_code |= 1 << IVD_FATALERROR;
-        dec_clr_op->u4_error_code |= IHEVCD_INIT_NOT_DONE;
+        ihevcd_free_static_bufs(ps_codec_obj);
+        ps_create_op->s_ivd_create_op_t.u4_error_code = IVD_MEM_ALLOC_FAILED;
+        ps_create_op->s_ivd_create_op_t.u4_error_code = 1 << IVD_FATALERROR;
+
         return IV_FAIL;
     }
+    ps_codec = (codec_t *)ps_codec_obj->pv_codec_handle;
+    ret = ihevcd_init(ps_codec);
 
-    memcpy(dec_clr_ip->pv_mem_rec_location, ps_codec->ps_mem_rec_backup,
-           MEM_REC_CNT * (sizeof(iv_mem_rec_t)));
-    dec_clr_op->u4_num_mem_rec_filled = MEM_REC_CNT;
+    TRACE_INIT(NULL);
+    STATS_INIT();
 
-#ifdef APPLY_CONCEALMENT
-    {
-        IV_API_CALL_STATUS_T status;
-        icncl_fill_mem_rec_ip_t cncl_fill_ip;
-        icncl_fill_mem_rec_op_t cncl_fill_op;
-
-        iv_mem_rec_t *ps_mem_rec;
-
-        UWORD8 mem_loc = MEM_REC_CNT;
-        UWORD8 num_cncl_mem = 0;
-
-        ps_mem_rec = dec_clr_ip->pv_mem_rec_location;
-
-        cncl_fill_ip.s_ivd_fill_mem_rec_ip_t.e_cmd = IV_CMD_FILL_NUM_MEM_REC;
-        cncl_fill_ip.s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location = &(ps_mem_rec[mem_loc]);
-        cncl_fill_ip.s_ivd_fill_mem_rec_ip_t.u4_size = sizeof(icncl_fill_mem_rec_ip_t);
-
-        status = icncl_api_function(NULL, (void *)&cncl_fill_ip, (void *)&cncl_fill_op);
-
-        cncl_fill_ip.s_ivd_fill_mem_rec_ip_t.e_cmd = IV_CMD_RETRIEVE_MEMREC;
-        cncl_fill_op.s_ivd_fill_mem_rec_op_t.u4_size = sizeof(icncl_fill_mem_rec_op_t);
-
-        status = icncl_api_function(ps_codec->ps_conceal, (void *)&cncl_fill_ip, (void *)&cncl_fill_op);
-
-        if(status == IV_SUCCESS)
-        {
-            /* Add the concealment library's memory requirements */
-            dec_clr_op->u4_num_mem_rec_filled += cncl_fill_op.s_ivd_fill_mem_rec_op_t.u4_num_mem_rec_filled;
-        }
-    }
-#endif //APPLY_CONCEALMENT
-    DEBUG("Retrieve num mem recs: %d\n",
-                    dec_clr_op->u4_num_mem_rec_filled);
-    STATS_PRINT();
-    ihevcd_jobq_free((jobq_t *)ps_codec->pv_proc_jobq);
-
-
-
-    return IV_SUCCESS;
-
+    return ret;
 }
 /**
 *******************************************************************************
 *
 * @brief
+*  Delete codec
+*
+* @par Description:
+*  Delete codec
+*
+* @param[in] ps_codec_obj
+*  Pointer to codec object at API level
+*
+* @param[in] pv_api_ip
+*  Pointer to input argument structure
+*
+* @param[out] pv_api_op
+*  Pointer to output argument structure
+*
+* @returns  Status
+*
+* @remarks
+*
+*
+*******************************************************************************
+*/
+WORD32 ihevcd_delete(iv_obj_t *ps_codec_obj, void *pv_api_ip, void *pv_api_op)
+{
+    codec_t *ps_dec;
+    ihevcd_cxa_delete_ip_t *ps_ip = (ihevcd_cxa_delete_ip_t *)pv_api_ip;
+    ihevcd_cxa_delete_op_t *ps_op = (ihevcd_cxa_delete_op_t *)pv_api_op;
+
+    ps_dec = (codec_t *)(ps_codec_obj->pv_codec_handle);
+    UNUSED(ps_ip);
+    ps_op->s_ivd_delete_op_t.u4_error_code = 0;
+    ihevcd_free_dynamic_bufs(ps_dec);
+    ihevcd_free_static_bufs(ps_codec_obj);
+    return IV_SUCCESS;
+}
+
+
+/**
+*******************************************************************************
+*
+* @brief
 *  Passes display buffer from application to codec
 *
 * @par Description:
@@ -3155,31 +2162,38 @@
         pic_buf_t *ps_pic_buf;
         UWORD8 *pu1_buf;
         WORD32 buf_ret;
-        WORD32 strd;
-        strd = ps_codec->i4_strd;
-        if(0 == strd)
-            strd = ps_codec->i4_max_wd + PAD_WD;
+
+        UWORD8 *pu1_chroma_buf = NULL;
         num_bufs = MIN(num_bufs, BUF_MGR_MAX_CNT);
         ps_codec->i4_num_disp_bufs = num_bufs;
 
         ps_pic_buf = (pic_buf_t *)ps_codec->ps_pic_buf;
+
+        /* If color format is 420P, then allocate chroma buffers to hold semiplanar
+         * chroma data */
+        if(ps_codec->e_chroma_fmt == IV_YUV_420P)
+        {
+            WORD32 num_samples = ps_dec_disp_ip->s_disp_buffer[0].u4_min_out_buf_size[1] << 1;
+            WORD32 size = num_samples * num_bufs;
+            void *pv_mem_ctxt = ps_codec->pv_mem_ctxt;
+
+            pu1_chroma_buf = ps_codec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
+            RETURN_IF((NULL == pu1_chroma_buf), IV_FAIL);
+
+            ps_codec->pu1_cur_chroma_ref_buf = pu1_chroma_buf;
+        }
         for(i = 0; i < (WORD32)num_bufs; i++)
         {
+            /* Stride is not available in some cases here.
+               So store base pointers to buffer manager now,
+               and update these pointers once header is decoded */
             pu1_buf =  ps_dec_disp_ip->s_disp_buffer[i].pu1_bufs[0];
-            ps_pic_buf->pu1_luma = pu1_buf + strd * PAD_TOP + PAD_LEFT;
+            ps_pic_buf->pu1_luma = pu1_buf;
 
             if(ps_codec->e_chroma_fmt == IV_YUV_420P)
             {
-                pu1_buf =  ps_codec->pu1_cur_chroma_ref_buf;
-                ps_codec->pu1_cur_chroma_ref_buf += strd * (ps_codec->i4_ht / 2 + PAD_HT / 2);
-                ps_codec->i4_remaining_pic_buf_size -= strd * (ps_codec->i4_ht / 2 + PAD_HT / 2);
-
-                if(0 > ps_codec->i4_remaining_pic_buf_size)
-                {
-                    ps_codec->i4_error_code = IHEVCD_BUF_MGR_ERROR;
-                    return IHEVCD_BUF_MGR_ERROR;
-                }
-
+                pu1_buf = pu1_chroma_buf;
+                pu1_chroma_buf += ps_dec_disp_ip->s_disp_buffer[0].u4_min_out_buf_size[1] << 1;
             }
             else
             {
@@ -3187,7 +2201,7 @@
                 pu1_buf =  ps_dec_disp_ip->s_disp_buffer[i].pu1_bufs[1];
             }
 
-            ps_pic_buf->pu1_chroma = pu1_buf + strd * (PAD_TOP / 2) + PAD_LEFT;
+            ps_pic_buf->pu1_chroma = pu1_buf;
 
             buf_ret = ihevc_buf_mgr_add((buf_mgr_t *)ps_codec->pv_pic_buf_mgr, ps_pic_buf, i);
 
@@ -3330,7 +2344,9 @@
 
     for(i = 0; i < (WORD32)ps_ctl_op->u4_min_num_in_bufs; i++)
     {
-        ps_ctl_op->u4_min_in_buf_size[i] = MAX((ps_codec->i4_wd * ps_codec->i4_ht), MIN_BITSBUF_SIZE);
+        wd = ALIGN64(ps_codec->i4_wd);
+        ht = ALIGN64(ps_codec->i4_ht);
+        ps_ctl_op->u4_min_in_buf_size[i] = MAX((wd * ht), MIN_BITSBUF_SIZE);
     }
 
     wd = ps_codec->i4_wd;
@@ -3350,19 +2366,6 @@
             ht = ps_codec->i4_ht + PAD_HT;
         }
     }
-    else
-    {
-        if(0 == ps_codec->i4_share_disp_buf)
-        {
-            wd = ps_codec->i4_new_max_wd;
-            ht = ps_codec->i4_new_max_ht;
-        }
-        else
-        {
-            wd = ALIGN32(wd + PAD_WD);
-            ht += PAD_HT;
-        }
-    }
 
     if(ps_codec->i4_disp_strd > wd)
         wd = ps_codec->i4_disp_strd;
@@ -3371,36 +2374,22 @@
         ps_ctl_op->u4_num_disp_bufs = 1;
     else
     {
-        WORD32 pic_size;
-        WORD32 max_dpb_size;
-
         if(ps_codec->i4_sps_done)
         {
             sps_t *ps_sps = (ps_codec->s_parse.ps_sps_base + ps_codec->i4_sps_id);
-            WORD32 reorder_pic_cnt;
-            WORD32 ref_pic_cnt;
-            WORD32 level;
+            WORD32 reorder_pic_cnt, ref_pic_cnt;
+            reorder_pic_cnt = 0;
+            if(ps_codec->e_frm_out_mode != IVD_DECODE_FRAME_OUT)
+                reorder_pic_cnt = ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1];
+            ref_pic_cnt = ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1];
 
-            reorder_pic_cnt = MIN(ps_sps->ai1_sps_max_num_reorder_pics[0], ps_codec->i4_init_num_reorder);
-            pic_size = ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples;
-
-            level = ps_codec->i4_init_level;
-            max_dpb_size = ihevcd_get_dpb_size(level, pic_size);
-            ref_pic_cnt = max_dpb_size;
             ps_ctl_op->u4_num_disp_bufs = reorder_pic_cnt;
 
             ps_ctl_op->u4_num_disp_bufs += ref_pic_cnt + 1;
-
         }
         else
         {
-            pic_size = ps_codec->i4_max_wd * ps_codec->i4_max_ht;
-            max_dpb_size = ihevcd_get_dpb_size(ps_codec->i4_init_level, pic_size);
-            ps_ctl_op->u4_num_disp_bufs = 2 * max_dpb_size;
-
-            ps_ctl_op->u4_num_disp_bufs = MIN(ps_ctl_op->u4_num_disp_bufs,
-                            (UWORD32)(ps_codec->i4_init_num_ref + ps_codec->i4_init_num_reorder + 1));
-
+            ps_ctl_op->u4_num_disp_bufs = MAX_REF_CNT;
         }
 
         ps_ctl_op->u4_num_disp_bufs = MIN(
@@ -3515,11 +2504,14 @@
 
     for(i = 0; i < ps_ctl_op->u4_min_num_in_bufs; i++)
     {
-        ps_ctl_op->u4_min_in_buf_size[i] = MAX((ps_codec->i4_wd * ps_codec->i4_ht), MIN_BITSBUF_SIZE);
+        wd = ALIGN64(ps_codec->i4_wd);
+        ht = ALIGN64(ps_codec->i4_ht);
+
+        ps_ctl_op->u4_min_in_buf_size[i] = MAX((wd * ht), MIN_BITSBUF_SIZE);
     }
 
-    wd = ps_codec->i4_max_wd;
-    ht = ps_codec->i4_max_ht;
+    wd = 0;
+    ht = 0;
 
     if(ps_codec->i4_sps_done)
     {
@@ -3551,36 +2543,22 @@
         ps_ctl_op->u4_num_disp_bufs = 1;
     else
     {
-        WORD32 pic_size;
-        WORD32 max_dpb_size;
-
         if(ps_codec->i4_sps_done)
         {
             sps_t *ps_sps = (ps_codec->s_parse.ps_sps_base + ps_codec->i4_sps_id);
-            WORD32 reorder_pic_cnt;
-            WORD32 ref_pic_cnt;
-            WORD32 level;
+            WORD32 reorder_pic_cnt, ref_pic_cnt;
+            reorder_pic_cnt = 0;
+            if(ps_codec->e_frm_out_mode != IVD_DECODE_FRAME_OUT)
+                reorder_pic_cnt = ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1];
+            ref_pic_cnt = ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1];
 
-            reorder_pic_cnt = MIN(ps_sps->ai1_sps_max_num_reorder_pics[0], ps_codec->i4_init_num_reorder);
-            pic_size = ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples;
-
-            level = ps_codec->i4_init_level;
-            max_dpb_size = ihevcd_get_dpb_size(level, pic_size);
-            ref_pic_cnt = max_dpb_size;
             ps_ctl_op->u4_num_disp_bufs = reorder_pic_cnt;
 
             ps_ctl_op->u4_num_disp_bufs += ref_pic_cnt + 1;
-
         }
         else
         {
-            pic_size = ps_codec->i4_max_wd * ps_codec->i4_max_ht;
-            max_dpb_size = ihevcd_get_dpb_size(ps_codec->i4_init_level, pic_size);
-            ps_ctl_op->u4_num_disp_bufs = 2 * max_dpb_size;
-
-            ps_ctl_op->u4_num_disp_bufs = MIN(ps_ctl_op->u4_num_disp_bufs,
-                            (UWORD32)(ps_codec->i4_init_num_ref + ps_codec->i4_init_num_reorder + 1));
-
+            ps_ctl_op->u4_num_disp_bufs = MAX_REF_CNT;
         }
 
         ps_ctl_op->u4_num_disp_bufs = MIN(
@@ -3690,18 +2668,8 @@
     }
 
 
-    if((-1 != (WORD32)s_ctl_dynparams_ip->u4_disp_wd) &&
-                    (0  != s_ctl_dynparams_ip->u4_disp_wd) &&
-                    (0  != strd) &&
-                    ((WORD32)s_ctl_dynparams_ip->u4_disp_wd < strd))
     {
-        s_ctl_dynparams_op->u4_error_code |= (1 << IVD_UNSUPPORTEDPARAM);
-        s_ctl_dynparams_op->u4_error_code |= IHEVCD_INVALID_DISP_STRD;
-        ret = IV_FAIL;
-    }
-    else
-    {
-        if((WORD32)s_ctl_dynparams_ip->u4_disp_wd >= ps_codec->i4_wd)
+        if((WORD32)s_ctl_dynparams_ip->u4_disp_wd >= ps_codec->i4_disp_wd)
         {
             strd = s_ctl_dynparams_ip->u4_disp_wd;
         }
@@ -3740,6 +2708,15 @@
         ret = IV_FAIL;
     }
 
+    ps_codec->e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
+
+    if((s_ctl_dynparams_ip->e_frm_out_mode != IVD_DECODE_FRAME_OUT) &&
+       (s_ctl_dynparams_ip->e_frm_out_mode != IVD_DISPLAY_FRAME_OUT))
+    {
+        s_ctl_dynparams_op->u4_error_code = (1 << IVD_UNSUPPORTEDPARAM);
+        ret = IV_FAIL;
+    }
+    ps_codec->e_frm_out_mode = s_ctl_dynparams_ip->e_frm_out_mode;
 
     return ret;
 
@@ -3948,8 +2925,8 @@
     else
     {
 
-        disp_wd = ps_codec->i4_max_wd;
-        disp_ht = ps_codec->i4_max_ht;
+        disp_wd = 0;
+        disp_ht = 0;
 
         if(0 == ps_codec->i4_share_disp_buf)
         {
@@ -4455,17 +3432,11 @@
 
     switch(command)
     {
-        case IV_CMD_GET_NUM_MEM_REC:
-            ret = ihevcd_get_num_rec((void *)pv_api_ip, (void *)pv_api_op);
-
+        case IVD_CMD_CREATE:
+            ret = ihevcd_create(ps_handle, (void *)pv_api_ip, (void *)pv_api_op);
             break;
-        case IV_CMD_FILL_NUM_MEM_REC:
-
-            ret = ihevcd_fill_num_mem_rec((void *)pv_api_ip, (void *)pv_api_op);
-            break;
-        case IV_CMD_INIT:
-            ret = ihevcd_init_mem_rec(ps_handle, (void *)pv_api_ip,
-                                      (void *)pv_api_op);
+        case IVD_CMD_DELETE:
+            ret = ihevcd_delete(ps_handle, (void *)pv_api_ip, (void *)pv_api_op);
             break;
 
         case IVD_CMD_VIDEO_DECODE:
@@ -4487,11 +3458,6 @@
                                            (void *)pv_api_op);
             break;
 
-        case IV_CMD_RETRIEVE_MEMREC:
-            ret = ihevcd_retrieve_memrec(ps_handle, (void *)pv_api_ip,
-                                         (void *)pv_api_op);
-            break;
-
         case IVD_CMD_VIDEO_CTL:
             ret = ihevcd_ctl(ps_handle, (void *)pv_api_ip, (void *)pv_api_op);
             break;
diff --git a/decoder/ihevcd_cxa.h b/decoder/ihevcd_cxa.h
index 7c71271..c23d1e7 100644
--- a/decoder/ihevcd_cxa.h
+++ b/decoder/ihevcd_cxa.h
@@ -140,159 +140,37 @@
 /* Extended Structures                                                       */
 /*****************************************************************************/
 
+
 /*****************************************************************************/
-/*  Get Number of Memory Records                                             */
+/*  Delete Codec                                                             */
 /*****************************************************************************/
 
+typedef struct {
+    ivd_delete_ip_t               s_ivd_delete_ip_t;
+}ihevcd_cxa_delete_ip_t;
+
 
 typedef struct {
-
-    /**
-     * ivd_num_mem_rec_ip_t
-     */
-    iv_num_mem_rec_ip_t                    s_ivd_num_mem_rec_ip_t;
-}ihevcd_cxa_num_mem_rec_ip_t;
-
-
-typedef struct {
-
-    /**
-     * ivd_num_mem_rec_op_t
-     */
-    iv_num_mem_rec_op_t                    s_ivd_num_mem_rec_op_t;
-}ihevcd_cxa_num_mem_rec_op_t;
-
-
-/*****************************************************************************/
-/*  Fill Memory Records                                                      */
-/*****************************************************************************/
-
-
-typedef struct {
-    /**
-     * ivd_fill_mem_rec_ip_t
-     */
-    iv_fill_mem_rec_ip_t                    s_ivd_fill_mem_rec_ip_t;
-
-    /**
-     * level
-     */
-    WORD32                                  i4_level;
-
-    /**
-     * num_reorder_frames
-     */
-    UWORD32                                 u4_num_reorder_frames;
-
-    /**
-     * num_ref_frames
-     */
-    UWORD32                                 u4_num_ref_frames;
-
-    /**
-     * share_disp_buf
-     */
-    UWORD32                                 u4_share_disp_buf;
-
-    /**
-     * format in which codec has to give out frame data for display
-     */
-    IV_COLOR_FORMAT_T                       e_output_format;
-
-    /**
-     * Number of extra display buffers that will be allocated to handle display pipeline depth
-     */
-    UWORD32                                 u4_num_extra_disp_buf;
-
-}ihevcd_cxa_fill_mem_rec_ip_t;
-
-
-typedef struct {
-
-    /**
-     * ivd_fill_mem_rec_op_t
-     */
-
-    iv_fill_mem_rec_op_t                   s_ivd_fill_mem_rec_op_t;
-
-}ihevcd_cxa_fill_mem_rec_op_t;
-
-/*****************************************************************************/
-/*  Retrieve Memory Records                                                  */
-/*****************************************************************************/
-
-
-typedef struct {
-
-    /**
-     * ivd_retrieve_mem_rec_ip_t
-     */
-    iv_retrieve_mem_rec_ip_t               s_ivd_retrieve_mem_rec_ip_t;
-}ihevcd_cxa_retrieve_mem_rec_ip_t;
-
-
-typedef struct {
-
-    /**
-     * ivd_retrieve_mem_rec_op_t
-     */
-    iv_retrieve_mem_rec_op_t               s_ivd_retrieve_mem_rec_op_t;
-}ihevcd_cxa_retrieve_mem_rec_op_t;
-
+    ivd_delete_op_t               s_ivd_delete_op_t;
+}ihevcd_cxa_delete_op_t;
 
 /*****************************************************************************/
 /*   Initialize decoder                                                      */
 /*****************************************************************************/
 
-
 typedef struct {
-
-    /**
-     * ivd_init_ip_t
-     */
-    ivd_init_ip_t                           s_ivd_init_ip_t;
-
-    /**
-     * level
-     */
-    WORD32                                  i4_level;
-
-    /**
-     * num_reorder_frames
-     */
-    UWORD32                                 u4_num_reorder_frames;
-
-    /**
-     * num_ref_frames
-     */
-    UWORD32                                 u4_num_ref_frames;
-
-    /**
-     * share_disp_buf
-     */
-    UWORD32                                 u4_share_disp_buf;
-
-    /**
-     * Number of extra display buffers that will be allocated to handle display pipeline depth
-     */
-    UWORD32                                 u4_num_extra_disp_buf;
-}ihevcd_cxa_init_ip_t;
+    ivd_create_ip_t                         s_ivd_create_ip_t;
+}ihevcd_cxa_create_ip_t;
 
 
 typedef struct {
-
-    /**
-     * ivd_init_op_t
-     */
-    ivd_init_op_t                           s_ivd_init_op_t;
-}ihevcd_cxa_init_op_t;
-
+    ivd_create_op_t                         s_ivd_create_op_t;
+}ihevcd_cxa_create_op_t;
 
 /*****************************************************************************/
 /*   Video Decode                                                            */
 /*****************************************************************************/
 
-
 typedef struct {
 
     /**
@@ -315,7 +193,6 @@
 /*   Get Display Frame                                                       */
 /*****************************************************************************/
 
-
 typedef struct
 {
     /**
diff --git a/decoder/ihevcd_decode.c b/decoder/ihevcd_decode.c
index 94de640..954f8f3 100644
--- a/decoder/ihevcd_decode.c
+++ b/decoder/ihevcd_decode.c
@@ -89,6 +89,9 @@
                                WORD32 cur_row,
                                WORD32 num_rows);
 WORD32 ihevcd_init(codec_t *ps_codec);
+
+WORD32 ihevcd_allocate_dynamic_bufs(codec_t *ps_codec);
+WORD32 ihevcd_free_dynamic_bufs(codec_t *ps_codec);
 /*****************************************************************************/
 /* Function Prototypes                                                       */
 /*****************************************************************************/
@@ -183,12 +186,7 @@
     ps_dec_op->u4_error_code = ihevcd_map_error((IHEVCD_ERROR_T)ps_codec->i4_error_code);
     ps_dec_op->u4_num_bytes_consumed = ps_dec_ip->u4_num_Bytes
                     - ps_codec->i4_bytes_remaining;
-    if(ps_codec->i4_error_code == IHEVCD_UNSUPPORTED_DIMENSIONS)
-    {
-        ps_dec_op->u4_pic_wd = ps_codec->i4_new_max_wd;
-        ps_dec_op->u4_pic_ht = ps_codec->i4_new_max_ht;
-    }
-    else if(ps_codec->i4_sps_done)
+    if(ps_codec->i4_sps_done)
     {
         ps_dec_op->u4_pic_wd = ps_codec->i4_disp_wd;
         ps_dec_op->u4_pic_ht = ps_codec->i4_disp_ht;
@@ -244,14 +242,25 @@
             }
             else
             {
-                ps_dec_op->s_disp_frm_buf.pv_u_buf =
-                                ps_dec_ip->s_out_buffer.pu1_bufs[1];
-                ps_dec_op->s_disp_frm_buf.pv_v_buf =
-                                ps_dec_ip->s_out_buffer.pu1_bufs[2];
+                WORD32 i;
+                UWORD8 *pu1_u_dst = NULL, *pu1_v_dst = NULL;
+                for(i = 0; i < ps_codec->i4_share_disp_buf_cnt; i++)
+                {
+                    WORD32 diff = ps_disp_buf->pu1_luma - ps_codec->s_disp_buffer[i].pu1_bufs[0];
+                    if(diff == (ps_codec->i4_strd * PAD_TOP + PAD_LEFT))
+                    {
+                        pu1_u_dst = ps_codec->s_disp_buffer[i].pu1_bufs[1];
+                        pu1_u_dst += (ps_codec->i4_strd * PAD_TOP) / 4 + (PAD_LEFT / 2);
 
+                        pu1_v_dst = ps_codec->s_disp_buffer[i].pu1_bufs[2];
+                        pu1_v_dst += (ps_codec->i4_strd * PAD_TOP) / 4 + (PAD_LEFT / 2);
+                        break;
+                    }
+                }
+                ps_dec_op->s_disp_frm_buf.pv_u_buf = pu1_u_dst;
+                ps_dec_op->s_disp_frm_buf.pv_v_buf = pu1_v_dst;
             }
             ps_dec_op->s_disp_frm_buf.u4_y_strd = ps_codec->i4_strd;
-
         }
         else
         {
@@ -554,6 +563,17 @@
                 ps_codec->i4_slice_error = 0;
         }
 
+        if(ps_codec->pu1_bitsbuf_dynamic)
+        {
+            ps_codec->pu1_bitsbuf = ps_codec->pu1_bitsbuf_dynamic;
+            ps_codec->u4_bitsbuf_size = ps_codec->u4_bitsbuf_size_dynamic;
+        }
+        else
+        {
+            ps_codec->pu1_bitsbuf = ps_codec->pu1_bitsbuf_static;
+            ps_codec->u4_bitsbuf_size = ps_codec->u4_bitsbuf_size_static;
+        }
+
         nal_ofst = ihevcd_nal_search_start_code(ps_codec->pu1_inp_bitsbuf,
                                                 ps_codec->i4_bytes_remaining);
 
@@ -566,6 +586,13 @@
                                         ps_codec->pu1_bitsbuf,
                                         bytes_remaining,
                                         &nal_len, &bits_len);
+
+            /* Decoder may read upto 8 extra bytes at the end of frame */
+            /* These are not used, but still set them to zero to avoid uninitialized reads */
+            if(bits_len < (WORD32)(ps_codec->u4_bitsbuf_size - 8))
+            {
+                memset(ps_codec->pu1_bitsbuf + bits_len, 0, 2 * sizeof(UWORD32));
+            }
         }
         /* This may be used to update the offsets for tiles and entropy sync row offsets */
         ps_codec->i4_num_emln_bytes = nal_len - bits_len;
@@ -637,6 +664,23 @@
             break;
         }
 
+        /* Allocate dynamic bitstream buffer once SPS is decoded */
+        if((ps_codec->u4_allocate_dynamic_done == 0) && ps_codec->i4_sps_done)
+        {
+            WORD32 ret;
+            ret = ihevcd_allocate_dynamic_bufs(ps_codec);
+            if(ret != IV_SUCCESS)
+            {
+                /* Free any dynamic buffers that are allocated */
+                ihevcd_free_dynamic_bufs(ps_codec);
+                ps_codec->i4_error_code = IVD_MEM_ALLOC_FAILED;
+                ps_dec_op->u4_error_code |= 1 << IVD_FATALERROR;
+                ps_dec_op->u4_error_code |= IVD_MEM_ALLOC_FAILED;
+
+                return IV_FAIL;
+            }
+        }
+
         BREAK_AFTER_SLICE_NAL();
     }
 
diff --git a/decoder/ihevcd_mv_merge.c b/decoder/ihevcd_mv_merge.c
index 9ec3029..08cec43 100644
--- a/decoder/ihevcd_mv_merge.c
+++ b/decoder/ihevcd_mv_merge.c
@@ -309,7 +309,7 @@
             cur_poc = ps_slice_hdr->i4_abs_pic_order_cnt;
 
             slice_idx = *(ps_mv_buf_col->pu1_pic_slice_map + col_ctb_x + col_ctb_y * ps_sps->i2_pic_wd_in_ctb);
-
+            slice_idx &= (MAX_SLICE_HDR_CNT - 1);
             if(au4_list_col[0] == 0)
             {
                 col_ref_poc_l0 =
diff --git a/decoder/ihevcd_parse_headers.c b/decoder/ihevcd_parse_headers.c
index 3ed70fb..ecc271a 100644
--- a/decoder/ihevcd_parse_headers.c
+++ b/decoder/ihevcd_parse_headers.c
@@ -1263,16 +1263,6 @@
     ps_sps->i2_pic_width_in_luma_samples = ALIGN8(ps_sps->i2_pic_width_in_luma_samples);
     ps_sps->i2_pic_height_in_luma_samples = ALIGN8(ps_sps->i2_pic_height_in_luma_samples);
 
-    if((ps_sps->i2_pic_width_in_luma_samples > ps_codec->i4_max_wd) ||
-       (ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples >
-                       ps_codec->i4_max_wd * ps_codec->i4_max_ht) ||
-       (ps_sps->i2_pic_height_in_luma_samples > MAX(ps_codec->i4_max_wd, ps_codec->i4_max_ht)))
-    {
-        ps_codec->i4_new_max_wd = ps_sps->i2_pic_width_in_luma_samples;
-        ps_codec->i4_new_max_ht = ps_sps->i2_pic_height_in_luma_samples;
-        return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_DIMENSIONS;
-    }
-
     BITS_PARSE("pic_cropping_flag", value, ps_bitstrm, 1);
     ps_sps->i1_pic_cropping_flag = value;
 
@@ -1917,10 +1907,13 @@
     WORD32 scaling_mat_size;
     tile_t *ps_tile_backup;
     WORD32 max_tile_cols, max_tile_rows;
+    WORD32 wd, ht;
+    wd = ALIGN64(ps_codec->i4_wd);
+    ht = ALIGN64(ps_codec->i4_ht);
 
     SCALING_MAT_SIZE(scaling_mat_size);
-    max_tile_cols = (ps_codec->i4_max_wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
-    max_tile_rows = (ps_codec->i4_max_ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
+    max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
+    max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
 
     ps_pps_ref = ps_codec->ps_pps_base + pps_id_ref;
     ps_pps = ps_codec->ps_pps_base + pps_id;
diff --git a/decoder/ihevcd_parse_residual.c b/decoder/ihevcd_parse_residual.c
index fc84fa3..6e2a39c 100644
--- a/decoder/ihevcd_parse_residual.c
+++ b/decoder/ihevcd_parse_residual.c
@@ -191,7 +191,7 @@
     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
     WORD32 last_significant_coeff_x_prefix, last_significant_coeff_y_prefix;
     WORD32 last_significant_coeff_x, last_significant_coeff_y;
-    const UWORD8 *pu1_scan_blk, *pu1_scan_coeff;
+    const UWORD8 *pu1_scan_blk = NULL, *pu1_scan_coeff;
     WORD32 scan_idx;
     WORD32 i;
     WORD32 sign_data_hiding_flag;
@@ -387,7 +387,8 @@
 
         last_scan_pos = pu1_scan_coeff[scan_pos];
     }
-    pu1_scan_blk = (UWORD8 *)gapv_ihevc_invscan[scan_idx * 3 + (log2_trafo_size - 2 - 1)];
+    if(log2_trafo_size > 2)
+        pu1_scan_blk = (UWORD8 *)gapv_ihevc_invscan[scan_idx * 3 + (log2_trafo_size - 2 - 1)];
     pu1_scan_coeff  = &gau1_ihevc_invscan4x4[scan_idx][0];
 
     /* Set CSBF array to zero */
diff --git a/decoder/ihevcd_parse_slice_header.c b/decoder/ihevcd_parse_slice_header.c
index 2afa4d2..e9c3073 100644
--- a/decoder/ihevcd_parse_slice_header.c
+++ b/decoder/ihevcd_parse_slice_header.c
@@ -419,10 +419,13 @@
             {
                 WORD32 numbits;
 
-                numbits = 32 - CLZ(ps_sps->i1_num_short_term_ref_pic_sets - 1);
-                BITS_PARSE("short_term_ref_pic_set_idx", value, ps_bitstrm, numbits);
-                ps_slice_hdr->i1_short_term_ref_pic_set_idx = value;
-                ps_slice_hdr->i1_short_term_ref_pic_set_idx = CLIP3(ps_slice_hdr->i1_short_term_ref_pic_set_idx, 0, MAX_STREF_PICS_SPS - 1);
+                ps_slice_hdr->i1_short_term_ref_pic_set_idx = 0;
+                if(ps_sps->i1_num_short_term_ref_pic_sets > 1)
+                {
+                    numbits = 32 - CLZ(ps_sps->i1_num_short_term_ref_pic_sets - 1);
+                    BITS_PARSE("short_term_ref_pic_set_idx", value, ps_bitstrm, numbits);
+                    ps_slice_hdr->i1_short_term_ref_pic_set_idx = value;
+                }
 
                 st_rps_idx = ps_slice_hdr->i1_short_term_ref_pic_set_idx;
                 num_neg_pics = ps_sps->as_stref_picset[st_rps_idx].i1_num_neg_pics;
diff --git a/decoder/ihevcd_structs.h b/decoder/ihevcd_structs.h
index 1e6bc20..ce0653e 100644
--- a/decoder/ihevcd_structs.h
+++ b/decoder/ihevcd_structs.h
@@ -196,21 +196,21 @@
     /**
      * Absolute POCs of reference List 0 for all slices in the frame from which this frame is reconstructed
      */
-    WORD32 ai4_l0_collocated_poc[MAX_SLICE_SEGMENTS_IN_FRAME][MAX_DPB_SIZE];
+    WORD32 ai4_l0_collocated_poc[MAX_SLICE_HDR_CNT][MAX_DPB_SIZE];
 
     /**
      * Flag to indicate Long Term reference for POCs of reference List 0 for all slices in the frame from which this frame is reconstructed
      */
-    WORD8 ai1_l0_collocated_poc_lt[MAX_SLICE_SEGMENTS_IN_FRAME][MAX_DPB_SIZE];
+    WORD8 ai1_l0_collocated_poc_lt[MAX_SLICE_HDR_CNT][MAX_DPB_SIZE];
 
     /**
      * Absolute POCs of reference List 1 for all slices in the frame from which this frame is reconstructed
      */
-    WORD32 ai4_l1_collocated_poc[MAX_SLICE_SEGMENTS_IN_FRAME][MAX_DPB_SIZE];
+    WORD32 ai4_l1_collocated_poc[MAX_SLICE_HDR_CNT][MAX_DPB_SIZE];
     /**
      * Flag to indicate Long Term reference for POCs of reference List 1 for all slices in the frame from which this frame is reconstructed
      */
-    WORD8 ai1_l1_collocated_poc_lt[MAX_SLICE_SEGMENTS_IN_FRAME][MAX_DPB_SIZE];
+    WORD8 ai1_l1_collocated_poc_lt[MAX_SLICE_HDR_CNT][MAX_DPB_SIZE];
 
 }mv_buf_t;
 
@@ -1627,16 +1627,6 @@
 struct _codec_t
 {
     /**
-     * Max width the codec can support
-     */
-    WORD32 i4_max_wd;
-
-    /**
-     * Max height the codec can support
-     */
-    WORD32 i4_max_ht;
-
-    /**
      * Width : pic_width_in_luma_samples
      */
     WORD32 i4_wd;
@@ -1661,21 +1651,6 @@
      */
     WORD32 i4_disp_strd;
 
-    /*
-     * In case stream width/height is greater than max_wd/max_ht used during init,
-     * it is stored in the following and in order to decode the current stream
-     * decoder has to be recreated with these dimensions.
-     */
-    /**
-     * Stream width if it is greater than i4_max_wd
-     */
-    WORD32 i4_new_max_wd;
-
-    /**
-     * Stream height if it is greater than i4_max_ht
-     */
-    WORD32 i4_new_max_ht;
-
     /**
      * Stride of reference buffers.
      * For shared mode even display buffer will use the same stride
@@ -1683,26 +1658,6 @@
     WORD32 i4_strd;
 
     /**
-     * Level specified during init
-     */
-    WORD32 i4_init_level;
-
-    /**
-     * number of reference frames specified during init
-     */
-    WORD32 i4_init_num_ref;
-
-    /**
-     * number of reorder frames specified during init
-     */
-    WORD32 i4_init_num_reorder;
-
-    /**
-     * Number of extra display buffers allocated by application
-     */
-    WORD32 i4_init_num_extra_disp_buf;
-
-    /**
      * Number of cores to be used
      */
     WORD32 i4_num_cores;
@@ -1904,19 +1859,36 @@
     UWORD8 *pu1_bitsbuf;
 
     /**
+     * Pointer to static bitstream after emulation prevention
+     * This is a fixed size buffer used initially till SPS is decoded
+     */
+    UWORD8 *pu1_bitsbuf_static;
+
+    /**
+     * Pointer to dynamic bitstream after emulation prevention
+     * This is allocated after SPS is done, based on width and height
+     */
+    UWORD8 *pu1_bitsbuf_dynamic;
+
+    /**
      * Size of intermediate bitstream buffer
      */
     UWORD32 u4_bitsbuf_size;
 
     /**
+     * Size of intermediate static bitstream buffer
+     */
+    UWORD32 u4_bitsbuf_size_static;
+
+    /**
+     * Size of intermediate dynamic bitstream buffer
+     */
+    UWORD32 u4_bitsbuf_size_dynamic;
+
+    /**
      * Pointer to hold TU data for a set of CTBs or a picture
      */
     void *pv_tu_data;
-    /**
-     * Holds mem records passed during init.
-     * This will be used to return the mem records during retrieve call
-     */
-    iv_mem_rec_t *ps_mem_rec_backup;
 
     /**
      * Process Job queue buffer base
@@ -1937,6 +1909,12 @@
      * Current pictures intra mode map at 8x8 level
      */
     UWORD8 *pu1_pic_intra_flag;
+
+    /**
+     * No LPF buffer base
+     */
+    UWORD8 *pu1_pic_no_loop_filter_flag_base;
+
     /**
      * Current pictures loop filter flag map at 8x8 level
      */
@@ -1982,11 +1960,6 @@
     WORD32 i4_total_pic_buf_size;
 
     /**
-     * Remaining pic buffer size - used for shared mode with 420p support
-     */
-    WORD32 i4_remaining_pic_buf_size;
-
-    /**
      * Current chroma buffer base - used for shared mode with 420p output
      */
     UWORD8 *pu1_cur_chroma_ref_buf;
@@ -2124,6 +2097,28 @@
 
     /** Mask used to change MVs to full pel when configured to run in reduced complexity mode */
     WORD32 i4_mv_frac_mask;
+
+    /** Memory holding tile indices */
+    UWORD8 *pu1_tile_idx_base;
+
+    /** Callback for aligned allocation */
+    void *(*pf_aligned_alloc)(void *pv_mem_ctxt, WORD32 alignment, WORD32 size);
+
+    /** Callback for aligned free */
+    void (*pf_aligned_free)(void *pv_mem_ctxt, void *pv_buf);
+
+    /** Memory context passed from application */
+    void *pv_mem_ctxt;
+
+    /** Base address of reference buffrers allocated */
+    UWORD8 *pu1_ref_pic_buf_base;
+
+    /** Flag to indicate if dynamic buffers are allocated */
+    UWORD32 u4_allocate_dynamic_done;
+
+    /** Flag to signal display order */
+    IVD_DISPLAY_FRAME_OUT_MODE_T  e_frm_out_mode;
+
     /**  Funtion pointers for inter_pred leaf level functions */
     pf_inter_pred apf_inter_pred[22];
 
diff --git a/decoder/ihevcd_utils.c b/decoder/ihevcd_utils.c
index dfd8806..4587694 100644
--- a/decoder/ihevcd_utils.c
+++ b/decoder/ihevcd_utils.c
@@ -163,59 +163,6 @@
 *******************************************************************************
 *
 * @brief
-*  Used to get DPB size for a given level, and number of luma samples
-*
-* @par Description:
-*  For given width, height and level number of max_dpb_size is computed as per
-*  Annex A.4.1
-*
-* @param[in] level
-*  Level of the stream
-*
-* @param[in] pic_size
-*  Width * Height
-*
-* @returns  Number of buffers in DPB
-*
-* @remarks
-*
-*
-*******************************************************************************
-*/
-WORD32 ihevcd_get_dpb_size(WORD32 level, WORD32 pic_size)
-{
-
-    WORD32 max_luma_samples;
-
-    WORD32 max_dpb_size;
-    WORD32 lvl_idx = ihevcd_get_lvl_idx(level);
-    max_luma_samples = gai4_ihevc_max_luma_pic_size[lvl_idx];
-
-
-
-    if(pic_size <= (max_luma_samples >> 2))
-    {
-        max_dpb_size = MIN(4 * MAX_DPB_PIC_BUF, 16);
-    }
-    else if(pic_size <= (max_luma_samples >> 1))
-    {
-        max_dpb_size = MIN(2 * MAX_DPB_PIC_BUF, 16);
-    }
-    else if(pic_size <= ((3 * max_luma_samples) >> 2))
-    {
-        max_dpb_size = MIN((4 * MAX_DPB_PIC_BUF) / 3, 16);
-    }
-    else
-    {
-        max_dpb_size = MAX_DPB_PIC_BUF;
-    }
-
-    return max_dpb_size;
-}
-/**
-*******************************************************************************
-*
-* @brief
 *  Used to get reference picture buffer size for a given level and
 *  and padding used
 *
@@ -242,85 +189,37 @@
 *
 *******************************************************************************
 */
-WORD32 ihevcd_get_total_pic_buf_size(WORD32 pic_size,
-                                     WORD32 level,
-                                     WORD32 horz_pad,
-                                     WORD32 vert_pad,
-                                     WORD32 init_num_bufs,
-                                     WORD32 init_extra_bufs,
-                                     WORD32 chroma_only)
+WORD32 ihevcd_get_total_pic_buf_size(codec_t *ps_codec,
+                                     WORD32 wd,
+                                     WORD32 ht)
 {
     WORD32 size;
     WORD32 num_luma_samples;
-    WORD32 lvl_idx;
-    WORD32 max_wd, min_ht;
     WORD32 max_dpb_size;
     WORD32 num_samples;
-    WORD32 max_num_bufs;
-    WORD32 pad = MAX(horz_pad, vert_pad);
 
 
+    sps_t *ps_sps = (ps_codec->s_parse.ps_sps_base + ps_codec->i4_sps_id);
+
     /* Get maximum number of buffers for the current picture size */
-    max_dpb_size = ihevcd_get_dpb_size(level, pic_size);
+    max_dpb_size = ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1];
 
+    if(ps_codec->e_frm_out_mode != IVD_DECODE_FRAME_OUT)
+        max_dpb_size += ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1];
 
-    max_num_bufs = (2 * max_dpb_size + 1);
-    /* If num_ref_frames and num_reorder_frmaes is specified
-     * Use minimum value
-     */
-    max_num_bufs = MIN(max_num_bufs, init_num_bufs);
-
-    /*
-     * Add extra buffers if required
-     */
-    max_num_bufs += init_extra_bufs;
-    max_num_bufs = MIN(max_num_bufs, BUF_MGR_MAX_CNT);
-
-    /* Get level index */
-    lvl_idx = ihevcd_get_lvl_idx(level);
-
-
-    /* Maximum width of luma samples in a picture at given level */
-    max_wd = ALIGN64(gai4_ihevc_max_wd_ht[lvl_idx]);
-
-    /* Minimum height of luma samples in a picture at given level */
-    min_ht = ALIGN64(gai4_ihevc_min_wd_ht[lvl_idx]);
-
-    /* Use max_wd and min_ht to get maximum number of luma samples for given level */
-    /* Because max_wd and min_ht are aligned to 64, product will be higher than the
-     * value given by the spec for a given level
-     */
-    num_luma_samples = max_wd * min_ht;
-
+    max_dpb_size++;
     /* Allocation is required for
      * (Wd + horz_pad) * (Ht + vert_pad) * (2 * max_dpb_size + 1)
-     *
-     * Above expanded as
-     * ((Wd * Ht) + (horz_pad * vert_pad) + Wd * vert_pad + Ht * horz_pad) * (2 * max_dpb_size + 1)
-     * (Wd * Ht) * (2 * max_dpb_size + 1) + ((horz_pad * vert_pad) + Wd * vert_pad + Ht * horz_pad) * (2 * max_dpb_size + 1)
-     * Now  max_dpb_size increases with smaller Wd and Ht, but Wd * ht * max_dpb_size will still be lesser or equal to max_wd * max_ht * dpb_size
-     *
-     * In the above equation (Wd * Ht) * (2 * max_dpb_size + 1) is accounted by using num_samples * (2 * max_dpb_size + 1) below
-     *
-     * For the padded area use MAX(horz_pad, vert_pad) as pad
-     * ((pad * pad) + pad * (Wd + Ht)) * (2 * max_dpb_size + 1) has to accounted from the above for padding
-     *
-     * Since Width and Height can change worst Wd + Ht is when One of the dimensions is max and other is min
-     * So use max_wd and min_ht
      */
 
     /* Account for padding area */
-
-    num_luma_samples += (pad * pad) + pad * (max_wd + min_ht);
+    num_luma_samples = (wd + PAD_WD) * (ht + PAD_HT);
 
     /* Account for chroma */
-    if(0 == chroma_only)
-        num_samples = num_luma_samples * 3 / 2;
-    else
-        num_samples = num_luma_samples / 2;
+    num_samples = num_luma_samples * 3 / 2;
 
     /* Number of bytes in reference pictures */
-    size = num_samples * max_num_bufs;
+    size = num_samples * max_dpb_size;
 
 
     return size;
@@ -561,43 +460,29 @@
     pic_buf_t *ps_pic_buf;
     WORD32 pic_buf_size_allocated;
 
-    WORD32 max_num_bufs;
-    WORD32 pic_size;
-    WORD32 level;
 
 
-    /* Initialize MV Bank buffer manager */
+
+    /* Initialize Pic buffer manager */
     ps_sps = ps_codec->s_parse.ps_sps;
 
-    pic_size = ps_sps->i2_pic_width_in_luma_samples *
-                    ps_sps->i2_pic_height_in_luma_samples;
+    /* Compute the number of Pic buffers needed */
+    max_dpb_size = ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1];
 
+    if(ps_codec->e_frm_out_mode != IVD_DECODE_FRAME_OUT)
+        max_dpb_size += ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1];
 
-    /* Compute the number of MB Bank buffers needed */
-    level = ps_codec->i4_init_level;
-    max_dpb_size = ihevcd_get_dpb_size(level, pic_size);
-    /* Allocate twice dpb size to handle worst case reorder without returning more
-     * than one output per call
-     */
-    max_dpb_size *= 2;
     /* Allocate one extra picture to handle current frame
      * In case of asynchronous parsing and processing, number of buffers should increase here
      * based on when parsing and processing threads are synchronized
      */
     max_dpb_size++;
 
-    /* If num_ref_frames and num_reorder_frmaes is specified
-     * Use minimum value
-     */
-    max_num_bufs = MIN(max_dpb_size, (ps_codec->i4_init_num_ref + ps_codec->i4_init_num_reorder + 1));
 
-
-    pu1_buf = (UWORD8 *)ps_codec->ps_pic_buf;
+    pu1_buf = (UWORD8 *)ps_codec->pu1_ref_pic_buf_base;
 
     ps_pic_buf = (pic_buf_t *)ps_codec->ps_pic_buf;
 
-    pu1_buf += BUF_MGR_MAX_CNT  * sizeof(pic_buf_t);
-
     /* In case of non-shared mode, add picture buffers to buffer manager
      * In case of shared mode buffers are added in the run-time
      */
@@ -606,8 +491,7 @@
         WORD32 buf_ret;
         WORD32 luma_samples;
         WORD32 chroma_samples;
-        pic_buf_size_allocated = ps_codec->i4_total_pic_buf_size -
-                        BUF_MGR_MAX_CNT * sizeof(pic_buf_t);
+        pic_buf_size_allocated = ps_codec->i4_total_pic_buf_size;
 
         luma_samples = (ps_codec->i4_strd) *
                         (ps_sps->i2_pic_height_in_luma_samples + PAD_HT);
@@ -618,18 +502,14 @@
         /* If the number of buffers that can be added is less than max_num_bufs
          * return with an error.
          */
-        for(i = 0; i < (2 * MAX_DPB_SIZE) + 1; i++)
+        for(i = 0; i < max_dpb_size; i++)
         {
             pic_buf_size_allocated -= (luma_samples + chroma_samples);
 
             if(pic_buf_size_allocated < 0)
             {
-                if(i < max_num_bufs)
-                {
-                    ps_codec->s_parse.i4_error_code = IHEVCD_INSUFFICIENT_MEM_PICBUF;
-                    return IHEVCD_INSUFFICIENT_MEM_PICBUF;
-                }
-                break;
+                ps_codec->s_parse.i4_error_code = IHEVCD_INSUFFICIENT_MEM_PICBUF;
+                return IHEVCD_INSUFFICIENT_MEM_PICBUF;
             }
 
             ps_pic_buf->pu1_luma = pu1_buf + ps_codec->i4_strd * PAD_TOP + PAD_LEFT;
@@ -640,6 +520,7 @@
 
             buf_ret = ihevc_buf_mgr_add((buf_mgr_t *)ps_codec->pv_pic_buf_mgr, ps_pic_buf, i);
 
+
             if(0 != buf_ret)
             {
                 ps_codec->s_parse.i4_error_code = IHEVCD_BUF_MGR_ERROR;
@@ -648,6 +529,26 @@
             ps_pic_buf++;
         }
     }
+    else
+    {
+        /* In case of shared mode, buffers are added without adjusting for padding.
+           Update luma and chroma pointers here to account for padding as per stride.
+           In some cases stride might not be available when set_display_frame is called.
+           Hence updated luma and chroma pointers here */
+
+        for(i = 0; i < BUF_MGR_MAX_CNT; i++)
+        {
+            ps_pic_buf = ihevc_buf_mgr_get_buf((buf_mgr_t *)ps_codec->pv_pic_buf_mgr, i);
+            if((NULL == ps_pic_buf) ||
+               (NULL == ps_pic_buf->pu1_luma) ||
+               (NULL == ps_pic_buf->pu1_chroma))
+            {
+                break;
+            }
+            ps_pic_buf->pu1_luma += ps_codec->i4_strd * PAD_TOP + PAD_LEFT;
+            ps_pic_buf->pu1_chroma += ps_codec->i4_strd * (PAD_TOP / 2) + PAD_LEFT;
+        }
+    }
 
     return ret;
 }
@@ -678,7 +579,7 @@
     WORD32 max_dpb_size;
     WORD32 mv_bank_size_allocated;
     WORD32 pic_mv_bank_size;
-    WORD32 level;
+
     sps_t *ps_sps;
     UWORD8 *pu1_buf;
     mv_buf_t *ps_mv_buf;
@@ -688,11 +589,8 @@
     ps_sps = ps_codec->s_parse.ps_sps;
 
 
-    /* Compute the number of MB Bank buffers needed */
-    level = ps_codec->i4_init_level;
-    max_dpb_size = ihevcd_get_dpb_size(level,
-                                       ps_sps->i2_pic_width_in_luma_samples *
-                                       ps_sps->i2_pic_height_in_luma_samples);
+    /* Compute the number of MV Bank buffers needed */
+    max_dpb_size = ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1];
 
     /* Allocate one extra MV Bank to handle current frame
      * In case of asynchronous parsing and processing, number of buffers should increase here
@@ -703,9 +601,9 @@
     pu1_buf = (UWORD8 *)ps_codec->pv_mv_bank_buf_base;
 
     ps_mv_buf = (mv_buf_t *)pu1_buf;
-    pu1_buf += (MAX_DPB_SIZE + 1) * sizeof(mv_buf_t);
+    pu1_buf += max_dpb_size * sizeof(mv_buf_t);
     ps_codec->ps_mv_buf = ps_mv_buf;
-    mv_bank_size_allocated = ps_codec->i4_total_mv_bank_size - (MAX_DPB_SIZE + 1) * sizeof(mv_buf_t);
+    mv_bank_size_allocated = ps_codec->i4_total_mv_bank_size - max_dpb_size  * sizeof(mv_buf_t);
 
     /* Compute MV bank size per picture */
     pic_mv_bank_size = ihevcd_get_pic_mv_bank_size(ps_sps->i2_pic_width_in_luma_samples *
@@ -1184,7 +1082,7 @@
     /* Get picture to be displayed if number of pictures decoded is more than max allowed reorder */
     /* Since the current will be decoded, check is fore >= instead of > */
     if(((WORD32)(ps_codec->u4_pic_cnt - ps_codec->u4_disp_cnt) >= ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1]) ||
-       ((WORD32)(ps_codec->u4_pic_cnt - ps_codec->u4_disp_cnt) >= ps_codec->i4_init_num_reorder))
+       (ps_codec->e_frm_out_mode == IVD_DECODE_FRAME_OUT))
 
     {
         ps_codec->ps_disp_buf = (pic_buf_t *)ihevc_disp_mgr_get((disp_mgr_t *)ps_codec->pv_disp_buf_mgr, &ps_codec->i4_disp_buf_id);
diff --git a/decoder/ihevcd_utils.h b/decoder/ihevcd_utils.h
index 7282ae7..893acaa 100644
--- a/decoder/ihevcd_utils.h
+++ b/decoder/ihevcd_utils.h
@@ -51,11 +51,7 @@
                                    WORD32 *pi4_ctb_tile_y,
                                    WORD32 *pi4_tile_idx);
 IHEVCD_ERROR_T ihevcd_parse_pic_init(codec_t *ps_codec);
-WORD32 ihevcd_get_total_pic_buf_size(WORD32 pic_size,
-                                     WORD32 level,
-                                     WORD32 horz_pad,
-                                     WORD32 vert_pad,
-                                     WORD32 init_num_bufs,
-                                     WORD32 init_extra_bufs,
-                                     WORD32 chroma_only);
+WORD32 ihevcd_get_total_pic_buf_size(codec_t *ps_codec,
+                                     WORD32 wd,
+                                     WORD32 ht);
 #endif /* _IHEVCD_UTILS_H_ */
diff --git a/decoder/ihevcd_version.c b/decoder/ihevcd_version.c
index b1b6e21..30499d6 100644
--- a/decoder/ihevcd_version.c
+++ b/decoder/ihevcd_version.c
@@ -62,7 +62,7 @@
  * Version string. First two digits signify major version and last two minor
  * Increment major version for API change or major feature update
  */
-#define CODEC_RELEASE_VER       "04.04"
+#define CODEC_RELEASE_VER       "05.00"
 /**
  * Vendor name
  */
@@ -73,20 +73,11 @@
 * Concatenates various strings to form a version string
 *******************************************************************************
 */
+#define MAXVERSION_STRLEN       511
 #define VERSION(version_string, codec_name, codec_release_type, codec_release_ver, codec_vendor)    \
-    strcpy(version_string,"@(#)Id:");                                                               \
-    strcat(version_string,codec_name);                                                              \
-    strcat(version_string,"_");                                                                     \
-    strcat(version_string,codec_release_type);                                                      \
-    strcat(version_string," Ver:");                                                                 \
-    strcat(version_string,codec_release_ver);                                                       \
-    strcat(version_string," Released by ");                                                         \
-    strcat(version_string,codec_vendor);                                                            \
-    strcat(version_string," Build: ");                                                              \
-    strcat(version_string,__DATE__);                                                                \
-    strcat(version_string," @ ");                                                                   \
-    strcat(version_string,__TIME__);
-
+    snprintf(version_string, MAXVERSION_STRLEN,                                                     \
+             "@(#)Id:%s_%s Ver:%s Released by %s Build: %s @ %s",                                   \
+             codec_name, codec_release_type, codec_release_ver, codec_vendor, __DATE__, __TIME__)
 
 /**
 *******************************************************************************
@@ -113,12 +104,13 @@
 IV_API_CALL_STATUS_T ihevcd_get_version(CHAR *pc_version_string,
                                         UWORD32 u4_version_buffer_size)
 {
-    CHAR ac_version_tmp[512];
+    CHAR ac_version_tmp[MAXVERSION_STRLEN + 1];
+    UWORD32 u4_len;
     VERSION(ac_version_tmp, CODEC_NAME, CODEC_RELEASE_TYPE, CODEC_RELEASE_VER, CODEC_VENDOR);
-
-    if(u4_version_buffer_size >= (strlen(ac_version_tmp) + 1))
+    u4_len = strnlen(ac_version_tmp, MAXVERSION_STRLEN) + 1;
+    if(u4_version_buffer_size >= u4_len)
     {
-        memcpy(pc_version_string, ac_version_tmp, (strlen(ac_version_tmp) + 1));
+        memcpy(pc_version_string, ac_version_tmp, u4_len);
         return IV_SUCCESS;
     }
     else
diff --git a/test/decoder/main.c b/test/decoder/main.c
index f102add..680d7c7 100644
--- a/test/decoder/main.c
+++ b/test/decoder/main.c
@@ -56,9 +56,7 @@
 #include "ihevcd_cxa.h"
 #include "ithread.h"
 
-
-//#define MD5_DISABLE
-#ifdef X86_MSVC
+#ifdef WINDOWS_TIMER
 #include <windows.h>
 #else
 #include <sys/time.h>
@@ -68,12 +66,14 @@
 #define NUM_DISPLAY_BUFFERS 4
 #define DEFAULT_FPS         30
 
-
 #define ENABLE_DEGRADE 0
 #define MAX_DISP_BUFFERS    64
 #define EXTRA_DISP_BUFFERS  0
 #define STRLENGTH 1000
 
+#define ADAPTIVE_TEST
+#define ADAPTIVE_MAX_WD 8192
+#define ADAPTIVE_MAX_HT 4096
 //#define TEST_FLUSH
 #define FLUSH_FRM_CNT 100
 
@@ -240,9 +240,9 @@
     void (*set_disp_buffers)(void *, WORD32, UWORD8 **, UWORD8 **, UWORD8 **);
     void (*disp_deinit)(void *);
     void (*disp_usleep)(UWORD32);
-    IV_COLOR_FORMAT_T(*get_color_fmt)(void);
-    UWORD32(*get_stride)(void);
-}vid_dec_ctx_t;
+    IV_COLOR_FORMAT_T (*get_color_fmt)(void);
+    UWORD32 (*get_stride)(void);
+} vid_dec_ctx_t;
 
 
 
@@ -266,9 +266,6 @@
     FULLSCREEN,
     FPS,
     TRACE,
-    MAX_WD,
-    MAX_HT,
-    MAX_LEVEL,
     CONFIG,
 
     DEGRADE_TYPE,
@@ -332,12 +329,6 @@
         "FPS to be used for display \n" },
     { "-i",  "--trace",                   TRACE,
         "Trace file\n" },
-    { "--", "--max_wd",      MAX_WD,
-        "Maximum width (Default: 2560) \n" },
-    { "--", "--max_ht",      MAX_HT,
-        "Maximum height (Default: 1600)\n" },
-    { "--", "--max_level",      MAX_LEVEL,
-        "Maximum Decoder Level (Default: 50)\n" },
     { "--",  "--arch", ARCH,
         "Set Architecture. Supported values  ARM_NONEON, ARM_A9Q, ARM_A7, ARM_A5, ARM_NEONINTR, X86_GENERIC, X86_SSSE3, X86_SSE4 \n" },
     { "--",  "--soc", SOC,
@@ -345,11 +336,6 @@
 };
 
 #define PEAK_WINDOW_SIZE            8
-#define MAX_FRAME_WIDTH             2560
-#define MAX_FRAME_HEIGHT            1600
-#define MAX_LEVEL_SUPPORTED         50
-#define MAX_REF_FRAMES              16
-#define MAX_REORDER_FRAMES          16
 #define DEFAULT_SHARE_DISPLAY_BUF   0
 #define STRIDE                      0
 #define DEFAULT_NUM_CORES           1
@@ -422,39 +408,45 @@
 /*                                                                           */
 /*****************************************************************************/
 
-void* ihevca_aligned_malloc(WORD32 alignment, WORD32 size)
+void *ihevca_aligned_malloc(void *pv_ctxt, WORD32 alignment, WORD32 i4_size)
 {
-    return (void *)_aligned_malloc(size, alignment);
+    (void)pv_ctxt;
+    return (void *)_aligned_malloc(i4_size, alignment);
 }
 
-void ihevca_aligned_free(void *pv_buf)
+void ihevca_aligned_free(void *pv_ctxt, void *pv_buf)
 {
+    (void)pv_ctxt;
     _aligned_free(pv_buf);
     return;
 }
 #endif
 
 #if IOS
-void* ihevca_aligned_malloc(WORD32 alignment, WORD32 size)
+void *ihevca_aligned_malloc(void *pv_ctxt, WORD32 alignment, WORD32 i4_size)
 {
-    return malloc(size);
+    (void)pv_ctxt;
+    return malloc(i4_size);
 }
 
-void ihevca_aligned_free(void *pv_buf)
+void ihevca_aligned_free(void *pv_ctxt, void *pv_buf)
 {
+    (void)pv_ctxt;
     free(pv_buf);
     return;
 }
 #endif
 
 #if (!defined(IOS)) && (!defined(_WIN32))
-void* ihevca_aligned_malloc(WORD32 alignment, WORD32 size)
+void *ihevca_aligned_malloc(void *pv_ctxt, WORD32 alignment, WORD32 i4_size)
 {
-    return memalign(alignment, size);
+   (void)pv_ctxt;
+    return memalign(alignment, i4_size);
 }
 
-void ihevca_aligned_free(void *pv_buf)
+void ihevca_aligned_free(void *pv_ctxt, void *pv_buf)
 {
+    (void)pv_ctxt;
     free(pv_buf);
     return;
 }
@@ -934,10 +926,10 @@
     {
 #if DUMP_SINGLE_BUF
         {
-            UWORD8 *buf = s_dump_disp_frm_buf.pv_y_buf - 24 - (s_dump_disp_frm_buf.u4_y_strd * 40);
+            UWORD8 *buf = s_dump_disp_frm_buf.pv_y_buf - 80 - (s_dump_disp_frm_buf.u4_y_strd * 80);
 
-            UWORD32 size = s_dump_disp_frm_buf.u4_y_strd * ((s_dump_disp_frm_buf.u4_y_ht + 80) + (s_dump_disp_frm_buf.u4_u_ht + 40));
-            fwrite(buf, 1, size, ps_op_file);
+            UWORD32 size = s_dump_disp_frm_buf.u4_y_strd * ((s_dump_disp_frm_buf.u4_y_ht + 160) + (s_dump_disp_frm_buf.u4_u_ht + 80));
+            fwrite(buf, 1, size ,ps_op_file);
 
         }
 #else
@@ -1003,7 +995,7 @@
             UWORD8 *buf = s_dump_disp_frm_buf.pv_y_buf - 24 - (s_dump_disp_frm_buf.u4_y_strd * 40);
 
             UWORD32 size = s_dump_disp_frm_buf.u4_y_strd * ((s_dump_disp_frm_buf.u4_y_ht + 80) + (s_dump_disp_frm_buf.u4_u_ht + 40));
-            fwrite(buf, 1, size, ps_op_file);
+            fwrite(buf, 1, size ,ps_op_file);
         }
 #else
         {
@@ -1242,15 +1234,6 @@
             if(ps_app_ctx->fps <= 0)
                 ps_app_ctx->fps = DEFAULT_FPS;
             break;
-        case MAX_WD:
-            sscanf(value, "%d", &ps_app_ctx->max_wd);
-            break;
-        case MAX_HT:
-            sscanf(value, "%d", &ps_app_ctx->max_ht);
-            break;
-        case MAX_LEVEL:
-            sscanf(value, "%d", &ps_app_ctx->max_level);
-            break;
         case ARCH:
             if((strcmp(value, "ARM_NONEON")) == 0)
                 ps_app_ctx->e_arch = ARCH_ARM_NONEON;
@@ -1274,6 +1257,8 @@
                 ps_app_ctx->e_arch = ARCH_MIPS_GENERIC;
             else if((strcmp(value, "MIPS_32")) == 0)
                 ps_app_ctx->e_arch = ARCH_MIPS_32;
+            else if((strcmp(value, "ARMV8_GENERIC")) == 0)
+                ps_app_ctx->e_arch = ARCH_ARMV8_GENERIC;
             else
             {
                 printf("\nInvalid Arch. Setting it to ARM_A9Q\n");
@@ -1527,14 +1512,11 @@
     TIMER   s_first_frame_time;
     UWORD32 first_frame_displayed;
 
-#ifdef X86_MINGW
-    UWORD32 frequency = 0;
-#endif
-#ifdef X86_MSVC
+#ifdef WINDOWS_TIMER
     TIMER frequency;
 #endif
 
-#ifdef X86_MSVC
+#ifdef WINDOWS_TIMER
     QueryPerformanceFrequency(&frequency);
 #endif
     first_frame_displayed = 0;
@@ -1745,17 +1727,15 @@
     WORD32 ret;
     CHAR ac_error_str[STRLENGTH];
     vid_dec_ctx_t s_app_ctx;
-    UWORD8 *pu1_bs_buf;
+    UWORD8 *pu1_bs_buf = NULL;
 
     ivd_out_bufdesc_t *ps_out_buf;
     UWORD32 u4_num_bytes_dec = 0;
     UWORD32 file_pos = 0;
-    IV_API_CALL_STATUS_T e_dec_status;
+
     UWORD32 u4_ip_frm_ts = 0, u4_op_frm_ts = 0;
 
     WORD32 u4_bytes_remaining = 0;
-    void *pv_mem_rec_location;
-    UWORD32 u4_num_mem_recs;
     UWORD32 i;
     UWORD32 u4_ip_buf_len;
     UWORD32 frm_cnt = 0;
@@ -1773,14 +1753,15 @@
 #endif
 #endif
 
-#ifdef X86_MINGW
-    UWORD32 frequency = 0;
-#endif
-#ifdef X86_MSVC
+#ifdef WINDOWS_TIMER
     TIMER frequency;
 #endif
     WORD32 width = 0, height = 0;
     iv_obj_t *codec_obj;
+#if defined(GPU_BUILD) && !defined(X86)
+//    int ioctl_init();
+//    ioctl_init();
+#endif
 
 #ifdef X86_MINGW
     //For getting printfs without any delay
@@ -1844,9 +1825,6 @@
     s_app_ctx.u4_num_cores = DEFAULT_NUM_CORES;
     s_app_ctx.i4_degrade_type = 0;
     s_app_ctx.i4_degrade_pics = 0;
-    s_app_ctx.max_wd = 0;
-    s_app_ctx.max_ht = 0;
-    s_app_ctx.max_level = 0;
     s_app_ctx.e_arch = ARCH_ARM_A9Q;
     s_app_ctx.e_soc = SOC_GENERIC;
 
@@ -2064,217 +2042,37 @@
 
         ps_out_buf = (ivd_out_bufdesc_t *)malloc(sizeof(ivd_out_bufdesc_t));
 
-        {
-            iv_num_mem_rec_ip_t s_no_of_mem_rec_query_ip;
-            iv_num_mem_rec_op_t s_no_of_mem_rec_query_op;
-
-            s_no_of_mem_rec_query_ip.u4_size = sizeof(s_no_of_mem_rec_query_ip);
-            s_no_of_mem_rec_query_op.u4_size = sizeof(s_no_of_mem_rec_query_op);
-            s_no_of_mem_rec_query_ip.e_cmd = IV_CMD_GET_NUM_MEM_REC;
-
-            /*****************************************************************************/
-            /*   API Call: Get Number of Mem Records                                     */
-            /*****************************************************************************/
-            e_dec_status = ivd_cxa_api_function(
-                            NULL, (void *)&s_no_of_mem_rec_query_ip,
-                            (void *)&s_no_of_mem_rec_query_op);
-            if(IV_SUCCESS != e_dec_status)
-            {
-                sprintf(ac_error_str, "Error in get mem records");
-                codec_exit(ac_error_str);
-            }
-
-            u4_num_mem_recs = s_no_of_mem_rec_query_op.u4_num_mem_rec;
-        }
-
-        pv_mem_rec_location = malloc(u4_num_mem_recs * sizeof(iv_mem_rec_t));
-        if(pv_mem_rec_location == NULL)
-        {
-            sprintf(ac_error_str, "Allocation failure for mem_rec_location");
-            codec_exit(ac_error_str);
-
-        }
-
-        {
-            ihevcd_cxa_fill_mem_rec_ip_t s_fill_mem_rec_ip;
-            ihevcd_cxa_fill_mem_rec_op_t s_fill_mem_rec_op;
-            iv_mem_rec_t *ps_mem_rec;
-            UWORD32 total_size;
-
-            s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.e_cmd =
-                            IV_CMD_FILL_NUM_MEM_REC;
-            s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location =
-                            (iv_mem_rec_t *)pv_mem_rec_location;
-            s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_wd =
-                            (s_app_ctx.max_wd == 0) ? MAX_FRAME_WIDTH : s_app_ctx.max_wd;
-            s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_ht =
-                            (s_app_ctx.max_ht == 0) ? MAX_FRAME_HEIGHT : s_app_ctx.max_ht;
-            s_fill_mem_rec_ip.i4_level = (s_app_ctx.max_level == 0) ? MAX_LEVEL_SUPPORTED : s_app_ctx.max_level;
-            s_fill_mem_rec_ip.u4_num_ref_frames = MAX_REF_FRAMES;
-            s_fill_mem_rec_ip.u4_num_reorder_frames = MAX_REORDER_FRAMES;
-            s_fill_mem_rec_ip.u4_share_disp_buf = s_app_ctx.share_disp_buf;
-            s_fill_mem_rec_ip.e_output_format =
-                            (IV_COLOR_FORMAT_T)s_app_ctx.e_output_chroma_format;
-            s_fill_mem_rec_ip.u4_num_extra_disp_buf = EXTRA_DISP_BUFFERS;
-
-            s_fill_mem_rec_ip.s_ivd_fill_mem_rec_ip_t.u4_size =
-                            sizeof(ihevcd_cxa_fill_mem_rec_ip_t);
-            s_fill_mem_rec_op.s_ivd_fill_mem_rec_op_t.u4_size =
-                            sizeof(ihevcd_cxa_fill_mem_rec_op_t);
-
-            ps_mem_rec = (iv_mem_rec_t *)pv_mem_rec_location;
-            for(i = 0; i < u4_num_mem_recs; i++)
-                ps_mem_rec[i].u4_size = sizeof(iv_mem_rec_t);
-
-            /*****************************************************************************/
-            /*   API Call: Fill Mem Records                                     */
-            /*****************************************************************************/
-
-            e_dec_status = ivd_cxa_api_function(NULL,
-                                                (void *)&s_fill_mem_rec_ip,
-                                                (void *)&s_fill_mem_rec_op);
-
-            u4_num_mem_recs =
-                            s_fill_mem_rec_op.s_ivd_fill_mem_rec_op_t.u4_num_mem_rec_filled;
-
-            if(IV_SUCCESS != e_dec_status)
-            {
-                sprintf(ac_error_str, "Error in fill mem records: %x", s_fill_mem_rec_op.s_ivd_fill_mem_rec_op_t.u4_error_code);
-                codec_exit(ac_error_str);
-            }
-
-            ps_mem_rec = (iv_mem_rec_t *)pv_mem_rec_location;
-            total_size = 0;
-            for(i = 0; i < u4_num_mem_recs; i++)
-            {
-                ps_mem_rec->pv_base = ihevca_aligned_malloc(ps_mem_rec->u4_mem_alignment,
-                                                            ps_mem_rec->u4_mem_size);
-                if(ps_mem_rec->pv_base == NULL)
-                {
-                    sprintf(ac_error_str,
-                            "\nAllocation failure for mem record id %d size %d\n",
-                            i, ps_mem_rec->u4_mem_size);
-                    codec_exit(ac_error_str);
-
-                }
-                total_size += ps_mem_rec->u4_mem_size;
-
-                ps_mem_rec++;
-            }
-            //printf("\nTotal memory for codec %d\n", total_size);
-        }
         /*****************************************************************************/
         /*   API Call: Initialize the Decoder                                        */
         /*****************************************************************************/
         {
-            ihevcd_cxa_init_ip_t s_init_ip;
-            ihevcd_cxa_init_op_t s_init_op;
+            ihevcd_cxa_create_ip_t s_create_ip;
+            ihevcd_cxa_create_op_t s_create_op;
             void *fxns = &ivd_cxa_api_function;
-            iv_mem_rec_t *mem_tab;
 
-            mem_tab = (iv_mem_rec_t *)pv_mem_rec_location;
-            s_init_ip.s_ivd_init_ip_t.e_cmd = (IVD_API_COMMAND_TYPE_T)IV_CMD_INIT;
-            s_init_ip.s_ivd_init_ip_t.pv_mem_rec_location = mem_tab;
-            s_init_ip.s_ivd_init_ip_t.u4_frm_max_wd = (s_app_ctx.max_wd == 0) ? MAX_FRAME_WIDTH : s_app_ctx.max_wd;
-            s_init_ip.s_ivd_init_ip_t.u4_frm_max_ht = (s_app_ctx.max_ht == 0) ? MAX_FRAME_HEIGHT : s_app_ctx.max_ht;
-            s_init_ip.i4_level = (s_app_ctx.max_level == 0) ? MAX_LEVEL_SUPPORTED : s_app_ctx.max_level;
-            s_init_ip.u4_num_ref_frames = MAX_REF_FRAMES;
-            s_init_ip.u4_num_reorder_frames = MAX_REORDER_FRAMES;
-            s_init_ip.u4_share_disp_buf = s_app_ctx.share_disp_buf;
-            s_init_ip.u4_num_extra_disp_buf = EXTRA_DISP_BUFFERS;
-            s_init_ip.s_ivd_init_ip_t.u4_num_mem_rec = u4_num_mem_recs;
-            s_init_ip.s_ivd_init_ip_t.e_output_format =
-                            (IV_COLOR_FORMAT_T)s_app_ctx.e_output_chroma_format;
-            s_init_ip.s_ivd_init_ip_t.u4_size = sizeof(ihevcd_cxa_init_ip_t);
-            s_init_op.s_ivd_init_op_t.u4_size = sizeof(ihevcd_cxa_init_op_t);
+            s_create_ip.s_ivd_create_ip_t.e_cmd = IVD_CMD_CREATE;
+            s_create_ip.s_ivd_create_ip_t.u4_share_disp_buf = s_app_ctx.share_disp_buf;
+            s_create_ip.s_ivd_create_ip_t.e_output_format = (IV_COLOR_FORMAT_T)s_app_ctx.e_output_chroma_format;
+            s_create_ip.s_ivd_create_ip_t.pf_aligned_alloc = ihevca_aligned_malloc;
+            s_create_ip.s_ivd_create_ip_t.pf_aligned_free = ihevca_aligned_free;
+            s_create_ip.s_ivd_create_ip_t.pv_mem_ctxt = NULL;
+            s_create_ip.s_ivd_create_ip_t.u4_size = sizeof(ihevcd_cxa_create_ip_t);
+            s_create_op.s_ivd_create_op_t.u4_size = sizeof(ihevcd_cxa_create_op_t);
 
-            codec_obj = (iv_obj_t *)mem_tab[0].pv_base;
-            codec_obj->pv_fxns = fxns;
-            codec_obj->u4_size = sizeof(iv_obj_t);
 
-            s_app_ctx.cocodec_obj = codec_obj;
 
-            ret = ivd_cxa_api_function((iv_obj_t *)codec_obj, (void *)&s_init_ip,
-                                       (void *)&s_init_op);
+            ret = ivd_cxa_api_function(NULL, (void *)&s_create_ip,
+                                       (void *)&s_create_op);
             if(ret != IV_SUCCESS)
             {
-                sprintf(ac_error_str, "Error in Init %8x\n",
-                        s_init_op.s_ivd_init_op_t.u4_error_code);
+                sprintf(ac_error_str, "Error in Create %8x\n",
+                        s_create_op.s_ivd_create_op_t.u4_error_code);
                 codec_exit(ac_error_str);
             }
-
-            /*****************************************************************************/
-            /*  Input and output buffer allocation                                       */
-            /*****************************************************************************/
-            {
-
-                ivd_ctl_getbufinfo_ip_t s_ctl_ip;
-                ivd_ctl_getbufinfo_op_t s_ctl_op;
-
-                s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
-                s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_GETBUFINFO;
-                s_ctl_ip.u4_size = sizeof(ivd_ctl_getbufinfo_ip_t);
-                s_ctl_op.u4_size = sizeof(ivd_ctl_getbufinfo_op_t);
-                ret = ivd_cxa_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
-                                           (void *)&s_ctl_op);
-                if(ret != IV_SUCCESS)
-                {
-                    sprintf(ac_error_str, "Error in Get Buf Info %x", s_ctl_op.u4_error_code);
-                    codec_exit(ac_error_str);
-                }
-
-                /* Allocate input buffer */
-                u4_ip_buf_len = s_ctl_op.u4_min_in_buf_size[0];
-                pu1_bs_buf = (UWORD8 *)malloc(u4_ip_buf_len);
-
-                if(pu1_bs_buf == NULL)
-                {
-                    sprintf(ac_error_str,
-                            "\nAllocation failure for input buffer of size %d",
-                            u4_ip_buf_len);
-                    codec_exit(ac_error_str);
-                }
-                s_app_ctx.num_disp_buf = s_ctl_op.u4_num_disp_bufs;
-                /* Allocate output buffer only if display buffers are not shared */
-                /* Or if shared and output is 420P */
-                if((0 == s_app_ctx.share_disp_buf) || (IV_YUV_420P == s_app_ctx.e_output_chroma_format))
-                {
-                    UWORD32 outlen;
-                    ps_out_buf->u4_min_out_buf_size[0] =
-                                    s_ctl_op.u4_min_out_buf_size[0];
-                    ps_out_buf->u4_min_out_buf_size[1] =
-                                    s_ctl_op.u4_min_out_buf_size[1];
-                    ps_out_buf->u4_min_out_buf_size[2] =
-                                    s_ctl_op.u4_min_out_buf_size[2];
-
-                    outlen = s_ctl_op.u4_min_out_buf_size[0];
-                    if(s_ctl_op.u4_min_num_out_bufs > 1)
-                        outlen += s_ctl_op.u4_min_out_buf_size[1];
-
-                    if(s_ctl_op.u4_min_num_out_bufs > 2)
-                        outlen += s_ctl_op.u4_min_out_buf_size[2];
-
-                    ps_out_buf->pu1_bufs[0] = (UWORD8 *)malloc(outlen);
-                    if(ps_out_buf->pu1_bufs[0] == NULL)
-                    {
-                        sprintf(ac_error_str,
-                                "\nAllocation failure for output buffer of size %d",
-                                outlen);
-                        codec_exit(ac_error_str);
-                    }
-
-                    if(s_ctl_op.u4_min_num_out_bufs > 1)
-                        ps_out_buf->pu1_bufs[1] = ps_out_buf->pu1_bufs[0]
-                                        + (s_ctl_op.u4_min_out_buf_size[0]);
-
-                    if(s_ctl_op.u4_min_num_out_bufs > 2)
-                        ps_out_buf->pu1_bufs[2] = ps_out_buf->pu1_bufs[1]
-                                        + (s_ctl_op.u4_min_out_buf_size[1]);
-
-                    ps_out_buf->u4_num_bufs = s_ctl_op.u4_min_num_out_bufs;
-                }
-
-            }
+            codec_obj = (iv_obj_t*)s_create_op.s_ivd_create_op_t.pv_handle;
+            codec_obj->pv_fxns = fxns;
+            codec_obj->u4_size = sizeof(iv_obj_t);
+            s_app_ctx.cocodec_obj = codec_obj;
         }
 
     }
@@ -2328,36 +2126,56 @@
 
     }
 
+    flush_output(codec_obj, &s_app_ctx, ps_out_buf,
+                 pu1_bs_buf, &u4_op_frm_ts,
+                 ps_op_file, ps_op_chksum_file,
+                 u4_ip_frm_ts, u4_bytes_remaining);
 
     /*****************************************************************************/
     /*   Decode header to get width and height and buffer sizes                  */
     /*****************************************************************************/
     {
-
-        ivd_ctl_set_config_ip_t s_ctl_ip;
-        ivd_ctl_set_config_op_t s_ctl_op;
-
         ivd_video_decode_ip_t s_video_decode_ip;
         ivd_video_decode_op_t s_video_decode_op;
 
-        s_ctl_ip.u4_disp_wd = STRIDE;
-        if(1 == s_app_ctx.display)
-            s_ctl_ip.u4_disp_wd = s_app_ctx.get_stride();
 
-        s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;
-        s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
-        s_ctl_ip.e_vid_dec_mode = IVD_DECODE_HEADER;
-        s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
-        s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
-        s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
-        s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);
 
-        ret = ivd_cxa_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
-                                   (void *)&s_ctl_op);
-        if(ret != IV_SUCCESS)
+        {
+            ivd_ctl_set_config_ip_t s_ctl_ip;
+            ivd_ctl_set_config_op_t s_ctl_op;
+
+
+            s_ctl_ip.u4_disp_wd = STRIDE;
+            if(1 == s_app_ctx.display)
+                s_ctl_ip.u4_disp_wd = s_app_ctx.get_stride();
+
+            s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;
+            s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
+            s_ctl_ip.e_vid_dec_mode = IVD_DECODE_HEADER;
+            s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
+            s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
+            s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
+            s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);
+
+            ret = ivd_cxa_api_function((iv_obj_t*)codec_obj, (void *)&s_ctl_ip,
+                                       (void *)&s_ctl_op);
+            if(ret != IV_SUCCESS)
+            {
+                sprintf(ac_error_str,
+                        "\nError in setting the codec in header decode mode");
+                codec_exit(ac_error_str);
+            }
+        }
+
+        /* Allocate input buffer for header */
+        u4_ip_buf_len = 256 * 1024;
+        pu1_bs_buf = (UWORD8 *)malloc(u4_ip_buf_len);
+
+        if(pu1_bs_buf == NULL)
         {
             sprintf(ac_error_str,
-                    "\nError in setting the codec in header decode mode");
+                    "\nAllocation failure for input buffer of i4_size %d",
+                    u4_ip_buf_len);
             codec_exit(ac_error_str);
         }
 
@@ -2408,7 +2226,7 @@
 
             u4_num_bytes_dec = s_video_decode_op.u4_num_bytes_consumed;
 #ifndef PROFILE_ENABLE
-            printf("%d\n", s_video_decode_op.u4_num_bytes_consumed);
+            printf("%d\n",s_video_decode_op.u4_num_bytes_consumed);
 #endif
             file_pos += u4_num_bytes_dec;
             total_bytes_comsumed += u4_num_bytes_dec;
@@ -2418,10 +2236,177 @@
         s_app_ctx.u4_pic_wd = s_video_decode_op.u4_pic_wd;
         s_app_ctx.u4_pic_ht = s_video_decode_op.u4_pic_ht;
 
+        free(pu1_bs_buf);
+
 #if IOS_DISPLAY
         s_app_ctx.i4_screen_wd = screen_wd;
         s_app_ctx.i4_screen_ht = screen_ht;
 #endif
+        {
+
+            ivd_ctl_getbufinfo_ip_t s_ctl_ip;
+            ivd_ctl_getbufinfo_op_t s_ctl_op;
+            WORD32 outlen = 0;
+
+            s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
+            s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_GETBUFINFO;
+            s_ctl_ip.u4_size = sizeof(ivd_ctl_getbufinfo_ip_t);
+            s_ctl_op.u4_size = sizeof(ivd_ctl_getbufinfo_op_t);
+            ret = ivd_cxa_api_function((iv_obj_t*)codec_obj, (void *)&s_ctl_ip,
+                                       (void *)&s_ctl_op);
+            if(ret != IV_SUCCESS)
+            {
+                sprintf(ac_error_str, "Error in Get Buf Info %x", s_ctl_op.u4_error_code);
+                codec_exit(ac_error_str);
+            }
+
+            /* Allocate bitstream buffer */
+            u4_ip_buf_len = s_ctl_op.u4_min_in_buf_size[0];
+#ifdef ADAPTIVE_TEST
+            u4_ip_buf_len = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT * 3 >> 1;
+#endif
+            pu1_bs_buf = (UWORD8 *)malloc(u4_ip_buf_len);
+
+            if(pu1_bs_buf == NULL)
+            {
+                sprintf(ac_error_str,
+                        "\nAllocation failure for input buffer of i4_size %d",
+                        u4_ip_buf_len);
+                codec_exit(ac_error_str);
+            }
+
+#ifdef ADAPTIVE_TEST
+            switch(s_app_ctx.e_output_chroma_format)
+            {
+                case IV_YUV_420P:
+                {
+                    s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT;
+                    s_ctl_op.u4_min_out_buf_size[1] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT >> 2;
+                    s_ctl_op.u4_min_out_buf_size[2] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT >> 2;
+                    break;
+                }
+                case IV_YUV_420SP_UV:
+                case IV_YUV_420SP_VU:
+                {
+                    s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT;
+                    s_ctl_op.u4_min_out_buf_size[1] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT >> 1;
+                    s_ctl_op.u4_min_out_buf_size[2] = 0;
+                    break;
+                }
+                case IV_YUV_422ILE:
+                {
+                    s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT * 2;
+                    s_ctl_op.u4_min_out_buf_size[1] = 0;
+                    s_ctl_op.u4_min_out_buf_size[2] = 0;
+                    break;
+                }
+                case IV_RGBA_8888:
+                {
+                    s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT * 4;
+                    s_ctl_op.u4_min_out_buf_size[1] = 0;
+                    s_ctl_op.u4_min_out_buf_size[2] = 0;
+                    break;
+                }
+                case IV_RGB_565:
+                {
+                    s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT * 2;
+                    s_ctl_op.u4_min_out_buf_size[1] = 0;
+                    s_ctl_op.u4_min_out_buf_size[2] = 0;
+                    break;
+                }
+                default:
+                    break;
+
+            }
+#endif
+            /* Allocate output buffer only if display buffers are not shared */
+            /* Or if shared and output is 420P */
+            if((0 == s_app_ctx.share_disp_buf) || (IV_YUV_420P == s_app_ctx.e_output_chroma_format))
+            {
+                ps_out_buf->u4_min_out_buf_size[0] =
+                                s_ctl_op.u4_min_out_buf_size[0];
+                ps_out_buf->u4_min_out_buf_size[1] =
+                                s_ctl_op.u4_min_out_buf_size[1];
+                ps_out_buf->u4_min_out_buf_size[2] =
+                                s_ctl_op.u4_min_out_buf_size[2];
+
+                outlen = s_ctl_op.u4_min_out_buf_size[0];
+                if(s_ctl_op.u4_min_num_out_bufs > 1)
+                    outlen += s_ctl_op.u4_min_out_buf_size[1];
+
+                if(s_ctl_op.u4_min_num_out_bufs > 2)
+                    outlen += s_ctl_op.u4_min_out_buf_size[2];
+
+                ps_out_buf->pu1_bufs[0] = (UWORD8 *)malloc(outlen);
+                if(ps_out_buf->pu1_bufs[0] == NULL)
+                {
+                    sprintf(ac_error_str, "\nAllocation failure for output buffer of i4_size %d",
+                            outlen);
+                    codec_exit(ac_error_str);
+                }
+
+                if(s_ctl_op.u4_min_num_out_bufs > 1)
+                    ps_out_buf->pu1_bufs[1] = ps_out_buf->pu1_bufs[0]
+                                    + (s_ctl_op.u4_min_out_buf_size[0]);
+
+                if(s_ctl_op.u4_min_num_out_bufs > 2)
+                    ps_out_buf->pu1_bufs[2] = ps_out_buf->pu1_bufs[1]
+                                    + (s_ctl_op.u4_min_out_buf_size[1]);
+
+                ps_out_buf->u4_num_bufs = s_ctl_op.u4_min_num_out_bufs;
+            }
+
+#ifdef APP_EXTRA_BUFS
+            s_app_ctx.disp_delay = EXTRA_DISP_BUFFERS;
+            s_ctl_op.u4_num_disp_bufs += EXTRA_DISP_BUFFERS;
+#endif
+
+            /*****************************************************************************/
+            /*   API Call: Allocate display buffers for display buffer shared case       */
+            /*****************************************************************************/
+
+            for(i = 0; i < s_ctl_op.u4_num_disp_bufs; i++)
+            {
+
+                s_app_ctx.s_disp_buffers[i].u4_min_out_buf_size[0] =
+                                s_ctl_op.u4_min_out_buf_size[0];
+                s_app_ctx.s_disp_buffers[i].u4_min_out_buf_size[1] =
+                                s_ctl_op.u4_min_out_buf_size[1];
+                s_app_ctx.s_disp_buffers[i].u4_min_out_buf_size[2] =
+                                s_ctl_op.u4_min_out_buf_size[2];
+
+                outlen = s_ctl_op.u4_min_out_buf_size[0];
+                if(s_ctl_op.u4_min_num_out_bufs > 1)
+                    outlen += s_ctl_op.u4_min_out_buf_size[1];
+
+                if(s_ctl_op.u4_min_num_out_bufs > 2)
+                    outlen += s_ctl_op.u4_min_out_buf_size[2];
+
+                s_app_ctx.s_disp_buffers[i].pu1_bufs[0] = (UWORD8 *)malloc(outlen);
+
+                if(s_app_ctx.s_disp_buffers[i].pu1_bufs[0] == NULL)
+                {
+                    sprintf(ac_error_str,
+                            "\nAllocation failure for output buffer of i4_size %d",
+                            outlen);
+                    codec_exit(ac_error_str);
+                }
+
+                if(s_ctl_op.u4_min_num_out_bufs > 1)
+                    s_app_ctx.s_disp_buffers[i].pu1_bufs[1] =
+                                    s_app_ctx.s_disp_buffers[i].pu1_bufs[0]
+                                                    + (s_ctl_op.u4_min_out_buf_size[0]);
+
+                if(s_ctl_op.u4_min_num_out_bufs > 2)
+                    s_app_ctx.s_disp_buffers[i].pu1_bufs[2] =
+                                    s_app_ctx.s_disp_buffers[i].pu1_bufs[1]
+                                                    + (s_ctl_op.u4_min_out_buf_size[1]);
+
+                s_app_ctx.s_disp_buffers[i].u4_num_bufs =
+                                s_ctl_op.u4_min_num_out_bufs;
+            }
+            s_app_ctx.num_disp_buf = s_ctl_op.u4_num_disp_bufs;
+        }
 
         /* Create display thread and wait for the display buffers to be initialized */
         if(1 == s_app_ctx.display)
@@ -2430,7 +2415,7 @@
             {
                 s_app_ctx.display_init_done = 0;
                 ithread_create(s_app_ctx.display_thread_handle, NULL,
-                               (void *)&display_thread, (void *)&s_app_ctx);
+                                                    (void *) &display_thread, (void *) &s_app_ctx);
                 s_app_ctx.display_thread_created = 1;
 
                 while(1)
@@ -2441,84 +2426,9 @@
                     ithread_msleep(1);
                 }
             }
-
             s_app_ctx.u4_strd = s_app_ctx.get_stride();
         }
-    }
 
-    /*************************************************************************/
-    /* Get actual number of output buffers requried, which is dependent      */
-    /* on stream properties such as width, height and level etc              */
-    /* This is needed mainly for shared display mode                         */
-    /*************************************************************************/
-    //if(1 == s_app_ctx.share_disp_buf)
-    {
-        ivd_ctl_getbufinfo_ip_t s_ctl_ip;
-        ivd_ctl_getbufinfo_op_t s_ctl_op;
-        WORD32 outlen = 0;
-
-        s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
-        s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_GETBUFINFO;
-        s_ctl_ip.u4_size = sizeof(ivd_ctl_getbufinfo_ip_t);
-        s_ctl_op.u4_size = sizeof(ivd_ctl_getbufinfo_op_t);
-        ret = ivd_cxa_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
-                                   (void *)&s_ctl_op);
-        if(ret != IV_SUCCESS)
-        {
-            sprintf(ac_error_str, "Error in Get Buf Info %x", s_ctl_op.u4_error_code);
-            codec_exit(ac_error_str);
-        }
-
-#ifdef APP_EXTRA_BUFS
-        s_app_ctx.disp_delay = EXTRA_DISP_BUFFERS;
-        s_ctl_op.u4_num_disp_bufs += EXTRA_DISP_BUFFERS;
-#endif
-
-        /*****************************************************************************/
-        /*   API Call: Allocate display buffers for display buffer shared case       */
-        /*****************************************************************************/
-
-        for(i = 0; i < s_ctl_op.u4_num_disp_bufs; i++)
-        {
-
-            s_app_ctx.s_disp_buffers[i].u4_min_out_buf_size[0] =
-                            s_ctl_op.u4_min_out_buf_size[0];
-            s_app_ctx.s_disp_buffers[i].u4_min_out_buf_size[1] =
-                            s_ctl_op.u4_min_out_buf_size[1];
-            s_app_ctx.s_disp_buffers[i].u4_min_out_buf_size[2] =
-                            s_ctl_op.u4_min_out_buf_size[2];
-
-            outlen = s_ctl_op.u4_min_out_buf_size[0];
-            if(s_ctl_op.u4_min_num_out_bufs > 1)
-                outlen += s_ctl_op.u4_min_out_buf_size[1];
-
-            if(s_ctl_op.u4_min_num_out_bufs > 2)
-                outlen += s_ctl_op.u4_min_out_buf_size[2];
-
-            s_app_ctx.s_disp_buffers[i].pu1_bufs[0] = (UWORD8 *)malloc(outlen);
-
-            if(s_app_ctx.s_disp_buffers[i].pu1_bufs[0] == NULL)
-            {
-                sprintf(ac_error_str,
-                        "\nAllocation failure for output buffer of size %d",
-                        outlen);
-                codec_exit(ac_error_str);
-            }
-
-            if(s_ctl_op.u4_min_num_out_bufs > 1)
-                s_app_ctx.s_disp_buffers[i].pu1_bufs[1] =
-                                s_app_ctx.s_disp_buffers[i].pu1_bufs[0]
-                                                + (s_ctl_op.u4_min_out_buf_size[0]);
-
-            if(s_ctl_op.u4_min_num_out_bufs > 2)
-                s_app_ctx.s_disp_buffers[i].pu1_bufs[2] =
-                                s_app_ctx.s_disp_buffers[i].pu1_bufs[1]
-                                                + (s_ctl_op.u4_min_out_buf_size[1]);
-
-            s_app_ctx.s_disp_buffers[i].u4_num_bufs =
-                            s_ctl_op.u4_min_num_out_bufs;
-        }
-        s_app_ctx.num_disp_buf = s_ctl_op.u4_num_disp_bufs;
 
         /*****************************************************************************/
         /*   API Call: Send the allocated display buffers to codec                   */
@@ -2535,7 +2445,7 @@
 
             memcpy(&(s_set_display_frame_ip.s_disp_buffer),
                    &(s_app_ctx.s_disp_buffers),
-                   s_ctl_op.u4_num_disp_bufs * sizeof(ivd_out_bufdesc_t));
+                   s_app_ctx.num_disp_buf * sizeof(ivd_out_bufdesc_t));
 
             ret = ivd_cxa_api_function((iv_obj_t *)codec_obj,
                                        (void *)&s_set_display_frame_ip,
@@ -2648,7 +2558,7 @@
     /* If required disable deblocking and sao at given level                 */
     /*************************************************************************/
     set_degrade(codec_obj, s_app_ctx.i4_degrade_type, s_app_ctx.i4_degrade_pics);
-#ifdef X86_MSVC
+#ifdef WINDOWS_TIMER
     QueryPerformanceFrequency(&frequency);
 #endif
 #ifndef PRINT_PICSIZE
@@ -3051,39 +2961,21 @@
     }
 
     {
-        iv_retrieve_mem_rec_ip_t s_retrieve_dec_ip;
-        iv_retrieve_mem_rec_op_t s_retrieve_dec_op;
-        s_retrieve_dec_ip.pv_mem_rec_location = (iv_mem_rec_t *)pv_mem_rec_location;
+        ivd_delete_ip_t s_delete_dec_ip;
+        ivd_delete_op_t s_delete_dec_op;
 
-        s_retrieve_dec_ip.e_cmd = IV_CMD_RETRIEVE_MEMREC;
-        s_retrieve_dec_ip.u4_size = sizeof(iv_retrieve_mem_rec_ip_t);
-        s_retrieve_dec_op.u4_size = sizeof(iv_retrieve_mem_rec_op_t);
+        s_delete_dec_ip.e_cmd = IVD_CMD_DELETE;
+        s_delete_dec_ip.u4_size = sizeof(ivd_delete_ip_t);
+        s_delete_dec_op.u4_size = sizeof(ivd_delete_op_t);
 
-        ret = ivd_cxa_api_function((iv_obj_t *)codec_obj, (void *)&s_retrieve_dec_ip,
-                                   (void *)&s_retrieve_dec_op);
+        ret = ivd_cxa_api_function((iv_obj_t *)codec_obj, (void *)&s_delete_dec_ip,
+                                   (void *)&s_delete_dec_op);
 
         if(IV_SUCCESS != ret)
         {
-            sprintf(ac_error_str, "Error in Retrieve Memrec");
+            sprintf(ac_error_str, "Error in Codec delete");
             codec_exit(ac_error_str);
         }
-
-        {
-            iv_mem_rec_t *ps_mem_rec;
-            UWORD16 u2_i;
-
-            u4_num_mem_recs = s_retrieve_dec_op.u4_num_mem_rec_filled;
-
-            ps_mem_rec = s_retrieve_dec_ip.pv_mem_rec_location;
-
-            for(u2_i = 0; u2_i < u4_num_mem_recs; u2_i++)
-            {
-                ihevca_aligned_free(ps_mem_rec->pv_base);
-                ps_mem_rec++;
-            }
-            free(s_retrieve_dec_ip.pv_mem_rec_location);
-        }
-
     }
     /***********************************************************************/
     /*              Close all the files and free all the memory            */
@@ -3115,5 +3007,8 @@
     free(ps_out_buf);
     free(pu1_bs_buf);
 
+    if(s_app_ctx.display_thread_handle)
+        free(s_app_ctx.display_thread_handle);
+
     return (0);
 }