Snap for 5044688 from 9188a705589dda54be2588ce3551f68fb469e2f0 to pi-qpr2-release

Change-Id: Ia012dfbf58317aea3eaa2e9ccc14bca354dc032b
diff --git a/libplatformconfig/ConfigParser.cpp b/libplatformconfig/ConfigParser.cpp
index ef07d7c..785c953 100644
--- a/libplatformconfig/ConfigParser.cpp
+++ b/libplatformconfig/ConfigParser.cpp
@@ -32,7 +32,6 @@
 #include <errno.h>
 #include <utils/Log.h>
 #include <sys/mman.h>
-#include "vidc_debug.h"
 #include "PlatformConfig.h"
 #include "ConfigParser.h"
 
@@ -42,14 +41,14 @@
 
 void ConfigParser::processProperty(const XML_Char **attr, ConfigMap &configMap) {
     if (strcmp(attr[0], "name") != 0) {
-        DEBUG_PRINT_HIGH("%s: Element 'name' not found!", __func__);
+        VIDC_PLAT_LOGH("%s: Element 'name' not found!", __func__);
         return;
     }
 
     std::string propName(attr[1]);
 
     if (strcmp(attr[2], "value") != 0) {
-        DEBUG_PRINT_HIGH("%s: Element 'value' not found for %s!", __func__, propName.c_str());
+        VIDC_PLAT_LOGH("%s: Element 'value' not found for %s!", __func__, propName.c_str());
         return;
     }
 
@@ -78,7 +77,7 @@
     FILE *file;
     file = fopen(configFile.c_str(), "r");
     if (!file) {
-        DEBUG_PRINT_HIGH("%s: Error: %d (%s). Using defaults!",
+        VIDC_PLAT_LOGH("%s: Error: %d (%s). Using defaults!",
             __func__, errno, strerror(errno));
         return err;
     }
@@ -86,7 +85,7 @@
     // Create Parser
     parser = XML_ParserCreate(NULL);
     if (!parser) {
-        DEBUG_PRINT_HIGH("%s: Failed to create XML parser!", __func__);
+        VIDC_PLAT_LOGH("%s: Failed to create XML parser!", __func__);
         err = -ENODEV;
         goto fileError;
     }
@@ -101,21 +100,21 @@
     while (1) {
         buf = XML_GetBuffer(parser, BUF_SIZE);
         if (buf == NULL) {
-            DEBUG_PRINT_HIGH("%s: XML_GetBuffer failed", __func__);
+            VIDC_PLAT_LOGH("%s: XML_GetBuffer failed", __func__);
             err = -ENOMEM;
             goto parserError;
         }
 
         bytesRead = fread(buf, 1, BUF_SIZE, file);
         if (bytesRead < 0) {
-            DEBUG_PRINT_HIGH("%s: fread failed, bytes read = %d", __func__, bytesRead);
+            VIDC_PLAT_LOGH("%s: fread failed, bytes read = %d", __func__, bytesRead);
             err = bytesRead;
             goto parserError;
         }
 
         if (XML_ParseBuffer(parser, bytesRead,
                 bytesRead == 0) == XML_STATUS_ERROR) {
-            DEBUG_PRINT_HIGH("%s: XML_ParseBuffer failed, for %s",
+            VIDC_PLAT_LOGH("%s: XML_ParseBuffer failed, for %s",
                     __func__, configFile.c_str());
             err = -EINVAL;
             goto parserError;
diff --git a/libplatformconfig/ConfigParser.h b/libplatformconfig/ConfigParser.h
index 1170b7e..7aadc8f 100644
--- a/libplatformconfig/ConfigParser.h
+++ b/libplatformconfig/ConfigParser.h
@@ -46,6 +46,10 @@
     extern "C" {
 #endif
 
+#define VIDC_PLAT_LOGH(fmt, args...) ({ \
+          ALOGD(fmt,##args); \
+      })
+
 namespace Platform {
 
 class ConfigParser {
diff --git a/libplatformconfig/PlatformConfig.cpp b/libplatformconfig/PlatformConfig.cpp
index df52fef..35237e8 100644
--- a/libplatformconfig/PlatformConfig.cpp
+++ b/libplatformconfig/PlatformConfig.cpp
@@ -32,7 +32,6 @@
 #include <errno.h>
 #include <utils/Log.h>
 #include <sys/mman.h>
-#include "vidc_debug.h"
 #include "PlatformConfig.h"
 #include "ConfigParser.h"
 
@@ -47,7 +46,7 @@
 }
 
 Config* Config::getInstance() {
-    DEBUG_PRINT_LOW("%s: Enter", __func__);
+    VIDC_PLAT_LOGH("%s: Enter", __func__);
     if (!mInstance) {
         mInstance = new Config();
     }
@@ -62,12 +61,12 @@
         return FAIL;
     }
     if (conf->mConfigMap.find(configStrMap[config].name) == conf->mConfigMap.end()) {
-        DEBUG_PRINT_HIGH("%s: Returning default", __func__);
+        VIDC_PLAT_LOGH("%s: Returning default", __func__);
         *value = defaultValue;
         return FAIL;
     }
     *value = (int32_t) atoi(conf->mConfigMap[configStrMap[config].name].c_str());
-    DEBUG_PRINT_LOW("%s Config name: %s value: %d",
+    VIDC_PLAT_LOGH("%s Config name: %s value: %d",
             __func__, configStrMap[config].name, *value);
     return OK;
 }
diff --git a/mm-core/inc/OMX_Video.h b/mm-core/inc/OMX_Video.h
index a5568fe..4ac6071 100644
--- a/mm-core/inc/OMX_Video.h
+++ b/mm-core/inc/OMX_Video.h
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2008, 2018 The Khronos Group Inc.
+ * Copyright (c) 2008 The Khronos Group Inc.
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -71,7 +71,7 @@
     OMX_VIDEO_CodingVP8,        /**< Google VP8, formerly known as On2 VP8 */
     OMX_VIDEO_CodingVP9,        /**< Google VP9 */
     OMX_VIDEO_CodingHEVC,       /**< HEVC */
-    OMX_VIDEO_CodingHEIC = 13,  /**< HEIC */
+    OMX_VIDEO_CodingImageHEIC = 13,  /**< HEIC */
     OMX_VIDEO_CodingKhronosExtensions = 0x6F000000, /**< Reserved region for introducing Khronos Standard Extensions */ 
     OMX_VIDEO_CodingVendorStartUnused = 0x7F000000, /**< Reserved region for introducing Vendor Extensions */
     OMX_VIDEO_CodingMax = 0x7FFFFFFF
diff --git a/mm-core/src/sdm845/registry_table_android.c b/mm-core/src/sdm845/registry_table_android.c
index 82ea3e4..a8239cf 100644
--- a/mm-core/src/sdm845/registry_table_android.c
+++ b/mm-core/src/sdm845/registry_table_android.c
@@ -72,54 +72,6 @@
     }
   },
   {
-    "OMX.qcom.video.decoder.mpeg2",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxVdec.so",
-    {
-      "video_decoder.mpeg2"
-    }
-  },
-  {
-    "OMX.qcom.video.decoder.mpeg2.secure",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxVdec.so",
-    {
-      "video_decoder.mpeg2"
-    }
-  },
-  {
-    "OMX.qti.video.decoder.vc1sw",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxSwVdec.so",
-    {
-      "video_decoder.vc1"
-    }
-  },
-  {
     "OMX.qcom.video.decoder.hevc",
     NULL,   // Create instance function
     // Unique instance handle
@@ -200,90 +152,6 @@
     }
   },
   {
-    "OMX.qti.video.decoder.mpeg4sw",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxSwVdec.so",
-    {
-      "video_decoder.mpeg4"
-    }
-  },
-  {
-    "OMX.qti.video.decoder.divxsw",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxSwVdec.so",
-    {
-      "video_decoder.divx"
-    }
-  },
-  {
-    "OMX.qti.video.decoder.divx4sw",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxSwVdec.so",
-    {
-      "video_decoder.divx"
-    }
-  },
-  {
-    "OMX.qti.video.decoder.h263sw",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxSwVdec.so",
-    {
-      "video_decoder.h263"
-    }
-  },
-   {
-    "OMX.qcom.video.encoder.mpeg4sw",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxSwVencMpeg4.so",
-    {
-      "video_encoder.mpeg4"
-    }
-  },
-  {
-    "OMX.qcom.video.encoder.h263sw",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxSwVencMpeg4.so",
-    {
-      "video_encoder.h263"
-    }
-  },
-  {
     "OMX.qcom.video.encoder.avc",
     NULL, // Create instance function
     // Unique instance handle
@@ -300,22 +168,6 @@
     }
   },
   {
-    "OMX.qcom.video.encoder.avc.secure",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxVenc.so",
-    {
-      "video_encoder.avc"
-    }
-  },
-  {
     "OMX.qcom.video.encoder.vp8",
     NULL, // Create instance function
     // Unique instance handle
@@ -363,388 +215,6 @@
       "image_encoder.heic"
     }
   },
-  {
-    "OMX.qcom.video.encoder.tme",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL, // Shared object library handle
-    "libOmxVenc.so",
-    {
-      "video_encoder.tme"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.Qcelp13",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxQcelp13Dec.so",
-    {
-      "audio_decoder.Qcelp13"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.evrc",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxEvrcDec.so",
-    {
-      "audio_decoder.evrc"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.wma",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-     NULL,
-     NULL,
-     NULL,
-     NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxWmaDec.so",
-    {
-     "audio_decoder.wma"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.wma10Pro",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-     NULL,
-     NULL,
-     NULL,
-     NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxWmaDec.so",
-    {
-     "audio_decoder.wma"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.wmaLossLess",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-     NULL,
-     NULL,
-     NULL,
-     NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxWmaDec.so",
-    {
-     "audio_decoder.wma"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.amrwbplus",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-     NULL,
-     NULL,
-     NULL,
-     NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxAmrwbplusDec.so",
-    {
-     "audio_decoder.awbplus"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.alac",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxAlacDec.so",
-    {
-      "audio_decoder.alac"
-    }
-  },
-  {
-    "OMX.qti.audio.decoder.alac.sw",
-    NULL,   // Create instance function
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxAlacDecSw.so",
-    {
-      "audio_decoder.alac"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.ape",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxApeDec.so",
-    {
-      "audio_decoder.ape"
-    }
-  },
-  {
-    "OMX.qti.audio.decoder.ape.sw",
-    NULL,   // Create instance function
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxApeDecSw.so",
-    {
-      "audio_decoder.ape"
-    }
-  },
-  {
-    "OMX.qti.audio.decoder.dsd",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxDsdDec.so",
-    {
-      "audio_decoder.dsd"
-    }
-  },
-  {
-    "OMX.qcom.audio.encoder.aac",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,  // Shared object library handle
-    "libOmxAacEnc.so",
-    {
-      "audio_encoder.aac"
-    }
-  },
-  {
-    "OMX.qcom.audio.encoder.qcelp13",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxQcelp13Enc.so",
-    {
-      "audio_encoder.qcelp13"
-    }
-  },
-  {
-    "OMX.qcom.audio.encoder.evrc",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxEvrcEnc.so",
-    {
-      "audio_encoder.evrc"
-    }
-  },
-  {
-    "OMX.qcom.audio.encoder.amrnb",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxAmrEnc.so",
-    {
-      "audio_encoder.amrnb"
-    }
-  },
-  {
-    "OMX.qti.vdec.vpp",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxVpp.so",
-    {
-      "iv_processor.yuv"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.multiaac",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxAacDec.so",
-    {
-      "audio_decoder.aac"
-    }
-  },
-  {
-    "OMX.QCOM.CUST.COMP.START",
-    NULL, //Create instance function
-    //Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,
-    NULL,   // Shared object library handle
-    {
-      NULL
-    }
-  },
-  {
-    "OMX.qcom.file.muxer",
-    NULL, // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxMux.so",
-    {
-      "container_muxer.mp2"
-    }
-  },
-  {
-    "OMX.qcom.audio.decoder.aac",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxAacDec.so",
-    {
-      "audio_decoder.aac"
-    }
-  },
-  {
-    "OMX.qcom.video.decoder.avc.secure.dsmode",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxVideoDSMode.so",
-    {
-      "video_decoder.avc"
-    }
-  },
-  {
-    "OMX.qcom.video.decoder.avc.dsmode",
-    NULL, // Create instance function
-         // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxVideoDSMode.so",
-    {
-      "video_decoder.avc"
-    }
-  },
-  {
-    "OMX.qcom.video.encoder.hevc.secure",
-    NULL,   // Create instance function
-    // Unique instance handle
-    {
-      NULL,
-      NULL,
-      NULL,
-      NULL
-    },
-    NULL,   // Shared object library handle
-    "libOmxVenc.so",
-    {
-      "video_encoder.hevc"
-    }
-  }
 };
 
 const unsigned int SIZE_OF_CORE = sizeof(core) / sizeof(omx_core_cb_type);
diff --git a/mm-video-v4l2/vidc/vdec/inc/omx_vdec.h b/mm-video-v4l2/vidc/vdec/inc/omx_vdec.h
index 876119a..40937e9 100644
--- a/mm-video-v4l2/vidc/vdec/inc/omx_vdec.h
+++ b/mm-video-v4l2/vidc/vdec/inc/omx_vdec.h
@@ -1359,6 +1359,7 @@
 public:
         bool is_down_scalar_enabled;
         bool m_is_split_mode;
+        bool m_buffer_error;
 };
 
 enum instance_state {
diff --git a/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp b/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
index 8e92cfe..b93af8b 100644
--- a/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
+++ b/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
@@ -729,7 +729,8 @@
     secure_scaling_to_non_secure_opb(false),
     m_force_compressed_for_dpb(true),
     m_is_display_session(false),
-    m_is_split_mode(false)
+    m_is_split_mode(false),
+    m_buffer_error(false)
 {
     m_poll_efd = -1;
     drv_ctx.video_driver_fd = -1;
@@ -5565,6 +5566,7 @@
         eRet = allocate_output_headers();
         if (eRet == OMX_ErrorNone)
             eRet = allocate_extradata();
+        output_use_buffer = true;
     }
 
     if (eRet == OMX_ErrorNone) {
@@ -6029,7 +6031,6 @@
     index = bufferHdr - m_inp_mem_ptr;
     DEBUG_PRINT_LOW("Free Input Buffer index = %d",index);
 
-    auto_lock l(buf_lock);
     bufferHdr->pInputPortPrivate = NULL;
 
     if (index < drv_ctx.ip_buf.actualcount && drv_ctx.ptr_inputbuffer) {
@@ -6158,6 +6159,7 @@
     unsigned   i = 0;
     unsigned char *buf_addr = NULL;
     int pmem_fd = -1, ret = 0;
+    unsigned int align_size = 0;
 
     (void) hComp;
     (void) port;
@@ -6235,8 +6237,10 @@
         int rc;
         DEBUG_PRINT_LOW("Allocate input Buffer");
 #ifdef USE_ION
+        align_size = drv_ctx.ip_buf.buffer_size + 512;
+        align_size = (align_size + drv_ctx.ip_buf.alignment - 1)&(~(drv_ctx.ip_buf.alignment - 1));
         drv_ctx.ip_buf_ion_info[i].ion_device_fd = alloc_map_ion_memory(
-                drv_ctx.ip_buf.buffer_size,drv_ctx.op_buf.alignment,
+                align_size, drv_ctx.op_buf.alignment,
                 &drv_ctx.ip_buf_ion_info[i].ion_alloc_data,
                 &drv_ctx.ip_buf_ion_info[i].fd_ion_data, secure_mode ?
                 SECURE_FLAGS_INPUT_BUFFER : 0);
@@ -6643,6 +6647,10 @@
         }
         eRet = allocate_input_buffer(hComp,bufferHdr,port,appData,bytes);
     } else if (port == OMX_CORE_OUTPUT_PORT_INDEX) {
+        if (output_use_buffer) {
+            DEBUG_PRINT_ERROR("Allocate output buffer not allowed after use buffer");
+            return OMX_ErrorBadParameter;
+        }
         eRet = client_buffers.allocate_buffers_color_convert(hComp,bufferHdr,port,
                 appData,bytes);
     } else {
@@ -6701,6 +6709,7 @@
     unsigned int nPortIndex;
     (void) hComp;
 
+    auto_lock l(buf_lock);
     if (m_state == OMX_StateIdle &&
             (BITMASK_PRESENT(&m_flags ,OMX_COMPONENT_LOADING_PENDING))) {
         DEBUG_PRINT_LOW(" free buffer while Component in Loading pending");
@@ -6717,7 +6726,7 @@
         post_event(OMX_EventError,
                 OMX_ErrorPortUnpopulated,
                 OMX_COMPONENT_GENERATE_EVENT);
-
+        m_buffer_error = true;
         return OMX_ErrorIncorrectStateOperation;
     } else if (m_state != OMX_StateInvalid) {
         DEBUG_PRINT_ERROR("Invalid state to free buffer,port lost Buffers");
@@ -6825,6 +6834,7 @@
             BITMASK_CLEAR((&m_flags),OMX_COMPONENT_LOADING_PENDING);
             post_event(OMX_CommandStateSet, OMX_StateLoaded,
                     OMX_COMPONENT_GENERATE_EVENT);
+            m_buffer_error = false;
         }
     }
     return eRet;
@@ -6995,6 +7005,11 @@
         return OMX_ErrorBadParameter;
     }
 
+    if (BITMASK_ABSENT(&m_inp_bm_count, nPortIndex) || m_buffer_error) {
+        DEBUG_PRINT_ERROR("ETBProxy: ERROR: invalid buffer, nPortIndex %u", nPortIndex);
+        return OMX_ErrorBadParameter;
+    }
+
     VIDC_TRACE_INT_LOW("ETB-TS", buffer->nTimeStamp / 1000);
     VIDC_TRACE_INT_LOW("ETB-size", buffer->nFilledLen);
     /*for use buffer we need to memcpy the data*/
@@ -7276,6 +7291,7 @@
     struct vdec_bufferpayload     *ptr_outputbuffer = NULL;
     struct vdec_output_frameinfo  *ptr_respbuffer = NULL;
 
+    auto_lock l(buf_lock);
     nPortIndex = buffer-((OMX_BUFFERHEADERTYPE *)client_buffers.get_il_buf_hdr());
 
     if (bufferAdd == NULL || nPortIndex >= drv_ctx.op_buf.actualcount) {
@@ -7284,6 +7300,11 @@
         return OMX_ErrorBadParameter;
     }
 
+    if (BITMASK_ABSENT(&m_out_bm_count, nPortIndex) || m_buffer_error) {
+        DEBUG_PRINT_ERROR("FTBProxy: ERROR: invalid buffer, nPortIndex %u", nPortIndex);
+        return OMX_ErrorBadParameter;
+    }
+
     /*Return back the output buffer to client*/
     if (m_out_bEnabled != OMX_TRUE || output_flush_progress == true || in_reconfig) {
         DEBUG_PRINT_LOW("Output Buffers return flush/disable condition");
@@ -8537,7 +8558,7 @@
                    if (omxhdr->nFilledLen)
                        omx->prev_n_filled_len = omxhdr->nFilledLen;
 
-                   if (omx->output_use_buffer && omxhdr->pBuffer &&
+                   if (!omx->m_enable_android_native_buffers && omx->output_use_buffer && omxhdr->pBuffer &&
                        vdec_msg->msgdata.output_frame.bufferaddr)
                        memcpy ( omxhdr->pBuffer, (void *)
                                ((unsigned long)vdec_msg->msgdata.output_frame.bufferaddr +
diff --git a/mm-video-v4l2/vidc/venc/inc/video_encoder_device_v4l2.h b/mm-video-v4l2/vidc/venc/inc/video_encoder_device_v4l2.h
index 1d2fc5a..0c7ee4f 100644
--- a/mm-video-v4l2/vidc/venc/inc/video_encoder_device_v4l2.h
+++ b/mm-video-v4l2/vidc/venc/inc/video_encoder_device_v4l2.h
@@ -406,6 +406,7 @@
 #endif
         };
 
+        int nPframes_cache;
         int stopped;
         int resume_in_stopped;
         bool m_max_allowed_bitrate_check;
@@ -591,6 +592,8 @@
         BatchInfo mBatchInfo;
         bool mUseAVTimerTimestamps;
         bool venc_set_hdr_info(const MasteringDisplay&, const ContentLightLevel&);
+        bool mIsGridset;
+        OMX_U32 mTileDimension;
 };
 
 enum instance_state {
diff --git a/mm-video-v4l2/vidc/venc/src/omx_video_base.cpp b/mm-video-v4l2/vidc/venc/src/omx_video_base.cpp
index 016f5cb..7fe1c97 100644
--- a/mm-video-v4l2/vidc/venc/src/omx_video_base.cpp
+++ b/mm-video-v4l2/vidc/venc/src/omx_video_base.cpp
@@ -1586,7 +1586,7 @@
                     // At the same time, FWK needs original WxH for various purposes
                     // Sending input WxH as output port def WxH to FWK
                     if (m_sOutPortDef.format.video.eCompressionFormat ==
-                            OMX_VIDEO_CodingHEIC) {
+                            OMX_VIDEO_CodingImageHEIC) {
                         portDefn->format.video.nFrameWidth =
                             m_sInPortDef.format.video.nFrameWidth;
                         portDefn->format.video.nFrameHeight =
diff --git a/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp b/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp
index 8181347..cd85448 100644
--- a/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp
+++ b/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp
@@ -127,7 +127,7 @@
     } else if (!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.heic",    \
                 OMX_MAX_STRINGNAME_SIZE)) {
         strlcpy((char *)m_cRole, "video_encoder.hevc", OMX_MAX_STRINGNAME_SIZE);
-        codec_type = OMX_VIDEO_CodingHEIC;
+        codec_type = OMX_VIDEO_CodingImageHEIC;
     } else if (!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.hevc.secure",    \
                 OMX_MAX_STRINGNAME_SIZE)) {
         strlcpy((char *)m_cRole, "video_encoder.hevc", OMX_MAX_STRINGNAME_SIZE);
@@ -268,7 +268,7 @@
     } else if (codec_type == OMX_VIDEO_CodingHEVC) {
         m_sParamProfileLevel.eProfile = (OMX_U32) OMX_VIDEO_HEVCProfileMain;
         m_sParamProfileLevel.eLevel = (OMX_U32) OMX_VIDEO_HEVCMainTierLevel1;
-    } else if (codec_type == OMX_VIDEO_CodingHEIC) {
+    } else if (codec_type == OMX_VIDEO_CodingImageHEIC) {
         m_sParamProfileLevel.eProfile = (OMX_U32) OMX_VIDEO_HEVCProfileMainStill;
         m_sParamProfileLevel.eLevel = (OMX_U32) OMX_VIDEO_HEVCMainTierLevel1;
     }
@@ -320,8 +320,8 @@
         m_sOutPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingVP8;
     } else if (codec_type == OMX_VIDEO_CodingHEVC) {
         m_sOutPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingHEVC;
-    } else if (codec_type == OMX_VIDEO_CodingHEIC) {
-        m_sOutPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingHEIC;
+    } else if (codec_type == OMX_VIDEO_CodingImageHEIC) {
+        m_sOutPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingImageHEIC;
     } else if (codec_type == (OMX_VIDEO_CODINGTYPE)QOMX_VIDEO_CodingTME) {
         m_sOutPortDef.format.video.eCompressionFormat =  (OMX_VIDEO_CODINGTYPE)QOMX_VIDEO_CodingTME;
     }
@@ -353,8 +353,8 @@
         m_sOutPortFormat.eCompressionFormat =  OMX_VIDEO_CodingVP8;
     } else if (codec_type == OMX_VIDEO_CodingHEVC) {
         m_sOutPortFormat.eCompressionFormat =  OMX_VIDEO_CodingHEVC;
-    } else if (codec_type == OMX_VIDEO_CodingHEIC) {
-        m_sOutPortFormat.eCompressionFormat =  OMX_VIDEO_CodingHEIC;
+    } else if (codec_type == OMX_VIDEO_CodingImageHEIC) {
+        m_sOutPortFormat.eCompressionFormat =  OMX_VIDEO_CodingImageHEIC;
     } else if (codec_type == (OMX_VIDEO_CODINGTYPE)QOMX_VIDEO_CodingTME) {
         m_sOutPortFormat.eCompressionFormat =  (OMX_VIDEO_CODINGTYPE)QOMX_VIDEO_CodingTME;
     }
diff --git a/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp b/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp
index e798d4f..0f15a1e 100644
--- a/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp
+++ b/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp
@@ -191,6 +191,8 @@
          strlcpy(m_debug.log_loc, property_value, PROPERTY_VALUE_MAX);
 
     mUseAVTimerTimestamps = false;
+    mIsGridset = false;
+    mTileDimension = 0;
 
     profile_level_converter::init();
 }
@@ -1409,12 +1411,12 @@
         profile_level.level = V4L2_MPEG_VIDC_VIDEO_VP8_VERSION_0;
         minqp = 0;
         maxqp = 127;
-    } else if (codec == OMX_VIDEO_CodingHEVC || codec == OMX_VIDEO_CodingHEIC) {
+    } else if (codec == OMX_VIDEO_CodingHEVC || codec == OMX_VIDEO_CodingImageHEIC) {
         m_sVenc_cfg.codectype = V4L2_PIX_FMT_HEVC;
         idrperiod.idrperiod = 1;
         minqp = 0;
         maxqp = 51;
-        if (codec == OMX_VIDEO_CodingHEIC)
+        if (codec == OMX_VIDEO_CodingImageHEIC)
             codec_profile.profile = V4L2_MPEG_VIDC_VIDEO_HEVC_PROFILE_MAIN_STILL_PIC;
         else
             codec_profile.profile = V4L2_MPEG_VIDC_VIDEO_HEVC_PROFILE_MAIN;
@@ -2013,13 +2015,6 @@
                 } else if (portDefn->nPortIndex == PORT_INDEX_OUT) {
                     unsigned long codectype = venc_get_codectype(portDefn->format.video.eCompressionFormat);
 
-                    if (portDefn->format.video.eCompressionFormat == OMX_VIDEO_CodingHEIC) {
-                        portDefn->format.video.nFrameWidth = DEFAULT_TILE_DIMENSION;
-                        portDefn->format.video.nFrameHeight = DEFAULT_TILE_DIMENSION;
-                        DEBUG_PRINT_HIGH("set_parameter: OMX_IndexParamPortDefinition: port %d, wxh (for HEIC coding type) %dx%d",
-                            portDefn->nPortIndex, portDefn->format.video.nFrameWidth,
-                            portDefn->format.video.nFrameHeight);
-                    }
                     //Don't worry about width/height if downscalar is enabled.
                     if (((m_sVenc_cfg.dvs_height != portDefn->format.video.nFrameHeight ||
                             m_sVenc_cfg.dvs_width != portDefn->format.video.nFrameWidth) && !downscalar_enabled) ||
@@ -2115,13 +2110,13 @@
                 DEBUG_PRINT_LOW("venc_set_param: OMX_IndexParamVideoBitrate");
 
                 if (pParam->nPortIndex == (OMX_U32) PORT_INDEX_OUT) {
-                    if (!venc_set_target_bitrate(pParam->nTargetBitrate)) {
-                        DEBUG_PRINT_ERROR("ERROR: Setting Target Bit Rate / Quality Factor failed");
+                    if (!venc_set_ratectrl_cfg(pParam->eControlRate)) {
+                        DEBUG_PRINT_ERROR("ERROR: Rate Control setting failed");
                         return false;
                     }
 
-                    if (!venc_set_ratectrl_cfg(pParam->eControlRate)) {
-                        DEBUG_PRINT_ERROR("ERROR: Rate Control setting failed");
+                    if (!venc_set_target_bitrate(pParam->nTargetBitrate)) {
+                        DEBUG_PRINT_ERROR("ERROR: Setting Target Bit Rate / Quality Factor failed");
                         return false;
                     }
                 } else {
@@ -2258,9 +2253,9 @@
                 OMX_VIDEO_PARAM_ANDROID_IMAGEGRIDTYPE* pParam =
                        (OMX_VIDEO_PARAM_ANDROID_IMAGEGRIDTYPE*)paramData;
 
-                if (m_codec != OMX_VIDEO_CodingHEIC) {
-                    DEBUG_PRINT_ERROR("OMX_IndexParamVideoAndroidImageGrid is only supported for HEIC");
-                    return false;
+                if (m_codec != OMX_VIDEO_CodingImageHEIC) {
+                    DEBUG_PRINT_ERROR("OMX_IndexParamVideoAndroidImageGrid is only set for HEIC Encode (HW tiling)");
+                    return true;
                 }
 
                 if (!venc_set_tile_dimension(pParam->nTileWidth)) {
@@ -3446,6 +3441,27 @@
         return 1;
     }
 
+    if(m_codec == OMX_VIDEO_CodingImageHEIC && mIsGridset) {
+        struct v4l2_format fmt;
+        memset(&fmt, 0, sizeof(fmt));
+        fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+        fmt.fmt.pix_mp.height = mTileDimension;
+        fmt.fmt.pix_mp.width = mTileDimension;
+        fmt.fmt.pix_mp.pixelformat = m_sVenc_cfg.codectype;
+        DEBUG_PRINT_INFO("set format type %d, wxh %dx%d, pixelformat %#x",
+                 fmt.type, fmt.fmt.pix_mp.width, fmt.fmt.pix_mp.height,
+                 fmt.fmt.pix_mp.pixelformat);
+        if (ioctl(m_nDriver_fd, VIDIOC_S_FMT, &fmt)) {
+            DEBUG_PRINT_ERROR("set format failed, type %d, wxh %dx%d, pixelformat %#x",
+                 fmt.type, fmt.fmt.pix_mp.width, fmt.fmt.pix_mp.height,
+                 fmt.fmt.pix_mp.pixelformat);
+            hw_overload = errno == EBUSY;
+            return false;
+        }
+
+
+    }
+
     buf_type=V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
     DEBUG_PRINT_LOW("send_command_proxy(): Idle-->Executing");
     ret=ioctl(m_nDriver_fd, VIDIOC_STREAMON,&buf_type);
@@ -4161,6 +4177,7 @@
          m_sVenc_cfg.inputformat != V4L2_PIX_FMT_NV12_UBWC)) {
         if (bframe_implicitly_enabled) {
             DEBUG_PRINT_HIGH("Disabling implicitly enabled B-frames");
+            intra_period.num_pframes = nPframes_cache;
             if (!_venc_set_intra_period(intra_period.num_pframes, 0)) {
                 DEBUG_PRINT_ERROR("Failed to set nPframes/nBframes");
                 return OMX_ErrorUndefined;
@@ -4253,7 +4270,7 @@
             }
 
             // Tiling in HEIC requires output WxH to be Tile size; difference is permitted
-            if (!(m_codec == OMX_VIDEO_CodingHEIC) &&
+            if (!(m_codec == OMX_VIDEO_CodingImageHEIC) &&
                 inp_width * inp_height != out_width * out_height) {
                 DEBUG_PRINT_ERROR("Downscalar is disabled and input/output dimenstions don't match");
                 DEBUG_PRINT_ERROR("Input WxH : %dx%d Output WxH : %dx%d",inp_width, inp_height, out_width, out_height);
@@ -5034,7 +5051,7 @@
         ((codec_profile.profile == V4L2_MPEG_VIDC_VIDEO_HEVC_PROFILE_MAIN) ||
          (codec_profile.profile == V4L2_MPEG_VIDC_VIDEO_HEVC_PROFILE_MAIN10) ||
          (codec_profile.profile == V4L2_MPEG_VIDC_VIDEO_HEVC_PROFILE_MAIN_STILL_PIC)) &&
-         (m_codec != OMX_VIDEO_CodingHEIC)) {
+         (m_codec != OMX_VIDEO_CodingImageHEIC)) {
         isValidCodec = true;
     }
 
@@ -5081,6 +5098,7 @@
 
     if (enableBframes && intra_period.num_bframes == 0) {
         intra_period.num_bframes = VENC_BFRAME_MAX_COUNT;
+        nPframes_cache = intra_period.num_pframes;
         intra_period.num_pframes = intra_period.num_pframes / (1 + intra_period.num_bframes);
         bframe_implicitly_enabled = true;
     } else if (!enableBframes && intra_period.num_bframes > 0) {
@@ -5150,6 +5168,8 @@
     }
 
     DEBUG_PRINT_LOW("Success IOCTL set control for id=%d, value=%d", control.id, control.value);
+    mIsGridset = true;
+    mTileDimension = nTileDimension;
 
     return true;
 }
@@ -5173,7 +5193,7 @@
 
     if (m_sVenc_cfg.codectype != V4L2_PIX_FMT_H264 &&
         m_sVenc_cfg.codectype != V4L2_PIX_FMT_HEVC &&
-        m_codec == OMX_VIDEO_CodingHEIC) {
+        m_codec == OMX_VIDEO_CodingImageHEIC) {
         nBFrames = 0;
     }
 
@@ -5636,7 +5656,7 @@
     case OMX_VIDEO_CodingVP9:
         codectype = V4L2_PIX_FMT_VP9;
         break;
-    case OMX_VIDEO_CodingHEIC:
+    case OMX_VIDEO_CodingImageHEIC:
     case OMX_VIDEO_CodingHEVC:
         codectype = V4L2_PIX_FMT_HEVC;
         break;